Skip to contentSkip to conversation

x86: Fix breakage of fw_cfg for 32-bit unit tests

PATCH In review Sean Christopherson 2 participants 3 messages Aug 22, 2019 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Fix breakage of fw_cfg for 32-bit unit tests

Ensure the fw_cfg overrides are parsed prior consuming any of said overrides. fwcfg_get_u() treats zero as a valid overide value, which is slightly problematic since the overrides are in the .bss and thus initialized to zero.

Add a limit check when indexing fw_override so that future code doesn't spontaneously explode.

Cc Nadav Amit <[email protected]>
Fixes 03b1e4570f967 ("x86: Support environments without test-devices")
---
 lib/x86/fwcfg.c | 10 ++++++++--
 lib/x86/fwcfg.h |  2 --
 x86/cstart64.S  |  2 --
 3 files changed, 8 insertions(+), 6 deletions(-)
lib/x86/fwcfg.c +8 -2
index d8d797f..06ef62c 100644
@@ -5,10 +5,11 @@
 static struct spinlock lock;
 
 static long fw_override[FW_CFG_MAX_ENTRY];
+static bool fw_override_done;
 
 bool no_test_device;
 
-void read_cfg_override(void)
+static void read_cfg_override(void)
 {
 	const char *str;
 	int i;
@@ -26,6 +27,8 @@ void read_cfg_override(void)
 
 	if ((str = getenv("TEST_DEVICE")))
 		no_test_device = !atol(str);
+
+    fw_override_done = true;
 }
 
 static uint64_t fwcfg_get_u(uint16_t index, int bytes)
@@ -34,7 +37,10 @@ static uint64_t fwcfg_get_u(uint16_t index, int bytes)
     uint8_t b;
     int i;
 
-    if (fw_override[index] >= 0)
+    if (!fw_override_done)
+        read_cfg_override();
+
+    if (index < FW_CFG_MAX_ENTRY && fw_override[index] >= 0)
 	    return fw_override[index];
 
     spin_lock(&lock);
lib/x86/fwcfg.h +0 -2
index 88dc7a7..2f17461 100644
@@ -36,8 +36,6 @@
 
 extern bool no_test_device;
 
-void read_cfg_override(void);
-
 static inline bool test_device_enabled(void)
 {
 	return !no_test_device;
x86/cstart64.S +0 -2
index 23c1bd4..d4e4652 100644
@@ -254,8 +254,6 @@ start64:
 	mov %rax, __args(%rip)
 	call __setup_args
 
-	/* Read the configuration before running smp_init */
-	call read_cfg_override
 	call smp_init
 	call enable_x2apic
 
Signature · 1 line
2.22.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 46 lines · Sean Christopherson

Ensure the fw_cfg overrides are parsed prior consuming any of said overrides. fwcfg_get_u() treats zero as a valid overide value, which is slightly problematic since the overrides are in the .bss and thus initialized to zero.

Add a limit check when indexing fw_override so that future code doesn't spontaneously explode.

Cc Nadav Amit <[email protected]>
Fixes 03b1e4570f967 ("x86: Support environments without test-devices")

---

lib/x86/fwcfg.c | 10 ++++++++--
lib/x86/fwcfg.h |  2 --
x86/cstart64.S  |  2 --
3 files changed, 8 insertions(+), 6 deletions(-)
lib/x86/fwcfg.c +4 -1
index d8d797f..06ef62c 100644
@@ -5,10 +5,11 @@
 static struct spinlock lock;
 
 static long fw_override[FW_CFG_MAX_ENTRY];
+static bool fw_override_done;
 
 bool no_test_device;
 
-void read_cfg_override(void)
+static void read_cfg_override(void)
 {
 	const char *str;
 	int i;
@@ -26,6 +27,8 @@ void read_cfg_override(void)
 
 	if ((str = getenv("TEST_DEVICE")))
 		no_test_device = !atol(str);
+
+    fw_override_done = true;
 }
 
 static uint64_t fwcfg_get_u(uint16_t index, int bytes)

lib/x86/fwcfg.c static uint64_t fwcfg_get_u(uint16_t index, int bytes) · line 44

Quoted 9 lines
lib/x86/fwcfg.c +4 -1
@@ -34,7 +37,10 @@ static uint64_t fwcfg_get_u(uint16_t index, int bytes)
    uint8_t b;
    int i;
 
-    if (fw_override[index] >= 0)
+    if (!fw_override_done)
+        read_cfg_override();
+
+    if (index < FW_CFG_MAX_ENTRY && fw_override[index] >= 0)
 	    return fw_override[index];

How did that happen? I remember I tested this code with KVM..

Anyhow,

Thanks for fixing it.

#2 lore raw
Quoted 49 lines · Nadav Amit
Quoted 49 lines · Sean Christopherson

Ensure the fw_cfg overrides are parsed prior consuming any of said overrides. fwcfg_get_u() treats zero as a valid overide value, which is slightly problematic since the overrides are in the .bss and thus initialized to zero.

Add a limit check when indexing fw_override so that future code doesn't spontaneously explode.

Cc Nadav Amit <[email protected]>
Fixes 03b1e4570f967 ("x86: Support environments without test-devices")

---

lib/x86/fwcfg.c | 10 ++++++++--
lib/x86/fwcfg.h |  2 --
x86/cstart64.S  |  2 --
3 files changed, 8 insertions(+), 6 deletions(-)
lib/x86/fwcfg.c +4 -1
index d8d797f..06ef62c 100644
@@ -5,10 +5,11 @@
 static struct spinlock lock;
 
 static long fw_override[FW_CFG_MAX_ENTRY];
+static bool fw_override_done;
 
 bool no_test_device;
 
-void read_cfg_override(void)
+static void read_cfg_override(void)
 {
 	const char *str;
 	int i;
@@ -26,6 +27,8 @@ void read_cfg_override(void)
 
 	if ((str = getenv("TEST_DEVICE")))
 		no_test_device = !atol(str);
+
+    fw_override_done = true;
 }
 
 static uint64_t fwcfg_get_u(uint16_t index, int bytes)
@@ -34,7 +37,10 @@ static uint64_t fwcfg_get_u(uint16_t index, int bytes)
    uint8_t b;
    int i;
 
Quoted 8 lines

lib/x86/fwcfg.c static uint64_t fwcfg_get_u(uint16_t index, int bytes) · line 44

Quoted 6 lines
lib/x86/fwcfg.c +4 -1
-    if (fw_override[index] >= 0)
+    if (!fw_override_done)
+        read_cfg_override();
+
+    if (index < FW_CFG_MAX_ENTRY && fw_override[index] >= 0)
 	    return fw_override[index];

How did that happen? I remember I tested this code with KVM..

It only breaks 32-bit KVM.