1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/* Tests that the process can exit even if daemon thread is still running.
This test does *not* use any libc; it interfaces only with kernel. */
#include <sys/lwp.h>
#include <sys/mman.h>
#include <sys/regset.h>
#include <sys/syscall.h>
#include <ucontext.h>
#if defined(__amd64) || defined(__i386)
#include <sys/segments.h>
#endif
extern void bzero(void *ptr, size_t n);
#if defined(VGP_x86_solaris)
asm("\n"
".text\n"
".globl bzero\n"
"bzero:\n"
" push %edi\n"
" movl $0, %eax\n"
" movl 12(%esp), %ecx\n"
" movl 8(%esp), %edi\n"
" rep stosb\n"
" pop %edi\n"
" ret\n"
);
#elif defined(VGP_amd64_solaris)
asm("\n"
".text\n"
".globl bzero\n"
"bzero:\n"
" push %rdi\n"
" movq %rsi, %rcx\n"
" movq $0, %rax\n"
" rep stosb\n"
" pop %rdi\n"
" ret\n"
);
#else
# error "Unknown platform"
#endif
static void sleep(unsigned int sec) {
timespec_t ts;
ts.tv_sec = (time_t)sec;
ts.tv_nsec = 0;
#if defined(VGP_x86_solaris)
__asm__ __volatile__ (
"pushl $0\n"
"pushl %[TS]\n"
"pushl $0xdeadbeef\n"
"movl %[SYSNO], %%eax\n"
"int $0x91\n"
"addl $12, %%esp\n"
:
: [TS] "g" (&ts), [SYSNO] "n" (SYS_nanosleep)
: "eax", "edx", "cc", "memory");
#elif defined(VGP_amd64_solaris)
__asm__ __volatile__ (
"movq %[SYSNO], %%rax\n"
"movq %[TS], %%rdi\n"
"movq $0, %%rsi\n"
"syscall\n"
:
: [TS] "g" (&ts), [SYSNO] "n" (SYS_nanosleep)
: "rax", "rdx", "rdi", "rsi", "cc", "memory");
#else
# error "Unknown platform"
#endif
}
static void lwp_exit(void) {
#if defined(VGP_x86_solaris)
__asm__ __volatile__ (
"movl %[SYSNO], %%eax\n"
"int $0x91\n"
:
: [SYSNO] "n" (SYS_lwp_exit)
: "eax", "edx", "cc", "memory");
#elif defined(VGP_amd64_solaris)
__asm__ __volatile__ (
"movq %[SYSNO], %%rax\n"
"syscall\n"
:
: [SYSNO] "n" (SYS_lwp_exit)
: "rax", "rdx", "cc", "memory");
#else
# error "Unknown platform"
#endif
}
#define STACK_FLAGS (MAP_PRIVATE | MAP_NORESERVE | MAP_ANON)
#define STACK_PROT (PROT_READ | PROT_WRITE)
static void *allocate_stack(size_t stacksize) {
void *address = NULL;
#if defined(VGP_x86_solaris)
__asm__ __volatile__ (
"pushl $0\n"
"pushl $-1\n"
"pushl %[FLAGS]\n"
"pushl %[PROT]\n"
"pushl %[SIZE]\n"
"pushl $0\n"
"pushl $0xdeadbeef\n"
"movl %[SYSNO], %%eax\n"
"int $0x91\n"
"addl $28, %%esp\n"
"movl %%eax, %[ADDRESS]\n"
: [ADDRESS] "=r" (address)
: [FLAGS] "n" (STACK_FLAGS), [PROT] "n" (STACK_PROT),
[SIZE] "g" (stacksize), [SYSNO] "n" (SYS_mmap)
: "eax", "edx", "cc", "memory");
#elif defined(VGP_amd64_solaris)
__asm__ __volatile__ (
"movq %[SYSNO], %%rax\n"
"movq $0, %%rdi\n"
"movq %[SIZE], %%rsi\n"
"movq %[PROT], %%rdx\n"
"movq %[FLAGS], %%r10\n"
"movq $-1, %%r8\n"
"movq $0, %%r9\n"
"syscall\n"
"movq %%rax, %[ADDRESS]\n"
: [ADDRESS] "=r" (address)
: [FLAGS] "n" (STACK_FLAGS), [PROT] "n" (STACK_PROT),
[SIZE] "g" (stacksize), [SYSNO] "n" (SYS_mmap)
: "rax", "rdx", "rdi", "rsi", "r10", "r8", "r9", "cc", "memory");
#else
# error "Unknown platform"
#endif
return address;
}
#undef STACK_FLAGS
#undef STACK_PROT
static void thread_func(void) {
sleep(10000);
}
#define LWP_FLAGS (LWP_SUSPENDED | LWP_DETACHED | LWP_DAEMON)
static id_t lwp_create(void *stack) {
id_t tid;
ucontext_t ucontext;
bzero(&ucontext, sizeof(ucontext));
ucontext.uc_flags = UC_CPU;
#if defined(VGP_x86_solaris)
__asm__ __volatile__ (
"mov %%ss, %[STACK_SEG]\n"
: [STACK_SEG] "=r" (ucontext.uc_mcontext.gregs[SS])
:
:);
ucontext.uc_mcontext.gregs[EIP] = (greg_t) thread_func;
ucontext.uc_mcontext.gregs[UESP] = (greg_t) stack;
ucontext.uc_mcontext.gregs[EBP] = (greg_t) stack;
#elif defined(VGP_amd64_solaris)
ucontext.uc_mcontext.gregs[REG_SS] = UDS_SEL;
ucontext.uc_mcontext.gregs[REG_RIP] = (greg_t) thread_func;
ucontext.uc_mcontext.gregs[REG_RSP] = (greg_t) stack;
ucontext.uc_mcontext.gregs[REG_RBP] = (greg_t) stack;
#else
# error "Unknown platform"
#endif
#if defined(VGP_x86_solaris)
__asm__ __volatile__ (
"pushl $0\n"
"pushl %[FLAGS]\n"
"pushl %[UCONTEXT]\n"
"pushl $0xdeadbeef\n"
"movl %[SYSNO], %%eax\n"
"int $0x91\n"
"addl $16, %%esp\n"
"movl %%eax, %[TID]\n"
: [TID] "=r" (tid)
: [FLAGS] "n" (LWP_FLAGS), [UCONTEXT] "g" (&ucontext),
[SYSNO] "n" (SYS_lwp_create)
: "eax", "edx", "cc", "memory");
#elif defined(VGP_amd64_solaris)
__asm__ __volatile__ (
"movq %[SYSNO], %%rax\n"
"movq %[UCONTEXT], %%rdi\n"
"movq %[FLAGS], %%rsi\n"
"movq $0, %%rdx\n"
"syscall\n"
"movl %%eax, %[TID]\n"
: [TID] "=r" (tid)
: [FLAGS] "n" (LWP_FLAGS), [UCONTEXT] "g" (&ucontext),
[SYSNO] "n" (SYS_lwp_create)
: "rax", "rdx", "rdi", "rsi", "cc", "memory");
#else
# error "Unknown platform"
#endif
return tid;
}
static void lwp_continue(id_t tid) {
#if defined(VGP_x86_solaris)
__asm__ __volatile__ (
"pushl %[TID]\n"
"pushl $0xdeadbeef\n"
"movl %[SYSNO], %%eax\n"
"int $0x91\n"
"addl $8, %%esp\n"
:
: [TID] "m" (tid), [SYSNO] "n" (SYS_lwp_continue)
: "eax", "edx", "cc", "memory");
#elif defined(VGP_amd64_solaris)
__asm__ __volatile__ (
"movq %[SYSNO], %%rax\n"
"xor %%rdi, %%rdi\n"
"movl %[TID], %%edi\n"
"syscall\n"
:
: [TID] "r" (tid), [SYSNO] "n" (SYS_lwp_continue)
: "rax", "rdx", "rdi", "cc", "memory");
#else
# error "Unknown platform"
#endif
}
#define STACK_SIZE 16384
void _start(void) {
void *stack = allocate_stack(STACK_SIZE);
id_t tid = lwp_create((char *) stack + STACK_SIZE);
lwp_continue(tid);
sleep(5);
lwp_exit();
return; /* not reached */
}
|