[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH 3/3] plat/kvm: Add KVM (x86_64) timer support


  • To: Wei Chen <Wei.Chen@xxxxxxx>, Simon Kuenzer <simon.kuenzer@xxxxxxxxx>, "minios-devel@xxxxxxxxxxxxx" <minios-devel@xxxxxxxxxxxxx>
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Mon, 14 May 2018 18:56:27 +0300
  • Cc: nd <nd@xxxxxxx>
  • Delivery-date: Mon, 14 May 2018 15:56:41 +0000
  • Ironport-phdr: 9a23:dY4RvB1WLto3gcFysmDT+DRfVm0co7zxezQtwd8ZsesWIvjxwZ3uMQTl6Ol3ixeRBMOHs6kC07KempujcFRI2YyGvnEGfc4EfD4+ouJSoTYdBtWYA1bwNv/gYn9yNs1DUFh44yPzahANS47xaFLIv3K98yMZFAnhOgppPOT1HZPZg9iq2+yo9JDffwtFiCChbb9uMR67sRjfus4KjIV4N60/0AHJonxGe+RXwWNnO1eelAvi68mz4ZBu7T1et+ou+MBcX6r6eb84TaFDAzQ9L281/szrugLdQgaJ+3ART38ZkhtMAwjC8RH6QpL8uTb0u+ZhxCWXO9D9QKsqUjq+8ahkVB7oiD8GNzEn9mHXltdwh79frB64uhBz35LYbISTOfFjfK3SYMkaSHJcUMhPWSxPAoCyYYUBAOUOP+lXs4bzqkASrRunHwSgGP/jxzFKi3LwwKY00/4hEQbD3AE4Ad8Ot2/UrMn0NKcUTOu7yrTIzS3eb/hL3jry9I/Icgs9ofGNW7J8a87RyVQ3FwPYlViftZflMymN1uQQqGeX9fBgVeW2hmMhtgp/rD+vxsI2hYnIgIIY0l/F+jt/wIYtP924T0x7bsC4H5tMrS2aMJF2QswkTmp1uyg60qULtYO0cSQX0pgqxB7SZ+aZf4SW4B/vTuicLS9liH9qZb6znRW//Ei6xuHiWMS53kxGojdKn9XSsH0Gygbd5dKdSvRn+0eswTOP1wfO5e5aOU00jq/bK4I5wr43i5oTrVzPHi/ol0Xyi6+bblsk+u2y5OTmZrXqvIOTN5RuhgHmL6QhgdazAf4/MgQWXmib//qz1KH78EHkT7hGk+c6n6rZvZzAO8gXuKy0DxVb34o77hawFTam0NAWnXkdK1JFfQqKgJTuO1HVPPD4DPG/j06okDd23/DGI6bsDYnWLnTZiLfhYK1y51RGyAo01dxf/Y5bCqkdIPLvXU/8rNzXAQE4MwCu3evnCMty1oUFVG2VGK+ZNbnevkOP5uIqO+OMfpMauC7hK/g54P7jlXk5lkIGcKazx5sXbWq0EehhI0WceXDsmMsOEX8WvgoiS+znkFiCUT9VZ3azWKI8/Sg3B5+4AofdWI+thKeM3CO6Hp1NemBGEUqAEHj2eIWCQfsMbziSIsB5mDweSbehU5Mh1Q2ptALi0LpnL+/U+ioetZ7519h1+vPclRcz9DNqFcSQyGeNQH9okWMMXTA5x7pzrlJgygTL7a8tvPFZXfdT6vUBBjg3NNj3z+F8I9noX0TKedLfDB6dQ9ChB3kSU9830dIUKxJmGtCliFbAwiesEbIPv7qGD5g9+OTRwi61b4x4ynDH07Jkg1Q4T89nMWy9mrU55wXVQYnTnA/Rw6Orc6Ubx2vB+XmOyUKKvVpES0hgXKODWmoQMBj4t9P8s2jFVKOvD/wDLxNcgZqJLbBWa9uviUheWd/oI5LGfmj3gWDmVkXA/a+FcIe/IzZV5y7aEkVR1llLpXs=
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

Hi Wei,

