Skip to contentSkip to conversation

x86/hvmloader: fix usage of NULL with cpuid_count()

PATCH In review Roger Pau Monne 3 participants 6 messages Apr 24, 2025 – Apr 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvmloader: fix usage of NULL with cpuid_count()

The commit that added support for retrieving the APIC IDs from the APs introduced several usages of cpuid() with NULL parameters, which is not handled by the underlying implementation. For GCC I expect this results in writes to the physical address at 0, however for Clang the generated code in smp.o is:

tools/firmware/hvmloader/smp.o: file format elf32-i386

Disassembly of section .text:

00000000 <smp_initialise>:

       0: 55                            pushl   %ebp
       1: 89 e5                         movl    %esp, %ebp
       3: 53                            pushl   %ebx
       4: 31 c0                         xorl    %eax, %eax
       6: 31 c9                         xorl    %ecx, %ecx
       8: 0f a2                         cpuid

Showing the usage of a NULL pointer results in undefined behavior, and clang refusing to generate further code after it.

Fix by using a temporary variable in cpuid_count() in place for any NULL parameter.

Fixes 9ad0db58c7e2 ('tools/hvmloader: Retrieve APIC IDs from the APs themselves')

---
Could also be fixed by using the temporary variable in the call sites, however that's more code in the call sites at the expense of less checking. I don't think the extra NULL check logic in cpuid_count() is that bad.

Overall the solution proposed in this patch is safer going forward, as it prevent issues like this from being introduced in the first place.

---
 tools/firmware/hvmloader/util.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/firmware/hvmloader/util.h +11 -0
