Merged revisions 8245-8298 via svnmerge from
[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 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: md-os.c 8299 2007-08-13 08:41:18Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34 #include <ucontext.h>
35
36 #include "vm/types.h"
37
38 #include "vm/jit/alpha/codegen.h"
39 #include "vm/jit/alpha/md-abi.h"
40
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/threads.h"
43 #endif
44
45 #include "vm/signallocal.h"
46
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/stacktrace.h"
49
50
51 /* md_signal_handler_sigsegv ***************************************************
52
53    NullPointerException signal handler for hardware null pointer
54    check.
55
56 *******************************************************************************/
57
58 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
59 {
60         stackframeinfo  sfi;
61         ucontext_t     *_uc;
62         mcontext_t     *_mc;
63         u1             *pv;
64         u1             *sp;
65         u1             *ra;
66         u1             *xpc;
67         u4              mcode;
68         s4              d;
69         s4              s1;
70         s4              disp;
71         intptr_t        val;
72         intptr_t        addr;
73         int             type;
74         void           *p;
75
76         _uc = (ucontext_t *) _p;
77         _mc = &_uc->uc_mcontext;
78
79         pv  = (u1 *) _mc->sc_regs[REG_PV];
80         sp  = (u1 *) _mc->sc_regs[REG_SP];
81         ra  = (u1 *) _mc->sc_regs[REG_RA];           /* this is correct for leafs */
82         xpc = (u1 *) _mc->sc_pc;
83
84         /* get exception-throwing instruction */
85
86         mcode = *((u4 *) xpc);
87
88         d    = M_MEM_GET_A(mcode);
89         s1   = M_MEM_GET_B(mcode);
90         disp = M_MEM_GET_DISP(mcode);
91
92         val  = _mc->sc_regs[d];
93
94         /* check for special-load */
95
96         if (s1 == REG_ZERO) {
97                 /* we use the exception type as load displacement */
98
99                 type = disp;
100         }
101         else {
102                 /* This is a normal NPE: addr must be NULL and the NPE-type
103                    define is 0. */
104
105                 addr = _mc->sc_regs[s1];
106                 type = (int) addr;
107         }
108
109         /* create stackframeinfo */
110
111         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
112
113         /* Handle the type. */
114
115         p = signal_handle(xpc, type, val);
116
117         /* remove stackframeinfo */
118
119         stacktrace_remove_stackframeinfo(&sfi);
120
121         /* set registers */
122
123         if (p != NULL) {
124                 _mc->sc_regs[REG_ITMP1_XPTR] = (intptr_t) p;
125                 _mc->sc_regs[REG_ITMP2_XPC]  = (intptr_t) xpc;
126                 _mc->sc_pc                   = (intptr_t) asm_handle_exception;
127         }
128 }
129
130
131 /* md_signal_handler_sigusr1 ***************************************************
132
133    Signal handler for suspending threads.
134
135 *******************************************************************************/
136
137 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
138 void md_signal_handler_sigusr1(int sig, siginfo_t *siginfo, void *_p)
139 {
140         ucontext_t *_uc;
141         mcontext_t *_mc;
142         u1         *pc;
143         u1         *sp;
144
145         _uc = (ucontext_t *) _p;
146         _mc = &_uc->uc_mcontext;
147
148         /* get the PC and SP for this thread */
149         pc = (u1 *) _mc->sc_pc;
150         sp = (u1 *) _mc->sc_regs[REG_SP];
151
152         /* now suspend the current thread */
153         threads_suspend_ack(pc, sp);
154 }
155 #endif
156
157
158 /* md_signal_handler_sigusr2 ***************************************************
159
160    Signal handler for profiling sampling.
161
162 *******************************************************************************/
163
164 #if defined(ENABLE_THREADS)
165 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
166 {
167         threadobject *tobj;
168         ucontext_t   *_uc;
169         mcontext_t   *_mc;
170         u1           *pc;
171
172         tobj = THREADOBJECT;
173
174         _uc = (ucontext_t *) _p;
175         _mc = &_uc->uc_mcontext;
176
177         pc = (u1 *) _mc->sc_pc;
178
179         tobj->pc = pc;
180 }
181 #endif
182
183
184 /* md_replace_executionstate_read **********************************************
185
186    Read the given context into an executionstate for Replacement.
187
188 *******************************************************************************/
189
190 #if defined(ENABLE_REPLACEMENT)
191 void md_replace_executionstate_read(executionstate_t *es, void *context)
192 {
193         ucontext_t *_uc;
194         mcontext_t *_mc;
195         s4          i;
196
197         _uc = (ucontext_t *) context;
198         _mc = &_uc->uc_mcontext;
199
200         /* read special registers */
201         es->pc = (u1 *) _mc->sc_pc;
202         es->sp = (u1 *) _mc->sc_regs[REG_SP];
203         es->pv = (u1 *) _mc->sc_regs[REG_PV];
204
205         /* read integer registers */
206         for (i = 0; i < INT_REG_CNT; i++)
207                 es->intregs[i] = _mc->sc_regs[i];
208
209         /* read float registers */
210         for (i = 0; i < FLT_REG_CNT; i++)
211                 es->fltregs[i] = _mc->sc_fpregs[i];
212 }
213 #endif
214
215
216 /* md_replace_executionstate_write *********************************************
217
218    Write the given executionstate back to the context for Replacement.
219
220 *******************************************************************************/
221
222 #if defined(ENABLE_REPLACEMENT)
223 void md_replace_executionstate_write(executionstate_t *es, void *context)
224 {
225         ucontext_t *_uc;
226         mcontext_t *_mc;
227         s4          i;
228
229         _uc = (ucontext_t *) context;
230         _mc = &_uc->uc_mcontext;
231
232         /* write integer registers */
233         for (i = 0; i < INT_REG_CNT; i++)
234                 _mc->sc_regs[i] = es->intregs[i];
235
236         /* write float registers */
237         for (i = 0; i < FLT_REG_CNT; i++)
238                 _mc->sc_fpregs[i] = es->fltregs[i];
239
240         /* write special registers */
241         _mc->sc_pc = es->pc;
242         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
243         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
244 }
245 #endif
246
247
248 /* md_critical_section_restart *************************************************
249
250    Search the critical sections tree for a matching section and set
251    the PC to the restart point, if necessary.
252
253 *******************************************************************************/
254
255 #if defined(ENABLE_THREADS)
256 void md_critical_section_restart(ucontext_t *_uc)
257 {
258         mcontext_t *_mc;
259         u1         *pc;
260         u1         *npc;
261
262         _mc = &_uc->uc_mcontext;
263
264         pc = (u1 *) _mc->sc_pc;
265
266         npc = critical_find_restart_point(pc);
267
268         if (npc != NULL)
269                 _mc->sc_pc = (ptrint) npc;
270 }
271 #endif
272
273
274 /*
275  * These are local overrides for various environment variables in Emacs.
276  * Please do not remove this and leave it at the end of the file, where
277  * Emacs will automagically detect them.
278  * ---------------------------------------------------------------------
279  * Local variables:
280  * mode: c
281  * indent-tabs-mode: t
282  * c-basic-offset: 4
283  * tab-width: 4
284  * End:
285  */