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 / net / core / virtio.om

273 lines · 6336 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/net/core/virtio.om: Pure-Omega Virtio 1.x PCI capability discovery and split-virtqueue net driver.
3note Converted from aienux native/net/aienos_virtio_pci.{h,c} and native/net/aienos_virtio_net.{h,c}.
4note Enforces VIRTIO_F_VERSION_1, optional/required VIRTIO_F_ACCESS_PLATFORM (IOMMU fail-closed),
5note and sticky broken state on untrusted used-ring anomalies.
6note Compose with: si/directives/om2.om aienux/net/core/virtio.om ...
7
8const VPCI_OK int 0
9const VPCI_ERR_NO_CAPS int -1
10const VPCI_ERR_MISSING int -2
11const VPCI_ERR_LOOP int -3
12
13const VPCI_CAP_VENDOR uchar 0x09
14const VPCI_CFG_COMMON uchar 1
15const VPCI_CFG_NOTIFY uchar 2
16const VPCI_CFG_ISR uchar 3
17const VPCI_CFG_DEVICE uchar 4
18
19const VNET_OK int 0
20const VNET_ERR_ARG int -1
21const VNET_ERR_FEATURE int -2
22const VNET_ERR_ACCESS_PLATFORM int -3
23const VNET_ERR_QUEUE int -4
24const VNET_ERR_BROKEN int -5
25const VNET_ERR_EMPTY int -6
26const VNET_ERR_FULL int -7
27
28const VNET_INIT_REQUIRE_ACCESS_PLATFORM uint 1
29
30const VNET_F_MAC ulong 0x20
31const VNET_F_VERSION_1 ulong 0x100000000
32const VNET_F_ACCESS_PLATFORM ulong 0x200000000
33
34const VNET_ST_ACK uchar 1
35const VNET_ST_DRIVER uchar 2
36const VNET_ST_DRIVER_OK uchar 4
37const VNET_ST_FEATURES_OK uchar 8
38const VNET_ST_FAILED uchar 128
39
40const VNET_HDR_LEN ulong 12
41const VNET_FRAME_MAX ulong 1514
42const VNET_BUF_LEN ulong 1526
43const VNET_Q_CAP ulong 32
44
45note cap_out uint 10: 0=common_bar 1=common_off 2=notify_bar 3=notify_off 4=notify_mult 5=isr_bar 6=isr_off 7=dev_bar 8=dev_off 9=seen_mask
46
47note Simulated Virtio-Net device + split-virtqueue state
48note vnet_st: 0=init, 1=broken, 2=qsize, 3=tx_avail, 4=tx_used, 5=rx_avail, 6=rx_used, 7=status, 8=iommu_en, 9=tx_notifies, 10=rx_notifies
49global vnet_st ulong 12
50global vnet_tx_out uchar 32
51global vnet_rx_out uchar 32
52global vnet_rx_buf uchar 49152
53global vnet_rx_len ulong 32
54
55fn vpci_le32 cfg view uchar * off ulong to uint
56b0 = uint get cfg off
57b1 = shl uint get cfg add off 1 8
58b2 = shl uint get cfg add off 2 16
59b3 = shl uint get cfg add off 3 24
60ret or or b0 b1 or b2 b3
61end
62
63fn vpci_parse_caps cfg view uchar * cap_out edit uint 10 to int
64if lt len cfg 256
65ret VPCI_ERR_NO_CAPS
66end
67for i ulong 10
68put cap_out i 0
69end
70st = uint get cfg 6
71if eq and st 0x10 0
72ret VPCI_ERR_NO_CAPS
73end
74ptr = ulong and get cfg 52 0xfc
75steps = ulong 0
76while and ne ptr 0 lt ptr 252 bound 64
77steps = add steps 1
78if gt steps 48
79ret VPCI_ERR_LOOP
80end
81cid = get cfg ptr
82nxt = ulong and get cfg add ptr 1 0xfc
83if eq cid VPCI_CAP_VENDOR
84clen = ulong get cfg add ptr 2
85if and ge clen 16 le add ptr clen 256
86ctype = get cfg add ptr 3
87bar = uint get cfg add ptr 4
88off = vpci_le32 cfg add ptr 8
89if eq ctype VPCI_CFG_COMMON
90put cap_out 0 bar
91put cap_out 1 off
92put cap_out 9 or get cap_out 9 1
93end
94if eq ctype VPCI_CFG_NOTIFY
95if ge clen 20
96put cap_out 2 bar
97put cap_out 3 off
98put cap_out 4 vpci_le32 cfg add ptr 16
99put cap_out 9 or get cap_out 9 2
100end
101end
102if eq ctype VPCI_CFG_ISR
103put cap_out 5 bar
104put cap_out 6 off
105put cap_out 9 or get cap_out 9 4
106end
107if eq ctype VPCI_CFG_DEVICE
108put cap_out 7 bar
109put cap_out 8 off
110put cap_out 9 or get cap_out 9 8
111end
112end
113end
114ptr = nxt
115end
116if ne and get cap_out 9 15 15
117ret VPCI_ERR_MISSING
118end
119ret VPCI_OK
120end
121
122note ---- Virtio-Net Split-Virtqueue Driver ----
123
124fn vnet_reset to void
125for i ulong 12
126put vnet_st i 0
127end
128for i ulong 32
129put vnet_tx_out i 0
130put vnet_rx_out i 0
131put vnet_rx_len i 0
132end
133end
134
135fn vnet_init dev_features ulong qsize ulong flags uint to int
136vnet_reset
137if or lt qsize 2 gt qsize VNET_Q_CAP
138ret VNET_ERR_QUEUE
139end
140if eq and dev_features VNET_F_VERSION_1 0
141put vnet_st 7 ulong VNET_ST_FAILED
142ret VNET_ERR_FEATURE
143end
144has_ap = ne and dev_features VNET_F_ACCESS_PLATFORM 0
145if and ne and flags VNET_INIT_REQUIRE_ACCESS_PLATFORM 0 not has_ap
146put vnet_st 7 ulong VNET_ST_FAILED
147ret VNET_ERR_ACCESS_PLATFORM
148end
149put vnet_st 0 1
150put vnet_st 1 0
151put vnet_st 2 qsize
152st = or ulong VNET_ST_ACK or ulong VNET_ST_DRIVER or ulong VNET_ST_FEATURES_OK ulong VNET_ST_DRIVER_OK
153put vnet_st 7 st
154if has_ap
155put vnet_st 8 1
156end
157ret VNET_OK
158end
159
160fn vnet_is_broken to bool
161ret ne get vnet_st 1 0
162end
163
164fn vnet_tx_submit frame view uchar * to int
165if eq get vnet_st 0 0
166ret VNET_ERR_ARG
167end
168if ne get vnet_st 1 0
169ret VNET_ERR_BROKEN
170end
171if or eq len frame 0 gt len frame VNET_FRAME_MAX
172ret VNET_ERR_ARG
173end
174qsz = get vnet_st 2
175avail = get vnet_st 3
176used = get vnet_st 4
177if ge sub avail used qsz
178ret VNET_ERR_FULL
179end
180desc_id = rem avail qsz
181if ne get vnet_tx_out desc_id 0
182put vnet_st 1 1
183ret VNET_ERR_BROKEN
184end
185put vnet_tx_out desc_id 1
186put vnet_st 3 add avail 1
187put vnet_st 9 add get vnet_st 9 1
188ret VNET_OK
189end
190
191note Process untrusted device TX used-ring completion; marks broken (sticky) on any anomaly
192fn vnet_tx_complete desc_id ulong to int
193if ne get vnet_st 1 0
194ret VNET_ERR_BROKEN
195end
196qsz = get vnet_st 2
197avail = get vnet_st 3
198used = get vnet_st 4
199if ge used avail
200put vnet_st 1 1
201ret VNET_ERR_BROKEN
202end
203if ge desc_id qsz
204put vnet_st 1 1
205ret VNET_ERR_BROKEN
206end
207if eq get vnet_tx_out desc_id 0
208put vnet_st 1 1
209ret VNET_ERR_BROKEN
210end
211put vnet_tx_out desc_id 0
212put vnet_st 4 add used 1
213ret VNET_OK
214end
215
216fn vnet_rx_post to int
217if eq get vnet_st 0 0
218ret VNET_ERR_ARG
219end
220if ne get vnet_st 1 0
221ret VNET_ERR_BROKEN
222end
223qsz = get vnet_st 2
224avail = get vnet_st 5
225used = get vnet_st 6
226if ge sub avail used qsz
227ret VNET_ERR_FULL
228end
229desc_id = rem avail qsz
230put vnet_rx_out desc_id 1
231put vnet_st 5 add avail 1
232put vnet_st 10 add get vnet_st 10 1
233ret VNET_OK
234end
235
236note Process untrusted device RX used-ring completion; validates id, outstanding flag, and length
237fn vnet_rx_complete desc_id ulong total_len ulong payload view uchar * to int
238if ne get vnet_st 1 0
239ret VNET_ERR_BROKEN
240end
241qsz = get vnet_st 2
242avail = get vnet_st 5
243used = get vnet_st 6
244if ge used avail
245put vnet_st 1 1
246ret VNET_ERR_BROKEN
247end
248if ge desc_id qsz
249put vnet_st 1 1
250ret VNET_ERR_BROKEN
251end
252if eq get vnet_rx_out desc_id 0
253put vnet_st 1 1
254ret VNET_ERR_BROKEN
255end
256if or lt total_len VNET_HDR_LEN gt total_len VNET_BUF_LEN
257put vnet_st 1 1
258ret VNET_ERR_BROKEN
259end
260flen = sub total_len VNET_HDR_LEN
261if lt len payload flen
262put vnet_st 1 1
263ret VNET_ERR_BROKEN
264end
265base = mul desc_id 1536
266for i ulong flen bound 1514
267put vnet_rx_buf add base i get payload i
268end
269put vnet_rx_len desc_id flen
270put vnet_rx_out desc_id 0
271put vnet_st 6 add used 1
272ret VNET_OK
273end