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