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