LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr()
Subsystems (4)
PATCH 1/1 LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr()
The LoongArch CSRXCHG instruction returns the full old CSR value in rd after applying the masked update. kvm_emu_xchg_csr() currently masks the saved value before returning it to the guest, so rd receives only the bits selected by the write mask.
That breaks the architectural behavior and makes a zero mask return 0 instead of the previous CSR value. Keep the masked CSR update, but return the unmodified old CSR value.
--- arch/loongarch/kvm/exit.c | 1 - 1 file changed, 1 deletion(-)
arch/loongarch/kvm/exit.c +0 -1
@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, old =kvm_read_sw_gcsr (csr, csrid); val = (old & ~csr_mask) | (val & csr_mask);kvm_write_sw_gcsr (csr, csrid, val); - old = old & csr_mask; }else pr_warn_once ("Unsupported csrxchg 0x%x with pc %lx\n" , csrid, vcpu->arch.pc);
Signature · 1 line
2.20.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Emulated instructions writing to $r0 corrupt vcpu->arch.gprs[0] for subsequent emulated instructions. --
commit 8b252fdbff14650492d9c9a2864373942e2fb35e
Author: Qiang Ma <[email protected]>
LoongArch: KVM: return full old CSR value from kvm_emu_xchg_csr()
The LoongArch CSRXCHG instruction returns the full old CSR value in rd after applying the masked update. kvm_emu_xchg_csr() currently masks the saved value before returning it to the guest, so rd receives only the bits selected by the write mask.
That breaks the architectural behavior and makes a zero mask return 0 instead of the previous CSR value. Keep the masked CSR update, but return the unmodified old CSR value.
arch/loongarch/kvm/exit.c static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, · line 108
Quoted 12 lines
arch/loongarch/kvm/exit.c +0
-1@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, old =kvm_read_sw_gcsr (csr, csrid); val = (old & ~csr_mask) | (val & csr_mask);kvm_write_sw_gcsr (csr, csrid, val); - old = old & csr_mask; }else pr_warn_once ("Unsupported csrxchg 0x%x with pc %lx\n" , csrid, vcpu->arch.pc);
[Severity: High]
This isn't a bug introduced by this patch, but does the return value from kvm_emu_xchg_csr() overwrite vcpu->arch.gprs[0] if the destination register rd is 0?
Looking at the caller kvm_handle_csr() in arch/loongarch/kvm/exit.c:
switch (rj) {
case 0: /* process csrrd */
val = kvm_emu_read_csr(vcpu, csrid);
vcpu->arch.gprs[rd] = val;
break;
...
default: /* process csrxchg */
val = vcpu->arch.gprs[rd];
csr_mask = vcpu->arch.gprs[rj];
val = kvm_emu_xchg_csr(vcpu, csrid, csr_mask, val);
vcpu->arch.gprs[rd] = val;
}Since $r0 is hardwired to 0 on LoongArch, if a guest executes an emulated instruction with $r0 as the destination, kvm_handle_csr() will write a non-zero value to vcpu->arch.gprs[0].
KVM does not clear vcpu->arch.gprs[0] between VM exits. If the guest subsequently executes another KVM-emulated instruction that uses $r0 as an input, KVM will read the stale non-zero value instead of 0.
Can this corrupt the guest context state for subsequent emulated instructions?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Bibo Mao
maintainerQuoted 21 lines · Qiang Ma
The LoongArch CSRXCHG instruction returns the full old CSR value in rd after applying the masked update.
kvm_emu_xchg_csr()currently masks the saved value before returning it to the guest, so rd receives only the bits selected by the write mask.That breaks the architectural behavior and makes a zero mask return 0 instead of the previous CSR value. Keep the masked CSR update, but return the unmodified old CSR value.
Fixes da50f5a693ff ("LoongArch: KVM: Implement handle csr exception")Signed-off-by Qiang Ma <[email protected]>--- arch/loongarch/kvm/exit.c | 1 - 1 file changed, 1 deletion(-)arch/loongarch/kvm/exit.c +0
-0
arch/loongarch/kvm/exit.c static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, · line 106
Quoted 4 lines
arch/loongarch/kvm/exit.c +0
-1@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, old =kvm_read_sw_gcsr (csr, csrid); val = (old & ~csr_mask) | (val & csr_mask);kvm_write_sw_gcsr (csr, csrid, val); - old = old & csr_mask;
Hi Qiang Ma
This is correct from the manual. Is there any test case or problem in practice? I want to evaluate severity about this problem.
Regards
Bibo Mao
Quoted 4 lines
arch/loongarch/kvm/exit.c +0
-0}else pr_warn_once ("Unsupported csrxchg 0x%x with pc %lx\n" , csrid, vcpu->arch.pc);
A message that is not in the archive (sent off-list or not yet received): 4CD61A5AB07B2388+7fa119a1-af5f-4def-b2c4-6073fc397b10@uniontech.com
Bibo Mao
maintainerQuoted 54 lines · Qiang Ma
Quoted 32 lines · Bibo Mao
Quoted 21 lines · Qiang Ma
The LoongArch CSRXCHG instruction returns the full old CSR value in rd after applying the masked update.
kvm_emu_xchg_csr()currently masks the saved value before returning it to the guest, so rd receives only the bits selected by the write mask.That breaks the architectural behavior and makes a zero mask return 0 instead of the previous CSR value. Keep the masked CSR update, but return the unmodified old CSR value.
Fixes da50f5a693ff ("LoongArch: KVM: Implement handle csr exception")Signed-off-by Qiang Ma <[email protected]>---
arch/loongarch/kvm/exit.c| 1 -1 file changed, 1 deletion(-)arch/loongarch/kvm/exit.c +0
-0arch/loongarch/kvm/exit.c static unsigned long kvm_emu_xchg_csr(struct · line 107
Quoted 5 lines
arch/loongarch/kvm/exit.c +0
-1@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, old = kvm_read_sw_gcsr(csr, csrid); val = (old & ~csr_mask) | (val & csr_mask); kvm_write_sw_gcsr(csr, csrid, val); - old = old & csr_mask;Hi Qiang Ma
Quoted 1 line
This is correct from the manual. Is there any test case or problem in practice? I want to evaluate severity about this problem.
Yes, I have written a selftest for this. Below are the test results comparing without and with the patch.
I have not encountered this problem in an actual operating environment.
without this patch:
[root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 (implementation-specific control 1)Initial guest CSR value: 0x10000100Checking that CSRXCHG updates the CSR per mask and returnsthe full old CSR value in rd.
Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned old CSR value : 0expected old CSR value : 0x10000100CSR value after update : 0x10000100expected CSR after update: 0x10000100result : FAIL
Case: partial-maskwrite value : 0write mask : 0x100returned old CSR value : 0x100expected old CSR value : 0x10000100CSR value after update : 0x10000000expected CSR after update: 0x10000000result : FAIL
CSRXCHG test FAILED
with this patch:
[root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 (implementation-specific control 1)Initial guest CSR value: 0x10000100Checking that CSRXCHG updates the CSR per mask and returnsthe full old CSR value in rd.
Quoted 7 lines
Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned old CSR value : 0x10000100expected old CSR value : 0x10000100CSR value after update : 0x10000100expected CSR after update: 0x10000100result : PASS
Case: partial-maskwrite value : 0write mask : 0x100returned old CSR value : 0x10000100expected old CSR value : 0x10000100CSR value after update : 0x10000000expected CSR after update: 0x10000000result : PASS
CSRXCHG test PASSED
Should this selftest case be included as a patch and sent along with version 2?
No, it is not necessary. I test csrxchg instruction by myself, the manual is right, return value should be the whole old value.
My meaning is that what is the scenery where CSR register is SW emulated in KVM mode, or this problem is found by code browsing.
Regards
Bibo Mao
Quoted 9 lines
Quoted 9 lines
Regards
Bibo MaoQuoted 3 lines
arch/loongarch/kvm/exit.c +0
-0} else pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", csrid, vcpu->arch.pc);
Quoted 59 lines · Bibo Mao
Quoted 56 lines · Qiang Ma
Quoted 32 lines · Bibo Mao
Quoted 21 lines · Qiang Ma
The LoongArch CSRXCHG instruction returns the full old CSR value in rd after applying the masked update.
kvm_emu_xchg_csr()currently masks the saved value before returning it to the guest, so rd receives only the bits selected by the write mask.That breaks the architectural behavior and makes a zero mask return 0 instead of the previous CSR value. Keep the masked CSR update, but return the unmodified old CSR value.
Fixes da50f5a693ff ("LoongArch: KVM: Implement handle csr exception")Signed-off-by Qiang Ma <[email protected]>---
arch/loongarch/kvm/exit.c| 1 -1 file changed, 1 deletion(-)arch/loongarch/kvm/exit.c +0
-0arch/loongarch/kvm/exit.c static unsigned long kvm_emu_xchg_csr(struct · line 107
Quoted 5 lines
arch/loongarch/kvm/exit.c +0
-1@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, old = kvm_read_sw_gcsr(csr, csrid); val = (old & ~csr_mask) | (val & csr_mask); kvm_write_sw_gcsr(csr, csrid, val); - old = old & csr_mask;Hi Qiang Ma
Quoted 1 line
This is correct from the manual. Is there any test case or problem in practice? I want to evaluate severity about this problem.
Yes, I have written a selftest for this. Below are the test results comparing without and with the patch.
I have not encountered this problem in an actual operating environment.
without this patch:
[root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 (implementation-specific control 1)Initial guest CSR value: 0x10000100Checking that CSRXCHG updates the CSR per mask and returnsthe full old CSR value in rd.
Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned old CSR value : 0expected old CSR value : 0x10000100CSR value after update : 0x10000100expected CSR after update: 0x10000100result : FAIL
Case: partial-maskwrite value : 0write mask : 0x100returned old CSR value : 0x100expected old CSR value : 0x10000100CSR value after update : 0x10000000expected CSR after update: 0x10000000result : FAIL
CSRXCHG test FAILED
with this patch:
[root@node1 loongarch]# ./csrxchg_testRandom seed: 0x6b8b4567Testing guest mode: PA-bits:47, VA-bits:47, 16K pagesTesting CSR: IMPCTL1 (implementation-specific control 1)Initial guest CSR value: 0x10000100Checking that CSRXCHG updates the CSR per mask and returnsthe full old CSR value in rd.
Case: zero-maskwrite value : 0xffffffffffffffffwrite mask : 0returned old CSR value : 0x10000100expected old CSR value : 0x10000100CSR value after update : 0x10000100expected CSR after update: 0x10000100result : PASS
Quoted 8 lines
Quoted 5 lines
Case: partial-maskwrite value : 0write mask : 0x100returned old CSR value : 0x10000100expected old CSR value : 0x10000100CSR value after update : 0x10000000expected CSR after update: 0x10000000result : PASS
CSRXCHG test PASSED
Should this selftest case be included as a patch and sent along with version 2?
No, it is not necessary. I test csrxchg instruction by myself, the manual is right, return value should be the whole old value.
My meaning is that what is the scenery where CSR register is SW emulated in KVM mode, or this problem is found by code browsing.
Understood. This was discovered while browsing the code.
Quoted 15 lines
Regards
Bibo MaoQuoted 9 lines
Quoted 9 lines
Regards
Bibo MaoQuoted 3 lines
arch/loongarch/kvm/exit.c +0
-0} else pr_warn_once("Unsupported csrxchg 0x%x with pc %lx\n", csrid, vcpu->arch.pc);
Quoted 22 lines · Qiang Ma
The LoongArch CSRXCHG instruction returns the full old CSR value in rd after applying the masked update.
kvm_emu_xchg_csr()currently masks the saved value before returning it to the guest, so rd receives only the bits selected by the write mask.That breaks the architectural behavior and makes a zero mask return 0 instead of the previous CSR value. Keep the masked CSR update, but return the unmodified old CSR value.
Fixes da50f5a693ff ("LoongArch: KVM: Implement handle csr exception")Signed-off-by Qiang Ma <[email protected]>--- arch/loongarch/kvm/exit.c | 1 - 1 file changed, 1 deletion(-)arch/loongarch/kvm/exit.c +0
-0
arch/loongarch/kvm/exit.c static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, · line 108
Quoted 7 lines
arch/loongarch/kvm/exit.c +0
-1@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, old =kvm_read_sw_gcsr (csr, csrid); val = (old & ~csr_mask) | (val & csr_mask);kvm_write_sw_gcsr (csr, csrid, val); - old = old & csr_mask; }else pr_warn_once ("Unsupported csrxchg 0x%x with pc %lx\n" , csrid, vcpu->arch.pc);
Applied, thanks.
Huacai
Quoted 32 lines · Qiang Ma
The LoongArch CSRXCHG instruction returns the full old CSR value in rd after applying the masked update.
kvm_emu_xchg_csr()currently masks the saved value before returning it to the guest, so rd receives only the bits selected by the write mask.That breaks the architectural behavior and makes a zero mask return 0 instead of the previous CSR value. Keep the masked CSR update, but return the unmodified old CSR value.
Fixes da50f5a693ff ("LoongArch: KVM: Implement handle csr exception")Signed-off-by Qiang Ma <[email protected]>--- arch/loongarch/kvm/exit.c | 1 - 1 file changed, 1 deletion(-)arch/loongarch/kvm/exit.c +0
-2@@ -103,7 +103,6 @@ static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, old =kvm_read_sw_gcsr (csr, csrid); val = (old & ~csr_mask) | (val & csr_mask);kvm_write_sw_gcsr (csr, csrid, val); - old = old & csr_mask; }else pr_warn_once ("Unsupported csrxchg 0x%x with pc %lx\n" , csrid, vcpu->arch.pc); --2.20.1