gencgc: Don't use defconstant for DYNAMIC-SPACE-END
[sbcl.git] / src / code / cold-init.lisp
blobee8f9ab19e4723575f94b0fa506f949edb8e6ab9
1 ;;;; cold initialization stuff, plus some other miscellaneous stuff
2 ;;;; that we don't have any better place for
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
15 ;;;; putting ourselves out of our misery when things become too much to bear
17 (declaim (ftype (function (simple-string) nil) !cold-lose))
18 (defun !cold-lose (msg)
19 (%primitive print msg)
20 (%primitive print "too early in cold init to recover from errors")
21 (%halt))
23 ;;; last-ditch error reporting for things which should never happen
24 ;;; and which, if they do happen, are sufficiently likely to torpedo
25 ;;; the normal error-handling system that we want to bypass it
26 (declaim (ftype (function (simple-string) nil) critically-unreachable))
27 (defun critically-unreachable (where)
28 (%primitive print "internal error: Control should never reach here, i.e.")
29 (%primitive print where)
30 (%halt))
32 ;;;; !COLD-INIT
34 ;;; a list of toplevel things set by GENESIS
35 (defvar *!cold-toplevels*) ; except for DEFUNs and SETF macros
36 (defvar *!cold-setf-macros*) ; just SETF macros
37 (defvar *!cold-defconstants*) ; just DEFCONSTANT-EQXs
38 (defvar *!cold-defuns*) ; just DEFUNs
40 ;;; a SIMPLE-VECTOR set by GENESIS
41 (defvar *!load-time-values*)
43 (eval-when (:compile-toplevel :execute)
44 ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
45 ;; and use it for most of the cold-init functions. (Just be careful
46 ;; not to use it for the COLD-INIT-OR-REINIT functions.)
47 (sb!xc:defmacro show-and-call (name)
48 `(progn
49 (/primitive-print ,(symbol-name name))
50 (,name))))
52 (defun !encapsulate-stuff-for-cold-init (&aux names)
53 (flet ((encapsulate-1 (name handler)
54 (encapsulate name '!cold-init handler)
55 (push name names)))
56 (encapsulate-1 '%failed-aver
57 (lambda (f expr)
58 ;; output the message before signaling error,
59 ;; as it may be this is too early in the cold init.
60 (fresh-line)
61 (write-line "failed AVER:")
62 (write expr)
63 (terpri)
64 (funcall f expr)))
66 (encapsulate-1
67 'find-package
68 (lambda (f designator)
69 (cond ((packagep designator) designator)
70 (t (funcall f (let ((s (string designator)))
71 (if (eql (mismatch s "SB!") 3)
72 (concatenate 'string "SB-" (subseq s 3))
73 s))))))))
74 names)
76 (defmacro !with-init-wrappers (&rest forms)
77 `(let ((wrapped-functions (!encapsulate-stuff-for-cold-init)))
78 ,@forms
79 (dolist (f wrapped-functions) (unencapsulate f '!cold-init))))
81 ;;; called when a cold system starts up
82 (defun !cold-init (&aux real-choose-symbol-out-fun)
83 "Give the world a shove and hope it spins."
85 #!+sb-show
86 (sb!int::cannot-/show "Test of CANNOT-/SHOW [don't worry - this is expected]")
87 (/show0 "entering !COLD-INIT")
88 (setq *readtable* (make-readtable)
89 *print-length* 6 *print-level* 3)
90 (setq *error-output* (!make-cold-stderr-stream)
91 *standard-output* *error-output*
92 *trace-output* *error-output*)
93 (write-string "COLD-INIT... ")
95 ;; Assert that FBOUNDP doesn't choke when its answer is NIL.
96 ;; It was fine if T because in that case the legality of the arg is certain.
97 ;; And be extra paranoid - ensure that it really gets called.
98 (locally (declare (notinline fboundp)) (fboundp '(setf !zzzzzz)))
100 ;; Putting data in a synchronized hashtable (*PACKAGE-NAMES*)
101 ;; requires that the main thread be properly initialized.
102 (show-and-call thread-init-or-reinit)
103 ;; Printing of symbols requires that packages be filled in, because
104 ;; OUTPUT-SYMBOL calls FIND-SYMBOL to determine accessibility.
105 (show-and-call !package-cold-init)
106 ;; Fill in the printer's character attribute tables now.
107 ;; If Genesis could write constant arrays into a target core,
108 ;; that would be nice, and would tidy up some other things too.
109 (show-and-call !printer-cold-init)
110 ;; Because L-T-V forms have not executed, CHOOSE-SYMBOL-OUT-FUN doesn't work.
111 (setf real-choose-symbol-out-fun #'choose-symbol-out-fun)
112 (setf (symbol-function 'choose-symbol-out-fun)
113 (lambda (&rest args) (declare (ignore args)) #'output-preserve-symbol))
115 ;; *RAW-SLOT-DATA* is essentially a compile-time constant
116 ;; but isn't dumpable as such because it has functions in it.
117 (show-and-call sb!kernel::!raw-slot-data-init)
119 ;; Anyone might call RANDOM to initialize a hash value or something;
120 ;; and there's nothing which needs to be initialized in order for
121 ;; this to be initialized, so we initialize it right away.
122 (show-and-call !random-cold-init)
124 ;; Must be done before any non-opencoded array references are made.
125 (show-and-call !hairy-data-vector-reffer-init)
127 (show-and-call !character-database-cold-init)
128 (show-and-call !character-name-database-cold-init)
129 (show-and-call sb!unicode::!unicode-properties-cold-init)
131 ;; All sorts of things need INFO and/or (SETF INFO).
132 (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
133 (show-and-call !globaldb-cold-init)
135 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
136 ;; the basic type machinery needs to be initialized before toplevel
137 ;; forms run.
138 (show-and-call !type-class-cold-init)
139 ;; cold-init-wrappers are closures. Installing a closure as a
140 ;; named function requires consing immobile space code.
141 #!+immobile-code (setq sb!vm::*immobile-space-mutex*
142 (sb!thread:make-mutex :name "Immobile space"))
143 (!with-init-wrappers (show-and-call sb!kernel::!primordial-type-cold-init))
144 (show-and-call !world-lock-cold-init)
145 (show-and-call !classes-cold-init)
146 (show-and-call !early-type-cold-init)
147 (show-and-call !late-type-cold-init)
148 (show-and-call !alien-type-cold-init)
149 (show-and-call !target-type-cold-init)
150 ;; FIXME: It would be tidy to make sure that that these cold init
151 ;; functions are called in the same relative order as the toplevel
152 ;; forms of the corresponding source files.
154 (show-and-call !policy-cold-init-or-resanify)
155 (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
157 ;; Must be done before toplevel forms are invoked
158 ;; because a toplevel defstruct will need to add itself
159 ;; to the subclasses of STRUCTURE-OBJECT.
160 (show-and-call sb!kernel::!set-up-structure-object-class)
162 (dolist (x *!cold-defconstants*)
163 (destructuring-bind (name source-loc &optional docstring) x
164 (setf (info :variable :kind name) :constant)
165 (when source-loc (setf (info :source-location :constant name) source-loc))
166 (when docstring (setf (fdocumentation name 'variable) docstring))))
167 (!with-init-wrappers
168 (dolist (x *!cold-defuns*)
169 (destructuring-bind (name . inline-expansion) x
170 (%defun name (fdefinition name) nil inline-expansion))))
172 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
173 ;; fixups be done separately? Wouldn't that be clearer and better?
174 ;; -- WHN 19991204
175 (/show0 "doing cold toplevel forms and fixups")
176 (progn (write `("Length(TLFs)= " ,(length *!cold-toplevels*)))
177 (terpri))
179 (!with-init-wrappers
180 (loop for index-in-cold-toplevels from 0
181 for toplevel-thing in (prog1 *!cold-toplevels*
182 (makunbound '*!cold-toplevels*))
184 #!+sb-show
185 (when (zerop (mod index-in-cold-toplevels 1024))
186 (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
187 (/hexstr index-in-cold-toplevels))
188 (typecase toplevel-thing
189 (function
190 (funcall toplevel-thing))
191 ((cons (eql :load-time-value))
192 (setf (svref *!load-time-values* (third toplevel-thing))
193 (funcall (second toplevel-thing))))
194 ((cons (eql :load-time-value-fixup))
195 (setf (sap-ref-word (int-sap (get-lisp-obj-address (second toplevel-thing)))
196 (third toplevel-thing))
197 (get-lisp-obj-address
198 (svref *!load-time-values* (fourth toplevel-thing)))))
199 ((cons (eql defstruct))
200 (apply 'sb!kernel::%defstruct (cdr toplevel-thing)))
202 (!cold-lose "bogus operation in *!COLD-TOPLEVELS*")))))
203 (/show0 "done with loop over cold toplevel forms and fixups")
205 ;; Now that L-T-V forms have executed, the symbol output chooser works.
206 (setf (symbol-function 'choose-symbol-out-fun) real-choose-symbol-out-fun)
208 (show-and-call time-reinit)
210 ;; Set sane values again, so that the user sees sane values instead
211 ;; of whatever is left over from the last DECLAIM/PROCLAIM.
212 (show-and-call !policy-cold-init-or-resanify)
214 ;; Only do this after toplevel forms have run, 'cause that's where
215 ;; DEFTYPEs are.
216 (setf *type-system-initialized* t)
218 ;; now that the type system is definitely initialized, fixup UNKNOWN
219 ;; types that have crept in.
220 (show-and-call !fixup-type-cold-init)
221 ;; run the PROCLAIMs.
222 (show-and-call !late-proclaim-cold-init)
224 (show-and-call os-cold-init-or-reinit)
225 (show-and-call !pathname-cold-init)
226 (show-and-call !debug-info-cold-init)
228 (show-and-call stream-cold-init-or-reset)
229 (show-and-call !loader-cold-init)
230 (show-and-call !foreign-cold-init)
231 #!-(and win32 (not sb-thread))
232 (show-and-call signal-cold-init-or-reinit)
234 (show-and-call float-cold-init-or-reinit)
236 (show-and-call !class-finalize)
238 ;; Install closures as guards on some early PRINT-OBJECT methods so that
239 ;; THREAD and RESTART print nicely prior to the real methods being installed.
240 (dovector (method (cdr (assoc 'print-object sb!pcl::*!trivial-methods*)))
241 (unless (car method)
242 (let ((classoid (find-classoid (third method))))
243 (rplaca method
244 (lambda (x) (classoid-typep (layout-of x) classoid x))))))
246 ;; The reader and printer are initialized very late, so that they
247 ;; can do hairy things like invoking the compiler as part of their
248 ;; initialization.
249 (let ((*readtable* (make-readtable)))
250 (show-and-call !reader-cold-init)
251 (show-and-call !sharpm-cold-init)
252 (show-and-call !backq-cold-init)
253 ;; The *STANDARD-READTABLE* is assigned at last because the above
254 ;; functions would operate on the standard readtable otherwise---
255 ;; which would result in an error.
256 (setf *standard-readtable* *readtable*))
257 (setf *readtable* (copy-readtable *standard-readtable*))
258 (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
259 (sb!pretty:!pprint-cold-init)
260 (setq *print-level* nil *print-length* nil) ; restore defaults
262 ;; the ANSI-specified initial value of *PACKAGE*
263 (setf *package* (find-package "COMMON-LISP-USER"))
265 ;; Enable normal (post-cold-init) behavior of INFINITE-ERROR-PROTECT.
266 (setf sb!kernel::*maximum-error-depth* 10)
267 (/show0 "enabling internal errors")
268 (setf (extern-alien "internal_errors_enabled" int) 1)
270 ;; Toggle some readonly bits
271 (dovector (sc sb!c:*backend-sc-numbers*)
272 (when sc
273 (logically-readonlyize (sb!c::sc-move-funs sc))
274 (logically-readonlyize (sb!c::sc-load-costs sc))
275 (logically-readonlyize (sb!c::sc-move-vops sc))
276 (logically-readonlyize (sb!c::sc-move-costs sc))))
278 ; hppa heap is segmented, lisp and c uses a stub to call eachother
279 #!+hpux (%primitive sb!vm::setup-return-from-lisp-stub)
280 ;; The system is finally ready for GC.
281 (/show0 "enabling GC")
282 (setq *gc-inhibit* nil)
283 (/show0 "doing first GC")
284 (gc :full t)
285 (/show0 "back from first GC")
287 ;; The show is on.
288 (terpri)
289 (/show0 "going into toplevel loop")
290 (handling-end-of-the-world
291 (toplevel-init)
292 (critically-unreachable "after TOPLEVEL-INIT")))
294 (define-deprecated-function :early "1.0.56.55" quit (exit sb!thread:abort-thread)
295 (&key recklessly-p (unix-status 0))
296 (if (or recklessly-p (sb!thread:main-thread-p))
297 (exit :code unix-status :abort recklessly-p)
298 (sb!thread:abort-thread))
299 (critically-unreachable "after trying to die in QUIT"))
301 (declaim (ftype (sfunction (&key (:code (or null exit-code))
302 (:timeout (or null real))
303 (:abort t))
304 nil)
305 exit))
306 (defun exit (&key code abort (timeout *exit-timeout*))
307 "Terminates the process, causing SBCL to exit with CODE. CODE
308 defaults to 0 when ABORT is false, and 1 when it is true.
310 When ABORT is false (the default), current thread is first unwound,
311 *EXIT-HOOKS* are run, other threads are terminated, and standard
312 output streams are flushed before SBCL calls exit(3) -- at which point
313 atexit(3) functions will run. If multiple threads call EXIT with ABORT
314 being false, the first one to call it will complete the protocol.
316 When ABORT is true, SBCL exits immediately by calling _exit(2) without
317 unwinding stack, or calling exit hooks. Note that _exit(2) does not
318 call atexit(3) functions unlike exit(3).
320 Recursive calls to EXIT cause EXIT to behave as it ABORT was true.
322 TIMEOUT controls waiting for other threads to terminate when ABORT is
323 NIL. Once current thread has been unwound and *EXIT-HOOKS* have been
324 run, spawning new threads is prevented and all other threads are
325 terminated by calling TERMINATE-THREAD on them. The system then waits
326 for them to finish using JOIN-THREAD, waiting at most a total TIMEOUT
327 seconds for all threads to join. Those threads that do not finish
328 in time are simply ignored while the exit protocol continues. TIMEOUT
329 defaults to *EXIT-TIMEOUT*, which in turn defaults to 60. TIMEOUT NIL
330 means to wait indefinitely.
332 Note that TIMEOUT applies only to JOIN-THREAD, not *EXIT-HOOKS*. Since
333 TERMINATE-THREAD is asynchronous, getting multithreaded application
334 termination with complex cleanups right using it can be tricky. To
335 perform an orderly synchronous shutdown use an exit hook instead of
336 relying on implicit thread termination.
338 Consequences are unspecified if serious conditions occur during EXIT
339 excepting errors from *EXIT-HOOKS*, which cause warnings and stop
340 execution of the hook that signaled, but otherwise allow the exit
341 process to continue normally."
342 (if (or abort *exit-in-process*)
343 (os-exit (or code 1) :abort t)
344 (let ((code (or code 0)))
345 (with-deadline (:seconds nil :override t)
346 (sb!thread:grab-mutex *exit-lock*))
347 (setf *exit-in-process* code
348 *exit-timeout* timeout)
349 (throw '%end-of-the-world t)))
350 (critically-unreachable "After trying to die in EXIT."))
352 ;;;; initialization functions
354 (defun thread-init-or-reinit ()
355 (sb!thread::init-initial-thread)
356 (sb!thread::init-job-control)
357 (sb!thread::get-foreground))
359 (defun reinit ()
360 #!+win32
361 (setf sb!win32::*ansi-codepage* nil)
362 (setf *default-external-format* nil)
363 (setf sb!alien::*default-c-string-external-format* nil)
364 ;; WITHOUT-GCING implies WITHOUT-INTERRUPTS.
365 (without-gcing
366 ;; Initialize streams first, so that any errors can be printed later
367 (stream-reinit t)
368 (os-cold-init-or-reinit)
369 (thread-init-or-reinit)
370 #!-(and win32 (not sb-thread))
371 (signal-cold-init-or-reinit)
372 (setf (extern-alien "internal_errors_enabled" int) 1)
373 (float-cold-init-or-reinit))
374 (gc-reinit)
375 (foreign-reinit)
376 (time-reinit)
377 ;; If the debugger was disabled in the saved core, we need to
378 ;; re-disable ldb again.
379 (when (eq *invoke-debugger-hook* 'sb!debug::debugger-disabled-hook)
380 (sb!debug::disable-debugger))
381 (call-hooks "initialization" *init-hooks*))
383 ;;;; some support for any hapless wretches who end up debugging cold
384 ;;;; init code
386 ;;; Decode THING into hexadecimal notation using only machinery
387 ;;; available early in cold init.
388 #!+sb-show
389 (defun hexstr (thing)
390 (/noshow0 "entering HEXSTR")
391 (let* ((addr (get-lisp-obj-address thing))
392 (nchars (* sb!vm:n-word-bytes 2))
393 (str (make-string (+ nchars 2) :element-type 'base-char)))
394 (/noshow0 "ADDR and STR calculated")
395 (setf (char str 0) #\0
396 (char str 1) #\x)
397 (/noshow0 "CHARs 0 and 1 set")
398 (dotimes (i nchars)
399 (/noshow0 "at head of DOTIMES loop")
400 (let* ((nibble (ldb (byte 4 0) addr))
401 (chr (char "0123456789abcdef" nibble)))
402 (declare (type (unsigned-byte 4) nibble)
403 (base-char chr))
404 (/noshow0 "NIBBLE and CHR calculated")
405 (setf (char str (- (1+ nchars) i)) chr
406 addr (ash addr -4))))
407 str))
409 ;; But: you almost never need this. Just use WRITE in all its glory.
410 #!+sb-show
411 (defun cold-print (x)
412 (labels ((%cold-print (obj depthoid)
413 (if (> depthoid 4)
414 (%primitive print "...")
415 (typecase obj
416 (simple-string
417 (%primitive print obj))
418 (symbol
419 (%primitive print (symbol-name obj)))
420 (cons
421 (%primitive print "cons:")
422 (let ((d (1+ depthoid)))
423 (%cold-print (car obj) d)
424 (%cold-print (cdr obj) d)))
426 (%primitive print (hexstr obj)))))))
427 (%cold-print x 0))
428 (values))
430 (in-package "SB!INT")
431 (defun !unintern-symbols ()
432 ;; For some reason uninterning these:
433 ;; DEF!TYPE DEF!CONSTANT DEF!STRUCT
434 ;; does not work, they stick around as uninterned symbols.
435 ;; Some other macros must expand into them. Ugh.
436 '("SB-INT"
437 defenum defun-cached with-globaldb-name
439 #!+sb-show ()
440 #!-sb-show (/hexstr /nohexstr /noshow /noshow0 /noxhow
441 /primitive-print /show /show0 /xhow)))