interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / kernel / core / ipc.om

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