0.7.2.17:
[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   ;; Why was this marked #!+alpha?  CMUCL does it here on all architectures
210   (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
211
212   (show-and-call !class-finalize)
213
214   ;; The reader and printer are initialized very late, so that they
215   ;; can do hairy things like invoking the compiler as part of their
216   ;; initialization.
217   (show-and-call !reader-cold-init)
218   (let ((*readtable* *standard-readtable*))
219     (show-and-call !sharpm-cold-init)
220     (show-and-call !backq-cold-init))
221   (setf *readtable* (copy-readtable *standard-readtable*))
222   (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
223   (sb!pretty:!pprint-cold-init)
224
225   ;; the ANSI-specified initial value of *PACKAGE*
226   (setf *package* (find-package "COMMON-LISP-USER"))
227
228   (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
229   (setf *cold-init-complete-p* t)
230
231   ;; The system is finally ready for GC.
232   (setf *already-maybe-gcing* nil)
233   (/show0 "enabling GC")
234   (gc-on)
235   (/show0 "doing first GC")
236   (gc :full t)
237   (/show0 "back from first GC")
238
239   ;; The show is on.
240   (terpri)
241   (/show0 "going into toplevel loop")
242   (handling-end-of-the-world 
243     (toplevel-init)
244     (critically-unreachable "after TOPLEVEL-INIT")))
245
246 (defun quit (&key recklessly-p
247                   (unix-code 0 unix-code-p)
248                   (unix-status unix-code))
249   #!+sb-doc
250   "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
251   and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
252   UNIX-STATUS is used as the status code."
253   (declare (type (signed-byte 32) unix-status unix-code))
254   (/show0 "entering QUIT")
255   ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
256   ;; around for less than a year. It should be safe to remove it after
257   ;; a year.
258   (when unix-code-p
259     (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
260 instead (which is another name for the same thing)."))
261   (if recklessly-p
262       (sb!unix:unix-exit unix-status)
263       (throw '%end-of-the-world unix-status))
264   (critically-unreachable "after trying to die in QUIT"))
265 \f
266 ;;;; initialization functions
267
268 (defun reinit ()
269   (without-interrupts
270     (without-gcing
271       (os-cold-init-or-reinit)
272       (stream-reinit)
273       (signal-cold-init-or-reinit)
274       (gc-reinit)
275       (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
276       ;; PRINT seems not to like x86 NPX denormal floats like
277       ;; LEAST-NEGATIVE-SINGLE-FLOAT, so the :UNDERFLOW exceptions are
278       ;; disabled by default. Joe User can explicitly enable them if
279       ;; desired.
280       (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
281
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 (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)))))