0.pre8.4
[sbcl/lichteblau.git] / src / code / cold-init.lisp
blobeae152783df6dab8ac3507f5315f0b205bb7ff57
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 ;;;; 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
23 ;;; uninterned.
24 ;;;
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))
29 (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)))))
47 (unless any-changes?
48 (return))))
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")
56 (%halt))
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)
65 (%halt))
67 ;;;; !COLD-INIT
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)
80 `(progn
81 (/primitive-print ,(symbol-name name))
82 (,name))))
84 ;;; called when a cold system starts up
85 (defun !cold-init ()
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 *gc-notify-stream* nil
96 *before-gc-hooks* nil
97 *after-gc-hooks* nil
98 *already-maybe-gcing* t
99 *gc-inhibit* 1
100 *need-to-collect-garbage* nil
101 sb!unix::*interrupts-enabled* t
102 sb!unix::*interrupt-pending* nil
103 *break-on-signals* nil
104 *maximum-error-depth* 10
105 *current-error-depth* 0
106 *cold-init-complete-p* nil
107 *type-system-initialized* nil)
109 (show-and-call !typecheckfuns-cold-init)
111 ;; Anyone might call RANDOM to initialize a hash value or something;
112 ;; and there's nothing which needs to be initialized in order for
113 ;; this to be initialized, so we initialize it right away.
114 (show-and-call !random-cold-init)
116 (show-and-call !package-cold-init)
118 ;; All sorts of things need INFO and/or (SETF INFO).
119 (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
120 (show-and-call !globaldb-cold-init)
122 ;; This needs to be done early, but needs to be after INFO is
123 ;; initialized.
124 (show-and-call !fdefn-cold-init)
126 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
127 ;; the basic type machinery needs to be initialized before toplevel
128 ;; forms run.
129 (show-and-call !type-class-cold-init)
130 (show-and-call !typedefs-cold-init)
131 (show-and-call !classes-cold-init)
132 (show-and-call !early-type-cold-init)
133 (show-and-call !late-type-cold-init)
134 (show-and-call !alien-type-cold-init)
135 (show-and-call !target-type-cold-init)
136 (show-and-call !vm-type-cold-init)
137 ;; FIXME: It would be tidy to make sure that that these cold init
138 ;; functions are called in the same relative order as the toplevel
139 ;; forms of the corresponding source files.
141 ;;(show-and-call !package-cold-init)
142 (show-and-call !policy-cold-init-or-resanify)
143 (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
145 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
146 ;; fixups be done separately? Wouldn't that be clearer and better?
147 ;; -- WHN 19991204
148 (/show0 "doing cold toplevel forms and fixups")
149 (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
150 (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
151 (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
152 (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
153 #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
154 (/show0 "(length calculated..)")
155 (let ((hexstr (hexstr r-c-tl-length)))
156 (/show0 "(hexstr calculated..)")
157 (/primitive-print hexstr)))
158 (let (#!+sb-show (index-in-cold-toplevels 0))
159 #!+sb-show (declare (type fixnum index-in-cold-toplevels))
161 (dolist (toplevel-thing (prog1
162 (nreverse *!reversed-cold-toplevels*)
163 ;; (Now that we've NREVERSEd it, it's
164 ;; somewhat scrambled, so keep anyone
165 ;; else from trying to get at it.)
166 (makunbound '*!reversed-cold-toplevels*)))
167 #!+sb-show
168 (when (zerop (mod index-in-cold-toplevels 1024))
169 (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
170 (/hexstr index-in-cold-toplevels))
171 #!+sb-show
172 (setf index-in-cold-toplevels
173 (the fixnum (1+ index-in-cold-toplevels)))
174 (typecase toplevel-thing
175 (function
176 (funcall toplevel-thing))
177 (cons
178 (case (first toplevel-thing)
179 (:load-time-value
180 (setf (svref *!load-time-values* (third toplevel-thing))
181 (funcall (second toplevel-thing))))
182 (:load-time-value-fixup
183 (setf (sap-ref-32 (second toplevel-thing) 0)
184 (get-lisp-obj-address
185 (svref *!load-time-values* (third toplevel-thing)))))
186 #!+(and x86 gencgc)
187 (:load-time-code-fixup
188 (sb!vm::!envector-load-time-code-fixup (second toplevel-thing)
189 (third toplevel-thing)
190 (fourth toplevel-thing)
191 (fifth toplevel-thing)))
193 (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
194 (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
195 (/show0 "done with loop over cold toplevel forms and fixups")
197 ;; Set sane values again, so that the user sees sane values instead
198 ;; of whatever is left over from the last DECLAIM/PROCLAIM.
199 (show-and-call !policy-cold-init-or-resanify)
201 ;; Only do this after toplevel forms have run, 'cause that's where
202 ;; DEFTYPEs are.
203 (setf *type-system-initialized* t)
205 (show-and-call os-cold-init-or-reinit)
207 (show-and-call stream-cold-init-or-reset)
208 (show-and-call !loader-cold-init)
209 (show-and-call signal-cold-init-or-reinit)
210 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
212 ;; FIXME: This list of modes should be defined in one place and
213 ;; explicitly shared between here and REINIT.
215 ;; Why was this marked #!+alpha? CMUCL does it here on all architectures
216 (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
218 (show-and-call !class-finalize)
220 ;; The reader and printer are initialized very late, so that they
221 ;; can do hairy things like invoking the compiler as part of their
222 ;; initialization.
223 (show-and-call !reader-cold-init)
224 (let ((*readtable* *standard-readtable*))
225 (show-and-call !sharpm-cold-init)
226 (show-and-call !backq-cold-init))
227 (setf *readtable* (copy-readtable *standard-readtable*))
228 (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
229 (sb!pretty:!pprint-cold-init)
231 ;; the ANSI-specified initial value of *PACKAGE*
232 (setf *package* (find-package "COMMON-LISP-USER"))
234 (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
235 (setf *cold-init-complete-p* t)
237 ;; The system is finally ready for GC.
238 (setf *already-maybe-gcing* nil)
239 (/show0 "enabling GC")
240 (gc-on)
241 (/show0 "doing first GC")
242 (gc :full t)
243 (/show0 "back from first GC")
245 ;; The show is on.
246 (terpri)
247 (/show0 "going into toplevel loop")
248 (handling-end-of-the-world
249 (toplevel-init)
250 (critically-unreachable "after TOPLEVEL-INIT")))
252 (defun quit (&key recklessly-p
253 (unix-code 0 unix-code-p)
254 (unix-status unix-code))
255 #!+sb-doc
256 "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
257 and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
258 UNIX-STATUS is used as the status code."
259 (declare (type (signed-byte 32) unix-status unix-code))
260 (/show0 "entering QUIT")
261 ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
262 ;; around for less than a year. It should be safe to remove it after
263 ;; a year.
264 (when unix-code-p
265 (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
266 instead (which is another name for the same thing)."))
267 (if recklessly-p
268 (sb!unix:unix-exit unix-status)
269 (throw '%end-of-the-world unix-status))
270 (critically-unreachable "after trying to die in QUIT"))
272 ;;;; initialization functions
274 (defun reinit ()
275 (without-interrupts
276 (without-gcing
277 (os-cold-init-or-reinit)
278 (stream-reinit)
279 (signal-cold-init-or-reinit)
280 (gc-reinit)
281 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
282 ;; PRINT seems not to like x86 NPX denormal floats like
283 ;; LEAST-NEGATIVE-SINGLE-FLOAT, so the :UNDERFLOW exceptions are
284 ;; disabled by default. Joe User can explicitly enable them if
285 ;; desired.
286 (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
288 ;; Clear pseudo atomic in case this core wasn't compiled with
289 ;; support.
291 ;; FIXME: In SBCL our cores are always compiled with support. So
292 ;; we don't need to do this, do we? At least not for this
293 ;; reason.. (Perhaps we should do it anyway in case someone
294 ;; manages to save an image from within a pseudo-atomic-atomic
295 ;; operation?)
296 #!+x86 (setf *pseudo-atomic-atomic* 0))
297 (gc-on)))
299 ;;;; some support for any hapless wretches who end up debugging cold
300 ;;;; init code
302 ;;; Decode THING into hexadecimal notation using only machinery
303 ;;; available early in cold init.
304 #!+sb-show
305 (defun hexstr (thing)
306 (/noshow0 "entering HEXSTR")
307 (let ((addr (get-lisp-obj-address thing))
308 (str (make-string 10)))
309 (/noshow0 "ADDR and STR calculated")
310 (setf (char str 0) #\0
311 (char str 1) #\x)
312 (/noshow0 "CHARs 0 and 1 set")
313 (dotimes (i 8)
314 (/noshow0 "at head of DOTIMES loop")
315 (let* ((nibble (ldb (byte 4 0) addr))
316 (chr (char "0123456789abcdef" nibble)))
317 (declare (type (unsigned-byte 4) nibble)
318 (base-char chr))
319 (/noshow0 "NIBBLE and CHR calculated")
320 (setf (char str (- 9 i)) chr
321 addr (ash addr -4))))
322 str))
324 #!+sb-show
325 (defun cold-print (x)
326 (typecase x
327 (simple-string (sb!sys:%primitive print x))
328 (symbol (sb!sys:%primitive print (symbol-name x)))
329 (list (let ((count 0))
330 (sb!sys:%primitive print "list:")
331 (dolist (i x)
332 (when (>= (incf count) 4)
333 (sb!sys:%primitive print "...")
334 (return))
335 (cold-print i))))
336 (t (sb!sys:%primitive print (hexstr x)))))