aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSuzuki K Poulose <suzuki.poulose@arm.com>2022-06-16 14:48:24 +0100
committerWill Deacon <will@kernel.org>2022-07-01 16:08:06 +0100
commita9c24ec78b4fb23596fa4cb0894e2c58e9345f8b (patch)
tree4d65c0596c021ad015309da1eaabe9a9791eb2e8
parentd6d4220133b7624e372daa45f9761d8f15050a80 (diff)
downloadkvmtool-a9c24ec78b4fb23596fa4cb0894e2c58e9345f8b.tar.gz
builtin_run: Allow standard size specifiers for memory
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 <suzuki.poulose@arm.com> Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com> Reviewed-by: Andre Przywara <andre.przywara@arm.com> Reviewed-and-Tested-by: Suzuki K Poulose <suzuki.poulose@arm.com> Link: https://lore.kernel.org/r/20220616134828.129006-9-alexandru.elisei@arm.com Signed-off-by: Will Deacon <will@kernel.org>
-rw-r--r--builtin-run.c59
1 files changed, 54 insertions, 5 deletions
diff --git a/builtin-run.c b/builtin-run.c
index dcd08f73..8b4e865f 100644
--- a/builtin-run.c
+++ b/builtin-run.c
@@ -49,9 +49,11 @@
#include <ctype.h>
#include <stdio.h>
-#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",