1.0.23.37: more CLOS and classoid thread safety
[sbcl/tcr.git] / src / code / cold-init.lisp
blob57c5c12d64e3495dc32fe1a776f7507d8876dd0b
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) !cold-lose))
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 *after-gc-hooks* nil
96 *gc-inhibit* t
97 *gc-pending* nil
98 #!+sb-thread *stop-for-gc-pending* #!+sb-thread nil
99 *allow-with-interrupts* t
100 *interrupts-enabled* t
101 *interrupt-pending* nil
102 *break-on-signals* nil
103 *maximum-error-depth* 10
104 *current-error-depth* 0
105 *cold-init-complete-p* nil
106 *type-system-initialized* nil
107 sb!vm:*alloc-signal* nil
108 sb!kernel::*gc-epoch* (cons nil nil))
110 ;; I'm not sure where eval is first called, so I put this first.
111 (show-and-call !eval-cold-init)
113 (show-and-call thread-init-or-reinit)
114 (show-and-call !typecheckfuns-cold-init)
116 ;; Anyone might call RANDOM to initialize a hash value or something;
117 ;; and there's nothing which needs to be initialized in order for
118 ;; this to be initialized, so we initialize it right away.
119 (show-and-call !random-cold-init)
121 ;; Must be done before any non-opencoded array references are made.
122 (show-and-call !hairy-data-vector-reffer-init)
124 (show-and-call !character-database-cold-init)
125 (show-and-call !character-name-database-cold-init)
127 (show-and-call !early-package-cold-init)
128 (show-and-call !package-cold-init)
130 ;; All sorts of things need INFO and/or (SETF INFO).
131 (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
132 (show-and-call !globaldb-cold-init)
134 ;; This needs to be done early, but needs to be after INFO is
135 ;; initialized.
136 (show-and-call !function-names-cold-init)
137 (show-and-call !fdefn-cold-init)
139 ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
140 ;; the basic type machinery needs to be initialized before toplevel
141 ;; forms run.
142 (show-and-call !type-class-cold-init)
143 (show-and-call !typedefs-cold-init)
144 (show-and-call !world-lock-cold-init)
145 (show-and-call !classes-cold-init)
146 (show-and-call !early-type-cold-init)
147 (show-and-call !late-type-cold-init)
148 (show-and-call !alien-type-cold-init)
149 (show-and-call !target-type-cold-init)
150 (show-and-call !vm-type-cold-init)
151 ;; FIXME: It would be tidy to make sure that that these cold init
152 ;; functions are called in the same relative order as the toplevel
153 ;; forms of the corresponding source files.
155 ;;(show-and-call !package-cold-init)
156 (show-and-call !policy-cold-init-or-resanify)
157 (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
159 (show-and-call !constantp-cold-init)
160 (show-and-call !early-proclaim-cold-init)
162 ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
163 ;; fixups be done separately? Wouldn't that be clearer and better?
164 ;; -- WHN 19991204
165 (/show0 "doing cold toplevel forms and fixups")
166 (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
167 (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
168 (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
169 (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
170 #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
171 (/show0 "(length calculated..)")
172 (let ((hexstr (hexstr r-c-tl-length)))
173 (/show0 "(hexstr calculated..)")
174 (/primitive-print hexstr)))
175 (let (#!+sb-show (index-in-cold-toplevels 0))
176 #!+sb-show (declare (type fixnum index-in-cold-toplevels))
178 (dolist (toplevel-thing (prog1
179 (nreverse *!reversed-cold-toplevels*)
180 ;; (Now that we've NREVERSEd it, it's
181 ;; somewhat scrambled, so keep anyone
182 ;; else from trying to get at it.)
183 (makunbound '*!reversed-cold-toplevels*)))
184 #!+sb-show
185 (when (zerop (mod index-in-cold-toplevels 1024))
186 (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
187 (/hexstr index-in-cold-toplevels))
188 #!+sb-show
189 (setf index-in-cold-toplevels
190 (the fixnum (1+ index-in-cold-toplevels)))
191 (typecase toplevel-thing
192 (function
193 (funcall toplevel-thing))
194 (cons
195 (case (first toplevel-thing)
196 (:load-time-value
197 (setf (svref *!load-time-values* (third toplevel-thing))
198 (funcall (second toplevel-thing))))
199 (:load-time-value-fixup
200 (setf (sap-ref-word (second toplevel-thing) 0)
201 (get-lisp-obj-address
202 (svref *!load-time-values* (third toplevel-thing)))))
203 #!+(and (or x86 x86-64) gencgc)
204 (:load-time-code-fixup
205 (sb!vm::!envector-load-time-code-fixup (second toplevel-thing)
206 (third toplevel-thing)
207 (fourth toplevel-thing)
208 (fifth toplevel-thing)))
210 (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
211 (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
212 (/show0 "done with loop over cold toplevel forms and fixups")
214 ;; Set sane values again, so that the user sees sane values instead
215 ;; of whatever is left over from the last DECLAIM/PROCLAIM.
216 (show-and-call !policy-cold-init-or-resanify)
218 ;; Only do this after toplevel forms have run, 'cause that's where
219 ;; DEFTYPEs are.
220 (setf *type-system-initialized* t)
222 ;; now that the type system is definitely initialized, fixup UNKNOWN
223 ;; types that have crept in.
224 (show-and-call !fixup-type-cold-init)
225 ;; run the PROCLAIMs.
226 (show-and-call !late-proclaim-cold-init)
228 (show-and-call os-cold-init-or-reinit)
230 (show-and-call stream-cold-init-or-reset)
231 (show-and-call !loader-cold-init)
232 (show-and-call !foreign-cold-init)
233 #!-win32 (show-and-call signal-cold-init-or-reinit)
234 (/show0 "enabling internal errors")
235 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
237 (show-and-call float-cold-init-or-reinit)
239 (show-and-call !class-finalize)
241 ;; The reader and printer are initialized very late, so that they
242 ;; can do hairy things like invoking the compiler as part of their
243 ;; initialization.
244 (let ((*readtable* (make-readtable)))
245 (show-and-call !reader-cold-init)
246 (show-and-call !sharpm-cold-init)
247 (show-and-call !backq-cold-init)
248 ;; The *STANDARD-READTABLE* is assigned at last because the above
249 ;; functions would operate on the standard readtable otherwise---
250 ;; which would result in an error.
251 (setf *standard-readtable* *readtable*))
252 (setf *readtable* (copy-readtable *standard-readtable*))
253 (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
254 (sb!pretty:!pprint-cold-init)
256 ;; the ANSI-specified initial value of *PACKAGE*
257 (setf *package* (find-package "COMMON-LISP-USER"))
259 (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
260 (setf *cold-init-complete-p* t)
262 ;; The system is finally ready for GC.
263 (/show0 "enabling GC")
264 (gc-on)
265 (/show0 "doing first GC")
266 (gc :full t)
267 (/show0 "back from first GC")
269 ;; The show is on.
270 (terpri)
271 (/show0 "going into toplevel loop")
272 (handling-end-of-the-world
273 (toplevel-init)
274 (critically-unreachable "after TOPLEVEL-INIT")))
276 (defun quit (&key recklessly-p (unix-status 0))
277 #!+sb-doc
278 "Terminate the current Lisp. *EXIT-HOOKS* are pending unwind-protect
279 cleanup forms are run unless RECKLESSLY-P is true. On UNIX-like
280 systems, UNIX-STATUS is used as the status code."
281 (declare (type (signed-byte 32) unix-status))
282 ;; FIXME: Windows is not "unix-like", but still has the same
283 ;; unix-status... maybe we should just revert to calling it :STATUS?
284 (/show0 "entering QUIT")
285 (if recklessly-p
286 (sb!unix:unix-exit unix-status)
287 (throw '%end-of-the-world unix-status))
288 (critically-unreachable "after trying to die in QUIT"))
290 ;;;; initialization functions
292 (defun thread-init-or-reinit ()
293 (sb!thread::init-initial-thread)
294 (sb!thread::init-job-control)
295 (sb!thread::get-foreground))
297 (defun reinit ()
298 #!+win32
299 (setf sb!win32::*ansi-codepage* nil)
300 (setf *default-external-format* nil)
301 (setf sb!alien::*default-c-string-external-format* nil)
302 ;; WITHOUT-GCING implies WITHOUT-INTERRUPTS.
303 (without-gcing
304 (os-cold-init-or-reinit)
305 (thread-init-or-reinit)
306 (stream-reinit t)
307 #!-win32
308 (signal-cold-init-or-reinit)
309 (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
310 (float-cold-init-or-reinit))
311 (gc-reinit)
312 (foreign-reinit)
313 (time-reinit)
314 ;; If the debugger was disabled in the saved core, we need to
315 ;; re-disable ldb again.
316 (when (eq *invoke-debugger-hook* 'sb!debug::debugger-disabled-hook)
317 (sb!debug::disable-debugger))
318 (call-hooks "initialization" *init-hooks*))
320 ;;;; some support for any hapless wretches who end up debugging cold
321 ;;;; init code
323 ;;; Decode THING into hexadecimal notation using only machinery
324 ;;; available early in cold init.
325 #!+sb-show
326 (defun hexstr (thing)
327 (/noshow0 "entering HEXSTR")
328 (let ((addr (get-lisp-obj-address thing))
329 (str (make-string 10 :element-type 'base-char)))
330 (/noshow0 "ADDR and STR calculated")
331 (setf (char str 0) #\0
332 (char str 1) #\x)
333 (/noshow0 "CHARs 0 and 1 set")
334 (dotimes (i 8)
335 (/noshow0 "at head of DOTIMES loop")
336 (let* ((nibble (ldb (byte 4 0) addr))
337 (chr (char "0123456789abcdef" nibble)))
338 (declare (type (unsigned-byte 4) nibble)
339 (base-char chr))
340 (/noshow0 "NIBBLE and CHR calculated")
341 (setf (char str (- 9 i)) chr
342 addr (ash addr -4))))
343 str))
345 #!+sb-show
346 (defun cold-print (x)
347 (labels ((%cold-print (obj depthoid)
348 (if (> depthoid 4)
349 (sb!sys:%primitive print "...")
350 (typecase obj
351 (simple-string
352 (sb!sys:%primitive print obj))
353 (symbol
354 (sb!sys:%primitive print (symbol-name obj)))
355 (cons
356 (sb!sys:%primitive print "cons:")
357 (let ((d (1+ depthoid)))
358 (%cold-print (car obj) d)
359 (%cold-print (cdr obj) d)))
361 (sb!sys:%primitive print (hexstr x)))))))
362 (%cold-print x 0))
363 (values))