2b7f3294fe6745f3cab81711bb0cf92af3fcf740
[sbcl.git] / src / code / cold-init.lisp
1 ;;;; cold initialization stuff, plus some other miscellaneous stuff
2 ;;;; that we don't have any better place for
3
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.
12
13 (in-package "SB!IMPL")
14 \f
15 ;;;; burning our ships behind us
16
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: 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))
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             (unintern symbol package)
39             (setf any-changes? t)))))
40     (unless any-changes?
41       (return))))
42 \f
43 ;;;; putting ourselves out of our misery when things become too much to bear
44
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")
49   (%halt))
50
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)
58   (%halt))
59 \f
60 ;;;; !COLD-INIT
61
62 ;;; a list of toplevel things set by GENESIS
63 (defvar *!reversed-cold-toplevels*)
64
65 ;;; a SIMPLE-VECTOR set by GENESIS
66 (defvar *!load-time-values*)
67
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)
73     `(progn
74        (/primitive-print ,(symbol-name name))
75        (,name))))
76
77 ;;; called when a cold system starts up
78 (defun !cold-init ()
79   #!+sb-doc "Give the world a shove and hope it spins."
80
81   (/show0 "entering !COLD-INIT")
82
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
89         *before-gc-hooks* nil
90         *after-gc-hooks* nil
91         *already-maybe-gcing* t
92         *gc-inhibit* 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)
101
102   (show-and-call !typecheckfuns-cold-init)
103
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)
108
109   (show-and-call !package-cold-init)
110
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)
114
115   ;; This needs to be done early, but needs to be after INFO is
116   ;; initialized.
117   (show-and-call !fdefn-cold-init)
118
119   ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
120   ;; the basic type machinery needs to be initialized before toplevel
121   ;; forms run.
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.
133
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")
137
138   ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
139   ;; fixups be done separately? Wouldn't that be clearer and better?
140   ;; -- WHN 19991204
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))
153
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*)))
160       #!+sb-show
161       (when (zerop (mod index-in-cold-toplevels 1024))
162         (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
163         (/hexstr index-in-cold-toplevels))
164       #!+sb-show
165       (setf index-in-cold-toplevels
166             (the fixnum (1+ index-in-cold-toplevels)))
167       (typecase toplevel-thing
168         (function
169          (funcall toplevel-thing))
170         (cons
171          (case (first toplevel-thing)
172            (:load-time-value
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)))))
179            #!+(and x86 gencgc)
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)))
185            (t
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")
189
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)
193
194   ;; Only do this after toplevel forms have run, 'cause that's where
195   ;; DEFTYPEs are.
196   (setf *type-system-initialized* t)
197
198   (show-and-call os-cold-init-or-reinit)
199
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)
204
205   ;; FIXME: This list of modes should be defined in one place and
206   ;; explicitly shared between here and REINIT.
207
208   ;; Why was this marked #!+alpha?  CMUCL does it here on all architectures
209   (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
210
211   (show-and-call !class-finalize)
212
213   ;; The reader and printer are initialized very late, so that they
214   ;; can do hairy things like invoking the compiler as part of their
215   ;; initialization.
216   (show-and-call !reader-cold-init)
217   (let ((*readtable* *standard-readtable*))
218     (show-and-call !sharpm-cold-init)
219     (show-and-call !backq-cold-init))
220   (setf *readtable* (copy-readtable *standard-readtable*))
221   (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
222   (sb!pretty:!pprint-cold-init)
223
224   ;; the ANSI-specified initial value of *PACKAGE*
225   (setf *package* (find-package "COMMON-LISP-USER"))
226
227   (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
228   (setf *cold-init-complete-p* t)
229
230   ;; The system is finally ready for GC.
231   (setf *already-maybe-gcing* nil)
232   (/show0 "enabling GC")
233   (gc-on)
234   (/show0 "doing first GC")
235   (gc :full t)
236   (/show0 "back from first GC")
237
238   ;; The show is on.
239   (terpri)
240   (/show0 "going into toplevel loop")
241   (handling-end-of-the-world 
242     (toplevel-init)
243     (critically-unreachable "after TOPLEVEL-INIT")))
244
245 (defun quit (&key recklessly-p
246                   (unix-code 0 unix-code-p)
247                   (unix-status unix-code))
248   #!+sb-doc
249   "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
250   and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
251   UNIX-STATUS is used as the status code."
252   (declare (type (signed-byte 32) unix-status unix-code))
253   (/show0 "entering QUIT")
254   ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
255   ;; around for less than a year. It should be safe to remove it after
256   ;; a year.
257   (when unix-code-p
258     (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
259 instead (which is another name for the same thing)."))
260   (if recklessly-p
261       (sb!unix:unix-exit unix-status)
262       (throw '%end-of-the-world unix-status))
263   (critically-unreachable "after trying to die in QUIT"))
264 \f
265 ;;;; initialization functions
266
267 (defun reinit ()
268   (without-interrupts
269     (without-gcing
270       (os-cold-init-or-reinit)
271       (stream-reinit)
272       (signal-cold-init-or-reinit)
273       (gc-reinit)
274       (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
275       ;; PRINT seems not to like x86 NPX denormal floats like
276       ;; LEAST-NEGATIVE-SINGLE-FLOAT, so the :UNDERFLOW exceptions are
277       ;; disabled by default. Joe User can explicitly enable them if
278       ;; desired.
279       (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
280
281       ;; Clear pseudo atomic in case this core wasn't compiled with
282       ;; support.
283       ;;
284       ;; FIXME: In SBCL our cores are always compiled with support. So
285       ;; we don't need to do this, do we? At least not for this
286       ;; reason.. (Perhaps we should do it anyway in case someone
287       ;; manages to save an image from within a pseudo-atomic-atomic
288       ;; operation?)
289       #!+x86 (setf *pseudo-atomic-atomic* 0))
290     (gc-on)))
291 \f
292 ;;;; some support for any hapless wretches who end up debugging cold
293 ;;;; init code
294
295 ;;; Decode THING into hexadecimal notation using only machinery
296 ;;; available early in cold init.
297 #!+sb-show
298 (defun hexstr (thing)
299   (/noshow0 "entering HEXSTR")
300   (let ((addr (get-lisp-obj-address thing))
301         (str (make-string 10)))
302     (/noshow0 "ADDR and STR calculated")
303     (setf (char str 0) #\0
304           (char str 1) #\x)
305     (/noshow0 "CHARs 0 and 1 set")
306     (dotimes (i 8)
307       (/noshow0 "at head of DOTIMES loop")
308       (let* ((nibble (ldb (byte 4 0) addr))
309              (chr (char "0123456789abcdef" nibble)))
310         (declare (type (unsigned-byte 4) nibble)
311                  (base-char chr))
312         (/noshow0 "NIBBLE and CHR calculated")
313         (setf (char str (- 9 i)) chr
314               addr (ash addr -4))))
315     str))
316
317 #!+sb-show
318 (defun cold-print (x)
319   (typecase x
320     (simple-string (sb!sys:%primitive print x))
321     (symbol (sb!sys:%primitive print (symbol-name x)))
322     (list (let ((count 0))
323             (sb!sys:%primitive print "list:")
324             (dolist (i x)
325               (when (>= (incf count) 4)
326                 (sb!sys:%primitive print "...")
327                 (return))
328               (cold-print i))))
329     (t (sb!sys:%primitive print (hexstr x)))))