cregit-Linux how code gets into the kernel

Release 4.14 arch/powerpc/kvm/book3s_paired_singles.c

Directory: arch/powerpc/kvm
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright Novell Inc 2010
 *
 * Authors: Alexander Graf <agraf@suse.de>
 */

#include <asm/kvm.h>
#include <asm/kvm_ppc.h>
#include <asm/disassemble.h>
#include <asm/kvm_book3s.h>
#include <asm/kvm_fpu.h>
#include <asm/reg.h>
#include <asm/cacheflush.h>
#include <asm/switch_to.h>
#include <linux/vmalloc.h>

/* #define DEBUG */

#ifdef DEBUG

#define dprintk printk
#else

#define dprintk(...) do { } while(0);
#endif


#define OP_LFS			48

#define OP_LFSU			49

#define OP_LFD			50

#define OP_LFDU			51

#define OP_STFS			52

#define OP_STFSU		53

#define OP_STFD			54

#define OP_STFDU		55

#define OP_PSQ_L		56

#define OP_PSQ_LU		57

#define OP_PSQ_ST		60

#define OP_PSQ_STU		61


#define OP_31_LFSX		535

#define OP_31_LFSUX		567

#define OP_31_LFDX		599

#define OP_31_LFDUX		631

#define OP_31_STFSX		663

#define OP_31_STFSUX		695

#define OP_31_STFX		727

#define OP_31_STFUX		759

#define OP_31_LWIZX		887

#define OP_31_STFIWX		983


#define OP_59_FADDS		21

#define OP_59_FSUBS		20

#define OP_59_FSQRTS		22

#define OP_59_FDIVS		18

#define OP_59_FRES		24

#define OP_59_FMULS		25

#define OP_59_FRSQRTES		26

#define OP_59_FMSUBS		28

#define OP_59_FMADDS		29

#define OP_59_FNMSUBS		30

#define OP_59_FNMADDS		31


#define OP_63_FCMPU		0

#define OP_63_FCPSGN		8

#define OP_63_FRSP		12

#define OP_63_FCTIW		14

#define OP_63_FCTIWZ		15

#define OP_63_FDIV		18

#define OP_63_FADD		21

#define OP_63_FSQRT		22

#define OP_63_FSEL		23

#define OP_63_FRE		24

#define OP_63_FMUL		25

#define OP_63_FRSQRTE		26

#define OP_63_FMSUB		28

#define OP_63_FMADD		29

#define OP_63_FNMSUB		30

#define OP_63_FNMADD		31

#define OP_63_FCMPO		32

#define OP_63_MTFSB1		38 
// XXX

#define OP_63_FSUB		20

#define OP_63_FNEG		40

#define OP_63_MCRFS		64

#define OP_63_MTFSB0		70

#define OP_63_FMR		72

#define OP_63_MTFSFI		134

#define OP_63_FABS		264

#define OP_63_MFFS		583

#define OP_63_MTFSF		711


#define OP_4X_PS_CMPU0		0

#define OP_4X_PSQ_LX		6

#define OP_4XW_PSQ_STX		7

#define OP_4A_PS_SUM0		10

#define OP_4A_PS_SUM1		11

#define OP_4A_PS_MULS0		12

#define OP_4A_PS_MULS1		13

#define OP_4A_PS_MADDS0		14

#define OP_4A_PS_MADDS1		15

#define OP_4A_PS_DIV		18

#define OP_4A_PS_SUB		20

#define OP_4A_PS_ADD		21

#define OP_4A_PS_SEL		23

#define OP_4A_PS_RES		24

#define OP_4A_PS_MUL		25

#define OP_4A_PS_RSQRTE		26

#define OP_4A_PS_MSUB		28

#define OP_4A_PS_MADD		29

#define OP_4A_PS_NMSUB		30

#define OP_4A_PS_NMADD		31

#define OP_4X_PS_CMPO0		32

#define OP_4X_PSQ_LUX		38

#define OP_4XW_PSQ_STUX		39

#define OP_4X_PS_NEG		40

#define OP_4X_PS_CMPU1		64

#define OP_4X_PS_MR		72

#define OP_4X_PS_CMPO1		96

#define OP_4X_PS_NABS		136

#define OP_4X_PS_ABS		264

#define OP_4X_PS_MERGE00	528

#define OP_4X_PS_MERGE01	560

#define OP_4X_PS_MERGE10	592

#define OP_4X_PS_MERGE11	624


#define SCALAR_NONE		0

#define SCALAR_HIGH		(1 << 0)

