* src/threads/posix/thread-posix.cpp: Implemented thread suspension mechanism.
[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_sigusr2 ***************************************************
81
82    Signal handler for profiling sampling.
83
84 *******************************************************************************/
85
86 #if defined(ENABLE_THREADS)
87 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
88 {
89         threadobject *tobj;
90         ucontext_t   *_uc;
91         mcontext_t   *_mc;
92         u1           *pc;
93
94         tobj = THREADOBJECT;
95
96         _uc = (ucontext_t *) _p;
97         _mc = &_uc->uc_mcontext;
98
99         pc = (u1 *) _mc->sc_pc;
100
101         tobj->pc = pc;
102 }
103 #endif
104
105
106 /* md_executionstate_read ******************************************************
107
108    Read the given context into an executionstate.
109
110 *******************************************************************************/
111
112 void md_executionstate_read(executionstate_t *es, void *context)
113 {
114         ucontext_t *_uc;
115         mcontext_t *_mc;
116         int         i;
117
118         _uc = (ucontext_t *) context;
119         _mc = &_uc->uc_mcontext;
120
121         /* read special registers */
122         es->pc = (u1 *) _mc->sc_pc;
123         es->sp = (u1 *) _mc->sc_regs[REG_SP];
124         es->pv = (u1 *) _mc->sc_regs[REG_PV];
125         es->ra = (u1 *) _mc->sc_regs[REG_RA];
126
127         /* read integer registers */
128         for (i = 0; i < INT_REG_CNT; i++)
129                 es->intregs[i] = _mc->sc_regs[i];
130
131         /* read float registers */
132         /* Do not use the assignment operator '=', as the type of
133          * the _mc->sc_fpregs[i] can cause invalid conversions. */
134
135         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
136         os_memcpy(&es->fltregs, &_mc->sc_fpregs, sizeof(_mc->sc_fpregs));
137 }
138
139
140 /* md_executionstate_write *****************************************************
141
142    Write the given executionstate back to the context.
143
144 *******************************************************************************/
145
146 void md_executionstate_write(executionstate_t *es, void *context)
147 {
148         ucontext_t *_uc;
149         mcontext_t *_mc;
150         int         i;
151
152         _uc = (ucontext_t *) context;
153         _mc = &_uc->uc_mcontext;
154
155         /* write integer registers */
156         for (i = 0; i < INT_REG_CNT; i++)
157                 _mc->sc_regs[i] = es->intregs[i];
158
159         /* write float registers */
160         /* Do not use the assignment operator '=', as the type of
161          * the _mc->sc_fpregs[i] can cause invalid conversions. */
162
163         assert(sizeof(_mc->sc_fpregs) == sizeof(es->fltregs));
164         os_memcpy(&_mc->sc_fpregs, &es->fltregs, sizeof(_mc->sc_fpregs));
165
166         /* write special registers */
167         _mc->sc_pc           = (ptrint) es->pc;
168         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
169         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
170         _mc->sc_regs[REG_RA] = (ptrint) es->ra;
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  */