Merged revisions 8137-8178 via svnmerge from
[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 8178 2007-07-05 11:13:20Z michi $
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         stackframeinfo   sfi;
65         ucontext_t      *_uc;
66         mcontext_t      *_mc;
67         u1              *pv;
68         u1              *sp;
69         u1              *ra;
70         u1              *xpc;
71         u4              mcode;
72         s4              s1;
73         s4              disp;
74         s4              d;
75         s4              type;
76         ptrint          addr;
77         ptrint          val;
78         java_objectheader *e;
79
80         _uc = (ucontext_t *) _p;
81         _mc = &(_uc->uc_mcontext);
82
83         /* get register values */
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         mcode = *((u4*)xpc);
91
92         s1   = M_INSTR_OP2_IMM_A(mcode);
93         disp = M_INSTR_OP2_IMM_I(mcode);
94         d    = M_INSTR_OP2_IMM_D(mcode);
95
96         val  = _mc->gp_regs[d];
97
98         if (s1 == REG_ZERO)     {
99                 /* we use the exception type as load displacement */
100                 type = disp;
101         } else  {
102                 /* normal NPE */
103                 addr = _mc->gp_regs[s1];
104                 type = (s4) addr;
105         }
106         e = exceptions_new_hardware_exception(pv, sp, ra, xpc, type, val, &sfi);
107
108         _mc->gp_regs[REG_ITMP1]     = (ptrint) e;
109         _mc->gp_regs[REG_ITMP2_XPC] = (ptrint) xpc;
110         _mc->gp_regs[PT_NIP]        = (ptrint) asm_handle_exception;
111 }
112
113
114 /* md_signal_handler_sigusr2 ***************************************************
115
116    Signal handler for profiling sampling.
117
118 *******************************************************************************/
119
120 #if defined(ENABLE_THREADS)
121 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
122 {
123         threadobject *tobj;
124         ucontext_t   *_uc;
125         mcontext_t   *_mc;
126         u1           *pc;
127
128         tobj = THREADOBJECT;
129
130         _uc = (ucontext_t *) _p;
131         _mc = &(_uc->uc_mcontext);
132
133         pc = (u1 *) _mc->gp_regs[PT_NIP];
134
135         tobj->pc = pc;
136 }
137 #endif
138
139
140 /* md_critical_section_restart *************************************************
141
142    Search the critical sections tree for a matching section and set
143    the PC to the restart point, if necessary.
144
145 *******************************************************************************/
146
147 #if defined(ENABLE_THREADS)
148 void md_critical_section_restart(ucontext_t *_uc)
149 {
150         mcontext_t *_mc;
151         u1         *pc;
152         u1         *npc;
153
154         _mc = &(_uc->uc_mcontext);
155
156         pc = (u1 *) _mc->gp_regs[PT_NIP];
157
158         npc = critical_find_restart_point(pc);
159
160         if (npc != NULL)
161                 _mc->gp_regs[PT_NIP] = (ptrint) npc;
162 }
163 #endif
164
165
166 /*
167  * These are local overrides for various environment variables in Emacs.
168  * Please do not remove this and leave it at the end of the file, where
169  * Emacs will automagically detect them.
170  * ---------------------------------------------------------------------
171  * Local variables:
172  * mode: c
173  * indent-tabs-mode: t
174  * c-basic-offset: 4
175  * tab-width: 4
176  * End:
177  */