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