From patchwork Thu Apr 28 15:55:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830971 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 0167EC433EF for ; Thu, 28 Apr 2022 15:57:27 +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=5i9lMi0HZDdGf+QUWrzN5KChssip3+arEHit1vhkvSM=; b=Lx0Uwm8y94gi6B DbKBDEroQ0+P37Wq7vxt/i7JstctZNCflz3SqtCZJbVucVEU41MwFziTratypFA6BdDrCZUDgVL07 x/FYS2305lGcf1ix/XlF7NnJOiYToBpHXOYcFYztTNgrm/JSo3866CyQDVA6KFn5NumY36JTjqSO2 1syTkA+YIG8NYI5rhdO0JP+OtLL/aLVvjaa7IrTT/Y/YImPjs+rAZsaBTLRz54Oy0DZPCTUHFhRir wkRuTZefu8YkxsjGNDckJAv6o+nD5foOaYy2KlJw0zbbLiz0tLj+tb9bX8RCA7EdjK/IFU9h8p06V ypsH5DuFmlWh4/qwMKdQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6VE-007kOJ-8n; Thu, 28 Apr 2022 15:56:24 +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 1nk6Uz-007kJH-Om for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:11 +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 1D2491477; Thu, 28 Apr 2022 08:56:08 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AEDA63F774; Thu, 28 Apr 2022 08:56:06 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 01/15] Use MB for megabytes consistently Date: Thu, 28 Apr 2022 16:55:48 +0100 Message-Id: <20220428155602.29445-2-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085609_887167_D2BC15A5 X-CRM114-Status: GOOD ( 11.61 ) 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 Apr 28 15:55:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830973 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 12096C433EF for ; Thu, 28 Apr 2022 15:57:41 +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=HtMklQme+A7qsPzH5GrFLvuk8DXPVz11U8jmndaSEFs=; b=tCcr3M+mkLpEYy 7FpPsBl8vem8yJB47m9w6TtrE/OCorEgkP9DVmlK2DnXTCRyqpo/z8E4mkcYgWyHBJpFnxE4ErQw+ pZujh1Rdfk8ZZCokcajShaRHX2jswmJa/OluZ3dN3204W9PlH7NGAgflslXjXXg9uAWjNkpU40hHx RMfmbqANi/To2OMtmTR1/W6nFkBsZIocvGuiO6WKgHVGPWh0uOGsQofg4bfTKAyUxv7jXCwA3qy0w 0S+Q3i+ayqRh/pzO5cRZtvwS4T0CfC3v3HhgEakcYFGeUYfgk2Zq9q/VrsAsz/Z/bKE3CZA6NSEmC ClC4cc+kB8p9D3KcdpBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6VO-007kSR-LO; Thu, 28 Apr 2022 15:56: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 1nk6V0-007kJb-Ty for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:12 +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 1BB6014BF; Thu, 28 Apr 2022 08:56:10 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id ACDA83F774; Thu, 28 Apr 2022 08:56:08 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 02/15] builtin-run: Always use RAM size in bytes Date: Thu, 28 Apr 2022 16:55:49 +0100 Message-Id: <20220428155602.29445-3-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085611_117133_29F87FA0 X-CRM114-Status: GOOD ( 14.79 ) 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 --- 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..236c59d2f86f 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 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 = 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 Apr 28 15:55:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830974 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 9644EC433F5 for ; Thu, 28 Apr 2022 15:57:43 +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=+4S0+ClVk0fb0t8ep7bFZYsXDvvAAs5geNCucjKsq44=; b=yThESTURsR/zTv l2wxAxhlfmG+PdMlXP4HDSK+UD4e1npzsy6SVjjL77m5UZBoCT+xW/kbkewEBL+vq0iTyAARH/Z+c nceJFhLWEnFGktJ+oOONdA50zBgTEZRBS1XdU7dDz7xvIdQk5cGhjKBpvE6iM91MQXpy9GHccuAYj wU/3BK19RFMrJw+B6T1HYF9k5Chwo/uRgW+i1XZdb4GuzFcVJfTbRWAwFuPzD8F/iDNRGt/in9eBR B6pct3CXHWJVug6T2fGebRH7DD003NzoABNge2Bu5xy+dyGaDH2OlmmeIh1HO/7oTtTCMDRHxkajY bynQCHS8hbb1HVZ+YJrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6VY-007kUA-D2; Thu, 28 Apr 2022 15:56:44 +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 1nk6V3-007kK7-Ez for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:15 +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 BF2861474; Thu, 28 Apr 2022 08:56:11 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5A1763F774; Thu, 28 Apr 2022 08:56:10 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 03/15] builtin-run: Rework RAM size validation Date: Thu, 28 Apr 2022 16:55:50 +0100 Message-Id: <20220428155602.29445-4-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085613_585652_D2464863 X-CRM114-Status: GOOD ( 12.47 ) 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. 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 236c59d2f86f..4491fd244f8c 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 Apr 28 15:55:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830975 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 9BFD1C433F5 for ; Thu, 28 Apr 2022 15:57:58 +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=WbUMk2HN/NW+bPfMhgpE+JgJMEDBoFCssMEej32WOOc=; b=n2kRZceSvkWiyT 8KFd61tixhZ4MS5Ghs8+idyq54BaZAV5ohq317mgQW/cR81QuFBanSRRId4R9zTgegUe8eYXh8x2z gXqnvJGGkhlBgpgKBDZImrF+cRUuWNOjF2ISgaYy7PRupfOWIXM5xJCHXwlAWwUFLWc20uLiLGfIh GPwiyjyNXdYfmu8SthX5+EZePOM6nswH49V03+LHU+iL22KTwIKrLfZ+9n52Zd/rUPlZ69l18JOmr 2ZgbpPc0lCjIcD7jSyVrnAa61girkTwZgWOrurCifQlzbh9CwqaRMlf79F3TE6DzlucbcxwdBF/88 x0/Y/h9+oExxavyTk/PA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6Vi-007kWu-Mg; Thu, 28 Apr 2022 15:56:54 +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 1nk6V4-007kKT-2X for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:15 +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 6C1111477; Thu, 28 Apr 2022 08:56:13 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0835C3F774; Thu, 28 Apr 2022 08:56:11 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 04/15] builtin-run: Add arch hook to validate VM configuration Date: Thu, 28 Apr 2022 16:55:51 +0100 Message-Id: <20220428155602.29445-5-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085614_252464_509172F3 X-CRM114-Status: GOOD ( 16.01 ) 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 --- Makefile | 3 ++- 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, 30 insertions(+), 1 deletion(-) create mode 100644 arm/aarch32/kvm.c diff --git a/Makefile b/Makefile index e9121dc39d02..7149c22d75ac 100644 --- a/Makefile +++ b/Makefile @@ -167,6 +167,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 @@ -583,4 +584,4 @@ ifneq ($(MAKECMDGOALS),clean) KVMTOOLS-VERSION-FILE: @$(SHELL_PATH) util/KVMTOOLS-VERSION-GEN $(OUTPUT) -endif \ No newline at end of file +endif 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 28d608d98831..b66a285b6359 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -5,6 +5,10 @@ #include #include +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 4491fd244f8c..2c37b91b6574 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 3470dbb2e433..f5b137f46dff 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -12,6 +12,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 Apr 28 15:55:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830976 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 422CAC433F5 for ; Thu, 28 Apr 2022 15:58:24 +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=YlIBprs9l8ZY8Nc4jFlL6+kEQujnyF53Ysq8FRPnJ3w=; b=zdwqIVI+aBYpaR zWTo4pvE/ye/7PpQwKCZ1G9MXgQm0kfa/atJCzgXovXORxNNjiRy3qB04/O+9nR1CmJmBKJFkaU99 VJTwNjq9PGuPyYj2YWPOtMb3KPYL95/bjRdvCBZnyt6/rfuNikPxFnx61itSD1e1+KlAZxLJX79iw IapPEBOku0a4Vp+loihSTF4vFIpFWGUmW5Fo4RbNQhrZwj6oOZwxHm3/lNM59im4/kVCMEkPT3MLk 97hfoCW3NCCFLIbLv8DYa756Ojxr/jIcfofJT8LkON8jhzD1xf4dqcT6waurXCuCsPsg7OPR7ntxO HNqffvFa3aAFFCgQLhWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6W2-007kdl-PC; Thu, 28 Apr 2022 15:57:14 +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 1nk6V5-007kK7-Gl for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:17 +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 1AAA314BF; Thu, 28 Apr 2022 08:56:15 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A98A23F774; Thu, 28 Apr 2022 08:56:13 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 05/15] arm/arm64: Fail if RAM size is too large for 32-bit guests Date: Thu, 28 Apr 2022 16:55:52 +0100 Message-Id: <20220428155602.29445-6-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085615_648892_F59D78BF X-CRM114-Status: GOOD ( 10.84 ) 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. 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 b66a285b6359..73b2c5970f28 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -7,6 +7,11 @@ 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 Apr 28 15:55:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830977 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 39122C433EF for ; Thu, 28 Apr 2022 15:58:39 +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=zZp+FUABxKDE6qfEpuWD+8N27eHFd3YVQn/B6K1RD+Q=; b=vxtlaqSoh1Nz9K G/0g2ULgcGmYKVkOze3rVHqjSSsCF20zM3PY+BfHVFQ66mfXF+QiZDijKzrAkAJxMwP5BJg48zx4w jLtY4D2JzpBmj2ITXof6MLcxALejodi+rudjyQceDG6dVDR41aD/D2UxmhUdAyrRwSQalwv07/yNN UVSkjSlwJoYzE9E4ooZsfdet980+423KebxgKKGU+B2CX7CDHW0zkZs5XtF0+xvYNLSGVctjT7VfB KFJ4NKPSavooFGl8Tg2opL7EdvFRs7DH411/Je1gUmY194UcBjvDDy0iVEqOo18ObE1gzzuaue0cE 6df328NhtS7YOMcWipPQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6WL-007knt-Vx; Thu, 28 Apr 2022 15:57: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 1nk6V7-007kKT-7E for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:19 +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 BABD91474; Thu, 28 Apr 2022 08:56:16 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 583EC3F774; Thu, 28 Apr 2022 08:56:15 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 06/15] builtin_run: Allow standard size specifiers for memory Date: Thu, 28 Apr 2022 16:55:53 +0100 Message-Id: <20220428155602.29445-7-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085617_400701_BC513FA8 X-CRM114-Status: GOOD ( 14.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 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 --- builtin-run.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 2c37b91b6574..0632a0c393a0 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,60 @@ void kvm_run_set_wrapper_sandbox(void) kvm_run_wrapper = KVM_RUN_SANDBOX; } +static int parse_mem_unit(char **next) +{ + int shift = -1; + + switch (**next) { + case 'B': case 'b': shift = 0; break; + case 'K': case 'k': shift = KB_SHIFT; break; + case 'M': case 'm': shift = MB_SHIFT; break; + case 'G': case 'g': shift = GB_SHIFT; break; + case 'T': case 't': shift = TB_SHIFT; break; + case 'P': case 'p': shift = PB_SHIFT; break; + } + + if (shift == -1) { + /* The default is megabytes. */ + shift = MB_SHIFT; + } else { + (*next)++; + } + + return shift; +} + +static u64 parse_mem_option(const char *nptr, char **next) +{ + u64 shift; + u64 val; + + val = strtoull(nptr, next, 10); + if (errno == ERANGE) + die("Memory too large: %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 +153,10 @@ 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. The default unit" \ + " of measurement is 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 +580,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 Apr 28 15:55:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830982 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 DC154C433F5 for ; Thu, 28 Apr 2022 15:59:10 +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=MlYxoFPu+eqrIz6BnEXMkMfxpFpGdt6DlzJZ57azXAw=; b=SJ5+CKPSiu0a8A /SkKxRUuA3cK5VFQZYSlyMoTfVmIxlIty5MzmTPa7nNkzzddibN6/ebGFvqCi8ynDNQ775DVncARx /EtHGN/Oks2PmNdTYe2t+S3c4oGCYbGZqGtlslOECykmOUOGLAt4f3q8Atd05fW76GOKdTiTPSOf+ NLFHNwBhuMRDfKXRfHC5TNmcBXssr6BV+qOEaeNOZiN4RZa0Y0pJtXyUJdimf6v9SMXi1VVVfVipS oTLCfSRrje30sJyM8PaKu6pyg+k04xDAzhsDbdu1rh1076YsDc5K7AuaPVNaVqUm0cePfyymp0O9w dSDBG+nTsbUTuKACZWFg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6Wo-007l1E-Hq; Thu, 28 Apr 2022 15:58:02 +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 1nk6V8-007kK7-Op for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:21 +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 657781477; Thu, 28 Apr 2022 08:56:18 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0438D3F774; Thu, 28 Apr 2022 08:56:16 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 07/15] arm/arm64: Kill the ARM_MAX_MEMORY() macro Date: Thu, 28 Apr 2022 16:55:54 +0100 Message-Id: <20220428155602.29445-8-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085618_886598_514C763F X-CRM114-Status: UNSURE ( 8.13 ) 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. 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 Apr 28 15:55:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830983 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 76170C43219 for ; Thu, 28 Apr 2022 15:59:32 +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=UlvVVP1X/KLLhBj4ItAZ2aykVobUxRIcnd0is6ErcEE=; b=DKGP0PDI3RuVNR OOr9tAx3Ix8ZFDo9pIZ/skYf7fDaAh6JGw7VD8v8Gq2GYEgqK7D6kFqUr028vRw5aFslcYlxfjpoX rUGaPqqxWLWBLZR8u7o1ZRpoY4I7UFYXQwHQaBrdnyxR46xK2id9Cz6+YArtBTVJgEvt5c+NPX7Tz ep5vdAdKcydn9rGo6qhDHEG8oUJD0oM2YPYoubMGnWzGicsM9fhNlpVXNgC+HrFGOyodBxR+/x4SO aS9jMKnMb8nl6go7C0Gxew6u7MSBSpTJGKG3Euo8DrzXRryk/lCjFJb5FxtTnNgDSZJ3/ymY+Dt6j WUtTMjcRsYTWNrJLEvGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6XA-007lCA-Im; Thu, 28 Apr 2022 15:58: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 1nk6VA-007kKT-Bm for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:21 +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 1302B1474; Thu, 28 Apr 2022 08:56:20 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A290A3F774; Thu, 28 Apr 2022 08:56:18 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 08/15] arm/arm64: Kill the ARM_HIMAP_MAX_MEMORY() macro Date: Thu, 28 Apr 2022 16:55:55 +0100 Message-Id: <20220428155602.29445-9-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085620_486436_5D814C92 X-CRM114-Status: UNSURE ( 7.55 ) 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. 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 43b1f77df394..cbe2bb2381ef 100644 --- a/arm/include/arm-common/kvm-arch.h +++ b/arm/include/arm-common/kvm-arch.h @@ -60,7 +60,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 Apr 28 15:55:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830984 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 DE204C433EF for ; Thu, 28 Apr 2022 16:00:23 +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=WcvZokInUqadsQAHX2S37G6Z8BhY0WYmqO7sdAY7nDE=; b=D2ZY7UG3CcyWHv 8ecyQT0yqelz/WypaW9xP7liWtab1ynzyoOk+V8PuEBoEDHSeQ9l1RStB0A2e98BNf76KxdgG0Ws8 RXa7PX9a9Hp+9CVVJzKmHSGjSnabZxnNR/O0lB/qSubdrlnIrXd3ieEE5O31EBWxCDmlsckBY2aBY 4XH5VJc/lEaVjaVDVV9tBb9B8/+gFUYjNJBambhqjFvq3a2B9OPYG7CSTwJ2iWYJj+cmTxGVFsHFq YZJFL17pEV/ExXEq+ssHSbOreMXTdtkVbYarSYKMf+NSoYeBu9FqZ58KICd34cdMGBQRLaQ9fJ1Ag WKBk7JGp52pc4Vg/M1zA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6Xy-007ldH-G8; Thu, 28 Apr 2022 15:59:14 +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 1nk6VC-007kK7-0e for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:24 +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 D5E0314BF; Thu, 28 Apr 2022 08:56:21 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 502CB3F774; Thu, 28 Apr 2022 08:56:20 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Cc: Julien Grall Subject: [PATCH kvmtool 09/15] kvm__arch_init: Remove hugetlbfs_path and ram_size as parameters Date: Thu, 28 Apr 2022 16:55:56 +0100 Message-Id: <20220428155602.29445-10-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085622_184215_31FDF4AE X-CRM114-Status: GOOD ( 12.29 ) 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(). 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 f5b137f46dff..d8610cf81b94 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -61,12 +61,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 Apr 28 15:55:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830985 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 50868C433F5 for ; Thu, 28 Apr 2022 16:00:38 +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=knXjBNydW6lfCSHsxxKLL4xWdXsgn4aqPhlJ6cnUnZA=; b=fOmXj6y+1+t+/Z feXsNcq0kz8GXiR9auoUpBA313a97N1rsViivf/dz9EO2U8vDy2J+ttlwRpBjdf85ZJcGhK5S5pw9 TMlZOKGO8rNfNZmgYjZ+CC8hEOPWSy55nl0mLM22p1JfeY3/Hs+5ue3oGVFETwnLrPvLkQaetaHIm Yomj43rzXzKHK8zDS/dSZsVcOAIVCNM6GMsVLsL4oUbKYVs72i11eMnkNxYSdqA6rshkBpv8Hto2C LevSF6agau+dth+uxPpFbUVn6u0iJjhXpcoKGcbkxvtUP+aZLmMv9hBBuvy5MefIsB48y3IfAQ3pS UzfqP2Xk3/Upnoe58RHw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6YJ-007loi-SC; Thu, 28 Apr 2022 15:59:36 +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 1nk6VE-007kOb-CF for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:26 +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 A55921474; Thu, 28 Apr 2022 08:56:23 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1EF623F774; Thu, 28 Apr 2022 08:56:21 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Cc: Julien Grall Subject: [PATCH kvmtool 10/15] arm/arm64: Consolidate RAM initialization in kvm__init_ram() Date: Thu, 28 Apr 2022 16:55:57 +0100 Message-Id: <20220428155602.29445-11-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085624_578247_9C5B8A8A X-CRM114-Status: GOOD ( 12.93 ) 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. 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 Apr 28 15:55:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830986 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 CCD3EC433EF for ; Thu, 28 Apr 2022 16:01:23 +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=4K7+Ga55hVEfpibFKJlOf7E006c9Jm0mP3FLZ59EG68=; b=V1/YWTtao3exln 2n9NW5L3JTykfgiTLVhnVmoHSgQu+luM/JDhpfI0Y6bAFICDzd/8kTIR1Jnlqv65g4k0wnIkqcNP1 YnvT/TxEJ6T+R2wyiiGUiEaPfFwonw2V3HlxgG2+ZPJx3U62vcN1DtMPr7c0RKi94HduitXgHv57b MvB0cGxRmNLETEPn/qAEMAI0OZZPMRfkI68yPY9MT5e5Q0McPSRii7Y7jYB/zdVela0o/9lRS8dPw RftAg0z6qZTGLaDiQ+1X6HEfAwDDECPLRjkodGFonogFzzJjppyNT6jCdah4tUQ9Bj0jEFYeMo+Ee dt4YImfRF28xo6/DrRJQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6Yu-007mBT-VG; Thu, 28 Apr 2022 16:00:13 +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 1nk6VF-007kK7-LV for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:27 +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 515491477; Thu, 28 Apr 2022 08:56:25 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E27B63F774; Thu, 28 Apr 2022 08:56:23 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 11/15] Introduce kvm__arch_default_ram_address() Date: Thu, 28 Apr 2022 16:55:58 +0100 Message-Id: <20220428155602.29445-12-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085625_776575_6FA05B6B X-CRM114-Status: UNSURE ( 9.23 ) 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 Add a new function, kvm__arch_default_ram_address(), which returns the default address for guest RAM for each architecture. 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 73b2c5970f28..c52243b1f67f 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -5,6 +5,11 @@ #include #include +u64 kvm__arch_default_ram_address(void) +{ + return ARM_MEMORY_AREA; +} + void kvm__arch_validate_cfg(struct kvm *kvm) { if (kvm->cfg.arch.aarch32_guest && 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 d8610cf81b94..4cbc9907731b 100644 --- a/mips/kvm.c +++ b/mips/kvm.c @@ -12,6 +12,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 Apr 28 15:55:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830987 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 C924BC433F5 for ; Thu, 28 Apr 2022 16:01: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=zu6Y54wKlQ1dAGOmA8WyTEywoX5FFURd/RX9rD2pZDo=; b=CAMN/u+8mK9tnz eOHRxkAcMoq03h/wBs51lQWNAH9IMmHe4tdhQGSE7K/JleT1JKasDE2Y/XhmiH8B0sskEC0auG8re 3LtGTRftomeSUufxPb4iGeEaalwBV+3O/NXlKWQHzaDLQAHMdIuUtb5rsu6qyL+4WsXd0iTg0yUTz UXDt2PtdkKhdIM2WUS49/5MsoiTf8r0zhj6MLqjQKS49JIKz8Im+1Nb5Rc1l/ksv7aIwbR28pRUaZ ByG52KHIjZsaoENv8ThUIoMVi1limfd7u8qDWc9olitKTKMt0bl8Up9HQ2Mt3JUtxRPAH6yLvwTHf EomdFA0osnVboW9UnqZQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6ZK-007mNc-PW; Thu, 28 Apr 2022 16:00:38 +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 1nk6VH-007kOb-6l for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:29 +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 EFB721474; Thu, 28 Apr 2022 08:56:26 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8E8373F774; Thu, 28 Apr 2022 08:56:25 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 12/15] arm64: Allow the user to specify the RAM base address Date: Thu, 28 Apr 2022 16:55:59 +0100 Message-Id: <20220428155602.29445-13-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085627_407427_5141A164 X-CRM114-Status: GOOD ( 15.10 ) 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. Signed-off-by: Alexandru Elisei --- arm/aarch64/include/kvm/kvm-arch.h | 2 ++ arm/aarch64/kvm.c | 5 ++++- arm/kvm.c | 7 +++++-- builtin-run.c | 30 +++++++++++++++++++++++++----- include/kvm/kvm-config.h | 1 + include/kvm/kvm.h | 12 ++++++++++++ 6 files changed, 49 insertions(+), 8 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 c52243b1f67f..581c836eb6ae 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -17,6 +17,9 @@ void kvm__arch_validate_cfg(struct kvm *kvm) die("RAM size 0x%llx exceeds maximum allowed 0x%llx", kvm->cfg.ram_size, ARM_LOMAP_MAX_MEMORY); } + + if (kvm->cfg.ram_addr < ARM_MEMORY_AREA) + die("RAM address is below %luGB", ARM_MEMORY_AREA >> 30); } /* @@ -85,7 +88,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 0632a0c393a0..1eef1ffd0afc 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -131,12 +131,22 @@ 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); + kvm->cfg.ram_addr = kvm__arch_default_ram_address(); + 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); @@ -147,16 +157,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." \ + " The default unit of measurement is megabytes (M)" +#else +#define MEM_OPT_HELP_SHORT "size[BKMGTP]" +#define MEM_OPT_HELP_LONG \ + "Virtual machine memory size." \ + " The default unit of measurement is 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. The default unit" \ - " of measurement is 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), \ 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); From patchwork Thu Apr 28 15:56:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830988 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 CEEEFC433F5 for ; Thu, 28 Apr 2022 16:02:05 +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=Z/79S28Ayol5vCkmDuyvfHe4LTTD2/RfjAdnTrH4WY8=; b=tS51J8WPobOqb7 MbxAV5BaFUh/lDKt1v3BItRYBQCJ2cNagAWl/0EExgNaTnyBY714mlUPDR6VFOpM0pfFyYP+db1hm QQbQuNChjXBYF73lOYhewnB3NB4KLA6z6LZtaodDBR6+Jnk/WHD0F4q/wl1VqG85FMlTw0ficZE2P AtrMZHPSZ0jjATnKmshywJIZVDAdNn9IEfmsVBOHNgAEdilz4WShbqURQxyGl4Ir62MvKDBitIwuX wjjP76ElqP7bp3cfvmKrPPQgtQvdsZENhpw+sxt2qcrM7ouTACflijR0HETDp0hklX7Jihkqw1/bO uNHuy8iWC2Gzf8yOAptw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6Zd-007mXj-TD; Thu, 28 Apr 2022 16:00: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 1nk6VJ-007kK7-29 for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56:30 +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 9A27E1477; Thu, 28 Apr 2022 08:56:28 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3884A3F774; Thu, 28 Apr 2022 08:56:27 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 13/15] arm/arm64: Allow standard size specifiers for firmware address Date: Thu, 28 Apr 2022 16:56:00 +0100 Message-Id: <20220428155602.29445-14-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085629_223307_10A94F88 X-CRM114-Status: GOOD ( 13.12 ) 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 and arm64 permit the user to specify the load address, in RAM, for the firmware image. Make it possible to use the standard size specifiers (B/K/M/G/P/T) when doing so. Signed-off-by: Alexandru Elisei --- arm/include/arm-common/kvm-config-arch.h | 7 +++++-- arm/kvm.c | 13 +++++++++++++ builtin-run.c | 2 +- include/kvm/kvm.h | 1 + 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/arm/include/arm-common/kvm-config-arch.h b/arm/include/arm-common/kvm-config-arch.h index cdcbf235ac4e..9306608cbe4e 100644 --- a/arm/include/arm-common/kvm-config-arch.h +++ b/arm/include/arm-common/kvm-config-arch.h @@ -17,6 +17,7 @@ struct kvm_config_arch { }; int irqchip_parser(const struct option *opt, const char *arg, int unset); +int fw_addr_parser(const struct option *opt, const char *arg, int unset); #define OPT_ARCH_RUN(pfx, cfg) \ pfx, \ @@ -34,7 +35,9 @@ int irqchip_parser(const struct option *opt, const char *arg, int unset); "[gicv2|gicv2m|gicv3|gicv3-its]", \ "Type of interrupt controller to emulate in the guest", \ irqchip_parser, NULL), \ - OPT_U64('\0', "firmware-address", &(cfg)->fw_addr, \ - "Address where firmware should be loaded"), + OPT_CALLBACK('\0', "firmware-address", NULL, "addr[BKMGTP]", \ + "Address where firmware should be loaded. The default unit"\ + " of measurement is megabytes (M)", \ + fw_addr_parser, kvm), #endif /* ARM_COMMON__KVM_CONFIG_ARCH_H */ diff --git a/arm/kvm.c b/arm/kvm.c index d51cc15d8b1c..83df1c9a0cde 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -18,6 +18,19 @@ struct kvm_ext kvm_req_ext[] = { { 0, 0 }, }; +int fw_addr_parser(const struct option *opt, const char *arg, int unset) +{ + struct kvm *kvm = opt->ptr; + char *next; + + kvm->cfg.arch.fw_addr = parse_mem_option(arg, &next); + + if (*next != '\0') + die("Invalid firmware address: %s", arg); + + return 0; +} + bool kvm__arch_cpu_supports_vm(void) { /* The KVM capability check is enough. */ diff --git a/builtin-run.c b/builtin-run.c index 1eef1ffd0afc..710080f1831c 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -112,7 +112,7 @@ static int parse_mem_unit(char **next) return shift; } -static u64 parse_mem_option(const char *nptr, char **next) +u64 parse_mem_option(const char *nptr, char **next) { u64 shift; u64 val; diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index eb23e2f77310..af3441156725 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -211,6 +211,7 @@ static inline bool kvm__arch_has_cfg_ram_address(void) void *guest_flat_to_host(struct kvm *kvm, u64 offset); u64 host_to_guest_flat(struct kvm *kvm, void *ptr); +u64 parse_mem_option(const char *nptr, char **next); bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline); From patchwork Thu Apr 28 15:56:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830989 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 8D6D3C433F5 for ; Thu, 28 Apr 2022 16:02:20 +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=Tz3bpU9sxQ0HRlW0PMuhp1udCHjAiJfPvH9eFa9sTo8=; b=B/oFMT1Bf9qoMc shrdpg1gm9WqK058WBTjeIZnyC/mla0MueFPmJZgN3A13E0LwDtQh1a/4r3ZiscDED5rGjIV8G5Jz lbsqcnx6yUcKkkcu2X94zj4Ptb+TEXfzAzcuSCwat4qIi5oMu88yGRKWWfP495MVoesnE9mtJDvfn niJd4xar6uL8vLph9NhSCiO99GynDG983rmpmFoDFI/hvlRmCz7dBAGAmpazxa/3IM42yamIqWVJg 5YROe9ZNA3nRYqI/CAa+7J1gXz+q/K4DFTzBAErgjzZ34bh2APOdvjXJxpK26+BmTb8EcZkno3iZq mcP+rv3B5HaKem8tYa6Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6Zw-007mhd-5Z; Thu, 28 Apr 2022 16:01:16 +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 1nk6VK-007kOb-N9 for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56: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 473121474; Thu, 28 Apr 2022 08:56:30 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D750B3F774; Thu, 28 Apr 2022 08:56:28 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 14/15] arm/arm64: Treat --firmware-address when no --firmware as an error Date: Thu, 28 Apr 2022 16:56:01 +0100 Message-Id: <20220428155602.29445-15-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085630_845589_2A966FAC X-CRM114-Status: GOOD ( 13.09 ) 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 It makes little sense for the user to specify the firmware address when loading a kernel instead of a firmware image. Treat it as an error. Signed-off-by: Alexandru Elisei --- arm/aarch32/kvm.c | 2 +- arm/aarch64/kvm.c | 2 +- arm/include/arm-common/kvm-arch.h | 3 +++ arm/kvm.c | 8 ++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arm/aarch32/kvm.c b/arm/aarch32/kvm.c index 768a56bbb5b4..a7bb3b79db8c 100644 --- a/arm/aarch32/kvm.c +++ b/arm/aarch32/kvm.c @@ -1,6 +1,6 @@ #include "kvm/kvm.h" -void kvm__arch_validate_cfg(struct kvm *kvm) +void kvm__arm_validate_cfg(struct kvm *kvm) { if (kvm->cfg.ram_size > ARM_LOMAP_MAX_MEMORY) { die("RAM size 0x%llx exceeds maximum allowed 0x%llx", diff --git a/arm/aarch64/kvm.c b/arm/aarch64/kvm.c index 581c836eb6ae..8fb44a924aa4 100644 --- a/arm/aarch64/kvm.c +++ b/arm/aarch64/kvm.c @@ -10,7 +10,7 @@ u64 kvm__arch_default_ram_address(void) return ARM_MEMORY_AREA; } -void kvm__arch_validate_cfg(struct kvm *kvm) +void kvm__arm_validate_cfg(struct kvm *kvm) { if (kvm->cfg.arch.aarch32_guest && kvm->cfg.ram_size > ARM_LOMAP_MAX_MEMORY) { diff --git a/arm/include/arm-common/kvm-arch.h b/arm/include/arm-common/kvm-arch.h index cbe2bb2381ef..1d1ccc8bb172 100644 --- a/arm/include/arm-common/kvm-arch.h +++ b/arm/include/arm-common/kvm-arch.h @@ -88,6 +88,9 @@ static inline bool arm_addr_in_ioport_region(u64 phys_addr) return phys_addr >= KVM_IOPORT_AREA && phys_addr < limit; } +struct kvm; +void kvm__arm_validate_cfg(struct kvm *kvm); + struct kvm_arch { /* * We may have to align the guest memory for virtio, so keep the diff --git a/arm/kvm.c b/arm/kvm.c index 83df1c9a0cde..3edc6fdbcb5f 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -31,6 +31,14 @@ int fw_addr_parser(const struct option *opt, const char *arg, int unset) return 0; } +void kvm__arch_validate_cfg(struct kvm *kvm) +{ + if (kvm->cfg.arch.fw_addr && !kvm->cfg.firmware_filename) + die("--firmware-address is only valid when loading a firmware image"); + + kvm__arm_validate_cfg(kvm); +} + bool kvm__arch_cpu_supports_vm(void) { /* The KVM capability check is enough. */ From patchwork Thu Apr 28 15:56:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12830990 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 14419C433EF for ; Thu, 28 Apr 2022 16:02: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=8y5KzVDpX0aV6MNKSw4BrzGItcKGn9HPZkCEjCj5Uto=; b=TsXeblLH2IX1Ny VqSwsUyObq/ZEjMb1VL0HM12EtomQK74cOv60cNSmdxzCx6ZqafHms0e1MJZukcceEVY4JHl3pzQp QkOrc9mbleiLqROzizGBJjK6mJsS6AOGckYYlF6CI1dtTGZD+v3FqKwMidbYdPitbdf0OVcTLgWyA SsqeLUJdudpnYUGRS4/V93bKLjs0fm1hqYCEAbPjOMHgnB3FyBxqG5kPscgWmNWeg9PiVzeIPZQAX qqL6FusjfEJ0639uY3bM12ROocKPGWqFOIUBLgBczTVKlWFlRq+TFqnq6Vh6AQy4jjyltAKkSQT7Y 4JY2zkQ0fm3zhR9QdePg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nk6aJ-007mua-Il; Thu, 28 Apr 2022 16:01:39 +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 1nk6VM-007kS0-Nl for linux-arm-kernel@lists.infradead.org; Thu, 28 Apr 2022 15:56: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 E88891477; Thu, 28 Apr 2022 08:56:31 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 84AB93F774; Thu, 28 Apr 2022 08:56:30 -0700 (PDT) From: Alexandru Elisei To: julien.thierry.kdev@gmail.com, will@kernel.org, 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 Subject: [PATCH kvmtool 15/15] arm/arm64: Validate firmware address in kvm__arch_validate_cfg() Date: Thu, 28 Apr 2022 16:56:02 +0100 Message-Id: <20220428155602.29445-16-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220428155602.29445-1-alexandru.elisei@arm.com> References: <20220428155602.29445-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-20220428_085632_883062_31C22974 X-CRM114-Status: UNSURE ( 9.90 ) 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 We know at user configuration time if the firmware address is outside RAM, validate the address in kvm__arch_validate_cfg() before creating the VM. Signed-off-by: Alexandru Elisei --- arm/kvm.c | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/arm/kvm.c b/arm/kvm.c index 3edc6fdbcb5f..79d085ab9965 100644 --- a/arm/kvm.c +++ b/arm/kvm.c @@ -33,8 +33,20 @@ int fw_addr_parser(const struct option *opt, const char *arg, int unset) void kvm__arch_validate_cfg(struct kvm *kvm) { - if (kvm->cfg.arch.fw_addr && !kvm->cfg.firmware_filename) - die("--firmware-address is only valid when loading a firmware image"); + u64 fw_addr = kvm->cfg.arch.fw_addr; + u64 ram_addr = kvm->cfg.ram_addr; + u64 ram_size = kvm->cfg.ram_size; + + if (fw_addr) { + if (!kvm->cfg.firmware_filename) + die("--firmware-address is only valid when loading a firmware image"); + + if (fw_addr < ram_addr || fw_addr >= ram_addr + ram_size) { + die("Firmware address 0x%016llx outside guest memory: " + "0x%016llx - 0x%016llx", + fw_addr, ram_addr, ram_addr + ram_size); + } + } kvm__arm_validate_cfg(kvm); } @@ -196,23 +208,6 @@ bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd, return true; } -static bool validate_fw_addr(struct kvm *kvm, u64 fw_addr) -{ - u64 ram_phys; - - ram_phys = host_to_guest_flat(kvm, kvm->ram_start); - - if (fw_addr < ram_phys || fw_addr >= ram_phys + kvm->ram_size) { - pr_err("Provide --firmware-address an address in RAM: " - "0x%016llx - 0x%016llx", - ram_phys, ram_phys + kvm->ram_size); - - return false; - } - - return true; -} - bool kvm__load_firmware(struct kvm *kvm, const char *firmware_filename) { u64 fw_addr = kvm->cfg.arch.fw_addr; @@ -227,9 +222,6 @@ bool kvm__load_firmware(struct kvm *kvm, const char *firmware_filename) if (fw_addr == 0) fw_addr = kvm->arch.memory_guest_start; - if (!validate_fw_addr(kvm, fw_addr)) - die("Bad firmware destination: 0x%016llx", fw_addr); - fd = open(firmware_filename, O_RDONLY); if (fd < 0) return false;