#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <machine/cpu.h>
#include <machine/fpu.h>
#include <machine/reg.h>
#include <alpha/alpha/db_instruction.h>
#include <lib/libkern/softfloat.h>
#define TSWINSIZE 4
#define CPUREG_CLASS (0xfUL << 0x10)
#define FPUREG_CLASS (0xfUL << 0x14)
#define CHECKFUNCTIONCODE (1UL << 0x18)
#define TRAPSHADOWBOUNDARY (1UL << 0x00 | \
1UL << 0x19 | \
1UL << 0x1a | \
1UL << 0x1b | \
1UL << 0x1d | \
1UL << 0x1e | \
1UL << 0x1f | \
0xffffUL << 0x30 | \
CHECKFUNCTIONCODE)
#define MAKE_FLOATXX(width, expwidth, sign, exp, msb, rest_of_frac) \
(u_int ## width ## _t)(sign) << ((width) - 1) |\
(u_int ## width ## _t)(exp) << ((width) - 1 - (expwidth)) |\
(u_int ## width ## _t)(msb) << ((width) - 1 - (expwidth) - 1) |\
(u_int ## width ## _t)(rest_of_frac)
#define FLOAT32QNAN MAKE_FLOATXX(32, 8, 0, 0xff, 1, 0)
#define FLOAT64QNAN MAKE_FLOATXX(64, 11, 0, 0x7ff, 1, 0)
#define IS_SUBNORMAL(v) ((v)->exp == 0 && (v)->frac != 0)
#define PREFILTER_SUBNORMAL(p,v) \
do { \
if ((p)->p_md.md_flags & IEEE_MAP_DMZ && IS_SUBNORMAL(v)) \
(v)->frac = 0; \
} while (0)
#define POSTFILTER_SUBNORMAL(p,v) \
do { \
if ((p)->p_md.md_flags & IEEE_MAP_UMZ && IS_SUBNORMAL(v)) \
(v)->frac = 0; \
} while (0)
#define CMP_RESULT(flag) ((flag) ? 4UL << 60 : 0L)
#define CRBLIT(sw, hw, m, offs) (((sw) & ~(m)) | ((hw) >> (offs) & (m)))
struct alpha_shadow {
u_int64_t resolved;
u_int64_t unresolved;
u_int64_t scans;
u_int64_t len;
u_int64_t uop;
u_int64_t sqrts;
u_int64_t sqrtt;
u_int32_t ufunc;
u_int32_t max;
u_int32_t nilswop;
u_int32_t nilswfunc;
u_int32_t nilanyop;
u_int32_t vax;
} alpha_shadow, alpha_shadow_zero;
static float64 float64_unk(float64, float64);
static float64 compare_un(float64, float64);
static float64 compare_eq(float64, float64);
static float64 compare_lt(float64, float64);
static float64 compare_le(float64, float64);
static void cvt_qs_ts_st_gf_qf(u_int32_t, struct proc *);
static void cvt_gd(u_int32_t, struct proc *);
static void cvt_qt_dg_qg(u_int32_t, struct proc *);
static void cvt_tq_gq(u_int32_t, struct proc *);
static float32 (*swfp_s[])(float32, float32) = {
float32_add, float32_sub, float32_mul, float32_div,
};
static float64 (*swfp_t[])(float64, float64) = {
float64_add, float64_sub, float64_mul, float64_div,
compare_un, compare_eq, compare_lt, compare_le,
float64_unk, float64_unk, float64_unk, float64_unk
};
static void (*swfp_cvt[])(u_int32_t, struct proc *) = {
cvt_qs_ts_st_gf_qf, cvt_gd, cvt_qt_dg_qg, cvt_tq_gq
};
static void
this_cannot_happen(int what_cannot_happen, int64_t bits)
{
static int total;
alpha_instruction inst;
static u_int64_t reported;
inst.bits = bits;
++alpha_shadow.nilswfunc;
if (bits != -1)
alpha_shadow.uop |= 1UL << inst.generic_format.opcode;
if (1UL << what_cannot_happen & reported)
return;
reported |= 1UL << what_cannot_happen;
if (total >= 1000)
return;
++total;
if (bits)
printf("FP instruction %x\n", (unsigned int)bits);
printf("FP event %d/%lx/%lx\n", what_cannot_happen,
(unsigned long)reported, (unsigned long)alpha_shadow.uop);
}
static __inline void
sts(unsigned int rn, s_float *v, struct proc *p)
{
alpha_sts(rn, v);
PREFILTER_SUBNORMAL(p, v);
}
static __inline void
stt(unsigned int rn, t_float *v, struct proc *p)
{
alpha_stt(rn, v);
PREFILTER_SUBNORMAL(p, v);
}
static __inline void
lds(unsigned int rn, s_float *v, struct proc *p)
{
POSTFILTER_SUBNORMAL(p, v);
alpha_lds(rn, v);
}
static __inline void
ldt(unsigned int rn, t_float *v, struct proc *p)
{
POSTFILTER_SUBNORMAL(p, v);
alpha_ldt(rn, v);
}
static float64
compare_lt(float64 a, float64 b)
{
return CMP_RESULT(float64_lt(a, b));
}
static float64
compare_le(float64 a, float64 b)
{
return CMP_RESULT(float64_le(a, b));
}
static float64
compare_un(float64 a, float64 b)
{
if (float64_is_nan(a) | float64_is_nan(b)) {
if (float64_is_signaling_nan(a) | float64_is_signaling_nan(b))
float_set_invalid();
return CMP_RESULT(1);
}
return CMP_RESULT(0);
}
static float64
compare_eq(float64 a, float64 b)
{
return CMP_RESULT(float64_eq(a, b));
}
static void
cvt_qs_ts_st_gf_qf(u_int32_t inst_bits, struct proc *p)
{
t_float tfb, tfc;
s_float sfb, sfc;
alpha_instruction inst;
inst.bits = inst_bits;
switch(inst.float_format.function) {
case op_cvtst:
case op_cvtst_u:
sts(inst.float_detail.fb, &sfb, p);
tfc.i = float32_to_float64(sfb.i);
ldt(inst.float_detail.fc, &tfc, p);
return;
}
if(inst.float_detail.src == 2) {
stt(inst.float_detail.fb, &tfb, p);
sfc.i = float64_to_float32(tfb.i);
lds(inst.float_detail.fc, &sfc, p);
return;
}
this_cannot_happen(5, inst.generic_format.opcode);
tfc.i = FLOAT64QNAN;
ldt(inst.float_detail.fc, &tfc, p);
return;
}
static void
cvt_gd(u_int32_t inst_bits, struct proc *p)
{
t_float tfb, tfc;
alpha_instruction inst;
inst.bits = inst_bits;
stt(inst.float_detail.fb, &tfb, p);
(void) float64_to_float32(tfb.i);
p->p_md.md_flags &= ~OPENBSD_FLAG_TO_FP_C(FP_X_IMP);
tfc.i = float64_add(tfb.i, (float64)0);
ldt(inst.float_detail.fc, &tfc, p);
}
static void
cvt_qt_dg_qg(u_int32_t inst_bits, struct proc *p)
{
t_float tfb, tfc;
alpha_instruction inst;
inst.bits = inst_bits;
switch(inst.float_detail.src) {
case 0:
this_cannot_happen(3, inst.bits);
case 1:
tfc.i = 0;
break;
case 2:
this_cannot_happen(4, inst.bits);
tfc.i = 0;
break;
case 3:
stt(inst.float_detail.fb, &tfb, p);
tfc.i = int64_to_float64(tfb.i);
break;
}
alpha_ldt(inst.float_detail.fc, &tfc);
}
static void
cvt_tq_gq(u_int32_t inst_bits, struct proc *p)
{
t_float tfb, tfc;
alpha_instruction inst;
inst.bits = inst_bits;
stt(inst.float_detail.fb, &tfb, p);
tfc.i = float64_to_int64_no_overflow(tfb.i);
alpha_ldt(inst.float_detail.fc, &tfc);
}
static u_int64_t
fp_c_to_fpcr_1(u_int64_t fpcr, u_int64_t fp_c)
{
u_int64_t disables;
fpcr &= FPCR_DYN(3);
disables = FP_C_TO_OPENBSD_FLAG(fp_c) & ~FP_C_TO_OPENBSD_MASK(fp_c);
fpcr |= (disables & (FP_X_IMP | FP_X_UFL)) << (61 - 3);
fpcr |= (disables & (FP_X_OFL | FP_X_DZ | FP_X_INV)) << (49 - 0);
# if !(FP_X_INV == 1 && FP_X_DZ == 2 && FP_X_OFL == 4 && \
FP_X_UFL == 8 && FP_X_IMP == 16 && FP_X_IOV == 32 && \
FP_X_UFL << (61 - 3) == FPCR_UNFD && \
FP_X_IMP << (61 - 3) == FPCR_INED && \
FP_X_OFL << (49 - 0) == FPCR_OVFD)
# error "Assertion failed"
# endif
fpcr |= fp_c & FP_C_MIRRORED << (FPCR_MIR_START - FP_C_MIR_START);
fpcr |= (fp_c & IEEE_MAP_DMZ) << 36;
if (fp_c & FP_C_MIRRORED)
fpcr |= FPCR_SUM;
if (fp_c & IEEE_MAP_UMZ)
fpcr |= FPCR_UNDZ | FPCR_UNFD;
fpcr |= (~fp_c & IEEE_TRAP_ENABLE_DNO) << 41;
return fpcr;
}
static void
fp_c_to_fpcr(struct proc *p)
{
alpha_write_fpcr(fp_c_to_fpcr_1(alpha_read_fpcr(), p->p_md.md_flags));
}
void
alpha_write_fp_c(struct proc *p, u_int64_t fp_c)
{
u_int64_t md_flags;
fp_c &= MDP_FP_C;
md_flags = p->p_md.md_flags;
if ((md_flags & MDP_FP_C) == fp_c)
return;
p->p_md.md_flags = (md_flags & ~MDP_FP_C) | fp_c;
alpha_enable_fp(p, 1);
alpha_pal_wrfen(1);
fp_c_to_fpcr(p);
alpha_pal_wrfen(0);
}
u_int64_t
alpha_read_fp_c(struct proc *p)
{
return p->p_md.md_flags & MDP_FP_C;
}
static float64
float64_unk(float64 a, float64 b)
{
return 0;
}
static void
alpha_fp_interpret(struct proc *p, u_int64_t bits)
{
s_float sfa, sfb, sfc;
t_float tfa, tfb, tfc;
alpha_instruction inst;
inst.bits = bits;
switch(inst.generic_format.opcode) {
default:
this_cannot_happen(2, inst.bits);
return;
case op_any_float:
if (inst.float_format.function == op_cvtql_sv ||
inst.float_format.function == op_cvtql_v) {
alpha_stt(inst.float_detail.fb, &tfb);
sfc.i = (int64_t)tfb.i >= 0L ? INT_MAX : INT_MIN;
alpha_lds(inst.float_detail.fc, &sfc);
float_raise(FP_X_INV);
} else {
++alpha_shadow.nilanyop;
this_cannot_happen(3, inst.bits);
}
break;
case op_vax_float:
++alpha_shadow.vax;
case op_ieee_float:
case op_fix_float:
switch(inst.float_detail.src) {
case op_src_sf:
sts(inst.float_detail.fb, &sfb, p);
if (inst.float_detail.opclass == 10)
sfc.i = float32_sqrt(sfb.i);
else if (inst.float_detail.opclass & ~3) {
this_cannot_happen(1, inst.bits);
sfc.i = FLOAT32QNAN;
} else {
sts(inst.float_detail.fa, &sfa, p);
sfc.i = (*swfp_s[inst.float_detail.opclass])(
sfa.i, sfb.i);
}
lds(inst.float_detail.fc, &sfc, p);
break;
case op_src_xd:
case op_src_tg:
if (inst.float_detail.opclass >= 12)
(*swfp_cvt[inst.float_detail.opclass - 12])(
inst.bits, p);
else {
stt(inst.float_detail.fb, &tfb, p);
if (inst.float_detail.opclass == 10)
tfc.i = float64_sqrt(tfb.i);
else {
stt(inst.float_detail.fa, &tfa, p);
tfc.i = (*swfp_t[inst.float_detail
.opclass])(tfa.i, tfb.i);
}
ldt(inst.float_detail.fc, &tfc, p);
}
break;
case op_src_qq:
float_raise(FP_X_IMP);
break;
}
}
}
int
alpha_fp_complete_at(u_long trigger_pc, struct proc *p, u_int64_t *ucode)
{
int needsig;
alpha_instruction inst;
u_int64_t rm, fpcr, orig_fpcr;
u_int64_t orig_flags, new_flags, changed_flags, md_flags;
if (__predict_false(copyinsn(NULL, (u_int32_t *)trigger_pc,
(u_int32_t *)&inst))) {
this_cannot_happen(6, -1);
return SIGSEGV;
}
alpha_enable_fp(p, 1);
alpha_pal_wrfen(1);
orig_fpcr = fpcr = alpha_read_fpcr();
rm = inst.float_detail.rnd;
if (__predict_false(rm != 3 && rm != (fpcr >> 58 & 3))) {
fpcr = (fpcr & ~FPCR_DYN(3)) | FPCR_DYN(rm);
alpha_write_fpcr(fpcr);
}
orig_flags = FP_C_TO_OPENBSD_FLAG(p->p_md.md_flags);
alpha_fp_interpret(p, inst.bits);
md_flags = p->p_md.md_flags;
new_flags = FP_C_TO_OPENBSD_FLAG(md_flags);
changed_flags = orig_flags ^ new_flags;
KASSERT((orig_flags | changed_flags) == new_flags);
alpha_write_fpcr(fp_c_to_fpcr_1(orig_fpcr, md_flags));
needsig = changed_flags & FP_C_TO_OPENBSD_MASK(md_flags);
alpha_pal_wrfen(0);
if (__predict_false(needsig)) {
*ucode = needsig;
return SIGFPE;
}
return 0;
}
int
alpha_fp_complete(u_long a0, u_long a1, struct proc *p, u_int64_t *ucode)
{
int t;
int sig;
u_int64_t op_class;
alpha_instruction inst;
u_long trigger_pc, usertrap_pc;
alpha_instruction *pc, *win_begin, tsw[TSWINSIZE];
sig = SIGFPE;
pc = (alpha_instruction *)p->p_md.md_tf->tf_regs[FRAME_PC];
trigger_pc = (u_long)pc - 4;
if (cpu_amask & ALPHA_AMASK_PAT) {
if (a0 & 1 || alpha_fp_sync_complete) {
sig = alpha_fp_complete_at(trigger_pc, p, ucode);
goto done;
}
}
*ucode = a0;
if (!(a0 & 1))
return sig;
trigger_pc = 0;
win_begin = pc;
++alpha_shadow.scans;
t = alpha_shadow.len;
for (--pc; a1; --pc) {
++alpha_shadow.len;
if (pc < win_begin) {
win_begin = pc - TSWINSIZE + 1;
if (_copyin(win_begin, tsw, sizeof tsw)) {
win_begin = pc;
if (_copyin(win_begin, tsw, 4))
return SIGSEGV;
}
}
inst = tsw[pc - win_begin];
op_class = 1UL << inst.generic_format.opcode;
if (op_class & FPUREG_CLASS) {
a1 &= ~(1UL << (inst.operate_generic_format.rc + 32));
trigger_pc = (u_long)pc;
} else if (op_class & CPUREG_CLASS) {
a1 &= ~(1UL << inst.operate_generic_format.rc);
trigger_pc = (u_long)pc;
} else if (op_class & TRAPSHADOWBOUNDARY) {
if (op_class & CHECKFUNCTIONCODE) {
if (inst.mem_format.displacement == op_trapb ||
inst.mem_format.displacement == op_excb)
break;
} else
break;
}
}
t = alpha_shadow.len - t;
if (t > alpha_shadow.max)
alpha_shadow.max = t;
if (__predict_true(trigger_pc != 0 && a1 == 0)) {
++alpha_shadow.resolved;
sig = alpha_fp_complete_at(trigger_pc, p, ucode);
} else {
++alpha_shadow.unresolved;
return sig;
}
done:
if (sig) {
usertrap_pc = trigger_pc + 4;
p->p_md.md_tf->tf_regs[FRAME_PC] = usertrap_pc;
return sig;
}
return 0;
}