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 helper_raise_exception(CPUSPARCState
*env
, int tt
)
29 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
31 cs
->exception_index
= tt
;
35 void helper_debug(CPUSPARCState
*env
)
37 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
39 cs
->exception_index
= EXCP_DEBUG
;
44 target_ulong
helper_popc(target_ulong val
)
49 void helper_tick_set_count(void *opaque
, uint64_t count
)
51 #if !defined(CONFIG_USER_ONLY)
52 cpu_tick_set_count(opaque
, count
);
56 uint64_t helper_tick_get_count(CPUSPARCState
*env
, void *opaque
, int mem_idx
)
58 #if !defined(CONFIG_USER_ONLY)
59 CPUTimer
*timer
= opaque
;
61 if (timer
->npt
&& mem_idx
< MMU_KERNEL_IDX
) {
62 helper_raise_exception(env
, TT_PRIV_INSN
);
65 return cpu_tick_get_count(timer
);
71 void helper_tick_set_limit(void *opaque
, uint64_t limit
)
73 #if !defined(CONFIG_USER_ONLY)
74 cpu_tick_set_limit(opaque
, limit
);
79 static target_ulong
helper_udiv_common(CPUSPARCState
*env
, target_ulong a
,
80 target_ulong b
, int cc
)
82 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
87 x0
= (a
& 0xffffffff) | ((int64_t) (env
->y
) << 32);
88 x1
= (b
& 0xffffffff);
91 cpu_restore_state(CPU(cpu
), GETPC());
92 helper_raise_exception(env
, TT_DIV_ZERO
);
96 if (x0
> UINT32_MAX
) {
103 env
->cc_src2
= overflow
;
104 env
->cc_op
= CC_OP_DIV
;
109 target_ulong
helper_udiv(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
111 return helper_udiv_common(env
, a
, b
, 0);
114 target_ulong
helper_udiv_cc(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
116 return helper_udiv_common(env
, a
, b
, 1);
119 static target_ulong
helper_sdiv_common(CPUSPARCState
*env
, target_ulong a
,
120 target_ulong b
, int cc
)
122 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
127 x0
= (a
& 0xffffffff) | ((int64_t) (env
->y
) << 32);
128 x1
= (b
& 0xffffffff);
131 cpu_restore_state(CPU(cpu
), GETPC());
132 helper_raise_exception(env
, TT_DIV_ZERO
);
133 } else if (x1
== -1 && x0
== INT64_MIN
) {
138 if ((int32_t) x0
!= x0
) {
139 x0
= x0
< 0 ? INT32_MIN
: INT32_MAX
;
146 env
->cc_src2
= overflow
;
147 env
->cc_op
= CC_OP_DIV
;
152 target_ulong
helper_sdiv(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
154 return helper_sdiv_common(env
, a
, b
, 0);
157 target_ulong
helper_sdiv_cc(CPUSPARCState
*env
, target_ulong a
, target_ulong b
)
159 return helper_sdiv_common(env
, a
, b
, 1);
162 #ifdef TARGET_SPARC64
163 int64_t helper_sdivx(CPUSPARCState
*env
, int64_t a
, int64_t b
)
166 /* Raise divide by zero trap. */
167 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
169 cpu_restore_state(CPU(cpu
), GETPC());
170 helper_raise_exception(env
, TT_DIV_ZERO
);
171 } else if (b
== -1) {
172 /* Avoid overflow trap with i386 divide insn. */
179 uint64_t helper_udivx(CPUSPARCState
*env
, uint64_t a
, uint64_t b
)
182 /* Raise divide by zero trap. */
183 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
185 cpu_restore_state(CPU(cpu
), GETPC());
186 helper_raise_exception(env
, TT_DIV_ZERO
);
192 target_ulong
helper_taddcctv(CPUSPARCState
*env
, target_ulong src1
,
195 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
198 /* Tag overflow occurs if either input has bits 0 or 1 set. */
199 if ((src1
| src2
) & 3) {
205 /* Tag overflow occurs if the addition overflows. */
206 if (~(src1
^ src2
) & (src1
^ dst
) & (1u << 31)) {
210 /* Only modify the CC after any exceptions have been generated. */
211 env
->cc_op
= CC_OP_TADDTV
;
218 cpu_restore_state(CPU(cpu
), GETPC());
219 helper_raise_exception(env
, TT_TOVF
);
222 target_ulong
helper_tsubcctv(CPUSPARCState
*env
, target_ulong src1
,
225 SPARCCPU
*cpu
= sparc_env_get_cpu(env
);
228 /* Tag overflow occurs if either input has bits 0 or 1 set. */
229 if ((src1
| src2
) & 3) {
235 /* Tag overflow occurs if the subtraction overflows. */
236 if ((src1
^ src2
) & (src1
^ dst
) & (1u << 31)) {
240 /* Only modify the CC after any exceptions have been generated. */
241 env
->cc_op
= CC_OP_TSUBTV
;
248 cpu_restore_state(CPU(cpu
), GETPC());
249 helper_raise_exception(env
, TT_TOVF
);
252 #ifndef TARGET_SPARC64
253 void helper_power_down(CPUSPARCState
*env
)
255 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
258 cs
->exception_index
= EXCP_HLT
;
260 env
->npc
= env
->pc
+ 4;