* src/vm/jit/i386/linux/md-os.c: Cleaned up.
[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 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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: md-os.c 7482 2007-03-08 13:16:34Z michi $
32
33 */
34
35
36 #define _GNU_SOURCE                   /* include REG_ defines from ucontext.h */
37
38 #include "config.h"
39
40 #include <ucontext.h>
41
42 #include "vm/types.h"
43
44 #if defined(ENABLE_GC_CACAO)
45 # include "mm/cacao-gc/gc.h"
46 #endif
47
48 #include "vm/exceptions.h"
49 #include "vm/signallocal.h"
50 #include "vm/stringlocal.h"
51 #include "vm/jit/asmpart.h"
52 #include "vm/jit/stacktrace.h"
53
54
55 /* md_signal_handler_sigsegv ***************************************************
56
57    NullPointerException signal handler for hardware null pointer
58    check.
59
60 *******************************************************************************/
61
62 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
63 {
64         ucontext_t *_uc;
65         mcontext_t *_mc;
66         u1         *sp;
67         u1         *ra;
68         u1         *xpc;
69
70         _uc = (ucontext_t *) _p;
71         _mc = &_uc->uc_mcontext;
72
73         sp  = (u1 *) _mc->gregs[REG_ESP];
74         xpc = (u1 *) _mc->gregs[REG_EIP];
75         ra  = xpc;                          /* return address is equal to xpc     */
76
77         _mc->gregs[REG_EAX] =
78                 (ptrint) stacktrace_hardware_nullpointerexception(NULL, sp, ra, xpc);
79
80         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
81         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
82 }
83
84
85 /* md_signal_handler_sigfpe ****************************************************
86
87    ArithmeticException signal handler for hardware divide by zero
88    check.
89
90 *******************************************************************************/
91
92 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
93 {
94         ucontext_t *_uc;
95         mcontext_t *_mc;
96         u1         *sp;
97         u1         *ra;
98         u1         *xpc;
99
100         _uc = (ucontext_t *) _p;
101         _mc = &_uc->uc_mcontext;
102
103         sp  = (u1 *) _mc->gregs[REG_ESP];
104         xpc = (u1 *) _mc->gregs[REG_EIP];
105         ra  = xpc;                          /* return address is equal to xpc     */
106
107         _mc->gregs[REG_EAX] =
108                 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
109
110         _mc->gregs[REG_ECX] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
111         _mc->gregs[REG_EIP] = (ptrint) asm_handle_exception;
112 }
113
114
115 /* md_signal_handler_sigusr1 ***************************************************
116
117    Signale handler the exact GC to suspend threads.
118
119 *******************************************************************************/
120
121 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
122 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
123 {
124         threadobject     *t;
125         ucontext_t       *_uc;
126         mcontext_t       *_mc;
127
128         t = THREADOBJECT;
129
130         _uc = (ucontext_t *) _p;
131         _mc = &_uc->uc_mcontext;
132
133         /* assume there is a GC pending */
134         assert(gc_pending);
135
136         /* fill in the PC for this thread */
137         t->pc = (u1 *) _mc->gregs[REG_EIP];
138
139         /* now suspend the current thread for GC */
140         gc_suspend(t, _uc);
141 }
142 #endif
143
144
145 /* md_signal_handler_sigusr2 ***************************************************
146
147    Signal handler for profiling sampling.
148
149 *******************************************************************************/
150
151 #if defined(ENABLE_THREADS)
152 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
153 {
154         threadobject *t;
155         ucontext_t   *_uc;
156         mcontext_t   *_mc;
157         u1           *pc;
158
159         t = THREADOBJECT;
160
161         _uc = (ucontext_t *) _p;
162         _mc = &_uc->uc_mcontext;
163
164         pc = (u1 *) _mc->gregs[REG_EIP];
165
166         t->pc = pc;
167 }
168 #endif
169
170
171 #if defined(ENABLE_THREADS)
172 void thread_restartcriticalsection(ucontext_t *uc)
173 {
174         void *critical;
175
176         critical = critical_find_restart_point((void *) uc->uc_mcontext.gregs[REG_EIP]);
177
178         if (critical)
179                 uc->uc_mcontext.gregs[REG_EIP] = (ptrint) critical;
180 }
181 #endif
182
183
184 /*
185  * These are local overrides for various environment variables in Emacs.
186  * Please do not remove this and leave it at the end of the file, where
187  * Emacs will automagically detect them.
188  * ---------------------------------------------------------------------
189  * Local variables:
190  * mode: c
191  * indent-tabs-mode: t
192  * c-basic-offset: 4
193  * tab-width: 4
194  * End:
195  */