CACAO
md-os.cpp
Go to the documentation of this file.
1 /* src/vm/jit/i386/linux/md-os.cpp - machine dependent i386 Linux functions
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5  Copyright (C) 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 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE
29 #endif
30 
31 #include "config.h"
32 
33 #include <stdint.h>
34 #include <ucontext.h>
35 
36 #include "vm/types.hpp"
37 
38 #include "vm/jit/i386/codegen.hpp"
39 #include "vm/jit/i386/md.hpp"
40 
41 #include "threads/thread.hpp"
42 
43 #include "vm/signallocal.hpp"
44 
46 #include "vm/jit/trap.hpp"
47 
48 
49 /**
50  * Signal handler for hardware exceptions.
51  */
52 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
53 {
54  ucontext_t* _uc = (ucontext_t *) _p;
55  mcontext_t* _mc = &_uc->uc_mcontext;
56 
57  void* xpc = (void*) _mc->gregs[REG_EIP];
58 
59  // Handle the trap.
60  trap_handle(TRAP_SIGSEGV, xpc, _p);
61 }
62 
63 
64 /**
65  * Signal handler for hardware divide by zero (ArithmeticException)
66  * check.
67  */
68 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
69 {
70  ucontext_t* _uc = (ucontext_t *) _p;
71  mcontext_t* _mc = &_uc->uc_mcontext;
72 
73  void* xpc = (void*) _mc->gregs[REG_EIP];
74 
75  // Handle the trap.
76  trap_handle(TRAP_SIGFPE, xpc, _p);
77 }
78 
79 
80 /**
81  * Signal handler for hardware patcher traps (ud2).
82  */
83 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
84 {
85  ucontext_t* _uc = (ucontext_t *) _p;
86  mcontext_t* _mc = &_uc->uc_mcontext;
87 
88  void* xpc = (void*) _mc->gregs[REG_EIP];
89 
90  // Handle the trap.
91  trap_handle(TRAP_SIGILL, xpc, _p);
92 }
93 
94 
95 /* md_signal_handler_sigusr2 ***************************************************
96 
97  Signal handler for profiling sampling.
98 
99 *******************************************************************************/
100 
101 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
102 {
103  threadobject *t;
104  ucontext_t *_uc;
105  mcontext_t *_mc;
106  u1 *pc;
107 
108  t = THREADOBJECT;
109 
110  _uc = (ucontext_t *) _p;
111  _mc = &_uc->uc_mcontext;
112 
113  pc = (u1 *) _mc->gregs[REG_EIP];
114 
115  t->pc = pc;
116 }
117 
118 
119 /* md_executionstate_read ******************************************************
120 
121  Read the given context into an executionstate for Replacement.
122 
123 *******************************************************************************/
124 
125 void md_executionstate_read(executionstate_t *es, void *context)
126 {
127  ucontext_t *_uc;
128  mcontext_t *_mc;
129  s4 i;
130 
131  _uc = (ucontext_t *) context;
132  _mc = &_uc->uc_mcontext;
133 
134  /* read special registers */
135  es->pc = (u1 *) _mc->gregs[REG_EIP];
136  es->sp = (u1 *) _mc->gregs[REG_ESP];
137  es->pv = NULL; /* pv must be looked up via AVL tree */
138 
139  /* read integer registers */
140  for (i = 0; i < INT_REG_CNT; i++)
141  es->intregs[i] = _mc->gregs[REG_EAX - i];
142 
143  /* read float registers */
144  for (i = 0; i < FLT_REG_CNT; i++)
145  es->fltregs[i] = 0xdeadbeefdeadbeefULL;
146 }
147 
148 
149 /* md_executionstate_write *****************************************************
150 
151  Write the given executionstate back to the context for Replacement.
152 
153 *******************************************************************************/
154 
156 {
157  ucontext_t *_uc;
158  mcontext_t *_mc;
159  s4 i;
160 
161  _uc = (ucontext_t *) context;
162  _mc = &_uc->uc_mcontext;
163 
164  /* write integer registers */
165  for (i = 0; i < INT_REG_CNT; i++)
166  _mc->gregs[REG_EAX - i] = es->intregs[i];
167 
168  /* write special registers */
169  _mc->gregs[REG_EIP] = (ptrint) es->pc;
170  _mc->gregs[REG_ESP] = (ptrint) es->sp;
171 }
172 
173 
174 /*
175  * These are local overrides for various environment variables in Emacs.
176  * Please do not remove this and leave it at the end of the file, where
177  * Emacs will automagically detect them.
178  * ---------------------------------------------------------------------
179  * Local variables:
180  * mode: c++
181  * indent-tabs-mode: t
182  * c-basic-offset: 4
183  * tab-width: 4
184  * End:
185  */
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