CACAO
md-os.cpp
Go to the documentation of this file.
1 /* src/vm/jit/powerpc/linux/md-os.cpp - machine dependent PowerPC Linux functions
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5  Copyright (C) 2008, 2009 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 <cassert>
30 #include <stdint.h>
31 #include <ucontext.h>
32 
33 #include "vm/types.hpp"
34 
36 #include "vm/jit/powerpc/md.hpp"
38 
39 #include "threads/thread.hpp"
40 
41 #include "vm/signallocal.hpp"
42 #include "vm/os.hpp"
43 
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;
55  unsigned long* _gregs;
56 
57 #if defined(__UCLIBC__)
58  _mc = &(_uc->uc_mcontext);
59  _gregs = _mc->regs->gpr;
60 #else
61  _mc = _uc->uc_mcontext.uc_regs;
62  _gregs = _mc->gregs;
63 #endif
64 
65  void* xpc = (void*) _gregs[PT_NIP];
66 
67  // Handle the trap.
68  trap_handle(TRAP_SIGSEGV, xpc, _p);
69 }
70 
71 
72 /**
73  * Signal handler for patcher calls.
74  */
75 void md_signal_handler_sigill(int sig, siginfo_t* siginfo, void* _p)
76 {
77  ucontext_t* _uc = (ucontext_t*) _p;
78  mcontext_t* _mc;
79  unsigned long* _gregs;
80 
81 #if defined(__UCLIBC__)
82  _mc = &(_uc->uc_mcontext);
83  _gregs = _mc->regs->gpr;
84 #else
85  _mc = _uc->uc_mcontext.uc_regs;
86  _gregs = _mc->gregs;
87 #endif
88 
89  void* xpc = (void*) _gregs[PT_NIP];
90 
91  // Handle the trap.
92  trap_handle(TRAP_SIGILL, xpc, _p);
93 }
94 
95 
96 /**
97  * Signal handler for hardware-traps.
98  */
99 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
100 {
101  ucontext_t* _uc = (ucontext_t*) _p;
102  mcontext_t* _mc;
103  unsigned long* _gregs;
104 
105 #if defined(__UCLIBC__)
106  _mc = &(_uc->uc_mcontext);
107  _gregs = _mc->regs->gpr;
108 #else
109  _mc = _uc->uc_mcontext.uc_regs;
110  _gregs = _mc->gregs;
111 #endif
112 
113  void* xpc = (void*) _gregs[PT_NIP];
114 
115  // Handle the trap.
116  trap_handle(TRAP_SIGTRAP, xpc, _p);
117 }
118 
119 
120 /* md_signal_handler_sigusr2 ***************************************************
121 
122  Signal handler for profiling sampling.
123 
124 *******************************************************************************/
125 
126 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
127 {
128  threadobject *tobj;
129  ucontext_t *_uc;
130  mcontext_t *_mc;
131  unsigned long *_gregs;
132  u1 *pc;
133 
134  tobj = THREADOBJECT;
135 
136  _uc = (ucontext_t *) _p;
137 
138 #if defined(__UCLIBC__)
139  _mc = &(_uc->uc_mcontext);
140  _gregs = _mc->regs->gpr;
141 #else
142  _mc = _uc->uc_mcontext.uc_regs;
143  _gregs = _mc->gregs;
144 #endif
145 
146  pc = (u1 *) _gregs[PT_NIP];
147 
148  tobj->pc = pc;
149 }
150 
151 
152 /* md_executionstate_read ******************************************************
153 
154  Read the given context into an executionstate.
155 
156 *******************************************************************************/
157 
158 void md_executionstate_read(executionstate_t *es, void *context)
159 {
160  ucontext_t *_uc;
161  mcontext_t *_mc;
162  unsigned long *_gregs;
163  s4 i;
164 
165  _uc = (ucontext_t *) context;
166 
167 #if defined(__UCLIBC__)
168 #error Please port md_executionstate_read to __UCLIBC__
169 #else
170  _mc = _uc->uc_mcontext.uc_regs;
171  _gregs = _mc->gregs;
172 #endif
173 
174  /* read special registers */
175  es->pc = (u1 *) _gregs[PT_NIP];
176  es->sp = (u1 *) _gregs[REG_SP];
177  es->pv = (u1 *) _gregs[REG_PV];
178  es->ra = (u1 *) _gregs[PT_LNK];
179 
180  /* read integer registers */
181  for (i = 0; i < INT_REG_CNT; i++)
182  es->intregs[i] = _gregs[i];
183 
184  /* read float registers */
185  /* Do not use the assignment operator '=', as the type of
186  * the _mc->fpregs[i] can cause invalid conversions. */
187 
188  assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
189  os::memcpy(&es->fltregs, &_mc->fpregs.fpregs, sizeof(_mc->fpregs.fpregs));
190 }
191 
192 
193 /* md_executionstate_write *****************************************************
194 
195  Write the given executionstate back to the context.
196 
197 *******************************************************************************/
198 
200 {
201  ucontext_t *_uc;
202  mcontext_t *_mc;
203  unsigned long *_gregs;
204  s4 i;
205 
206  _uc = (ucontext_t *) context;
207 
208 #if defined(__UCLIBC__)
209 #error Please port md_executionstate_write to __UCLIBC__
210 #else
211  _mc = _uc->uc_mcontext.uc_regs;
212  _gregs = _mc->gregs;
213 #endif
214 
215  /* write integer registers */
216  for (i = 0; i < INT_REG_CNT; i++)
217  _gregs[i] = es->intregs[i];
218 
219  /* write float registers */
220  /* Do not use the assignment operator '=', as the type of
221  * the _mc->fpregs[i] can cause invalid conversions. */
222 
223  assert(sizeof(_mc->fpregs.fpregs) == sizeof(es->fltregs));
224  os::memcpy(&_mc->fpregs.fpregs, &es->fltregs, sizeof(_mc->fpregs.fpregs));
225 
226  /* write special registers */
227  _gregs[PT_NIP] = (ptrint) es->pc;
228  _gregs[REG_SP] = (ptrint) es->sp;
229  _gregs[REG_PV] = (ptrint) es->pv;
230  _gregs[PT_LNK] = (ptrint) es->ra;
231 }
232 
233 
234 /*
235  * These are local overrides for various environment variables in Emacs.
236  * Please do not remove this and leave it at the end of the file, where
237  * Emacs will automagically detect them.
238  * ---------------------------------------------------------------------
239  * Local variables:
240  * mode: c++
241  * indent-tabs-mode: t
242  * c-basic-offset: 4
243  * tab-width: 4
244  * End:
245  */
#define REG_SP
Definition: md-abi.hpp:53
#define REG_PV
Definition: md-abi.hpp:42
void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
NullPointerException signal handler for hardware null pointer check.
Definition: md-os.cpp:50
void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
Illegal Instruction signal handler for hardware exception checks.
Definition: md-os.cpp:65
struct sigcontext uc_mcontext
Definition: md-os.cpp:42
#define INT_REG_CNT
Definition: md-abi.hpp:72
uint8_t u1
Definition: types.hpp:40
void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
Signal handler for hardware-traps.
Definition: md-os.cpp:72
#define xpc
Definition: md-asm.hpp:51
void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
Definition: md-os.cpp:83
MIIterator i
int32_t s4
Definition: types.hpp:45
static void * memcpy(void *dest, const void *src, size_t n)
Definition: os.hpp:492
void md_executionstate_write(executionstate_t *es, void *context)
Definition: md-os.cpp:147
CONTEXT mcontext_t
Definition: ucontext.h:27
#define pc
Definition: md-asm.hpp:56
void md_executionstate_read(executionstate_t *es, void *context)
Definition: md-os.cpp:107
void trap_handle(int sig, void *xpc, void *context)
Handles the signal which is generated by trap instructions, caught by a signal handler and calls the ...
Definition: trap.cpp:101
uintptr_t intregs[INT_REG_CNT]
uintptr_t ptrint
Definition: types.hpp:54
double fltregs[FLT_REG_CNT]
#define THREADOBJECT
Definition: thread-none.hpp:47