CACAO
md-os.cpp
Go to the documentation of this file.
1 /* src/vm/jit/x86_64/solaris/md-os.cpp - machine dependent x86_64 Solaris functions
2 
3  Copyright (C) 2008-2013
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 <cassert>
29 #include <cstdlib>
30 #include <stdint.h>
31 #include <ucontext.h>
32 
33 #include "vm/types.hpp"
34 
36 #include "vm/jit/x86_64/md.hpp"
37 
38 #include "threads/thread.hpp"
39 
40 #include "vm/signallocal.hpp"
41 
42 #include "vm/jit/asmpart.hpp"
44 #include "vm/jit/trap.hpp"
45 
46 
47 /**
48  * Signal handler for hardware exceptions.
49  */
50 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
51 {
52  ucontext_t *_uc = (ucontext_t *) _p;
53  mcontext_t *_mc = &_uc->uc_mcontext;
54 
55  /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
56  different to the ones in <ucontext.h>. */
57 
58  void* xpc = (void *) _mc->gregs[REG_RIP];
59 
60  // Handle the trap.
61  trap_handle(TRAP_SIGSEGV, xpc, _p);
62 }
63 
64 
65 /**
66  * Signal handler for hardware divide by zero (ArithmeticException)
67  * check.
68  */
69 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
70 {
71  ucontext_t *_uc = (ucontext_t *) _p;
72  mcontext_t *_mc = &_uc->uc_mcontext;
73 
74  /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
75  different to the ones in <ucontext.h>. */
76 
77  void* xpc = (void *) _mc->gregs[REG_RIP];
78 
79  // Handle the trap.
80  trap_handle(TRAP_SIGFPE, xpc, _p);
81 }
82 
83 
84 /**
85  * Signal handler for hardware patcher traps (ud2).
86  */
87 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
88 {
89  ucontext_t *_uc = (ucontext_t *) _p;
90  mcontext_t *_mc = &_uc->uc_mcontext;
91 
92  /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
93  different to the ones in <ucontext.h>. */
94 
95  void* xpc = (void *) _mc->gregs[REG_RIP];
96 
97  // Handle the trap.
98  trap_handle(TRAP_SIGILL, xpc, _p);
99 }
100 
101 
102 /* md_signal_handler_sigusr2 ***************************************************
103 
104  Signal handler for profiling sampling.
105 
106 *******************************************************************************/
107 
108 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
109 {
110  threadobject *t;
111  ucontext_t *_uc;
112  mcontext_t *_mc;
113  u1 *pc;
114 
115  t = THREADOBJECT;
116 
117  _uc = (ucontext_t *) _p;
118  _mc = &_uc->uc_mcontext;
119 
120  /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
121  different to the ones in <ucontext.h>. */
122 
123  pc = (u1 *) _mc->gregs[REG_RIP];
124 
125  t->pc = pc;
126 }
127 
128 
129 /* md_executionstate_read ******************************************************
130 
131  Read the given context into an executionstate.
132 
133 *******************************************************************************/
134 
135 void md_executionstate_read(executionstate_t *es, void *context)
136 {
137  ucontext_t *_uc;
138  mcontext_t *_mc;
139  s4 i;
140  s4 d;
141 
142  _uc = (ucontext_t *) context;
143  _mc = &_uc->uc_mcontext;
144 
145  /* read special registers */
146  es->pc = (u1 *) _mc->gregs[REG_RIP];
147  es->sp = (u1 *) _mc->gregs[REG_RSP];
148  es->pv = NULL;
149 
150  /* read integer registers */
151  for (i = 0; i < INT_REG_CNT; i++) {
152  switch (i) {
153  case 0: /* REG_RAX */
154  d = REG_RAX;
155  break;
156  case 1: /* REG_RCX */
157  d = REG_RCX;
158  break;
159  case 2: /* REG_RDX */
160  d = REG_RDX;
161  break;
162  case 3: /* REG_RBX */
163  d = REG_RBX;
164  break;
165  case 4: /* REG_RSP */
166  d = REG_RSP;
167  break;
168  case 5: /* REG_RBP */
169  d = REG_RBP;
170  break;
171  case 6: /* REG_RSI */
172  d = REG_RSI;
173  break;
174  case 7: /* REG_RDI */
175  d = REG_RDI;
176  break;
177  case 8: /* REG_R8 == 7 */
178  case 9: /* REG_R9 == 6 */
179  case 10: /* REG_R10 == 5 */
180  case 11: /* REG_R11 == 4 */
181  case 12: /* REG_R12 == 3 */
182  case 13: /* REG_R13 == 2 */
183  case 14: /* REG_R14 == 1 */
184  case 15: /* REG_R15 == 0 */
185  d = 15 - i;
186  break;
187  }
188 
189  es->intregs[i] = _mc->gregs[d];
190  }
191 
192  /* read float registers */
193  for (i = 0; i < FLT_REG_CNT; i++)
194  es->fltregs[i] = 0xdeadbeefdeadbeefL;
195 }
196 
197 
198 /* md_executionstate_write *****************************************************
199 
200  Write the given executionstate back to the context.
201 
202 *******************************************************************************/
203 
205 {
206  ucontext_t *_uc;
207  mcontext_t *_mc;
208  s4 i;
209  s4 d;
210 
211  _uc = (ucontext_t *) context;
212  _mc = &_uc->uc_mcontext;
213 
214  /* write integer registers */
215  for (i = 0; i < INT_REG_CNT; i++) {
216  switch (i) {
217  case 0: /* REG_RAX */
218  d = REG_RAX;
219  break;
220  case 1: /* REG_RCX */
221  d = REG_RCX;
222  break;
223  case 2: /* REG_RDX */
224  d = REG_RDX;
225  break;
226  case 3: /* REG_RBX */
227  d = REG_RBX;
228  break;
229  case 4: /* REG_RSP */
230  d = REG_RSP;
231  break;
232  case 5: /* REG_RBP */
233  d = REG_RBP;
234  break;
235  case 6: /* REG_RSI */
236  d = REG_RSI;
237  break;
238  case 7: /* REG_RDI */
239  d = REG_RDI;
240  break;
241  case 8: /* REG_R8 == 7 */
242  case 9: /* REG_R9 == 6 */
243  case 10: /* REG_R10 == 5 */
244  case 11: /* REG_R11 == 4 */
245  case 12: /* REG_R12 == 3 */
246  case 13: /* REG_R13 == 2 */
247  case 14: /* REG_R14 == 1 */
248  case 15: /* REG_R15 == 0 */
249  d = 15 - i;
250  break;
251  }
252 
253  _mc->gregs[d] = es->intregs[i];
254  }
255 
256  /* write special registers */
257  _mc->gregs[REG_RIP] = (ptrint) es->pc;
258  _mc->gregs[REG_RSP] = (ptrint) es->sp;
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  * vim:noexpandtab:sw=4:ts=4:
274  */
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_sigfpe(int sig, siginfo_t *siginfo, void *_p)
Definition: md-os.cpp:59
#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
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
#define FLT_REG_CNT
Definition: md-abi.hpp:79