45cd2bc1e4b500417cc42e6c4d0ee7dd3f4981b0
[sbcl.git] / src / runtime / os.h
1 /*
2  * common interface for OS-dependent functions
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
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.
14  */
15
16 /*
17  * $Header$
18  */
19
20 #if !defined(_OS_H_INCLUDED_)
21
22 #define _OS_H_INCLUDED_
23
24 #include "runtime.h"
25
26 /* Some standard preprocessor definitions and typedefs are needed from
27  * the OS-specific #include files. This is an attempt to document
28  * them on 20000729, by WHN the impatient reverse engineer.
29  *
30  * OS_VM_PROT_READ, OS_VM_PROT_WRITE, OS_VM_PROT_EXECUTE
31  *   flags for mmap, mprotect, etc. controlling memory protection
32  * os_vm_prot_t
33  *   type used for flags for mmap, mprotect, etc.
34  *
35  * OS_VM_DEFAULT_PAGESIZE
36  *   used by core dumping and loading logic (but dunno its exact
37  *   definition, in particular why we can't just use getpagesize()
38  *   instead)
39  *
40  * os_vm_address_t
41  *   the type used to represent addresses? (dunno why not just void*)
42  * os_vm_size_t, os_vm_off_t
43  *   corresponding to standard (POSIX?) types size_t, off_t
44  * os_context_t
45  *   the type used to represent context in a POSIX sigaction SA_SIGACTION
46  *   handler, i.e. the actual type of the thing pointed to by the
47  *   void* third argument of a handler */
48 #if defined __FreeBSD__
49 #include "bsd-os.h"
50 #elif defined __OpenBSD__
51 #include "bsd-os.h"
52 #elif defined __linux__
53 #include "linux-os.h"
54 #else
55 #error unsupported OS
56 #endif
57
58 #define OS_VM_PROT_ALL \
59   (OS_VM_PROT_READ | OS_VM_PROT_WRITE | OS_VM_PROT_EXECUTE)
60
61 extern os_vm_size_t os_vm_page_size;
62
63 /* Do anything we need to do when starting up the runtime environment
64  * in this OS. */
65 extern void os_init(void);
66
67 /* Install any OS-dependent low-level signal handlers which are needed
68  * by the runtime environment. E.g. the signals raised by a violation
69  * of the gencgc write barrier need to be caught at a low level, and
70  * they may be SIGSEGV on one OS and SIGBUS on another, so we install
71  * them in an OS-dependent way. */
72 extern void os_install_interrupt_handlers(void);
73
74 /* Clear a possibly-huge region of memory using any tricks available to
75  * do it efficiently, e.g. possibly unmapping it and then remapping it.
76  *
77  * FIXME: For the x86 Linux/OpenBSD/FreeBSD ports, I'd be somewhat
78  * surprised if bzero() wasn't substantially as efficient as
79  * any tricks like this. It might make sense to benchmark it
80  * and simplify if the difference isn't too large. */
81 extern void os_zero(os_vm_address_t addr, os_vm_size_t length);
82
83 /* It looks as though this function allocates 'len' bytes at 'addr',
84  * or at an OS-chosen address if 'addr' is zero.
85  *
86  * FIXME: There was some documentation for these functions in
87  * "hp-ux.c" in the old CMU CL code. Perhaps move/merge it in here. */
88 extern os_vm_address_t os_validate(os_vm_address_t addr, os_vm_size_t len);
89
90 /* This function seems to undo the effect of os_validate(..). */
91 extern void os_invalidate(os_vm_address_t addr, os_vm_size_t len);
92
93 /* This maps a file into memory, or calls lose(..) for various
94  * failures. */
95 extern os_vm_address_t os_map(int fd,
96                               int offset,
97                               os_vm_address_t addr,
98                               os_vm_size_t len);
99
100 /* This presumably flushes the instruction cache, if that can be done
101  * explicitly. (It doesn't seem to be an issue for the i386 port,
102  * which is all that exists for SBCL. It might be important for some
103  * other architecture which CMU CL has been ported to, though. */
104 extern void os_flush_icache(os_vm_address_t addr, os_vm_size_t len);
105
106 /* This sets access rights for an area of memory, e.g.
107  * write-protecting a page so that the garbage collector can find out
108  * whether it's modified by handling the signal. */
109 extern void os_protect(os_vm_address_t addr,
110                        os_vm_size_t len,
111                        os_vm_prot_t protection);
112
113 /* This returns true for an address which makes sense at the Lisp level. */
114 extern boolean is_valid_lisp_addr(os_vm_address_t test);
115
116 /* Given a signal context, return the address for storage of the
117  * register, of the specified offset, for that context. The offset is
118  * defined in the storage class (SC) defined in the Lisp virtual
119  * machine (i.e. the file "vm.lisp" for the appropriate architecture). */
120 int *os_context_register_addr(os_context_t *context, int offset);
121
122 /* Given a signal context, return the address for storage of the
123  * program counter for that context. */
124 int *os_context_pc_addr(os_context_t *context);
125
126 /* Given a signal context, return the address for storage of the
127  * system stack pointer for that context. */
128 int *os_context_sp_addr(os_context_t *context);
129
130 /* Given a signal context, return the address for storage of the
131  * signal mask for that context. */
132 sigset_t *os_context_sigmask_addr(os_context_t *context);
133
134 /* (Note that there may be other accessors for os_context_t which
135  * depend not only on the OS, but also on the architecture, e.g.
136  * getting at EFL/EFLAGS on the x86. Such things are defined in the
137  * architecture-dependence files, not the OS-dependence files.) */
138    
139 /* These are not architecture-specific functions, but are instead
140  * general utilities defined in terms of the architecture-specific
141  * function os_validate(..) and os_invalidate(..).
142  *
143  * FIXME: os_reallocate(..) is complicated and seems no longer to be
144  * used for anything. Perhaps we could delete it? */
145 extern os_vm_address_t os_allocate(os_vm_size_t len);
146 extern os_vm_address_t os_allocate_at(os_vm_address_t addr, os_vm_size_t len);
147 extern os_vm_address_t os_reallocate(os_vm_address_t addr,
148                                      os_vm_size_t old_len,
149                                      os_vm_size_t len);
150 extern void os_deallocate(os_vm_address_t addr, os_vm_size_t len);
151
152
153 /* FIXME: The os_trunc_foo(..) and os_round_foo(..) macros here could
154  * be functions. */
155
156 #define os_trunc_to_page(addr) \
157     (os_vm_address_t)(((long)(addr))&~(os_vm_page_size-1))
158 #define os_round_up_to_page(addr) \
159     os_trunc_to_page((addr)+(os_vm_page_size-1))
160
161 #define os_trunc_size_to_page(size) \
162     (os_vm_size_t)(((long)(size))&~(os_vm_page_size-1))
163 #define os_round_up_size_to_page(size) \
164     os_trunc_size_to_page((size)+(os_vm_page_size-1))
165
166 /* KLUDGE: The errno error reporting system is an ugly nonreentrant
167  * botch which nonetheless wasn't too painful in the old days.
168  * However, it's obviously not good for multithreaded programs, and n
169  * order to accommodate multithreading while retaining the C-level
170  * syntax of the old UNIX interface, errno has now been changed from a
171  * true variable to a preprocessor definition which is too hairy for
172  * us to try to unscrew in Lisp code. Instead, Lisp code calls this
173  * service routine to do whatever hackery is necessary in C code, and
174  * to return the value in a way that Lisp can understand. */
175 int os_get_errno(void);
176
177 #endif