e263853b899a3c345ca9d19ee71d06ed0bb6b517
[sbcl.git] / src / code / x86-64-vm.lisp
1 ;;;; X86-64-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-64")
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 ;;; This gets called by LOAD to resolve newly positioned objects
67 ;;; with things (like code instructions) that have to refer to them.
68 (defun fixup-code-object (code offset fixup kind)
69   (declare (type index offset))
70   (sb!sys:without-gcing
71     (let ((sap (truly-the system-area-pointer
72                           (sb!kernel:code-instructions code))))
73       (unless (member kind '(:absolute :absolute64 :relative))
74         (error "Unknown code-object-fixup kind ~S." kind))
75       (ecase kind
76         (:absolute64
77          ;; Word at sap + offset contains a value to be replaced by
78          ;; adding that value to fixup.
79          (setf (sap-ref-64 sap offset) (+ fixup (sap-ref-64 sap offset))))
80         (:absolute
81          ;; Word at sap + offset contains a value to be replaced by
82          ;; adding that value to fixup.
83          (setf (sap-ref-32 sap offset) (+ fixup (sap-ref-32 sap offset))))
84         (:relative
85          ;; Fixup is the actual address wanted.
86          ;; Replace word with value to add to that loc to get there.
87          (let* ((loc-sap (+ (sap-int sap) offset))
88                 (rel-val (- fixup loc-sap (/ n-word-bytes 2))))
89            (declare (type (unsigned-byte 64) loc-sap)
90                     (type (signed-byte 32) rel-val))
91            (setf (signed-sap-ref-32 sap offset) rel-val))))))
92     nil)
93 \f
94 ;;;; low-level signal context access functions
95 ;;;;
96 ;;;; Note: In CMU CL, similar functions were hardwired to access
97 ;;;; BSD-style sigcontext structures defined as alien objects. Our
98 ;;;; approach is different in two ways:
99 ;;;;   1. We use POSIX SA_SIGACTION-style signals, so our context is
100 ;;;;      whatever the void pointer in the sigaction handler dereferences
101 ;;;;      to, not necessarily a sigcontext.
102 ;;;;   2. We don't try to maintain alien definitions of the context
103 ;;;;      structure at Lisp level, but instead call alien C functions
104 ;;;;      which take care of access for us. (Since the C functions can
105 ;;;;      be defined in terms of system standard header files, they
106 ;;;;      should be easier to maintain; and since Lisp code uses signal
107 ;;;;      contexts only in interactive or exception code (like the debugger
108 ;;;;      and internal error handling) the extra runtime cost should be
109 ;;;;      negligible.
110
111 (declaim (inline context-pc-addr))
112 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
113   ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
114   ;; 'unsigned *' interpretation for the 32-bit word passed to us by
115   ;; the C code, even though the C code may think it's an 'int *'.)
116   (context (* os-context-t)))
117
118 (declaim (inline context-pc))
119 (defun context-pc (context)
120   (declare (type (alien (* os-context-t)) context))
121   (let ((addr (context-pc-addr context)))
122     (declare (type (alien (* unsigned-long)) addr))
123     (int-sap (deref addr))))
124
125 (declaim (inline context-register-addr))
126 (define-alien-routine ("os_context_register_addr" context-register-addr)
127   (* unsigned-long)
128   ;; (Note the mismatch here between the 'int *' value that the C code
129   ;; may think it's giving us and the 'unsigned *' value that we
130   ;; receive. It's intentional: the C header files may think of
131   ;; register values as signed, but the CMU CL code tends to think of
132   ;; register values as unsigned, and might get bewildered if we ask
133   ;; it to work with signed values.)
134   (context (* os-context-t))
135   (index int))
136
137 (declaim (inline context-register))
138 (defun context-register (context index)
139   (declare (type (alien (* os-context-t)) context))
140   (let ((addr (context-register-addr context index)))
141     (declare (type (alien (* unsigned-long)) addr))
142     (deref addr)))
143
144 (defun %set-context-register (context index new)
145   (declare (type (alien (* os-context-t)) context))
146   (let ((addr (context-register-addr context index)))
147     (declare (type (alien (* unsigned-long)) addr))
148     (setf (deref addr) new)))
149
150 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
151 ;;; register. FORMAT is the type of float to return.
152 ;;;
153 ;;; As of sbcl-0.6.7, there is no working code which calls this code,
154 ;;; so it's stubbed out. Someday, in order to make the debugger work
155 ;;; better, it may be necessary to unstubify it.
156 (defun context-float-register (context index format)
157   (declare (ignore context index))
158   (warn "stub CONTEXT-FLOAT-REGISTER")
159   (coerce 0.0 format))
160 (defun %set-context-float-register (context index format new-value)
161   (declare (ignore context index))
162   (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
163   (coerce new-value format))
164
165 ;;; Given a signal context, return the floating point modes word in
166 ;;; the same format as returned by FLOATING-POINT-MODES.
167 #!-linux
168 (defun context-floating-point-modes (context)
169   (declare (ignore context)) ; stub!
170   (warn "stub CONTEXT-FLOATING-POINT-MODES")
171   0)
172 #!+linux
173 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
174     (sb!alien:unsigned 32)
175   (context (* os-context-t)))
176
177 (define-alien-routine
178     ("arch_get_fp_modes" floating-point-modes) (sb!alien:unsigned 32))
179
180 (define-alien-routine
181     ("arch_set_fp_modes" %floating-point-modes-setter) void (fp (sb!alien:unsigned 32)))
182
183 (defun (setf floating-point-modes) (val) (%floating-point-modes-setter val))
184
185 \f
186 ;;;; INTERNAL-ERROR-ARGS
187
188 ;;; Given a (POSIX) signal context, extract the internal error
189 ;;; arguments from the instruction stream.
190 (defun internal-error-args (context)
191   (declare (type (alien (* os-context-t)) context))
192   (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
193   (/hexstr context)
194   (let ((pc (context-pc context)))
195     (declare (type system-area-pointer pc))
196     (/show0 "got PC")
197     ;; using INT3 the pc is .. INT3 <here> code length bytes...
198     (let* ((length (sap-ref-8 pc 1))
199            (vector (make-array length :element-type '(unsigned-byte 8))))
200       (declare (type (unsigned-byte 8) length)
201                (type (simple-array (unsigned-byte 8) (*)) vector))
202       (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
203       (/hexstr length)
204       (/hexstr vector)
205       (copy-ub8-from-system-area pc 2 vector 0 length)
206       (let* ((index 0)
207              (error-number (sb!c:read-var-integer vector index)))
208         (/hexstr error-number)
209         (collect ((sc-offsets))
210           (loop
211            (/show0 "INDEX=..")
212            (/hexstr index)
213            (when (>= index length)
214              (return))
215            (let ((sc-offset (sb!c:read-var-integer vector index)))
216              (/show0 "SC-OFFSET=..")
217              (/hexstr sc-offset)
218              (sc-offsets sc-offset)))
219           (values error-number (sc-offsets)))))))
220 \f
221
222 ;;; the current alien stack pointer; saved/restored for non-local exits
223 (defvar *alien-stack*)