0.9.0.37:
[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 #error Threads are not supported on x86-64 in this SBCL version
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     sigaltstack(&sigstack,0);
69 #endif
70     return 1;
71 }
72
73 /* free any arch/os-specific resources used by thread, which is now
74  * defunct.  Not called on live threads
75  */
76
77 int arch_os_thread_cleanup(struct thread *thread) {
78     return 1;
79 }
80
81
82 os_context_register_t *
83 os_context_register_addr(os_context_t *context, int offset)
84 {
85 #define RCASE(name) case reg_ ## name: return &context->uc_mcontext.gregs[REG_ ## name];
86     switch(offset) {
87         RCASE(RAX)
88         RCASE(RCX)
89         RCASE(RDX)
90         RCASE(RBX)
91         RCASE(RSP)
92         RCASE(RBP)
93         RCASE(RSI)
94         RCASE(RDI)
95         RCASE(R8)
96         RCASE(R9)
97         RCASE(R10)
98         RCASE(R11)
99         RCASE(R12)
100         RCASE(R13)
101         RCASE(R14)
102         RCASE(R15)
103       default: 
104         if(offset<NGREG) 
105             return &context->uc_mcontext.gregs[offset/2+4];
106         else return 0;
107     }
108     return &context->uc_mcontext.gregs[offset];
109 }
110
111 os_context_register_t *
112 os_context_pc_addr(os_context_t *context)
113 {
114     return &context->uc_mcontext.gregs[REG_RIP]; /*  REG_EIP */
115 }
116
117 os_context_register_t *
118 os_context_sp_addr(os_context_t *context)
119 {                               
120     return &context->uc_mcontext.gregs[REG_RSP];
121 }
122
123 os_context_register_t *
124 os_context_fp_addr(os_context_t *context)
125 {
126     return &context->uc_mcontext.gregs[REG_RBP];
127 }
128
129 unsigned long
130 os_context_fp_control(os_context_t *context)
131 {
132     int mxcsr = context->uc_mcontext.fpregs->mxcsr;
133     return ((mxcsr & 0x3F) << 16 | ((mxcsr >> 7) & 0x3F)) ^ 0x3F;
134 }
135
136 sigset_t *
137 os_context_sigmask_addr(os_context_t *context)
138 {
139     return &context->uc_sigmask;
140 }
141
142 void
143 os_restore_fp_control(os_context_t *context)
144 {
145     asm ("ldmxcsr %0" : : "m" (context->uc_mcontext.fpregs->mxcsr));
146 }
147
148 void
149 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
150 {
151 }
152