* Removed all Id tags.
[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 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <ucontext.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/alpha/codegen.h"
37 #include "vm/jit/alpha/md-abi.h"
38
39 #if defined(ENABLE_THREADS)
40 # include "threads/native/threads.h"
41 #endif
42
43 #include "vm/signallocal.h"
44
45 #include "vm/jit/asmpart.h"
46 #include "vm/jit/stacktrace.h"
47
48
49 /* md_signal_handler_sigsegv ***************************************************
50
51    NullPointerException signal handler for hardware null pointer
52    check.
53
54 *******************************************************************************/
55
56 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
57 {
58         stackframeinfo  sfi;
59         ucontext_t     *_uc;
60         mcontext_t     *_mc;
61         u1             *pv;
62         u1             *sp;
63         u1             *ra;
64         u1             *xpc;
65         u4              mcode;
66         s4              d;
67         s4              s1;
68         s4              disp;
69         intptr_t        val;
70         intptr_t        addr;
71         int             type;
72         void           *p;
73
74         _uc = (ucontext_t *) _p;
75         _mc = &_uc->uc_mcontext;
76
77         pv  = (u1 *) _mc->sc_regs[REG_PV];
78         sp  = (u1 *) _mc->sc_regs[REG_SP];
79         ra  = (u1 *) _mc->sc_regs[REG_RA];           /* this is correct for leafs */
80         xpc = (u1 *) _mc->sc_pc;
81
82         /* get exception-throwing instruction */
83
84         mcode = *((u4 *) xpc);
85
86         d    = M_MEM_GET_A(mcode);
87         s1   = M_MEM_GET_B(mcode);
88         disp = M_MEM_GET_DISP(mcode);
89
90         val  = _mc->sc_regs[d];
91
92         /* check for special-load */
93
94         if (s1 == REG_ZERO) {
95                 /* we use the exception type as load displacement */
96
97                 type = disp;
98         }
99         else {
100                 /* This is a normal NPE: addr must be NULL and the NPE-type
101                    define is 0. */
102
103                 addr = _mc->sc_regs[s1];
104                 type = (int) addr;
105         }
106
107         /* create stackframeinfo */
108
109         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
110
111         /* Handle the type. */
112
113         p = signal_handle(xpc, type, val);
114
115         /* remove stackframeinfo */
116
117         stacktrace_remove_stackframeinfo(&sfi);
118
119         /* set registers */
120
121         if (p != NULL) {
122                 _mc->sc_regs[REG_ITMP1_XPTR] = (intptr_t) p;
123                 _mc->sc_regs[REG_ITMP2_XPC]  = (intptr_t) xpc;
124                 _mc->sc_pc                   = (intptr_t) asm_handle_exception;
125         }
126 }
127
128
129 /* md_signal_handler_sigusr2 ***************************************************
130
131    Signal handler for profiling sampling.
132
133 *******************************************************************************/
134
135 #if defined(ENABLE_THREADS)
136 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
137 {
138         threadobject *tobj;
139         ucontext_t   *_uc;
140         mcontext_t   *_mc;
141         u1           *pc;
142
143         tobj = THREADOBJECT;
144
145         _uc = (ucontext_t *) _p;
146         _mc = &_uc->uc_mcontext;
147
148         pc = (u1 *) _mc->sc_pc;
149
150         tobj->pc = pc;
151 }
152 #endif
153
154
155 /* md_replace_executionstate_read **********************************************
156
157    Read the given context into an executionstate for Replacement.
158
159 *******************************************************************************/
160
161 #if defined(ENABLE_REPLACEMENT)
162 void md_replace_executionstate_read(executionstate_t *es, void *context)
163 {
164         ucontext_t *_uc;
165         mcontext_t *_mc;
166         s4          i;
167
168         _uc = (ucontext_t *) context;
169         _mc = &_uc->uc_mcontext;
170
171         /* read special registers */
172         es->pc = (u1 *) _mc->sc_pc;
173         es->sp = (u1 *) _mc->sc_regs[REG_SP];
174         es->pv = (u1 *) _mc->sc_regs[REG_PV];
175
176         /* read integer registers */
177         for (i = 0; i < INT_REG_CNT; i++)
178                 es->intregs[i] = _mc->sc_regs[i];
179
180         /* read float registers */
181         for (i = 0; i < FLT_REG_CNT; i++)
182                 es->fltregs[i] = _mc->sc_fpregs[i];
183 }
184 #endif
185
186
187 /* md_replace_executionstate_write *********************************************
188
189    Write the given executionstate back to the context for Replacement.
190
191 *******************************************************************************/
192
193 #if defined(ENABLE_REPLACEMENT)
194 void md_replace_executionstate_write(executionstate_t *es, void *context)
195 {
196         ucontext_t *_uc;
197         mcontext_t *_mc;
198         s4          i;
199
200         _uc = (ucontext_t *) context;
201         _mc = &_uc->uc_mcontext;
202
203         /* write integer registers */
204         for (i = 0; i < INT_REG_CNT; i++)
205                 _mc->sc_regs[i] = es->intregs[i];
206
207         /* write float registers */
208         for (i = 0; i < FLT_REG_CNT; i++)
209                 _mc->sc_fpregs[i] = es->fltregs[i];
210
211         /* write special registers */
212         _mc->sc_pc = es->pc;
213         _mc->sc_regs[REG_SP] = (ptrint) es->sp;
214         _mc->sc_regs[REG_PV] = (ptrint) es->pv;
215 }
216 #endif
217
218
219 /* md_critical_section_restart *************************************************
220
221    Search the critical sections tree for a matching section and set
222    the PC to the restart point, if necessary.
223
224 *******************************************************************************/
225
226 #if defined(ENABLE_THREADS)
227 void md_critical_section_restart(ucontext_t *_uc)
228 {
229         mcontext_t *_mc;
230         u1         *pc;
231         u1         *npc;
232
233         _mc = &_uc->uc_mcontext;
234
235         pc = (u1 *) _mc->sc_pc;
236
237         npc = critical_find_restart_point(pc);
238
239         if (npc != NULL)
240                 _mc->sc_pc = (ptrint) npc;
241 }
242 #endif
243
244
245 /*
246  * These are local overrides for various environment variables in Emacs.
247  * Please do not remove this and leave it at the end of the file, where
248  * Emacs will automagically detect them.
249  * ---------------------------------------------------------------------
250  * Local variables:
251  * mode: c
252  * indent-tabs-mode: t
253  * c-basic-offset: 4
254  * tab-width: 4
255  * End:
256  */