d04a842bc3d10282084513fa4648047351b22267
[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/codegen.h"
45
46 #include "threads/threads-common.h"
47
48 #include "vm/exceptions.h"
49 #include "vm/global.h"
50 #include "vm/signallocal.h"
51 #include "vm/stringlocal.h"
52 #include "vm/jit/asmpart.h"
53 #include "vm/jit/stacktrace.h"
54
55 #include "vm/jit/i386/codegen.h"
56
57
58 /* md_signal_handler_sigsegv ***************************************************
59
60    Signal handler for hardware exceptions.
61
62 *******************************************************************************/
63
64 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
65 {
66         stackframeinfo       sfi;
67         ucontext_t          *_uc;
68         mcontext_t           _mc;
69         u1                  *pv;
70         i386_thread_state_t *_ss;
71         u1                  *sp;
72         u1                  *ra;
73         u1                  *xpc;
74     u1                   opc;
75     u1                   mod;
76     u1                   rm;
77     s4                   d;
78     s4                   disp;
79     ptrint               val;
80     s4                   type;
81     java_objectheader   *e;
82
83         _uc = (ucontext_t *) _p;
84         _mc = _uc->uc_mcontext;
85         _ss = &_mc->ss;
86
87     pv  = NULL;                 /* is resolved during stackframeinfo creation */
88         sp  = (u1 *) _ss->esp;
89         xpc = (u1 *) _ss->eip;
90     ra  = xpc;                              /* return address is equal to XPC */
91
92     /* get exception-throwing instruction */
93
94     opc = M_ALD_MEM_GET_OPC(xpc);
95     mod = M_ALD_MEM_GET_MOD(xpc);
96     rm  = M_ALD_MEM_GET_RM(xpc);
97
98     /* for values see emit_mov_mem_reg and emit_mem */
99
100     if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
101         /* this was a hardware-exception */
102
103         d    = M_ALD_MEM_GET_REG(xpc);
104         disp = M_ALD_MEM_GET_DISP(xpc);
105
106         /* we use the exception type as load displacement */
107
108         type = disp;
109
110         val = (d == 0) ? _ss->eax :
111             ((d == 1) ? _ss->ecx :
112             ((d == 2) ? _ss->edx :
113             ((d == 3) ? _ss->ebx :
114             ((d == 4) ? _ss->esp :
115             ((d == 5) ? _ss->ebp :
116             ((d == 6) ? _ss->esi : _ss->edi))))));
117     }
118     else {
119         /* this was a normal NPE */
120
121         type = EXCEPTION_HARDWARE_NULLPOINTER;
122     }
123
124         /* create stackframeinfo */
125
126         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
127
128         /* generate appropriate exception */
129
130         e = exceptions_new_hardware_exception(xpc, type, val);
131
132         /* remove stackframeinfo */
133
134         stacktrace_remove_stackframeinfo(&sfi);
135
136     /* set registers */
137
138     _ss->eax = (ptrint) e;
139         _ss->ecx = (ptrint) xpc;
140         _ss->eip = (ptrint) asm_handle_exception;
141 }
142
143
144 /* md_signal_handler_sigfpe ****************************************************
145
146    ArithmeticException signal handler for hardware divide by zero
147    check.
148
149 *******************************************************************************/
150
151 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
152 {
153         stackframeinfo       sfi;
154         ucontext_t          *_uc;
155         mcontext_t           _mc;
156     u1                  *pv;
157         i386_thread_state_t *_ss;
158         u1                  *sp;
159         u1                  *ra;
160         u1                  *xpc;
161     s4                   type;
162     ptrint               val;
163     java_objectheader   *e;
164
165
166         _uc = (ucontext_t *) _p;
167         _mc = _uc->uc_mcontext;
168         _ss = &_mc->ss;
169
170     pv  = NULL;                 /* is resolved during stackframeinfo creation */
171         sp  = (u1 *) _ss->esp;
172         xpc = (u1 *) _ss->eip;
173         ra  = xpc;                          /* return address is equal to xpc     */
174
175     /* this is an ArithmeticException */
176
177     type = EXCEPTION_HARDWARE_ARITHMETIC;
178     val  = 0;
179
180         /* create stackframeinfo */
181
182         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
183
184         /* generate appropriate exception */
185
186         e = exceptions_new_hardware_exception(xpc, type, val);
187
188         /* remove stackframeinfo */
189
190         stacktrace_remove_stackframeinfo(&sfi);
191
192     _ss->eax = (ptrint) e;
193         _ss->ecx = (ptrint) xpc;
194         _ss->eip = (ptrint) asm_handle_exception;
195 }
196
197
198 /* md_signal_handler_sigusr2 ***************************************************
199
200    Signal handler for profiling sampling.
201
202 *******************************************************************************/
203
204 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
205 {
206         threadobject        *t;
207         ucontext_t          *_uc;
208         mcontext_t           _mc;
209         i386_thread_state_t *_ss;
210         u1                  *pc;
211
212         t = THREADOBJECT;
213
214         _uc = (ucontext_t *) _p;
215         _mc = _uc->uc_mcontext;
216         _ss = &_mc->ss;
217
218         pc = (u1 *) _ss->eip;
219
220         t->pc = pc;
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  */