omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / tests / irqvfio_ref.py
| 1 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | """Verification-only Linux/AArch64 UAPI emulator over a child-owned memfd. |
| 3 | |
| 4 | No device is opened. Real eventfd/read/write/poll/mmap execute in the kernel. |
| 5 | Only ENOTTY responses to the memfd's VFIO ioctls are replaced. This is NOT a |
| 6 | VFIO device, firmware emulator, interrupt delivery test, or hardware receipt. |
| 7 | Constants/layout: Linux asm-generic/unistd.h, linux/ptrace.h, linux/vfio.h, |
| 8 | and asm/ptrace.h (user_pt_regs); the compiler lane's C oracle is independent. |
| 9 | """ |
| 10 | import ctypes as C |
| 11 | import errno |
| 12 | import os |
| 13 | import signal |
| 14 | import struct |
| 15 | |
| 16 | libc = C.CDLL(None, use_errno=True) |
| 17 | libc.ptrace.restype = C.c_long |
| 18 | libc.ptrace.argtypes = (C.c_uint, C.c_int, C.c_void_p, C.c_void_p) |
| 19 | |
| 20 | class Registers(C.Structure): |
| 21 | _fields_ = [('r', C.c_ulonglong * 31), ('sp', C.c_ulonglong), |
| 22 | ('pc', C.c_ulonglong), ('pstate', C.c_ulonglong)] |
| 23 | |
| 24 | class Iovec(C.Structure): |
| 25 | _fields_ = [('base', C.c_void_p), ('length', C.c_size_t)] |
| 26 | |
| 27 | def ptrace(request, pid, address=0, data=0): |
| 28 | C.set_errno(0) |
| 29 | result = libc.ptrace(request, pid, address, data) |
| 30 | error = C.get_errno() |
| 31 | if result == -1 and error: |
| 32 | raise OSError(error, os.strerror(error)) |
| 33 | return result |
| 34 | |
| 35 | def registers(pid): |
| 36 | regs = Registers() |
| 37 | vec = Iovec(C.cast(C.pointer(regs), C.c_void_p), C.sizeof(regs)) |
| 38 | ptrace(0x4204, pid, 1, C.cast(C.pointer(vec), C.c_void_p)) |
| 39 | return regs |
| 40 | |
| 41 | def setregisters(pid, regs): |
| 42 | vec = Iovec(C.cast(C.pointer(regs), C.c_void_p), C.sizeof(regs)) |
| 43 | ptrace(0x4205, pid, 1, C.cast(C.pointer(vec), C.c_void_p)) |
| 44 | |
| 45 | def fetch(pid, address, count): |
| 46 | result = bytearray() |
| 47 | for offset in range(0, count, 8): |
| 48 | result += struct.pack('<Q', C.c_ulonglong(ptrace(2, pid, address + offset)).value) |
| 49 | return bytes(result[:count]) |
| 50 | |
| 51 | def store(pid, address, data): |
| 52 | for offset in range(0, len(data), 8): |
| 53 | part = data[offset:offset + 8] |
| 54 | if len(part) < 8: |
| 55 | part += fetch(pid, address + offset, 8)[len(part):] |
| 56 | ptrace(5, pid, address + offset, struct.unpack('<Q', part)[0]) |
| 57 | |
| 58 | def expected(mode): |
| 59 | fields = [0] * 16 |
| 60 | fields[0] = 128 |
| 61 | fields[12] = 1 |
| 62 | if mode in (9, 10): |
| 63 | fields[0] += -1 if mode == 9 else -95 |
| 64 | fields[12] = 0 |
| 65 | return tuple(fields) |
| 66 | query_error = {1: -5, 2: -95, 3: -1, 13: -1, 14: -1, |
| 67 | 15: -1, 16: -95, 17: -95, 18: -95, 19: -95} |
| 68 | if mode in query_error: |
| 69 | fields[1] = 128 + query_error[mode] |
| 70 | return tuple(fields) |
| 71 | if mode in (4, 5, 11, 20): |
| 72 | fields[1] = 123 |
| 73 | fields[9] = 7 if mode in (11, 20) else 6 |
| 74 | if mode in (11, 20): |
| 75 | fields[10] = int(mode == 11) |
| 76 | fields[11] = 1 |
| 77 | fields[13] = int(mode == 11) |
| 78 | fields[14] = 1 |
| 79 | return tuple(fields) |
| 80 | fields[1] = 128 |
| 81 | fields[2] = 123 if mode == 8 else 128 |
| 82 | fields[9:12] = [7, 1, 1] |
| 83 | fields[13:15] = [int(mode in (6, 8)), 1] |
| 84 | if mode == 8: |
| 85 | return tuple(fields) |
| 86 | fields[3:9] = [128, 192, 128, 128, 129, 123 if mode in (6, 7) else 33] |
| 87 | fields[15] = 2 |
| 88 | return tuple(fields) |
| 89 | |
| 90 | def exercise(binary, mode): |
| 91 | output_r, output_w = os.pipe() |
| 92 | child = os.fork() |
| 93 | if child == 0: |
| 94 | try: |
| 95 | os.close(output_r) |
| 96 | os.dup2(output_w, 1) |
| 97 | os.close(output_w) |
| 98 | ptrace(0, 0) |
| 99 | os.kill(os.getpid(), signal.SIGSTOP) |
| 100 | os.execv(str(binary), [str(binary), chr(65 + mode)]) |
| 101 | finally: |
| 102 | os._exit(90) |
| 103 | os.close(output_w) |
| 104 | failures, records = [], [] |
| 105 | device = eventfd = -1 |
| 106 | injected = writes = closes = 0 |
| 107 | active = True |
| 108 | call = request = fd = address = 0 |
| 109 | incoming = b'' |
| 110 | try: |
| 111 | _, status = os.waitpid(child, 0) |
| 112 | if not os.WIFSTOPPED(status): |
| 113 | raise AssertionError(('initial stop', status)) |
| 114 | ptrace(0x4200, child, 0, 1 | (1 << 20)) |
| 115 | while True: |
| 116 | ptrace(24, child) |
| 117 | _, status = os.waitpid(child, 0) |
| 118 | if os.WIFEXITED(status): |
| 119 | rc = os.WEXITSTATUS(status) |
| 120 | break |
| 121 | if not os.WIFSTOPPED(status): |
| 122 | raise AssertionError(('unexpected status', status)) |
| 123 | sig = os.WSTOPSIG(status) |
| 124 | if sig == signal.SIGTRAP: |
| 125 | continue |
| 126 | if sig != (signal.SIGTRAP | 0x80): |
| 127 | raise AssertionError(('unexpected signal', sig)) |
| 128 | regs = registers(child) |
| 129 | if active: |
| 130 | call, fd, request, address = regs.r[8], regs.r[0], regs.r[1], regs.r[2] |
| 131 | incoming = b'' |
| 132 | if call == 19 and (fd != 0 or request != (os.O_CLOEXEC | os.O_NONBLOCK)): |
| 133 | failures.append('eventfd initial/flags') |
| 134 | if call == 29: |
| 135 | if request == 15210: |
| 136 | device = fd |
| 137 | if fetch(child, address, len(b'model-device\0')) != b'model-device\0': |
| 138 | failures.append('GET_DEVICE_FD name') |
| 139 | elif request in (15212, 15213, 15214): |
| 140 | if fd != device: |
| 141 | failures.append('ioctl device provenance') |
| 142 | size = struct.unpack('<I', fetch(child, address, 4))[0] |
| 143 | incoming = fetch(child, address, size) |
| 144 | records.append((request, incoming)) |
| 145 | if request == 15212 and struct.unpack('<IIIIQQ', incoming) != (32, 0, 0, 0, 0, 0): |
| 146 | failures.append('GET_REGION request') |
| 147 | if request == 15213 and struct.unpack('<IIII', incoming) != (16, 0, 0, 0): |
| 148 | failures.append('GET_IRQ_INFO request') |
| 149 | if request == 15214: |
| 150 | argsz, flags, index, start, count = struct.unpack('<IIIII', incoming[:20]) |
| 151 | wanted_start, wanted_count = (0, 64) if mode == 12 else (3, 1) |
| 152 | if index != 0: |
| 153 | failures.append('IRQ index') |
| 154 | if flags == 36: |
| 155 | if (argsz, start, count) != (20 + 4 * wanted_count, wanted_start, wanted_count): |
| 156 | failures.append('bind range/size') |
| 157 | descriptors = struct.unpack('<' + 'i' * count, incoming[20:]) |
| 158 | if any(value != eventfd for value in descriptors): |
| 159 | failures.append('bind actual owned eventfd') |
| 160 | elif flags == 33: |
| 161 | if (argsz, start, count) != (20, 0, 0): |
| 162 | failures.append('disable shape') |
| 163 | elif flags in (9, 17): |
| 164 | if (argsz, start, count) != (20, wanted_start, wanted_count): |
| 165 | failures.append('mask/unmask range') |
| 166 | else: |
| 167 | failures.append('unexpected IRQ flags') |
| 168 | else: |
| 169 | failures.append('unexpected ioctl') |
| 170 | if call == 64 and fd == eventfd: |
| 171 | writes += 1 |
| 172 | if address != 8 or fetch(child, request, 8) != struct.pack('<Q', 1): |
| 173 | failures.append('cancellation wake payload') |
| 174 | if call == 57 and fd == eventfd: |
| 175 | closes += 1 |
| 176 | else: |
| 177 | result = C.c_longlong(regs.r[0]).value |
| 178 | replacement = None |
| 179 | if call == 19 and result >= 0: |
| 180 | eventfd = result |
| 181 | if call == 29: |
| 182 | if result != -errno.ENOTTY: |
| 183 | failures.append('VFIO fixture must be memfd ENOTTY') |
| 184 | if request == 15210: |
| 185 | replacement = device |
| 186 | injected += 1 |
| 187 | elif request == 15212: |
| 188 | flags = 3 if mode == 10 else 7 |
| 189 | size = 2048 if mode == 9 else 4096 |
| 190 | store(child, address, struct.pack('<IIIIQQ', 32, flags, 0, 0, size, 0)) |
| 191 | replacement = 0 |
| 192 | elif request == 15213: |
| 193 | flags = {2: 0, 18: 1, 19: 2}.get(mode, 3) |
| 194 | size = 0 if mode == 17 else 16 |
| 195 | index = 1 if mode == 16 else 0 |
| 196 | count = 0 if mode == 3 else 64 |
| 197 | store(child, address, struct.pack('<IIII', size, flags, index, count)) |
| 198 | replacement = -5 if mode == 1 else 0 |
| 199 | elif request == 15214: |
| 200 | flags = struct.unpack('<I', incoming[4:8])[0] |
| 201 | fail = ((mode in (4, 11, 20) and flags == 36) or |
| 202 | (mode == 5 and flags == 9) or |
| 203 | (mode in (6, 11) and flags == 33) or |
| 204 | (mode == 8 and flags == 17)) |
| 205 | replacement = -5 if fail else 0 |
| 206 | if call == 64 and fd == eventfd and mode == 7 and writes == 2: |
| 207 | replacement = -5 |
| 208 | if call == 57 and fd == eventfd and mode == 20: |
| 209 | replacement = -5 |
| 210 | if replacement is not None: |
| 211 | regs.r[0] = C.c_ulonglong(replacement).value |
| 212 | setregisters(child, regs) |
| 213 | active = not active |
| 214 | data = os.read(output_r, 4096) |
| 215 | if rc != 0: |
| 216 | raise AssertionError(('fixture must terminate normally', mode, rc, data)) |
| 217 | if len(data) != 128: |
| 218 | raise AssertionError(('fixture output length', mode, len(data))) |
| 219 | observed = struct.unpack('<16Q', data) |
| 220 | if observed != expected(mode): |
| 221 | failures.append(('state/output', observed, expected(mode))) |
| 222 | if injected != 1: |
| 223 | failures.append(('GET_DEVICE_FD injection', injected)) |
| 224 | if closes != int(mode in (4, 5, 20)): |
| 225 | failures.append(('close exactly once and only after disable ACK', closes)) |
| 226 | if mode == 20 and closes != 1: |
| 227 | failures.append('ambiguous close was retried') |
| 228 | expected_flags = [] |
| 229 | if mode not in (1, 2, 3, 9, 10, 13, 14, 15, 16, 17, 18, 19): |
| 230 | expected_flags = [36] |
| 231 | if mode not in (4, 11, 20): |
| 232 | expected_flags += [9] |
| 233 | if mode in (4, 5, 11, 20): |
| 234 | expected_flags += [33] |
| 235 | elif mode not in (4, 5, 11, 20): |
| 236 | expected_flags += [17] |
| 237 | if mode != 8: |
| 238 | expected_flags += [33] |
| 239 | actual_flags = [struct.unpack('<I', b[4:8])[0] for req, b in records if req == 15214] |
| 240 | if actual_flags != expected_flags: |
| 241 | failures.append(('ordered IRQ effects', actual_flags, expected_flags)) |
| 242 | return failures, records |
| 243 | finally: |
| 244 | os.close(output_r) |
| 245 | try: |
| 246 | os.kill(child, signal.SIGKILL) |
| 247 | os.waitpid(child, 0) |
| 248 | except ProcessLookupError: |
| 249 | pass |
| 250 | except ChildProcessError: |
| 251 | pass |