Skip to contentSkip to conversation

x86/irq: introduce APIC_VECTOR_VALID()

PATCH In review [email protected] 4 participants 8 messages Mar 20, 2025 – Mar 31, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/irq: introduce APIC_VECTOR_VALID()

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

---
Changes v1->v2:
- update APIC_VECTOR_VALID to a macro w/ parameter
- CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1727330658
- Link to v1: https://lore.kernel.org/xen-devel/[email protected]/

---
 xen/arch/x86/cpu/mcheck/mce_intel.c |  3 +--
 xen/arch/x86/hvm/vlapic.c           | 10 +++++-----
 xen/arch/x86/include/asm/apicdef.h  |  1 +
 3 files changed, 7 insertions(+), 7 deletions(-)
xen/arch/x86/cpu/mcheck/mce_intel.c +1 -2
index 07b50f8793..1e52b1ac25 100644
@@ -135,8 +135,7 @@ static void intel_init_thermal(struct cpuinfo_x86 *c)
      * BIOS has programmed on AP based on BSP's info we saved (since BIOS
      * is required to set the same value for all threads/cores).
      */
-    if ( (val & APIC_DM_MASK) != APIC_DM_FIXED
-         || (val & APIC_VECTOR_MASK) > 0xf )
+    if ( (val & APIC_DM_MASK) != APIC_DM_FIXED || APIC_VECTOR_VALID(val) )
         apic_write(APIC_LVTTHMR, val);
 
     if ( (msr_content & (1ULL<<3))
xen/arch/x86/hvm/vlapic.c +5 -5
index 065b2aab5b..993e972cd7 100644
@@ -123,7 +123,7 @@ static void vlapic_error(struct vlapic *vlapic, unsigned int err_bit)
              * will end up back here.  Break the cycle by only injecting LVTERR
              * if it will succeed, and folding in RECVILL otherwise.
              */
-            if ( (lvterr & APIC_VECTOR_MASK) >= 16 )
+            if ( APIC_VECTOR_VALID(lvterr) )
                 inj = true;
             else
                 set_bit(ilog2(APIC_ESR_RECVILL), &vlapic->hw.pending_esr);
@@ -136,7 +136,7 @@ static void vlapic_error(struct vlapic *vlapic, unsigned int err_bit)
 
 bool vlapic_test_irq(const struct vlapic *vlapic, uint8_t vec)
 {
-    if ( unlikely(vec < 16) )
+    if ( unlikely(!APIC_VECTOR_VALID(vec)) )
         return false;
 
     if ( hvm_funcs.test_pir &&
@@ -150,7 +150,7 @@ void vlapic_set_irq(struct vlapic *vlapic, uint8_t vec, uint8_t trig)
 {
     struct vcpu *target = vlapic_vcpu(vlapic);
 
-    if ( unlikely(vec < 16) )
+    if ( unlikely(!APIC_VECTOR_VALID(vec)) )
     {
         vlapic_error(vlapic, ilog2(APIC_ESR_RECVILL));
         return;
@@ -523,7 +523,7 @@ void vlapic_ipi(
         struct vlapic *target = vlapic_lowest_prio(
             vlapic_domain(vlapic), vlapic, short_hand, dest, dest_mode);
 
-        if ( unlikely((icr_low & APIC_VECTOR_MASK) < 16) )
+        if ( unlikely(!APIC_VECTOR_VALID(icr_low)) )
             vlapic_error(vlapic, ilog2(APIC_ESR_SENDILL));
         else if ( target )
             vlapic_accept_irq(vlapic_vcpu(target), icr_low);
@@ -531,7 +531,7 @@ void vlapic_ipi(
     }
 
     case APIC_DM_FIXED:
-        if ( unlikely((icr_low & APIC_VECTOR_MASK) < 16) )
+        if ( unlikely(!APIC_VECTOR_VALID(icr_low)) )
         {
             vlapic_error(vlapic, ilog2(APIC_ESR_SENDILL));
             break;
xen/arch/x86/include/asm/apicdef.h +1 -0
index 49e29ec801..7b431190d2 100644
@@ -78,6 +78,7 @@
 #define			APIC_DM_STARTUP		0x00600
 #define			APIC_DM_EXTINT		0x00700
 #define			APIC_VECTOR_MASK	0x000FF
+#define			APIC_VECTOR_VALID(x)	(((x) & APIC_VECTOR_MASK) >= 16)
 #define		APIC_ICR2	0x310
 #define			GET_xAPIC_DEST_FIELD(x)	(((x)>>24)&0xFF)
 #define			SET_xAPIC_DEST_FIELD(x)	((x)<<24)
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · On 21.03.2025 00:05, [email protected] wrote:

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

with ...

xen/arch/x86/include/asm/apicdef.h line 81

Quoted 7 lines
xen/arch/x86/include/asm/apicdef.h +1 -0
@@ -78,6 +78,7 @@
 #define			APIC_DM_STARTUP		0x00600
 #define			APIC_DM_EXTINT		0x00700
 #define			APIC_VECTOR_MASK	0x000FF
+#define			APIC_VECTOR_VALID(x)	(((x) & APIC_VECTOR_MASK) >= 16)

... line length restrictions respected here. I'll see about taking care of this while committing, provided other x86 maintainers wouldn't prefer this to not go in in the first place (so I'll also give it another day or two).

Jan

#2 lore raw
Quoted 14 lines · Jan Beulich
Quoted 10 lines · On 21.03.2025 00:05, [email protected] wrote:

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

with ...

Quoted 10 lines

xen/arch/x86/include/asm/apicdef.h line 81

Quoted 7 lines
xen/arch/x86/include/asm/apicdef.h +1 -0
@@ -78,6 +78,7 @@
 #define			APIC_DM_STARTUP		0x00600
 #define			APIC_DM_EXTINT		0x00700
 #define			APIC_VECTOR_MASK	0x000FF
+#define			APIC_VECTOR_VALID(x)	(((x) & APIC_VECTOR_MASK) >= 16)

... line length restrictions respected here. I'll see about taking care of this while committing, provided other x86 maintainers wouldn't prefer this to not go in in the first place (so I'll also give it another day or two).

I'm ok with this change.  Unlike v1, it's meaningful in context.

~Andrew

#3 lore raw
In reply to Jan Beulich
Quoted 20 lines · Jan Beulich
Quoted 10 lines · On 21.03.2025 00:05, [email protected] wrote:

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

with ...

Quoted 12 lines

xen/arch/x86/include/asm/apicdef.h line 81

Quoted 7 lines
xen/arch/x86/include/asm/apicdef.h +1 -0
@@ -78,6 +78,7 @@
 #define APIC_DM_STARTUP 0x00600
 #define APIC_DM_EXTINT 0x00700
 #define APIC_VECTOR_MASK 0x000FF
+#define APIC_VECTOR_VALID(x) (((x) & APIC_VECTOR_MASK) >= 16)

... line length restrictions respected here. I'll see about taking care of this while committing, provided other x86 maintainers wouldn't prefer this to not go in in the first place (so I'll also give it another day or two).

Thanks!

Quoted 2 lines

Jan

#4 lore raw
In reply to [email protected]
Quoted 10 lines · On 20/03/2025 11:05 pm, [email protected] wrote:

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

Would it be possible to adjust your git configuration so committer is set to Denis Mukhin <[email protected]> ?

Right now, it takes manual fixup to prevent your commits going in as authored by [email protected], and one already slipped through.
https://xenbits.xen.org/gitweb/?p=xen.git&a=search&h=refs%2Fheads%2Fstaging&st=author&s=dmkhn%40proton.me

Thanks,

~Andrew

#5 lore raw
Quoted 16 lines · Andrew Cooper
Quoted 10 lines · On 20/03/2025 11:05 pm, [email protected] wrote:

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

Quoted 7 lines

Would it be possible to adjust your git configuration so committer is set to Denis Mukhin [email protected] ?

Right now, it takes manual fixup to prevent your commits going in as authored by [email protected], and one already slipped through.
https://xenbits.xen.org/gitweb/?p=xen.git&a=search&h=refs%2Fheads%2Fstaging&st=author&s=dmkhn%40proton.me

I will fix my environment, sorry for inconvenience. Thanks!

Quoted 4 lines

Thanks,

~Andrew

#6 lore raw
Quoted 22 lines · Denis Mukhin
Quoted 20 lines · Andrew Cooper
Quoted 10 lines · On 20/03/2025 11:05 pm, [email protected] wrote:

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

Would it be possible to adjust your git configuration so committer is set to Denis Mukhin [email protected] ?

Quoted 6 lines
Quoted 3 lines

Right now, it takes manual fixup to prevent your commits going in as authored by [email protected], and one already slipped through.
https://xenbits.xen.org/gitweb/?p=xen.git&a=search&h=refs%2Fheads%2Fstaging&st=author&s=dmkhn%40proton.me

I will fix my environment, sorry for inconvenience. Thanks!

Hi Denis,

FYI it is suffiecient to add:

From: Denis Mukhin <[email protected]>

as part of the body of the email (first line), git will automatically pick it up for the Author field. You don't necessarily need to change your email setup.

#7 lore raw
Quoted 32 lines · Stefano Stabellini
Quoted 18 lines · Denis Mukhin
Quoted 16 lines · On Monday, March 24th, 2025 at 6:51 AM, Andrew Cooper…
Quoted 10 lines · On 20/03/2025 11:05 pm, [email protected] wrote:

Add new macro APIC_VECTOR_VALID() to validate the interrupt vector range as per [1]. This macro replaces hardcoded checks against the open-coded value 16 in LAPIC and virtual LAPIC code and simplifies the code a bit.

[1] Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"

Would it be possible to adjust your git configuration so committer is set to Denis Mukhin [email protected] ?

Quoted 6 lines
Quoted 3 lines

Right now, it takes manual fixup to prevent your commits going in as authored by [email protected], and one already slipped through.
https://xenbits.xen.org/gitweb/?p=xen.git&a=search&h=refs%2Fheads%2Fstaging&st=author&s=dmkhn%40proton.me

I will fix my environment, sorry for inconvenience. Thanks!

Hi Denis,

Quoted 8 lines

FYI it is suffiecient to add:

From: Denis Mukhin [email protected]

as part of the body of the email (first line), git will automatically pick it up for the Author field. You don't necessarily need to change your email setup.

Thanks!