* src/vm/jit/powerpc/linux/md-os.c, src/vm/jit/powerpc/darwin/md-os.c,
[cacao.git] / src / vm / jit / x86_64 / md.c
1 /* src/vm/jit/x86_64/md.c - machine dependent x86_64 Linux functions
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: md.c 4326 2006-01-20 13:25:24Z twisti $
32
33 */
34
35
36 #define _GNU_SOURCE
37
38 #include "config.h"
39
40 #include <stdlib.h>
41 #include <ucontext.h>
42
43 #include "vm/jit/x86_64/md-abi.h"
44
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49
50
51 /* md_init *********************************************************************
52
53    Do some machine dependent initialization.
54
55 *******************************************************************************/
56
57 void md_init(void)
58 {
59         /* nothing to do */
60 }
61
62
63 /* md_signal_handler_sigsegv ***************************************************
64
65    NullPointerException signal handler for hardware null pointer
66    check.
67
68 *******************************************************************************/
69
70 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
71 {
72         ucontext_t  *_uc;
73         mcontext_t  *_mc;
74         u1          *sp;
75         u1          *ra;
76         u1          *xpc;
77
78         _uc = (ucontext_t *) _p;
79         _mc = &_uc->uc_mcontext;
80
81         /* ATTENTION: don't use CACAO internal REG_* defines as they are          */
82         /* different to the ones in <ucontext.h>                                  */
83
84         sp  = (u1 *) _mc->gregs[REG_RSP];
85         xpc = (u1 *) _mc->gregs[REG_RIP];
86         ra  = xpc;                          /* return address is equal to xpc     */
87
88         _mc->gregs[REG_RAX] =
89                 (ptrint) stacktrace_hardware_nullpointerexception(NULL, sp, ra, xpc);
90
91         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
92         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
93 }
94
95
96 /* md_signal_handler_sigfpe ****************************************************
97
98    ArithmeticException signal handler for hardware divide by zero
99    check.
100
101 *******************************************************************************/
102
103 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
104 {
105         ucontext_t  *_uc;
106         mcontext_t  *_mc;
107         u1          *sp;
108         u1          *ra;
109         u1          *xpc;
110
111         _uc = (ucontext_t *) _p;
112         _mc = &_uc->uc_mcontext;
113
114         /* ATTENTION: don't use CACAO internal REG_* defines as they are          */
115         /* different to the ones in <ucontext.h>                                  */
116
117         sp  = (u1 *) _mc->gregs[REG_RSP];
118         xpc = (u1 *) _mc->gregs[REG_RIP];
119         ra  = xpc;                          /* return address is equal to xpc     */
120
121         _mc->gregs[REG_RAX] =
122                 (ptrint) stacktrace_hardware_arithmeticexception(NULL, sp, ra, xpc);
123
124         _mc->gregs[REG_R10] = (ptrint) xpc;                      /* REG_ITMP2_XPC */
125         _mc->gregs[REG_RIP] = (ptrint) asm_handle_exception;
126 }
127
128
129 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
130 void thread_restartcriticalsection(ucontext_t *uc)
131 {
132         void *critical;
133
134         critical = thread_checkcritical((void *) uc->uc_mcontext.gregs[REG_RIP]);
135
136         if (critical)
137                 uc->uc_mcontext.gregs[REG_RIP] = (ptrint) critical;
138 }
139 #endif
140
141
142 /* md_stacktrace_get_returnaddress *********************************************
143
144    Returns the return address of the current stackframe, specified by
145    the passed stack pointer and the stack frame size.
146
147 *******************************************************************************/
148
149 u1 *md_stacktrace_get_returnaddress(u1 *sp, u4 framesize)
150 {
151         u1 *ra;
152
153         /* on x86_64 the return address is above the current stack frame */
154
155         ra = *((u1 **) (sp + framesize));
156
157         return ra;
158 }
159
160
161 /* md_codegen_findmethod *******************************************************
162
163    On this architecture just a wrapper function to codegen_findmethod.
164
165 *******************************************************************************/
166
167 u1 *md_codegen_findmethod(u1 *ra)
168 {
169         u1 *pv;
170
171         /* the the start address of the function which contains this
172        address from the method table */
173
174         pv = codegen_findmethod(ra);
175
176         return pv;
177 }
178
179
180 /*
181  * These are local overrides for various environment variables in Emacs.
182  * Please do not remove this and leave it at the end of the file, where
183  * Emacs will automagically detect them.
184  * ---------------------------------------------------------------------
185  * Local variables:
186  * mode: c
187  * indent-tabs-mode: t
188  * c-basic-offset: 4
189  * tab-width: 4
190  * End:
191  */