From nobody Sat Apr 27 22:39:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=zx2c4.com ARC-Seal: i=1; a=rsa-sha256; t=1658408270; cv=none; d=zohomail.com; s=zohoarc; b=kaXBbC6ie43DKHT6K4jx+fhP74XB04was+cyeZctw30/FxrLw6keXJ7zuXFW6P3c4AOSBqWAoRRifQOD2jWgKbGkS5CTX+Td54xhKw0TJH1EZyFszgAA9qFTB2j0vZvp6jnWypk2OxT3cR0U72NVx+kBAqHn+WlRPMqQDYCOf4I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658408270; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fcJT6V+iqLk8XId12/0cpE1bRt1nHf9s+p7mIhOPfpg=; b=iNzOXnubnQN/GpKRQAz6pQpT+Ot4UguWExxNUrdE/rF23xEJj23ItK2137kInDycMKxCCEkFXVYIJvWNELshRuT7hKWLDJ8ZdrquKhhoBHAdz6fxYQTQwDNOtd0KumOfEPDyPHywOow+IRPvWPIsH6CuhsBVA/KSxstiYX8gDxw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658408270786555.9141731268405; Thu, 21 Jul 2022 05:57:50 -0700 (PDT) Received: from localhost ([::1]:51098 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEVkT-0002wR-Qf for importer@patchew.org; Thu, 21 Jul 2022 08:57:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41372) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVjV-00022w-58 for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:56:49 -0400 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]:60934) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVjS-0002FB-Uw for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:56:48 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0976C61DB8; Thu, 21 Jul 2022 12:56:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7BDE1C3411E; Thu, 21 Jul 2022 12:56:42 +0000 (UTC) Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id c101f79a (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Thu, 21 Jul 2022 12:56:40 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="Li99ECUC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1658408200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fcJT6V+iqLk8XId12/0cpE1bRt1nHf9s+p7mIhOPfpg=; b=Li99ECUCbDukictSKeWegIMYZez4EmukKNd2PJSh7U+c9D/4gc/rV9pP21ufIHDYtPm7hM yaj4oJaxN0wXulECnJyaEoyMaA40+P+zYHJHq28wB68ryWqkel6BTmxxHKm7arvDCYLM87 swIuTdWtgojLTp5zuGkVKBiufHXcmHY= From: "Jason A. Donenfeld" To: Paolo Bonzini , "Michael S. Tsirkin" , QEMU Developers Cc: "Jason A. Donenfeld" , Marcel Apfelbaum , Richard Henderson , Eduardo Habkost , Peter Maydell , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Laurent Vivier Subject: [PATCH v7] hw/i386: pass RNG seed via setup_data entry Date: Thu, 21 Jul 2022 14:56:36 +0200 Message-Id: <20220721125636.446842-1-Jason@zx2c4.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2604:1380:4641:c500::1; envelope-from=SRS0=2GtW=X2=zx2c4.com=Jason@kernel.org; helo=dfw.source.kernel.org X-Spam_score_int: -67 X-Spam_score: -6.8 X-Spam_bar: ------ X-Spam_report: (-6.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @zx2c4.com) X-ZM-MESSAGEID: 1658408271370100001 Tiny machines optimized for fast boot time generally don't use EFI, which means a random seed has to be supplied some other way. For this purpose, Linux (=E2=89=A55.20) supports passing a seed in the setup_data ta= ble with SETUP_RNG_SEED, specially intended for hypervisors, kexec, and specialized bootloaders. The linked commit shows the upstream kernel implementation. At Paolo's request, we don't pass these to versioned machine types =E2=89= =A47.0. Link: https://git.kernel.org/tip/tip/c/68b8e9713c8 Cc: Marcel Apfelbaum Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Cc: Peter Maydell Cc: Philippe Mathieu-Daud=C3=A9 Cc: Laurent Vivier Reviewed-by: Michael S. Tsirkin Signed-off-by: Jason A. Donenfeld --- hw/i386/microvm.c | 2 +- hw/i386/pc.c | 4 +-- hw/i386/pc_piix.c | 2 ++ hw/i386/pc_q35.c | 2 ++ hw/i386/x86.c | 26 +++++++++++++++++--- include/hw/i386/pc.h | 3 +++ include/hw/i386/x86.h | 3 ++- include/standard-headers/asm-x86/bootparam.h | 1 + 8 files changed, 35 insertions(+), 8 deletions(-) diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c index 754f1d0593..5b0ee3ab26 100644 --- a/hw/i386/microvm.c +++ b/hw/i386/microvm.c @@ -332,7 +332,7 @@ static void microvm_memory_init(MicrovmMachineState *mm= s) rom_set_fw(fw_cfg); =20 if (machine->kernel_filename !=3D NULL) { - x86_load_linux(x86ms, fw_cfg, 0, true); + x86_load_linux(x86ms, fw_cfg, 0, true, false); } =20 if (mms->option_roms) { diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 774cb2bf07..d2b5823ffb 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -796,7 +796,7 @@ void xen_load_linux(PCMachineState *pcms) rom_set_fw(fw_cfg); =20 x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size, - pcmc->pvh_enabled); + pcmc->pvh_enabled, pcmc->legacy_no_rng_seed); for (i =3D 0; i < nb_option_roms; i++) { assert(!strcmp(option_rom[i].name, "linuxboot.bin") || !strcmp(option_rom[i].name, "linuxboot_dma.bin") || @@ -992,7 +992,7 @@ void pc_memory_init(PCMachineState *pcms, =20 if (linux_boot) { x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size, - pcmc->pvh_enabled); + pcmc->pvh_enabled, pcmc->legacy_no_rng_seed); } =20 for (i =3D 0; i < nb_option_roms; i++) { diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c index a234989ac3..fbf9465318 100644 --- a/hw/i386/pc_piix.c +++ b/hw/i386/pc_piix.c @@ -438,9 +438,11 @@ DEFINE_I440FX_MACHINE(v7_1, "pc-i440fx-7.1", NULL, =20 static void pc_i440fx_7_0_machine_options(MachineClass *m) { + PCMachineClass *pcmc =3D PC_MACHINE_CLASS(m); pc_i440fx_7_1_machine_options(m); m->alias =3D NULL; m->is_default =3D false; + pcmc->legacy_no_rng_seed =3D true; compat_props_add(m->compat_props, hw_compat_7_0, hw_compat_7_0_len); compat_props_add(m->compat_props, pc_compat_7_0, pc_compat_7_0_len); } diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c index f96cbd04e2..12cc76aaf8 100644 --- a/hw/i386/pc_q35.c +++ b/hw/i386/pc_q35.c @@ -375,8 +375,10 @@ DEFINE_Q35_MACHINE(v7_1, "pc-q35-7.1", NULL, =20 static void pc_q35_7_0_machine_options(MachineClass *m) { + PCMachineClass *pcmc =3D PC_MACHINE_CLASS(m); pc_q35_7_1_machine_options(m); m->alias =3D NULL; + pcmc->legacy_no_rng_seed =3D true; compat_props_add(m->compat_props, hw_compat_7_0, hw_compat_7_0_len); compat_props_add(m->compat_props, pc_compat_7_0, pc_compat_7_0_len); } diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 6003b4b2df..ecea25d249 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -26,6 +26,7 @@ #include "qemu/cutils.h" #include "qemu/units.h" #include "qemu/datadir.h" +#include "qemu/guest-random.h" #include "qapi/error.h" #include "qapi/qmp/qerror.h" #include "qapi/qapi-visit-common.h" @@ -766,7 +767,8 @@ static bool load_elfboot(const char *kernel_filename, void x86_load_linux(X86MachineState *x86ms, FWCfgState *fw_cfg, int acpi_data_size, - bool pvh_enabled) + bool pvh_enabled, + bool legacy_no_rng_seed) { bool linuxboot_dma_enabled =3D X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma= _enabled; uint16_t protocol; @@ -774,7 +776,7 @@ void x86_load_linux(X86MachineState *x86ms, int dtb_size, setup_data_offset; uint32_t initrd_max; uint8_t header[8192], *setup, *kernel; - hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr =3D 0; + hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr =3D 0, first_se= tup_data =3D 0; FILE *f; char *vmode; MachineState *machine =3D MACHINE(x86ms); @@ -784,6 +786,7 @@ void x86_load_linux(X86MachineState *x86ms, const char *dtb_filename =3D machine->dtb; const char *kernel_cmdline =3D machine->kernel_cmdline; SevKernelLoaderContext sev_load_ctx =3D {}; + enum { RNG_SEED_LENGTH =3D 32 }; =20 /* Align to 16 bytes as a paranoia measure */ cmdline_size =3D (strlen(kernel_cmdline) + 16) & ~15; @@ -1063,16 +1066,31 @@ void x86_load_linux(X86MachineState *x86ms, kernel_size =3D setup_data_offset + sizeof(struct setup_data) + dt= b_size; kernel =3D g_realloc(kernel, kernel_size); =20 - stq_p(header + 0x250, prot_addr + setup_data_offset); =20 setup_data =3D (struct setup_data *)(kernel + setup_data_offset); - setup_data->next =3D 0; + setup_data->next =3D cpu_to_le64(first_setup_data); + first_setup_data =3D prot_addr + setup_data_offset; setup_data->type =3D cpu_to_le32(SETUP_DTB); setup_data->len =3D cpu_to_le32(dtb_size); =20 load_image_size(dtb_filename, setup_data->data, dtb_size); } =20 + if (!legacy_no_rng_seed) { + setup_data_offset =3D QEMU_ALIGN_UP(kernel_size, 16); + kernel_size =3D setup_data_offset + sizeof(struct setup_data) + RN= G_SEED_LENGTH; + kernel =3D g_realloc(kernel, kernel_size); + setup_data =3D (struct setup_data *)(kernel + setup_data_offset); + setup_data->next =3D cpu_to_le64(first_setup_data); + first_setup_data =3D prot_addr + setup_data_offset; + setup_data->type =3D cpu_to_le32(SETUP_RNG_SEED); + setup_data->len =3D cpu_to_le32(RNG_SEED_LENGTH); + qemu_guest_getrandom_nofail(setup_data->data, RNG_SEED_LENGTH); + } + + /* Offset 0x250 is a pointer to the first setup_data link. */ + stq_p(header + 0x250, first_setup_data); + /* * If we're starting an encrypted VM, it will be OVMF based, which use= s the * efi stub for booting and doesn't require any values to be placed in= the diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h index b7735dccfc..2a8ffbcfa8 100644 --- a/include/hw/i386/pc.h +++ b/include/hw/i386/pc.h @@ -127,6 +127,9 @@ struct PCMachineClass { =20 /* create kvmclock device even when KVM PV features are not exposed */ bool kvmclock_create_always; + + /* skip passing an rng seed for legacy machines */ + bool legacy_no_rng_seed; }; =20 #define TYPE_PC_MACHINE "generic-pc-machine" diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index 9089bdd99c..6bdf1f6ab2 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -123,7 +123,8 @@ void x86_bios_rom_init(MachineState *ms, const char *de= fault_firmware, void x86_load_linux(X86MachineState *x86ms, FWCfgState *fw_cfg, int acpi_data_size, - bool pvh_enabled); + bool pvh_enabled, + bool legacy_no_rng_seed); =20 bool x86_machine_is_smm_enabled(const X86MachineState *x86ms); bool x86_machine_is_acpi_enabled(const X86MachineState *x86ms); diff --git a/include/standard-headers/asm-x86/bootparam.h b/include/standar= d-headers/asm-x86/bootparam.h index 072e2ed546..b2aaad10e5 100644 --- a/include/standard-headers/asm-x86/bootparam.h +++ b/include/standard-headers/asm-x86/bootparam.h @@ -10,6 +10,7 @@ #define SETUP_EFI 4 #define SETUP_APPLE_PROPERTIES 5 #define SETUP_JAILHOUSE 6 +#define SETUP_RNG_SEED 9 =20 #define SETUP_INDIRECT (1<<31) =20 --=20 2.35.1