1a0955b86SJohn Levon /*
2a0955b86SJohn Levon * This file and its contents are supplied under the terms of the
3a0955b86SJohn Levon * Common Development and Distribution License ("CDDL"), version 1.0.
4a0955b86SJohn Levon * You may only use this file in accordance with the terms of version
5a0955b86SJohn Levon * 1.0 of the CDDL.
6a0955b86SJohn Levon *
7a0955b86SJohn Levon * A full copy of the text of the CDDL should have accompanied this
8a0955b86SJohn Levon * source. A copy of the CDDL is also available via the Internet at
9a0955b86SJohn Levon * http://www.illumos.org/license/CDDL.
10a0955b86SJohn Levon */
11a0955b86SJohn Levon
12a0955b86SJohn Levon /*
13a0955b86SJohn Levon * Copyright 2018 Joyent, Inc.
14a0955b86SJohn Levon */
15a0955b86SJohn Levon
16a0955b86SJohn Levon #include <sys/types.h>
17a0955b86SJohn Levon #include <sys/sysi86.h>
18a0955b86SJohn Levon #include <sys/segments.h>
19a0955b86SJohn Levon #include <sys/segment.h>
20a0955b86SJohn Levon #include <unistd.h>
21a0955b86SJohn Levon #include <string.h>
22a0955b86SJohn Levon #include <errno.h>
23a0955b86SJohn Levon #include <pthread.h>
24a0955b86SJohn Levon #include <err.h>
25a0955b86SJohn Levon
26a0955b86SJohn Levon char foo[4096];
27a0955b86SJohn Levon
28a0955b86SJohn Levon static void *
donothing(void * nothing)29a0955b86SJohn Levon donothing(void *nothing)
30a0955b86SJohn Levon {
31*dc5e7685SJohn Levon (void) sleep(5);
32a0955b86SJohn Levon return (NULL);
33a0955b86SJohn Levon }
34a0955b86SJohn Levon
35a0955b86SJohn Levon int
main(void)36a0955b86SJohn Levon main(void)
37a0955b86SJohn Levon {
38a0955b86SJohn Levon pthread_t tid;
39a0955b86SJohn Levon
40a0955b86SJohn Levon /*
41a0955b86SJohn Levon * This first is similar to what sbcl does in some variants. Note the
42a0955b86SJohn Levon * SDT_MEMRW (not SDT_MEMRWA) so we check that the kernel is forcing the
43a0955b86SJohn Levon * 'accessed' bit too.
44a0955b86SJohn Levon */
45a0955b86SJohn Levon int sel = SEL_LDT(7);
46a0955b86SJohn Levon
47a0955b86SJohn Levon struct ssd ssd = { sel, (unsigned long)&foo, 4096,
48a0955b86SJohn Levon SDT_MEMRW | (SEL_UPL << 5) | (1 << 7), 0x4 };
49a0955b86SJohn Levon
50a0955b86SJohn Levon if (sysi86(SI86DSCR, &ssd) < 0)
51a0955b86SJohn Levon err(-1, "failed to setup segment");
52a0955b86SJohn Levon
53a0955b86SJohn Levon __asm__ __volatile__("mov %0, %%fs" : : "r" (sel));
54a0955b86SJohn Levon
55a0955b86SJohn Levon ssd.acc1 = 0;
56a0955b86SJohn Levon
57a0955b86SJohn Levon if (sysi86(SI86DSCR, &ssd) == 0)
58a0955b86SJohn Levon errx(-1, "removed in-use segment?");
59a0955b86SJohn Levon
60a0955b86SJohn Levon __asm__ __volatile__("mov %0, %%fs" : : "r" (0));
61a0955b86SJohn Levon
62a0955b86SJohn Levon if (sysi86(SI86DSCR, &ssd) < 0)
63a0955b86SJohn Levon err(-1, "failed to remove segment");
64a0955b86SJohn Levon
65a0955b86SJohn Levon for (int i = 0; i < MAXNLDT; i++) {
66a0955b86SJohn Levon ssd.sel = SEL_LDT(i);
67a0955b86SJohn Levon (void) sysi86(SI86DSCR, &ssd);
68a0955b86SJohn Levon }
69a0955b86SJohn Levon
70a0955b86SJohn Levon for (int i = 0; i < 10; i++)
71*dc5e7685SJohn Levon (void) pthread_create(&tid, NULL, donothing, NULL);
72a0955b86SJohn Levon
73a0955b86SJohn Levon if (forkall() == 0) {
74*dc5e7685SJohn Levon (void) sleep(2);
75a0955b86SJohn Levon _exit(0);
76a0955b86SJohn Levon }
77a0955b86SJohn Levon
78*dc5e7685SJohn Levon (void) sleep(6);
79a0955b86SJohn Levon return (0);
80a0955b86SJohn Levon }
81