summaryrefslogtreecommitdiff
path: root/usr.sbin/vmd
diff options
context:
space:
mode:
authorDave Voutila <dv@cvs.openbsd.org>2022-05-03 21:39:20 +0000
committerDave Voutila <dv@cvs.openbsd.org>2022-05-03 21:39:20 +0000
commitd479378255c624fc6c59decbdb6d7bb1e8116f90 (patch)
treecaf894147d0004510e88894ede3de641155504ff /usr.sbin/vmd
parent0f1c45970db92052b0289d483f6692318feda2f8 (diff)
vmm/vmd/vmctl: standardize memory units to bytes
At different points in the vm lifecycle vmm(4), vmctl(8), and vmd(8) refer to a vm's memory range sizes in either bytes or megabytes. This is needlessly complex. Switch to using bytes everywhere and adjust types and constants accordingly. While this makes it possible to specify vm's with memory in fractions of megabytes, the logic requiring whole megabyte values remains. Feedback from deraadt@, mlarkin@, and Matthew Martin. ok mlarkin@
Diffstat (limited to 'usr.sbin/vmd')
-rw-r--r--usr.sbin/vmd/parse.y30
-rw-r--r--usr.sbin/vmd/vm.c12
-rw-r--r--usr.sbin/vmd/vmd.h4
3 files changed, 30 insertions, 16 deletions
diff --git a/usr.sbin/vmd/parse.y b/usr.sbin/vmd/parse.y
index ebebbf24750..90d74e28401 100644
--- a/usr.sbin/vmd/parse.y
+++ b/usr.sbin/vmd/parse.y
@@ -1,4 +1,4 @@
-/* $OpenBSD: parse.y,v 1.59 2021/10/15 15:01:29 naddy Exp $ */
+/* $OpenBSD: parse.y,v 1.60 2022/05/03 21:39:18 dv Exp $ */
/*
* Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
@@ -1248,25 +1248,41 @@ symget(const char *nam)
ssize_t
parse_size(char *word, int64_t val)
{
+ char result[FMT_SCALED_STRSIZE];
ssize_t size;
long long res;
if (word != NULL) {
if (scan_scaled(word, &res) != 0) {
- log_warn("invalid size: %s", word);
+ log_warn("invalid memory size: %s", word);
return (-1);
}
val = (int64_t)res;
}
if (val < (1024 * 1024)) {
- log_warnx("size must be at least one megabyte");
+ log_warnx("memory size must be at least 1MB");
return (-1);
- } else
- size = val / 1024 / 1024;
+ }
- if ((size * 1024 * 1024) != val)
- log_warnx("size rounded to %zd megabytes", size);
+ if (val > VMM_MAX_VM_MEM_SIZE) {
+ if (fmt_scaled(VMM_MAX_VM_MEM_SIZE, result) == 0)
+ log_warnx("memory size too large (limit is %s)",
+ result);
+ else
+ log_warnx("memory size too large");
+ return (-1);
+ }
+
+ /* Round down to the megabyte. */
+ size = (val / (1024 * 1024)) * (1024 * 1024);
+
+ if (size != val) {
+ if (fmt_scaled(size, result) == 0)
+ log_warnx("memory size rounded to %s", result);
+ else
+ log_warnx("memory size rounded to %zd bytes", size);
+ }
return ((ssize_t)size);
}
diff --git a/usr.sbin/vmd/vm.c b/usr.sbin/vmd/vm.c
index 55d938ed1d1..d952ba4d8d0 100644
--- a/usr.sbin/vmd/vm.c
+++ b/usr.sbin/vmd/vm.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vm.c,v 1.68 2022/03/01 21:46:19 dv Exp $ */
+/* $OpenBSD: vm.c,v 1.69 2022/05/03 21:39:18 dv Exp $ */
/*
* Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -233,7 +233,7 @@ loadfile_bios(gzFile fp, off_t size, struct vcpu_reg_state *vrs)
if (gzseek(fp, 0, SEEK_SET) == -1)
return (-1);
- /* The BIOS image must end at 1M */
+ /* The BIOS image must end at 1MB */
if ((off = 1048576 - size) < 0)
return (-1);
@@ -871,15 +871,13 @@ vcpu_reset(uint32_t vmid, uint32_t vcpu_id, struct vcpu_reg_state *vrs)
void
create_memory_map(struct vm_create_params *vcp)
{
- size_t len, mem_bytes, mem_mb;
+ size_t len, mem_bytes;
- mem_mb = vcp->vcp_memranges[0].vmr_size;
+ mem_bytes = vcp->vcp_memranges[0].vmr_size;
vcp->vcp_nmemranges = 0;
- if (mem_mb < 1 || mem_mb > VMM_MAX_VM_MEM_SIZE)
+ if (mem_bytes == 0 || mem_bytes > VMM_MAX_VM_MEM_SIZE)
return;
- mem_bytes = mem_mb * 1024 * 1024;
-
/* First memory region: 0 - LOWMEM_KB (DOS low mem) */
len = LOWMEM_KB * 1024;
vcp->vcp_memranges[0].vmr_gpa = 0x0;
diff --git a/usr.sbin/vmd/vmd.h b/usr.sbin/vmd/vmd.h
index 5f33b648317..31bc642afdb 100644
--- a/usr.sbin/vmd/vmd.h
+++ b/usr.sbin/vmd/vmd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: vmd.h,v 1.108 2022/01/04 15:22:53 claudio Exp $ */
+/* $OpenBSD: vmd.h,v 1.109 2022/05/03 21:39:18 dv Exp $ */
/*
* Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -56,7 +56,7 @@
#define MAX_TAP 256
#define NR_BACKLOG 5
#define VMD_SWITCH_TYPE "bridge"
-#define VM_DEFAULT_MEMORY 512
+#define VM_DEFAULT_MEMORY 512 * 1024 * 1024 /* 512 MiB */
#define VMD_DEFAULT_STAGGERED_START_DELAY 30