4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
23 #include "qemu/host-utils.h"
24 #include "exec/helper-proto.h"
25 #include "sysemu/sysemu.h"
27 void cpu_raise_exception_ra(CPUSPARCState
*env
, int tt
, uintptr_t ra
)
29 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
31 cs
->exception_index
= tt
;
32 cpu_loop_exit_restore(cs
, ra
);
35 void helper_raise_exception(CPUSPARCState
*env
, int tt
)
37 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
39 cs
->exception_index
= tt
;
43 void helper_debug(CPUSPARCState
*env
)
45 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
47 cs
->exception_index
= EXCP_DEBUG
;
52 target_ulong
helper_popc(target_ulong val
)
57 void helper_tick_set_count(void *opaque
, uint64_t count
)
59 #if !defined(CONFIG_USER_ONLY)
60 cpu_tick_set_count(opaque
, count
);
64 uint64_t helper_tick_get_count(CPUSPARCState
*env
, void *opaque
, int mem_idx
)
66 #if !defined(CONFIG_USER_ONLY)
67 CPUTimer
*timer
= opaque
;
69 if (timer
->npt
&& mem_idx
< MMU_KERNEL_IDX
) {
70 cpu_raise_exception_ra(env
, TT_PRIV_INSN
, GETPC());
73 return cpu_tick_get_count(timer
);
79 void helper_tick_set_limit(void *opaque
, uint64_t limit
)
81 #if !defined(CONFIG_USER_ONLY)
82 cpu_tick_set_limit(opaque
, limit
);
87 static target_ulong
do_udiv(CPUSPARCState
*env
, target_ulong a
,
88 target_ulong b
, int cc
, uintptr_t ra
)
94 x0
= (a
& 0xffffffff) | ((int64_t) (env
->y
) << 32);
95 x1
= (b
& 0xffffffff);
98 cpu_raise_exception_ra(env
, TT_DIV_ZERO
, ra
);
102 if (x0
> UINT32_MAX
) {
109 env
->cc_src2
= overflow
;
110 env
->cc_op
= CC_OP_DIV
;
115 target_ulong
helper_udiv(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
117 return do_udiv(env
, a
, b
, 0, GETPC());
120 target_ulong
helper_udiv_cc(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
122 return do_udiv(env
, a
, b
, 1, GETPC());
125 static target_ulong
do_sdiv(CPUSPARCState
*env
, target_ulong a
,
126 target_ulong b
, int cc
, uintptr_t ra
)
132 x0
= (a
& 0xffffffff) | ((int64_t) (env
->y
) << 32);
133 x1
= (b
& 0xffffffff);
136 cpu_raise_exception_ra(env
, TT_DIV_ZERO
, ra
);
137 } else if (x1
== -1 && x0
== INT64_MIN
) {
142 if ((int32_t) x0
!= x0
) {
143 x0
= x0
< 0 ? INT32_MIN
: INT32_MAX
;
150 env
->cc_src2
= overflow
;
151 env
->cc_op
= CC_OP_DIV
;
156 target_ulong
helper_sdiv(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
158 return do_sdiv(env
, a
, b
, 0, GETPC());
161 target_ulong
helper_sdiv_cc(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
163 return do_sdiv(env
, a
, b
, 1, GETPC());
166 #ifdef TARGET_SPARC64
167 int64_t helper_sdivx(CPUSPARCState
*env
, int64_t a
, int64_t b
)
170 /* Raise divide by zero trap. */
171 cpu_raise_exception_ra(env
, TT_DIV_ZERO
, GETPC());
172 } else if (b
== -1) {
173 /* Avoid overflow trap with i386 divide insn. */
180 uint64_t helper_udivx(CPUSPARCState
*env
, uint64_t a
, uint64_t b
)
183 /* Raise divide by zero trap. */
184 cpu_raise_exception_ra(env
, TT_DIV_ZERO
, GETPC());
190 target_ulong
helper_taddcctv(CPUSPARCState
*env
, target_ulong src1
,
195 /* Tag overflow occurs if either input has bits 0 or 1 set. */
196 if ((src1
| src2
) & 3) {
202 /* Tag overflow occurs if the addition overflows. */
203 if (~(src1
^ src2
) & (src1
^ dst
) & (1u << 31)) {
207 /* Only modify the CC after any exceptions have been generated. */
208 env
->cc_op
= CC_OP_TADDTV
;
215 cpu_raise_exception_ra(env
, TT_TOVF
, GETPC());
218 target_ulong
helper_tsubcctv(CPUSPARCState
*env
, target_ulong src1
,
223 /* Tag overflow occurs if either input has bits 0 or 1 set. */
224 if ((src1
| src2
) & 3) {
230 /* Tag overflow occurs if the subtraction overflows. */
231 if ((src1
^ src2
) & (src1
^ dst
) & (1u << 31)) {
235 /* Only modify the CC after any exceptions have been generated. */
236 env
->cc_op
= CC_OP_TSUBTV
;
243 cpu_raise_exception_ra(env
, TT_TOVF
, GETPC());
246 #ifndef TARGET_SPARC64
247 void helper_power_down(CPUSPARCState
*env
)
249 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
252 cs
->exception_index
= EXCP_HLT
;
254 env
->npc
= env
->pc
+ 4;