4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "cpu_loop-common.h"
24 #define SPARC64_STACK_BIAS 2047
28 /* WARNING: dealing with register windows _is_ complicated. More info
29 can be found at http://www.sics.se/~psm/sparcstack.html */
30 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
32 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
33 /* wrap handling : if cwp is on the last window, then we use the
34 registers 'after' the end */
35 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
36 index
+= 16 * env
->nwindows
;
40 /* save the register window 'cwp1' */
41 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
46 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
49 sp_ptr
+= SPARC64_STACK_BIAS
;
51 #if defined(DEBUG_WIN)
52 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
55 for(i
= 0; i
< 16; i
++) {
56 /* FIXME - what to do if put_user() fails? */
57 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
58 sp_ptr
+= sizeof(abi_ulong
);
62 static void save_window(CPUSPARCState
*env
)
64 #ifndef TARGET_SPARC64
66 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
67 ((1LL << env
->nwindows
) - 1);
68 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
71 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
77 static void restore_window(CPUSPARCState
*env
)
79 #ifndef TARGET_SPARC64
85 #ifndef TARGET_SPARC64
86 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
87 ((1LL << env
->nwindows
) - 1);
90 /* restore the invalid window */
91 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
92 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
95 sp_ptr
+= SPARC64_STACK_BIAS
;
97 #if defined(DEBUG_WIN)
98 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
101 for(i
= 0; i
< 16; i
++) {
102 /* FIXME - what to do if get_user() fails? */
103 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
104 sp_ptr
+= sizeof(abi_ulong
);
106 #ifdef TARGET_SPARC64
108 if (env
->cleanwin
< env
->nwindows
- 1)
116 static void flush_windows(CPUSPARCState
*env
)
122 /* if restore would invoke restore_window(), then we can stop */
123 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
124 #ifndef TARGET_SPARC64
125 if (env
->wim
& (1 << cwp1
))
128 if (env
->canrestore
== 0)
133 save_window_offset(env
, cwp1
);
136 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
137 #ifndef TARGET_SPARC64
138 /* set wim so that restore will reload the registers */
139 env
->wim
= 1 << cwp1
;
141 #if defined(DEBUG_WIN)
142 printf("flush_windows: nb=%d\n", offset
- 1);
146 void cpu_loop (CPUSPARCState
*env
)
148 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
151 target_siginfo_t info
;
155 trapnr
= cpu_exec(cs
);
157 process_queued_cpu_work(cs
);
159 /* Compute PSR before exposing state. */
160 if (env
->cc_op
!= CC_OP_FLAGS
) {
165 #ifndef TARGET_SPARC64
172 ret
= do_syscall (env
, env
->gregs
[1],
173 env
->regwptr
[0], env
->regwptr
[1],
174 env
->regwptr
[2], env
->regwptr
[3],
175 env
->regwptr
[4], env
->regwptr
[5],
177 if (ret
== -TARGET_ERESTARTSYS
|| ret
== -TARGET_QEMU_ESIGRETURN
) {
180 if ((abi_ulong
)ret
>= (abi_ulong
)(-515)) {
181 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
182 env
->xcc
|= PSR_CARRY
;
184 env
->psr
|= PSR_CARRY
;
188 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
189 env
->xcc
&= ~PSR_CARRY
;
191 env
->psr
&= ~PSR_CARRY
;
194 env
->regwptr
[0] = ret
;
195 /* next instruction */
197 env
->npc
= env
->npc
+ 4;
199 case 0x83: /* flush windows */
204 /* next instruction */
206 env
->npc
= env
->npc
+ 4;
208 #ifndef TARGET_SPARC64
209 case TT_WIN_OVF
: /* window overflow */
212 case TT_WIN_UNF
: /* window underflow */
218 info
.si_signo
= TARGET_SIGSEGV
;
220 /* XXX: check env->error_code */
221 info
.si_code
= TARGET_SEGV_MAPERR
;
222 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
223 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
227 case TT_SPILL
: /* window overflow */
230 case TT_FILL
: /* window underflow */
236 info
.si_signo
= TARGET_SIGSEGV
;
238 /* XXX: check env->error_code */
239 info
.si_code
= TARGET_SEGV_MAPERR
;
240 if (trapnr
== TT_DFAULT
)
241 info
._sifields
._sigfault
._addr
= env
->dmmu
.mmuregs
[4];
243 info
._sifields
._sigfault
._addr
= cpu_tsptr(env
)->tpc
;
244 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
250 sparc64_get_context(env
);
254 sparc64_set_context(env
);
259 /* just indicate that signals should be handled asap */
263 info
.si_signo
= TARGET_SIGILL
;
265 info
.si_code
= TARGET_ILL_ILLOPC
;
266 info
._sifields
._sigfault
._addr
= env
->pc
;
267 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
271 info
.si_signo
= TARGET_SIGTRAP
;
273 info
.si_code
= TARGET_TRAP_BRKPT
;
274 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
277 cpu_exec_step_atomic(cs
);
280 fprintf(stderr
, "Unhandled trap: 0x%x\n", trapnr
);
281 cpu_dump_state(cs
, stderr
, fprintf
, 0);
284 process_pending_signals (env
);
288 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
292 env
->npc
= regs
->npc
;
294 for(i
= 0; i
< 8; i
++)
295 env
->gregs
[i
] = regs
->u_regs
[i
];
296 for(i
= 0; i
< 8; i
++)
297 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];