* Implemented stacktraces
[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 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: md-os.c 2962 2005-07-09 18:08:06Z twisti $
32
33 */
34
35
36 #include <signal.h>
37 #include <ucontext.h>
38
39 #include "config.h"
40
41 #include "vm/jit/powerpc/types.h"
42 #include "vm/jit/powerpc/darwin/md-abi.h"
43
44 #include "vm/exceptions.h"
45 #include "vm/global.h"
46 #include "vm/stringlocal.h"
47 #include "vm/jit/asmpart.h"
48
49
50 #if 0
51 /* cacao_catch_Handler *********************************************************
52
53    XXX
54
55 *******************************************************************************/
56
57 int cacao_catch_Handler(mach_port_t thread)
58 {
59 #if defined(USE_THREADS)
60         unsigned int *regs;
61         unsigned int crashpc;
62         s4 instr, reg;
63 /*      java_objectheader *xptr; */
64
65         /* Mach stuff */
66         thread_state_flavor_t flavor = PPC_THREAD_STATE;
67         mach_msg_type_number_t thread_state_count = PPC_THREAD_STATE_COUNT;
68         ppc_thread_state_t thread_state;
69         kern_return_t r;
70         
71         if (checknull)
72                 return 0;
73
74         r = thread_get_state(thread, flavor,
75                 (natural_t*)&thread_state, &thread_state_count);
76         if (r != KERN_SUCCESS) {
77                 log_text("thread_get_state failed");
78                 assert(0);
79         }
80
81         regs = &thread_state.r0;
82         crashpc = thread_state.srr0;
83
84         instr = *(s4*) crashpc;
85         reg = (instr >> 16) & 31;
86
87         if (!regs[reg]) {
88 /*      This is now handled in asmpart because it needs to run in the throwing
89  *      thread */
90 /*              xptr = new_nullpointerexception(); */
91
92                 regs[REG_ITMP2_XPC] = crashpc;
93 /*              regs[REG_ITMP1_XPTR] = (u4) xptr; */
94                 thread_state.srr0 = (u4) asm_handle_nullptr_exception;
95
96                 r = thread_set_state(thread, flavor,
97                         (natural_t*)&thread_state, thread_state_count);
98                 if (r != KERN_SUCCESS) {
99                         log_text("thread_set_state failed");
100                         assert(0);
101                 }
102
103                 return 1;
104         }
105
106         throw_cacao_exception_exit(string_java_lang_InternalError,
107                                    "Segmentation fault at %p", regs[reg]);
108 #endif
109
110         return 0;
111 }
112 #endif
113
114
115 /* signal_handler_sigsegv ******************************************************
116
117    NullPointerException signal handler for hardware null pointer check.
118
119 *******************************************************************************/
120
121 void signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
122 {
123         ucontext_t         *_uc;
124         mcontext_t          _mc;
125         ppc_thread_state_t *_ss;
126         ptrint             *gregs;
127         u4                  instr;
128         s4                  reg;
129         ptrint              addr;
130         stackframeinfo     *sfi;
131         u1                 *pv;
132         u1                 *sp;
133         functionptr         ra;
134
135         _uc = (ucontext_t *) _p;
136         _mc = _uc->uc_mcontext;
137         _ss = &_mc->ss;
138
139         /* check for NullPointerException */
140
141         gregs = &_ss->r0;
142
143         instr = *((u4 *) _ss->srr0);
144         reg = (instr >> 16) & 31;
145         addr = gregs[reg];
146
147         if (addr == 0) {
148                 /* allocate stackframeinfo on heap */
149
150                 sfi = NEW(stackframeinfo);
151
152                 /* create exception */
153
154                 pv = (u1 *) _ss->r13;
155                 sp = (u1 *) _ss->r1;
156                 ra = (functionptr) _ss->srr0;
157
158                 stacktrace_create_inline_stackframeinfo(sfi, pv, sp, ra);
159
160                 _ss->r11 = (ptrint) new_nullpointerexception();
161
162                 stacktrace_remove_stackframeinfo(sfi);
163
164                 FREE(sfi, stackframeinfo);
165
166                 /* set the REG_ITMP1_XPTR, REG_ITMP2_XPC and new PC */
167
168                 _ss->r12 = _ss->srr0;
169                 _ss->srr0 = (ptrint) asm_handle_exception;
170
171         } else {
172                 throw_cacao_exception_exit(string_java_lang_InternalError,
173                                            "Segmentation fault: 0x%08lx at 0x%08lx",
174                                            addr, _ss->srr0);
175         }
176 }
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  */