index 644450c51ceb..765a013ddd9e 100644
@@ -190,6 +190,17 @@ static inline void cpuid_count(
     uint32_t *ecx,
     uint32_t *edx)
 {
+    uint32_t tmp;
+
+    if ( !eax )
+        eax = &tmp;
+    if ( !ebx )
+        ebx = &tmp;
+    if ( !ecx )
+        ecx = &tmp;
+    if ( !edx )
+        edx = &tmp;
+
     asm volatile ( "cpuid"
                    : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
                    : "a" (leaf), "c" (subleaf) );
Signature · 1 line
2.48.1
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Roger Pau Monne

The commit that added support for retrieving the APIC IDs from the APs introduced several usages of cpuid() with NULL parameters, which is not handled by the underlying implementation. For GCC I expect this results in writes to the physical address at 0, however for Clang the generated code in smp.o is:

Oh lovely.  I guess we need full VM testing for the Clang builds too.

Quoted 11 lines

tools/firmware/hvmloader/smp.o: file format elf32-i386

Disassembly of section .text:

00000000 <smp_initialise>:

       0: 55                            pushl   %ebp
       1: 89 e5                         movl    %esp, %ebp
       3: 53                            pushl   %ebx
       4: 31 c0                         xorl    %eax, %eax
       6: 31 c9                         xorl    %ecx, %ecx
       8: 0f a2                         cpuid

I get the hint that this is the whole file?  But you don't say that explicitly.

Quoted 28 lines

Showing the usage of a NULL pointer results in undefined behavior, and clang refusing to generate further code after it.

Fix by using a temporary variable in cpuid_count() in place for any NULL parameter.

Fixes 9ad0db58c7e2 ('tools/hvmloader: Retrieve APIC IDs from the APs themselves')

---
Could also be fixed by using the temporary variable in the call sites, however that's more code in the call sites at the expense of less checking. I don't think the extra NULL check logic in cpuid_count() is that bad.

Overall the solution proposed in this patch is safer going forward, as it prevent issues like this from being introduced in the first place.

---
 tools/firmware/hvmloader/util.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/firmware/hvmloader/util.h +1 -0
index 644450c51ceb..765a013ddd9e 100644
@@ -190,6 +190,17 @@ static inline void cpuid_count(
     uint32_t *ecx,
     uint32_t *edx)
 {
+    uint32_t tmp;

tools/firmware/hvmloader/util.h static inline void cpuid_count( · line 203

Quoted 10 lines
tools/firmware/hvmloader/util.h +10 -0
+
+    if ( !eax )
+        eax = &tmp;
+    if ( !ebx )
+        ebx = &tmp;
+    if ( !ecx )
+        ecx = &tmp;
+    if ( !edx )
+        edx = &tmp;
+

Personally I dislike this pattern, and some of that is definitely PTSD from Xen's original hvm_cpuid() function.

hvmloader is a small enough codebase that I don't think it matters either way.

clarity over the disassembly.

Quoted 3 lines
tools/firmware/hvmloader/util.h +0 -0
     asm volatile ( "cpuid"
                    : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
                    : "a" (leaf), "c" (subleaf) );
#2 lore raw
Quoted 21 lines · Andrew Cooper
Quoted 5 lines · Roger Pau Monne

The commit that added support for retrieving the APIC IDs from the APs introduced several usages of cpuid() with NULL parameters, which is not handled by the underlying implementation. For GCC I expect this results in writes to the physical address at 0, however for Clang the generated code in smp.o is:

Oh lovely.  I guess we need full VM testing for the Clang builds too.

Quoted 11 lines

tools/firmware/hvmloader/smp.o: file format elf32-i386

Disassembly of section .text:

00000000 <smp_initialise>:

       0: 55                            pushl   %ebp
       1: 89 e5                         movl    %esp, %ebp
       3: 53                            pushl   %ebx
       4: 31 c0                         xorl    %eax, %eax
       6: 31 c9                         xorl    %ecx, %ecx
       8: 0f a2                         cpuid
Quoted 2 lines

I get the hint that this is the whole file?  But you don't say that explicitly.

Yeah, realized later while reading the message myself. This is indeed the whole file.

Quoted 42 lines
Quoted 28 lines

Showing the usage of a NULL pointer results in undefined behavior, and clang refusing to generate further code after it.

Fix by using a temporary variable in cpuid_count() in place for any NULL parameter.

Fixes 9ad0db58c7e2 ('tools/hvmloader: Retrieve APIC IDs from the APs themselves')

---
Could also be fixed by using the temporary variable in the call sites, however that's more code in the call sites at the expense of less checking. I don't think the extra NULL check logic in cpuid_count() is that bad.

Overall the solution proposed in this patch is safer going forward, as it prevent issues like this from being introduced in the first place.

---
 tools/firmware/hvmloader/util.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/firmware/hvmloader/util.h +1 -0
index 644450c51ceb..765a013ddd9e 100644
@@ -190,6 +190,17 @@ static inline void cpuid_count(
     uint32_t *ecx,
     uint32_t *edx)
 {
+    uint32_t tmp;

tools/firmware/hvmloader/util.h static inline void cpuid_count( · line 203

Quoted 10 lines
tools/firmware/hvmloader/util.h +10 -0
+
+    if ( !eax )
+        eax = &tmp;
+    if ( !ebx )
+        ebx = &tmp;
+    if ( !ecx )
+        ecx = &tmp;
+    if ( !edx )
+        edx = &tmp;
+

Personally I dislike this pattern, and some of that is definitely PTSD from Xen's original hvm_cpuid() function.

Quoted 5 lines

hvmloader is a small enough codebase that I don't think it matters either way.

clarity over the disassembly.

I've adjusted to:

"""
For GCC I expect this results in writes to the physical address at 0, however for Clang the generated code in smp.o for the whole file is:

tools/firmware/hvmloader/smp.o: file format elf32-i386
...
"""

Thanks, Roger.

#3 lore raw
In reply to Roger Pau Monne
Quoted 18 lines · Roger Pau Monne

The commit that added support for retrieving the APIC IDs from the APs introduced several usages of cpuid() with NULL parameters, which is not handled by the underlying implementation. For GCC I expect this results in writes to the physical address at 0, however for Clang the generated code in smp.o is:

tools/firmware/hvmloader/smp.o: file format elf32-i386

Disassembly of section .text:

00000000 <smp_initialise>:

       0: 55                            pushl   %ebp
       1: 89 e5                         movl    %esp, %ebp
       3: 53                            pushl   %ebx
       4: 31 c0                         xorl    %eax, %eax
       6: 31 c9                         xorl    %ecx, %ecx
       8: 0f a2                         cpuid
Quoted 8 lines

Showing the usage of a NULL pointer results in undefined behavior, and clang refusing to generate further code after it.

Fix by using a temporary variable in cpuid_count() in place for any NULL parameter.

Fixes 9ad0db58c7e2 ('tools/hvmloader: Retrieve APIC IDs from the APs themselves')

Ugh, that's on me. I was sure I saw the pattern in Xen (from where the code came from), but clearly I hallucinated.

Quoted 7 lines

---
Could also be fixed by using the temporary variable in the call sites, however that's more code in the call sites at the expense of less checking. I don't think the extra NULL check logic in cpuid_count() is that bad.

Overall the solution proposed in this patch is safer going forward, as it prevent issues like this from being introduced in the first place.

Might be worth moving this same extra checks onto Xen's cpuid. There's no shortage of junk variables at the callsites.

Quoted 13 lines
---
 tools/firmware/hvmloader/util.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/firmware/hvmloader/util.h +1 -0
index 644450c51ceb..765a013ddd9e 100644
@@ -190,6 +190,17 @@ static inline void cpuid_count(
     uint32_t *ecx,
     uint32_t *edx)
 {
+    uint32_t tmp;

tools/firmware/hvmloader/util.h static inline void cpuid_count( · line 203

Quoted 10 lines
tools/firmware/hvmloader/util.h +10 -0
+
+    if ( !eax )
+        eax = &tmp;
+    if ( !ebx )
+        ebx = &tmp;
+    if ( !ecx )
+        ecx = &tmp;
+    if ( !edx )
+        edx = &tmp;
+

A somewhat more compact alternative that doesn't require tmp would be:

  eax = eax ?: &leaf;
  ebx = ebx ?: &leaf;
  ecx = ecx ?: &leaf;
  edx = edx ?: &leaf;

It clobbers leaf, but only after it's no longer relevant.

tools/firmware/hvmloader/util.h static inline void cpuid_count( · line 206

Quoted 3 lines
tools/firmware/hvmloader/util.h +0 -0
     asm volatile ( "cpuid"
                    : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
                    : "a" (leaf), "c" (subleaf) );

Cheers,
Alejandro

#4 lore raw
Quoted 68 lines · Alejandro Vallejo
Quoted 18 lines · Roger Pau Monne

The commit that added support for retrieving the APIC IDs from the APs introduced several usages of cpuid() with NULL parameters, which is not handled by the underlying implementation. For GCC I expect this results in writes to the physical address at 0, however for Clang the generated code in smp.o is:

tools/firmware/hvmloader/smp.o: file format elf32-i386

Disassembly of section .text:

00000000 <smp_initialise>:

       0: 55                            pushl   %ebp
       1: 89 e5                         movl    %esp, %ebp
       3: 53                            pushl   %ebx
       4: 31 c0                         xorl    %eax, %eax
       6: 31 c9                         xorl    %ecx, %ecx
       8: 0f a2                         cpuid
Quoted 8 lines

Showing the usage of a NULL pointer results in undefined behavior, and clang refusing to generate further code after it.

Fix by using a temporary variable in cpuid_count() in place for any NULL parameter.

Fixes 9ad0db58c7e2 ('tools/hvmloader: Retrieve APIC IDs from the APs themselves')

Ugh, that's on me. I was sure I saw the pattern in Xen (from where the code came from), but clearly I hallucinated.

Quoted 7 lines

---
Could also be fixed by using the temporary variable in the call sites, however that's more code in the call sites at the expense of less checking. I don't think the extra NULL check logic in cpuid_count() is that bad.

Overall the solution proposed in this patch is safer going forward, as it prevent issues like this from being introduced in the first place.

Might be worth moving this same extra checks onto Xen's cpuid. There's no shortage of junk variables at the callsites.

Quoted 13 lines
---
 tools/firmware/hvmloader/util.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/firmware/hvmloader/util.h +1 -0
index 644450c51ceb..765a013ddd9e 100644
@@ -190,6 +190,17 @@ static inline void cpuid_count(
     uint32_t *ecx,
     uint32_t *edx)
 {
+    uint32_t tmp;

tools/firmware/hvmloader/util.h static inline void cpuid_count( · line 203

Quoted 10 lines
tools/firmware/hvmloader/util.h +10 -0
+
+    if ( !eax )
+        eax = &tmp;
+    if ( !ebx )
+        ebx = &tmp;
+    if ( !ecx )
+        ecx = &tmp;
+    if ( !edx )
+        edx = &tmp;
+

A somewhat more compact alternative that doesn't require tmp would be:

Quoted 4 lines
  eax = eax ?: &leaf;
  ebx = ebx ?: &leaf;
  ecx = ecx ?: &leaf;
  edx = edx ?: &leaf;

But that performs the write unconditionally? It might be more compact code-wise, but might incur in an unneeded store?

Quoted 1 line

It clobbers leaf, but only after it's no longer relevant.

My preference was to use a explicitly tmp variable, but I could switch to using leaf if that's the preference. Given that Andrew has already Acked the current version I'm tempted to just go with what has already been Acked.

Thanks, Roger.

#5 lore raw
Quoted 69 lines · Roger Pau Monné
Quoted 68 lines · Alejandro Vallejo
Quoted 18 lines · Roger Pau Monne

The commit that added support for retrieving the APIC IDs from the APs introduced several usages of cpuid() with NULL parameters, which is not handled by the underlying implementation. For GCC I expect this results in writes to the physical address at 0, however for Clang the generated code in smp.o is:

tools/firmware/hvmloader/smp.o: file format elf32-i386

Disassembly of section .text:

00000000 <smp_initialise>:

       0: 55                            pushl   %ebp
       1: 89 e5                         movl    %esp, %ebp
       3: 53                            pushl   %ebx
       4: 31 c0                         xorl    %eax, %eax
       6: 31 c9                         xorl    %ecx, %ecx
       8: 0f a2                         cpuid
Quoted 8 lines

Showing the usage of a NULL pointer results in undefined behavior, and clang refusing to generate further code after it.

Fix by using a temporary variable in cpuid_count() in place for any NULL parameter.

Fixes 9ad0db58c7e2 ('tools/hvmloader: Retrieve APIC IDs from the APs themselves')

Ugh, that's on me. I was sure I saw the pattern in Xen (from where the code came from), but clearly I hallucinated.

Quoted 7 lines

---
Could also be fixed by using the temporary variable in the call sites, however that's more code in the call sites at the expense of less checking. I don't think the extra NULL check logic in cpuid_count() is that bad.

Overall the solution proposed in this patch is safer going forward, as it prevent issues like this from being introduced in the first place.

Might be worth moving this same extra checks onto Xen's cpuid. There's no shortage of junk variables at the callsites.

Quoted 13 lines
---
 tools/firmware/hvmloader/util.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/firmware/hvmloader/util.h +1 -0
index 644450c51ceb..765a013ddd9e 100644
@@ -190,6 +190,17 @@ static inline void cpuid_count(
     uint32_t *ecx,
     uint32_t *edx)
 {
+    uint32_t tmp;

tools/firmware/hvmloader/util.h static inline void cpuid_count( · line 203

Quoted 10 lines
tools/firmware/hvmloader/util.h +10 -0
+
+    if ( !eax )
+        eax = &tmp;
+    if ( !ebx )
+        ebx = &tmp;
+    if ( !ecx )
+        ecx = &tmp;
+    if ( !edx )
+        edx = &tmp;
+

A somewhat more compact alternative that doesn't require tmp would be:

Quoted 7 lines
Quoted 4 lines
  eax = eax ?: &leaf;
  ebx = ebx ?: &leaf;
  ecx = ecx ?: &leaf;
  edx = edx ?: &leaf;

But that performs the write unconditionally? It might be more compact code-wise, but might incur in an unneeded store?

Pretty sure it would all optimise to very similar, if not identical code.

Quoted 7 lines
Quoted 2 lines

It clobbers leaf, but only after it's no longer relevant.

My preference was to use a explicitly tmp variable, but I could switch to using leaf if that's the preference. Given that Andrew has already Acked the current version I'm tempted to just go with what has already been Acked.

That's perfectly fine. It was merely a cosmetic nit. LGTM as it is too.

Quoted 2 lines

Thanks, Roger.

Cheers,
Alejandro