merged volatile memory barriers
[cacao.git] / src / vm / jit / arm / linux / md-os.c
1 /* src/vm/jit/arm/linux/md-os.c - machine dependent ARM 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 <stdint.h>
30
31 #define ucontext broken_glibc_ucontext
32 #define ucontext_t broken_glibc_ucontext_t
33 #include <ucontext.h>
34 #undef ucontext
35 #undef ucontext_t
36
37 typedef struct ucontext {
38    unsigned long     uc_flags;
39    struct ucontext  *uc_link;
40    stack_t           uc_stack;
41    struct sigcontext uc_mcontext;
42    sigset_t          uc_sigmask;
43 } ucontext_t;
44
45 #define scontext_t struct sigcontext
46
47 #include "vm/types.h"
48
49 #include "vm/jit/arm/md.h"
50 #include "vm/jit/arm/md-abi.h"
51
52 #include "threads/thread.hpp"
53
54 #include "vm/signallocal.hpp"
55
56 #include "vm/jit/asmpart.h"
57 #include "vm/jit/executionstate.h"
58 #include "vm/jit/trap.hpp"
59
60
61 /**
62  * Signal handler for hardware exceptions.
63  */
64 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
65 {
66         ucontext_t* _uc = (ucontext_t*) _p;
67         scontext_t* _sc = &_uc->uc_mcontext;
68
69         /* ATTENTION: glibc included messed up kernel headers we needed a
70            workaround for the ucontext structure. */
71
72         void* xpc = (u1 *) _sc->arm_pc;
73
74         // Handle the trap.
75         trap_handle(TRAP_SIGSEGV, xpc, _p);
76 }
77
78
79 /**
80  * Illegal instruction signal handler for hardware exception checks.
81  */
82 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
83 {
84         ucontext_t* _uc = (ucontext_t*) _p;
85         scontext_t* _sc = &_uc->uc_mcontext;
86
87         /* ATTENTION: glibc included messed up kernel headers we needed a
88            workaround for the ucontext structure. */
89
90         void* xpc = (void*) _sc->arm_pc;
91
92         // Handle the trap.
93         trap_handle(TRAP_SIGILL, xpc, _p);
94 }
95
96
97 /* md_signal_handler_sigusr2 ***************************************************
98
99    Signal handler for profiling sampling.
100
101 *******************************************************************************/
102
103 #if defined(ENABLE_THREADS)
104 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
105 {
106         threadobject *thread;
107         ucontext_t   *_uc;
108         scontext_t   *_sc;
109         u1           *pc;
110
111         thread = THREADOBJECT;
112
113         _uc = (ucontext_t*) _p;
114         _sc = &_uc->uc_mcontext;
115
116         pc = (u1 *) _sc->arm_pc;
117
118         thread->pc = pc;
119 }
120 #endif
121
122
123 /**
124  * Read the given context into an executionstate.
125  *
126  * @param es      execution state
127  * @param context machine context
128  */
129 void md_executionstate_read(executionstate_t *es, void *context)
130 {
131         ucontext_t *_uc;
132         scontext_t *_sc;
133         int         i;
134
135         _uc = (ucontext_t *) context;
136         _sc = &_uc->uc_mcontext;
137
138         /* ATTENTION: glibc included messed up kernel headers we needed a
139            workaround for the ucontext structure. */
140
141         /* read special registers */
142
143         es->pc = (u1 *) _sc->arm_pc;
144         es->sp = (u1 *) _sc->arm_sp;
145         es->pv = (u1 *) _sc->arm_ip;
146         es->ra = (u1 *) _sc->arm_lr;
147
148         /* read integer registers */
149
150         for (i = 0; i < INT_REG_CNT; i++)
151                 es->intregs[i] = *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i);
152
153         /* read float registers */
154
155         for (i = 0; i < FLT_REG_CNT; i++)
156                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
157 }
158
159
160 /**
161  * Write the given executionstate back to the context.
162  *
163  * @param es      execution state
164  * @param context machine context
165  */
166 void md_executionstate_write(executionstate_t *es, void *context)
167 {
168         ucontext_t *_uc;
169         scontext_t *_sc;
170         int         i;
171
172         _uc = (ucontext_t *) context;
173         _sc = &_uc->uc_mcontext;
174
175         /* ATTENTION: glibc included messed up kernel headers we needed a
176            workaround for the ucontext structure. */
177
178         /* write integer registers */
179
180         for (i = 0; i < INT_REG_CNT; i++)
181                 *((int32_t*) _sc + OFFSET(scontext_t, arm_r0)/4 + i) = es->intregs[i];
182
183         /* write special registers */
184
185         _sc->arm_pc = (ptrint) es->pc;
186         _sc->arm_sp = (ptrint) es->sp;
187         _sc->arm_ip = (ptrint) es->pv;
188         _sc->arm_lr = (ptrint) es->ra;
189 }
190
191
192 /*
193  * These are local overrides for various environment variables in Emacs.
194  * Please do not remove this and leave it at the end of the file, where
195  * Emacs will automagically detect them.
196  * ---------------------------------------------------------------------
197  * Local variables:
198  * mode: c
199  * indent-tabs-mode: t
200  * c-basic-offset: 4
201  * tab-width: 4
202  * End:
203  */
204