bf5d64804d820b0756a62cbffe0b6b09d2284c0a
[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 7667 2007-04-05 00:16:05Z michi $
26
27 */
28
29
30 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
31
32 #include "config.h"
33
34 #include <ucontext.h>
35
36 #include "vm/types.h"
37
38 #if defined(ENABLE_GC_CACAO)
39 # include "mm/cacao-gc/gc.h"
40 #endif
41
42 #include "vm/jit/i386/codegen.h"
43
44 #include "threads/threads-common.h"
45
46 #include "vm/exceptions.h"
47 #include "vm/signallocal.h"
48 #include "vm/stringlocal.h"
49
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/stacktrace.h"
52
53
54 /* md_signal_handler_sigsegv ***************************************************
55
56    Signal handler for hardware exceptions.
57
58 *******************************************************************************/
59
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
61 {
62         ucontext_t        *_uc;
63         mcontext_t        *_mc;
64         u1                *pv;
65         u1                *sp;
66         u1                *ra;
67         u1                *xpc;
68         u1                 opc;
69         u1                 mod;
70         u1                 rm;
71         s4                 d;
72         s4                 disp;
73         ptrint             val;
74         s4                 type;
75         java_objectheader *o;
76
77         _uc = (ucontext_t *) _p;
78         _mc = &_uc->uc_mcontext;
79
80         pv  = NULL;                 /* is resolved during stackframeinfo creation */
81         sp  = (u1 *) _mc->gregs[REG_ESP];
82         xpc = (u1 *) _mc->gregs[REG_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                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
104                    registers are reversed starting with number 4 for REG_EDI
105                    (see /usr/include/sys/ucontext.h).  We have to convert that
106                    here. */
107
108                 val = _mc->gregs[REG_EAX - d];
109         }
110         else {
111                 /* this was a normal NPE */
112
113                 type = EXCEPTION_HARDWARE_NULLPOINTER;
114         }
115
116         /* generate appropriate exception */
117
118         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
119
120         /* set registers */
121
122         _mc->gregs[REG_EAX] = (ptrint) o;
123         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
124         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
125 }
126
127
128 /* md_signal_handler_sigfpe ****************************************************
129
130    ArithmeticException signal handler for hardware divide by zero
131    check.
132
133 *******************************************************************************/
134
135 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
136 {
137         ucontext_t        *_uc;
138         mcontext_t        *_mc;
139         u1                *pv;
140         u1                *sp;
141         u1                *ra;
142         u1                *xpc;
143         s4                 type;
144         ptrint             val;
145         java_objectheader *o;
146
147         _uc = (ucontext_t *) _p;
148         _mc = &_uc->uc_mcontext;
149
150         pv  = NULL;                 /* is resolved during stackframeinfo creation */
151         sp  = (u1 *) _mc->gregs[REG_ESP];
152         xpc = (u1 *) _mc->gregs[REG_EIP];
153         ra  = xpc;                          /* return address is equal to xpc     */
154
155         /* this is an ArithmeticException */
156
157         type = EXCEPTION_HARDWARE_ARITHMETIC;
158         val  = 0;
159
160         /* generate appropriate exception */
161
162         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
163
164         _mc->gregs[REG_EAX] = (ptrint) o;
165         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
166         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
167 }
168
169
170 /* md_signal_handler_sigusr1 ***************************************************
171
172    Signal handler for suspending threads.
173
174 *******************************************************************************/
175
176 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
177 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
178 {
179         ucontext_t *_uc;
180         mcontext_t *_mc;
181         u1         *pc;
182         u1         *sp;
183
184         _uc = (ucontext_t *) _p;
185         _mc = &_uc->uc_mcontext;
186
187         /* assume there is a GC pending */
188         assert(gc_pending);
189
190         /* get the PC and SP for this thread */
191         pc = (u1 *) _mc->gregs[REG_EIP];
192         sp = (u1 *) _mc->gregs[REG_ESP];
193
194         /* now suspend the current thread */
195         threads_suspend_ack(pc, sp);
196 }
197 #endif
198
199
200 /* md_signal_handler_sigusr2 ***************************************************
201
202    Signal handler for profiling sampling.
203
204 *******************************************************************************/
205
206 #if defined(ENABLE_THREADS)
207 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
208 {
209         threadobject *t;
210         ucontext_t   *_uc;
211         mcontext_t   *_mc;
212         u1           *pc;
213
214         t = THREADOBJECT;
215
216         _uc = (ucontext_t *) _p;
217         _mc = &_uc->uc_mcontext;
218
219         pc = (u1 *) _mc->gregs[REG_EIP];
220
221         t->pc = pc;
222 }
223 #endif
224
225
226 #if defined(ENABLE_THREADS)
227 void thread_restartcriticalsection(ucontext_t *uc)
228 {
229         void *critical;
230
231         critical = critical_find_restart_point((void *) uc->uc_mcontext.gregs[REG_EIP]);
232
233         if (critical)
234                 uc->uc_mcontext.gregs[REG_EIP] = (ptrint) critical;
235 }
236 #endif
237
238
239 /*
240  * These are local overrides for various environment variables in Emacs.
241  * Please do not remove this and leave it at the end of the file, where
242  * Emacs will automagically detect them.
243  * ---------------------------------------------------------------------
244  * Local variables:
245  * mode: c
246  * indent-tabs-mode: t
247  * c-basic-offset: 4
248  * tab-width: 4
249  * End:
250  */