a5b8d5559b19a61c0b95030eb946b652fad39ce9
[cacao.git] / src / vm / jit / i386 / linux / md-os.c
1 /* src/vm/jit/i386/linux/md-os.c - machine dependent i386 Linux 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 8283 2007-08-09 15:10:05Z twisti $
26
27 */
28
29
30 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
31
32 #include "config.h"
33
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/signallocal.h"
45 #include "vm/stringlocal.h"
46
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.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         u1             *sp;
64         u1             *ra;
65         u1             *xpc;
66         u1              opc;
67         u1              mod;
68         u1              rm;
69         s4              d;
70         s4              disp;
71         ptrint          val;
72         s4              type;
73         void           *p;
74
75         _uc = (ucontext_t *) _p;
76         _mc = &_uc->uc_mcontext;
77
78         pv  = NULL;                 /* is resolved during stackframeinfo creation */
79         sp  = (u1 *) _mc->gregs[REG_ESP];
80         xpc = (u1 *) _mc->gregs[REG_EIP];
81         ra  = xpc;                              /* return address is equal to XPC */
82
83         /* get exception-throwing instruction */
84
85         opc = M_ALD_MEM_GET_OPC(xpc);
86         mod = M_ALD_MEM_GET_MOD(xpc);
87         rm  = M_ALD_MEM_GET_RM(xpc);
88
89         /* for values see emit_mov_mem_reg and emit_mem */
90
91         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
92                 /* this was a hardware-exception */
93
94                 d    = M_ALD_MEM_GET_REG(xpc);
95                 disp = M_ALD_MEM_GET_DISP(xpc);
96
97                 /* we use the exception type as load displacement */
98
99                 type = disp;
100
101                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
102                    registers are reversed starting with number 4 for REG_EDI
103                    (see /usr/include/sys/ucontext.h).  We have to convert that
104                    here. */
105
106                 val = _mc->gregs[REG_EAX - d];
107         }
108         else {
109                 /* this was a normal NPE */
110
111                 type = EXCEPTION_HARDWARE_NULLPOINTER;
112                 val  = 0;
113         }
114
115         /* create stackframeinfo */
116
117         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
118
119         /* Handle the type. */
120
121         p = signal_handle(xpc, type, val);
122
123         /* remove stackframeinfo */
124
125         stacktrace_remove_stackframeinfo(&sfi);
126
127         /* set registers */
128
129         _mc->gregs[REG_EAX] = (intptr_t) p;
130         _mc->gregs[REG_ECX] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
131         _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
132 }
133
134
135 /* md_signal_handler_sigfpe ****************************************************
136
137    ArithmeticException signal handler for hardware divide by zero
138    check.
139
140 *******************************************************************************/
141
142 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
143 {
144         stackframeinfo  sfi;
145         ucontext_t     *_uc;
146         mcontext_t     *_mc;
147         u1             *pv;
148         u1             *sp;
149         u1             *ra;
150         u1             *xpc;
151         s4              type;
152         ptrint          val;
153         void           *p;
154
155         _uc = (ucontext_t *) _p;
156         _mc = &_uc->uc_mcontext;
157
158         pv  = NULL;                 /* is resolved during stackframeinfo creation */
159         sp  = (u1 *) _mc->gregs[REG_ESP];
160         xpc = (u1 *) _mc->gregs[REG_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         /* create stackframeinfo */
169
170         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
171
172         /* Handle the type. */
173
174         p = signal_handle(xpc, type, val);
175
176         /* remove stackframeinfo */
177
178         stacktrace_remove_stackframeinfo(&sfi);
179
180         _mc->gregs[REG_EAX] = (intptr_t) p;
181         _mc->gregs[REG_ECX] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
182         _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
183 }
184
185
186 /* md_signal_handler_sigusr2 ***************************************************
187
188    Signal handler for profiling sampling.
189
190 *******************************************************************************/
191
192 #if defined(ENABLE_THREADS)
193 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
194 {
195         threadobject *t;
196         ucontext_t   *_uc;
197         mcontext_t   *_mc;
198         u1           *pc;
199
200         t = THREADOBJECT;
201
202         _uc = (ucontext_t *) _p;
203         _mc = &_uc->uc_mcontext;
204
205         pc = (u1 *) _mc->gregs[REG_EIP];
206
207         t->pc = pc;
208 }
209 #endif
210
211
212 /* md_critical_section_restart *************************************************
213
214    Search the critical sections tree for a matching section and set
215    the PC to the restart point, if necessary.
216
217 *******************************************************************************/
218
219 #if defined(ENABLE_THREADS)
220 void md_critical_section_restart(ucontext_t *_uc)
221 {
222         mcontext_t *_mc;
223         u1         *pc;
224         u1         *npc;
225
226         _mc = &_uc->uc_mcontext;
227
228         pc = (u1 *) _mc->gregs[REG_EIP];
229
230         npc = critical_find_restart_point(pc);
231
232         if (npc != NULL)
233                 _mc->gregs[REG_EIP] = (ptrint) npc;
234 }
235 #endif
236
237
238 /*
239  * These are local overrides for various environment variables in Emacs.
240  * Please do not remove this and leave it at the end of the file, where
241  * Emacs will automagically detect them.
242  * ---------------------------------------------------------------------
243  * Local variables:
244  * mode: c
245  * indent-tabs-mode: t
246  * c-basic-offset: 4
247  * tab-width: 4
248  * End:
249  */