target-arm: cpregs list for migration, kvm reset
cover target-arm: cpregs list for migration, kvm reset
This patch series overhauls how we handle ARM coprocessor registers, so that we use a consistent approach for migration, reset and QEMU<->KVM synchronisation, driven by the kernel's list of supported registers.
The basic principle here is that we trust the kernel's list of what registers it knows about, and that QEMU doesn't have to have specific knowledge of a coprocessor register to support running and migrating a KVM session on a kernel that does support that register.
We maintain a list of cp registers, which is initialized either from the current cpreg hashtable (for TCG), or by querying the kernel (for KVM). For migration we simply send the lists of register indexes and values; migration fails if there's a register the destination kernel is unaware of, or if the value can't be set as required, but isn't gated on whether source or destination QEMU know about the register.
We also use the register list to properly reset the vcpu by simply feeding it back the initial set of register values; this fixes a bug where we weren't resetting everything we should have (though Linux guests don't care about most reset values).
Note that vm save/load with KVM requires that you run with -machine kernel_irqchip=off, because the kernel doesn't currently support save/load of either the VGIC or virtual timer state. It may also be necessary to nobble the device tree blob to remove the "armv7-timer" node so the guest doesn't try to use the vtimers. Migration between TCG and KVM is not supported at the moment (it would require us to add a lot of registers to TCG, which I may do at some point, but this is a bit of an obscure usecase IMHO).
Peter Maydell (7):
target-arm: Allow special cpregs to have flags set
target-arm: Add raw_readfn and raw_writefn to ARMCPRegInfo
target-arm: mark up cpregs for no-migrate or raw access
target-arm: Convert TCG to using (index,value) list for cp migration
target-arm: Initialize cpreg list from KVM when using KVM
target-arm: Reinitialize all KVM VCPU registers on reset
target-arm: Use tuple list to sync cp regs with KVM
target-arm/Makefile.objs | 1 + target-arm/cpu-qom.h | 24 ++++ target-arm/cpu.c | 2 + target-arm/cpu.h | 89 +++++++++++++- target-arm/helper.c | 281 +++++++++++++++++++++++++++++++++++++++----- target-arm/kvm.c | 292 +++++++++++++++++++++++++++++++++++----------- target-arm/kvm_arm.h | 33 ++++++ target-arm/machine.c | 134 +++++++++++++-------- 8 files changed, 705 insertions(+), 151 deletions(-)
Signature · 1 line
1.7.9.5
PATCH 1/7 target-arm: Allow special cpregs to have flags set
Relax the "is this a valid ARMCPRegInfo type value?" check to permit "special" cpregs to have flags other than ARM_CP_SPECIAL set. At the moment none of the other flags are relevant for special regs, but the migration related flag we're about to introduce can apply here too.
--- target-arm/cpu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target-arm/cpu.h +1 -1
@@ -456,7 +456,7 @@ static inline bool cptype_valid(int cptype) {return ((cptype & ~ARM_CP_FLAG_MASK ) ==0 ) || ((cptype &ARM_CP_SPECIAL ) && - (cptype <=ARM_LAST_SPECIAL )); + ((cptype & ~ARM_CP_FLAG_MASK ) <=ARM_LAST_SPECIAL )); }/* Access rights:
Signature · 1 line
1.7.9.5
PATCH 2/7 target-arm: Add raw_readfn and raw_writefn to ARMCPRegInfo
In reply to Peter MaydellFor reading and writing register values from the kernel for KVM, we need to provide accessor functions which are guaranteed to succeed and don't impose access checks, mask out unwritable bits, etc. Define new fields raw_readfn and raw_writefn for this purpose; these only need to be provided if there is a readfn or writefn already and it is not suitable.
--- target-arm/cpu.h | 18 +++++++++++++++++- target-arm/helper.c | 13 +++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-)
target-arm/cpu.h +17 -1
@@ -434,19 +434,22 @@ void armv7m_nvic_complete_irq(void *opaque, int irq);* 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). */ #define ARM_CP_SPECIAL 1#define ARM_CP_CONST 2#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_NOP (ARM_CP_SPECIAL | (1 << 8))#define ARM_CP_WFI (ARM_CP_SPECIAL | (2 << 8))#define ARM_LAST_SPECIAL ARM_CP_WFI/* 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 0x1f +#define ARM_CP_FLAG_MASK 0x3f/* Return true if cptype is a valid type field. This is used to try to * catch errors where the sentinel has been accidentally left off the end
@@ -562,6 +565,19 @@ struct ARMCPRegInfo {* by fieldoffset. */ CPWriteFn *writefn; +/* Function for doing a "raw" read; used when we need to copy +* coprocessor state to the kernel for KVM or out for +* migration. This only needs to be provided if there is also a +* readfn and it makes an access permission check. +*/ +CPReadFn *raw_readfn; +/* Function for doing a "raw" write; used when we need to copy KVM +* kernel coprocessor state into userspace, or for inbound +* migration. This only needs to be provided if there is also a +* writefn and it makes an access permission check or masks out +* "unwritable" bits or has write-one-to-clear or similar behaviour. +*/ +CPWriteFn *raw_writefn;/* Function for resetting the register. If NULL, then reset will be done * by writing resetvalue to the field specified in fieldoffset. If * fieldoffset is 0 then no reset will be done.
target-arm/helper.c +13 -0
@@ -1392,6 +1392,19 @@ void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, r2->crm = crm; r2->opc1 = opc1; r2->opc2 = opc2; +/* By convention, for wildcarded registers only the first +* entry is used for migration; the others are marked as +* NO_MIGRATE so we don't try to transfer the register +* multiple times. Special registers (ie NOP/WFI) are +* never migratable. +*/ +if ((r->type &ARM_CP_SPECIAL ) || + ((r->crm ==CP_ANY ) && crm !=0 ) || + ((r->opc1 ==CP_ANY ) && opc1 !=0 ) || + ((r->opc2 ==CP_ANY ) && opc2 !=0 )) { + r2->type |=ARM_CP_NO_MIGRATE ; + } +/* Overriding of an existing definition must be explicitly * requested. */
Signature · 1 line
1.7.9.5
PATCH 3/7 target-arm: mark up cpregs for no-migrate or raw access
In reply to Peter MaydellMark up coprocessor register definitions to add raw access functions or mark the register as non-migratable where necessary.
--- target-arm/helper.c | 94 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 31 deletions(-)
target-arm/helper.c +63 -31
@@ -155,13 +155,17 @@ static const ARMCPRegInfo cp_reginfo[] = {* the unified TLB ops but also the dside/iside/inner-shareable variants. */ { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, - .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, }, + .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, + .type = ARM_CP_NO_MIGRATE }, { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, - .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, }, + .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, + .type = ARM_CP_NO_MIGRATE }, { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, - .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, }, + .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, + .type = ARM_CP_NO_MIGRATE }, { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, - .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, }, + .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, + .type = ARM_CP_NO_MIGRATE }, /* Cache maintenance ops; some of this space may be overridden later. */ { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
@@ -196,7 +200,8 @@ static const ARMCPRegInfo not_v7_cp_reginfo[] = { .resetvalue = 0 },/* v6 doesn't have the cache ID registers but Linux reads them anyway */ { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, - .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, + .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, + .resetvalue = 0 }, REGINFO_SENTINEL };
@@ -235,6 +240,21 @@ static const ARMCPRegInfo v6_cp_reginfo[] = { REGINFO_SENTINEL }; + +static int raw_read (CPUARMState *env,const ARMCPRegInfo *ri, +uint64_t *value) +{ + *value =CPREG_FIELD32 (env, ri); +return 0 ; +} + +static int raw_write (CPUARMState *env,const ARMCPRegInfo *ri, +uint64_t value) +{ +CPREG_FIELD32 (env, ri) = value; +return 0 ; +} +static int pmreg_read (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t *value) {
@@ -366,13 +386,16 @@ static const ARMCPRegInfo v7_cp_reginfo[] = { { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, .access = PL0_RW, .resetvalue = 0, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), - .readfn = pmreg_read, .writefn = pmcntenset_write }, + .readfn = pmreg_read, .writefn = pmcntenset_write, + .raw_readfn = raw_read, .raw_writefn = raw_write }, { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), - .readfn = pmreg_read, .writefn = pmcntenclr_write }, + .readfn = pmreg_read, .writefn = pmcntenclr_write, + .type = ARM_CP_NO_MIGRATE }, { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), - .readfn = pmreg_read, .writefn = pmovsr_write }, + .readfn = pmreg_read, .writefn = pmovsr_write, + .raw_readfn = raw_read, .raw_writefn = raw_write },/* Unimplemented so WI. Strictly speaking write accesses in PL0 should * respect PMUSERENR. */
@@ -389,7 +412,8 @@ static const ARMCPRegInfo v7_cp_reginfo[] = { { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper), - .readfn = pmreg_read, .writefn = pmxevtyper_write }, + .readfn = pmreg_read, .writefn = pmxevtyper_write, + .raw_readfn = raw_read, .raw_writefn = raw_write }, /* Unimplemented, RAZ/WI. XXX PMUSERENR */ { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
@@ -397,22 +421,21 @@ static const ARMCPRegInfo v7_cp_reginfo[] = { .access = PL0_R | PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), .resetvalue = 0, - .writefn = pmuserenr_write }, + .writefn = pmuserenr_write, .raw_writefn = raw_write }, { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), .resetvalue = 0, - .writefn = pmintenset_write }, + .writefn = pmintenset_write, .raw_writefn = raw_write }, { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, - .access = PL1_RW, + .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), - .resetvalue = 0, - .writefn = pmintenclr_write }, + .resetvalue = 0, .writefn = pmintenclr_write, }, { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr), .resetvalue = 0, }, { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, - .access = PL1_R, .readfn = ccsidr_read }, + .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE }, { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel), .writefn = csselr_write, .resetvalue = 0 },
@@ -461,7 +484,7 @@ static const ARMCPRegInfo t2ee_cp_reginfo[] = { .writefn = teecr_write }, { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), - .resetvalue = 0, + .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write, .readfn = teehbr_read, .writefn = teehbr_write }, REGINFO_SENTINEL };
@@ -486,7 +509,8 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {/* Dummy implementation: RAZ/WI the whole crn=14 space */ { .name = "GENERIC_TIMER", .cp = 15, .crn = 14, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, - .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, + .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, + .resetvalue = 0 }, REGINFO_SENTINEL };
@@ -579,7 +603,7 @@ static const ARMCPRegInfo vapa_cp_reginfo[] = { .writefn = par_write },#ifndef CONFIG_USER_ONLY { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, - .access = PL1_W, .writefn = ats_write }, + .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE }, #endif REGINFO_SENTINEL };
@@ -664,11 +688,11 @@ static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,static const ARMCPRegInfo pmsav5_cp_reginfo[] = { { .name ="DATA_AP" , .cp =15 , .crn =5 , .crm =0 , .opc1 =0 , .opc2 =0 , - .access =PL1_RW , + .access =PL1_RW , .type =ARM_CP_NO_MIGRATE , .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, { .name ="INSN_AP" , .cp =15 , .crn =5 , .crm =0 , .opc1 =0 , .opc2 =1 , - .access =PL1_RW , + .access =PL1_RW , .type =ARM_CP_NO_MIGRATE , .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, { .name ="DATA_EXT_AP" , .cp =15 , .crn =5 , .crm =0 , .opc1 =0 , .opc2 =2 ,
@@ -801,6 +825,7 @@ static const ARMCPRegInfo omap_cp_reginfo[] = { .writefn = omap_threadid_write }, { .name = "TI925T_STATUS", .cp = 15, .crn = 15, .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, + .type = ARM_CP_NO_MIGRATE, .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },/* TODO: Peripheral port remap register: * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
@@ -808,7 +833,8 @@ static const ARMCPRegInfo omap_cp_reginfo[] = {* when MMU is off. */ { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, - .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, .type = ARM_CP_OVERRIDE, + .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, + .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE, .writefn = omap_cachemaint_write }, { .name = "C9", .cp = 15, .crn = 9, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
@@ -848,21 +874,24 @@ static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {*/ { .name = "C15_IMPDEF", .cp = 15, .crn = 15, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, - .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, + .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, + .resetvalue = 0 }, REGINFO_SENTINEL };static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {/* Cache status: RAZ because we have no cache so it's always clean */ { .name ="CDSR" , .cp =15 , .crn =7 , .crm =10 , .opc1 =0 , .opc2 =6 , - .access =PL1_R , .type =ARM_CP_CONST , .resetvalue =0 }, + .access =PL1_R , .type =ARM_CP_CONST |ARM_CP_NO_MIGRATE , + .resetvalue =0 },REGINFO_SENTINEL };static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {/* We never have a a block transfer operation in progress */ { .name ="BXSR" , .cp =15 , .crn =7 , .crm =12 , .opc1 =0 , .opc2 =4 , - .access =PL0_R , .type =ARM_CP_CONST , .resetvalue =0 }, + .access =PL0_R , .type =ARM_CP_CONST |ARM_CP_NO_MIGRATE , + .resetvalue =0 },/* The cache ops themselves: these all NOP for QEMU */ { .name ="IICR" , .cp =15 , .crm =5 , .opc1 =0 , .access =PL1_W , .type =ARM_CP_NOP |ARM_CP_64BIT },
@@ -884,9 +913,11 @@ static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {* to indicate that there are no dirty cache lines. */ { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, - .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) }, + .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, + .resetvalue = (1 << 30) }, { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, - .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) }, + .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, + .resetvalue = (1 << 30) }, REGINFO_SENTINEL };
@@ -894,8 +925,8 @@ static const ARMCPRegInfo strongarm_cp_reginfo[] = {/* Ignore ReadBuffer accesses */ { .name = "C9_READBUFFER", .cp = 15, .crn = 9, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, - .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE, - .resetvalue = 0 }, + .access = PL1_RW, .resetvalue = 0, + .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE }, REGINFO_SENTINEL };
@@ -921,7 +952,7 @@ static int mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri,static const ARMCPRegInfo mpidr_cp_reginfo[] = { { .name ="MPIDR" , .cp =15 , .crn =0 , .crm =0 , .opc1 =0 , .opc2 =5 , - .access =PL1_R , .readfn = mpidr_read }, + .access =PL1_R , .readfn = mpidr_read, .type =ARM_CP_NO_MIGRATE },REGINFO_SENTINEL };
@@ -1104,7 +1135,8 @@ void register_cp_regs_for_features(ARMCPU *cpu) .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), - .readfn = pmreg_read, .writefn = pmcr_write + .readfn = pmreg_read, .writefn = pmcr_write, + .raw_readfn = raw_read, .raw_writefn = raw_write, };ARMCPRegInfo clidr = { .name ="CLIDR" , .cp =15 , .crn =0 , .crm =0 , .opc1 =1 , .opc2 =1 ,
@@ -1176,7 +1208,7 @@ void register_cp_regs_for_features(ARMCPU *cpu) { .name = "MIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, .access = PL1_R, .resetvalue = cpu->midr, - .writefn = arm_cp_write_ignore, + .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid) }, { .name = "CTR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH 3/7] target-arm: mark up cpregs for no-migrate or raw access
Quoted 13 lines · Peter Maydell
Mark up coprocessor register definitions to add raw access functions or mark the register as non-migratable where necessary.
Signed-off-by Peter Maydell <[email protected]>--- target-arm/helper.c | 94 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 31 deletions(-)target-arm/helper.c +0
-0
target-arm/helper.c static const ARMCPRegInfo cp_reginfo[] = { · line 159
Quoted 6 lines
target-arm/helper.c +2
-1@@ -155,13 +155,17 @@ static const ARMCPRegInfo cp_reginfo[] = {* the unified TLB ops but also the dside/iside/inner-shareable variants. */ { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, - .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, }, + .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, + .type = ARM_CP_NO_MIGRATE },
[...]
What happens with registers which don't have the raw_write function set (even though the write function imposes some access checks or has side effects) and also is not marked as ARM_CP_NO_MIGRATE, CONTEXTIDR seems to be such an example. ?
-Christoffer
Re: [Qemu-devel] [PATCH 3/7] target-arm: mark up cpregs for no-migrate or raw access
Quoted 3 lines · Christoffer Dall
What happens with registers which don't have the
raw_writefunction set (even though the write function imposes some access checks or has side effects) and also is not marked asARM_CP_NO_MIGRATE,
In the general case what happens is that we probably don't sync (or migrate, for TCG) the register properly, because we'll use the standard write function and get whatever it does. (Note that mistakes in annotation don't affect KVM migration because we always trust the kernel's register list and values and work with them directly; we don't indirect through the TCG CPUState structures to migrate the data.)
The alternative would seem to be to require a raw_read/write function to be explicitly specified if there's a read/write function (even if it's specified to be the same thing), but that seemed to me like it would add a lot of boilerplate for most register descriptions. Do you think it would be better anyway, or do you have a better idea?
Quoted 1 line
CONTEXTIDR seems to be such an example. ?
In this specific case I decided it was safe to let the non-raw write function do a tlb_flush(). Looking again that is kinda expensive though, so we should probably mark these registers up with raw_write functions.
thanks
-- PMM
Re: [Qemu-devel] [PATCH 3/7] target-arm: mark up cpregs for no-migrate or raw access
Quoted 13 lines · Peter Maydell
Quoted 3 lines · Christoffer Dall
What happens with registers which don't have the
raw_writefunction set (even though the write function imposes some access checks or has side effects) and also is not marked asARM_CP_NO_MIGRATE,In the general case what happens is that we probably don't sync (or migrate, for TCG) the register properly, because we'll use the standard write function and get whatever it does. (Note that mistakes in annotation don't affect KVM migration because we always trust the kernel's register list and values and work with them directly; we don't indirect through the TCG CPUState structures to migrate the data.)
Quoted 6 lines
The alternative would seem to be to require a raw_read/write function to be explicitly specified if there's a read/write function (even if it's specified to be the same thing), but that seemed to me like it would add a lot of boilerplate for most register descriptions. Do you think it would be better anyway, or do you have a better idea?
Depends on how many places you add the raw functions. I was thinking about whether the absence of such a function could substitute the need for the NO_MIGRATE flag, but, eh, there's probably other uses for having that flag so it's not really preferred.
You probably did the best thing.
Quoted 8 lines
Quoted 2 lines
CONTEXTIDR seems to be such an example. ?
In this specific case I decided it was safe to let the non-raw write function do a
tlb_flush(). Looking again that is kinda expensive though, so we should probably mark these registers up withraw_writefunctions.
Migration is sort of an expensive operation, so not sure if it's worth it.
I am mostly worries about the case where we would miss raw read/write functions and that could be hard to track down in the case where migration fails, but I don't really have great suggestions on how to ensure this.
-Christoffer
Re: [Qemu-devel] [PATCH 3/7] target-arm: mark up cpregs for no-migrate or raw access
Quoted 4 lines · Christoffer Dall
Quoted 3 lines · Peter Maydell
Quoted 1 line · Christoffer Dall
CONTEXTIDR seems to be such an example. ?
Quoted 8 lines
Quoted 5 lines
In this specific case I decided it was safe to let the non-raw write function do a
tlb_flush(). Looking again that is kinda expensive though, so we should probably mark these registers up withraw_writefunctions.Migration is sort of an expensive operation, so not sure if it's worth it.
It'll also happen every time we drop out to a gdb attached to the VM. But yes, this definitely isn't on any fast paths.
Quoted 4 lines
I am mostly worries about the case where we would miss raw read/write functions and that could be hard to track down in the case where migration fails, but I don't really have great suggestions on how to ensure this.
That's the one argument for insisting on a raw function -- it does mean that when looking at code and patches you can see it's an explicit decision on how to handle the situation.
thanks
-- PMM
PATCH 4/7 target-arm: Convert TCG to using (index, value) list for cp migration
In reply to Peter MaydellConvert the TCG ARM target to using an (index,value) list for migrating coprocessors. The primary benefit of the (index,value) list is for passing state between KVM and QEMU, but it works for TCG-to-TCG migration as well and is a useful self-contained first step.
--- target-arm/cpu-qom.h | 20 ++++++ target-arm/cpu.c | 2 + target-arm/cpu.h | 69 ++++++++++++++++++++ target-arm/helper.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++ target-arm/kvm.c | 9 +++ target-arm/machine.c | 114 +++++++++++++++++++-------------- 6 files changed, 341 insertions(+), 47 deletions(-)
target-arm/cpu-qom.h +20 -0
@@ -62,6 +62,25 @@ typedef struct ARMCPU {/* Coprocessor information */ GHashTable *cp_regs; +/* For marshalling (mostly coprocessor) register state between the +* kernel and QEMU (for KVM) and between two QEMUs (for migration), +* we use these arrays. +*/ +/* List of register indexes managed via these arrays; (full KVM style +* 64 bit indexes, not CPRegInfo 32 bit indexes) +*/ +uint64_t *cpreg_indexes; +/* Values of the registers (cpreg_indexes[i]'s value is cpreg_values[i]) */ +uint64_t *cpreg_values; +/* Length of the indexes, values arrays */ +int32_t cpreg_array_len; +/* These are used only for migration: incoming data arrives in +* these fields and is sanity checked in post_load before copying +* to the working data structures above. +*/ +uint64_t *cpreg_vmstate_indexes; +uint64_t *cpreg_vmstate_values; +int32_t cpreg_vmstate_array_len;/* The instance init functions for implementation-specific subclasses * set these fields to specify the implementation-dependent values of
@@ -116,6 +135,7 @@ extern const struct VMStateDescription vmstate_arm_cpu;#endif void register_cp_regs_for_features (ARMCPU *cpu); +void init_cpreg_list (ARMCPU *cpu);void arm_cpu_do_interrupt (CPUState *cpu);void arm_v7m_cpu_do_interrupt (CPUState *cpu);
target-arm/cpu.c +2 -0
@@ -204,6 +204,8 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)register_cp_regs_for_features (cpu);arm_cpu_register_gdb_regs_for_features (cpu); +init_cpreg_list (cpu); +cpu_reset (CPU (cpu));qemu_init_vcpu (env);
target-arm/cpu.h +69 -0
81 lines not on this page · raw message
target-arm/helper.c +174 -0
180 lines not on this page · raw message
target-arm/kvm.c +9 -0
@@ -23,6 +23,15 @@#include "cpu.h" #include "hw/arm/arm.h" +/* Check that cpu.h's idea of coprocessor fields matches KVM's */ +#if (CP_REG_SIZE_SHIFT !=KVM_REG_SIZE_SHIFT ) || \ + (CP_REG_SIZE_MASK !=KVM_REG_SIZE_MASK ) || \ + (CP_REG_SIZE_U32 !=KVM_REG_SIZE_U32 ) || \ + (CP_REG_SIZE_U64 !=KVM_REG_SIZE_U64 ) || \ + (CP_REG_ARM !=KVM_REG_ARM ) +#error mismatch between cpu.h and KVM header definitions +#endif +const KVMCapabilityInfo kvm_arch_required_capabilities[] = {KVM_CAP_LAST_INFO };
target-arm/machine.c +67 -47
128 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH 4/7] target-arm: Convert TCG to using (index, value) list for cp migration
Quoted 140 lines · Peter Maydell
Convert the TCG ARM target to using an (index,value) list for migrating coprocessors. The primary benefit of the (index,value) list is for passing state between KVM and QEMU, but it works for TCG-to-TCG migration as well and is a useful self-contained first step.
Signed-off-by Peter Maydell <[email protected]>--- target-arm/cpu-qom.h | 20 ++++++ target-arm/cpu.c | 2 + target-arm/cpu.h | 69 ++++++++++++++++++++ target-arm/helper.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++ target-arm/kvm.c | 9 +++ target-arm/machine.c | 114 +++++++++++++++++++-------------- 6 files changed, 341 insertions(+), 47 deletions(-)target-arm/cpu-qom.h +20
-0@@ -62,6 +62,25 @@ typedef struct ARMCPU {/* Coprocessor information */ GHashTable *cp_regs; +/* For marshalling (mostly coprocessor) register state between the +* kernel and QEMU (for KVM) and between two QEMUs (for migration), +* we use these arrays. +*/ +/* List of register indexes managed via these arrays; (full KVM style +* 64 bit indexes, not CPRegInfo 32 bit indexes) +*/ +uint64_t *cpreg_indexes; +/* Values of the registers (cpreg_indexes[i]'s value is cpreg_values[i]) */ +uint64_t *cpreg_values; +/* Length of the indexes, values arrays */ +int32_t cpreg_array_len; +/* These are used only for migration: incoming data arrives in +* these fields and is sanity checked in post_load before copying +* to the working data structures above. +*/ +uint64_t *cpreg_vmstate_indexes; +uint64_t *cpreg_vmstate_values; +int32_t cpreg_vmstate_array_len;/* The instance init functions for implementation-specific subclasses * set these fields to specify the implementation-dependent values of @@ -116,6 +135,7 @@ extern const struct VMStateDescription vmstate_arm_cpu;#endif void register_cp_regs_for_features (ARMCPU *cpu); +void init_cpreg_list (ARMCPU *cpu);void arm_cpu_do_interrupt (CPUState *cpu);void arm_v7m_cpu_do_interrupt (CPUState *cpu);target-arm/cpu.c +2
-0@@ -204,6 +204,8 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)register_cp_regs_for_features (cpu);arm_cpu_register_gdb_regs_for_features (cpu); +init_cpreg_list (cpu); +cpu_reset (CPU (cpu));qemu_init_vcpu (env);target-arm/cpu.h +59
-0@@ -424,6 +424,43 @@ void armv7m_nvic_complete_irq(void *opaque, int irq); (((cp) <<16 ) | ((is64) <<15 ) | ((crn) <<11 ) | \ ((crm) <<7 ) | ((opc1) <<3 ) | (opc2)) +/* Note that these must line up with the KVM/ARM register +* ID field definitions (kvm.c will check this, but we +* can't just use the KVM defines here as the kvm headers +* are unavailable to non-KVM-specific files) + */ +#define CP_REG_SIZE_SHIFT 52 +#define CP_REG_SIZE_MASK 0x00f0000000000000ULL +#define CP_REG_SIZE_U32 0x0020000000000000ULL +#define CP_REG_SIZE_U64 0x0030000000000000ULL +#define CP_REG_ARM 0x4000000000000000ULL + +/* Convert a full 64 bit KVM register ID to the truncated 32 bit +* version used as a key for the coprocessor register hashtable +*/ +static inline uint32_t kvm_to_cpreg_id (uint64_t kvmid) +{ +uint32_t cpregid = kvmid; +if ((kvmid &CP_REG_SIZE_MASK ) ==CP_REG_SIZE_U64 ) { + cpregid |= (1 <<15 ); + } +return cpregid; +} + +/* Convert a truncated 32 bit hashtable key into the full +* 64 bit KVM register ID. +*/ +static inline uint64_t cpreg_to_kvm_id (uint32_t cpregid) +{ +uint64_t kvmid = cpregid & ~(1 <<15 ); +if (cpregid & (1 <<15 )) { + kvmid |=CP_REG_SIZE_U64 |CP_REG_ARM ; + }else { + kvmid |=CP_REG_SIZE_U32 |CP_REG_ARM ; + } +return kvmid; +} +/* ARMCPRegInfo type field bits. If the SPECIAL bit is set this is a * special-behaviour cp reg and bits [15..8] indicate what behaviour * it has. Otherwise it is a simple cp reg, where CONST indicates that @@ -621,6 +658,38 @@ static inline bool cp_access_ok(CPUARMState *env,return (ri->access >> ((arm_current_pl (env) *2 ) + isread)) &1 ; } +/** +* write_list_to_cpustate +* @cpu: ARMCPU +* +* For each register listed in the ARMCPU cpreg_indexes list, write +* its value from the cpreg_values list into the ARMCPUState structure. +* This updates TCG's working data structures from KVM data or +* from incoming migration state. +* +* Returns: true if all register values were updated correctly, +* false if some register was unknown or could not be written. +* Note that we do not stop early on failure -- we will attempt +* writing all registers in the list. +*/ +bool write_list_to_cpustate (ARMCPU *cpu); + +/** +* write_cpustate_to_list: +* @cpu: ARMCPU +* +* For each register listed in the ARMCPU cpreg_indexes list, write +* its value from the ARMCPUState structure into the cpreg_values list.
target-arm/cpu.h static inline bool cp_access_ok(CPUARMState *env, · line 692
Quoted 10 lines
target-arm/cpu.h +10
-0+* This is used to copy info from TCG's working data structures into +* KVM or for outbound migration. +* +* Returns: true if all register values were read correctly, +* false if some register was unknown or could not be read. +* Note that we do not stop early on failure -- we will attempt +* reading all registers in the list. +*/ +bool write_cpustate_to_list (ARMCPU *cpu); +
the naming is not super clear when you just see the caller in path 7, perhaps something like:
write_cpregs_to_cpustateread_cpregs_from_cpustate
or maybe snapshot_cpregs_to_list, hmmm, maybe I suck, dunno.
Quoted 349 lines
target-arm/cpu.h +0
-0/* Does the core conform to the the "MicroController" profile. e.g. Cortex-M3. Note the M in older cores (eg. ARM7TDMI) stands for Multiply. These are conventional cores (ie. Application or Realtime profile). */ target-arm/helper.c +174
-0@@ -64,6 +64,180 @@ static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)return 0 ; } +static bool read_raw_cp_reg (CPUARMState *env,const ARMCPRegInfo *ri, +uint64_t *v) +{ +/* Raw read of a coprocessor register (as needed for migration, etc) +* return true on success, false if the read is impossible for some reason. +*/ +if (ri->type &ARM_CP_CONST ) { + *v = ri->resetvalue; + }else if (ri->raw_readfn) { +return (ri->raw_readfn (env, ri, v) ==0 ); + }else if (ri->readfn) { +return (ri->readfn (env, ri, v) ==0 ); + }else { +if (ri->type &ARM_CP_64BIT ) { + *v =CPREG_FIELD64 (env, ri); + }else { + *v =CPREG_FIELD32 (env, ri); + } + } +return true; +} + +static bool write_raw_cp_reg (CPUARMState *env,const ARMCPRegInfo *ri, +int64_t v) +{ +/* Raw write of a coprocessor register (as needed for migration, etc). +* Return true on success, false if the write is impossible for some reason. +* Note that constant registers are treated as write-ignored; the +* caller should check for success by whether a readback gives the +* value written. +*/ +if (ri->type &ARM_CP_CONST ) { +return true; + }else if (ri->raw_writefn) { +return (ri->raw_writefn (env, ri, v) ==0 ); + }else if (ri->writefn) { +return (ri->writefn (env, ri, v) ==0 ); + }else { +if (ri->type &ARM_CP_64BIT ) { +CPREG_FIELD64 (env, ri) = v; + }else { +CPREG_FIELD32 (env, ri) = v; + } + } +return true; +} + +bool write_cpustate_to_list (ARMCPU *cpu) +{ +/* Write the coprocessor state from cpu->env to the (index,value) list. */ +int i; +bool ok = true; + +for (i =0 ; i < cpu->cpreg_array_len; i++) { +uint32_t regidx =kvm_to_cpreg_id (cpu->cpreg_indexes[i]); +const ARMCPRegInfo *ri; +uint64_t v; + ri =get_arm_cp_reginfo (cpu, regidx); +if (!ri) { + ok = false; +continue ; + } +if (ri->type &ARM_CP_NO_MIGRATE ) { +continue ; + } +if (!read_raw_cp_reg (&cpu->env, ri, &v)) { + ok = false; +continue ; + } + cpu->cpreg_values[i] = v; + } +return ok; +} + +bool write_list_to_cpustate (ARMCPU *cpu) +{ +int i; +bool ok = true; + +for (i =0 ; i < cpu->cpreg_array_len; i++) { +uint32_t regidx =kvm_to_cpreg_id (cpu->cpreg_indexes[i]); +uint64_t v = cpu->cpreg_values[i]; +uint64_t readback; +const ARMCPRegInfo *ri; + + ri =get_arm_cp_reginfo (cpu, regidx); +if (!ri) { + ok = false; +continue ; + } +if (ri->type &ARM_CP_NO_MIGRATE ) { +continue ; + } +/* Write value and confirm it reads back as written +* (to catch read-only registers and partially read-only +* registers where the incoming migration value doesn't match) +*/ +if (!write_raw_cp_reg (&cpu->env, ri, v) || + !read_raw_cp_reg (&cpu->env, ri, &readback) || + readback != v) { + ok = false; + } + } +return ok; +} + +static void add_cpreg_to_list (gpointer key,gpointer opaque) +{ +ARMCPU *cpu = opaque; +uint64_t regidx; +const ARMCPRegInfo *ri; + + regidx = *(uint32_t *)key; + ri =get_arm_cp_reginfo (cpu, regidx); + +if (!(ri->type &ARM_CP_NO_MIGRATE )) { + cpu->cpreg_indexes[cpu->cpreg_array_len] =cpreg_to_kvm_id (regidx); +/* The value array need not be initialized at this point */ + cpu->cpreg_array_len++; + } +} + +static void count_cpreg (gpointer key,gpointer opaque) +{ +ARMCPU *cpu = opaque; +uint64_t regidx; +const ARMCPRegInfo *ri; + + regidx = *(uint32_t *)key; + ri =get_arm_cp_reginfo (cpu, regidx); + +if (!(ri->type &ARM_CP_NO_MIGRATE )) { + cpu->cpreg_array_len++; + } +} + +static gint cpreg_key_compare (gconstpointer a,gconstpointer b) +{ +uint32_t aidx = *(uint32_t *)a; +uint32_t bidx = *(uint32_t *)b; + +return aidx - bidx; +} + +void init_cpreg_list (ARMCPU *cpu) +{ +/* Initialise the cpreg_tuples[] array based on the cp_regs hash. +* Note that we require cpreg_tuples[] to be sorted by key ID. +*/ +GList *keys; +int arraylen; + + keys =g_hash_table_get_keys (cpu->cp_regs); + keys =g_list_sort (keys, cpreg_key_compare); + + cpu->cpreg_array_len =0 ; + +g_list_foreach (keys, count_cpreg, cpu); + + arraylen = cpu->cpreg_array_len; + cpu->cpreg_indexes =g_new (uint64_t, arraylen); + cpu->cpreg_values =g_new (uint64_t, arraylen); + cpu->cpreg_vmstate_indexes =g_new (uint64_t, arraylen); + cpu->cpreg_vmstate_values =g_new (uint64_t, arraylen); + cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; + cpu->cpreg_array_len =0 ; + +g_list_foreach (keys, add_cpreg_to_list, cpu); + +assert (cpu->cpreg_array_len == arraylen); + +g_list_free (keys); +} +static int dacr_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { env->cp15.c3 = value;target-arm/kvm.c +9
-0@@ -23,6 +23,15 @@#include "cpu.h" #include "hw/arm/arm.h" +/* Check that cpu.h's idea of coprocessor fields matches KVM's */ +#if (CP_REG_SIZE_SHIFT !=KVM_REG_SIZE_SHIFT ) || \ + (CP_REG_SIZE_MASK !=KVM_REG_SIZE_MASK ) || \ + (CP_REG_SIZE_U32 !=KVM_REG_SIZE_U32 ) || \ + (CP_REG_SIZE_U64 !=KVM_REG_SIZE_U64 ) || \ + (CP_REG_ARM !=KVM_REG_ARM ) +#error mismatch between cpu.h and KVM header definitions +#endif +const KVMCapabilityInfo kvm_arch_required_capabilities[] = {KVM_CAP_LAST_INFO };target-arm/machine.c +67
-47@@ -148,11 +148,65 @@ static const VMStateInfo vmstate_cpsr = { .put = put_cpsr, }; +static void cpu_pre_save (void *opaque) +{ +ARMCPU *cpu = opaque; + +if (!write_cpustate_to_list (cpu)) { +/* This should never fail. */ +abort (); + } + + cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; +memcpy (cpu->cpreg_vmstate_indexes, cpu->cpreg_indexes, + cpu->cpreg_array_len *sizeof (uint64_t )); +memcpy (cpu->cpreg_vmstate_values, cpu->cpreg_values, + cpu->cpreg_array_len *sizeof (uint64_t )); +} + +static int cpu_post_load (void *opaque,int version_id) +{ +ARMCPU *cpu = opaque; +int i, v; + +/* Update the values list from the incoming migration data. +* Anything in the incoming data which we don't know about is +* a migration failure; anything we know about but the incoming +* data doesn't specify retains its current (reset) value. +* The indexes list remains untouched -- we only inspect the +* incoming migration index list so we can match the values array +* entries with the right slots in our own values array. +*/ + +for (i =0 , v =0 ; i < cpu->cpreg_array_len + && v < cpu->cpreg_vmstate_array_len; i++) { +if (cpu->cpreg_vmstate_indexes[v] > cpu->cpreg_indexes[i]) { +/* register in our list but not incoming : skip it */ +continue ; + } +if (cpu->cpreg_vmstate_indexes[v] < cpu->cpreg_indexes[i]) { +/* register in their list but not ours: fail migration */ +return -1 ; + } +/* matching register, copy the value over */ + cpu->cpreg_values[i] = cpu->cpreg_vmstate_values[v]; + v++; + } + +if (!write_list_to_cpustate (cpu)) { +return -1 ; + } + +return 0 ; +} +const VMStateDescription vmstate_arm_cpu = { .name ="cpu" , - .version_id =11 , - .minimum_version_id =11 , - .minimum_version_id_old =11 , + .version_id =12 , + .minimum_version_id =12 , + .minimum_version_id_old =12 , + .pre_save = cpu_pre_save, + .post_load = cpu_post_load, .fields = (VMStateField []) {VMSTATE_UINT32_ARRAY (env.regs,ARMCPU ,16 ), {@@ -169,50 +223,16 @@ const VMStateDescription vmstate_arm_cpu = {VMSTATE_UINT32_ARRAY (env.banked_r14,ARMCPU ,6 ),VMSTATE_UINT32_ARRAY (env.usr_regs,ARMCPU ,5 ),VMSTATE_UINT32_ARRAY (env.fiq_regs,ARMCPU ,5 ), -VMSTATE_UINT32 (env.cp15.c0_cpuid,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c0_cssel,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c1_sys,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c1_coproc,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c1_xscaleauxcr,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c1_scr,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_base0,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_base0_hi,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_base1,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_base1_hi,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_control,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_mask,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_base_mask,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_data,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c2_insn,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c3,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c5_insn,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c5_data,ARMCPU ), -VMSTATE_UINT32_ARRAY (env.cp15.c6_region,ARMCPU ,8 ), -VMSTATE_UINT32 (env.cp15.c6_insn,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c6_data,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c7_par,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c7_par_hi,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_insn,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_data,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_pmcr,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_pmcnten,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_pmovsr,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_pmxevtyper,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_pmuserenr,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c9_pminten,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c13_fcse,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c13_context,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c13_tls1,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c13_tls2,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c13_tls3,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_cpar,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_ticonfig,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_i_max,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_i_min,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_threadid,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_power_control,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_diagnostic,ARMCPU ), -VMSTATE_UINT32 (env.cp15.c15_power_diagnostic,ARMCPU ), +/* The length-check must come before the arrays to avoid +* incoming data possibly overflowing the array. +*/ +VMSTATE_INT32_LE (cpreg_vmstate_array_len,ARMCPU ), +VMSTATE_VARRAY_INT32 (cpreg_vmstate_indexes,ARMCPU , + cpreg_vmstate_array_len, +0 , vmstate_info_uint64, uint64_t), +VMSTATE_VARRAY_INT32 (cpreg_vmstate_values,ARMCPU , + cpreg_vmstate_array_len, +0 , vmstate_info_uint64, uint64_t),VMSTATE_UINT32 (env.exclusive_addr,ARMCPU ),VMSTATE_UINT32 (env.exclusive_val,ARMCPU ), VMSTATE_UINT32(env.exclusive_high, ARMCPU),Signature · 6 lines
1.7.9.5 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/cucslists/listinfo/kvmarm
PATCH 5/7 target-arm: Initialize cpreg list from KVM when using KVM
In reply to Peter MaydellWhen using KVM, use the kernel's initial state to set up the cpreg list, and sync to and from the kernel when doing migration.
--- target-arm/Makefile.objs | 1 + target-arm/kvm.c | 164 +++++++++++++++++++++++++++++++++++++++++++++- target-arm/kvm_arm.h | 33 ++++++++++ target-arm/machine.c | 30 +++++++-- 4 files changed, 222 insertions(+), 6 deletions(-)
target-arm/Makefile.objs +1 -0
@@ -1,5 +1,6 @@ obj-y += arm-semi.o obj-$(CONFIG_SOFTMMU) += machine.o obj-$(CONFIG_KVM) += kvm.o +obj-$(CONFIG_NO_KVM) += kvm-stub.o obj-y += translate.o op_helper.o helper.o cpu.o obj-y += neon_helper.o iwmmxt_helper.o
target-arm/kvm.c +163 -1
187 lines not on this page · raw message
target-arm/kvm_arm.h +33 -0
@@ -29,4 +29,37 @@*/ void kvm_arm_register_device (MemoryRegion *mr,uint64_t devid); +/** +* write_list_to_kvmstate: +* @cpu: ARMCPU +* +* For each register listed in the ARMCPU cpreg_indexes list, write +* its value from the cpreg_values list into the kernel (via ioctl). +* This updates KVM's working data structures from TCG data or +* from incoming migration state. +* +* Returns: true if all register values were updated correctly, +* false if some register was unknown to the kernel or could not +* be written (eg constant register with the wrong value). +* Note that we do not stop early on failure -- we will attempt +* writing all registers in the list. +*/ +bool write_list_to_kvmstate (ARMCPU *cpu); + +/** +* write_kvmstate_to_list: +* @cpu: ARMCPU +* +* For each register listed in the ARMCPU cpreg_indexes list, write +* its value from the kernel into the cpreg_values list. This is used to +* copy info from KVM's working data structures into TCG or +* for outbound migration. +* +* Returns: true if all register values were read correctly, +* false if some register was unknown or could not be read. +* Note that we do not stop early on failure -- we will attempt +* reading all registers in the list. +*/ +bool write_kvmstate_to_list (ARMCPU *cpu); +#endif
target-arm/machine.c +25 -5
@@ -1,5 +1,7 @@#include "hw/hw.h" #include "hw/boards.h" +#include "sysemu/kvm.h" +#include "kvm_arm.h" static bool vfp_needed (void *opaque) {
@@ -152,9 +154,16 @@ static void cpu_pre_save(void *opaque) {ARMCPU *cpu = opaque; -if (!write_cpustate_to_list (cpu)) { -/* This should never fail. */ -abort (); +if (kvm_enabled ()) { +if (!write_kvmstate_to_list (cpu)) { +/* This should never fail */ +abort (); + } + }else { +if (!write_cpustate_to_list (cpu)) { +/* This should never fail. */ +abort (); + } } cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
@@ -193,8 +202,19 @@ static int cpu_post_load(void *opaque, int version_id) v++; } -if (!write_list_to_cpustate (cpu)) { -return -1 ; +if (kvm_enabled ()) { +if (!write_list_to_kvmstate (cpu)) { +return -1 ; + } +/* Note that it's OK for the TCG side not to know about +* every register in the list; KVM is authoritative if +* we're using it. +*/ +write_list_to_cpustate (cpu); + }else { +if (!write_list_to_cpustate (cpu)) { +return -1 ; + } }return 0 ;
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH 5/7] target-arm: Initialize cpreg list from KVM when using KVM
Quoted 11 lines · Peter Maydell
target-arm/Makefile.objs | 1 + target-arm/kvm.c | 164 +++++++++++++++++++++++++++++++++++++++++++++- target-arm/kvm_arm.h | 33 ++++++++++ target-arm/machine.c | 30 +++++++-- 4 files changed, 222 insertions(+), 6 deletions(-)target-arm/Makefile.objs +0
-0
target-arm/Makefile.objs line 4
Quoted 4 lines
target-arm/Makefile.objs +1
-0@@ -1,5 +1,6 @@ obj-y += arm-semi.o obj-$(CONFIG_SOFTMMU) += machine.o obj-$(CONFIG_KVM) += kvm.o +obj-$(CONFIG_NO_KVM) += kvm-stub.o
but I forgot to git add kvm-stub.c. Whoops; will be fixed in v2.
thanks
-- PMM
PATCH 6/7 target-arm: Reinitialize all KVM VCPU registers on reset
In reply to Peter MaydellSince the ARM KVM API doesn't include a "reset this VCPU" ioctl, we have to capture the initial values of every register it knows about so that we can reset the VCPU by feeding those values back again.
--- target-arm/cpu-qom.h | 6 +++++- target-arm/kvm.c | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)
target-arm/cpu-qom.h +5 -1
@@ -72,7 +72,11 @@ typedef struct ARMCPU {uint64_t *cpreg_indexes;/* Values of the registers (cpreg_indexes[i]'s value is cpreg_values[i]) */ uint64_t *cpreg_values; -/* Length of the indexes, values arrays */ +/* When using KVM, keeps a copy of the initial state of the VCPU, +* so that on reset we can feed the reset values back into the kernel. +*/ +uint64_t *cpreg_reset_values; +/* Length of the indexes, values, reset_values arrays */ int32_t cpreg_array_len;/* These are used only for migration: incoming data arrives in * these fields and is sanity checked in post_load before copying
target-arm/kvm.c +16 -0
@@ -162,6 +162,13 @@ int kvm_arch_init_vcpu(CPUState *cs) goto out; } +/* Save a copy of the initial register values so that we can +* feed it back to the kernel on VCPU reset. +*/ + cpu->cpreg_reset_values =g_memdup (cpu->cpreg_values, + cpu->cpreg_array_len * +sizeof (cpu->cpreg_values[0 ])); + out:g_free (rlp);return ret;
@@ -603,6 +610,15 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)void kvm_arch_reset_vcpu (CPUState *cs) { +/* Feed the kernel back its initial register state */ +ARMCPU *cpu =ARM_CPU (cs); + +memmove (cpu->cpreg_values, cpu->cpreg_reset_values, + cpu->cpreg_array_len *sizeof (cpu->cpreg_values[0 ])); + +if (!write_list_to_kvmstate (cpu)) { +abort (); + } }bool kvm_arch_stop_on_emulation_error (CPUState *cs)
Signature · 1 line
1.7.9.5
PATCH 7/7 target-arm: Use tuple list to sync cp regs with KVM
In reply to Peter MaydellUse the tuple list of cp registers for syncing KVM state to QEMU, rather than only syncing a very minimal set by hand.
--- target-arm/kvm.c | 103 +++++++++++++++++------------------------------------- 1 file changed, 33 insertions(+), 70 deletions(-)
target-arm/kvm.c +33 -70
@@ -344,17 +344,6 @@ typedef struct Reg { offsetof(CPUARMState, QEMUFIELD) \ } -#define CP15REG (CRN ,CRM ,OPC1 ,OPC2 ,QEMUFIELD ) \ - { \ - KVM_REG_ARM | KVM_REG_SIZE_U32 | \ - (15 << KVM_REG_ARM_COPROC_SHIFT) | \ - ((CRN) << KVM_REG_ARM_32_CRN_SHIFT) | \ - ((CRM) << KVM_REG_ARM_CRM_SHIFT) | \ - ((OPC1) << KVM_REG_ARM_OPC1_SHIFT) | \ - ((OPC2) << KVM_REG_ARM_32_OPC2_SHIFT), \ - offsetof(CPUARMState, QEMUFIELD) \ - } -#define VFPSYSREG (R ) \ { \ KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | \
@@ -403,12 +392,6 @@ static const Reg regs[] = {COREREG (fiq_regs[7 ], banked_spsr[5 ]),/* R15 */ COREREG (usr_regs.uregs[15 ], regs[15 ]), -/* A non-comprehensive set of cp15 registers. -* TODO: drive this from the cp_regs hashtable instead. -*/ -CP15REG (1 ,0 ,0 ,0 , cp15.c1_sys),/* SCTLR */ -CP15REG (2 ,0 ,0 ,2 , cp15.c2_control),/* TTBCR */ -CP15REG (3 ,0 ,0 ,0 , cp15.c3),/* DACR */ /* VFP system registers */ VFPSYSREG (FPSID ), VFPSYSREG(MVFR1),
@@ -426,7 +409,6 @@ int kvm_arch_put_registers(CPUState *cs, int level)int mode, bn;int ret, i;uint32_t cpsr, fpscr; -uint64_t ttbr;/* Make sure the banked regs are properly set */ mode = env->uncached_cpsr &CPSR_M ;
@@ -460,26 +442,6 @@ int kvm_arch_put_registers(CPUState *cs, int level)return ret; } -/* TTBR0: cp15 crm=2 opc1=0 */ - ttbr = ((uint64_t )env->cp15.c2_base0_hi <<32 ) | env->cp15.c2_base0; - r.id =KVM_REG_ARM |KVM_REG_SIZE_U64 | (15 <<KVM_REG_ARM_COPROC_SHIFT ) | - (2 <<KVM_REG_ARM_CRM_SHIFT ) | (0 <<KVM_REG_ARM_OPC1_SHIFT ); - r.addr = (uintptr_t )(&ttbr); - ret =kvm_vcpu_ioctl (cs,KVM_SET_ONE_REG , &r); -if (ret) { -return ret; - } - -/* TTBR1: cp15 crm=2 opc1=1 */ - ttbr = ((uint64_t )env->cp15.c2_base1_hi <<32 ) | env->cp15.c2_base1; - r.id =KVM_REG_ARM |KVM_REG_SIZE_U64 | (15 <<KVM_REG_ARM_COPROC_SHIFT ) | - (2 <<KVM_REG_ARM_CRM_SHIFT ) | (1 <<KVM_REG_ARM_OPC1_SHIFT ); - r.addr = (uintptr_t )(&ttbr); - ret =kvm_vcpu_ioctl (cs,KVM_SET_ONE_REG , &r); -if (ret) { -return ret; - } -/* VFP registers */ r.id =KVM_REG_ARM |KVM_REG_SIZE_U64 |KVM_REG_ARM_VFP ;for (i =0 ; i <32 ; i++) {
@@ -496,6 +458,31 @@ int kvm_arch_put_registers(CPUState *cs, int level) fpscr =vfp_get_fpscr (env); r.addr = (uintptr_t )&fpscr; ret =kvm_vcpu_ioctl (cs,KVM_SET_ONE_REG , &r); +if (ret) { +return ret; + } + +/* Note that we do not call write_cpustate_to_list() +* here, so we are only writing the tuple list back to +* KVM. This is safe because nothing can change the +* CPUARMState cp15 fields (in particular gdb accesses cannot) +* and so there are no changes to sync. In fact syncing would +* be wrong at this point: for a constant register where TCG and +* KVM disagree about its value, the preceding write_list_to_cpustate() +* would not have had any effect on the CPUARMState value (since the +* register is read-only), and a write_cpustate_to_list() here would +* then try to write the TCG value back into KVM -- this would either +* fail or incorrectly change the value the guest sees. +* +* If we ever want to allow the user to modify cp15 registers via +* the gdb stub, we would need to be more clever here (for instance +* tracking the set of registers kvm_arch_get_registers() successfully +* managed to update the CPUARMState with, and only allowing those +* to be written back up into the kernel). +*/ +if (!write_list_to_kvmstate (cpu)) { +return EINVAL ; + }return ret; }
@@ -508,7 +495,6 @@ int kvm_arch_get_registers(CPUState *cs)int mode, bn;int ret, i;uint32_t cpsr, fpscr; -uint64_t ttbr;for (i =0 ; i <ARRAY_SIZE (regs); i++) { r.id = regs[i].id;
@@ -529,28 +515,6 @@ int kvm_arch_get_registers(CPUState *cs) }cpsr_write (env, cpsr,0xffffffff ); -/* TTBR0: cp15 crm=2 opc1=0 */ - r.id =KVM_REG_ARM |KVM_REG_SIZE_U64 | (15 <<KVM_REG_ARM_COPROC_SHIFT ) | - (2 <<KVM_REG_ARM_CRM_SHIFT ) | (0 <<KVM_REG_ARM_OPC1_SHIFT ); - r.addr = (uintptr_t )(&ttbr); - ret =kvm_vcpu_ioctl (cs,KVM_GET_ONE_REG , &r); -if (ret) { -return ret; - } - env->cp15.c2_base0_hi = ttbr >>32 ; - env->cp15.c2_base0 = ttbr; - -/* TTBR1: cp15 crm=2 opc1=1 */ - r.id =KVM_REG_ARM |KVM_REG_SIZE_U64 | (15 <<KVM_REG_ARM_COPROC_SHIFT ) | - (2 <<KVM_REG_ARM_CRM_SHIFT ) | (1 <<KVM_REG_ARM_OPC1_SHIFT ); - r.addr = (uintptr_t )(&ttbr); - ret =kvm_vcpu_ioctl (cs,KVM_GET_ONE_REG , &r); -if (ret) { -return ret; - } - env->cp15.c2_base1_hi = ttbr >>32 ; - env->cp15.c2_base1 = ttbr; -/* Make sure the current mode regs are properly set */ mode = env->uncached_cpsr &CPSR_M ; bn =bank_number (mode);
@@ -563,15 +527,6 @@ int kvm_arch_get_registers(CPUState *cs) env->regs[14 ] = env->banked_r14[bn]; env->spsr = env->banked_spsr[bn]; -/* The main GET_ONE_REG loop above set c2_control, but we need to -* update some extra cached precomputed values too. -* When this is driven from the cp_regs hashtable then this ugliness -* can disappear because we'll use the access function which sets -* these values automatically. -*/ - env->cp15.c2_mask = ~(0xffffffffu >> env->cp15.c2_control); - env->cp15.c2_base_mask = ~(0x3fffu >> env->cp15.c2_control); -/* VFP registers */ r.id =KVM_REG_ARM |KVM_REG_SIZE_U64 |KVM_REG_ARM_VFP ;for (i =0 ; i <32 ; i++) {
@@ -592,6 +547,14 @@ int kvm_arch_get_registers(CPUState *cs) }vfp_set_fpscr (env, fpscr); +if (!write_kvmstate_to_list (cpu)) { +return EINVAL ; + } +/* Note that it's OK to have registers which aren't in CPUState, +* so we can ignore a failure return here. +*/ +write_list_to_cpustate (cpu); +return 0 ; }
Signature · 1 line
1.7.9.5
Quoted 22 lines · Peter Maydell
This patch series overhauls how we handle ARM coprocessor registers, so that we use a consistent approach for migration, reset and QEMU<->KVM synchronisation, driven by the kernel's list of supported registers.
The basic principle here is that we trust the kernel's list of what registers it knows about, and that QEMU doesn't have to have specific knowledge of a coprocessor register to support running and migrating a KVM session on a kernel that does support that register.
We maintain a list of cp registers, which is initialized either from the current cpreg hashtable (for TCG), or by querying the kernel (for KVM). For migration we simply send the lists of register indexes and values; migration fails if there's a register the destination kernel is unaware of, or if the value can't be set as required, but isn't gated on whether source or destination QEMU know about the register.
We also use the register list to properly reset the vcpu by simply feeding it back the initial set of register values; this fixes a bug where we weren't resetting everything we should have (though Linux guests don't care about most reset values).
Quoted 9 lines
Note that vm save/load with KVM requires that you run with -machine kernel_irqchip=off, because the kernel doesn't currently support save/load of either the VGIC or virtual timer state. It may also be necessary to nobble the device tree blob to remove the "armv7-timer" node so the guest doesn't try to use the vtimers. Migration between TCG and KVM is not supported at the moment (it would require us to add a lot of registers to TCG, which I may do at some point, but this is a bit of an obscure usecase IMHO).
[...]
So there were some qemu magic in here that I didn't get into, so I didn't do a full comprehensive review, but it looks pretty good overall, and I couldn't spot any obvious bugs.
If we're ever going to benchmark live migration I have a feeling that {GET/SET}_MULTIPLE_REGS interface that Alex once talked about may come in handy, but until then I'm happy with exercising the kernel entry/exit path a bit.
-Christoffer