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