target-arm: fix broken sync of sysregs between QEMU and KVM 32
v3 cover target-arm: fix broken sync of sysregs between QEMU and KVM 32
This patchset fixes a regression in the synchronization of system registers between QEMU and KVM for 32-bit ARM hosts. The most obvious effect of the bug is that trying to access memory via the gdbstub doesn't work, because gdbstub thinks the MMU is off and doesn't get the virt-to-phys translation right. (Migration would not be broken.)
The underlying cause of this is that we are using the cpreg definition flag ARM_CP_NO_MIGRATE for two different purposes:
1) register is an alias on to state that's also visible via some other register, and that other register is the one responsible for migrating the state
2) register is not actually state at all (for instance the TLB or cache maintenance operation "registers") and it makes no sense to attempt to migrate it or otherwise access the raw state
This works fine for identifying which registers should be ignored when performing migration, but we also use the same functions for synchronizing system register state between QEMU and the kernel when using KVM. In this case we don't want to try to sync state into registers in category 2, but we do want to sync into registers in category 1, because the kernel might have picked a different one of the aliases as its choice for which one to expose for migration. (In particular, on 32 bit hosts the kernel will expose the state in the AArch32 version of the register, but TCG's convention is to mark the AArch64 version as the version to migrate, even if the CPU being emulated happens to be 32 bit, so almost all system registers will hit this issue now that we've added AArch64 system emulation.)
Fix this by splitting the NO_MIGRATE flag in two (ALIAS and NO_RAW) corresponding to the two different reasons we might not want to migrate a register. When setting up the TCG list of registers to migrate we honour both flags; when populating the list from KVM, only ignore registers which are NO_RAW.
Changes v1->v2:
* change raw_accessors_valid() to raw_accessors_invalid() and beef up its comment, following confusion during review of v1
Changes v2->v3:
* actually change behaviour of the raw_accessors_invalid() function rather than merely its name...
Peter Maydell (2):
target-arm: Split NO_MIGRATE into ALIAS and NO_RAW
target-arm: Add checks that cpreg raw accesses are handled
target-arm/cpu.h | 15 +++- target-arm/helper.c | 237 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 146 insertions(+), 106 deletions(-)
Signature · 1 line
1.9.1
PATCH v3 1/2 target-arm: Split NO_MIGRATE into ALIAS and NO_RAW
We currently mark ARM coprocessor/system register definitions with the flag ARM_CP_NO_MIGRATE for two different reasons:
1) register is an alias on to state that's also visible via some other register, and that other register is the one responsible for migrating the state
2) register is not actually state at all (for instance the TLB or cache maintenance operation "registers") and it makes no sense to attempt to migrate it or otherwise access the raw state
This works fine for identifying which registers should be ignored when performing migration, but we also use the same functions for synchronizing system register state between QEMU and the kernel when using KVM. In this case we don't want to try to sync state into registers in category 2, but we do want to sync into registers in category 1, because the kernel might have picked a different one of the aliases as its choice for which one to expose for migration. (In particular, on 32 bit hosts the kernel will expose the state in the AArch32 version of the register, but TCG's convention is to mark the AArch64 version as the version to migrate, even if the CPU being emulated happens to be 32 bit, so almost all system registers will hit this issue now that we've added AArch64 system emulation.)
Fix this by splitting the NO_MIGRATE flag in two (ALIAS and NO_RAW) corresponding to the two different reasons we might not want to migrate a register. When setting up the TCG list of registers to migrate we honour both flags; when populating the list from KVM, only ignore registers which are NO_RAW.
--- target-arm/cpu.h | 15 +++- target-arm/helper.c | 206 ++++++++++++++++++++++++++-------------------------- 2 files changed, 115 insertions(+), 106 deletions(-)
target-arm/cpu.h +11 -4
@@ -1112,8 +1112,14 @@ static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)* a register definition to override a previous definition for the * same (cp, is64, crn, crm, opc1, opc2) tuple: either the new or the * old must have the OVERRIDE bit set. -* NO_MIGRATE indicates that this register should be ignored for migration; -* (eg because any state is accessed via some other coprocessor register). +* ALIAS indicates that this register is an alias view of some underlying +* state which is also visible via another register, and that the other +* register is handling migration; registers marked ALIAS will not be migrated +* but may have their state set by syncing of register state from KVM. +* NO_RAW indicates that this register has no underlying state and does not +* support raw access for state saving/loading; it will not be used for either +* migration or KVM state synchronization. (Typically this is for "registers" +* which are actually used as instructions for cache maintenance and so on.) * IO indicates that this register does I/O and therefore its accesses * need to be surrounded by gen_io_start()/gen_io_end(). In particular, * registers which implement clocks or timers require this.
@@ -1123,8 +1129,9 @@ static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)#define ARM_CP_64BIT 4#define ARM_CP_SUPPRESS_TB_END 8#define ARM_CP_OVERRIDE 16 -#define ARM_CP_NO_MIGRATE 32 +#define ARM_CP_ALIAS 32#define ARM_CP_IO 64 +#define ARM_CP_NO_RAW 128#define ARM_CP_NOP (ARM_CP_SPECIAL | (1 << 8))#define ARM_CP_WFI (ARM_CP_SPECIAL | (2 << 8))#define ARM_CP_NZCV (ARM_CP_SPECIAL | (3 << 8))
@@ -1134,7 +1141,7 @@ static inline uint64_t cpreg_to_kvm_id(uint32_t cpregid)/* Used only as a terminator for ARMCPRegInfo lists */ #define ARM_CP_SENTINEL 0xffff/* Mask of only the flag bits in a type field */ -#define ARM_CP_FLAG_MASK 0x7f +#define ARM_CP_FLAG_MASK 0xff/* Valid values for ARMCPRegInfo state field, indicating which of * the AArch32 and AArch64 execution states this register is visible in.
target-arm/helper.c +104 -102
631 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v3 2/2 target-arm: Add checks that cpreg raw accesses are handled
In reply to Peter MaydellAdd assertion checking when cpreg structures are registered that they either forbid raw-access attempts or at least make an attempt at handling them. Also add an assert in the raw-accessor-of-last-resort, to avoid silently doing a read or write from offset zero, which is actually AArch32 CPU register r0.
--- target-arm/helper.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
target-arm/helper.c +31 -0
@@ -119,6 +119,7 @@ static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)static uint64_t raw_read (CPUARMState *env,const ARMCPRegInfo *ri) { +assert (ri->fieldoffset);if (cpreg_field_is_64bit (ri)) {return CPREG_FIELD64 (env, ri); }else {
@@ -129,6 +130,7 @@ static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)static void raw_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { +assert (ri->fieldoffset);if (cpreg_field_is_64bit (ri)) {CPREG_FIELD64 (env, ri) = value; }else {
@@ -174,6 +176,27 @@ static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, } } +static bool raw_accessors_invalid (const ARMCPRegInfo *ri) +{ +/* Return true if the regdef would cause an assertion if you called +* read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a +* program bug for it not to have the NO_RAW flag). +* NB that returning false here doesn't necessarily mean that calling +* read/write_raw_cp_reg() is safe, because we can't distinguish "has +* read/write access functions which are safe for raw use" from "has +* read/write access functions which have side effects but has forgotten +* to provide raw access functions". +* The tests here line up with the conditions in read/write_raw_cp_reg() +* and assertions in raw_read()/raw_write(). +*/ +if ((ri->type &ARM_CP_CONST ) || + ri->fieldoffset || + ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { +return false; + } +return true; +} +bool write_cpustate_to_list (ARMCPU *cpu) {/* Write the coprocessor state from cpu->env to the (index,value) list. */
@@ -3509,6 +3532,14 @@ static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, r2->type |=ARM_CP_ALIAS ; } +/* Check that raw accesses are either forbidden or handled. Note that +* we can't assert this earlier because the setup of fieldoffset for +* banked registers has to be done first. +*/ +if (!(r2->type &ARM_CP_NO_RAW )) { +assert (!raw_accessors_invalid (r2)); + } +/* Overriding of an existing definition must be explicitly * requested. */
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH v3 2/2] target-arm: Add checks that cpreg raw accesses are handled
Quoted 82 lines · Peter Maydell
Add assertion checking when cpreg structures are registered that they either forbid raw-access attempts or at least make an attempt at handling them. Also add an assert in the raw-accessor-of-last-resort, to avoid silently doing a read or write from offset zero, which is actually AArch32 CPU register r0.
Signed-off-by Peter Maydell <[email protected]>--- target-arm/helper.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)target-arm/helper.c +1
-0@@ -119,6 +119,7 @@ static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)static uint64_t raw_read (CPUARMState *env,const ARMCPRegInfo *ri) { +assert (ri->fieldoffset);if (cpreg_field_is_64bit (ri)) {return CPREG_FIELD64 (env, ri);} else {target-arm/helper.c +1
-0@@ -129,6 +130,7 @@ static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)static void raw_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { +assert (ri->fieldoffset);if (cpreg_field_is_64bit (ri)) {CPREG_FIELD64 (env, ri) = value;} else {target-arm/helper.c +9
-0@@ -174,6 +176,27 @@ static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, } } +static bool raw_accessors_invalid (const ARMCPRegInfo *ri) +{ +/* Return true if the regdef would cause an assertion if you called +* read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a +* program bug for it not to have the NO_RAW flag). +* NB that returning false here doesn't necessarily mean that calling +* read/write_raw_cp_reg() is safe, because we can't distinguish "has +* read/write access functions which are safe for raw use" from "has +* read/write access functions which have side effects but has forgotten
target-arm/helper.c +2
-0+* to provide raw access functions". +* The tests here line up with the conditions in read/write_raw_cp_reg()
target-arm/helper.c +5
-0+* and assertions in raw_read()/raw_write(). +*/ + if ((ri->type & ARM_CP_CONST) || + ri->fieldoffset || + ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn ||
ri->readfn))) {target-arm/helper.c +5
-0+return false; + } +return true; +} +bool write_cpustate_to_list (ARMCPU *cpu) {/* Write the coprocessor state from cpu->env to the (index,value) list. */
target-arm/helper.c +8
-0@@ -3509,6 +3532,14 @@ static void add_cpreg_to_hashtable(ARMCPU *cpu,const ARMCPRegInfo *r, r2->type |= ARM_CP_ALIAS; } +/* Check that raw accesses are either forbidden or handled. Note that +* we can't assert this earlier because the setup of fieldoffset for +* banked registers has to be done first. +*/ +if (!(r2->type &ARM_CP_NO_RAW )) { +assert (!raw_accessors_invalid (r2)); + } +/* Overriding of an existing definition must be explicitly * requested. */
Quoted 4 lines
--
1.9.1
Reviewed-by: Greg Bellows <[email protected]>