Message ID | 1360317896-22666-2-git-send-email-bharat.bhushan@freescale.com |
---|---|
State | New |
Headers | show |
On 08.02.2013, at 11:04, Bharat Bhushan wrote: > Enable the KVM emulated watchdog if KVM supports (use the > capability enablement in watchdog handler). Also watchdog exit > (KVM_EXIT_WATCHDOG) handling is added. > Watchdog state machine is cleared whenever VM state changes to running. > This is to handle the cases like return from debug halt etc. > > Signed-off-by: Bharat Bhushan <bharat.bhushan@freescale.com> > --- > hw/ppc_booke.c | 43 +++++++++++++++++++++- > target-ppc/kvm.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++ > target-ppc/kvm_ppc.h | 33 +++++++++++++++++ > 3 files changed, 173 insertions(+), 2 deletions(-) > > diff --git a/hw/ppc_booke.c b/hw/ppc_booke.c > index 25a4e91..1eb8415 100644 > --- a/hw/ppc_booke.c > +++ b/hw/ppc_booke.c > @@ -28,7 +28,7 @@ > #include "nvram.h" > #include "qemu/log.h" > #include "loader.h" > - > +#include "kvm_ppc.h" > > /* Timer Control Register */ > > @@ -211,6 +211,7 @@ void store_booke_tsr(CPUPPCState *env, target_ulong val) > PowerPCCPU *cpu = ppc_env_get_cpu(env); > > env->spr[SPR_BOOKE_TSR] &= ~val; > + kvmppc_clear_tsr_bits(cpu, val); > booke_update_irq(cpu); > } > > @@ -222,6 +223,7 @@ void store_booke_tcr(CPUPPCState *env, target_ulong val) > > tb_env = env->tb_env; > env->spr[SPR_BOOKE_TCR] = val; > + kvmppc_set_tcr(cpu); > > booke_update_irq(cpu); > > @@ -234,7 +236,6 @@ void store_booke_tcr(CPUPPCState *env, target_ulong val) > booke_get_wdt_target(env, tb_env), > &booke_timer->wdt_next, > booke_timer->wdt_timer); > - > } > > static void ppc_booke_timer_reset_handle(void *opaque) > @@ -244,14 +245,40 @@ static void ppc_booke_timer_reset_handle(void *opaque) > > env->spr[SPR_BOOKE_TSR] = 0; > env->spr[SPR_BOOKE_TCR] = 0; > + kvmppc_sync_set_timer_regs(cpu); If you replace the direct spr access with store_booke_tcr(env, 0); store_booke_tsr(env, -1); then you should automatically get the kvm updates for free, no? Reset is not timing critical, so optimizing this path is useless. > > booke_update_irq(cpu); > } > > +/* > + * This function will be called whenever the CPU state changes. > + * CPU states are defined "typedef enum RunState". > + * Regarding timer, When CPU state changes to running after debug halt > + * or similar cases which takes time then in between final watchdog > + * expiry happenes. This will cause exit to QEMU and configured watchdog > + * action will be taken. To avoid this we always clear the watchdog state when > + * state changes to running. > + */ > +static void cpu_state_change_handler(void *opaque, int running, RunState state) > +{ > + PowerPCCPU *cpu = opaque; > + CPUPPCState *env = &cpu->env; > + > + if (!running) { > + return; > + } > + > + /* > + * Clear watchdog interrupt condition by clearing TSR. > + */ > + store_booke_tsr(env, TSR_ENW | TSR_WIS | TSR_WRS_MASK); > +} > + > void ppc_booke_timers_init(PowerPCCPU *cpu, uint32_t freq, uint32_t flags) > { > ppc_tb_t *tb_env; > booke_timer_t *booke_timer; > + int ret = 0; > > tb_env = g_malloc0(sizeof(ppc_tb_t)); > booke_timer = g_malloc0(sizeof(booke_timer_t)); > @@ -269,5 +296,17 @@ void ppc_booke_timers_init(PowerPCCPU *cpu, uint32_t freq, uint32_t flags) > booke_timer->wdt_timer = > qemu_new_timer_ns(vm_clock, &booke_wdt_cb, cpu); > > + ret = kvmppc_booke_watchdog_enable(cpu); > + > + if (ret) { > + /* TODO: Start the QEMU emulated watchdog if not running on KVM. > + * Also start the QEMU emulated watchdog if KVM does not support > + * emulated watchdog or somehow it is not enabled (supported but > + * not enabled is though some bug and requires debugging :)). > + */ > + } > + > + qemu_add_vm_change_state_handler(cpu_state_change_handler, cpu); > + > qemu_register_reset(ppc_booke_timer_reset_handle, cpu); > } > diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c > index 2c64c63..d0898f5 100644 > --- a/target-ppc/kvm.c > +++ b/target-ppc/kvm.c > @@ -36,6 +36,7 @@ > #include "hw/sysbus.h" > #include "hw/spapr.h" > #include "hw/spapr_vio.h" > +#include "hw/watchdog.h" > > //#define DEBUG_KVM > > @@ -61,6 +62,7 @@ static int cap_ppc_smt; > static int cap_ppc_rma; > static int cap_spapr_tce; > static int cap_hior; > +static int cap_ppc_watchdog; > > /* XXX We have a race condition where we actually have a level triggered > * interrupt, but the infrastructure can't expose that yet, so the guest > @@ -90,6 +92,7 @@ int kvm_arch_init(KVMState *s) > cap_ppc_rma = kvm_check_extension(s, KVM_CAP_PPC_RMA); > cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE); > cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR); > + cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG); > > if (!cap_interrupt_level) { > fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the " > @@ -856,6 +859,12 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) > run->epr.epr = ldl_phys(env->mpic_iack); > ret = 0; > break; > + case KVM_EXIT_WATCHDOG: > + dprintf("handle watchdog expiry\n"); > + watchdog_perform_action(); > + ret = 0; > + break; > + > default: > fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); > ret = -1; > @@ -865,6 +874,96 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) > return ret; > } > > +void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu) > +{ > + CPUPPCState *env = &cpu->env; > + if (kvm_enabled()) { > + cpu_synchronize_state(env); > + } > +} unused function Alex > + > +int kvmppc_sync_set_timer_regs(PowerPCCPU *cpu) > +{ > + CPUPPCState *env = &cpu->env; > + CPUState *cs = CPU(cpu); > + struct kvm_sregs sregs; > + > + if (!kvm_enabled() || !cap_booke_sregs) { > + return -1; > + } > + > + sregs.u.e.tsr = env->spr[SPR_BOOKE_TSR]; > + sregs.u.e.tcr = env->spr[SPR_BOOKE_TCR]; > + sregs.u.e.update_special = KVM_SREGS_E_BASE | KVM_SREGS_E_UPDATE_TSR; > + > + return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); > +} > + > +int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) > +{ > + CPUState *cs = CPU(cpu); > + uint32_t bits = tsr_bits; > + struct kvm_one_reg reg = { > + .id = KVM_REG_PPC_OR_TSR, > + .addr = (uintptr_t) &bits, > + }; > + > + return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); > +} > + > +int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) > +{ > + > + CPUState *cs = CPU(cpu); > + uint32_t bits = tsr_bits; > + struct kvm_one_reg reg = { > + .id = KVM_REG_PPC_CLEAR_TSR, > + .addr = (uintptr_t) &bits, > + }; > + > + return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); > +} > + > +int kvmppc_set_tcr(PowerPCCPU *cpu) > +{ > + CPUState *cs = CPU(cpu); > + CPUPPCState *env = &cpu->env; > + uint32_t tcr = env->spr[SPR_BOOKE_TCR]; > + > + struct kvm_one_reg reg = { > + .id = KVM_REG_PPC_SET_TCR, > + .addr = (uintptr_t) &tcr, > + }; > + > + return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); > +} > + > +int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu) > +{ > + CPUState *cs = CPU(cpu); > + struct kvm_enable_cap encap = {}; > + int ret; > + > + if (!kvm_enabled()) { > + return -1; > + } > + > + if (!cap_ppc_watchdog) { > + printf("warning: KVM does not support watchdog"); > + return -1; > + } > + > + encap.cap = KVM_CAP_PPC_BOOKE_WATCHDOG; > + ret = kvm_vcpu_ioctl(cs, KVM_ENABLE_CAP, &encap); > + if (ret < 0) { > + fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n", > + __func__, strerror(-ret)); > + return ret; > + } > + > + return ret; > +} > + > static int read_cpuinfo(const char *field, char *value, int len) > { > FILE *f; > diff --git a/target-ppc/kvm_ppc.h b/target-ppc/kvm_ppc.h > index c30b006..f0bf95a 100644 > --- a/target-ppc/kvm_ppc.h > +++ b/target-ppc/kvm_ppc.h > @@ -27,6 +27,12 @@ int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level); > void kvmppc_set_papr(PowerPCCPU *cpu); > void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy); > int kvmppc_smt_threads(void); > +void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu); > +int kvmppc_sync_set_timer_regs(PowerPCCPU *cpu); > +int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits); > +int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits); > +int kvmppc_set_tcr(PowerPCCPU *cpu); > +int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu); > #ifndef CONFIG_USER_ONLY > off_t kvmppc_alloc_rma(const char *name, MemoryRegion *sysmem); > void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd); > @@ -90,6 +96,33 @@ static inline int kvmppc_smt_threads(void) > { > return 1; > } > +static void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu) > +{ > +} > + > +static int kvmppc_sync_set_timer_regs(PowerPCCPU *cpu) > +{ > + return 0; > +} > + > +int kvmppc_or_tsr_bits(PowerPCCPU *cpu, u32 tsr_bits) > +{ > + return 0; > +} > +int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, u32 tsr_bits) > +{ > + return 0; > +} > + > +int kvmppc_set_tcr(PowerPCCPU *cpu) > +{ > + return 0; > +} > + > +static inline int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu) > +{ > + return -1; > +} > > #ifndef CONFIG_USER_ONLY > static inline off_t kvmppc_alloc_rma(const char *name, MemoryRegion *sysmem) > -- > 1.7.0.4 > >
diff --git a/hw/ppc_booke.c b/hw/ppc_booke.c index 25a4e91..1eb8415 100644 --- a/hw/ppc_booke.c +++ b/hw/ppc_booke.c @@ -28,7 +28,7 @@ #include "nvram.h" #include "qemu/log.h" #include "loader.h" - +#include "kvm_ppc.h" /* Timer Control Register */ @@ -211,6 +211,7 @@ void store_booke_tsr(CPUPPCState *env, target_ulong val) PowerPCCPU *cpu = ppc_env_get_cpu(env); env->spr[SPR_BOOKE_TSR] &= ~val; + kvmppc_clear_tsr_bits(cpu, val); booke_update_irq(cpu); } @@ -222,6 +223,7 @@ void store_booke_tcr(CPUPPCState *env, target_ulong val) tb_env = env->tb_env; env->spr[SPR_BOOKE_TCR] = val; + kvmppc_set_tcr(cpu); booke_update_irq(cpu); @@ -234,7 +236,6 @@ void store_booke_tcr(CPUPPCState *env, target_ulong val) booke_get_wdt_target(env, tb_env), &booke_timer->wdt_next, booke_timer->wdt_timer); - } static void ppc_booke_timer_reset_handle(void *opaque) @@ -244,14 +245,40 @@ static void ppc_booke_timer_reset_handle(void *opaque) env->spr[SPR_BOOKE_TSR] = 0; env->spr[SPR_BOOKE_TCR] = 0; + kvmppc_sync_set_timer_regs(cpu); booke_update_irq(cpu); } +/* + * This function will be called whenever the CPU state changes. + * CPU states are defined "typedef enum RunState". + * Regarding timer, When CPU state changes to running after debug halt + * or similar cases which takes time then in between final watchdog + * expiry happenes. This will cause exit to QEMU and configured watchdog + * action will be taken. To avoid this we always clear the watchdog state when + * state changes to running. + */ +static void cpu_state_change_handler(void *opaque, int running, RunState state) +{ + PowerPCCPU *cpu = opaque; + CPUPPCState *env = &cpu->env; + + if (!running) { + return; + } + + /* + * Clear watchdog interrupt condition by clearing TSR. + */ + store_booke_tsr(env, TSR_ENW | TSR_WIS | TSR_WRS_MASK); +} + void ppc_booke_timers_init(PowerPCCPU *cpu, uint32_t freq, uint32_t flags) { ppc_tb_t *tb_env; booke_timer_t *booke_timer; + int ret = 0; tb_env = g_malloc0(sizeof(ppc_tb_t)); booke_timer = g_malloc0(sizeof(booke_timer_t)); @@ -269,5 +296,17 @@ void ppc_booke_timers_init(PowerPCCPU *cpu, uint32_t freq, uint32_t flags) booke_timer->wdt_timer = qemu_new_timer_ns(vm_clock, &booke_wdt_cb, cpu); + ret = kvmppc_booke_watchdog_enable(cpu); + + if (ret) { + /* TODO: Start the QEMU emulated watchdog if not running on KVM. + * Also start the QEMU emulated watchdog if KVM does not support + * emulated watchdog or somehow it is not enabled (supported but + * not enabled is though some bug and requires debugging :)). + */ + } + + qemu_add_vm_change_state_handler(cpu_state_change_handler, cpu); + qemu_register_reset(ppc_booke_timer_reset_handle, cpu); } diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c index 2c64c63..d0898f5 100644 --- a/target-ppc/kvm.c +++ b/target-ppc/kvm.c @@ -36,6 +36,7 @@ #include "hw/sysbus.h" #include "hw/spapr.h" #include "hw/spapr_vio.h" +#include "hw/watchdog.h" //#define DEBUG_KVM @@ -61,6 +62,7 @@ static int cap_ppc_smt; static int cap_ppc_rma; static int cap_spapr_tce; static int cap_hior; +static int cap_ppc_watchdog; /* XXX We have a race condition where we actually have a level triggered * interrupt, but the infrastructure can't expose that yet, so the guest @@ -90,6 +92,7 @@ int kvm_arch_init(KVMState *s) cap_ppc_rma = kvm_check_extension(s, KVM_CAP_PPC_RMA); cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE); cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR); + cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG); if (!cap_interrupt_level) { fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the " @@ -856,6 +859,12 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) run->epr.epr = ldl_phys(env->mpic_iack); ret = 0; break; + case KVM_EXIT_WATCHDOG: + dprintf("handle watchdog expiry\n"); + watchdog_perform_action(); + ret = 0; + break; + default: fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); ret = -1; @@ -865,6 +874,96 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) return ret; } +void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu) +{ + CPUPPCState *env = &cpu->env; + if (kvm_enabled()) { + cpu_synchronize_state(env); + } +} + +int kvmppc_sync_set_timer_regs(PowerPCCPU *cpu) +{ + CPUPPCState *env = &cpu->env; + CPUState *cs = CPU(cpu); + struct kvm_sregs sregs; + + if (!kvm_enabled() || !cap_booke_sregs) { + return -1; + } + + sregs.u.e.tsr = env->spr[SPR_BOOKE_TSR]; + sregs.u.e.tcr = env->spr[SPR_BOOKE_TCR]; + sregs.u.e.update_special = KVM_SREGS_E_BASE | KVM_SREGS_E_UPDATE_TSR; + + return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); +} + +int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) +{ + CPUState *cs = CPU(cpu); + uint32_t bits = tsr_bits; + struct kvm_one_reg reg = { + .id = KVM_REG_PPC_OR_TSR, + .addr = (uintptr_t) &bits, + }; + + return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); +} + +int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) +{ + + CPUState *cs = CPU(cpu); + uint32_t bits = tsr_bits; + struct kvm_one_reg reg = { + .id = KVM_REG_PPC_CLEAR_TSR, + .addr = (uintptr_t) &bits, + }; + + return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); +} + +int kvmppc_set_tcr(PowerPCCPU *cpu) +{ + CPUState *cs = CPU(cpu); + CPUPPCState *env = &cpu->env; + uint32_t tcr = env->spr[SPR_BOOKE_TCR]; + + struct kvm_one_reg reg = { + .id = KVM_REG_PPC_SET_TCR, + .addr = (uintptr_t) &tcr, + }; + + return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); +} + +int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu) +{ + CPUState *cs = CPU(cpu); + struct kvm_enable_cap encap = {}; + int ret; + + if (!kvm_enabled()) { + return -1; + } + + if (!cap_ppc_watchdog) { + printf("warning: KVM does not support watchdog"); + return -1; + } + + encap.cap = KVM_CAP_PPC_BOOKE_WATCHDOG; + ret = kvm_vcpu_ioctl(cs, KVM_ENABLE_CAP, &encap); + if (ret < 0) { + fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n", + __func__, strerror(-ret)); + return ret; + } + + return ret; +} + static int read_cpuinfo(const char *field, char *value, int len) { FILE *f; diff --git a/target-ppc/kvm_ppc.h b/target-ppc/kvm_ppc.h index c30b006..f0bf95a 100644 --- a/target-ppc/kvm_ppc.h +++ b/target-ppc/kvm_ppc.h @@ -27,6 +27,12 @@ int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level); void kvmppc_set_papr(PowerPCCPU *cpu); void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy); int kvmppc_smt_threads(void); +void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu); +int kvmppc_sync_set_timer_regs(PowerPCCPU *cpu); +int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits); +int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits); +int kvmppc_set_tcr(PowerPCCPU *cpu); +int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu); #ifndef CONFIG_USER_ONLY off_t kvmppc_alloc_rma(const char *name, MemoryRegion *sysmem); void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd); @@ -90,6 +96,33 @@ static inline int kvmppc_smt_threads(void) { return 1; } +static void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu) +{ +} + +static int kvmppc_sync_set_timer_regs(PowerPCCPU *cpu) +{ + return 0; +} + +int kvmppc_or_tsr_bits(PowerPCCPU *cpu, u32 tsr_bits) +{ + return 0; +} +int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, u32 tsr_bits) +{ + return 0; +} + +int kvmppc_set_tcr(PowerPCCPU *cpu) +{ + return 0; +} + +static inline int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu) +{ + return -1; +} #ifndef CONFIG_USER_ONLY static inline off_t kvmppc_alloc_rma(const char *name, MemoryRegion *sysmem)
Enable the KVM emulated watchdog if KVM supports (use the capability enablement in watchdog handler). Also watchdog exit (KVM_EXIT_WATCHDOG) handling is added. Watchdog state machine is cleared whenever VM state changes to running. This is to handle the cases like return from debug halt etc. Signed-off-by: Bharat Bhushan <bharat.bhushan@freescale.com> --- hw/ppc_booke.c | 43 +++++++++++++++++++++- target-ppc/kvm.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++ target-ppc/kvm_ppc.h | 33 +++++++++++++++++ 3 files changed, 173 insertions(+), 2 deletions(-)