[PATCH V2 3/4] qemu: Add support for max physical address size

Jim Fehlig jfehlig at suse.com
Fri Jul 29 19:34:35 UTC 2022


From: Dario Faggioli <dfaggioli at suse.com>

This patch maps /domain/cpu/maxphysaddr into -cpu parameters:

  - <maxphysaddr mode='passthrough'/> becomes host-phys-bits=on
  - <maxphysaddr mode='emualte' bits='42'/> becomes phys-bits=42

Passthrough mode can only be used if the chosen CPU model is
'host-passthrough'. Also validate that an explicitly specified
bits value does not exceed the physical address bits on the host.

The feature is available since QEMU 2.7.0.

Signed-off-by: Dario Faggioli <dfaggioli at suse.com>
Signed-off-by: Jim Fehlig <jfehlig at suse.com>
---
 src/qemu/qemu_command.c                       | 21 +++++++++
 src/qemu/qemu_domain.c                        | 46 +++++++++++++++++++
 src/qemu/qemu_validate.c                      | 12 +++++
 .../cpu-phys-bits-emulate.args                | 32 +++++++++++++
 .../cpu-phys-bits-emulate.xml                 | 20 ++++++++
 .../cpu-phys-bits-emulate2.args               | 32 +++++++++++++
 .../cpu-phys-bits-emulate2.xml                | 20 ++++++++
 .../cpu-phys-bits-emulate3.err                |  1 +
 .../cpu-phys-bits-emulate3.xml                | 20 ++++++++
 .../cpu-phys-bits-passthrough.args            | 32 +++++++++++++
 .../cpu-phys-bits-passthrough.xml             | 20 ++++++++
 .../cpu-phys-bits-passthrough2.err            |  1 +
 .../cpu-phys-bits-passthrough2.xml            | 20 ++++++++
 .../cpu-phys-bits-passthrough3.err            |  1 +
 .../cpu-phys-bits-passthrough3.xml            | 20 ++++++++
 tests/qemuxml2argvtest.c                      |  7 +++
 16 files changed, 305 insertions(+)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 30c9bbbf2e..acf214be00 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6847,6 +6847,27 @@ qemuBuildCpuCommandLine(virCommand *cmd,
             virBufferAddLit(&buf, ",l3-cache=off");
     }
 
+    if (def->cpu && def->cpu->addr) {
+        virCPUMaxPhysAddrDef *addr = def->cpu->addr;
+
+        switch (addr->mode) {
+        case VIR_CPU_MAX_PHYS_ADDR_MODE_PASSTHROUGH:
+            virBufferAddLit(&buf, ",host-phys-bits=on");
+            break;
+
+        case VIR_CPU_MAX_PHYS_ADDR_MODE_EMULATE:
+            if (addr->bits != -1)
+                virBufferAsprintf(&buf, ",phys-bits=%d", addr->bits);
+            else
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("Physical address bits unspecified"));
+            break;
+
+        case VIR_CPU_MAX_PHYS_ADDR_MODE_LAST:
+            break;
+        }
+    }
+
     cpu = virBufferContentAndReset(&cpu_buf);
     cpu_flags = virBufferContentAndReset(&buf);
 
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index b02ffc9a2e..0c97744b72 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -4429,6 +4429,52 @@ qemuDomainDefCPUPostParse(virDomainDef *def,
         }
     }
 
