* Merged in twisti-branch.
[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 7571 2007-03-24 22:37:09Z twisti $
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/exceptions.h"
39 #include "vm/signallocal.h"
40 #include "vm/stringlocal.h"
41
42 #include "vm/jit/asmpart.h"
43 #include "vm/jit/stacktrace.h"
44
45 #include "vm/jit/i386/codegen.h"
46
47
48 /* md_signal_handler_sigsegv ***************************************************
49
50    Signal handler for hardware exceptions.
51
52 *******************************************************************************/
53
54 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
55 {
56         ucontext_t        *_uc;
57         mcontext_t        *_mc;
58         u1                *pv;
59         u1                *sp;
60         u1                *ra;
61         u1                *xpc;
62         u1                 opc;
63         u1                 mod;
64         u1                 rm;
65         s4                 d;
66         s4                 disp;
67         ptrint             val;
68         s4                 type;
69         java_objectheader *e;
70
71         _uc = (ucontext_t *) _p;
72         _mc = &_uc->uc_mcontext;
73
74         pv  = NULL;                 /* is resolved during stackframeinfo creation */
75         sp  = (u1 *) _mc->gregs[REG_ESP];
76         xpc = (u1 *) _mc->gregs[REG_EIP];
77         ra  = xpc;                              /* return address is equal to XPC */
78
79         /* get exception-throwing instruction */
80
81         opc = M_ALD_MEM_GET_OPC(xpc);
82         mod = M_ALD_MEM_GET_MOD(xpc);
83         rm  = M_ALD_MEM_GET_RM(xpc);
84
85         /* for values see emit_mov_mem_reg and emit_mem */
86
87         if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
88                 /* this was a hardware-exception */
89
90                 d    = M_ALD_MEM_GET_REG(xpc);
91                 disp = M_ALD_MEM_GET_DISP(xpc);
92
93                 /* we use the exception type as load displacement */
94
95                 type = disp;
96
97                 /* ATTENTION: The _mc->gregs layout is completely crazy!  The
98                    registers are reversed starting with number 4 for REG_EDI
99                    (see /usr/include/sys/ucontext.h).  We have to convert that
100                    here. */
101
102                 val = _mc->gregs[REG_EAX - d];
103         }
104         else {
105                 /* this was a normal NPE */
106
107                 type = EXCEPTION_HARDWARE_NULLPOINTER;
108         }
109
110         /* generate appropriate exception */
111
112         e = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
113
114         /* set registers */
115
116         _mc->gregs[REG_EAX] = (ptrint) e;
117         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
118         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
119 }
120
121
122 /* md_signal_handler_sigfpe ****************************************************
123
124    ArithmeticException signal handler for hardware divide by zero
125    check.
126
127 *******************************************************************************/
128
129 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
130 {
131         ucontext_t *_uc;
132         mcontext_t *_mc;
133         u1         *sp;
134         u1         *ra;
135         u1         *xpc;
136
137         _uc = (ucontext_t *) _p;
138         _mc = &_uc->uc_mcontext;
139
140         sp  = (u1 *) _mc->gregs[REG_ESP];
141         xpc = (u1 *) _mc->gregs[REG_EIP];
142         ra  = xpc;                          /* return address is equal to xpc     */
143
144         _mc->gregs[REG_EAX] =
145                 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
146
147         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
148         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
149 }
150
151
152 /* md_signal_handler_sigusr2 ***************************************************
153
154    Signal handler for profiling sampling.
155
156 *******************************************************************************/
157
158 #if defined(ENABLE_THREADS)
159 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
160 {
161         threadobject *t;
162         ucontext_t   *_uc;
163         mcontext_t   *_mc;
164         u1           *pc;
165
166         t = THREADOBJECT;
167
168         _uc = (ucontext_t *) _p;
169         _mc = &_uc->uc_mcontext;
170
171         pc = (u1 *) _mc->gregs[REG_EIP];
172
173         t->pc = pc;
174 }
175 #endif
176
177
178 #if defined(ENABLE_THREADS)
179 void thread_restartcriticalsection(ucontext_t *uc)
180 {
181         void *critical;
182
183         critical = critical_find_restart_point((void *) uc->uc_mcontext.gregs[REG_EIP]);
184
185         if (critical)
186                 uc->uc_mcontext.gregs[REG_EIP] = (ptrint) critical;
187 }
188 #endif
189
190
191 /*
192  * These are local overrides for various environment variables in Emacs.
193  * Please do not remove this and leave it at the end of the file, where
194  * Emacs will automagically detect them.
195  * ---------------------------------------------------------------------
196  * Local variables:
197  * mode: c
198  * indent-tabs-mode: t
199  * c-basic-offset: 4
200  * tab-width: 4
201  * End:
202  */