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