1.0.28.44: better MACHINE-VERSION answers on BSD'ish platforms
[sbcl.git] / src / code / ppc-vm.lisp
1 ;;; This file contains the PPC specific runtime stuff.
2 ;;;
3 (in-package "SB!VM")
4
5 (define-alien-type os-context-t (struct os-context-t-struct))
6
7 \f
8 ;;;; MACHINE-TYPE
9
10 (defun machine-type ()
11   "Returns a string describing the type of the local machine."
12   "PowerPC")
13 \f
14 ;;;; FIXUP-CODE-OBJECT
15
16 (defun fixup-code-object (code offset fixup kind)
17   (declare (type index offset))
18   (unless (zerop (rem offset n-word-bytes))
19     (error "Unaligned instruction?  offset=#x~X." offset))
20   (sb!sys:without-gcing
21    (let ((sap (%primitive sb!kernel::code-instructions code)))
22      (ecase kind
23        (:b
24         (error "Can't deal with CALL fixups, yet."))
25        (:ba
26         (setf (ldb (byte 24 2) (sap-ref-32 sap offset))
27               (ash fixup -2)))
28        (:ha
29         (let* ((h (ldb (byte 16 16) fixup))
30                (l (ldb (byte 16 0) fixup)))
31           ; Compensate for possible sign-extension when the low half
32           ; is added to the high.  We could avoid this by ORI-ing
33           ; the low half in 32-bit absolute loads, but it'd be
34           ; nice to be able to do:
35           ;  lis rX,foo@ha
36           ;  lwz rY,foo@l(rX)
37           ; and lwz/stw and friends all use a signed 16-bit offset.
38           (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
39                  (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
40        (:l
41         (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
42               (ldb (byte 16 0) fixup)))))))
43
44
45 ;;;; "Sigcontext" access functions, cut & pasted from x86-vm.lisp then
46 ;;;; hacked for types.
47
48 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
49   (context (* os-context-t)))
50
51 (defun context-pc (context)
52   (declare (type (alien (* os-context-t)) context))
53   (int-sap (deref (context-pc-addr context))))
54
55 (define-alien-routine ("os_context_register_addr" context-register-addr)
56   (* unsigned-long)
57   (context (* os-context-t))
58   (index int))
59
60 (defun context-register (context index)
61   (declare (type (alien (* os-context-t)) context))
62   (deref (context-register-addr context index)))
63
64 (define-alien-routine ("os_context_lr_addr" context-lr-addr) (* unsigned-long)
65   (context (* os-context-t)))
66
67 (defun context-lr (context)
68   (declare (type (alien (* os-context-t)) context))
69   (int-sap (deref (context-lr-addr context))))
70
71 (defun %set-context-register (context index new)
72 (declare (type (alien (* os-context-t)) context))
73 (setf (deref (context-register-addr context index))
74       new))
75 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
76 ;;; register. FORMAT is the type of float to return.
77
78 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
79 ;;; long is another question. This stuff still needs testing.
80 #+nil
81 (define-alien-routine ("os_context_fpregister_addr" context-float-register-addr)
82   (* long)
83   (context (* os-context-t))
84   (index int))
85 #+nil
86 (defun context-float-register (context index format)
87   (declare (type (alien (* os-context-t)) context))
88   (coerce (deref (context-float-register-addr context index)) format))
89 #+nil
90 (defun %set-context-float-register (context index format new)
91   (declare (type (alien (* os-context-t)) context))
92   (setf (deref (context-float-register-addr context index))
93         (coerce new format)))
94
95 ;;; Given a signal context, return the floating point modes word in
96 ;;; the same format as returned by FLOATING-POINT-MODES.
97 ;;;
98 ;;; FIXME: surely this must be accessible somewhere under Darwin?  Or
99 ;;; under NetBSD?
100 #!+linux
101 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
102     (sb!alien:unsigned 32)
103   (context (* os-context-t)))
104
105 \f
106 ;;;; INTERNAL-ERROR-ARGS.
107
108 ;;; GIVEN a (POSIX) signal context, extract the internal error
109 ;;; arguments from the instruction stream.  This is e.g.
110
111 ;;; INTERNAL-ERROR-ARGS -- interface.
112 ;;;
113 ;;; Given the sigcontext, extract the internal error arguments from the
114 ;;; instruction stream.
115 ;;;
116 (defun internal-error-args (context)
117   (declare (type (alien (* os-context-t)) context))
118   (let* ((pc (context-pc context))
119          (bad-inst (sap-ref-32 pc 0))
120          (op (ldb (byte 16 16) bad-inst)))
121     (declare (type system-area-pointer pc))
122     (cond ((= op (logior (ash 3 10) (ash 6 5)))
123            (args-for-unimp-inst context))
124           ((and (= (ldb (byte 6 10) op) 3)
125                 (= (ldb (byte 5 5) op) 24))
126            (let* ((regnum (ldb (byte 5 0) op))
127                   (prev (sap-ref-32 (int-sap (- (sap-int pc) 4)) 0)))
128              (if (and (= (ldb (byte 6 26) prev) 3)
129                       (= (ldb (byte 5 21) prev) 0))
130                  (values (ldb (byte 16 0) prev)
131                          (list (sb!c::make-sc-offset sb!vm:any-reg-sc-number
132                                                      (ldb (byte 5 16) prev))))
133                  (values #.(sb!kernel:error-number-or-lose
134                             'sb!kernel:invalid-arg-count-error)
135                          (list (sb!c::make-sc-offset sb!vm:any-reg-sc-number regnum))))))
136
137           (t
138            (values #.(error-number-or-lose 'unknown-error) nil)))))
139
140 (defun args-for-unimp-inst (context)
141   (declare (type (alien (* os-context-t)) context))
142   (let* ((pc (context-pc context))
143          (length (sap-ref-8 pc 4))
144          (vector (make-array length :element-type '(unsigned-byte 8))))
145     (declare (type system-area-pointer pc)
146              (type (unsigned-byte 8) length)
147              (type (simple-array (unsigned-byte 8) (*)) vector))
148     (copy-ub8-from-system-area pc 5 vector 0 length)
149     (let* ((index 0)
150            (error-number (sb!c:read-var-integer vector index)))
151       (collect ((sc-offsets))
152                (loop
153                 (when (>= index length)
154                   (return))
155                 (sc-offsets (sb!c:read-var-integer vector index)))
156                (values error-number (sc-offsets))))))
157