#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.27 2020/04/16 05:44:43 skrll Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <uvm/uvm_extern.h>
#include <machine/cpufunc.h>
#include <machine/frame.h>
#include <machine/reg.h>
#include <machine/pcb.h>
#include <machine/pmap.h>
#include <hppa/hppa/machdep.h>
#include "../spmath/float.h"
#include "../spmath/fpudispatch.h"
#define OPCODE_NOP 0x08000240
#define OPCODE_COPR_0_0 0x30000000
#define OPCODE_CMPLT_S 0x00002000
#define OPCODE_CMPLT_M 0x00000020
#define OPCODE_CMPLT_SM (OPCODE_CMPLT_S | OPCODE_CMPLT_M)
#define OPCODE_CMPLT_MB OPCODE_CMPLT_M
#define OPCODE_CMPLT_MA (OPCODE_CMPLT_S | OPCODE_CMPLT_M)
#define OPCODE_CMPLT (OPCODE_CMPLT_S | OPCODE_CMPLT_M)
#define OPCODE_DOUBLE 0x08000000
#define OPCODE_STORE 0x00000200
#define OPCODE_INDEXED 0x00001000
int fpu_present;
u_int fpu_version;
u_int fpu_csw;
void hppa_fpu_swapout(struct pcb *);
void hppa_fpu_swap(struct fpreg *, struct fpreg *);
static int hppa_fpu_ls(struct trapframe *, struct lwp *);
#define FRAME_REG(f, reg, r0) \
((reg) == 0 ? (&r0) : ((&(f)->tf_t1) + _frame_reg_positions[reg]))
#define _FRAME_POSITION(f) \
((&((struct trapframe *) 0)->f) - (&((struct trapframe *) 0)->tf_t1))
const int _frame_reg_positions[32] = {
-1,
_FRAME_POSITION(tf_r1),
_FRAME_POSITION(tf_rp),
_FRAME_POSITION(tf_r3),
_FRAME_POSITION(tf_r4),
_FRAME_POSITION(tf_r5),
_FRAME_POSITION(tf_r6),
_FRAME_POSITION(tf_r7),
_FRAME_POSITION(tf_r8),
_FRAME_POSITION(tf_r9),
_FRAME_POSITION(tf_r10),
_FRAME_POSITION(tf_r11),
_FRAME_POSITION(tf_r12),
_FRAME_POSITION(tf_r13),
_FRAME_POSITION(tf_r14),
_FRAME_POSITION(tf_r15),
_FRAME_POSITION(tf_r16),
_FRAME_POSITION(tf_r17),
_FRAME_POSITION(tf_r18),
_FRAME_POSITION(tf_t4),
_FRAME_POSITION(tf_t3),
_FRAME_POSITION(tf_t2),
_FRAME_POSITION(tf_t1),
_FRAME_POSITION(tf_arg3),
_FRAME_POSITION(tf_arg2),
_FRAME_POSITION(tf_arg1),
_FRAME_POSITION(tf_arg0),
_FRAME_POSITION(tf_dp),
_FRAME_POSITION(tf_ret0),
_FRAME_POSITION(tf_ret1),
_FRAME_POSITION(tf_sp),
_FRAME_POSITION(tf_r31),
};
void
hppa_fpu_bootstrap(u_int ccr_enable)
{
uint32_t junk[2];
uint32_t vers[2];
fpu_present = (ccr_enable & HPPA_FPUS) == HPPA_FPUS;
if (!fpu_present) {
fpu_csw = 0;
curcpu()->ci_fpu_state = 0;
return;
}
KASSERT(fpu_present);
__asm volatile(
" ldo %0, %%r22 \n"
" fstds %%fr0, 0(%%r22) \n"
" ldo %1, %%r22 \n"
" copr,0,0 \n"
" fstds %%fr0, 0(%%r22) \n"
: "=m" (junk), "=m" (vers) : : "r22");
fpu_csw = 0;
curcpu()->ci_fpu_state = 0;
mtctl(ccr_enable & (CCR_MASK ^ HPPA_FPUS), CR_CCR);
fpu_version = vers[0];
}
void
hppa_fpu_flush(struct lwp *l)
{
struct trapframe *tf = l->l_md.md_regs;
struct pcb *pcb = lwp_getpcb(l);
struct cpu_info *ci = curcpu();
if (!fpu_present)
return;
if (ci->ci_fpu_state == 0 ||
ci->ci_fpu_state != tf->tf_cr30) {
return;
}
hppa_fpu_swapout(pcb);
ci->ci_fpu_state = 0;
}
static int
hppa_fpu_ls(struct trapframe *frame, struct lwp *l)
{
struct pcb *pcb = lwp_getpcb(l);
u_int inst, inst_b, inst_x, inst_s, inst_t;
int log2size;
u_int *base;
u_int offset, index, im5;
void *fpreg;
u_int r0 = 0;
int error;
inst = frame->tf_iir;
__asm volatile(
" extru %4, 10, 5, %1 \n"
" extru %4, 15, 5, %2 \n"
" extru %4, 17, 2, %3 \n"
" extru %4, 31, 5, %4 \n"
: "=r" (inst_b), "=r" (inst_x), "=r" (inst_s), "=r" (inst_t)
: "r" (inst));
if (inst_s != pcb->pcb_space)
return EFAULT;
log2size = (inst & OPCODE_DOUBLE) ? 3 : 2;
fpreg = ((char *)pcb->pcb_fpregs) + (inst_t << log2size);
base = FRAME_REG(frame, inst_b, r0);
if (inst & OPCODE_INDEXED) {
index = *FRAME_REG(frame, inst_x, r0);
switch (inst & OPCODE_CMPLT) {
case OPCODE_CMPLT_S:
offset = *base + (index << log2size);
break;
case OPCODE_CMPLT_M:
offset = *base;
*base = *base + index;
break;
case OPCODE_CMPLT_SM:
offset = *base;
*base = *base + (index << log2size);
break;
default:
offset = *base + index;
break;
}
} else {
im5 = inst_x >> 1;
if (inst_x & 1)
im5 |= 0xfffffff0;
switch (inst & OPCODE_CMPLT) {
case OPCODE_CMPLT_MB:
offset = *base + im5;
*base = *base + im5;
break;
case OPCODE_CMPLT_MA:
offset = *base;
*base = *base + im5;
break;
default:
offset = *base + im5;
break;
}
}
KASSERT(offset == frame->tf_ior);
error = (inst & OPCODE_STORE) ?
copyout(fpreg, (void *) offset, 1 << log2size) :
copyin((const void *) offset, fpreg, 1 << log2size);
return error;
}
void
hppa_fpu_emulate(struct trapframe *frame, struct lwp *l, u_int inst)
{
struct pcb *pcb = lwp_getpcb(l);
u_int opcode, class, sub;
u_int *fpregs;
int exception;
ksiginfo_t ksi;
hppa_fpu_flush(l);
#if 0
__asm volatile(
" extru %3, 22, 2, %1 \n"
" extru %3, 5, 6, %0 \n"
" extru %3, 18, 3, %2 \n"
" comib,<> 1, %1, 0 \n"
" extru %3, 16, 2, %2 \n"
: "=r" (opcode), "=r" (class), "=r" (sub)
: "r" (inst));
#else
opcode = (inst >> (31 - 5)) & 0x3f;
class = (inst >> (31 - 22)) & 0x3;
if (class == 1) {
sub = (inst >> (31 - 16)) & 3;
} else {
sub = (inst >> (31 - 18)) & 7;
}
#endif
fpregs = (u_int *)pcb->pcb_fpregs;
switch (opcode) {
case 0x09:
case 0x0b:
if (hppa_fpu_ls(frame, l) != 0) {
KSI_INIT_TRAP(&ksi);
ksi.ksi_signo = SIGSEGV;
ksi.ksi_code = SEGV_MAPERR;
ksi.ksi_trap = T_DTLBMISS;
ksi.ksi_addr = (void *)frame->tf_iioq_head;
trapsignal(l, &ksi);
}
return;
case 0x0c:
exception = decode_0c(inst, class, sub, fpregs);
break;
case 0x0e:
exception = decode_0e(inst, class, sub, fpregs);
break;
case 0x06:
exception = decode_06(inst, fpregs);
break;
case 0x26:
exception = decode_26(inst, fpregs);
break;
default:
exception = UNIMPLEMENTEDEXCEPTION;
break;
}
if (exception) {
KSI_INIT_TRAP(&ksi);
if (exception & UNIMPLEMENTEDEXCEPTION) {
ksi.ksi_signo = SIGILL;
ksi.ksi_code = ILL_COPROC;
} else {
ksi.ksi_signo = SIGFPE;
if (exception & INVALIDEXCEPTION) {
ksi.ksi_code = FPE_FLTINV;
} else if (exception & DIVISIONBYZEROEXCEPTION) {
ksi.ksi_code = FPE_FLTDIV;
} else if (exception & OVERFLOWEXCEPTION) {
ksi.ksi_code = FPE_FLTOVF;
} else if (exception & UNDERFLOWEXCEPTION) {
ksi.ksi_code = FPE_FLTUND;
} else if (exception & INEXACTEXCEPTION) {
ksi.ksi_code = FPE_FLTRES;
}
}
ksi.ksi_trap = T_EMULATION;
ksi.ksi_addr = (void *)frame->tf_iioq_head;
trapsignal(l, &ksi);
}
}