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