1.0.9.62: Performance and stability improvement of threading on FreeBSD
[sbcl.git] / src / runtime / x86-64-linux-os.c
1 /*
2  * The x86-64 Linux incarnation of arch-dependent OS-dependent
3  * routines.  See also "linux-os.c".
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
10  * This software is derived from the CMU CL system, which was
11  * written at Carnegie Mellon University and released into the
12  * public domain. The software is in the public domain and is
13  * provided with absolutely no warranty. See the COPYING and CREDITS
14  * files for more information.
15  */
16
17 #include <stdio.h>
18 #include <stddef.h>
19 #include <sys/param.h>
20 #include <sys/file.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #define __USE_GNU
26 #include <sys/ucontext.h>
27 #undef __USE_GNU
28
29
30 #include "./signal.h"
31 #include "os.h"
32 #include "arch.h"
33 #include "globals.h"
34 #include "interrupt.h"
35 #include "interr.h"
36 #include "lispregs.h"
37 #include "sbcl.h"
38 #include <sys/socket.h>
39 #include <sys/utsname.h>
40
41 #include <sys/types.h>
42 #include <signal.h>
43 /* #include <sys/sysinfo.h> */
44 #include <sys/time.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <asm/ldt.h>
48 #include <linux/unistd.h>
49 #include <sys/mman.h>
50 #include <linux/version.h>
51 #include "thread.h"             /* dynamic_values_bytes */
52
53 #include "validate.h"
54 size_t os_vm_page_size;
55
56 int arch_os_thread_init(struct thread *thread) {
57     stack_t sigstack;
58 #ifdef LISP_FEATURE_SB_THREAD
59 #ifdef LISP_FEATURE_GCC_TLS
60     current_thread = thread;
61 #else
62     pthread_setspecific(specials,thread);
63 #endif
64 #endif
65 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
66     /* Signal handlers are run on the control stack, so if it is exhausted
67      * we had better use an alternate stack for whatever signal tells us
68      * we've exhausted it */
69     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
70     sigstack.ss_flags=0;
71     sigstack.ss_size = 32*SIGSTKSZ;
72     if(sigaltstack(&sigstack,0)<0) {
73         lose("Cannot sigaltstack: %s\n",strerror(errno));
74     }
75 #endif
76     return 1;
77 }
78
79 /* free any arch/os-specific resources used by thread, which is now
80  * defunct.  Not called on live threads
81  */
82
83 int arch_os_thread_cleanup(struct thread *thread) {
84     return 1;
85 }
86
87
88 os_context_register_t *
89 os_context_register_addr(os_context_t *context, int offset)
90 {
91 #define RCASE(name) case reg_ ## name: return &context->uc_mcontext.gregs[REG_ ## name];
92     switch(offset) {
93         RCASE(RAX)
94         RCASE(RCX)
95         RCASE(RDX)
96         RCASE(RBX)
97         RCASE(RSP)
98         RCASE(RBP)
99         RCASE(RSI)
100         RCASE(RDI)
101         RCASE(R8)
102         RCASE(R9)
103         RCASE(R10)
104         RCASE(R11)
105         RCASE(R12)
106         RCASE(R13)
107         RCASE(R14)
108         RCASE(R15)
109       default:
110         if(offset<NGREG)
111             return &context->uc_mcontext.gregs[offset/2+4];
112         else return 0;
113     }
114     return &context->uc_mcontext.gregs[offset];
115 }
116
117 os_context_register_t *
118 os_context_pc_addr(os_context_t *context)
119 {
120     return &context->uc_mcontext.gregs[REG_RIP]; /*  REG_EIP */
121 }
122
123 os_context_register_t *
124 os_context_sp_addr(os_context_t *context)
125 {
126     return &context->uc_mcontext.gregs[REG_RSP];
127 }
128
129 os_context_register_t *
130 os_context_fp_addr(os_context_t *context)
131 {
132     return &context->uc_mcontext.gregs[REG_RBP];
133 }
134
135 unsigned long
136 os_context_fp_control(os_context_t *context)
137 {
138     /* return the x87 exception flags ored in with the sse2
139      * control+status flags */
140     unsigned int result = (context->uc_mcontext.fpregs->swd & 0x3F) | context->uc_mcontext.fpregs->mxcsr;
141     /* flip exception mask bits */
142     return result ^ (0x3F << 7);
143 }
144
145 sigset_t *
146 os_context_sigmask_addr(os_context_t *context)
147 {
148     return &context->uc_sigmask;
149 }
150
151 void
152 os_restore_fp_control(os_context_t *context)
153 {
154     /* reset exception flags and restore control flags on SSE2 FPU */
155     unsigned int temp = (context->uc_mcontext.fpregs->mxcsr) & ~0x3F;
156     asm ("ldmxcsr %0" : : "m" (temp));
157     /* same for x87 FPU. */
158     asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cwd));
159 }
160
161 void
162 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
163 {
164 }
165