2004-10-01 01:55:55 +04:00
|
|
|
/*
|
2011-09-11 15:30:01 +04:00
|
|
|
* Misc Sparc helpers
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2005-07-23 18:27:54 +04:00
|
|
|
* Copyright (c) 2003-2005 Fabrice Bellard
|
2004-10-01 01:55:55 +04:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 15:42:35 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-10-01 01:55:55 +04:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-07-17 00:47:01 +04:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2004-10-01 01:55:55 +04:00
|
|
|
*/
|
2005-07-05 02:18:23 +04:00
|
|
|
|
2016-01-26 21:16:59 +03:00
|
|
|
#include "qemu/osdep.h"
|
2005-07-05 02:18:23 +04:00
|
|
|
#include "cpu.h"
|
2016-03-15 15:18:37 +03:00
|
|
|
#include "exec/exec-all.h"
|
2022-02-07 11:27:54 +03:00
|
|
|
#include "qemu/timer.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/host-utils.h"
|
2014-04-08 09:31:41 +04:00
|
|
|
#include "exec/helper-proto.h"
|
2004-10-01 01:55:55 +04:00
|
|
|
|
2016-07-12 23:12:50 +03:00
|
|
|
void cpu_raise_exception_ra(CPUSPARCState *env, int tt, uintptr_t ra)
|
|
|
|
{
|
2019-03-23 05:36:20 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2016-07-12 23:12:50 +03:00
|
|
|
|
|
|
|
cs->exception_index = tt;
|
|
|
|
cpu_loop_exit_restore(cs, ra);
|
|
|
|
}
|
|
|
|
|
2012-03-14 04:38:22 +04:00
|
|
|
void helper_raise_exception(CPUSPARCState *env, int tt)
|
2011-07-03 12:19:42 +04:00
|
|
|
{
|
2019-03-23 05:36:20 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2013-08-26 10:31:06 +04:00
|
|
|
|
|
|
|
cs->exception_index = tt;
|
2013-08-27 19:52:12 +04:00
|
|
|
cpu_loop_exit(cs);
|
2011-07-03 12:19:42 +04:00
|
|
|
}
|
|
|
|
|
2012-03-14 04:38:22 +04:00
|
|
|
void helper_debug(CPUSPARCState *env)
|
2011-07-03 12:19:42 +04:00
|
|
|
{
|
2019-03-23 05:36:20 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2013-08-26 10:31:06 +04:00
|
|
|
|
|
|
|
cs->exception_index = EXCP_DEBUG;
|
2013-08-27 19:52:12 +04:00
|
|
|
cpu_loop_exit(cs);
|
2011-07-03 12:19:42 +04:00
|
|
|
}
|
|
|
|
|
2011-07-03 11:05:50 +04:00
|
|
|
#ifdef TARGET_SPARC64
|
|
|
|
void helper_tick_set_count(void *opaque, uint64_t count)
|
|
|
|
{
|
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
|
|
|
cpu_tick_set_count(opaque, count);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-11-08 20:11:59 +03:00
|
|
|
uint64_t helper_tick_get_count(CPUSPARCState *env, void *opaque, int mem_idx)
|
2011-07-03 11:05:50 +04:00
|
|
|
{
|
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
2015-11-08 20:11:59 +03:00
|
|
|
CPUTimer *timer = opaque;
|
|
|
|
|
|
|
|
if (timer->npt && mem_idx < MMU_KERNEL_IDX) {
|
2016-07-12 23:12:50 +03:00
|
|
|
cpu_raise_exception_ra(env, TT_PRIV_INSN, GETPC());
|
2015-11-08 20:11:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return cpu_tick_get_count(timer);
|
2011-07-03 11:05:50 +04:00
|
|
|
#else
|
2018-05-28 22:48:12 +03:00
|
|
|
/* In user-mode, QEMU_CLOCK_VIRTUAL doesn't exist.
|
|
|
|
Just pass through the host cpu clock ticks. */
|
|
|
|
return cpu_get_host_ticks();
|
2011-07-03 11:05:50 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_tick_set_limit(void *opaque, uint64_t limit)
|
|
|
|
{
|
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
|
|
|
cpu_tick_set_limit(opaque, limit);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-04 22:15:42 +04:00
|
|
|
|
2023-10-15 05:44:03 +03:00
|
|
|
uint64_t helper_udiv(CPUSPARCState *env, target_ulong a, target_ulong b)
|
2011-07-04 22:15:42 +04:00
|
|
|
{
|
2023-10-15 05:44:03 +03:00
|
|
|
uint64_t a64 = (uint32_t)a | ((uint64_t)env->y << 32);
|
|
|
|
uint32_t b32 = b;
|
|
|
|
uint32_t r;
|
2011-07-04 22:15:42 +04:00
|
|
|
|
2023-10-15 05:44:03 +03:00
|
|
|
if (b32 == 0) {
|
|
|
|
cpu_raise_exception_ra(env, TT_DIV_ZERO, GETPC());
|
2011-07-04 22:15:42 +04:00
|
|
|
}
|
|
|
|
|
2023-10-15 05:44:03 +03:00
|
|
|
a64 /= b32;
|
|
|
|
r = a64;
|
|
|
|
if (unlikely(a64 > UINT32_MAX)) {
|
|
|
|
return -1; /* r = UINT32_MAX, v = 1 */
|
2011-07-04 22:15:42 +04:00
|
|
|
}
|
2023-10-15 05:44:03 +03:00
|
|
|
return r;
|
2011-07-04 22:15:42 +04:00
|
|
|
}
|
|
|
|
|
2023-10-15 05:44:03 +03:00
|
|
|
uint64_t helper_sdiv(CPUSPARCState *env, target_ulong a, target_ulong b)
|
2011-07-04 22:15:42 +04:00
|
|
|
{
|
2023-10-15 05:44:03 +03:00
|
|
|
int64_t a64 = (uint32_t)a | ((uint64_t)env->y << 32);
|
|
|
|
int32_t b32 = b;
|
|
|
|
int32_t r;
|
2011-07-04 22:15:42 +04:00
|
|
|
|
2023-10-15 05:44:03 +03:00
|
|
|
if (b32 == 0) {
|
|
|
|
cpu_raise_exception_ra(env, TT_DIV_ZERO, GETPC());
|
2011-07-04 22:15:42 +04:00
|
|
|
}
|
|
|
|
|
2023-10-15 05:44:03 +03:00
|
|
|
if (unlikely(a64 == INT64_MIN)) {
|
|
|
|
/*
|
|
|
|
* Special case INT64_MIN / -1 is required to avoid trap on x86 host.
|
|
|
|
* However, with a dividend of INT64_MIN, there is no 32-bit divisor
|
|
|
|
* which can yield a 32-bit result:
|
|
|
|
* INT64_MIN / INT32_MIN = 0x1_0000_0000
|
|
|
|
* INT64_MIN / INT32_MAX = -0x1_0000_0002
|
|
|
|
* Therefore we know we must overflow and saturate.
|
|
|
|
*/
|
|
|
|
return (uint32_t)(b32 < 0 ? INT32_MAX : INT32_MIN) | (-1ull << 32);
|
2011-07-04 22:15:42 +04:00
|
|
|
}
|
|
|
|
|
2024-06-06 17:43:31 +03:00
|
|
|
a64 /= b32;
|
2023-10-15 05:44:03 +03:00
|
|
|
r = a64;
|
|
|
|
if (unlikely(r != a64)) {
|
|
|
|
return (uint32_t)(a64 < 0 ? INT32_MIN : INT32_MAX) | (-1ull << 32);
|
|
|
|
}
|
|
|
|
return (uint32_t)r;
|
2011-07-04 22:15:42 +04:00
|
|
|
}
|
2012-10-06 03:55:03 +04:00
|
|
|
|
2012-10-06 03:55:05 +04:00
|
|
|
target_ulong helper_taddcctv(CPUSPARCState *env, target_ulong src1,
|
|
|
|
target_ulong src2)
|
|
|
|
{
|
2023-10-15 07:38:12 +03:00
|
|
|
target_ulong dst, v;
|
2012-10-06 03:55:05 +04:00
|
|
|
|
|
|
|
/* Tag overflow occurs if either input has bits 0 or 1 set. */
|
|
|
|
if ((src1 | src2) & 3) {
|
|
|
|
goto tag_overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = src1 + src2;
|
|
|
|
|
|
|
|
/* Tag overflow occurs if the addition overflows. */
|
2023-10-15 07:38:12 +03:00
|
|
|
v = ~(src1 ^ src2) & (src1 ^ dst);
|
|
|
|
if (v & (1u << 31)) {
|
2012-10-06 03:55:05 +04:00
|
|
|
goto tag_overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only modify the CC after any exceptions have been generated. */
|
2023-10-15 07:38:12 +03:00
|
|
|
env->cc_V = v;
|
|
|
|
env->cc_N = dst;
|
|
|
|
env->icc_Z = dst;
|
|
|
|
#ifdef TARGET_SPARC64
|
|
|
|
env->xcc_Z = dst;
|
|
|
|
env->icc_C = dst ^ src1 ^ src2;
|
|
|
|
env->xcc_C = dst < src1;
|
|
|
|
#else
|
|
|
|
env->icc_C = dst < src1;
|
|
|
|
#endif
|
|
|
|
|
2012-10-06 03:55:05 +04:00
|
|
|
return dst;
|
|
|
|
|
|
|
|
tag_overflow:
|
2016-07-12 23:12:50 +03:00
|
|
|
cpu_raise_exception_ra(env, TT_TOVF, GETPC());
|
2012-10-06 03:55:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
target_ulong helper_tsubcctv(CPUSPARCState *env, target_ulong src1,
|
|
|
|
target_ulong src2)
|
|
|
|
{
|
2023-10-15 07:38:12 +03:00
|
|
|
target_ulong dst, v;
|
2012-10-06 03:55:05 +04:00
|
|
|
|
|
|
|
/* Tag overflow occurs if either input has bits 0 or 1 set. */
|
|
|
|
if ((src1 | src2) & 3) {
|
|
|
|
goto tag_overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = src1 - src2;
|
|
|
|
|
|
|
|
/* Tag overflow occurs if the subtraction overflows. */
|
2023-10-15 07:38:12 +03:00
|
|
|
v = (src1 ^ src2) & (src1 ^ dst);
|
|
|
|
if (v & (1u << 31)) {
|
2012-10-06 03:55:05 +04:00
|
|
|
goto tag_overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only modify the CC after any exceptions have been generated. */
|
2023-10-15 07:38:12 +03:00
|
|
|
env->cc_V = v;
|
|
|
|
env->cc_N = dst;
|
|
|
|
env->icc_Z = dst;
|
|
|
|
#ifdef TARGET_SPARC64
|
|
|
|
env->xcc_Z = dst;
|
|
|
|
env->icc_C = dst ^ src1 ^ src2;
|
|
|
|
env->xcc_C = src1 < src2;
|
|
|
|
#else
|
|
|
|
env->icc_C = src1 < src2;
|
|
|
|
#endif
|
|
|
|
|
2012-10-06 03:55:05 +04:00
|
|
|
return dst;
|
|
|
|
|
|
|
|
tag_overflow:
|
2016-07-12 23:12:50 +03:00
|
|
|
cpu_raise_exception_ra(env, TT_TOVF, GETPC());
|
2012-10-06 03:55:05 +04:00
|
|
|
}
|
2013-02-19 15:45:07 +04:00
|
|
|
|
|
|
|
#ifndef TARGET_SPARC64
|
|
|
|
void helper_power_down(CPUSPARCState *env)
|
|
|
|
{
|
2019-03-23 05:36:20 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2013-01-17 21:51:17 +04:00
|
|
|
|
|
|
|
cs->halted = 1;
|
2013-08-26 10:31:06 +04:00
|
|
|
cs->exception_index = EXCP_HLT;
|
2013-02-19 15:45:07 +04:00
|
|
|
env->pc = env->npc;
|
|
|
|
env->npc = env->pc + 4;
|
2013-08-27 19:52:12 +04:00
|
|
|
cpu_loop_exit(cs);
|
2013-02-19 15:45:07 +04:00
|
|
|
}
|
2024-01-31 11:50:43 +03:00
|
|
|
|
|
|
|
target_ulong helper_rdasr17(CPUSPARCState *env)
|
|
|
|
{
|
|
|
|
CPUState *cs = env_cpu(env);
|
|
|
|
target_ulong val;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: There are many more fields to be filled,
|
|
|
|
* some of which are writable.
|
|
|
|
*/
|
|
|
|
val = env->def.nwindows - 1; /* [4:0] NWIN */
|
|
|
|
val |= 1 << 8; /* [8] V8 */
|
|
|
|
val |= (cs->cpu_index) << 28; /* [31:28] INDEX */
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2013-02-19 15:45:07 +04:00
|
|
|
#endif
|