#define SCALAR_LOW		(1 << 1)

#define SCALAR_NO_PS0		(1 << 2)

#define SCALAR_NO_PS1		(1 << 3)


#define GQR_ST_TYPE_MASK	0x00000007

#define GQR_ST_TYPE_SHIFT	0

#define GQR_ST_SCALE_MASK	0x00003f00

#define GQR_ST_SCALE_SHIFT	8

#define GQR_LD_TYPE_MASK	0x00070000

#define GQR_LD_TYPE_SHIFT	16

#define GQR_LD_SCALE_MASK	0x3f000000

#define GQR_LD_SCALE_SHIFT	24


#define GQR_QUANTIZE_FLOAT	0

#define GQR_QUANTIZE_U8		4

#define GQR_QUANTIZE_U16	5

#define GQR_QUANTIZE_S8		6

#define GQR_QUANTIZE_S16	7


#define FPU_LS_SINGLE		0

#define FPU_LS_DOUBLE		1

#define FPU_LS_SINGLE_LOW	2


static inline void kvmppc_sync_qpr(struct kvm_vcpu *vcpu, int rt) { kvm_cvt_df(&VCPU_FPR(vcpu, rt), &vcpu->arch.qpr[rt]); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf3186.11%133.33%
Paul Mackerras411.11%133.33%
Andreas Schwab12.78%133.33%
Total36100.00%3100.00%


static void kvmppc_inject_pf(struct kvm_vcpu *vcpu, ulong eaddr, bool is_store) { u32 dsisr; u64 msr = kvmppc_get_msr(vcpu); msr = kvmppc_set_field(msr, 33, 36, 0); msr = kvmppc_set_field(msr, 42, 47, 0); kvmppc_set_msr(vcpu, msr); kvmppc_set_dar(vcpu, eaddr); /* Page Fault */ dsisr = kvmppc_set_field(0, 33, 33, 1); if (is_store) dsisr = kvmppc_set_field(dsisr, 38, 38, 1); kvmppc_set_dsisr(vcpu, dsisr); kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf113100.00%3100.00%
Total113100.00%3100.00%


static int kvmppc_emulate_fpr_load(struct kvm_run *run, struct kvm_vcpu *vcpu, int rs, ulong addr, int ls_type) { int emulated = EMULATE_FAIL; int r; char tmp[8]; int len = sizeof(u32); if (ls_type == FPU_LS_DOUBLE) len = sizeof(u64); /* read from memory */ r = kvmppc_ld(vcpu, &addr, len, tmp, true); vcpu->arch.paddr_accessed = addr; if (r < 0) { kvmppc_inject_pf(vcpu, addr, false); goto done_load; } else if (r == EMULATE_DO_MMIO) { emulated = kvmppc_handle_load(run, vcpu, KVM_MMIO_REG_FPR | rs, len, 1); goto done_load; } emulated = EMULATE_DONE; /* put in registers */ switch (ls_type) { case FPU_LS_SINGLE: kvm_cvt_fd((u32*)tmp, &VCPU_FPR(vcpu, rs)); vcpu->arch.qpr[rs] = *((u32*)tmp); break; case FPU_LS_DOUBLE: VCPU_FPR(vcpu, rs) = *((u64*)tmp); break; } dprintk(KERN_INFO "KVM: FPR_LD [0x%llx] at 0x%lx (%d)\n", *(u64*)tmp, addr, len); done_load: return emulated; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf21695.58%250.00%
Paul Mackerras83.54%125.00%
Andreas Schwab20.88%125.00%
Total226100.00%4100.00%


static int kvmppc_emulate_fpr_store(struct kvm_run *run, struct kvm_vcpu *vcpu, int rs, ulong addr, int ls_type) { int emulated = EMULATE_FAIL; int r; char tmp[8]; u64 val; int len; switch (ls_type) { case FPU_LS_SINGLE: kvm_cvt_df(&VCPU_FPR(vcpu, rs), (u32*)tmp); val = *((u32*)tmp); len = sizeof(u32); break; case FPU_LS_SINGLE_LOW: *((u32*)tmp) = VCPU_FPR(vcpu, rs); val = VCPU_FPR(vcpu, rs) & 0xffffffff; len = sizeof(u32); break; case FPU_LS_DOUBLE: *((u64*)tmp) = VCPU_FPR(vcpu, rs); val = VCPU_FPR(vcpu, rs); len = sizeof(u64); break; default: val = 0; len = 0; } r = kvmppc_st(vcpu, &addr, len, tmp, true); vcpu->arch.paddr_accessed = addr; if (r < 0) { kvmppc_inject_pf(vcpu, addr, true); } else if (r == EMULATE_DO_MMIO) { emulated = kvmppc_handle_store(run, vcpu, val, len, 1); } else { emulated = EMULATE_DONE; } dprintk(KERN_INFO "KVM: FPR_ST [0x%llx] at 0x%lx (%d)\n", val, addr, len); return emulated; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf23891.54%133.33%
Paul Mackerras207.69%133.33%
Andreas Schwab20.77%133.33%
Total260100.00%3100.00%


static int kvmppc_emulate_psq_load(struct kvm_run *run, struct kvm_vcpu *vcpu, int rs, ulong addr, bool w, int i) { int emulated = EMULATE_FAIL; int r; float one = 1.0; u32 tmp[2]; /* read from memory */ if (w) { r = kvmppc_ld(vcpu, &addr, sizeof(u32), tmp, true); memcpy(&tmp[1], &one, sizeof(u32)); } else { r = kvmppc_ld(vcpu, &addr, sizeof(u32) * 2, tmp, true); } vcpu->arch.paddr_accessed = addr; if (r < 0) { kvmppc_inject_pf(vcpu, addr, false); goto done_load; } else if ((r == EMULATE_DO_MMIO) && w) { emulated = kvmppc_handle_load(run, vcpu, KVM_MMIO_REG_FPR | rs, 4, 1); vcpu->arch.qpr[rs] = tmp[1]; goto done_load; } else if (r == EMULATE_DO_MMIO) { emulated = kvmppc_handle_load(run, vcpu, KVM_MMIO_REG_FQPR | rs, 8, 1); goto done_load; } emulated = EMULATE_DONE; /* put in registers */ kvm_cvt_fd(&tmp[0], &VCPU_FPR(vcpu, rs)); vcpu->arch.qpr[rs] = tmp[1]; dprintk(KERN_INFO "KVM: PSQ_LD [0x%x, 0x%x] at 0x%lx (%d)\n", tmp[0], tmp[1], addr, w ? 4 : 8); done_load: return emulated; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf27898.23%250.00%
Paul Mackerras41.41%125.00%
Andreas Schwab10.35%125.00%
Total283100.00%4100.00%


static int kvmppc_emulate_psq_store(struct kvm_run *run, struct kvm_vcpu *vcpu, int rs, ulong addr, bool w, int i) { int emulated = EMULATE_FAIL; int r; u32 tmp[2]; int len = w ? sizeof(u32) : sizeof(u64); kvm_cvt_df(&VCPU_FPR(vcpu, rs), &tmp[0]); tmp[1] = vcpu->arch.qpr[rs]; r = kvmppc_st(vcpu, &addr, len, tmp, true); vcpu->arch.paddr_accessed = addr; if (r < 0) { kvmppc_inject_pf(vcpu, addr, true); } else if ((r == EMULATE_DO_MMIO) && w) { emulated = kvmppc_handle_store(run, vcpu, tmp[0], 4, 1); } else if (r == EMULATE_DO_MMIO) { u64 val = ((u64)tmp[0] << 32) | tmp[1]; emulated = kvmppc_handle_store(run, vcpu, val, 8, 1); } else { emulated = EMULATE_DONE; } dprintk(KERN_INFO "KVM: PSQ_ST [0x%x, 0x%x] at 0x%lx (%d)\n", tmp[0], tmp[1], addr, len); return emulated; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf22997.86%133.33%
Paul Mackerras41.71%133.33%
Andreas Schwab10.43%133.33%
Total234100.00%3100.00%

/* * Cuts out inst bits with ordering according to spec. * That means the leftmost bit is zero. All given bits are included. */
static inline u32 inst_get_field(u32 inst, int msb, int lsb) { return kvmppc_get_field(inst, msb + 32, lsb + 32); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf30100.00%1100.00%
Total30100.00%1100.00%


static bool kvmppc_inst_is_paired_single(struct kvm_vcpu *vcpu, u32 inst) { if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)) return false; switch (get_op(inst)) { case OP_PSQ_L: case OP_PSQ_LU: case OP_PSQ_ST: case OP_PSQ_STU: case OP_LFS: case OP_LFSU: case OP_LFD: case OP_LFDU: case OP_STFS: case OP_STFSU: case OP_STFD: case OP_STFDU: return true; case 4: /* X form */ switch (inst_get_field(inst, 21, 30)) { case OP_4X_PS_CMPU0: case OP_4X_PSQ_LX: case OP_4X_PS_CMPO0: case OP_4X_PSQ_LUX: case OP_4X_PS_NEG: case OP_4X_PS_CMPU1: case OP_4X_PS_MR: case OP_4X_PS_CMPO1: case OP_4X_PS_NABS: case OP_4X_PS_ABS: case OP_4X_PS_MERGE00: case OP_4X_PS_MERGE01: case OP_4X_PS_MERGE10: case OP_4X_PS_MERGE11: return true; } /* XW form */ switch (inst_get_field(inst, 25, 30)) { case OP_4XW_PSQ_STX: case OP_4XW_PSQ_STUX: return true; } /* A form */ switch (inst_get_field(inst, 26, 30)) { case OP_4A_PS_SUM1: case OP_4A_PS_SUM0: case OP_4A_PS_MULS0: case OP_4A_PS_MULS1: case OP_4A_PS_MADDS0: case OP_4A_PS_MADDS1: case OP_4A_PS_DIV: case OP_4A_PS_SUB: case OP_4A_PS_ADD: case OP_4A_PS_SEL: case OP_4A_PS_RES: case OP_4A_PS_MUL: case OP_4A_PS_RSQRTE: case OP_4A_PS_MSUB: case OP_4A_PS_MADD: case OP_4A_PS_NMSUB: case OP_4A_PS_NMADD: return true; } break; case 59: switch (inst_get_field(inst, 21, 30)) { case OP_59_FADDS: case OP_59_FSUBS: case OP_59_FDIVS: case OP_59_FRES: case OP_59_FRSQRTES: return true; } switch (inst_get_field(inst, 26, 30)) { case OP_59_FMULS: case OP_59_FMSUBS: case OP_59_FMADDS: case OP_59_FNMSUBS: case OP_59_FNMADDS: return true; } break; case 63: switch (inst_get_field(inst, 21, 30)) { case OP_63_MTFSB0: case OP_63_MTFSB1: case OP_63_MTFSF: case OP_63_MTFSFI: case OP_63_MCRFS: case OP_63_MFFS: case OP_63_FCMPU: case OP_63_FCMPO: case OP_63_FNEG: case OP_63_FMR: case OP_63_FABS: case OP_63_FRSP: case OP_63_FDIV: case OP_63_FADD: case OP_63_FSUB: case OP_63_FCTIW: case OP_63_FCTIWZ: case OP_63_FRSQRTE: case OP_63_FCPSGN: return true; } switch (inst_get_field(inst, 26, 30)) { case OP_63_FMUL: case OP_63_FSEL: case OP_63_FMSUB: case OP_63_FMADD: case OP_63_FNMSUB: case OP_63_FNMADD: return true; } break; case 31: switch (inst_get_field(inst, 21, 30)) { case OP_31_LFSX: case OP_31_LFSUX: case OP_31_LFDX: case OP_31_LFDUX: case OP_31_STFSX: case OP_31_STFSUX: case OP_31_STFX: case OP_31_STFUX: case OP_31_STFIWX: return true; } break; } return false; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf45899.78%150.00%
Thomas Huth10.22%150.00%
Total459100.00%2100.00%


static int get_d_signext(u32 inst) { int d = inst & 0x8ff; if (d & 0x800) return -(d & 0x7ff); return (d & 0x7ff); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf37100.00%1100.00%
Total37100.00%1100.00%


static int kvmppc_ps_three_in(struct kvm_vcpu *vcpu, bool rc, int reg_out, int reg_in1, int reg_in2, int reg_in3, int scalar, void (*func)(u64 *fpscr, u32 *dst, u32 *src1, u32 *src2, u32 *src3)) { u32 *qpr = vcpu->arch.qpr; u32 ps0_out; u32 ps0_in1, ps0_in2, ps0_in3; u32 ps1_in1, ps1_in2, ps1_in3; /* RC */ WARN_ON(rc); /* PS0 */ kvm_cvt_df(&VCPU_FPR(vcpu, reg_in1), &ps0_in1); kvm_cvt_df(&VCPU_FPR(vcpu, reg_in2), &ps0_in2); kvm_cvt_df(&VCPU_FPR(vcpu, reg_in3), &ps0_in3); if (scalar & SCALAR_LOW) ps0_in2 = qpr[reg_in2]; func(&vcpu->arch.fp.fpscr, &ps0_out, &ps0_in1, &ps0_in2, &ps0_in3); dprintk(KERN_INFO "PS3 ps0 -> f(0x%x, 0x%x, 0x%x) = 0x%x\n", ps0_in1, ps0_in2, ps0_in3, ps0_out); if (!(scalar & SCALAR_NO_PS0)) kvm_cvt_fd(&ps0_out, &VCPU_FPR(vcpu, reg_out)); /* PS1 */ ps1_in1 = qpr[reg_in1]; ps1_in2 = qpr[reg_in2]; ps1_in3 = qpr[reg_in3]; if (scalar & SCALAR_HIGH) ps1_in2 = ps0_in2; if (!(scalar & SCALAR_NO_PS1)) func(&vcpu->arch.fp.fpscr, &qpr[reg_out], &ps1_in1, &ps1_in2, &ps1_in3); dprintk(KERN_INFO "PS3 ps1 -> f(0x%x, 0x%x, 0x%x) = 0x%x\n", ps1_in1, ps1_in2, ps1_in3, qpr[reg_out]); return EMULATE_DONE; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf25486.39%133.33%
Paul Mackerras248.16%133.33%
Andreas Schwab165.44%133.33%
Total294100.00%3100.00%


static int kvmppc_ps_two_in(struct kvm_vcpu *vcpu, bool rc, int reg_out, int reg_in1, int reg_in2, int scalar, void (*func)(u64 *fpscr, u32 *dst, u32 *src1, u32 *src2)) { u32 *qpr = vcpu->arch.qpr; u32 ps0_out; u32 ps0_in1, ps0_in2; u32 ps1_out; u32 ps1_in1, ps1_in2; /* RC */ WARN_ON(rc); /* PS0 */ kvm_cvt_df(&VCPU_FPR(vcpu, reg_in1), &ps0_in1); if (scalar & SCALAR_LOW) ps0_in2 = qpr[reg_in2]; else kvm_cvt_df(&VCPU_FPR(vcpu, reg_in2), &ps0_in2); func(&vcpu->arch.fp.fpscr, &ps0_out, &ps0_in1, &ps0_in2); if (!(scalar & SCALAR_NO_PS0)) { dprintk(KERN_INFO "PS2 ps0 -> f(0x%x, 0x%x) = 0x%x\n", ps0_in1, ps0_in2, ps0_out); kvm_cvt_fd(&ps0_out, &VCPU_FPR(vcpu, reg_out)); } /* PS1 */ ps1_in1 = qpr[reg_in1]; ps1_in2 = qpr[reg_in2]; if (scalar & SCALAR_HIGH) ps1_in2 = ps0_in2; func(&vcpu->arch.fp.fpscr, &ps1_out, &ps1_in1, &ps1_in2); if (!(scalar & SCALAR_NO_PS1)) { qpr[reg_out] = ps1_out; dprintk(KERN_INFO "PS2 ps1 -> f(0x%x, 0x%x) = 0x%x\n", ps1_in1, ps1_in2, qpr[reg_out]); } return EMULATE_DONE; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf23087.12%133.33%
Paul Mackerras197.20%133.33%
Andreas Schwab155.68%133.33%
Total264100.00%3100.00%


static int kvmppc_ps_one_in(struct kvm_vcpu *vcpu, bool rc, int reg_out, int reg_in, void (*func)(u64 *t, u32 *dst, u32 *src1)) { u32 *qpr = vcpu->arch.qpr; u32 ps0_out, ps0_in; u32 ps1_in; /* RC */ WARN_ON(rc); /* PS0 */ kvm_cvt_df(&VCPU_FPR(vcpu, reg_in), &ps0_in); func(&vcpu->arch.fp.fpscr, &ps0_out, &ps0_in); dprintk(KERN_INFO "PS1 ps0 -> f(0x%x) = 0x%x\n", ps0_in, ps0_out); kvm_cvt_fd(&ps0_out, &VCPU_FPR(vcpu, reg_out)); /* PS1 */ ps1_in = qpr[reg_in]; func(&vcpu->arch.fp.fpscr, &qpr[reg_out], &ps1_in); dprintk(KERN_INFO "PS1 ps1 -> f(0x%x) = 0x%x\n", ps1_in, qpr[reg_out]); return EMULATE_DONE; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf13883.64%133.33%
Paul Mackerras148.48%133.33%
Andreas Schwab137.88%133.33%
Total165100.00%3100.00%


int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu) { u32 inst; enum emulation_result emulated = EMULATE_DONE; int ax_rd, ax_ra, ax_rb, ax_rc; short full_d; u64 *fpr_d, *fpr_a, *fpr_b, *fpr_c; bool rcomp; u32 cr; #ifdef DEBUG int i; #endif emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst); if (