1 ;;;; X86-specific runtime stuff
3 ;;;; This software is part of the SBCL system. See the README file for
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.
19 ;;; a POSIX signal context, i.e. the type passed as the third
20 ;;; argument to an SA_SIGACTION-style signal handler
22 ;;; The real type does have slots, but at Lisp level, we never
23 ;;; access them, or care about the size of the object. Instead, we
24 ;;; always refer to these objects by pointers handed to us by the C
25 ;;; runtime library, and ask the runtime library any time we need
26 ;;; information about the contents of one of these objects. Thus, it
27 ;;; works to represent this as an object with no slots.
29 ;;; KLUDGE: It would be nice to have a type definition analogous to
30 ;;; C's "struct os_context_t;", for an incompletely specified object
31 ;;; which can only be referred to by reference, but I don't know how
32 ;;; to do that in the FFI, so instead we just this bogus no-slots
33 ;;; representation. -- WHN 20000730
35 ;;; FIXME: Since SBCL, unlike CMU CL, uses this as an opaque type,
36 ;;; it's no longer architecture-dependent, and probably belongs in
37 ;;; some other package, perhaps SB-KERNEL.
38 (def-alien-type os-context-t (struct os-context-t-struct))
40 ;;;; MACHINE-TYPE and MACHINE-VERSION
42 (defun machine-type ()
44 "Returns a string describing the type of the local machine."
47 (defun machine-version ()
49 "Returns a string describing the version of the local machine."
52 ;;;; :CODE-OBJECT fixups
54 ;;; a counter to measure the storage overhead of these fixups
55 (defvar *num-fixups* 0)
56 ;;; FIXME: When the system runs, it'd be interesting to see what this is.
58 ;;; This gets called by LOAD to resolve newly positioned objects
59 ;;; with things (like code instructions) that have to refer to them.
61 ;;; Add a fixup offset to the vector of fixup offsets for the given
63 (defun fixup-code-object (code offset fixup kind)
64 (declare (type index offset))
65 (flet ((add-fixup (code offset)
66 ;; Although this could check for and ignore fixups for code
67 ;; objects in the read-only and static spaces, this should
68 ;; only be the case when *enable-dynamic-space-code* is
70 (when sb!impl::*enable-dynamic-space-code*
72 (let ((fixups (code-header-ref code code-constants-offset)))
73 (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
75 (adjust-array fixups (1+ (length fixups))
76 :element-type '(unsigned-byte 32))))
77 (setf (aref new-fixups (length fixups)) offset)
78 (setf (code-header-ref code code-constants-offset)
81 (unless (or (eq (get-type fixups)
82 sb!vm:unbound-marker-type)
84 (format t "** Init. code FU = ~S~%" fixups)) ; FIXME
85 (setf (code-header-ref code code-constants-offset)
86 (make-specializable-array
88 :element-type '(unsigned-byte 32)
89 :initial-element offset))))))))
91 (let* ((sap (truly-the system-area-pointer
92 (sb!kernel:code-instructions code)))
93 (obj-start-addr (logand (sb!kernel:get-lisp-obj-address code)
95 #+nil (const-start-addr (+ obj-start-addr (* 5 4)))
96 (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
98 (ncode-words (sb!kernel:code-header-ref code 1))
99 (code-end-addr (+ code-start-addr (* ncode-words 4))))
100 (unless (member kind '(:absolute :relative))
101 (error "Unknown code-object-fixup kind ~S." kind))
104 ;; Word at sap + offset contains a value to be replaced by
105 ;; adding that value to fixup.
106 (setf (sap-ref-32 sap offset) (+ fixup (sap-ref-32 sap offset)))
107 ;; Record absolute fixups that point within the code object.
108 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
109 (add-fixup code offset)))
111 ;; Fixup is the actual address wanted.
113 ;; Record relative fixups that point outside the code
115 (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
116 (add-fixup code offset))
117 ;; Replace word with value to add to that loc to get there.
118 (let* ((loc-sap (+ (sap-int sap) offset))
119 (rel-val (- fixup loc-sap 4)))
120 (declare (type (unsigned-byte 32) loc-sap)
121 (type (signed-byte 32) rel-val))
122 (setf (signed-sap-ref-32 sap offset) rel-val))))))
125 ;;; Add a code fixup to a code object generated by GENESIS. The fixup has
126 ;;; already been applied, it's just a matter of placing the fixup in the code's
127 ;;; fixup vector if necessary.
129 ;;; KLUDGE: I'd like a good explanation of why this has to be done at
130 ;;; load time instead of in GENESIS. It's probably simple, I just haven't
131 ;;; figured it out, or found it written down anywhere. -- WHN 19990908
133 (defun do-load-time-code-fixup (code offset fixup kind)
134 (flet ((add-load-time-code-fixup (code offset)
135 (let ((fixups (code-header-ref code sb!vm:code-constants-offset)))
136 (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
138 (adjust-array fixups (1+ (length fixups))
139 :element-type '(unsigned-byte 32))))
140 (setf (aref new-fixups (length fixups)) offset)
141 (setf (code-header-ref code sb!vm:code-constants-offset)
144 ;; FIXME: This doesn't look like production code, and
145 ;; should be a fatal error, not just a print.
146 (unless (or (eq (get-type fixups)
147 sb!vm:unbound-marker-type)
149 (%primitive print "** Init. code FU"))
150 (setf (code-header-ref code sb!vm:code-constants-offset)
151 (make-specializable-array
153 :element-type '(unsigned-byte 32)
154 :initial-element offset)))))))
155 (let* ((sap (truly-the system-area-pointer
156 (sb!kernel:code-instructions code)))
158 ;; FIXME: looks like (LOGANDC2 foo typebits)
159 (logand (sb!kernel:get-lisp-obj-address code) #xfffffff8))
160 (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
162 (ncode-words (sb!kernel:code-header-ref code 1))
163 (code-end-addr (+ code-start-addr (* ncode-words 4))))
166 ;; Record absolute fixups that point within the code object.
167 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
168 (add-load-time-code-fixup code offset)))
170 ;; Record relative fixups that point outside the code object.
171 (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
172 (add-load-time-code-fixup code offset)))))))
174 ;;;; low-level signal context access functions
176 ;;;; Note: In CMU CL, similar functions were hardwired to access
177 ;;;; BSD-style sigcontext structures defined as alien objects. Our
178 ;;;; approach is different in two ways:
179 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
180 ;;;; whatever the void pointer in the sigaction handler dereferences
181 ;;;; to, not necessarily a sigcontext.
182 ;;;; 2. We don't try to maintain alien definitions of the context
183 ;;;; structure at Lisp level, but instead call alien C functions
184 ;;;; which take care of access for us. (Since the C functions can
185 ;;;; be defined in terms of system standard header files, they
186 ;;;; should be easier to maintain; and since Lisp code uses signal
187 ;;;; contexts only in interactive or exception code (like the debugger
188 ;;;; and internal error handling) the extra runtime cost should be
191 (def-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
192 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
193 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
194 ;; the C code, even though the C code may think it's an 'int *'.)
195 (context (* os-context-t)))
197 (defun context-pc (context)
198 (declare (type (alien (* os-context-t)) context))
199 (int-sap (deref (context-pc-addr context))))
201 (def-alien-routine ("os_context_register_addr" context-register-addr)
203 ;; (Note the mismatch here between the 'int *' value that the C code
204 ;; may think it's giving us and the 'unsigned *' value that we
205 ;; receive. It's intentional: the C header files may think of
206 ;; register values as signed, but the CMU CL code tends to think of
207 ;; register values as unsigned, and might get bewildered if we ask
208 ;; it to work with signed values.)
209 (context (* os-context-t))
212 ;;; FIXME: Should this and CONTEXT-PC be INLINE to reduce consing?
213 ;;; (Are they used in anything time-critical, or just the debugger?)
214 (defun context-register (context index)
215 (declare (type (alien (* os-context-t)) context))
216 (deref (context-register-addr context index)))
218 (defun %set-context-register (context index new)
219 (declare (type (alien (* os-context-t)) context))
220 (setf (deref (context-register-addr context index))
223 ;;; Like CONTEXT-REGISTER, but returns the value of a float register.
224 ;;; FORMAT is the type of float to return.
226 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
227 ;;; so it's stubbed out. Someday, in order to make the debugger work
228 ;;; better, it may be necessary to unstubify it.
229 (defun context-float-register (context index format)
230 (declare (ignore context index))
231 (warn "stub CONTEXT-FLOAT-REGISTER")
233 (defun %set-context-float-register (context index format new-value)
234 (declare (ignore context index))
235 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
236 (coerce new-value format))
238 ;;; Given a signal context, return the floating point modes word in
239 ;;; the same format as returned by FLOATING-POINT-MODES.
240 (defun context-floating-point-modes (context)
241 ;; FIXME: As of sbcl-0.6.7 and the big rewrite of signal handling for
242 ;; POSIXness and (at the Lisp level) opaque signal contexts,
243 ;; this is stubified. It needs to be rewritten as an
245 (warn "stub CONTEXT-FLOATING-POINT-MODES")
247 ;; old code for Linux:
249 (let ((cw (slot (deref (slot context 'fpstate) 0) 'cw))
250 (sw (slot (deref (slot context 'fpstate) 0) 'sw)))
251 ;;(format t "cw = ~4X~%sw = ~4X~%" cw sw)
252 ;; NOT TESTED -- Clear sticky bits to clear interrupt condition.
253 (setf (slot (deref (slot context 'fpstate) 0) 'sw) (logandc2 sw #x3f))
254 ;;(format t "new sw = ~X~%" (slot (deref (slot context 'fpstate) 0) 'sw))
255 ;; Simulate floating-point-modes VOP.
256 (logior (ash (logand sw #xffff) 16) (logxor (logand cw #xffff) #x3f)))
260 ;;;; INTERNAL-ERROR-ARGUMENTS
262 ;;; Given a (POSIX) signal context, extract the internal error
263 ;;; arguments from the instruction stream.
264 (defun internal-error-arguments (context)
265 (declare (type (alien (* os-context-t)) context))
266 (/show0 "entering INTERNAL-ERROR-ARGUMENTS, CONTEXT=..")
267 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr context))
268 (let ((pc (context-pc context)))
269 (declare (type system-area-pointer pc))
270 ;; using INT3 the pc is .. INT3 <here> code length bytes...
271 (let* ((length (sap-ref-8 pc 1))
272 (vector (make-array length :element-type '(unsigned-byte 8))))
273 (declare (type (unsigned-byte 8) length)
274 (type (simple-array (unsigned-byte 8) (*)) vector))
275 (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
276 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr length))
277 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr vector))
278 (copy-from-system-area pc (* sb!vm:byte-bits 2)
279 vector (* sb!vm:word-bits
280 sb!vm:vector-data-offset)
281 (* length sb!vm:byte-bits))
283 (error-number (sb!c::read-var-integer vector index)))
284 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr error-number))
285 (collect ((sc-offsets))
288 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr index))
289 (when (>= index length)
291 (let ((sc-offset (sb!c::read-var-integer vector index)))
292 (/show0 "SC-OFFSET=..")
293 #!+sb-show (sb!sys:%primitive print (sb!impl::hexstr sc-offset))
294 (sc-offsets sc-offset)))
295 (values error-number (sc-offsets)))))))
297 ;;; Do whatever is necessary to make the given code component
298 ;;; executable. (This is a no-op on the x86.)
299 (defun sanctify-for-execution (component)
300 (declare (ignore component))
305 ;;; This is used in error.lisp to insure that floating-point exceptions
306 ;;; are properly trapped. The compiler translates this to a VOP.
312 ;;; These are used by the FP MOVE-FROM-{SINGLE|DOUBLE} VOPs rather than the
313 ;;; i387 load constant instructions to avoid consing in some cases. Note these
314 ;;; are initialized by GENESIS as they are needed early.
315 (defvar *fp-constant-0s0*)
316 (defvar *fp-constant-1s0*)
317 (defvar *fp-constant-0d0*)
318 (defvar *fp-constant-1d0*)
319 ;;; The long-float constants.
320 (defvar *fp-constant-0l0*)
321 (defvar *fp-constant-1l0*)
322 (defvar *fp-constant-pi*)
323 (defvar *fp-constant-l2t*)
324 (defvar *fp-constant-l2e*)
325 (defvar *fp-constant-lg2*)
326 (defvar *fp-constant-ln2*)
328 ;;; Enable/disable scavenging of the read-only space.
329 (defvar *scavenge-read-only-space* nil)
330 ;;; FIXME: should be *SCAVENGE-READ-ONLY-SPACE-P*
332 ;;; The current alien stack pointer; saved/restored for non-local exits.
333 (defvar *alien-stack*)
335 (defun sb!kernel::%instance-set-conditional (object slot test-value new-value)
336 (declare (type instance object)
339 "Atomically compare object's slot value to test-value and if EQ store
340 new-value in the slot. The original value of the slot is returned."
341 (sb!kernel::%instance-set-conditional object slot test-value new-value))
343 ;;; Support for the MT19937 random number generator. The update
344 ;;; function is implemented as an assembly routine. This definition is
345 ;;; transformed to a call to the assembly routine allowing its use in byte
347 (defun random-mt19937 (state)
348 (declare (type (simple-array (unsigned-byte 32) (627)) state))
349 (random-mt19937 state))