1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * entry_from_vm86.c - tests kernel entries from vm86 mode 4 * Copyright (c) 2014-2015 Andrew Lutomirski 5 * 6 * This exercises a few paths that need to special-case vm86 mode. 7 */ 8 9 #define _GNU_SOURCE 10 11 #include <assert.h> 12 #include <stdlib.h> 13 #include <sys/syscall.h> 14 #include <sys/signal.h> 15 #include <sys/ucontext.h> 16 #include <unistd.h> 17 #include <stdio.h> 18 #include <string.h> 19 #include <inttypes.h> 20 #include <sys/mman.h> 21 #include <err.h> 22 #include <stddef.h> 23 #include <stdbool.h> 24 #include <errno.h> 25 #include <sys/vm86.h> 26 27 #include "helpers.h" 28 29 static unsigned long load_addr = 0x10000; 30 static int nerrs = 0; 31 32 static sig_atomic_t got_signal; 33 34 static void sighandler(int sig, siginfo_t *info, void *ctx_void) 35 { 36 ucontext_t *ctx = (ucontext_t*)ctx_void; 37 38 if (ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_VM || 39 (ctx->uc_mcontext.gregs[REG_CS] & 3) != 3) { 40 printf("[FAIL]\tSignal frame should not reflect vm86 mode\n"); 41 nerrs++; 42 } 43 44 const char *signame; 45 if (sig == SIGSEGV) 46 signame = "SIGSEGV"; 47 else if (sig == SIGILL) 48 signame = "SIGILL"; 49 else 50 signame = "unexpected signal"; 51 52 printf("[INFO]\t%s: FLAGS = 0x%lx, CS = 0x%hx\n", signame, 53 (unsigned long)ctx->uc_mcontext.gregs[REG_EFL], 54 (unsigned short)ctx->uc_mcontext.gregs[REG_CS]); 55 56 got_signal = 1; 57 } 58 59 asm ( 60 ".pushsection .rodata\n\t" 61 ".type vmcode_bound, @object\n\t" 62 "vmcode:\n\t" 63 "vmcode_bound:\n\t" 64 ".code16\n\t" 65 "bound %ax, (2048)\n\t" 66 "int3\n\t" 67 "vmcode_sysenter:\n\t" 68 "sysenter\n\t" 69 "vmcode_syscall:\n\t" 70 "syscall\n\t" 71 "vmcode_sti:\n\t" 72 "sti\n\t" 73 "vmcode_int3:\n\t" 74 "int3\n\t" 75 "vmcode_int80:\n\t" 76 "int $0x80\n\t" 77 "vmcode_popf_hlt:\n\t" 78 "push %ax\n\t" 79 "popf\n\t" 80 "hlt\n\t" 81 "vmcode_umip:\n\t" 82 /* addressing via displacements */ 83 "smsw (2052)\n\t" 84 "sidt (2054)\n\t" 85 "sgdt (2060)\n\t" 86 /* addressing via registers */ 87 "mov $2066, %bx\n\t" 88 "smsw (%bx)\n\t" 89 "mov $2068, %bx\n\t" 90 "sidt (%bx)\n\t" 91 "mov $2074, %bx\n\t" 92 "sgdt (%bx)\n\t" 93 /* register operands, only for smsw */ 94 "smsw %ax\n\t" 95 "mov %ax, (2080)\n\t" 96 "int3\n\t" 97 "vmcode_umip_str:\n\t" 98 "str %eax\n\t" 99 "vmcode_umip_sldt:\n\t" 100 "sldt %eax\n\t" 101 "int3\n\t" 102 ".size vmcode, . - vmcode\n\t" 103 "end_vmcode:\n\t" 104 ".code32\n\t" 105 ".popsection" 106 ); 107 108 extern unsigned char vmcode[], end_vmcode[]; 109 extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[], 110 vmcode_sti[], vmcode_int3[], vmcode_int80[], vmcode_popf_hlt[], 111 vmcode_umip[], vmcode_umip_str[], vmcode_umip_sldt[]; 112 113 /* Returns false if the test was skipped. */ 114 static bool do_test(struct vm86plus_struct *v86, unsigned long eip, 115 unsigned int rettype, unsigned int retarg, 116 const char *text) 117 { 118 long ret; 119 120 printf("[RUN]\t%s from vm86 mode\n", text); 121 v86->regs.eip = eip; 122 ret = vm86(VM86_ENTER, v86); 123 124 if (ret == -1 && (errno == ENOSYS || errno == EPERM)) { 125 printf("[SKIP]\tvm86 %s\n", 126 errno == ENOSYS ? "not supported" : "not allowed"); 127 return false; 128 } 129 130 if (VM86_TYPE(ret) == VM86_INTx) { 131 char trapname[32]; 132 int trapno = VM86_ARG(ret); 133 if (trapno == 13) 134 strcpy(trapname, "GP"); 135 else if (trapno == 5) 136 strcpy(trapname, "BR"); 137 else if (trapno == 14) 138 strcpy(trapname, "PF"); 139 else 140 sprintf(trapname, "%d", trapno); 141 142 printf("[INFO]\tExited vm86 mode due to #%s\n", trapname); 143 } else if (VM86_TYPE(ret) == VM86_UNKNOWN) { 144 printf("[INFO]\tExited vm86 mode due to unhandled GP fault\n"); 145 } else if (VM86_TYPE(ret) == VM86_TRAP) { 146 printf("[INFO]\tExited vm86 mode due to a trap (arg=%ld)\n", 147 VM86_ARG(ret)); 148 } else if (VM86_TYPE(ret) == VM86_SIGNAL) { 149 printf("[INFO]\tExited vm86 mode due to a signal\n"); 150 } else if (VM86_TYPE(ret) == VM86_STI) { 151 printf("[INFO]\tExited vm86 mode due to STI\n"); 152 } else { 153 printf("[INFO]\tExited vm86 mode due to type %ld, arg %ld\n", 154 VM86_TYPE(ret), VM86_ARG(ret)); 155 } 156 157 if (rettype == -1 || 158 (VM86_TYPE(ret) == rettype && VM86_ARG(ret) == retarg)) { 159 printf("[OK]\tReturned correctly\n"); 160 } else { 161 printf("[FAIL]\tIncorrect return reason (started at eip = 0x%lx, ended at eip = 0x%lx)\n", eip, v86->regs.eip); 162 nerrs++; 163 } 164 165 return true; 166 } 167 168 void do_umip_tests(struct vm86plus_struct *vm86, unsigned char *test_mem) 169 { 170 struct table_desc { 171 unsigned short limit; 172 unsigned long base; 173 } __attribute__((packed)); 174 175 /* Initialize variables with arbitrary values */ 176 struct table_desc gdt1 = { .base = 0x3c3c3c3c, .limit = 0x9999 }; 177 struct table_desc gdt2 = { .base = 0x1a1a1a1a, .limit = 0xaeae }; 178 struct table_desc idt1 = { .base = 0x7b7b7b7b, .limit = 0xf1f1 }; 179 struct table_desc idt2 = { .base = 0x89898989, .limit = 0x1313 }; 180 unsigned short msw1 = 0x1414, msw2 = 0x2525, msw3 = 3737; 181 182 /* UMIP -- exit with INT3 unless kernel emulation did not trap #GP */ 183 do_test(vm86, vmcode_umip - vmcode, VM86_TRAP, 3, "UMIP tests"); 184 185 /* Results from displacement-only addressing */ 186 msw1 = *(unsigned short *)(test_mem + 2052); 187 memcpy(&idt1, test_mem + 2054, sizeof(idt1)); 188 memcpy(&gdt1, test_mem + 2060, sizeof(gdt1)); 189 190 /* Results from register-indirect addressing */ 191 msw2 = *(unsigned short *)(test_mem + 2066); 192 memcpy(&idt2, test_mem + 2068, sizeof(idt2)); 193 memcpy(&gdt2, test_mem + 2074, sizeof(gdt2)); 194 195 /* Results when using register operands */ 196 msw3 = *(unsigned short *)(test_mem + 2080); 197 198 printf("[INFO]\tResult from SMSW:[0x%04x]\n", msw1); 199 printf("[INFO]\tResult from SIDT: limit[0x%04x]base[0x%08lx]\n", 200 idt1.limit, idt1.base); 201 printf("[INFO]\tResult from SGDT: limit[0x%04x]base[0x%08lx]\n", 202 gdt1.limit, gdt1.base); 203 204 if (msw1 != msw2 || msw1 != msw3) 205 printf("[FAIL]\tAll the results of SMSW should be the same.\n"); 206 else 207 printf("[PASS]\tAll the results from SMSW are identical.\n"); 208 209 if (memcmp(&gdt1, &gdt2, sizeof(gdt1))) 210 printf("[FAIL]\tAll the results of SGDT should be the same.\n"); 211 else 212 printf("[PASS]\tAll the results from SGDT are identical.\n"); 213 214 if (memcmp(&idt1, &idt2, sizeof(idt1))) 215 printf("[FAIL]\tAll the results of SIDT should be the same.\n"); 216 else 217 printf("[PASS]\tAll the results from SIDT are identical.\n"); 218 219 sethandler(SIGILL, sighandler, 0); 220 do_test(vm86, vmcode_umip_str - vmcode, VM86_SIGNAL, 0, 221 "STR instruction"); 222 clearhandler(SIGILL); 223 224 sethandler(SIGILL, sighandler, 0); 225 do_test(vm86, vmcode_umip_sldt - vmcode, VM86_SIGNAL, 0, 226 "SLDT instruction"); 227 clearhandler(SIGILL); 228 } 229 230 int main(void) 231 { 232 struct vm86plus_struct v86; 233 unsigned char *addr = mmap((void *)load_addr, 4096, 234 PROT_READ | PROT_WRITE | PROT_EXEC, 235 MAP_ANONYMOUS | MAP_PRIVATE, -1,0); 236 if (addr != (unsigned char *)load_addr) 237 err(1, "mmap"); 238 239 memcpy(addr, vmcode, end_vmcode - vmcode); 240 addr[2048] = 2; 241 addr[2050] = 3; 242 243 memset(&v86, 0, sizeof(v86)); 244 245 v86.regs.cs = load_addr / 16; 246 v86.regs.ss = load_addr / 16; 247 v86.regs.ds = load_addr / 16; 248 v86.regs.es = load_addr / 16; 249 250 /* Use the end of the page as our stack. */ 251 v86.regs.esp = 4096; 252 253 assert((v86.regs.cs & 3) == 0); /* Looks like RPL = 0 */ 254 255 /* #BR -- should deliver SIG??? */ 256 do_test(&v86, vmcode_bound - vmcode, VM86_INTx, 5, "#BR"); 257 258 /* 259 * SYSENTER -- should cause #GP or #UD depending on CPU. 260 * Expected return type -1 means that we shouldn't validate 261 * the vm86 return value. This will avoid problems on non-SEP 262 * CPUs. 263 */ 264 sethandler(SIGILL, sighandler, 0); 265 do_test(&v86, vmcode_sysenter - vmcode, -1, 0, "SYSENTER"); 266 clearhandler(SIGILL); 267 268 /* 269 * SYSCALL would be a disaster in VM86 mode. Fortunately, 270 * there is no kernel that both enables SYSCALL and sets 271 * EFER.SCE, so it's #UD on all systems. But vm86 is 272 * buggy (or has a "feature"), so the SIGILL will actually 273 * be delivered. 274 */ 275 sethandler(SIGILL, sighandler, 0); 276 do_test(&v86, vmcode_syscall - vmcode, VM86_SIGNAL, 0, "SYSCALL"); 277 clearhandler(SIGILL); 278 279 /* STI with VIP set */ 280 v86.regs.eflags |= X86_EFLAGS_VIP; 281 v86.regs.eflags &= ~X86_EFLAGS_IF; 282 do_test(&v86, vmcode_sti - vmcode, VM86_STI, 0, "STI with VIP set"); 283 284 /* POPF with VIP set but IF clear: should not trap */ 285 v86.regs.eflags = X86_EFLAGS_VIP; 286 v86.regs.eax = 0; 287 do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP set and IF clear"); 288 289 /* POPF with VIP set and IF set: should trap */ 290 v86.regs.eflags = X86_EFLAGS_VIP; 291 v86.regs.eax = X86_EFLAGS_IF; 292 do_test(&v86, vmcode_popf_hlt - vmcode, VM86_STI, 0, "POPF with VIP and IF set"); 293 294 /* POPF with VIP clear and IF set: should not trap */ 295 v86.regs.eflags = 0; 296 v86.regs.eax = X86_EFLAGS_IF; 297 do_test(&v86, vmcode_popf_hlt - vmcode, VM86_UNKNOWN, 0, "POPF with VIP clear and IF set"); 298 299 v86.regs.eflags = 0; 300 301 /* INT3 -- should cause #BP */ 302 do_test(&v86, vmcode_int3 - vmcode, VM86_TRAP, 3, "INT3"); 303 304 /* INT80 -- should exit with "INTx 0x80" */ 305 v86.regs.eax = (unsigned int)-1; 306 do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80"); 307 308 /* UMIP -- should exit with INTx 0x80 unless UMIP was not disabled */ 309 do_umip_tests(&v86, addr); 310 311 /* Execute a null pointer */ 312 v86.regs.cs = 0; 313 v86.regs.ss = 0; 314 sethandler(SIGSEGV, sighandler, 0); 315 got_signal = 0; 316 if (do_test(&v86, 0, VM86_SIGNAL, 0, "Execute null pointer") && 317 !got_signal) { 318 printf("[FAIL]\tDid not receive SIGSEGV\n"); 319 nerrs++; 320 } 321 clearhandler(SIGSEGV); 322 323 /* Make sure nothing explodes if we fork. */ 324 if (fork() == 0) 325 return 0; 326 327 return (nerrs == 0 ? 0 : 1); 328 } 329