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