From patchwork Thu Nov 30 11:18:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 171842 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp315436vqy; Thu, 30 Nov 2023 03:31:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHudUctfdoSaT6GnTcBJoHjNJyUIcNJvH1RikhoOiPv0l19ZVY/UyTeJvQ76sYyyk1UibH0 X-Received: by 2002:a05:6a00:244e:b0:6cd:cd77:afae with SMTP id d14-20020a056a00244e00b006cdcd77afaemr9638181pfj.3.1701343901554; Thu, 30 Nov 2023 03:31:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701343901; cv=none; d=google.com; s=arc-20160816; b=Grk2TZmEA+aYTpCdy4eg/7Rj4NzqqK2YsoLzQ4aLsGJvkqMEUiaTyqJX+zo1aCpAwo 0+ALAsNWUb7Wdh2EBIE5uBFF7JEkctHzt2WK9p1wDCERAJy1vOQ3BA1c75+IW76BOH98 kPocIPb01Z3KtduOtInYFep49udtlmIaPHmsqotwjGG05Bv9t/ys8L+pMiuntsDnSkse jQ9aXY3AJGGOKmJqK82S2wIf+07Ou4RwbNjblrATmS4PwvQfEa4ieyT1pVOoVLSlI8w5 J6sG98tAVhD6b54OJO3i9gww2ev8wm923NALtGuwxU4ddg2wrh251NxOS5slRip77Mtg 9TeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2CNdjnRkdPLaQ6laCxY+5jJ+QAOeVz3hXSbjiGj9bmE=; fh=Qi8O4GPf+E9kHNNUfjv2vNMHhM7kTwYQ+uMwpaT6JZQ=; b=M5YuLVua9IeDn0qjtkodaonZq2+kl9Upse7lAuLv4W6U4rWztWqsm21NwU3d4LJEDx 79A223NR5ZXPxg1jrZIehCoK8p7nDkE0I9ehj5D6Trk7wJNcFJNNvMuRf6JzVZgcdBhG +Yy+h2ljLjPVGcgHrT0xRxOfMVX1pW1Dwfb8uqJskwWRkFlAETfSoPTFmX+v5AdaL8dm fdSppOvEC8/ikJyqKP2Uztl+0m09bK22NAPPoRUr5CrciDJ/HPpMfK2l3XOtcOy3QZRb DAQcN3M91D687ppEWN4dG41zfj66YMcjGFSb35H2HxLk3RVjVppEXoV9k/kD3HEPK5dT MV2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id z5-20020a656645000000b005b969c62b27si1081083pgv.747.2023.11.30.03.31.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 03:31:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 96ADB805B2E9; Thu, 30 Nov 2023 03:31:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345208AbjK3LbW (ORCPT + 99 others); Thu, 30 Nov 2023 06:31:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345279AbjK3LbA (ORCPT ); Thu, 30 Nov 2023 06:31:00 -0500 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7F328198E; Thu, 30 Nov 2023 03:31:04 -0800 (PST) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Dxl+h2cmhlBug9AA--.21699S3; Thu, 30 Nov 2023 19:31:02 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxG9xycmhlNblQAA--.47327S5; Thu, 30 Nov 2023 19:31:01 +0800 (CST) From: Tianrui Zhao To: Shuah Khan , Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Sean Christopherson Cc: Vishal Annapurve , Huacai Chen , WANG Xuerui , loongarch@lists.linux.dev, Peter Xu , Vipin Sharma , maobibo@loongson.cn, zhaotianrui@loongson.cn Subject: [PATCH v5 3/4] KVM: selftests: Add ucall test support for LoongArch Date: Thu, 30 Nov 2023 19:18:03 +0800 Message-Id: <20231130111804.2227570-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20231130111804.2227570-1-zhaotianrui@loongson.cn> References: <20231130111804.2227570-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8AxG9xycmhlNblQAA--.47327S5 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 30 Nov 2023 03:31:36 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783988382845816234 X-GMAIL-MSGID: 1783988382845816234 Add ucall test support for LoongArch. A ucall is a "hypercall to userspace". Signed-off-by: Tianrui Zhao Reviewed-by: Bibo Mao --- .../selftests/kvm/include/loongarch/ucall.h | 28 ++++++++++++++ .../selftests/kvm/lib/loongarch/ucall.c | 38 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/loongarch/ucall.h create mode 100644 tools/testing/selftests/kvm/lib/loongarch/ucall.c diff --git a/tools/testing/selftests/kvm/include/loongarch/ucall.h b/tools/testing/selftests/kvm/include/loongarch/ucall.h new file mode 100644 index 00000000000..e9033ea6fbf --- /dev/null +++ b/tools/testing/selftests/kvm/include/loongarch/ucall.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_KVM_UCALL_H +#define SELFTEST_KVM_UCALL_H + +#include "kvm_util_base.h" + +#define UCALL_EXIT_REASON KVM_EXIT_MMIO + +/* + * Default base address for application loading is 0x120000000, + * DEFAULT_GUEST_TEST_MEM should be larger than app loading address, + * so that PER_VCPU_MEM_SIZE can be large enough, and kvm selftests + * app size is smaller than 256M in generic + */ +#define DEFAULT_GUEST_TEST_MEM 0x130000000 + +/* + * ucall_exit_mmio_addr holds per-VM values (global data is duplicated by each + * VM), it must not be accessed from host code. + */ +extern vm_vaddr_t *ucall_exit_mmio_addr; + +static inline void ucall_arch_do_ucall(vm_vaddr_t uc) +{ + WRITE_ONCE(*ucall_exit_mmio_addr, uc); +} + +#endif diff --git a/tools/testing/selftests/kvm/lib/loongarch/ucall.c b/tools/testing/selftests/kvm/lib/loongarch/ucall.c new file mode 100644 index 00000000000..fc6cbb50573 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/loongarch/ucall.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * ucall support. A ucall is a "hypercall to userspace". + * + */ +#include "kvm_util.h" + +/* + * ucall_exit_mmio_addr holds per-VM values (global data is duplicated by each + * VM), it must not be accessed from host code. + */ +vm_vaddr_t *ucall_exit_mmio_addr; + +void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) +{ + vm_vaddr_t mmio_gva = vm_vaddr_unused_gap(vm, vm->page_size, KVM_UTIL_MIN_VADDR); + + virt_map(vm, mmio_gva, mmio_gpa, 1); + + vm->ucall_mmio_addr = mmio_gpa; + + write_guest_global(vm, ucall_exit_mmio_addr, (vm_vaddr_t *)mmio_gva); +} + +void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) +{ + struct kvm_run *run = vcpu->run; + + if (run->exit_reason == KVM_EXIT_MMIO && + run->mmio.phys_addr == vcpu->vm->ucall_mmio_addr) { + TEST_ASSERT(run->mmio.is_write && run->mmio.len == sizeof(uint64_t), + "Unexpected ucall exit mmio address access"); + + return (void *)(*((uint64_t *)run->mmio.data)); + } + + return NULL; +}