IOCSR instruction supports 1/2/4/8 bytes access, the address should be naturally aligned with its access size. Here address alignment check is added in eiointc kernel emulation. At the same time len must be 1/2/4/8 bytes from iocsr exit emulation function kvm_emu_iocsr(), remove the default case in switch case statements. Signed-off-by: Bibo Mao <maobibo@xxxxxxxxxxx> --- arch/loongarch/kvm/intc/eiointc.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c index 8b0d9376eb54..4e9d12300cc4 100644 --- a/arch/loongarch/kvm/intc/eiointc.c +++ b/arch/loongarch/kvm/intc/eiointc.c @@ -311,6 +311,12 @@ static int kvm_eiointc_read(struct kvm_vcpu *vcpu, return -EINVAL; } + /* len must be 1/2/4/8 from function kvm_emu_iocsr() */ + if (addr & (len - 1)) { + kvm_err("%s: eiointc not aligned addr %llx len %d\n", __func__, addr, len); + return -EINVAL; + } + vcpu->stat.eiointc_read_exits++; spin_lock_irqsave(&eiointc->lock, flags); switch (len) { @@ -323,12 +329,9 @@ static int kvm_eiointc_read(struct kvm_vcpu *vcpu, case 4: ret = loongarch_eiointc_readl(vcpu, eiointc, addr, val); break; - case 8: + default: ret = loongarch_eiointc_readq(vcpu, eiointc, addr, val); break; - default: - WARN_ONCE(1, "%s: Abnormal address access: addr 0x%llx, size %d\n", - __func__, addr, len); } spin_unlock_irqrestore(&eiointc->lock, flags); @@ -682,6 +685,11 @@ static int kvm_eiointc_write(struct kvm_vcpu *vcpu, return -EINVAL; } + if (addr & (len - 1)) { + kvm_err("%s: eiointc not aligned addr %llx len %d\n", __func__, addr, len); + return -EINVAL; + } + vcpu->stat.eiointc_write_exits++; spin_lock_irqsave(&eiointc->lock, flags); switch (len) { @@ -694,12 +702,9 @@ static int kvm_eiointc_write(struct kvm_vcpu *vcpu, case 4: ret = loongarch_eiointc_writel(vcpu, eiointc, addr, val); break; - case 8: + default: ret = loongarch_eiointc_writeq(vcpu, eiointc, addr, val); break; - default: - WARN_ONCE(1, "%s: Abnormal address access: addr 0x%llx, size %d\n", - __func__, addr, len); } spin_unlock_irqrestore(&eiointc->lock, flags); -- 2.39.3