* src/vm/jit/alpha/linux/md-os.c: Simplified signal handlers.
[cacao.git] / src / vm / jit / alpha / linux / md-os.c
1 /* src/vm/jit/alpha/linux/md-os.c - machine dependent Alpha Linux functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2008, 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 #include "config.h"
28
29 #include <assert.h>
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/alpha/codegen.h"
36 #include "vm/jit/alpha/md.h"
37 #include "vm/jit/alpha/md-abi.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  * NullPointerException signal handler for hardware null pointer check.
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->sc_pc;
56
57         // Handle the trap.
58         trap_handle(TRAP_SIGSEGV, xpc, _p);
59 }
60
61
62 /**
63  * Illegal Instruction signal handler for hardware exception checks.
64  */
65 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
66 {
67         ucontext_t* _uc = (ucontext_t*) _p;
68         mcontext_t* _mc = &_uc->uc_mcontext;
69
70         void* xpc = (void*) _mc->sc_pc;
71
72         // The PC points to the instruction after the illegal instruction.
73         xpc = (void*) (((uintptr_t) xpc) - 4);
74
75         // Handle the trap.
76         trap_handle(TRAP_SIGILL, xpc, _p);
77 }
78
79
80 /* md_signal_handler_sigusr1 ***************************************************
81
82    Signal handler for suspending threads.
83
84 *******************************************************************************/
85
86 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
87 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
88 {
89         ucontext_t *_uc;
90         mcontext_t *_mc;
91         u1         *pc;
92         u1         *sp;
93
94         _uc = (ucontext_t *) _p;
95         _mc = &_uc->uc_mcontext;
96
97         /* get the PC and SP for this thread */
98         pc = (u1 *) _mc->sc_pc;
99         sp = (u1 *) _mc->sc_regs[REG_SP];
100
101         /* now suspend the current thread */
102         threads_suspend_ack(pc, sp);
103 }
104 #endif
105
106
107 /* md_signal_handler_sigusr2 ***************************************************
108
109    Signal handler for profiling sampling.
110
111 *******************************************************************************/
112
113 #if defined(ENABLE_THREADS)
114 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
115 {
116         threadobject *tobj;
117         ucontext_t   *_uc;
118         mcontext_t   *_mc;
119         u1           *pc;
120
121         tobj = THREADOBJECT;
122
123         _uc = (ucontext_t *) _p;
124         _mc = &_uc->uc_mcontext;
125
126         pc = (u1 *) _mc->sc_pc;
127
128         tobj->pc = pc;
129 }
130 #endif
131
132
133 /* md_executionstate_read ******************************************************
134
135    Read the given context into an executionstate.
136
137 *******************************************************************************/
138
139 void md_executionstate_read(executionstate_t *es, void *context)
140 {
141         ucontext_t *_uc;
142         mcontext_t *_mc;
143         int         i;
144
145         _uc = (ucontext_t *) context;
146         _mc = &_uc->uc_mcontext;
147
148         /* read special registers */
149         es->pc = (u1 *) _mc->sc_pc;
150         es->sp = (u1 *) _mc->sc_regs[REG_SP];
151         es->pv = (u1 *) _mc->sc_regs[REG_PV];
152         es->ra = (u1 *) _mc->sc_regs[REG_RA];
153
154         /* read integer registers */
155         for (i = 0; i < INT_REG_CNT; i++)
156                 es->intregs[i] = _mc->sc_regs[i];
157
158         /* read float registers */
159         /* Do not use the assignment operator '=', as the type of
160          * the _mc->sc_fpregs[i] can cause invalid conversions. */
161
162         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
163         os_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
164 }
165
166
167 /* md_executionstate_write *****************************************************
168
169    Write the given executionstate back to the context.
170
171 *******************************************************************************/
172
173 void md_executionstate_write(executionstate_t *es, void *context)
174 {
175         ucontext_t *_uc;
176         mcontext_t *_mc;
177         int         i;
178
179         _uc = (ucontext_t *) context;
180         _mc = &_uc->uc_mcontext;
181
182         /* write integer registers */
183         for (i = 0; i < INT_REG_CNT; i++)
184                 _mc->sc_regs[i] = es->intregs[i];
185
186         /* write float registers */
187         /* Do not use the assignment operator '=', as the type of
188          * the _mc->sc_fpregs[i] can cause invalid conversions. */
189
190         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
191         os_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
192
193         /* write special registers */
194         _mc->sc_pc           = (ptrint) es->pc;
195         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
196         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
197         _mc->sc_regs[REG_RA] = (ptrint) es->ra;
198 }
199
200
201 /*
202  * These are local overrides for various environment variables in Emacs.
203  * Please do not remove this and leave it at the end of the file, where
204  * Emacs will automagically detect them.
205  * ---------------------------------------------------------------------
206  * Local variables:
207  * mode: c
208  * indent-tabs-mode: t
209  * c-basic-offset: 4
210  * tab-width: 4
211  * End:
212  */