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 (defun !unintern-init-only-stuff ()
25 (do ((any-changes? nil nil))
27 (dolist (package (list-all-packages))
28 (do-symbols (symbol package)
29 (let ((name (symbol-name symbol)))
30 (when (or (string= name "!" :end1 1 :end2 1)
31 (and (>= (length name) 2)
32 (string= name "*!" :end1 2 :end2 2)))
33 (/show0 "uninterning cold-init-only symbol..")
34 #!+sb-show (%primitive print name)
35 (unintern symbol package)
36 (setf any-changes? t)))))
42 ;;; a list of toplevel things set by GENESIS
43 (defvar *!reversed-cold-toplevels*)
45 ;;; a SIMPLE-VECTOR set by genesis
46 (defvar *!load-time-values*)
48 (defun !cold-lose (msg)
49 (%primitive print msg)
50 (%primitive print "too early in cold init to recover from errors")
54 (defun do-load-time-value-fixup (object offset index)
55 (declare (type index offset))
56 (let ((value (svref *!load-time-values* index)))
60 (0 (setf (car object) value))
61 (1 (setf (cdr object) value))
62 (t (!cold-lose "bogus offset in cons cell"))))
64 (setf (%instance-ref object (- offset sb!vm:instance-slots-offset))
67 (setf (code-header-ref object offset) value))
69 (setf (svref object (- offset sb!vm:vector-data-offset)) value))
71 (!cold-lose "unknown kind of object for load-time-value fixup")))))
73 (eval-when (:compile-toplevel :execute)
74 ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
75 ;; and use it for most of the cold-init functions. (Just be careful
76 ;; not to use it for the COLD-INIT-OR-REINIT functions.)
77 (sb!xc:defmacro show-and-call (name)
79 #!+sb-show (%primitive print ,(symbol-name name))
82 ;;; called when a cold system starts up
84 #!+sb-doc "Give the world a shove and hope it spins."
86 (/show0 "entering !COLD-INIT")
88 ;; FIXME: It'd probably be cleaner to have most of the stuff here
89 ;; handled by calls like !GC-COLD-INIT, !ERROR-COLD-INIT, and
90 ;; !UNIX-COLD-INIT. And *TYPE-SYSTEM-INITIALIZED* could be changed to
91 ;; *TYPE-SYSTEM-INITIALIZED-WHEN-BOUND* so that it doesn't need to
92 ;; be explicitly set in order to be meaningful.
93 (setf *gc-verbose* nil)
94 (setf *gc-notify-stream* nil)
95 (setf *before-gc-hooks* nil)
96 (setf *after-gc-hooks* nil)
97 #!+gengc (setf *handler-clusters* nil)
98 #!-gengc (setf *already-maybe-gcing* t
100 *need-to-collect-garbage* nil
101 sb!unix::*interrupts-enabled* t
102 sb!unix::*interrupt-pending* nil)
103 (setf *break-on-signals* nil)
104 (setf *maximum-error-depth* 10)
105 (setf *current-error-depth* 0)
106 (setf *cold-init-complete-p* nil)
107 (setf *type-system-initialized* nil)
109 ;; Anyone might call RANDOM to initialize a hash value or something;
110 ;; and there's nothing which needs to be initialized in order for
111 ;; this to be initialized, so we initialize it right away.
112 (show-and-call !random-cold-init)
114 ;; All sorts of things need INFO and/or (SETF INFO).
115 (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
116 (show-and-call !globaldb-cold-init)
118 ;; This needs to be done early, but needs to be after INFO is
120 (show-and-call !fdefn-cold-init)
122 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
123 ;; the basic type machinery needs to be initialized before toplevel
125 (show-and-call !type-class-cold-init)
126 (show-and-call !typedefs-cold-init)
127 (show-and-call !classes-cold-init)
128 (show-and-call !early-type-cold-init)
129 (show-and-call !late-type-cold-init)
130 (show-and-call !alien-type-cold-init)
131 (show-and-call !target-type-cold-init)
132 (show-and-call !vm-type-cold-init)
133 ;; FIXME: It would be tidy to make sure that that these cold init
134 ;; functions are called in the same relative order as the toplevel
135 ;; forms of the corresponding source files.
137 (show-and-call !package-cold-init)
138 (show-and-call !policy-cold-init-or-resanify)
139 (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
141 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
142 ;; fixups be done separately? Wouldn't that be clearer and better?
144 (/show0 "doing cold toplevel forms and fixups")
145 (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
146 #!+sb-show (%primitive print
147 (sb!impl::hexstr (length *!reversed-cold-toplevels*)))
148 (let (#!+sb-show (index-in-cold-toplevels 0)
149 #!+sb-show (filename-in-cold-toplevels nil))
150 #!+sb-show (declare (type fixnum index-in-cold-toplevels))
151 (dolist (toplevel-thing (prog1
152 (nreverse *!reversed-cold-toplevels*)
153 ;; (Now that we've NREVERSEd it, it's
154 ;; somewhat scrambled, so keep anyone
155 ;; else from trying to get at it.)
156 (makunbound '*!reversed-cold-toplevels*)))
158 (when (zerop (mod index-in-cold-toplevels 1024))
159 (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
160 (%primitive print (sb!impl::hexstr index-in-cold-toplevels)))
162 (setf index-in-cold-toplevels
163 (the fixnum (1+ index-in-cold-toplevels)))
164 (typecase toplevel-thing
166 (funcall toplevel-thing))
168 (case (first toplevel-thing)
170 (setf (svref *!load-time-values* (third toplevel-thing))
171 (funcall (second toplevel-thing))))
172 (:load-time-value-fixup
174 (setf (sap-ref-32 (second toplevel-thing) 0)
175 (get-lisp-obj-address
176 (svref *!load-time-values* (third toplevel-thing))))
178 (do-load-time-value-fixup (second toplevel-thing)
179 (third toplevel-thing)
180 (fourth toplevel-thing)))
182 (:load-time-code-fixup
183 (sb!vm::do-load-time-code-fixup (second toplevel-thing)
184 (third toplevel-thing)
185 (fourth toplevel-thing)
186 (fifth toplevel-thing)))
188 (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
189 (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
190 (/show0 "done with loop over cold toplevel forms and fixups")
192 ;; Set sane values again, so that the user sees sane values instead of
193 ;; whatever is left over from the last DECLAIM/PROCLAIM.
194 (show-and-call !policy-cold-init-or-resanify)
196 ;; Only do this after toplevel forms have run, 'cause that's where
198 (setf *type-system-initialized* t)
200 (show-and-call os-cold-init-or-reinit)
202 (show-and-call stream-cold-init-or-reset)
203 (show-and-call !loader-cold-init)
204 (show-and-call signal-cold-init-or-reinit)
205 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
207 ;; FIXME: This list of modes should be defined in one place and
208 ;; explicitly shared between here and REINIT.
209 (set-floating-point-modes :traps '(:overflow
214 (show-and-call !class-finalize)
216 ;; The reader and printer are initialized very late, so that they
217 ;; can do hairy things like invoking the compiler as part of their
219 (show-and-call !reader-cold-init)
220 (let ((*readtable* *standard-readtable*))
221 (show-and-call !sharpm-cold-init)
222 (show-and-call !backq-cold-init))
223 (setf *readtable* (copy-readtable *standard-readtable*))
224 (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
225 (sb!pretty:!pprint-cold-init)
227 ;; the ANSI-specified initial value of *PACKAGE*
228 (setf *package* (find-package "COMMON-LISP-USER"))
229 ;; FIXME: I'm not sure where it should be done, but CL-USER really
230 ;; ought to USE-PACKAGE publicly accessible packages like SB-DEBUG
231 ;; (for ARG and VAR), SB-EXT, SB-EXT-C-CALL, and SB-EXT-ALIEN so
232 ;; that the user has a hint about which symbols we consider public.
233 ;; (Perhaps SB-DEBUG wouldn't need to be in the list if ARG and VAR
234 ;; could be typed directly, with no parentheses, at the debug prompt
235 ;; the way that e.g. F or BACKTRACE can be?)
237 (/show0 "done initializing")
238 (setf *cold-init-complete-p* t)
240 ;; The system is finally ready for GC.
241 #!-gengc (setf *already-maybe-gcing* nil)
242 (/show0 "enabling GC")
244 (/show0 "doing first GC")
246 (/show0 "back from first GC")
250 (/show0 "going into toplevel loop")
251 (handling-end-of-the-world
254 (defun quit (&key recklessly-p
255 (unix-code 0 unix-code-p)
256 (unix-status unix-code))
258 "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
259 and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
260 UNIX-STATUS is used as the status code."
261 (declare (type (signed-byte 32) unix-code))
262 ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
263 ;; around for less than a year. It should be safe to remove it after
266 (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
267 instead (which is another name for the same thing)."))
269 (sb!unix:unix-exit unix-status)
270 (throw '%end-of-the-world unix-status)))
272 ;;;; initialization functions
277 (os-cold-init-or-reinit)
279 (signal-cold-init-or-reinit)
280 (gc-cold-init-or-reinit)
281 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
282 (set-floating-point-modes :traps
283 ;; PRINT seems to not like x86 NPX denormal
284 ;; floats like LEAST-NEGATIVE-SINGLE-FLOAT, so
285 ;; the :UNDERFLOW exceptions are disabled by
286 ;; default. Joe User can explicitly enable them
288 '(:overflow #!-x86 :underflow :invalid
290 ;; Clear pseudo atomic in case this core wasn't compiled with
293 ;; FIXME: In SBCL our cores are always compiled with support. So
294 ;; we don't need to do this, do we? At least not for this
295 ;; reason.. (Perhaps we should do it anyway in case someone
296 ;; manages to save an image from within a pseudo-atomic-atomic
298 #!+x86 (setf sb!impl::*pseudo-atomic-atomic* 0))
301 ;;;; some support for any hapless wretches who end up debugging cold
304 ;;; Decode THING into hex using only machinery available early in cold
307 (defun hexstr (thing)
308 (let ((addr (sb!kernel:get-lisp-obj-address thing))
309 (str (make-string 10)))
310 (setf (char str 0) #\0
313 (let* ((nibble (ldb (byte 4 0) addr))
314 (chr (char "0123456789abcdef" nibble)))
315 (declare (type (unsigned-byte 4) nibble)
317 (setf (char str (- 9 i)) chr
318 addr (ash addr -4))))
322 (defun cold-print (x)
324 (simple-string (sb!sys:%primitive print x))
325 (symbol (sb!sys:%primitive print (symbol-name x)))
326 (list (let ((count 0))
327 (sb!sys:%primitive print "list:")
329 (when (>= (incf count) 4)
330 (sb!sys:%primitive print "...")
333 (t (sb!sys:%primitive print (hexstr x)))))