Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4288245rdb; Thu, 14 Sep 2023 19:03:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDoUHsaJoybEwRRPN1dC2l3LIKmG5RJTVDJ7GbyI4LL+l+B/MX8xDoId5lLp4o16wCSv+L X-Received: by 2002:a05:6a00:10c3:b0:68f:b72f:9aca with SMTP id d3-20020a056a0010c300b0068fb72f9acamr359044pfu.27.1694743390979; Thu, 14 Sep 2023 19:03:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694743390; cv=none; d=google.com; s=arc-20160816; b=TSaZJ/c5tCoi4tcRi2qiKw/2ION1d7CpuMHZA2f6G7pc7KHiB6m9tQM1yTMLnd4Q8B RiFxXI5SaSOjMSHssC1ZDavT8aAUwREwQBPImc5Y3n2ndAe2qqmcjNpx+dd/GfpjvJbY MYQL9fK+OLc4G1enk+p/fhdSq4QuvK3Q4mcq/e1+bt9Grr5NmLNQNvA0E7RinW+haGez 76W7F8hQdXkFZ91V5GTnQ9pTyiqf2L6u63FXq1DhzurqcvP3dH+RoXGKQGrlJWJQVYJr B5IsHjx5VB0NcTBfMyDLkTN4frg7DYN6TbontepzTS6ol7X3cTeHFhCRrkuDTsK5Rh/u 1yhw== 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=rNpFnYHKotvW5eL9y0XZ9ODa1RR+51h5ez0+vDZVoMg=; fh=ejoaVAUUbBXsA/6jM/beuI9xCYtV2osdR9SpI7hjY84=; b=XA246iHOdp/+jRGDI9rUmJsWx+lf3MQ1SmQs6RyH8xMX2Z4cyqWMGWQF0/9Wblas2x Ng1ECkgzIEAxBhzcTHI6lawc5XkpSDJIlnNcS7Knc8WWqxUNZOgQ5OIqV7YOKXt7yiSd ocW4xJeTzWwoH5AaTcdJsrVgIyzm53xbjYjyB0h7TQHJuJd5z5Yu/fSR2yePe+ZsuJra 3Vyu9ItGsJOUq56k7YM8lW1106LMOrEK6dahQfiCwsKlNzEafrkD8PmQ/QLoQhI6YOAx xH0ai/zVVjNTcZ9ctrGqvZ4Ulnlv80JLzuq4cWnawy/KYfvETMSEUDUfNUEddqcwl5C7 lRtg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n20-20020a056a00213400b0068bc4337051si2346048pfj.359.2023.09.14.19.03.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 19:03:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 D711782F4E0F; Thu, 14 Sep 2023 18:50:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231503AbjIOBu0 (ORCPT + 99 others); Thu, 14 Sep 2023 21:50:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231310AbjIOBuF (ORCPT ); Thu, 14 Sep 2023 21:50:05 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2ED5E270F; Thu, 14 Sep 2023 18:49:57 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8AxDOtDuANliv4nAA--.6203S3; Fri, 15 Sep 2023 09:49:55 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Axndw9uANl+ioGAA--.11927S6; Fri, 15 Sep 2023 09:49:54 +0800 (CST) From: Tianrui Zhao To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao , zhaotianrui@loongson.cn Subject: [PATCH v21 04/29] LoongArch: KVM: Implement VM related functions Date: Fri, 15 Sep 2023 09:49:24 +0800 Message-Id: <20230915014949.1222777-5-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230915014949.1222777-1-zhaotianrui@loongson.cn> References: <20230915014949.1222777-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Axndw9uANl+ioGAA--.11927S6 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]); Thu, 14 Sep 2023 18:50:24 -0700 (PDT) Implement LoongArch VM operations: Init and destroy vm interface, allocating memory page to save the vm pgd when init vm. Implement vm check extension, such as getting vcpu number info, memory slots info, and fpu info. And implement vm status description. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/vm.c | 92 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 arch/loongarch/kvm/vm.c diff --git a/arch/loongarch/kvm/vm.c b/arch/loongarch/kvm/vm.c new file mode 100644 index 0000000000..abc345bf37 --- /dev/null +++ b/arch/loongarch/kvm/vm.c @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2020-2023 Loongson Technology Corporation Limited + */ + +#include +#include + +const struct _kvm_stats_desc kvm_vm_stats_desc[] = { + KVM_GENERIC_VM_STATS(), + STATS_DESC_ICOUNTER(VM, pages), + STATS_DESC_ICOUNTER(VM, hugepages), +}; + +const struct kvm_stats_header kvm_vm_stats_header = { + .name_size = KVM_STATS_NAME_SIZE, + .num_desc = ARRAY_SIZE(kvm_vm_stats_desc), + .id_offset = sizeof(struct kvm_stats_header), + .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, + .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + + sizeof(kvm_vm_stats_desc), +}; + +int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) +{ + int i; + + /* Allocate page table to map GPA -> RPA */ + kvm->arch.pgd = kvm_pgd_alloc(); + if (!kvm->arch.pgd) + return -ENOMEM; + + kvm_init_vmcs(kvm); + kvm->arch.gpa_size = BIT(cpu_vabits - 1); + kvm->arch.root_level = CONFIG_PGTABLE_LEVELS - 1; + kvm->arch.invalid_ptes[0] = 0; + kvm->arch.invalid_ptes[1] = (unsigned long)invalid_pte_table; + kvm->arch.invalid_ptes[2] = (unsigned long)invalid_pmd_table; +#if CONFIG_PGTABLE_LEVELS > 3 + kvm->arch.invalid_ptes[3] = (unsigned long)invalid_pud_table; +#endif + for (i = 0; i <= kvm->arch.root_level; i++) + kvm->arch.pte_shifts[i] = PAGE_SHIFT + i * (PAGE_SHIFT - 3); + + return 0; +} + +void kvm_arch_destroy_vm(struct kvm *kvm) +{ + kvm_destroy_vcpus(kvm); + free_page((unsigned long)kvm->arch.pgd); + kvm->arch.pgd = NULL; +} + +int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) +{ + int r; + + switch (ext) { + case KVM_CAP_ONE_REG: + case KVM_CAP_ENABLE_CAP: + case KVM_CAP_READONLY_MEM: + case KVM_CAP_SYNC_MMU: + case KVM_CAP_IMMEDIATE_EXIT: + case KVM_CAP_IOEVENTFD: + case KVM_CAP_MP_STATE: + r = 1; + break; + case KVM_CAP_NR_VCPUS: + r = num_online_cpus(); + break; + case KVM_CAP_MAX_VCPUS: + r = KVM_MAX_VCPUS; + break; + case KVM_CAP_MAX_VCPU_ID: + r = KVM_MAX_VCPU_IDS; + break; + case KVM_CAP_NR_MEMSLOTS: + r = KVM_USER_MEM_SLOTS; + break; + default: + r = 0; + break; + } + + return r; +} + +int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) +{ + return -ENOIOCTLCMD; +} -- 2.39.1