From patchwork Thu Sep 9 01:38:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Raghavendra Rao Ananta X-Patchwork-Id: 12482237 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=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 6BCD5C433EF for ; Thu, 9 Sep 2021 01:44:55 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 365EC61166 for ; Thu, 9 Sep 2021 01:44:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 365EC61166 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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:References: Mime-Version:Message-Id: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=ljzQLGTRqus8KNWdtWH4cbiEUUeRNMVADThQQQ/uogY=; b=DSkpJSR/hDcYsHaMlrMxoG92D8 UFEcVxl3bs4TDq79RIbZgsvB5HHZ0mpK27dulljwwKMH3Bgbqwq86KVabXrKPPUZfYvdEI6Q5PnTa hRU93DhJuSHQFbMAuRmmV9JXdAG3YXquY0w7VmbuIjRMvx0p9jIFFDJoPKMCUrwyXT8d/H2YFFOD/ C4xyZYvfmtCoBIQVOotXi3iLPzaF4g1F7DQp3G+0yvi3rcIbIMdtcXDs/Q9oOhDDeq+XnZMrKdP54 LR0OIwm5vuFbQcr+aZQLII0ur5PXbmZgSoQFEzbKjtjL+HvkXjYGj3IPRMcGLa5/FcFQicPmiDJNz MXgRghDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mO95M-007xBp-6m; Thu, 09 Sep 2021 01:42:40 +0000 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mO91e-007vQ5-Vu for linux-arm-kernel@lists.infradead.org; Thu, 09 Sep 2021 01:38:52 +0000 Received: by mail-pl1-x649.google.com with SMTP id x5-20020a1709028ec500b0013a347b89e4so36280plo.3 for ; Wed, 08 Sep 2021 18:38:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=CjBV1NcFy7p27eV1/jQ5yVLZ7UVN92HtWY4NlfI6+Nk=; b=qs44jTJxzDkHSQpQ3NQhsxvConzusDrdn8GVAQZkcQJ5b+Gf5cFcq+1LrnKjJ3qm4U IuVGUIv+Jm5UBRAL0UWp2YKCJiJWATPdxBLM7NOxgHM87/Ubckxo1gtt4k9jA/7RGXBM Qou8fUA1VNeeZou9MCS5yT/PR9gx7Il1wk+bXjTtgmUyNXl/4E7N57Q+12qLz3f3ZE2y ecBKEYRHqKGHv80Qqr+cRsYSsEpyK8YERJW3DxnoO0EtISch3Bvh9NCq5sZSd3XWfv4Y ABcqFuy0WPFgkDqTcVpYf5C4WGuZvOQ+4Vej5j0NsDyBBV5zXMiKM0YkCvBFObLVGX13 FLDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=CjBV1NcFy7p27eV1/jQ5yVLZ7UVN92HtWY4NlfI6+Nk=; b=KCZvjqDqEGrrjEMNcT906wJbOv2bM/LBqonTrERVqOp89LkQKe6wMZqptae6jIRpQ3 Evg9dOPktSbZwrzmmu2hZ9ipIiDdfqx9HIMSs3QhCOsEzD5lAJNAkhTkRBIxo9H5IkaL JLjbyekggEQkqHVOEwSWJbQNyy6UhUiKaiQO6ppGStrt4FUnGv/IHxb3afzhifMOHEj2 A7em4+5bbo6nnKOz8e4Z07e81+RWkSNVHyNaPlIcsx5y5mC3p3o/JNxTSU9+eRp3/92e PkN7WkQzB9wUJ5DBuh3AuDv634+GCwzBsEPv66wwGqqcGAm4Pp+a7vo75TgxJREiJYyV SNFw== X-Gm-Message-State: AOAM532mAOXQpNLt5d/sp1zf+MlfwvcpD0OeiLLjw4HGHrnpirMITCla qMGAg9KaIKH0V8bAm/6bncMT+2d/d6Su X-Google-Smtp-Source: ABdhPJyuqoo++7vtCPsZXEiAOGwdyb60M1j/VQFztVww0kA042US1QoYXW+DsIcfE2quLAQ598C/fZFY3ypv X-Received: from rananta-virt.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1bcc]) (user=rananta job=sendgmr) by 2002:a62:cdcd:0:b0:405:27a5:fbbb with SMTP id o196-20020a62cdcd000000b0040527a5fbbbmr605460pfg.7.1631151526738; Wed, 08 Sep 2021 18:38:46 -0700 (PDT) Date: Thu, 9 Sep 2021 01:38:09 +0000 In-Reply-To: <20210909013818.1191270-1-rananta@google.com> Message-Id: <20210909013818.1191270-10-rananta@google.com> Mime-Version: 1.0 References: <20210909013818.1191270-1-rananta@google.com> X-Mailer: git-send-email 2.33.0.153.gba50c8fa24-goog Subject: [PATCH v4 09/18] KVM: arm64: selftests: Add guest support to get the vcpuid From: Raghavendra Rao Ananta To: Paolo Bonzini , Marc Zyngier , Andrew Jones , James Morse , Alexandru Elisei , Suzuki K Poulose Cc: Catalin Marinas , Will Deacon , Peter Shier , Ricardo Koller , Oliver Upton , Reiji Watanabe , Jing Zhang , Raghavendra Rao Anata , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kvm@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210908_183851_087253_0071EB06 X-CRM114-Status: GOOD ( 16.85 ) 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 At times, such as when in the interrupt handler, the guest wants to get the vcpuid that it's running on. As a result, introduce get_vcpuid() that returns the vcpuid of the calling vcpu. At its backend, the VMM prepares a map of vcpuid and mpidr during VM initialization and exports the map to the guest for it to read. Signed-off-by: Raghavendra Rao Ananta --- .../selftests/kvm/include/aarch64/processor.h | 3 ++ .../selftests/kvm/lib/aarch64/processor.c | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/tools/testing/selftests/kvm/include/aarch64/processor.h b/tools/testing/selftests/kvm/include/aarch64/processor.h index b6088c3c67a3..150f63101f4c 100644 --- a/tools/testing/selftests/kvm/include/aarch64/processor.h +++ b/tools/testing/selftests/kvm/include/aarch64/processor.h @@ -133,6 +133,7 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, handler_fn handler); void vm_install_sync_handler(struct kvm_vm *vm, int vector, int ec, handler_fn handler); +void vm_vcpuid_map_init(struct kvm_vm *vm); static inline void cpu_relax(void) { @@ -194,4 +195,6 @@ static inline void local_irq_disable(void) asm volatile("msr daifset, #3" : : : "memory"); } +int get_vcpuid(void); + #endif /* SELFTEST_KVM_PROCESSOR_H */ diff --git a/tools/testing/selftests/kvm/lib/aarch64/processor.c b/tools/testing/selftests/kvm/lib/aarch64/processor.c index 632b74d6b3ca..9844b62227b1 100644 --- a/tools/testing/selftests/kvm/lib/aarch64/processor.c +++ b/tools/testing/selftests/kvm/lib/aarch64/processor.c @@ -13,9 +13,17 @@ #include "processor.h" #define DEFAULT_ARM64_GUEST_STACK_VADDR_MIN 0xac0000 +#define VM_VCPUID_MAP_INVAL -1 static vm_vaddr_t exception_handlers; +struct vm_vcpuid_map { + uint64_t mpidr; + int vcpuid; +}; + +static struct vm_vcpuid_map vcpuid_map[KVM_MAX_VCPUS]; + static uint64_t page_align(struct kvm_vm *vm, uint64_t v) { return (v + vm->page_size) & ~(vm->page_size - 1); @@ -426,3 +434,41 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, assert(vector < VECTOR_NUM); handlers->exception_handlers[vector][0] = handler; } + +void vm_vcpuid_map_init(struct kvm_vm *vm) +{ + int i = 0; + struct vcpu *vcpu; + struct vm_vcpuid_map *map; + + list_for_each_entry(vcpu, &vm->vcpus, list) { + map = &vcpuid_map[i++]; + map->vcpuid = vcpu->id; + get_reg(vm, vcpu->id, + ARM64_SYS_KVM_REG(SYS_MPIDR_EL1), &map->mpidr); + map->mpidr &= MPIDR_HWID_BITMASK; + } + + if (i < KVM_MAX_VCPUS) + vcpuid_map[i].vcpuid = VM_VCPUID_MAP_INVAL; + + sync_global_to_guest(vm, vcpuid_map); +} + +int get_vcpuid(void) +{ + int i, vcpuid; + uint64_t mpidr = read_sysreg(mpidr_el1) & MPIDR_HWID_BITMASK; + + for (i = 0; i < KVM_MAX_VCPUS; i++) { + vcpuid = vcpuid_map[i].vcpuid; + GUEST_ASSERT_1(vcpuid != VM_VCPUID_MAP_INVAL, mpidr); + + if (mpidr == vcpuid_map[i].mpidr) + return vcpuid; + } + + /* We should not be reaching here */ + GUEST_ASSERT_1(0, mpidr); + return -1; +}