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
25 ;;; FIXME: should also go through globaldb (and perhaps other tables)
26 ;;; blowing away associated entries
27 (defun !unintern-init-only-stuff ()
28 (do ((any-changes? nil nil))
30 (dolist (package (list-all-packages))
31 (do-symbols (symbol package)
32 (let ((name (symbol-name symbol)))
33 (when (or (string= name "!" :end1 1 :end2 1)
34 (and (>= (length name) 2)
35 (string= name "*!" :end1 2 :end2 2)))
36 (/show0 "uninterning cold-init-only symbol..")
37 (/primitive-print name)
38 (unintern symbol package)
39 (setf any-changes? t)))))
43 ;;;; putting ourselves out of our misery when things become too much to bear
45 (declaim (ftype (function (simple-string) nil) critically-unreachable))
46 (defun !cold-lose (msg)
47 (%primitive print msg)
48 (%primitive print "too early in cold init to recover from errors")
51 ;;; last-ditch error reporting for things which should never happen
52 ;;; and which, if they do happen, are sufficiently likely to torpedo
53 ;;; the normal error-handling system that we want to bypass it
54 (declaim (ftype (function (simple-string) nil) critically-unreachable))
55 (defun critically-unreachable (where)
56 (%primitive print "internal error: Control should never reach here, i.e.")
57 (%primitive print where)
62 ;;; a list of toplevel things set by GENESIS
63 (defvar *!reversed-cold-toplevels*)
65 ;;; a SIMPLE-VECTOR set by GENESIS
66 (defvar *!load-time-values*)
68 (eval-when (:compile-toplevel :execute)
69 ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
70 ;; and use it for most of the cold-init functions. (Just be careful
71 ;; not to use it for the COLD-INIT-OR-REINIT functions.)
72 (sb!xc:defmacro show-and-call (name)
74 (/primitive-print ,(symbol-name name))
77 ;;; called when a cold system starts up
79 #!+sb-doc "Give the world a shove and hope it spins."
81 (/show0 "entering !COLD-INIT")
83 ;; FIXME: It'd probably be cleaner to have most of the stuff here
84 ;; handled by calls like !GC-COLD-INIT, !ERROR-COLD-INIT, and
85 ;; !UNIX-COLD-INIT. And *TYPE-SYSTEM-INITIALIZED* could be changed to
86 ;; *TYPE-SYSTEM-INITIALIZED-WHEN-BOUND* so that it doesn't need to
87 ;; be explicitly set in order to be meaningful.
88 (setf *gc-notify-stream* nil
91 *already-maybe-gcing* t
93 *need-to-collect-garbage* nil
94 sb!unix::*interrupts-enabled* t
95 sb!unix::*interrupt-pending* nil
96 *break-on-signals* nil
97 *maximum-error-depth* 10
98 *current-error-depth* 0
99 *cold-init-complete-p* nil
100 *type-system-initialized* nil)
102 (show-and-call !typecheckfuns-cold-init)
104 ;; Anyone might call RANDOM to initialize a hash value or something;
105 ;; and there's nothing which needs to be initialized in order for
106 ;; this to be initialized, so we initialize it right away.
107 (show-and-call !random-cold-init)
109 (show-and-call !package-cold-init)
111 ;; All sorts of things need INFO and/or (SETF INFO).
112 (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
113 (show-and-call !globaldb-cold-init)
115 ;; This needs to be done early, but needs to be after INFO is
117 (show-and-call !fdefn-cold-init)
119 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
120 ;; the basic type machinery needs to be initialized before toplevel
122 (show-and-call !type-class-cold-init)
123 (show-and-call !typedefs-cold-init)
124 (show-and-call !classes-cold-init)
125 (show-and-call !early-type-cold-init)
126 (show-and-call !late-type-cold-init)
127 (show-and-call !alien-type-cold-init)
128 (show-and-call !target-type-cold-init)
129 (show-and-call !vm-type-cold-init)
130 ;; FIXME: It would be tidy to make sure that that these cold init
131 ;; functions are called in the same relative order as the toplevel
132 ;; forms of the corresponding source files.
134 ;;(show-and-call !package-cold-init)
135 (show-and-call !policy-cold-init-or-resanify)
136 (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
138 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
139 ;; fixups be done separately? Wouldn't that be clearer and better?
141 (/show0 "doing cold toplevel forms and fixups")
142 (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
143 (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
144 (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
145 (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
146 #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
147 (/show0 "(length calculated..)")
148 (let ((hexstr (hexstr r-c-tl-length)))
149 (/show0 "(hexstr calculated..)")
150 (/primitive-print hexstr)))
151 (let (#!+sb-show (index-in-cold-toplevels 0))
152 #!+sb-show (declare (type fixnum index-in-cold-toplevels))
154 (dolist (toplevel-thing (prog1
155 (nreverse *!reversed-cold-toplevels*)
156 ;; (Now that we've NREVERSEd it, it's
157 ;; somewhat scrambled, so keep anyone
158 ;; else from trying to get at it.)
159 (makunbound '*!reversed-cold-toplevels*)))
161 (when (zerop (mod index-in-cold-toplevels 1024))
162 (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
163 (/hexstr index-in-cold-toplevels))
165 (setf index-in-cold-toplevels
166 (the fixnum (1+ index-in-cold-toplevels)))
167 (typecase toplevel-thing
169 (funcall toplevel-thing))
171 (case (first toplevel-thing)
173 (setf (svref *!load-time-values* (third toplevel-thing))
174 (funcall (second toplevel-thing))))
175 (:load-time-value-fixup
176 (setf (sap-ref-32 (second toplevel-thing) 0)
177 (get-lisp-obj-address
178 (svref *!load-time-values* (third toplevel-thing)))))
180 (:load-time-code-fixup
181 (sb!vm::!envector-load-time-code-fixup (second toplevel-thing)
182 (third toplevel-thing)
183 (fourth toplevel-thing)
184 (fifth toplevel-thing)))
186 (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
187 (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
188 (/show0 "done with loop over cold toplevel forms and fixups")
190 ;; Set sane values again, so that the user sees sane values instead
191 ;; of whatever is left over from the last DECLAIM/PROCLAIM.
192 (show-and-call !policy-cold-init-or-resanify)
194 ;; Only do this after toplevel forms have run, 'cause that's where
196 (setf *type-system-initialized* t)
198 (show-and-call os-cold-init-or-reinit)
200 (show-and-call stream-cold-init-or-reset)
201 (show-and-call !loader-cold-init)
202 (show-and-call signal-cold-init-or-reinit)
203 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
205 ;; FIXME: This list of modes should be defined in one place and
206 ;; explicitly shared between here and REINIT.
208 ;; FIXME: In CMU CL, this is done "here" (i.e. in the analogous
209 ;; lispinit.lisp code) for every processor architecture. But Daniel
210 ;; Barlow's Alpha patches suppress it for Alpha. Why the difference?
212 (set-floating-point-modes :traps '(:overflow
217 (show-and-call !class-finalize)
219 ;; The reader and printer are initialized very late, so that they
220 ;; can do hairy things like invoking the compiler as part of their
222 (show-and-call !reader-cold-init)
223 (let ((*readtable* *standard-readtable*))
224 (show-and-call !sharpm-cold-init)
225 (show-and-call !backq-cold-init))
226 (setf *readtable* (copy-readtable *standard-readtable*))
227 (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
228 (sb!pretty:!pprint-cold-init)
230 ;; the ANSI-specified initial value of *PACKAGE*
231 (setf *package* (find-package "COMMON-LISP-USER"))
233 (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
234 (setf *cold-init-complete-p* t)
236 ;; The system is finally ready for GC.
237 (setf *already-maybe-gcing* nil)
238 (/show0 "enabling GC")
240 (/show0 "doing first GC")
242 (/show0 "back from first GC")
246 (/show0 "going into toplevel loop")
247 (handling-end-of-the-world
249 (critically-unreachable "after TOPLEVEL-INIT")))
251 (defun quit (&key recklessly-p
252 (unix-code 0 unix-code-p)
253 (unix-status unix-code))
255 "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
256 and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
257 UNIX-STATUS is used as the status code."
258 (declare (type (signed-byte 32) unix-status unix-code))
259 (/show0 "entering QUIT")
260 ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
261 ;; around for less than a year. It should be safe to remove it after
264 (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
265 instead (which is another name for the same thing)."))
267 (sb!unix:unix-exit unix-status)
268 (throw '%end-of-the-world unix-status))
269 (critically-unreachable "after trying to die in QUIT"))
271 ;;;; initialization functions
276 (os-cold-init-or-reinit)
278 (signal-cold-init-or-reinit)
280 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
281 (set-floating-point-modes :traps
285 ;; PRINT seems not to like x86 NPX
286 ;; denormal floats like
287 ;; LEAST-NEGATIVE-SINGLE-FLOAT, so
288 ;; the :UNDERFLOW exceptions are
289 ;; disabled by default. Joe User can
290 ;; explicitly enable them if
292 #!+alpha :underflow))
293 ;; Clear pseudo atomic in case this core wasn't compiled with
296 ;; FIXME: In SBCL our cores are always compiled with support. So
297 ;; we don't need to do this, do we? At least not for this
298 ;; reason.. (Perhaps we should do it anyway in case someone
299 ;; manages to save an image from within a pseudo-atomic-atomic
301 #!+x86 (setf *pseudo-atomic-atomic* 0))
304 ;;;; some support for any hapless wretches who end up debugging cold
307 ;;; Decode THING into hexadecimal notation using only machinery
308 ;;; available early in cold init.
310 (defun hexstr (thing)
311 (/noshow0 "entering HEXSTR")
312 (let ((addr (get-lisp-obj-address thing))
313 (str (make-string 10)))
314 (/noshow0 "ADDR and STR calculated")
315 (setf (char str 0) #\0
317 (/noshow0 "CHARs 0 and 1 set")
319 (/noshow0 "at head of DOTIMES loop")
320 (let* ((nibble (ldb (byte 4 0) addr))
321 (chr (char "0123456789abcdef" nibble)))
322 (declare (type (unsigned-byte 4) nibble)
324 (/noshow0 "NIBBLE and CHR calculated")
325 (setf (char str (- 9 i)) chr
326 addr (ash addr -4))))
330 (defun cold-print (x)
332 (simple-string (sb!sys:%primitive print x))
333 (symbol (sb!sys:%primitive print (symbol-name x)))
334 (list (let ((count 0))
335 (sb!sys:%primitive print "list:")
337 (when (>= (incf count) 4)
338 (sb!sys:%primitive print "...")
341 (t (sb!sys:%primitive print (hexstr x)))))