vhpet: check that the set interrupt route is valid
PATCH 1/1 vhpet: check that the set interrupt route is valid
The value written by the guest must be valid according to the mask provided in the interrupt routing capabilities register. If the interrupt is not valid set it to the first valid IRQ in the capabilities field if the timer is enabled, else just clear the field.
Also refuse to start any timer that has an invalid interrupt route.
---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
--- xen/arch/x86/hvm/hpet.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
xen/arch/x86/hvm/hpet.c +36 -0
@@ -73,6 +73,9 @@ ((timer_config (h, n) &HPET_TN_INT_ROUTE_CAP_MASK ) \ >> HPET_TN_INT_ROUTE_CAP_SHIFT) +#define timer_int_valid (h, n) \ + ((1u << timer_int_route(h, n)) & timer_int_route_cap(h, n)) +static inline uint64_t hpet_read_maincounter (HPETState *h,uint64_t guest_time) {ASSERT (rw_is_locked (&h->lock));
@@ -244,6 +247,12 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,if ( !timer_enabled (h, tn) )return ; +if ( !timer_int_valid (h, tn) ) + { +ASSERT_UNREACHABLE (); +return ; + } + tn_cmp =hpet_get_comparator (h, tn, guest_time); cur_tick =hpet_read_maincounter (h, guest_time);if (timer_is_32bit (h, tn) )
@@ -304,6 +313,30 @@ static inline uint64_t hpet_fixup_reg(return new; } +static void timer_sanitize_int (HPETState *h,unsigned int tn) +{ +unsigned int irq; + +if (timer_int_valid (h, tn) ) +return ; + + h->hpet.timers[tn].config &= ~HPET_TN_ROUTE ; +if ( !timer_enabled (h, tn) ) +return ; + +/* +* If the requested interrupt is not valid and the timer is +* enabled pick the first irq. +*/ + irq =ffs (timer_int_route_cap (h, tn)); +if ( !irq ) + { +ASSERT_UNREACHABLE (); +return ; + } + h->hpet.timers[tn].config |= (irq -1 ) <<HPET_TN_ROUTE_SHIFT ; +} +static int hpet_write (struct vcpu *v,unsigned long addr,unsigned int length,unsigned long val)
@@ -386,6 +419,8 @@ static int hpet_write( h->hpet.timers[tn].config =hpet_fixup_reg (new_val, old_val,0x3f4e ); +timer_sanitize_int (h, tn); +if (timer_level (h, tn) ) { gdprintk(XENLOG_ERR,
@@ -621,6 +656,7 @@ static int hpet_load(struct domain *d, hvm_domain_context_t *h)if (timer_is_32bit (hp, i) ) cmp = (uint32_t )cmp; hp->hpet.timers[i].cmp = cmp; +timer_sanitize_int (hp, i); }#undef C
Signature · 7 lines
2.17.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/arch/x86/hvm/hpet.c static inline uint64_t hpet_fixup_reg( · line 316
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 10.05.18 at 11:08, <[email protected]> wrote:
xen/arch/x86/hvm/hpet.c +1
-0@@ -304,6 +313,30 @@ static inline uint64_t hpet_fixup_reg(return new; } +static void timer_sanitize_int (HPETState *h,unsigned int tn)
"int" as sort of a suffix here might be misleading (could be viewed as some sort of integer as well) - how about "route" (or "int_route") instead?
xen/arch/x86/hvm/hpet.c static inline uint64_t hpet_fixup_reg( · line 323
Quoted 7 lines
xen/arch/x86/hvm/hpet.c +7
-0+{ +unsigned int irq; + +if (timer_int_valid (h, tn) ) +return ; + + h->hpet.timers[tn].config &= ~HPET_TN_ROUTE ;
Please don't open-code timer_config() (also below), despite there being quite a few examples in pre-existing code.
Quoted 4 lines
xen/arch/x86/hvm/hpet.c +4
-0+if ( !timer_enabled (h, tn) ) +return ; + +/*
xen/arch/x86/hvm/hpet.c static inline uint64_t hpet_fixup_reg( · line 337
Quoted 10 lines
xen/arch/x86/hvm/hpet.c +10
-0+* If the requested interrupt is not valid and the timer is +* enabled pick the first irq. +*/ + irq =ffs (timer_int_route_cap (h, tn)); +if ( !irq ) + { +ASSERT_UNREACHABLE (); +return ; + } + h->hpet.timers[tn].config |= (irq -1 ) <<HPET_TN_ROUTE_SHIFT ;
I think I would prefer find_first_set_bit() instead of ffs() here, to avoid the subtraction of 1. Also please use MASK_INSR() here - HPET_TN_ROUTE_SHIFT is a #define that should actually go away.
I'm also unconvinced the assertion is really useful here - the field in question is r/o, and we initialize it unconditionally in hpet_set().
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel