mc146818rtc: Make PF independent of PIE
PATCH v2 1/1 mc146818rtc: Make PF independent of PIE
Make the PF flag behave like real hardware by always running the periodic timer without regard to the setting of the PIE bit, so that the PF will be set when the period expires even if an interrupt will not be raised. This behavior is documented on page 16 of the MC146818A advance information datasheet.
--- hw/rtc/mc146818rtc.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
hw/rtc/mc146818rtc.c +18 -3
@@ -155,9 +155,24 @@ static uint32_t rtc_periodic_clock_ticks(RTCState *s) {int period_code; -if (!(s->cmos_data[RTC_REG_B ] ®_B_PIE )) { -return 0 ; - } +/* +* Quoting the data sheet "MC146818A Advance Information", 1984, +* page 16: +* +* <quote> +* PF - The periodic interrupt flag (PF) is a read-only bit which is +* set to a "1" when a particular edge is detected on the selected tap +* of the divider chain. The RS3 to RS0 bits establish the periodic +* rate. PF is set to "1" independent of the state of the PIE bit. +* PF initiates an ~IRQ signal and sets the IRQF bit when PIE is also +* a "1". The PF bit is cleared by a ~RESET or a software read of +* Register C. +* </quote> +* +* As such, we always run the timer irrespective if the state of +* either bit so as to always set PF at regular intervals regardless +* of when software reads it. +*/ period_code = s->cmos_data[RTC_REG_A ] &0x0f ;
Signature · 1 line
2.30.2
Quoted 7 lines · Jason Thorpe
Make the PF flag behave like real hardware by always running the periodic timer without regard to the setting of the PIE bit, so that the PF will be set when the period expires even if an interrupt will not be raised. This behavior is documented on page 16 of the MC146818A advance information datasheet.
Signed-off-by Jason Thorpe <[email protected]>
I agree that there's obviously a bug in QEMU. However, I'm worried of two things with this patch.
First, the RTC device model has a complicated mechanism to deliver missed ticks of the periodic timer. This is used with old versions of Windows which lose track of time if periodic timer interrupts are not delivered. This mechanism (implemented by rtc_coalesced_timer) right now always triggers an interrupt. You need to change periodic_timer_update to not activate this mechanism if PIE=0, by checking for PIE=1 at the
if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW)
conditional.
Second, the firmware could set a nonzero period, and this would cause continuous interruptions of the guest after the firmware stops, due to s->periodic_timer firing. This is "optimized" by the bug that you are fixing. To keep the optimization you could:
- do the timer_mod in periodic_timer_update only if !PF || (PIE && lost_tick_policy==SLEW)
- in cmos_ioport_read, if !timer_pending(s->periodic_timer) call
periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
s->period, true);to update s->next_periodic_time for the next tick and ensure PF will be set.
Thanks,
Paolo
Quoted 37 lines
--- hw/rtc/mc146818rtc.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)hw/rtc/mc146818rtc.c +18
-3@@ -155,9 +155,24 @@ static uint32_t rtc_periodic_clock_ticks(RTCState *s) {int period_code; -if (!(s->cmos_data[RTC_REG_B ] ®_B_PIE )) { -return 0 ; - } +/* +* Quoting the data sheet "MC146818A Advance Information", 1984, +* page 16: +* +* <quote> +* PF - The periodic interrupt flag (PF) is a read-only bit which is +* set to a "1" when a particular edge is detected on the selected tap +* of the divider chain. The RS3 to RS0 bits establish the periodic +* rate. PF is set to "1" independent of the state of the PIE bit. +* PF initiates an ~IRQ signal and sets the IRQF bit when PIE is also +* a "1". The PF bit is cleared by a ~RESET or a software read of +* Register C. +* </quote> +* +* As such, we always run the timer irrespective if the state of +* either bit so as to always set PF at regular intervals regardless +* of when software reads it. +*/ period_code = s->cmos_data[RTC_REG_A ] &0x0f ;
Quoted 11 lines · Paolo Bonzini
I agree that there's obviously a bug in QEMU. However, I'm worried of two things with this patch.
First, the RTC device model has a complicated mechanism to deliver missed ticks of the periodic timer. This is used with old versions of Windows which lose track of time if periodic timer interrupts are not delivered. This mechanism (implemented by
rtc_coalesced_timer) right now always triggers an interrupt. You need to changeperiodic_timer_updateto not activate this mechanism if PIE=0, by checking for PIE=1 at theif (s->lost_tick_policy == LOST_TICK_POLICY_SLEW)conditional.
Makes sense.
Quoted 10 lines
Second, the firmware could set a nonzero period, and this would cause continuous interruptions of the guest after the firmware stops, due to
s->periodic_timerfiring. This is "optimized" by the bug that you are fixing. To keep the optimization you could:- do the
timer_modinperiodic_timer_updateonly if !PF || (PIE && lost_tick_policy==SLEW)- in
cmos_ioport_read, if !timer_pending(s->periodic_timer) callperiodic_timer_update(s, qemu_clock_get_ns(rtc_clock), s->period, true);to update
s->next_periodic_timefor the next tick and ensure PF will be set.
I might be missing some subtlety here, but by my reading of periodic_timer_update(), either one of those changes would result in a delay of the next latching of PF by however many ns the CPU was late in reading PF since the last time it was latched Please correct me if I’m wrong about this!
There exists software out there in the wild that depends on PF latching at regular intervals regardless if when the CPU reads, it, i.e.:
PF PF PF PF PF PF
C C C C C
-- thorpej
Quoted 12 lines · Jason Thorpe
Quoted 12 lines
Second, the firmware could set a nonzero period, and this would cause continuous interruptions of the guest after the firmware stops, due to
s->periodic_timerfiring. This is "optimized" by the bug that you are fixing. To keep the optimization you could:- do the
timer_modinperiodic_timer_updateonly if !PF || (PIE && lost_tick_policy==SLEW)- in
cmos_ioport_read, if !timer_pending(s->periodic_timer) callperiodic_timer_update(s, qemu_clock_get_ns(rtc_clock), s->period, true);
Quoted 7 lines
Quoted 1 line
to update
s->next_periodic_timefor the next tick and ensure PF will be set.I might be missing some subtlety here, but by my reading of
periodic_timer_update(), either one of those changes would result in a delay of the next latching of PF by however many ns the CPU was late in reading PF since the last time it was latched Please correct me if I’m wrong about this!
No, it shouldn't. I may be wrong, but the process is the following:
- the current rtc_clock value is stored in cur_clock
- because period_change is true, the delay between writing PF and reading C is stored in lost_clock
- then the delay is compensated by next_irq_clock = cur_clock + period - lost_clock
The best way to confirm this would be by writing a testcase (there's already an mc146818 suite in tests/qtest).
Paolo
Quoted 8 lines
There exists software out there in the wild that depends on PF latching at regular intervals regardless if when the CPU reads, it, i.e.:
PF PF PF PF PF PF
C C C C C-- thorpej
Quoted 24 lines · Paolo Bonzini
Quoted 13 lines · Jason Thorpe
Quoted 13 lines
Second, the firmware could set a nonzero period, and this would cause continuous interruptions of the guest after the firmware stops, due to
s->periodic_timerfiring. This is "optimized" by the bug that you are fixing. To keep the optimization you could:- do the
timer_modinperiodic_timer_updateonly if !PF || (PIE && lost_tick_policy==SLEW)- in
cmos_ioport_read, if !timer_pending(s->periodic_timer) call
periodic_timer_update(s, qemu_clock_get_ns(rtc_clock), s->period, true);Quoted 7 lines
Quoted 1 line
to update
s->next_periodic_timefor the next tick and ensure PF will be set.I might be missing some subtlety here, but by my reading of
periodic_timer_update(), either one of those changes would result in a delay of the next latching of PF by however many ns the CPU was late in reading PF since the last time it was latched Please correct me if I’m wrong about this!No, it shouldn't. I may be wrong, but the process is the following:
Quoted 7 lines
- the current
rtc_clockvalue is stored incur_clock- because
period_changeis true, the delay between writing PF and reading C is stored inlost_clock- then the delay is compensated by
next_irq_clock=cur_clock+ period -lost_clock
Indeed we skip the periodic timer entirely when PIE is clear, so PF never latched with PIE=0.
The following (based on top of f29ccc287a6c) seems to fix it:
-- >8 --
hw/rtc/mc146818rtc.c +14 -5
@@ -132,10 +132,6 @@ static uint32_t rtc_periodic_clock_ticks(MC146818RtcState *s) {int period_code; -if (!(s->cmos_data[RTC_REG_B ] ®_B_PIE )) { -return 0 ; - } - period_code = s->cmos_data[RTC_REG_A ] &0x0f ;return periodic_period_to_clock (period_code);
@@ -160,6 +156,12 @@ static void periodic_timer_update(MC146818RtcState *s, int64_t current_time,return ; } +if (!(s->cmos_data[RTC_REG_B ] ®_B_PIE ) && + (s->cmos_data[RTC_REG_C ] ®_C_PF )) { +timer_del (s->periodic_timer); +return ; + } +/* compute 32 khz clock */ cur_clock =muldiv64 (current_time,RTC_CLOCK_RATE ,NANOSECONDS_PER_SECOND );
@@ -192,7 +194,8 @@ static void periodic_timer_update(MC146818RtcState *s, int64_t current_time,* to a shorter period, scale up the missing ticks since the * OS handler will treat past delayed ticks as shorter. */ -if (s->lost_tick_policy ==LOST_TICK_POLICY_SLEW ) { +if (s->lost_tick_policy ==LOST_TICK_POLICY_SLEW && + (s->cmos_data[RTC_REG_B ] ®_B_PIE )) {uint32_t old_irq_coalesced = s->irq_coalesced; lost_clock += old_irq_coalesced * old_period;
@@ -239,6 +242,8 @@ static void rtc_periodic_timer(void *opaque) } }else qemu_irq_raise (s->irq); + }else { +timer_del (s->periodic_timer); } }
@@ -700,6 +705,10 @@ static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,if (ret & (REG_C_UF |REG_C_AF )) {check_update_timer (s); } +if ((ret ®_C_PF ) && !(s->cmos_data[RTC_REG_B ] ®_B_PIE )) { +periodic_timer_update (s,qemu_clock_get_ns (rtc_clock), + s->period, false); + } if(s->irq_coalesced && (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
---
Jason, do you happen to still have your setup to test it?
Quoted 15 lines
The best way to confirm this would be by writing a testcase (there's already an mc146818 suite in tests/qtest).
Paolo
Quoted 8 lines
There exists software out there in the wild that depends on PF latching at regular intervals regardless if when the CPU reads, it, i.e.:
PF PF PF PF PF PF C C C C C
-- thorpej
Quoted 4 lines · Philippe Mathieu-Daudé
Jason, do you happen to still have your setup to test it?
I can do so later this week, sure.
-- thorpej
Quoted 7 lines · Jason Thorpe
Quoted 4 lines · Philippe Mathieu-Daudé
Jason, do you happen to still have your setup to test it?
I can do so later this week, sure.
ping? :)