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 / crates / aienos-capability-ffi / cap_ffi.om

394 lines · 9018 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-capability-ffi/cap_ffi.om: Pure-Omega 32-bit-generation
3note capability authority ABI (aienos_cap32_*), layout contract, and split Admin/View handles.
4note Converted from aienux crates/aienos-capability-ffi/src/lib.rs.
5note Compose with: si/directives/om2.om aienux/crates/aienos-capability-ffi/cap_ffi.om ...
6
7layout Cap32Ref 8
8field cap_id uint at 0 little
9field gen uint at 4 little
10end
11
12layout Cap32Ent 64
13field cap_id uint at 0 little
14field gen uint at 4 little
15field state uint at 8 little
16field issuer uint at 12 little
17field subject uint at 16 little
18field rights uint at 20 little
19field res ulong at 24 little
20field epoch ulong at 32 little
21field lease ulong at 40 little
22field par_id uint at 48 little
23field par_gen uint at 52 little
24field mnt_id uint at 56 little
25field mnt_gen uint at 60 little
26end
27
28layout Cap32Mnt 48
29field issuer uint at 0 little
30field subject uint at 4 little
31field res ulong at 8 little
32field rights uint at 16 little
33field lease ulong at 24 little
34field par_id uint at 32 little
35field par_gen uint at 36 little
36field auth_id uint at 40 little
37field auth_gen uint at 44 little
38end
39
40const CAP32_ABI_VERSION uint 1
41const CAP32_GEN_BITS uint 32
42const CAP32_REF_SIZE uint 8
43const CAP32_REF_GEN_OFF uint 4
44const CAP32_ENT_SIZE uint 64
45const CAP32_MNT_SIZE uint 48
46
47const CAP32_OK int 0
48const CAP32_ERR_STATE int -1
49const CAP32_ERR_NOT_FOUND int -2
50const CAP32_ERR_STALE_GEN int -3
51const CAP32_ERR_REVOKED int -4
52const CAP32_ERR_EXPIRED int -5
53const CAP32_ERR_SUBJECT int -6
54const CAP32_ERR_RESOURCE int -7
55const CAP32_ERR_RIGHTS int -8
56const CAP32_ERR_UNAUTH int -9
57const CAP32_ERR_EXHAUSTED int -10
58const CAP32_ERR_CAPACITY int -11
59
60const CAP32_STATE_FREE uint 0
61const CAP32_STATE_ACTIVE uint 1
62const CAP32_STATE_REVOKED uint 2
63
64const CAP32_RIGHT_READ uint 1
65const CAP32_RIGHT_WRITE uint 2
66const CAP32_RIGHT_ADMIN uint 0x80000000
67
68const CAP32_MAX_SLOTS ulong 32
69
70note Global authority table
71note meta: 0=active, 1=writer_alive, 2=boot_gen, 3=gen_floor, 4=clock, 5=epoch, 6=office_gen
72global cap32_meta ulong 7
73global cap32_s_gen uint 32
74global cap32_s_state uint 32
75global cap32_s_issuer uint 32
76global cap32_s_subj uint 32
77global cap32_s_rights uint 32
78global cap32_s_res ulong 32
79global cap32_s_epoch ulong 32
80global cap32_s_lease ulong 32
81global cap32_s_pid uint 32
82global cap32_s_pgen uint 32
83global cap32_s_mid uint 32
84global cap32_s_mgen uint 32
85
86note Report layout contract [REF_SIZE, REF_GEN_OFF, 4, ENT_SIZE] -> returns ABI version 1
87fn cap32_abi_layout words edit uint * to uint
88if lt len words 4
89ret 0
90end
91put words 0 CAP32_REF_SIZE
92put words 1 CAP32_REF_GEN_OFF
93put words 2 4
94put words 3 CAP32_ENT_SIZE
95ret CAP32_ABI_VERSION
96end
97
98fn cap32_gen_advance g uint dst edit uint * to int
99if eq g 0xffffffff
100ret CAP32_ERR_EXHAUSTED
101end
102put dst 0 add g 1
103ret CAP32_OK
104end
105
106fn cap32_bootstrap boot_gen uint to int
107if eq boot_gen 0
108ret CAP32_ERR_STATE
109end
110for i ulong CAP32_MAX_SLOTS
111put cap32_s_gen i boot_gen
112put cap32_s_state i CAP32_STATE_FREE
113put cap32_s_issuer i 0
114put cap32_s_subj i 0
115put cap32_s_rights i 0
116put cap32_s_res i 0
117put cap32_s_epoch i 0
118put cap32_s_lease i 0
119put cap32_s_pid i 0
120put cap32_s_pgen i 0
121put cap32_s_mid i 0
122put cap32_s_mgen i 0
123end
124note Slot 0 is the Office Authority capability
125put cap32_s_state 0 CAP32_STATE_ACTIVE
126put cap32_s_gen 0 boot_gen
127put cap32_s_issuer 0 0
128put cap32_s_subj 0 0
129put cap32_s_rights 0 CAP32_RIGHT_ADMIN
130put cap32_s_res 0 0
131put cap32_s_epoch 0 1
132put cap32_s_lease 0 0
133put cap32_meta 0 1
134put cap32_meta 1 1
135put cap32_meta 2 ulong boot_gen
136put cap32_meta 3 ulong boot_gen
137put cap32_meta 4 0
138put cap32_meta 5 1
139put cap32_meta 6 ulong boot_gen
140ret CAP32_OK
141end
142
143fn cap32_start to int
144ret cap32_bootstrap 1
145end
146
147fn cap32_office dst edit Cap32Ref to int
148if eq get cap32_meta 0 0
149ret CAP32_ERR_STATE
150end
151put dst cap_id 0
152put dst gen uint get cap32_meta 6
153ret CAP32_OK
154end
155
156fn cap32_check_admin auth_id uint auth_gen uint to int
157if eq get cap32_meta 0 0
158ret CAP32_ERR_STATE
159end
160if eq get cap32_meta 1 0
161ret CAP32_ERR_STATE
162end
163if ne auth_id 0
164ret CAP32_ERR_UNAUTH
165end
166if ne auth_gen uint get cap32_meta 6
167ret CAP32_ERR_STALE_GEN
168end
169if ne get cap32_s_state 0 CAP32_STATE_ACTIVE
170ret CAP32_ERR_REVOKED
171end
172ret CAP32_OK
173end
174
175fn cap32_mint req view Cap32Mnt dst edit Cap32Ref to int
176rc = cap32_check_admin get req auth_id get req auth_gen
177if ne rc CAP32_OK
178ret rc
179end
180slot = ulong CAP32_MAX_SLOTS
181for i ulong sub CAP32_MAX_SLOTS 1
182idx = add i 1
183if eq get cap32_s_state idx CAP32_STATE_FREE
184slot = idx
185break
186end
187end
188if eq slot CAP32_MAX_SLOTS
189ret CAP32_ERR_CAPACITY
190end
191g = get cap32_s_gen slot
192clk = get cap32_meta 4
193l_ticks = get req lease
194exp = ulong 0
195if gt l_ticks 0
196exp = add clk l_ticks
197end
198put cap32_s_state slot CAP32_STATE_ACTIVE
199put cap32_s_issuer slot get req issuer
200put cap32_s_subj slot get req subject
201put cap32_s_rights slot get req rights
202put cap32_s_res slot get req res
203put cap32_s_epoch slot get cap32_meta 5
204put cap32_s_lease slot exp
205put cap32_s_pid slot get req par_id
206put cap32_s_pgen slot get req par_gen
207put cap32_s_mid slot get req auth_id
208put cap32_s_mgen slot get req auth_gen
209put dst cap_id uint slot
210put dst gen g
211ret CAP32_OK
212end
213
214fn cap32_revoke auth view Cap32Ref tgt view Cap32Ref to int
215rc = cap32_check_admin get auth cap_id get auth gen
216if ne rc CAP32_OK
217ret rc
218end
219id = ulong get tgt cap_id
220if or eq id 0 ge id CAP32_MAX_SLOTS
221ret CAP32_ERR_NOT_FOUND
222end
223if ne get cap32_s_gen id get tgt gen
224ret CAP32_ERR_STALE_GEN
225end
226if ne get cap32_s_state id CAP32_STATE_ACTIVE
227ret CAP32_ERR_REVOKED
228end
229put cap32_s_state id CAP32_STATE_REVOKED
230ret CAP32_OK
231end
232
233fn cap32_reclaim auth view Cap32Ref cid uint to int
234rc = cap32_check_admin get auth cap_id get auth gen
235if ne rc CAP32_OK
236ret rc
237end
238id = ulong cid
239if or eq id 0 ge id CAP32_MAX_SLOTS
240ret CAP32_ERR_NOT_FOUND
241end
242if ne get cap32_s_state id CAP32_STATE_REVOKED
243ret CAP32_ERR_STATE
244end
245cur_g = get cap32_s_gen id
246nxt = new uint 1 0
247rc = cap32_gen_advance cur_g nxt
248if ne rc CAP32_OK
249ret rc
250end
251ng = get nxt 0
252put cap32_s_gen id ng
253put cap32_s_state id CAP32_STATE_FREE
254if gt ulong ng get cap32_meta 3
255put cap32_meta 3 ulong ng
256end
257ret CAP32_OK
258end
259
260fn cap32_advance_clock auth view Cap32Ref ticks ulong to int
261rc = cap32_check_admin get auth cap_id get auth gen
262if ne rc CAP32_OK
263ret rc
264end
265put cap32_meta 4 add get cap32_meta 4 ticks
266ret CAP32_OK
267end
268
269fn cap32_bump_epoch auth view Cap32Ref to int
270rc = cap32_check_admin get auth cap_id get auth gen
271if ne rc CAP32_OK
272ret rc
273end
274put cap32_meta 5 add get cap32_meta 5 1
275ret CAP32_OK
276end
277
278fn cap32_kill to int
279if eq get cap32_meta 0 0
280ret CAP32_ERR_STATE
281end
282put cap32_meta 1 0
283ret CAP32_OK
284end
285
286fn cap32_restart to int
287if eq get cap32_meta 0 0
288ret CAP32_ERR_STATE
289end
290flr = uint get cap32_meta 3
291nxt = new uint 1 0
292rc = cap32_gen_advance flr nxt
293if ne rc CAP32_OK
294ret rc
295end
296ret cap32_bootstrap get nxt 0
297end
298
299fn cap32_fill_ent id ulong dst edit Cap32Ent to void
300put dst cap_id uint id
301put dst gen get cap32_s_gen id
302put dst state get cap32_s_state id
303put dst issuer get cap32_s_issuer id
304put dst subject get cap32_s_subj id
305put dst rights get cap32_s_rights id
306put dst res get cap32_s_res id
307put dst epoch get cap32_s_epoch id
308put dst lease get cap32_s_lease id
309put dst par_id get cap32_s_pid id
310put dst par_gen get cap32_s_pgen id
311put dst mnt_id get cap32_s_mid id
312put dst mnt_gen get cap32_s_mgen id
313end
314
315fn cap32_validate ref view Cap32Ref subj uint res ulong rights uint dst edit Cap32Ent to int
316if eq get cap32_meta 0 0
317ret CAP32_ERR_STATE
318end
319id = ulong get ref cap_id
320if ge id CAP32_MAX_SLOTS
321ret CAP32_ERR_NOT_FOUND
322end
323if ne get cap32_s_gen id get ref gen
324ret CAP32_ERR_STALE_GEN
325end
326st = get cap32_s_state id
327if eq st CAP32_STATE_FREE
328ret CAP32_ERR_NOT_FOUND
329end
330if eq st CAP32_STATE_REVOKED
331ret CAP32_ERR_REVOKED
332end
333exp = get cap32_s_lease id
334if and gt exp 0 ge get cap32_meta 4 exp
335ret CAP32_ERR_EXPIRED
336end
337if ne get cap32_s_subj id subj
338ret CAP32_ERR_SUBJECT
339end
340if ne get cap32_s_res id res
341ret CAP32_ERR_RESOURCE
342end
343if ne and get cap32_s_rights id rights rights
344ret CAP32_ERR_RIGHTS
345end
346cap32_fill_ent id dst
347ret CAP32_OK
348end
349
350fn cap32_inspect ref view Cap32Ref dst edit Cap32Ent to int
351if eq get cap32_meta 0 0
352ret CAP32_ERR_STATE
353end
354id = ulong get ref cap_id
355if ge id CAP32_MAX_SLOTS
356ret CAP32_ERR_NOT_FOUND
357end
358if ne get cap32_s_gen id get ref gen
359ret CAP32_ERR_STALE_GEN
360end
361cap32_fill_ent id dst
362ret CAP32_OK
363end
364
365fn cap32_clock to ulong
366ret get cap32_meta 4
367end
368
369note View-side cognition attempts are unconditionally unauthorized
370fn cap32_cognition_mint req view Cap32Mnt to int
371if eq get cap32_meta 0 0
372ret CAP32_ERR_STATE
373end
374ret CAP32_ERR_UNAUTH
375end
376
377fn cap32_cognition_admin op uint auth view Cap32Ref tgt view Cap32Ref to int
378if eq get cap32_meta 0 0
379ret CAP32_ERR_STATE
380end
381ret CAP32_ERR_UNAUTH
382end
383
384fn cap32_force_gen cid uint g uint to int
385id = ulong cid
386if ge id CAP32_MAX_SLOTS
387ret CAP32_ERR_NOT_FOUND
388end
389put cap32_s_gen id g
390if gt ulong g get cap32_meta 3
391put cap32_meta 3 ulong g
392end
393ret CAP32_OK
394end