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