aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2008-03-11 10:35:56 -0400
committerRusty Russell <rusty@rustcorp.com.au>2008-03-10 18:35:57 -0400
commit3fabc55f34b72720e8a10aa442bd3415a211edb3 (patch)
tree2ccc469ce6daff4430c04d89b139f3d7ac02aaac /arch/x86
parentf14ae652baa3d72ae378f0c06b89cc2c4ef15ff8 (diff)
lguest: Sanitize the lguest clock.
Now the TSC code handles a zero return from calculate_cpu_khz(), lguest can simply pass through the value it gets from the Host: if non-zero, all the normal TSC code applies. Otherwise (or if the Host really doesn't support TSC), the clocksource code will fall back to the slower but reasonable lguest clock. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'arch/x86')
-rw-r--r--arch/x86/lguest/boot.c53
1 files changed, 21 insertions, 32 deletions
diff --git a/arch/x86/lguest/boot.c b/arch/x86/lguest/boot.c
index cccb38a59653..9c27c104d83c 100644
--- a/arch/x86/lguest/boot.c
+++ b/arch/x86/lguest/boot.c
@@ -84,7 +84,6 @@ struct lguest_data lguest_data = {
84 .blocked_interrupts = { 1 }, /* Block timer interrupts */ 84 .blocked_interrupts = { 1 }, /* Block timer interrupts */
85 .syscall_vec = SYSCALL_VECTOR, 85 .syscall_vec = SYSCALL_VECTOR,
86}; 86};
87static cycle_t clock_base;
88 87
89/*G:037 async_hcall() is pretty simple: I'm quite proud of it really. We have a 88/*G:037 async_hcall() is pretty simple: I'm quite proud of it really. We have a
90 * ring buffer of stored hypercalls which the Host will run though next time we 89 * ring buffer of stored hypercalls which the Host will run though next time we
@@ -327,8 +326,8 @@ static void lguest_cpuid(unsigned int *ax, unsigned int *bx,
327 case 1: /* Basic feature request. */ 326 case 1: /* Basic feature request. */
328 /* We only allow kernel to see SSE3, CMPXCHG16B and SSSE3 */ 327 /* We only allow kernel to see SSE3, CMPXCHG16B and SSSE3 */
329 *cx &= 0x00002201; 328 *cx &= 0x00002201;
330 /* SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, FPU. */ 329 /* SSE, SSE2, FXSR, MMX, CMOV, CMPXCHG8B, TSC, FPU. */
331 *dx &= 0x07808101; 330 *dx &= 0x07808111;
332 /* The Host can do a nice optimization if it knows that the 331 /* The Host can do a nice optimization if it knows that the
333 * kernel mappings (addresses above 0xC0000000 or whatever 332 * kernel mappings (addresses above 0xC0000000 or whatever
334 * PAGE_OFFSET is set to) haven't changed. But Linux calls 333 * PAGE_OFFSET is set to) haven't changed. But Linux calls
@@ -595,19 +594,25 @@ static unsigned long lguest_get_wallclock(void)
595 return lguest_data.time.tv_sec; 594 return lguest_data.time.tv_sec;
596} 595}
597 596
597/* The TSC is a Time Stamp Counter. The Host tells us what speed it runs at,
598 * or 0 if it's unusable as a reliable clock source. This matches what we want
599 * here: if we return 0 from this function, the x86 TSC clock will not register
600 * itself. */
601static unsigned long lguest_cpu_khz(void)
602{
603 return lguest_data.tsc_khz;
604}
605
606/* If we can't use the TSC, the kernel falls back to our "lguest_clock", where
607 * we read the time value given to us by the Host. */
598static cycle_t lguest_clock_read(void) 608static cycle_t lguest_clock_read(void)
599{ 609{
600 unsigned long sec, nsec; 610 unsigned long sec, nsec;
601 611
602 /* If the Host tells the TSC speed, we can trust that. */ 612 /* Since the time is in two parts (seconds and nanoseconds), we risk
603 if (lguest_data.tsc_khz) 613 * reading it just as it's changing from 99 & 0.999999999 to 100 and 0,
604 return native_read_tsc(); 614 * and getting 99 and 0. As Linux tends to come apart under the stress
605 615 * of time travel, we must be careful: */
606 /* If we can't use the TSC, we read the time value written by the Host.
607 * Since it's in two parts (seconds and nanoseconds), we risk reading
608 * it just as it's changing from 99 & 0.999999999 to 100 and 0, and
609 * getting 99 and 0. As Linux tends to come apart under the stress of
610 * time travel, we must be careful: */
611 do { 616 do {
612 /* First we read the seconds part. */ 617 /* First we read the seconds part. */
613 sec = lguest_data.time.tv_sec; 618 sec = lguest_data.time.tv_sec;
@@ -622,14 +627,14 @@ static cycle_t lguest_clock_read(void)
622 /* Now if the seconds part has changed, try again. */ 627 /* Now if the seconds part has changed, try again. */
623 } while (unlikely(lguest_data.time.tv_sec != sec)); 628 } while (unlikely(lguest_data.time.tv_sec != sec));
624 629
625 /* Our non-TSC clock is in real nanoseconds. */ 630 /* Our lguest clock is in real nanoseconds. */
626 return sec*1000000000ULL + nsec; 631 return sec*1000000000ULL + nsec;
627} 632}
628 633
629/* This is what we tell the kernel is our clocksource. */ 634/* This is the fallback clocksource: lower priority than the TSC clocksource. */
630static struct clocksource lguest_clock = { 635static struct clocksource lguest_clock = {
631 .name = "lguest", 636 .name = "lguest",
632 .rating = 400, 637 .rating = 200,
633 .read = lguest_clock_read, 638 .read = lguest_clock_read,
634 .mask = CLOCKSOURCE_MASK(64), 639 .mask = CLOCKSOURCE_MASK(64),
635 .mult = 1 << 22, 640 .mult = 1 << 22,
@@ -637,12 +642,6 @@ static struct clocksource lguest_clock = {
637 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 642 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
638}; 643};
639 644
640/* The "scheduler clock" is just our real clock, adjusted to start at zero */
641static unsigned long long lguest_sched_clock(void)
642{
643 return cyc2ns(&lguest_clock, lguest_clock_read() - clock_base);
644}
645
646/* We also need a "struct clock_event_device": Linux asks us to set it to go 645/* We also need a "struct clock_event_device": Linux asks us to set it to go
647 * off some time in the future. Actually, James Morris figured all this out, I 646 * off some time in the future. Actually, James Morris figured all this out, I
648 * just applied the patch. */ 647 * just applied the patch. */
@@ -712,19 +711,8 @@ static void lguest_time_init(void)
712 /* Set up the timer interrupt (0) to go to our simple timer routine */ 711 /* Set up the timer interrupt (0) to go to our simple timer routine */
713 set_irq_handler(0, lguest_time_irq); 712 set_irq_handler(0, lguest_time_irq);
714 713
715 /* Our clock structure looks like arch/x86/kernel/tsc_32.c if we can
716 * use the TSC, otherwise it's a dumb nanosecond-resolution clock.
717 * Either way, the "rating" is set so high that it's always chosen over
718 * any other clocksource. */
719 if (lguest_data.tsc_khz)
720 lguest_clock.mult = clocksource_khz2mult(lguest_data.tsc_khz,
721 lguest_clock.shift);
722 clock_base = lguest_clock_read();
723 clocksource_register(&lguest_clock); 714 clocksource_register(&lguest_clock);
724 715
725 /* Now we've set up our clock, we can use it as the scheduler clock */
726 pv_time_ops.sched_clock = lguest_sched_clock;
727
728 /* We can't set cpumask in the initializer: damn C limitations! Set it 716 /* We can't set cpumask in the initializer: damn C limitations! Set it
729 * here and register our timer device. */ 717 * here and register our timer device. */
730 lguest_clockevent.cpumask = cpumask_of_cpu(0); 718 lguest_clockevent.cpumask = cpumask_of_cpu(0);
@@ -995,6 +983,7 @@ __init void lguest_init(void)
995 /* time operations */ 983 /* time operations */
996 pv_time_ops.get_wallclock = lguest_get_wallclock; 984 pv_time_ops.get_wallclock = lguest_get_wallclock;
997 pv_time_ops.time_init = lguest_time_init; 985 pv_time_ops.time_init = lguest_time_init;
986 pv_time_ops.get_cpu_khz = lguest_cpu_khz;
998 987
999 /* Now is a good time to look at the implementations of these functions 988 /* Now is a good time to look at the implementations of these functions
1000 * before returning to the rest of lguest_init(). */ 989 * before returning to the rest of lguest_init(). */