* configure.ac (AC_CONFIG_FILES): Added
[cacao.git] / src / vm / jit / x86_64 / linux / md-os.c
1 /* src/vm/jit/x86_64/linux/md-os.c - machine dependent x86_64 Linux functions
2
3    Copyright (C) 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.c 7249 2007-01-29 19:32:52Z twisti $
26
27 */
28
29
30 #define _GNU_SOURCE
31
32 #include "config.h"
33
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <ucontext.h>
37
38 #if defined(ENABLE_THREADS)
39 # include "threads/native/threads.h"
40 #endif
41
42 #include "vm/signallocal.h"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/stacktrace.h"
46
47
48 /* md_signal_handler_sigsegv ***************************************************
49
50    NullPointerException signal handler for hardware null pointer
51    check.
52
53 *******************************************************************************/
54
55 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
56 {
57         ucontext_t *_uc;
58         mcontext_t *_mc;
59         u1         *sp;
60         u1         *ra;
61         u1         *xpc;
62
63         _uc = (ucontext_t *) _p;
64         _mc = &_uc->uc_mcontext;
65
66         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
67            different to the ones in <ucontext.h>. */
68
69         sp  = (u1 *) _mc->gregs[REG_RSP];
70         xpc = (u1 *) _mc->gregs[REG_RIP];
71         ra  = xpc;                          /* return address is equal to xpc     */
72
73 #if 0
74         /* check for StackOverflowException */
75
76         threads_check_stackoverflow(sp);
77 #endif
78
79         _mc->gregs[REG_RAX] =
80                 (ptrint) stacktrace_hardware_nullpointerexception(NULL, sp, ra, xpc);
81
82         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
83         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
84 }
85
86
87 /* md_signal_handler_sigfpe ****************************************************
88
89    ArithmeticException signal handler for hardware divide by zero
90    check.
91
92 *******************************************************************************/
93
94 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
95 {
96         ucontext_t  *_uc;
97         mcontext_t  *_mc;
98         u1          *sp;
99         u1          *ra;
100         u1          *xpc;
101
102         _uc = (ucontext_t *) _p;
103         _mc = &_uc->uc_mcontext;
104
105         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
106            different to the ones in <ucontext.h>. */
107
108         sp  = (u1 *) _mc->gregs[REG_RSP];
109         xpc = (u1 *) _mc->gregs[REG_RIP];
110         ra  = xpc;                          /* return address is equal to xpc     */
111
112         _mc->gregs[REG_RAX] =
113                 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
114
115         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
116         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
117 }
118
119
120 /* md_signal_handler_sigusr2 ***************************************************
121
122    Signal handler for profiling sampling.
123
124 *******************************************************************************/
125
126 #if defined(ENABLE_THREADS)
127 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
128 {
129         threadobject *t;
130         ucontext_t   *_uc;
131         mcontext_t   *_mc;
132         u1           *pc;
133
134         t = THREADOBJECT;
135
136         _uc = (ucontext_t *) _p;
137         _mc = &_uc->uc_mcontext;
138
139         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
140            different to the ones in <ucontext.h>. */
141
142         pc = (u1 *) _mc->gregs[REG_RIP];
143
144         t->pc = pc;
145 }
146 #endif
147
148
149 #if defined(ENABLE_THREADS)
150 void thread_restartcriticalsection(ucontext_t *_uc)
151 {
152         mcontext_t *_mc;
153         u1         *pc;
154         void       *critical;
155
156         _mc = &_uc->uc_mcontext;
157
158         pc = _mc->gregs[REG_RIP];
159
160         critical = critical_find_restart_point();
161
162         if (critical != NULL)
163                 _mc->gregs[REG_RIP] = (ptrint) critical;
164 }
165 #endif
166
167
168 /*
169  * These are local overrides for various environment variables in Emacs.
170  * Please do not remove this and leave it at the end of the file, where
171  * Emacs will automagically detect them.
172  * ---------------------------------------------------------------------
173  * Local variables:
174  * mode: c
175  * indent-tabs-mode: t
176  * c-basic-offset: 4
177  * tab-width: 4
178  * End:
179  * vim:noexpandtab:sw=4:ts=4:
180  */