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