From patchwork Fri Dec 10 16:46:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670169 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 E8A28C433FE for ; Fri, 10 Dec 2021 16:48:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244029AbhLJQwI (ORCPT ); Fri, 10 Dec 2021 11:52:08 -0500 Received: from mail-co1nam11on2069.outbound.protection.outlook.com ([40.107.220.69]:52385 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S244287AbhLJQv5 (ORCPT ); Fri, 10 Dec 2021 11:51:57 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jeCx8/J8DUyHrTnSZos0RbnZvdxH/v1Z7tj3V70wnre5jJPVzKNC67KL/MrvIHK5N2/ckAY3CPUWYlmIl/388fjdMgPgLHcREyc7BkQrBhtPUF+vM4mokCO4pEzD2LwgjVU4VdX9yVifzNTpSA2giNznCac0/hQMtBXBYHouQMQ0S4/DZdPmkiFPCSjNzuxbP7xNXAbIOzjjVLsk2WFQmIUxY92z3WAO5MpCV+PEIPOCqV20W0O/3S65Ma8l/AUdg0a459RCtikHpwjU2MXpEZKVLjIReD28VxpRP50rf4dxXZuSBQmgBH9PhdVln41kMt5+emKNSYsYYLwKEyFh2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/mWO/8WOxEIkwLByFCfm7KVUDUjWvtOpS7pLVnYcF5A=; b=YZU/n21imR1G5ycCjJ3HJ2qvYtU7sWzsvh7e7ehRUckRCKxRNIMNkNU+zhisX3jLgP6G4HSfWF2+yiWIjE2//BglSA+jxDzRM2ox1xnNPkZ71sbK3M99aRFrKjPvMLVtnQL7SBxAs/4lLEXlSvYP8HJCZFcJ8/4hN86uXAUOU2DIYaZeJBVhfg3iiLk+XoxyAMH5CEwB0zDBIsZsoJnP60zggD+dcSB+RlJzD1nHV5/6E/dtdBTr8gbB7mYaU3F7K0QZFjW5VWGtOw9zdIG+4vUSAF5oQx9Pb0pQdjxTbby6AvxPBaniWGbqbK9byM2w6XIW/zKnPuwYFsp1VKC19A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/mWO/8WOxEIkwLByFCfm7KVUDUjWvtOpS7pLVnYcF5A=; b=QR2Q6JDr/jC11OKdWx1hizDepmrm57VzdqsJLjvnCa3F75JePWkADbDF9nd7zYTh0zTBdHphEE5i/3Dyb9UQ9CF+X92kUSZo9yzE09au/4agKVzH7j6eAzyWPWPnS9HEpg5XdLQ9F7Qo9b5voDddwtftv/AoJriwfdycPuPQU3g= Received: from DM5PR07CA0155.namprd07.prod.outlook.com (2603:10b6:3:ee::21) by BN8PR12MB2916.namprd12.prod.outlook.com (2603:10b6:408:6a::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Fri, 10 Dec 2021 16:48:11 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:ee:cafe::9f) by DM5PR07CA0155.outlook.office365.com (2603:10b6:3:ee::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.20 via Frontend Transport; Fri, 10 Dec 2021 16:48:11 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:48:11 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:48:10 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 01/10] kvm: selftests: move base kvm_util.h declarations to kvm_util_base.h Date: Fri, 10 Dec 2021 10:46:11 -0600 Message-ID: <20211210164620.11636-2-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2210f964-c16f-431f-dcf4-08d9bbfcd9fd X-MS-TrafficTypeDiagnostic: BN8PR12MB2916:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bpMfuXfhQGgMFo+tWK/qhS4x2Px/DqzQq0hEgMOVvx/RcmC9NtUwz9UyTTVjdVcaYliTUqYRxF9QRTFuEz1Yrc+xk/5N33fB6+bTS8hAd1/YTn8NgTS1KNPo7hRaiTEGjEBO4ByvWfYzMeZgjnnhNX7rAldnl40LTZlHgOS/5bcqWm3dwWdrrq6uMRaKdjZwa+fBuIJ39XPFYEcCMRFVAkP6y6dJGlu0+xvx9K8GrzI92Y5Ow5ikX/uU731lTY2J06kBkhvy8L46jswC8JWzdah25QTxn249tgbMx2BP0ONeGyBwYRFwQWjO+Ces94J3354tclIQdYDfT1FxztfpfM9Ia7ILCzK/gfwU1jpbVpUl5KbnAVSDs84BBfUMUlQv9vUPm9kySA6aCbcNGZPBByopHx//TmGlo+SxPpr5gid97VUWz7CWnL6VhZgu513sM63MDT7UvCG0pL8OCoZc9YR84Y8Qd3mE1kuiJuI7Q820IxSzMPvKkHgcJ8DP6GjCQ3wlT1NJtoBF3vUwsqzq4Ztv+fihWRV1cilUHf55hfwB+7p9I+sKWQ/rVc+5/mtQmuPoPSRwFDjFFtNoN5GmXsbljc8pLMtI2asJJdOcCAAAnd8VVx9xxVSrGUAKpflhWDf1JE8CwfamGlsnz+cDHjMQFexSnuRyf5DHhpY1XX5gHKcMF2HQfeRuPtiPQ2N7Q0ZLFgjwXKayOstWABd2kw5kndR3H8FQY72dsMUs+8dfMj6LKnU0SUCJWh+mSZ1m8H0HJbPAdODciQc3grS1F4sWQ5XIynz0Y4QVDaVII9c= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(508600001)(86362001)(8936002)(81166007)(336012)(70206006)(16526019)(426003)(2906002)(4326008)(82310400004)(186003)(2616005)(40460700001)(47076005)(8676002)(26005)(7416002)(30864003)(356005)(5660300002)(44832011)(54906003)(36860700001)(6916009)(36756003)(70586007)(83380400001)(316002)(1076003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:48:11.2218 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2210f964-c16f-431f-dcf4-08d9bbfcd9fd X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8PR12MB2916 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Between helper macros and interfaces that will be introduced in subsequent patches, much of kvm_util.h would end up being declarations specific to ucall. Ideally these could be separated out into a separate header since they are not strictly required for writing guest tests and are mostly self-contained interfaces other than a reliance on a few core declarations like struct kvm_vm. This doesn't make a big difference as far as how tests will be compiled/written since all these interfaces will still be packaged up into a single/common libkvm.a used by all tests, but it is still nice to be able to compartmentalize to improve readabilty and reduce merge conflicts in the future for common tasks like adding new interfaces to kvm_util.h. Furthermore, some of the ucall declarations will be arch-specific, requiring various #ifdef'ery in kvm_util.h. Ideally these declarations could live in separate arch-specific headers, e.g. include//ucall.h, which would handle arch-specific declarations as well as pulling in common ucall-related declarations shared by all archs. One simple way to do this would be to #include ucall.h at the bottom of kvm_util.h, after declarations it relies upon like struct kvm_vm. This is brittle however, and doesn't scale easily to other sets of interfaces that may be added in the future. Instead, move all declarations currently in kvm_util.h into kvm_util_base.h, then have kvm_util.h #include it. With this change, non-base declarations can be selectively moved/introduced into separate headers, which can then be included in kvm_util.h so that individual tests don't need to be touched. Subsequent patches will then move ucall-related declarations into a separate header to meet the above goals. Signed-off-by: Michael Roth --- .../testing/selftests/kvm/include/kvm_util.h | 407 +---------------- .../selftests/kvm/include/kvm_util_base.h | 417 ++++++++++++++++++ 2 files changed, 418 insertions(+), 406 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/kvm_util_base.h diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 6a1a37f30494..c860ced3888d 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -7,411 +7,6 @@ #ifndef SELFTEST_KVM_UTIL_H #define SELFTEST_KVM_UTIL_H -#include "test_util.h" - -#include "asm/kvm.h" -#include "linux/list.h" -#include "linux/kvm.h" -#include - -#include "sparsebit.h" - -#define KVM_DEV_PATH "/dev/kvm" -#define KVM_MAX_VCPUS 512 - -#define NSEC_PER_SEC 1000000000L - -/* - * Callers of kvm_util only have an incomplete/opaque description of the - * structure kvm_util is using to maintain the state of a VM. - */ -struct kvm_vm; - -typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */ -typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ - -/* Minimum allocated guest virtual and physical addresses */ -#define KVM_UTIL_MIN_VADDR 0x2000 -#define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 - -#define DEFAULT_GUEST_PHY_PAGES 512 -#define DEFAULT_GUEST_STACK_VADDR_MIN 0xab6000 -#define DEFAULT_STACK_PGS 5 - -enum vm_guest_mode { - VM_MODE_P52V48_4K, - VM_MODE_P52V48_64K, - VM_MODE_P48V48_4K, - VM_MODE_P48V48_64K, - VM_MODE_P40V48_4K, - VM_MODE_P40V48_64K, - VM_MODE_PXXV48_4K, /* For 48bits VA but ANY bits PA */ - VM_MODE_P47V64_4K, - VM_MODE_P44V64_4K, - NUM_VM_MODES, -}; - -#if defined(__aarch64__) - -#define VM_MODE_DEFAULT VM_MODE_P40V48_4K -#define MIN_PAGE_SHIFT 12U -#define ptes_per_page(page_size) ((page_size) / 8) - -#elif defined(__x86_64__) - -#define VM_MODE_DEFAULT VM_MODE_PXXV48_4K -#define MIN_PAGE_SHIFT 12U -#define ptes_per_page(page_size) ((page_size) / 8) - -#elif defined(__s390x__) - -#define VM_MODE_DEFAULT VM_MODE_P44V64_4K -#define MIN_PAGE_SHIFT 12U -#define ptes_per_page(page_size) ((page_size) / 16) - -#endif - -#define MIN_PAGE_SIZE (1U << MIN_PAGE_SHIFT) -#define PTES_PER_MIN_PAGE ptes_per_page(MIN_PAGE_SIZE) - -struct vm_guest_mode_params { - unsigned int pa_bits; - unsigned int va_bits; - unsigned int page_size; - unsigned int page_shift; -}; -extern const struct vm_guest_mode_params vm_guest_mode_params[]; - -int open_path_or_exit(const char *path, int flags); -int open_kvm_dev_path_or_exit(void); -int kvm_check_cap(long cap); -int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap); -int vcpu_enable_cap(struct kvm_vm *vm, uint32_t vcpu_id, - struct kvm_enable_cap *cap); -void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size); -const char *vm_guest_mode_string(uint32_t i); - -struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm); -void kvm_vm_free(struct kvm_vm *vmp); -void kvm_vm_restart(struct kvm_vm *vmp, int perm); -void kvm_vm_release(struct kvm_vm *vmp); -void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log); -void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log, - uint64_t first_page, uint32_t num_pages); -uint32_t kvm_vm_reset_dirty_ring(struct kvm_vm *vm); - -int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, const vm_vaddr_t gva, - size_t len); - -void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename); - -void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent); - -/* - * VM VCPU Dump - * - * Input Args: - * stream - Output FILE stream - * vm - Virtual Machine - * vcpuid - VCPU ID - * indent - Left margin indent amount - * - * Output Args: None - * - * Return: None - * - * Dumps the current state of the VCPU specified by @vcpuid, within the VM - * given by @vm, to the FILE stream given by @stream. - */ -void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, - uint8_t indent); - -void vm_create_irqchip(struct kvm_vm *vm); - -void vm_userspace_mem_region_add(struct kvm_vm *vm, - enum vm_mem_backing_src_type src_type, - uint64_t guest_paddr, uint32_t slot, uint64_t npages, - uint32_t flags); - -void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, - void *arg); -int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, - void *arg); -void vm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); -int _vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg); -void kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); -int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); -void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); -void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); -void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); -void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid); -vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); -vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); -vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm); - -void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, - unsigned int npages); -void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa); -void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); -vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); -void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa); - -/* - * Address Guest Virtual to Guest Physical - * - * Input Args: - * vm - Virtual Machine - * gva - VM virtual address - * - * Output Args: None - * - * Return: - * Equivalent VM physical address - * - * Returns the VM physical address of the translated VM virtual - * address given by @gva. - */ -vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva); - -struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid); -void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid); -int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid); -int vcpu_get_fd(struct kvm_vm *vm, uint32_t vcpuid); -void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid); -void vcpu_set_guest_debug(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_guest_debug *debug); -void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_mp_state *mp_state); -struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vm *vm, uint32_t vcpuid); -void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs); -void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs); - -/* - * VM VCPU Args Set - * - * Input Args: - * vm - Virtual Machine - * vcpuid - VCPU ID - * num - number of arguments - * ... - arguments, each of type uint64_t - * - * Output Args: None - * - * Return: None - * - * Sets the first @num function input registers of the VCPU with @vcpuid, - * per the C calling convention of the architecture, to the values given - * as variable args. Each of the variable args is expected to be of type - * uint64_t. The maximum @num can be is specific to the architecture. - */ -void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...); - -void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_sregs *sregs); -void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_sregs *sregs); -int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_sregs *sregs); -void vcpu_fpu_get(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_fpu *fpu); -void vcpu_fpu_set(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_fpu *fpu); -void vcpu_get_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg); -void vcpu_set_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg); -#ifdef __KVM_HAVE_VCPU_EVENTS -void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_vcpu_events *events); -void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_vcpu_events *events); -#endif -#ifdef __x86_64__ -void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_nested_state *state); -int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid, - struct kvm_nested_state *state, bool ignore_error); -#endif -void *vcpu_map_dirty_ring(struct kvm_vm *vm, uint32_t vcpuid); - -int _kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr); -int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr); -int _kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test, int *fd); -int kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test); -int _kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, - void *val, bool write); -int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, - void *val, bool write); - -int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, - uint64_t attr); -int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, - uint64_t attr); -int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, - uint64_t attr, void *val, bool write); -int vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, - uint64_t attr, void *val, bool write); - -const char *exit_reason_str(unsigned int exit_reason); - -void virt_pgd_alloc(struct kvm_vm *vm); - -/* - * VM Virtual Page Map - * - * Input Args: - * vm - Virtual Machine - * vaddr - VM Virtual Address - * paddr - VM Physical Address - * memslot - Memory region slot for new virtual translation tables - * - * Output Args: None - * - * Return: None - * - * Within @vm, creates a virtual translation for the page starting - * at @vaddr to the page starting at @paddr. - */ -void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr); - -vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, - uint32_t memslot); -vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, - vm_paddr_t paddr_min, uint32_t memslot); -vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm); - -/* - * Create a VM with reasonable defaults - * - * Input Args: - * vcpuid - The id of the single VCPU to add to the VM. - * extra_mem_pages - The number of extra pages to add (this will - * decide how much extra space we will need to - * setup the page tables using memslot 0) - * guest_code - The vCPU's entry point - * - * Output Args: None - * - * Return: - * Pointer to opaque structure that describes the created VM. - */ -struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages, - void *guest_code); - -/* Same as vm_create_default, but can be used for more than one vcpu */ -struct kvm_vm *vm_create_default_with_vcpus(uint32_t nr_vcpus, uint64_t extra_mem_pages, - uint32_t num_percpu_pages, void *guest_code, - uint32_t vcpuids[]); - -/* Like vm_create_default_with_vcpus, but accepts mode and slot0 memory as a parameter */ -struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, - uint64_t slot0_mem_pages, uint64_t extra_mem_pages, - uint32_t num_percpu_pages, void *guest_code, - uint32_t vcpuids[]); - -/* - * Adds a vCPU with reasonable defaults (e.g. a stack) - * - * Input Args: - * vm - Virtual Machine - * vcpuid - The id of the VCPU to add to the VM. - * guest_code - The vCPU's entry point - */ -void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code); - -bool vm_is_unrestricted_guest(struct kvm_vm *vm); - -unsigned int vm_get_page_size(struct kvm_vm *vm); -unsigned int vm_get_page_shift(struct kvm_vm *vm); -uint64_t vm_get_max_gfn(struct kvm_vm *vm); -int vm_get_fd(struct kvm_vm *vm); - -unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size); -unsigned int vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages); -unsigned int vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages); -static inline unsigned int -vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages) -{ - unsigned int n; - n = vm_num_guest_pages(mode, vm_num_host_pages(mode, num_guest_pages)); -#ifdef __s390x__ - /* s390 requires 1M aligned guest sizes */ - n = (n + 255) & ~255; -#endif - return n; -} - -struct kvm_userspace_memory_region * -kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, - uint64_t end); - -struct kvm_dirty_log * -allocate_kvm_dirty_log(struct kvm_userspace_memory_region *region); - -int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd); - -#define sync_global_to_guest(vm, g) ({ \ - typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ - memcpy(_p, &(g), sizeof(g)); \ -}) - -#define sync_global_from_guest(vm, g) ({ \ - typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ - memcpy(&(g), _p, sizeof(g)); \ -}) - -void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid); - -/* Common ucalls */ -enum { - UCALL_NONE, - UCALL_SYNC, - UCALL_ABORT, - UCALL_DONE, - UCALL_UNHANDLED, -}; - -#define UCALL_MAX_ARGS 6 - -struct ucall { - uint64_t cmd; - uint64_t args[UCALL_MAX_ARGS]; -}; - -void ucall_init(struct kvm_vm *vm, void *arg); -void ucall_uninit(struct kvm_vm *vm); -void ucall(uint64_t cmd, int nargs, ...); -uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); - -#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ - ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) -#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) -#define GUEST_DONE() ucall(UCALL_DONE, 0) -#define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...) do { \ - if (!(_condition)) \ - ucall(UCALL_ABORT, 2 + _nargs, \ - "Failed guest assert: " \ - _condstr, __LINE__, _args); \ -} while (0) - -#define GUEST_ASSERT(_condition) \ - __GUEST_ASSERT(_condition, #_condition, 0, 0) - -#define GUEST_ASSERT_1(_condition, arg1) \ - __GUEST_ASSERT(_condition, #_condition, 1, (arg1)) - -#define GUEST_ASSERT_2(_condition, arg1, arg2) \ - __GUEST_ASSERT(_condition, #_condition, 2, (arg1), (arg2)) - -#define GUEST_ASSERT_3(_condition, arg1, arg2, arg3) \ - __GUEST_ASSERT(_condition, #_condition, 3, (arg1), (arg2), (arg3)) - -#define GUEST_ASSERT_4(_condition, arg1, arg2, arg3, arg4) \ - __GUEST_ASSERT(_condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4)) - -#define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b) - -int vm_get_stats_fd(struct kvm_vm *vm); -int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid); - -uint32_t guest_get_vcpuid(void); +#include "kvm_util_base.h" #endif /* SELFTEST_KVM_UTIL_H */ diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h new file mode 100644 index 000000000000..8fb6aeff5469 --- /dev/null +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -0,0 +1,417 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * tools/testing/selftests/kvm/include/kvm_util_base.h + * + * Copyright (C) 2018, Google LLC. + */ +#ifndef SELFTEST_KVM_UTIL_BASE_H +#define SELFTEST_KVM_UTIL_BASE_H + +#include "test_util.h" + +#include "asm/kvm.h" +#include "linux/list.h" +#include "linux/kvm.h" +#include + +#include "sparsebit.h" + +#define KVM_DEV_PATH "/dev/kvm" +#define KVM_MAX_VCPUS 512 + +#define NSEC_PER_SEC 1000000000L + +/* + * Callers of kvm_util only have an incomplete/opaque description of the + * structure kvm_util is using to maintain the state of a VM. + */ +struct kvm_vm; + +typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */ +typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ + +/* Minimum allocated guest virtual and physical addresses */ +#define KVM_UTIL_MIN_VADDR 0x2000 +#define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000 + +#define DEFAULT_GUEST_PHY_PAGES 512 +#define DEFAULT_GUEST_STACK_VADDR_MIN 0xab6000 +#define DEFAULT_STACK_PGS 5 + +enum vm_guest_mode { + VM_MODE_P52V48_4K, + VM_MODE_P52V48_64K, + VM_MODE_P48V48_4K, + VM_MODE_P48V48_64K, + VM_MODE_P40V48_4K, + VM_MODE_P40V48_64K, + VM_MODE_PXXV48_4K, /* For 48bits VA but ANY bits PA */ + VM_MODE_P47V64_4K, + VM_MODE_P44V64_4K, + NUM_VM_MODES, +}; + +#if defined(__aarch64__) + +#define VM_MODE_DEFAULT VM_MODE_P40V48_4K +#define MIN_PAGE_SHIFT 12U +#define ptes_per_page(page_size) ((page_size) / 8) + +#elif defined(__x86_64__) + +#define VM_MODE_DEFAULT VM_MODE_PXXV48_4K +#define MIN_PAGE_SHIFT 12U +#define ptes_per_page(page_size) ((page_size) / 8) + +#elif defined(__s390x__) + +#define VM_MODE_DEFAULT VM_MODE_P44V64_4K +#define MIN_PAGE_SHIFT 12U +#define ptes_per_page(page_size) ((page_size) / 16) + +#endif + +#define MIN_PAGE_SIZE (1U << MIN_PAGE_SHIFT) +#define PTES_PER_MIN_PAGE ptes_per_page(MIN_PAGE_SIZE) + +struct vm_guest_mode_params { + unsigned int pa_bits; + unsigned int va_bits; + unsigned int page_size; + unsigned int page_shift; +}; +extern const struct vm_guest_mode_params vm_guest_mode_params[]; + +int open_path_or_exit(const char *path, int flags); +int open_kvm_dev_path_or_exit(void); +int kvm_check_cap(long cap); +int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap); +int vcpu_enable_cap(struct kvm_vm *vm, uint32_t vcpu_id, + struct kvm_enable_cap *cap); +void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size); +const char *vm_guest_mode_string(uint32_t i); + +struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm); +void kvm_vm_free(struct kvm_vm *vmp); +void kvm_vm_restart(struct kvm_vm *vmp, int perm); +void kvm_vm_release(struct kvm_vm *vmp); +void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log); +void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log, + uint64_t first_page, uint32_t num_pages); +uint32_t kvm_vm_reset_dirty_ring(struct kvm_vm *vm); + +int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, const vm_vaddr_t gva, + size_t len); + +void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename); + +void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent); + +/* + * VM VCPU Dump + * + * Input Args: + * stream - Output FILE stream + * vm - Virtual Machine + * vcpuid - VCPU ID + * indent - Left margin indent amount + * + * Output Args: None + * + * Return: None + * + * Dumps the current state of the VCPU specified by @vcpuid, within the VM + * given by @vm, to the FILE stream given by @stream. + */ +void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, + uint8_t indent); + +void vm_create_irqchip(struct kvm_vm *vm); + +void vm_userspace_mem_region_add(struct kvm_vm *vm, + enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, uint64_t npages, + uint32_t flags); + +void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, + void *arg); +int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl, + void *arg); +void vm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); +int _vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg); +void kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); +int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg); +void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); +void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); +void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); +void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid); +vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); +vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); +vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm); + +void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, + unsigned int npages); +void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa); +void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); +vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); +void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa); + +/* + * Address Guest Virtual to Guest Physical + * + * Input Args: + * vm - Virtual Machine + * gva - VM virtual address + * + * Output Args: None + * + * Return: + * Equivalent VM physical address + * + * Returns the VM physical address of the translated VM virtual + * address given by @gva. + */ +vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva); + +struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid); +void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid); +int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid); +int vcpu_get_fd(struct kvm_vm *vm, uint32_t vcpuid); +void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid); +void vcpu_set_guest_debug(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_guest_debug *debug); +void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_mp_state *mp_state); +struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vm *vm, uint32_t vcpuid); +void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs); +void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs); + +/* + * VM VCPU Args Set + * + * Input Args: + * vm - Virtual Machine + * vcpuid - VCPU ID + * num - number of arguments + * ... - arguments, each of type uint64_t + * + * Output Args: None + * + * Return: None + * + * Sets the first @num function input registers of the VCPU with @vcpuid, + * per the C calling convention of the architecture, to the values given + * as variable args. Each of the variable args is expected to be of type + * uint64_t. The maximum @num can be is specific to the architecture. + */ +void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...); + +void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_sregs *sregs); +void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_sregs *sregs); +int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_sregs *sregs); +void vcpu_fpu_get(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_fpu *fpu); +void vcpu_fpu_set(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_fpu *fpu); +void vcpu_get_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg); +void vcpu_set_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg); +#ifdef __KVM_HAVE_VCPU_EVENTS +void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_vcpu_events *events); +void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_vcpu_events *events); +#endif +#ifdef __x86_64__ +void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_nested_state *state); +int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid, + struct kvm_nested_state *state, bool ignore_error); +#endif +void *vcpu_map_dirty_ring(struct kvm_vm *vm, uint32_t vcpuid); + +int _kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr); +int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr); +int _kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test, int *fd); +int kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test); +int _kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, + void *val, bool write); +int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, + void *val, bool write); + +int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr); +int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr); +int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr, void *val, bool write); +int vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, + uint64_t attr, void *val, bool write); + +const char *exit_reason_str(unsigned int exit_reason); + +void virt_pgd_alloc(struct kvm_vm *vm); + +/* + * VM Virtual Page Map + * + * Input Args: + * vm - Virtual Machine + * vaddr - VM Virtual Address + * paddr - VM Physical Address + * memslot - Memory region slot for new virtual translation tables + * + * Output Args: None + * + * Return: None + * + * Within @vm, creates a virtual translation for the page starting + * at @vaddr to the page starting at @paddr. + */ +void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr); + +vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, + uint32_t memslot); +vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, + vm_paddr_t paddr_min, uint32_t memslot); +vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm); + +/* + * Create a VM with reasonable defaults + * + * Input Args: + * vcpuid - The id of the single VCPU to add to the VM. + * extra_mem_pages - The number of extra pages to add (this will + * decide how much extra space we will need to + * setup the page tables using memslot 0) + * guest_code - The vCPU's entry point + * + * Output Args: None + * + * Return: + * Pointer to opaque structure that describes the created VM. + */ +struct kvm_vm *vm_create_default(uint32_t vcpuid, uint64_t extra_mem_pages, + void *guest_code); + +/* Same as vm_create_default, but can be used for more than one vcpu */ +struct kvm_vm *vm_create_default_with_vcpus(uint32_t nr_vcpus, uint64_t extra_mem_pages, + uint32_t num_percpu_pages, void *guest_code, + uint32_t vcpuids[]); + +/* Like vm_create_default_with_vcpus, but accepts mode and slot0 memory as a parameter */ +struct kvm_vm *vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, + uint64_t slot0_mem_pages, uint64_t extra_mem_pages, + uint32_t num_percpu_pages, void *guest_code, + uint32_t vcpuids[]); + +/* + * Adds a vCPU with reasonable defaults (e.g. a stack) + * + * Input Args: + * vm - Virtual Machine + * vcpuid - The id of the VCPU to add to the VM. + * guest_code - The vCPU's entry point + */ +void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code); + +bool vm_is_unrestricted_guest(struct kvm_vm *vm); + +unsigned int vm_get_page_size(struct kvm_vm *vm); +unsigned int vm_get_page_shift(struct kvm_vm *vm); +uint64_t vm_get_max_gfn(struct kvm_vm *vm); +int vm_get_fd(struct kvm_vm *vm); + +unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size); +unsigned int vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages); +unsigned int vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages); +static inline unsigned int +vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages) +{ + unsigned int n; + n = vm_num_guest_pages(mode, vm_num_host_pages(mode, num_guest_pages)); +#ifdef __s390x__ + /* s390 requires 1M aligned guest sizes */ + n = (n + 255) & ~255; +#endif + return n; +} + +struct kvm_userspace_memory_region * +kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, + uint64_t end); + +struct kvm_dirty_log * +allocate_kvm_dirty_log(struct kvm_userspace_memory_region *region); + +int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd); + +#define sync_global_to_guest(vm, g) ({ \ + typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ + memcpy(_p, &(g), sizeof(g)); \ +}) + +#define sync_global_from_guest(vm, g) ({ \ + typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ + memcpy(&(g), _p, sizeof(g)); \ +}) + +void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid); + +/* Common ucalls */ +enum { + UCALL_NONE, + UCALL_SYNC, + UCALL_ABORT, + UCALL_DONE, + UCALL_UNHANDLED, +}; + +#define UCALL_MAX_ARGS 6 + +struct ucall { + uint64_t cmd; + uint64_t args[UCALL_MAX_ARGS]; +}; + +void ucall_init(struct kvm_vm *vm, void *arg); +void ucall_uninit(struct kvm_vm *vm); +void ucall(uint64_t cmd, int nargs, ...); +uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); + +#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ + ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) +#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) +#define GUEST_DONE() ucall(UCALL_DONE, 0) +#define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...) do { \ + if (!(_condition)) \ + ucall(UCALL_ABORT, 2 + _nargs, \ + "Failed guest assert: " \ + _condstr, __LINE__, _args); \ +} while (0) + +#define GUEST_ASSERT(_condition) \ + __GUEST_ASSERT(_condition, #_condition, 0, 0) + +#define GUEST_ASSERT_1(_condition, arg1) \ + __GUEST_ASSERT(_condition, #_condition, 1, (arg1)) + +#define GUEST_ASSERT_2(_condition, arg1, arg2) \ + __GUEST_ASSERT(_condition, #_condition, 2, (arg1), (arg2)) + +#define GUEST_ASSERT_3(_condition, arg1, arg2, arg3) \ + __GUEST_ASSERT(_condition, #_condition, 3, (arg1), (arg2), (arg3)) + +#define GUEST_ASSERT_4(_condition, arg1, arg2, arg3, arg4) \ + __GUEST_ASSERT(_condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4)) + +#define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b) + +int vm_get_stats_fd(struct kvm_vm *vm); +int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid); + +uint32_t guest_get_vcpuid(void); + +#endif /* SELFTEST_KVM_UTIL_BASE_H */ From patchwork Fri Dec 10 16:46:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670171 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 8C24AC433F5 for ; Fri, 10 Dec 2021 16:48:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244219AbhLJQwP (ORCPT ); Fri, 10 Dec 2021 11:52:15 -0500 Received: from mail-mw2nam10on2067.outbound.protection.outlook.com ([40.107.94.67]:13537 "EHLO NAM10-MW2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S244163AbhLJQwL (ORCPT ); Fri, 10 Dec 2021 11:52:11 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JeTDvnJf5rHc/4ItMhnNN8vN8HPRN4U5oIkUfZDjcNa/i3Fhhk2zVFHR3PgC/QQTtQMAgd5uessvjd63XyFGyVVbtKcCFcPz7j5B2GBFGRSxy0MwWIBs7MANbeJPfmmG4VCZ6xHUuASvI6uHaIFlPl3vBePNzJZvsRxkr5qytkW7+HmDTTquSDOj5zaDUsXBM1am+FJdt4PgVOWaCFb4ZhinvmQOymorKuaZZDXsFVPqwxgwV9+mx84X5qreEub5tC3KqqI22ZLHBaTFthxOPGxwJCm3HUbblQZ/FSoyJZvtkEj2o9Puy2O16jg3jbxjeh+gENYWdN914HwII9Sd+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MuXkIj6tZLmVl1zci4j02/2OeVXLKmCINcM5BeeJbho=; b=L82vrwLecNFgmhMKwBSziXSDj6mzDRvdihMtdAFwSQ7xO1TcS4NoOnyakBsiRiDGieRhFVyw9oMEKbgCbk5TMGPVpBTiF9ZH37FuTUkczM49pvZgJ1mqhbg/hm7YmHMK6oIssvzavkYOMWP6ZOtsVzbhH/3TK4x+oNq4w9L7UTu2l8pWFk9luHXaEthG1PbL6iLYuTccm8AweQK7R9dSU/ddY6HBtbWNyW+ndbkZ4JpkvJB843S1LrVK7RRhy+iMbsgW1kBXukbClWGK00jxyF3bz6+nSRuub78qCUx2VTzxd9Bg6eCmD1+MkLswsfj1QgvULJCmzxkmWf71XN/mTg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MuXkIj6tZLmVl1zci4j02/2OeVXLKmCINcM5BeeJbho=; b=dO1eQVsKX2hgqcLvHn/0OBGkPE4rN7OKayf84PFVhyqJ9y1He8rQuFnQycP1HxxCaqY8FFmX9DNSxWZZ33pF/6nEcA+UmeEdXD8CjraCRbJ0uXEpN5gEaL7xUB6t1dpbBotjq+gOflZR4SNzBOEkjE8QrtQqqivhs5rgqZrRR8w= Received: from DM5PR19CA0006.namprd19.prod.outlook.com (2603:10b6:3:151::16) by BN6PR12MB1601.namprd12.prod.outlook.com (2603:10b6:405:8::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.11; Fri, 10 Dec 2021 16:48:32 +0000 Received: from DM6NAM11FT045.eop-nam11.prod.protection.outlook.com (2603:10b6:3:151:cafe::aa) by DM5PR19CA0006.outlook.office365.com (2603:10b6:3:151::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:48:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT045.mail.protection.outlook.com (10.13.173.123) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:48:32 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:48:31 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 02/10] kvm: selftests: move ucall declarations into ucall_common.h Date: Fri, 10 Dec 2021 10:46:12 -0600 Message-ID: <20211210164620.11636-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f40fb92f-fe2a-4dd5-b0f4-08d9bbfce699 X-MS-TrafficTypeDiagnostic: BN6PR12MB1601:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:155; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FwVQ6uDO5TyNh+t23kxyZ47zxIwp7yS3ZViNYc/T0PfQ/i/ZO/91Di6i66QHxDp8L2dMwdrPnDCvKcDyKWS4I8lGh8/fYJ+LBIXeUnH9ZBAQP00z96l78786Ut5UzrII37nko7C29MRnHvWlyH+cTEjtlmqbX2ymS1DUyyHc4QVp05sgxn9spn+NXkpIyPsti4sJjTsRDFMI0U1ak0uvc+pDzDI0dMrtb8WE8TGFQfXIrgCMxnsiewN1B+4tgJEFS3bd1MWo8sFUMc0Ta8onSUJkEN5OcAQnB8ECWlGgdOGyr9dzOltui5sFsfCNqRGArhSNNbpMZ3CCnrjSS12RSSltJ0IIp/tAt+xzksclBrXUUNGH2TZww2qDlRJ1tYRzJ0YMXkfgHBm7tuktunBZz/CeLC+J4F1iuEA+GNq0ZCbr2Iidt9Iz8WIB9WTx4tDwLVMP74n/VZ+o0U4peHAdOzbwQqzztMU4T1l4axTKPts/px1n2mXYOEfe2+4hfBOdcKILNvjsNZxiwc4EQjgj6WsAia9WMlg4gTTsfij8Xl21xp6SzAol7n15yjLA/POKA22FYNt/soVv1ZPRiABkKuzwraIFz2hz3iYujeJ7BLhEjTi85mkrzFz4YRYuk3sU3UPR7Ptw+xkV8U78V5shq24wdihaYyAbQl1CCBj+WFP2PtbtVqb6/vJzTo5JPdITnzhWNUAN2TaDoCXP5FI7CrTSGek8oNArtYTvANDPd9PvfYPlSGyYDHcOq6ts5tHZ1166iOHphVUG/Nx2mlSTOq0x3wcQ+lZdlDZgvwga/E8= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(36840700001)(46966006)(40470700001)(356005)(81166007)(26005)(44832011)(5660300002)(47076005)(83380400001)(1076003)(316002)(40460700001)(36860700001)(508600001)(7416002)(82310400004)(36756003)(6916009)(186003)(86362001)(336012)(426003)(2906002)(8936002)(16526019)(8676002)(6666004)(70586007)(70206006)(54906003)(2616005)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:48:32.3214 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f40fb92f-fe2a-4dd5-b0f4-08d9bbfce699 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT045.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1601 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Now that core kvm_util declarations have special home in kvm_util_base.h, move ucall-related declarations out into a separate header. Signed-off-by: Michael Roth --- .../testing/selftests/kvm/include/kvm_util.h | 1 + .../selftests/kvm/include/kvm_util_base.h | 49 --------------- .../selftests/kvm/include/ucall_common.h | 59 +++++++++++++++++++ 3 files changed, 60 insertions(+), 49 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/ucall_common.h diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index c860ced3888d..c9286811a4cb 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -8,5 +8,6 @@ #define SELFTEST_KVM_UTIL_H #include "kvm_util_base.h" +#include "ucall_common.h" #endif /* SELFTEST_KVM_UTIL_H */ diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 8fb6aeff5469..4e2946ba3ff7 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -360,55 +360,6 @@ int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd); void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid); -/* Common ucalls */ -enum { - UCALL_NONE, - UCALL_SYNC, - UCALL_ABORT, - UCALL_DONE, - UCALL_UNHANDLED, -}; - -#define UCALL_MAX_ARGS 6 - -struct ucall { - uint64_t cmd; - uint64_t args[UCALL_MAX_ARGS]; -}; - -void ucall_init(struct kvm_vm *vm, void *arg); -void ucall_uninit(struct kvm_vm *vm); -void ucall(uint64_t cmd, int nargs, ...); -uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); - -#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ - ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) -#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) -#define GUEST_DONE() ucall(UCALL_DONE, 0) -#define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...) do { \ - if (!(_condition)) \ - ucall(UCALL_ABORT, 2 + _nargs, \ - "Failed guest assert: " \ - _condstr, __LINE__, _args); \ -} while (0) - -#define GUEST_ASSERT(_condition) \ - __GUEST_ASSERT(_condition, #_condition, 0, 0) - -#define GUEST_ASSERT_1(_condition, arg1) \ - __GUEST_ASSERT(_condition, #_condition, 1, (arg1)) - -#define GUEST_ASSERT_2(_condition, arg1, arg2) \ - __GUEST_ASSERT(_condition, #_condition, 2, (arg1), (arg2)) - -#define GUEST_ASSERT_3(_condition, arg1, arg2, arg3) \ - __GUEST_ASSERT(_condition, #_condition, 3, (arg1), (arg2), (arg3)) - -#define GUEST_ASSERT_4(_condition, arg1, arg2, arg3, arg4) \ - __GUEST_ASSERT(_condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4)) - -#define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b) - int vm_get_stats_fd(struct kvm_vm *vm); int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid); diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h new file mode 100644 index 000000000000..9eecc9d40b79 --- /dev/null +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * tools/testing/selftests/kvm/include/kvm_util.h + * + * Copyright (C) 2018, Google LLC. + */ +#ifndef SELFTEST_KVM_UCALL_COMMON_H +#define SELFTEST_KVM_UCALL_COMMON_H + +/* Common ucalls */ +enum { + UCALL_NONE, + UCALL_SYNC, + UCALL_ABORT, + UCALL_DONE, + UCALL_UNHANDLED, +}; + +#define UCALL_MAX_ARGS 6 + +struct ucall { + uint64_t cmd; + uint64_t args[UCALL_MAX_ARGS]; +}; + +void ucall_init(struct kvm_vm *vm, void *arg); +void ucall_uninit(struct kvm_vm *vm); +void ucall(uint64_t cmd, int nargs, ...); +uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); + +#define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ + ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) +#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) +#define GUEST_DONE() ucall(UCALL_DONE, 0) +#define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...) do { \ + if (!(_condition)) \ + ucall(UCALL_ABORT, 2 + _nargs, \ + "Failed guest assert: " \ + _condstr, __LINE__, _args); \ +} while (0) + +#define GUEST_ASSERT(_condition) \ + __GUEST_ASSERT(_condition, #_condition, 0, 0) + +#define GUEST_ASSERT_1(_condition, arg1) \ + __GUEST_ASSERT(_condition, #_condition, 1, (arg1)) + +#define GUEST_ASSERT_2(_condition, arg1, arg2) \ + __GUEST_ASSERT(_condition, #_condition, 2, (arg1), (arg2)) + +#define GUEST_ASSERT_3(_condition, arg1, arg2, arg3) \ + __GUEST_ASSERT(_condition, #_condition, 3, (arg1), (arg2), (arg3)) + +#define GUEST_ASSERT_4(_condition, arg1, arg2, arg3, arg4) \ + __GUEST_ASSERT(_condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4)) + +#define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b) + +#endif /* SELFTEST_KVM_UCALL_COMMON_H */ From patchwork Fri Dec 10 16:46:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670187 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 B32E5C433EF for ; Fri, 10 Dec 2021 16:49:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237502AbhLJQwg (ORCPT ); Fri, 10 Dec 2021 11:52:36 -0500 Received: from mail-bn8nam12on2042.outbound.protection.outlook.com ([40.107.237.42]:11440 "EHLO NAM12-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S240511AbhLJQwb (ORCPT ); Fri, 10 Dec 2021 11:52:31 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bMO0F8awZsJ+thSBhfY1bDtLgYjZrbCHhII2KMZZ43TthcBag9ojQIWl/GLrYSFfMl92dl60E8NcM1PWcOWGG6CjROTRunLKpJJ7sNmfygOHmRcpp1Lt1hYhOdfOADLm2mxIQgZ8Dt5tQeXhBrkbuC9plzwX+34BgE9fZVQIa6TpRo15O6Fqr5naLwXMalnwME4FmJhhX3i5bfQBi5HxKKR4OjzR9ymOAjsAnGSlXcuDrKoTAtEI9gkgy1cm1b9eTO0YF0/xRV6ai5D2UYD+m2Jx9HWfZCnmFX++Sb0LdfptuC/fv+ZtdiHSI+3zNnvB02lDv9pgwb6u9SBuR/q2cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3/12okhrmrB4xuQZH192RDbXC9BoT/Vvu/pMTiY+30Y=; b=QZrtbnyxR26x7vVx24at5jQxc2qRzXMR/yIcGtUBqG+qQLASwbpfNAIc4Uw8Vqc26bnsWOgQ3cA5IOAPAPUqIT3Doaft4aG8sLy0sT4yTXb0K33PKTLoO9X5VGktFWYXAXZsbDlOw8pq6VOojjTtlzbI2d1BiyQb8XRU9IKsxeHHbj77QaI0ugNegvuMyMUN88Qd0ppv1We7NFFCIMZPKkZ1nsIffActAQ/+Doz+nhESsIV2+P1oBxSAT3C6oCNp/pTgjf6w6gEFNZMhFA4zrLhvN+iVLlj41jDRXMMZrOyQ+WpG64H3gLVYesSRoXyyZwKzm64FhCGrsNP/6EA2ww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3/12okhrmrB4xuQZH192RDbXC9BoT/Vvu/pMTiY+30Y=; b=GB4lBojFgrvFCU4umO5yvq5PmEIsELcf3WVmtaR2zWqbMa/0zu5v3/vaHha8qMjpx+tKYMUR4faNlQI2GB213WYQPbfMktlmGiwjsTk4nrMGt09t/nkJ0iXdQ1t1B2m/DNSRqpQL39HivaIrs1kYqptECDtFeuC2+fwzV27uBMg= Received: from DS7PR03CA0304.namprd03.prod.outlook.com (2603:10b6:8:2b::16) by DM5PR12MB1626.namprd12.prod.outlook.com (2603:10b6:4:d::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Fri, 10 Dec 2021 16:48:53 +0000 Received: from DM6NAM11FT055.eop-nam11.prod.protection.outlook.com (2603:10b6:8:2b:cafe::be) by DS7PR03CA0304.outlook.office365.com (2603:10b6:8:2b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12 via Frontend Transport; Fri, 10 Dec 2021 16:48:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT055.mail.protection.outlook.com (10.13.173.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:48:53 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:48:52 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 03/10] kvm: selftests: introduce ucall_ops for test/arch-specific ucall implementations Date: Fri, 10 Dec 2021 10:46:13 -0600 Message-ID: <20211210164620.11636-4-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 58d97c4d-9f90-4a7e-3916-08d9bbfcf308 X-MS-TrafficTypeDiagnostic: DM5PR12MB1626:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b/9kUOMpO0HEO5aE0Pvzp19pLHnOnsS1Z6mY5SEPWQVCBWkIo8LYcV7MlVI0Rvhfipm9TVeQjOX77bNfQwGn6JOEKSXvNIi4EADFKvXNrwFco0dil0ier5z6YUi3sWYXDhdwkt8AavsdfKnBdJqqii3v+Cq8L/Ue0a1sq+yCdC6N/xBiCZNiSz7u4O8Vwo5j3doEUvPaUMh2S8J6oeK0ulic29CJtTpygWSx3DuvQn+p7xPmy+Qz8EcZSpEbEufvf2vCYTkIlNfc9S7H9iZOD7m4ZehPAT3QMhmYgCSF8OuzdHji72fHkgJ6wqnU53K/lkYeBstJrQj2zyfy5RfTgVtu8tP09cXEizjGRNeWqlljTs0iWfPgiafYT51H1XIHVJLUU7yyy6snEy0rS624M/ziOGRUJ42mriEokGSj5qZW9GDP+wk6r1ZGcjAivAJXO5vTadiYBAvDqyX1LzlRFckaPvx6qXQg5knv9NQTbdv7KjgycejzysawdwlccToEm9LzCGWrjAZI/PcpS+I4WEwzbaoLFepgal0OZp3IBg1MGB0C8g+3Xttq7hM86ogZDR+ldnVJ3WP+Hm9BlQzqlXYIVvOw/Vr3STWMSLBfJgMXuVMRs4NCILwAD3bUXnWCtRJEetgSlOQjmNI/ytf+lvIRajNbVmllVTx8w27B1zXc7NPnfviuoylBB7W9Kl9pGgSRXwAJcH5Sig2tV8hFuYfkLN4OEVPk2rHJ4WfL7W/v8afJ2zayN/d8eNjM9La1GbM0ELAiKBnadFk8Op1s06Sfbonxkskbd/XFxbrPIZg= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(36840700001)(46966006)(40470700001)(7416002)(5660300002)(70206006)(426003)(6916009)(6666004)(26005)(83380400001)(4326008)(47076005)(336012)(86362001)(8936002)(81166007)(44832011)(2616005)(16526019)(36860700001)(356005)(82310400004)(54906003)(36756003)(508600001)(70586007)(316002)(40460700001)(8676002)(1076003)(2906002)(186003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:48:53.2253 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 58d97c4d-9f90-4a7e-3916-08d9bbfcf308 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT055.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1626 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org To support SEV, x86 tests will need an alternative to using PIO instructions to handle ucall-related functionality since ucall structs are currently allocated on the guest stack, which will generally be encrypted memory that can't be accessed by tests through the normal mechanisms (along with some other complications which will requires some new ucall interfaces as well). To prepare for this, introduce a ucall_ops struct and supporting interfaces that can be used to define multiple ucall implementations that can be selected on a per-test basis, and re-work the existing PIO-based ucall implementation to make use of these changes. Subsequent patches will do the same for other archs as well, and then extend this ops interface to address complications when dealing with encrypted/private guest memory. Signed-off-by: Michael Roth --- tools/testing/selftests/kvm/Makefile | 2 +- .../testing/selftests/kvm/include/kvm_util.h | 10 ++ .../selftests/kvm/include/ucall_common.h | 17 +++- .../selftests/kvm/include/x86_64/ucall.h | 18 ++++ .../testing/selftests/kvm/lib/ucall_common.c | 95 +++++++++++++++++++ .../testing/selftests/kvm/lib/x86_64/ucall.c | 46 ++++----- 6 files changed, 157 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86_64/ucall.h create mode 100644 tools/testing/selftests/kvm/lib/ucall_common.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index c4e34717826a..05bff4039890 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -34,7 +34,7 @@ ifeq ($(ARCH),s390) endif LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c -LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S +LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S lib/ucall_common.c LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index c9286811a4cb..2701bf98c0db 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -8,6 +8,16 @@ #define SELFTEST_KVM_UTIL_H #include "kvm_util_base.h" +/* + * TODO: ucall.h contains arch-specific declarations along with + * ucall_common.h. For now only a subset of archs provide the + * new header. Once all archs implement the new header the #include for + * ucall_common.h can be dropped. + */ +#ifdef __x86_64__ +#include "ucall.h" +#else #include "ucall_common.h" +#endif #endif /* SELFTEST_KVM_UTIL_H */ diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h index 9eecc9d40b79..fcd32607dcff 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -1,8 +1,12 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * tools/testing/selftests/kvm/include/kvm_util.h + * Common interfaces related to ucall support. + * + * A ucall is a hypercall to userspace. * * Copyright (C) 2018, Google LLC. + * Copyright (C) 2018, Red Hat, Inc. + * Copyright (C) 2021, Advanced Micro Devices, Inc. */ #ifndef SELFTEST_KVM_UCALL_COMMON_H #define SELFTEST_KVM_UCALL_COMMON_H @@ -14,6 +18,7 @@ enum { UCALL_ABORT, UCALL_DONE, UCALL_UNHANDLED, + UCALL_NOT_IMPLEMENTED, }; #define UCALL_MAX_ARGS 6 @@ -23,8 +28,18 @@ struct ucall { uint64_t args[UCALL_MAX_ARGS]; }; +struct ucall_ops { + const char *name; + void (*init)(struct kvm_vm *vm, void *arg); + void (*uninit)(struct kvm_vm *vm); + void (*send_cmd)(struct ucall *uc); + uint64_t (*recv_cmd)(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); +}; + void ucall_init(struct kvm_vm *vm, void *arg); void ucall_uninit(struct kvm_vm *vm); +void ucall_init_ops(struct kvm_vm *vm, void *arg, const struct ucall_ops *ops); +void ucall_uninit_ops(struct kvm_vm *vm); void ucall(uint64_t cmd, int nargs, ...); uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); diff --git a/tools/testing/selftests/kvm/include/x86_64/ucall.h b/tools/testing/selftests/kvm/include/x86_64/ucall.h new file mode 100644 index 000000000000..8366bdc9c04e --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/ucall.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Arch-specific ucall implementations. + * + * A ucall is a "hypercall to userspace". + * + * Copyright (C) 2021 Advanced Micro Devices + */ +#ifndef SELFTEST_KVM_UCALL_H +#define SELFTEST_KVM_UCALL_H + +#include "ucall_common.h" + +extern const struct ucall_ops ucall_ops_pio; + +extern const struct ucall_ops ucall_ops_default; + +#endif /* SELFTEST_KVM_UCALL_H */ diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c new file mode 100644 index 000000000000..db0129edcbc1 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Common interfaces related to ucall support. A ucall is a hypercall to + * userspace. + * + * Copyright (C) 2018, Red Hat, Inc. + * Copyright (C) 2021, Advanced Micro Devices, Inc. + */ +#include "kvm_util_base.h" +#include "ucall_common.h" + +extern const struct ucall_ops ucall_ops_default; + +/* Some archs rely on a default that is available even without ucall_init(). */ +#if defined(__x86_64__) || defined(__s390x__) +static const struct ucall_ops *ucall_ops = &ucall_ops_default; +#else +static const struct ucall_ops *ucall_ops; +#endif + +void ucall_init_ops(struct kvm_vm *vm, void *arg, const struct ucall_ops *ops) +{ + TEST_ASSERT(ops, "ucall ops must be specified"); + ucall_ops = ops; + sync_global_to_guest(vm, ucall_ops); + + if (ucall_ops->init) + ucall_ops->init(vm, arg); +} + +void ucall_init(struct kvm_vm *vm, void *arg) +{ + ucall_init_ops(vm, arg, &ucall_ops_default); +} + +void ucall_uninit_ops(struct kvm_vm *vm) +{ + if (ucall_ops && ucall_ops->uninit) + ucall_ops->uninit(vm); + + ucall_ops = NULL; + sync_global_to_guest(vm, ucall_ops); +} + +void ucall_uninit(struct kvm_vm *vm) +{ + ucall_uninit_ops(vm); +} + +static void ucall_process_args(struct ucall *uc, uint64_t cmd, int nargs, va_list va_args) +{ + int i; + + nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS; + uc->cmd = cmd; + + for (i = 0; i < nargs; ++i) + uc->args[i] = va_arg(va_args, uint64_t); +} + +/* + * Allocate/populate a ucall buffer from the guest's stack and then generate an + * exit to host userspace. ucall_ops->send_cmd should have some way of + * communicating the address of the ucall buffer to the host. + */ +void ucall(uint64_t cmd, int nargs, ...) +{ + struct ucall uc; + va_list va; + + if (!ucall_ops->send_cmd) + return; + + va_start(va, nargs); + ucall_process_args(&uc, cmd, nargs, va); + va_end(va); + + ucall_ops->send_cmd(&uc); +} + +/* + * Parse the ucall buffer allocated by the guest via ucall() to determine what + * ucall message/command was sent by the guest. If 'uc' is provided, copy the + * contents of the guest's ucall buffer into it. + */ +uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) +{ + if (!ucall_ops->recv_cmd) + return UCALL_NOT_IMPLEMENTED; + + if (uc) + memset(uc, 0, sizeof(*uc)); + + return ucall_ops->recv_cmd(vm, vcpu_id, uc); +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/ucall.c b/tools/testing/selftests/kvm/lib/x86_64/ucall.c index a3489973e290..f5d9aba0d803 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/ucall.c +++ b/tools/testing/selftests/kvm/lib/x86_64/ucall.c @@ -1,48 +1,28 @@ // SPDX-License-Identifier: GPL-2.0 /* - * ucall support. A ucall is a "hypercall to userspace". + * Arch-specific ucall implementations. + * + * A ucall is a "hypercall to userspace". * * Copyright (C) 2018, Red Hat, Inc. */ -#include "kvm_util.h" +#include "kvm_util_base.h" +#include "ucall.h" #define UCALL_PIO_PORT ((uint16_t)0x1000) -void ucall_init(struct kvm_vm *vm, void *arg) -{ -} - -void ucall_uninit(struct kvm_vm *vm) -{ -} - -void ucall(uint64_t cmd, int nargs, ...) +static void ucall_ops_pio_send_cmd(struct ucall *uc) { - struct ucall uc = { - .cmd = cmd, - }; - va_list va; - int i; - - nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS; - - va_start(va, nargs); - for (i = 0; i < nargs; ++i) - uc.args[i] = va_arg(va, uint64_t); - va_end(va); - asm volatile("in %[port], %%al" - : : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax", "memory"); + : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory"); } -uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) +static uint64_t ucall_ops_pio_recv_cmd(struct kvm_vm *vm, uint32_t vcpu_id, + struct ucall *uc) { struct kvm_run *run = vcpu_state(vm, vcpu_id); struct ucall ucall = {}; - if (uc) - memset(uc, 0, sizeof(*uc)); - if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) { struct kvm_regs regs; @@ -57,3 +37,11 @@ uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) return ucall.cmd; } + +const struct ucall_ops ucall_ops_pio = { + .name = "PIO", + .send_cmd = ucall_ops_pio_send_cmd, + .recv_cmd = ucall_ops_pio_recv_cmd, +}; + +const struct ucall_ops ucall_ops_default = ucall_ops_pio; From patchwork Fri Dec 10 16:46:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670189 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 F352CC433F5 for ; Fri, 10 Dec 2021 16:49:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240582AbhLJQxE (ORCPT ); Fri, 10 Dec 2021 11:53:04 -0500 Received: from mail-co1nam11on2047.outbound.protection.outlook.com ([40.107.220.47]:55264 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S240602AbhLJQxD (ORCPT ); Fri, 10 Dec 2021 11:53:03 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U3HgOkNgov8C1PVqHuhbgScCdJsuI/9/nXc8bjhzJGwb/7ARuJgsP99bqpT9kUfGVj/Z2ulinWUGVILhEI6QCK5Anld5AOwgzNKWJKAlXY9ZXcaGXlD7zX3TzWkTdjVIJDZJN9FN8DdA4I4Lt1b/Gjlwp/QfrMnJNWushdYKMSx/++Oedao08OWlyeUaS5vRkbfJ5I7qS8vPhfohq1XVHRiC4U/mTP91WNtruYuf0KKCxtOuq/CSKL6WGVIVGTkxVKNkl+8LkfAqNiF49KiPIBBT3Y9FXoI6BZX4lTUx6vhNLkgGY3dczxgRtKf5aOKKRO8aD144oly8IRQ0qxB5Gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PSmM4s+hqJTlbhoYmw6xwaMc+NBXHAQkNw3bSCMrj1Y=; b=JWoD61SIrHAdjH5N3GG3+GQipxm9Wj49qgPJiO+4I0Be9rjsKaCW+i6Bqy3DPrdWJBPzyTKZlAToVRjvu0m2SYU9DvsLwlWcCVCFz7/cBnDqM4WpGt/QZg7CoEjqUknTLxGDLrq3ZtXeMOr4loX+oWtW9tv3t1zFgag42KswUqD4g/i7FzIWNPzOqImYdGAvTEk5lGheLDKA8yGleGSV3UqVgPo1GdL5cs5Wt6PkY5m6269Sc4DKQPdGjPjA4xg19yPan6CanBaW1RbuKeI4rlkf6GGbLe+71H9P8Tav691yeaLYdlniSm2Hhu66++DvT8Mdui9vu+1yAdXdhoEseA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PSmM4s+hqJTlbhoYmw6xwaMc+NBXHAQkNw3bSCMrj1Y=; b=rpHf1+j0JHP+w39UmPOl8uWvECEE4boiUpw48bHiWr7NFdDgJuAueiWcfz+CSPlDQu9mcbervS30tp/ulIIGlFiBtheyd71gfvFJlaFcCVkWETfh8P5Asme2WjmOL8YWCy12W3UrbbqiEMoj3eGItLEMLEyLzQhrqvCrtEgqLOk= Received: from DM5PR21CA0006.namprd21.prod.outlook.com (2603:10b6:3:ac::16) by MWHPR12MB1277.namprd12.prod.outlook.com (2603:10b6:300:f::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Fri, 10 Dec 2021 16:49:22 +0000 Received: from DM6NAM11FT003.eop-nam11.prod.protection.outlook.com (2603:10b6:3:ac:cafe::29) by DM5PR21CA0006.outlook.office365.com (2603:10b6:3:ac::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4801.8 via Frontend Transport; Fri, 10 Dec 2021 16:49:22 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT003.mail.protection.outlook.com (10.13.173.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:49:22 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:49:13 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 04/10] kvm: arm64: selftests: use ucall_ops to define default ucall implementation Date: Fri, 10 Dec 2021 10:46:14 -0600 Message-ID: <20211210164620.11636-5-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b5ce928b-2e96-4ca1-8d77-08d9bbfd045b X-MS-TrafficTypeDiagnostic: MWHPR12MB1277:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cpZw4FezY8Pj0kOl2dow7P1rgRmDU62Gb9dZswx6yfcCRgIlteAInvooU5zRulzucxnqtImgvRN8nLRO5tTzLyM/nRYUAgd+rZXQHCcKKqQuRp5iRTAA2Cj5jfj+DdUedA6CZ6oKFMg32BJwjtU74vnkBIX5fv6gv4oNNcWzskv2AosY+2Pae3IumtGo3u7cTSJD821qFYMGxlTWoQo3hQaYW85LqaI1ZU4AWs+P1LIp1q6CGAc+FT1Ci6YTTv49g1UR1RgFTuzxrYZ4yiaCNrDpKe2TI8USz5Vd924LUhCnP/aZE5yMUf8QJvLX7cTgdcvpErSwRjYRWcoVmghaq577yNECJ374asMWl63eBxWsdzaaAHgga3P3Hp0LbAA2TtUfSNYoAZQzvcplpOrNRygJ8F4us9FgYW4PY8tvs0NIHwfgJGYtJK10ri7DhqGBR96p/yeI95RK2ynCvVgy/PGR8VUNHHRRAp1XAP1THiNpt5W2ZTemBuJK3TUNVanBHukaGdOnLJ8uyguM1iWT7lQ1U41pGjrido0x8l5KFVcRftwCTATzNL4gTYMx760X5/AQedbLViNU1m5+tEraLf1+x3aG5qRAklIQfFaTstm75eVXwglzjbSGNGx77tggld5nr+RlYvfIgbsisUyYz+VG6wPyBzK3uew0Z7+dsnQct1FoQoSMyrIATxlgKouelcKcOElivlcL0VcO/2GRMzj/RHBPK/r3hYd13Fqx+usM5BljfMWXRnq/dqYCJxurvqGr9uC9GgKphBnORQrjZUH40F7vbpOwFulWA5EuKy8= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(36840700001)(46966006)(40470700001)(2906002)(70586007)(316002)(70206006)(83380400001)(7416002)(40460700001)(82310400004)(8936002)(356005)(508600001)(81166007)(26005)(8676002)(86362001)(6916009)(36860700001)(47076005)(426003)(336012)(4326008)(44832011)(2616005)(5660300002)(16526019)(186003)(54906003)(1076003)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:49:22.2971 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b5ce928b-2e96-4ca1-8d77-08d9bbfd045b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT003.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1277 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org As with x86, switch the default ucall implementation to using the new ucall_ops infrastructure. With this change ucall_init/ucall_uninit are no longer arch-specific and can now be dropped in favor of the ones in ucall_common.c. Signed-off-by: Michael Roth --- tools/testing/selftests/kvm/Makefile | 2 +- .../selftests/kvm/include/aarch64/ucall.h | 18 ++++++++ .../testing/selftests/kvm/include/kvm_util.h | 2 +- .../testing/selftests/kvm/lib/aarch64/ucall.c | 43 +++++++++---------- 4 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/aarch64/ucall.h diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 05bff4039890..2d4299961d0e 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -35,7 +35,7 @@ endif LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S lib/ucall_common.c -LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c +LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c lib/ucall_common.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test diff --git a/tools/testing/selftests/kvm/include/aarch64/ucall.h b/tools/testing/selftests/kvm/include/aarch64/ucall.h new file mode 100644 index 000000000000..d3189d0a4d68 --- /dev/null +++ b/tools/testing/selftests/kvm/include/aarch64/ucall.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Arch-specific ucall implementations. + * + * A ucall is a "hypercall to userspace". + * + * Copyright (C) 2021 Advanced Micro Devices + */ +#ifndef SELFTEST_KVM_UCALL_H +#define SELFTEST_KVM_UCALL_H + +#include "ucall_common.h" + +extern const struct ucall_ops ucall_ops_mmio; + +extern const struct ucall_ops ucall_ops_default; + +#endif /* SELFTEST_KVM_UCALL_H */ diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 2701bf98c0db..16ec8c53cd81 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -14,7 +14,7 @@ * new header. Once all archs implement the new header the #include for * ucall_common.h can be dropped. */ -#ifdef __x86_64__ +#if defined(__x86_64__) || defined (__aarch64__) #include "ucall.h" #else #include "ucall_common.h" diff --git a/tools/testing/selftests/kvm/lib/aarch64/ucall.c b/tools/testing/selftests/kvm/lib/aarch64/ucall.c index e0b0164e9af8..ab052ab5d5de 100644 --- a/tools/testing/selftests/kvm/lib/aarch64/ucall.c +++ b/tools/testing/selftests/kvm/lib/aarch64/ucall.c @@ -1,11 +1,14 @@ // SPDX-License-Identifier: GPL-2.0 /* - * ucall support. A ucall is a "hypercall to userspace". + * Arch-specific ucall implementations. + * + * A ucall is a "hypercall to userspace". * * Copyright (C) 2018, Red Hat, Inc. */ -#include "kvm_util.h" +#include "kvm_util_base.h" #include "../kvm_util_internal.h" +#include "ucall.h" static vm_vaddr_t *ucall_exit_mmio_addr; @@ -22,7 +25,7 @@ static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa) return true; } -void ucall_init(struct kvm_vm *vm, void *arg) +static void ucall_ops_mmio_init(struct kvm_vm *vm, void *arg) { vm_paddr_t gpa, start, end, step, offset; unsigned int bits; @@ -65,38 +68,22 @@ void ucall_init(struct kvm_vm *vm, void *arg) TEST_FAIL("Can't find a ucall mmio address"); } -void ucall_uninit(struct kvm_vm *vm) +static void ucall_ops_mmio_uninit(struct kvm_vm *vm) { ucall_exit_mmio_addr = 0; sync_global_to_guest(vm, ucall_exit_mmio_addr); } -void ucall(uint64_t cmd, int nargs, ...) +static void ucall_ops_mmio_send_cmd(struct ucall *uc) { - struct ucall uc = { - .cmd = cmd, - }; - va_list va; - int i; - - nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS; - - va_start(va, nargs); - for (i = 0; i < nargs; ++i) - uc.args[i] = va_arg(va, uint64_t); - va_end(va); - - *ucall_exit_mmio_addr = (vm_vaddr_t)&uc; + *ucall_exit_mmio_addr = (vm_vaddr_t)uc; } -uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) +static uint64_t ucall_ops_mmio_recv_cmd(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) { struct kvm_run *run = vcpu_state(vm, vcpu_id); struct ucall ucall = {}; - if (uc) - memset(uc, 0, sizeof(*uc)); - if (run->exit_reason == KVM_EXIT_MMIO && run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) { vm_vaddr_t gva; @@ -113,3 +100,13 @@ uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) return ucall.cmd; } + +const struct ucall_ops ucall_ops_mmio = { + .name = "MMIO", + .init = ucall_ops_mmio_init, + .uninit = ucall_ops_mmio_uninit, + .send_cmd = ucall_ops_mmio_send_cmd, + .recv_cmd = ucall_ops_mmio_recv_cmd, +}; + +const struct ucall_ops ucall_ops_default = ucall_ops_mmio; From patchwork Fri Dec 10 16:46:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670191 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 56344C433EF for ; Fri, 10 Dec 2021 16:49:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240518AbhLJQxN (ORCPT ); Fri, 10 Dec 2021 11:53:13 -0500 Received: from mail-co1nam11on2049.outbound.protection.outlook.com ([40.107.220.49]:58817 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S244140AbhLJQxM (ORCPT ); Fri, 10 Dec 2021 11:53:12 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OWtUptNjSDoR/Dvw/7bLz/TPDlhvDzir5yHzZZ6DoSH88CG8A6HOlHHGmGCTu2WjotmQrg1EC2tZFCiV61qwU7qLJ2WWdqQMZO7kg81Yox419UOEab69+cnLh5I+RqvHPhZClu/f3hsusG70njRPiyIR7VjU9CsgudGmJ2D0u9q0q6s2oXQMIZCL9oBMVLEzEMsSkpcPVJ5KbkNoJadVvtt31NheneYHMgU+RL/JIo02+CFJYyjZtcR+NkxysA4aew4HlOZOuc2Ih4EZXRH9pSrHTjjdcWXFPmhAXwSbFx5+WcBpNiY9A+UQc54vjCMivIB7HCSJEEj1y4oR1DqEgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=W9nCFk6+rGgW5VR2SNapsFNkgbbgH+bQ5jnsnfkSEfQ=; b=gTwzUS+1nqnhyHYnR8yueVs8cAbNzMJoEKxvG7uwJYqXV1ZfLuF6D7WUFfER4cwugMyJfM2mzb1E7s1Rmyq4n+cokbzl+8Zr+pYI3iQ4cVCGk3RzKJ1FDpzLzgR27Y0jIiEG85ROslKGwUzhgTOzcEJNBA92+brZsc8/4s9BUiZhfNdhEg8jcVSZMwN3mt1QDN60k0gfiDU/pKadWCsnLdXblg8TSWx0dpz54J8H+aVGo4PLB5hBtCebCk3grqeeKTtdAjfHLnbVrEHUX7u900zZv0+PHg1DDr4h1RFnouCr6CbK0mm7UXfplHuFbrGaXxAhCwcEurJ2zoi5H5yAYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=W9nCFk6+rGgW5VR2SNapsFNkgbbgH+bQ5jnsnfkSEfQ=; b=CQr+VgQwTooi7eagyJLw9Ztld7H9Dxr97tkwWiLFHylbFNSYwoTiSBCSWU9av5KmgXmUa/pIsfPjXw0d8fnBSaqJLWxmRM4LTSJiHC2GR69Bld+3Sc3cFn84WvJZyAoYC0t2d/JxQHn5CJ2AYEdv2jdRswWHHeA9RiJpbkkwrUw= Received: from DM6PR01CA0005.prod.exchangelabs.com (2603:10b6:5:296::10) by BY5PR12MB4034.namprd12.prod.outlook.com (2603:10b6:a03:205::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.11; Fri, 10 Dec 2021 16:49:35 +0000 Received: from DM6NAM11FT040.eop-nam11.prod.protection.outlook.com (2603:10b6:5:296:cafe::b9) by DM6PR01CA0005.outlook.office365.com (2603:10b6:5:296::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:49:35 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT040.mail.protection.outlook.com (10.13.173.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:49:35 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:49:34 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 05/10] kvm: s390: selftests: use ucall_ops to define default ucall implementation Date: Fri, 10 Dec 2021 10:46:15 -0600 Message-ID: <20211210164620.11636-6-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 019c4edf-1e10-4762-5f04-08d9bbfd0c0a X-MS-TrafficTypeDiagnostic: BY5PR12MB4034:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6imi8g9jEf4u6gpR1UuQ2gyT/JDXZJfHiwC6yfWny3lxSgmbeUFJU80px5lHvQsO9zRZdvPykizduvVwGoM6PX+Wcg2/mnC9J81Fw/MqBVFCGLi5gN+SueG/SRb7K8J+4H0DvtNtFiwee2qQFf8Au7XSATF4Nj9L5jfnpjEovFBzlRX9Wfcee/sAqnzptfJNK9Vu9lDUyoATwW6fozjt4oN2FbQR39SGn1Scw3TCRKANtpp37ysYPqiSYw0nN+pimMdH1mUFvp9A6fxsBjKNvWDzEIcpg5sGO1k0EPLK/fhZLtj2fc35b7Tykbkd38VlM3c3z9ZYgcd63S8scV0mfOBYVUxIcSQ/Rc6HCDmFWF4bfVK7B0Dt7waOI3lLOtX+3xJcm5SnUWKaccUcgiqZvECDh+c2bBDVPQeboPya+4BBbbFlJM7sRgLYJ4SVznjWi/4u367UG+g07l2s0XnlDzTvoGR3rTWDBnt1QY1Jask3rvlGwRCI/ShfXEidK7mCw15PU67BF5qv3QwngbnWuX2N2NXPldHoYj6v3bTpXBQdVrRKBBHunX2AR24SPb+JMZCFkIjXdSc9vvG0feyHpDkQGFrSfGpAm1jQhMn3C41J+ce1J1bYTQLZVkC0Ay3yH3eW5tw/FmAvgrAAA9MBBQjR+4koQ6RwIqQ/xi3m0QoOpIRUqXxApl6FsuR4XYdliKZEPd9K6qPtV4VjlaEDdP636q0Y865U/FY4n+FEPDbfe2R8uPFp4i52GeVLk6NzfGJmy/HhmjNa9gqgX775ypgaM0rkzJk348zk532BiaE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(36840700001)(46966006)(40470700001)(316002)(82310400004)(356005)(508600001)(8936002)(2616005)(1076003)(40460700001)(2906002)(86362001)(5660300002)(4326008)(6916009)(54906003)(26005)(6666004)(186003)(81166007)(36756003)(16526019)(83380400001)(70206006)(336012)(36860700001)(47076005)(70586007)(7416002)(44832011)(426003)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:49:35.1908 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 019c4edf-1e10-4762-5f04-08d9bbfd0c0a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT040.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4034 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org As with x86, switch the default ucall implementation to using the new ucall_ops infrastructure. With this change ucall_init/ucall_uninit are no longer arch-specific and can now be dropped in favor of the ones in ucall_common.c. Signed-off-by: Michael Roth --- tools/testing/selftests/kvm/Makefile | 6 +-- .../testing/selftests/kvm/include/kvm_util.h | 10 ----- .../selftests/kvm/include/s390x/ucall.h | 18 ++++++++ tools/testing/selftests/kvm/lib/s390x/ucall.c | 45 +++++++------------ 4 files changed, 38 insertions(+), 41 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/s390x/ucall.h diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 2d4299961d0e..06a02b6fa907 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -33,9 +33,9 @@ ifeq ($(ARCH),s390) UNAME_M := s390x endif -LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c -LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S lib/ucall_common.c -LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c lib/ucall_common.c +LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c lib/ucall_common.c +LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S +LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 16ec8c53cd81..f6ec4fcd66d9 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -8,16 +8,6 @@ #define SELFTEST_KVM_UTIL_H #include "kvm_util_base.h" -/* - * TODO: ucall.h contains arch-specific declarations along with - * ucall_common.h. For now only a subset of archs provide the - * new header. Once all archs implement the new header the #include for - * ucall_common.h can be dropped. - */ -#if defined(__x86_64__) || defined (__aarch64__) #include "ucall.h" -#else -#include "ucall_common.h" -#endif #endif /* SELFTEST_KVM_UTIL_H */ diff --git a/tools/testing/selftests/kvm/include/s390x/ucall.h b/tools/testing/selftests/kvm/include/s390x/ucall.h new file mode 100644 index 000000000000..6ba8bc77d667 --- /dev/null +++ b/tools/testing/selftests/kvm/include/s390x/ucall.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Arch-specific ucall implementations. + * + * A ucall is a "hypercall to userspace". + * + * Copyright (C) 2021 Advanced Micro Devices + */ +#ifndef SELFTEST_KVM_UCALL_H +#define SELFTEST_KVM_UCALL_H + +#include "ucall_common.h" + +extern const struct ucall_ops ucall_ops_diag501; + +extern const struct ucall_ops ucall_ops_default; + +#endif /* SELFTEST_KVM_UCALL_H */ diff --git a/tools/testing/selftests/kvm/lib/s390x/ucall.c b/tools/testing/selftests/kvm/lib/s390x/ucall.c index 9d3b0f15249a..67f1baa42b28 100644 --- a/tools/testing/selftests/kvm/lib/s390x/ucall.c +++ b/tools/testing/selftests/kvm/lib/s390x/ucall.c @@ -1,46 +1,27 @@ // SPDX-License-Identifier: GPL-2.0 /* - * ucall support. A ucall is a "hypercall to userspace". + * Arch-specific ucall implementations. + * + * A ucall is a "hypercall to userspace". * * Copyright (C) 2019 Red Hat, Inc. */ -#include "kvm_util.h" - -void ucall_init(struct kvm_vm *vm, void *arg) -{ -} - -void ucall_uninit(struct kvm_vm *vm) -{ -} +#include "kvm_util_base.h" +#include "ucall.h" -void ucall(uint64_t cmd, int nargs, ...) +static void +ucall_ops_diag501_send_cmd(struct ucall *uc) { - struct ucall uc = { - .cmd = cmd, - }; - va_list va; - int i; - - nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS; - - va_start(va, nargs); - for (i = 0; i < nargs; ++i) - uc.args[i] = va_arg(va, uint64_t); - va_end(va); - /* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */ asm volatile ("diag 0,%0,0x501" : : "a"(&uc) : "memory"); } -uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) +static uint64_t +ucall_ops_diag501_recv_cmd(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) { struct kvm_run *run = vcpu_state(vm, vcpu_id); struct ucall ucall = {}; - if (uc) - memset(uc, 0, sizeof(*uc)); - if (run->exit_reason == KVM_EXIT_S390_SIEIC && run->s390_sieic.icptcode == 4 && (run->s390_sieic.ipa >> 8) == 0x83 && /* 0x83 means DIAGNOSE */ @@ -57,3 +38,11 @@ uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) return ucall.cmd; } + +const struct ucall_ops ucall_ops_diag501 = { + .name = "diag501", + .send_cmd = ucall_ops_diag501_send_cmd, + .recv_cmd = ucall_ops_diag501_recv_cmd, +}; + +const struct ucall_ops ucall_ops_default = ucall_ops_diag501; From patchwork Fri Dec 10 16:46:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670193 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 ECC91C433F5 for ; Fri, 10 Dec 2021 16:50:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240607AbhLJQxf (ORCPT ); Fri, 10 Dec 2021 11:53:35 -0500 Received: from mail-sn1anam02on2047.outbound.protection.outlook.com ([40.107.96.47]:3522 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S233753AbhLJQxe (ORCPT ); Fri, 10 Dec 2021 11:53:34 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YwZC4ZyghQfMnB+vIHQTPjXlGebjgwNT3dAqJju0LIFo3bOtZgHuabMhymfv+H1CKpWQcyjO5+xyBU3/4zGsKgCHU+2AnOcylJCzy4Gl9hVx04TwMzrJXCsek8khJohy1cgzvcW7levimqCEFquPCqqR8CS9zVTQP8w3PsL9ZT2K3QCb3UTsqwPtTfLgqHOsV0yzsmmdw/Ao6uMO3a1cw6KnT+hjeo7I9mkgmkpUbVAIts5H3aNEtGSdH8/TyZtwqclhicrnrafKb6qelzxqk55TP6UK4IEbExcfK8MUXmj4gRW185/2Lubhuako+W1svTQLN5R8TqkPW5Oww2gclQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=meSvBmDO/cMIhCV6ZtoghB6ppaUXrxPV/DFc5npjgD4=; b=BWhBU4h3JUs4TNyxREtieVaK3D8AJmZI5xlfUZtD5sHMZIoNGqBzME7D+K2gvJvs4Zr0TshKwHkyOOU5WiPOTFkU2c2yoWEmzlqxZgPdqG6n84ktnWkvkzhVyPW8lVLzFfGwY0oINb6gz3NPpJZkgALeZXTcSysGqTRXaSO/Z14h9oJXv5WzPnoOySOAoiIWyYMZg1bOa+ZDGzYGMtilqyAUqYz3MRgDU/hYcaSspGAGZwfXbEr1feyAnrMjX8tvVWQ0qzoJJSeSA9WpXV5FIOxlAV87AHkpAdFkldyrQjp5GTBxBkccDXg9Zdt2t2H0TrarbGmTn3DQx74FFdbVOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=meSvBmDO/cMIhCV6ZtoghB6ppaUXrxPV/DFc5npjgD4=; b=KKZgg/tbSbV5k0dlPIIZq+BLQfuzEBmjqTujIy2Cq2azx/EtrhvLC+j8j9+zd0INs1wVeY2LAffCImvBJTbYhuP6d8jr5PiYAxH9/0AFf8ThpVbwQkO5Vn5qbiu8Pb2Ybr9++waFsTZpZf0vEKXmc9BS0NsfbzxB02OKQcl4cmM= Received: from DM6PR03CA0079.namprd03.prod.outlook.com (2603:10b6:5:333::12) by CY4PR12MB1877.namprd12.prod.outlook.com (2603:10b6:903:11c::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Fri, 10 Dec 2021 16:49:56 +0000 Received: from DM6NAM11FT057.eop-nam11.prod.protection.outlook.com (2603:10b6:5:333:cafe::ae) by DM6PR03CA0079.outlook.office365.com (2603:10b6:5:333::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.20 via Frontend Transport; Fri, 10 Dec 2021 16:49:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT057.mail.protection.outlook.com (10.13.172.252) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:49:56 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:49:55 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 06/10] kvm: selftests: add ucall interfaces based around shared memory Date: Fri, 10 Dec 2021 10:46:16 -0600 Message-ID: <20211210164620.11636-7-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5db43f4d-63ab-43e7-ee79-08d9bbfd18a1 X-MS-TrafficTypeDiagnostic: CY4PR12MB1877:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: k575Uzq4pqvkXNUZZgcD5KX9WU8bmVFmdy44pVDXAL7wTCAtJk2sL9Ct5dh2FHnqcklXn4ZY0eOkrCe/R9V7aLo/k+L0fyGmmQgnNsoX5GNa4gWJqPLn/ND8atfNNON0TiJKuk9swtlDV/PLW8VEMYSUDB+t6OrMMRBdfNkFvN8Ac/04eh1N3GMkI2u6xBoKAzVQKxmZ2T7abDua/b29DPgNFu+iG8oLgXkcRq8hw0qOXh7JN8ppYbInARY3loGOwh7BEC6drUSoBie3dgyPY3+72Y7CHuEqpxI5eegVDFOcEhT4pukhC49rxm2Kka1PjyYi82iNnizoaDQl11pJOT3z1dWZP0/GtMt6hjCD08uvlSKsxp2MzGhzm/wvkYbyuJiyTgwOy4clgUgIl4vsHUVbL37wLVvFoWWcNhA+KPAhtjyFRRhIa8KgpT3zzCn9Oidk0icPuMjqCWvqbVxqcGXxe5HykJYCYZPIUnUCjbV/zOwLALNg0WvVZeo/a1mNHNk+IWr7j7iRjvoXSwE+Ske+OYZDcyhtbY4VxOCGYu+1cKIwW2/rJlJ/u+eH6o6FxtHPEXd7rrjhzzu6mPsmSyPLExX4g5svdqUDykdPsyo6iE+TcX7yxJpxQZeAkPU0i4dqEwbYNhanuAVctbEMyfyr8KNOmNogyqQ0MBR5JO75YoPOXgbgsUysgrR5WitAwDqx7BCrx6S89mWNhXvIXVl2YIqfFUpYNtAeGCsGp86y/obrY3A+ur1endP1RdqPYT5Hg3u5fnue9BU+YNVNGoMhi0uR+wjH/Op7/C/FdQY= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(186003)(16526019)(70206006)(70586007)(8676002)(54906003)(6666004)(83380400001)(356005)(316002)(4326008)(26005)(81166007)(2906002)(5660300002)(86362001)(8936002)(6916009)(44832011)(7416002)(40460700001)(82310400004)(47076005)(36860700001)(36756003)(2616005)(336012)(426003)(1076003)(508600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:49:56.2927 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5db43f4d-63ab-43e7-ee79-08d9bbfd18a1 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT057.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1877 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org One complication with SEV and likely other Confidential Computing implementations is the fact that guest stacks might be encrypted, so existing interfaces like ucall()/get_ucall() will not work as-is since they allocate ucall structs on the guest stack dynamically. Additionally, the basic task of communicating the location in guest memory of these structs is complicated by the fact that guest register state may also be also encrypted, so existing approaches like reading vCPU register values to get these addresses would need to take this into consideration. One way to address both of these in a (hopefully) robust way is to introduce a new set of ucall interfaces that rely on tests setting up shared guest memory in advance so that these ucall struct addresses are communicated between host/guest in advance, along with any work needed to ensure the memory is shared/public. With that in place, a ucall implementation only needs to trigger an exit back to host userspace to allow for host/guest communication via this shared memory / ucall struct. Implement this approach by extending ucall_ops to allow for ucall implementations based on shared memory, and introducing ucall_shared()/get_ucall_shared() analogs to the existing ucall()/get_ucall() interfaces. Signed-off-by: Michael Roth --- .../selftests/kvm/include/ucall_common.h | 5 +++ .../testing/selftests/kvm/lib/ucall_common.c | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h index fcd32607dcff..ae0e8eec9734 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -34,6 +34,8 @@ struct ucall_ops { void (*uninit)(struct kvm_vm *vm); void (*send_cmd)(struct ucall *uc); uint64_t (*recv_cmd)(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); + void (*send_cmd_shared)(struct ucall *uc); + uint64_t (*recv_cmd_shared)(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); }; void ucall_init(struct kvm_vm *vm, void *arg); @@ -42,6 +44,9 @@ void ucall_init_ops(struct kvm_vm *vm, void *arg, const struct ucall_ops *ops); void ucall_uninit_ops(struct kvm_vm *vm); void ucall(uint64_t cmd, int nargs, ...); uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); +vm_vaddr_t ucall_shared_alloc(struct kvm_vm *vm, int count); +void ucall_shared(struct ucall *uc, uint64_t cmd, int nargs, ...); +uint64_t get_ucall_shared(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index db0129edcbc1..8e5738241a7c 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -93,3 +93,41 @@ uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) return ucall_ops->recv_cmd(vm, vcpu_id, uc); } + +/* Allocate shared memory within a guest to for a shared ucall buffer. */ +vm_vaddr_t ucall_shared_alloc(struct kvm_vm *vm, int count) +{ + return vm_vaddr_alloc(vm, count * sizeof(struct ucall), + vm_get_page_size(vm)); +} + +/* + * Populate a shared ucall buffer previously allocated by ucall_shared_alloc() + * and then generate an exit to host userspace. + */ +void ucall_shared(struct ucall *uc, uint64_t cmd, int nargs, ...) +{ + va_list va; + + if (!ucall_ops->send_cmd_shared) + return; + + va_start(va, nargs); + ucall_process_args(uc, cmd, nargs, va); + va_end(va); + + ucall_ops->send_cmd_shared(uc); +} + +/* + * Parse a ucall buffer that has been allocated by ucall_shared_alloc() and + * shared with the guest in advance to determine the ucall message/command that + * was sent by the guest. + */ +uint64_t get_ucall_shared(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) +{ + if (!ucall_ops->recv_cmd_shared) + return UCALL_NOT_IMPLEMENTED; + + return ucall_ops->recv_cmd_shared(vm, vcpu_id, uc); +} From patchwork Fri Dec 10 16:46:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670195 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 E9194C433F5 for ; Fri, 10 Dec 2021 16:50:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233753AbhLJQyB (ORCPT ); Fri, 10 Dec 2021 11:54:01 -0500 Received: from mail-dm6nam12on2086.outbound.protection.outlook.com ([40.107.243.86]:32992 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S244133AbhLJQyA (ORCPT ); Fri, 10 Dec 2021 11:54:00 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=du3/ZEWx+ZIB9gl2P4p9Qs8bYlBeF+cCFudFv7grftAWkVp7j1GcY1ymg/y9J3ubwpPH1DrtOAQtd0sW3ifZ7GdG3xgb76Wdk8DfCw9Lb3a0k3UY2KQ5+fTcrQ6XFroWCHwlQoZyXmf0lk93m/qCecxxEqAWi+IBCx9rVlW3cq2COVBqsh3FyNNJW7by0Y76SiHCGcjq0zo7fIN8y4W8Zz6JmEUSxhdxBpRcxfMs9x9N3X+invAW+C2FDTXoROSH3lDtDqbE7khpyanlu/E6XyMn59M3VETn4FO4GICqno2DAnqCvqbGE8utTveAiOKyq+pjgUD+tvtRANy76FPUHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=V4l9G+qjt85QqPOsGsYkXeCESx2XZ8ap5AIE6PkjoMM=; b=QaNZNn42mPRDT/O8Czz1W2s8D8sKx5mR9mz/MbwWTr8sP61AgrVRB5Xn8MwUSPS1zTfnsoTX9hG1GobcqIyWuP2gFHtVT6GA3p30R7LM1Vd9ox+52NLGnuTZ6njfkvV96kouZiU4do7TZpB8yfA8Ua2tDwdD+7YUag75cVK9TEe+u5AG06WW2OMI+oChn7y0xw6ksPTRWqYWA/7dGPitol41Rra2mpzP6okj33w4F6XPPx6AJySSpzw6cyvUm2SxrQGqTCRo5F1ox0bi4G40O8BxCK76kvox+XRP0I8I99RQz+phhHkF+ozY2lpMdsMnObX0mA9bzB0gx0IqJnG6JQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=V4l9G+qjt85QqPOsGsYkXeCESx2XZ8ap5AIE6PkjoMM=; b=EMVb7M3rLeOAfnpq+EpzPouYcj76PDoDgsGWTnmdE5HMGRsvmAegT2V78j+zHgD44ufXwMzpl1F3mbY9UnrH7tQZ7Lzffp2JjRf9ZIGCZzlbTUuI91f5ScWiU5pFgYMYHdkyKoZYt/+LcbqueihNqPekiAOpVAJcasmQ901tkYg= Received: from DM5PR2201CA0004.namprd22.prod.outlook.com (2603:10b6:4:14::14) by BYAPR12MB3272.namprd12.prod.outlook.com (2603:10b6:a03:131::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.11; Fri, 10 Dec 2021 16:50:17 +0000 Received: from DM6NAM11FT049.eop-nam11.prod.protection.outlook.com (2603:10b6:4:14:cafe::df) by DM5PR2201CA0004.outlook.office365.com (2603:10b6:4:14::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.11 via Frontend Transport; Fri, 10 Dec 2021 16:50:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT049.mail.protection.outlook.com (10.13.172.188) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:50:17 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:50:16 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 07/10] kvm: selftests: add ucall_shared ops for PIO Date: Fri, 10 Dec 2021 10:46:17 -0600 Message-ID: <20211210164620.11636-8-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: be49715d-8936-4960-5614-08d9bbfd2519 X-MS-TrafficTypeDiagnostic: BYAPR12MB3272:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: v7W7CFHDhxDzCG0z9KczymrXGCAFjQOmQatbPWGL4gwXbP1t1ulg36/Esmld4o4VWw/vDFAhgFr2oYsEwqXHbZz+HoJCfdam5Elq27dSfNFfLMgaIMRJQA/zme4Q00FezwUu+xWfAWs4m/7xQ4dIexC3OqMkK37rAAsY8O32+Ie5UHPSJg2sQd8MizFZrlf/KCn2nuj3rhaxNCsRTZ+V9gwkYuzRNhtRM4mf+96W940sbQffMW3qJs5X4SRCPrO29vkF3YFxD8Cn6x24tEPTpqXlKdBGzHG6UN8tl+v3rBrZPffwWuM80329rf16fjYDvOg0fQrbHlNTCRUE4C4Nuiq9FWBZx44gdJYJOeDT0WqhmFfYpH3UCbd2VRNm6yq9PfqNDjGlyfBFWD+MmN3RktR3ibfVtH9i6JChnRQVSiNzwvV/g238Sh5gTzvSqFgSpwUYaME1vK32yLu3yu66YSp2xvizwlyQOTCs8bSgbaziBNSEb/4GDr3C/p3ko72PFwIedKK1+r+VW3x3VFVFSeJii7uYiqy2vjq3L7Wdt9ARsiUILVGlmOiBkJi0/LbwJLKpt0vJ//QkZT0lPTB9Y75JZyInnEoonigPc7ONXvdb4/qyxl2xnOXVQ0oI3Tw3XWqxz9sxlluexneo7fjU1eDieg8zTi9tWLnxnMIvThH8yCDAP9L/kIyzcR1cOHBuq+CuF9B2GuxdGnzx7tgbrspAoFCAHXzWLRMApyLxoJPE/eMCuzeUj9C+863itmgJNVFJyJRe8l7DOJbjp+P89zsa3A6Yqy4cgkO6XfaXPCw= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(5660300002)(36756003)(336012)(316002)(356005)(81166007)(40460700001)(1076003)(44832011)(8676002)(2906002)(54906003)(82310400004)(86362001)(8936002)(16526019)(6916009)(70586007)(186003)(36860700001)(4326008)(26005)(2616005)(508600001)(426003)(7416002)(70206006)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:50:17.2392 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: be49715d-8936-4960-5614-08d9bbfd2519 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT049.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB3272 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org While the PIO-based ucall implementation won't actually be ideal for SEV guests due to the requirements/constraints on how the guest code would needed to handle resulting #VC exceptions generated by PIO instructions, it does provide a simple way to write/convert tests using the new ucall_shared() interfaces so that they can be more easily transitioned to handle running as confidential guests in the future by selecting a different ucall_ops implementation. Signed-off-by: Michael Roth --- tools/testing/selftests/kvm/lib/x86_64/ucall.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/testing/selftests/kvm/lib/x86_64/ucall.c b/tools/testing/selftests/kvm/lib/x86_64/ucall.c index f5d9aba0d803..157d2a102547 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/ucall.c +++ b/tools/testing/selftests/kvm/lib/x86_64/ucall.c @@ -38,10 +38,23 @@ static uint64_t ucall_ops_pio_recv_cmd(struct kvm_vm *vm, uint32_t vcpu_id, return ucall.cmd; } +static uint64_t ucall_ops_pio_recv_cmd_shared(struct kvm_vm *vm, uint32_t vcpu_id, + struct ucall *uc) +{ + struct kvm_run *run = vcpu_state(vm, vcpu_id); + + if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) + vcpu_run_complete_io(vm, vcpu_id); + + return uc->cmd; +} + const struct ucall_ops ucall_ops_pio = { .name = "PIO", .send_cmd = ucall_ops_pio_send_cmd, .recv_cmd = ucall_ops_pio_recv_cmd, + .send_cmd_shared = ucall_ops_pio_send_cmd, + .recv_cmd_shared = ucall_ops_pio_recv_cmd_shared, }; const struct ucall_ops ucall_ops_default = ucall_ops_pio; From patchwork Fri Dec 10 16:46:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670197 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 8BBF3C433F5 for ; Fri, 10 Dec 2021 16:50:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240790AbhLJQyS (ORCPT ); Fri, 10 Dec 2021 11:54:18 -0500 Received: from mail-bn8nam12on2075.outbound.protection.outlook.com ([40.107.237.75]:5313 "EHLO NAM12-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234977AbhLJQyR (ORCPT ); Fri, 10 Dec 2021 11:54:17 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Pcc0qPPnFHNYGo0RFNpPROJ9c59/biHwO2CgTKLr/6NWofnq/jAbLK860zckQUCNggaxk1cUkHBaD7qqi3neLE0VwPxvnnxkbevaEUpBIjyDzTNrS1WgKvjzVeVD5DIODMtOPtECBAeoMXw9tXz6NKB5c9vDRv/6IILO8KlGHOB+88SSDQ7+IkZK2hER68dutVPGtN92zfDBUnwtauzOBikD0ESnD5sEm3LyYkeUKuAUIFClTd4O41GyWv24QwcgHsljQ7uv2ySTXMqAQVW2lhEgaaJQo+RwjlCFPGe9el69OOyI3kx0HKPooPHW9PEGIkJGvLDo0l839iZNJ8Zqxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rggfAyljN28bFCpAoefDdna3xuZYIfL7ltTJS7WGYQE=; b=NCKd8pBCDQL/XrBLoyWnUUAhw6JTXJX0+a/Ct177NSpjBKdmdqiUYxq75kDZk6XCh7SkimQ5xqh6uxRxPy2wDIzPJhsK+vEjs2AQYT9qf2XfOd3bVXCXK9+v2ILk4h0bE5856OQP9TnD+Z3qFPzLwibJYQQOcnKbC2W/CDEx5QA46PnZm5nav7ovC/0LEiqdkFIDKv4z5Q4PFEYATzzBYwr+gsSwNrHKKN6kMyxBhG81Xnr7EfN096cXy4/fE3keY/M8Wtc0rYZaU5uxL5hFX9C/BTCrIzHZW5oc447r/dIe9ej2P0wiTAo0nRl2aMduBAMA1yDg7z4AlSYyRk5ZqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=rggfAyljN28bFCpAoefDdna3xuZYIfL7ltTJS7WGYQE=; b=UJgC6JypO8/erQKlpJJA8xCGENbSlNE7gKjFYTuyWsM1WBPyu77DGn8HkAb7MEKS0LRZF93JY1Lt6W2NGLTRKREOdM7gjgnkpFBFLz/EHokcarW073g6QyjOrob3VyGItIPZCO01jxlLpaHNYOBP4psvQTDeEtb+q3TZd8EymLQ= Received: from DM5PR21CA0007.namprd21.prod.outlook.com (2603:10b6:3:ac::17) by MWHPR12MB1485.namprd12.prod.outlook.com (2603:10b6:301:4::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.13; Fri, 10 Dec 2021 16:50:38 +0000 Received: from DM6NAM11FT003.eop-nam11.prod.protection.outlook.com (2603:10b6:3:ac:cafe::c0) by DM5PR21CA0007.outlook.office365.com (2603:10b6:3:ac::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4801.7 via Frontend Transport; Fri, 10 Dec 2021 16:50:38 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT003.mail.protection.outlook.com (10.13.173.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:50:38 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:50:37 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 08/10] kvm: selftests: introduce ucall implementation based on halt instructions Date: Fri, 10 Dec 2021 10:46:18 -0600 Message-ID: <20211210164620.11636-9-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5b3f90c2-7619-47a2-1dc0-08d9bbfd31ac X-MS-TrafficTypeDiagnostic: MWHPR12MB1485:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vNYjccCML4ZqmPid9lPWaSyu1GJ6fxsD+/5Eh3WZpWCuPzTOsh4Ulj9KaLRc8HkYAdyFKl/oAxi/TeNiBv6XafX8WsCjX4U0XWd0q0baPskPFzJDlqvr7rxgvFLJU+DvGkf0jRbobH01dT6S84yGbghfBLX7FVk+Ts9LAC8Z6BkbtqItZX8EuhtGJ/MVJvMPTP720KyXEJBuZuRfKqYavv65l/bxeZV+7jersnmJKwcYf7sLcmAhIcz6yZ/SQ7NX6yXC9X4NQbpisV8I/7brEpRwo+ru/2Y+SgdI+9tYzfF2R0y6F0NoIrtjTn70rfQmdCDIogqFBCAX4L5oE+SNLBdESMfkqs9C/96iyugOI6w5jWH2FnQMAJVeifwTFVQtXxuSAjlvf15zOWWhlCshvtIQjaBR0bRiEjEVHCBoQq7zLN+Q9bRYq9kY/zfoFC3pCMsm6FMfYAhNzQ0x2AujdStP2Of8cvegL5G5XvFvVrgVi6gxZ2gJUuXeoU6TOkBNW0BCGWxewdUAPR1lzeqEm+xWRnh8SRkaQ9cXWrOF1+LBx1DSpeCD1NmUivw4wTyvnZgRdl2NSAMuWmSrOFEJf8anD1mLgjP1+bbbllcSiEH9qis23vTlCvaYTIj4JdiWZOMA6SHRn5s1epz6tJPipakaTj/up1K7uRiFTkh/7Nkjglfn0pRgDAQdihOHd458r28FuAXvx11K69pW2/dtMX5MNjhiSVh2xpvhSNm1gQ+ub4Un/W3YlD/91+pfaUW0mscjxmfP6yJVdDzIRkfqhK2EPML2KmUFqMtR3YqBlZ0= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(44832011)(86362001)(26005)(2616005)(8676002)(16526019)(316002)(186003)(5660300002)(1076003)(508600001)(54906003)(6666004)(336012)(426003)(4326008)(356005)(70586007)(70206006)(2906002)(47076005)(81166007)(36860700001)(36756003)(7416002)(82310400004)(6916009)(40460700001)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:50:38.3280 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5b3f90c2-7619-47a2-1dc0-08d9bbfd31ac X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT003.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1485 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org With the shared ucall interfaces the only thing the ucall implementation really needs to do is generate an exit to host userspace so that the shared ucall struct can be examined. This implementation uses a 'halt' instruction to generate such an exit, and is suitable for use with SEV guests, and potentially other confidential guest implementations. Signed-off-by: Michael Roth --- .../selftests/kvm/include/x86_64/ucall.h | 1 + .../testing/selftests/kvm/lib/x86_64/ucall.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/ucall.h b/tools/testing/selftests/kvm/include/x86_64/ucall.h index 8366bdc9c04e..457fc1406746 100644 --- a/tools/testing/selftests/kvm/include/x86_64/ucall.h +++ b/tools/testing/selftests/kvm/include/x86_64/ucall.h @@ -12,6 +12,7 @@ #include "ucall_common.h" extern const struct ucall_ops ucall_ops_pio; +extern const struct ucall_ops ucall_ops_halt; extern const struct ucall_ops ucall_ops_default; diff --git a/tools/testing/selftests/kvm/lib/x86_64/ucall.c b/tools/testing/selftests/kvm/lib/x86_64/ucall.c index 157d2a102547..4dfb12881434 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/ucall.c +++ b/tools/testing/selftests/kvm/lib/x86_64/ucall.c @@ -57,4 +57,27 @@ const struct ucall_ops ucall_ops_pio = { .recv_cmd_shared = ucall_ops_pio_recv_cmd_shared, }; +static void ucall_ops_halt_send_cmd_shared(struct ucall *uc) +{ + asm volatile("hlt" : : : "memory"); +} + +static uint64_t ucall_ops_halt_recv_cmd_shared(struct kvm_vm *vm, uint32_t vcpu_id, + struct ucall *uc) +{ + struct kvm_run *run = vcpu_state(vm, vcpu_id); + + TEST_ASSERT(run->exit_reason == KVM_EXIT_HLT, + "unexpected exit reason: %u (%s)", + run->exit_reason, exit_reason_str(run->exit_reason)); + + return uc->cmd; +} + +const struct ucall_ops ucall_ops_halt = { + .name = "halt", + .send_cmd_shared = ucall_ops_halt_send_cmd_shared, + .recv_cmd_shared = ucall_ops_halt_recv_cmd_shared, +}; + const struct ucall_ops ucall_ops_default = ucall_ops_pio; From patchwork Fri Dec 10 16:46:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670163 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 72C94C433EF for ; Fri, 10 Dec 2021 16:47:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240638AbhLJQur (ORCPT ); Fri, 10 Dec 2021 11:50:47 -0500 Received: from mail-dm6nam11on2066.outbound.protection.outlook.com ([40.107.223.66]:4192 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S243893AbhLJQuq (ORCPT ); Fri, 10 Dec 2021 11:50:46 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JarbS9qgcRge4szO27zP6B/HrlJ2ziphX4nJlfKbN+9sWqEv8rMa9K4OO66PrBjc2SJi7Ylq4BygYO07gSftFHA8J0xRxJhRhh0Xnm03yuRb+rFCT/a/cpVRs5P6RZG856cMpUD6G7XBUkqBF3yQxexQfZIQ2I++p45fB5x3djT614gi8nAFYeWRSigk15RnZ+IY31jtGZq3wKahLCS8JCLN/x9/B+tGMwDWgTlxTyoEfvMHy7QbKYl6OesgX2lVVA2eA03+ExvUL1NYr1mwSCmYeszga3zGzVFP/AzRFABpvhQ8VHYttmSrraXrlERh0v95gEat6NSX4LgzvjJeBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wFQMga+ozr4yriDnVn5+bwhC0iqbIN5kaimzahgvsgI=; b=YUd7Q1iROxpbZJz3HkemFNiNJq+xi8ByP/BDVleos7zjhWog2WzqIJpq0qvbvRgLLlu6+k6sdzN5Trw5a/5VL8Axgrs3/UG/xGlBkds0tDQSu66uVhN0Qvjc0iv06CpepxN1Z3oRK7nlr9kCTJMzBixWzEAfEaSBDmjyPitePVd3AqXYS6V4dh4bVIO26gE3uM6Ahina603g5lkFXFmg98zY/UhhtJ2+K3Z8FEZ5TREgJysHDB0b2RmlLCVyJrfSHzSpXOtPByAlmlU4aKVx1nqI8VjIubDg9oP54BrgNvFqRjBfl9qVBzAXFFeMHE6X1VXpdh/UV4hkMihOcwrYNA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wFQMga+ozr4yriDnVn5+bwhC0iqbIN5kaimzahgvsgI=; b=ESBDpsfcNbOUM1BUKfniC5H6cJXBlPJH6y6LduJ0dQlFxzE4E3N/Z/bHe70ob2JsNEUkr87CALLc/Z90ziaa1ZLrizANzWfKrVbTyF9KARlV0sQpEfEQbZv4AIeBlMsfmy0wyLvpmqSGkwHErY07B0VlEkCFiZGs4E27eekmf94= Received: from DM6PR02CA0078.namprd02.prod.outlook.com (2603:10b6:5:1f4::19) by SN1PR12MB2352.namprd12.prod.outlook.com (2603:10b6:802:25::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12; Fri, 10 Dec 2021 16:47:08 +0000 Received: from DM6NAM11FT042.eop-nam11.prod.protection.outlook.com (2603:10b6:5:1f4:cafe::cc) by DM6PR02CA0078.outlook.office365.com (2603:10b6:5:1f4::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.11 via Frontend Transport; Fri, 10 Dec 2021 16:47:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT042.mail.protection.outlook.com (10.13.173.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:47:08 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:47:07 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 09/10] kvm: selftests: add GUEST_SHARED_* macros for shared ucall implementations Date: Fri, 10 Dec 2021 10:46:19 -0600 Message-ID: <20211210164620.11636-10-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b25bd00d-6fb9-44e6-01b0-08d9bbfcb45c X-MS-TrafficTypeDiagnostic: SN1PR12MB2352:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:901; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hs8lobdZJsdc6++dNhKS9nUnDyFZtT2DMFyGh1C6hDj7Lc6MBJ07GELq+/2Ca4uLDZ35HkW90xIbK79KbLdEmvwxsBXTODwr4Jwa7cJ+wfyUW45bpjpDpwmahGwzw/qQUODc9m1dw+cHEGkkdAenSxheKxylgo9by1rQOQtNWhjR2Ulyv4VYbkzZ5I6gM3ULto0xuDkzP0jCxT9F3DTgHLWa36vihoNg8FUFQtARnveRyT2dsuwfVMErcUphE9aCWFQDVm5Q1jpKFUpAixQRAT9rASDiOxgNIBpbUUisohLD1YyBVPKTk/n+NSTPUnhvxa5mp7CzL4VWJyNzd0HpyIeinslxCWgiNQCAnNnxpFCzPHi651uDW8WmkMgSfQlELAjfSTk5uIf8T5Sf4pE8sZ3Pulv2cuYoOF8sY0EbjfSStqZwa1beOpJezF7n7/x1ajLom98CReFuZmHOU+ya7rebVBRuB9T8plfQgHPwvEtIRhRidxjFPJYIUfogutlLb1I66/344vhMosF9PEaohzHuHWeg7RQQVJipyHshjROOGvwTjzdk0GPk/1Jc3jABMQViiEtQcQS2uDJvdEgtxdlweJxceGSxLIxYSQlDfhppSoI1s+M3G68Ed/IPvepPu5zbyUIMPMN9t0aE7BR7CqIDkbRFA05Oj85LRt7KbMdWG0pGjqqqckw9b78/b9bk1oQDx2Tsy4PrsIvw5RdNB0Ew1MTCOvHevgwM8MLaePAoymZQbDhC0y0b4acmV2lgzwp9O0Wgha89lFki6FDNKFmeNBi140f5Z4ZijDpsaZI= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(2906002)(54906003)(16526019)(316002)(6916009)(186003)(336012)(4326008)(47076005)(8676002)(26005)(7416002)(36860700001)(508600001)(8936002)(36756003)(44832011)(2616005)(86362001)(81166007)(356005)(40460700001)(70206006)(1076003)(5660300002)(426003)(82310400004)(70586007)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:47:08.0311 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b25bd00d-6fb9-44e6-01b0-08d9bbfcb45c X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT042.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2352 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Introduce GUEST_SHARED_* macros, which are mostly analogous to existing GUEST_SYNC/GUEST_ASSERT/etc macros used to simplify guest code that uses ucall for host/guest synchronization. There are also some new CHECK_GUEST_SHARED_* macros intended to provide similar helpers in the host code that can pair directly with the guest versions. Signed-off-by: Michael Roth --- .../selftests/kvm/include/ucall_common.h | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h index ae0e8eec9734..b9b220dbd6c3 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -48,6 +48,7 @@ vm_vaddr_t ucall_shared_alloc(struct kvm_vm *vm, int count); void ucall_shared(struct ucall *uc, uint64_t cmd, int nargs, ...); uint64_t get_ucall_shared(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc); +/* Helpers for host/guest synchronization using ucall_shared */ #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) @@ -76,4 +77,71 @@ uint64_t get_ucall_shared(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) #define GUEST_ASSERT_EQ(a, b) __GUEST_ASSERT((a) == (b), #a " == " #b, 2, a, b) +/* Helper macros for ucall synchronization via shared memory/ucall struct. */ +#define GUEST_SHARED_SYNC_ARGS(uc, stage, arg1, arg2, arg3, arg4) \ + ucall_shared(uc, UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) +#define GUEST_SHARED_SYNC(uc, stage) \ + ucall_shared(uc, UCALL_SYNC, 2, "hello", stage) +#define GUEST_SHARED_DONE(uc) \ + ucall_shared(uc, UCALL_DONE, 0) +#define __GUEST_SHARED_ASSERT(uc, _condition, _condstr, _nargs, _args...) do { \ + if (!(_condition)) \ + ucall_shared(uc, UCALL_ABORT, 2 + _nargs, \ + "Failed guest assert: " \ + _condstr, __LINE__, _args); \ +} while (0) + +#define GUEST_SHARED_ASSERT(uc, _condition) \ + __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 0, 0) + +#define GUEST_SHARED_ASSERT_1(uc, _condition, arg1) \ + __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 1, (arg1)) + +#define GUEST_SHARED_ASSERT_2(uc, _condition, arg1, arg2) \ + __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 2, (arg1), (arg2)) + +#define GUEST_SHARED_ASSERT_3(uc, _condition, arg1, arg2, arg3) \ + __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 3, (arg1), (arg2), (arg3)) + +#define GUEST_SHARED_ASSERT_4(uc, _condition, arg1, arg2, arg3, arg4) \ + __GUEST_SHARED_ASSERT(uc, _condition, #_condition, 4, (arg1), (arg2), (arg3), (arg4)) + +#define GUEST_SHARED_ASSERT_EQ(uc, a, b) \ + __GUEST_SHARED_ASSERT(uc, (a) == (b), #a " == " #b, 2, a, b) + +#define __CHECK_SHARED_STATE(uc, uc_cmd, uc_cmd_expected) do { \ + if (uc_cmd != uc_cmd_expected) { \ + if (uc_cmd == UCALL_ABORT) \ + TEST_FAIL("Unexpected guest abort: \"%s\" at %s:%ld", \ + (const char *)uc->args[0], __FILE__, \ + uc->args[1]); \ + else \ + TEST_FAIL("Unexpected ucall command/state: %" PRIu64, \ + uc_cmd); \ + } \ +} while (0) + +#define CHECK_SHARED_SYNC(vm, vcpu_id, uc, stage) do { \ + uint64_t uc_cmd = get_ucall_shared(vm, vcpu_id, uc); \ + TEST_ASSERT(uc_cmd == UCALL_SYNC, \ + "Unexpected ucall command/state: %" PRIu64, uc_cmd); \ + TEST_ASSERT(!strcmp((char *)uc->args[0], "hello"), \ + "Invalid ucall signature argument."); \ + TEST_ASSERT(uc->args[1] == stage, \ + "Invalid ucall sync stage: %" PRIu64, uc->args[1]); \ +} while (0) + +#define CHECK_SHARED_DONE(vm, vcpu_id, uc) do { \ + uint64_t uc_cmd = get_ucall_shared(vm, vcpu_id, uc); \ + __CHECK_SHARED_STATE(uc, uc_cmd, UCALL_DONE); \ + TEST_ASSERT(uc_cmd == UCALL_DONE, \ + "Unexpected ucall command/state: %" PRIu64, uc_cmd); \ +} while (0) + +#define CHECK_SHARED_ABORT(vm, vcpu_id, uc) do { \ + uint64_t uc_cmd = get_ucall_shared(vm, vcpu_id, uc); \ + TEST_ASSERT(uc_cmd == UCALL_ABORT, \ + "Unexpected ucall command/state: %" PRIu64, uc_cmd); \ +} while (0) + #endif /* SELFTEST_KVM_UCALL_COMMON_H */ From patchwork Fri Dec 10 16:46:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 12670165 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 A2CBAC433F5 for ; Fri, 10 Dec 2021 16:47:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243933AbhLJQvI (ORCPT ); Fri, 10 Dec 2021 11:51:08 -0500 Received: from mail-dm6nam12on2074.outbound.protection.outlook.com ([40.107.243.74]:53409 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S243935AbhLJQvH (ORCPT ); Fri, 10 Dec 2021 11:51:07 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dWZ0doW7COeEektDqC7gA/ZCbb25069ahLJ4RAKo5KyPmb+NUAMfjJiM7oLHpp2h3NLfGAxJFbidXjSjMP1PXuH3IayvqUAXPlWprO83GWJL866eRkx/ur/FfcvUZM8pkjjfYePTDyWZrX02wwm/GWC1PRLMzjADeA2wUbtrTwESpZbizJPMT4ODD1qYl2tw6iyQaQ1ilCaxlxdMlbVVW7WbUU8ID8DLUs/YWP2grbn29Pil8by5YkQmvgCpEpz1rGyv0CbLOIro5W6zCj9nWNAJBZkHxMOpS9jrM/KaEFELZP3BZe6ueOdVRF92+D+0Ih5lDCmDNfoNphxdaiOsog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=BVePAZMnyynrWGF7TPR9d4//6tOFHnhk271ltjU1AgA=; b=ZYMUlLeyaEfuz0ZAD5Pg7OzmMUxyHYAzuxN8A9Y8qDIkuAMyMTruv34SfmhQPqg8A1a05eccvByx/B2HnTmPg2iulLHiUOlizjM9Jz3WuAHKt435ZHrK7mK/FHPSREJ89T/GuFXhe+a8TQdMY32Qm8JsvI8v08cLbrFo+M/B1tO+BDDD2TEKK+HJ7rxBXbi95D45VNneFwsVGhDdfe8WA690jSmNrHDQTTKkLbO+uRGzPAUKkcGC9wZ6O2eae6aqB3wPntAKCaDokbIsfYPLJRbXoyJV29UaEWw4Fie95d2tScdwck4iOdxWakmbafQ+Cl5UlmlHHngLQl7Z8np6QQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BVePAZMnyynrWGF7TPR9d4//6tOFHnhk271ltjU1AgA=; b=I35TrDd8iKtEfgcc1PMxeklWPrMQUn6TgGm54ll2b3qkNRy24de2S7yPSpte0tFGJ9kvnTx3UaUcvkBot+UdN+7NTSpkQqWb5lZewivMoV8KqsY2Qbyivua8nCOabd5UTiIKliyNhqqyqfkz31FlgW8YohQaTFb4gcsZsFuswvw= Received: from DS7PR03CA0265.namprd03.prod.outlook.com (2603:10b6:5:3b3::30) by BL0PR12MB5539.namprd12.prod.outlook.com (2603:10b6:208:1c3::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.11; Fri, 10 Dec 2021 16:47:29 +0000 Received: from DM6NAM11FT060.eop-nam11.prod.protection.outlook.com (2603:10b6:5:3b3:cafe::28) by DS7PR03CA0265.outlook.office365.com (2603:10b6:5:3b3::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21 via Frontend Transport; Fri, 10 Dec 2021 16:47:29 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; Received: from SATLEXMB03.amd.com (165.204.84.17) by DM6NAM11FT060.mail.protection.outlook.com (10.13.173.63) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4778.13 via Frontend Transport; Fri, 10 Dec 2021 16:47:29 +0000 Received: from localhost (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Fri, 10 Dec 2021 10:47:28 -0600 From: Michael Roth To: CC: , , , Nathan Tempelman , Marc Orr , "Steve Rutherford" , Sean Christopherson , Mingwei Zhang , Brijesh Singh , Tom Lendacky , Varad Gautam , Shuah Khan , Vitaly Kuznetsov , "David Woodhouse" , Ricardo Koller , "Jim Mattson" , Joerg Roedel , "Thomas Gleixner" , Ingo Molnar , "Borislav Petkov" , "H . Peter Anvin" , "Christian Borntraeger" , Janosch Frank , David Hildenbrand , "Claudio Imbrenda" , Marc Zyngier , James Morse , Alexandru Elisei , "Suzuki K Poulose" , Subject: [PATCH RFC 10/10] kvm: selftests: add ucall_test to test various ucall functionality Date: Fri, 10 Dec 2021 10:46:20 -0600 Message-ID: <20211210164620.11636-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211210164620.11636-1-michael.roth@amd.com> References: <20211210164620.11636-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB03.amd.com (10.181.40.144) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d90dda22-d6ba-4a5d-9f53-08d9bbfcc0e9 X-MS-TrafficTypeDiagnostic: BL0PR12MB5539:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:747; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: IiJkq39z+MDrAfOtDv70LiGPCgMQ2/Vpl2iYAKEGeiNzFPuuyENuoBiH5CHX7lx823MZcXu9aqYmLXNw0dTPLK+oXriZEb2vwS+4TuLTLJnpPIN9vxUUyXFoDXvTu16jALmBz30z9DB5iRPvFl2uAxU++8v0xSLP+yxdGsahi1vTxAHuCDYZg9xd4G4TRDW95/P8cObU7j+J+0W/xQs0+yx06YoONdYQBprvBbklGAL0R2LBK0+egKcJDygETsPsw9H+6GWQy6ee8TSNljDY8CoAD7+qwxIDJvIQ539aJCW4+rvb7Zg4L2oLlKBOfE1kEWBVcvY28Nd6xdcH8MPkjdz8zyiTfl2scX1cqvBeW9BzsxI6+U+r1y23XhDXJ9tAMa4piJPcVVNpeEkX8yM2i3A8A0uL+od4O2kZEbK55UyEoJnuB0dm/ImGCP3YmgE3orZVP6cU43kuD80N4xSVtnVw13XppHfH5BegcGOEhMv5VPyC+lRgzFKzNIjd4CUCz/UNygVgP2epetfBkhRh/1PWxRRQpqJBsAiZcn/LYa4v622hjtDvJ27H3+frt/kZSpP2s8A2WRNpBgRFfJmQjITZk5f8QWx8HA/Ee9z4O5VB/ccpaKePu54Mj4SFw8uxrJkpj1XI34HZ8RRoz34X0m7mAHQJxGs0+t6Ix267wQb6POAYQ/+80E3OZJQnMQIhUYGWKf079P0jzNyiSOMNcueUfl/v4U3SXHvZLsAtcYk684uyme/+a8cqqvgt/112GbhHRwdfSwO1rJSyKC0U1kq/JgK+7JC2LDmfMV1jIdQ= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(4636009)(46966006)(36840700001)(40470700001)(81166007)(70586007)(47076005)(44832011)(2616005)(54906003)(70206006)(86362001)(36860700001)(508600001)(2906002)(6666004)(7416002)(8676002)(1076003)(26005)(40460700001)(36756003)(82310400004)(16526019)(5660300002)(186003)(426003)(356005)(6916009)(316002)(8936002)(336012)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2021 16:47:29.1387 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d90dda22-d6ba-4a5d-9f53-08d9bbfcc0e9 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT060.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB5539 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Tests will initialize the ucall implementation in 3 main ways, depending on the test/architecture: 1) by relying on a default ucall implementation being available without the need to do any additional setup 2) by calling ucall_init() to initialize the default ucall implementation 3) by using ucall_init_ops() to initialize a specific ucall implementation and in each of these cases it may use the ucall implementation to execute the standard ucall()/get_ucall() interfaces, or the new ucall_shared()/get_ucall_shared() interfaces. Implement a basic self-test to exercise ucall under all the scenarios that are applicable for a particular architecture. Signed-off-by: Michael Roth --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 3 + tools/testing/selftests/kvm/ucall_test.c | 182 +++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 tools/testing/selftests/kvm/ucall_test.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 3763105029fb..4a801cba9c62 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -57,3 +57,4 @@ /steal_time /kvm_binary_stats_test /system_counter_offset_test +/ucall_test diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 06a02b6fa907..412de8093e6c 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -88,6 +88,7 @@ TEST_GEN_PROGS_x86_64 += set_memory_region_test TEST_GEN_PROGS_x86_64 += steal_time TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test +TEST_GEN_PROGS_x86_64 += ucall_test TEST_GEN_PROGS_aarch64 += aarch64/arch_timer TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions @@ -105,6 +106,7 @@ TEST_GEN_PROGS_aarch64 += rseq_test TEST_GEN_PROGS_aarch64 += set_memory_region_test TEST_GEN_PROGS_aarch64 += steal_time TEST_GEN_PROGS_aarch64 += kvm_binary_stats_test +TEST_GEN_PROGS_aarch64 += ucall_test TEST_GEN_PROGS_s390x = s390x/memop TEST_GEN_PROGS_s390x += s390x/resets @@ -116,6 +118,7 @@ TEST_GEN_PROGS_s390x += kvm_page_table_test TEST_GEN_PROGS_s390x += rseq_test TEST_GEN_PROGS_s390x += set_memory_region_test TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_s390x += ucall_test TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M)) LIBKVM += $(LIBKVM_$(UNAME_M)) diff --git a/tools/testing/selftests/kvm/ucall_test.c b/tools/testing/selftests/kvm/ucall_test.c new file mode 100644 index 000000000000..f0e6e4e79786 --- /dev/null +++ b/tools/testing/selftests/kvm/ucall_test.c @@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ucall interface/implementation tests. + * + * Copyright (C) 2021 Advanced Micro Devices + */ +#define _GNU_SOURCE /* for program_invocation_short_name */ +#include +#include +#include +#include +#include + +#include "test_util.h" + +#include "kvm_util.h" +#include "processor.h" + +#define VCPU_ID 2 +#define TOTAL_PAGES 512 + +enum uc_test_type { + UC_TEST_WITHOUT_UCALL_INIT, + UC_TEST_WITH_UCALL_INIT, + UC_TEST_WITH_UCALL_INIT_OPS, + UC_TEST_WITH_UCALL_INIT_OPS_SHARED, + UC_TEST_MAX, +}; + +struct uc_test_config { + enum uc_test_type type; + const struct ucall_ops *ops; +}; + +static void test_ucall(void) +{ + GUEST_SYNC(1); + GUEST_SYNC(2); + GUEST_DONE(); + GUEST_ASSERT(false); +} + +static void check_ucall(struct kvm_vm *vm) +{ + struct ucall uc_tmp; + + vcpu_run(vm, VCPU_ID); + TEST_ASSERT(get_ucall(vm, VCPU_ID, &uc_tmp) == UCALL_SYNC, "sync failed"); + + vcpu_run(vm, VCPU_ID); + TEST_ASSERT(get_ucall(vm, VCPU_ID, &uc_tmp) == UCALL_SYNC, "sync failed"); + + vcpu_run(vm, VCPU_ID); + TEST_ASSERT(get_ucall(vm, VCPU_ID, &uc_tmp) == UCALL_DONE, "done failed"); + + vcpu_run(vm, VCPU_ID); + TEST_ASSERT(get_ucall(vm, VCPU_ID, &uc_tmp) == UCALL_ABORT, "abort failed"); +} + +static void test_ucall_shared(struct ucall *uc) +{ + GUEST_SHARED_SYNC(uc, 1); + GUEST_SHARED_SYNC(uc, 2); + GUEST_SHARED_DONE(uc); + GUEST_SHARED_ASSERT(uc, false); +} + +static void check_ucall_shared(struct kvm_vm *vm, struct ucall *uc) +{ + vcpu_run(vm, VCPU_ID); + CHECK_SHARED_SYNC(vm, VCPU_ID, uc, 1); + + vcpu_run(vm, VCPU_ID); + CHECK_SHARED_SYNC(vm, VCPU_ID, uc, 2); + + vcpu_run(vm, VCPU_ID); + CHECK_SHARED_DONE(vm, VCPU_ID, uc); + + vcpu_run(vm, VCPU_ID); + CHECK_SHARED_ABORT(vm, VCPU_ID, uc); +} + +static void __attribute__((__flatten__)) +guest_code(struct ucall *uc) +{ + if (uc) + test_ucall_shared(uc); + else + test_ucall(); +} + +static struct kvm_vm *setup_vm(void) +{ + struct kvm_vm *vm; + + vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR); + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, TOTAL_PAGES, 0); + + /* Set up VCPU and initial guest kernel. */ + vm_vcpu_add_default(vm, VCPU_ID, guest_code); + kvm_vm_elf_load(vm, program_invocation_name); + + return vm; +} + +static void setup_vm_args(struct kvm_vm *vm, vm_vaddr_t uc_gva) +{ + vcpu_args_set(vm, VCPU_ID, 1, uc_gva); +} + +static void run_ucall_test(const struct uc_test_config *config) +{ + struct kvm_vm *vm = setup_vm(); + const struct ucall_ops *ops = config->ops; + bool is_default_ops = (!ops || ops == &ucall_ops_default); + bool shared = (config->type == UC_TEST_WITH_UCALL_INIT_OPS_SHARED); + + pr_info("Testing ucall%s ops for: %s%s\n", + shared ? "_shared" : "", + ops ? ops->name : "unspecified", + is_default_ops ? " (via default)" : ""); + + if (config->type == UC_TEST_WITH_UCALL_INIT) + ucall_init(vm, NULL); + else if (config->type == UC_TEST_WITH_UCALL_INIT_OPS || + config->type == UC_TEST_WITH_UCALL_INIT_OPS_SHARED) + ucall_init_ops(vm, NULL, config->ops); + + if (shared) { + struct ucall *uc; + vm_vaddr_t uc_gva; + + /* Set up ucall buffer. */ + uc_gva = ucall_shared_alloc(vm, 1); + uc = addr_gva2hva(vm, uc_gva); + + setup_vm_args(vm, uc_gva); + check_ucall_shared(vm, uc); + } else { + setup_vm_args(vm, 0); + check_ucall(vm); + } + + if (config->type == UC_TEST_WITH_UCALL_INIT) + ucall_uninit(vm); + else if (config->type == UC_TEST_WITH_UCALL_INIT_OPS || + config->type == UC_TEST_WITH_UCALL_INIT_OPS_SHARED) + ucall_uninit_ops(vm); + + kvm_vm_free(vm); +} + +static const struct uc_test_config test_configs[] = { +#if defined(__x86_64__) + { UC_TEST_WITHOUT_UCALL_INIT, NULL }, + { UC_TEST_WITH_UCALL_INIT, NULL }, + { UC_TEST_WITH_UCALL_INIT_OPS, &ucall_ops_default }, + { UC_TEST_WITH_UCALL_INIT_OPS, &ucall_ops_pio }, + { UC_TEST_WITH_UCALL_INIT_OPS_SHARED, &ucall_ops_pio }, + { UC_TEST_WITH_UCALL_INIT_OPS_SHARED, &ucall_ops_halt }, +#elif defined(__aarch64__) + { UC_TEST_WITH_UCALL_INIT, NULL }, + { UC_TEST_WITH_UCALL_INIT_OPS, &ucall_ops_default }, + { UC_TEST_WITH_UCALL_INIT_OPS, &ucall_ops_mmio }, +#elif defined(__s390x__) + { UC_TEST_WITHOUT_UCALL_INIT, NULL }, + { UC_TEST_WITH_UCALL_INIT, NULL }, + { UC_TEST_WITH_UCALL_INIT_OPS, &ucall_ops_default }, + { UC_TEST_WITH_UCALL_INIT_OPS, &ucall_ops_diag501 }, +#endif + { UC_TEST_MAX, NULL }, +}; + +int main(int argc, char *argv[]) +{ + int i; + + for (i = 0; test_configs[i].type != UC_TEST_MAX; i++) + run_ucall_test(&test_configs[i]); + + return 0; +}