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