From patchwork Tue Jan 15 04:31:55 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bharat Bhushan X-Patchwork-Id: 211988 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 6F6D92C00A3 for ; Tue, 15 Jan 2013 15:40:06 +1100 (EST) Received: from localhost ([::1]:60610 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TuyJf-0002dm-BT for incoming@patchwork.ozlabs.org; Mon, 14 Jan 2013 23:40:03 -0500 Received: from eggs.gnu.org ([208.118.235.92]:53025) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TuyJR-0002bD-G9 for qemu-devel@nongnu.org; Mon, 14 Jan 2013 23:39:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TuyJK-0004II-DW for qemu-devel@nongnu.org; Mon, 14 Jan 2013 23:39:49 -0500 Received: from ch1ehsobe003.messaging.microsoft.com ([216.32.181.183]:33498 helo=ch1outboundpool.messaging.microsoft.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TuyJK-0004Hx-8j for qemu-devel@nongnu.org; Mon, 14 Jan 2013 23:39:42 -0500 Received: from mail3-ch1-R.bigfish.com (10.43.68.250) by CH1EHSOBE003.bigfish.com (10.43.70.53) with Microsoft SMTP Server id 14.1.225.23; Tue, 15 Jan 2013 04:39:41 +0000 Received: from mail3-ch1 (localhost [127.0.0.1]) by mail3-ch1-R.bigfish.com (Postfix) with ESMTP id 462E83C0102; Tue, 15 Jan 2013 04:39:41 +0000 (UTC) X-Forefront-Antispam-Report: CIP:70.37.183.190; KIP:(null); UIP:(null); IPV:NLI; H:mail.freescale.net; RD:none; EFVD:NLI X-SpamScore: 6 X-BigFish: VS6(zz853kzz1ee6h1de0h1202h1e76h1d1ah1d2ah1082kzz8275bhz2dh2a8h668h839hd24he5bhf0ah107ah1288h12a5h12a9h12bdh12e5h137ah139eh13b6h1441h1504h1537h162dh1631h1758h1898h1155h) Received: from mail3-ch1 (localhost.localdomain [127.0.0.1]) by mail3-ch1 (MessageSwitch) id 1358224778585749_17172; Tue, 15 Jan 2013 04:39:38 +0000 (UTC) Received: from CH1EHSMHS022.bigfish.com (snatpool3.int.messaging.microsoft.com [10.43.68.227]) by mail3-ch1.bigfish.com (Postfix) with ESMTP id 8348C4C047F; Tue, 15 Jan 2013 04:39:38 +0000 (UTC) Received: from mail.freescale.net (70.37.183.190) by CH1EHSMHS022.bigfish.com (10.43.70.22) with Microsoft SMTP Server (TLS) id 14.1.225.23; Tue, 15 Jan 2013 04:39:38 +0000 Received: from az84smr01.freescale.net (10.64.34.197) by 039-SN1MMR1-001.039d.mgd.msft.net (10.84.1.13) with Microsoft SMTP Server (TLS) id 14.2.318.3; Tue, 15 Jan 2013 04:39:37 +0000 Received: from freescale.com ([10.232.15.72]) by az84smr01.freescale.net (8.14.3/8.14.0) with SMTP id r0F4dXPF014151; Mon, 14 Jan 2013 21:39:34 -0700 Received: by freescale.com (sSMTP sendmail emulation); Tue, 15 Jan 2013 10:02:01 +0530 From: Bharat Bhushan To: , , Date: Tue, 15 Jan 2013 10:01:55 +0530 Message-ID: <1358224315-8101-2-git-send-email-bharat.bhushan@freescale.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1358224315-8101-1-git-send-email-bharat.bhushan@freescale.com> References: <1358224315-8101-1-git-send-email-bharat.bhushan@freescale.com> MIME-Version: 1.0 X-OriginatorOrg: freescale.com X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 X-Received-From: 216.32.181.183 Cc: Bharat Bhushan Subject: [Qemu-devel] [PATCH v3] Enable kvm emulated watchdog X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- hw/ppc_booke.c | 48 ++++++++++++++++++++++++++++++++++++- target-ppc/kvm.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ target-ppc/kvm_ppc.h | 17 +++++++++++++ 3 files changed, 128 insertions(+), 1 deletions(-) diff --git a/hw/ppc_booke.c b/hw/ppc_booke.c index 25a4e91..a6adca9 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 */ @@ -210,7 +210,9 @@ void store_booke_tsr(CPUPPCState *env, target_ulong val) { PowerPCCPU *cpu = ppc_env_get_cpu(env); + kvmppc_sync_get_timer_regs(cpu); env->spr[SPR_BOOKE_TSR] &= ~val; + kvmppc_sync_set_timer_regs(cpu); booke_update_irq(cpu); } @@ -220,8 +222,10 @@ void store_booke_tcr(CPUPPCState *env, target_ulong val) ppc_tb_t *tb_env = env->tb_env; booke_timer_t *booke_timer = tb_env->opaque; + kvmppc_sync_get_timer_regs(cpu); tb_env = env->tb_env; env->spr[SPR_BOOKE_TCR] = val; + kvmppc_sync_set_timer_regs(cpu); booke_update_irq(cpu); @@ -237,13 +241,42 @@ void store_booke_tcr(CPUPPCState *env, target_ulong val) } +/* + * 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); +} + static void ppc_booke_timer_reset_handle(void *opaque) { PowerPCCPU *cpu = opaque; CPUPPCState *env = &cpu->env; + /* + * No need to call kvmppc_sync_get_timer_regs() as all cpu registers + * will be synchronized before calling the reset handlers. + */ env->spr[SPR_BOOKE_TSR] = 0; env->spr[SPR_BOOKE_TCR] = 0; + kvmppc_sync_set_timer_regs(cpu); booke_update_irq(cpu); } @@ -252,6 +285,7 @@ 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 +303,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 4846acf..1c65478 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 " @@ -816,6 +819,61 @@ static int kvmppc_handle_dcr_write(CPUPPCState *env, uint32_t dcrn, uint32_t dat return 0; } +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) +{ + CPUState *cs = CPU(cpu); + CPUPPCState *env = &cpu->env; + struct kvm_sregs sregs; + int ret; + + 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; + + ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); + + return ret; +} + +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; +} + int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) { PowerPCCPU *cpu = POWERPC_CPU(cs); @@ -845,6 +903,12 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) ret = 0; break; #endif + 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; diff --git a/target-ppc/kvm_ppc.h b/target-ppc/kvm_ppc.h index 4b21723..8ae8fd8 100644 --- a/target-ppc/kvm_ppc.h +++ b/target-ppc/kvm_ppc.h @@ -26,6 +26,9 @@ int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len); int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level); void kvmppc_set_papr(PowerPCCPU *cpu); int kvmppc_smt_threads(void); +void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu); +int kvmppc_sync_set_timer_regs(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); @@ -86,6 +89,20 @@ static inline int kvmppc_smt_threads(void) return 1; } +static inline void kvmppc_sync_get_timer_regs(PowerPCCPU *cpu) +{ +} + +static inline int kvmppc_sync_set_timer_regs(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) {