1.0.25.44: INTERRUPT-THREAD and timer improvements
[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) !cold-lose))
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 *after-gc-hooks* nil
96         *in-without-gcing* nil
97         *gc-inhibit* t
98         *gc-pending* nil
99         #!+sb-thread *stop-for-gc-pending* #!+sb-thread nil
100         *allow-with-interrupts* t
101         sb!unix::*unblock-deferrables-on-enabling-interrupts-p* nil
102         *interrupts-enabled* t
103         *interrupt-pending* nil
104         *break-on-signals* nil
105         *maximum-error-depth* 10
106         *current-error-depth* 0
107         *cold-init-complete-p* nil
108         *type-system-initialized* nil
109         sb!vm:*alloc-signal* nil
110         sb!kernel::*gc-epoch* (cons nil nil))
111
112   ;; I'm not sure where eval is first called, so I put this first.
113   (show-and-call !eval-cold-init)
114
115   (show-and-call thread-init-or-reinit)
116   (show-and-call !typecheckfuns-cold-init)
117
118   ;; Anyone might call RANDOM to initialize a hash value or something;
119   ;; and there's nothing which needs to be initialized in order for
120   ;; this to be initialized, so we initialize it right away.
121   (show-and-call !random-cold-init)
122
123   ;; Must be done before any non-opencoded array references are made.
124   (show-and-call !hairy-data-vector-reffer-init)
125
126   (show-and-call !character-database-cold-init)
127   (show-and-call !character-name-database-cold-init)
128
129   (show-and-call !early-package-cold-init)
130   (show-and-call !package-cold-init)
131
132   ;; All sorts of things need INFO and/or (SETF INFO).
133   (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
134   (show-and-call !globaldb-cold-init)
135
136   ;; This needs to be done early, but needs to be after INFO is
137   ;; initialized.
138   (show-and-call !function-names-cold-init)
139   (show-and-call !fdefn-cold-init)
140
141   ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
142   ;; the basic type machinery needs to be initialized before toplevel
143   ;; forms run.
144   (show-and-call !type-class-cold-init)
145   (show-and-call !typedefs-cold-init)
146   (show-and-call !world-lock-cold-init)
147   (show-and-call !classes-cold-init)
148   (show-and-call !early-type-cold-init)
149   (show-and-call !late-type-cold-init)
150   (show-and-call !alien-type-cold-init)
151   (show-and-call !target-type-cold-init)
152   (show-and-call !vm-type-cold-init)
153   ;; FIXME: It would be tidy to make sure that that these cold init
154   ;; functions are called in the same relative order as the toplevel
155   ;; forms of the corresponding source files.
156
157   ;;(show-and-call !package-cold-init)
158   (show-and-call !policy-cold-init-or-resanify)
159   (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
160
161   (show-and-call !constantp-cold-init)
162   (show-and-call !early-proclaim-cold-init)
163
164   ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
165   ;; fixups be done separately? Wouldn't that be clearer and better?
166   ;; -- WHN 19991204
167   (/show0 "doing cold toplevel forms and fixups")
168   (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
169   (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
170   (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
171   (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
172   #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
173                (/show0 "(length calculated..)")
174                (let ((hexstr (hexstr r-c-tl-length)))
175                  (/show0 "(hexstr calculated..)")
176                  (/primitive-print hexstr)))
177   (let (#!+sb-show (index-in-cold-toplevels 0))
178     #!+sb-show (declare (type fixnum index-in-cold-toplevels))
179
180     (dolist (toplevel-thing (prog1
181                                 (nreverse *!reversed-cold-toplevels*)
182                               ;; (Now that we've NREVERSEd it, it's
183                               ;; somewhat scrambled, so keep anyone
184                               ;; else from trying to get at it.)
185                               (makunbound '*!reversed-cold-toplevels*)))
186       #!+sb-show
187       (when (zerop (mod index-in-cold-toplevels 1024))
188         (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
189         (/hexstr index-in-cold-toplevels))
190       #!+sb-show
191       (setf index-in-cold-toplevels
192             (the fixnum (1+ index-in-cold-toplevels)))
193       (typecase toplevel-thing
194         (function
195          (funcall toplevel-thing))
196         (cons
197          (case (first toplevel-thing)
198            (:load-time-value
199             (setf (svref *!load-time-values* (third toplevel-thing))
200                   (funcall (second toplevel-thing))))
201            (:load-time-value-fixup
202             (setf (sap-ref-word (int-sap (get-lisp-obj-address (second toplevel-thing)))
203                                 (third toplevel-thing))
204                   (get-lisp-obj-address
205                    (svref *!load-time-values* (fourth toplevel-thing)))))
206            (t
207             (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
208         (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
209   (/show0 "done with loop over cold toplevel forms and fixups")
210
211   ;; Set sane values again, so that the user sees sane values instead
212   ;; of whatever is left over from the last DECLAIM/PROCLAIM.
213   (show-and-call !policy-cold-init-or-resanify)
214
215   ;; Only do this after toplevel forms have run, 'cause that's where
216   ;; DEFTYPEs are.
217   (setf *type-system-initialized* t)
218
219   ;; now that the type system is definitely initialized, fixup UNKNOWN
220   ;; types that have crept in.
221   (show-and-call !fixup-type-cold-init)
222   ;; run the PROCLAIMs.
223   (show-and-call !late-proclaim-cold-init)
224
225   (show-and-call os-cold-init-or-reinit)
226
227   (show-and-call stream-cold-init-or-reset)
228   (show-and-call !loader-cold-init)
229   (show-and-call !foreign-cold-init)
230   #!-win32 (show-and-call signal-cold-init-or-reinit)
231   (/show0 "enabling internal errors")
232   (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
233
234   (show-and-call float-cold-init-or-reinit)
235
236   (show-and-call !class-finalize)
237
238   ;; The reader and printer are initialized very late, so that they
239   ;; can do hairy things like invoking the compiler as part of their
240   ;; initialization.
241   (let ((*readtable* (make-readtable)))
242     (show-and-call !reader-cold-init)
243     (show-and-call !sharpm-cold-init)
244     (show-and-call !backq-cold-init)
245     ;; The *STANDARD-READTABLE* is assigned at last because the above
246     ;; functions would operate on the standard readtable otherwise---
247     ;; which would result in an error.
248     (setf *standard-readtable* *readtable*))
249   (setf *readtable* (copy-readtable *standard-readtable*))
250   (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
251   (sb!pretty:!pprint-cold-init)
252
253   ;; the ANSI-specified initial value of *PACKAGE*
254   (setf *package* (find-package "COMMON-LISP-USER"))
255
256   (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
257   (setf *cold-init-complete-p* t)
258
259   ; hppa heap is segmented, lisp and c uses a stub to call eachother
260   #!+hpux (sb!sys:%primitive sb!vm::setup-return-from-lisp-stub)
261   ;; The system is finally ready for GC.
262   (/show0 "enabling GC")
263   (setq *gc-inhibit* nil)
264   (/show0 "doing first GC")
265   (gc :full t)
266   (/show0 "back from first GC")
267
268   ;; The show is on.
269   (terpri)
270   (/show0 "going into toplevel loop")
271   (handling-end-of-the-world
272     (toplevel-init)
273     (critically-unreachable "after TOPLEVEL-INIT")))
274
275 (defun quit (&key recklessly-p (unix-status 0))
276   #!+sb-doc
277   "Terminate the current Lisp. *EXIT-HOOKS* are pending unwind-protect
278 cleanup forms are run unless RECKLESSLY-P is true. On UNIX-like
279 systems, UNIX-STATUS is used as the status code."
280   (declare (type (signed-byte 32) unix-status))
281   ;; FIXME: Windows is not "unix-like", but still has the same
282   ;; unix-status... maybe we should just revert to calling it :STATUS?
283   (/show0 "entering QUIT")
284   (if recklessly-p
285       (sb!unix:unix-exit unix-status)
286       (throw '%end-of-the-world unix-status))
287   (critically-unreachable "after trying to die in QUIT"))
288 \f
289 ;;;; initialization functions
290
291 (defun thread-init-or-reinit ()
292   (sb!thread::init-initial-thread)
293   (sb!thread::init-job-control)
294   (sb!thread::get-foreground))
295
296 (defun reinit ()
297   #!+win32
298   (setf sb!win32::*ansi-codepage* nil)
299   (setf *default-external-format* nil)
300   (setf sb!alien::*default-c-string-external-format* nil)
301   ;; WITHOUT-GCING implies WITHOUT-INTERRUPTS.
302   (without-gcing
303     (os-cold-init-or-reinit)
304     (thread-init-or-reinit)
305     (stream-reinit t)
306     #!-win32
307     (signal-cold-init-or-reinit)
308     (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
309     (float-cold-init-or-reinit))
310   (gc-reinit)
311   (foreign-reinit)
312   (time-reinit)
313   ;; If the debugger was disabled in the saved core, we need to
314   ;; re-disable ldb again.
315   (when (eq *invoke-debugger-hook* 'sb!debug::debugger-disabled-hook)
316     (sb!debug::disable-debugger))
317   (call-hooks "initialization" *init-hooks*))
318 \f
319 ;;;; some support for any hapless wretches who end up debugging cold
320 ;;;; init code
321
322 ;;; Decode THING into hexadecimal notation using only machinery
323 ;;; available early in cold init.
324 #!+sb-show
325 (defun hexstr (thing)
326   (/noshow0 "entering HEXSTR")
327   (let ((addr (get-lisp-obj-address thing))
328         (str (make-string 10 :element-type 'base-char)))
329     (/noshow0 "ADDR and STR calculated")
330     (setf (char str 0) #\0
331           (char str 1) #\x)
332     (/noshow0 "CHARs 0 and 1 set")
333     (dotimes (i 8)
334       (/noshow0 "at head of DOTIMES loop")
335       (let* ((nibble (ldb (byte 4 0) addr))
336              (chr (char "0123456789abcdef" nibble)))
337         (declare (type (unsigned-byte 4) nibble)
338                  (base-char chr))
339         (/noshow0 "NIBBLE and CHR calculated")
340         (setf (char str (- 9 i)) chr
341               addr (ash addr -4))))
342     str))
343
344 #!+sb-show
345 (defun cold-print (x)
346   (labels ((%cold-print (obj depthoid)
347              (if (> depthoid 4)
348                  (sb!sys:%primitive print "...")
349                  (typecase obj
350                    (simple-string
351                     (sb!sys:%primitive print obj))
352                    (symbol
353                     (sb!sys:%primitive print (symbol-name obj)))
354                    (cons
355                     (sb!sys:%primitive print "cons:")
356                     (let ((d (1+ depthoid)))
357                       (%cold-print (car obj) d)
358                       (%cold-print (cdr obj) d)))
359                    (t
360                     (sb!sys:%primitive print (hexstr x)))))))
361     (%cold-print x 0))
362   (values))