interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / aienos_virtio_pci.c

112 lines · 4831 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* See aienos_virtio_pci.h. */
18#include "aienos_virtio_pci.h"
19
20#include <string.h>
21
22static uint16_t le16(const uint8_t *b) { return (uint16_t)(b[0] | (uint16_t)b[1] << 8); }
23static uint32_t le32(const uint8_t *b)
24{
25 return (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
26}
27
28#define VIRTIO_PCI_MAX_STEPS 48
29
30static virtio_pci_err set_region(virtio_pci_region *slot, virtio_pci_region r)
31{
32 int dup = slot->present;
33 *slot = r; /* Rust Option::replace stores before reporting the duplicate */
34 return dup ? VIRTIO_PCI_DUPLICATE_CAPABILITY : VIRTIO_PCI_OK;
35}
36
37const char *virtio_pci_err_name(virtio_pci_err e)
38{
39 switch (e) {
40 case VIRTIO_PCI_OK: return "Ok";
41 case VIRTIO_PCI_INVALID_CONFIG_LENGTH: return "InvalidConfigLength";
42 case VIRTIO_PCI_CAPABILITIES_NOT_PRESENT: return "CapabilitiesNotPresent";
43 case VIRTIO_PCI_INVALID_POINTER: return "InvalidPointer";
44 case VIRTIO_PCI_CAPABILITY_LOOP: return "CapabilityLoop";
45 case VIRTIO_PCI_SHORT_CAPABILITY: return "ShortCapability";
46 case VIRTIO_PCI_INVALID_REGION: return "InvalidRegion";
47 case VIRTIO_PCI_DUPLICATE_CAPABILITY: return "DuplicateCapability";
48 }
49 return "Unknown";
50}
51
52virtio_pci_err virtio_pci_parse_caps(const uint8_t *config, size_t len, virtio_pci_caps *out)
53{
54 if (!out) return VIRTIO_PCI_INVALID_CONFIG_LENGTH;
55 memset(out, 0, sizeof *out);
56 if (!config || (len != 256 && len != 4096)) return VIRTIO_PCI_INVALID_CONFIG_LENGTH;
57 if ((le16(config + 0x06) & (1u << 4)) == 0) return VIRTIO_PCI_CAPABILITIES_NOT_PRESENT;
58
59 uint8_t visited[256];
60 memset(visited, 0, sizeof visited);
61 unsigned steps = 0;
62 uint8_t pointer = config[0x34] & (uint8_t)~0x03u;
63 while (pointer != 0) {
64 size_t index = pointer; /* <= 252, so every bound below is in range of size_t */
65 if (index < 0x40 || index + 2 > len) return VIRTIO_PCI_INVALID_POINTER;
66 if (visited[index] || steps >= VIRTIO_PCI_MAX_STEPS) return VIRTIO_PCI_CAPABILITY_LOOP;
67 visited[index] = 1;
68 steps++;
69
70 uint8_t next = config[index + 1] & (uint8_t)~0x03u;
71 if (config[index] == 0x09) {
72 if (index + 16 > len) return VIRTIO_PCI_SHORT_CAPABILITY;
73 size_t cap_len = config[index + 2];
74 uint8_t kind = config[index + 3];
75 size_t minimum = kind == 2 ? 20 : 16;
76 if (cap_len < minimum || index + cap_len > len) return VIRTIO_PCI_SHORT_CAPABILITY;
77 virtio_pci_region r = {1, config[index + 4], le32(config + index + 8),
78 le32(config + index + 12)};
79 /* Region checks apply only to the structures this driver maps
80 * (cfg_type 1..4). VIRTIO_PCI_CAP_PCI_CFG (5) legally carries
81 * bar 0 / offset 0 / length 0 (real QEMU virtio-net-pci does), and
82 * the spec tells drivers to ignore cfg types they do not use. The
83 * Rust reference checked every vendor cap and so refused real
84 * QEMU; AIENOS_VIRTIO_PCI_REFERENCE_COMPAT restores that order for
85 * the pinned Rust differential only (tests/net_diff.c). */
86#ifdef AIENOS_VIRTIO_PCI_REFERENCE_COMPAT
87 int mapped = 1;
88#else
89 int mapped = kind >= 1 && kind <= 4;
90#endif
91 if (mapped && (r.bar > 5 || r.length == 0 || r.offset > UINT32_MAX - r.length))
92 return VIRTIO_PCI_INVALID_REGION;
93 virtio_pci_err e = VIRTIO_PCI_OK;
94 switch (kind) {
95 case 1: e = set_region(&out->common_cfg, r); break;
96 case 2:
97 e = set_region(&out->notify_cfg, r);
98 if (e) break;
99 if (out->has_notify_off_multiplier) { e = VIRTIO_PCI_DUPLICATE_CAPABILITY; break; }
100 out->has_notify_off_multiplier = 1;
101 out->notify_off_multiplier = le32(config + index + 16);
102 break;
103 case 3: e = set_region(&out->isr_cfg, r); break;
104 case 4: e = set_region(&out->device_cfg, r); break;
105 default: break;
106 }
107 if (e) return e;
108 }
109 pointer = next;
110 }
111 return VIRTIO_PCI_OK;
112}