qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] hw/i386: pass RNG seed via setup_data entry
@ 2022-07-11 14:54 Jason A. Donenfeld
  2022-07-19 11:53 ` [PATCH resend " Jason A. Donenfeld
  0 siblings, 1 reply; 23+ messages in thread
From: Jason A. Donenfeld @ 2022-07-11 14:54 UTC (permalink / raw)
  To: qemu-devel
  Cc: Jason A. Donenfeld, Laurent Vivier, Philippe Mathieu-Daudé

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 (≥5.20) supports passing a seed in the setup_data table
with SETUP_RNG_SEED, specially intended for hypervisors, kexec, and
specialized bootloaders. The linked commit shows the upstream kernel
implementation.

Link: https://git.kernel.org/tip/tip/c/68b8e9713c8
Cc: Laurent Vivier <laurent@vivier.eu>
Cc: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 hw/i386/x86.c                                | 19 ++++++++++++++-----
 include/standard-headers/asm-x86/bootparam.h |  1 +
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/hw/i386/x86.c b/hw/i386/x86.c
index 6003b4b2df..0724759eec 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"
@@ -1045,6 +1046,16 @@ void x86_load_linux(X86MachineState *x86ms,
     }
     fclose(f);
 
+    setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
+    kernel_size = setup_data_offset + sizeof(struct setup_data) + 32;
+    kernel = g_realloc(kernel, kernel_size);
+    stq_p(header + 0x250, prot_addr + setup_data_offset);
+    setup_data = (struct setup_data *)(kernel + setup_data_offset);
+    setup_data->next = 0;
+    setup_data->type = cpu_to_le32(SETUP_RNG_SEED);
+    setup_data->len = cpu_to_le32(32);
+    qemu_guest_getrandom_nofail(setup_data->data, 32);
+
     /* append dtb to kernel */
     if (dtb_filename) {
         if (protocol < 0x209) {
@@ -1059,13 +1070,11 @@ void x86_load_linux(X86MachineState *x86ms,
             exit(1);
         }
 
-        setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
-        kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
+        kernel_size += sizeof(struct setup_data) + dtb_size;
         kernel = g_realloc(kernel, kernel_size);
 
-        stq_p(header + 0x250, prot_addr + setup_data_offset);
-
-        setup_data = (struct setup_data *)(kernel + setup_data_offset);
+        setup_data->next = prot_addr + setup_data_offset + sizeof(*setup_data) + setup_data->len;
+        ++setup_data;
         setup_data->next = 0;
         setup_data->type = cpu_to_le32(SETUP_DTB);
         setup_data->len = cpu_to_le32(dtb_size);
diff --git a/include/standard-headers/asm-x86/bootparam.h b/include/standard-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
 
 #define SETUP_INDIRECT			(1<<31)
 
-- 
2.35.1



^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2022-07-22  6:12 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-11 14:54 [PATCH v3] hw/i386: pass RNG seed via setup_data entry Jason A. Donenfeld
2022-07-19 11:53 ` [PATCH resend " Jason A. Donenfeld
2022-07-20 13:03   ` Jason A. Donenfeld
2022-07-21  9:19     ` Paolo Bonzini
2022-07-21  9:47       ` Jason A. Donenfeld
2022-07-21  9:56         ` Jason A. Donenfeld
2022-07-21 10:09           ` [PATCH v4] " Jason A. Donenfeld
2022-07-21 10:35             ` Michael S. Tsirkin
2022-07-21 10:42               ` Jason A. Donenfeld
2022-07-21 10:47                 ` [PATCH v5] " Jason A. Donenfeld
2022-07-21 10:49                   ` [PATCH v6] " Jason A. Donenfeld
2022-07-21 11:00                     ` Michael S. Tsirkin
2022-07-21 11:00                       ` Jason A. Donenfeld
2022-07-21 11:47                       ` Paolo Bonzini
2022-07-21 12:16                         ` Jason A. Donenfeld
2022-07-21 12:27                           ` Daniel P. Berrangé
2022-07-21 12:41                             ` Paolo Bonzini
2022-07-21 12:52                               ` Jason A. Donenfeld
2022-07-21 12:56                                 ` [PATCH v7] " Jason A. Donenfeld
2022-07-21 13:00                                   ` Paolo Bonzini
2022-07-21 13:00                                   ` Paolo Bonzini
2022-07-21 13:04                                     ` Jason A. Donenfeld
2022-07-22  6:10                                       ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).