ad8fbd9fdbbe56596bf8ee76789aa97cf90946cf
[cacao.git] / src / vm / jit / x86_64 / linux / md-os.c
1 /* src/vm/jit/x86_64/linux/md-os.c - machine dependent x86_64 Linux functions
2
3    Copyright (C) 2007, 2008
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 #define _GNU_SOURCE
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/x86_64/codegen.h"
38 #include "vm/jit/x86_64/md.h"
39
40 #include "threads/thread.hpp"
41
42 #include "vm/signallocal.hpp"
43
44 #include "vm/jit/asmpart.h"
45 #include "vm/jit/executionstate.h"
46 #include "vm/jit/trap.hpp"
47
48
49 /**
50  * Signal handler for hardware exception.
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         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
58            different to the ones in <ucontext.h>. */
59
60         void* xpc = (void*) _mc->gregs[REG_RIP];
61
62         // Handle the trap.
63         trap_handle(TRAP_SIGSEGV, xpc, _p);
64 }
65
66
67 /**
68  * ArithmeticException signal handler for hardware divide by zero
69  * check.
70  */
71 void md_signal_handler_sigfpe(int sig, siginfo_t *siginfo, void *_p)
72 {
73         ucontext_t* _uc = (ucontext_t *) _p;
74         mcontext_t* _mc = &_uc->uc_mcontext;
75
76         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
77            different to the ones in <ucontext.h>. */
78
79         void* xpc = (void*) _mc->gregs[REG_RIP];
80
81         // Handle the trap.
82         trap_handle(TRAP_SIGFPE, xpc, _p);
83 }
84
85
86 /**
87  * Signal handler for patchers.
88  */
89 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
90 {
91         ucontext_t* _uc = (ucontext_t *) _p;
92         mcontext_t* _mc = &_uc->uc_mcontext;
93
94         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
95            different to the ones in <ucontext.h>. */
96
97         void* xpc = (void*) _mc->gregs[REG_RIP];
98
99         // Handle the trap.
100         trap_handle(TRAP_SIGILL, xpc, _p);
101 }
102
103
104 /* md_signal_handler_sigusr1 ***************************************************
105
106    Signal handler for suspending threads.
107
108 *******************************************************************************/
109
110 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
111 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
112 {
113         ucontext_t *_uc;
114         mcontext_t *_mc;
115         u1         *pc;
116         u1         *sp;
117
118         _uc = (ucontext_t *) _p;
119         _mc = &_uc->uc_mcontext;
120
121         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
122            different to the ones in <ucontext.h>. */
123
124         /* get the PC and SP for this thread */
125         pc = (u1 *) _mc->gregs[REG_RIP];
126         sp = (u1 *) _mc->gregs[REG_RSP];
127
128         /* now suspend the current thread */
129         threads_suspend_ack(pc, sp);
130 }
131 #endif
132
133
134 /* md_signal_handler_sigusr2 ***************************************************
135
136    Signal handler for profiling sampling.
137
138 *******************************************************************************/
139
140 #if defined(ENABLE_THREADS)
141 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
142 {
143         threadobject *t;
144         ucontext_t   *_uc;
145         mcontext_t   *_mc;
146         u1           *pc;
147
148         t = THREADOBJECT;
149
150         _uc = (ucontext_t *) _p;
151         _mc = &_uc->uc_mcontext;
152
153         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
154            different to the ones in <ucontext.h>. */
155
156         pc = (u1 *) _mc->gregs[REG_RIP];
157
158         t->pc = pc;
159 }
160 #endif
161
162
163 /* md_executionstate_read ******************************************************
164
165    Read the given context into an executionstate.
166
167 *******************************************************************************/
168
169 void md_executionstate_read(executionstate_t *es, void *context)
170 {
171         ucontext_t *_uc;
172         mcontext_t *_mc;
173         s4          i;
174         s4          d;
175
176         _uc = (ucontext_t *) context;
177         _mc = &_uc->uc_mcontext;
178
179         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
180            different to the ones in <ucontext.h>. */
181
182         /* read special registers */
183         es->pc = (u1 *) _mc->gregs[REG_RIP];
184         es->sp = (u1 *) _mc->gregs[REG_RSP];
185         es->pv = NULL;
186
187         /* read integer registers */
188         for (i = 0; i < INT_REG_CNT; i++) {
189                 /* XXX FIX ME! */
190
191                 switch (i) {
192                 case 0:  /* REG_RAX == 13 */
193                         d = REG_RAX;
194                         break;
195                 case 1:  /* REG_RCX == 14 */
196                         d = REG_RCX;
197                         break;
198                 case 2:  /* REG_RDX == 12 */
199                         d = REG_RDX;
200                         break;
201                 case 3:  /* REG_RBX == 11 */
202                         d = REG_RBX;
203                         break;
204                 case 4:  /* REG_RSP == 15 */
205                         d = REG_RSP;
206                         break;
207                 case 5:  /* REG_RBP == 10 */
208                         d = REG_RBP;
209                         break;
210                 case 6:  /* REG_RSI == 9  */
211                         d = REG_RSI;
212                         break;
213                 case 7:  /* REG_RDI == 8  */
214                         d = REG_RDI;
215                         break;
216                 case 8:  /* REG_R8  == 0  */
217                 case 9:  /* REG_R9  == 1  */
218                 case 10: /* REG_R10 == 2  */
219                 case 11: /* REG_R11 == 3  */
220                 case 12: /* REG_R12 == 4  */
221                 case 13: /* REG_R13 == 5  */
222                 case 14: /* REG_R14 == 6  */
223                 case 15: /* REG_R15 == 7  */
224                         d = i - 8;
225                         break;
226                 }
227
228                 es->intregs[i] = _mc->gregs[d];
229         }
230
231         /* read float registers */
232         for (i = 0; i < FLT_REG_CNT; i++)
233                 es->fltregs[i] = 0xdeadbeefdeadbeefL;
234 }
235
236
237 /* md_executionstate_write *****************************************************
238
239    Write the given executionstate back to the context.
240
241 *******************************************************************************/
242
243 void md_executionstate_write(executionstate_t *es, void *context)
244 {
245         ucontext_t *_uc;
246         mcontext_t *_mc;
247         s4          i;
248         s4          d;
249
250         _uc = (ucontext_t *) context;
251         _mc = &_uc->uc_mcontext;
252
253         /* ATTENTION: Don't use CACAO's internal REG_* defines as they are
254            different to the ones in <ucontext.h>. */
255
256         /* write integer registers */
257         for (i = 0; i < INT_REG_CNT; i++) {
258                 /* XXX FIX ME! */
259
260                 switch (i) {
261                 case 0:  /* REG_RAX == 13 */
262                         d = REG_RAX;
263                         break;
264                 case 1:  /* REG_RCX == 14 */
265                         d = REG_RCX;
266                         break;
267                 case 2:  /* REG_RDX == 12 */
268                         d = REG_RDX;
269                         break;
270                 case 3:  /* REG_RBX == 11 */
271                         d = REG_RBX;
272                         break;
273                 case 4:  /* REG_RSP == 15 */
274                         d = REG_RSP;
275                         break;
276                 case 5:  /* REG_RBP == 10 */
277                         d = REG_RBP;
278                         break;
279                 case 6:  /* REG_RSI == 9  */
280                         d = REG_RSI;
281                         break;
282                 case 7:  /* REG_RDI == 8  */
283                         d = REG_RDI;
284                         break;
285                 case 8:  /* REG_R8  == 0  */
286                 case 9:  /* REG_R9  == 1  */
287                 case 10: /* REG_R10 == 2  */
288                 case 11: /* REG_R11 == 3  */
289                 case 12: /* REG_R12 == 4  */
290                 case 13: /* REG_R13 == 5  */
291                 case 14: /* REG_R14 == 6  */
292                 case 15: /* REG_R15 == 7  */
293                         d = i - 8;
294                         break;
295                 }
296
297                 _mc->gregs[d] = es->intregs[i];
298         }
299
300         /* write special registers */
301         _mc->gregs[REG_RIP] = (ptrint) es->pc;
302         _mc->gregs[REG_RSP] = (ptrint) es->sp;
303 }
304
305
306 /*
307  * These are local overrides for various environment variables in Emacs.
308  * Please do not remove this and leave it at the end of the file, where
309  * Emacs will automagically detect them.
310  * ---------------------------------------------------------------------
311  * Local variables:
312  * mode: c
313  * indent-tabs-mode: t
314  * c-basic-offset: 4
315  * tab-width: 4
316  * End:
317  * vim:noexpandtab:sw=4:ts=4:
318  */