Fix make-array transforms.
[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 #define _GNU_SOURCE /* for REG_RAX etc. from sys/ucontext */
18
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <sys/param.h>
22 #include <sys/file.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <errno.h>
26
27 #include <sys/ucontext.h>
28
29 #include "./signal.h"
30 #include "os.h"
31 #include "arch.h"
32 #include "globals.h"
33 #include "interrupt.h"
34 #include "interr.h"
35 #include "lispregs.h"
36 #include "sbcl.h"
37 #include <sys/socket.h>
38 #include <sys/utsname.h>
39
40 #include <sys/types.h>
41 #include <signal.h>
42 /* #include <sys/sysinfo.h> */
43 #include <sys/time.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <asm/ldt.h>
47 #include <linux/unistd.h>
48 #include <sys/mman.h>
49 #include <linux/version.h>
50 #include "thread.h"             /* dynamic_values_bytes */
51
52 #include "validate.h"
53 size_t os_vm_page_size;
54
55 int arch_os_thread_init(struct thread *thread) {
56     stack_t sigstack;
57 #ifdef LISP_FEATURE_SB_THREAD
58 #ifdef LISP_FEATURE_GCC_TLS
59     current_thread = thread;
60 #else
61     pthread_setspecific(specials,thread);
62 #endif
63 #endif
64 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
65     /* Signal handlers are run on the control stack, so if it is exhausted
66      * we had better use an alternate stack for whatever signal tells us
67      * we've exhausted it */
68     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
69     sigstack.ss_flags=0;
70     sigstack.ss_size = 32*SIGSTKSZ;
71     if(sigaltstack(&sigstack,0)<0) {
72         lose("Cannot sigaltstack: %s\n",strerror(errno));
73     }
74 #endif
75     return 1;
76 }
77
78 /* free any arch/os-specific resources used by thread, which is now
79  * defunct.  Not called on live threads
80  */
81
82 int arch_os_thread_cleanup(struct thread *thread) {
83     return 1;
84 }
85
86
87 os_context_register_t *
88 os_context_register_addr(os_context_t *context, int offset)
89 {
90 #define RCASE(name) case reg_ ## name: return &context->uc_mcontext.gregs[REG_ ## name];
91     switch(offset) {
92         RCASE(RAX)
93         RCASE(RCX)
94         RCASE(RDX)
95         RCASE(RBX)
96         RCASE(RSP)
97         RCASE(RBP)
98         RCASE(RSI)
99         RCASE(RDI)
100         RCASE(R8)
101         RCASE(R9)
102         RCASE(R10)
103         RCASE(R11)
104         RCASE(R12)
105         RCASE(R13)
106         RCASE(R14)
107         RCASE(R15)
108       default:
109         if(offset<NGREG)
110             return &context->uc_mcontext.gregs[offset/2+4];
111         else return 0;
112     }
113     return &context->uc_mcontext.gregs[offset];
114 }
115
116 os_context_register_t *
117 os_context_pc_addr(os_context_t *context)
118 {
119     return &context->uc_mcontext.gregs[REG_RIP]; /*  REG_EIP */
120 }
121
122 os_context_register_t *
123 os_context_sp_addr(os_context_t *context)
124 {
125     return &context->uc_mcontext.gregs[REG_RSP];
126 }
127
128 os_context_register_t *
129 os_context_fp_addr(os_context_t *context)
130 {
131     return &context->uc_mcontext.gregs[REG_RBP];
132 }
133
134 unsigned long
135 os_context_fp_control(os_context_t *context)
136 {
137     /* return the x87 exception flags ored in with the sse2
138      * control+status flags */
139     unsigned int result = (context->uc_mcontext.fpregs->swd & 0x3F) | context->uc_mcontext.fpregs->mxcsr;
140     /* flip exception mask bits */
141     return result ^ (0x3F << 7);
142 }
143
144 sigset_t *
145 os_context_sigmask_addr(os_context_t *context)
146 {
147     return &context->uc_sigmask;
148 }
149
150 void
151 os_restore_fp_control(os_context_t *context)
152 {
153     if (context->uc_mcontext.fpregs) {
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
162 void
163 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
164 {
165 }
166