a891dbd0c5b101d62963864b7a74f6c0fe6e86c1
[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 (defun !unintern-init-only-stuff ()
25   (do ((any-changes? nil nil))
26       (nil)
27     (dolist (package (list-all-packages))
28       (do-symbols (symbol package)
29         (let ((name (symbol-name symbol)))
30           (when (or (string= name "!" :end1 1 :end2 1)
31                     (and (>= (length name) 2)
32                          (string= name "*!" :end1 2 :end2 2)))
33             (/show0 "uninterning cold-init-only symbol..")
34             (/primitive-print name)
35             (unintern symbol package)
36             (setf any-changes? t)))))
37     (unless any-changes?
38       (return))))
39 \f
40 ;;;; !COLD-INIT
41
42 ;;; a list of toplevel things set by GENESIS
43 (defvar *!reversed-cold-toplevels*)
44
45 ;;; a SIMPLE-VECTOR set by genesis
46 (defvar *!load-time-values*)
47
48 (defun !cold-lose (msg)
49   (%primitive print msg)
50   (%primitive print "too early in cold init to recover from errors")
51   (%halt))
52
53 #!+gengc
54 (defun !do-load-time-value-fixup (object offset index)
55   (declare (type index offset))
56   (let ((value (svref *!load-time-values* index)))
57     (typecase object
58       (list
59        (case offset
60          (0 (setf (car object) value))
61          (1 (setf (cdr object) value))
62          (t (!cold-lose "bogus offset in cons cell"))))
63       (instance
64        (setf (%instance-ref object (- offset sb!vm:instance-slots-offset))
65              value))
66       (code-component
67        (setf (code-header-ref object offset) value))
68       (simple-vector
69        (setf (svref object (- offset sb!vm:vector-data-offset)) value))
70       (t
71        (!cold-lose "unknown kind of object for load-time-value fixup")))))
72
73 (eval-when (:compile-toplevel :execute)
74   ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
75   ;; and use it for most of the cold-init functions. (Just be careful
76   ;; not to use it for the COLD-INIT-OR-REINIT functions.)
77   (sb!xc:defmacro show-and-call (name)
78     `(progn
79        (/primitive-print ,(symbol-name name))
80        (,name))))
81
82 ;;; called when a cold system starts up
83 (defun !cold-init ()
84   #!+sb-doc "Give the world a shove and hope it spins."
85
86   (/show0 "entering !COLD-INIT")
87
88   ;; FIXME: It'd probably be cleaner to have most of the stuff here
89   ;; handled by calls like !GC-COLD-INIT, !ERROR-COLD-INIT, and
90   ;; !UNIX-COLD-INIT. And *TYPE-SYSTEM-INITIALIZED* could be changed to
91   ;; *TYPE-SYSTEM-INITIALIZED-WHEN-BOUND* so that it doesn't need to
92   ;; be explicitly set in order to be meaningful.
93   (setf *gc-verbose* nil)
94   (setf *gc-notify-stream* nil)
95   (setf *before-gc-hooks* nil)
96   (setf *after-gc-hooks* nil)
97   #!+gengc (setf *handler-clusters* nil)
98   #!-gengc (setf *already-maybe-gcing* t
99                  *gc-inhibit* t
100                  *need-to-collect-garbage* nil
101                  sb!unix::*interrupts-enabled* t
102                  sb!unix::*interrupt-pending* nil)
103   (setf *break-on-signals* nil)
104   (setf *maximum-error-depth* 10)
105   (setf *current-error-depth* 0)
106   (setf *cold-init-complete-p* nil)
107   (setf *type-system-initialized* nil)
108
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)
113
114   (show-and-call !package-cold-init)
115
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)
119
120   ;; This needs to be done early, but needs to be after INFO is
121   ;; initialized.
122   (show-and-call !fdefn-cold-init)
123
124   ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
125   ;; the basic type machinery needs to be initialized before toplevel
126   ;; forms run.
127   (show-and-call !type-class-cold-init)
128   (show-and-call !typedefs-cold-init)
129   (show-and-call !classes-cold-init)
130   (show-and-call !early-type-cold-init)
131   (show-and-call !late-type-cold-init)
132   (show-and-call !alien-type-cold-init)
133   (show-and-call !target-type-cold-init)
134   (show-and-call !vm-type-cold-init)
135   ;; FIXME: It would be tidy to make sure that that these cold init
136   ;; functions are called in the same relative order as the toplevel
137   ;; forms of the corresponding source files.
138
139   ;;(show-and-call !package-cold-init)
140   (show-and-call !policy-cold-init-or-resanify)
141   (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
142
143   ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
144   ;; fixups be done separately? Wouldn't that be clearer and better?
145   ;; -- WHN 19991204
146   (/show0 "doing cold toplevel forms and fixups")
147   (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
148   (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
149   (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
150   (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
151   #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
152                (/show0 "(length calculated..)")
153                (let ((hexstr (hexstr r-c-tl-length)))
154                  (/show0 "(hexstr calculated..)")
155                  (/primitive-print hexstr)))
156   (let (#!+sb-show (index-in-cold-toplevels 0))
157     #!+sb-show (declare (type fixnum index-in-cold-toplevels))
158     (dolist (toplevel-thing (prog1
159                                 (nreverse *!reversed-cold-toplevels*)
160                               ;; (Now that we've NREVERSEd it, it's
161                               ;; somewhat scrambled, so keep anyone
162                               ;; else from trying to get at it.)
163                               (makunbound '*!reversed-cold-toplevels*)))
164       #!+sb-show
165       (when (zerop (mod index-in-cold-toplevels 1024))
166         (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
167         (/hexstr index-in-cold-toplevels))
168       #!+sb-show
169       (setf index-in-cold-toplevels
170             (the fixnum (1+ index-in-cold-toplevels)))
171       (typecase toplevel-thing
172         (function
173          (funcall toplevel-thing))
174         (cons
175          (case (first toplevel-thing)
176            (:load-time-value
177             (setf (svref *!load-time-values* (third toplevel-thing))
178                   (funcall (second toplevel-thing))))
179            (:load-time-value-fixup
180             #!-gengc
181             (setf (sap-ref-32 (second toplevel-thing) 0)
182                   (get-lisp-obj-address
183                    (svref *!load-time-values* (third toplevel-thing))))
184             #!+gengc
185             (!do-load-time-value-fixup (second toplevel-thing)
186                                        (third  toplevel-thing)
187                                        (fourth toplevel-thing)))
188            #!+(and x86 gencgc)
189            (:load-time-code-fixup
190             (sb!vm::!do-load-time-code-fixup (second toplevel-thing)
191                                              (third  toplevel-thing)
192                                              (fourth toplevel-thing)
193                                              (fifth  toplevel-thing)))
194            (t
195             (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
196         (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
197   (/show0 "done with loop over cold toplevel forms and fixups")
198
199   ;; Set sane values again, so that the user sees sane values instead
200   ;; of whatever is left over from the last DECLAIM/PROCLAIM.
201   (show-and-call !policy-cold-init-or-resanify)
202
203   ;; Only do this after toplevel forms have run, 'cause that's where
204   ;; DEFTYPEs are.
205   (setf *type-system-initialized* t)
206
207   (show-and-call os-cold-init-or-reinit)
208
209   (show-and-call stream-cold-init-or-reset)
210   (show-and-call !loader-cold-init)
211   (show-and-call signal-cold-init-or-reinit)
212   (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
213
214   ;; FIXME: This list of modes should be defined in one place and
215   ;; explicitly shared between here and REINIT.
216   (set-floating-point-modes :traps '(:overflow
217                                      #!-x86 :underflow
218                                      :invalid
219                                      :divide-by-zero))
220
221   (show-and-call !class-finalize)
222
223   ;; The reader and printer are initialized very late, so that they
224   ;; can do hairy things like invoking the compiler as part of their
225   ;; initialization.
226   (show-and-call !reader-cold-init)
227   (let ((*readtable* *standard-readtable*))
228     (show-and-call !sharpm-cold-init)
229     (show-and-call !backq-cold-init))
230   (setf *readtable* (copy-readtable *standard-readtable*))
231   (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
232   (sb!pretty:!pprint-cold-init)
233
234   ;; the ANSI-specified initial value of *PACKAGE*
235   (setf *package* (find-package "COMMON-LISP-USER"))
236   ;; FIXME: I'm not sure where it should be done, but CL-USER really
237   ;; ought to USE-PACKAGE publicly accessible packages like SB-DEBUG
238   ;; (for ARG and VAR), SB-EXT, SB-EXT-C-CALL, and SB-EXT-ALIEN so
239   ;; that the user has a hint about which symbols we consider public.
240   ;; (Perhaps SB-DEBUG wouldn't need to be in the list if ARG and VAR
241   ;; could be typed directly, with no parentheses, at the debug prompt
242   ;; the way that e.g. F or BACKTRACE can be?)
243
244   (/show0 "done initializing")
245   (setf *cold-init-complete-p* t)
246
247   ;; The system is finally ready for GC.
248   #!-gengc (setf *already-maybe-gcing* nil)
249   (/show0 "enabling GC")
250   (gc-on)
251   (/show0 "doing first GC")
252   (gc :full t)
253   (/show0 "back from first GC")
254
255   ;; The show is on.
256   (terpri)
257   (/show0 "going into toplevel loop")
258   (handling-end-of-the-world 
259     (toplevel-init)))
260
261 (defun quit (&key recklessly-p
262                   (unix-code 0 unix-code-p)
263                   (unix-status unix-code))
264   #!+sb-doc
265   "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
266   and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
267   UNIX-STATUS is used as the status code."
268   (declare (type (signed-byte 32) unix-code))
269   ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
270   ;; around for less than a year. It should be safe to remove it after
271   ;; a year.
272   (when unix-code-p
273     (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
274 instead (which is another name for the same thing)."))
275   (if recklessly-p
276       (sb!unix:unix-exit unix-status)
277       (throw '%end-of-the-world unix-status)))
278 \f
279 ;;;; initialization functions
280
281 (defun reinit ()
282   (without-interrupts
283     (without-gcing
284       (os-cold-init-or-reinit)
285       (stream-reinit)
286       (signal-cold-init-or-reinit)
287       (gc-cold-init-or-reinit)
288       (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
289       (set-floating-point-modes :traps
290                                 '(:overflow
291                                   :invalid
292                                   :divide-by-zero
293                                   ;; PRINT seems not to like x86 NPX
294                                   ;; denormal floats like
295                                   ;; LEAST-NEGATIVE-SINGLE-FLOAT, so
296                                   ;; the :UNDERFLOW exceptions are
297                                   ;; disabled by default. Joe User can
298                                   ;; explicitly enable them if
299                                   ;; desired.
300                                   #!-x86 :underflow))
301       ;; Clear pseudo atomic in case this core wasn't compiled with
302       ;; support.
303       ;;
304       ;; FIXME: In SBCL our cores are always compiled with support. So
305       ;; we don't need to do this, do we? At least not for this
306       ;; reason.. (Perhaps we should do it anyway in case someone
307       ;; manages to save an image from within a pseudo-atomic-atomic
308       ;; operation?)
309       #!+x86 (setf sb!impl::*pseudo-atomic-atomic* 0))
310     (gc-on)))
311 \f
312 ;;;; some support for any hapless wretches who end up debugging cold
313 ;;;; init code
314
315 ;;; Decode THING into hexadecimal notation using only machinery
316 ;;; available early in cold init.
317 #!+sb-show
318 (defun hexstr (thing)
319   (/noshow0 "entering HEXSTR")
320   (let ((addr (sb!kernel:get-lisp-obj-address thing))
321         (str (make-string 10)))
322     (/noshow0 "ADDR and STR calculated")
323     (setf (char str 0) #\0
324           (char str 1) #\x)
325     (/noshow0 "CHARs 0 and 1 set")
326     (dotimes (i 8)
327       (/noshow0 "at head of DOTIMES loop")
328       (let* ((nibble (ldb (byte 4 0) addr))
329              (chr (char "0123456789abcdef" nibble)))
330         (declare (type (unsigned-byte 4) nibble)
331                  (base-char chr))
332         (/noshow0 "NIBBLE and CHR calculated")
333         (setf (char str (- 9 i)) chr
334               addr (ash addr -4))))
335     str))
336
337 #!+sb-show
338 (defun cold-print (x)
339   (typecase x
340     (simple-string (sb!sys:%primitive print x))
341     (symbol (sb!sys:%primitive print (symbol-name x)))
342     (list (let ((count 0))
343             (sb!sys:%primitive print "list:")
344             (dolist (i x)
345               (when (>= (incf count) 4)
346                 (sb!sys:%primitive print "...")
347                 (return))
348               (cold-print i))))
349     (t (sb!sys:%primitive print (hexstr x)))))