Add optimizers from string=* for simple-base-string= too.
[sbcl.git] / src / runtime / x86-linux-os.c
blob8e3e300e4bc98bf1bb7fb934817d2b66709841d4
1 /*
2 * The x86 Linux incarnation of arch-dependent OS-dependent routines.
3 * 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 #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>
25 #include "genesis/sbcl.h"
26 #include "os.h"
27 #include "arch.h"
28 #include "globals.h"
29 #include "interrupt.h"
30 #include "interr.h"
31 #include "lispregs.h"
33 #include <sys/types.h>
34 #include <signal.h>
35 /* #include <sys/sysinfo.h> */
36 #include <sys/time.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <asm/ldt.h>
40 #include <sys/syscall.h>
41 #include <sys/mman.h>
42 #include <linux/version.h>
43 #include "thread.h" /* dynamic_values_bytes */
45 static inline int set_thread_area(struct user_desc *u_info)
47 return syscall(SYS_set_thread_area, u_info);
50 #include "validate.h"
52 int arch_os_thread_init(struct thread *thread) {
53 stack_t sigstack;
54 #ifdef LISP_FEATURE_SB_THREAD
55 struct user_desc desc = {
56 0, (unsigned long) thread,
57 (dynamic_values_bytes>>12)-1,
58 1, 0, 0, 1, 0, 1
61 static int entry_number = -1;
63 desc.entry_number = entry_number;
65 if (set_thread_area(&desc) != 0) {
66 return 0;
69 entry_number = desc.entry_number;
71 __asm__ __volatile__ ("movw %w0, %%fs" : : "q"
72 ((entry_number << 3) + 3));
74 if(entry_number < 0) return 0;
75 #endif
77 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
78 /* Signal handlers are run on the control stack, so if it is exhausted
79 * we had better use an alternate stack for whatever signal tells us
80 * we've exhausted it */
81 sigstack.ss_sp = calc_altstack_base(thread);
82 sigstack.ss_flags = 0;
83 sigstack.ss_size = calc_altstack_size(thread);
84 if(sigaltstack(&sigstack,0)<0)
85 lose("Cannot sigaltstack: %s",strerror(errno));
86 #endif
87 return 1;
90 struct thread *debug_get_fs() {
91 register uint32_t fs;
92 __asm__ __volatile__ ("movl %%fs,%0" : "=r" (fs) : );
93 return (struct thread *)fs;
96 /* free any arch/os-specific resources used by thread, which is now
97 * defunct. Not called on live threads
100 int arch_os_thread_cleanup(struct thread *thread) {
101 return 1;
106 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
107 * <sys/ucontext.h> file to define symbolic names for offsets into
108 * gregs[], but it's conditional on __USE_GNU and not defined, so
109 * we need to do this nasty absolute index magic number thing
110 * instead. */
111 os_context_register_t *
112 os_context_register_addr(os_context_t *context, int offset)
114 switch(offset) {
115 case reg_EAX: return &context->uc_mcontext.gregs[11];
116 case reg_ECX: return &context->uc_mcontext.gregs[10];
117 case reg_EDX: return &context->uc_mcontext.gregs[9];
118 case reg_EBX: return &context->uc_mcontext.gregs[8];
119 case reg_ESP: return &context->uc_mcontext.gregs[7];
120 case reg_EBP: return &context->uc_mcontext.gregs[6];
121 case reg_ESI: return &context->uc_mcontext.gregs[5];
122 case reg_EDI: return &context->uc_mcontext.gregs[4];
123 default: return 0;
125 return &context->uc_mcontext.gregs[offset];
128 os_context_register_t *
129 os_context_sp_addr(os_context_t *context)
131 return &context->uc_mcontext.gregs[17]; /* REG_UESP */
134 os_context_register_t *
135 os_context_fp_addr(os_context_t *context)
137 return &context->uc_mcontext.gregs[6]; /* REG_EBP */
140 unsigned long
141 os_context_fp_control(os_context_t *context)
143 return ((((context->uc_mcontext.fpregs->cw) & 0xffff) ^ 0x3f) |
144 (((context->uc_mcontext.fpregs->sw) & 0xffff) << 16));
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 asm ("fldcw %0" : : "m" (context->uc_mcontext.fpregs->cw));
160 os_context_register_t *
161 os_context_float_register_addr(os_context_t *context, int offset)
163 return (os_context_register_t*)&context->uc_mcontext.fpregs->_st[offset];
166 void
167 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
171 // To avoid "Missing required foreign symbol" errors in os_link_runtime()
172 // the executable must actually depend on libm. It would not require libm,
173 // despite -lm in the link step, if there is no reference to a libm symbol
174 // observable to the linker. Any one symbol suffices to resolve all of them.
175 #include <math.h>
176 const long libm_anchor = (long)acos;