* Removed all Id tags.
[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 */
26
27
28 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
29
30 #include "config.h"
31
32 #include <stdint.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/i386/codegen.h"
38
39 #include "threads/threads-common.h"
40
41 #include "vm/exceptions.h"
42 #include "vm/signallocal.h"
43 #include "vm/stringlocal.h"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/stacktrace.h"
47
48
49 /* md_signal_handler_sigsegv ***************************************************
50
51    Signal handler for hardware exceptions.
52
53 *******************************************************************************/
54
55 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
56 {
57         stackframeinfo  sfi;
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         void           *p;
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                 val  = 0;
111         }
112
113         /* create stackframeinfo */
114
115         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
116
117         /* Handle the type. */
118
119         p = signal_handle(xpc, type, val);
120
121         /* remove stackframeinfo */
122
123         stacktrace_remove_stackframeinfo(&sfi);
124
125         /* set registers */
126
127         _mc->gregs[REG_EAX] = (intptr_t) p;
128         _mc->gregs[REG_ECX] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
129         _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
130 }
131
132
133 /* md_signal_handler_sigfpe ****************************************************
134
135    ArithmeticException signal handler for hardware divide by zero
136    check.
137
138 *******************************************************************************/
139
140 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
141 {
142         stackframeinfo  sfi;
143         ucontext_t     *_uc;
144         mcontext_t     *_mc;
145         u1             *pv;
146         u1             *sp;
147         u1             *ra;
148         u1             *xpc;
149         s4              type;
150         ptrint          val;
151         void           *p;
152
153         _uc = (ucontext_t *) _p;
154         _mc = &_uc->uc_mcontext;
155
156         pv  = NULL;                 /* is resolved during stackframeinfo creation */
157         sp  = (u1 *) _mc->gregs[REG_ESP];
158         xpc = (u1 *) _mc->gregs[REG_EIP];
159         ra  = xpc;                          /* return address is equal to xpc     */
160
161         /* this is an ArithmeticException */
162
163         type = EXCEPTION_HARDWARE_ARITHMETIC;
164         val  = 0;
165
166         /* create stackframeinfo */
167
168         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
169
170         /* Handle the type. */
171
172         p = signal_handle(xpc, type, val);
173
174         /* remove stackframeinfo */
175
176         stacktrace_remove_stackframeinfo(&sfi);
177
178         _mc->gregs[REG_EAX] = (intptr_t) p;
179         _mc->gregs[REG_ECX] = (intptr_t) xpc;                    /* REG_ITMP2_XPC */
180         _mc->gregs[REG_EIP] = (intptr_t) asm_handle_exception;
181 }
182
183
184 /* md_signal_handler_sigusr2 ***************************************************
185
186    Signal handler for profiling sampling.
187
188 *******************************************************************************/
189
190 #if defined(ENABLE_THREADS)
191 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
192 {
193         threadobject *t;
194         ucontext_t   *_uc;
195         mcontext_t   *_mc;
196         u1           *pc;
197
198         t = THREADOBJECT;
199
200         _uc = (ucontext_t *) _p;
201         _mc = &_uc->uc_mcontext;
202
203         pc = (u1 *) _mc->gregs[REG_EIP];
204
205         t->pc = pc;
206 }
207 #endif
208
209
210 /* md_critical_section_restart *************************************************
211
212    Search the critical sections tree for a matching section and set
213    the PC to the restart point, if necessary.
214
215 *******************************************************************************/
216
217 #if defined(ENABLE_THREADS)
218 void md_critical_section_restart(ucontext_t *_uc)
219 {
220         mcontext_t *_mc;
221         u1         *pc;
222         u1         *npc;
223
224         _mc = &_uc->uc_mcontext;
225
226         pc = (u1 *) _mc->gregs[REG_EIP];
227
228         npc = critical_find_restart_point(pc);
229
230         if (npc != NULL)
231                 _mc->gregs[REG_EIP] = (ptrint) npc;
232 }
233 #endif
234
235
236 /*
237  * These are local overrides for various environment variables in Emacs.
238  * Please do not remove this and leave it at the end of the file, where
239  * Emacs will automagically detect them.
240  * ---------------------------------------------------------------------
241  * Local variables:
242  * mode: c
243  * indent-tabs-mode: t
244  * c-basic-offset: 4
245  * tab-width: 4
246  * End:
247  */