GNU Linux-libre 4.9.297-gnu1
[releases.git] / tools / testing / selftests / x86 / ldt_gdt.c
1 /*
2  * ldt_gdt.c - Test cases for LDT and GDT access
3  * Copyright (c) 2015 Andrew Lutomirski
4  */
5
6 #define _GNU_SOURCE
7 #include <err.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <signal.h>
11 #include <setjmp.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <sys/syscall.h>
17 #include <asm/ldt.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <stdbool.h>
21 #include <pthread.h>
22 #include <sched.h>
23 #include <linux/futex.h>
24 #include <sys/mman.h>
25 #include <asm/prctl.h>
26 #include <sys/prctl.h>
27
28 #define AR_ACCESSED             (1<<8)
29
30 #define AR_TYPE_RODATA          (0 * (1<<9))
31 #define AR_TYPE_RWDATA          (1 * (1<<9))
32 #define AR_TYPE_RODATA_EXPDOWN  (2 * (1<<9))
33 #define AR_TYPE_RWDATA_EXPDOWN  (3 * (1<<9))
34 #define AR_TYPE_XOCODE          (4 * (1<<9))
35 #define AR_TYPE_XRCODE          (5 * (1<<9))
36 #define AR_TYPE_XOCODE_CONF     (6 * (1<<9))
37 #define AR_TYPE_XRCODE_CONF     (7 * (1<<9))
38
39 #define AR_DPL3                 (3 * (1<<13))
40
41 #define AR_S                    (1 << 12)
42 #define AR_P                    (1 << 15)
43 #define AR_AVL                  (1 << 20)
44 #define AR_L                    (1 << 21)
45 #define AR_DB                   (1 << 22)
46 #define AR_G                    (1 << 23)
47
48 #ifdef __x86_64__
49 # define INT80_CLOBBERS "r8", "r9", "r10", "r11"
50 #else
51 # define INT80_CLOBBERS
52 #endif
53
54 static int nerrs;
55
56 /* Points to an array of 1024 ints, each holding its own index. */
57 static const unsigned int *counter_page;
58 static struct user_desc *low_user_desc;
59 static struct user_desc *low_user_desc_clear;  /* Use to delete GDT entry */
60 static int gdt_entry_num;
61
62 static void check_invalid_segment(uint16_t index, int ldt)
63 {
64         uint32_t has_limit = 0, has_ar = 0, limit, ar;
65         uint32_t selector = (index << 3) | (ldt << 2) | 3;
66
67         asm ("lsl %[selector], %[limit]\n\t"
68              "jnz 1f\n\t"
69              "movl $1, %[has_limit]\n\t"
70              "1:"
71              : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
72              : [selector] "r" (selector));
73         asm ("larl %[selector], %[ar]\n\t"
74              "jnz 1f\n\t"
75              "movl $1, %[has_ar]\n\t"
76              "1:"
77              : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
78              : [selector] "r" (selector));
79
80         if (has_limit || has_ar) {
81                 printf("[FAIL]\t%s entry %hu is valid but should be invalid\n",
82                        (ldt ? "LDT" : "GDT"), index);
83                 nerrs++;
84         } else {
85                 printf("[OK]\t%s entry %hu is invalid\n",
86                        (ldt ? "LDT" : "GDT"), index);
87         }
88 }
89
90 static void check_valid_segment(uint16_t index, int ldt,
91                                 uint32_t expected_ar, uint32_t expected_limit,
92                                 bool verbose)
93 {
94         uint32_t has_limit = 0, has_ar = 0, limit, ar;
95         uint32_t selector = (index << 3) | (ldt << 2) | 3;
96
97         asm ("lsl %[selector], %[limit]\n\t"
98              "jnz 1f\n\t"
99              "movl $1, %[has_limit]\n\t"
100              "1:"
101              : [limit] "=r" (limit), [has_limit] "+rm" (has_limit)
102              : [selector] "r" (selector));
103         asm ("larl %[selector], %[ar]\n\t"
104              "jnz 1f\n\t"
105              "movl $1, %[has_ar]\n\t"
106              "1:"
107              : [ar] "=r" (ar), [has_ar] "+rm" (has_ar)
108              : [selector] "r" (selector));
109
110         if (!has_limit || !has_ar) {
111                 printf("[FAIL]\t%s entry %hu is invalid but should be valid\n",
112                        (ldt ? "LDT" : "GDT"), index);
113                 nerrs++;
114                 return;
115         }
116
117         if (ar != expected_ar) {
118                 printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n",
119                        (ldt ? "LDT" : "GDT"), index, ar, expected_ar);
120                 nerrs++;
121         } else if (limit != expected_limit) {
122                 printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n",
123                        (ldt ? "LDT" : "GDT"), index, limit, expected_limit);
124                 nerrs++;
125         } else if (verbose) {
126                 printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n",
127                        (ldt ? "LDT" : "GDT"), index, ar, limit);
128         }
129 }
130
131 static bool install_valid_mode(const struct user_desc *desc, uint32_t ar,
132                                bool oldmode)
133 {
134         int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
135                           desc, sizeof(*desc));
136         if (ret < -1)
137                 errno = -ret;
138         if (ret == 0) {
139                 uint32_t limit = desc->limit;
140                 if (desc->limit_in_pages)
141                         limit = (limit << 12) + 4095;
142                 check_valid_segment(desc->entry_number, 1, ar, limit, true);
143                 return true;
144         } else if (errno == ENOSYS) {
145                 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
146                 return false;
147         } else {
148                 if (desc->seg_32bit) {
149                         printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
150                                errno);
151                         nerrs++;
152                         return false;
153                 } else {
154                         printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
155                         return false;
156                 }
157         }
158 }
159
160 static bool install_valid(const struct user_desc *desc, uint32_t ar)
161 {
162         return install_valid_mode(desc, ar, false);
163 }
164
165 static void install_invalid(const struct user_desc *desc, bool oldmode)
166 {
167         int ret = syscall(SYS_modify_ldt, oldmode ? 1 : 0x11,
168                           desc, sizeof(*desc));
169         if (ret < -1)
170                 errno = -ret;
171         if (ret == 0) {
172                 check_invalid_segment(desc->entry_number, 1);
173         } else if (errno == ENOSYS) {
174                 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
175         } else {
176                 if (desc->seg_32bit) {
177                         printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
178                                errno);
179                         nerrs++;
180                 } else {
181                         printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
182                 }
183         }
184 }
185
186 static int safe_modify_ldt(int func, struct user_desc *ptr,
187                            unsigned long bytecount)
188 {
189         int ret = syscall(SYS_modify_ldt, 0x11, ptr, bytecount);
190         if (ret < -1)
191                 errno = -ret;
192         return ret;
193 }
194
195 static void fail_install(struct user_desc *desc)
196 {
197         if (safe_modify_ldt(0x11, desc, sizeof(*desc)) == 0) {
198                 printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n");
199                 nerrs++;
200         } else if (errno == ENOSYS) {
201                 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
202         } else {
203                 printf("[OK]\tmodify_ldt failure %d\n", errno);
204         }
205 }
206
207 static void do_simple_tests(void)
208 {
209         struct user_desc desc = {
210                 .entry_number    = 0,
211                 .base_addr       = 0,
212                 .limit           = 10,
213                 .seg_32bit       = 1,
214                 .contents        = 2, /* Code, not conforming */
215                 .read_exec_only  = 0,
216                 .limit_in_pages  = 0,
217                 .seg_not_present = 0,
218                 .useable         = 0
219         };
220         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
221
222         desc.limit_in_pages = 1;
223         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
224                       AR_S | AR_P | AR_DB | AR_G);
225
226         check_invalid_segment(1, 1);
227
228         desc.entry_number = 2;
229         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
230                       AR_S | AR_P | AR_DB | AR_G);
231
232         check_invalid_segment(1, 1);
233
234         desc.base_addr = 0xf0000000;
235         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
236                       AR_S | AR_P | AR_DB | AR_G);
237
238         desc.useable = 1;
239         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
240                       AR_S | AR_P | AR_DB | AR_G | AR_AVL);
241
242         desc.seg_not_present = 1;
243         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
244                       AR_S | AR_DB | AR_G | AR_AVL);
245
246         desc.seg_32bit = 0;
247         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
248                       AR_S | AR_G | AR_AVL);
249
250         desc.seg_32bit = 1;
251         desc.contents = 0;
252         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA |
253                       AR_S | AR_DB | AR_G | AR_AVL);
254
255         desc.read_exec_only = 1;
256         install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA |
257                       AR_S | AR_DB | AR_G | AR_AVL);
258
259         desc.contents = 1;
260         install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN |
261                       AR_S | AR_DB | AR_G | AR_AVL);
262
263         desc.read_exec_only = 0;
264         desc.limit_in_pages = 0;
265         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN |
266                       AR_S | AR_DB | AR_AVL);
267
268         desc.contents = 3;
269         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE_CONF |
270                       AR_S | AR_DB | AR_AVL);
271
272         desc.read_exec_only = 1;
273         install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE_CONF |
274                       AR_S | AR_DB | AR_AVL);
275
276         desc.read_exec_only = 0;
277         desc.contents = 2;
278         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE |
279                       AR_S | AR_DB | AR_AVL);
280
281         desc.read_exec_only = 1;
282
283 #ifdef __x86_64__
284         desc.lm = 1;
285         install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
286                       AR_S | AR_DB | AR_AVL);
287         desc.lm = 0;
288 #endif
289
290         bool entry1_okay = install_valid(&desc, AR_DPL3 | AR_TYPE_XOCODE |
291                                          AR_S | AR_DB | AR_AVL);
292
293         if (entry1_okay) {
294                 printf("[RUN]\tTest fork\n");
295                 pid_t child = fork();
296                 if (child == 0) {
297                         nerrs = 0;
298                         check_valid_segment(desc.entry_number, 1,
299                                             AR_DPL3 | AR_TYPE_XOCODE |
300                                             AR_S | AR_DB | AR_AVL, desc.limit,
301                                             true);
302                         check_invalid_segment(1, 1);
303                         exit(nerrs ? 1 : 0);
304                 } else {
305                         int status;
306                         if (waitpid(child, &status, 0) != child ||
307                             !WIFEXITED(status)) {
308                                 printf("[FAIL]\tChild died\n");
309                                 nerrs++;
310                         } else if (WEXITSTATUS(status) != 0) {
311                                 printf("[FAIL]\tChild failed\n");
312                                 nerrs++;
313                         } else {
314                                 printf("[OK]\tChild succeeded\n");
315                         }
316                 }
317
318                 printf("[RUN]\tTest size\n");
319                 int i;
320                 for (i = 0; i < 8192; i++) {
321                         desc.entry_number = i;
322                         desc.limit = i;
323                         if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
324                                 printf("[FAIL]\tFailed to install entry %d\n", i);
325                                 nerrs++;
326                                 break;
327                         }
328                 }
329                 for (int j = 0; j < i; j++) {
330                         check_valid_segment(j, 1, AR_DPL3 | AR_TYPE_XOCODE |
331                                             AR_S | AR_DB | AR_AVL, j, false);
332                 }
333                 printf("[DONE]\tSize test\n");
334         } else {
335                 printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n");
336         }
337
338         /* Test entry_number too high. */
339         desc.entry_number = 8192;
340         fail_install(&desc);
341
342         /* Test deletion and actions mistakeable for deletion. */
343         memset(&desc, 0, sizeof(desc));
344         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P);
345
346         desc.seg_not_present = 1;
347         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
348
349         desc.seg_not_present = 0;
350         desc.read_exec_only = 1;
351         install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P);
352
353         desc.read_exec_only = 0;
354         desc.seg_not_present = 1;
355         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S);
356
357         desc.read_exec_only = 1;
358         desc.limit = 1;
359         install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
360
361         desc.limit = 0;
362         desc.base_addr = 1;
363         install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S);
364
365         desc.base_addr = 0;
366         install_invalid(&desc, false);
367
368         desc.seg_not_present = 0;
369         desc.seg_32bit = 1;
370         desc.read_exec_only = 0;
371         desc.limit = 0xfffff;
372
373         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB);
374
375         desc.limit_in_pages = 1;
376
377         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA | AR_S | AR_P | AR_DB | AR_G);
378         desc.read_exec_only = 1;
379         install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA | AR_S | AR_P | AR_DB | AR_G);
380         desc.contents = 1;
381         desc.read_exec_only = 0;
382         install_valid(&desc, AR_DPL3 | AR_TYPE_RWDATA_EXPDOWN | AR_S | AR_P | AR_DB | AR_G);
383         desc.read_exec_only = 1;
384         install_valid(&desc, AR_DPL3 | AR_TYPE_RODATA_EXPDOWN | AR_S | AR_P | AR_DB | AR_G);
385
386         desc.limit = 0;
387         install_invalid(&desc, true);
388 }
389
390 /*
391  * 0: thread is idle
392  * 1: thread armed
393  * 2: thread should clear LDT entry 0
394  * 3: thread should exit
395  */
396 static volatile unsigned int ftx;
397
398 static void *threadproc(void *ctx)
399 {
400         cpu_set_t cpuset;
401         CPU_ZERO(&cpuset);
402         CPU_SET(1, &cpuset);
403         if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
404                 err(1, "sched_setaffinity to CPU 1");   /* should never fail */
405
406         while (1) {
407                 syscall(SYS_futex, &ftx, FUTEX_WAIT, 0, NULL, NULL, 0);
408                 while (ftx != 2) {
409                         if (ftx >= 3)
410                                 return NULL;
411                 }
412
413                 /* clear LDT entry 0 */
414                 const struct user_desc desc = {};
415                 if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) != 0)
416                         err(1, "modify_ldt");
417
418                 /* If ftx == 2, set it to zero.  If ftx == 100, quit. */
419                 unsigned int x = -2;
420                 asm volatile ("lock xaddl %[x], %[ftx]" :
421                               [x] "+r" (x), [ftx] "+m" (ftx));
422                 if (x != 2)
423                         return NULL;
424         }
425 }
426
427 #ifdef __i386__
428
429 #ifndef SA_RESTORE
430 #define SA_RESTORER 0x04000000
431 #endif
432
433 /*
434  * The UAPI header calls this 'struct sigaction', which conflicts with
435  * glibc.  Sigh.
436  */
437 struct fake_ksigaction {
438         void *handler;  /* the real type is nasty */
439         unsigned long sa_flags;
440         void (*sa_restorer)(void);
441         unsigned char sigset[8];
442 };
443
444 static void fix_sa_restorer(int sig)
445 {
446         struct fake_ksigaction ksa;
447
448         if (syscall(SYS_rt_sigaction, sig, NULL, &ksa, 8) == 0) {
449                 /*
450                  * glibc has a nasty bug: it sometimes writes garbage to
451                  * sa_restorer.  This interacts quite badly with anything
452                  * that fiddles with SS because it can trigger legacy
453                  * stack switching.  Patch it up.  See:
454                  *
455                  * https://sourceware.org/bugzilla/show_bug.cgi?id=21269
456                  */
457                 if (!(ksa.sa_flags & SA_RESTORER) && ksa.sa_restorer) {
458                         ksa.sa_restorer = NULL;
459                         if (syscall(SYS_rt_sigaction, sig, &ksa, NULL,
460                                     sizeof(ksa.sigset)) != 0)
461                                 err(1, "rt_sigaction");
462                 }
463         }
464 }
465 #else
466 static void fix_sa_restorer(int sig)
467 {
468         /* 64-bit glibc works fine. */
469 }
470 #endif
471
472 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
473                        int flags)
474 {
475         struct sigaction sa;
476         memset(&sa, 0, sizeof(sa));
477         sa.sa_sigaction = handler;
478         sa.sa_flags = SA_SIGINFO | flags;
479         sigemptyset(&sa.sa_mask);
480         if (sigaction(sig, &sa, 0))
481                 err(1, "sigaction");
482
483         fix_sa_restorer(sig);
484 }
485
486 static jmp_buf jmpbuf;
487
488 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
489 {
490         siglongjmp(jmpbuf, 1);
491 }
492
493 static void do_multicpu_tests(void)
494 {
495         cpu_set_t cpuset;
496         pthread_t thread;
497         int failures = 0, iters = 5, i;
498         unsigned short orig_ss;
499
500         CPU_ZERO(&cpuset);
501         CPU_SET(1, &cpuset);
502         if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
503                 printf("[SKIP]\tCannot set affinity to CPU 1\n");
504                 return;
505         }
506
507         CPU_ZERO(&cpuset);
508         CPU_SET(0, &cpuset);
509         if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
510                 printf("[SKIP]\tCannot set affinity to CPU 0\n");
511                 return;
512         }
513
514         sethandler(SIGSEGV, sigsegv, 0);
515 #ifdef __i386__
516         /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
517         sethandler(SIGILL, sigsegv, 0);
518 #endif
519
520         printf("[RUN]\tCross-CPU LDT invalidation\n");
521
522         if (pthread_create(&thread, 0, threadproc, 0) != 0)
523                 err(1, "pthread_create");
524
525         asm volatile ("mov %%ss, %0" : "=rm" (orig_ss));
526
527         for (i = 0; i < 5; i++) {
528                 if (sigsetjmp(jmpbuf, 1) != 0)
529                         continue;
530
531                 /* Make sure the thread is ready after the last test. */
532                 while (ftx != 0)
533                         ;
534
535                 struct user_desc desc = {
536                         .entry_number    = 0,
537                         .base_addr       = 0,
538                         .limit           = 0xfffff,
539                         .seg_32bit       = 1,
540                         .contents        = 0, /* Data */
541                         .read_exec_only  = 0,
542                         .limit_in_pages  = 1,
543                         .seg_not_present = 0,
544                         .useable         = 0
545                 };
546
547                 if (safe_modify_ldt(0x11, &desc, sizeof(desc)) != 0) {
548                         if (errno != ENOSYS)
549                                 err(1, "modify_ldt");
550                         printf("[SKIP]\tmodify_ldt unavailable\n");
551                         break;
552                 }
553
554                 /* Arm the thread. */
555                 ftx = 1;
556                 syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
557
558                 asm volatile ("mov %0, %%ss" : : "r" (0x7));
559
560                 /* Go! */
561                 ftx = 2;
562
563                 while (ftx != 0)
564                         ;
565
566                 /*
567                  * On success, modify_ldt will segfault us synchronously,
568                  * and we'll escape via siglongjmp.
569                  */
570
571                 failures++;
572                 asm volatile ("mov %0, %%ss" : : "rm" (orig_ss));
573         };
574
575         ftx = 100;  /* Kill the thread. */
576         syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
577
578         if (pthread_join(thread, NULL) != 0)
579                 err(1, "pthread_join");
580
581         if (failures) {
582                 printf("[FAIL]\t%d of %d iterations failed\n", failures, iters);
583                 nerrs++;
584         } else {
585                 printf("[OK]\tAll %d iterations succeeded\n", iters);
586         }
587 }
588
589 static int finish_exec_test(void)
590 {
591         /*
592          * In a sensible world, this would be check_invalid_segment(0, 1);
593          * For better or for worse, though, the LDT is inherited across exec.
594          * We can probably change this safely, but for now we test it.
595          */
596         check_valid_segment(0, 1,
597                             AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB,
598                             42, true);
599
600         return nerrs ? 1 : 0;
601 }
602
603 static void do_exec_test(void)
604 {
605         printf("[RUN]\tTest exec\n");
606
607         struct user_desc desc = {
608                 .entry_number    = 0,
609                 .base_addr       = 0,
610                 .limit           = 42,
611                 .seg_32bit       = 1,
612                 .contents        = 2, /* Code, not conforming */
613                 .read_exec_only  = 0,
614                 .limit_in_pages  = 0,
615                 .seg_not_present = 0,
616                 .useable         = 0
617         };
618         install_valid(&desc, AR_DPL3 | AR_TYPE_XRCODE | AR_S | AR_P | AR_DB);
619
620         pid_t child = fork();
621         if (child == 0) {
622                 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL);
623                 printf("[FAIL]\tCould not exec self\n");
624                 exit(1);        /* exec failed */
625         } else {
626                 int status;
627                 if (waitpid(child, &status, 0) != child ||
628                     !WIFEXITED(status)) {
629                         printf("[FAIL]\tChild died\n");
630                         nerrs++;
631                 } else if (WEXITSTATUS(status) != 0) {
632                         printf("[FAIL]\tChild failed\n");
633                         nerrs++;
634                 } else {
635                         printf("[OK]\tChild succeeded\n");
636                 }
637         }
638 }
639
640 static void setup_counter_page(void)
641 {
642         unsigned int *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
643                          MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
644         if (page == MAP_FAILED)
645                 err(1, "mmap");
646
647         for (int i = 0; i < 1024; i++)
648                 page[i] = i;
649         counter_page = page;
650 }
651
652 static int invoke_set_thread_area(void)
653 {
654         int ret;
655         asm volatile ("int $0x80"
656                       : "=a" (ret), "+m" (low_user_desc) :
657                         "a" (243), "b" (low_user_desc)
658                       : INT80_CLOBBERS);
659         return ret;
660 }
661
662 static void setup_low_user_desc(void)
663 {
664         low_user_desc = mmap(NULL, 2 * sizeof(struct user_desc),
665                              PROT_READ | PROT_WRITE,
666                              MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
667         if (low_user_desc == MAP_FAILED)
668                 err(1, "mmap");
669
670         low_user_desc->entry_number     = -1;
671         low_user_desc->base_addr        = (unsigned long)&counter_page[1];
672         low_user_desc->limit            = 0xfffff;
673         low_user_desc->seg_32bit        = 1;
674         low_user_desc->contents         = 0; /* Data, grow-up*/
675         low_user_desc->read_exec_only   = 0;
676         low_user_desc->limit_in_pages   = 1;
677         low_user_desc->seg_not_present  = 0;
678         low_user_desc->useable          = 0;
679
680         if (invoke_set_thread_area() == 0) {
681                 gdt_entry_num = low_user_desc->entry_number;
682                 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num);
683         } else {
684                 printf("[NOTE]\tset_thread_area is unavailable\n");
685         }
686
687         low_user_desc_clear = low_user_desc + 1;
688         low_user_desc_clear->entry_number = gdt_entry_num;
689         low_user_desc_clear->read_exec_only = 1;
690         low_user_desc_clear->seg_not_present = 1;
691 }
692
693 static void test_gdt_invalidation(void)
694 {
695         if (!gdt_entry_num)
696                 return; /* 64-bit only system -- we can't use set_thread_area */
697
698         unsigned short prev_sel;
699         unsigned short sel;
700         unsigned int eax;
701         const char *result;
702 #ifdef __x86_64__
703         unsigned long saved_base;
704         unsigned long new_base;
705 #endif
706
707         /* Test DS */
708         invoke_set_thread_area();
709         eax = 243;
710         sel = (gdt_entry_num << 3) | 3;
711         asm volatile ("movw %%ds, %[prev_sel]\n\t"
712                       "movw %[sel], %%ds\n\t"
713 #ifdef __i386__
714                       "pushl %%ebx\n\t"
715 #endif
716                       "movl %[arg1], %%ebx\n\t"
717                       "int $0x80\n\t"   /* Should invalidate ds */
718 #ifdef __i386__
719                       "popl %%ebx\n\t"
720 #endif
721                       "movw %%ds, %[sel]\n\t"
722                       "movw %[prev_sel], %%ds"
723                       : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
724                         "+a" (eax)
725                       : "m" (low_user_desc_clear),
726                         [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
727                       : INT80_CLOBBERS);
728
729         if (sel != 0) {
730                 result = "FAIL";
731                 nerrs++;
732         } else {
733                 result = "OK";
734         }
735         printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
736                result, sel);
737
738         /* Test ES */
739         invoke_set_thread_area();
740         eax = 243;
741         sel = (gdt_entry_num << 3) | 3;
742         asm volatile ("movw %%es, %[prev_sel]\n\t"
743                       "movw %[sel], %%es\n\t"
744 #ifdef __i386__
745                       "pushl %%ebx\n\t"
746 #endif
747                       "movl %[arg1], %%ebx\n\t"
748                       "int $0x80\n\t"   /* Should invalidate es */
749 #ifdef __i386__
750                       "popl %%ebx\n\t"
751 #endif
752                       "movw %%es, %[sel]\n\t"
753                       "movw %[prev_sel], %%es"
754                       : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
755                         "+a" (eax)
756                       : "m" (low_user_desc_clear),
757                         [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
758                       : INT80_CLOBBERS);
759
760         if (sel != 0) {
761                 result = "FAIL";
762                 nerrs++;
763         } else {
764                 result = "OK";
765         }
766         printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
767                result, sel);
768
769         /* Test FS */
770         invoke_set_thread_area();
771         eax = 243;
772         sel = (gdt_entry_num << 3) | 3;
773 #ifdef __x86_64__
774         syscall(SYS_arch_prctl, ARCH_GET_FS, &saved_base);
775 #endif
776         asm volatile ("movw %%fs, %[prev_sel]\n\t"
777                       "movw %[sel], %%fs\n\t"
778 #ifdef __i386__
779                       "pushl %%ebx\n\t"
780 #endif
781                       "movl %[arg1], %%ebx\n\t"
782                       "int $0x80\n\t"   /* Should invalidate fs */
783 #ifdef __i386__
784                       "popl %%ebx\n\t"
785 #endif
786                       "movw %%fs, %[sel]\n\t"
787                       : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
788                         "+a" (eax)
789                       : "m" (low_user_desc_clear),
790                         [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
791                       : INT80_CLOBBERS);
792
793 #ifdef __x86_64__
794         syscall(SYS_arch_prctl, ARCH_GET_FS, &new_base);
795 #endif
796
797         /* Restore FS/BASE for glibc */
798         asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel] "rm" (prev_sel));
799 #ifdef __x86_64__
800         if (saved_base)
801                 syscall(SYS_arch_prctl, ARCH_SET_FS, saved_base);
802 #endif
803
804         if (sel != 0) {
805                 result = "FAIL";
806                 nerrs++;
807         } else {
808                 result = "OK";
809         }
810         printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
811                result, sel);
812
813 #ifdef __x86_64__
814         if (sel == 0 && new_base != 0) {
815                 nerrs++;
816                 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base);
817         } else {
818                 printf("[OK]\tNew FSBASE was zero\n");
819         }
820 #endif
821
822         /* Test GS */
823         invoke_set_thread_area();
824         eax = 243;
825         sel = (gdt_entry_num << 3) | 3;
826 #ifdef __x86_64__
827         syscall(SYS_arch_prctl, ARCH_GET_GS, &saved_base);
828 #endif
829         asm volatile ("movw %%gs, %[prev_sel]\n\t"
830                       "movw %[sel], %%gs\n\t"
831 #ifdef __i386__
832                       "pushl %%ebx\n\t"
833 #endif
834                       "movl %[arg1], %%ebx\n\t"
835                       "int $0x80\n\t"   /* Should invalidate gs */
836 #ifdef __i386__
837                       "popl %%ebx\n\t"
838 #endif
839                       "movw %%gs, %[sel]\n\t"
840                       : [prev_sel] "=&r" (prev_sel), [sel] "+r" (sel),
841                         "+a" (eax)
842                       : "m" (low_user_desc_clear),
843                         [arg1] "r" ((unsigned int)(unsigned long)low_user_desc_clear)
844                       : INT80_CLOBBERS);
845
846 #ifdef __x86_64__
847         syscall(SYS_arch_prctl, ARCH_GET_GS, &new_base);
848 #endif
849
850         /* Restore GS/BASE for glibc */
851         asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel] "rm" (prev_sel));
852 #ifdef __x86_64__
853         if (saved_base)
854                 syscall(SYS_arch_prctl, ARCH_SET_GS, saved_base);
855 #endif
856
857         if (sel != 0) {
858                 result = "FAIL";
859                 nerrs++;
860         } else {
861                 result = "OK";
862         }
863         printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
864                result, sel);
865
866 #ifdef __x86_64__
867         if (sel == 0 && new_base != 0) {
868                 nerrs++;
869                 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base);
870         } else {
871                 printf("[OK]\tNew GSBASE was zero\n");
872         }
873 #endif
874 }
875
876 int main(int argc, char **argv)
877 {
878         if (argc == 1 && !strcmp(argv[0], "ldt_gdt_test_exec"))
879                 return finish_exec_test();
880
881         setup_counter_page();
882         setup_low_user_desc();
883
884         do_simple_tests();
885
886         do_multicpu_tests();
887
888         do_exec_test();
889
890         test_gdt_invalidation();
891
892         return nerrs ? 1 : 0;
893 }