* src/vm/jit/i386/linux/md-os.c (md_signal_handler_sigusr1): This function is no
[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 7925 2007-05-21 00:06:33Z 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 #include "vm/jit/i386/codegen.h"
39
40 #include "threads/threads-common.h"
41
42 #include "vm/exceptions.h"
43 #include "vm/signallocal.h"
44 #include "vm/stringlocal.h"
45
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/stacktrace.h"
48
49
50 /* md_signal_handler_sigsegv ***************************************************
51
52    Signal handler for hardware exceptions.
53
54 *******************************************************************************/
55
56 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
57 {
58         ucontext_t        *_uc;
59         mcontext_t        *_mc;
60         u1                *pv;
61         u1                *sp;
62         u1                *ra;
63         u1                *xpc;
64         u1                 opc;
65         u1                 mod;
66         u1                 rm;
67         s4                 d;
68         s4                 disp;
69         ptrint             val;
70         s4                 type;
71         java_objectheader *o;
72
73         _uc = (ucontext_t *) _p;
74         _mc = &_uc->uc_mcontext;
75
76         pv  = NULL;                 /* is resolved during stackframeinfo creation */
77         sp  = (u1 *) _mc->gregs[REG_ESP];
78         xpc = (u1 *) _mc->gregs[REG_EIP];
79         ra  = xpc;                              /* return address is equal to XPC */
80
81         /* get exception-throwing instruction */
82
83         opc = M_ALD_MEM_GET_OPC(xpc);
84         mod = M_ALD_MEM_GET_MOD(xpc);
85         rm  = M_ALD_MEM_GET_RM(xpc);
86
87         /* for values see emit_mov_mem_reg and emit_mem */
88
89         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
90                 /* this was a hardware-exception */
91
92                 d    = M_ALD_MEM_GET_REG(xpc);
93                 disp = M_ALD_MEM_GET_DISP(xpc);
94
95                 /* we use the exception type as load displacement */
96
97                 type = disp;
98
99                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
100                    registers are reversed starting with number 4 for REG_EDI
101                    (see /usr/include/sys/ucontext.h).  We have to convert that
102                    here. */
103
104                 val = _mc->gregs[REG_EAX - d];
105         }
106         else {
107                 /* this was a normal NPE */
108
109                 type = EXCEPTION_HARDWARE_NULLPOINTER;
110         }
111
112         /* generate appropriate exception */
113
114         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
115
116         /* set registers */
117
118         _mc->gregs[REG_EAX] = (ptrint) o;
119         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
120         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
121 }
122
123
124 /* md_signal_handler_sigfpe ****************************************************
125
126    ArithmeticException signal handler for hardware divide by zero
127    check.
128
129 *******************************************************************************/
130
131 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
132 {
133         ucontext_t        *_uc;
134         mcontext_t        *_mc;
135         u1                *pv;
136         u1                *sp;
137         u1                *ra;
138         u1                *xpc;
139         s4                 type;
140         ptrint             val;
141         java_objectheader *o;
142
143         _uc = (ucontext_t *) _p;
144         _mc = &_uc->uc_mcontext;
145
146         pv  = NULL;                 /* is resolved during stackframeinfo creation */
147         sp  = (u1 *) _mc->gregs[REG_ESP];
148         xpc = (u1 *) _mc->gregs[REG_EIP];
149         ra  = xpc;                          /* return address is equal to xpc     */
150
151         /* this is an ArithmeticException */
152
153         type = EXCEPTION_HARDWARE_ARITHMETIC;
154         val  = 0;
155
156         /* generate appropriate exception */
157
158         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
159
160         _mc->gregs[REG_EAX] = (ptrint) o;
161         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
162         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
163 }
164
165
166 /* md_signal_handler_sigusr1 ***************************************************
167
168    Signal handler for suspending threads.
169
170 *******************************************************************************/
171
172 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
173 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
174 {
175         ucontext_t *_uc;
176         mcontext_t *_mc;
177         u1         *pc;
178         u1         *sp;
179
180         _uc = (ucontext_t *) _p;
181         _mc = &_uc->uc_mcontext;
182
183         /* get the PC and SP for this thread */
184         pc = (u1 *) _mc->gregs[REG_EIP];
185         sp = (u1 *) _mc->gregs[REG_ESP];
186
187         /* now suspend the current thread */
188         threads_suspend_ack(pc, sp);
189 }
190 #endif
191
192
193 /* md_signal_handler_sigusr2 ***************************************************
194
195    Signal handler for profiling sampling.
196
197 *******************************************************************************/
198
199 #if defined(ENABLE_THREADS)
200 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
201 {
202         threadobject *t;
203         ucontext_t   *_uc;
204         mcontext_t   *_mc;
205         u1           *pc;
206
207         t = THREADOBJECT;
208
209         _uc = (ucontext_t *) _p;
210         _mc = &_uc->uc_mcontext;
211
212         pc = (u1 *) _mc->gregs[REG_EIP];
213
214         t->pc = pc;
215 }
216 #endif
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 md_critical_section_restart(ucontext_t *_uc)
228 {
229         mcontext_t    *_mc;
230         u1            *pc;
231         void          *npc;
232
233         _mc = &_uc->uc_mcontext;
234
235         pc = (u1 *) _mc->gregs[REG_EIP];
236
237         npc = critical_find_restart_point(pc);
238
239         if (npc != NULL) {
240                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, npc);
241                 _mc->gregs[REG_EIP] = (ptrint) npc;
242         }
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  */