1.0.19.1: DERIVE-TYPE optimizer for %%PRIMITIVE
[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 and MACHINE-VERSION
9
10 (defun machine-type ()
11   "Returns a string describing the type of the local machine."
12   "PowerPC")
13
14 ;;; support for CL:MACHINE-VERSION defined OAOO elsewhere
15 (defun get-machine-version ()
16   #!+linux
17   (with-open-file (stream "/proc/cpuinfo"
18                           ;; /proc is optional even in Linux, so
19                           ;; fail gracefully.
20                           :if-does-not-exist nil)
21     (loop with line while (setf line (read-line stream nil))
22           ;; hoping "cpu" exists and gives something useful in
23           ;; all relevant Linuxen...
24           ;;
25           ;; from Lars Brinkhoff sbcl-devel 26 Jun 2003:
26           ;;   I examined different versions of Linux/PPC at
27           ;;   http://lxr.linux.no/ (the file that outputs
28           ;;   /proc/cpuinfo is arch/ppc/kernel/setup.c, if
29           ;;   you want to check), and all except 2.0.x
30           ;;   seemed to do the same thing as far as the
31           ;;   "cpu" field is concerned, i.e. it always
32           ;;   starts with the (C-syntax) string "cpu\t\t: ".
33           when (eql (search "cpu" line) 0)
34           return (string-trim " " (subseq line (1+ (position #\: line))))))
35   #!-linux
36   nil)
37 \f
38 ;;;; FIXUP-CODE-OBJECT
39
40 (defun fixup-code-object (code offset fixup kind)
41   (declare (type index offset))
42   (unless (zerop (rem offset n-word-bytes))
43     (error "Unaligned instruction?  offset=#x~X." offset))
44   (sb!sys:without-gcing
45    (let ((sap (%primitive sb!kernel::code-instructions code)))
46      (ecase kind
47        (:b
48         (error "Can't deal with CALL fixups, yet."))
49        (:ba
50         (setf (ldb (byte 24 2) (sap-ref-32 sap offset))
51               (ash fixup -2)))
52        (:ha
53         (let* ((h (ldb (byte 16 16) fixup))
54                (l (ldb (byte 16 0) fixup)))
55           ; Compensate for possible sign-extension when the low half
56           ; is added to the high.  We could avoid this by ORI-ing
57           ; the low half in 32-bit absolute loads, but it'd be
58           ; nice to be able to do:
59           ;  lis rX,foo@ha
60           ;  lwz rY,foo@l(rX)
61           ; and lwz/stw and friends all use a signed 16-bit offset.
62           (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
63                  (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
64        (:l
65         (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
66               (ldb (byte 16 0) fixup)))))))
67
68
69 ;;;; "Sigcontext" access functions, cut & pasted from x86-vm.lisp then
70 ;;;; hacked for types.
71
72 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
73   (context (* os-context-t)))
74
75 (defun context-pc (context)
76   (declare (type (alien (* os-context-t)) context))
77   (int-sap (deref (context-pc-addr context))))
78
79 (define-alien-routine ("os_context_register_addr" context-register-addr)
80   (* unsigned-long)
81   (context (* os-context-t))
82   (index int))
83
84 (defun context-register (context index)
85   (declare (type (alien (* os-context-t)) context))
86   (deref (context-register-addr context index)))
87
88 (define-alien-routine ("os_context_lr_addr" context-lr-addr) (* unsigned-long)
89   (context (* os-context-t)))
90
91 (defun context-lr (context)
92   (declare (type (alien (* os-context-t)) context))
93   (int-sap (deref (context-lr-addr context))))
94
95 (defun %set-context-register (context index new)
96 (declare (type (alien (* os-context-t)) context))
97 (setf (deref (context-register-addr context index))
98       new))
99 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
100 ;;; register. FORMAT is the type of float to return.
101
102 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
103 ;;; long is another question. This stuff still needs testing.
104 #+nil
105 (define-alien-routine ("os_context_fpregister_addr" context-float-register-addr)
106   (* long)
107   (context (* os-context-t))
108   (index int))
109 #+nil
110 (defun context-float-register (context index format)
111   (declare (type (alien (* os-context-t)) context))
112   (coerce (deref (context-float-register-addr context index)) format))
113 #+nil
114 (defun %set-context-float-register (context index format new)
115   (declare (type (alien (* os-context-t)) context))
116   (setf (deref (context-float-register-addr context index))
117         (coerce new format)))
118
119 ;;; Given a signal context, return the floating point modes word in
120 ;;; the same format as returned by FLOATING-POINT-MODES.
121 ;;;
122 ;;; FIXME: surely this must be accessible somewhere under Darwin?  Or
123 ;;; under NetBSD?
124 #!+linux
125 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
126     (sb!alien:unsigned 32)
127   (context (* os-context-t)))
128
129 \f
130 ;;;; INTERNAL-ERROR-ARGS.
131
132 ;;; GIVEN a (POSIX) signal context, extract the internal error
133 ;;; arguments from the instruction stream.  This is e.g.
134
135 ;;; INTERNAL-ERROR-ARGS -- interface.
136 ;;;
137 ;;; Given the sigcontext, extract the internal error arguments from the
138 ;;; instruction stream.
139 ;;;
140 (defun internal-error-args (context)
141   (declare (type (alien (* os-context-t)) context))
142   (let* ((pc (context-pc context))
143          (bad-inst (sap-ref-32 pc 0))
144          (op (ldb (byte 16 16) bad-inst)))
145     (declare (type system-area-pointer pc))
146     (cond ((= op (logior (ash 3 10) (ash 6 5)))
147            (args-for-unimp-inst context))
148           ((and (= (ldb (byte 6 10) op) 3)
149                 (= (ldb (byte 5 5) op) 24))
150            (let* ((regnum (ldb (byte 5 0) op))
151                   (prev (sap-ref-32 (int-sap (- (sap-int pc) 4)) 0)))
152              (if (and (= (ldb (byte 6 26) prev) 3)
153                       (= (ldb (byte 5 21) prev) 0))
154                  (values (ldb (byte 16 0) prev)
155                          (list (sb!c::make-sc-offset sb!vm:any-reg-sc-number
156                                                      (ldb (byte 5 16) prev))))
157                  (values #.(sb!kernel:error-number-or-lose
158                             'sb!kernel:invalid-arg-count-error)
159                          (list (sb!c::make-sc-offset sb!vm:any-reg-sc-number regnum))))))
160
161           (t
162            (values #.(error-number-or-lose 'unknown-error) nil)))))
163
164 (defun args-for-unimp-inst (context)
165   (declare (type (alien (* os-context-t)) context))
166   (let* ((pc (context-pc context))
167          (length (sap-ref-8 pc 4))
168          (vector (make-array length :element-type '(unsigned-byte 8))))
169     (declare (type system-area-pointer pc)
170              (type (unsigned-byte 8) length)
171              (type (simple-array (unsigned-byte 8) (*)) vector))
172     (copy-ub8-from-system-area pc 5 vector 0 length)
173     (let* ((index 0)
174            (error-number (sb!c:read-var-integer vector index)))
175       (collect ((sc-offsets))
176                (loop
177                 (when (>= index length)
178                   (return))
179                 (sc-offsets (sb!c:read-var-integer vector index)))
180                (values error-number (sc-offsets))))))
181