release, will be tagged as sbcl_1_0_27
[sbcl/tcr.git] / src / code / x86-vm.lisp
blobbb1bd223a1affc253e5dc7945eae822319e46ba1
1 ;;;; X86-specific runtime stuff
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.
12 (in-package "SB!VM")
14 ;;;; OS-CONTEXT-T
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))
37 ;;;; MACHINE-TYPE and MACHINE-VERSION
39 (defun machine-type ()
40 #!+sb-doc
41 "Return a string describing the type of the local machine."
42 "X86")
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)
64 ;;;; :CODE-OBJECT fixups
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.
70 (declaim (inline adjust-fixup-array))
71 (defun adjust-fixup-array (array size)
72 (let ((new (make-array size :element-type '(unsigned-byte 32))))
73 (replace new array)
74 new))
76 ;;; This gets called by LOAD to resolve newly positioned objects
77 ;;; with things (like code instructions) that have to refer to them.
78 ;;;
79 ;;; Add a fixup offset to the vector of fixup offsets for the given
80 ;;; code object.
81 (defun fixup-code-object (code offset fixup kind)
82 (declare (type index offset))
83 (flet ((add-fixup (code offset)
84 ;; (We check for and ignore fixups for code objects in the
85 ;; read-only and static spaces. (In the old CMU CL code
86 ;; this check was conditional on *ENABLE-DYNAMIC-SPACE-CODE*,
87 ;; but in SBCL relocatable dynamic space code is always in
88 ;; use, so we always do the check.)
89 (incf *num-fixups*)
90 (let ((fixups (code-header-ref code code-constants-offset)))
91 (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
92 (let ((new-fixups
93 (adjust-fixup-array fixups (1+ (length fixups)))))
94 (setf (aref new-fixups (length fixups)) offset)
95 (setf (code-header-ref code code-constants-offset)
96 new-fixups)))
98 (unless (or (eq (widetag-of fixups)
99 unbound-marker-widetag)
100 (zerop fixups))
101 (format t "** Init. code FU = ~S~%" fixups)) ; FIXME
102 (setf (code-header-ref code code-constants-offset)
103 (make-array
105 :element-type '(unsigned-byte 32)
106 :initial-element offset)))))))
107 (sb!sys:without-gcing
108 (let* ((sap (truly-the system-area-pointer
109 (sb!kernel:code-instructions code)))
110 (obj-start-addr (logand (sb!kernel:get-lisp-obj-address code)
111 #xfffffff8))
112 ;; FIXME: what is this 5?
113 #+nil (const-start-addr (+ obj-start-addr (* 5 n-word-bytes)))
114 (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
115 code)))
116 (ncode-words (sb!kernel:code-header-ref code 1))
117 (code-end-addr (+ code-start-addr (* ncode-words n-word-bytes))))
118 (unless (member kind '(:absolute :relative))
119 (error "Unknown code-object-fixup kind ~S." kind))
120 (ecase kind
121 (:absolute
122 ;; Word at sap + offset contains a value to be replaced by
123 ;; adding that value to fixup.
124 (setf (sap-ref-32 sap offset) (+ fixup (sap-ref-32 sap offset)))
125 ;; Record absolute fixups that point within the code object.
126 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
127 (add-fixup code offset)))
128 (:relative
129 ;; Fixup is the actual address wanted.
131 ;; Record relative fixups that point outside the code
132 ;; object.
133 (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
134 (add-fixup code offset))
135 ;; Replace word with value to add to that loc to get there.
136 (let* ((loc-sap (+ (sap-int sap) offset))
137 (rel-val (- fixup loc-sap n-word-bytes)))
138 (declare (type (unsigned-byte 32) loc-sap)
139 (type (signed-byte 32) rel-val))
140 (setf (signed-sap-ref-32 sap offset) rel-val))))))
141 nil))
143 ;;;; low-level signal context access functions
144 ;;;;
145 ;;;; Note: In CMU CL, similar functions were hardwired to access
146 ;;;; BSD-style sigcontext structures defined as alien objects. Our
147 ;;;; approach is different in two ways:
148 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
149 ;;;; whatever the void pointer in the sigaction handler dereferences
150 ;;;; to, not necessarily a sigcontext.
151 ;;;; 2. We don't try to maintain alien definitions of the context
152 ;;;; structure at Lisp level, but instead call alien C functions
153 ;;;; which take care of access for us. (Since the C functions can
154 ;;;; be defined in terms of system standard header files, they
155 ;;;; should be easier to maintain; and since Lisp code uses signal
156 ;;;; contexts only in interactive or exception code (like the debugger
157 ;;;; and internal error handling) the extra runtime cost should be
158 ;;;; negligible.
160 (declaim (inline context-pc-addr))
161 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
162 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
163 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
164 ;; the C code, even though the C code may think it's an 'int *'.)
165 (context (* os-context-t)))
167 (declaim (inline context-pc))
168 (defun context-pc (context)
169 (declare (type (alien (* os-context-t)) context))
170 (let ((addr (context-pc-addr context)))
171 (declare (type (alien (* unsigned-int)) addr))
172 (int-sap (deref addr))))
174 (declaim (inline context-register-addr))
175 (define-alien-routine ("os_context_register_addr" context-register-addr)
176 (* unsigned-int)
177 ;; (Note the mismatch here between the 'int *' value that the C code
178 ;; may think it's giving us and the 'unsigned *' value that we
179 ;; receive. It's intentional: the C header files may think of
180 ;; register values as signed, but the CMU CL code tends to think of
181 ;; register values as unsigned, and might get bewildered if we ask
182 ;; it to work with signed values.)
183 (context (* os-context-t))
184 (index int))
186 (declaim (inline context-register))
187 (defun context-register (context index)
188 (declare (type (alien (* os-context-t)) context))
189 (let ((addr (context-register-addr context index)))
190 (declare (type (alien (* unsigned-int)) addr))
191 (deref addr)))
193 (defun %set-context-register (context index new)
194 (declare (type (alien (* os-context-t)) context))
195 (let ((addr (context-register-addr context index)))
196 (declare (type (alien (* unsigned-int)) addr))
197 (setf (deref addr) new)))
199 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
200 ;;; register. FORMAT is the type of float to return.
202 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
203 ;;; so it's stubbed out. Someday, in order to make the debugger work
204 ;;; better, it may be necessary to unstubify it.
205 (defun context-float-register (context index format)
206 (declare (ignore context index))
207 (warn "stub CONTEXT-FLOAT-REGISTER")
208 (coerce 0.0 format))
209 (defun %set-context-float-register (context index format new-value)
210 (declare (ignore context index))
211 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
212 (coerce new-value format))
214 ;;; Given a signal context, return the floating point modes word in
215 ;;; the same format as returned by FLOATING-POINT-MODES.
216 #!-(or linux sunos)
217 (defun context-floating-point-modes (context)
218 ;; FIXME: As of sbcl-0.6.7 and the big rewrite of signal handling for
219 ;; POSIXness and (at the Lisp level) opaque signal contexts,
220 ;; this is stubified. It needs to be rewritten as an
221 ;; alien function.
222 (declare (ignore context)) ; stub!
223 (warn "stub CONTEXT-FLOATING-POINT-MODES")
226 #!+(or linux sunos)
227 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
228 (sb!alien:unsigned 32)
229 (context (* os-context-t)))
231 ;;;; INTERNAL-ERROR-ARGS
233 ;;; Given a (POSIX) signal context, extract the internal error
234 ;;; arguments from the instruction stream.
235 (defun internal-error-args (context)
236 (declare (type (alien (* os-context-t)) context))
237 (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
238 (/hexstr context)
239 (let ((pc (context-pc context)))
240 (declare (type system-area-pointer pc))
241 (/show0 "got PC")
242 ;; using INT3 the pc is .. INT3 <here> code length bytes...
243 (let* ((length (sap-ref-8 pc 1))
244 (vector (make-array length :element-type '(unsigned-byte 8))))
245 (declare (type (unsigned-byte 8) length)
246 (type (simple-array (unsigned-byte 8) (*)) vector))
247 (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
248 (/hexstr length)
249 (/hexstr vector)
250 (copy-ub8-from-system-area pc 2 vector 0 length)
251 (let* ((index 0)
252 (error-number (sb!c:read-var-integer vector index)))
253 (/hexstr error-number)
254 (collect ((sc-offsets))
255 (loop
256 (/show0 "INDEX=..")
257 (/hexstr index)
258 (when (>= index length)
259 (return))
260 (let ((sc-offset (sb!c:read-var-integer vector index)))
261 (/show0 "SC-OFFSET=..")
262 (/hexstr sc-offset)
263 (sc-offsets sc-offset)))
264 (values error-number (sc-offsets)))))))
266 ;;; This is used in error.lisp to insure that floating-point exceptions
267 ;;; are properly trapped. The compiler translates this to a VOP.
268 (defun float-wait ()
269 (float-wait))
271 ;;; float constants
273 ;;; These are used by the FP MOVE-FROM-{SINGLE|DOUBLE} VOPs rather
274 ;;; than the i387 load constant instructions to avoid consing in some
275 ;;; cases. Note these are initialized by GENESIS as they are needed
276 ;;; early.
277 (defvar *fp-constant-0f0*)
278 (defvar *fp-constant-1f0*)
279 (defvar *fp-constant-0d0*)
280 (defvar *fp-constant-1d0*)
281 ;;; the long-float constants
282 (defvar *fp-constant-0l0*)
283 (defvar *fp-constant-1l0*)
284 (defvar *fp-constant-pi*)
285 (defvar *fp-constant-l2t*)
286 (defvar *fp-constant-l2e*)
287 (defvar *fp-constant-lg2*)
288 (defvar *fp-constant-ln2*)
290 ;;; the current alien stack pointer; saved/restored for non-local exits
291 (defvar *alien-stack*)
293 ;;; Support for the MT19937 random number generator. The update
294 ;;; function is implemented as an assembly routine. This definition is
295 ;;; transformed to a call to the assembly routine allowing its use in
296 ;;; interpreted code.
297 (defun random-mt19937 (state)
298 (declare (type (simple-array (unsigned-byte 32) (627)) state))
299 (random-mt19937 state))