1 ;;;; X86-64-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.
16 ;;; a POSIX signal context, i.e. the type passed as the third
17 ;;; argument to an SA_SIGACTION-style signal handler
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.
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
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
))
39 (defun machine-type ()
40 "Return a string describing the type of the local machine."
43 ;;;; :CODE-OBJECT fixups
45 ;;; This gets called by LOAD to resolve newly positioned objects
46 ;;; with things (like code instructions) that have to refer to them.
47 (defun fixup-code-object (code offset fixup kind
&optional flavor
)
48 (declare (type index offset
) (ignorable flavor
))
50 (let ((sap (code-instructions code
)))
53 ;; Word at sap + offset contains a value to be replaced by
54 ;; adding that value to fixup.
55 (setf (sap-ref-64 sap offset
) (+ fixup
(sap-ref-64 sap offset
))))
57 ;; Word at sap + offset contains a value to be replaced by
58 ;; adding that value to fixup.
59 (setf (sap-ref-32 sap offset
) (+ fixup
(signed-sap-ref-32 sap offset
))))
61 ;; Fixup is the actual address wanted.
62 ;; Replace word with value to add to that loc to get there.
63 ;; In the #!-immobile-code case, there's nothing to assert.
64 ;; Relative fixups pretty much can't happen.
66 (unless (<= immobile-space-start
(get-lisp-obj-address code
) immobile-space-end
)
67 (error "Can't compute fixup relative to movable object ~S" code
))
68 (setf (signed-sap-ref-32 sap offset
)
71 ;; JMP/CALL are relative to the next instruction,
72 ;; so add 4 bytes for the size of the displacement itself.
74 (the (unsigned-byte 64) (+ (sap-int sap
) offset
4))))))))))
75 ;; An absolute fixup is stored in the code header if it
76 ;; references an immobile-space (but not static-space) object.
77 ;; This needn't be inside WITHOUT-GCING, because code fixups will point
78 ;; only to objects that don't move except during save-lisp-and-die.
79 ;; So there is no race with GC here.
81 (when (eq flavor
:immobile-object
)
82 (let ((fixups (sb!vm
::%code-fixups code
)))
83 ;; Sanctifying the code component will compact these into a bignum.
84 (setf (sb!vm
::%code-fixups code
)
85 (cons offset
(if (eql fixups
0) nil fixups
)))))
89 (defun sanctify-for-execution (code)
90 (let ((fixups (sb!vm
::%code-fixups code
)))
92 (setf (sb!vm
::%code-fixups code
) (sb!c
::pack-code-fixup-locs fixups
))))
95 ;;;; low-level signal context access functions
97 ;;;; Note: In CMU CL, similar functions were hardwired to access
98 ;;;; BSD-style sigcontext structures defined as alien objects. Our
99 ;;;; approach is different in two ways:
100 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
101 ;;;; whatever the void pointer in the sigaction handler dereferences
102 ;;;; to, not necessarily a sigcontext.
103 ;;;; 2. We don't try to maintain alien definitions of the context
104 ;;;; structure at Lisp level, but instead call alien C functions
105 ;;;; which take care of access for us. (Since the C functions can
106 ;;;; be defined in terms of system standard header files, they
107 ;;;; should be easier to maintain; and since Lisp code uses signal
108 ;;;; contexts only in interactive or exception code (like the debugger
109 ;;;; and internal error handling) the extra runtime cost should be
112 (declaim (inline context-pc-addr
))
113 (define-alien-routine ("os_context_pc_addr" context-pc-addr
) (* unsigned
)
114 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
115 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
116 ;; the C code, even though the C code may think it's an 'int *'.)
117 (context (* os-context-t
)))
119 (declaim (inline context-pc
))
120 (defun context-pc (context)
121 (declare (type (alien (* os-context-t
)) context
))
122 (let ((addr (context-pc-addr context
)))
123 (declare (type (alien (* unsigned
)) addr
))
124 (int-sap (deref addr
))))
126 (declaim (inline set-context-pc
))
127 (defun incf-context-pc (context offset
)
128 (declare (type (alien (* os-context-t
)) context
))
129 (let ((addr (context-pc-addr context
)))
130 (declare (type (alien (* unsigned
)) addr
))
131 (setf (deref addr
) (+ (deref addr
) offset
))))
133 (declaim (inline context-register-addr
))
134 (define-alien-routine ("os_context_register_addr" context-register-addr
)
136 ;; (Note the mismatch here between the 'int *' value that the C code
137 ;; may think it's giving us and the 'unsigned *' value that we
138 ;; receive. It's intentional: the C header files may think of
139 ;; register values as signed, but the CMU CL code tends to think of
140 ;; register values as unsigned, and might get bewildered if we ask
141 ;; it to work with signed values.)
142 (context (* os-context-t
))
145 #!+(or darwin linux win32
)
146 (define-alien-routine ("os_context_float_register_addr" context-float-register-addr
)
147 (* unsigned
) (context (* os-context-t
)) (index int
))
149 (declaim (inline context-register
))
150 (defun context-register (context index
)
151 (declare (type (alien (* os-context-t
)) context
))
152 (let ((addr (context-register-addr context index
)))
153 (declare (type (alien (* unsigned
)) addr
))
156 (defun %set-context-register
(context index new
)
157 (declare (type (alien (* os-context-t
)) context
))
158 (let ((addr (context-register-addr context index
)))
159 (declare (type (alien (* unsigned
)) addr
))
160 (setf (deref addr
) new
)))
162 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
163 ;;; register. FORMAT is the type of float to return.
165 (defun context-float-register (context index format
)
166 (declare (ignorable context index
))
167 #!-
(or darwin linux win32
)
169 (warn "stub CONTEXT-FLOAT-REGISTER")
171 #!+(or darwin linux win32
)
172 (let ((sap (alien-sap (context-float-register-addr context index
))))
175 (sap-ref-single sap
0))
177 (sap-ref-double sap
0))
178 (complex-single-float
179 (complex (sap-ref-single sap
0)
180 (sap-ref-single sap
4)))
181 (complex-double-float
182 (complex (sap-ref-double sap
0)
183 (sap-ref-double sap
8))))))
185 (defun %set-context-float-register
(context index format value
)
186 (declare (ignorable context index format
))
189 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
192 (let ((sap (alien-sap (context-float-register-addr context index
))))
195 (setf (sap-ref-single sap
0) value
))
197 (setf (sap-ref-double sap
0) value
))
198 (complex-single-float
200 (declare (type (complex single-float
) value
))
201 (setf (sap-ref-single sap
0) (realpart value
)
202 (sap-ref-single sap
4) (imagpart value
))))
203 (complex-double-float
205 (declare (type (complex double-float
) value
))
206 (setf (sap-ref-double sap
0) (realpart value
)
207 (sap-ref-double sap
8) (imagpart value
)))))))
209 ;;; Given a signal context, return the floating point modes word in
210 ;;; the same format as returned by FLOATING-POINT-MODES.
212 (defun context-floating-point-modes (context)
213 (declare (ignore context
)) ; stub!
214 (warn "stub CONTEXT-FLOATING-POINT-MODES")
217 (define-alien-routine ("os_context_fp_control" context-floating-point-modes
)
219 (context (* os-context-t
)))
221 (define-alien-routine
222 ("arch_get_fp_modes" floating-point-modes
) (unsigned 32))
224 (define-alien-routine
225 ("arch_set_fp_modes" %floating-point-modes-setter
) void
(fp (unsigned 32)))
227 (defun (setf floating-point-modes
) (val) (%floating-point-modes-setter val
))
230 ;;;; INTERNAL-ERROR-ARGS
232 ;;; Given a (POSIX) signal context, extract the internal error
233 ;;; arguments from the instruction stream.
234 (defun internal-error-args (context)
235 (declare (type (alien (* os-context-t
)) context
))
236 (/show0
"entering INTERNAL-ERROR-ARGS, CONTEXT=..")
238 (let* ((pc (context-pc context
))
239 (trap-number (sap-ref-8 pc
0)))
240 (declare (type system-area-pointer pc
))
242 ;; using INT3 the pc is .. INT3 <here> code length bytes...
243 (if (= trap-number invalid-arg-count-trap
)
244 (values #.
(error-number-or-lose 'invalid-arg-count-error
)
246 (let ((error-number (sap-ref-8 pc
1)))
248 (sb!kernel
::decode-internal-error-args
(sap+ pc
2) error-number
)
252 ;;; the current alien stack pointer; saved/restored for non-local exits
253 (defvar *alien-stack-pointer
*)