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
&& 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 * Older kernel versions did inherit the LDT on exec() which is
630 * wrong because exec() starts from a clean state.
632 check_invalid_segment(0, 1);
634 return nerrs
? 1 : 0;
637 static void do_exec_test(void)
639 printf("[RUN]\tTest exec\n");
641 struct user_desc desc
= {
646 .contents
= 2, /* Code, not conforming */
649 .seg_not_present
= 0,
652 install_valid(&desc
, AR_DPL3
| AR_TYPE_XRCODE
| AR_S
| AR_P
| AR_DB
);
654 pid_t child
= fork();
656 execl("/proc/self/exe", "ldt_gdt_test_exec", NULL
);
657 printf("[FAIL]\tCould not exec self\n");
658 exit(1); /* exec failed */
661 if (waitpid(child
, &status
, 0) != child
||
662 !WIFEXITED(status
)) {
663 printf("[FAIL]\tChild died\n");
665 } else if (WEXITSTATUS(status
) != 0) {
666 printf("[FAIL]\tChild failed\n");
669 printf("[OK]\tChild succeeded\n");
674 static void setup_counter_page(void)
676 unsigned int *page
= mmap(NULL
, 4096, PROT_READ
| PROT_WRITE
,
677 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
678 if (page
== MAP_FAILED
)
681 for (int i
= 0; i
< 1024; i
++)
686 static int invoke_set_thread_area(void)
689 asm volatile ("int $0x80"
690 : "=a" (ret
), "+m" (low_user_desc
) :
691 "a" (243), "b" (low_user_desc
)
696 static void setup_low_user_desc(void)
698 low_user_desc
= mmap(NULL
, 2 * sizeof(struct user_desc
),
699 PROT_READ
| PROT_WRITE
,
700 MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_32BIT
, -1, 0);
701 if (low_user_desc
== MAP_FAILED
)
704 low_user_desc
->entry_number
= -1;
705 low_user_desc
->base_addr
= (unsigned long)&counter_page
[1];
706 low_user_desc
->limit
= 0xfffff;
707 low_user_desc
->seg_32bit
= 1;
708 low_user_desc
->contents
= 0; /* Data, grow-up*/
709 low_user_desc
->read_exec_only
= 0;
710 low_user_desc
->limit_in_pages
= 1;
711 low_user_desc
->seg_not_present
= 0;
712 low_user_desc
->useable
= 0;
714 if (invoke_set_thread_area() == 0) {
715 gdt_entry_num
= low_user_desc
->entry_number
;
716 printf("[NOTE]\tset_thread_area is available; will use GDT index %d\n", gdt_entry_num
);
718 printf("[NOTE]\tset_thread_area is unavailable\n");
721 low_user_desc_clear
= low_user_desc
+ 1;
722 low_user_desc_clear
->entry_number
= gdt_entry_num
;
723 low_user_desc_clear
->read_exec_only
= 1;
724 low_user_desc_clear
->seg_not_present
= 1;
727 static void test_gdt_invalidation(void)
730 return; /* 64-bit only system -- we can't use set_thread_area */
732 unsigned short prev_sel
;
737 unsigned long saved_base
;
738 unsigned long new_base
;
742 invoke_set_thread_area();
744 sel
= (gdt_entry_num
<< 3) | 3;
745 asm volatile ("movw %%ds, %[prev_sel]\n\t"
746 "movw %[sel], %%ds\n\t"
750 "movl %[arg1], %%ebx\n\t"
751 "int $0x80\n\t" /* Should invalidate ds */
755 "movw %%ds, %[sel]\n\t"
756 "movw %[prev_sel], %%ds"
757 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
759 : "m" (low_user_desc_clear
),
760 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
769 printf("[%s]\tInvalidate DS with set_thread_area: new DS = 0x%hx\n",
773 invoke_set_thread_area();
775 sel
= (gdt_entry_num
<< 3) | 3;
776 asm volatile ("movw %%es, %[prev_sel]\n\t"
777 "movw %[sel], %%es\n\t"
781 "movl %[arg1], %%ebx\n\t"
782 "int $0x80\n\t" /* Should invalidate es */
786 "movw %%es, %[sel]\n\t"
787 "movw %[prev_sel], %%es"
788 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
790 : "m" (low_user_desc_clear
),
791 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
800 printf("[%s]\tInvalidate ES with set_thread_area: new ES = 0x%hx\n",
804 invoke_set_thread_area();
806 sel
= (gdt_entry_num
<< 3) | 3;
808 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &saved_base
);
810 asm volatile ("movw %%fs, %[prev_sel]\n\t"
811 "movw %[sel], %%fs\n\t"
815 "movl %[arg1], %%ebx\n\t"
816 "int $0x80\n\t" /* Should invalidate fs */
820 "movw %%fs, %[sel]\n\t"
821 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
823 : "m" (low_user_desc_clear
),
824 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
828 syscall(SYS_arch_prctl
, ARCH_GET_FS
, &new_base
);
831 /* Restore FS/BASE for glibc */
832 asm volatile ("movw %[prev_sel], %%fs" : : [prev_sel
] "rm" (prev_sel
));
835 syscall(SYS_arch_prctl
, ARCH_SET_FS
, saved_base
);
844 printf("[%s]\tInvalidate FS with set_thread_area: new FS = 0x%hx\n",
848 if (sel
== 0 && new_base
!= 0) {
850 printf("[FAIL]\tNew FSBASE was 0x%lx\n", new_base
);
852 printf("[OK]\tNew FSBASE was zero\n");
857 invoke_set_thread_area();
859 sel
= (gdt_entry_num
<< 3) | 3;
861 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &saved_base
);
863 asm volatile ("movw %%gs, %[prev_sel]\n\t"
864 "movw %[sel], %%gs\n\t"
868 "movl %[arg1], %%ebx\n\t"
869 "int $0x80\n\t" /* Should invalidate gs */
873 "movw %%gs, %[sel]\n\t"
874 : [prev_sel
] "=&r" (prev_sel
), [sel
] "+r" (sel
),
876 : "m" (low_user_desc_clear
),
877 [arg1
] "r" ((unsigned int)(unsigned long)low_user_desc_clear
)
881 syscall(SYS_arch_prctl
, ARCH_GET_GS
, &new_base
);
884 /* Restore GS/BASE for glibc */
885 asm volatile ("movw %[prev_sel], %%gs" : : [prev_sel
] "rm" (prev_sel
));
888 syscall(SYS_arch_prctl
, ARCH_SET_GS
, saved_base
);
897 printf("[%s]\tInvalidate GS with set_thread_area: new GS = 0x%hx\n",
901 if (sel
== 0 && new_base
!= 0) {
903 printf("[FAIL]\tNew GSBASE was 0x%lx\n", new_base
);
905 printf("[OK]\tNew GSBASE was zero\n");
910 int main(int argc
, char **argv
)
912 if (argc
== 1 && !strcmp(argv
[0], "ldt_gdt_test_exec"))
913 return finish_exec_test();
915 setup_counter_page();
916 setup_low_user_desc();
924 test_gdt_invalidation();
926 return nerrs
? 1 : 0;