From patchwork Thu Apr 7 15:23:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Cave-Ayland X-Patchwork-Id: 8774051 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7B3EBC0553 for ; Thu, 7 Apr 2016 15:26:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D209820204 for ; Thu, 7 Apr 2016 15:26:23 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1F775201BB for ; Thu, 7 Apr 2016 15:26:23 +0000 (UTC) Received: from localhost ([::1]:50582 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoBp8-0006lZ-Gz for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Apr 2016 11:26:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46261) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoBn2-0003Oj-2U for qemu-devel@nongnu.org; Thu, 07 Apr 2016 11:24:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoBn1-0001yR-7I for qemu-devel@nongnu.org; Thu, 07 Apr 2016 11:24:12 -0400 Received: from chuckie.co.uk ([82.165.15.123]:32818 helo=s16892447.onlinehome-server.info) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoBmy-0001vY-Dk; Thu, 07 Apr 2016 11:24:08 -0400 Received: from host81-154-31-210.range81-154.btcentralplus.com ([81.154.31.210] helo=kentang.home) by s16892447.onlinehome-server.info with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.76) (envelope-from ) id 1aoBmn-0003tW-0L; Thu, 07 Apr 2016 16:23:57 +0100 From: Mark Cave-Ayland To: david@gibson.dropbear.id.au, aik@ozlabs.ru, agraf@suse.de, qemu-ppc@nongnu.org, qemu-devel@nongnu.org Date: Thu, 7 Apr 2016 16:23:13 +0100 Message-Id: <1460042594-8056-4-git-send-email-mark.cave-ayland@ilande.co.uk> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1460042594-8056-1-git-send-email-mark.cave-ayland@ilande.co.uk> References: <1460042594-8056-1-git-send-email-mark.cave-ayland@ilande.co.uk> X-SA-Exim-Connect-IP: 81.154.31.210 X-SA-Exim-Mail-From: mark.cave-ayland@ilande.co.uk X-SA-Exim-Version: 4.2.1 (built Sun, 08 Jan 2012 02:45:44 +0000) X-SA-Exim-Scanned: Yes (on s16892447.onlinehome-server.info) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 82.165.15.123 Subject: [Qemu-devel] [RFC 3/4] target-ppc: synchronise tb_offset with KVM host on machine start 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Recalculate the tb_offset between the guest and host, applying it to all CPUs when (re)starting the virtual machine. This has the effect of providing a near-seamless virtual timebase for KVM guests that support KVM_REG_PPC_TB_OFFSET. Signed-off-by: Mark Cave-Ayland --- hw/ppc/ppc.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/hw/ppc/ppc.c b/hw/ppc/ppc.c index ccdca5d..39e15b1 100644 --- a/hw/ppc/ppc.c +++ b/hw/ppc/ppc.c @@ -1345,12 +1345,51 @@ PowerPCCPU *ppc_get_vcpu_by_dt_id(int cpu_dt_id) } /* Generic PPC machine */ +static void _ppc_update_timebase(PPCMachineState *pms) +{ + /* Update guest timebase offset with respect to host */ + int64_t tb_off, new_tb_off; + int i; + + PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu); + tb_off = first_ppc_cpu->env.tb_env->tb_offset; + + new_tb_off = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), + first_ppc_cpu->env.tb_env->tb_freq, NANOSECONDS_PER_SECOND) + + tb_off - cpu_get_host_ticks(); + + //fprintf(stderr, "tb_off: %" PRIx64 " new_tb_off: %" PRIx64 "\n", tb_off, new_tb_off); + + /* Set new offset to all CPUs */ + for (i = 0; i < smp_cpus; i++) { + PowerPCCPU *pcpu = POWERPC_CPU(qemu_get_cpu(i)); + pcpu->env.tb_env->tb_offset = new_tb_off; + } +} + +static void ppc_machine_change_state(void *opaque, int running, RunState state) +{ + PPCMachineState *s = opaque; + + if (running && kvm_enabled()) { + _ppc_update_timebase(s); + } +} + +static void ppc_machine_class_init(ObjectClass *oc, void *data) +{ + PPCMachineState *s = g_malloc0(sizeof(PPCMachineState)); + + qemu_add_vm_change_state_handler(ppc_machine_change_state, s); +} + static const TypeInfo ppc_machine_info = { .name = TYPE_PPC_MACHINE, .parent = TYPE_MACHINE, .abstract = true, .instance_size = sizeof(PPCMachineState), - .class_size = sizeof(PPCMachineClass) + .class_size = sizeof(PPCMachineClass), + .class_init = ppc_machine_class_init }; static void ppc_machine_register_types(void)