2 * common interface for OS-dependent functions
6 * This software is part of the SBCL system. See the README file for
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 "genesis/sbcl.h"
26 #if defined LISP_FEATURE_GENERATIONAL && !defined ENABLE_PAGE_PROTECTION
27 /* Should we use page protection to help avoid the scavenging of pages
28 * that don't have pointers to younger generations?
29 * (Protection can mean either MMU-based or logical protection)
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
36 #if defined LISP_FEATURE_CHENEYGC || defined LISP_FEATURE_SB_SAFEPOINT
37 // safepoint traps always require a signal handler
38 #define INSTALL_SIG_MEMORY_FAULT_HANDLER 1
39 #elif defined LISP_FEATURE_GENERATIONAL
40 #define INSTALL_SIG_MEMORY_FAULT_HANDLER ENABLE_PAGE_PROTECTION
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
50 * type used for flags for mmap, mprotect, etc.
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
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"
63 #ifndef LISP_FEATURE_WIN32
64 // all posix systems can use the same #defines
65 #define OS_VM_PROT_READ PROT_READ
66 #define OS_VM_PROT_WRITE PROT_WRITE
67 #define OS_VM_PROT_EXECUTE PROT_EXEC
70 #define OS_VM_PROT_ALL \
71 (OS_VM_PROT_READ | OS_VM_PROT_WRITE | OS_VM_PROT_EXECUTE)
73 #define OS_VM_PROT_NONE 0
75 extern os_vm_size_t os_vm_page_size
;
77 #if defined LISP_FEATURE_WIN32 || defined LISP_FEATURE_LINUX
78 int os_preinit(char *argv
[], char *envp
[]);
80 #define os_preinit(dummy1,dummy2) (0)
82 void os_link_runtime();
83 void os_unlink_runtime();
85 /* Do anything we need to do when starting up the runtime environment
87 extern void os_init();
89 /* Install any OS-dependent low-level signal handlers which are needed
90 * by the runtime environment. E.g. the signals raised by a violation
91 * of the gencgc write barrier need to be caught at a low level, and
92 * they may be SIGSEGV on one OS and SIGBUS on another, so we install
93 * them in an OS-dependent way. */
94 extern void os_install_interrupt_handlers(void);
96 /* Clear a possibly-huge region of memory using any tricks available to
97 * do it efficiently, e.g. possibly unmapping it and then remapping it.
99 * FIXME: For the x86 Linux/OpenBSD/FreeBSD ports, I'd be somewhat
100 * surprised if bzero() wasn't substantially as efficient as
101 * any tricks like this. It might make sense to benchmark it
102 * and simplify if the difference isn't too large. */
103 extern void os_zero(os_vm_address_t addr
, os_vm_size_t length
);
105 /* Allocate 'len' bytes at 'addr',
106 * or at an OS-chosen address if 'addr' is zero.
107 * If 'movable' then 'addr' is a preference, not a requirement.
108 * These are discrete bits, not opaque enumerated values.
109 * i.e. the consuming code might test via either (x & bit)
110 * or (x == bit) depending on the use-case */
111 #define NOT_MOVABLE 0
113 #define ALLOCATE_LOW 2
114 #define IS_THREAD_STRUCT 4
115 #define MOVABLE_LOW (MOVABLE|ALLOCATE_LOW)
116 #define IS_GUARD_PAGE 8
117 extern os_vm_address_t
os_alloc_gc_space(int space_id
, int attributes
,
118 os_vm_address_t addr
, os_vm_size_t len
);
120 #ifdef LISP_FEATURE_WIN32
121 void os_commit_memory(os_vm_address_t addr
, os_vm_size_t len
);
124 /* This maps a file into memory, or calls lose(..) for various
126 extern void* load_core_bytes(int fd
,
127 os_vm_offset_t offset
,
128 os_vm_address_t addr
,
131 extern void* load_core_bytes_jit(int fd
,
132 os_vm_offset_t offset
,
133 os_vm_address_t addr
,
136 /* This presumably flushes the instruction cache, if that can be done
137 * explicitly. (It doesn't seem to be an issue for the i386 port,
138 * which is all that exists for SBCL. It might be important for some
139 * other architecture which CMU CL has been ported to, though. */
140 extern void os_flush_icache(os_vm_address_t addr
, os_vm_size_t len
);
142 /* This sets access rights for an area of memory, e.g.
143 * write-protecting a page so that the garbage collector can find out
144 * whether it's modified by handling the signal. */
145 extern void os_protect(os_vm_address_t addr
,
147 os_vm_prot_t protection
);
149 /* Return true for an address (with or without lowtag bits) within
150 * any range of memory understood by the garbage collector. */
151 extern bool gc_managed_addr_p(lispobj addr
);
152 /* As for above, but consider only the heap spaces, not stacks */
153 extern bool gc_managed_heap_space_p(lispobj addr
);
155 /* Given a signal context, return the address for storage of the
156 * register, of the specified offset, for that context. The offset is
157 * defined in the storage class (SC) defined in the Lisp virtual
158 * machine (i.e. the file "vm.lisp" for the appropriate architecture). */
159 os_context_register_t
*
160 os_context_register_addr(os_context_t
*context
, int offset
);
162 os_context_register_t
*
163 os_context_float_register_addr(os_context_t
*context
, int offset
);
165 #ifdef ARCH_HAS_NPC_REGISTER
166 os_context_register_t
*os_context_npc_addr(os_context_t
*context
);
168 #ifdef ARCH_HAS_LINK_REGISTER
169 os_context_register_t
*os_context_lr_addr(os_context_t
*context
);
172 /* Given a signal context, return the address for storage of the
173 * system stack pointer for that context. */
174 #ifdef ARCH_HAS_STACK_POINTER
175 os_context_register_t
*os_context_sp_addr(os_context_t
*context
);
176 // os_context_fp_addr might not be defined
177 os_context_register_t
*os_context_fp_addr(os_context_t
*context
);
178 #if defined LISP_FEATURE_X86_64
179 # define os_context_frame_pointer(context) *os_context_register_addr(context,reg_RBP)
180 #elif defined LISP_FEATURE_X86
181 # define os_context_frame_pointer(context) *os_context_register_addr(context,reg_EBP)
183 # define os_context_frame_pointer(context) 0
186 /* Given a signal context, return the address for storage of the
187 * signal mask for that context. */
188 sigset_t
*os_context_sigmask_addr(os_context_t
*context
);
190 /* (Note that there may be other accessors for os_context_t which
191 * depend not only on the OS, but also on the architecture, e.g.
192 * getting at EFL/EFLAGS on the x86. Such things are defined in the
193 * architecture-dependence files, not the OS-dependence files.) */
195 /* These are not architecture-specific functions, but are instead
196 * general utilities defined in terms of the architecture-specific
197 * function os_alloc_gc_space(..) and os_deallocate(..).
199 extern os_vm_address_t
os_allocate(os_vm_size_t len
);
200 extern void os_deallocate(os_vm_address_t addr
, os_vm_size_t len
);
202 #define os_trunc_to_page(addr) PTR_ALIGN_DOWN(addr, os_vm_page_size)
203 #define os_round_up_to_page(addr) PTR_ALIGN_UP(addr, os_vm_page_size)
205 #define os_trunc_size_to_page(size) \
206 (os_vm_size_t)ALIGN_DOWN((uword_t)size, os_vm_page_size)
207 #define os_round_up_size_to_page(size) \
208 (os_vm_size_t)ALIGN_UP((uword_t)size, os_vm_page_size)
210 /* KLUDGE: The errno error reporting system is an ugly nonreentrant
211 * botch which nonetheless wasn't too painful in the old days.
212 * However, it's obviously not good for multithreaded programs, and n
213 * order to accommodate multithreading while retaining the C-level
214 * syntax of the old UNIX interface, errno has now been changed from a
215 * true variable to a preprocessor definition which is too hairy for
216 * us to try to unscrew in Lisp code. Instead, Lisp code calls this
217 * service routine to do whatever hackery is necessary in C code, and
218 * to return the value in a way that Lisp can understand. */
219 int os_get_errno(void);
221 /* Return an absolute path to the runtime executable, or NULL if this
222 * information is unavailable. If a non-null pathname is
223 * returned, it must be 'free'd. */
224 extern char *os_get_runtime_executable_path();
226 /* Write platforms specific ones when necessary. This is to get us off
228 #define OS_VM_SIZE_FMT PRIuPTR
229 #define OS_VM_SIZE_FMTX PRIxPTR
231 #if defined LISP_FEATURE_SB_THREAD && defined LISP_FEATURE_UNIX
232 # if !defined USE_DARWIN_GCD_SEMAPHORES && !defined CANNOT_USE_POSIX_SEM_T
233 # include <semaphore.h>
234 typedef sem_t os_sem_t
;
236 void os_sem_init(os_sem_t
*sem
, unsigned int value
);
237 void os_sem_wait(os_sem_t
*sem
);
238 void os_sem_post(os_sem_t
*sem
);
239 void os_sem_destroy(os_sem_t
*sem
);
242 extern int os_reported_page_size
;
244 // Opaque context accessor
245 uword_t
os_context_pc(os_context_t
*);
246 void set_os_context_pc(os_context_t
*, uword_t
);
248 #ifdef LISP_FEATURE_WIN32
250 #define GETPAGESIZE 4096
251 # define FTELL _ftelli64
252 # define FSEEK _fseeki64
253 # define LSEEK _lseeki64
254 typedef __int64 ftell_type
;
257 #define GETPAGESIZE getpagesize()
261 typedef long ftell_type
;