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: Are there other tables that need to have entries removed?
26 ;;; What about symbols of the form DEF!FOO?
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 ;; FIXME: Is this (FIRST (LAST *INFO-ENVIRONMENT*)) really
39 ;; meant to be an idiom to use? Is there a more obvious
40 ;; name for this? [e.g. (GLOBAL-ENVIRONMENT)?]
41 (do-info ((first (last *info-environment*))
42 :name entry :class class :type type)
43 (when (eq entry symbol)
44 (clear-info class type entry)))
45 (unintern symbol package)
46 (setf any-changes? t)))))
50 ;;;; putting ourselves out of our misery when things become too much to bear
52 (declaim (ftype (function (simple-string) nil) critically-unreachable))
53 (defun !cold-lose (msg)
54 (%primitive print msg)
55 (%primitive print "too early in cold init to recover from errors")
58 ;;; last-ditch error reporting for things which should never happen
59 ;;; and which, if they do happen, are sufficiently likely to torpedo
60 ;;; the normal error-handling system that we want to bypass it
61 (declaim (ftype (function (simple-string) nil) critically-unreachable))
62 (defun critically-unreachable (where)
63 (%primitive print "internal error: Control should never reach here, i.e.")
64 (%primitive print where)
69 ;;; a list of toplevel things set by GENESIS
70 (defvar *!reversed-cold-toplevels*)
72 ;;; a SIMPLE-VECTOR set by GENESIS
73 (defvar *!load-time-values*)
75 (eval-when (:compile-toplevel :execute)
76 ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
77 ;; and use it for most of the cold-init functions. (Just be careful
78 ;; not to use it for the COLD-INIT-OR-REINIT functions.)
79 (sb!xc:defmacro show-and-call (name)
81 (/primitive-print ,(symbol-name name))
84 ;;; called when a cold system starts up
86 #!+sb-doc "Give the world a shove and hope it spins."
88 (/show0 "entering !COLD-INIT")
90 ;; FIXME: It'd probably be cleaner to have most of the stuff here
91 ;; handled by calls like !GC-COLD-INIT, !ERROR-COLD-INIT, and
92 ;; !UNIX-COLD-INIT. And *TYPE-SYSTEM-INITIALIZED* could be changed to
93 ;; *TYPE-SYSTEM-INITIALIZED-WHEN-BOUND* so that it doesn't need to
94 ;; be explicitly set in order to be meaningful.
95 (setf *before-gc-hooks* nil
98 *need-to-collect-garbage* nil
99 sb!unix::*interrupts-enabled* t
100 sb!unix::*interrupt-pending* nil
101 *break-on-signals* nil
102 *maximum-error-depth* 10
103 *current-error-depth* 0
104 *cold-init-complete-p* nil
105 *type-system-initialized* nil)
107 (show-and-call !typecheckfuns-cold-init)
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 (show-and-call !package-cold-init)
116 ;; All sorts of things need INFO and/or (SETF INFO).
117 (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
118 (show-and-call !globaldb-cold-init)
120 ;; This needs to be done early, but needs to be after INFO is
122 (show-and-call !function-names-cold-init)
123 (show-and-call !fdefn-cold-init)
125 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
126 ;; the basic type machinery needs to be initialized before toplevel
128 (show-and-call !type-class-cold-init)
129 (show-and-call !typedefs-cold-init)
130 (show-and-call !classes-cold-init)
131 (show-and-call !early-type-cold-init)
132 (show-and-call !late-type-cold-init)
133 (show-and-call !alien-type-cold-init)
134 (show-and-call !target-type-cold-init)
135 (show-and-call !vm-type-cold-init)
136 ;; FIXME: It would be tidy to make sure that that these cold init
137 ;; functions are called in the same relative order as the toplevel
138 ;; forms of the corresponding source files.
140 ;;(show-and-call !package-cold-init)
141 (show-and-call !policy-cold-init-or-resanify)
142 (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
144 (show-and-call !early-proclaim-cold-init)
146 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
147 ;; fixups be done separately? Wouldn't that be clearer and better?
149 (/show0 "doing cold toplevel forms and fixups")
150 (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
151 (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
152 (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
153 (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
154 #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
155 (/show0 "(length calculated..)")
156 (let ((hexstr (hexstr r-c-tl-length)))
157 (/show0 "(hexstr calculated..)")
158 (/primitive-print hexstr)))
159 (let (#!+sb-show (index-in-cold-toplevels 0))
160 #!+sb-show (declare (type fixnum index-in-cold-toplevels))
162 (dolist (toplevel-thing (prog1
163 (nreverse *!reversed-cold-toplevels*)
164 ;; (Now that we've NREVERSEd it, it's
165 ;; somewhat scrambled, so keep anyone
166 ;; else from trying to get at it.)
167 (makunbound '*!reversed-cold-toplevels*)))
169 (when (zerop (mod index-in-cold-toplevels 1024))
170 (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
171 (/hexstr index-in-cold-toplevels))
173 (setf index-in-cold-toplevels
174 (the fixnum (1+ index-in-cold-toplevels)))
175 (typecase toplevel-thing
177 (funcall toplevel-thing))
179 (case (first toplevel-thing)
181 (setf (svref *!load-time-values* (third toplevel-thing))
182 (funcall (second toplevel-thing))))
183 (:load-time-value-fixup
184 (setf (sap-ref-32 (second toplevel-thing) 0)
185 (get-lisp-obj-address
186 (svref *!load-time-values* (third toplevel-thing)))))
188 (:load-time-code-fixup
189 (sb!vm::!envector-load-time-code-fixup (second toplevel-thing)
190 (third toplevel-thing)
191 (fourth toplevel-thing)
192 (fifth toplevel-thing)))
194 (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
195 (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
196 (/show0 "done with loop over cold toplevel forms and fixups")
198 ;; Set sane values again, so that the user sees sane values instead
199 ;; of whatever is left over from the last DECLAIM/PROCLAIM.
200 (show-and-call !policy-cold-init-or-resanify)
202 ;; Only do this after toplevel forms have run, 'cause that's where
204 (setf *type-system-initialized* t)
206 ;; now that the type system is definitely initialized, fixup UNKNOWN
207 ;; types that have crept in.
208 (show-and-call !fixup-type-cold-init)
209 ;; run the PROCLAIMs.
210 (show-and-call !late-proclaim-cold-init)
212 (show-and-call os-cold-init-or-reinit)
214 (show-and-call stream-cold-init-or-reset)
215 (show-and-call !loader-cold-init)
216 (show-and-call signal-cold-init-or-reinit)
217 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
219 ;; FIXME: This list of modes should be defined in one place and
220 ;; explicitly shared between here and REINIT.
222 ;; Why was this marked #!+alpha? CMUCL does it here on all architectures
223 (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
225 (show-and-call !class-finalize)
227 ;; The reader and printer are initialized very late, so that they
228 ;; can do hairy things like invoking the compiler as part of their
230 (show-and-call !reader-cold-init)
231 (let ((*readtable* *standard-readtable*))
232 (show-and-call !sharpm-cold-init)
233 (show-and-call !backq-cold-init))
234 (setf *readtable* (copy-readtable *standard-readtable*))
235 (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
236 (sb!pretty:!pprint-cold-init)
238 ;; the ANSI-specified initial value of *PACKAGE*
239 (setf *package* (find-package "COMMON-LISP-USER"))
241 (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
242 (setf *cold-init-complete-p* t)
244 ;; The system is finally ready for GC.
245 (/show0 "enabling GC")
247 (/show0 "doing first GC")
249 (/show0 "back from first GC")
253 (/show0 "going into toplevel loop")
254 (handling-end-of-the-world
256 (critically-unreachable "after TOPLEVEL-INIT")))
258 (defun quit (&key recklessly-p
259 (unix-code 0 unix-code-p)
260 (unix-status unix-code))
262 "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
263 and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
264 UNIX-STATUS is used as the status code."
265 (declare (type (signed-byte 32) unix-status unix-code))
266 (/show0 "entering QUIT")
267 ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
268 ;; around for less than a year. It should be safe to remove it after
271 (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
272 instead (which is another name for the same thing)."))
274 (sb!unix:unix-exit unix-status)
275 (throw '%end-of-the-world unix-status))
276 (critically-unreachable "after trying to die in QUIT"))
278 ;;;; initialization functions
283 (os-cold-init-or-reinit)
285 (signal-cold-init-or-reinit)
286 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
287 ;; PRINT seems not to like x86 NPX denormal floats like
288 ;; LEAST-NEGATIVE-SINGLE-FLOAT, so the :UNDERFLOW exceptions are
289 ;; disabled by default. Joe User can explicitly enable them if
291 (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
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)))
305 ;;;; some support for any hapless wretches who end up debugging cold
308 ;;; Decode THING into hexadecimal notation using only machinery
309 ;;; available early in cold init.
311 (defun hexstr (thing)
312 (/noshow0 "entering HEXSTR")
313 (let ((addr (get-lisp-obj-address thing))
314 (str (make-string 10)))
315 (/noshow0 "ADDR and STR calculated")
316 (setf (char str 0) #\0
318 (/noshow0 "CHARs 0 and 1 set")
320 (/noshow0 "at head of DOTIMES loop")
321 (let* ((nibble (ldb (byte 4 0) addr))
322 (chr (char "0123456789abcdef" nibble)))
323 (declare (type (unsigned-byte 4) nibble)
325 (/noshow0 "NIBBLE and CHR calculated")
326 (setf (char str (- 9 i)) chr
327 addr (ash addr -4))))
331 (defun cold-print (x)
333 (simple-string (sb!sys:%primitive print x))
334 (symbol (sb!sys:%primitive print (symbol-name x)))
335 (list (let ((count 0))
336 (sb!sys:%primitive print "list:")
338 (when (>= (incf count) 4)
339 (sb!sys:%primitive print "...")
342 (t (sb!sys:%primitive print (hexstr x)))))