1.0.2.11:
[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 (truly-the system-area-pointer
46                          (%primitive sb!kernel::code-instructions code))))
47      (ecase kind
48        (:b
49         (error "Can't deal with CALL fixups, yet."))
50        (:ba
51         (setf (ldb (byte 24 2) (sap-ref-32 sap offset))
52               (ash fixup -2)))
53        (:ha
54         (let* ((h (ldb (byte 16 16) fixup))
55                (l (ldb (byte 16 0) fixup)))
56           ; Compensate for possible sign-extension when the low half
57           ; is added to the high.  We could avoid this by ORI-ing
58           ; the low half in 32-bit absolute loads, but it'd be
59           ; nice to be able to do:
60           ;  lis rX,foo@ha
61           ;  lwz rY,foo@l(rX)
62           ; and lwz/stw and friends all use a signed 16-bit offset.
63           (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
64                  (if (logbitp 15 l) (ldb (byte 16 0) (1+ h)) h))))
65        (:l
66         (setf (ldb (byte 16 0) (sap-ref-32 sap offset))
67               (ldb (byte 16 0) fixup)))))))
68
69
70 ;;;; "Sigcontext" access functions, cut & pasted from x86-vm.lisp then
71 ;;;; hacked for types.
72
73 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-long)
74   (context (* os-context-t)))
75
76 (defun context-pc (context)
77   (declare (type (alien (* os-context-t)) context))
78   (int-sap (deref (context-pc-addr context))))
79
80 (define-alien-routine ("os_context_register_addr" context-register-addr)
81   (* unsigned-long)
82   (context (* os-context-t))
83   (index int))
84
85 (defun context-register (context index)
86   (declare (type (alien (* os-context-t)) context))
87   (deref (context-register-addr context index)))
88
89 (define-alien-routine ("os_context_lr_addr" context-lr-addr) (* unsigned-long)
90   (context (* os-context-t)))
91
92 (defun context-lr (context)
93   (declare (type (alien (* os-context-t)) context))
94   (int-sap (deref (context-lr-addr context))))
95
96 (defun %set-context-register (context index new)
97 (declare (type (alien (* os-context-t)) context))
98 (setf (deref (context-register-addr context index))
99       new))
100 ;;; This is like CONTEXT-REGISTER, but returns the value of a float
101 ;;; register. FORMAT is the type of float to return.
102
103 ;;; FIXME: Whether COERCE actually knows how to make a float out of a
104 ;;; long is another question. This stuff still needs testing.
105 #+nil
106 (define-alien-routine ("os_context_fpregister_addr" context-float-register-addr)
107   (* long)
108   (context (* os-context-t))
109   (index int))
110 #+nil
111 (defun context-float-register (context index format)
112   (declare (type (alien (* os-context-t)) context))
113   (coerce (deref (context-float-register-addr context index)) format))
114 #+nil
115 (defun %set-context-float-register (context index format new)
116   (declare (type (alien (* os-context-t)) context))
117   (setf (deref (context-float-register-addr context index))
118         (coerce new format)))
119
120 ;;; Given a signal context, return the floating point modes word in
121 ;;; the same format as returned by FLOATING-POINT-MODES.
122 ;;;
123 ;;; FIXME: surely this must be accessible somewhere under Darwin?  Or
124 ;;; under NetBSD?
125 #!+linux
126 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
127     (sb!alien:unsigned 32)
128   (context (* os-context-t)))
129
130 \f
131 ;;;; INTERNAL-ERROR-ARGS.
132
133 ;;; GIVEN a (POSIX) signal context, extract the internal error
134 ;;; arguments from the instruction stream.  This is e.g.
135
136 ;;; INTERNAL-ERROR-ARGS -- interface.
137 ;;;
138 ;;; Given the sigcontext, extract the internal error arguments from the
139 ;;; instruction stream.
140 ;;;
141 (defun internal-error-args (context)
142   (declare (type (alien (* os-context-t)) context))
143   (let* ((pc (context-pc context))
144          (bad-inst (sap-ref-32 pc 0))
145          (op (ldb (byte 16 16) bad-inst)))
146     (declare (type system-area-pointer pc))
147     (cond ((= op (logior (ash 3 10) (ash 6 5)))
148            (args-for-unimp-inst context))
149           ((and (= (ldb (byte 6 10) op) 3)
150                 (= (ldb (byte 5 5) op) 24))
151            (let* ((regnum (ldb (byte 5 0) op))
152                   (prev (sap-ref-32 (int-sap (- (sap-int pc) 4)) 0)))
153              (if (and (= (ldb (byte 6 26) prev) 3)
154                       (= (ldb (byte 5 21) prev) 0))
155                  (values (ldb (byte 16 0) prev)
156                          (list (sb!c::make-sc-offset sb!vm:any-reg-sc-number
157                                                      (ldb (byte 5 16) prev))))
158                  (values #.(sb!kernel:error-number-or-lose
159                             'sb!kernel:invalid-arg-count-error)
160                          (list (sb!c::make-sc-offset sb!vm:any-reg-sc-number regnum))))))
161
162           (t
163            (values #.(error-number-or-lose 'unknown-error) nil)))))
164
165 (defun args-for-unimp-inst (context)
166   (declare (type (alien (* os-context-t)) context))
167   (let* ((pc (context-pc context))
168          (length (sap-ref-8 pc 4))
169          (vector (make-array length :element-type '(unsigned-byte 8))))
170     (declare (type system-area-pointer pc)
171              (type (unsigned-byte 8) length)
172              (type (simple-array (unsigned-byte 8) (*)) vector))
173     (copy-ub8-from-system-area pc 5 vector 0 length)
174     (let* ((index 0)
175            (error-number (sb!c:read-var-integer vector index)))
176       (collect ((sc-offsets))
177                (loop
178                 (when (>= index length)
179                   (return))
180                 (sc-offsets (sb!c:read-var-integer vector index)))
181                (values error-number (sc-offsets))))))
182