Merged revisions 8137-8178 via svnmerge from
[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 8179 2007-07-05 11:21:08Z 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         stackframeinfo     sfi;
59         ucontext_t        *_uc;
60         mcontext_t        *_mc;
61         u1                *pv;
62         u1                *sp;
63         u1                *ra;
64         u1                *xpc;
65         u1                 opc;
66         u1                 mod;
67         u1                 rm;
68         s4                 d;
69         s4                 disp;
70         ptrint             val;
71         s4                 type;
72         java_objectheader *o;
73
74         _uc = (ucontext_t *) _p;
75         _mc = &_uc->uc_mcontext;
76
77         pv  = NULL;                 /* is resolved during stackframeinfo creation */
78         sp  = (u1 *) _mc->gregs[REG_ESP];
79         xpc = (u1 *) _mc->gregs[REG_EIP];
80         ra  = xpc;                              /* return address is equal to XPC */
81
82         /* get exception-throwing instruction */
83
84         opc = M_ALD_MEM_GET_OPC(xpc);
85         mod = M_ALD_MEM_GET_MOD(xpc);
86         rm  = M_ALD_MEM_GET_RM(xpc);
87
88         /* for values see emit_mov_mem_reg and emit_mem */
89
90         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
91                 /* this was a hardware-exception */
92
93                 d    = M_ALD_MEM_GET_REG(xpc);
94                 disp = M_ALD_MEM_GET_DISP(xpc);
95
96                 /* we use the exception type as load displacement */
97
98                 type = disp;
99
100                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
101                    registers are reversed starting with number 4 for REG_EDI
102                    (see /usr/include/sys/ucontext.h).  We have to convert that
103                    here. */
104
105                 val = _mc->gregs[REG_EAX - d];
106         }
107         else {
108                 /* this was a normal NPE */
109
110                 type = EXCEPTION_HARDWARE_NULLPOINTER;
111                 val  = 0;
112         }
113
114         /* generate appropriate exception */
115
116         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val, &sfi);
117
118         /* set registers */
119
120         _mc->gregs[REG_EAX] = (ptrint) o;
121         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
122         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
123 }
124
125
126 /* md_signal_handler_sigfpe ****************************************************
127
128    ArithmeticException signal handler for hardware divide by zero
129    check.
130
131 *******************************************************************************/
132
133 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
134 {
135         stackframeinfo     sfi;
136         ucontext_t        *_uc;
137         mcontext_t        *_mc;
138         u1                *pv;
139         u1                *sp;
140         u1                *ra;
141         u1                *xpc;
142         s4                 type;
143         ptrint             val;
144         java_objectheader *o;
145
146         _uc = (ucontext_t *) _p;
147         _mc = &_uc->uc_mcontext;
148
149         pv  = NULL;                 /* is resolved during stackframeinfo creation */
150         sp  = (u1 *) _mc->gregs[REG_ESP];
151         xpc = (u1 *) _mc->gregs[REG_EIP];
152         ra  = xpc;                          /* return address is equal to xpc     */
153
154         /* this is an ArithmeticException */
155
156         type = EXCEPTION_HARDWARE_ARITHMETIC;
157         val  = 0;
158
159         /* generate appropriate exception */
160
161         o = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val, &sfi);
162
163         _mc->gregs[REG_EAX] = (ptrint) o;
164         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
165         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
166 }
167
168
169 /* md_signal_handler_sigusr1 ***************************************************
170
171    Signal handler for suspending threads.
172
173 *******************************************************************************/
174
175 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
176 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
177 {
178         ucontext_t *_uc;
179         mcontext_t *_mc;
180         u1         *pc;
181         u1         *sp;
182
183         _uc = (ucontext_t *) _p;
184         _mc = &_uc->uc_mcontext;
185
186         /* get the PC and SP for this thread */
187         pc = (u1 *) _mc->gregs[REG_EIP];
188         sp = (u1 *) _mc->gregs[REG_ESP];
189
190         /* now suspend the current thread */
191         threads_suspend_ack(pc, sp);
192 }
193 #endif
194
195
196 /* md_signal_handler_sigusr2 ***************************************************
197
198    Signal handler for profiling sampling.
199
200 *******************************************************************************/
201
202 #if defined(ENABLE_THREADS)
203 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
204 {
205         threadobject *t;
206         ucontext_t   *_uc;
207         mcontext_t   *_mc;
208         u1           *pc;
209
210         t = THREADOBJECT;
211
212         _uc = (ucontext_t *) _p;
213         _mc = &_uc->uc_mcontext;
214
215         pc = (u1 *) _mc->gregs[REG_EIP];
216
217         t->pc = pc;
218 }
219 #endif
220
221
222 /* md_critical_section_restart *************************************************
223
224    Search the critical sections tree for a matching section and set
225    the PC to the restart point, if necessary.
226
227 *******************************************************************************/
228
229 #if defined(ENABLE_THREADS)
230 void md_critical_section_restart(ucontext_t *_uc)
231 {
232         mcontext_t *_mc;
233         u1         *pc;
234         u1         *npc;
235
236         _mc = &_uc->uc_mcontext;
237
238         pc = (u1 *) _mc->gregs[REG_EIP];
239
240         npc = critical_find_restart_point(pc);
241
242         if (npc != NULL)
243                 _mc->gregs[REG_EIP] = (ptrint) npc;
244 }
245 #endif
246
247
248 /*
249  * These are local overrides for various environment variables in Emacs.
250  * Please do not remove this and leave it at the end of the file, where
251  * Emacs will automagically detect them.
252  * ---------------------------------------------------------------------
253  * Local variables:
254  * mode: c
255  * indent-tabs-mode: t
256  * c-basic-offset: 4
257  * tab-width: 4
258  * End:
259  */