public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v7 qemu-server 2/4] Prevent starting a 32-bit VM using a 64-bit OVMF BIOS
Date: Tue, 19 Dec 2023 10:40:21 +0100	[thread overview]
Message-ID: <20231219094023.25726-4-f.schauer@proxmox.com> (raw)
In-Reply-To: <20231219094023.25726-1-f.schauer@proxmox.com>

Instead of starting a VM with a 32-bit CPU type and a 64-bit OVMF image,
throw an error before starting the VM telling the user that OVMF is not
supported on 32-bit CPU types.

To obtain a list of 32-bit CPU types, refer to the builtin_x86_defs in
target/i386/cpu.c of QEMU. Exclude any entries that have the long mode
feature (CPUID_EXT2_LM).

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 PVE/QemuServer.pm           |  5 ++++-
 PVE/QemuServer/CPUConfig.pm | 39 +++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 2063e66..a7b237e 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -52,7 +52,7 @@ use PVE::QemuConfig;
 use PVE::QemuServer::Helpers qw(config_aware_timeout min_version windows_version);
 use PVE::QemuServer::Cloudinit;
 use PVE::QemuServer::CGroup;
-use PVE::QemuServer::CPUConfig qw(print_cpu_device get_cpu_options);
+use PVE::QemuServer::CPUConfig qw(print_cpu_device get_cpu_options get_cpu_bitness);
 use PVE::QemuServer::Drive qw(is_valid_drivename drive_is_cloudinit drive_is_cdrom drive_is_read_only parse_drive print_drive);
 use PVE::QemuServer::Machine;
 use PVE::QemuServer::Memory qw(get_current_memory);
@@ -3689,6 +3689,9 @@ sub config_to_command {
     }
 
     if ($conf->{bios} && $conf->{bios} eq 'ovmf') {
+	die "OVMF (UEFI) BIOS is not supported on 32-bit CPU types\n"
+	    if !$forcecpu && get_cpu_bitness($conf->{cpu}, $arch) == 32;
+
 	my ($code_drive_str, $var_drive_str) =
 	    print_ovmf_drive_commandlines($conf, $storecfg, $vmid, $arch, $q35, $version_guard);
 	push $cmd->@*, '-drive', $code_drive_str;
diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index c25c2c8..abf3198 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -12,6 +12,7 @@ use base qw(PVE::SectionConfig Exporter);
 our @EXPORT_OK = qw(
 print_cpu_device
 get_cpu_options
+get_cpu_bitness
 );
 
 # under certain race-conditions, this module might be loaded before pve-cluster
@@ -57,6 +58,17 @@ my $depreacated_cpu_map = {
     'Icelake-Client-noTSX' => 'Icelake-Server-noTSX',
 };
 
+my $cputypes_32bit = {
+    '486' => 1,
+    'pentium' => 1,
+    'pentium2' => 1,
+    'pentium3' => 1,
+    'coreduo' => 1,
+    'athlon' => 1,
+    'kvm32' => 1,
+    'qemu32' => 1,
+};
+
 my $cpu_vendor_list = {
     # Intel CPUs
     486 => 'GenuineIntel',
@@ -716,6 +728,33 @@ sub get_cpu_from_running_vm {
     return $1;
 }
 
+sub get_cpu_bitness {
+    my ($cpu_prop_str, $arch) = @_;
+
+    die "missing 'arch'\n" if !$arch;
+
+    my $cputype = get_default_cpu_type($arch, 0);
+
+    if ($cpu_prop_str) {
+	my $cpu = PVE::JSONSchema::parse_property_string('pve-vm-cpu-conf', $cpu_prop_str)
+	    or die "Cannot parse cpu description: $cpu_prop_str\n";
+
+	my $cputype = $cpu->{cputype};
+
+	if (my $model = $builtin_models->{$cputype}) {
+	    $cputype = $model->{'reported-model'};
+	} elsif (is_custom_model($cputype)) {
+	    my $custom_cpu = get_custom_model($cputype);
+	    $cputype = $custom_cpu->{'reported-model'} // $cpu_fmt->{'reported-model'}->{default};
+	}
+    }
+
+    return $cputypes_32bit->{$cputype} ? 32 : 64 if $arch eq 'x86_64';
+    return 64 if $arch eq 'aarch64';
+
+    die "unsupported architecture '$arch'\n";
+}
+
 __PACKAGE__->register();
 __PACKAGE__->init();
 
-- 
2.39.2





  parent reply	other threads:[~2023-12-19  9:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-19  9:40 [pve-devel] [PATCH-SERIES v7 qemu-server, common] " Filip Schauer
2023-12-19  9:40 ` [pve-devel] [PATCH common 1/1] tools: Add is_native sub to compare the CPU architecture Filip Schauer
2024-02-19 14:46   ` Fiona Ebner
2024-02-21 14:37     ` Filip Schauer
2023-12-19  9:40 ` [pve-devel] [PATCH qemu-server 1/4] cpu config: Add helper to get the default CPU type Filip Schauer
2024-02-19 14:47   ` Fiona Ebner
2023-12-19  9:40 ` Filip Schauer [this message]
2024-02-19 14:47   ` [pve-devel] [PATCH v7 qemu-server 2/4] Prevent starting a 32-bit VM using a 64-bit OVMF BIOS Fiona Ebner
2024-02-19 14:58     ` Fiona Ebner
2023-12-19  9:40 ` [pve-devel] [PATCH qemu-server 3/4] Move is_native from PVE::QemuServer to PVE::Tools Filip Schauer
2023-12-19  9:40 ` [pve-devel] [PATCH qemu-server 4/4] cpu config: Unify the default value for 'kvm' Filip Schauer
2024-02-19 14:47   ` Fiona Ebner
2024-02-21 15:39     ` Filip Schauer
2024-02-22  9:35       ` Fiona Ebner
2024-02-23 11:59         ` Filip Schauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231219094023.25726-4-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal