* src/mm/cacao-gc/gc.h (gc_suspend): Added prototype.
[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 7480 2007-03-08 12:46:19Z 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         executionstate_t *es;
126         ucontext_t       *_uc;
127         mcontext_t       *_mc;
128
129         t = THREADOBJECT;
130         t->es = NEW(executionstate_t); /* TODO: this must be done before!!! */
131         es = t->es;
132
133         _uc = (ucontext_t *) _p;
134         _mc = &_uc->uc_mcontext;
135
136         /* assume there is a GC pending */
137         assert(gc_pending);
138
139         /* fill in the execution state of this thread */
140         es->pc = (u1 *) _mc->gregs[REG_EIP];
141         es->sp = (u1 *) _mc->gregs[REG_ESP];
142         es->pv = (u1 *) NULL;
143
144         /* TODO: int registers are missing */
145
146         /* now suspend the current thread for GC */
147         gc_suspend(t, _uc);
148 }
149 #endif
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  */