1.0.19.7: refactor stack allocation decisions
[sbcl/pkhuong.git] / src / code / x86-64-vm.lisp
bloba5a025b816df4a5eb471cbff9eacde019cbb5266
1 ;;;; X86-64-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-64")
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 64))))
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 64) (*)))
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 64)
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 (logandc2 (sb!kernel:get-lisp-obj-address code)
111 sb!vm:lowtag-mask))
112 (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
113 code)))
114 (ncode-words (sb!kernel:code-header-ref code 1))
115 (code-end-addr (+ code-start-addr (* ncode-words n-word-bytes))))
116 (unless (member kind '(:absolute :absolute64 :relative))
117 (error "Unknown code-object-fixup kind ~S." kind))
118 (ecase kind
119 (:absolute64
120 ;; Word at sap + offset contains a value to be replaced by
121 ;; adding that value to fixup.
122 (setf (sap-ref-64 sap offset) (+ fixup (sap-ref-64 sap offset)))
123 ;; Record absolute fixups that point within the code object.
124 (when (> code-end-addr (sap-ref-64 sap offset) obj-start-addr)
125 (add-fixup code offset)))
126 (:absolute
127 ;; Word at sap + offset contains a value to be replaced by
128 ;; adding that value to fixup.
129 (setf (sap-ref-32 sap offset) (+ fixup (sap-ref-32 sap offset)))
130 ;; Record absolute fixups that point within the code object.
131 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
132 (add-fixup code offset)))
133 (:relative
134 ;; Fixup is the actual address wanted.
136 ;; Record relative fixups that point outside the code
137 ;; object.
138 (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
139 (add-fixup code offset))
140 ;; Replace word with value to add to that loc to get there.
141 (let* ((loc-sap (+ (sap-int sap) offset))
142 (rel-val (- fixup loc-sap (/ n-word-bytes 2))))
143 (declare (type (unsigned-byte 64) loc-sap)
144 (type (signed-byte 32) rel-val))
145 (setf (signed-sap-ref-32 sap offset) rel-val))))))
146 nil))
148 ;;; Add a code fixup to a code object generated by GENESIS. The fixup
149 ;;; has already been applied, it's just a matter of placing the fixup
150 ;;; in the code's fixup vector if necessary.
152 ;;; KLUDGE: I'd like a good explanation of why this has to be done at
153 ;;; load time instead of in GENESIS. It's probably simple, I just haven't
154 ;;; figured it out, or found it written down anywhere. -- WHN 19990908
155 #!+gencgc
156 (defun !envector-load-time-code-fixup (code offset fixup kind)
157 (flet ((frob (code offset)
158 (let ((fixups (code-header-ref code code-constants-offset)))
159 (cond ((typep fixups '(simple-array (unsigned-byte 64) (*)))
160 (let ((new-fixups
161 (adjust-fixup-array fixups (1+ (length fixups)))))
162 (setf (aref new-fixups (length fixups)) offset)
163 (setf (code-header-ref code code-constants-offset)
164 new-fixups)))
166 (unless (or (eq (widetag-of fixups)
167 unbound-marker-widetag)
168 (zerop fixups))
169 (sb!impl::!cold-lose "Argh! can't process fixup"))
170 (setf (code-header-ref code code-constants-offset)
171 (make-array
173 :element-type '(unsigned-byte 64)
174 :initial-element offset)))))))
175 (let* ((sap (truly-the system-area-pointer
176 (sb!kernel:code-instructions code)))
177 (obj-start-addr
178 (logandc2 (sb!kernel:get-lisp-obj-address code) sb!vm:lowtag-mask))
179 (code-start-addr (sb!sys:sap-int (sb!kernel:code-instructions
180 code)))
181 (ncode-words (sb!kernel:code-header-ref code 1))
182 (code-end-addr (+ code-start-addr (* ncode-words n-word-bytes))))
183 (ecase kind
184 (:absolute
185 ;; Record absolute fixups that point within the code object.
186 ;; The fixup data is 32 bits, don't use SAP-REF-64 here.
187 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
188 (frob code offset)))
189 (:relative
190 ;; Record relative fixups that point outside the code object.
191 (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
192 (frob code offset)))))))
194 ;;;; low-level signal context access functions
195 ;;;;
196 ;;;; Note: In CMU CL, similar functions were hardwired to access
197 ;;;; BSD-style sigcontext structures defined as alien objects. Our
198 ;;;; approach is different in two ways:
199 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
200 ;;;; whatever the void pointer in the sigaction handler dereferences
201 ;;;; to, not necessarily a sigcontext.
202 ;;;; 2. We don't try to maintain alien definitions of the context
203 ;;;; structure at Lisp level, but instead call alien C functions
204 ;;;; which take care of access for us. (Since the C functions can
205 ;;;; be defined in terms of system standard header files, they
206 ;;;; should be easier to maintain; and since Lisp code uses signal
207 ;;;; contexts only in interactive or exception code (like the debugger
208 ;;;; and internal error handling) the extra runtime cost should be
209 ;;;; negligible.
211 (declaim (inline context-pc-addr))
212 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
213 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
214 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
215 ;; the C code, even though the C code may think it's an 'int *'.)
216 (context (* os-context-t)))
218 (declaim (inline context-pc))
219 (defun context-pc (context)
220 (declare (type (alien (* os-context-t)) context))
221 (let ((addr (context-pc-addr context)))
222 (declare (type (alien (* unsigned-long)) addr))
223 (int-sap (deref addr))))
225 (declaim (inline context-register-addr))
226 (define-alien-routine ("os_context_register_addr" context-register-addr)
227 (* unsigned-long)
228 ;; (Note the mismatch here between the 'int *' value that the C code
229 ;; may think it's giving us and the 'unsigned *' value that we
230 ;; receive. It's intentional: the C header files may think of
231 ;; register values as signed, but the CMU CL code tends to think of
232 ;; register values as unsigned, and might get bewildered if we ask
233 ;; it to work with signed values.)
234 (context (* os-context-t))
235 (index int))
237 (declaim (inline context-register))
238 (defun context-register (context index)
239 (declare (type (alien (* os-context-t)) context))
240 (let ((addr (context-register-addr context index)))
241 (declare (type (alien (* unsigned-long)) addr))
242 (deref addr)))
244 (defun %set-context-register (context index new)
245 (declare (type (alien (* os-context-t)) context))
246 (let ((addr (context-register-addr context index)))
247 (declare (type (alien (* unsigned-long)) addr))
248 (setf (deref addr) new)))
250 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
251 ;;; register. FORMAT is the type of float to return.
253 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
254 ;;; so it's stubbed out. Someday, in order to make the debugger work
255 ;;; better, it may be necessary to unstubify it.
256 (defun context-float-register (context index format)
257 (declare (ignore context index))
258 (warn "stub CONTEXT-FLOAT-REGISTER")
259 (coerce 0.0 format))
260 (defun %set-context-float-register (context index format new-value)
261 (declare (ignore context index))
262 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
263 (coerce new-value format))
265 ;;; Given a signal context, return the floating point modes word in
266 ;;; the same format as returned by FLOATING-POINT-MODES.
267 #!-linux
268 (defun context-floating-point-modes (context)
269 (declare (ignore context)) ; stub!
270 (warn "stub CONTEXT-FLOATING-POINT-MODES")
272 #!+linux
273 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
274 (sb!alien:unsigned 32)
275 (context (* os-context-t)))
277 (define-alien-routine
278 ("arch_get_fp_modes" floating-point-modes) (sb!alien:unsigned 32))
280 (define-alien-routine
281 ("arch_set_fp_modes" %floating-point-modes-setter) void (fp (sb!alien:unsigned 32)))
283 (defun (setf floating-point-modes) (val) (%floating-point-modes-setter val))
286 ;;;; INTERNAL-ERROR-ARGS
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-ub8-from-system-area pc 2 vector 0 length)
306 (let* ((index 0)
307 (error-number (sb!c:read-var-integer vector index)))
308 (/hexstr error-number)
309 (collect ((sc-offsets))
310 (loop
311 (/show0 "INDEX=..")
312 (/hexstr index)
313 (when (>= index length)
314 (return))
315 (let ((sc-offset (sb!c:read-var-integer vector index)))
316 (/show0 "SC-OFFSET=..")
317 (/hexstr sc-offset)
318 (sc-offsets sc-offset)))
319 (values error-number (sc-offsets)))))))
322 ;;; the current alien stack pointer; saved/restored for non-local exits
323 (defvar *alien-stack*)