* Removed all Id tags.
[cacao.git] / src / vm / jit / powerpc64 / linux / md-os.c
1 /* src/vm/jit/powerpc64/linux/md-os.c - machine dependent PowerPC64 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/powerpc64/codegen.h"
37 #include "vm/jit/powerpc64/linux/md-abi.h"
38
39 #if defined(ENABLE_THREADS)
40 # include "threads/native/threads.h"
41 #endif
42
43 #include "vm/exceptions.h"
44 #include "vm/signallocal.h"
45
46 #include "vm/jit/asmpart.h"
47
48 #if defined(ENABLE_PROFILING)
49 # include "vm/jit/optimizing/profile.h"
50 #endif
51
52 #include "vm/jit/stacktrace.h"
53
54
55 /* md_signal_handler_sigsegv ***************************************************
56  
57         Signal handler for hardware-exceptions.
58
59 *******************************************************************************/
60
61 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
62 {
63         stackframeinfo  sfi;
64         ucontext_t     *_uc;
65         mcontext_t     *_mc;
66         u1             *pv;
67         u1             *sp;
68         u1             *ra;
69         u1             *xpc;
70         u4              mcode;
71         int             s1;
72         int16_t         disp;
73         int             d;
74         int             type;
75         intptr_t        addr;
76         intptr_t        val;
77         void           *p;
78
79         _uc = (ucontext_t *) _p;
80         _mc = &(_uc->uc_mcontext);
81
82         /* get register values */
83
84         pv = (u1*) _mc->gp_regs[REG_PV];
85         sp = (u1*) _mc->gp_regs[REG_SP];
86         ra = (u1*) _mc->gp_regs[PT_LNK];                     /* correct for leafs */
87         xpc =(u1*) _mc->gp_regs[PT_NIP];
88
89         /* get the throwing instruction */
90
91         mcode = *((u4*)xpc);
92
93         s1   = M_INSTR_OP2_IMM_A(mcode);
94         disp = M_INSTR_OP2_IMM_I(mcode);
95         d    = M_INSTR_OP2_IMM_D(mcode);
96
97         val  = _mc->gp_regs[d];
98
99         if (s1 == REG_ZERO) {
100                 /* we use the exception type as load displacement */
101                 type = disp;
102         }
103         else {
104                 /* normal NPE */
105                 addr = _mc->gp_regs[s1];
106                 type = (s4) 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         _mc->gp_regs[REG_ITMP1]     = (intptr_t) p;
122         _mc->gp_regs[REG_ITMP2_XPC] = (intptr_t) xpc;
123         _mc->gp_regs[PT_NIP]        = (intptr_t) asm_handle_exception;
124 }
125
126
127 /* md_signal_handler_sigusr2 ***************************************************
128
129    Signal handler for profiling sampling.
130
131 *******************************************************************************/
132
133 #if defined(ENABLE_THREADS)
134 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
135 {
136         threadobject *tobj;
137         ucontext_t   *_uc;
138         mcontext_t   *_mc;
139         u1           *pc;
140
141         tobj = THREADOBJECT;
142
143         _uc = (ucontext_t *) _p;
144         _mc = &(_uc->uc_mcontext);
145
146         pc = (u1 *) _mc->gp_regs[PT_NIP];
147
148         tobj->pc = pc;
149 }
150 #endif
151
152
153 /* md_critical_section_restart *************************************************
154
155    Search the critical sections tree for a matching section and set
156    the PC to the restart point, if necessary.
157
158 *******************************************************************************/
159
160 #if defined(ENABLE_THREADS)
161 void md_critical_section_restart(ucontext_t *_uc)
162 {
163         mcontext_t *_mc;
164         u1         *pc;
165         u1         *npc;
166
167         _mc = &(_uc->uc_mcontext);
168
169         pc = (u1 *) _mc->gp_regs[PT_NIP];
170
171         npc = critical_find_restart_point(pc);
172
173         if (npc != NULL)
174                 _mc->gp_regs[PT_NIP] = (ptrint) npc;
175 }
176 #endif
177
178
179 /*
180  * These are local overrides for various environment variables in Emacs.
181  * Please do not remove this and leave it at the end of the file, where
182  * Emacs will automagically detect them.
183  * ---------------------------------------------------------------------
184  * Local variables:
185  * mode: c
186  * indent-tabs-mode: t
187  * c-basic-offset: 4
188  * tab-width: 4
189  * End:
190  */