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
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 ;;;; burning our ships behind us
17 ;;; There's a fair amount of machinery which is needed only at cold
18 ;;; init time, and should be discarded before freezing the final
19 ;;; system. We discard it by uninterning the associated symbols.
20 ;;; Rather than using a special table of symbols to be uninterned,
21 ;;; which might be tedious to maintain, instead we use a hack:
22 ;;; anything whose name matches a magic character pattern is
24 ;;; Additionally, you can specify an arbitrary way to destroy
25 ;;; random bootstrap stuff on per-package basis.
26 (defun !unintern-init-only-stuff
()
27 (dolist (package (list-all-packages))
28 (awhen (find-symbol "UNINTERN-INIT-ONLY-STUFF" package
)
29 (format t
"~&Calling ~/sb-impl::print-symbol-with-prefix/~%" it
)
31 (unintern it package
)))
32 (flet ((uninternable-p (symbol)
33 (let ((name (symbol-name symbol
)))
34 (or (and (>= (length name
) 1) (char= (char name
0) #\
!))
35 (and (>= (length name
) 2) (string= name
"*!" :end1
2))
37 ;; DEF!METHOD need no longer be accessible,
38 ;; but *DELAYED-DEF!METHOD-ARGS* remains,
39 ;; due to a reference from pcl/methods.lisp.
40 ;; It would be nice to fix that.
42 sb
!c
::sb
!pcl sb
!c
::sb
!impl sb
!c
::sb
!kernel
43 sb
!c
::sb
!c sb
!c
::sb
!int
))))))
44 ;; A structure constructor name, in particular !MAKE-SAETP,
45 ;; can't be uninterned if referenced by a defstruct-description.
46 ;; So loop over all structure classoids and clobber any
47 ;; symbol that should be uninternable.
48 (maphash (lambda (classoid layout
)
49 (when (structure-classoid-p classoid
)
50 (let ((dd (layout-info layout
)))
51 (setf (dd-constructors dd
)
52 (delete-if (lambda (x)
53 (and (consp x
) (uninternable-p (car x
))))
54 (dd-constructors dd
))))))
55 (classoid-subclasses (find-classoid t
)))
56 ;; Todo: perform one pass, then a full GC, then a final pass to confirm
57 ;; it worked. It shoud be an error if any uninternable symbols remain,
58 ;; but at present there are about 13 other "!" symbols with referers.
59 (with-package-iterator (iter (list-all-packages) :internal
:external
)
60 (loop (multiple-value-bind (winp symbol accessibility package
) (iter)
61 (declare (ignore accessibility
))
64 (when (uninternable-p symbol
)
65 ;; Uninternable symbols which are referenced by other stuff
66 ;; can't disappear from the image, but we don't need to preserve
67 ;; their functions, so FMAKUNBOUND them. This doesn't have
68 ;; the intended effect if the function shares a code-component
69 ;; with non-cold-init lambdas, such as in !CONSTANTP-COLD-INIT
70 ;; and !GLOBALDB-COLD-INIT. Though the cold-init function is
71 ;; never called post-build, it is not discarded. Also, I suspect
72 ;; that the following loop should print nothing, but it does:
74 (sb-vm::map-allocated-objects
75 (lambda (obj type size
)
76 (declare (ignore size
))
77 (when (= type sb-vm
:code-header-widetag
)
78 (let ((name (sb-c::debug-info-name
79 (sb-kernel:%code-debug-info obj
))))
80 (when (and (stringp name
) (search "COLD-INIT-FORMS" name
))
85 (unintern symbol package
)))))))
87 ;;;; putting ourselves out of our misery when things become too much to bear
89 (declaim (ftype (function (simple-string) nil
) !cold-lose
))
90 (defun !cold-lose
(msg)
91 (%primitive print msg
)
92 (%primitive print
"too early in cold init to recover from errors")
95 ;;; last-ditch error reporting for things which should never happen
96 ;;; and which, if they do happen, are sufficiently likely to torpedo
97 ;;; the normal error-handling system that we want to bypass it
98 (declaim (ftype (function (simple-string) nil
) critically-unreachable
))
99 (defun critically-unreachable (where)
100 (%primitive print
"internal error: Control should never reach here, i.e.")
101 (%primitive print where
)
106 ;;; a list of toplevel things set by GENESIS
107 (defvar *!reversed-cold-toplevels
*)
108 (!defvar
*!reversed-cold-setf-macros
* nil
) ; just SETF macros
109 (!defvar
*!reversed-cold-defuns
* nil
) ; just DEFUNs
111 ;;; a SIMPLE-VECTOR set by GENESIS
112 (defvar *!load-time-values
*)
114 (eval-when (:compile-toplevel
:execute
)
115 ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
116 ;; and use it for most of the cold-init functions. (Just be careful
117 ;; not to use it for the COLD-INIT-OR-REINIT functions.)
118 (sb!xc
:defmacro show-and-call
(name)
120 (/primitive-print
,(symbol-name name
))
123 ;;; called when a cold system starts up
125 #!+sb-doc
"Give the world a shove and hope it spins."
128 (sb!int
::cannot-
/show
"Test of CANNOT-/SHOW [don't worry - this is expected]")
129 (/show0
"entering !COLD-INIT")
130 (setq *readtable
* (make-readtable)
132 *previous-readtable-case
* nil
133 *print-length
* 6 *print-level
* 3)
135 (write-string "COLD-INIT... "
136 (setq *error-output
* (!make-cold-stderr-stream
)
137 *standard-output
* *error-output
*
138 *trace-output
* *error-output
*))
140 ;; Assert that FBOUNDP doesn't choke when its answer is NIL.
141 ;; It was fine if T because in that case the legality of the arg is certain.
142 ;; And be extra paranoid - ensure that it really gets called.
143 (locally (declare (notinline fboundp
)) (fboundp '(setf !zzzzzz
)))
145 ;; Putting data in a synchronized hashtable (*PACKAGE-NAMES*)
146 ;; requires that the main thread be properly initialized.
147 (show-and-call thread-init-or-reinit
)
148 ;; Printing of symbols requires that packages be filled in, because
149 ;; OUTPUT-SYMBOL calls FIND-SYMBOL to determine accessibility.
150 (show-and-call !package-cold-init
)
151 ;; Fill in the printer's character attribute tables now.
152 ;; If Genesis could write constant arrays into a target core,
153 ;; that would be nice, and would tidy up some other things too.
154 (show-and-call !printer-cold-init
)
156 (progn (prin1 `(package = ,(package-name *package
*)))
159 ;; *RAW-SLOT-DATA* is essentially a compile-time constant
160 ;; but isn't dumpable as such because it has functions in it.
161 (show-and-call sb
!kernel
::!raw-slot-data-init
)
163 ;; Anyone might call RANDOM to initialize a hash value or something;
164 ;; and there's nothing which needs to be initialized in order for
165 ;; this to be initialized, so we initialize it right away.
166 (show-and-call !random-cold-init
)
168 ;; Must be done before any non-opencoded array references are made.
169 (show-and-call !hairy-data-vector-reffer-init
)
171 (show-and-call !character-database-cold-init
)
172 (show-and-call !character-name-database-cold-init
)
173 (show-and-call sb
!unicode
::!unicode-properties-cold-init
)
175 ;; All sorts of things need INFO and/or (SETF INFO).
176 (/show0
"about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
177 (show-and-call !globaldb-cold-init
)
179 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
180 ;; the basic type machinery needs to be initialized before toplevel
182 (show-and-call !type-class-cold-init
)
183 (show-and-call !world-lock-cold-init
)
184 (show-and-call !classes-cold-init
)
185 (show-and-call !early-type-cold-init
)
186 (show-and-call !late-type-cold-init
)
187 ;; See comment at the DEFUN explaining why there are 2 of them.
188 (show-and-call sb
!kernel
::!late-type-cold-init2
)
189 (show-and-call !alien-type-cold-init
)
190 (show-and-call !target-type-cold-init
)
191 ;; FIXME: It would be tidy to make sure that that these cold init
192 ;; functions are called in the same relative order as the toplevel
193 ;; forms of the corresponding source files.
195 (show-and-call !policy-cold-init-or-resanify
)
196 (/show0
"back from !POLICY-COLD-INIT-OR-RESANIFY")
198 (show-and-call !constantp-cold-init
)
199 (show-and-call !constantp2-cold-init
)
200 ;; Must be done before toplevel forms are invoked
201 ;; because a toplevel defstruct will need to add itself
202 ;; to the subclasses of STRUCTURE-OBJECT.
203 (show-and-call sb
!kernel
::!set-up-structure-object-class
)
205 (dolist (x (nreverse *!reversed-cold-setf-macros
*))
206 (apply #'!quietly-defsetf x
))
207 (dolist (x (nreverse *!reversed-cold-defuns
*))
208 (destructuring-bind (name . inline-expansion
) x
209 (!%quietly-defun
name inline-expansion
)))
211 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
212 ;; fixups be done separately? Wouldn't that be clearer and better?
214 (/show0
"doing cold toplevel forms and fixups")
215 (/show0
"(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
216 (/hexstr
(if (listp *!reversed-cold-toplevels
*) "true" "NIL"))
218 (progn (write `("Length(TLFs)= " ,(length *!reversed-cold-toplevels
*)))
221 (progn (/show0
"about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
222 (/show0
"(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
223 #!+sb-show
(let ((r-c-tl-length (length *!reversed-cold-toplevels
*)))
224 (/show0
"(length calculated..)")
225 (let ((hexstr (hexstr r-c-tl-length
)))
226 (/show0
"(hexstr calculated..)")
227 (/primitive-print hexstr
))))
228 (let (#!+sb-show
(index-in-cold-toplevels 0))
229 #!+sb-show
(declare (type fixnum index-in-cold-toplevels
))
232 'find-package
'!bootstrap
233 (lambda (f designator
)
234 (cond ((packagep designator
) designator
)
235 (t (funcall f
(let ((s (string designator
)))
236 (if (eql (mismatch s
"SB!") 3)
237 (concatenate 'string
"SB-" (subseq s
3))
239 (encapsulate '%failed-aver
'!bootstrap
241 ;; output the message before signaling error,
242 ;; as it may be this is too early in the cold init.
244 (write-line "failed AVER:")
249 (dolist (toplevel-thing (prog1
250 (nreverse *!reversed-cold-toplevels
*)
251 ;; (Now that we've NREVERSEd it, it's
252 ;; somewhat scrambled, so keep anyone
253 ;; else from trying to get at it.)
254 (makunbound '*!reversed-cold-toplevels
*)))
256 (when (zerop (mod index-in-cold-toplevels
1024))
257 (/show0
"INDEX-IN-COLD-TOPLEVELS=..")
258 (/hexstr index-in-cold-toplevels
))
260 (setf index-in-cold-toplevels
261 (the fixnum
(1+ index-in-cold-toplevels
)))
262 (typecase toplevel-thing
264 (funcall toplevel-thing
))
266 (case (first toplevel-thing
)
268 (setf (svref *!load-time-values
* (third toplevel-thing
))
269 (funcall (second toplevel-thing
))))
270 (:load-time-value-fixup
271 (setf (sap-ref-word (int-sap (get-lisp-obj-address (second toplevel-thing
)))
272 (third toplevel-thing
))
273 (get-lisp-obj-address
274 (svref *!load-time-values
* (fourth toplevel-thing
)))))
276 (apply 'sb
!kernel
::%defstruct
(cdr toplevel-thing
)))
278 (!cold-lose
"bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
279 (t (!cold-lose
"bogus operation in *!REVERSED-COLD-TOPLEVELS*")))))
280 (/show0
"done with loop over cold toplevel forms and fixups")
281 (unencapsulate '%failed-aver
'!bootstrap
)
282 (unencapsulate 'find-package
'!bootstrap
)
284 (show-and-call time-reinit
)
286 ;; Set sane values again, so that the user sees sane values instead
287 ;; of whatever is left over from the last DECLAIM/PROCLAIM.
288 (show-and-call !policy-cold-init-or-resanify
)
290 ;; Only do this after toplevel forms have run, 'cause that's where
292 (setf *type-system-initialized
* t
)
294 ;; now that the type system is definitely initialized, fixup UNKNOWN
295 ;; types that have crept in.
296 (show-and-call !fixup-type-cold-init
)
297 ;; run the PROCLAIMs.
298 (show-and-call !late-proclaim-cold-init
)
300 (show-and-call os-cold-init-or-reinit
)
301 (show-and-call !pathname-cold-init
)
302 (show-and-call !debug-info-cold-init
)
304 (show-and-call stream-cold-init-or-reset
)
305 (show-and-call !loader-cold-init
)
306 (show-and-call !foreign-cold-init
)
307 #!-
(and win32
(not sb-thread
))
308 (show-and-call signal-cold-init-or-reinit
)
310 (show-and-call float-cold-init-or-reinit
)
312 (show-and-call !class-finalize
)
314 ;; The reader and printer are initialized very late, so that they
315 ;; can do hairy things like invoking the compiler as part of their
317 (let ((*readtable
* (make-readtable)))
318 (show-and-call !reader-cold-init
)
319 (show-and-call !sharpm-cold-init
)
320 (show-and-call !backq-cold-init
)
321 ;; The *STANDARD-READTABLE* is assigned at last because the above
322 ;; functions would operate on the standard readtable otherwise---
323 ;; which would result in an error.
324 (setf *standard-readtable
* *readtable
*))
325 (setf *readtable
* (copy-readtable *standard-readtable
*))
326 (setf sb
!debug
:*debug-readtable
* (copy-readtable *standard-readtable
*))
327 (sb!pretty
:!pprint-cold-init
)
328 (setq *print-level
* nil
*print-length
* nil
) ; restore defaults
330 ;; the ANSI-specified initial value of *PACKAGE*
331 (setf *package
* (find-package "COMMON-LISP-USER"))
333 ;; Enable normal (post-cold-init) behavior of INFINITE-ERROR-PROTECT.
334 (setf sb
!kernel
::*maximum-error-depth
* 10)
335 (/show0
"enabling internal errors")
336 (setf (extern-alien "internal_errors_enabled" int
) 1)
339 ; hppa heap is segmented, lisp and c uses a stub to call eachother
340 #!+hpux
(%primitive sb
!vm
::setup-return-from-lisp-stub
)
341 ;; The system is finally ready for GC.
342 (/show0
"enabling GC")
343 (setq *gc-inhibit
* nil
)
344 (/show0
"doing first GC")
346 (/show0
"back from first GC")
350 (/show0
"going into toplevel loop")
351 (handling-end-of-the-world
353 (critically-unreachable "after TOPLEVEL-INIT")))
355 (define-deprecated-function :early
"1.0.56.55" quit
(exit sb
!thread
:abort-thread
)
356 (&key recklessly-p
(unix-status 0))
357 (if (or recklessly-p
(sb!thread
:main-thread-p
))
358 (exit :code unix-status
:abort recklessly-p
)
359 (sb!thread
:abort-thread
))
360 (critically-unreachable "after trying to die in QUIT"))
362 (declaim (ftype (sfunction (&key
(:code
(or null exit-code
))
363 (:timeout
(or null real
))
367 (defun exit (&key code abort
(timeout *exit-timeout
*))
369 "Terminates the process, causing SBCL to exit with CODE. CODE
370 defaults to 0 when ABORT is false, and 1 when it is true.
372 When ABORT is false (the default), current thread is first unwound,
373 *EXIT-HOOKS* are run, other threads are terminated, and standard
374 output streams are flushed before SBCL calls exit(3) -- at which point
375 atexit(3) functions will run. If multiple threads call EXIT with ABORT
376 being false, the first one to call it will complete the protocol.
378 When ABORT is true, SBCL exits immediately by calling _exit(2) without
379 unwinding stack, or calling exit hooks. Note that _exit(2) does not
380 call atexit(3) functions unlike exit(3).
382 Recursive calls to EXIT cause EXIT to behave as it ABORT was true.
384 TIMEOUT controls waiting for other threads to terminate when ABORT is
385 NIL. Once current thread has been unwound and *EXIT-HOOKS* have been
386 run, spawning new threads is prevented and all other threads are
387 terminated by calling TERMINATE-THREAD on them. The system then waits
388 for them to finish using JOIN-THREAD, waiting at most a total TIMEOUT
389 seconds for all threads to join. Those threads that do not finish
390 in time are simply ignored while the exit protocol continues. TIMEOUT
391 defaults to *EXIT-TIMEOUT*, which in turn defaults to 60. TIMEOUT NIL
392 means to wait indefinitely.
394 Note that TIMEOUT applies only to JOIN-THREAD, not *EXIT-HOOKS*. Since
395 TERMINATE-THREAD is asynchronous, getting multithreaded application
396 termination with complex cleanups right using it can be tricky. To
397 perform an orderly synchronous shutdown use an exit hook instead of
398 relying on implicit thread termination.
400 Consequences are unspecified if serious conditions occur during EXIT
401 excepting errors from *EXIT-HOOKS*, which cause warnings and stop
402 execution of the hook that signaled, but otherwise allow the exit
403 process to continue normally."
404 (if (or abort
*exit-in-process
*)
405 (os-exit (or code
1) :abort t
)
406 (let ((code (or code
0)))
407 (with-deadline (:seconds nil
:override t
)
408 (sb!thread
:grab-mutex
*exit-lock
*))
409 (setf *exit-in-process
* code
410 *exit-timeout
* timeout
)
411 (throw '%end-of-the-world t
)))
412 (critically-unreachable "After trying to die in EXIT."))
414 ;;;; initialization functions
416 (defun thread-init-or-reinit ()
417 (sb!thread
::init-initial-thread
)
418 (sb!thread
::init-job-control
)
419 (sb!thread
::get-foreground
))
423 (setf sb
!win32
::*ansi-codepage
* nil
)
424 (setf *default-external-format
* nil
)
425 (setf sb
!alien
::*default-c-string-external-format
* nil
)
426 ;; WITHOUT-GCING implies WITHOUT-INTERRUPTS.
428 ;; Initialize streams first, so that any errors can be printed later
430 (os-cold-init-or-reinit)
431 (thread-init-or-reinit)
432 #!-
(and win32
(not sb-thread
))
433 (signal-cold-init-or-reinit)
434 (setf (extern-alien "internal_errors_enabled" int
) 1)
435 (float-cold-init-or-reinit))
439 ;; If the debugger was disabled in the saved core, we need to
440 ;; re-disable ldb again.
441 (when (eq *invoke-debugger-hook
* 'sb
!debug
::debugger-disabled-hook
)
442 (sb!debug
::disable-debugger
))
443 (call-hooks "initialization" *init-hooks
*))
445 ;;;; some support for any hapless wretches who end up debugging cold
448 ;;; Decode THING into hexadecimal notation using only machinery
449 ;;; available early in cold init.
451 (defun hexstr (thing)
452 (/noshow0
"entering HEXSTR")
453 (let* ((addr (get-lisp-obj-address thing
))
454 (nchars (* sb
!vm
:n-word-bytes
2))
455 (str (make-string (+ nchars
2) :element-type
'base-char
)))
456 (/noshow0
"ADDR and STR calculated")
457 (setf (char str
0) #\
0
459 (/noshow0
"CHARs 0 and 1 set")
461 (/noshow0
"at head of DOTIMES loop")
462 (let* ((nibble (ldb (byte 4 0) addr
))
463 (chr (char "0123456789abcdef" nibble
)))
464 (declare (type (unsigned-byte 4) nibble
)
466 (/noshow0
"NIBBLE and CHR calculated")
467 (setf (char str
(- (1+ nchars
) i
)) chr
468 addr
(ash addr -
4))))
471 ;; But: you almost never need this. Just use WRITE in all its glory.
473 (defun cold-print (x)
474 (labels ((%cold-print
(obj depthoid
)
476 (%primitive print
"...")
479 (%primitive print obj
))
481 (%primitive print
(symbol-name obj
)))
483 (%primitive print
"cons:")
484 (let ((d (1+ depthoid
)))
485 (%cold-print
(car obj
) d
)
486 (%cold-print
(cdr obj
) d
)))
488 (%primitive print
(hexstr obj
)))))))
492 (in-package "SB!INT")
493 (defun unintern-init-only-stuff ()
494 (let ((this-package (find-package "SB-INT")))
495 ;; For some reason uninterning these:
496 ;; DEF!TYPE DEF!CONSTANT DEF!MACRO DEF!STRUCT
497 ;; does not work, they stick around as uninterned symbols.
498 ;; Some other macros must expand into them. Ugh.
499 (dolist (s '(defenum defmacro-mundanely defun-cached
503 #!-sb-show
(/hexstr
/nohexstr
/noshow
/noshow0
/noxhow
504 /primitive-print
/show
/show0
/xhow
)))
505 (unintern s this-package
))))