aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / dmar.c
| 1 | /* Byte-based reads: ACPI fields are little-endian and may be unaligned. |
| 2 | * Inventory only. This file never reads or writes a device register. */ |
| 3 | #include "dmar.h" |
| 4 | #include <string.h> |
| 5 | |
| 6 | static uint16_t r16(const uint8_t *p) { return (uint16_t)p[0] | (uint16_t)p[1] << 8; } |
| 7 | static uint32_t r32(const uint8_t *p) { return (uint32_t)r16(p) | (uint32_t)r16(p + 2) << 16; } |
| 8 | static uint64_t r64(const uint8_t *p) { return (uint64_t)r32(p) | (uint64_t)r32(p + 4) << 32; } |
| 9 | |
| 10 | static int scopes(const uint8_t *p, unsigned bytes, uint8_t kind, uint16_t owner, |
| 11 | int include_all, struct ck_dmar *d) |
| 12 | { |
| 13 | while (bytes) { |
| 14 | if (bytes < 2 || p[1] < 8 || p[1] > bytes || ((p[1] - 6) & 1)) |
| 15 | return CK_DMAR_BAD_TABLE; |
| 16 | if (p[0] < 1 || p[0] > 5) |
| 17 | return CK_DMAR_UNSUPPORTED; |
| 18 | if (p[2] || p[3] || ((p[0] == 1 || p[0] == 2) && p[4]) || |
| 19 | (include_all && (p[0] == 1 || p[0] == 2))) |
| 20 | return CK_DMAR_BAD_TABLE; |
| 21 | unsigned npath = (p[1] - 6) / 2; |
| 22 | if (npath > CK_DMAR_MAX_PATH || d->nscopes == CK_DMAR_MAX_SCOPES) |
| 23 | return CK_DMAR_CAPACITY; |
| 24 | struct ck_dmar_scope *s = &d->scope[d->nscopes++]; |
| 25 | s->kind = kind; |
| 26 | s->owner = owner; |
| 27 | s->type = p[0]; |
| 28 | s->enumeration_id = p[4]; |
| 29 | s->start_bus = p[5]; |
| 30 | s->npath = (uint8_t)npath; |
| 31 | for (unsigned i = 0; i < npath; i++) { |
| 32 | if (p[6 + 2 * i] > 31 || p[7 + 2 * i] > 7) |
| 33 | return CK_DMAR_BAD_TABLE; |
| 34 | s->path[i].device = p[6 + 2 * i]; |
| 35 | s->path[i].function = p[7 + 2 * i]; |
| 36 | } |
| 37 | bytes -= p[1]; |
| 38 | p += p[1]; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | int ck_dmar_parse(const void *table, size_t available, struct ck_dmar *out) |
| 44 | { |
| 45 | if (!out) |
| 46 | return CK_DMAR_BAD_TABLE; |
| 47 | memset(out, 0, sizeof *out); |
| 48 | if (!table || available < 48) |
| 49 | return CK_DMAR_BAD_TABLE; |
| 50 | const uint8_t *p = table; |
| 51 | uint32_t bytes = r32(p + 4); |
| 52 | if (memcmp(p, "DMAR", 4) || bytes < 48 || bytes > available) |
| 53 | return CK_DMAR_BAD_TABLE; |
| 54 | if (bytes > CK_DMAR_MAX_BYTES) |
| 55 | return CK_DMAR_CAPACITY; |
| 56 | uint8_t sum = 0; |
| 57 | for (uint32_t i = 0; i < bytes; i++) |
| 58 | sum = (uint8_t)(sum + p[i]); |
| 59 | if (sum || p[36] > 63 || (p[37] & ~7u)) |
| 60 | return CK_DMAR_BAD_TABLE; |
| 61 | if (p[8] != 1) |
| 62 | return CK_DMAR_UNSUPPORTED; |
| 63 | for (unsigned i = 38; i < 48; i++) |
| 64 | if (p[i]) |
| 65 | return CK_DMAR_BAD_TABLE; |
| 66 | struct ck_dmar d = {0}; |
| 67 | d.bytes = bytes; |
| 68 | d.address_bits = p[36] + 1; |
| 69 | d.flags = p[37]; |
| 70 | uint16_t previous_type = 0; |
| 71 | for (uint32_t at = 48; at < bytes;) { |
| 72 | if (bytes - at < 4) |
| 73 | return CK_DMAR_BAD_TABLE; |
| 74 | const uint8_t *entry = p + at; |
| 75 | uint16_t type = r16(entry), size = r16(entry + 2); |
| 76 | if (size < 4 || size > bytes - at || type < previous_type) |
| 77 | return CK_DMAR_BAD_TABLE; |
| 78 | previous_type = type; |
| 79 | int rc = 0; |
| 80 | if (type == 0) { |
| 81 | if (size < 16 || (entry[4] & ~1u) || (entry[5] & 0xf0)) |
| 82 | return CK_DMAR_BAD_TABLE; |
| 83 | if (d.nunits == CK_DMAR_MAX_UNITS) |
| 84 | return CK_DMAR_CAPACITY; |
| 85 | struct ck_dmar_unit *u = &d.unit[d.nunits]; |
| 86 | u->base = r64(entry + 8); |
| 87 | u->register_bytes = 1u << (12 + entry[5]); |
| 88 | u->segment = r16(entry + 6); |
| 89 | u->include_all = entry[4] & 1; |
| 90 | if (!u->base || (u->base & (u->register_bytes - 1))) |
| 91 | return CK_DMAR_BAD_TABLE; |
| 92 | rc = scopes(entry + 16, size - 16, 0, (uint16_t)d.nunits, u->include_all, &d); |
| 93 | d.nunits++; |
| 94 | } else if (type == 1) { |
| 95 | if (size < 32 || r16(entry + 4)) |
| 96 | return CK_DMAR_BAD_TABLE; |
| 97 | if (d.nregions == CK_DMAR_MAX_REGIONS) |
| 98 | return CK_DMAR_CAPACITY; |
| 99 | struct ck_dmar_region *r = &d.region[d.nregions]; |
| 100 | r->base = r64(entry + 8); |
| 101 | r->limit = r64(entry + 16); |
| 102 | r->segment = r16(entry + 6); |
| 103 | if ((r->base & 4095) || (r->limit & 4095) != 4095 || r->base >= r->limit || |
| 104 | (d.address_bits < 64 && (r->limit >> d.address_bits))) |
| 105 | return CK_DMAR_BAD_TABLE; |
| 106 | rc = scopes(entry + 24, size - 24, 1, (uint16_t)d.nregions, 0, &d); |
| 107 | d.nregions++; |
| 108 | } else { |
| 109 | d.nother++; |
| 110 | } |
| 111 | if (rc) |
| 112 | return rc; |
| 113 | at += size; |
| 114 | } |
| 115 | if (!d.nunits) |
| 116 | return CK_DMAR_BAD_TABLE; |
| 117 | *out = d; |
| 118 | return 0; |
| 119 | } |