Skip to contentSkip to conversation

x86/vhpet: Fix sanitization of legacy IRQ route

PATCH Superseded Tu Dinh 2 participants 5 messages Nov 24, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tu Dinh

author
#0 lore raw

PATCH 1/1 x86/vhpet: Fix sanitization of legacy IRQ route

When setting a timer's config register, timer_sanitize_int_route will always reset the IRQ route value to what's valid corresponding to the !HPET_CFG_LEGACY case. This is applied even if the HPET is set to HPET_CFG_LEGACY.

When some operating systems (e.g. Windows) try to write to a timer config, they will verify and rewrite the register if the values don't match what they expect. This causes an unnecessary write to HPET_Tn_CFG.

Note, the HPET specification states that for the Tn_INT_ROUTE_CNF field:

"If the value is not supported by this prarticular timer, then the value read back will not match what is written. [...] If the LegacyReplacement Route bit is set, then Timers 0 and 1 will have a different routing, and this bit field has no effect for those two timers."

Therefore, Xen should not reset timer_int_route if legacy mode is enabled, regardless of what's in there.

---
 xen/arch/x86/hvm/hpet.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hpet.c +8 -3
index f0e5f877f4..fb2f4f94aa 100644
@@ -48,6 +48,8 @@
 #define timer_is_32bit(h, n)     (timer_config(h, n) & HPET_TN_32BIT)
 #define hpet_enabled(h)          ((h)->hpet.config & HPET_CFG_ENABLE)
 #define timer_level(h, n)        (timer_config(h, n) & HPET_TN_LEVEL)
+#define timer_is_legacy(h, n) \
+    (((n) <= 1) && ((h)->hpet.config & HPET_CFG_LEGACY))
 
 #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n), HPET_TN_ROUTE)
 
@@ -244,7 +246,7 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
          (timer_level(h, tn) && test_bit(tn, &h->hpet.isr)) )
         return;
 
