2 * entry_from_vm86.c - tests kernel entries from vm86 mode
3 * Copyright (c) 2014-2015 Andrew Lutomirski
5 * This exercises a few paths that need to special-case vm86 mode.
14 #include <sys/syscall.h>
15 #include <sys/signal.h>
16 #include <sys/ucontext.h>
28 static unsigned long load_addr = 0x10000;
32 ".pushsection .rodata\n\t"
33 ".type vmcode_bound, @object\n\t"
37 "bound %ax, (2048)\n\t"
39 "vmcode_sysenter:\n\t"
41 ".size vmcode, . - vmcode\n\t"
47 extern unsigned char vmcode[], end_vmcode[];
48 extern unsigned char vmcode_bound[], vmcode_sysenter[];
50 static void do_test(struct vm86plus_struct *v86, unsigned long eip,
55 printf("[RUN]\t%s from vm86 mode\n", text);
57 ret = vm86(VM86_ENTER, v86);
59 if (ret == -1 && errno == ENOSYS) {
60 printf("[SKIP]\tvm86 not supported\n");
64 if (VM86_TYPE(ret) == VM86_INTx) {
66 int trapno = VM86_ARG(ret);
68 strcpy(trapname, "GP");
70 strcpy(trapname, "BR");
71 else if (trapno == 14)
72 strcpy(trapname, "PF");
74 sprintf(trapname, "%d", trapno);
76 printf("[OK]\tExited vm86 mode due to #%s\n", trapname);
77 } else if (VM86_TYPE(ret) == VM86_UNKNOWN) {
78 printf("[OK]\tExited vm86 mode due to unhandled GP fault\n");
80 printf("[OK]\tExited vm86 mode due to type %ld, arg %ld\n",
81 VM86_TYPE(ret), VM86_ARG(ret));
87 struct vm86plus_struct v86;
88 unsigned char *addr = mmap((void *)load_addr, 4096,
89 PROT_READ | PROT_WRITE | PROT_EXEC,
90 MAP_ANONYMOUS | MAP_PRIVATE, -1,0);
91 if (addr != (unsigned char *)load_addr)
94 memcpy(addr, vmcode, end_vmcode - vmcode);
98 memset(&v86, 0, sizeof(v86));
100 v86.regs.cs = load_addr / 16;
101 v86.regs.ss = load_addr / 16;
102 v86.regs.ds = load_addr / 16;
103 v86.regs.es = load_addr / 16;
105 assert((v86.regs.cs & 3) == 0); /* Looks like RPL = 0 */
107 /* #BR -- should deliver SIG??? */
108 do_test(&v86, vmcode_bound - vmcode, "#BR");
110 /* SYSENTER -- should cause #GP or #UD depending on CPU */
111 do_test(&v86, vmcode_sysenter - vmcode, "SYSENTER");
113 return (nerrs == 0 ? 0 : 1);