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