From patchwork Wed Nov 8 02:51:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 162869 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp657814vqo; Tue, 7 Nov 2023 18:52:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHLs9KrjhTEN12Q08CNTABwL8TNeXufWcY7KK2k+TQnIYrGfZIQVtIjQGfJ2tGM+k11yuBv X-Received: by 2002:a05:6358:2915:b0:168:e7ab:6877 with SMTP id y21-20020a056358291500b00168e7ab6877mr557213rwb.6.1699411922458; Tue, 07 Nov 2023 18:52:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699411922; cv=none; d=google.com; s=arc-20160816; b=dWRD6oIOK2tm3q8+mTA/BOEtXCONIvvFRGN20WJMzdBMaUopfZydwhI4N0UwHwhhKM Ua3V/Hr3VCCbQel3qguMxRT8DZLfvReYR9F3lsSB7/ad6DX/ccVsqu9xUx1mlECpjOnc NGYm1CGoOD2syaOEOIkxysDzsQis7qB0yygeJqZW3JyTxtogTUcxX0g3fJP2mmpVfq+i FWfwWcpIALr+EJF5t5mS7JC2bIwQqhKxGdPzHeb3UoM9CAJ/D16REZqoImppvoiTXOcy 55tv2UvA7eoFK2nsip1MEcCNoG0GpcBgmSlCOJORblMSpzqcsrKzFoJ0vRE7FmMouMPD ng3w== 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=SXO40rmDO9cm3o/XXWX6rPRwzvGb92K4jNEJM54y7l0=; fh=nMfjIhEjRr3TY5wbPhfmwv04cfhnjdjQ3fRbSHuEkVo=; b=f2LzeSs3aFWeasvNfYJ3Otgy11BobnUdfVGJU4G6wqKjCYKLfZFVUdfc0z/r06vR+m NxdYbTyaif3Q/N/5tVVo1cbnIrmQam2V+SE13yC+RedGVWq5vr8zd+zaJNtyPigEbtEP Zlu8FcaiZcvqU3oFCiHtf1gW2kXynWBex9ZyR3HEbpze85/VQw7S8B/5p7Dh7cwx76uK Zv9Ug6Wj0zuojRiJnXzZFkeqH9NkX1UWI0/ZdSMmYK7/jSXZBz4tqZNI95v+tFnm4DCN OpIYfa+6LhuRg/FqL90OFhEIH5E8gdgudWjNB1yW947iCnXDZpG67htIeJBOTPVgFq1h jbgQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id u9-20020a655c09000000b005b90b2d9178si3458506pgr.796.2023.11.07.18.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 18:52:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 8CD8582F1DF3; Tue, 7 Nov 2023 18:51:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343759AbjKHCvk (ORCPT + 32 others); Tue, 7 Nov 2023 21:51:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234780AbjKHCvi (ORCPT ); Tue, 7 Nov 2023 21:51:38 -0500 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AED05D79; Tue, 7 Nov 2023 18:51:35 -0800 (PST) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Bx5fC290pll+c3AA--.45111S3; Wed, 08 Nov 2023 10:51:34 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Ax3tyy90plX7U7AA--.63786S5; Wed, 08 Nov 2023 10:51:33 +0800 (CST) From: Tianrui Zhao To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Shuah Khan , Paolo Bonzini , Vishal Annapurve , Huacai Chen , loongarch@lists.linux.dev, Peter Xu , Vipin Sharma , maobibo@loongson.cn, zhaotianrui@loongson.cn, Sean Christopherson Subject: [PATCH v4 3/4] KVM: selftests: Add ucall test support for LoongArch Date: Wed, 8 Nov 2023 10:51:33 +0800 Message-Id: <20231108025134.2592663-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20231108025134.2592663-1-zhaotianrui@loongson.cn> References: <20231108025134.2592663-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Ax3tyy90plX7U7AA--.63786S5 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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 (snail.vger.email [0.0.0.0]); Tue, 07 Nov 2023 18:51:46 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781962555441986324 X-GMAIL-MSGID: 1781962555441986324 Add ucall test support for LoongArch. A ucall is a "hypercall to userspace". Signed-off-by: Tianrui Zhao --- .../selftests/kvm/include/loongarch/ucall.h | 20 ++++++++++ .../selftests/kvm/lib/loongarch/ucall.c | 38 +++++++++++++++++++ 2 files changed, 58 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..4b68f37efd3 --- /dev/null +++ b/tools/testing/selftests/kvm/include/loongarch/ucall.h @@ -0,0 +1,20 @@ +/* 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 + +/* + * 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; +}