From patchwork Fri Sep 9 04:46:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 12971017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 55113ECAAA1 for ; Fri, 9 Sep 2022 04:48:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=CGqmlS4k0n9MCCEOJGM9Ud5KVvzHU9nidabYSnoQr5g=; b=FdToI8/kqEpnVVsr3b7T1RSIvH itVbOGc8xyRwgDyYtrbo9lH1VAL/VnRHtitOuWxetAlXzVOvt7tnOAEutLgJZp3GGrMej3MsXfw65 XDk0sv/L443mD7yHVwPDcs5kaTz3qjYqy15jj5a3BJwleuGutGusmpyZUime00xxSP+OumIBo33vf EAc3Gxh0J9HHTHgwcLYpGepml6/PJcTM1kusHUv+tR5CrqjaC80OMV3Y1eeJavWY6R6g4rhHLKQql omBr/yxhLm0pS5+IE4cHLL5AI4+J3p/kK6zTcVac8pF+uV++crtSO6N1w1h1520paOIhlEHtcI8Oo IZhGDNrw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oWVvF-00CO6h-5x; Fri, 09 Sep 2022 04:47:21 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oWVv6-00CO2z-MQ for linux-arm-kernel@lists.infradead.org; Fri, 09 Sep 2022 04:47:14 +0000 Received: by mail-yb1-xb49.google.com with SMTP id n17-20020a254011000000b006a90a92cf87so720285yba.19 for ; Thu, 08 Sep 2022 21:47:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=MbeYcUZH5rZnv7Srky0s60QQaNvSpZyXF49LSUjvDEA=; b=HystTyEzO0y7slgnAoyhgmwyE6//Ptny72kWSKwWDwnF/pewU2YZGmTe4aXKH4BsD9 tOZCF/pJNqzQMbGFrADdmXjXCQP9EvzOUU4QmcRDcYj0PKa/uqArjk2L8VC51x0vCNoO PgY+1uxqASwNnCD/990sGSZL6aehIUh45mhAzNGV95InvmredIbbFxcU/UhevR3DlKdd jBZQdIU98NOmjPHQ3GG2KSDMtSP0PZmznTJKoIdfgvWTm491PoCsSbPyh2fU+AwvhvQZ M6vOVpJjEVujY6am0l8Vd3XInLJJ/6QY+l3Am8kVuk9XHMmoz0hEwbnH9UrjjsrJX5of 6Rsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=MbeYcUZH5rZnv7Srky0s60QQaNvSpZyXF49LSUjvDEA=; b=MgyAUW8uAxkxP25kNb6lAlBHkHt3P0aeUdAJ6+NQL+xkortJQtYpcPiUtR2govZZZp qpgqqy8eKMqb3GE4DiV24QOLh5gVmsg4AaaAuzsg1wnvPQ4yDtCXyHfIyai2qDilk8nZ QlNYjdhMy/I0x4dsGAVhzmF2tqwb991MykLUMNEY7K7X3lqEwzeWV43XosoaH1C9m6UR SaA4aMfPKjzKTvw8IEHDBVgaTiVmmGBSBxjiBKDULS4TShvcTV75lxR8KZWVWDkK46UI P+3opL11/bcJKEmVZnbZFKClP5OHqqy27+e0W0npydefaxpq1tTGBr8DmOk2E3Yg9i3f LkWQ== X-Gm-Message-State: ACgBeo23irFTZQGggEH4IzWgwT4hmNC1I3YVKbGkJmDMi7ejIcWUqJ1y vMq+7OaJREx/tmpezkWYRWvM7p6kN2g= X-Google-Smtp-Source: AA6agR5IWYF7xulqdDWKw4Vild+Gpo9RYX8tS1XQklBJGnd+q0eLLVs/7l2nNFWoVG/E37pxU2HqzDpQBJc= X-Received: from reijiw-west4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:aa1]) (user=reijiw job=sendgmr) by 2002:a25:d491:0:b0:6a8:6d1d:465b with SMTP id m139-20020a25d491000000b006a86d1d465bmr9986451ybf.64.1662698831044; Thu, 08 Sep 2022 21:47:11 -0700 (PDT) Date: Thu, 8 Sep 2022 21:46:36 -0700 In-Reply-To: <20220909044636.1997755-1-reijiw@google.com> Mime-Version: 1.0 References: <20220909044636.1997755-1-reijiw@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220909044636.1997755-4-reijiw@google.com> Subject: [PATCH 3/3] KVM: arm64: selftests: Add a test case for KVM_GUESTDBG_SINGLESTEP From: Reiji Watanabe To: Marc Zyngier , kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, James Morse , Alexandru Elisei , Suzuki K Poulose , Paolo Bonzini , Ricardo Koller , Oliver Upton , Jing Zhang , Raghavendra Rao Anata , Reiji Watanabe X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220908_214712_764385_37592C3B X-CRM114-Status: GOOD ( 20.43 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a test case for KVM_GUESTDBG_SINGLESTEP to the debug-exceptions test. The test enables single-step execution from userspace, and check if the exit to userspace occurs for each instruction that is stepped. Set the default number of the test iterations to a number of iterations sufficient to always reproduce the problem that the previous patch fixes on an Ampere Altra machine. Signed-off-by: Reiji Watanabe --- .../selftests/kvm/aarch64/debug-exceptions.c | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/tools/testing/selftests/kvm/aarch64/debug-exceptions.c b/tools/testing/selftests/kvm/aarch64/debug-exceptions.c index e6e83b895fd5..947bd201435c 100644 --- a/tools/testing/selftests/kvm/aarch64/debug-exceptions.c +++ b/tools/testing/selftests/kvm/aarch64/debug-exceptions.c @@ -22,6 +22,7 @@ #define SPSR_SS (1 << 21) extern unsigned char sw_bp, sw_bp2, hw_bp, hw_bp2, bp_svc, bp_brk, hw_wp, ss_start; +extern unsigned char iter_ss_begin, iter_ss_end; static volatile uint64_t sw_bp_addr, hw_bp_addr; static volatile uint64_t wp_addr, wp_data_addr; static volatile uint64_t svc_addr; @@ -238,6 +239,46 @@ static void guest_svc_handler(struct ex_regs *regs) svc_addr = regs->pc; } +enum single_step_op { + SINGLE_STEP_ENABLE = 0, + SINGLE_STEP_DISABLE = 1, +}; + +static void guest_code_ss(int test_cnt) +{ + uint64_t i; + uint64_t bvr, wvr, w_bvr, w_wvr; + + for (i = 0; i < test_cnt; i++) { + /* Bits [1:0] of dbg{b,w}vr are RES0 */ + w_bvr = i << 2; + w_wvr = i << 2; + + /* Enable Single Step execution */ + GUEST_SYNC(SINGLE_STEP_ENABLE); + + /* + * The userspace will veriry that the pc is as expected during + * single step execution between iter_ss_begin and iter_ss_end. + */ + asm volatile("iter_ss_begin:nop\n"); + + write_sysreg(w_bvr, dbgbvr0_el1); + write_sysreg(w_wvr, dbgwvr0_el1); + bvr = read_sysreg(dbgbvr0_el1); + wvr = read_sysreg(dbgwvr0_el1); + + asm volatile("iter_ss_end:\n"); + + /* Disable Single Step execution */ + GUEST_SYNC(SINGLE_STEP_DISABLE); + + GUEST_ASSERT(bvr == w_bvr); + GUEST_ASSERT(wvr == w_wvr); + } + GUEST_DONE(); +} + static int debug_version(struct kvm_vcpu *vcpu) { uint64_t id_aa64dfr0; @@ -293,16 +334,106 @@ static void test_guest_debug_exceptions(void) kvm_vm_free(vm); } +void test_single_step_from_userspace(int test_cnt) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + struct kvm_run *run; + uint64_t pc, cmd; + uint64_t test_pc = 0; + bool ss_enable = false; + struct kvm_guest_debug debug = {}; + + vm = vm_create_with_one_vcpu(&vcpu, guest_code_ss); + ucall_init(vm, NULL); + run = vcpu->run; + vcpu_args_set(vcpu, 1, test_cnt); + + while (1) { + vcpu_run(vcpu); + if (run->exit_reason != KVM_EXIT_DEBUG) { + cmd = get_ucall(vcpu, &uc); + if (cmd == UCALL_ABORT) { + REPORT_GUEST_ASSERT(uc); + /* NOT REACHED */ + } else if (cmd == UCALL_DONE) { + break; + } + + TEST_ASSERT(cmd == UCALL_SYNC, + "Unexpected ucall cmd 0x%lx", cmd); + + if (uc.args[1] == SINGLE_STEP_ENABLE) { + debug.control = KVM_GUESTDBG_ENABLE | + KVM_GUESTDBG_SINGLESTEP; + ss_enable = true; + } else { + debug.control = SINGLE_STEP_DISABLE; + ss_enable = false; + } + + vcpu_guest_debug_set(vcpu, &debug); + continue; + } + + TEST_ASSERT(ss_enable, "Unexpected KVM_EXIT_DEBUG"); + + /* Check if the current pc is expected. */ + vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &pc); + TEST_ASSERT(!test_pc || pc == test_pc, + "Unexpected pc 0x%lx (expected 0x%lx)", + pc, test_pc); + + /* + * If the current pc is between iter_ss_bgin and + * iter_ss_end, the pc for the next KVM_EXIT_DEBUG should + * be the current pc + 4. + */ + if ((pc >= (uint64_t)&iter_ss_begin) && + (pc < (uint64_t)&iter_ss_end)) + test_pc = pc + 4; + else + test_pc = 0; + } + + kvm_vm_free(vm); +} + +static void help(char *name) +{ + puts(""); + printf("Usage: %s [-h] [-i iterations of the single step test]\n", name); + puts(""); + exit(0); +} + int main(int argc, char *argv[]) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; + int opt; + int ss_iteration = 10000; vm = vm_create_with_one_vcpu(&vcpu, guest_code); __TEST_REQUIRE(debug_version(vcpu) >= 6, "Armv8 debug architecture not supported."); kvm_vm_free(vm); + + while ((opt = getopt(argc, argv, "i:")) != -1) { + switch (opt) { + case 'i': + ss_iteration = atoi(optarg); + break; + case 'h': + default: + help(argv[0]); + break; + } + } + test_guest_debug_exceptions(); + test_single_step_from_userspace(ss_iteration); return 0; }