qemu-e2k/target/e2k/helper.c

293 lines
6.9 KiB
C
Raw Normal View History

#include "qemu/osdep.h"
2020-11-13 15:49:28 +01:00
#include "qemu/log.h"
#include "cpu.h"
#include "exec/exec-all.h"
#include "qemu/host-utils.h"
#include "exec/helper-proto.h"
2020-11-12 14:52:51 +01:00
#include "translate.h"
2020-12-10 08:44:15 +01:00
#define PS_FORCE_FX true
static inline void reset_ctprs(CPUE2KState *env)
{
unsigned int i;
2020-11-22 08:37:45 +01:00
for (i = 0; i < 3; i++) {
2020-12-08 09:22:01 +01:00
env->ctprs[i].tag = CTPR_TAG_NONE;
}
}
2020-12-09 20:38:16 +01:00
static inline void stack_push(CPUE2KState *env, E2KStackState *s, uint64_t value)
2020-11-26 20:14:40 +01:00
{
2020-12-09 20:38:16 +01:00
if ((s->index + 8) > s->size) {
2020-11-26 17:31:25 +01:00
helper_raise_exception(env, E2K_EXCP_MAPERR);
return;
2020-11-15 17:39:30 +01:00
}
2020-12-09 20:38:16 +01:00
cpu_stq_le_data_ra(env, s->base + s->index, value, GETPC());
s->index += 8;
2020-11-16 15:26:39 +01:00
}
2020-12-09 20:38:16 +01:00
static inline uint64_t stack_pop(CPUE2KState *env, E2KStackState *s)
2020-11-16 15:26:39 +01:00
{
2020-12-09 20:38:16 +01:00
if (s->index < 8) {
2020-11-26 16:02:49 +01:00
helper_raise_exception(env, E2K_EXCP_MAPERR);
2020-12-09 20:38:16 +01:00
return 0;
2020-11-16 15:26:39 +01:00
}
2020-12-09 20:38:16 +01:00
s->index -= 8;
return cpu_ldq_le_data(env, s->base + s->index);
2020-11-16 15:26:39 +01:00
}
2020-12-09 20:38:16 +01:00
#define pcs_push(env, value) stack_push(env, &env->pcsp, (value))
#define pcs_pop(env) stack_pop(env, &env->pcsp)
static inline void ps_push(CPUE2KState *env, uint64_t value, uint8_t tag)
{
cpu_stb_data(env, env->psp.base_tag + env->psp.index / 8, tag);
stack_push(env, &env->psp, value);
}
static inline uint64_t ps_pop(CPUE2KState *env, uint8_t *ret_tag)
{
uint64_t ret = stack_pop(env, &env->psp);
if (ret_tag != NULL) {
*ret_tag = cpu_ldub_data(env, env->psp.base_tag + env->psp.index / 8);
}
return ret;
}
2020-11-26 16:02:49 +01:00
2021-01-15 19:28:35 +01:00
static void proc_chain_save(CPUE2KState *env, int wd_base, target_ulong ret_ip)
2020-11-26 16:02:49 +01:00
{
pcs_push(env, env->crs.cr0_lo);
pcs_push(env, env->crs.cr0_hi);
pcs_push(env, env->crs.cr1.lo);
pcs_push(env, env->crs.cr1.hi);
2021-01-09 23:01:20 +01:00
env->crs.cr0_lo = env->pregs;
2021-01-15 19:28:35 +01:00
env->crs.cr0_hi = ret_ip;
env->crs.cr1.wbs = wd_base / 2;
2021-01-09 23:01:20 +01:00
env->crs.cr1.wpsz = env->wd.psize / 2;
env->crs.cr1.wfx = env->wd.fx;
env->crs.cr1.wdbl = env->wdbl;
env->crs.cr1.br = e2k_state_br(env);
env->crs.cr1.ussz = env->usd.size >> 4;
2020-11-26 16:02:49 +01:00
2020-12-09 20:38:16 +01:00
env->wd.fx = true;
2021-01-15 19:28:35 +01:00
env->wd.size -= wd_base;
2020-12-09 20:38:16 +01:00
env->wd.psize = env->wd.size;
2020-11-26 16:02:49 +01:00
}
2021-01-15 19:28:35 +01:00
static void proc_chain_restore(CPUE2KState *env)
2020-11-26 16:02:49 +01:00
{
2021-01-15 19:28:35 +01:00
int wd_base;
2021-01-09 23:01:20 +01:00
env->pregs = env->crs.cr0_lo;
env->ip = env->crs.cr0_hi;
2021-01-15 19:28:35 +01:00
wd_base = env->crs.cr1.wbs * 2;
2021-01-09 23:01:20 +01:00
e2k_state_br_set(env, env->crs.cr1.br);
2021-01-15 19:28:35 +01:00
env->wd.size = env->wd.psize + wd_base;
2021-01-09 23:01:20 +01:00
env->wd.psize = env->crs.cr1.wpsz * 2;
env->wd.fx = env->crs.cr1.wfx;
env->wdbl = env->crs.cr1.wdbl;
env->usd.size = env->crs.cr1.ussz << 4;
env->usd.base = env->sbr - env->usd.size;
2020-11-15 17:39:30 +01:00
env->crs.cr1.hi = pcs_pop(env);
env->crs.cr1.lo = pcs_pop(env);
env->crs.cr0_hi = pcs_pop(env);
env->crs.cr0_lo = pcs_pop(env);
2020-12-09 20:38:16 +01:00
}
2021-01-19 18:48:29 +01:00
static void ps_spill(CPUE2KState *env, int n, bool fx)
2020-12-09 20:38:16 +01:00
{
2021-01-19 18:48:29 +01:00
int i;
for (i = 0; i < n; i += 2) {
ps_push(env, env->regs[i], env->tags[i]);
ps_push(env, env->regs[i + 1], env->tags[i + 1]);
if (fx) {
ps_push(env, env->xregs[i + 0], 0);
ps_push(env, env->xregs[i + 1], 0);
}
2021-01-09 23:01:20 +01:00
}
}
2021-01-19 18:48:29 +01:00
static void ps_fill(CPUE2KState *env, int n, bool fx)
2021-01-09 23:01:20 +01:00
{
2021-01-19 18:48:29 +01:00
int i;
for (i = n; i > 0; i -= 2) {
if (fx) {
env->xregs[i - 1] = ps_pop(env, NULL);
env->xregs[i - 2] = ps_pop(env, NULL);
}
env->regs[i - 1] = ps_pop(env, &env->tags[i - 1]);
env->regs[i - 2] = ps_pop(env, &env->tags[i - 2]);
2021-01-09 23:01:20 +01:00
}
}
2021-01-19 18:48:29 +01:00
static inline void ps_spill_all(CPUE2KState *env)
2021-01-09 23:01:20 +01:00
{
2021-01-21 23:20:49 +01:00
ps_spill(env, env->wd.size, true);
}
static inline void ps_fill_all(CPUE2KState *env)
{
ps_fill(env, env->wd.size, true);
2021-01-09 23:01:20 +01:00
}
2021-01-19 18:48:29 +01:00
static void move_regs(CPUE2KState *env, int dst, int src, int n)
2021-01-09 23:01:20 +01:00
{
2021-01-19 18:48:29 +01:00
memmove(&env->regs[dst], &env->regs[src], n * sizeof(env->regs[0]));
memmove(&env->tags[dst], &env->tags[src], n * sizeof(env->tags[0]));
memmove(&env->xregs[dst], &env->xregs[src], n * sizeof(env->xregs[0]));
2020-11-26 23:03:54 +01:00
}
2021-01-19 18:48:29 +01:00
static void callee_window(CPUE2KState *env, int wbs)
2020-11-26 23:03:54 +01:00
{
2021-01-19 18:48:29 +01:00
int s = wbs * 2;
ps_spill(env, s, env->wd.fx);
move_regs(env, 0, s, env->wd.size - s);
}
static void caller_window(CPUE2KState *env)
{
int s = env->crs.cr1.wbs * 2;
move_regs(env, s, 0, env->wd.psize);
ps_fill(env, s, env->crs.cr1.wfx);
2020-11-26 23:03:54 +01:00
}
2021-01-15 19:28:35 +01:00
static inline void do_call(CPUE2KState *env, int wbs, target_ulong ret_ip)
2020-11-12 14:52:51 +01:00
{
2021-01-19 18:48:29 +01:00
callee_window(env, wbs);
2021-01-15 19:28:35 +01:00
proc_chain_save(env, wbs * 2, ret_ip);
reset_ctprs(env);
}
2021-01-15 19:28:35 +01:00
static inline void do_syscall(CPUE2KState *env, int wbs, target_ulong ret_ip)
2020-12-09 20:38:16 +01:00
{
2021-01-15 19:28:35 +01:00
CPUState *cs = env_cpu(env);
do_call(env, wbs, ret_ip);
cs->exception_index = E2K_EXCP_SYSCALL;
cpu_loop_exit(cs);
}
2021-01-15 19:28:35 +01:00
void HELPER(call)(CPUE2KState *env, uint64_t ctpr_raw, int call_wbs,
target_ulong pc_next)
{
E2KCtpr ctpr = { .raw = ctpr_raw };
2021-01-15 19:28:35 +01:00
switch (ctpr.tag) {
case CTPR_TAG_DISP:
do_call(env, call_wbs, pc_next);
env->ip = ctpr.base;
break;
case CTPR_TAG_SDISP:
do_syscall(env, call_wbs, pc_next);
env->ip = ctpr.base;
break;
default:
helper_raise_exception(env, E2K_EXCP_ILLOPC);
break;
}
2020-12-09 20:38:16 +01:00
}
2021-01-15 19:28:35 +01:00
uint64_t HELPER(prep_return)(CPUE2KState *env, int ipd)
2020-11-28 10:32:50 +01:00
{
2020-12-08 09:22:01 +01:00
E2KCtpr ret = { 0 };
2020-11-28 10:32:50 +01:00
if (env->pcsp.index < 32) {
helper_raise_exception(env, E2K_EXCP_MAPERR);
return 0;
}
ret.base = env->crs.cr0_hi;
2020-12-08 09:22:01 +01:00
ret.tag = CTPR_TAG_RETURN;
ret.ipd = ipd;
return ret.raw;
2020-11-28 10:32:50 +01:00
}
2021-01-15 19:28:35 +01:00
void HELPER(return)(CPUE2KState *env)
{
2021-01-19 18:48:29 +01:00
caller_window(env);
2020-12-09 20:38:16 +01:00
proc_chain_restore(env);
reset_ctprs(env);
}
2021-01-15 19:28:35 +01:00
void HELPER(raise_exception)(CPUE2KState *env, int tt)
2020-11-23 07:14:26 +01:00
{
CPUState *cs = env_cpu(env);
cs->exception_index = tt;
2021-01-19 18:48:29 +01:00
ps_spill_all(env);
2021-01-15 19:28:35 +01:00
proc_chain_save(env, env->wd.size, env->ip);
2020-11-23 07:14:26 +01:00
cpu_loop_exit(cs);
}
2020-12-14 08:22:21 +01:00
void HELPER(raise_exception_no_spill)(CPUE2KState *env, int tt)
{
CPUState *cs = env_cpu(env);
cs->exception_index = tt;
cpu_loop_exit(cs);
}
2021-01-21 23:20:49 +01:00
void HELPER(setwd)(CPUE2KState *env, int wsz, int nfx, int dbl)
2020-11-23 07:14:26 +01:00
{
2021-01-21 23:20:49 +01:00
int i, size = wsz * 2;
2021-01-15 19:28:35 +01:00
if (size < env->wd.psize) {
helper_raise_exception(env, E2K_EXCP_ILLOPN);
return;
}
2021-01-21 23:20:49 +01:00
for (i = env->wd.size; i < size; i++) {
env->tags[i] = E2K_TAG_NON_NUMBER64;
2021-01-15 19:28:35 +01:00
}
2021-01-21 23:20:49 +01:00
env->wd.size = size;
env->wd.fx = nfx == 0;
env->wdbl = dbl;
}
2020-12-13 21:55:58 +01:00
bool e2k_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
MMUAccessType access_type, int mmu_idx, bool probe, uintptr_t retaddr)
{
E2KCPU *cpu = E2K_CPU(cs);
CPUE2KState *env = &cpu->env;
2021-01-19 18:48:29 +01:00
ps_spill_all(env);
2021-01-15 19:28:35 +01:00
proc_chain_save(env, env->wd.size, env->ip);
2020-12-13 21:55:58 +01:00
cs->exception_index = E2K_EXCP_MAPERR;
cpu_loop_exit_restore(cs, retaddr);
}
2020-11-13 15:49:28 +01:00
2021-01-15 19:28:35 +01:00
void e2k_break_save_state(CPUE2KState *env)
{
env->is_bp = true;
2021-01-19 18:48:29 +01:00
ps_spill_all(env);
2021-01-15 19:28:35 +01:00
proc_chain_save(env, env->wd.size, env->ip);
}
void HELPER(break_restore_state)(CPUE2KState *env)
{
proc_chain_restore(env);
2021-01-21 23:20:49 +01:00
ps_fill_all(env);
2021-01-19 18:48:29 +01:00
env->is_bp = false;
2021-01-15 19:28:35 +01:00
}
void HELPER(debug_i32)(uint32_t x)
2020-11-13 15:49:28 +01:00
{
qemu_log_mask(LOG_UNIMP, "log %#x\n", x);
}
2021-01-15 19:28:35 +01:00
void HELPER(debug_i64)(uint64_t x)
2020-11-13 15:49:28 +01:00
{
qemu_log_mask(LOG_UNIMP, "log %#lx\n", x);
}
2021-01-15 19:28:35 +01:00
void HELPER(debug_ptr)(void *x)
2020-11-28 17:43:24 +01:00
{
qemu_log_mask(LOG_UNIMP, "log %p\n", x);
2020-11-28 17:43:24 +01:00
}