911b62fb384853606d11fac542ddd32b5a424b59
[cacao.git] / src / vm / jit / i386 / solaris / md-os.c
1 /* src/vm/jit/i386/solaris/md-os.c - machine dependent i386 Solaris functions
2
3    Copyright (C) 2008, 2009
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 <stdint.h>
29 #include <ucontext.h>
30
31 #include "vm/types.h"
32 #include "vm/os.hpp"
33
34 #define SKIP_REG_DEFS 1
35
36 #include "vm/jit/i386/codegen.h"
37 #include "vm/jit/i386/md.h"
38
39 #include "threads/thread.hpp"
40
41 #include "vm/signallocal.hpp"
42
43 #include "vm/jit/executionstate.h"
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         void* xpc = (void*) _mc->gregs[EIP];
56
57     // Handle the trap.
58     trap_handle(TRAP_SIGSEGV, xpc, _p);
59 }
60
61
62 /* md_signal_handler_sigfpe ****************************************************
63
64    ArithmeticException signal handler for hardware divide by zero
65    check.
66
67 *******************************************************************************/
68
69 /**
70  * Signal handler for hardware divide by zero (ArithmeticException)
71  * check.
72  */
73 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
74 {
75         ucontext_t *_uc = (ucontext_t *) _p;
76         mcontext_t *_mc = &_uc->uc_mcontext;
77
78         void* xpc = (void*) _mc->gregs[EIP];
79
80     // Handle the trap.
81     trap_handle(TRAP_SIGFPE, xpc, _p);
82 }
83
84
85 /**
86  * Signal handler for hardware patcher traps (ud2).
87  */
88 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
89 {
90         ucontext_t *_uc = (ucontext_t *) _p;
91         mcontext_t *_mc = &_uc->uc_mcontext;
92
93         void* xpc = (void*) _mc->gregs[EIP];
94
95     // Handle the trap.
96     trap_handle(TRAP_SIGILL, xpc, _p);
97 }
98
99
100 /* md_signal_handler_sigusr1 ***************************************************
101
102    Signal handler for suspending threads.
103
104 *******************************************************************************/
105
106 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
107 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
108 {
109         ucontext_t *_uc;
110         mcontext_t *_mc;
111         u1         *pc;
112         u1         *sp;
113
114         _uc = (ucontext_t *) _p;
115         _mc = &_uc->uc_mcontext;
116
117         /* get the PC and SP for this thread */
118         pc = (u1 *) _mc->gregs[EIP];
119         sp = (u1 *) _mc->gregs[UESP];
120
121         /* now suspend the current thread */
122         threads_suspend_ack(pc, sp);
123 }
124 #endif
125
126
127 /* md_signal_handler_sigusr2 ***************************************************
128
129    Signal handler for profiling sampling.
130
131 *******************************************************************************/
132
133 #if defined(ENABLE_THREADS)
134 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
135 {
136         threadobject *t;
137         ucontext_t   *_uc;
138         mcontext_t   *_mc;
139         u1           *pc;
140
141         t = THREADOBJECT;
142
143         _uc = (ucontext_t *) _p;
144         _mc = &_uc->uc_mcontext;
145
146         pc = (u1 *) _mc->gregs[EIP];
147
148         t->pc = pc;
149 }
150 #endif
151
152
153 /* md_executionstate_read ******************************************************
154
155    Read the given context into an executionstate for Replacement.
156
157 *******************************************************************************/
158
159 void md_executionstate_read(executionstate_t *es, void *context)
160 {
161         ucontext_t *_uc;
162         mcontext_t *_mc;
163         s4          i;
164
165         _uc = (ucontext_t *) context;
166         _mc = &_uc->uc_mcontext;
167
168         /* read special registers */
169         es->pc = (u1 *) _mc->gregs[EIP];
170         es->sp = (u1 *) _mc->gregs[UESP];
171         es->pv = NULL;                   /* pv must be looked up via AVL tree */
172
173         /* read integer registers */
174         for (i = 0; i < INT_REG_CNT; i++)
175                 es->intregs[i] = _mc->gregs[EAX - i];
176
177         /* read float registers */
178         for (i = 0; i < FLT_REG_CNT; i++)
179                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
180 }
181
182
183 /* md_executionstate_write *****************************************************
184
185    Write the given executionstate back to the context for Replacement.
186
187 *******************************************************************************/
188
189 void md_executionstate_write(executionstate_t *es, void *context)
190 {
191         ucontext_t *_uc;
192         mcontext_t *_mc;
193         s4          i;
194
195         _uc = (ucontext_t *) context;
196         _mc = &_uc->uc_mcontext;
197
198         /* write integer registers */
199         for (i = 0; i < INT_REG_CNT; i++)
200                 _mc->gregs[EAX - i] = es->intregs[i];
201
202         /* write special registers */
203         _mc->gregs[EIP] = (ptrint) es->pc;
204         _mc->gregs[UESP] = (ptrint) es->sp;
205 }
206
207
208 /*
209  * These are local overrides for various environment variables in Emacs.
210  * Please do not remove this and leave it at the end of the file, where
211  * Emacs will automagically detect them.
212  * ---------------------------------------------------------------------
213  * Local variables:
214  * mode: c
215  * indent-tabs-mode: t
216  * c-basic-offset: 4
217  * tab-width: 4
218  * End:
219  */