Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp101129pxb; Wed, 8 Sep 2021 18:42:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbB00pcFgNNHmOq59ZydpzeyBt4L1circ2VdhU8kQSdl9ChgCZPiRxjHBx1JvoeRHBE5wC X-Received: by 2002:a05:6638:34a6:: with SMTP id t38mr501015jal.19.1631151736700; Wed, 08 Sep 2021 18:42:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631151736; cv=none; d=google.com; s=arc-20160816; b=e048dgzYywIbMIStUxgb2xZtMHny/y3FHVxMzzkgHpdeIulwXDl+tbjGwbpxX1dTTA kVxB97DVZKg+/T7j/SK3mlGq3uqnsGaHUGPlhazN3fjyZpHVOYxXnUDhqFwlZ+8HuPfb ILR8iIZAojtyjag1BxWY3Bi9mKmA2qTUDRM4isCf3u2l8eLheo7SU9m2M0uc4th0+dj6 qXrz50sdsiVi38SuvHJsUnOLkJuC9GWmz81OlP6mcO3a0SZ9WmTWEDrbQ1H+nCCBeuX+ KH7tgbwSMsGY4sSffELOeftyA2wkd4A7amTjEBMUXF2Qb3SKh/QycmF1nKeSLgGY6TJx L2CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=CjBV1NcFy7p27eV1/jQ5yVLZ7UVN92HtWY4NlfI6+Nk=; b=Kqdb5+gcPuK5VYekUHHYsYiq04DjKIpfgl219hnQELWdHigYfvTujh7ZTCbKY/whbJ dS5ptfQ+oItAUShUx94QQSz2MayDmTBcUhf3ZcAXXYiqIz/FAqGX9N2qlLIFnW15AO7b 0MA9E4Sb6nnpZ8eC67AFpuK4E6gbNkq+XmG7Apixuk4eoGsa93RbpapHdeyx5swCciSj Pa5fQhWmG3J5SgkEk+gI5rwtlJsvFxHw77uuuMQpeqwS6mUcCJ85OP4SsFiNUpgwFjLX LjcWZ5UiWkMSs99IlS11mz1/aToivoWEOCiOfHU9m6/X1OmcZlRJV6rNsiPt1YVye/9r 7HuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qs44jTJx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t10si291841iof.42.2021.09.08.18.42.05; Wed, 08 Sep 2021 18:42:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qs44jTJx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349740AbhIIBk0 (ORCPT + 99 others); Wed, 8 Sep 2021 21:40:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349333AbhIIBkF (ORCPT ); Wed, 8 Sep 2021 21:40:05 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EF24C0612A3 for ; Wed, 8 Sep 2021 18:38:47 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id f9-20020a1709028609b0290128bcba6be7so11617plo.18 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=etP7sA+yX4oDlboThJs1bAZK+a8V4b9/uRVlLoLoO3ORXX2JM6Wflx7g4xsJh5qChA pczKfnVqu647IHIItkfF3hiNlV8QZ6DIJq++7h5J7CHEqEAzkVI6vionJL6FR3rz6sA5 QRGGxsdZ4umx0NZfRVl1FhN5HsDrwv2/M0amJdKJbcXPkJv60M81TCGeyvbiPg+zAsc4 2+nNF35ickyInqa2XHGEhE23iNPt244cBUegl46HkVSNPPvbCPiKXZgsMUfiiCQyoXRT 7qgWkv44K63JqOi0SbHMIeG1lkRg6ZYF96xlUxUI4JN7nnqHNoMLjf+DjpHForWFD4dW bMmQ== X-Gm-Message-State: AOAM530Ea7/wDBKYBZcKG1qM3EiT6UaJF3HpEwLLIXXN5turtvSuRN/1 yaC6uxbtJrwNbKRJqbd1FSJIJYLGHnuW 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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; +} -- 2.33.0.153.gba50c8fa24-goog