Skip to contentSkip to conversation

m68k: rework BI_VIRT_RNG_SEED as BI_RNG_SEED

PATCH Applied Jason A. Donenfeld 2 participants 9 messages Sep 26, 2022 – Sep 29, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 m68k: rework BI_VIRT_RNG_SEED as BI_RNG_SEED

Following a change on the kernel side (see link), pass BI_RNG_SEED instead of BI_VIRT_RNG_SEED. This should have no impact on compatibility, as there will simply be no effect if it's an old kernel, which is how things have always been. We then use this as an opportunity to add this to q800, since now we can, which is a nice improvement.

---
 hw/m68k/q800.c                                    | 7 +++++++
 hw/m68k/virt.c                                    | 2 +-
 include/standard-headers/asm-m68k/bootinfo-virt.h | 4 +++-
 include/standard-headers/asm-m68k/bootinfo.h      | 8 +++++++-
 4 files changed, 18 insertions(+), 3 deletions(-)
hw/m68k/q800.c +7 -0
index 101ab0f803..9106382066 100644
@@ -23,6 +23,7 @@
 #include "qemu/osdep.h"
 #include "qemu/units.h"
 #include "qemu/datadir.h"
+#include "qemu/guest-random.h"
 #include "sysemu/sysemu.h"
 #include "cpu.h"
 #include "hw/boards.h"
@@ -385,6 +386,7 @@ static void q800_init(MachineState *machine)
     NubusBus *nubus;
     DeviceState *glue;
     DriveInfo *dinfo;
+    uint8_t rng_seed[32];
 
     linux_boot = (kernel_filename != NULL);
 
@@ -634,6 +636,11 @@ static void q800_init(MachineState *machine)
                         kernel_cmdline);
         }
 
+	/* Pass seed to RNG. */
+	qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
+	BOOTINFODATA(cs->as, parameters_base, BI_RNG_SEED,
+		     rng_seed, sizeof(rng_seed));
+
         /* load initrd */
         if (initrd_filename) {
             initrd_size = get_image_size(initrd_filename);
hw/m68k/virt.c +1 -1
index 2f3ffc0de6..7180325e54 100644
@@ -250,7 +250,7 @@ static void virt_init(MachineState *machine)
 
 	/* Pass seed to RNG. */
 	qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
-	BOOTINFODATA(cs->as, parameters_base, BI_VIRT_RNG_SEED,
+	BOOTINFODATA(cs->as, parameters_base, BI_RNG_SEED,
 		     rng_seed, sizeof(rng_seed));
 
         /* load initrd */
include/standard-headers/asm-m68k/bootinfo-virt.h +3 -1
index 1b1ffd4705..75ac6bbd7d 100644
@@ -12,7 +12,9 @@
 #define BI_VIRT_GF_TTY_BASE	0x8003
 #define BI_VIRT_VIRTIO_BASE	0x8004
 #define BI_VIRT_CTRL_BASE	0x8005
-#define BI_VIRT_RNG_SEED	0x8006
+
+/* No longer used -- replaced with BI_RNG_SEED -- but don't reuse this index:
+ * #define BI_VIRT_RNG_SEED	0x8006 */
 
 #define VIRT_BOOTI_VERSION	MK_BI_VERSION(2, 0)
 
include/standard-headers/asm-m68k/bootinfo.h +7 -1
index 7b790e8ec8..b7a8dd2514 100644
@@ -57,7 +57,13 @@ struct mem_info {
 					/* (struct mem_info) */
 #define BI_COMMAND_LINE		0x0007	/* kernel command line parameters */
 					/* (string) */
-
+/*
+ * A random seed used to initialize the RNG. Record format:
+ *
+ *   - length       [ 2 bytes, 16-bit big endian ]
+ *   - seed data    [ `length` bytes, padded to preserve 4-byte struct alignment ]
+ */
+#define BI_RNG_SEED		0x0008
 
     /*
      *  Linux/m68k Architectures (BI_MACHTYPE)
Signature · 1 line
2.37.3
#1 lore raw

PATCH v2 2/2 m68k: align bootinfo strings and data to 4 bytes

Various tools, such as kexec-tools and m68k-bootinfo, expect each bootinfo entry to be aligned to 4 bytes, not 2 bytes. So adjust the padding to fill this out as such.

Also, break apart the padding additions from the other field length additions, so that it's more clear why these magic numbers are being added, and comment them too.

---
 hw/m68k/bootinfo.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
hw/m68k/bootinfo.h +6 -4
index bd8b212fd3..897162b818 100644
@@ -48,13 +48,14 @@
         stw_phys(as, base, id); \
         base += 2; \
         stw_phys(as, base, \
-                 (sizeof(struct bi_record) + strlen(string) + 2) & ~1); \
+                 (sizeof(struct bi_record) + strlen(string) + \
+                  1 /* null termination */ + 3 /* padding */) & ~3); \
         base += 2; \
         for (i = 0; string[i]; i++) { \
             stb_phys(as, base++, string[i]); \
         } \
         stb_phys(as, base++, 0); \
-        base = (base + 1) & ~1; \
+        base = (base + 3) & ~3; \
     } while (0)
 
 #define BOOTINFODATA(as, base, id, data, len) \
