CHANGE-CLASS finalizes inheritance of new class if necessary
[sbcl.git] / src / runtime / os-common.c
blob6d87dd5a9c733761e2cecfa91ffaa6048bfaedfd
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
11 # define _GNU_SOURCE /* needed for RTLD_DEFAULT from dlfcn.h */
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
16 #include "sbcl.h"
17 #include "globals.h"
18 #include "runtime.h"
19 #include "genesis/config.h"
20 #include "genesis/constants.h"
21 #include "genesis/cons.h"
22 #include "genesis/vector.h"
23 #include "genesis/symbol.h"
24 #include "genesis/static-symbols.h"
25 #include "thread.h"
26 #include "sbcl.h"
27 #include "os.h"
28 #include "interr.h"
29 #if defined(LISP_FEATURE_OS_PROVIDES_DLOPEN) && !defined(LISP_FEATURE_WIN32)
30 # include <dlfcn.h>
31 #endif
34 /* Except for os_zero, these routines are only called by Lisp code.
35 * These routines may also be replaced by os-dependent versions
36 * instead. See hpux-os.c for some useful restrictions on actual
37 * usage. */
39 void
40 os_zero(os_vm_address_t addr, os_vm_size_t length)
42 os_vm_address_t block_start;
43 os_vm_size_t block_size;
45 #ifdef DEBUG
46 fprintf(stderr,";;; os_zero: addr: 0x%08x, len: 0x%08x\n",addr,length);
47 #endif
49 block_start = os_round_up_to_page(addr);
51 length -= block_start-addr;
52 block_size = os_trunc_size_to_page(length);
54 if (block_start > addr)
55 bzero((char *)addr, block_start-addr);
56 if (block_size < length)
57 bzero((char *)block_start+block_size, length-block_size);
59 if (block_size != 0) {
60 /* Now deallocate and allocate the block so that it faults in
61 * zero-filled. */
63 os_invalidate(block_start, block_size);
64 addr = os_validate(block_start, block_size);
66 if (addr == NULL || addr != block_start)
67 lose("os_zero: block moved! 0x%08x ==> 0x%08x\n",
68 block_start,
69 addr);
73 os_vm_address_t
74 os_allocate(os_vm_size_t len)
76 return os_validate((os_vm_address_t)NULL, len);
79 void
80 os_deallocate(os_vm_address_t addr, os_vm_size_t len)
82 os_invalidate(addr,len);
85 int
86 os_get_errno(void)
88 return errno;
92 #if defined(LISP_FEATURE_SB_THREAD) && (!defined(CANNOT_USE_POSIX_SEM_T) || defined(LISP_FEATURE_WIN32))
94 void
95 os_sem_init(os_sem_t *sem, unsigned int value)
97 if (-1==sem_init(sem, 0, value))
98 lose("os_sem_init(%p, %u): %s", sem, value, strerror(errno));
99 FSHOW((stderr, "os_sem_init(%p, %u)\n", sem, value));
102 void
103 os_sem_wait(os_sem_t *sem, char *what)
105 FSHOW((stderr, "%s: os_sem_wait(%p) ...\n", what, sem));
106 while (-1 == sem_wait(sem))
107 if (EINTR!=errno)
108 lose("%s: os_sem_wait(%p): %s", what, sem, strerror(errno));
109 FSHOW((stderr, "%s: os_sem_wait(%p) => ok\n", what, sem));
112 void
113 os_sem_post(sem_t *sem, char *what)
115 if (-1 == sem_post(sem))
116 lose("%s: os_sem_post(%p): %s", what, sem, strerror(errno));
117 FSHOW((stderr, "%s: os_sem_post(%p)\n", what, sem));
120 void
121 os_sem_destroy(os_sem_t *sem)
123 if (-1==sem_destroy(sem))
124 lose("os_sem_destroy(%p): %s", sem, strerror(errno));
127 #endif
129 #if defined(LISP_FEATURE_OS_PROVIDES_DLOPEN) && !defined(LISP_FEATURE_WIN32)
130 void* os_dlopen(char* name, int flags) {
131 return dlopen(name,flags);
133 #endif
135 #if defined(LISP_FEATURE_SB_DYNAMIC_CORE)
136 /* When this feature is enabled, the special category of /static/ foreign
137 * symbols disappears. Foreign fixups are resolved to linkage table locations
138 * during genesis, and for each of them a record is added to
139 * REQUIRED_RUNTIME_C_SYMBOLS list, of the form (cons name datap).
141 * Name is a base-string of a symbol name, and non-nil datap marks data
142 * references.
144 * Before any code in lisp image can be called, we have to resolve all
145 * references to runtime foreign symbols that used to be static, adding linkage
146 * table entry for each element of REQUIRED_RUNTIME_C_SYMBOLS.
149 /* We start with a little greenspunning to make car, cdr and base-string data
150 * accessible. */
152 /* Object tagged? (dereference (cast (untag (obj)))) */
153 #define FOLLOW(obj,lowtagtype,ctype) \
154 (*(struct ctype*)(obj - lowtagtype##_LOWTAG))
156 /* For all types sharing OTHER_POINTER_LOWTAG: */
157 #define FOTHERPTR(obj,ctype) \
158 FOLLOW(obj,OTHER_POINTER,ctype)
160 static inline lispobj car(lispobj conscell)
162 return FOLLOW(conscell,LIST_POINTER,cons).car;
165 static inline lispobj cdr(lispobj conscell)
167 return FOLLOW(conscell,LIST_POINTER,cons).cdr;
170 #ifndef LISP_FEATURE_WIN32
171 void *
172 os_dlsym_default(char *name)
174 void *frob = dlsym(RTLD_DEFAULT, name);
175 odxprint(misc, "%p", frob);
176 return frob;
178 #endif
180 void os_link_runtime()
182 lispobj head;
183 void *link_target = (void*)(intptr_t)LINKAGE_TABLE_SPACE_START;
184 void *validated_end = link_target;
185 lispobj symbol_name;
186 char *namechars;
187 boolean datap;
188 void* result;
189 int strict /* If in a cold core, fail early and often. */
190 = (SymbolValue(GC_INHIBIT, 0) & WIDETAG_MASK) == UNBOUND_MARKER_WIDETAG;
191 int n = 0, m = 0;
193 for (head = SymbolValue(REQUIRED_RUNTIME_C_SYMBOLS,0);
194 head!=NIL; head = cdr(head), n++)
196 lispobj item = car(head);
197 symbol_name = car(item);
198 datap = (NIL!=(cdr(item)));
199 namechars = (void*)(intptr_t)FOTHERPTR(symbol_name,vector).data;
200 result = os_dlsym_default(namechars);
201 odxprint(runtime_link, "linking %s => %p", namechars, result);
203 if (link_target == validated_end) {
204 validated_end += os_vm_page_size;
205 #ifdef LISP_FEATURE_WIN32
206 os_validate_recommit(link_target,os_vm_page_size);
207 #endif
209 if (result) {
210 if (datap)
211 arch_write_linkage_table_ref(link_target,result);
212 else
213 arch_write_linkage_table_jmp(link_target,result);
214 } else {
215 m++;
216 if (strict)
217 fprintf(stderr,
218 "undefined foreign symbol in cold init: %s\n",
219 namechars);
222 link_target = (void*)(((uintptr_t)link_target)+LINKAGE_TABLE_ENTRY_SIZE);
224 odxprint(runtime_link, "%d total symbols linked, %d undefined",
225 n, m);
226 if (strict && m)
227 /* We could proceed, but rather than run into improperly
228 * displayed internal errors, let's make ourselves heard right
229 * here and now. */
230 lose("Undefined aliens in cold init.");
232 #endif /* sb-dynamic-core */