1 /* Tests that the process can exit even if daemon thread is still running.
2 This test does *not* use any libc; it interfaces only with kernel. */
6 #include <sys/regset.h>
7 #include <sys/syscall.h>
10 #if defined(__amd64) || defined(__i386)
11 #include <sys/segments.h>
14 extern void bzero(void *ptr
, size_t n
);
16 #if defined(VGP_x86_solaris)
23 " movl 12(%esp), %ecx\n"
24 " movl 8(%esp), %edi\n"
29 #elif defined(VGP_amd64_solaris)
42 # error "Unknown platform"
45 static void sleep(unsigned int sec
) {
47 ts
.tv_sec
= (time_t)sec
;
50 #if defined(VGP_x86_solaris)
51 __asm__
__volatile__ (
55 "movl %[SYSNO], %%eax\n"
59 : [TS
] "g" (&ts
), [SYSNO
] "n" (SYS_nanosleep
)
60 : "eax", "edx", "cc", "memory");
61 #elif defined(VGP_amd64_solaris)
62 __asm__
__volatile__ (
63 "movq %[SYSNO], %%rax\n"
68 : [TS
] "g" (&ts
), [SYSNO
] "n" (SYS_nanosleep
)
69 : "rax", "rdx", "rdi", "rsi", "cc", "memory");
71 # error "Unknown platform"
75 static void lwp_exit(void) {
76 #if defined(VGP_x86_solaris)
77 __asm__
__volatile__ (
78 "movl %[SYSNO], %%eax\n"
81 : [SYSNO
] "n" (SYS_lwp_exit
)
82 : "eax", "edx", "cc", "memory");
83 #elif defined(VGP_amd64_solaris)
84 __asm__
__volatile__ (
85 "movq %[SYSNO], %%rax\n"
88 : [SYSNO
] "n" (SYS_lwp_exit
)
89 : "rax", "rdx", "cc", "memory");
91 # error "Unknown platform"
95 #define STACK_FLAGS (MAP_PRIVATE | MAP_NORESERVE | MAP_ANON)
96 #define STACK_PROT (PROT_READ | PROT_WRITE)
97 static void *allocate_stack(size_t stacksize
) {
100 #if defined(VGP_x86_solaris)
101 __asm__
__volatile__ (
108 "pushl $0xdeadbeef\n"
109 "movl %[SYSNO], %%eax\n"
112 "movl %%eax, %[ADDRESS]\n"
113 : [ADDRESS
] "=r" (address
)
114 : [FLAGS
] "n" (STACK_FLAGS
), [PROT
] "n" (STACK_PROT
),
115 [SIZE
] "g" (stacksize
), [SYSNO
] "n" (SYS_mmap
)
116 : "eax", "edx", "cc", "memory");
117 #elif defined(VGP_amd64_solaris)
118 __asm__
__volatile__ (
119 "movq %[SYSNO], %%rax\n"
121 "movq %[SIZE], %%rsi\n"
122 "movq %[PROT], %%rdx\n"
123 "movq %[FLAGS], %%r10\n"
127 "movq %%rax, %[ADDRESS]\n"
128 : [ADDRESS
] "=r" (address
)
129 : [FLAGS
] "n" (STACK_FLAGS
), [PROT
] "n" (STACK_PROT
),
130 [SIZE
] "g" (stacksize
), [SYSNO
] "n" (SYS_mmap
)
131 : "rax", "rdx", "rdi", "rsi", "r10", "r8", "r9", "cc", "memory");
133 # error "Unknown platform"
141 static void thread_func(void) {
145 #define LWP_FLAGS (LWP_SUSPENDED | LWP_DETACHED | LWP_DAEMON)
146 static id_t
lwp_create(void *stack
) {
150 bzero(&ucontext
, sizeof(ucontext
));
151 ucontext
.uc_flags
= UC_CPU
;
153 #if defined(VGP_x86_solaris)
154 __asm__
__volatile__ (
155 "mov %%ss, %[STACK_SEG]\n"
156 : [STACK_SEG
] "=r" (ucontext
.uc_mcontext
.gregs
[SS
])
159 ucontext
.uc_mcontext
.gregs
[EIP
] = (greg_t
) thread_func
;
160 ucontext
.uc_mcontext
.gregs
[UESP
] = (greg_t
) stack
;
161 ucontext
.uc_mcontext
.gregs
[EBP
] = (greg_t
) stack
;
162 #elif defined(VGP_amd64_solaris)
163 ucontext
.uc_mcontext
.gregs
[REG_SS
] = UDS_SEL
;
164 ucontext
.uc_mcontext
.gregs
[REG_RIP
] = (greg_t
) thread_func
;
165 ucontext
.uc_mcontext
.gregs
[REG_RSP
] = (greg_t
) stack
;
166 ucontext
.uc_mcontext
.gregs
[REG_RBP
] = (greg_t
) stack
;
168 # error "Unknown platform"
171 #if defined(VGP_x86_solaris)
172 __asm__
__volatile__ (
175 "pushl %[UCONTEXT]\n"
176 "pushl $0xdeadbeef\n"
177 "movl %[SYSNO], %%eax\n"
180 "movl %%eax, %[TID]\n"
182 : [FLAGS
] "n" (LWP_FLAGS
), [UCONTEXT
] "g" (&ucontext
),
183 [SYSNO
] "n" (SYS_lwp_create
)
184 : "eax", "edx", "cc", "memory");
185 #elif defined(VGP_amd64_solaris)
186 __asm__
__volatile__ (
187 "movq %[SYSNO], %%rax\n"
188 "movq %[UCONTEXT], %%rdi\n"
189 "movq %[FLAGS], %%rsi\n"
192 "movl %%eax, %[TID]\n"
194 : [FLAGS
] "n" (LWP_FLAGS
), [UCONTEXT
] "g" (&ucontext
),
195 [SYSNO
] "n" (SYS_lwp_create
)
196 : "rax", "rdx", "rdi", "rsi", "cc", "memory");
198 # error "Unknown platform"
204 static void lwp_continue(id_t tid
) {
205 #if defined(VGP_x86_solaris)
206 __asm__
__volatile__ (
208 "pushl $0xdeadbeef\n"
209 "movl %[SYSNO], %%eax\n"
213 : [TID
] "m" (tid
), [SYSNO
] "n" (SYS_lwp_continue
)
214 : "eax", "edx", "cc", "memory");
215 #elif defined(VGP_amd64_solaris)
216 __asm__
__volatile__ (
217 "movq %[SYSNO], %%rax\n"
219 "movl %[TID], %%edi\n"
222 : [TID
] "r" (tid
), [SYSNO
] "n" (SYS_lwp_continue
)
223 : "rax", "rdx", "rdi", "cc", "memory");
225 # error "Unknown platform"
229 #define STACK_SIZE 16384
231 void *stack
= allocate_stack(STACK_SIZE
);
232 id_t tid
= lwp_create((char *) stack
+ STACK_SIZE
);
236 return; /* not reached */