@@ -63,13 +64,14 @@
         stw_phys(as, base, id); \
         base += 2; \
         stw_phys(as, base, \
-                 (sizeof(struct bi_record) + len + 3) & ~1); \
+                 (sizeof(struct bi_record) + len + \
+                  2 /* length field */ + 3 /* padding */) & ~3); \
         base += 2; \
         stw_phys(as, base, len); \
         base += 2; \
         for (i = 0; i < len; ++i) { \
             stb_phys(as, base++, data[i]); \
         } \
-        base = (base + 1) & ~1; \
+        base = (base + 3) & ~3; \
     } while (0)
 #endif
Signature · 1 line
2.37.3
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH qemu v2 2/2] m68k: align bootinfo strings and data to 4 bytes

Le 26/09/2022 à 13:39, Jason A. Donenfeld a écrit :

Quoted 3 lines

Various tools, such as kexec-tools and m68k-bootinfo, expect each bootinfo entry to be aligned to 4 bytes, not 2 bytes. So adjust the padding to fill this out as such.

Agree, I found the same problem using petitboot as a ROM for the virt machine [1]. (I didn't update BOOTINFOSTR() but I think I should).

[1] https://github.com/vivier/qemu-m68k/commits/m68k-virt

Quoted 50 lines

Also, break apart the padding additions from the other field length additions, so that it's more clear why these magic numbers are being added, and comment them too.

---
  hw/m68k/bootinfo.h | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
hw/m68k/bootinfo.h +6 -4
index bd8b212fd3..897162b818 100644
@@ -48,13 +48,14 @@
          stw_phys(as, base, id); \
          base += 2; \
          stw_phys(as, base, \
-                 (sizeof(struct bi_record) + strlen(string) + 2) & ~1); \
+                 (sizeof(struct bi_record) + strlen(string) + \
+                  1 /* null termination */ + 3 /* padding */) & ~3); \
          base += 2; \
          for (i = 0; string[i]; i++) { \
              stb_phys(as, base++, string[i]); \
          } \
          stb_phys(as, base++, 0); \
-        base = (base + 1) & ~1; \
+        base = (base + 3) & ~3; \
      } while (0)
 
  #define BOOTINFODATA(as, base, id, data, len) \
@@ -63,13 +64,14 @@
          stw_phys(as, base, id); \
          base += 2; \
          stw_phys(as, base, \
-                 (sizeof(struct bi_record) + len + 3) & ~1); \
+                 (sizeof(struct bi_record) + len + \
+                  2 /* length field */ + 3 /* padding */) & ~3); \
          base += 2; \
          stw_phys(as, base, len); \
          base += 2; \
          for (i = 0; i < len; ++i) { \
              stb_phys(as, base++, data[i]); \
          } \
-        base = (base + 1) & ~1; \
+        base = (base + 3) & ~3; \
      } while (0)
  #endif
#3 lore raw

Re: [PATCH qemu v2 2/2] m68k: align bootinfo strings and data to 4 bytes

In reply to Jason A. Donenfeld

Le 26/09/2022 à 13:39, Jason A. Donenfeld a écrit :

Quoted 8 lines

Various tools, such as kexec-tools and m68k-bootinfo, expect each bootinfo entry to be aligned to 4 bytes, not 2 bytes. So adjust the padding to fill this out as such.

Also, break apart the padding additions from the other field length additions, so that it's more clear why these magic numbers are being added, and comment them too.

Quoted 7 lines
---
  hw/m68k/bootinfo.h | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)

Applied to my m68k-for-7.2 branch

Thanks,
Laurent

