5c7648744ec5615ca70fa4e133d0ac3126b37390
[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 7888 2007-05-09 08:36:16Z tbfg $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <ucontext.h>
34
35 #include "vm/types.h"
36
37 #include "vm/jit/powerpc64/codegen.h"
38 #include "vm/jit/powerpc64/linux/md-abi.h"
39
40 #if defined(ENABLE_THREADS)
41 # include "threads/native/threads.h"
42 #endif
43
44 #include "vm/exceptions.h"
45 #include "vm/signallocal.h"
46
47 #include "vm/jit/asmpart.h"
48
49 #if defined(ENABLE_PROFILING)
50 # include "vm/jit/optimizing/profile.h"
51 #endif
52
53 #include "vm/jit/stacktrace.h"
54
55
56 /* md_signal_handler_sigsegv ***************************************************
57  
58         Signal handler for hardware-exceptions.
59
60 *******************************************************************************/
61
62 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
63 {
64         ucontext_t      *_uc;
65         mcontext_t      *_mc;
66         u1              *pv;
67         u1              *sp;
68         u1              *ra;
69         u1              *xpc;
70         u4              mcode;
71         s4              s1;
72         s4              disp;
73         s4              d;
74         s4              type;
75         ptrint          addr;
76         ptrint          val;
77         java_objectheader *e;
78
79         _uc = (ucontext_t *) _p;
80         _mc = &(_uc->uc_mcontext);
81
82         /* get register values */
83         pv = (u1*) _mc->gp_regs[REG_PV];
84         sp = (u1*) _mc->gp_regs[REG_SP];
85         ra = (u1*) _mc->gp_regs[PT_LNK];                     /* correct for leafs */
86         xpc =(u1*) _mc->gp_regs[PT_NIP];
87
88         /* get the throwing instruction */
89         mcode = *((u4*)xpc);
90
91         s1   = M_INSTR_OP2_IMM_A(mcode);
92         disp = M_INSTR_OP2_IMM_I(mcode);
93         d    = M_INSTR_OP2_IMM_D(mcode);
94
95         val  = _mc->gp_regs[d];
96
97         if (s1 == REG_ZERO)     {
98                 /* we use the exception type as load displacement */
99                 type = disp;
100         } else  {
101                 /* normal NPE */
102                 addr = _mc->gp_regs[s1];
103                 type = (s4) addr;
104         }
105         e = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val);
106
107         _mc->gp_regs[REG_ITMP1]     = (ptrint) e;
108         _mc->gp_regs[REG_ITMP2_XPC] = (ptrint) xpc;
109         _mc->gp_regs[PT_NIP]        = (ptrint) asm_handle_exception;
110 }
111
112
113 /* md_signal_handler_sigusr2 ***************************************************
114
115    Signal handler for profiling sampling.
116
117 *******************************************************************************/
118
119 #if defined(ENABLE_THREADS)
120 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
121 {
122         threadobject *tobj;
123         ucontext_t   *_uc;
124         mcontext_t   *_mc;
125         u1           *pc;
126
127         tobj = THREADOBJECT;
128
129         _uc = (ucontext_t *) _p;
130         _mc = &(_uc->uc_mcontext);
131
132         pc = (u1 *) _mc->gp_regs[PT_NIP];
133
134         tobj->pc = pc;
135 }
136
137
138 void md_critical_section_restart(ucontext_t *_uc)
139 {
140         mcontext_t *_mc;
141         u1         *pc;
142         void       *critical;
143
144         _mc = &(_uc->uc_mcontext);
145
146         pc = (u1 *) _mc->gp_regs[PT_NIP];
147
148         critical = critical_find_restart_point(pc);
149
150         if (critical != NULL)   {
151                 log_println("md_critical_section_restart: pc=%p, npc=%p", pc, critical);
152                 _mc->gp_regs[PT_NIP] = (ptrint) critical;
153         }
154 }
155 #endif
156
157
158 /*
159  * These are local overrides for various environment variables in Emacs.
160  * Please do not remove this and leave it at the end of the file, where
161  * Emacs will automagically detect them.
162  * ---------------------------------------------------------------------
163  * Local variables:
164  * mode: c
165  * indent-tabs-mode: t
166  * c-basic-offset: 4
167  * tab-width: 4
168  * End:
169  */