]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/testing/selftests/x86/ldt_gdt.c
2 * ldt_gdt.c - Test cases for LDT and GDT access
3 * Copyright (c) 2015 Andrew Lutomirski
16 #include <sys/syscall.h>
18 #include <sys/types.h>
23 #include <linux/futex.h>
25 #include <asm/prctl.h>
26 #include <sys/prctl.h>
28 #define AR_ACCESSED (1<<8)
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))
39 #define AR_DPL3 (3 * (1<<13))
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)
49 # define INT80_CLOBBERS "r8", "r9", "r10", "r11"
51 # define INT80_CLOBBERS
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
;
62 static void check_invalid_segment(uint16_t index
, int ldt
)
64 uint32_t has_limit
= 0, has_ar
= 0, limit
, ar
;
65 uint32_t selector
= (index
<< 3) | (ldt
<< 2) | 3;
67 asm ("lsl %[selector], %[limit]\n\t"
69 "movl $1, %[has_limit]\n\t"
71 : [limit
] "=r" (limit
), [has_limit
] "+rm" (has_limit
)
72 : [selector
] "r" (selector
));
73 asm ("larl %[selector], %[ar]\n\t"
75 "movl $1, %[has_ar]\n\t"
77 : [ar
] "=r" (ar
), [has_ar
] "+rm" (has_ar
)
78 : [selector
] "r" (selector
));
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
);
85 printf("[OK]\t%s entry %hu is invalid\n",
86 (ldt
? "LDT" : "GDT"), index
);
90 static void check_valid_segment(uint16_t index
, int ldt
,
91 uint32_t expected_ar
, uint32_t expected_limit
,
94 uint32_t has_limit
= 0, has_ar
= 0, limit
, ar
;
95 uint32_t selector
= (index
<< 3) | (ldt
<< 2) | 3;
97 asm ("lsl %[selector], %[limit]\n\t"
99 "movl $1, %[has_limit]\n\t"
101 : [limit
] "=r" (limit
), [has_limit
] "+rm" (has_limit
)
102 : [selector
] "r" (selector
));
103 asm ("larl %[selector], %[ar]\n\t"
105 "movl $1, %[has_ar]\n\t"
107 : [ar
] "=r" (ar
), [has_ar
] "+rm" (has_ar
)
108 : [selector
] "r" (selector
));
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
);
117 /* The SDM says "bits 19:16 are undefined". Thanks. */
121 * NB: Different Linux versions do different things with the
122 * accessed bit in set_thread_area().
124 if (ar
!= expected_ar
&&
125 (ldt
|| ar
!= (expected_ar
| AR_ACCESSED
))) {
126 printf("[FAIL]\t%s entry %hu has AR 0x%08X but expected 0x%08X\n",
127 (ldt
? "LDT" : "GDT"), index
, ar
, expected_ar
);
129 } else if (limit
!= expected_limit
) {
130 printf("[FAIL]\t%s entry %hu has limit 0x%08X but expected 0x%08X\n",
131 (ldt
? "LDT" : "GDT"), index
, limit
, expected_limit
);
133 } else if (verbose
) {
134 printf("[OK]\t%s entry %hu has AR 0x%08X and limit 0x%08X\n",
135 (ldt
? "LDT" : "GDT"), index
, ar
, limit
);
139 static bool install_valid_mode(const struct user_desc
*d
, uint32_t ar
,
140 bool oldmode
, bool ldt
)
142 struct user_desc desc
= *d
;
147 /* No point testing set_thread_area in a 64-bit build */
152 desc
.entry_number
= gdt_entry_num
;
154 ret
= syscall(SYS_set_thread_area
, &desc
);
156 ret
= syscall(SYS_modify_ldt
, oldmode
? 1 : 0x11,
157 &desc
, sizeof(desc
));
162 if (ret
!= 0 && errno
== ENOSYS
) {
163 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
169 uint32_t limit
= desc
.limit
;
170 if (desc
.limit_in_pages
)
171 limit
= (limit
<< 12) + 4095;
172 check_valid_segment(desc
.entry_number
, ldt
, ar
, limit
, true);
175 if (desc
.seg_32bit
) {
176 printf("[FAIL]\tUnexpected %s failure %d\n",
177 ldt
? "modify_ldt" : "set_thread_area",
182 printf("[OK]\t%s rejected 16 bit segment\n",
183 ldt
? "modify_ldt" : "set_thread_area");
189 static bool install_valid(const struct user_desc
*desc
, uint32_t ar
)
191 bool ret
= install_valid_mode(desc
, ar
, false, true);
193 if (desc
->contents
<= 1 && desc
->seg_32bit
&&
194 !desc
->seg_not_present
) {
195 /* Should work in the GDT, too. */
196 install_valid_mode(desc
, ar
, false, false);
202 static void install_invalid(const struct user_desc
*desc
, bool oldmode
)
204 int ret
= syscall(SYS_modify_ldt
, oldmode
? 1 : 0x11,
205 desc
, sizeof(*desc
));
209 check_invalid_segment(desc
->entry_number
, 1);
210 } else if (errno
== ENOSYS
) {
211 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
213 if (desc
->seg_32bit
) {
214 printf("[FAIL]\tUnexpected modify_ldt failure %d\n",
218 printf("[OK]\tmodify_ldt rejected 16 bit segment\n");
223 static int safe_modify_ldt(int func
, struct user_desc
*ptr
,
224 unsigned long bytecount
)
226 int ret
= syscall(SYS_modify_ldt
, 0x11, ptr
, bytecount
);
232 static void fail_install(struct user_desc
*desc
)
234 if (safe_modify_ldt(0x11, desc
, sizeof(*desc
)) == 0) {
235 printf("[FAIL]\tmodify_ldt accepted a bad descriptor\n");
237 } else if (errno
== ENOSYS
) {
238 printf("[OK]\tmodify_ldt returned -ENOSYS\n");
240 printf("[OK]\tmodify_ldt failure %d\n", errno
);
244 static void do_simple_tests(void)
246 struct user_desc desc
= {
251 .contents
= 2, /* Code, not conforming */
254 .seg_not_present
= 0,
257 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
259 desc
.limit_in_pages
= 1;
260 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
261 AR_S
| AR_P
| AR_DB
| AR_G
);
263 check_invalid_segment(1, 1);
265 desc
.entry_number
= 2;
266 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
267 AR_S
| AR_P
| AR_DB
| AR_G
);
269 check_invalid_segment(1, 1);
271 desc
.base_addr
= 0xf0000000;
272 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
273 AR_S
| AR_P
| AR_DB
| AR_G
);
276 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
277 AR_S
| AR_P
| AR_DB
| AR_G
| AR_AVL
);
279 desc
.seg_not_present
= 1;
280 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
281 AR_S
| AR_DB
| AR_G
| AR_AVL
);
284 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
285 AR_S
| AR_G
| AR_AVL
);
289 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
|
290 AR_S
| AR_DB
| AR_G
| AR_AVL
);
292 desc
.read_exec_only
= 1;
293 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
|
294 AR_S
| AR_DB
| AR_G
| AR_AVL
);
297 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA_EXPDOWN
|
298 AR_S
| AR_DB
| AR_G
| AR_AVL
);
300 desc
.read_exec_only
= 0;
301 desc
.limit_in_pages
= 0;
302 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA_EXPDOWN
|
303 AR_S
| AR_DB
| AR_AVL
);
306 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE_CONF
|
307 AR_S
| AR_DB
| AR_AVL
);
309 desc
.read_exec_only
= 1;
310 install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE_CONF
|
311 AR_S
| AR_DB
| AR_AVL
);
313 desc
.read_exec_only
= 0;
315 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
|
316 AR_S
| AR_DB
| AR_AVL
);
318 desc
.read_exec_only
= 1;
322 install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE
|
323 AR_S
| AR_DB
| AR_AVL
);
327 bool entry1_okay
= install_valid(&desc
, AR_DPL3
| AR_TYPE_XOCODE
|
328 AR_S
| AR_DB
| AR_AVL
);
331 printf("[RUN]\tTest fork\n");
332 pid_t child
= fork();
335 check_valid_segment(desc
.entry_number
, 1,
336 AR_DPL3
| AR_TYPE_XOCODE
|
337 AR_S
| AR_DB
| AR_AVL
, desc
.limit
,
339 check_invalid_segment(1, 1);
343 if (waitpid(child
, &status
, 0) != child
||
344 !WIFEXITED(status
)) {
345 printf("[FAIL]\tChild died\n");
347 } else if (WEXITSTATUS(status
) != 0) {
348 printf("[FAIL]\tChild failed\n");
351 printf("[OK]\tChild succeeded\n");
355 printf("[RUN]\tTest size\n");
357 for (i
= 0; i
< 8192; i
++) {
358 desc
.entry_number
= i
;
360 if (safe_modify_ldt(0x11, &desc
, sizeof(desc
)) != 0) {
361 printf("[FAIL]\tFailed to install entry %d\n", i
);
366 for (int j
= 0; j
< i
; j
++) {
367 check_valid_segment(j
, 1, AR_DPL3
| AR_TYPE_XOCODE
|
368 AR_S
| AR_DB
| AR_AVL
, j
, false);
370 printf("[DONE]\tSize test\n");
372 printf("[SKIP]\tSkipping fork and size tests because we have no LDT\n");
375 /* Test entry_number too high. */
376 desc
.entry_number
= 8192;
379 /* Test deletion and actions mistakeable for deletion. */
380 memset(&desc
, 0, sizeof(desc
));
381 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
);
383 desc
.seg_not_present
= 1;
384 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
);
386 desc
.seg_not_present
= 0;
387 desc
.read_exec_only
= 1;
388 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
| AR_P
);
390 desc
.read_exec_only
= 0;
391 desc
.seg_not_present
= 1;
392 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
);
394 desc
.read_exec_only
= 1;
396 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
);
400 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
);
403 install_invalid(&desc
, false);
405 desc
.seg_not_present
= 0;
407 desc
.read_exec_only
= 0;
408 desc
.limit
= 0xfffff;
410 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
| AR_DB
);
412 desc
.limit_in_pages
= 1;
414 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA
| AR_S
| AR_P
| AR_DB
| AR_G
);
415 desc
.read_exec_only
= 1;
416 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA
| AR_S
| AR_P
| AR_DB
| AR_G
);
418 desc
.read_exec_only
= 0;
419 install_valid(&desc
, AR_DPL3
| AR_TYPE_RWDATA_EXPDOWN
| AR_S
| AR_P
| AR_DB
| AR_G
);
420 desc
.read_exec_only
= 1;
421 install_valid(&desc
, AR_DPL3
| AR_TYPE_RODATA_EXPDOWN
| AR_S
| AR_P
| AR_DB
| AR_G
);
424 install_invalid(&desc
, true);
430 * 2: thread should clear LDT entry 0
431 * 3: thread should exit
433 static volatile unsigned int ftx
;
435 static void *threadproc(void *ctx
)
440 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
441 err(1, "sched_setaffinity to CPU 1"); /* should never fail */
444 syscall(SYS_futex
, &ftx
, FUTEX_WAIT
, 0, NULL
, NULL
, 0);
450 /* clear LDT entry 0 */
451 const struct user_desc desc
= {};
452 if (syscall(SYS_modify_ldt
, 1, &desc
, sizeof(desc
)) != 0)
453 err(1, "modify_ldt");
455 /* If ftx == 2, set it to zero. If ftx == 100, quit. */
457 asm volatile ("lock xaddl %[x], %[ftx]" :
458 [x
] "+r" (x
), [ftx
] "+m" (ftx
));
467 #define SA_RESTORER 0x04000000
471 * The UAPI header calls this 'struct sigaction', which conflicts with
474 struct fake_ksigaction
{
475 void *handler
; /* the real type is nasty */
476 unsigned long sa_flags
;
477 void (*sa_restorer
)(void);
478 unsigned char sigset
[8];
481 static void fix_sa_restorer(int sig
)
483 struct fake_ksigaction ksa
;
485 if (syscall(SYS_rt_sigaction
, sig
, NULL
, &ksa
, 8) == 0) {
487 * glibc has a nasty bug: it sometimes writes garbage to
488 * sa_restorer. This interacts quite badly with anything
489 * that fiddles with SS because it can trigger legacy
490 * stack switching. Patch it up. See:
492 * https://sourceware.org/bugzilla/show_bug.cgi?id=21269
494 if (!(ksa
.sa_flags
& SA_RESTORER
) && ksa
.sa_restorer
) {
495 ksa
.sa_restorer
= NULL
;
496 if (syscall(SYS_rt_sigaction
, sig
, &ksa
, NULL
,
497 sizeof(ksa
.sigset
)) != 0)
498 err(1, "rt_sigaction");
503 static void fix_sa_restorer(int sig
)
505 /* 64-bit glibc works fine. */
509 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
513 memset(&sa
, 0, sizeof(sa
));
514 sa
.sa_sigaction
= handler
;
515 sa
.sa_flags
= SA_SIGINFO
| flags
;
516 sigemptyset(&sa
.sa_mask
);
517 if (sigaction(sig
, &sa
, 0))
520 fix_sa_restorer(sig
);
523 static jmp_buf jmpbuf
;
525 static void sigsegv(int sig
, siginfo_t
*info
, void *ctx_void
)
527 siglongjmp(jmpbuf
, 1);
530 static void do_multicpu_tests(void)
534 int failures
= 0, iters
= 5, i
;
535 unsigned short orig_ss
;
539 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
540 printf("[SKIP]\tCannot set affinity to CPU 1\n");
546 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
547 printf("[SKIP]\tCannot set affinity to CPU 0\n");
551 sethandler(SIGSEGV
, sigsegv
, 0);
553 /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
554 sethandler(SIGILL
, sigsegv
, 0);
557 printf("[RUN]\tCross-CPU LDT invalidation\n");
559 if (pthread_create(&thread
, 0, threadproc
, 0) != 0)
560 err(1, "pthread_create");
562 asm volatile ("mov %%ss, %0" : "=rm" (orig_ss
));
564 for (i
= 0; i
< 5; i
++) {
565 if (sigsetjmp(jmpbuf
, 1) != 0)
568 /* Make sure the thread is ready after the last test. */
572 struct user_desc desc
= {
577 .contents
= 0, /* Data */
580 .seg_not_present
= 0,
584 if (safe_modify_ldt(0x11, &desc
, sizeof(desc
)) != 0) {
586 err(1, "modify_ldt");
587 printf("[SKIP]\tmodify_ldt unavailable\n");
591 /* Arm the thread. */
593 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
595 asm volatile ("mov %0, %%ss" : : "r" (0x7));
604 * On success, modify_ldt will segfault us synchronously,
605 * and we'll escape via siglongjmp.
609 asm volatile ("mov %0, %%ss" : : "rm" (orig_ss
));
612 ftx
= 100; /* Kill the thread. */
613 syscall(SYS_futex
, &ftx
, FUTEX_WAKE
, 0, NULL
, NULL
, 0);
615 if (pthread_join(thread
, NULL
) != 0)
616 err(1, "pthread_join");
619 printf("[FAIL]\t%d of %d iterations failed\n", failures
, iters
);
622 printf("[OK]\tAll %d iterations succeeded\n", iters
);
626 static int finish_exec_test(void)
629 * In a sensible world, this would be check_invalid_segment(0, 1);
630 * For better or for worse, though, the LDT is inherited across exec.
631 * We can probably change this safely, but for now we test it.
633 check_valid_segment(0, 1,
634 AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
,
637 return nerrs
? 1 : 0;
640 static void do_exec_test(void)
642 printf("[RUN]\tTest exec\n");
644 struct user_desc desc
= {
649 .contents
= 2, /* Code, not conforming */
652 .seg_not_present
= 0,
655 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
657 pid_t child
= fork();
659 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL
);
660 printf("[FAIL]\tCould not exec self\n");
661 exit(1); /* exec failed */
664 if (waitpid(child
, &status
, 0) != child
||
665 !WIFEXITED(status
)) {
666 printf("[FAIL]\tChild died\n");
668 } else if (WEXITSTATUS(status
) != 0) {
669 printf("[FAIL]\tChild failed\n");
672 printf("[OK]\tChild succeeded\n");
677 static void setup_counter_page(void)
679 unsigned int *page
= mmap(NULL
, 4096, PROT_READ
| PROT_WRITE
,
680 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
681 if (page
== MAP_FAILED
)
684 for (int i
= 0; i
< 1024; i
++)
689 static int invoke_set_thread_area(void)
692 asm volatile ("int $0x80"
693 : "=a" (ret
), "+m" (low_user_desc
) :
694 "a" (243), "b" (low_user_desc
)
699 static void setup_low_user_desc(void)
701 low_user_desc
= mmap(NULL
, 2 * sizeof(struct user_desc
),
702 PROT_READ
| PROT_WRITE
,
703 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
704 if (low_user_desc
== MAP_FAILED
)
707 low_user_desc
->entry_number
= -1;
708 low_user_desc
->base_addr
= (unsigned long)&counter_page
[1];
709 low_user_desc
->limit
= 0xfffff;
710 low_user_desc
->seg_32bit
= 1;
711 low_user_desc
->contents
= 0; /* Data, grow-up*/
712 low_user_desc
->read_exec_only
= 0;
713 low_user_desc
->limit_in_pages
= 1;
714 low_user_desc
->seg_not_present
= 0;
715 low_user_desc
->useable
= 0;
717 if (invoke_set_thread_area() == 0) {
718 gdt_entry_num
= low_user_desc
->entry_number
;
719 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num
);
721 printf("[NOTE]\tset_thread_area is unavailable\n");
724 low_user_desc_clear
= low_user_desc
+ 1;
725 low_user_desc_clear
->entry_number
= gdt_entry_num
;
726 low_user_desc_clear
->read_exec_only
= 1;
727 low_user_desc_clear
->seg_not_present
= 1;
730 static void test_gdt_invalidation(void)
733 return; /* 64-bit only system -- we can't use set_thread_area */
735 unsigned short prev_sel
;
740 unsigned long saved_base
;
741 unsigned long new_base
;
745 invoke_set_thread_area();
747 sel
= (gdt_entry_num
<< 3) | 3;
748 asm volatile ("movw %%ds, %[prev_sel]\n\t"
749 "movw %[sel], %%ds\n\t"
753 "movl %[arg1], %%ebx\n\t"
754 "int $0x80\n\t" /* Should invalidate ds */
758 "movw %%ds, %[sel]\n\t"
759 "movw %[prev_sel], %%ds"
760 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
762 : "m" (low_user_desc_clear
),
763 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
772 printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
776 invoke_set_thread_area();
778 sel
= (gdt_entry_num
<< 3) | 3;
779 asm volatile ("movw %%es, %[prev_sel]\n\t"
780 "movw %[sel], %%es\n\t"
784 "movl %[arg1], %%ebx\n\t"
785 "int $0x80\n\t" /* Should invalidate es */
789 "movw %%es, %[sel]\n\t"
790 "movw %[prev_sel], %%es"
791 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
793 : "m" (low_user_desc_clear
),
794 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
803 printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
807 invoke_set_thread_area();
809 sel
= (gdt_entry_num
<< 3) | 3;
811 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &saved_base
);
813 asm volatile ("movw %%fs, %[prev_sel]\n\t"
814 "movw %[sel], %%fs\n\t"
818 "movl %[arg1], %%ebx\n\t"
819 "int $0x80\n\t" /* Should invalidate fs */
823 "movw %%fs, %[sel]\n\t"
824 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
826 : "m" (low_user_desc_clear
),
827 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
831 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &new_base
);
834 /* Restore FS/BASE for glibc */
835 asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel
] "rm" (prev_sel
));
838 syscall(SYS_arch_prctl
, ARCH_SET_FS
, saved_base
);
847 printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
851 if (sel
== 0 && new_base
!= 0) {
853 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base
);
855 printf("[OK]\tNew FSBASE was zero\n");
860 invoke_set_thread_area();
862 sel
= (gdt_entry_num
<< 3) | 3;
864 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &saved_base
);
866 asm volatile ("movw %%gs, %[prev_sel]\n\t"
867 "movw %[sel], %%gs\n\t"
871 "movl %[arg1], %%ebx\n\t"
872 "int $0x80\n\t" /* Should invalidate gs */
876 "movw %%gs, %[sel]\n\t"
877 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
879 : "m" (low_user_desc_clear
),
880 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
884 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &new_base
);
887 /* Restore GS/BASE for glibc */
888 asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel
] "rm" (prev_sel
));
891 syscall(SYS_arch_prctl
, ARCH_SET_GS
, saved_base
);
900 printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
904 if (sel
== 0 && new_base
!= 0) {
906 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base
);
908 printf("[OK]\tNew GSBASE was zero\n");
913 int main(int argc
, char **argv
)
915 if (argc
== 1 && !strcmp(argv
[0], "ldt_gdt_test_exec"))
916 return finish_exec_test();
918 setup_counter_page();
919 setup_low_user_desc();
927 test_gdt_invalidation();
929 return nerrs
? 1 : 0;