target/arm: cp15.dacr migration
I recently encountered a problem with cp15.dacr register.
It has _s and _ns versions. During the migration only dacr_ns is saved/loaded.
But both of the values are used in get_phys_addr_v5 and get_phys_addr_v6 functions. Therefore VM behavior becomes incorrect after loading the vmstate.
I found that kvm_to_cpreg_id is responsible for disabling dacr_s migration, because it always selects ns variant.
I used the following changes to solve the problem, but I'm not sure that these changes do not break anything in KVM.
Can anyone give me an advice about that?
target/arm/cpu.h +0 -5
@@ -2510,11 +2510,6 @@ static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)if ((kvmid &CP_REG_SIZE_MASK ) ==CP_REG_SIZE_U64 ) { cpregid |= (1 <<15 ); } - -/* KVM is always non-secure so add the NS flag on AArch32 register -* entries. -*/ - cpregid |=1 <<CP_REG_NS_SHIFT ; }return cpregid; }
--
Pavel Dovgalyuk
Quoted 7 lines · Pavel Dovgalyuk
I recently encountered a problem with cp15.dacr register.
It has _s and_nsversions. During the migration onlydacr_nsis saved/loaded.
But both of the values are used inget_phys_addr_v5andget_phys_addr_v6functions. Therefore VM behavior becomes incorrect after loading the vmstate.
Yes, we don't correctly save and restore the Secure banked registers. This is a long standing bug (eg it is the cause of https://gitlab.com/qemu-project/qemu/-/issues/467).
Almost nobody notices this, because almost nobody both runs Secure-world AArch32 code and also tries migration or save/restore.
Quoted 19 lines
I found that
kvm_to_cpreg_idis responsible for disablingdacr_smigration, because it always selects ns variant.target/arm/cpu.h +0
-5@@ -2510,11 +2510,6 @@ static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)if ((kvmid &CP_REG_SIZE_MASK ) ==CP_REG_SIZE_U64 ) { cpregid |= (1 <<15 ); } - -/* KVM is always non-secure so add the NS flag on AArch32 register -* entries. -*/ - cpregid |=1 <<CP_REG_NS_SHIFT ; }return cpregid;
Quoted 1 line
}
This change is wrong, or at least incomplete -- as the comment notes, a guest running under KVM is always NonSecure, so when KVM says "this is DACR" (or whatever) it always means "this is the NS banked DACR". (Though now AArch32 KVM support has been dropped we have some flexibility to not necessarily use KVM register ID values that exactly match what the kernel uses, if we need to do that.)
Also, kvm_to_cpreg_id() and cpreg_to_kvm_id() are supposed to be inverses of each other -- at the moment they are not, hence this bug, but I think your change has probably resulted in both the S and the NS banked versions of each register being treated as the S banked version, which will have a different set of problems.
There is also the question of migration compatibility to consider in any change in this area.
thanks
-- PMM
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Pavel Dovgalyuk
I recently encountered a problem with cp15.dacr register.
It has _s and_nsversions. During the migration onlydacr_nsis saved/loaded.
But both of the values are used inget_phys_addr_v5andget_phys_addr_v6functions. Therefore VM behavior becomes incorrect after loading the vmstate.
Quoted 5 lines
Yes, we don't correctly save and restore the Secure banked registers. This is a long standing bug (eg it is the cause of https://gitlab.com/qemu-project/qemu/-/issues/467).
Almost nobody notices this, because almost nobody both runs Secure-world AArch32 code and also tries migration or save/restore.
We actually did it for reverse debugging of custom firmware.
Quoted 19 lines
Quoted 19 lines
I found that
kvm_to_cpreg_idis responsible for disablingdacr_smigration, because it always selects ns variant.target/arm/cpu.h +0
-5@@ -2510,11 +2510,6 @@ static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)if ((kvmid &CP_REG_SIZE_MASK ) ==CP_REG_SIZE_U64 ) { cpregid |= (1 <<15 ); } - -/* KVM is always non-secure so add the NS flag on AArch32 register -* entries. -*/ - cpregid |=1 <<CP_REG_NS_SHIFT ; }return cpregid;
Quoted 8 lines
Quoted 1 line
}This change is wrong, or at least incomplete -- as the comment notes, a guest running under KVM is always NonSecure, so when KVM says "this is DACR" (or whatever) it always means "this is the NS banked DACR". (Though now AArch32 KVM support has been dropped we have some flexibility to not necessarily use KVM register ID values that exactly match what the kernel uses, if we need to do that.)
Unfortunately, I can't test anything with AArch32 KVM.
Quoted 5 lines
Also,
kvm_to_cpreg_id()andcpreg_to_kvm_id()are supposed to be inverses of each other -- at the moment they are not, hence this bug, but I think your change has probably resulted in both the S and the NS banked versions of each register being treated as the S banked version, which will have a different set of problems.
I checked the flags coming through write_cpustate_to_list. There were both dacr_s and dacr_ns flags. Therefore both values were saved.
Quoted 3 lines
There is also the question of migration compatibility to consider in any change in this area.
Pavel Dovgalyuk
Quoted 47 lines · Pavel Dovgalyuk
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Pavel Dovgalyuk
I recently encountered a problem with cp15.dacr register.
It has _s and_nsversions. During the migration onlydacr_nsis saved/loaded.
But both of the values are used inget_phys_addr_v5andget_phys_addr_v6functions. Therefore VM behavior becomes incorrect after loading the vmstate.Quoted 5 lines
Yes, we don't correctly save and restore the Secure banked registers. This is a long standing bug (eg it is the cause of https://gitlab.com/qemu-project/qemu/-/issues/467).
Almost nobody notices this, because almost nobody both runs Secure-world AArch32 code and also tries migration or save/restore.We actually did it for reverse debugging of custom firmware.
Quoted 19 lines
Quoted 19 lines
I found that
kvm_to_cpreg_idis responsible for disablingdacr_smigration, because it always selects ns variant.target/arm/cpu.h +0
-5@@ -2510,11 +2510,6 @@ static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)if ((kvmid &CP_REG_SIZE_MASK ) ==CP_REG_SIZE_U64 ) { cpregid |= (1 <<15 ); } - -/* KVM is always non-secure so add the NS flag on AArch32 register -* entries. -*/ - cpregid |=1 <<CP_REG_NS_SHIFT ; }return cpregid;Quoted 8 lines
Quoted 1 line
}This change is wrong, or at least incomplete -- as the comment notes, a guest running under KVM is always NonSecure, so when KVM says "this is DACR" (or whatever) it always means "this is the NS banked DACR". (Though now AArch32 KVM support has been dropped we have some flexibility to not necessarily use KVM register ID values that exactly match what the kernel uses, if we need to do that.)
Quoted 1 line
Unfortunately, I can't test anything with AArch32 KVM.
As I say, it doesn't exist any more, so you don't need to. In any case, this patch isn't sufficient on its own.
thanks
-- PMM