From: Paolo Bonzini <pbonzini@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>,
QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] pl031 time across vm save/reload
Date: Fri, 5 Jul 2019 11:48:49 +0200 [thread overview]
Message-ID: <ac682594-fac7-ca67-2cef-85db15920c7c@redhat.com> (raw)
In-Reply-To: <CAFEAcA-yzWhwYPzC9px0H-=x3XEPVBjZOhk402is49YpbmJX9g@mail.gmail.com>
On 04/07/19 18:02, Peter Maydell wrote:
> I've had a report that the way the PL031 model handles time
> across a vm save/reload fails to correctly advance the guest
> RTC when the host RTC has advanced between the save and reload.
> I looked at the code and my correspondent's analysis (which
> I quote below, lightly edited) looks correct to me, but I'm not
> entirely sure how our RTC stuff is supposed to work. Paolo,
> you wrote this (way back in commit b0f26631bc5179006) -- any opinions?
>
> In the pl031 RTC device. the current time is given by:
>
> int64_t now = qemu_clock_get_ns(rtc_clock);
> return s->tick_offset + now / NANOSECONDS_PER_SECOND;
>
> On save we do:
>
> /* tick_offset is base_time - rtc_clock base time. Instead, we want to
> * store the base time relative to the QEMU_CLOCK_VIRTUAL for
> backwards-compatibility. */
> int64_t delta = qemu_clock_get_ns(rtc_clock) -
> qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> s->tick_offset_vmstate = s->tick_offset + delta / NANOSECONDS_PER_SECOND;
>
> On restore:
>
> int64_t delta = qemu_clock_get_ns(rtc_clock) -
> qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> s->tick_offset = s->tick_offset_vmstate - delta / NANOSECONDS_PER_SECOND;
>
> So, no matter what is requested, if "qemu_clock_get_ns(rtc_clock)"
> increases (eg, because host time increased), then tick_offset
> reduces, which makes time follow QEMU_CLOCK_VIRTUAL no matter what
> was requested on qemu's command line.
>
> (That is, because we migrate "offset relative to CLOCK_VIRTUAL"
> and CLOCK_VIRTUAL does not advance when the VM is stopped,
> we don't get the right behaviour of "offset is relative to
> the new CLOCK_RTC, which might have advanced".).
You're right, the compatibility causes wrong behavior for the default
-rtc settings (the RC pauses across migration). The right thing to do
would be to store the base rather than the offset: that is, you store
the time at which LR was written. Then the offset is s->lr - s->base
and it's independent of the machine on which the rtc_clock is being read.
By the way, the data sheet says "the counter and match values are
compared in a comparator. When both values are equal, the RTCINTR
interrupt is asserted HIGH"; QEMU compares the RTC value (read from
RTC_DR) and not the counter value, but Linux code seems to expect QEMU's
behavior.
Paolo
next prev parent reply other threads:[~2019-07-05 9:52 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-04 16:02 [Qemu-devel] pl031 time across vm save/reload Peter Maydell
2019-07-05 9:48 ` Paolo Bonzini [this message]
2019-07-05 9:58 ` Peter Maydell
2019-07-05 10:13 ` Paolo Bonzini
2019-07-05 10:21 ` Peter Maydell
2019-07-05 10:32 ` Paolo Bonzini
2019-07-05 10:42 ` Peter Maydell
2019-07-08 17:41 ` Dr. David Alan Gilbert
2019-07-05 10:26 ` Peter Maydell
2019-07-08 14:03 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ac682594-fac7-ca67-2cef-85db15920c7c@redhat.com \
--to=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).