c90f8d8bea1c947be76cf63d39f6929fecb51b9d
[cacao.git] / src / vm / jit / powerpc / darwin / md-os.c
1 /* src/vm/jit/powerpc/darwin/md-os.c - machine dependent PowerPC Darwin functions
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: md-os.c 6123 2006-12-05 21:10:54Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <signal.h>
40 #include <ucontext.h>
41
42 #include "vm/types.h"
43
44 #include "vm/jit/powerpc/darwin/md-abi.h"
45
46 #include "vm/exceptions.h"
47 #include "vm/global.h"
48 #include "vm/signallocal.h"
49 #include "vm/stringlocal.h"
50 #include "vm/jit/asmpart.h"
51 #include "vm/jit/stacktrace.h"
52
53
54 /* md_signal_handler_sigsegv ***************************************************
55
56    NullPointerException signal handler for hardware null pointer
57    check.
58
59 *******************************************************************************/
60
61 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
62 {
63         ucontext_t         *_uc;
64         mcontext_t          _mc;
65         ppc_thread_state_t *_ss;
66         ptrint             *gregs;
67         u4                  instr;
68         s4                  reg;
69         s4                  disp;
70         ptrint              addr;
71         u1                 *pv;
72         u1                 *sp;
73         u1                 *ra;
74         u1                 *xpc;
75         stackframeinfo      sfi;
76         java_objectheader  *o;
77
78         _uc = (ucontext_t *) _p;
79         _mc = _uc->uc_mcontext;
80         _ss = &_mc->ss;
81
82         /* check for NullPointerException */
83
84         gregs = &_ss->r0;
85
86         instr = *((u4 *) _ss->srr0);
87         reg   = (instr >> 16) & 31;
88         disp  = (instr & 0xffff);
89         addr  = gregs[reg];
90
91         pv  = (u1 *) _ss->r13;
92         sp  = (u1 *) _ss->r1;
93         ra  = (u1 *) _ss->lr;                    /* this is correct for leafs */
94         xpc = (u1 *) _ss->srr0;
95
96         /* create stackframeinfo */
97
98         stacktrace_create_extern_stackframeinfo(&sfi, pv, sp, ra, xpc);
99
100         if (reg == REG_ZERO) {
101                 switch (disp) {
102                 case EXCEPTION_LOAD_DISP_ARITHMETIC:
103                         vm_abort("ArithmeticException");
104                         break;
105                 case EXCEPTION_LOAD_DISP_ARRAYINDEXOUTOFBOUNDS:
106                         log_println("ArrayIndexOutOfBoundsException");
107                         o = new_arrayindexoutofboundsexception(0);
108                         break;
109                 case EXCEPTION_LOAD_DISP_CLASSCAST:
110                         vm_abort("ClassCastException");
111                         break;
112                 default:
113                         vm_abort("unknown exception %d", disp);
114                 }
115         }
116         else if (addr == 0) {
117                 o = exceptions_new_nullpointerexception();
118         }
119         else {
120                 codegen_get_pv_from_pc(xpc);
121
122                 /* this should not happen */
123
124                 assert(0);
125         }
126
127         /* remove stackframeinfo */
128
129         stacktrace_remove_stackframeinfo(&sfi);
130
131         _ss->r11  = (ptrint) o;
132         _ss->r12  = (ptrint) xpc;
133         _ss->srr0 = (ptrint) asm_handle_exception;
134 }
135
136
137 /* md_signal_handler_sigusr2 ***************************************************
138
139    Signal handler for profiling sampling.
140
141 *******************************************************************************/
142
143 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
144 {
145         threadobject       *t;
146         ucontext_t         *_uc;
147         mcontext_t          _mc;
148         ppc_thread_state_t *_ss;
149         u1                 *pc;
150
151         t = THREADOBJECT;
152
153         _uc = (ucontext_t *) _p;
154         _mc = _uc->uc_mcontext;
155         _ss = &_mc->ss;
156
157         pc = (u1 *) _ss->srr0;
158
159         t->pc = pc;
160 }
161
162
163 #if defined(ENABLE_THREADS)
164 void thread_restartcriticalsection(ucontext_t *_uc)
165 {
166         mcontext_t          _mc;
167         ppc_thread_state_t *_ss;
168         void               *critical;
169
170         _mc = _uc->uc_mcontext;
171         _ss = &_mc->ss;
172
173         critical = critical_find_restart_point((void *) _ss->srr0);
174
175         if (critical)
176                 _ss->srr0 = (ptrint) critical;
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  */