Change immobile space free pointers to alien vars
[sbcl.git] / src / runtime / os.h
blobba5b6fcd2e2c4193fcfe3974aaa95c9bdb08c764
1 /*
2 * common interface for OS-dependent functions
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
16 #if !defined(_OS_H_INCLUDED_)
18 #define _OS_H_INCLUDED_
20 #include "sbcl.h"
21 #include "runtime.h"
23 #ifdef LISP_FEATURE_RELOCATABLE_HEAP
24 extern uword_t DYNAMIC_SPACE_START;
25 #endif
27 #if defined(LISP_FEATURE_GENCGC) && !defined(ENABLE_PAGE_PROTECTION)
28 /* Should we use page protection to help avoid the scavenging of pages
29 * that don't have pointers to younger generations?
30 * You can change this to 0 if you want SBCL not to install the handlers
31 * for SIGSEGV and SIGBUS. That will slow down GC, but might be desirable
32 * for debugging or for exploring GC strategies such as remembered sets */
33 #define ENABLE_PAGE_PROTECTION 1
34 #endif
36 #ifdef LISP_FEATURE_CHENEYGC
37 #define INSTALL_SIG_MEMORY_FAULT_HANDLER 1
38 #endif
39 #ifdef LISP_FEATURE_GENCGC
40 #define INSTALL_SIG_MEMORY_FAULT_HANDLER ENABLE_PAGE_PROTECTION
41 #endif
43 /* Some standard preprocessor definitions and typedefs are needed from
44 * the OS-specific #include files. This is an attempt to document
45 * them on 20000729, by WHN the impatient reverse engineer.
47 * OS_VM_PROT_READ, OS_VM_PROT_WRITE, OS_VM_PROT_EXECUTE
48 * flags for mmap, mprotect, etc. controlling memory protection
49 * os_vm_prot_t
50 * type used for flags for mmap, mprotect, etc.
52 * os_vm_address_t
53 * the type used to represent addresses? (dunno why not just void*)
54 * os_vm_size_t, os_vm_off_t
55 * corresponding to standard (POSIX?) types size_t, off_t
56 * os_context_t
57 * the type used to represent context in a POSIX sigaction SA_SIGACTION
58 * handler, i.e. the actual type of the thing pointed to by the
59 * void* third argument of a handler */
61 #include "target-os.h"
64 #define OS_VM_PROT_ALL \
65 (OS_VM_PROT_READ | OS_VM_PROT_WRITE | OS_VM_PROT_EXECUTE)
67 #define OS_VM_PROT_NONE 0
69 extern os_vm_size_t os_vm_page_size;
71 /* Do anything we need to do when starting up the runtime environment
72 * in this OS. */
73 extern void os_init(char *argv[], char *envp[]);
75 /* Install any OS-dependent low-level signal handlers which are needed
76 * by the runtime environment. E.g. the signals raised by a violation
77 * of the gencgc write barrier need to be caught at a low level, and
78 * they may be SIGSEGV on one OS and SIGBUS on another, so we install
79 * them in an OS-dependent way. */
80 extern void os_install_interrupt_handlers(void);
82 /* Clear a possibly-huge region of memory using any tricks available to
83 * do it efficiently, e.g. possibly unmapping it and then remapping it.
85 * FIXME: For the x86 Linux/OpenBSD/FreeBSD ports, I'd be somewhat
86 * surprised if bzero() wasn't substantially as efficient as
87 * any tricks like this. It might make sense to benchmark it
88 * and simplify if the difference isn't too large. */
89 extern void os_zero(os_vm_address_t addr, os_vm_size_t length);
91 /* It looks as though this function allocates 'len' bytes at 'addr',
92 * or at an OS-chosen address if 'addr' is zero.
94 * FIXME: There was some documentation for these functions in
95 * "hp-ux.c" in the old CMU CL code. Perhaps move/merge it in here. */
96 #define MOVABLE 1
97 #define NOT_MOVABLE 0
98 extern os_vm_address_t os_validate(boolean movable,
99 os_vm_address_t addr,
100 os_vm_size_t len);
102 #ifdef LISP_FEATURE_WIN32
103 void* os_validate_recommit(os_vm_address_t addr, os_vm_size_t len);
104 #endif
106 /* This function seems to undo the effect of os_validate(..). */
107 extern void os_invalidate(os_vm_address_t addr, os_vm_size_t len);
109 /* This maps a file into memory, or calls lose(..) for various
110 * failures. */
111 extern void os_map(int fd,
112 int offset,
113 os_vm_address_t addr,
114 os_vm_size_t len);
116 /* This presumably flushes the instruction cache, if that can be done
117 * explicitly. (It doesn't seem to be an issue for the i386 port,
118 * which is all that exists for SBCL. It might be important for some
119 * other architecture which CMU CL has been ported to, though. */
120 extern void os_flush_icache(os_vm_address_t addr, os_vm_size_t len);
122 /* This sets access rights for an area of memory, e.g.
123 * write-protecting a page so that the garbage collector can find out
124 * whether it's modified by handling the signal. */
125 extern void os_protect(os_vm_address_t addr,
126 os_vm_size_t len,
127 os_vm_prot_t protection);
129 /* Return true for an address (with or without lowtag bits) within
130 * any range of memory understood by the garbage collector. */
131 extern boolean gc_managed_addr_p(lispobj test);
133 /* Given a signal context, return the address for storage of the
134 * register, of the specified offset, for that context. The offset is
135 * defined in the storage class (SC) defined in the Lisp virtual
136 * machine (i.e. the file "vm.lisp" for the appropriate architecture). */
137 os_context_register_t *
138 os_context_register_addr(os_context_t *context, int offset);
140 os_context_register_t *
141 os_context_float_register_addr(os_context_t *context, int offset);
143 /* Given a signal context, return the address for storage of the
144 * program counter for that context. */
145 os_context_register_t *os_context_pc_addr(os_context_t *context);
146 #ifdef ARCH_HAS_NPC_REGISTER
147 os_context_register_t *os_context_npc_addr(os_context_t *context);
148 #endif
149 #ifdef ARCH_HAS_LINK_REGISTER
150 os_context_register_t *os_context_lr_addr(os_context_t *context);
151 #endif
153 /* Given a signal context, return the address for storage of the
154 * system stack pointer for that context. */
155 #ifdef ARCH_HAS_STACK_POINTER
156 os_context_register_t *os_context_sp_addr(os_context_t *context);
157 #endif
158 /* Given a signal context, return the address for storage of the
159 * signal mask for that context. */
160 sigset_t *os_context_sigmask_addr(os_context_t *context);
162 /* (Note that there may be other accessors for os_context_t which
163 * depend not only on the OS, but also on the architecture, e.g.
164 * getting at EFL/EFLAGS on the x86. Such things are defined in the
165 * architecture-dependence files, not the OS-dependence files.) */
167 /* These are not architecture-specific functions, but are instead
168 * general utilities defined in terms of the architecture-specific
169 * function os_validate(..) and os_invalidate(..).
171 extern os_vm_address_t os_allocate(os_vm_size_t len);
172 extern void os_deallocate(os_vm_address_t addr, os_vm_size_t len);
174 /* FIXME: The os_trunc_foo(..) and os_round_foo(..) macros here could
175 * be functions. */
177 #define os_trunc_to_page(addr) \
178 (os_vm_address_t)(((uword_t)(addr))&~(os_vm_page_size-1))
179 #define os_round_up_to_page(addr) \
180 os_trunc_to_page((addr)+(os_vm_page_size-1))
182 #define os_trunc_size_to_page(size) \
183 (os_vm_size_t)(((uword_t)(size))&~(os_vm_page_size-1))
184 #define os_round_up_size_to_page(size) \
185 os_trunc_size_to_page((size)+(os_vm_page_size-1))
187 /* KLUDGE: The errno error reporting system is an ugly nonreentrant
188 * botch which nonetheless wasn't too painful in the old days.
189 * However, it's obviously not good for multithreaded programs, and n
190 * order to accommodate multithreading while retaining the C-level
191 * syntax of the old UNIX interface, errno has now been changed from a
192 * true variable to a preprocessor definition which is too hairy for
193 * us to try to unscrew in Lisp code. Instead, Lisp code calls this
194 * service routine to do whatever hackery is necessary in C code, and
195 * to return the value in a way that Lisp can understand. */
196 int os_get_errno(void);
198 /* Return an absolute path to the runtime executable, or NULL if this
199 * information is unavailable. Unless external_path is non-zero the
200 * returned path may only be valid for the current process, ie:
201 * something like /proc/curproc/file. If a non-null pathname is
202 * returned, it must be 'free'd. */
203 extern char *os_get_runtime_executable_path(int external_path);
205 /* Write platforms specific ones when necessary. This is to get us off
206 * the ground. */
207 #if N_WORD_BITS == 32
208 # define OS_VM_SIZE_FMT "u"
209 # define OS_VM_SIZE_FMTX "x"
210 #else
211 #if defined(LISP_FEATURE_SB_WIN32)
212 # define OS_VM_SIZE_FMT "Iu"
213 # define OS_VM_SIZE_FMTX "Ix"
214 #else
215 # define OS_VM_SIZE_FMT "lu"
216 # define OS_VM_SIZE_FMTX "lx"
217 #endif
218 #endif
220 /* FIXME: this is not the right place for this, but here we have
221 * a convenient base type to hand. If it turns out we can just use
222 * size_t everywhere, this can more to runtime.h. */
223 typedef os_vm_size_t word_t;
224 #define WORD_FMTX OS_VM_SIZE_FMTX
226 #ifdef LISP_FEATURE_SB_THREAD
227 # ifndef CANNOT_USE_POSIX_SEM_T
228 # include <semaphore.h>
229 typedef sem_t os_sem_t;
230 # endif
231 void os_sem_init(os_sem_t *sem, unsigned int value);
232 void os_sem_wait(os_sem_t *sem, char *what);
233 void os_sem_post(os_sem_t *sem, char *what);
234 void os_sem_destroy(os_sem_t *sem);
235 #endif
237 #endif