Add safepoint mechanism
[sbcl.git] / src / runtime / x86-sunos-os.c
1 #include <stdio.h>
2 #include <sys/param.h>
3 #include <sys/file.h>
4 #include "sbcl.h"
5 #include "./signal.h"
6 #include "os.h"
7 #include "arch.h"
8 #include "globals.h"
9 #include "interrupt.h"
10 #include "interr.h"
11 #include "lispregs.h"
12 #include <sys/socket.h>
13 #include <sys/utsname.h>
14
15 #include <sys/types.h>
16 #include <signal.h>
17 #include <sys/time.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20
21 #ifdef LISP_FEATURE_SB_THREAD
22 #include <sys/segment.h>
23 #include <sys/sysi86.h>
24 #endif
25
26 #include "validate.h"
27
28 #ifdef LISP_FEATURE_SB_THREAD
29 pthread_mutex_t modify_ldt_lock = PTHREAD_MUTEX_INITIALIZER;
30
31 static int
32 ldt_index_selector (int index) {
33   return index << 3 | 7;
34 }
35
36 static int
37 find_free_ldt_index () {
38   struct ssd ssd;
39   int usage[65536/sizeof(int)];
40   int i;
41   FILE *fp;
42
43   memset(usage, 0, sizeof(usage));
44
45   fp = fopen("/proc/self/ldt", "r");
46
47   if (fp == NULL) {
48     lose("Couldn't open /proc/self/ldt");
49   }
50
51   while (fread(&ssd, sizeof(ssd), 1, fp) == 1) {
52     int index = ssd.sel >> 3;
53     if (index >= 65536) {
54       lose("segment selector index too large: %d", index);
55     }
56
57     usage[index / sizeof(int)] |= 1 << (index & (sizeof(int)-1));
58   }
59
60   fclose(fp);
61
62   /* Magic number 7 is the first LDT index that Solaris leaves free. */
63   for (i = 7; i < 65536; i++) {
64     if (~usage[i / sizeof(int)] & (1 << (i & (sizeof(int)-1)))) {
65       return i;
66     }
67   }
68
69   lose("Couldn't find a free LDT index");
70 }
71
72 static int
73 install_segment (unsigned long start, unsigned long size) {
74     int selector;
75
76     thread_mutex_lock(&modify_ldt_lock);
77
78     selector = ldt_index_selector(find_free_ldt_index());
79     struct ssd ssd = { selector,
80                        start,
81                        size,
82                        0xf2,
83                        0x4};
84     if (sysi86(SI86DSCR, &ssd) < 0) {
85         lose("Couldn't install segment for thread-local data");
86     }
87
88     thread_mutex_unlock(&modify_ldt_lock);
89
90     return selector;
91 }
92 #endif
93
94 int arch_os_thread_init(struct thread *thread) {
95   stack_t sigstack;
96
97 #ifdef LISP_FEATURE_SB_THREAD
98   int sel = install_segment((unsigned long) thread, dynamic_values_bytes);
99
100   FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", sel));
101   __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
102
103   thread->tls_cookie = sel;
104   pthread_setspecific(specials,thread);
105
106 # ifdef LISP_FEATURE_SB_SAFEPOINT
107     thread->selfptr = thread;
108 # endif
109 #endif
110
111 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
112     /* Signal handlers are run on the control stack, so if it is exhausted
113      * we had better use an alternate stack for whatever signal tells us
114      * we've exhausted it */
115     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
116     sigstack.ss_flags=0;
117     sigstack.ss_size = 32*SIGSTKSZ;
118     sigaltstack(&sigstack,0);
119 #endif
120      return 1;                   /* success */
121 }
122
123 int arch_os_thread_cleanup(struct thread *thread) {
124 #if defined(LISP_FEATURE_SB_THREAD)
125     int n = thread->tls_cookie;
126     struct ssd delete = { n, 0, 0, 0, 0};
127
128     /* Set the %%fs register back to 0 and free the ldt by setting it
129      * to NULL.
130      */
131     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
132
133     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
134
135     thread_mutex_lock(&modify_ldt_lock);
136     if (sysi86(SI86DSCR, &delete) < 0) {
137       lose("Couldn't remove segment\n");
138     }
139     thread_mutex_unlock(&modify_ldt_lock);
140 #endif
141     return 1;                   /* success */
142 }
143
144 os_context_register_t   *
145 os_context_register_addr(os_context_t *context, int offset)
146 {
147     /* Solaris x86 holds %esp value in UESP */
148     switch(offset) {
149     case reg_EAX: return &context->uc_mcontext.gregs[11];
150     case reg_ECX: return &context->uc_mcontext.gregs[10];
151     case reg_EDX: return &context->uc_mcontext.gregs[9];
152     case reg_EBX: return &context->uc_mcontext.gregs[8];
153     case reg_ESP: return &context->uc_mcontext.gregs[17]; /* REG_UESP */
154     case reg_EBP: return &context->uc_mcontext.gregs[6];
155     case reg_ESI: return &context->uc_mcontext.gregs[5];
156     case reg_EDI: return &context->uc_mcontext.gregs[4];
157     default: return 0;
158     }
159     return &context->uc_mcontext.gregs[offset];
160 }
161
162 os_context_register_t *
163 os_context_pc_addr(os_context_t *context)
164 {
165     return &(context->uc_mcontext.gregs[14]); /* REG_EIP */
166 }
167
168 os_context_register_t *
169 os_context_sp_addr(os_context_t *context)
170 {
171     return &(context->uc_mcontext.gregs[17]); /* REG_UESP */
172 }
173
174 sigset_t *
175 os_context_sigmask_addr(os_context_t *context)
176 {
177     return &(context->uc_sigmask);
178 }
179
180 void os_flush_icache(os_vm_address_t address, os_vm_size_t length)
181 {
182 }
183
184 unsigned long
185 os_context_fp_control(os_context_t *context)
186 {
187     int *state = context->uc_mcontext.fpregs.fp_reg_set.fpchip_state.state;
188     /* The STATE array is in the format used by the x86 instruction FNSAVE,
189      * so the FPU control word is in the first 16 bits */
190     int cw = (state[0] & 0xffff);
191     int sw = context->uc_mcontext.fpregs.fp_reg_set.fpchip_state.status;
192     return (cw ^ 0x3f) | (sw << 16);
193 }