From patchwork Thu Jun 16 13:48:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883888 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 88E77C433EF for ; Thu, 16 Jun 2022 13:49:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9/yfpND9cnUt3+YPcAmyOsejSOvWBzXUYsEtaVy0oUo=; b=Tq+z3xivhyqeTx NhHFeMOU8s/PRS+HUwNj6gt6MtSBLEldHfGgSd1Ox4aNPAAejx9EQXS8NEETd5neD0GmIiqlFignD tkF1veiEQ98+0JKPW4Y7Vtn4N7gcVsQQUiUtXN4xdWwM1ToigBEcd/Zb103RSBq72Dus7zZAV/R2t SjJlEhN3sdAH8y1xZhEdR7vugs24UZE+gtBSA0+ySh9OpFGm2wLy2Va2dHUVUE519Fv5tdQZx+9f+ IFTAIgd/7qyo2jvE74H8zCK+GHN7QxEkproVmdtDn3KKMyYELmxenjRKI4i9GnRp5NGpA6GIxk8e6 J70MJX3sSO5l5Fy95FxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prO-002ZUE-Gp; Thu, 16 Jun 2022 13:48:34 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prI-002ZPB-S9 for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:32 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D4FE112FC; Thu, 16 Jun 2022 06:48:25 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C98E03F7F5; Thu, 16 Jun 2022 06:48:23 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 01/12] Use MB for megabytes consistently Date: Thu, 16 Jun 2022 14:48:17 +0100 Message-Id: <20220616134828.129006-2-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064829_011382_0C74AC89 X-CRM114-Status: GOOD ( 11.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The help text for the -m/--mem argument states that the guest memory size is in MiB (mebibyte). MiB is the same thing as MB (megabyte), and indeed this is how MB is used throughout kvmtool. Replace MiB with MB, so people don't get the wrong idea and start believing that for kvmtool a MB is 10^6 bytes instead of 2^20. Signed-off-by: Alexandru Elisei --- Documentation/kvmtool.1 | 4 ++-- builtin-run.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Documentation/kvmtool.1 b/Documentation/kvmtool.1 index 2b8c274dc3ff..25d46f8f51f9 100644 --- a/Documentation/kvmtool.1 +++ b/Documentation/kvmtool.1 @@ -10,7 +10,7 @@ kvmtool is a userland tool for creating and controlling KVM guests. .SH "KVMTOOL COMMANDS" .sp .PP -.B run -k [\-c ] [\-m ] [\-p ] +.B run -k [\-c ] [\-m ] [\-p ] .br .B [\-i ] [\-d ] [\-\-console serial|virtio|hv] .br @@ -30,7 +30,7 @@ The number of virtual CPUs to run. .sp .B \-m, \-\-mem .RS 4 -Virtual machine memory size in MiB. +Virtual machine memory size in MB. .RE .sp .B \-p, \-\-params diff --git a/builtin-run.c b/builtin-run.c index 9a1a0c1fa6fb..0126c9fbcba6 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -97,7 +97,7 @@ void kvm_run_set_wrapper_sandbox(void) "A name for the guest"), \ OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"), \ OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory" \ - " size in MiB."), \ + " size in MB."), \ OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk " \ " image or rootfs directory", img_name_parser, \ kvm), \ From patchwork Thu Jun 16 13:48:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883890 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 846ACCCA47A for ; Thu, 16 Jun 2022 13:50:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=B+8DDIx6xfPFZwBxwIGUQsy5Zl5iw3+cG04JXL3ILhA=; b=vjVx1gCe484Ujm mJ8HP55UbdEvEH381d36qWmWlxMRusAQHwgSGdXhIi8hfIep6LqXnSJWNUFE2jfvnGYrLqKAPoR7C H4PN8eCaLtURdQz8oluHyrSCowmdnl1eZdHnmXmDJgkKWSZcPQgAjPifUABAVUDJMZLpbf/+yZ4RR jbrOvZ9FqD4NTBi5wMxx9ieKP+AKDZvv2MXTdeOxGbUZSCV/PKSP0xCXKoSUydbnF7e+GCSF5ApeX wkORlTyTSu7nw6XI1ncQ0hoxo70H4yMRSEYFtY3s8ONAaXzHbyIs1cudGn/bewBQPdXwtisyRQKzR GflEE9/w/KLwCrtPjkfQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prp-002Zgj-9g; Thu, 16 Jun 2022 13:49:01 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prJ-002ZQw-BX for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:32 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A95E513D5; Thu, 16 Jun 2022 06:48:27 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 08CB43F7F5; Thu, 16 Jun 2022 06:48:25 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 02/12] builtin-run: Always use RAM size in bytes Date: Thu, 16 Jun 2022 14:48:18 +0100 Message-Id: <20220616134828.129006-3-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064829_515786_845B807E X-CRM114-Status: GOOD ( 14.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The user can specify the virtual machine memory size in MB, which is saved in cfg->ram_size. kvmtool validates it against the host memory size, converted from bytes to MB. ram_size is then converted to bytes, and this is how it is used throughout the rest of kvmtool. To avoid any confusion about the unit of measurement, especially once the user is allowed to specify the unit of measurement, always use ram_size in bytes. Signed-off-by: Alexandru Elisei Reviewed-by: Andre Przywara --- builtin-run.c | 19 ++++++++++--------- include/kvm/kvm-config.h | 7 ++++--- include/kvm/kvm.h | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 0126c9fbcba6..2bf93fe13c92 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -264,7 +265,7 @@ static u64 host_ram_size(void) return 0; } - return (nr_pages * page_size) >> MB_SHIFT; + return (u64)nr_pages * page_size; } /* @@ -278,11 +279,11 @@ static u64 get_ram_size(int nr_cpus) u64 available; u64 ram_size; - ram_size = 64 * (nr_cpus + 3); + ram_size = (u64)SZ_64M * (nr_cpus + 3); available = host_ram_size() * RAM_SIZE_RATIO; if (!available) - available = MIN_RAM_SIZE_MB; + available = MIN_RAM_SIZE; if (ram_size > available) ram_size = available; @@ -595,13 +596,13 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) if (!kvm->cfg.ram_size) kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); + else + kvm->cfg.ram_size <<= MB_SHIFT; if (kvm->cfg.ram_size > host_ram_size()) pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", - (unsigned long long)kvm->cfg.ram_size, - (unsigned long long)host_ram_size()); - - kvm->cfg.ram_size <<= MB_SHIFT; + (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, + (unsigned long long)host_ram_size() >> MB_SHIFT); if (!kvm->cfg.dev) kvm->cfg.dev = DEFAULT_KVM_DEV; @@ -676,12 +677,12 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) if (kvm->cfg.kernel_filename) { printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, kvm->cfg.kernel_filename, - (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, + (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, kvm->cfg.nrcpus, kvm->cfg.guest_name); } else if (kvm->cfg.firmware_filename) { printf(" # %s run --firmware %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, kvm->cfg.firmware_filename, - (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, + (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, kvm->cfg.nrcpus, kvm->cfg.guest_name); } diff --git a/include/kvm/kvm-config.h b/include/kvm/kvm-config.h index 6a5720c4c7d4..31bc89520d52 100644 --- a/include/kvm/kvm-config.h +++ b/include/kvm/kvm-config.h @@ -5,6 +5,8 @@ #include "kvm/vfio.h" #include "kvm/kvm-config-arch.h" +#include + #define DEFAULT_KVM_DEV "/dev/kvm" #define DEFAULT_CONSOLE "serial" #define DEFAULT_NETWORK "user" @@ -15,14 +17,13 @@ #define DEFAULT_SCRIPT "none" #define DEFAULT_SANDBOX_FILENAME "guest/sandbox.sh" -#define MIN_RAM_SIZE_MB (64ULL) -#define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) +#define MIN_RAM_SIZE SZ_64M struct kvm_config { struct kvm_config_arch arch; struct disk_image_params disk_image[MAX_DISK_IMAGES]; struct vfio_device_params *vfio_devices; - u64 ram_size; + u64 ram_size; /* Guest memory size, in bytes */ u8 num_net_devices; u8 num_vfio_devices; u64 vsock_cid; diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index ad732e56f5ed..7b14b33b50ca 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -87,7 +87,7 @@ struct kvm { struct kvm_cpu **cpus; u32 mem_slots; /* for KVM_SET_USER_MEMORY_REGION */ - u64 ram_size; + u64 ram_size; /* Guest memory size, in bytes */ void *ram_start; u64 ram_pagesize; struct mutex mem_banks_lock; From patchwork Thu Jun 16 13:48:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883889 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8F12BC43334 for ; Thu, 16 Jun 2022 13:49:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=yXpUMqOmojI+sWnLm0Qdw49pUWKq41opKWuCldxROys=; b=C6xjfS7AHh57zV NAC2o+1EekYkx0VkYS4ZEvRtqYld1c64QCymQm2xrMj08/AVFEuTTz5Sk610JDwVH8sAGw71vg92c UDkd08YWyJvbIdFeadjM5Nej6nmt6wVJ545fP8ZhJCkCdFTukl24u9zFNNWK2keLLx40IyLgTGPdr IT77YLktHg+QznKXkkiOoxMD5yu+NTxBJPRSzBlOkmY3frRgGzzlbK3D8UGpaR3UN+3s9M8XBakc6 iV2zWLeIZ7DNvWDOEBllmLYzaOJevFePiayXEOxFMC/J1YUgZ/x9BIohbsPvgo0IYk9QfCl9A4NJH 6zEtJApPut3NzEg4SulA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1pre-002Zc8-OL; Thu, 16 Jun 2022 13:48:50 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prK-002ZRn-Ij for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:32 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 800131474; Thu, 16 Jun 2022 06:48:29 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D245A3F7F5; Thu, 16 Jun 2022 06:48:27 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 03/12] builtin-run: Rework RAM size validation Date: Thu, 16 Jun 2022 14:48:19 +0100 Message-Id: <20220616134828.129006-4-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064830_692890_10F83D9C X-CRM114-Status: GOOD ( 12.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org host_ram_size() uses sysconf() to calculate the available ram, and sysconf() can fail. When that happens, host_ram_size() returns 0. kvmtool warns the user when the configured VM ram size exceeds the size of the host's memory, but doesn't take into account that host_ram_size() can return 0. If the function returns zero, skip the warning. Since this can only happen when the user sets the memory size (via the -m/--mem command line argument), skip the check entirely if the user hasn't set it. Move the check to kvm_run_validate_cfg(), as it checks for valid user configuration. Reviewed-by: Andre Przywara Signed-off-by: Alexandru Elisei --- builtin-run.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 2bf93fe13c92..e1770b3c9df2 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -509,6 +509,8 @@ static void kvm_run_set_real_cmdline(struct kvm *kvm) static void kvm_run_validate_cfg(struct kvm *kvm) { + u64 available_ram; + if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename) die("Only one of --kernel or --firmware can be specified"); @@ -518,6 +520,17 @@ static void kvm_run_validate_cfg(struct kvm *kvm) if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename) pr_warning("Ignoring initrd file when loading a firmware image"); + + if (kvm->cfg.ram_size) { + /* User specifies RAM size in megabytes. */ + kvm->cfg.ram_size <<= MB_SHIFT; + available_ram = host_ram_size(); + if (available_ram && kvm->cfg.ram_size > available_ram) { + pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", + (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, + (unsigned long long)available_ram >> MB_SHIFT); + } + } } static struct kvm *kvm_cmd_run_init(int argc, const char **argv) @@ -596,13 +609,6 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) if (!kvm->cfg.ram_size) kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); - else - kvm->cfg.ram_size <<= MB_SHIFT; - - if (kvm->cfg.ram_size > host_ram_size()) - pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", - (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, - (unsigned long long)host_ram_size() >> MB_SHIFT); if (!kvm->cfg.dev) kvm->cfg.dev = DEFAULT_KVM_DEV; From patchwork Thu Jun 16 13:48:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883892 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 32D51C433EF for ; Thu, 16 Jun 2022 13:51:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xGJ2qeS5uMrZFoqU6o3vDHZtDDTekcj10IAW5ZWrZPk=; b=xV6uusxEuLT9n1 fAFyoz4YBu5jXRWr5+6s+dSyBzIQbUHkupCh9dGuolHkATTHLLdtetPP4fAsHrTfTuKUYVEEE6RqJ pSv1LKQXghBbBF68+CynHy6XgMTYbgFOpU868DgbRz9bLqUh2T2TxEwzVcZUPw5C5qc7SlQAE1Vpu Z7P55mRM5jF4FyL9hGf0+tJp4MRpP2+IMqsWsQKS8v1omwKfRr6y7guOt9M++PbO98Q57Z+jzagC7 +ZKXNcpV5DIXzObdD+8YGugzaKS7HfBJfR0ZlOPRmK6xD5uyBUc8CTBRwxFA2iTwBy2p6TRbg/SdC C7+2+D1Mqcv2fFlW0/pw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1psp-002aD2-Ni; Thu, 16 Jun 2022 13:50:03 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prM-002ZSf-Fx for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:34 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4ECC41477; Thu, 16 Jun 2022 06:48:31 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A5B483F7F5; Thu, 16 Jun 2022 06:48:29 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 04/12] builtin-run: Add arch hook to validate VM configuration Date: Thu, 16 Jun 2022 14:48:20 +0100 Message-Id: <20220616134828.129006-5-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064832_666323_7C9BC6A0 X-CRM114-Status: GOOD ( 15.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Architectures are free to set their own command line options. Add an architecture specific hook to validate these options. For now, the hook does nothing, but it will be used in later patches. Signed-off-by: Alexandru Elisei Reviewed-by: Andre Przywara --- Makefile | 1 + arm/aarch32/kvm.c | 5 +++++ arm/aarch64/kvm.c | 4 ++++ builtin-run.c | 2 ++ include/kvm/kvm.h | 1 + mips/kvm.c | 4 ++++ powerpc/kvm.c | 4 ++++ riscv/kvm.c | 4 ++++ x86/kvm.c | 4 ++++ 9 files changed, 29 insertions(+) create mode 100644 arm/aarch32/kvm.c diff --git a/Makefile b/Makefile index 6464446a9f24..64bb9c95b6f6 100644 --- a/Makefile +++ b/Makefile @@ -170,6 +170,7 @@ ifeq ($(ARCH), arm) OBJS += $(OBJS_ARM_COMMON) OBJS += arm/aarch32/arm-cpu.o OBJS += arm/aarch32/kvm-cpu.o + OBJS += arm/aarch32/kvm.o ARCH_INCLUDE := $(HDRS_ARM_COMMON) ARCH_INCLUDE += -Iarm/aarch32/include CFLAGS += -march=armv7-a diff --git a/arm/aarch32/kvm.c b/arm/aarch32/kvm.c new file mode 100644 index 000000000000..ae33ac92479a --- /dev/null +++ b/arm/aarch32/kvm.c @@ -0,0 +1,5 @@ +#include "kvm/kvm.h" + +void kvm__arch_validate_cfg(struct kvm *kvm) +{ +} diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index f3fe854e0b3f..ca348f118a56 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -37,6 +37,10 @@ int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset) return 0; } +void kvm__arch_validate_cfg(struct kvm *kvm) +{ +} + /* * Return the TEXT_OFFSET value that the guest kernel expects. Note * that pre-3.17 kernels expose this value using the native endianness diff --git a/builtin-run.c b/builtin-run.c index e1770b3c9df2..dcd08f739469 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -531,6 +531,8 @@ static void kvm_run_validate_cfg(struct kvm *kvm) (unsigned long long)available_ram >> MB_SHIFT); } } + + kvm__arch_validate_cfg(kvm); } static struct kvm *kvm_cmd_run_init(int argc, const char **argv) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 7b14b33b50ca..9f7b2fb26e95 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -187,6 +187,7 @@ int kvm__get_sock_by_instance(const char *name); int kvm__enumerate_instances(int (*callback)(const char *name, int pid)); void kvm__remove_socket(const char *name); +void kvm__arch_validate_cfg(struct kvm *kvm); void kvm__arch_set_cmdline(char *cmdline, bool video); void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size); void kvm__arch_delete_ram(struct kvm *kvm); diff --git a/mips/kvm.c b/mips/kvm.c index e668cbbefb25..cebec5ae0178 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -13,6 +13,10 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 } }; +void kvm__arch_validate_cfg(struct kvm *kvm) +{ +} + void kvm__arch_read_term(struct kvm *kvm) { virtio_console__inject_interrupt(kvm); diff --git a/powerpc/kvm.c b/powerpc/kvm.c index 702d67dca614..3215b579f5dc 100644 --- a/powerpc/kvm.c +++ b/powerpc/kvm.c @@ -48,6 +48,10 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 } }; +void kvm__arch_validate_cfg(struct kvm *kvm) +{ +} + static uint32_t mfpvr(void) { uint32_t r; diff --git a/riscv/kvm.c b/riscv/kvm.c index 84e02779a91c..7fb496282f4c 100644 --- a/riscv/kvm.c +++ b/riscv/kvm.c @@ -13,6 +13,10 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 }, }; +void kvm__arch_validate_cfg(struct kvm *kvm) +{ +} + bool kvm__arch_cpu_supports_vm(void) { /* The KVM capability check is enough. */ diff --git a/x86/kvm.c b/x86/kvm.c index 3e0f0b743f8c..6683a5c81d49 100644 --- a/x86/kvm.c +++ b/x86/kvm.c @@ -35,6 +35,10 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 } }; +void kvm__arch_validate_cfg(struct kvm *kvm) +{ +} + bool kvm__arch_cpu_supports_vm(void) { struct cpuid_regs regs; From patchwork Thu Jun 16 13:48:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883893 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 26370C43334 for ; Thu, 16 Jun 2022 13:51:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7qeHXx9+cIiKCwhVHSXiLBuUh1bfn+Qu3ycTCT4VzMs=; b=Sc64BMj8zGoVfu Z69/xvaD0OweQEbaEPbzxKurIfpug3U2stgeGy7qi2siIqj3wsMexJLHjD+1vcCcMAUpFdytQE0LH 30Z8poKRJC9g4ZGSQvZCGqIB74wpfZf8/N+bGPk/Ksbh2VK+B7Lm+ypbLHoSjvyShNm55HotNIzaT O6QzUgc/FKDMld+WwjaEb44J3Au2BCyEJalHDgS7JvOHqvuo2VxPQ33WqcluuBThnZiT5ldZrYGb+ amhP3O8RzHDsPdAxa2LlZCWvgxNPUw+9U+ogO4pWQoZ95fxu5orKXZpH1xiK/yD178cUGLyZsnKKw /LRmXMdilpWfLhxbwIRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1ptB-002aOb-Rj; Thu, 16 Jun 2022 13:50:25 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prO-002ZUP-RX for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:36 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1F9D612FC; Thu, 16 Jun 2022 06:48:33 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 762273F7F5; Thu, 16 Jun 2022 06:48:31 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 05/12] arm/arm64: Fail if RAM size is too large for 32-bit guests Date: Thu, 16 Jun 2022 14:48:21 +0100 Message-Id: <20220616134828.129006-6-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064834_987896_BECB7E85 X-CRM114-Status: GOOD ( 11.02 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org For 64-bit guests, kvmtool exists with an error in kvm__get_vm_type() if the memory size is larger than what KVM supports. For 32-bit guests, the RAM size is silently rounded down to ARM_LOMAP_MAX_MEMORY in kvm__arch_init(). Be consistent and exit with an error when the user has configured the wrong RAM size for 32-bit guests. Reviewed-by: Andre Przywara Signed-off-by: Alexandru Elisei --- arm/aarch32/kvm.c | 4 ++++ arm/aarch64/kvm.c | 5 +++++ arm/kvm.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arm/aarch32/kvm.c b/arm/aarch32/kvm.c index ae33ac92479a..9d68d7a15ee2 100644 --- a/arm/aarch32/kvm.c +++ b/arm/aarch32/kvm.c @@ -2,4 +2,8 @@ void kvm__arch_validate_cfg(struct kvm *kvm) { + if (kvm->cfg.ram_size > ARM_LOMAP_MAX_MEMORY) { + die("RAM size 0x%llx exceeds maximum allowed 0x%llx", + kvm->cfg.ram_size, ARM_LOMAP_MAX_MEMORY); + } } diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index ca348f118a56..2134528bf7f2 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -39,6 +39,11 @@ int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset) void kvm__arch_validate_cfg(struct kvm *kvm) { + if (kvm->cfg.arch.aarch32_guest && + kvm->cfg.ram_size > ARM_LOMAP_MAX_MEMORY) { + die("RAM size 0x%llx exceeds maximum allowed 0x%llx", + kvm->cfg.ram_size, ARM_LOMAP_MAX_MEMORY); + } } /* diff --git a/arm/kvm.c b/arm/kvm.c index c5913000e1ed..af0feae495d7 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -65,7 +65,7 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) * If using THP, then our minimal alignment becomes 2M. * 2M trumps 64K, so let's go with that. */ - kvm->ram_size = min(ram_size, (u64)ARM_MAX_MEMORY(kvm)); + kvm->ram_size = ram_size; kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M; kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path, kvm->arch.ram_alloc_size); From patchwork Thu Jun 16 13:48:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883894 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 886DFC433EF for ; Thu, 16 Jun 2022 13:51:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=OHURWrbQQ/XE7Ybe3xUFmm3zR8hl78HLHrUghne2oOY=; b=syHQN5a+EcVbIv cxPZc1t7aZetkrTy0RP8K081ePMENewZS9wRjsKLcFeqnW3JLn6mk4bjz4OZ+3UCmElr8B/KdJX59 5zfM/Sy7P1IYLCWAv2zxN+pCc6d0Cl8ooTC8ngaYF1BeYDPs7BsxlWpjlV4CMLYwhrefdaysNZqXo KWIfickk8isbnwE+SCUQ/ilMjRBWfP2snxQZrOoOIJcZ3YLcjXr61n2DK9xhJ2O+64Gz2nUWnOA6l sY5MVwoOQUvxha6kgmJ73HehhrqDCnHuom3gizMRrz08sAcza1bDeEnjhzibbIpBa/IF2fuIOheBI jSU8DxZusSiPFYIj95sg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1ptY-002aac-5i; Thu, 16 Jun 2022 13:50:48 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prQ-002ZVG-6i for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:38 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E659511FB; Thu, 16 Jun 2022 06:48:34 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 47A7F3F7F5; Thu, 16 Jun 2022 06:48:33 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 06/12] arm/arm64: Kill the ARM_MAX_MEMORY() macro Date: Thu, 16 Jun 2022 14:48:22 +0100 Message-Id: <20220616134828.129006-7-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064836_383178_1BC42BCA X-CRM114-Status: UNSURE ( 8.32 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org For 32-bit guests, the maximum memory size is represented by the define ARM_LOMAP_MAX_MEMORY, which ARM_MAX_MEMORY() returns. For 64-bit guests, the RAM size is checked against the maximum allowed by KVM in kvm__get_vm_type(). There are no users left for the ARM_MAX_MEMORY() macro, remove it. Reviewed-by: Andre Przywara Signed-off-by: Alexandru Elisei --- arm/aarch32/include/kvm/kvm-arch.h | 2 -- arm/aarch64/include/kvm/kvm-arch.h | 16 ---------------- 2 files changed, 18 deletions(-) diff --git a/arm/aarch32/include/kvm/kvm-arch.h b/arm/aarch32/include/kvm/kvm-arch.h index 5616b27e257e..467fb09175b8 100644 --- a/arm/aarch32/include/kvm/kvm-arch.h +++ b/arm/aarch32/include/kvm/kvm-arch.h @@ -8,8 +8,6 @@ struct kvm; static inline void kvm__arch_enable_mte(struct kvm *kvm) {} -#define ARM_MAX_MEMORY(...) ARM_LOMAP_MAX_MEMORY - #define MAX_PAGE_SIZE SZ_4K #include "arm-common/kvm-arch.h" diff --git a/arm/aarch64/include/kvm/kvm-arch.h b/arm/aarch64/include/kvm/kvm-arch.h index 9124f6919d0f..ff857ca6e7b4 100644 --- a/arm/aarch64/include/kvm/kvm-arch.h +++ b/arm/aarch64/include/kvm/kvm-arch.h @@ -8,22 +8,6 @@ unsigned long long kvm__arch_get_kern_offset(struct kvm *kvm, int fd); int kvm__arch_get_ipa_limit(struct kvm *kvm); void kvm__arch_enable_mte(struct kvm *kvm); -#define ARM_MAX_MEMORY(kvm) ({ \ - u64 max_ram; \ - \ - if ((kvm)->cfg.arch.aarch32_guest) { \ - max_ram = ARM_LOMAP_MAX_MEMORY; \ - } else { \ - int ipabits = kvm__arch_get_ipa_limit(kvm); \ - if (ipabits <= 0) \ - max_ram = ARM_HIMAP_MAX_MEMORY; \ - else \ - max_ram = (1ULL << ipabits) - ARM_MEMORY_AREA; \ - } \ - \ - max_ram; \ -}) - #define MAX_PAGE_SIZE SZ_64K #include "arm-common/kvm-arch.h" From patchwork Thu Jun 16 13:48:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883895 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F361CC43334 for ; Thu, 16 Jun 2022 13:52:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=aHFTFrrfwEGC+R5KKB6BjUjxJ5j5fDimDA5ZiON6+YE=; b=tR03ca952+6W6J WMVfMqVz03LrHoTZF/46n5q9o4x5+rDYQ1DXOOE9zfANn9Mo3NWvuA91I6WxXEu2GgteH1RmGs4N0 USWWBed9dsh5Huq6Tq76jTnKuGPjFjgmSYymEzmOMhK20bRsmaiI2P5fa5CtjlMwpOgciA22+we+X wL8S/eQWssv2V1IF0TVoYHCYFiMiPZM3fVcVr31FghDtzLi4dBUSjeTr2QVeKxot7RXrEDGLJsu9Z 1xgEcc1jaiMLk210vdh7vCKQyI8t8dMyDx2SDOGf6WG5XbZ/6zgNz+00tnzl66An0EzDy0zndCTHg YxnIlvmFLKpX4yephPHw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1ptp-002amS-Sg; Thu, 16 Jun 2022 13:51:05 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prR-002ZUP-8u for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:38 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B8BD613D5; Thu, 16 Jun 2022 06:48:36 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 19E283F7F5; Thu, 16 Jun 2022 06:48:34 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 07/12] arm/arm64: Kill the ARM_HIMAP_MAX_MEMORY() macro Date: Thu, 16 Jun 2022 14:48:23 +0100 Message-Id: <20220616134828.129006-8-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064837_410479_A9E2319A X-CRM114-Status: UNSURE ( 7.74 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The ARM_HIMAP_MAX_MEMORY() is a remnant of a time when KVM only supported 40 bits if IPA. There are no users left for this macro, remove it. Reviewed-by: Andre Przywara Signed-off-by: Alexandru Elisei --- arm/include/arm-common/kvm-arch.h | 1 - 1 file changed, 1 deletion(-) diff --git a/arm/include/arm-common/kvm-arch.h b/arm/include/arm-common/kvm-arch.h index fc55360d4d15..6d80aac17125 100644 --- a/arm/include/arm-common/kvm-arch.h +++ b/arm/include/arm-common/kvm-arch.h @@ -65,7 +65,6 @@ #define ARM_LOMAP_MAX_MEMORY ((1ULL << 32) - ARM_MEMORY_AREA) -#define ARM_HIMAP_MAX_MEMORY ((1ULL << 40) - ARM_MEMORY_AREA) #define KVM_IOEVENTFD_HAS_PIO 0 From patchwork Thu Jun 16 13:48:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883896 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 72392C43334 for ; Thu, 16 Jun 2022 13:52:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3r64hbXkqG3L10SXtqRFxMl+tnPvPH2SU2ImXnHfVa0=; b=PhrXLxBE7mh8aq jooDbaoe2L3Bp8XOjMKRdxgq1tzKAwswoXpuvCHW+tskA79c11StJKDrCzCdkClhf3TadEi0ZtX3A wWnh0d1UXQxtHZb4XZIpm1RL4BQeUL+bIKpQBbYdaMui0Q9MUZBVmV4r+ns0c7mQPMiEt0yg6xo38 OxRE9/ba97iEZZKA/gyb4//yeK9MoeXcAI7bU004Tnjc/ySYqy38FfPeeS49yfjCj0pT4xlJ4+A04 X70+ZkVIk3IqJ+1YpctE6wTEmwR1o/KZoOl4Upw85ynDhFmlZ6PH5iIJuX/khQdwW11hk4wdFbOc5 ML32G527s92QTVOESwfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1puC-002b0L-2m; Thu, 16 Jun 2022 13:51:28 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prT-002ZX0-Mx for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:42 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 88FF212FC; Thu, 16 Jun 2022 06:48:38 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E03E33F7F5; Thu, 16 Jun 2022 06:48:36 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 08/12] builtin_run: Allow standard size specifiers for memory Date: Thu, 16 Jun 2022 14:48:24 +0100 Message-Id: <20220616134828.129006-9-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064839_927044_25B0E235 X-CRM114-Status: GOOD ( 13.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Suzuki K Poulose Allow the user to use the standard B (bytes), K (kilobytes), M (megabytes), G (gigabytes), T (terabytes) and P (petabytes) suffixes for memory size. When none are specified, the default is megabytes. Also raise an error if the guest specifies 0 as the memory size, instead of treating it as uninitialized, as kvmtool has done so far. Signed-off-by: Suzuki K Poulose Signed-off-by: Alexandru Elisei Reviewed-by: Andre Przywara --- builtin-run.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index dcd08f739469..8b4e865f0a0e 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -49,9 +49,11 @@ #include #include -#define MB_SHIFT (20) #define KB_SHIFT (10) +#define MB_SHIFT (20) #define GB_SHIFT (30) +#define TB_SHIFT (40) +#define PB_SHIFT (50) __thread struct kvm_cpu *current_kvm_cpu; @@ -87,6 +89,54 @@ void kvm_run_set_wrapper_sandbox(void) kvm_run_wrapper = KVM_RUN_SANDBOX; } +static int parse_mem_unit(char **next) +{ + switch (**next) { + case 'B': case 'b': (*next)++; return 0; + case 'K': case 'k': (*next)++; return KB_SHIFT; + case 'M': case 'm': (*next)++; return MB_SHIFT; + case 'G': case 'g': (*next)++; return GB_SHIFT; + case 'T': case 't': (*next)++; return TB_SHIFT; + case 'P': case 'p': (*next)++; return PB_SHIFT; + } + + return MB_SHIFT; +} + +static u64 parse_mem_option(const char *nptr, char **next) +{ + u64 shift; + u64 val; + + errno = 0; + val = strtoull(nptr, next, 10); + if (errno == ERANGE) + die("Memory too large: %s", nptr); + if (*next == nptr) + die("Invalid memory specifier: %s", nptr); + + shift = parse_mem_unit(next); + if ((val << shift) < val) + die("Memory too large: %s", nptr); + + return val << shift; +} + +static int mem_parser(const struct option *opt, const char *arg, int unset) +{ + struct kvm *kvm = opt->ptr; + char *next; + + kvm->cfg.ram_size = parse_mem_option(arg, &next); + if (kvm->cfg.ram_size == 0) + die("Invalid RAM size: %s", arg); + + if (*next != '\0') + die("Invalid memory specifier: %s", arg); + + return 0; +} + #ifndef OPT_ARCH_RUN #define OPT_ARCH_RUN(...) #endif @@ -97,8 +147,9 @@ void kvm_run_set_wrapper_sandbox(void) OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name", \ "A name for the guest"), \ OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"), \ - OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory" \ - " size in MB."), \ + OPT_CALLBACK('m', "mem", NULL, "size[BKMGTP]", \ + "Virtual machine memory size, by default measured" \ + " in megabytes (M)", mem_parser, kvm), \ OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk " \ " image or rootfs directory", img_name_parser, \ kvm), \ @@ -522,8 +573,6 @@ static void kvm_run_validate_cfg(struct kvm *kvm) pr_warning("Ignoring initrd file when loading a firmware image"); if (kvm->cfg.ram_size) { - /* User specifies RAM size in megabytes. */ - kvm->cfg.ram_size <<= MB_SHIFT; available_ram = host_ram_size(); if (available_ram && kvm->cfg.ram_size > available_ram) { pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", From patchwork Thu Jun 16 13:48:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883897 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 94DC9C43334 for ; Thu, 16 Jun 2022 13:52:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=oY4m+N/TaSm64g8GqMKHoBRtDsQ4vgktM5CHQ/k+FqE=; b=KvxFFeD7xwOrLo nWKTyCeCSa+u3REvYdU2nRXeBOrDKs3rXLpkRj6Gf9wz1GkJp//hKthKEsLWkzOwA/BOCSzEwgSuP 5RbZmXjqbw5bGw0Zpa9ibqjsQinNbw5r2KK6MPKlz2GbPtra+VUdITVK31VobjU7wod1OwuuUQ+hE g504v4REvAEIM7wIhGC7pvnAktseCE0z1dAYfUX7xZrT+tv5y9/MOL8s8yBztb9PsLbmeO8VZv/g8 RHCDKsEuRK14GYE5RGalBSI2OuD4M5UZyME4/cm0cmH4Q5TKDoK/8dPiBQrESccTW3gTBQsj6p7wV Le66YXDaqm+A1Xp/Zm1A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1pue-002bFX-Ph; Thu, 16 Jun 2022 13:51:57 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prU-002ZUP-U3 for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:43 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7DB7211FB; Thu, 16 Jun 2022 06:48:40 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B0BDD3F7F5; Thu, 16 Jun 2022 06:48:38 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Cc: Julien Grall Subject: [PATCH v4 kvmtool 09/12] kvm__arch_init: Remove hugetlbfs_path and ram_size as parameters Date: Thu, 16 Jun 2022 14:48:25 +0100 Message-Id: <20220616134828.129006-10-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064841_111927_AC4948B3 X-CRM114-Status: GOOD ( 12.19 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Julien Grall The kvm struct already contains a pointer to the configuration, which contains both hugetlbfs_path and ram_size, so is it not necessary to pass them as arguments to kvm__arch_init(). Reviewed-by: Andre Przywara Signed-off-by: Julien Grall Signed-off-by: Alexandru Elisei --- arm/kvm.c | 7 ++++--- include/kvm/kvm.h | 2 +- kvm.c | 2 +- mips/kvm.c | 7 ++++--- powerpc/kvm.c | 5 +++-- riscv/kvm.c | 7 ++++--- x86/kvm.c | 4 +++- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/arm/kvm.c b/arm/kvm.c index af0feae495d7..bd44aa350796 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -57,7 +57,7 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) { } -void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) +void kvm__arch_init(struct kvm *kvm) { /* * Allocate guest memory. We must align our buffer to 64K to @@ -65,9 +65,10 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) * If using THP, then our minimal alignment becomes 2M. * 2M trumps 64K, so let's go with that. */ - kvm->ram_size = ram_size; + kvm->ram_size = kvm->cfg.ram_size; kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M; - kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path, + kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, + kvm->cfg.hugetlbfs_path, kvm->arch.ram_alloc_size); if (kvm->arch.ram_alloc_start == MAP_FAILED) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 9f7b2fb26e95..640b76c095f9 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -189,7 +189,7 @@ void kvm__remove_socket(const char *name); void kvm__arch_validate_cfg(struct kvm *kvm); void kvm__arch_set_cmdline(char *cmdline, bool video); -void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size); +void kvm__arch_init(struct kvm *kvm); void kvm__arch_delete_ram(struct kvm *kvm); int kvm__arch_setup_firmware(struct kvm *kvm); int kvm__arch_free_firmware(struct kvm *kvm); diff --git a/kvm.c b/kvm.c index 952ef1fbb41c..42b881217df6 100644 --- a/kvm.c +++ b/kvm.c @@ -479,7 +479,7 @@ int kvm__init(struct kvm *kvm) goto err_vm_fd; } - kvm__arch_init(kvm, kvm->cfg.hugetlbfs_path, kvm->cfg.ram_size); + kvm__arch_init(kvm); INIT_LIST_HEAD(&kvm->mem_banks); kvm__init_ram(kvm); diff --git a/mips/kvm.c b/mips/kvm.c index cebec5ae0178..fb60b210e7fc 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -62,12 +62,13 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) } /* Architecture-specific KVM init */ -void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) +void kvm__arch_init(struct kvm *kvm) { int ret; - kvm->ram_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path, ram_size); - kvm->ram_size = ram_size; + kvm->ram_size = kvm->cfg.ram_size; + kvm->ram_start = mmap_anon_or_hugetlbfs(kvm, kvm->cfg.hugetlbfs_path, + kvm->ram_size); if (kvm->ram_start == MAP_FAILED) die("out of memory"); diff --git a/powerpc/kvm.c b/powerpc/kvm.c index 3215b579f5dc..d281b070fd0e 100644 --- a/powerpc/kvm.c +++ b/powerpc/kvm.c @@ -92,12 +92,13 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) } /* Architecture-specific KVM init */ -void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) +void kvm__arch_init(struct kvm *kvm) { + const char *hugetlbfs_path = kvm->cfg.hugetlbfs_path; int cap_ppc_rma; unsigned long hpt; - kvm->ram_size = ram_size; + kvm->ram_size = kvm->cfg.ram_size; /* Map "default" hugetblfs path to the standard 16M mount point */ if (hugetlbfs_path && !strcmp(hugetlbfs_path, "default")) diff --git a/riscv/kvm.c b/riscv/kvm.c index 7fb496282f4c..c46660772aa0 100644 --- a/riscv/kvm.c +++ b/riscv/kvm.c @@ -56,7 +56,7 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) { } -void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) +void kvm__arch_init(struct kvm *kvm) { /* * Allocate guest memory. We must align our buffer to 64K to @@ -64,9 +64,10 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) * If using THP, then our minimal alignment becomes 2M. * 2M trumps 64K, so let's go with that. */ - kvm->ram_size = min(ram_size, (u64)RISCV_MAX_MEMORY(kvm)); + kvm->ram_size = min(kvm->cfg.ram_size, (u64)RISCV_MAX_MEMORY(kvm)); kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M; - kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path, + kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, + kvm->cfg.hugetlbfs_path, kvm->arch.ram_alloc_size); if (kvm->arch.ram_alloc_start == MAP_FAILED) diff --git a/x86/kvm.c b/x86/kvm.c index 6683a5c81d49..24b0305a1841 100644 --- a/x86/kvm.c +++ b/x86/kvm.c @@ -134,9 +134,11 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) } /* Architecture-specific KVM init */ -void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size) +void kvm__arch_init(struct kvm *kvm) { + const char *hugetlbfs_path = kvm->cfg.hugetlbfs_path; struct kvm_pit_config pit_config = { .flags = 0, }; + u64 ram_size = kvm->cfg.ram_size; int ret; ret = ioctl(kvm->vm_fd, KVM_SET_TSS_ADDR, 0xfffbd000); From patchwork Thu Jun 16 13:48:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883898 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2DF72C433EF for ; Thu, 16 Jun 2022 13:53:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=mpSGjK4ZL3XFQ9BqoGzSaJeu9QZc/iGBRVDMMHazpGI=; b=Gtdyj7opSVHWjq +qrKg5UvWvQe7hX/5I4qLWGSKLnrH++1xVzF8NLMcRi0IBwZ4ASZIkAX1V82gN+V1+SRA8tu994cC L6l9QNvImJfiJXthriC9auwAgRG4Hd5w3HMsI2qEKwGgU1M7u8T4AGr1HpsBgjtr7Vq37tpYzKUOf ZSzwFw2cJ3loLyK36+XfdDF74ognt69kkTpq9mrEhQdigie6n7Fp9F/W0AMxtVvft8zY6V/JY7ZO9 aQHPa6MAEraZASeGSleYHWIq9NytlXAyPndIjjpTNdFORQJhFWb9iN7EEoNLbmlP1hOtpvDdaVmtC P/Jz9tRBjC4pZXG7L1cA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1puz-002bRC-TH; Thu, 16 Jun 2022 13:52:18 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prX-002ZX0-Iw for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:45 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 70BC212FC; Thu, 16 Jun 2022 06:48:42 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A50483F7F5; Thu, 16 Jun 2022 06:48:40 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Cc: Julien Grall Subject: [PATCH v4 kvmtool 10/12] arm/arm64: Consolidate RAM initialization in kvm__init_ram() Date: Thu, 16 Jun 2022 14:48:26 +0100 Message-Id: <20220616134828.129006-11-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064843_752476_F8329467 X-CRM114-Status: GOOD ( 13.02 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Julien Grall RAM initialization is unnecessarily split between kvm__init_ram() and kvm__arch_init(). Move all code related to RAM initialization to kvm__init_ram(), making the code easier to follow and to modify. One thing to note is that the initialization order is slightly altered: kvm__arch_enable_mte() and gic__create() are now called before mmap'ing the guest RAM. That is perfectly fine, as they don't use the host's mapping of the guest memory. Reviewed-by: Andre Przywara Signed-off-by: Julien Grall Signed-off-by: Alexandru Elisei --- arm/kvm.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/arm/kvm.c b/arm/kvm.c index bd44aa350796..abcccfabf59e 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -26,9 +26,34 @@ bool kvm__arch_cpu_supports_vm(void) void kvm__init_ram(struct kvm *kvm) { - int err; u64 phys_start, phys_size; void *host_mem; + int err; + + /* + * Allocate guest memory. We must align our buffer to 64K to + * correlate with the maximum guest page size for virtio-mmio. + * If using THP, then our minimal alignment becomes 2M. + * 2M trumps 64K, so let's go with that. + */ + kvm->ram_size = kvm->cfg.ram_size; + kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M; + kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, + kvm->cfg.hugetlbfs_path, + kvm->arch.ram_alloc_size); + + if (kvm->arch.ram_alloc_start == MAP_FAILED) + die("Failed to map %lld bytes for guest memory (%d)", + kvm->arch.ram_alloc_size, errno); + + kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start, + SZ_2M); + + madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, + MADV_MERGEABLE); + + madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, + MADV_HUGEPAGE); phys_start = ARM_MEMORY_AREA; phys_size = kvm->ram_size; @@ -59,31 +84,6 @@ void kvm__arch_set_cmdline(char *cmdline, bool video) void kvm__arch_init(struct kvm *kvm) { - /* - * Allocate guest memory. We must align our buffer to 64K to - * correlate with the maximum guest page size for virtio-mmio. - * If using THP, then our minimal alignment becomes 2M. - * 2M trumps 64K, so let's go with that. - */ - kvm->ram_size = kvm->cfg.ram_size; - kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M; - kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, - kvm->cfg.hugetlbfs_path, - kvm->arch.ram_alloc_size); - - if (kvm->arch.ram_alloc_start == MAP_FAILED) - die("Failed to map %lld bytes for guest memory (%d)", - kvm->arch.ram_alloc_size, errno); - - kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start, - SZ_2M); - - madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, - MADV_MERGEABLE); - - madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, - MADV_HUGEPAGE); - /* Create the virtual GIC. */ if (gic__create(kvm, kvm->cfg.arch.irqchip)) die("Failed to create virtual GIC"); From patchwork Thu Jun 16 13:48:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883906 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 39B5DC43334 for ; Thu, 16 Jun 2022 13:53:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7/rlRpmIT/+m2XGW3b129aloo6zg01HAZx+6uhTiAUU=; b=JXi25vv4jEDgtE ZDY5kx/R5BdQ+TEdLFmSmcAyzlh+wgGANFpHXDbbwgf7w/QAW9unDV+Qp9BxlV3WY6Rc4bQV3I/w9 ovPwsLQAVbVMNuChCQI/QaJ5v/3U7D3nehcTJ1m5hP8Y6F4kOOYT1U4kkoToaJRTSA+XIstmEup2j lpDv1siNq19TqnleWKVJEzerk73TFbCz2q4V3gSWqdGeSNBFjn3ii1Anli5hCjNHXdnh8Ydv/PQEm plDiNV7Fzcr86CDuuWD7QGx1N8/FPxrRAmC6dYGA9jyp9Czzkd/hL1aeErMqfPmRpwr8xXE8/gr1U YEaxqQyf0r1EE/Z9C2WQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1pvN-002bi7-56; Thu, 16 Jun 2022 13:52:41 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prY-002ZUP-AK for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:45 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 42AA811FB; Thu, 16 Jun 2022 06:48:44 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 980283F7F5; Thu, 16 Jun 2022 06:48:42 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 11/12] Introduce kvm__arch_default_ram_address() Date: Thu, 16 Jun 2022 14:48:27 +0100 Message-Id: <20220616134828.129006-12-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064844_480734_AA36CC2A X-CRM114-Status: GOOD ( 10.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a new function, kvm__arch_default_ram_address(), which returns the default address for guest RAM for each architecture. Reviewed-by: Andre Przywara Signed-off-by: Alexandru Elisei --- arm/aarch32/kvm.c | 5 +++++ arm/aarch64/kvm.c | 5 +++++ include/kvm/kvm.h | 1 + mips/kvm.c | 5 +++++ powerpc/kvm.c | 5 +++++ riscv/kvm.c | 5 +++++ x86/kvm.c | 5 +++++ 7 files changed, 31 insertions(+) diff --git a/arm/aarch32/kvm.c b/arm/aarch32/kvm.c index 9d68d7a15ee2..768a56bbb5b4 100644 --- a/arm/aarch32/kvm.c +++ b/arm/aarch32/kvm.c @@ -7,3 +7,8 @@ void kvm__arch_validate_cfg(struct kvm *kvm) kvm->cfg.ram_size, ARM_LOMAP_MAX_MEMORY); } } + +u64 kvm__arch_default_ram_address(void) +{ + return ARM_MEMORY_AREA; +} diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index 2134528bf7f2..357936844046 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -46,6 +46,11 @@ void kvm__arch_validate_cfg(struct kvm *kvm) } } +u64 kvm__arch_default_ram_address(void) +{ + return ARM_MEMORY_AREA; +} + /* * Return the TEXT_OFFSET value that the guest kernel expects. Note * that pre-3.17 kernels expose this value using the native endianness diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 640b76c095f9..360430b78b1e 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -190,6 +190,7 @@ void kvm__remove_socket(const char *name); void kvm__arch_validate_cfg(struct kvm *kvm); void kvm__arch_set_cmdline(char *cmdline, bool video); void kvm__arch_init(struct kvm *kvm); +u64 kvm__arch_default_ram_address(void); void kvm__arch_delete_ram(struct kvm *kvm); int kvm__arch_setup_firmware(struct kvm *kvm); int kvm__arch_free_firmware(struct kvm *kvm); diff --git a/mips/kvm.c b/mips/kvm.c index fb60b210e7fc..0faa03a93518 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -13,6 +13,11 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 } }; +u64 kvm__arch_default_ram_address(void) +{ + return 0; +} + void kvm__arch_validate_cfg(struct kvm *kvm) { } diff --git a/powerpc/kvm.c b/powerpc/kvm.c index d281b070fd0e..7b0d0669aff4 100644 --- a/powerpc/kvm.c +++ b/powerpc/kvm.c @@ -48,6 +48,11 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 } }; +u64 kvm__arch_default_ram_address(void) +{ + return 0; +} + void kvm__arch_validate_cfg(struct kvm *kvm) { } diff --git a/riscv/kvm.c b/riscv/kvm.c index c46660772aa0..4d6f5cb57ac8 100644 --- a/riscv/kvm.c +++ b/riscv/kvm.c @@ -13,6 +13,11 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 }, }; +u64 kvm__arch_default_ram_address(void) +{ + return RISCV_RAM; +} + void kvm__arch_validate_cfg(struct kvm *kvm) { } diff --git a/x86/kvm.c b/x86/kvm.c index 24b0305a1841..328fa7500596 100644 --- a/x86/kvm.c +++ b/x86/kvm.c @@ -35,6 +35,11 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 } }; +u64 kvm__arch_default_ram_address(void) +{ + return 0; +} + void kvm__arch_validate_cfg(struct kvm *kvm) { } From patchwork Thu Jun 16 13:48:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12883907 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 67F2DC433EF for ; Thu, 16 Jun 2022 13:54:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=98L8nraP08Uxrc/k/k2qeG3I6hWoadcHxh8HPLuuk98=; b=yz/+LHcOcUugqS TeNFck1PanB1x0QXCZQco2nOLJFmWwL0CemXyunPRN7WszMQ9Q4NTytt0fznfqr6pka8RZEbwtWu1 h4VmIhicWkX2L0fR1nX/84rHtDmf3y3CoJrdjdGJPbXpKUaAVNCqqt0oJ1ltUT1/GWICF0cgqcDzG 9s1/A97QMlkQeEAWLaNc+6nam/WvXVfEgBY0Ng5YlRkPXKIfpOwCoBdEE8Kj98Rwx2PNtJF08DlZw rbCUH2kSUzojCjCbCfCh/THFhyFZ3xs8ZCBXDnH2tI+zhRIpXrG+YDiFKEeYD7kH+X9KMpQjrx7ki 0TtjQ7MRWC92chnCf2YA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1pvj-002buM-AE; Thu, 16 Jun 2022 13:53:03 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o1prb-002ZbB-BX for linux-arm-kernel@lists.infradead.org; Thu, 16 Jun 2022 13:48:49 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 15A4E12FC; Thu, 16 Jun 2022 06:48:46 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 69FC63F7F5; Thu, 16 Jun 2022 06:48:44 -0700 (PDT) From: Alexandru Elisei To: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org, suzuki.poulose@arm.com, julien@xen.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, james.morse@arm.com, andre.przywara@arm.com Subject: [PATCH v4 kvmtool 12/12] arm64: Allow the user to specify the RAM base address Date: Thu, 16 Jun 2022 14:48:28 +0100 Message-Id: <20220616134828.129006-13-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220616134828.129006-1-alexandru.elisei@arm.com> References: <20220616134828.129006-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220616_064847_530871_C7AF06DD X-CRM114-Status: GOOD ( 19.96 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Allow the user to specify the RAM base address by using -m/--mem size@addr command line argument. The base address must be above 2GB, as to not overlap with the MMIO I/O region. Reviewed-by: Andre Przywara Signed-off-by: Alexandru Elisei --- arm/aarch64/include/kvm/kvm-arch.h | 2 ++ arm/aarch64/kvm.c | 14 ++++++++---- arm/kvm.c | 7 ++++-- builtin-run.c | 36 ++++++++++++++++++++++++++---- include/kvm/kvm-config.h | 1 + include/kvm/kvm.h | 12 ++++++++++ include/linux/sizes.h | 2 ++ 7 files changed, 64 insertions(+), 10 deletions(-) diff --git a/arm/aarch64/include/kvm/kvm-arch.h b/arm/aarch64/include/kvm/kvm-arch.h index ff857ca6e7b4..02d09a413831 100644 --- a/arm/aarch64/include/kvm/kvm-arch.h +++ b/arm/aarch64/include/kvm/kvm-arch.h @@ -10,6 +10,8 @@ void kvm__arch_enable_mte(struct kvm *kvm); #define MAX_PAGE_SIZE SZ_64K +#define ARCH_HAS_CFG_RAM_ADDRESS 1 + #include "arm-common/kvm-arch.h" #endif /* KVM__KVM_ARCH_H */ diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index 357936844046..54200c9eec9d 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -4,6 +4,7 @@ #include #include +#include #include @@ -39,10 +40,15 @@ int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset) void kvm__arch_validate_cfg(struct kvm *kvm) { + + if (kvm->cfg.ram_addr < ARM_MEMORY_AREA) { + die("RAM address is below the I/O region ending at %luGB", + ARM_MEMORY_AREA >> 30); + } + if (kvm->cfg.arch.aarch32_guest && - kvm->cfg.ram_size > ARM_LOMAP_MAX_MEMORY) { - die("RAM size 0x%llx exceeds maximum allowed 0x%llx", - kvm->cfg.ram_size, ARM_LOMAP_MAX_MEMORY); + kvm->cfg.ram_addr + kvm->cfg.ram_size > SZ_4G) { + die("RAM extends above 4GB"); } } @@ -117,7 +123,7 @@ int kvm__get_vm_type(struct kvm *kvm) return 0; /* Otherwise, compute the minimal required IPA size */ - max_ipa = ARM_MEMORY_AREA + kvm->cfg.ram_size - 1; + max_ipa = kvm->cfg.ram_addr + kvm->cfg.ram_size - 1; ipa_bits = max(32, fls_long(max_ipa)); pr_debug("max_ipa %lx ipa_bits %d max_ipa_bits %d", max_ipa, ipa_bits, max_ipa_bits); diff --git a/arm/kvm.c b/arm/kvm.c index abcccfabf59e..d51cc15d8b1c 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -55,7 +55,7 @@ void kvm__init_ram(struct kvm *kvm) madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size, MADV_HUGEPAGE); - phys_start = ARM_MEMORY_AREA; + phys_start = kvm->cfg.ram_addr; phys_size = kvm->ram_size; host_mem = kvm->ram_start; @@ -65,6 +65,9 @@ void kvm__init_ram(struct kvm *kvm) "address 0x%llx [err %d]", phys_size, phys_start, err); kvm->arch.memory_guest_start = phys_start; + + pr_debug("RAM created at 0x%llx - 0x%llx", + phys_start, phys_start + phys_size - 1); } void kvm__arch_delete_ram(struct kvm *kvm) @@ -201,7 +204,7 @@ bool kvm__load_firmware(struct kvm *kvm, const char *firmware_filename) /* For default firmware address, lets load it at the begining of RAM */ if (fw_addr == 0) - fw_addr = ARM_MEMORY_AREA; + fw_addr = kvm->arch.memory_guest_start; if (!validate_fw_addr(kvm, fw_addr)) die("Bad firmware destination: 0x%016llx", fw_addr); diff --git a/builtin-run.c b/builtin-run.c index 8b4e865f0a0e..87023e390e73 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -125,12 +125,21 @@ static u64 parse_mem_option(const char *nptr, char **next) static int mem_parser(const struct option *opt, const char *arg, int unset) { struct kvm *kvm = opt->ptr; - char *next; + char *next, *nptr; kvm->cfg.ram_size = parse_mem_option(arg, &next); if (kvm->cfg.ram_size == 0) die("Invalid RAM size: %s", arg); + if (kvm__arch_has_cfg_ram_address() && *next == '@') { + next++; + if (*next == '\0') + die("Missing memory address: %s", arg); + + nptr = next; + kvm->cfg.ram_addr = parse_mem_option(nptr, &next); + } + if (*next != '\0') die("Invalid memory specifier: %s", arg); @@ -141,15 +150,26 @@ static int mem_parser(const struct option *opt, const char *arg, int unset) #define OPT_ARCH_RUN(...) #endif +#ifdef ARCH_HAS_CFG_RAM_ADDRESS +#define MEM_OPT_HELP_SHORT "size[BKMGTP][@addr[BKMGTP]]" +#define MEM_OPT_HELP_LONG \ + "Virtual machine memory size and optional base address, both" \ + " measured by default in megabytes (M)" +#else +#define MEM_OPT_HELP_SHORT "size[BKMGTP]" +#define MEM_OPT_HELP_LONG \ + "Virtual machine memory size, by default measured in" \ + " in megabytes (M)" +#endif + #define BUILD_OPTIONS(name, cfg, kvm) \ struct option name[] = { \ OPT_GROUP("Basic options:"), \ OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name", \ "A name for the guest"), \ OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"), \ - OPT_CALLBACK('m', "mem", NULL, "size[BKMGTP]", \ - "Virtual machine memory size, by default measured" \ - " in megabytes (M)", mem_parser, kvm), \ + OPT_CALLBACK('m', "mem", NULL, MEM_OPT_HELP_SHORT, \ + MEM_OPT_HELP_LONG, mem_parser, kvm), \ OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk " \ " image or rootfs directory", img_name_parser, \ kvm), \ @@ -595,6 +615,14 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); kvm->cfg.custom_rootfs_name = "default"; + /* + * An architecture can allow the user to set the RAM base address to + * zero. Initialize the address before parsing the command line + * arguments, otherwise it will be impossible to distinguish between the + * user setting the base address to zero or letting it unset and using + * the default value. + */ + kvm->cfg.ram_addr = kvm__arch_default_ram_address(); while (argc != 0) { BUILD_OPTIONS(options, &kvm->cfg, kvm); diff --git a/include/kvm/kvm-config.h b/include/kvm/kvm-config.h index 31bc89520d52..45fe1caaebce 100644 --- a/include/kvm/kvm-config.h +++ b/include/kvm/kvm-config.h @@ -23,6 +23,7 @@ struct kvm_config { struct kvm_config_arch arch; struct disk_image_params disk_image[MAX_DISK_IMAGES]; struct vfio_device_params *vfio_devices; + u64 ram_addr; /* Guest memory physical base address, in bytes */ u64 ram_size; /* Guest memory size, in bytes */ u8 num_net_devices; u8 num_vfio_devices; diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 360430b78b1e..eb23e2f77310 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -197,6 +197,18 @@ int kvm__arch_free_firmware(struct kvm *kvm); bool kvm__arch_cpu_supports_vm(void); void kvm__arch_read_term(struct kvm *kvm); +#ifdef ARCH_HAS_CFG_RAM_ADDRESS +static inline bool kvm__arch_has_cfg_ram_address(void) +{ + return true; +} +#else +static inline bool kvm__arch_has_cfg_ram_address(void) +{ + return false; +} +#endif + void *guest_flat_to_host(struct kvm *kvm, u64 offset); u64 host_to_guest_flat(struct kvm *kvm, void *ptr); diff --git a/include/linux/sizes.h b/include/linux/sizes.h index ce3e8150c174..bc621db852d9 100644 --- a/include/linux/sizes.h +++ b/include/linux/sizes.h @@ -44,4 +44,6 @@ #define SZ_1G 0x40000000 #define SZ_2G 0x80000000 +#define SZ_4G 0x100000000ULL + #endif /* __LINUX_SIZES_H__ */