-    if ( !timer_int_route_valid(h, tn) )
+    if ( !timer_is_legacy(h, tn) && !timer_int_route_valid(h, tn) )
     {
         ASSERT_UNREACHABLE();
         return;
@@ -275,7 +277,7 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
             ? (uint32_t)diff : 0;
 
     destroy_periodic_time(&h->pt[tn]);
-    if ( (tn <= 1) && (h->hpet.config & HPET_CFG_LEGACY) )
+    if ( timer_is_legacy(h, tn) )
     {
         /* if LegacyReplacementRoute bit is set, HPET specification requires
            timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
@@ -323,7 +325,7 @@ static inline uint64_t hpet_fixup_reg(
 
 static void timer_sanitize_int_route(HPETState *h, unsigned int tn)
 {
-    if ( timer_int_route_valid(h, tn) )
+    if ( timer_is_legacy(h, tn) || timer_int_route_valid(h, tn) )
         return;
 
     timer_config(h, tn) &= ~HPET_TN_ROUTE;
@@ -379,6 +381,9 @@ static int cf_check hpet_write(
         h->hpet.config = hpet_fixup_reg(new_val, old_val,
                                         HPET_CFG_ENABLE | HPET_CFG_LEGACY);
 
+        for ( i = 0; i < HPET_TIMER_NUM; i++ )
+            timer_sanitize_int_route(h, i);
+
         if ( !(old_val & HPET_CFG_ENABLE) && (new_val & HPET_CFG_ENABLE) )
         {
             /* Enable main counter and interrupt generation. */
Signature · 10 lines
2.43.0



--
Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw
Quoted 11 lines · Tu Dinh

When setting a timer's config register, timer_sanitize_int_route will always reset the IRQ route value to what's valid corresponding to the !HPET_CFG_LEGACY case. This is applied even if the HPET is set to HPET_CFG_LEGACY.

When some operating systems (e.g. Windows) try to write to a timer config, they will verify and rewrite the register if the values don't match what they expect. This causes an unnecessary write to HPET_Tn_CFG.

Note, the HPET specification states that for the Tn_INT_ROUTE_CNF field:

Quoted 7 lines

"If the value is not supported by this prarticular timer, then the value read back will not match what is written. [...] If the LegacyReplacement Route bit is set, then Timers 0 and 1 will have a different routing, and this bit field has no effect for those two timers."

Therefore, Xen should not reset timer_int_route if legacy mode is enabled, regardless of what's in there.

Fixes ec40d3fe2147 ("x86/vhpet: check that the set interrupt route is valid")

(I think)

Quoted 17 lines
---
 xen/arch/x86/hvm/hpet.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hpet.c +2 -0
@@ -48,6 +48,8 @@
 #define timer_is_32bit(h, n)     (timer_config(h, n) & HPET_TN_32BIT)
 #define hpet_enabled(h)          ((h)->hpet.config & HPET_CFG_ENABLE)
 #define timer_level(h, n)        (timer_config(h, n) & HPET_TN_LEVEL)
+#define timer_is_legacy(h, n) \
+    (((n) <= 1) && ((h)->hpet.config & HPET_CFG_LEGACY))
 
 #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n), HPET_TN_ROUTE)
 

xen/arch/x86/hvm/hpet.c static void hpet_set_timer(HPETState *h, unsigned int tn, · line 249

Quoted 5 lines
xen/arch/x86/hvm/hpet.c +1 -1
@@ -244,7 +246,7 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
          (timer_level(h, tn) && test_bit(tn, &h->hpet.isr)) )
         return;
 
-    if ( !timer_int_route_valid(h, tn) )
+    if ( !timer_is_legacy(h, tn) && !timer_int_route_valid(h, tn) )

Seeing this and the other use together with timer_int_route_valid(), wouldn't timer_int_route_valid() better itself invoke the new macro?

xen/arch/x86/hvm/hpet.c static int cf_check hpet_write( · line 385

Quoted 6 lines
xen/arch/x86/hvm/hpet.c +2 -0
@@ -379,6 +381,9 @@ static int cf_check hpet_write(
         h->hpet.config = hpet_fixup_reg(new_val, old_val,
                                         HPET_CFG_ENABLE | HPET_CFG_LEGACY);
 
+        for ( i = 0; i < HPET_TIMER_NUM; i++ )
+            timer_sanitize_int_route(h, i);

Strictly speaking this is needed only when HPET_CFG_LEGACY went from 1 to 0. Plus it's needed only on the first 2 channels, as it's only there where timer_sanitize_int_route() changes behavior. I'm not going to insist to limit it like this, but if you don't, then I'd like to ask for a comment here clarifying that excess work is done for simplicity's sake.

Jan

author
#2 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Tu Dinh

When setting a timer's config register, timer_sanitize_int_route will always reset the IRQ route value to what's valid corresponding to the !HPET_CFG_LEGACY case. This is applied even if the HPET is set to HPET_CFG_LEGACY.

When some operating systems (e.g. Windows) try to write to a timer config, they will verify and rewrite the register if the values don't match what they expect. This causes an unnecessary write to HPET_Tn_CFG.

Note, the HPET specification states that for the Tn_INT_ROUTE_CNF field:

"If the value is not supported by this prarticular timer, then the value read back will not match what is written. [...] If the LegacyReplacement Route bit is set, then Timers 0 and 1 will have a different routing, and this bit field has no effect for those two timers."

Quoted 5 lines
Quoted 2 lines

Therefore, Xen should not reset timer_int_route if legacy mode is enabled, regardless of what's in there.

Fixes ec40d3fe2147 ("x86/vhpet: check that the set interrupt route is valid")

(I think)

Yes, thanks.

Quoted 21 lines
Quoted 21 lines
---
  xen/arch/x86/hvm/hpet.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hpet.c +2 -0
@@ -48,6 +48,8 @@
  #define timer_is_32bit(h, n)     (timer_config(h, n) & HPET_TN_32BIT)
  #define hpet_enabled(h)          ((h)->hpet.config & HPET_CFG_ENABLE)
  #define timer_level(h, n)        (timer_config(h, n) & HPET_TN_LEVEL)
+#define timer_is_legacy(h, n) \
+    (((n) <= 1) && ((h)->hpet.config & HPET_CFG_LEGACY))
 
  #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n), HPET_TN_ROUTE)
 
@@ -244,7 +246,7 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
           (timer_level(h, tn) && test_bit(tn, &h->hpet.isr)) )
          return;
 
Quoted 5 lines

xen/arch/x86/hvm/hpet.c static void hpet_set_timer(HPETState *h, unsigned int tn, · line 249

Quoted 2 lines
xen/arch/x86/hvm/hpet.c +1 -1
-    if ( !timer_int_route_valid(h, tn) )
+    if ( !timer_is_legacy(h, tn) && !timer_int_route_valid(h, tn) )

Seeing this and the other use together with timer_int_route_valid(), wouldn't timer_int_route_valid() better itself invoke the new macro?

I thought about it, but I found that it was overloading the definition of timer_int_route_valid a little too much. Since timer_is_legacy() is being used by itself later anyway, I figured it'd be better to just separate the two.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/hvm/hpet.c +0 -0
@@ -379,6 +381,9 @@ static int cf_check hpet_write(
          h->hpet.config = hpet_fixup_reg(new_val, old_val,
                                          HPET_CFG_ENABLE | HPET_CFG_LEGACY);
 
Quoted 9 lines

xen/arch/x86/hvm/hpet.c static int cf_check hpet_write( · line 385

Quoted 2 lines
xen/arch/x86/hvm/hpet.c +2 -0
+        for ( i = 0; i < HPET_TIMER_NUM; i++ )
+            timer_sanitize_int_route(h, i);

Strictly speaking this is needed only when HPET_CFG_LEGACY went from 1 to 0. Plus it's needed only on the first 2 channels, as it's only there where timer_sanitize_int_route() changes behavior. I'm not going to insist to limit it like this, but if you don't, then I'd like to ask for a comment here clarifying that excess work is done for simplicity's sake.

Agreed, I can limit it to i <= 1.

I'll prepare a v2.

Quoted 3 lines

Jan

--
Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#3 lore raw
Quoted 11 lines · Tu Dinh
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Tu Dinh

When setting a timer's config register, timer_sanitize_int_route will always reset the IRQ route value to what's valid corresponding to the !HPET_CFG_LEGACY case. This is applied even if the HPET is set to HPET_CFG_LEGACY.

When some operating systems (e.g. Windows) try to write to a timer config, they will verify and rewrite the register if the values don't match what they expect. This causes an unnecessary write to HPET_Tn_CFG.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Note, the HPET specification states that for the Tn_INT_ROUTE_CNF field:

"If the value is not supported by this prarticular timer, then the value read back will not match what is written. [...] If the LegacyReplacement Route bit is set, then Timers 0 and 1 will have a different routing, and this bit field has no effect for those two timers."

According to this, ...

Quoted 35 lines
Quoted 5 lines
Quoted 2 lines

Therefore, Xen should not reset timer_int_route if legacy mode is enabled, regardless of what's in there.

Fixes ec40d3fe2147 ("x86/vhpet: check that the set interrupt route is valid")

(I think)

Yes, thanks.

Quoted 21 lines
Quoted 21 lines
---
  xen/arch/x86/hvm/hpet.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hpet.c +2 -0
@@ -48,6 +48,8 @@
  #define timer_is_32bit(h, n)     (timer_config(h, n) & HPET_TN_32BIT)
  #define hpet_enabled(h)          ((h)->hpet.config & HPET_CFG_ENABLE)
  #define timer_level(h, n)        (timer_config(h, n) & HPET_TN_LEVEL)
+#define timer_is_legacy(h, n) \
+    (((n) <= 1) && ((h)->hpet.config & HPET_CFG_LEGACY))
 
  #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n), HPET_TN_ROUTE)
 
@@ -244,7 +246,7 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
           (timer_level(h, tn) && test_bit(tn, &h->hpet.isr)) )
          return;
 
Quoted 5 lines

xen/arch/x86/hvm/hpet.c static void hpet_set_timer(HPETState *h, unsigned int tn, · line 249

Quoted 2 lines
xen/arch/x86/hvm/hpet.c +1 -1
-    if ( !timer_int_route_valid(h, tn) )
+    if ( !timer_is_legacy(h, tn) && !timer_int_route_valid(h, tn) )

Seeing this and the other use together with timer_int_route_valid(), wouldn't timer_int_route_valid() better itself invoke the new macro?

Quoted 4 lines

I thought about it, but I found that it was overloading the definition of timer_int_route_valid a little too much. Since timer_is_legacy() is being used by itself later anyway, I figured it'd be better to just separate the two.

... the route setting is valid (because of being ignored) when in legacy mode. Hence why I think the check wants integrating there.

Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/hpet.c +0 -0
@@ -379,6 +381,9 @@ static int cf_check hpet_write(
          h->hpet.config = hpet_fixup_reg(new_val, old_val,
                                          HPET_CFG_ENABLE | HPET_CFG_LEGACY);
 
Quoted 9 lines

xen/arch/x86/hvm/hpet.c static int cf_check hpet_write( · line 385

Quoted 2 lines
xen/arch/x86/hvm/hpet.c +2 -0
+        for ( i = 0; i < HPET_TIMER_NUM; i++ )
+            timer_sanitize_int_route(h, i);

Strictly speaking this is needed only when HPET_CFG_LEGACY went from 1 to 0. Plus it's needed only on the first 2 channels, as it's only there where timer_sanitize_int_route() changes behavior. I'm not going to insist to limit it like this, but if you don't, then I'd like to ask for a comment here clarifying that excess work is done for simplicity's sake.

Quoted 1 line

Agreed, I can limit it to i <= 1.

May I ask that you avoid such open-coding and use timer_is_legacy(h, i) as the loop continuation expression instead?

Jan

author
#4 lore raw
Quoted 56 lines · Jan Beulich
Quoted 11 lines · Tu Dinh
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Tu Dinh

When setting a timer's config register, timer_sanitize_int_route will always reset the IRQ route value to what's valid corresponding to the !HPET_CFG_LEGACY case. This is applied even if the HPET is set to HPET_CFG_LEGACY.

When some operating systems (e.g. Windows) try to write to a timer config, they will verify and rewrite the register if the values don't match what they expect. This causes an unnecessary write to HPET_Tn_CFG.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Note, the HPET specification states that for the Tn_INT_ROUTE_CNF field:

"If the value is not supported by this prarticular timer, then the value read back will not match what is written. [...] If the LegacyReplacement Route bit is set, then Timers 0 and 1 will have a different routing, and this bit field has no effect for those two timers."

According to this, ...

Quoted 35 lines
Quoted 5 lines
Quoted 2 lines

Therefore, Xen should not reset timer_int_route if legacy mode is enabled, regardless of what's in there.

Fixes ec40d3fe2147 ("x86/vhpet: check that the set interrupt route is valid")

(I think)

Yes, thanks.

Quoted 26 lines
Quoted 18 lines
---
   xen/arch/x86/hvm/hpet.c | 11 ++++++++---
   1 file changed, 8 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hpet.c +2 -0
@@ -48,6 +48,8 @@
   #define timer_is_32bit(h, n)     (timer_config(h, n) & HPET_TN_32BIT)
   #define hpet_enabled(h)          ((h)->hpet.config & HPET_CFG_ENABLE)
   #define timer_level(h, n)        (timer_config(h, n) & HPET_TN_LEVEL)
+#define timer_is_legacy(h, n) \
+    (((n) <= 1) && ((h)->hpet.config & HPET_CFG_LEGACY))
 
   #define timer_int_route(h, n)    MASK_EXTR(timer_config(h, n), HPET_TN_ROUTE)
 

xen/arch/x86/hvm/hpet.c static void hpet_set_timer(HPETState *h, unsigned int tn, · line 249

Quoted 5 lines
xen/arch/x86/hvm/hpet.c +1 -1
@@ -244,7 +246,7 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
            (timer_level(h, tn) && test_bit(tn, &h->hpet.isr)) )
           return;
 
-    if ( !timer_int_route_valid(h, tn) )
+    if ( !timer_is_legacy(h, tn) && !timer_int_route_valid(h, tn) )

Seeing this and the other use together with timer_int_route_valid(), wouldn't timer_int_route_valid() better itself invoke the new macro?

Quoted 7 lines
Quoted 4 lines

I thought about it, but I found that it was overloading the definition of timer_int_route_valid a little too much. Since timer_is_legacy() is being used by itself later anyway, I figured it'd be better to just separate the two.

... the route setting is valid (because of being ignored) when in legacy mode. Hence why I think the check wants integrating there.

Okay, I can offload the check there.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

xen/arch/x86/hvm/hpet.c static int cf_check hpet_write( · line 385

Quoted 7 lines
xen/arch/x86/hvm/hpet.c +2 -0
@@ -379,6 +381,9 @@ static int cf_check hpet_write(
           h->hpet.config = hpet_fixup_reg(new_val, old_val,
                                           HPET_CFG_ENABLE | HPET_CFG_LEGACY);
 
+        for ( i = 0; i < HPET_TIMER_NUM; i++ )
+            timer_sanitize_int_route(h, i);

Strictly speaking this is needed only when HPET_CFG_LEGACY went from 1 to 0. Plus it's needed only on the first 2 channels, as it's only there where timer_sanitize_int_route() changes behavior. I'm not going to insist to limit it like this, but if you don't, then I'd like to ask for a comment here clarifying that excess work is done for simplicity's sake.

Quoted 4 lines
Quoted 1 line

Agreed, I can limit it to i <= 1.

May I ask that you avoid such open-coding and use timer_is_legacy(h, i) as the loop continuation expression instead?

That wouldn't work as timer_is_legacy would check for hpet.config & HPET_CFG_LEGACY, whereas we want to sanitize in the opposite case, i.e. !(hpet.config & HPET_CFG_LEGACY).

It's probably better to use i < HPET_TIMER_NUM and add a comment as you suggested.

Quoted 2 lines

Jan

--
Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech