b26d04d42ca764736f9895778827d7e3c38ecc8a
[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, 2007 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    $Id: md-os.c 5074 2006-07-04 16:05:35Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <signal.h>
34 #include <stdint.h>
35 #include <ucontext.h>
36
37 #include "vm/types.h"
38
39 #include "vm/jit/i386/codegen.h"
40
41 #include "threads/threads-common.h"
42
43 #include "vm/exceptions.h"
44 #include "vm/global.h"
45 #include "vm/signallocal.h"
46 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49
50 #include "vm/jit/i386/codegen.h"
51
52
53 /* md_signal_handler_sigsegv ***************************************************
54
55    Signal handler for hardware exceptions.
56
57 *******************************************************************************/
58
59 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
60 {
61         stackframeinfo       sfi;
62         ucontext_t          *_uc;
63         mcontext_t           _mc;
64         u1                  *pv;
65         i386_thread_state_t *_ss;
66         u1                  *sp;
67         u1                  *ra;
68         u1                  *xpc;
69     u1                   opc;
70     u1                   mod;
71     u1                   rm;
72     int                  d;
73     int32_t              disp;
74         intptr_t             val;
75     int                  type;
76     void                *p;
77
78         _uc = (ucontext_t *) _p;
79         _mc = _uc->uc_mcontext;
80         _ss = &_mc->ss;
81
82     pv  = NULL;                 /* is resolved during stackframeinfo creation */
83         sp  = (u1 *) _ss->esp;
84         xpc = (u1 *) _ss->eip;
85     ra  = xpc;                              /* return address is equal to XPC */
86
87     /* get exception-throwing instruction */
88
89     opc = M_ALD_MEM_GET_OPC(xpc);
90     mod = M_ALD_MEM_GET_MOD(xpc);
91     rm  = M_ALD_MEM_GET_RM(xpc);
92
93     /* for values see emit_mov_mem_reg and emit_mem */
94
95     if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
96         /* this was a hardware-exception */
97
98         d    = M_ALD_MEM_GET_REG(xpc);
99         disp = M_ALD_MEM_GET_DISP(xpc);
100
101         /* we use the exception type as load displacement */
102
103         type = disp;
104
105         val = (d == 0) ? _ss->eax :
106             ((d == 1) ? _ss->ecx :
107             ((d == 2) ? _ss->edx :
108             ((d == 3) ? _ss->ebx :
109             ((d == 4) ? _ss->esp :
110             ((d == 5) ? _ss->ebp :
111             ((d == 6) ? _ss->esi : _ss->edi))))));
112     }
113     else {
114         /* this was a normal NPE */
115
116         type = EXCEPTION_HARDWARE_NULLPOINTER;
117     }
118
119         /* create stackframeinfo */
120
121         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
122
123         /* Handle the type. */
124
125         p = signal_handle(xpc, type, val);
126
127         /* remove stackframeinfo */
128
129         stacktrace_remove_stackframeinfo(&sfi);
130
131     /* set registers */
132
133     _ss->eax = (intptr_t) p;
134         _ss->ecx = (intptr_t) xpc;
135         _ss->eip = (intptr_t) asm_handle_exception;
136 }
137
138
139 /* md_signal_handler_sigfpe ****************************************************
140
141    ArithmeticException signal handler for hardware divide by zero
142    check.
143
144 *******************************************************************************/
145
146 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
147 {
148         stackframeinfo       sfi;
149         ucontext_t          *_uc;
150         mcontext_t           _mc;
151     u1                  *pv;
152         i386_thread_state_t *_ss;
153         u1                  *sp;
154         u1                  *ra;
155         u1                  *xpc;
156     int                  type;
157     intptr_t             val;
158     void                *p;
159
160
161         _uc = (ucontext_t *) _p;
162         _mc = _uc->uc_mcontext;
163         _ss = &_mc->ss;
164
165     pv  = NULL;                 /* is resolved during stackframeinfo creation */
166         sp  = (u1 *) _ss->esp;
167         xpc = (u1 *) _ss->eip;
168         ra  = xpc;                          /* return address is equal to xpc     */
169
170     /* this is an ArithmeticException */
171
172     type = EXCEPTION_HARDWARE_ARITHMETIC;
173     val  = 0;
174
175         /* create stackframeinfo */
176
177         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
178
179         /* Handle the type. */
180
181         p = signal_handle(xpc, type, val);
182
183         /* remove stackframeinfo */
184
185         stacktrace_remove_stackframeinfo(&sfi);
186
187     _ss->eax = (intptr_t) p;
188         _ss->ecx = (intptr_t) xpc;
189         _ss->eip = (intptr_t) asm_handle_exception;
190 }
191
192
193 /* md_signal_handler_sigusr2 ***************************************************
194
195    Signal handler for profiling sampling.
196
197 *******************************************************************************/
198
199 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
200 {
201         threadobject        *t;
202         ucontext_t          *_uc;
203         mcontext_t           _mc;
204         i386_thread_state_t *_ss;
205         u1                  *pc;
206
207         t = THREADOBJECT;
208
209         _uc = (ucontext_t *) _p;
210         _mc = _uc->uc_mcontext;
211         _ss = &_mc->ss;
212
213         pc = (u1 *) _ss->eip;
214
215         t->pc = pc;
216 }
217
218
219 /* md_critical_section_restart *************************************************
220
221    Search the critical sections tree for a matching section and set
222    the PC to the restart point, if necessary.
223
224 *******************************************************************************/
225
226 #if defined(ENABLE_THREADS)
227 void thread_restartcriticalsection(ucontext_t *_uc)
228 {
229         mcontext_t           _mc;
230         i386_thread_state_t *_ss;
231         u1                  *pc;
232         void                *rpc;
233
234         _mc = _uc->uc_mcontext;
235         _ss = &_mc->ss;
236
237         pc = (u1 *) _ss->eip;
238
239         rpc = critical_find_restart_point(pc);
240
241         if (rpc != NULL)
242                 _ss->eip = (ptrint) rpc;
243 }
244 #endif
245
246
247 /*
248  * These are local overrides for various environment variables in Emacs.
249  * Please do not remove this and leave it at the end of the file, where
250  * Emacs will automagically detect them.
251  * ---------------------------------------------------------------------
252  * Local variables:
253  * mode: c
254  * indent-tabs-mode: t
255  * c-basic-offset: 4
256  * tab-width: 4
257  * End:
258  */