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 / kernel / tests / test_dmar.c

141 lines · 6987 bytes Raw Blame History
1/* Crafted byte tables from Intel VT-d rev. 5.0 sections 8.1-8.4.
2 * These tests inventory firmware; they do not exercise an IOMMU. */
3#include <stdint.h>
4#include <string.h>
5#include "ck_test.h"
6#include "dmar.h"
7
8static uint8_t table[4096];
9static void w16(unsigned at, uint16_t v) { table[at] = (uint8_t)v; table[at + 1] = (uint8_t)(v >> 8); }
10static void w32(unsigned at, uint32_t v) { for (unsigned i = 0; i < 4; i++) table[at + i] = (uint8_t)(v >> (8 * i)); }
11static void w64(unsigned at, uint64_t v) { for (unsigned i = 0; i < 8; i++) table[at + i] = (uint8_t)(v >> (8 * i)); }
12static void seal(unsigned bytes)
13{
14 w32(4, bytes);
15 table[9] = 0;
16 uint8_t sum = 0;
17 for (unsigned i = 0; i < bytes; i++) sum = (uint8_t)(sum + table[i]);
18 table[9] = (uint8_t)-sum;
19}
20static void header(void)
21{
22 memset(table, 0, sizeof table);
23 memcpy(table, "DMAR", 4);
24 table[8] = 1;
25 table[36] = 38; /* encoded width: 39 physical address bits */
26 table[37] = 1;
27}
28static unsigned fixture(void)
29{
30 header();
31 /* Explicit unit with a two-hop PCI path. */
32 w16(48, 0); w16(50, 26); w64(56, 0xfed90000ull);
33 table[64] = 1; table[65] = 10;
34 table[70] = 28; table[71] = 0; table[72] = 0; table[73] = 1;
35 /* Include-all unit with an IOAPIC scope, in another segment. */
36 w16(74, 0); w16(76, 24); table[78] = 1; w16(80, 2); w64(82, 0xfed91000ull);
37 table[90] = 3; table[91] = 8; table[94] = 7; table[95] = 4;
38 table[96] = 31; table[97] = 7;
39 /* Reserved DMA region with one directly attached endpoint. */
40 w16(98, 1); w16(100, 32); w16(104, 2);
41 w64(106, 0x200000); w64(114, 0x203fff);
42 table[122] = 1; table[123] = 8; table[127] = 3; table[128] = 20;
43 /* Unknown future structure: skip by its declared length. */
44 w16(130, 7); w16(132, 4);
45 seal(134);
46 return 134;
47}
48static void rejected(unsigned bytes, int expected)
49{
50 struct ck_dmar out, zero = {0};
51 memset(&out, 0xa5, sizeof out);
52 seal(bytes);
53 CHECK(ck_dmar_parse(table, bytes, &out) == expected);
54 CHECK(memcmp(&out, &zero, sizeof out) == 0);
55}
56int main(void)
57{
58 struct ck_dmar out;
59 unsigned bytes = fixture();
60 CHECK(ck_dmar_parse(table, bytes, &out) == 0);
61 CHECK(out.bytes == bytes && out.address_bits == 39 && out.flags == 1);
62 CHECK(out.nunits == 2 && out.nregions == 1 && out.nscopes == 3 && out.nother == 1);
63 CHECK(out.unit[0].base == 0xfed90000ull && out.unit[0].register_bytes == 4096);
64 CHECK(out.unit[1].include_all == 1 && out.unit[1].segment == 2);
65 CHECK(out.scope[0].npath == 2 && out.scope[0].path[0].device == 28 && out.scope[0].path[1].function == 1);
66 CHECK(out.scope[1].type == 3 && out.scope[1].enumeration_id == 7 && out.scope[1].start_bus == 4);
67 CHECK(out.scope[2].kind == 1 && out.scope[2].owner == 0 && out.scope[2].start_bus == 3);
68 CHECK(out.region[0].base == 0x200000 && out.region[0].limit == 0x203fff && out.region[0].segment == 2);
69 /* Every prefix, even one byte short of a complete table, must refuse. */
70 for (unsigned n = 0; n < bytes; n++) {
71 memset(&out, 0xa5, sizeof out);
72 CHECK(ck_dmar_parse(table, n, &out) == CK_DMAR_BAD_TABLE);
73 CHECK(out.nunits == 0 && out.nregions == 0 && out.nscopes == 0);
74 }
75 CHECK(ck_dmar_parse(0, bytes, &out) == CK_DMAR_BAD_TABLE);
76 CHECK(ck_dmar_parse(table, bytes, 0) == CK_DMAR_BAD_TABLE);
77 table[9]++; CHECK(ck_dmar_parse(table, bytes, &out) == CK_DMAR_BAD_TABLE);
78 fixture(); table[0] = 'X'; rejected(bytes, CK_DMAR_BAD_TABLE);
79 fixture(); table[8] = 2; rejected(bytes, CK_DMAR_UNSUPPORTED);
80 fixture(); table[36] = 64; rejected(bytes, CK_DMAR_BAD_TABLE);
81 fixture(); table[38] = 1; rejected(bytes, CK_DMAR_BAD_TABLE);
82 fixture(); w16(50, 0); rejected(bytes, CK_DMAR_BAD_TABLE);
83 fixture(); w16(50, 3); rejected(bytes, CK_DMAR_BAD_TABLE);
84 fixture(); w16(50, 200); rejected(bytes, CK_DMAR_BAD_TABLE);
85 fixture(); w16(50, 15); rejected(bytes, CK_DMAR_BAD_TABLE);
86 fixture(); w64(56, 0); rejected(bytes, CK_DMAR_BAD_TABLE);
87 fixture(); w64(56, 0xfed90001ull); rejected(bytes, CK_DMAR_BAD_TABLE);
88 fixture(); table[53] = 1; /* 8 KiB register set, base not 8 KiB aligned */
89 w64(56, 0xfed91000ull); rejected(bytes, CK_DMAR_BAD_TABLE);
90 fixture(); table[65] = 9; rejected(bytes, CK_DMAR_BAD_TABLE);
91 fixture(); table[65] = 7; rejected(bytes, CK_DMAR_BAD_TABLE);
92 fixture(); table[66] = 1; rejected(bytes, CK_DMAR_BAD_TABLE);
93 fixture(); table[70] = 32; rejected(bytes, CK_DMAR_BAD_TABLE);
94 fixture(); table[71] = 8; rejected(bytes, CK_DMAR_BAD_TABLE);
95 fixture(); table[64] = 6; rejected(bytes, CK_DMAR_UNSUPPORTED);
96 fixture(); table[52] = 1; rejected(bytes, CK_DMAR_BAD_TABLE); /* PCI scope under include-all */
97 fixture(); w64(106, 0x200001); rejected(bytes, CK_DMAR_BAD_TABLE);
98 fixture(); w64(114, 0x203ffe); rejected(bytes, CK_DMAR_BAD_TABLE);
99 fixture(); w64(114, 0x1fffff); rejected(bytes, CK_DMAR_BAD_TABLE);
100 fixture(); w16(100, 24); rejected(122, CK_DMAR_BAD_TABLE); /* RMRR needs a scope */
101 header(); seal(48); rejected(48, CK_DMAR_BAD_TABLE); /* no remapping unit */
102 /* Capacity exhaustion clears all of the earlier valid entries. */
103 header();
104 unsigned at = 48;
105 for (unsigned n = 0; n <= CK_DMAR_MAX_UNITS; n++, at += 16) {
106 w16(at, 0); w16(at + 2, 16); w64(at + 8, 0xfed90000ull + 4096 * n);
107 }
108 rejected(at, CK_DMAR_CAPACITY);
109 /* A long path and either shared scope pool or region pool must stop
110 * before writing past their fixed storage, with no partial inventory. */
111 header();
112 w16(50, 16 + 6 + 2 * (CK_DMAR_MAX_PATH + 1));
113 w64(56, 0xfed90000ull); table[64] = 1;
114 table[65] = 6 + 2 * (CK_DMAR_MAX_PATH + 1);
115 rejected(48 + 16 + table[65], CK_DMAR_CAPACITY);
116 header();
117 w16(50, 16 + 8 * CK_DMAR_MAX_SCOPES); w64(56, 0xfed90000ull);
118 at = 64;
119 for (unsigned n = 0; n < CK_DMAR_MAX_SCOPES; n++, at += 8) {
120 table[at] = 1; table[at + 1] = 8;
121 }
122 /* The last scope is in an RMRR: capacities apply across both owners. */
123 w16(at, 1); w16(at + 2, 32); w64(at + 16, 4095);
124 table[at + 24] = 1; table[at + 25] = 8;
125 rejected(at + 32, CK_DMAR_CAPACITY);
126 header(); w16(50, 16); w64(56, 0xfed90000ull);
127 at = 64;
128 for (unsigned n = 0; n <= CK_DMAR_MAX_REGIONS; n++, at += 32) {
129 w16(at, 1); w16(at + 2, 32);
130 w64(at + 8, 4096 * n); w64(at + 16, 4096 * n + 4095);
131 table[at + 24] = 1; table[at + 25] = 8;
132 }
133 rejected(at, CK_DMAR_CAPACITY);
134 fixture(); w64(114, (1ull << 39) | 4095); rejected(bytes, CK_DMAR_BAD_TABLE);
135 fixture(); table[36] = 63; w64(114, UINT64_MAX); seal(bytes);
136 CHECK(ck_dmar_parse(table, bytes, &out) == 0 && out.address_bits == 64);
137 /* Extra supplied bytes are not silently added to the declared table. */
138 bytes = fixture(); table[bytes] = 0xff;
139 CHECK(ck_dmar_parse(table, sizeof table, &out) == 0 && out.bytes == bytes);
140 return ck_t_verdict("CK_DMAR");
141}