* src/vm/jit/i386/darwin/md-os.c: attempt at porting the exception changes
[cacao.git] / src / vm / jit / i386 / darwin / md-os.c
1 /* src/vm/jit/i386/darwin/md-os.c - machine dependent i386 Darwin functions
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: md-os.c 5074 2006-07-04 16:05:35Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <signal.h>
40 #include <ucontext.h>
41
42 #include "vm/types.h"
43
44 #include "vm/jit/i386/md-abi.h"
45
46 #include "vm/exceptions.h"
47 #include "vm/global.h"
48 #include "vm/signallocal.h"
49 #include "vm/stringlocal.h"
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/stacktrace.h"
52
53 #include "vm/jit/i386/codegen.h"
54
55
56 /* md_signal_handler_sigsegv ***************************************************
57
58    Signal handler for hardware exceptions.
59
60 *******************************************************************************/
61
62 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
63 {
64         ucontext_t          *_uc;
65         mcontext_t           _mc;
66         u1                  *pv;
67         i386_thread_state_t *_ss;
68         u1                  *sp;
69         u1                  *ra;
70         u1                  *xpc;
71     u1                   opc;
72     u1                   mod;
73     u1                   rm;
74     s4                   d;
75     s4                   disp;
76     ptrint               val;
77     s4                   type;
78     java_objectheader   *o;
79
80         _uc = (ucontext_t *) _p;
81         _mc = _uc->uc_mcontext;
82         _ss = &_mc->ss;
83
84     pv  = NULL;                 /* is resolved during stackframeinfo creation */
85         sp  = (u1 *) _ss->esp;
86         xpc = (u1 *) _ss->eip;
87     ra  = xpc;                              /* return address is equal to XPC */
88
89     /* get exception-throwing instruction */
90
91     opc = M_ALD_MEM_GET_OPC(xpc);
92     mod = M_ALD_MEM_GET_MOD(xpc);
93     rm  = M_ALD_MEM_GET_RM(xpc);
94
95     /* for values see emit_mov_mem_reg and emit_mem */
96
97     if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
98         /* this was a hardware-exception */
99
100         d    = M_ALD_MEM_GET_REG(xpc);
101         disp = M_ALD_MEM_GET_DISP(xpc);
102
103         /* we use the exception type as load displacement */
104
105         type = disp;
106
107         val = (d == 0) ? _ss->eax :
108             ((d == 1) ? _ss->ecx :
109             ((d == 2) ? _ss->edx :
110             ((d == 3) ? _ss->ebx :
111             ((d == 4) ? _ss->esp :
112             ((d == 5) ? _ss->ebp :
113             ((d == 6) ? _ss->esi : _ss->edi))))));
114     }
115     else {
116         /* this was a normal NPE */
117
118         type = EXCEPTION_HARDWARE_NULLPOINTER;
119     }
120
121     /* generate appropriate exception */
122
123     o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
124
125     /* set registers */
126
127     _ss->eax = (ptrint) o;
128         _ss->ecx = (ptrint) xpc;
129         _ss->eip = (ptrint) asm_handle_exception;
130 }
131
132
133 /* md_signal_handler_sigfpe ****************************************************
134
135    ArithmeticException signal handler for hardware divide by zero
136    check.
137
138 *******************************************************************************/
139
140 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
141 {
142         ucontext_t          *_uc;
143         mcontext_t           _mc;
144     u1                  *pv;
145         i386_thread_state_t *_ss;
146         u1                  *sp;
147         u1                  *ra;
148         u1                  *xpc;
149     s4                   type;
150     ptrint               val;
151     java_objectheader   *o;
152
153
154         _uc = (ucontext_t *) _p;
155         _mc = _uc->uc_mcontext;
156         _ss = &_mc->ss;
157
158     pv  = NULL;                 /* is resolved during stackframeinfo creation */
159         sp  = (u1 *) _ss->esp;
160         xpc = (u1 *) _ss->eip;
161         ra  = xpc;                          /* return address is equal to xpc     */
162
163     /* this is an ArithmeticException */
164
165     type = EXCEPTION_HARDWARE_ARITHMETIC;
166     val  = 0;
167
168     /* generate appropriate exception */
169
170     o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
171
172     _ss->eax = (ptrint) o;
173         _ss->ecx = (ptrint) xpc;
174         _ss->eip = (ptrint) asm_handle_exception;
175 }
176
177
178 /* md_signal_handler_sigusr2 ***************************************************
179
180    Signal handler for profiling sampling.
181
182 *******************************************************************************/
183
184 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
185 {
186         threadobject        *t;
187         ucontext_t          *_uc;
188         mcontext_t           _mc;
189         i386_thread_state_t *_ss;
190         u1                  *pc;
191
192         t = THREADOBJECT;
193
194         _uc = (ucontext_t *) _p;
195         _mc = _uc->uc_mcontext;
196         _ss = &_mc->ss;
197
198         pc = (u1 *) _ss->eip;
199
200         t->pc = pc;
201 }
202
203
204 #if defined(ENABLE_THREADS)
205 void thread_restartcriticalsection(ucontext_t *_uc)
206 {
207         mcontext_t           _mc;
208         i386_thread_state_t *_ss;
209         u1                  *pc;
210         void                *rpc;
211
212         _mc = _uc->uc_mcontext;
213         _ss = &_mc->ss;
214
215         pc = (u1 *) _ss->eip;
216
217         rpc = critical_find_restart_point(pc);
218
219         if (rpc != NULL)
220                 _ss->eip = (ptrint) rpc;
221 }
222 #endif
223
224
225 /*
226  * These are local overrides for various environment variables in Emacs.
227  * Please do not remove this and leave it at the end of the file, where
228  * Emacs will automagically detect them.
229  * ---------------------------------------------------------------------
230  * Local variables:
231  * mode: c
232  * indent-tabs-mode: t
233  * c-basic-offset: 4
234  * tab-width: 4
235  * End:
236  */