0.8.1.21:
[sbcl.git] / src / code / x86-vm.lisp
1 ;;;; X86-specific runtime stuff
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; OS-CONTEXT-T
15
16 ;;; a POSIX signal context, i.e. the type passed as the third 
17 ;;; argument to an SA_SIGACTION-style signal handler
18 ;;;
19 ;;; The real type does have slots, but at Lisp level, we never
20 ;;; access them, or care about the size of the object. Instead, we
21 ;;; always refer to these objects by pointers handed to us by the C
22 ;;; runtime library, and ask the runtime library any time we need
23 ;;; information about the contents of one of these objects. Thus, it
24 ;;; works to represent this as an object with no slots.
25 ;;;
26 ;;; KLUDGE: It would be nice to have a type definition analogous to
27 ;;; C's "struct os_context_t;", for an incompletely specified object
28 ;;; which can only be referred to by reference, but I don't know how
29 ;;; to do that in the FFI, so instead we just this bogus no-slots
30 ;;; representation. -- WHN 20000730
31 ;;;
32 ;;; FIXME: Since SBCL, unlike CMU CL, uses this as an opaque type,
33 ;;; it's no longer architecture-dependent, and probably belongs in
34 ;;; some other package, perhaps SB-KERNEL.
35 (define-alien-type os-context-t (struct os-context-t-struct))
36 \f
37 ;;;; MACHINE-TYPE and MACHINE-VERSION
38
39 (defun machine-type ()
40   #!+sb-doc
41   "Return a string describing the type of the local machine."
42   "X86")
43
44 ;;; arch-specific support for CL:MACHINE-VERSION, defined OAOO elsewhere
45 (defun get-machine-version ()
46   #!+linux
47   (with-open-file (stream "/proc/cpuinfo"
48                           ;; Even on Linux it's an option to build
49                           ;; kernels without /proc filesystems, so
50                           ;; degrade gracefully.
51                           :if-does-not-exist nil)
52     (loop with line while (setf line (read-line stream nil))
53           ;; The field "model name" exists on kernel 2.4.21-rc6-ac1
54           ;; anyway, with values e.g.
55           ;;   "AMD Athlon(TM) XP 2000+"
56           ;;   "Intel(R) Pentium(R) M processor 1300MHz"
57           ;; which seem comparable to the information in the example
58           ;; in the MACHINE-VERSION page of the ANSI spec.
59           when (eql (search "model name" line) 0)
60           return (string-trim " " (subseq line (1+ (position #\: line))))))
61   #!-linux
62   nil)
63 \f
64 ;;;; :CODE-OBJECT fixups
65
66 ;;; a counter to measure the storage overhead of these fixups
67 (defvar *num-fixups* 0)
68 ;;; FIXME: When the system runs, it'd be interesting to see what this is.
69
70 (declaim (inline adjust-fixup-array))
71 (defun adjust-fixup-array (array size)
72   (let ((length (length array))
73         (new (make-array size :element-type '(unsigned-byte 32))))
74     (replace new array)
75     new))
76
77 ;;; This gets called by LOAD to resolve newly positioned objects
78 ;;; with things (like code instructions) that have to refer to them.
79 ;;;
80 ;;; Add a fixup offset to the vector of fixup offsets for the given
81 ;;; code object.
82 (defun fixup-code-object (code offset fixup kind)
83   (declare (type index offset))
84   (flet ((add-fixup (code offset)
85            ;; (We check for and ignore fixups for code objects in the
86            ;; read-only and static spaces. (In the old CMU CL code
87            ;; this check was conditional on *ENABLE-DYNAMIC-SPACE-CODE*,
88            ;; but in SBCL relocatable dynamic space code is always in
89            ;; use, so we always do the check.)
90            (incf *num-fixups*)
91            (let ((fixups (code-header-ref code code-constants-offset)))
92              (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
93                     (let ((new-fixups
94                            (adjust-fixup-array fixups (1+ (length fixups)))))
95                       (setf (aref new-fixups (length fixups)) offset)
96                       (setf (code-header-ref code code-constants-offset)
97                             new-fixups)))
98                    (t
99                     (unless (or (eq (widetag-of fixups)
100                                     unbound-marker-widetag)
101                                 (zerop fixups))
102                       (format t "** Init. code FU = ~S~%" fixups)) ; FIXME
103                     (setf (code-header-ref code code-constants-offset)
104                           (make-array
105                            1
106                            :element-type '(unsigned-byte 32)
107                            :initial-element offset)))))))
108     (sb!sys:without-gcing
109      (let* ((sap (truly-the system-area-pointer
110                             (sb!kernel:code-instructions code)))
111             (obj-start-addr (logand (sb!kernel:get-lisp-obj-address code)
112                                     #xfffffff8))
113             ;; FIXME: what is this 5?
114             #+nil (const-start-addr (+ obj-start-addr (* 5 n-word-bytes)))
115             (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
116                                               code)))
117             (ncode-words (sb!kernel:code-header-ref code 1))
118             (code-end-addr (+ code-start-addr (* ncode-words n-word-bytes))))
119        (unless (member kind '(:absolute :relative))
120          (error "Unknown code-object-fixup kind ~S." kind))
121        (ecase kind
122          (:absolute
123           ;; Word at sap + offset contains a value to be replaced by
124           ;; adding that value to fixup.
125           (setf (sap-ref-32 sap offset) (+ fixup (sap-ref-32 sap offset)))
126           ;; Record absolute fixups that point within the code object.
127           (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
128             (add-fixup code offset)))
129          (:relative
130           ;; Fixup is the actual address wanted.
131           ;;
132           ;; Record relative fixups that point outside the code
133           ;; object.
134           (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
135             (add-fixup code offset))
136           ;; Replace word with value to add to that loc to get there.
137           (let* ((loc-sap (+ (sap-int sap) offset))
138                  (rel-val (- fixup loc-sap n-word-bytes)))
139             (declare (type (unsigned-byte 32) loc-sap)
140                      (type (signed-byte 32) rel-val))
141             (setf (signed-sap-ref-32 sap offset) rel-val))))))
142     nil))
143
144 ;;; Add a code fixup to a code object generated by GENESIS. The fixup
145 ;;; has already been applied, it's just a matter of placing the fixup
146 ;;; in the code's fixup vector if necessary.
147 ;;;
148 ;;; KLUDGE: I'd like a good explanation of why this has to be done at
149 ;;; load time instead of in GENESIS. It's probably simple, I just haven't
150 ;;; figured it out, or found it written down anywhere. -- WHN 19990908
151 #!+gencgc
152 (defun !envector-load-time-code-fixup (code offset fixup kind)
153   (flet ((frob (code offset)
154            (let ((fixups (code-header-ref code code-constants-offset)))
155              (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
156                     (let ((new-fixups
157                            (adjust-fixup-array fixups (1+ (length fixups)))))
158                       (setf (aref new-fixups (length fixups)) offset)
159                       (setf (code-header-ref code code-constants-offset)
160                             new-fixups)))
161                    (t
162                     (unless (or (eq (widetag-of fixups)
163                                     unbound-marker-widetag)
164                                 (zerop fixups))
165                       (sb!impl::!cold-lose "Argh! can't process fixup"))
166                     (setf (code-header-ref code code-constants-offset)
167                           (make-array
168                            1
169                            :element-type '(unsigned-byte 32)
170                            :initial-element offset)))))))
171     (let* ((sap (truly-the system-area-pointer
172                            (sb!kernel:code-instructions code)))
173            (obj-start-addr
174             ;; FIXME: looks like (LOGANDC2 foo typebits)
175             (logand (sb!kernel:get-lisp-obj-address code) #xfffffff8))
176            (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
177                                              code)))
178            (ncode-words (sb!kernel:code-header-ref code 1))
179          (code-end-addr (+ code-start-addr (* ncode-words n-word-bytes))))
180       (ecase kind
181         (:absolute
182          ;; Record absolute fixups that point within the code object.
183          (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
184            (frob code offset)))
185         (:relative
186          ;; Record relative fixups that point outside the code object.
187          (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
188            (frob code offset)))))))
189 \f
190 ;;;; low-level signal context access functions
191 ;;;;
192 ;;;; Note: In CMU CL, similar functions were hardwired to access
193 ;;;; BSD-style sigcontext structures defined as alien objects. Our
194 ;;;; approach is different in two ways:
195 ;;;;   1. We use POSIX SA_SIGACTION-style signals, so our context is
196 ;;;;      whatever the void pointer in the sigaction handler dereferences
197 ;;;;      to, not necessarily a sigcontext.
198 ;;;;   2. We don't try to maintain alien definitions of the context
199 ;;;;      structure at Lisp level, but instead call alien C functions
200 ;;;;      which take care of access for us. (Since the C functions can
201 ;;;;      be defined in terms of system standard header files, they
202 ;;;;      should be easier to maintain; and since Lisp code uses signal
203 ;;;;      contexts only in interactive or exception code (like the debugger
204 ;;;;      and internal error handling) the extra runtime cost should be
205 ;;;;      negligible.
206
207 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
208   ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
209   ;; 'unsigned *' interpretation for the 32-bit word passed to us by
210   ;; the C code, even though the C code may think it's an 'int *'.)
211   (context (* os-context-t)))
212
213 (defun context-pc (context)
214   (declare (type (alien (* os-context-t)) context))
215   (let ((addr (context-pc-addr context)))
216     (declare (type (alien (* unsigned-int)) addr))
217     (int-sap (deref addr))))
218
219 (define-alien-routine ("os_context_register_addr" context-register-addr)
220   (* unsigned-int)
221   ;; (Note the mismatch here between the 'int *' value that the C code
222   ;; may think it's giving us and the 'unsigned *' value that we
223   ;; receive. It's intentional: the C header files may think of
224   ;; register values as signed, but the CMU CL code tends to think of
225   ;; register values as unsigned, and might get bewildered if we ask
226   ;; it to work with signed values.)
227   (context (* os-context-t))
228   (index int))
229
230 (defun context-register (context index)
231   (declare (type (alien (* os-context-t)) context))
232   (let ((addr (context-register-addr context index)))
233     (declare (type (alien (* unsigned-int)) addr))
234     (deref addr)))
235
236 (defun %set-context-register (context index new)
237   (declare (type (alien (* os-context-t)) context))
238   (let ((addr (context-register-addr context index)))
239     (declare (type (alien (* unsigned-int)) addr))
240     (setf (deref addr) new)))
241
242 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
243 ;;; register. FORMAT is the type of float to return.
244 ;;;
245 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
246 ;;; so it's stubbed out. Someday, in order to make the debugger work
247 ;;; better, it may be necessary to unstubify it.
248 (defun context-float-register (context index format)
249   (declare (ignore context index))
250   (warn "stub CONTEXT-FLOAT-REGISTER")
251   (coerce 0.0 format))
252 (defun %set-context-float-register (context index format new-value)
253   (declare (ignore context index))
254   (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
255   (coerce new-value format))
256
257 ;;; Given a signal context, return the floating point modes word in
258 ;;; the same format as returned by FLOATING-POINT-MODES.
259 #!-linux
260 (defun context-floating-point-modes (context)
261   ;; FIXME: As of sbcl-0.6.7 and the big rewrite of signal handling for
262   ;; POSIXness and (at the Lisp level) opaque signal contexts,
263   ;; this is stubified. It needs to be rewritten as an
264   ;; alien function.
265   (declare (ignore context)) ; stub!
266   (warn "stub CONTEXT-FLOATING-POINT-MODES")
267
268   ;; old code for Linux:
269   #+nil
270   (let ((cw (slot (deref (slot context 'fpstate) 0) 'cw))
271         (sw (slot (deref (slot context 'fpstate) 0) 'sw)))
272     ;;(format t "cw = ~4X~%sw = ~4X~%" cw sw)
273     ;; NOT TESTED -- Clear sticky bits to clear interrupt condition.
274     (setf (slot (deref (slot context 'fpstate) 0) 'sw) (logandc2 sw #x3f))
275     ;;(format t "new sw = ~X~%" (slot (deref (slot context 'fpstate) 0) 'sw))
276     ;; Simulate floating-point-modes VOP.
277     (logior (ash (logand sw #xffff) 16) (logxor (logand cw #xffff) #x3f)))
278
279   0)
280
281 #!+linux
282 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
283     (sb!alien:unsigned 32)
284   (context (* os-context-t)))
285 \f
286 ;;;; INTERNAL-ERROR-ARGS
287
288 ;;; Given a (POSIX) signal context, extract the internal error
289 ;;; arguments from the instruction stream.
290 (defun internal-error-args (context)
291   (declare (type (alien (* os-context-t)) context))
292   (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
293   (/hexstr context)
294   (let ((pc (context-pc context)))
295     (declare (type system-area-pointer pc))
296     (/show0 "got PC")
297     ;; using INT3 the pc is .. INT3 <here> code length bytes...
298     (let* ((length (sap-ref-8 pc 1))
299            (vector (make-array length :element-type '(unsigned-byte 8))))
300       (declare (type (unsigned-byte 8) length)
301                (type (simple-array (unsigned-byte 8) (*)) vector))
302       (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
303       (/hexstr length)
304       (/hexstr vector)
305       (copy-from-system-area pc (* n-byte-bits 2)
306                              vector (* n-word-bits vector-data-offset)
307                              (* length n-byte-bits))
308       (let* ((index 0)
309              (error-number (sb!c:read-var-integer vector index)))
310         (/hexstr error-number)
311         (collect ((sc-offsets))
312           (loop
313            (/show0 "INDEX=..")
314            (/hexstr index)
315            (when (>= index length)
316              (return))
317            (let ((sc-offset (sb!c:read-var-integer vector index)))
318              (/show0 "SC-OFFSET=..")
319              (/hexstr sc-offset)
320              (sc-offsets sc-offset)))
321           (values error-number (sc-offsets)))))))
322 \f
323 ;;; This is used in error.lisp to insure that floating-point exceptions
324 ;;; are properly trapped. The compiler translates this to a VOP.
325 (defun float-wait ()
326   (float-wait))
327
328 ;;; float constants
329 ;;;
330 ;;; These are used by the FP MOVE-FROM-{SINGLE|DOUBLE} VOPs rather
331 ;;; than the i387 load constant instructions to avoid consing in some
332 ;;; cases. Note these are initialized by GENESIS as they are needed
333 ;;; early.
334 (defvar *fp-constant-0f0*)
335 (defvar *fp-constant-1f0*)
336 (defvar *fp-constant-0d0*)
337 (defvar *fp-constant-1d0*)
338 ;;; the long-float constants
339 (defvar *fp-constant-0l0*)
340 (defvar *fp-constant-1l0*)
341 (defvar *fp-constant-pi*)
342 (defvar *fp-constant-l2t*)
343 (defvar *fp-constant-l2e*)
344 (defvar *fp-constant-lg2*)
345 (defvar *fp-constant-ln2*)
346
347 ;;; the current alien stack pointer; saved/restored for non-local exits
348 (defvar *alien-stack*)
349
350 ;;; Support for the MT19937 random number generator. The update
351 ;;; function is implemented as an assembly routine. This definition is
352 ;;; transformed to a call to the assembly routine allowing its use in
353 ;;; interpreted code.
354 (defun random-mt19937 (state)
355   (declare (type (simple-array (unsigned-byte 32) (627)) state))
356   (random-mt19937 state))