98c2c2bbd0307b5d7732c47f4a66548472289c44
[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    $Id: md-os.c 8283 2007-08-09 15:10:05Z twisti $
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/powerpc64/codegen.h"
39 #include "vm/jit/powerpc64/linux/md-abi.h"
40
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/threads.h"
43 #endif
44
45 #include "vm/exceptions.h"
46 #include "vm/signallocal.h"
47
48 #include "vm/jit/asmpart.h"
49
50 #if defined(ENABLE_PROFILING)
51 # include "vm/jit/optimizing/profile.h"
52 #endif
53
54 #include "vm/jit/stacktrace.h"
55
56
57 /* md_signal_handler_sigsegv ***************************************************
58  
59         Signal handler for hardware-exceptions.
60
61 *******************************************************************************/
62
63 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
64 {
65         stackframeinfo  sfi;
66         ucontext_t     *_uc;
67         mcontext_t     *_mc;
68         u1             *pv;
69         u1             *sp;
70         u1             *ra;
71         u1             *xpc;
72         u4              mcode;
73         int             s1;
74         int16_t         disp;
75         int             d;
76         int             type;
77         intptr_t        addr;
78         intptr_t        val;
79         void           *p;
80
81         _uc = (ucontext_t *) _p;
82         _mc = &(_uc->uc_mcontext);
83
84         /* get register values */
85
86         pv = (u1*) _mc->gp_regs[REG_PV];
87         sp = (u1*) _mc->gp_regs[REG_SP];
88         ra = (u1*) _mc->gp_regs[PT_LNK];                     /* correct for leafs */
89         xpc =(u1*) _mc->gp_regs[PT_NIP];
90
91         /* get the throwing instruction */
92
93         mcode = *((u4*)xpc);
94
95         s1   = M_INSTR_OP2_IMM_A(mcode);
96         disp = M_INSTR_OP2_IMM_I(mcode);
97         d    = M_INSTR_OP2_IMM_D(mcode);
98
99         val  = _mc->gp_regs[d];
100
101         if (s1 == REG_ZERO) {
102                 /* we use the exception type as load displacement */
103                 type = disp;
104         }
105         else {
106                 /* normal NPE */
107                 addr = _mc->gp_regs[s1];
108                 type = (s4) addr;
109         }
110
111         /* create stackframeinfo */
112
113         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
114
115         /* Handle the type. */
116
117         p = signal_handle(xpc, type, val);
118
119         /* remove stackframeinfo */
120
121         stacktrace_remove_stackframeinfo(&sfi);
122
123         _mc->gp_regs[REG_ITMP1]     = (intptr_t) p;
124         _mc->gp_regs[REG_ITMP2_XPC] = (intptr_t) xpc;
125         _mc->gp_regs[PT_NIP]        = (intptr_t) asm_handle_exception;
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->gp_regs[PT_NIP];
149
150         tobj->pc = pc;
151 }
152 #endif
153
154
155 /* md_critical_section_restart *************************************************
156
157    Search the critical sections tree for a matching section and set
158    the PC to the restart point, if necessary.
159
160 *******************************************************************************/
161
162 #if defined(ENABLE_THREADS)
163 void md_critical_section_restart(ucontext_t *_uc)
164 {
165         mcontext_t *_mc;
166         u1         *pc;
167         u1         *npc;
168
169         _mc = &(_uc->uc_mcontext);
170
171         pc = (u1 *) _mc->gp_regs[PT_NIP];
172
173         npc = critical_find_restart_point(pc);
174
175         if (npc != NULL)
176                 _mc->gp_regs[PT_NIP] = (ptrint) npc;
177 }
178 #endif
179
180
181 /*
182  * These are local overrides for various environment variables in Emacs.
183  * Please do not remove this and leave it at the end of the file, where
184  * Emacs will automagically detect them.
185  * ---------------------------------------------------------------------
186  * Local variables:
187  * mode: c
188  * indent-tabs-mode: t
189  * c-basic-offset: 4
190  * tab-width: 4
191  * End:
192  */