omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / kernel / core / ipc.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/kernel/core/ipc.om: Pure-Omega Aienux Kernel Capability-Gated IPC Channels |
| 3 | note and Kernel-Owned Object Authority with Tree Revocation. |
| 4 | note Converted from upstream aienux crates/aienos-kernel/src/ipc.rs and caps.rs. |
| 5 | |
| 6 | const IPC_OK int 0 |
| 7 | const IPC_ERR_CH_FULL int -1 |
| 8 | const IPC_ERR_CH_EMPTY int -2 |
| 9 | const IPC_ERR_MISSING_RIGHTS int -3 |
| 10 | const IPC_ERR_INVALID_HANDLE int -4 |
| 11 | const IPC_ERR_WRONG_RESOURCE int -5 |
| 12 | const IPC_ERR_DELEG_FAILED int -6 |
| 13 | |
| 14 | const IPC_RIGHT_READ uint 0x1 |
| 15 | const IPC_RIGHT_WRITE uint 0x2 |
| 16 | const IPC_RIGHT_DERIVE uint 0x4 |
| 17 | const IPC_RIGHT_REVOKE uint 0x8 |
| 18 | |
| 19 | const IPC_RES_CHANNEL uint 0x49504348 |
| 20 | const IPC_RES_OBJECT uint 0x49504f42 |
| 21 | |
| 22 | const IPC_PRINCIPAL_A uint 0 |
| 23 | const IPC_PRINCIPAL_B uint 1 |
| 24 | const IPC_PRINCIPALS ulong 2 |
| 25 | const IPC_TAB_CAP ulong 8 |
| 26 | const IPC_TOTAL_CAPS ulong 16 |
| 27 | const IPC_CH_CAP ulong 8 |
| 28 | const IPC_OBJ_SLOTS ulong 4 |
| 29 | const IPC_PARENT_NONE uint 0xffffffff |
| 30 | |
| 31 | note Channel 0 FIFO ring (8 messages) |
| 32 | note ch_meta: 0=head, 1=len |
| 33 | global ipc_ch_meta ulong 4 |
| 34 | global ipc_ch_tag uint 8 |
| 35 | global ipc_ch_sender uint 8 |
| 36 | global ipc_ch_arg0 ulong 8 |
| 37 | global ipc_ch_arg1 ulong 8 |
| 38 | |
| 39 | note Kernel-owned object (4 u64 words) |
| 40 | global ipc_object ulong 4 |
| 41 | |
| 42 | note Per-principal capability tables (2 principals * 8 slots = 16 slots) |
| 43 | note Global slot index = principal * 8 + local_slot |
| 44 | global ipc_cap_live uchar 16 |
| 45 | global ipc_cap_gen ulong 16 |
| 46 | global ipc_cap_res uint 16 |
| 47 | global ipc_cap_rgt uint 16 |
| 48 | global ipc_cap_par uint 16 |
| 49 | global ipc_cap_pgen ulong 16 |
| 50 | global ipc_next_gen ulong 2 |
| 51 | |
| 52 | fn ipc_reset h_send edit ulong 1 h_obj edit ulong 1 h_recv edit ulong 1 to void |
| 53 | put ipc_ch_meta 0 0 |
| 54 | put ipc_ch_meta 1 0 |
| 55 | put ipc_object 0 0xc0ffee01 |
| 56 | put ipc_object 1 0x0badc0de |
| 57 | put ipc_object 2 0x12345678 |
| 58 | put ipc_object 3 0x9abcdef0 |
| 59 | for i ulong 16 |
| 60 | put ipc_cap_live i 0 |
| 61 | put ipc_cap_gen i 0 |
| 62 | put ipc_cap_res i 0 |
| 63 | put ipc_cap_rgt i 0 |
| 64 | put ipc_cap_par i IPC_PARENT_NONE |
| 65 | put ipc_cap_pgen i 0 |
| 66 | end |
| 67 | g = add get ipc_next_gen 0 1 |
| 68 | if eq g 0 |
| 69 | g = ulong 1 |
| 70 | end |
| 71 | note Principal A slot 0: CHANNEL WRITE |
| 72 | put ipc_cap_live 0 1 |
| 73 | put ipc_cap_gen 0 g |
| 74 | put ipc_cap_res 0 IPC_RES_CHANNEL |
| 75 | put ipc_cap_rgt 0 IPC_RIGHT_WRITE |
| 76 | put h_send 0 or shl g 8 0 |
| 77 | g = add g 1 |
| 78 | note Principal A slot 1: OBJECT READ|WRITE|DERIVE|REVOKE |
| 79 | put ipc_cap_live 1 1 |
| 80 | put ipc_cap_gen 1 g |
| 81 | put ipc_cap_res 1 IPC_RES_OBJECT |
| 82 | put ipc_cap_rgt 1 or or IPC_RIGHT_READ IPC_RIGHT_WRITE or IPC_RIGHT_DERIVE IPC_RIGHT_REVOKE |
| 83 | put h_obj 0 or shl g 8 1 |
| 84 | g = add g 1 |
| 85 | note Principal B slot 0 (global slot 8): CHANNEL READ |
| 86 | put ipc_cap_live 8 1 |
| 87 | put ipc_cap_gen 8 g |
| 88 | put ipc_cap_res 8 IPC_RES_CHANNEL |
| 89 | put ipc_cap_rgt 8 IPC_RIGHT_READ |
| 90 | put h_recv 0 or shl g 8 0 |
| 91 | put ipc_next_gen 0 add g 1 |
| 92 | end |
| 93 | |
| 94 | fn ipc_handle_slot h ulong to ulong |
| 95 | ret and h 0xff |
| 96 | end |
| 97 | |
| 98 | fn ipc_handle_gen h ulong to ulong |
| 99 | ret shr h 8 |
| 100 | end |
| 101 | |
| 102 | fn ipc_gated princ uint h ulong need uint want_res uint to int |
| 103 | if ge ulong princ IPC_PRINCIPALS |
| 104 | ret IPC_ERR_INVALID_HANDLE |
| 105 | end |
| 106 | lslot = ipc_handle_slot h |
| 107 | if ge lslot IPC_TAB_CAP |
| 108 | ret IPC_ERR_INVALID_HANDLE |
| 109 | end |
| 110 | gslot = add mul ulong princ IPC_TAB_CAP lslot |
| 111 | if eq get ipc_cap_live gslot 0 |
| 112 | ret IPC_ERR_INVALID_HANDLE |
| 113 | end |
| 114 | if ne get ipc_cap_gen gslot ipc_handle_gen h |
| 115 | ret IPC_ERR_INVALID_HANDLE |
| 116 | end |
| 117 | if ne and get ipc_cap_rgt gslot need need |
| 118 | ret IPC_ERR_MISSING_RIGHTS |
| 119 | end |
| 120 | if ne get ipc_cap_res gslot want_res |
| 121 | ret IPC_ERR_WRONG_RESOURCE |
| 122 | end |
| 123 | ret IPC_OK |
| 124 | end |
| 125 | |
| 126 | fn ipc_channel_send princ uint h ulong msg view IpcMsg to int |
| 127 | rc = ipc_gated princ h IPC_RIGHT_WRITE IPC_RES_CHANNEL |
| 128 | if ne rc IPC_OK |
| 129 | ret rc |
| 130 | end |
| 131 | clen = get ipc_ch_meta 1 |
| 132 | if eq clen IPC_CH_CAP |
| 133 | ret IPC_ERR_CH_FULL |
| 134 | end |
| 135 | head = get ipc_ch_meta 0 |
| 136 | tail = rem add head clen IPC_CH_CAP |
| 137 | put ipc_ch_tag tail get msg tag |
| 138 | put ipc_ch_sender tail get msg sender |
| 139 | put ipc_ch_arg0 tail get msg arg0 |
| 140 | put ipc_ch_arg1 tail get msg arg1 |
| 141 | put ipc_ch_meta 1 add clen 1 |
| 142 | ret IPC_OK |
| 143 | end |
| 144 | |
| 145 | fn ipc_channel_recv princ uint h ulong dst edit IpcMsg to int |
| 146 | rc = ipc_gated princ h IPC_RIGHT_READ IPC_RES_CHANNEL |
| 147 | if ne rc IPC_OK |
| 148 | ret rc |
| 149 | end |
| 150 | clen = get ipc_ch_meta 1 |
| 151 | if eq clen 0 |
| 152 | ret IPC_ERR_CH_EMPTY |
| 153 | end |
| 154 | head = get ipc_ch_meta 0 |
| 155 | put dst tag get ipc_ch_tag head |
| 156 | put dst sender get ipc_ch_sender head |
| 157 | put dst arg0 get ipc_ch_arg0 head |
| 158 | put dst arg1 get ipc_ch_arg1 head |
| 159 | put ipc_ch_meta 0 rem add head 1 IPC_CH_CAP |
| 160 | put ipc_ch_meta 1 sub clen 1 |
| 161 | ret IPC_OK |
| 162 | end |
| 163 | |
| 164 | fn ipc_object_read princ uint h ulong off ulong val_out edit ulong 1 to int |
| 165 | rc = ipc_gated princ h IPC_RIGHT_READ IPC_RES_OBJECT |
| 166 | if ne rc IPC_OK |
| 167 | ret rc |
| 168 | end |
| 169 | if ge off IPC_OBJ_SLOTS |
| 170 | ret IPC_ERR_WRONG_RESOURCE |
| 171 | end |
| 172 | put val_out 0 get ipc_object off |
| 173 | ret IPC_OK |
| 174 | end |
| 175 | |
| 176 | fn ipc_object_write princ uint h ulong off ulong val ulong to int |
| 177 | rc = ipc_gated princ h IPC_RIGHT_WRITE IPC_RES_OBJECT |
| 178 | if ne rc IPC_OK |
| 179 | ret rc |
| 180 | end |
| 181 | if ge off IPC_OBJ_SLOTS |
| 182 | ret IPC_ERR_WRONG_RESOURCE |
| 183 | end |
| 184 | put ipc_object off val |
| 185 | ret IPC_OK |
| 186 | end |
| 187 | |
| 188 | note Delegate an object capability from src_p to dst_p with attenuated rights. |
| 189 | note Refuses rights outside READ|WRITE or rights not held by source; links child to parent for tree revocation. |
| 190 | fn ipc_delegate_object src_p uint h ulong dst_p uint new_rgt uint h_out edit ulong 1 to int |
| 191 | if or ge ulong src_p IPC_PRINCIPALS ge ulong dst_p IPC_PRINCIPALS |
| 192 | ret IPC_ERR_INVALID_HANDLE |
| 193 | end |
| 194 | if eq src_p dst_p |
| 195 | ret IPC_ERR_INVALID_HANDLE |
| 196 | end |
| 197 | rc = ipc_gated src_p h or IPC_RIGHT_READ IPC_RIGHT_DERIVE IPC_RES_OBJECT |
| 198 | if ne rc IPC_OK |
| 199 | ret rc |
| 200 | end |
| 201 | rw_mask = or IPC_RIGHT_READ IPC_RIGHT_WRITE |
| 202 | if ne and new_rgt xor rw_mask 0xffffffff 0 |
| 203 | ret IPC_ERR_MISSING_RIGHTS |
| 204 | end |
| 205 | src_gslot = add mul ulong src_p IPC_TAB_CAP ipc_handle_slot h |
| 206 | src_rgt = get ipc_cap_rgt src_gslot |
| 207 | if ne and new_rgt xor src_rgt 0xffffffff 0 |
| 208 | ret IPC_ERR_MISSING_RIGHTS |
| 209 | end |
| 210 | base = mul ulong dst_p IPC_TAB_CAP |
| 211 | free_lslot = IPC_TAB_CAP |
| 212 | for i ulong 8 |
| 213 | if eq get ipc_cap_live add base i 0 |
| 214 | free_lslot = i |
| 215 | break |
| 216 | end |
| 217 | end |
| 218 | if ge free_lslot IPC_TAB_CAP |
| 219 | ret IPC_ERR_DELEG_FAILED |
| 220 | end |
| 221 | dst_gslot = add base free_lslot |
| 222 | g = get ipc_next_gen 0 |
| 223 | put ipc_next_gen 0 add g 1 |
| 224 | put ipc_cap_live dst_gslot 1 |
| 225 | put ipc_cap_gen dst_gslot g |
| 226 | put ipc_cap_res dst_gslot IPC_RES_OBJECT |
| 227 | put ipc_cap_rgt dst_gslot new_rgt |
| 228 | put ipc_cap_par dst_gslot uint src_gslot |
| 229 | put ipc_cap_pgen dst_gslot get ipc_cap_gen src_gslot |
| 230 | put h_out 0 or shl g 8 free_lslot |
| 231 | ret IPC_OK |
| 232 | end |
| 233 | |
| 234 | fn ipc_cascade_revoke to void |
| 235 | for pass ulong 16 |
| 236 | changed = false |
| 237 | for i ulong 16 |
| 238 | if ne get ipc_cap_live i 0 |
| 239 | par = get ipc_cap_par i |
| 240 | if ne par IPC_PARENT_NONE |
| 241 | pidx = ulong par |
| 242 | dead = eq get ipc_cap_live pidx 0 |
| 243 | if ne get ipc_cap_gen pidx get ipc_cap_pgen i |
| 244 | dead = true |
| 245 | end |
| 246 | if dead |
| 247 | put ipc_cap_live i 0 |
| 248 | changed = true |
| 249 | end |
| 250 | end |
| 251 | end |
| 252 | end |
| 253 | if not changed |
| 254 | break |
| 255 | end |
| 256 | end |
| 257 | end |
| 258 | |
| 259 | note Revoke a capability and cascade-revoke every derived descendant across all principal tables. |
| 260 | fn ipc_revoke princ uint h ulong to int |
| 261 | rc = ipc_gated princ h IPC_RIGHT_REVOKE IPC_RES_OBJECT |
| 262 | if ne rc IPC_OK |
| 263 | ret rc |
| 264 | end |
| 265 | root_gslot = add mul ulong princ IPC_TAB_CAP ipc_handle_slot h |
| 266 | put ipc_cap_live root_gslot 0 |
| 267 | ipc_cascade_revoke |
| 268 | ret IPC_OK |
| 269 | end |