* src/vm/exceptions.c: Moved to .cpp.
[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.hpp"
39
40 #include "vm/builtin.h"
41 #include "vm/signallocal.h"
42
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/executionstate.h"
45
46 #if defined(ENABLE_PROFILING)
47 # include "vm/jit/optimizing/profile.h"
48 #endif
49
50 #include "vm/jit/trap.h"
51
52
53 /* md_signal_handler_sigsegv ***************************************************
54  
55         Signal handler for hardware-exceptions.
56
57 *******************************************************************************/
58
59 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
60 {
61         ucontext_t     *_uc;
62         mcontext_t     *_mc;
63         u1             *pv;
64         u1             *sp;
65         u1             *ra;
66         u1             *xpc;
67         u4              mcode;
68         int             s1;
69         int16_t         disp;
70         int             d;
71         int             type;
72         intptr_t        addr;
73         intptr_t        val;
74         void           *p;
75
76         _uc = (ucontext_t *) _p;
77         _mc = &(_uc->uc_mcontext);
78
79         /* get register values */
80
81         pv = (u1*) _mc->gp_regs[REG_PV];
82         sp = (u1*) _mc->gp_regs[REG_SP];
83         ra = (u1*) _mc->gp_regs[PT_LNK];                     /* correct for leafs */
84         xpc =(u1*) _mc->gp_regs[PT_NIP];
85
86         /* get the throwing instruction */
87
88         mcode = *((u4*)xpc);
89
90         s1   = M_INSTR_OP2_IMM_A(mcode);
91         disp = M_INSTR_OP2_IMM_I(mcode);
92         d    = M_INSTR_OP2_IMM_D(mcode);
93
94         val  = _mc->gp_regs[d];
95
96         if (s1 == REG_ZERO) {
97                 /* We use the exception type as load displacement. */
98                 type = disp;
99
100                 if (type == TRAP_COMPILER) {
101                         /* The XPC is the RA minus 1, because the RA points to the
102                            instruction after the call. */
103
104                         xpc = ra - 4;
105                 }
106         }
107         else {
108                 /* Normal NPE. */
109                 addr = _mc->gp_regs[s1];
110                 type = (int) addr;
111         }
112
113         /* Handle the trap. */
114
115         p = trap_handle(type, val, pv, sp, ra, xpc, _p);
116
117         /* Set registers. */
118
119         switch (type) {
120         case TRAP_COMPILER:
121                 if (p != NULL) {
122                         _mc->gp_regs[REG_PV] = (uintptr_t) p;
123                         _mc->gp_regs[PT_NIP] = (uintptr_t) p;
124                         break;
125                 }
126
127                 /* Get and set the PV from the parent Java method. */
128
129                 pv = md_codegen_get_pv_from_pc(ra);
130
131                 _mc->gp_regs[REG_PV] = (uintptr_t) pv;
132
133                 /* Get the exception object. */
134
135                 p = builtin_retrieve_exception();
136
137                 assert(p != NULL);
138
139                 /* fall-through */
140
141         case TRAP_PATCHER:
142                 if (p == NULL)
143                         break;
144
145                 /* fall-through */
146                 
147         default:
148                 _mc->gp_regs[REG_ITMP1_XPTR] = (uintptr_t) p;
149                 _mc->gp_regs[REG_ITMP2_XPC]  = (uintptr_t) xpc;
150                 _mc->gp_regs[PT_NIP]         = (uintptr_t) asm_handle_exception;
151         }
152 }
153
154
155 /* md_signal_handler_sigusr2 ***************************************************
156
157    Signal handler for profiling sampling.
158
159 *******************************************************************************/
160
161 #if defined(ENABLE_THREADS)
162 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
163 {
164         threadobject *tobj;
165         ucontext_t   *_uc;
166         mcontext_t   *_mc;
167         u1           *pc;
168
169         tobj = THREADOBJECT;
170
171         _uc = (ucontext_t *) _p;
172         _mc = &(_uc->uc_mcontext);
173
174         pc = (u1 *) _mc->gp_regs[PT_NIP];
175
176         tobj->pc = pc;
177 }
178 #endif
179
180
181 /* md_executionstate_read ******************************************************
182
183    Read the given context into an executionstate.
184
185 *******************************************************************************/
186
187 void md_executionstate_read(executionstate_t *es, void *context)
188 {
189 #if 0
190         ucontext_t    *_uc;
191         mcontext_t    *_mc;
192         unsigned long *_gregs;
193         s4              i;
194
195         _uc = (ucontext_t *) context;
196
197         _mc    = _uc->uc_mcontext.uc_regs;
198         _gregs = _mc->gregs;
199
200         /* read special registers */
201         es->pc = (u1 *) _gregs[PT_NIP];
202         es->sp = (u1 *) _gregs[REG_SP];
203         es->pv = (u1 *) _gregs[REG_PV];
204         es->ra = (u1 *) _gregs[PT_LNK];
205
206         /* read integer registers */
207         for (i = 0; i < INT_REG_CNT; i++)
208                 es->intregs[i] = _gregs[i];
209
210         /* read float registers */
211         /* Do not use the assignment operator '=', as the type of
212          * the _mc->fpregs[i] can cause invalid conversions. */
213
214         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
215         system_memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
216 #endif
217
218         vm_abort("md_executionstate_read: IMPLEMENT ME!");
219 }
220
221
222 /* md_executionstate_write *****************************************************
223
224    Write the given executionstate back to the context.
225
226 *******************************************************************************/
227
228 void md_executionstate_write(executionstate_t *es, void *context)
229 {
230 #if 0
231         ucontext_t    *_uc;
232         mcontext_t    *_mc;
233         unsigned long *_gregs;
234         s4              i;
235
236         _uc = (ucontext_t *) context;
237
238         _mc    = _uc->uc_mcontext.uc_regs;
239         _gregs = _mc->gregs;
240
241         /* write integer registers */
242         for (i = 0; i < INT_REG_CNT; i++)
243                 _gregs[i] = es->intregs[i];
244
245         /* write float registers */
246         /* Do not use the assignment operator '=', as the type of
247          * the _mc->fpregs[i] can cause invalid conversions. */
248
249         assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
250         system_memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
251
252         /* write special registers */
253         _gregs[PT_NIP] = (ptrint) es->pc;
254         _gregs[REG_SP] = (ptrint) es->sp;
255         _gregs[REG_PV] = (ptrint) es->pv;
256         _gregs[PT_LNK] = (ptrint) es->ra;
257 #endif
258
259         vm_abort("md_executionstate_write: IMPLEMENT ME!");
260 }
261
262
263 /*
264  * These are local overrides for various environment variables in Emacs.
265  * Please do not remove this and leave it at the end of the file, where
266  * Emacs will automagically detect them.
267  * ---------------------------------------------------------------------
268  * Local variables:
269  * mode: c
270  * indent-tabs-mode: t
271  * c-basic-offset: 4
272  * tab-width: 4
273  * End:
274  */