+    if (def->cpu->addr) {
+        virCPUMaxPhysAddrDef *addr = def->cpu->addr;
+
+        if (!ARCH_IS_X86(def->os.arch)) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("CPU maximum physical address bits specification "
+                             "is not supported for '%s' architecture"),
+                           virArchToString(def->os.arch));
+            return -1;
+        }
+
+        switch (addr->mode) {
+        case VIR_CPU_MAX_PHYS_ADDR_MODE_PASSTHROUGH:
+            if (def->cpu->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                               _("CPU maximum physical address bits mode '%s' "
+                                 "can only be used with '%s' CPUs"),
+                               virCPUMaxPhysAddrModeTypeToString(addr->mode),
+                               virCPUModeTypeToString(VIR_CPU_MODE_HOST_PASSTHROUGH));
+                return -1;
+            }
+            if (addr->bits != -1) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                               _("CPU maximum physical address bits number "
+                                 "specification cannot be used with "
+                                 "mode='%s'"),
+                               virCPUMaxPhysAddrModeTypeToString(VIR_CPU_MAX_PHYS_ADDR_MODE_PASSTHROUGH));
+                return -1;
+            }
+            break;
+
+        case VIR_CPU_MAX_PHYS_ADDR_MODE_EMULATE:
+            if (addr->bits == -1) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                               _("if using CPU maximum physical address "
+                                 "mode='%s', bits= must be specified too"),
+                               virCPUMaxPhysAddrModeTypeToString(VIR_CPU_MAX_PHYS_ADDR_MODE_EMULATE));
+                return -1;
+            }
+            break;
+
+        case VIR_CPU_MAX_PHYS_ADDR_MODE_LAST:
+            break;
+        }
+    }
+
     for (i = 0; i < def->cpu->nfeatures; i++) {
         virCPUFeatureDef *feature = &def->cpu->features[i];
 
diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
index 764d5b029e..b6c7d3b06a 100644
--- a/src/qemu/qemu_validate.c
+++ b/src/qemu/qemu_validate.c
@@ -380,6 +380,18 @@ qemuValidateDomainDefCpu(virQEMUDriver *driver,
         break;
     }
 
+    if (cpu->addr &&
+        cpu->addr->mode == VIR_CPU_MAX_PHYS_ADDR_MODE_EMULATE &&
+        driver->hostcpu &&
+        driver->hostcpu->addr) {
+        if (cpu->addr->bits > driver->hostcpu->addr->bits) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("The number of virtual CPU address bits cannot "
+                             "exceed the number supported by the host CPU"));
+            return -1;
+        }
+    }
+
     return 0;
 }
 
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-emulate.args b/tests/qemuxml2argvdata/cpu-phys-bits-emulate.args
new file mode 100644
index 0000000000..f612f6889b
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-emulate.args
@@ -0,0 +1,32 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-foo \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-foo/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-foo/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-foo/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name guest=foo,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,file=/tmp/lib/domain--1-foo/master-key.aes \
+-machine pc,usb=off,dump-guest-core=off \
+-accel kvm \
+-cpu host,phys-bits=42 \
+-m 214 \
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server=on,wait=off \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-boot strict=on \
+-usb \
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x2 \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-emulate.xml b/tests/qemuxml2argvdata/cpu-phys-bits-emulate.xml
new file mode 100644
index 0000000000..f8bd63bc68
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-emulate.xml
@@ -0,0 +1,20 @@
+<domain type='kvm'>
+  <name>foo</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu mode='host-passthrough'>
+    <maxphysaddr mode='emulate' bits='42'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-emulate2.args b/tests/qemuxml2argvdata/cpu-phys-bits-emulate2.args
new file mode 100644
index 0000000000..3489f44ded
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-emulate2.args
@@ -0,0 +1,32 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-foo \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-foo/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-foo/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-foo/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name guest=foo,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,file=/tmp/lib/domain--1-foo/master-key.aes \
+-machine pc,usb=off,dump-guest-core=off \
+-accel kvm \
+-cpu core2duo,ds=on,acpi=on,ss=on,ht=on,tm=on,pbe=on,ds-cpl=on,vmx=on,est=on,tm2=on,cx16=on,xtpr=on,lahf-lm=on,phys-bits=42 \
+-m 214 \
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server=on,wait=off \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-boot strict=on \
+-usb \
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x2 \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-emulate2.xml b/tests/qemuxml2argvdata/cpu-phys-bits-emulate2.xml
new file mode 100644
index 0000000000..188b3066ed
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-emulate2.xml
@@ -0,0 +1,20 @@
+<domain type='kvm'>
+  <name>foo</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu mode='host-model'>
+    <maxphysaddr bits='42' mode='emulate'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-emulate3.err b/tests/qemuxml2argvdata/cpu-phys-bits-emulate3.err
new file mode 100644
index 0000000000..5e21998259
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-emulate3.err
@@ -0,0 +1 @@
+unsupported configuration: if using CPU maximum physical address mode='emulate', bits= must be specified too
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-emulate3.xml b/tests/qemuxml2argvdata/cpu-phys-bits-emulate3.xml
new file mode 100644
index 0000000000..30a14894dd
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-emulate3.xml
@@ -0,0 +1,20 @@
+<domain type='kvm'>
+  <name>foo</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu mode='host-passthrough'>
+    <maxphysaddr mode='emulate'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-passthrough.args b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough.args
new file mode 100644
index 0000000000..df04195d1e
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough.args
@@ -0,0 +1,32 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-foo \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-foo/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-foo/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-foo/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name guest=foo,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,file=/tmp/lib/domain--1-foo/master-key.aes \
+-machine pc,usb=off,dump-guest-core=off \
+-accel kvm \
+-cpu host,host-phys-bits=on \
+-m 214 \
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server=on,wait=off \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-boot strict=on \
+-usb \
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x2 \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-passthrough.xml b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough.xml
new file mode 100644
index 0000000000..db570beb8d
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough.xml
@@ -0,0 +1,20 @@
+<domain type='kvm'>
+  <name>foo</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu mode='host-passthrough'>
+    <maxphysaddr mode='passthrough'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-passthrough2.err b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough2.err
new file mode 100644
index 0000000000..22009cc6e6
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough2.err
@@ -0,0 +1 @@
+unsupported configuration: CPU maximum physical address bits mode 'passthrough' can only be used with 'host-passthrough' CPUs
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-passthrough2.xml b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough2.xml
new file mode 100644
index 0000000000..511bbf9949
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough2.xml
@@ -0,0 +1,20 @@
+<domain type='kvm'>
+  <name>foo</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu mode='host-model'>
+    <maxphysaddr mode='passthrough'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-passthrough3.err b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough3.err
new file mode 100644
index 0000000000..28f2e43432
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough3.err
@@ -0,0 +1 @@
+unsupported configuration: CPU maximum physical address bits number specification cannot be used with mode='passthrough'
diff --git a/tests/qemuxml2argvdata/cpu-phys-bits-passthrough3.xml b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough3.xml
new file mode 100644
index 0000000000..a94e567dcb
--- /dev/null
+++ b/tests/qemuxml2argvdata/cpu-phys-bits-passthrough3.xml
@@ -0,0 +1,20 @@
+<domain type='kvm'>
+  <name>foo</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu mode='host-passthrough'>
+    <maxphysaddr mode='passthrough' bits='42'/>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index b72d61c3bc..4fd5120b70 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -3470,6 +3470,13 @@ mymain(void)
     /* HVF guests should not work on Linux with KVM */
     DO_TEST_CAPS_LATEST_PARSE_ERROR("hvf-x86_64-q35-headless");
 
+    DO_TEST("cpu-phys-bits-passthrough", QEMU_CAPS_KVM);
+    DO_TEST("cpu-phys-bits-emulate", QEMU_CAPS_KVM);
+    DO_TEST("cpu-phys-bits-emulate2", QEMU_CAPS_KVM);
+    DO_TEST_PARSE_ERROR("cpu-phys-bits-emulate3", QEMU_CAPS_KVM);
+    DO_TEST_PARSE_ERROR("cpu-phys-bits-passthrough2", QEMU_CAPS_KVM);
+    DO_TEST_PARSE_ERROR("cpu-phys-bits-passthrough3", QEMU_CAPS_KVM);
+
     if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
         virFileDeleteTree(fakerootdir);
 
-- 
2.36.1



More information about the libvir-list mailing list