Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp5134323rwb; Mon, 31 Jul 2023 19:22:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlGDZvPNfFqLQ6/IwvPzPgEQ0z6FX8ixjqaWzAz2E4dNEsVWBt6IUQfGlGHYXxFRj+VViO2Q X-Received: by 2002:a05:6a00:a08:b0:686:fb87:7f55 with SMTP id p8-20020a056a000a0800b00686fb877f55mr13327654pfh.15.1690856531552; Mon, 31 Jul 2023 19:22:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690856531; cv=none; d=google.com; s=arc-20160816; b=vYunhdLh/8N9m1otzbWoepGZ3T5DqoGz9RegzYkfzJJmE0Ds4dPvVApXMuv8sqk+ND D38rHgk0stFmGhXGJ6vUYGnHderPUs/YLG4fvQWPY291UXOTvL2iUJpWxmz/doycBFsz s3tvF3q8+cKQmXVe98VlFZydAXJbFoktjd2qeMQHQGOJpTF1vjzQ8PKYAJyywTPk8rjw CPN+Db6oVZ52FMMxoykeQY+5Z5dMhckG/4BgpA/XweWSdF3w0DPb3IXkpnYWwvrsqc3M diY6vKP/KsasnRqDkzAeAPjC8vWljMrtSOm77NbyufbGuyRAz++MMS09Xq69JReegzrK 1qMQ== 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=rWk88dvtW/rlAUWZYD6Y4dzCl0AF/8vNXzCDSiZqgfE=; fh=78XkdsmXgGpMLEUd4018w1oFMTvg8nay2829+Tm9pTc=; b=FvFnDA2Nbvcy0PrvPxhtYABilqhQDVypLJT92FC1jEeEdGpsGNomPk3GNTA1PEPoWA Arz9HoL9a49DOkaSUjv/PP0OA/Q3DFbG+x/iDr9LYwJQwY609W/uPaNdldpTe4CUwIqD OvFtWqSRjQM+cD6QSEC0FzM2KLnXRekgCEukEsTWmrUNFeRXOadH4cIK1UbaFN1Ne6Ib L8VLu5Eu7HXgcnXV76FkTDy+aPxW0y+ZZzFg9Q+EV79O1YA2MGf0ZTC4KMEQVPq39xoy Gx0Q3uMmJj4dUCfyt+sjcfVjLYmy6ShnsTVFdi5tpzIc6IbDXeAUJ4hZllDUdV6+5CBj Jwuw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h10-20020a63384a000000b0053fe93b2d33si2499069pgn.201.2023.07.31.19.21.58; Mon, 31 Jul 2023 19:22:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232047AbjHACCZ (ORCPT + 99 others); Mon, 31 Jul 2023 22:02:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230307AbjHACCO (ORCPT ); Mon, 31 Jul 2023 22:02:14 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D92501BC8; Mon, 31 Jul 2023 19:02:12 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8AxTeujZ8hkL8gNAA--.27265S3; Tue, 01 Aug 2023 10:02:11 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxHCOeZ8hkPHJDAA--.25753S5; Tue, 01 Aug 2023 10:02:10 +0800 (CST) From: Tianrui Zhao To: Shuah Khan , Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Vishal Annapurve , Huacai Chen , WANG Xuerui , loongarch@lists.linux.dev, Peter Xu , Vipin Sharma , maobibo@loongson.cn, zhaotianrui@loongson.cn Subject: [PATCH v1 3/4] selftests: kvm: Add ucall tests for LoongArch KVM Date: Tue, 1 Aug 2023 10:02:05 +0800 Message-Id: <20230801020206.1957986-4-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230801020206.1957986-1-zhaotianrui@loongson.cn> References: <20230801020206.1957986-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8BxHCOeZ8hkPHJDAA--.25753S5 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add ucall tests for LoongArch KVM, it means that VM use hypercall to return to userspace and handle the mmio exception. Based-on: <20230720062813.4126751-1-zhaotianrui@loongson.cn> Signed-off-by: Tianrui Zhao --- .../selftests/kvm/lib/loongarch/ucall.c | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tools/testing/selftests/kvm/lib/loongarch/ucall.c 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 000000000000..b32f7c7f6548 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/loongarch/ucall.c @@ -0,0 +1,44 @@ +// 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. + */ +static 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_do_ucall(vm_vaddr_t uc) +{ + + WRITE_ONCE(*ucall_exit_mmio_addr, uc); +} + +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; +} -- 2.39.1