]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/testing/selftests/x86/ldt_gdt.c
0304ffb714f272a5646ff8134abdae92f491c5ad
1 // SPDX-License-Identifier: GPL-2.0
3 * ldt_gdt.c - Test cases for LDT and GDT access
4 * Copyright (c) 2015 Andrew Lutomirski
17 #include <sys/syscall.h>
19 #include <sys/types.h>
24 #include <linux/futex.h>
26 #include <asm/prctl.h>
27 #include <sys/prctl.h>
29 #define AR_ACCESSED (1<<8)
31 #define AR_TYPE_RODATA (0 * (1<<9))
32 #define AR_TYPE_RWDATA (1 * (1<<9))
33 #define AR_TYPE_RODATA_EXPDOWN (2 * (1<<9))
34 #define AR_TYPE_RWDATA_EXPDOWN (3 * (1<<9))
35 #define AR_TYPE_XOCODE (4 * (1<<9))
36 #define AR_TYPE_XRCODE (5 * (1<<9))
37 #define AR_TYPE_XOCODE_CONF (6 * (1<<9))
38 #define AR_TYPE_XRCODE_CONF (7 * (1<<9))
40 #define AR_DPL3 (3 * (1<<13))
42 #define AR_S (1 << 12)
43 #define AR_P (1 << 15)
44 #define AR_AVL (1 << 20)
45 #define AR_L (1 << 21)
46 #define AR_DB (1 << 22)
47 #define AR_G (1 << 23)
50 # define INT80_CLOBBERS "r8", "r9", "r10", "r11"
52 # define INT80_CLOBBERS
57 /* Points to an array of 1024 ints, each holding its own index. */
58 static const unsigned int *counter_page
;
59 static struct user_desc
*low_user_desc
;
60 static struct user_desc
*low_user_desc_clear
; /* Use to delete GDT entry */
61 static int gdt_entry_num
;
63 static void check_invalid_segment(uint16_t index
, int ldt
)
65 uint32_t has_limit
= 0, has_ar
= 0, limit
, ar
;
66 uint32_t selector
= (index
<< 3) | (ldt
<< 2) | 3;
68 asm ("lsl %[selector], %[limit]\n\t"
70 "movl $1, %[has_limit]\n\t"
72 : [limit
] "=r" (limit
), [has_limit
] "+rm" (has_limit
)
73 : [selector
] "r" (selector
));
74 asm ("larl %[selector], %[ar]\n\t"
76 "movl $1, %[has_ar]\n\t"
78 : [ar
] "=r" (ar
), [has_ar
] "+rm" (has_ar
)
79 : [selector
] "r" (selector
));
81 if (has_limit
|| has_ar
) {
82 printf("[FAIL]\t%s entry %hu is valid but should be invalid\n",
83 (ldt
? "LDT" : "GDT"), index
);
86 printf("[OK]\t%s entry %hu is invalid\n",
87 (ldt
? "LDT" : "GDT"), index
);
91 static void check_valid_segment(uint16_t index
, int ldt
,
92 uint32_t expected_ar
, uint32_t expected_limit
,
95 uint32_t has_limit
= 0, has_ar
= 0, limit
, ar
;
96 uint32_t selector
= (index
<< 3) | (ldt
<< 2) | 3;
98 asm ("lsl %[selector], %[limit]\n\t"
100 "movl $1, %[has_limit]\n\t"
102 : [limit
] "=r" (limit
), [has_limit
] "+rm" (has_limit
)
103 : [selector
] "r" (selector
));
104 asm ("larl %[selector], %[ar]\n\t"
106 "movl $1, %[has_ar]\n\t"
108 : [ar
] "=r" (ar
), [has_ar
] "+rm" (has_ar
)
109 : [selector
] "r" (selector
));
111 if (!has_limit
|| !has_ar
) {
112 printf("[FAIL]\t%s entry %hu is invalid but should be valid\n",
113 (ldt
? "LDT" : "GDT"), index
);
118 /* The SDM says "bits 19:16 are undefined". Thanks. */
122 * NB: Different Linux versions do different things with the
123 * accessed bit in set_thread_area().
125 if (ar
!= expected_ar
&&
126 (ldt
|| ar
!= (expected_ar
| AR_ACCESSED
))) {
127 printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n",
128 (ldt
? "LDT" : "GDT"), index
, ar
, expected_ar
);
130 } else if (limit
!= expected_limit
) {
131 printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n",
132 (ldt
? "LDT" : "GDT"), index
, limit
, expected_limit
);
134 } else if (verbose
) {
135 printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n",
136 (ldt
? "LDT" : "GDT"), index
, ar
, limit
);
140 static bool install_valid_mode(const struct user_desc
*d
, uint32_t ar
,
141 bool oldmode
, bool ldt
)
143 struct user_desc desc
= *d
;
148 /* No point testing set_thread_area in a 64-bit build */
153 desc
.entry_number
= gdt_entry_num
;
155 ret
= syscall(SYS_set_thread_area
, &desc
);
157 ret
= syscall(SYS_modify_ldt
, oldmode
? 1 : 0x11,
158 &desc
, sizeof(desc
));
163 if (ret
!= 0 && errno
== ENOSYS
) {
164 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
170 uint32_t limit
= desc
.limit
;
171 if (desc
.limit_in_pages
)
172 limit
= (limit
<< 12) + 4095;
173 check_valid_segment(desc
.entry_number
, ldt
, ar
, limit
, true);
176 if (desc
.seg_32bit
) {
177 printf("[FAIL]\tUnexpected %s failure %d\n",
178 ldt
? "modify_ldt" : "set_thread_area",
183 printf("[OK]\t%s rejected 16 bit segment\n",
184 ldt
? "modify_ldt" : "set_thread_area");
190 static bool install_valid(const struct user_desc
*desc
, uint32_t ar
)
192 bool ret
= install_valid_mode(desc
, ar
, false, true);
194 if (desc
->contents
<= 1 && desc
->seg_32bit
&&
195 !desc
->seg_not_present
) {
196 /* Should work in the GDT, too. */
197 install_valid_mode(desc
, ar
, false, false);
203 static void install_invalid(const struct user_desc
*desc
, bool oldmode
)
205 int ret
= syscall(SYS_modify_ldt
, oldmode
? 1 : 0x11,
206 desc
, sizeof(*desc
));
210 check_invalid_segment(desc
->entry_number
, 1);
211 } else if (errno
== ENOSYS
) {
212 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
214 if (desc
->seg_32bit
) {
215 printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
219 printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
224 static int safe_modify_ldt(int func
, struct user_desc
*ptr
,
225 unsigned long bytecount
)
227 int ret
= syscall(SYS_modify_ldt
, 0x11, ptr
, bytecount
);
233 static void fail_install(struct user_desc
*desc
)
235 if (safe_modify_ldt(0x11, desc
, sizeof(*desc
)) == 0) {
236 printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n");
238 } else if (errno
== ENOSYS
) {
239 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
241 printf("[OK]\tmodify_ldt failure %d\n", errno
);
245 static void do_simple_tests(void)
247 struct user_desc desc
= {
252 .contents
= 2, /* Code, not conforming */
255 .seg_not_present
= 0,
258 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
260 desc
.limit_in_pages
= 1;
261 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
262 AR_S
| AR_P
| AR_DB
| AR_G
);
264 check_invalid_segment(1, 1);
266 desc
.entry_number
= 2;
267 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
268 AR_S
| AR_P
| AR_DB
| AR_G
);
270 check_invalid_segment(1, 1);
272 desc
.base_addr
= 0xf0000000;
273 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
274 AR_S
| AR_P
| AR_DB
| AR_G
);
277 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
278 AR_S
| AR_P
| AR_DB
| AR_G
| AR_AVL
);
280 desc
.seg_not_present
= 1;
281 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
282 AR_S
| AR_DB
| AR_G
| AR_AVL
);
285 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
286 AR_S
| AR_G
| AR_AVL
);
290 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
|
291 AR_S
| AR_DB
| AR_G
| AR_AVL
);
293 desc
.read_exec_only
= 1;
294 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
|
295 AR_S
| AR_DB
| AR_G
| AR_AVL
);
298 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA_EXPDOWN
|
299 AR_S
| AR_DB
| AR_G
| AR_AVL
);
301 desc
.read_exec_only
= 0;
302 desc
.limit_in_pages
= 0;
303 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA_EXPDOWN
|
304 AR_S
| AR_DB
| AR_AVL
);
307 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE_CONF
|
308 AR_S
| AR_DB
| AR_AVL
);
310 desc
.read_exec_only
= 1;
311 install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE_CONF
|
312 AR_S
| AR_DB
| AR_AVL
);
314 desc
.read_exec_only
= 0;
316 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
317 AR_S
| AR_DB
| AR_AVL
);
319 desc
.read_exec_only
= 1;
323 install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE
|
324 AR_S
| AR_DB
| AR_AVL
);
328 bool entry1_okay
= install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE
|
329 AR_S
| AR_DB
| AR_AVL
);
332 printf("[RUN]\tTest fork\n");
333 pid_t child
= fork();
336 check_valid_segment(desc
.entry_number
, 1,
337 AR_DPL3
| AR_TYPE_XOCODE
|
338 AR_S
| AR_DB
| AR_AVL
, desc
.limit
,
340 check_invalid_segment(1, 1);
344 if (waitpid(child
, &status
, 0) != child
||
345 !WIFEXITED(status
)) {
346 printf("[FAIL]\tChild died\n");
348 } else if (WEXITSTATUS(status
) != 0) {
349 printf("[FAIL]\tChild failed\n");
352 printf("[OK]\tChild succeeded\n");
356 printf("[RUN]\tTest size\n");
358 for (i
= 0; i
< 8192; i
++) {
359 desc
.entry_number
= i
;
361 if (safe_modify_ldt(0x11, &desc
, sizeof(desc
)) != 0) {
362 printf("[FAIL]\tFailed to install entry %d\n", i
);
367 for (int j
= 0; j
< i
; j
++) {
368 check_valid_segment(j
, 1, AR_DPL3
| AR_TYPE_XOCODE
|
369 AR_S
| AR_DB
| AR_AVL
, j
, false);
371 printf("[DONE]\tSize test\n");
373 printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n");
376 /* Test entry_number too high. */
377 desc
.entry_number
= 8192;
380 /* Test deletion and actions mistakeable for deletion. */
381 memset(&desc
, 0, sizeof(desc
));
382 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
);
384 desc
.seg_not_present
= 1;
385 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
);
387 desc
.seg_not_present
= 0;
388 desc
.read_exec_only
= 1;
389 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
| AR_P
);
391 desc
.read_exec_only
= 0;
392 desc
.seg_not_present
= 1;
393 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
);
395 desc
.read_exec_only
= 1;
397 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
);
401 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
);
404 install_invalid(&desc
, false);
406 desc
.seg_not_present
= 0;
408 desc
.read_exec_only
= 0;
409 desc
.limit
= 0xfffff;
411 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
| AR_DB
);
413 desc
.limit_in_pages
= 1;
415 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
| AR_DB
| AR_G
);
416 desc
.read_exec_only
= 1;
417 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
| AR_P
| AR_DB
| AR_G
);
419 desc
.read_exec_only
= 0;
420 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA_EXPDOWN
| AR_S
| AR_P
| AR_DB
| AR_G
);
421 desc
.read_exec_only
= 1;
422 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA_EXPDOWN
| AR_S
| AR_P
| AR_DB
| AR_G
);
425 install_invalid(&desc
, true);
431 * 2: thread should clear LDT entry 0
432 * 3: thread should exit
434 static volatile unsigned int ftx
;
436 static void *threadproc(void *ctx
)
441 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
442 err(1, "sched_setaffinity to CPU 1"); /* should never fail */
445 syscall(SYS_futex
, &ftx
, FUTEX_WAIT
, 0, NULL
, NULL
, 0);
451 /* clear LDT entry 0 */
452 const struct user_desc desc
= {};
453 if (syscall(SYS_modify_ldt
, 1, &desc
, sizeof(desc
)) != 0)
454 err(1, "modify_ldt");
456 /* If ftx == 2, set it to zero. If ftx == 100, quit. */
458 asm volatile ("lock xaddl %[x], %[ftx]" :
459 [x
] "+r" (x
), [ftx
] "+m" (ftx
));
468 #define SA_RESTORER 0x04000000
472 * The UAPI header calls this 'struct sigaction', which conflicts with
475 struct fake_ksigaction
{
476 void *handler
; /* the real type is nasty */
477 unsigned long sa_flags
;
478 void (*sa_restorer
)(void);
479 unsigned char sigset
[8];
482 static void fix_sa_restorer(int sig
)
484 struct fake_ksigaction ksa
;
486 if (syscall(SYS_rt_sigaction
, sig
, NULL
, &ksa
, 8) == 0) {
488 * glibc has a nasty bug: it sometimes writes garbage to
489 * sa_restorer. This interacts quite badly with anything
490 * that fiddles with SS because it can trigger legacy
491 * stack switching. Patch it up. See:
493 * https://sourceware.org/bugzilla/show_bug.cgi?id=21269
495 if (!(ksa
.sa_flags
& SA_RESTORER
) && ksa
.sa_restorer
) {
496 ksa
.sa_restorer
= NULL
;
497 if (syscall(SYS_rt_sigaction
, sig
, &ksa
, NULL
,
498 sizeof(ksa
.sigset
)) != 0)
499 err(1, "rt_sigaction");
504 static void fix_sa_restorer(int sig
)
506 /* 64-bit glibc works fine. */
510 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
514 memset(&sa
, 0, sizeof(sa
));
515 sa
.sa_sigaction
= handler
;
516 sa
.sa_flags
= SA_SIGINFO
| flags
;
517 sigemptyset(&sa
.sa_mask
);
518 if (sigaction(sig
, &sa
, 0))
521 fix_sa_restorer(sig
);
524 static jmp_buf jmpbuf
;
526 static void sigsegv(int sig
, siginfo_t
*info
, void *ctx_void
)
528 siglongjmp(jmpbuf
, 1);
531 static void do_multicpu_tests(void)
535 int failures
= 0, iters
= 5, i
;
536 unsigned short orig_ss
;
540 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
541 printf("[SKIP]\tCannot set affinity to CPU 1\n");
547 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
548 printf("[SKIP]\tCannot set affinity to CPU 0\n");
552 sethandler(SIGSEGV
, sigsegv
, 0);
554 /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
555 sethandler(SIGILL
, sigsegv
, 0);
558 printf("[RUN]\tCross-CPU LDT invalidation\n");
560 if (pthread_create(&thread
, 0, threadproc
, 0) != 0)
561 err(1, "pthread_create");
563 asm volatile ("mov %%ss, %0" : "=rm" (orig_ss
));
565 for (i
= 0; i
< 5; i
++) {
566 if (sigsetjmp(jmpbuf
, 1) != 0)
569 /* Make sure the thread is ready after the last test. */
573 struct user_desc desc
= {
578 .contents
= 0, /* Data */
581 .seg_not_present
= 0,
585 if (safe_modify_ldt(0x11, &desc
, sizeof(desc
)) != 0) {
587 err(1, "modify_ldt");
588 printf("[SKIP]\tmodify_ldt unavailable\n");
592 /* Arm the thread. */
594 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
596 asm volatile ("mov %0, %%ss" : : "r" (0x7));
605 * On success, modify_ldt will segfault us synchronously,
606 * and we'll escape via siglongjmp.
610 asm volatile ("mov %0, %%ss" : : "rm" (orig_ss
));
613 ftx
= 100; /* Kill the thread. */
614 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
616 if (pthread_join(thread
, NULL
) != 0)
617 err(1, "pthread_join");
620 printf("[FAIL]\t%d of %d iterations failed\n", failures
, iters
);
623 printf("[OK]\tAll %d iterations succeeded\n", iters
);
627 static int finish_exec_test(void)
630 * Older kernel versions did inherit the LDT on exec() which is
631 * wrong because exec() starts from a clean state.
633 check_invalid_segment(0, 1);
635 return nerrs
? 1 : 0;
638 static void do_exec_test(void)
640 printf("[RUN]\tTest exec\n");
642 struct user_desc desc
= {
647 .contents
= 2, /* Code, not conforming */
650 .seg_not_present
= 0,
653 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
655 pid_t child
= fork();
657 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL
);
658 printf("[FAIL]\tCould not exec self\n");
659 exit(1); /* exec failed */
662 if (waitpid(child
, &status
, 0) != child
||
663 !WIFEXITED(status
)) {
664 printf("[FAIL]\tChild died\n");
666 } else if (WEXITSTATUS(status
) != 0) {
667 printf("[FAIL]\tChild failed\n");
670 printf("[OK]\tChild succeeded\n");
675 static void setup_counter_page(void)
677 unsigned int *page
= mmap(NULL
, 4096, PROT_READ
| PROT_WRITE
,
678 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
679 if (page
== MAP_FAILED
)
682 for (int i
= 0; i
< 1024; i
++)
687 static int invoke_set_thread_area(void)
690 asm volatile ("int $0x80"
691 : "=a" (ret
), "+m" (low_user_desc
) :
692 "a" (243), "b" (low_user_desc
)
697 static void setup_low_user_desc(void)
699 low_user_desc
= mmap(NULL
, 2 * sizeof(struct user_desc
),
700 PROT_READ
| PROT_WRITE
,
701 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
702 if (low_user_desc
== MAP_FAILED
)
705 low_user_desc
->entry_number
= -1;
706 low_user_desc
->base_addr
= (unsigned long)&counter_page
[1];
707 low_user_desc
->limit
= 0xfffff;
708 low_user_desc
->seg_32bit
= 1;
709 low_user_desc
->contents
= 0; /* Data, grow-up*/
710 low_user_desc
->read_exec_only
= 0;
711 low_user_desc
->limit_in_pages
= 1;
712 low_user_desc
->seg_not_present
= 0;
713 low_user_desc
->useable
= 0;
715 if (invoke_set_thread_area() == 0) {
716 gdt_entry_num
= low_user_desc
->entry_number
;
717 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num
);
719 printf("[NOTE]\tset_thread_area is unavailable\n");
722 low_user_desc_clear
= low_user_desc
+ 1;
723 low_user_desc_clear
->entry_number
= gdt_entry_num
;
724 low_user_desc_clear
->read_exec_only
= 1;
725 low_user_desc_clear
->seg_not_present
= 1;
728 static void test_gdt_invalidation(void)
731 return; /* 64-bit only system -- we can't use set_thread_area */
733 unsigned short prev_sel
;
738 unsigned long saved_base
;
739 unsigned long new_base
;
743 invoke_set_thread_area();
745 sel
= (gdt_entry_num
<< 3) | 3;
746 asm volatile ("movw %%ds, %[prev_sel]\n\t"
747 "movw %[sel], %%ds\n\t"
751 "movl %[arg1], %%ebx\n\t"
752 "int $0x80\n\t" /* Should invalidate ds */
756 "movw %%ds, %[sel]\n\t"
757 "movw %[prev_sel], %%ds"
758 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
760 : "m" (low_user_desc_clear
),
761 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
770 printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
774 invoke_set_thread_area();
776 sel
= (gdt_entry_num
<< 3) | 3;
777 asm volatile ("movw %%es, %[prev_sel]\n\t"
778 "movw %[sel], %%es\n\t"
782 "movl %[arg1], %%ebx\n\t"
783 "int $0x80\n\t" /* Should invalidate es */
787 "movw %%es, %[sel]\n\t"
788 "movw %[prev_sel], %%es"
789 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
791 : "m" (low_user_desc_clear
),
792 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
801 printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
805 invoke_set_thread_area();
807 sel
= (gdt_entry_num
<< 3) | 3;
809 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &saved_base
);
811 asm volatile ("movw %%fs, %[prev_sel]\n\t"
812 "movw %[sel], %%fs\n\t"
816 "movl %[arg1], %%ebx\n\t"
817 "int $0x80\n\t" /* Should invalidate fs */
821 "movw %%fs, %[sel]\n\t"
822 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
824 : "m" (low_user_desc_clear
),
825 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
829 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &new_base
);
832 /* Restore FS/BASE for glibc */
833 asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel
] "rm" (prev_sel
));
836 syscall(SYS_arch_prctl
, ARCH_SET_FS
, saved_base
);
845 printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
849 if (sel
== 0 && new_base
!= 0) {
851 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base
);
853 printf("[OK]\tNew FSBASE was zero\n");
858 invoke_set_thread_area();
860 sel
= (gdt_entry_num
<< 3) | 3;
862 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &saved_base
);
864 asm volatile ("movw %%gs, %[prev_sel]\n\t"
865 "movw %[sel], %%gs\n\t"
869 "movl %[arg1], %%ebx\n\t"
870 "int $0x80\n\t" /* Should invalidate gs */
874 "movw %%gs, %[sel]\n\t"
875 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
877 : "m" (low_user_desc_clear
),
878 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
882 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &new_base
);
885 /* Restore GS/BASE for glibc */
886 asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel
] "rm" (prev_sel
));
889 syscall(SYS_arch_prctl
, ARCH_SET_GS
, saved_base
);
898 printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
902 if (sel
== 0 && new_base
!= 0) {
904 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base
);
906 printf("[OK]\tNew GSBASE was zero\n");
911 int main(int argc
, char **argv
)
913 if (argc
== 1 && !strcmp(argv
[0], "ldt_gdt_test_exec"))
914 return finish_exec_test();
916 setup_counter_page();
917 setup_low_user_desc();
925 test_gdt_invalidation();
927 return nerrs
? 1 : 0;