d6b1cb92ca705957269dabb83484258de0793c3d
[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     pthread_setspecific(specials,thread);
60 #endif
61 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
62     /* Signal handlers are run on the control stack, so if it is exhausted
63      * we had better use an alternate stack for whatever signal tells us
64      * we've exhausted it */
65     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
66     sigstack.ss_flags=0;
67     sigstack.ss_size = 32*SIGSTKSZ;
68     if(sigaltstack(&sigstack,0)<0) {
69         lose("Cannot sigaltstack: %s\n",strerror(errno));
70     }
71 #endif
72     return 1;
73 }
74
75 /* free any arch/os-specific resources used by thread, which is now
76  * defunct.  Not called on live threads
77  */
78
79 int arch_os_thread_cleanup(struct thread *thread) {
80     return 1;
81 }
82
83
84 os_context_register_t *
85 os_context_register_addr(os_context_t *context, int offset)
86 {
87 #define RCASE(name) case reg_ ## name: return &context->uc_mcontext.gregs[REG_ ## name];
88     switch(offset) {
89         RCASE(RAX)
90         RCASE(RCX)
91         RCASE(RDX)
92         RCASE(RBX)
93         RCASE(RSP)
94         RCASE(RBP)
95         RCASE(RSI)
96         RCASE(RDI)
97         RCASE(R8)
98         RCASE(R9)
99         RCASE(R10)
100         RCASE(R11)
101         RCASE(R12)
102         RCASE(R13)
103         RCASE(R14)
104         RCASE(R15)
105       default:
106         if(offset<NGREG)
107             return &context->uc_mcontext.gregs[offset/2+4];
108         else return 0;
109     }
110     return &context->uc_mcontext.gregs[offset];
111 }
112
113 os_context_register_t *
114 os_context_pc_addr(os_context_t *context)
115 {
116     return &context->uc_mcontext.gregs[REG_RIP]; /*  REG_EIP */
117 }
118
119 os_context_register_t *
120 os_context_sp_addr(os_context_t *context)
121 {
122     return &context->uc_mcontext.gregs[REG_RSP];
123 }
124
125 os_context_register_t *
126 os_context_fp_addr(os_context_t *context)
127 {
128     return &context->uc_mcontext.gregs[REG_RBP];
129 }
130
131 unsigned long
132 os_context_fp_control(os_context_t *context)
133 {
134     /* return the x87 exception flags ored in with the sse2
135      * control+status flags */
136     unsigned int result = (context->uc_mcontext.fpregs->swd & 0x3F) | context->uc_mcontext.fpregs->mxcsr;
137     /* flip exception mask bits */
138     return result ^ (0x3F << 7);
139 }
140
141 sigset_t *
142 os_context_sigmask_addr(os_context_t *context)
143 {
144     return &context->uc_sigmask;
145 }
146
147 void
148 os_restore_fp_control(os_context_t *context)
149 {
150     /* reset exception flags and restore control flags on SSE2 FPU */
151     unsigned int temp = (context->uc_mcontext.fpregs->mxcsr) & ~0x3F;
152     asm ("ldmxcsr %0" : : "m" (temp));
153     /* same for x87 FPU. */
154     asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cwd));
155 }
156
157 void
158 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
159 {
160 }
161