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 / pipe.om

542 lines · 12507 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/core/pipe.om: Pure-Omega Capability-Gated Bounded Byte Pipes (AIENOS-NATIVE-PIPE-1).
3note Converted from upstream aienux native/kernel/core/pipe.h and native/kernel/core/pipe.c.
4note Fixed pool of 4 pipes * 64 KiB each, epoch-tagged capability handles, EOF/BROKEN_PIPE/WOULD_BLOCK,
5note revocation with buffer scrub, per-end cancel, user-VA window validation, and OSHF op-table dispatch.
6
7const PIPE_ABI_VERSION ulong 1
8const PIPE_FRAME_MAGIC ulong 0x4f534846
9const PIPE_CAPACITY ulong 65536
10const PIPE_POOL_PIPES ulong 4
11const PIPE_RES_TAG uint 0x50
12const PIPE_EPOCH_MAX uint 0xfffff
13const PIPE_USER_TOP ulong 0xffff000000000000
14const PIPE_BOUNCE ulong 1024
15
16note Pipe ends and capability rights
17const PIPE_END_READ uint 0
18const PIPE_END_WRITE uint 1
19const PIPE_R_READ uint 1
20const PIPE_R_WRITE uint 2
21const PIPE_R_DERIVE uint 4
22const PIPE_R_GRANT uint 8
23const PIPE_R_REVOKE uint 16
24const PIPE_CTL_RIGHTS uint 28
25
26note Pipe flags
27const PIPE_FLAG_LIVE uint 1
28const PIPE_FLAG_REVOKED uint 2
29const PIPE_FLAG_CANCEL_RD uint 4
30const PIPE_FLAG_CANCEL_WR uint 8
31
32note Platform op ids (OSHF)
33const PIPE_OP_CREATE ulong 1
34const PIPE_OP_READ ulong 2
35const PIPE_OP_WRITE ulong 3
36const PIPE_OP_CLOSE ulong 4
37const PIPE_OP_DUP ulong 5
38
39note OSH error codes
40const OSH_E_OK uint 0
41const OSH_E_INVALID_ARG uint 1
42const OSH_E_DENIED uint 2
43const OSH_E_STALE uint 3
44const OSH_E_REVOKED uint 4
45const OSH_E_LIMIT uint 5
46const OSH_E_WOULD_BLOCK uint 6
47const OSH_E_BROKEN_PIPE uint 7
48const OSH_E_INTERRUPTED uint 8
49const OSH_E_NOT_SUPPORTED uint 9
50
51note Pipe pool storage: 4 pipes * 65536 bytes = 262144 bytes
52global pipe_buf uchar 262144
53note Per-pipe metadata (4 pipes * 8 u32s = 32):
54note 0=head, 1=len, 2=epoch, 3=rrefs, 4=wrefs, 5=flags
55global pipe_meta uint 32
56note Pool totals: 0=live, 1=bytes_written, 2=bytes_read, 3=eof, 4=broken_pipe, 5=revocations, 6=refused
57global pipe_stats ulong 8
58
59note Capability tables: 2 principals (0=caller, 1=peer) * 16 slots = 32 slots
60const PIPE_CAP_SLOTS ulong 16
61global pcap_used uchar 32
62global pcap_gen uint 32
63global pcap_res uint 32
64global pcap_rights uint 32
65
66fn pipe_make_res idx uint end_id uint epoch uint to uint
67ret or or shl PIPE_RES_TAG 24 shl and end_id 1 23 or shl and idx 7 20 and epoch PIPE_EPOCH_MAX
68end
69
70fn pipe_make_handle slot uint gen uint to ulong
71ret or ulong slot shl ulong gen 32
72end
73
74fn pipe_pool_init to void
75for i ulong 32
76put pipe_meta i 0
77put pcap_used i 0
78put pcap_gen i 0
79put pcap_res i 0
80put pcap_rights i 0
81end
82for s ulong 8
83put pipe_stats s 0
84end
85end
86
87fn pipe_pool_empty to bool
88if ne get pipe_stats 0 0
89ret false
90end
91for i ulong 4
92if ne and get pipe_meta add mul i 8 5 PIPE_FLAG_LIVE 0
93ret false
94end
95end
96ret true
97end
98
99fn pcap_insert tbl ulong res uint rights uint h_out edit ulong 1 to bool
100base = mul tbl PIPE_CAP_SLOTS
101slot = PIPE_CAP_SLOTS
102for i ulong 16
103if eq get pcap_used add base i 0
104slot = i
105break
106end
107end
108if ge slot PIPE_CAP_SLOTS
109ret false
110end
111pos = add base slot
112gen = add get pcap_gen pos 1
113if eq gen 0
114gen = uint 1
115end
116put pcap_used pos 1
117put pcap_gen pos gen
118put pcap_res pos res
119put pcap_rights pos rights
120put h_out 0 pipe_make_handle uint slot gen
121ret true
122end
123
124fn pcap_remove tbl ulong h ulong to bool
125slot = ulong and h 0xffffffff
126gen = uint shr h 32
127if or ge slot PIPE_CAP_SLOTS eq gen 0
128ret false
129end
130pos = add mul tbl PIPE_CAP_SLOTS slot
131if or eq get pcap_used pos 0 ne get pcap_gen pos gen
132ret false
133end
134put pcap_used pos 0
135put pcap_res pos 0
136put pcap_rights pos 0
137ret true
138end
139
140fn pipe_check_live idx uint epoch uint live_ok bool to uint
141mbase = mul ulong idx 8
142flags = get pipe_meta add mbase 5
143if or eq and flags PIPE_FLAG_LIVE 0 ne get pipe_meta add mbase 2 epoch
144put pipe_stats 6 add get pipe_stats 6 1
145ret OSH_E_STALE
146end
147if and not live_ok ne and flags PIPE_FLAG_REVOKED 0
148put pipe_stats 6 add get pipe_stats 6 1
149ret OSH_E_REVOKED
150end
151ret OSH_E_OK
152end
153
154fn pcap_lookup_res tbl ulong h ulong need uint res_out edit uint 1 to uint
155slot = ulong and h 0xffffffff
156gen = uint shr h 32
157if or ge tbl 2 or ge slot PIPE_CAP_SLOTS eq gen 0
158ret OSH_E_INVALID_ARG
159end
160pos = add mul tbl PIPE_CAP_SLOTS slot
161if or eq get pcap_used pos 0 ne get pcap_gen pos gen
162put pipe_stats 6 add get pipe_stats 6 1
163ret OSH_E_STALE
164end
165if ne and get pcap_rights pos need need
166put pipe_stats 6 add get pipe_stats 6 1
167ret OSH_E_DENIED
168end
169put res_out 0 get pcap_res pos
170ret OSH_E_OK
171end
172
173note Resolve handle h in table tbl requiring rights `need` and end `want_end` (2 = either).
174note On OK writes idx to out_ie[0], end to out_ie[1].
175fn pipe_resolve tbl ulong h ulong need uint want_end uint live_ok bool out_ie edit uint 2 to uint
176rbox = new uint 1 0
177lrc = pcap_lookup_res tbl h need rbox
178if ne lrc OSH_E_OK
179ret lrc
180end
181res = get rbox 0
182if ne shr res 24 PIPE_RES_TAG
183put pipe_stats 6 add get pipe_stats 6 1
184ret OSH_E_DENIED
185end
186idx = and shr res 20 7
187end_id = and shr res 23 1
188if ge ulong idx PIPE_POOL_PIPES
189put pipe_stats 6 add get pipe_stats 6 1
190ret OSH_E_DENIED
191end
192if lt want_end 2
193if ne end_id want_end
194put pipe_stats 6 add get pipe_stats 6 1
195ret OSH_E_DENIED
196end
197end
198rc = pipe_check_live idx and res PIPE_EPOCH_MAX live_ok
199if ne rc OSH_E_OK
200ret rc
201end
202put out_ie 0 idx
203put out_ie 1 end_id
204ret OSH_E_OK
205end
206
207fn pipe_scrub idx ulong to void
208base = mul idx PIPE_CAPACITY
209for i ulong 65536
210put pipe_buf add base i 0
211end
212end
213
214fn pipe_create tbl ulong rd_out edit ulong 1 wr_out edit ulong 1 to uint
215if ge tbl 2
216ret OSH_E_INVALID_ARG
217end
218idx = PIPE_POOL_PIPES
219for i ulong 4
220mbase = mul i 8
221fl = get pipe_meta add mbase 5
222ep = get pipe_meta add mbase 2
223if and eq and fl PIPE_FLAG_LIVE 0 lt ep PIPE_EPOCH_MAX
224idx = i
225break
226end
227end
228if ge idx PIPE_POOL_PIPES
229ret OSH_E_LIMIT
230end
231mbase = mul idx 8
232epoch = add get pipe_meta add mbase 2 1
233rres = pipe_make_res uint idx PIPE_END_READ epoch
234wres = pipe_make_res uint idx PIPE_END_WRITE epoch
235if not pcap_insert tbl rres or PIPE_R_READ PIPE_CTL_RIGHTS rd_out
236ret OSH_E_LIMIT
237end
238if not pcap_insert tbl wres or PIPE_R_WRITE PIPE_CTL_RIGHTS wr_out
239ok_rm = pcap_remove tbl get rd_out 0
240if ok_rm
241ret OSH_E_LIMIT
242end
243ret OSH_E_LIMIT
244end
245put pipe_meta add mbase 0 0
246put pipe_meta add mbase 1 0
247put pipe_meta add mbase 2 epoch
248put pipe_meta add mbase 3 1
249put pipe_meta add mbase 4 1
250put pipe_meta add mbase 5 PIPE_FLAG_LIVE
251put pipe_stats 0 add get pipe_stats 0 1
252ret OSH_E_OK
253end
254
255fn pipe_ring_write idx ulong src view uchar * take ulong to void
256mbase = mul idx 8
257cur_len = ulong get pipe_meta add mbase 1
258head = ulong get pipe_meta add mbase 0
259wpos = rem add head cur_len PIPE_CAPACITY
260pbase = mul idx PIPE_CAPACITY
261i = ulong 0
262while lt i take bound 65536
263put pipe_buf add pbase wpos get src i
264wpos = rem add wpos 1 PIPE_CAPACITY
265i = add i 1
266end
267put pipe_meta add mbase 1 uint add cur_len take
268put pipe_stats 1 add get pipe_stats 1 take
269end
270
271fn pipe_write tbl ulong h ulong src view uchar * n_out edit ulong 1 to uint
272put n_out 0 0
273ie = new uint 2 0
274rc = pipe_resolve tbl h PIPE_R_WRITE PIPE_END_WRITE false ie
275if ne rc OSH_E_OK
276ret rc
277end
278idx = ulong get ie 0
279mbase = mul idx 8
280fl = get pipe_meta add mbase 5
281if ne and fl PIPE_FLAG_CANCEL_WR 0
282put pipe_meta add mbase 5 and fl xor uint 0xffffffff PIPE_FLAG_CANCEL_WR
283ret OSH_E_INTERRUPTED
284end
285if eq get pipe_meta add mbase 3 0
286put pipe_stats 4 add get pipe_stats 4 1
287ret OSH_E_BROKEN_PIPE
288end
289slen = len src
290if eq slen 0
291ret OSH_E_OK
292end
293room = sub PIPE_CAPACITY ulong get pipe_meta add mbase 1
294if eq room 0
295ret OSH_E_WOULD_BLOCK
296end
297take = slen
298if gt take room
299take = room
300end
301pipe_ring_write idx src take
302put n_out 0 take
303ret OSH_E_OK
304end
305
306fn pipe_ring_read idx ulong dst edit uchar * take ulong to void
307mbase = mul idx 8
308cur_len = ulong get pipe_meta add mbase 1
309head = ulong get pipe_meta add mbase 0
310pbase = mul idx PIPE_CAPACITY
311i = ulong 0
312while lt i take bound 65536
313put dst i get pipe_buf add pbase head
314put pipe_buf add pbase head 0
315head = rem add head 1 PIPE_CAPACITY
316i = add i 1
317end
318new_len = sub cur_len take
319if eq new_len 0
320head = ulong 0
321end
322put pipe_meta add mbase 0 uint head
323put pipe_meta add mbase 1 uint new_len
324put pipe_stats 2 add get pipe_stats 2 take
325end
326
327fn pipe_read tbl ulong h ulong dst edit uchar * n_out edit ulong 1 to uint
328put n_out 0 0
329ie = new uint 2 0
330rc = pipe_resolve tbl h PIPE_R_READ PIPE_END_READ false ie
331if ne rc OSH_E_OK
332ret rc
333end
334idx = ulong get ie 0
335mbase = mul idx 8
336fl = get pipe_meta add mbase 5
337if ne and fl PIPE_FLAG_CANCEL_RD 0
338put pipe_meta add mbase 5 and fl xor uint 0xffffffff PIPE_FLAG_CANCEL_RD
339ret OSH_E_INTERRUPTED
340end
341dlen = len dst
342if eq dlen 0
343ret OSH_E_OK
344end
345cur_len = ulong get pipe_meta add mbase 1
346if eq cur_len 0
347if eq get pipe_meta add mbase 4 0
348put pipe_stats 3 add get pipe_stats 3 1
349ret OSH_E_OK
350end
351ret OSH_E_WOULD_BLOCK
352end
353take = dlen
354if gt take cur_len
355take = cur_len
356end
357pipe_ring_read idx dst take
358put n_out 0 take
359ret OSH_E_OK
360end
361
362fn pipe_close tbl ulong h ulong to uint
363ie = new uint 2 0
364rc = pipe_resolve tbl h 0 2 true ie
365ok_rm = pcap_remove tbl h
366if ne rc OSH_E_OK
367if ok_rm
368ret rc
369end
370ret rc
371end
372idx = ulong get ie 0
373end_id = get ie 1
374mbase = mul idx 8
375if eq end_id PIPE_END_READ
376rr = get pipe_meta add mbase 3
377if gt rr 0
378put pipe_meta add mbase 3 sub rr 1
379end
380else
381wr = get pipe_meta add mbase 4
382if gt wr 0
383put pipe_meta add mbase 4 sub wr 1
384end
385end
386rr0 = get pipe_meta add mbase 3
387wr0 = get pipe_meta add mbase 4
388if and eq rr0 0 eq wr0 0
389pipe_scrub idx
390put pipe_meta add mbase 0 0
391put pipe_meta add mbase 1 0
392put pipe_meta add mbase 5 0
393if gt get pipe_stats 0 0
394put pipe_stats 0 sub get pipe_stats 0 1
395end
396end
397ret OSH_E_OK
398end
399
400fn pipe_revoke tbl ulong h ulong to uint
401ie = new uint 2 0
402rc = pipe_resolve tbl h PIPE_R_REVOKE 2 false ie
403if ne rc OSH_E_OK
404ret rc
405end
406idx = ulong get ie 0
407mbase = mul idx 8
408pipe_scrub idx
409put pipe_meta add mbase 0 0
410put pipe_meta add mbase 1 0
411put pipe_meta add mbase 5 or get pipe_meta add mbase 5 PIPE_FLAG_REVOKED
412put pipe_stats 5 add get pipe_stats 5 1
413ret OSH_E_OK
414end
415
416fn pipe_cancel tbl ulong h ulong to uint
417ie = new uint 2 0
418rc = pipe_resolve tbl h 0 2 false ie
419if ne rc OSH_E_OK
420ret rc
421end
422idx = ulong get ie 0
423end_id = get ie 1
424need = PIPE_R_WRITE
425cflag = PIPE_FLAG_CANCEL_WR
426if eq end_id PIPE_END_READ
427need = PIPE_R_READ
428cflag = PIPE_FLAG_CANCEL_RD
429end
430rc2 = pipe_resolve tbl h need end_id false ie
431if ne rc2 OSH_E_OK
432ret rc2
433end
434mbase = mul idx 8
435put pipe_meta add mbase 5 or get pipe_meta add mbase 5 cflag
436ret OSH_E_OK
437end
438
439fn pipe_dup_rights_ok src_r uint end_id uint rights uint to bool
440dright = PIPE_R_WRITE
441if eq end_id PIPE_END_READ
442dright = PIPE_R_READ
443end
444allowed = or dright PIPE_CTL_RIGHTS
445if eq and rights dright 0
446ret false
447end
448if ne and rights xor uint 0xffffffff src_r 0
449ret false
450end
451ret eq and rights xor uint 0xffffffff allowed 0
452end
453
454fn pipe_dup from_tbl ulong h ulong to_tbl ulong rights uint h_out edit ulong 1 to uint
455if ge to_tbl 2
456ret OSH_E_INVALID_ARG
457end
458need_ctl = PIPE_R_GRANT
459if eq from_tbl to_tbl
460need_ctl = PIPE_R_DERIVE
461end
462ie = new uint 2 0
463rc = pipe_resolve from_tbl h need_ctl 2 false ie
464if ne rc OSH_E_OK
465ret rc
466end
467pos = add mul from_tbl PIPE_CAP_SLOTS ulong and h 0xffffffff
468if not pipe_dup_rights_ok get pcap_rights pos get ie 1 rights
469ret OSH_E_DENIED
470end
471if not pcap_insert to_tbl get pcap_res pos rights h_out
472ret OSH_E_LIMIT
473end
474mbase = mul ulong get ie 0 8
475if eq get ie 1 PIPE_END_READ
476put pipe_meta add mbase 3 add get pipe_meta add mbase 3 1
477else
478put pipe_meta add mbase 4 add get pipe_meta add mbase 4 1
479end
480ret OSH_E_OK
481end
482
483fn pipe_user_window_ok base ulong slen ulong user_lo ulong user_hi ulong to bool
484if eq slen 0
485ret true
486end
487if or gt user_hi PIPE_USER_TOP le user_hi user_lo
488ret false
489end
490if or lt base user_lo ge base user_hi
491ret false
492end
493ret le slen sub user_hi base
494end
495
496fn pipe_dispatch_check frame view ulong * to uint
497ncells = len frame
498if lt ncells 4
499ret OSH_E_INVALID_ARG
500end
501if or ne get frame 0 PIPE_FRAME_MAGIC ne get frame 3 0
502ret OSH_E_INVALID_ARG
503end
504if ne get frame 1 PIPE_ABI_VERSION
505ret OSH_E_NOT_SUPPORTED
506end
507op = get frame 2
508if eq op PIPE_OP_CREATE
509if ne ncells 4
510ret OSH_E_INVALID_ARG
511end
512ret OSH_E_OK
513end
514if or eq op PIPE_OP_READ eq op PIPE_OP_WRITE
515if ne ncells 6
516ret OSH_E_INVALID_ARG
517end
518if or eq shr get frame 4 32 0 gt get frame 5 1
519ret OSH_E_INVALID_ARG
520end
521ret OSH_E_OK
522end
523if eq op PIPE_OP_CLOSE
524if ne ncells 5
525ret OSH_E_INVALID_ARG
526end
527if eq shr get frame 4 32 0
528ret OSH_E_INVALID_ARG
529end
530ret OSH_E_OK
531end
532if eq op PIPE_OP_DUP
533if ne ncells 7
534ret OSH_E_INVALID_ARG
535end
536if or eq shr get frame 4 32 0 gt get frame 6 1
537ret OSH_E_INVALID_ARG
538end
539ret OSH_E_OK
540end
541ret OSH_E_NOT_SUPPORTED
542end