Merged with new-trap-decoding branch at rev a792088a3f04 (branch closed).
[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, 2009
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/signallocal.hpp"
42
43 #include "vm/jit/asmpart.h"
44 #include "vm/jit/executionstate.h"
45 #include "vm/jit/trap.hpp"
46
47
48 /**
49  * Signal handler for hardware-exceptions.
50  */
51 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
52 {
53         ucontext_t* _uc = (ucontext_t *) _p;
54         mcontext_t* _mc = &(_uc->uc_mcontext);
55
56         void* xpc = (void*) _mc->gp_regs[PT_NIP];
57
58         // Handle the trap.
59         trap_handle(TRAP_SIGSEGV, xpc, _p);
60 }
61
62
63 /**
64  * Signal handler for patcher calls.
65  */
66 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
67 {
68         ucontext_t* _uc = (ucontext_t*) _p;
69         mcontext_t* _mc = &(_uc->uc_mcontext);
70
71         void* xpc =(void*) _mc->gp_regs[PT_NIP];
72
73         // Handle the trap.
74         trap_handle(TRAP_SIGILL, xpc, _p);
75 }
76
77
78 /* md_signal_handler_sigusr2 ***************************************************
79
80    Signal handler for profiling sampling.
81
82 *******************************************************************************/
83
84 #if defined(ENABLE_THREADS)
85 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
86 {
87         threadobject *tobj;
88         ucontext_t   *_uc;
89         mcontext_t   *_mc;
90         u1           *pc;
91
92         tobj = THREADOBJECT;
93
94         _uc = (ucontext_t *) _p;
95         _mc = &(_uc->uc_mcontext);
96
97         pc = (u1 *) _mc->gp_regs[PT_NIP];
98
99         tobj->pc = pc;
100 }
101 #endif
102
103
104 /* md_executionstate_read ******************************************************
105
106    Read the given context into an executionstate.
107
108 *******************************************************************************/
109
110 void md_executionstate_read(executionstate_t *es, void *context)
111 {
112         ucontext_t    *_uc;
113         mcontext_t    *_mc;
114         s4              i;
115
116         _uc = (ucontext_t *) context;
117         _mc = &(_uc->uc_mcontext);
118
119         /* read special registers */
120         es->pc = (u1 *) _mc->gp_regs[PT_NIP];
121         es->sp = (u1 *) _mc->gp_regs[REG_SP];
122         es->pv = (u1 *) _mc->gp_regs[REG_PV];
123         es->ra = (u1 *) _mc->gp_regs[PT_LNK];
124
125         /* read integer registers */
126         for (i = 0; i < INT_REG_CNT; i++)
127                 es->intregs[i] = _mc->gp_regs[i];
128
129         /* read float registers */
130         /* Do not use the assignment operator '=', as the type of
131          * the _mc->fpregs[i] can cause invalid conversions. */
132
133         // The assertion below will fail because _mc->fp_regs[] also
134         // contains the "fpscr" register.
135         //assert(sizeof(_mc->fp_regs) == sizeof(es->fltregs));
136         os_memcpy(&es->fltregs, &_mc->fp_regs, sizeof(es->fltregs));
137 }
138
139
140 /* md_executionstate_write *****************************************************
141
142    Write the given executionstate back to the context.
143
144 *******************************************************************************/
145
146 void md_executionstate_write(executionstate_t *es, void *context)
147 {
148         ucontext_t    *_uc;
149         mcontext_t    *_mc;
150         s4              i;
151
152         _uc = (ucontext_t *) context;
153         _mc = &(_uc->uc_mcontext);
154
155         /* write integer registers */
156         for (i = 0; i < INT_REG_CNT; i++)
157                 _mc->gp_regs[i] = es->intregs[i];
158
159         /* write float registers */
160         /* Do not use the assignment operator '=', as the type of
161          * the _mc->fpregs[i] can cause invalid conversions. */
162
163         // The assertion below will fail because _mc->fp_regs[] also
164         // contains the "fpscr" register.
165         //assert(sizeof(_mc->fp_regs) == sizeof(es->fltregs));
166         os_memcpy(&_mc->fp_regs, &es->fltregs, sizeof(es->fltregs));
167
168         /* write special registers */
169         _mc->gp_regs[PT_NIP] = (ptrint) es->pc;
170         _mc->gp_regs[REG_SP] = (ptrint) es->sp;
171         _mc->gp_regs[REG_PV] = (ptrint) es->pv;
172         _mc->gp_regs[PT_LNK] = (ptrint) es->ra;
173 }
174
175
176 /*
177  * These are local overrides for various environment variables in Emacs.
178  * Please do not remove this and leave it at the end of the file, where
179  * Emacs will automagically detect them.
180  * ---------------------------------------------------------------------
181  * Local variables:
182  * mode: c
183  * indent-tabs-mode: t
184  * c-basic-offset: 4
185  * tab-width: 4
186  * End:
187  */