#4 lore raw

Re: [PATCH qemu v2 2/2] m68k: align bootinfo strings and data to 4 bytes

Quoted 18 lines · Laurent Vivier
Quoted 8 lines · Jason A. Donenfeld

Various tools, such as kexec-tools and m68k-bootinfo, expect each bootinfo entry to be aligned to 4 bytes, not 2 bytes. So adjust the padding to fill this out as such.

Also, break apart the padding additions from the other field length additions, so that it's more clear why these magic numbers are being added, and comment them too.

Quoted 7 lines
---
  hw/m68k/bootinfo.h | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 1 line

Applied to my m68k-for-7.2 branch

What about 1/2?

Jason

#5 lore raw

Re: [PATCH qemu v2 2/2] m68k: align bootinfo strings and data to 4 bytes

Le 26/09/2022 à 23:40, Jason A. Donenfeld a écrit :

Quoted 19 lines · Laurent Vivier
Quoted 18 lines · Jason A. Donenfeld
Quoted 15 lines

Various tools, such as kexec-tools and m68k-bootinfo, expect each bootinfo entry to be aligned to 4 bytes, not 2 bytes. So adjust the padding to fill this out as such.

Also, break apart the padding additions from the other field length additions, so that it's more clear why these magic numbers are being added, and comment them too.

---
   hw/m68k/bootinfo.h | 10 ++++++----
   1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 4 lines
Quoted 1 line

Applied to my m68k-for-7.2 branch

What about 1/2?

I'd like to wait a little to see what happens on the linux side.

Thanks,
Laurent

#6 lore raw

Re: [PATCH qemu v2 2/2] m68k: align bootinfo strings and data to 4 bytes

Quoted 21 lines · Laurent Vivier
Quoted 19 lines · Jason A. Donenfeld
Quoted 18 lines · Laurent Vivier
Quoted 15 lines · Jason A. Donenfeld

Various tools, such as kexec-tools and m68k-bootinfo, expect each bootinfo entry to be aligned to 4 bytes, not 2 bytes. So adjust the padding to fill this out as such.

Also, break apart the padding additions from the other field length additions, so that it's more clear why these magic numbers are being added, and comment them too.

---
   hw/m68k/bootinfo.h | 10 ++++++----
   1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Applied to my m68k-for-7.2 branch

What about 1/2?

I'd like to wait a little to see what happens on the linux side.

Alright, makes sense. Just please don't forget about it.

Jason

#7 lore raw

Re: [PATCH qemu v2 2/2] m68k: align bootinfo strings and data to 4 bytes

Quoted 22 lines · Jason A. Donenfeld
Quoted 21 lines · Laurent Vivier
Quoted 19 lines · Jason A. Donenfeld
Quoted 18 lines · Laurent Vivier
Quoted 15 lines · Jason A. Donenfeld

Various tools, such as kexec-tools and m68k-bootinfo, expect each bootinfo entry to be aligned to 4 bytes, not 2 bytes. So adjust the padding to fill this out as such.

Also, break apart the padding additions from the other field length additions, so that it's more clear why these magic numbers are being added, and comment them too.

---
   hw/m68k/bootinfo.h | 10 ++++++----
   1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Applied to my m68k-for-7.2 branch

What about 1/2?

I'd like to wait a little to see what happens on the linux side.

Alright, makes sense. Just please don't forget about it.

Okay, all set. Uneventful.
https://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k.git/commit/?id=f1bb20c8be1929743fdb313b4770601afc39c1b7

Jason

#8 lore raw
In reply to Jason A. Donenfeld

Le 26/09/2022 à 13:38, Jason A. Donenfeld a écrit :

Quoted 10 lines

Following a change on the kernel side (see link), pass BI_RNG_SEED instead of BI_VIRT_RNG_SEED. This should have no impact on compatibility, as there will simply be no effect if it's an old kernel, which is how things have always been. We then use this as an opportunity to add this to q800, since now we can, which is a nice improvement.

Quoted 7 lines
---
  hw/m68k/q800.c                                    | 7 +++++++
  hw/m68k/virt.c                                    | 2 +-
  include/standard-headers/asm-m68k/bootinfo-virt.h | 4 +++-
  include/standard-headers/asm-m68k/bootinfo.h      | 8 +++++++-
  4 files changed, 18 insertions(+), 3 deletions(-)

Applied to my m68k-for-7.2 branch

Thanks,
Laurent