CACAO
md-os.cpp
Go to the documentation of this file.
1 /* src/vm/jit/x86_64/darwin/md-os.cpp - machine dependent x86_64 Darwin functions
2 
3  Copyright (C) 1996-2014
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 <signal.h>
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/global.hpp"
41 #include "vm/signallocal.hpp"
42 
44 #include "vm/jit/trap.hpp"
45 
46 /**
47  * Signal handler for hardware exceptions.
48  */
49 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
50 {
51  ucontext_t* _uc = (ucontext_t *) _p;
52  mcontext_t _mc = _uc->uc_mcontext;
53  x86_thread_state64_t* _ss = &_mc->__ss;
54 
55  void* xpc = (void*) _ss->__rip;
56 
57  // Handle the trap.
58  trap_handle(TRAP_SIGSEGV, xpc, _p);
59 }
60 
61 
62 /**
63  * Signal handler for hardware divide by zero (ArithmeticException)
64  * check.
65  */
66 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
67 {
68  ucontext_t* _uc = (ucontext_t *) _p;
69  mcontext_t _mc = _uc->uc_mcontext;
70  x86_thread_state64_t* _ss = &_mc->__ss;
71 
72  void* xpc = (void*) _ss->__rip;
73 
74  // Handle the trap.
75  trap_handle(TRAP_SIGFPE, xpc, _p);
76 }
77 
78 
79 /* md_signal_handler_sigusr2 ***************************************************
80 
81  Signal handler for profiling sampling.
82 
83 *******************************************************************************/
84 
85 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
86 {
87  threadobject *t;
88  ucontext_t *_uc;
89  mcontext_t _mc;
90  x86_thread_state64_t *_ss;
91  u1 *pc;
92 
93  t = THREADOBJECT;
94 
95  _uc = (ucontext_t *) _p;
96  _mc = _uc->uc_mcontext;
97  _ss = &_mc->__ss;
98 
99  pc = (u1 *) _ss->__rip;
100 
101  t->pc = pc;
102 }
103 
104 
105 /**
106  * Signal handler for hardware patcher traps (ud2).
107  */
108 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
109 {
110  ucontext_t* _uc = (ucontext_t *) _p;
111  mcontext_t _mc = _uc->uc_mcontext;
112  x86_thread_state64_t* _ss = &_mc->__ss;
113 
114  void* xpc = (void*) _ss->__rip;
115 
116  // Handle the trap.
117  trap_handle(TRAP_SIGILL, xpc, _p);
118 }
119 
120 /* md_executionstate_read ******************************************************
121 
122  Read the given context into an executionstate.
123 
124 *******************************************************************************/
125 
126 void md_executionstate_read(executionstate_t *es, void *context)
127 {
128  ucontext_t *_uc;
129  mcontext_t _mc;
130  x86_thread_state64_t *_ss;
131  int i;
132 
133  _uc = (ucontext_t *) context;
134  _mc = _uc->uc_mcontext;
135  _ss = &_mc->__ss;
136 
137  /* read special registers */
138  es->pc = (u1 *) _ss->__rip;
139  es->sp = (u1 *) _ss->__rsp;
140  es->pv = NULL; /* pv must be looked up via AVL tree */
141 
142  /* read integer registers */
143  for (i = 0; i < INT_REG_CNT; i++)
144  if (i<8)
145  es->intregs[i] = (i == 0) ? _ss->__rax :
146  ((i == 1) ? _ss->__rcx :
147  ((i == 2) ? _ss->__rdx :
148  ((i == 3) ? _ss->__rbx :
149  ((i == 4) ? _ss->__rsp :
150  ((i == 5) ? _ss->__rbp :
151  ((i == 6) ? _ss->__rsi : _ss->__rdi))))));
152  else
153  es->intregs[i] = (&_ss->__r8)[i-8];
154 
155  /* read float registers */
156  for (i = 0; i < FLT_REG_CNT; i++)
157  es->fltregs[i] = 0xdeadbeefdeadbeefULL;
158 }
159 
160 
161 /* md_executionstate_write *****************************************************
162 
163  Write the given executionstate back to the context.
164 
165 *******************************************************************************/
166 
168 {
169  ucontext_t* _uc;
170  mcontext_t _mc;
171  x86_thread_state64_t* _ss;
172  int i;
173 
174  _uc = (ucontext_t *) context;
175  _mc = _uc->uc_mcontext;
176  _ss = &_mc->__ss;
177 
178  /* write integer registers */
179  for (i = 0; i < INT_REG_CNT; i++)
180  if (i<8)
181  *((i == 0) ? &_ss->__rax :
182  ((i == 1) ? &_ss->__rcx :
183  ((i == 2) ? &_ss->__rdx :
184  ((i == 3) ? &_ss->__rbx :
185  ((i == 4) ? &_ss->__rsp :
186  ((i == 5) ? &_ss->__rbp :
187  ((i == 6) ? &_ss->__rsi : &_ss->__rdi))))))) = es->intregs[i];
188  else
189  (&_ss->__r8)[i-8] = es->intregs[i];
190 
191  /* write special registers */
192  _ss->__rip = (ptrint) es->pc;
193  _ss->__rsp = (ptrint) es->sp;
194 }
195 
196 
197 /*
198  * These are local overrides for various environment variables in Emacs.
199  * Please do not remove this and leave it at the end of the file, where
200  * Emacs will automagically detect them.
201  * ---------------------------------------------------------------------
202  * Local variables:
203  * mode: c
204  * indent-tabs-mode: t
205  * c-basic-offset: 4
206  * tab-width: 4
207  * End:
208  * vim:noexpandtab:sw=4:ts=4:
209  */
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:74
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
void md_executionstate_write(executionstate_t *es, void *context)
Definition: md-os.cpp:148
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:81