Skip to contentSkip to conversation

x86/HVM: correct partial HPET_STATUS write emulation

PATCH In review Jan Beulich 2 participants 3 messages Aug 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/HVM: correct partial HPET_STATUS write emulation

For partial writes the non-written parts of registers are folded into the full 64-bit value from what they're presently set to. That's wrong to do though when the behavior is write-1-to-clear: Writes not including to low 3 bits would unconditionally clear all ISR bits which are presently set. Re-calculate the value to use.

Fixes be07023be115 ("x86/vhpet: add support for level triggered interrupts")

---
Alternatively we could also suppress the folding in of read bits, but that looked to me to end up in a more intrusive change.

xen/arch/x86/hvm/hpet.c +2 -1
@@ -404,7 +404,8 @@ static int cf_check hpet_write(
         break;
 
     case HPET_STATUS:
-        /* write 1 to clear. */
+        /* Write 1 to clear. Therefore don't use new_val directly here. */
+        new_val = val << ((addr & 7) * 8);
         while ( new_val )
         {
             bool active;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Jan Beulich

For partial writes the non-written parts of registers are folded into the full 64-bit value from what they're presently set to. That's wrong to do though when the behavior is write-1-to-clear: Writes not including to low 3 bits would unconditionally clear all ISR bits which are presently set. Re-calculate the value to use.

Fixes be07023be115 ("x86/vhpet: add support for level triggered interrupts")

---
Alternatively we could also suppress the folding in of read bits, but that looked to me to end up in a more intrusive change.

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

Quoted 9 lines
xen/arch/x86/hvm/hpet.c +2 -1
@@ -404,7 +404,8 @@ static int cf_check hpet_write(
         break;
 
     case HPET_STATUS:
-        /* write 1 to clear. */
+        /* Write 1 to clear. Therefore don't use new_val directly here. */
+        new_val = val << ((addr & 7) * 8);
         while ( new_val )
         {
             bool active;

It's sad that we allow any sub-word accesses.  The spec makes it pretty clear that only aligned 32-bit and 64-bit accesses are acceptable, and it would simplify the merging logic substantially.

Nevertheless, this is the simplest bugfix for now.

#2 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich

For partial writes the non-written parts of registers are folded into the full 64-bit value from what they're presently set to. That's wrong to do though when the behavior is write-1-to-clear: Writes not including to low 3 bits would unconditionally clear all ISR bits which are presently set. Re-calculate the value to use.

Fixes be07023be115 ("x86/vhpet: add support for level triggered interrupts")

---
Alternatively we could also suppress the folding in of read bits, but that looked to me to end up in a more intrusive change.

xen/arch/x86/hvm/hpet.c +0 -0
@@ -404,7 +404,8 @@ static int cf_check hpet_write(
         break;
 
Quoted 11 lines

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

Quoted 7 lines
xen/arch/x86/hvm/hpet.c +2 -1
     case HPET_STATUS:
-        /* write 1 to clear. */
+        /* Write 1 to clear. Therefore don't use new_val directly here. */
+        new_val = val << ((addr & 7) * 8);
         while ( new_val )
         {
             bool active;

It's sad that we allow any sub-word accesses.  The spec makes it pretty clear that only aligned 32-bit and 64-bit accesses are acceptable, and it would simplify the merging logic substantially.

While that's true, the bug here would still have been there if we limited emulation to 32- and 64-bit accesses.

Quoted 3 lines

Nevertheless, this is the simplest bugfix for now.

Thanks.

Jan