From patchwork Wed Dec 2 22:19:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Yang X-Patchwork-Id: 11947283 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, HTML_MESSAGE,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AB1CC6369E for ; Wed, 2 Dec 2020 22:21:13 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4461C21D7A for ; Wed, 2 Dec 2020 22:21:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4461C21D7A Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:59876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kkaUo-0007yr-Ks for qemu-devel@archiver.kernel.org; Wed, 02 Dec 2020 17:21:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49308) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kkaTV-0007Ox-W7 for qemu-devel@nongnu.org; Wed, 02 Dec 2020 17:19:50 -0500 Received: from mail-yb1-xb2a.google.com ([2607:f8b0:4864:20::b2a]:46955) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kkaTT-000599-BN for qemu-devel@nongnu.org; Wed, 02 Dec 2020 17:19:49 -0500 Received: by mail-yb1-xb2a.google.com with SMTP id s8so115716yba.13 for ; Wed, 02 Dec 2020 14:19:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:from:date:message-id:subject:to:cc; bh=Z2oMFh0UtFsC+WX1cHN7TFNpNKzs/ZPOQBocx/DRAls=; b=Dr9FwZSSjNO/PsUB2bqkJTcem83yRpBtx+KF9X6VxJcaVIRrHZoQsNmLuPdNiDPus/ xUtTbJMpDayFt8vyoVVot9Y6lkCG9c5XJiF1W8tL9L71QfkzR+H0dUFBpBUwv+XdHruH rKqeR9cmJbxvVCQOyuNEC0RFAEcnRqQpoWtJONbWQfIbfWTRrX21L8YTm/fpz9qCB30H DCF2pZkWPIlNpKKs8d0I62OsSSeP+dKOTHzUGEk5YpwONWBeEeC9RZK0aN4PDlKzGa/A T+zGj8WJBCOJQR5kob12KFQ28mta3W/yq44tVRaiWKP8kWB+cSbz+WCBZfH1Ur1vlybs eJLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=Z2oMFh0UtFsC+WX1cHN7TFNpNKzs/ZPOQBocx/DRAls=; b=WHv5HFgJbrRd40QPyI0btMjOEwRzD505e7/TsvkWqUMVg7SpEd3km0sshlSWDwpPA/ JT7sMjiFmHYV44sKQ/0MBsxG/g9OEO3kTiqAKPA0wxmtEAz29YtZAxEuOmUq3iRWLGL1 FQJ02ziZfj7nepG1x7fUVHRDULBGcM7L6M0eSGQ2yUaHonxXkxh/WXe5A8/b5DfNINVS rzagOBk3Fs+zeH4hnZoqMWwao6lW+O0V5RgUK+anfLE8if83coHkMUI1RawlxJEZfADT JMODrPvkBvCEUjehH34RCK39ZzcfI5Nea9qfXNU6udRHdhmhsS62bsjILNBPCvGrh7PN 3JEQ== X-Gm-Message-State: AOAM532/+3FXyoCgIfcMoT9VkTBT86HD1FdRdB3EXColx4asph6LqBEr /yRNbBkxfv3rEUpt/WPwWRjdijZy9ZthduryVzXQwA== X-Google-Smtp-Source: ABdhPJwFVp3DYTmIeJu9n1hL/s/l6TkAdAYv4ytNsjEUMZ361mlWOBwYh53YJtW+f4wCTgLtyUoARMzz9kShjYa+D9I= X-Received: by 2002:a25:abce:: with SMTP id v72mr459970ybi.138.1606947583047; Wed, 02 Dec 2020 14:19:43 -0800 (PST) MIME-Version: 1.0 From: Frank Yang Date: Wed, 2 Dec 2020 14:19:31 -0800 Message-ID: Subject: [PATCH v1 1/1] hvf: arm: Properly sync guest time on migration To: Alexander Graf Cc: Peter Collingbourne , Frank Yang , Roman Bolshakov , Peter Maydell , Eduardo Habkost , Richard Henderson , qemu-devel , Cameron Esfahani , qemu-arm , Claudio Fontana , Paolo Bonzini Received-SPF: pass client-ip=2607:f8b0:4864:20::b2a; envelope-from=lfy@google.com; helo=mail-yb1-xb2a.google.com X-Spam_score_int: -175 X-Spam_score: -17.6 X-Spam_bar: ----------------- X-Spam_report: (-17.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From downstream: https://android-review.googlesource.com/c/platform/external/qemu/+/1515002 Based on v3 of Alexander Graf's patches https://patchew.org/QEMU/20201202190408.2041-1-agraf@csgraf.de We need to adjust CNTVOFF_EL2 so that time doesnt warp. Even though we can set separate CNTVOFF_EL2 values per vCPU, it just is not worth the require effort to do that accurately---with individual values, even if they are a tiny bit off it can result in a lockup due to inconsistent time differences between vCPUs. So just use a global approximate value for now. Not tested in upstream yet, but Android emulator snapshots work without time warp now. Signed-off-by: Lingfeng Yang --- accel/hvf/hvf-cpus.c | 3 +++ include/sysemu/hvf_int.h | 4 ++++ target/arm/hvf/hvf.c | 43 +++++++++++++++++++++++++++++++++++++++- target/i386/hvf/hvf.c | 4 ++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/accel/hvf/hvf-cpus.c b/accel/hvf/hvf-cpus.c index a981ccde70..484c7717f5 100644 --- a/accel/hvf/hvf-cpus.c +++ b/accel/hvf/hvf-cpus.c @@ -456,6 +456,9 @@ static int hvf_accel_init(MachineState *ms) hvf_state = s; memory_listener_register(&hvf_memory_listener, &address_space_memory); cpus_register_accel(&hvf_cpus); + + hvf_arch_init(s); + return 0; } diff --git a/include/sysemu/hvf_int.h b/include/sysemu/hvf_int.h index 13adf6ea77..08830782c9 100644 --- a/include/sysemu/hvf_int.h +++ b/include/sysemu/hvf_int.h @@ -55,6 +55,9 @@ struct HVFState { hvf_slot slots[32]; int num_slots; +#if defined(__aarch64__) + uint64_t ticks; +#endif hvf_vcpu_caps *hvf_caps; }; extern HVFState *hvf_state; @@ -73,5 +76,6 @@ void hvf_arch_vcpu_destroy(CPUState *cpu); int hvf_vcpu_exec(CPUState *cpu); hvf_slot *hvf_find_overlap_slot(uint64_t, uint64_t); void hvf_kick_vcpu_thread(CPUState *cpu); +void hvf_arch_init(HVFState* s); #endif diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c index 9442e2f232..37380c6c53 100644 --- a/target/arm/hvf/hvf.c +++ b/target/arm/hvf/hvf.c @@ -312,6 +312,10 @@ int hvf_put_registers(CPUState *cpu) uint64_t val; int i; + /* Sync up CNTVOFF_EL2 */ + env->cp15.cntvoff_el2 = hvf_state->ticks; + hv_vcpu_set_vtimer_offset(cpu->hvf->fd, env->cp15.cntvoff_el2); + for (i = 0; i < ARRAY_SIZE(hvf_reg_match); i++) { val = *(uint64_t *)((void *)env + hvf_reg_match[i].offset); ret = hv_vcpu_set_reg(cpu->hvf->fd, hvf_reg_match[i].reg, val); @@ -418,6 +422,8 @@ void hvf_arch_vcpu_destroy(CPUState *cpu) { } +static HVFState* hvf_state = 0; + int hvf_arch_init_vcpu(CPUState *cpu) { ARMCPU *arm_cpu = ARM_CPU(cpu); @@ -795,7 +801,11 @@ int hvf_vcpu_exec(CPUState *cpu) &cval); assert_hvf_ok(r); - int64_t ticks_to_sleep = cval - mach_absolute_time(); + /* mach_absolute_time() is an absolute host tick number. We + * have set up the guest to use the host tick number offset + * by env->cp15.cntvoff_el2. + */ + int64_t ticks_to_sleep = cval - (mach_absolute_time() - env->cp15.cntvoff_el2); if (ticks_to_sleep < 0) { break; } @@ -855,3 +865,34 @@ int hvf_vcpu_exec(CPUState *cpu) } } } + +static int hvf_mig_state_pre_save(void* opaque) { + struct HVFState* s = opaque; + s->ticks -= mach_absolute_time(); + return 0; +} + +static int hvf_mig_state_post_load(void* opaque) { + struct HVFState* s = opaque; + m->ticks += mach_absolute_time(); + return 0; +} + + +const VMStateDescription vmstate_hvf_migration = { + .name = "hvf-migration", + .version_id = 1, + .minimum_version_id = 1, + .pre_save = hvf_mig_state_pre_save, + .post_load = hvf_mig_state_post_load, + .fields = (VMStateField[]) { + VMSTATE_UINT64(ticks_to_save, HVFState), + VMSTATE_END_OF_LIST() + }, +}; + +void hvf_arch_init(HVFState* s) { + hvf_state = s; + hvf_state->ticks = 0; + vmstate_register(NULL, 0, &vmstate_hvf_migration, hvf_state); +} diff --git a/target/i386/hvf/hvf.c b/target/i386/hvf/hvf.c index 08b4adecd9..7ca6387620 100644 --- a/target/i386/hvf/hvf.c +++ b/target/i386/hvf/hvf.c @@ -557,3 +557,7 @@ int hvf_vcpu_exec(CPUState *cpu) return ret; } + +void hvf_arch_init(HVFState* s) { + (void)s; +}