Added some missing vim modelines.
[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, 2007, 2008, 2009
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/powerpc/codegen.h"
36 #include "vm/jit/powerpc/darwin/md-abi.h"
37
38 #include "threads/thread.hpp"
39
40 #include "vm/signallocal.hpp"
41
42 #include "vm/jit/trap.hpp"
43
44 #if !__DARWIN_UNIX03
45 #define __srr0 srr0
46 #define __r0   r0
47 #define __r1   r1
48 #define __r13  r13
49 #define __lr   lr
50 #define __ss   ss
51 #endif
52
53 /**
54  * Signal handler for hardware-exceptions.
55  */
56 void md_signal_handler_sigsegv(int sig, siginfo_t *siginfo, void *_p)
57 {
58         ucontext_t*         _uc = (ucontext_t *) _p;
59         mcontext_t          _mc = _uc->uc_mcontext;
60         ppc_thread_state_t* _ss = &(_mc->__ss);
61
62         void* xpc = (void*) _ss->__srr0;
63
64         // Handle the trap.
65         trap_handle(TRAP_SIGSEGV, xpc, _p);
66 }
67
68
69 /**
70  * Signal handler for hardware-traps.
71  */
72 void md_signal_handler_sigtrap(int sig, siginfo_t *siginfo, void *_p)
73 {
74         ucontext_t*         _uc = (ucontext_t *) _p;
75         mcontext_t          _mc = _uc->uc_mcontext;
76         ppc_thread_state_t* _ss = &(_mc->__ss);
77
78         void* xpc = (void*) _ss->__srr0;
79
80         // Handle the trap.
81         trap_handle(TRAP_SIGTRAP, xpc, _p);
82 }
83
84
85 /**
86  * Signal handler for hardware-patchers.
87  */
88 void md_signal_handler_sigill(int sig, siginfo_t *siginfo, void *_p)
89 {
90         ucontext_t*         _uc = (ucontext_t *) _p;
91         mcontext_t          _mc = _uc->uc_mcontext;
92         ppc_thread_state_t* _ss = &(_mc->__ss);
93
94         void* xpc = (void*) _ss->__srr0;
95
96         // Handle the trap.
97         trap_handle(TRAP_SIGILL, xpc, _p);
98 }
99
100
101 /* md_signal_handler_sigusr2 ***************************************************
102
103    Signal handler for profiling sampling.
104
105 *******************************************************************************/
106
107 void md_signal_handler_sigusr2(int sig, siginfo_t *siginfo, void *_p)
108 {
109         threadobject       *t;
110         ucontext_t         *_uc;
111         mcontext_t          _mc;
112         ppc_thread_state_t *_ss;
113         u1                 *pc;
114
115         t = THREADOBJECT;
116
117         _uc = (ucontext_t *) _p;
118         _mc = _uc->uc_mcontext;
119         _ss = &_mc->__ss;
120
121         pc = (u1 *) _ss->__srr0;
122
123         t->pc = pc;
124 }
125
126
127 /* md_executionstate_read ******************************************************
128
129    Read the given context into an executionstate.
130
131 *******************************************************************************/
132
133 void md_executionstate_read(executionstate_t *es, void *context)
134 {
135         ucontext_t*         _uc = (ucontext_t *) context;
136         mcontext_t          _mc = _uc->uc_mcontext;
137         ppc_thread_state_t* _ss = &(_mc->__ss);
138
139         /* read special registers */
140         es->pc = (uint8_t*) _ss->__srr0;
141         es->sp = (uint8_t*) _ss->__r1;
142         es->pv = (uint8_t*) _ss->__r13;
143         es->ra = (uint8_t*) _ss->__lr;
144
145         /* read integer registers */
146         unsigned int* regs = &(_ss->__r0);
147         for (int i = 0; i < INT_REG_CNT; i++)
148                 es->intregs[i] = regs[i];
149
150         /* read float registers */
151         for (int i = 0; i < FLT_REG_CNT; i++)
152                 es->fltregs[i] = 0xdeadbeefdeadbeefULL;
153 }
154
155
156 /* md_executionstate_write *****************************************************
157
158    Write the given executionstate back to the context.
159
160 *******************************************************************************/
161
162 void md_executionstate_write(executionstate_t *es, void *context)
163 {
164         ucontext_t*         _uc = (ucontext_t *) context;
165         mcontext_t          _mc = _uc->uc_mcontext;
166         ppc_thread_state_t* _ss = &(_mc->__ss);
167
168         /* write integer registers */
169         unsigned int* regs = &(_ss->__r0);
170         for (int i = 0; i < INT_REG_CNT; i++)
171                 regs[i] = es->intregs[i];
172
173         /* write special registers */
174         _ss->__srr0 = (intptr_t) es->pc;
175         _ss->__r1   = (intptr_t) es->sp;
176         _ss->__r13  = (intptr_t) es->pv;
177         _ss->__lr   = (intptr_t) es->ra;
178 }
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  */