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 "qemu/host-utils.h"
23 #include "exec/helper-proto.h"
24 #include "sysemu/sysemu.h"
26 void helper_raise_exception(CPUSPARCState
*env
, int tt
)
28 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
30 cs
->exception_index
= tt
;
34 void helper_debug(CPUSPARCState
*env
)
36 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
38 cs
->exception_index
= EXCP_DEBUG
;
43 target_ulong
helper_popc(target_ulong val
)
48 void helper_tick_set_count(void *opaque
, uint64_t count
)
50 #if !defined(CONFIG_USER_ONLY)
51 cpu_tick_set_count(opaque
, count
);
55 uint64_t helper_tick_get_count(CPUSPARCState
*env
, void *opaque
, int mem_idx
)
57 #if !defined(CONFIG_USER_ONLY)
58 CPUTimer
*timer
= opaque
;
60 if (timer
->npt
&& mem_idx
< MMU_KERNEL_IDX
) {
61 helper_raise_exception(env
, TT_PRIV_INSN
);
64 return cpu_tick_get_count(timer
);
70 void helper_tick_set_limit(void *opaque
, uint64_t limit
)
72 #if !defined(CONFIG_USER_ONLY)
73 cpu_tick_set_limit(opaque
, limit
);
78 static target_ulong
helper_udiv_common(CPUSPARCState
*env
, target_ulong a
,
79 target_ulong b
, int cc
)
81 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
86 x0
= (a
& 0xffffffff) | ((int64_t) (env
->y
) << 32);
87 x1
= (b
& 0xffffffff);
90 cpu_restore_state(CPU(cpu
), GETPC());
91 helper_raise_exception(env
, TT_DIV_ZERO
);
95 if (x0
> UINT32_MAX
) {
102 env
->cc_src2
= overflow
;
103 env
->cc_op
= CC_OP_DIV
;
108 target_ulong
helper_udiv(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
110 return helper_udiv_common(env
, a
, b
, 0);
113 target_ulong
helper_udiv_cc(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
115 return helper_udiv_common(env
, a
, b
, 1);
118 static target_ulong
helper_sdiv_common(CPUSPARCState
*env
, target_ulong a
,
119 target_ulong b
, int cc
)
121 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
126 x0
= (a
& 0xffffffff) | ((int64_t) (env
->y
) << 32);
127 x1
= (b
& 0xffffffff);
130 cpu_restore_state(CPU(cpu
), GETPC());
131 helper_raise_exception(env
, TT_DIV_ZERO
);
132 } else if (x1
== -1 && x0
== INT64_MIN
) {
137 if ((int32_t) x0
!= x0
) {
138 x0
= x0
< 0 ? INT32_MIN
: INT32_MAX
;
145 env
->cc_src2
= overflow
;
146 env
->cc_op
= CC_OP_DIV
;
151 target_ulong
helper_sdiv(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
153 return helper_sdiv_common(env
, a
, b
, 0);
156 target_ulong
helper_sdiv_cc(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
158 return helper_sdiv_common(env
, a
, b
, 1);
161 #ifdef TARGET_SPARC64
162 int64_t helper_sdivx(CPUSPARCState
*env
, int64_t a
, int64_t b
)
165 /* Raise divide by zero trap. */
166 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
168 cpu_restore_state(CPU(cpu
), GETPC());
169 helper_raise_exception(env
, TT_DIV_ZERO
);
170 } else if (b
== -1) {
171 /* Avoid overflow trap with i386 divide insn. */
178 uint64_t helper_udivx(CPUSPARCState
*env
, uint64_t a
, uint64_t b
)
181 /* Raise divide by zero trap. */
182 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
184 cpu_restore_state(CPU(cpu
), GETPC());
185 helper_raise_exception(env
, TT_DIV_ZERO
);
191 target_ulong
helper_taddcctv(CPUSPARCState
*env
, target_ulong src1
,
194 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
197 /* Tag overflow occurs if either input has bits 0 or 1 set. */
198 if ((src1
| src2
) & 3) {
204 /* Tag overflow occurs if the addition overflows. */
205 if (~(src1
^ src2
) & (src1
^ dst
) & (1u << 31)) {
209 /* Only modify the CC after any exceptions have been generated. */
210 env
->cc_op
= CC_OP_TADDTV
;
217 cpu_restore_state(CPU(cpu
), GETPC());
218 helper_raise_exception(env
, TT_TOVF
);
221 target_ulong
helper_tsubcctv(CPUSPARCState
*env
, target_ulong src1
,
224 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
227 /* Tag overflow occurs if either input has bits 0 or 1 set. */
228 if ((src1
| src2
) & 3) {
234 /* Tag overflow occurs if the subtraction overflows. */
235 if ((src1
^ src2
) & (src1
^ dst
) & (1u << 31)) {
239 /* Only modify the CC after any exceptions have been generated. */
240 env
->cc_op
= CC_OP_TSUBTV
;
247 cpu_restore_state(CPU(cpu
), GETPC());
248 helper_raise_exception(env
, TT_TOVF
);
251 #ifndef TARGET_SPARC64
252 void helper_power_down(CPUSPARCState
*env
)
254 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
257 cs
->exception_index
= EXCP_HLT
;
259 env
->npc
= env
->pc
+ 4;