From patchwork Tue Aug 23 23:47:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Koller X-Patchwork-Id: 12952736 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 522FAC32793 for ; Tue, 23 Aug 2022 23:48:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232438AbiHWXsU (ORCPT ); Tue, 23 Aug 2022 19:48:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232367AbiHWXsG (ORCPT ); Tue, 23 Aug 2022 19:48:06 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FFCA8B2E5 for ; Tue, 23 Aug 2022 16:47:58 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-33931a5c133so178103917b3.17 for ; Tue, 23 Aug 2022 16:47:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=Dfi0mJsD0tdhcRAmkrVPkz6XbnwyxoP42hlriK3Z1Vw=; b=ApQAvyESF0BAyecbnSd0S+VU17iuM4uxqa1SrkvSM6ppDeFQdidOWrj2OKJTRo6k+6 GI0vTvueTAyxD1Jyt9SEhdk91UO9Xy14ElrflMXcSp+lAVvq3P9AIrkVkhB+eXAhXaUU yj3rIbP56klSfl+fwjySQhwdoI7HTXea+PRSxUeQxQEl8Wh5Ab/RuCmwOp+fTIXhSeWS 8ZAoyC+gtLfv7ZNAeKkpBN4bV1r530QYRC6v3tbsNdYsnvPeHPTR5lzhnbTmYwd7h08m +DasfEXOwM5F/j8jKGvrhBm+/gqi1ShHDA9ITltOSMHRRZamDaESta0m/HmPxfRN6FC8 YwEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=Dfi0mJsD0tdhcRAmkrVPkz6XbnwyxoP42hlriK3Z1Vw=; b=Np8sok2CIuwbbhjQ2+Qxpx3YIdsASbzb1d5BsvmfzfhWWFPFdQpFwFBWiFNCBhKOfW cWmigVW426qZ22deFDls8Enld3DGxN19iyHITpOCwguZwbY7w2OUIP1HbNdbELx71/5r JnsBZtMY9RsaVbN/5Gym3mjtu/+LHmlaoYALw5SSe7iSy17kTZKlnm+YEv9CwQ1HuGNR D/rwxLnFtAfuy0RjSzmkA8FQi6FEdVaGxv+0m/JpiYECee7Hvnt9bYFA0FR8fx/nLxjd 6xWUV9iKIGaZZTkld5RN1qbbiCdubIvXs3kWHqBq0xpLGyTOlhIpzsKVOdbIWH5WUR5x d+Ag== X-Gm-Message-State: ACgBeo2es9TeU9F1lnj8Ka7126LW7zrNECVUpyG9Q7g2gdZiBM3HnGkE +KUADnJG69ETfqJlCSIT6REPcofYOedZZzuWjBsjpfaeOZnAlqd1jr+9s9N+ty4UbwxkOLgZiE3 3MomGx/BBHYLqX+1wkam7WJMFzEam+FP/2+tDihkBS6/mWhjzOwHeunDz22I2SGw= X-Google-Smtp-Source: AA6agR5uEYim3KsWjLKZ+ek2MN0aQGlLEN0U1pYdwEm8dGiq81LDIZPYhcDw4yTnZoZJp3VLG1wr87V0hSlxfQ== X-Received: from ricarkol4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1248]) (user=ricarkol job=sendgmr) by 2002:a81:1608:0:b0:334:7c07:e72d with SMTP id 8-20020a811608000000b003347c07e72dmr28178084yww.265.1661298477063; Tue, 23 Aug 2022 16:47:57 -0700 (PDT) Date: Tue, 23 Aug 2022 23:47:26 +0000 In-Reply-To: <20220823234727.621535-1-ricarkol@google.com> Message-Id: <20220823234727.621535-13-ricarkol@google.com> Mime-Version: 1.0 References: <20220823234727.621535-1-ricarkol@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH v5 12/13] KVM: selftests: aarch64: Add readonly memslot tests into page_fault_test From: Ricardo Koller To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, andrew.jones@linux.dev Cc: pbonzini@redhat.com, maz@kernel.org, seanjc@google.com, alexandru.elisei@arm.com, eric.auger@redhat.com, oupton@google.com, reijiw@google.com, rananta@google.com, bgardon@google.com, dmatclack@google.com, axelrasmussen@google.com, Ricardo Koller Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add some readonly memslot tests into page_fault_test. Mark the data and/or page-table memslots as readonly, perform some accesses, and check that the right fault is triggered when expected (e.g., a store with no write-back should lead to an mmio exit). Signed-off-by: Ricardo Koller --- .../selftests/kvm/aarch64/page_fault_test.c | 98 ++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/aarch64/page_fault_test.c b/tools/testing/selftests/kvm/aarch64/page_fault_test.c index 50117a070be0..c7a50d79dcda 100644 --- a/tools/testing/selftests/kvm/aarch64/page_fault_test.c +++ b/tools/testing/selftests/kvm/aarch64/page_fault_test.c @@ -41,6 +41,8 @@ static uint64_t *guest_test_memory = (uint64_t *)TEST_GVA; #define CHECK_FN_NR 10 static struct event_cnt { + int mmio_exits; + int fail_vcpu_runs; int uffd_faults; /* uffd_faults is incremented from multiple threads. */ pthread_mutex_t uffd_faults_mutex; @@ -57,6 +59,8 @@ struct test_desc { uffd_handler_t uffd_data_handler; void (*dabt_handler)(struct ex_regs *regs); void (*iabt_handler)(struct ex_regs *regs); + void (*mmio_handler)(struct kvm_vm *vm, struct kvm_run *run); + void (*fail_vcpu_run_handler)(int ret); uint32_t pt_memslot_flags; uint32_t data_memslot_flags; bool skip; @@ -418,6 +422,28 @@ static bool punch_hole_in_memslot(struct kvm_vm *vm, return true; } +static void mmio_on_test_gpa_handler(struct kvm_vm *vm, struct kvm_run *run) +{ + struct userspace_mem_region *region = vm_get_data_region(vm); + void *hva = (void *)region->region.userspace_addr; + + ASSERT_EQ(run->mmio.phys_addr, region->region.guest_phys_addr); + + memcpy(hva, run->mmio.data, run->mmio.len); + events.mmio_exits += 1; +} + +static void mmio_no_handler(struct kvm_vm *vm, struct kvm_run *run) +{ + uint64_t data; + + memcpy(&data, run->mmio.data, sizeof(data)); + pr_debug("addr=%lld len=%d w=%d data=%lx\n", + run->mmio.phys_addr, run->mmio.len, + run->mmio.is_write, data); + TEST_FAIL("There was no MMIO exit expected."); +} + static bool check_write_in_dirty_log(struct kvm_vm *vm, struct userspace_mem_region *region, uint64_t host_pg_nr) { @@ -465,6 +491,18 @@ static bool handle_cmd(struct kvm_vm *vm, int cmd) return continue_test; } +void fail_vcpu_run_no_handler(int ret) +{ + TEST_FAIL("Unexpected vcpu run failure\n"); +} + +void fail_vcpu_run_mmio_no_syndrome_handler(int ret) +{ + TEST_ASSERT(errno == ENOSYS, + "The mmio handler should have returned not implemented."); + events.fail_vcpu_runs += 1; +} + extern unsigned char __exec_test; void noinline __return_0x77(void) @@ -588,9 +626,20 @@ static struct kvm_vm_mem_params setup_memslots(enum vm_guest_mode mode, return mem_params; } +static void setup_default_handlers(struct test_desc *test) +{ + if (!test->mmio_handler) + test->mmio_handler = mmio_no_handler; + + if (!test->fail_vcpu_run_handler) + test->fail_vcpu_run_handler = fail_vcpu_run_no_handler; +} + static void check_event_counts(struct test_desc *test) { ASSERT_EQ(test->expected_events.uffd_faults, events.uffd_faults); + ASSERT_EQ(test->expected_events.mmio_exits, events.mmio_exits); + ASSERT_EQ(test->expected_events.fail_vcpu_runs, events.fail_vcpu_runs); } static void print_test_banner(enum vm_guest_mode mode, struct test_params *p) @@ -615,10 +664,18 @@ static void reset_event_counts(void) static void vcpu_run_loop(struct kvm_vm *vm, struct kvm_vcpu *vcpu, struct test_desc *test) { + struct kvm_run *run; struct ucall uc; + int ret; + + run = vcpu->run; for (;;) { - vcpu_run(vcpu); + ret = _vcpu_run(vcpu); + if (ret) { + test->fail_vcpu_run_handler(ret); + goto done; + } switch (get_ucall(vcpu, &uc)) { case UCALL_SYNC: @@ -632,6 +689,10 @@ static void vcpu_run_loop(struct kvm_vm *vm, struct kvm_vcpu *vcpu, break; case UCALL_DONE: goto done; + case UCALL_NONE: + if (run->exit_reason == KVM_EXIT_MMIO) + test->mmio_handler(vm, run); + break; default: TEST_FAIL("Unknown ucall %lu", uc.cmd); } @@ -673,6 +734,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) load_exec_code_for_test(vm); setup_uffd(vm, p, &pt_uffd, &data_uffd); setup_abort_handlers(vm, vcpu, test); + setup_default_handlers(test); vcpu_args_set(vcpu, 1, test); vcpu_run_loop(vm, vcpu, test); @@ -760,6 +822,25 @@ static void help(char *name) .expected_events = { 0 }, \ } +#define TEST_RO_MEMSLOT(_access, _mmio_handler, _mmio_exits) \ +{ \ + .name = SCAT3(ro_memslot, _access, _with_af), \ + .data_memslot_flags = KVM_MEM_READONLY, \ + .guest_prepare = { _PREPARE(_access) }, \ + .guest_test = _access, \ + .mmio_handler = _mmio_handler, \ + .expected_events = { .mmio_exits = _mmio_exits }, \ +} + +#define TEST_RO_MEMSLOT_NO_SYNDROME(_access) \ +{ \ + .name = SCAT2(ro_memslot_no_syndrome, _access), \ + .data_memslot_flags = KVM_MEM_READONLY, \ + .guest_test = _access, \ + .fail_vcpu_run_handler = fail_vcpu_run_mmio_no_syndrome_handler, \ + .expected_events = { .fail_vcpu_runs = 1 }, \ +} + static struct test_desc tests[] = { /* Check that HW is setting the Access Flag (AF) (sanity checks). */ @@ -828,6 +909,21 @@ static struct test_desc tests[] = { TEST_DIRTY_LOG(guest_dc_zva, with_af, guest_check_write_in_dirty_log), TEST_DIRTY_LOG(guest_st_preidx, with_af, guest_check_write_in_dirty_log), + /* + * Try accesses when the data memslot is marked read-only (with + * KVM_MEM_READONLY). Writes with a syndrome result in an MMIO exit, + * writes with no syndrome (e.g., CAS) result in a failed vcpu run, and + * reads/execs with and without syndroms do not fault. + */ + TEST_RO_MEMSLOT(guest_read64, 0, 0), + TEST_RO_MEMSLOT(guest_ld_preidx, 0, 0), + TEST_RO_MEMSLOT(guest_at, 0, 0), + TEST_RO_MEMSLOT(guest_exec, 0, 0), + TEST_RO_MEMSLOT(guest_write64, mmio_on_test_gpa_handler, 1), + TEST_RO_MEMSLOT_NO_SYNDROME(guest_dc_zva), + TEST_RO_MEMSLOT_NO_SYNDROME(guest_cas), + TEST_RO_MEMSLOT_NO_SYNDROME(guest_st_preidx), + { 0 } };