On 05/09/2018 08:32 AM, Wei Chen wrote:
> Hi Costin,
> 
>> -----Original Message-----
>> From: Minios-devel <minios-devel-bounces@xxxxxxxxxxxxxxxxxxxx> On Behalf Of
>> Costin Lupu
>> Sent: 2018年5月1日 7:55
>> To: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>; minios-devel@xxxxxxxxxxxxx
>> Subject: Re: [Minios-devel] [UNIKRAFT PATCH 3/3] plat/kvm: Add KVM (x86_64)
>> timer support
>>
>> On 04/30/2018 03:44 PM, Simon Kuenzer wrote:
>>> See my comments inline.
>>>
>>> On 05.04.2018 17:21, Costin Lupu wrote:
>>>> We are using TSC clock as main timer on KVM.
>>>>
>>>> Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
>>>> ---
>>>>   plat/kvm/Makefile.uk              |   3 +
>>>>   plat/kvm/clock_subr.c             | 226 ++++++++++++++++++++++++
>>>>   plat/kvm/include/kvm/clock_subr.h |  83 +++++++++
>>>>   plat/kvm/include/kvm/tscclock.h   |  42 +++++
>>>>   plat/kvm/irq.c                    |  10 ++
>>>>   plat/kvm/time.c                   |  62 +++++++
>>>>   plat/kvm/tscclock.c               | 356
> 
> 
> [...] trimming for easy reading
> 
>>>> +/*
>>>> + * Calibrate TSC and initialise TSC clock.
>>>> + */
>>>> +int tscclock_init(void)
>>>> +{
>>>> +    __u64 tsc_freq, rtc_boot;
>>>> +
>>>> +    /* Initialise i8254 timer channel 0 to mode 2 at 100 Hz */
>>>> +    outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
>>>> +    outb(TIMER_CNTR, (TIMER_HZ / 100) & 0xff);
>>>> +    outb(TIMER_CNTR, (TIMER_HZ / 100) >> 8);
>>>> +
>>>> +    /*
>>>> +     * Read RTC "time at boot". This must be done just before
>>>> tsc_base is
>>>> +     * initialised in order to get a correct offset below.
>>>> +     */
>>>> +    rtc_boot = rtc_gettimeofday();
>>>> +
>>>> +    /*
>>>> +     * Calculate TSC frequency by calibrating against an 0.1s delay
>>>> +     * using the i8254 timer.
>>>> +     */
>>>
>>> Wow, this is adds a 100ms boot delay to the Unikernels on KVM. Can you
>>> put an TODO comment for revisiting this later? Maybe we can find a
>>> different method to get the correct value for the TSC frequency.
>>
>> Right.
>>
> 
> It seems that QEMU-KVM publish TSC frequency to the guest OS in CPUID page
> 0x40000010. Can we read the tsc_freq from CPUID just like VMWare does?

Thanks for the heads-up. I've tried it, but I don't have a CPUID page
0x40000010. Using the base leaf (0x40000000) to get the maximum page
number gives me only a maximum value of 0x40000001. Can you please
indicate a reference where you got this information from?

Historically speaking, TSCs needed a calibration phase, thus it's part
of the classic boot process. Providing the frequency directly must be a
PV trick provided by the hypervisor. My guess is there should be some
special parameter for it when running QEMU.

The timer code for KVM is ported from Solo5 which has 2 timer sources:
pvclock and TSC. Most of the existing documentation recommends using the
pvclock. I chose TSC because I needed a source for timer interrupts that
should also be configurable (for pvclock the frequency is fixed). So
maybe a cleaner solution would be either:
- using a different timer for generating interrupts and keeping the wall
clock time, or
- keeping TSC as source for timer interrupts and using pvclock for wall
clock time

>>>> +    tsc_base = rdtsc();
>>>> +    i8254_delay(100000);
>>>> +    tsc_freq = (rdtsc() - tsc_base) * 10;
>>>> +    uk_printd(DLVL_INFO,
>>>> +        "Clock source: TSC, frequency estimate is %llu Hz\n",
>>>> +        (unsigned long long) tsc_freq); //TODO
>>>> +
>>>> +    /*
>>>> +     * Calculate TSC scaling multiplier.
>>>> +     *
>>>> +     * (0.32) tsc_mult = NSEC_PER_SEC (32.32) / tsc_freq (32.0)
>>>> +     */
>>>> +    tsc_mult = (NSEC_PER_SEC << 32) / tsc_freq;
>>>> +
>>>> +    /*
>>>> +     * Monotonic time begins at tsc_base (first read of TSC before
>>>> +     * calibration).
>>>> +     */
>>>> +    time_base = mul64_32(tsc_base, tsc_mult);
>>>> +
>>>> +    /*
>>>> +     * Compute RTC epoch offset by subtracting monotonic time_base
>>>> from RTC
>>>> +     * time at boot.
>>>> +     */
>>>> +    rtc_epochoffset = rtc_boot - time_base;
>>>> +
>>>> +    /*
>>>> +     * Initialise i8254 timer channel 0 to mode 4 (one shot).
>>>> +     */
>>>> +    outb(TIMER_MODE, TIMER_SEL0 | TIMER_ONESHOT | TIMER_16BIT);
>>>> +
>>>> +    return 0;
>>>> +}
> 
> _______________________________________________
> Minios-devel mailing list
> Minios-devel@xxxxxxxxxxxxxxxxxxxx
> https://lists.xenproject.org/mailman/listinfo/minios-devel
> 

_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.