Proper x86_64 mnemonics
[cacao.git] / src / vm / jit / powerpc64 / linux / md-os.c
1 /* src/vm/jit/powerpc64/linux/md-os.c - machine dependent PowerPC64 Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <ucontext.h>
31
32 #include "vm/types.h"
33
34 #include "vm/jit/powerpc64/codegen.h"
35 #include "vm/jit/powerpc64/md.h"
36 #include "vm/jit/powerpc64/linux/md-abi.h"
37
38 #include "threads/thread.h"
39
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/signallocal.h"
43
44 #include "vm/jit/asmpart.h"
45
46 #if defined(ENABLE_PROFILING)
47 # include "vm/jit/optimizing/profile.h"
48 #endif
49
50 #include "vm/jit/stacktrace.h"
51 #include "vm/jit/trap.h"
52
53
54 /* md_signal_handler_sigsegv ***************************************************
55  
56         Signal handler for hardware-exceptions.
57
58 *******************************************************************************/
59
60 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
61 {
62         ucontext_t     *_uc;
63         mcontext_t     *_mc;
64         u1             *pv;
65         u1             *sp;
66         u1             *ra;
67         u1             *xpc;
68         u4              mcode;
69         int             s1;
70         int16_t         disp;
71         int             d;
72         int             type;
73         intptr_t        addr;
74         intptr_t        val;
75         void           *p;
76
77         _uc = (ucontext_t *) _p;
78         _mc = &(_uc->uc_mcontext);
79
80         /* get register values */
81
82         pv = (u1*) _mc->gp_regs[REG_PV];
83         sp = (u1*) _mc->gp_regs[REG_SP];
84         ra = (u1*) _mc->gp_regs[PT_LNK];                     /* correct for leafs */
85         xpc =(u1*) _mc->gp_regs[PT_NIP];
86
87         /* get the throwing instruction */
88
89         mcode = *((u4*)xpc);
90
91         s1   = M_INSTR_OP2_IMM_A(mcode);
92         disp = M_INSTR_OP2_IMM_I(mcode);
93         d    = M_INSTR_OP2_IMM_D(mcode);
94
95         val  = _mc->gp_regs[d];
96
97         if (s1 == REG_ZERO) {
98                 /* We use the exception type as load displacement. */
99                 type = disp;
100
101                 if (type == TRAP_COMPILER) {
102                         /* The XPC is the RA minus 1, because the RA points to the
103                            instruction after the call. */
104
105                         xpc = ra - 4;
106                 }
107         }
108         else {
109                 /* Normal NPE. */
110                 addr = _mc->gp_regs[s1];
111                 type = (int) addr;
112         }
113
114         /* Handle the trap. */
115
116         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
117
118         /* Set registers. */
119
120         switch (type) {
121         case TRAP_COMPILER:
122                 if (p != NULL) {
123                         _mc->gp_regs[REG_PV] = (uintptr_t) p;
124                         _mc->gp_regs[PT_NIP] = (uintptr_t) p;
125                         break;
126                 }
127
128                 /* Get and set the PV from the parent Java method. */
129
130                 pv = md_codegen_get_pv_from_pc(ra);
131
132                 _mc->gp_regs[REG_PV] = (uintptr_t) pv;
133
134                 /* Get the exception object. */
135
136                 p = builtin_retrieve_exception();
137
138                 assert(p != NULL);
139
140                 /* fall-through */
141
142         case TRAP_PATCHER:
143                 if (p == NULL)
144                         break;
145
146                 /* fall-through */
147                 
148         default:
149                 _mc->gp_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
150                 _mc->gp_regs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
151                 _mc->gp_regs[PT_NIP]         = (uintptr_t) asm_handle_exception;
152         }
153 }
154
155
156 /* md_signal_handler_sigusr2 ***************************************************
157
158    Signal handler for profiling sampling.
159
160 *******************************************************************************/
161
162 #if defined(ENABLE_THREADS)
163 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
164 {
165         threadobject *tobj;
166         ucontext_t   *_uc;
167         mcontext_t   *_mc;
168         u1           *pc;
169
170         tobj = THREADOBJECT;
171
172         _uc = (ucontext_t *) _p;
173         _mc = &(_uc->uc_mcontext);
174
175         pc = (u1 *) _mc->gp_regs[PT_NIP];
176
177         tobj->pc = pc;
178 }
179 #endif
180
181
182 /* md_critical_section_restart *************************************************
183
184    Search the critical sections tree for a matching section and set
185    the PC to the restart point, if necessary.
186
187 *******************************************************************************/
188
189 #if defined(ENABLE_THREADS)
190 void md_critical_section_restart(ucontext_t *_uc)
191 {
192         mcontext_t *_mc;
193         u1         *pc;
194         u1         *npc;
195
196         _mc = &(_uc->uc_mcontext);
197
198         pc = (u1 *) _mc->gp_regs[PT_NIP];
199
200         npc = critical_find_restart_point(pc);
201
202         if (npc != NULL)
203                 _mc->gp_regs[PT_NIP] = (ptrint) npc;
204 }
205 #endif
206
207
208 /*
209  * These are local overrides for various environment variables in Emacs.
210  * Please do not remove this and leave it at the end of the file, where
211  * Emacs will automagically detect them.
212  * ---------------------------------------------------------------------
213  * Local variables:
214  * mode: c
215  * indent-tabs-mode: t
216  * c-basic-offset: 4
217  * tab-width: 4
218  * End:
219  */