De-flake 'traceroot' test
[sbcl.git] / src / runtime / x86-64-linux-os.c
blobf97bcde63c45f028c139bfc2f85642e9169635b5
1 /*
2 * The x86-64 Linux incarnation of arch-dependent OS-dependent
3 * routines. See also "linux-os.c".
4 */
6 /*
7 * This software is part of the SBCL system. See the README file for
8 * more information.
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.
17 #define _GNU_SOURCE /* for REG_RAX etc. from sys/ucontext */
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>
27 #include <sys/ucontext.h>
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>
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 <linux/unistd.h>
47 #include <sys/mman.h>
48 #include <linux/version.h>
49 #include "thread.h" /* dynamic_values_bytes */
51 #include "validate.h"
52 size_t os_vm_page_size;
54 int arch_os_thread_init(struct thread *thread) {
55 stack_t sigstack;
56 #ifdef LISP_FEATURE_SB_THREAD
57 #ifdef LISP_FEATURE_GCC_TLS
58 current_thread = thread;
59 #else
60 pthread_setspecific(specials,thread);
61 #endif
62 #endif
63 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
64 /* Signal handlers are run on the control stack, so if it is exhausted
65 * we had better use an alternate stack for whatever signal tells us
66 * we've exhausted it */
67 sigstack.ss_sp=((char *) thread)+dynamic_values_bytes;
68 sigstack.ss_flags=0;
69 sigstack.ss_size = 32*SIGSTKSZ;
70 if(sigaltstack(&sigstack,0)<0) {
71 lose("Cannot sigaltstack: %s\n",strerror(errno));
73 #endif
74 return 1;
77 /* free any arch/os-specific resources used by thread, which is now
78 * defunct. Not called on live threads
81 int arch_os_thread_cleanup(struct thread *thread) {
82 return 1;
86 os_context_register_t *
87 os_context_register_addr(os_context_t *context, int offset)
89 #define RCASE(name) case reg_ ## name: return \
90 (os_context_register_t*)&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 (os_context_register_t*)&context->uc_mcontext.gregs[offset/2+4];
111 else return 0;
113 return (os_context_register_t*)&context->uc_mcontext.gregs[offset];
116 os_context_register_t *
117 os_context_pc_addr(os_context_t *context)
119 return (os_context_register_t*)&context->uc_mcontext.gregs[REG_RIP]; /* REG_EIP */
122 os_context_register_t *
123 os_context_sp_addr(os_context_t *context)
125 return (os_context_register_t*)&context->uc_mcontext.gregs[REG_RSP];
128 os_context_register_t *
129 os_context_fp_addr(os_context_t *context)
131 return (os_context_register_t*)&context->uc_mcontext.gregs[REG_RBP];
134 unsigned long
135 os_context_fp_control(os_context_t *context)
137 return (uintptr_t)&context->uc_mcontext.gregs[REG_RSP];
141 os_context_register_t *
142 os_context_float_register_addr(os_context_t *context, int offset)
144 return (os_context_register_t*)&context->uc_mcontext.fpregs->_xmm[offset];
147 sigset_t *
148 os_context_sigmask_addr(os_context_t *context)
150 return &context->uc_sigmask;
153 void
154 os_restore_fp_control(os_context_t *context)
156 if (context->uc_mcontext.fpregs) {
157 /* reset exception flags and restore control flags on SSE2 FPU */
158 unsigned int temp = (context->uc_mcontext.fpregs->mxcsr) & ~0x3F;
159 asm ("ldmxcsr %0" : : "m" (temp));
160 /* same for x87 FPU. */
161 asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cwd));
165 void
166 os_flush_icache(os_vm_address_t address, os_vm_size_t length)