25d0b1bf4d19ddfffc47a64763ae45fea8b71e3f
[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    Copyright (C) 2008 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/powerpc64/codegen.h"
36 #include "vm/jit/powerpc64/md.h"
37 #include "vm/jit/powerpc64/linux/md-abi.h"
38
39 #include "threads/thread.hpp"
40
41 #include "vm/jit/builtin.hpp"
42 #include "vm/signallocal.hpp"
43 #include "vm/os.hpp"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/executionstate.h"
47
48 #if defined(ENABLE_PROFILING)
49 # include "vm/jit/optimizing/profile.h"
50 #endif
51
52 #include "vm/jit/disass.h"
53 #include "vm/jit/trap.hpp"
54
55
56 /* md_signal_handler_sigsegv ***************************************************
57  
58         Signal handler for hardware-exceptions.
59
60 *******************************************************************************/
61
62 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
63 {
64         ucontext_t     *_uc;
65         mcontext_t     *_mc;
66         u1             *pv;
67         u1             *sp;
68         u1             *ra;
69         u1             *xpc;
70         u4              mcode;
71         int             s1;
72         int16_t         disp;
73         int             d;
74         int             type;
75         intptr_t        addr;
76         intptr_t        val;
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         trap_handle(type, val, pv, sp, ra, xpc, _p);
118 }
119
120
121 /**
122  * Signal handler for patcher calls.
123  */
124 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
125 {
126         ucontext_t* _uc = (ucontext_t*) _p;
127         mcontext_t* _mc = &(_uc->uc_mcontext);
128
129         /* get register values */
130
131         void* pv = (void*) _mc->gp_regs[REG_PV];
132         void* sp = (void*) _mc->gp_regs[REG_SP];
133         void* ra = (void*) _mc->gp_regs[PT_LNK]; // The RA is correct for leag methods.
134         void* xpc =(void*) _mc->gp_regs[PT_NIP];
135
136         // Get the illegal-instruction.
137         uint32_t mcode = *((uint32_t*) xpc);
138
139         // Check if the trap instruction is valid.
140         // TODO Move this into patcher_handler.
141         if (patcher_is_valid_trap_instruction_at(xpc) == false) {
142                 // Check if the PC has been patched during our way to this
143                 // signal handler (see PR85).
144                 if (patcher_is_patched_at(xpc) == true)
145                         return;
146
147                 // We have a problem...
148                 log_println("md_signal_handler_sigill: Unknown illegal instruction 0x%x at 0x%lx", mcode, xpc);
149 #if defined(ENABLE_DISASSEMBLER)
150                 (void) disassinstr(xpc);
151 #endif
152                 vm_abort("Aborting...");
153         }
154
155         // This signal is always a patcher.
156         int      type = TRAP_PATCHER;
157         intptr_t val  = 0;
158
159         // Handle the trap.
160         trap_handle(type, val, pv, sp, ra, xpc, _p);
161 }
162
163
164 /* md_signal_handler_sigusr2 ***************************************************
165
166    Signal handler for profiling sampling.
167
168 *******************************************************************************/
169
170 #if defined(ENABLE_THREADS)
171 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
172 {
173         threadobject *tobj;
174         ucontext_t   *_uc;
175         mcontext_t   *_mc;
176         u1           *pc;
177
178         tobj = THREADOBJECT;
179
180         _uc = (ucontext_t *) _p;
181         _mc = &(_uc->uc_mcontext);
182
183         pc = (u1 *) _mc->gp_regs[PT_NIP];
184
185         tobj->pc = pc;
186 }
187 #endif
188
189
190 /* md_executionstate_read ******************************************************
191
192    Read the given context into an executionstate.
193
194 *******************************************************************************/
195
196 void md_executionstate_read(executionstate_t *es, void *context)
197 {
198         ucontext_t    *_uc;
199         mcontext_t    *_mc;
200         s4              i;
201
202         _uc = (ucontext_t *) context;
203         _mc = &(_uc->uc_mcontext);
204
205         /* read special registers */
206         es->pc = (u1 *) _mc->gp_regs[PT_NIP];
207         es->sp = (u1 *) _mc->gp_regs[REG_SP];
208         es->pv = (u1 *) _mc->gp_regs[REG_PV];
209         es->ra = (u1 *) _mc->gp_regs[PT_LNK];
210
211         /* read integer registers */
212         for (i = 0; i < INT_REG_CNT; i++)
213                 es->intregs[i] = _mc->gp_regs[i];
214
215         /* read float registers */
216         /* Do not use the assignment operator '=', as the type of
217          * the _mc->fpregs[i] can cause invalid conversions. */
218
219         // The assertion below will fail because _mc->fp_regs[] also
220         // contains the "fpscr" register.
221         //assert(sizeof(_mc->fp_regs) == sizeof(es->fltregs));
222         os_memcpy(&es->fltregs, &_mc->fp_regs, sizeof(es->fltregs));
223 }
224
225
226 /* md_executionstate_write *****************************************************
227
228    Write the given executionstate back to the context.
229
230 *******************************************************************************/
231
232 void md_executionstate_write(executionstate_t *es, void *context)
233 {
234         ucontext_t    *_uc;
235         mcontext_t    *_mc;
236         s4              i;
237
238         _uc = (ucontext_t *) context;
239         _mc = &(_uc->uc_mcontext);
240
241         /* write integer registers */
242         for (i = 0; i < INT_REG_CNT; i++)
243                 _mc->gp_regs[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         // The assertion below will fail because _mc->fp_regs[] also
250         // contains the "fpscr" register.
251         //assert(sizeof(_mc->fp_regs) == sizeof(es->fltregs));
252         os_memcpy(&_mc->fp_regs, &es->fltregs, sizeof(es->fltregs));
253
254         /* write special registers */
255         _mc->gp_regs[PT_NIP] = (ptrint) es->pc;
256         _mc->gp_regs[REG_SP] = (ptrint) es->sp;
257         _mc->gp_regs[REG_PV] = (ptrint) es->pv;
258         _mc->gp_regs[PT_LNK] = (ptrint) es->ra;
259 }
260
261
262 /*
263  * These are local overrides for various environment variables in Emacs.
264  * Please do not remove this and leave it at the end of the file, where
265  * Emacs will automagically detect them.
266  * ---------------------------------------------------------------------
267  * Local variables:
268  * mode: c
269  * indent-tabs-mode: t
270  * c-basic-offset: 4
271  * tab-width: 4
272  * End:
273  */