1 /* $OpenBSD: f00f.c,v 1.2 2018/05/21 23:34:20 bluhm Exp $ */ 2 /* 3 * Copyright (c) 2018 Alexander Bluhm <bluhm@genua.de> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 /* Test the Pentium f00f bug workaround by executing 0xF0,0x0F,0xC7,0xC8. */ 19 20 #include <err.h> 21 #include <signal.h> 22 #include <stdint.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 void 28 handler(int sig, siginfo_t *sip, void *ctx) 29 { 30 printf("signo %d, code %d, errno %d\n", 31 sip->si_signo, sip->si_code, sip->si_errno); 32 if (sig != SIGILL) 33 errx(1, "expected SIGILL: %d", sig); 34 printf("addr %p, trapno %d\n", sip->si_addr, sip->si_trapno); 35 printf("instruction %.2x %.2x %.2x %.2x\n", 36 ((uint8_t *)sip->si_addr)[0], 37 ((uint8_t *)sip->si_addr)[1], 38 ((uint8_t *)sip->si_addr)[2], 39 ((uint8_t *)sip->si_addr)[3]); 40 if (*((uint32_t *)sip->si_addr) != 0xc8c70ff0) 41 errx(1, "expected instrcution f0 0f c7 c8"); 42 43 exit(0); 44 } 45 46 int 47 main(int argc, char *argv[]) 48 { 49 struct sigaction sa; 50 uint64_t mem; 51 uint32_t eax, ebx, ecx, edx; 52 53 mem = 0x0123456789abcdefLL; 54 eax = 0x11111111; 55 ebx = 0x22222222; 56 ecx = 0x33333333; 57 edx = 0x44444444; 58 59 /* First check that the cmpxchg8b instruction works as expected. */ 60 printf("mem %.16llx, edx:eax %.8x%8x, ecx:ebx %.8x%.8x\n", 61 mem, edx, eax, ecx, ebx); 62 printf("cmpxchg8b mem, mem != edx:eax\n"); 63 asm volatile ( 64 "lock cmpxchg8b %0" 65 : "+m" (mem), "+a" (eax), "+b" (ebx), "+c" (ecx), "+d" (edx)); 66 printf("mem %.16llx, edx:eax %.8x%8x, ecx:ebx %.8x%.8x\n", 67 mem, edx, eax, ecx, ebx); 68 69 if (mem != 0x0123456789abcdefLL) 70 errx(1, "expected mem 0x0123456789abcdef"); 71 if (edx != 0x01234567 || eax != 0x89abcdef) 72 errx(1, "expected edx:eax 0x0123456789abcdef"); 73 if (ecx != 0x33333333 || ebx != 0x22222222) 74 errx(1, "expected ecx:ebx 0x3333333322222222"); 75 76 printf("cmpxchg8b mem, mem == edx:eax\n"); 77 asm volatile ( 78 "lock cmpxchg8b %0" 79 : "+m" (mem), "+a" (eax), "+b" (ebx), "+c" (ecx), "+d" (edx)); 80 printf("mem %.16llx, edx:eax %.8x%8x, ecx:ebx %.8x%.8x\n", 81 mem, edx, eax, ecx, ebx); 82 83 if (mem != 0x3333333322222222LL) 84 errx(1, "expected mem 0x3333333322222222"); 85 if (edx != 0x01234567 || eax != 0x89abcdef) 86 errx(1, "expected edx:eax 0x0123456789abcdef"); 87 if (ecx != 0x33333333 || ebx != 0x22222222) 88 errx(1, "expected ecx:ebx 0x3333333322222222"); 89 90 /* An illegal instruction must be signalled to user land. */ 91 memset(&sa, 0 ,sizeof(sa)); 92 sa.sa_sigaction = handler; 93 sa.sa_flags = SA_SIGINFO; 94 if (sigaction(SIGILL, &sa, NULL) == -1) 95 err(2, "sigaction"); 96 97 /* Execute the cmpxchg8b instruction with invalid addressing mode. */ 98 printf("cmpxchg8b eax\n"); 99 asm volatile ( 100 ".byte 0xF0,0x0F,0xC7,0xC8" 101 : : : "%eax", "%ebx", "%ecx", "%edx"); 102 printf("mem %.16llx, edx:eax %.8x%8x, ecx:ebx %.8x%.8x\n", 103 mem, edx, eax, ecx, ebx); 104 105 /* Not reached, the processor hangs or the signal handler exits. */ 106 errx(1, "expected signal"); 107 } 108