cregit-Linux how code gets into the kernel

Release 4.7 arch/x86/xen/time.c

Directory: arch/x86/xen
/*
 * Xen time implementation.
 *
 * This is implemented in terms of a clocksource driver which uses
 * the hypervisor clock as a nanosecond timebase, and a clockevent
 * driver which uses the hypervisor's timer mechanism.
 *
 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
 */
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/clocksource.h>
#include <linux/clockchips.h>
#include <linux/kernel_stat.h>
#include <linux/math64.h>
#include <linux/gfp.h>
#include <linux/slab.h>
#include <linux/pvclock_gtod.h>
#include <linux/timekeeper_internal.h>

#include <asm/pvclock.h>
#include <asm/xen/hypervisor.h>
#include <asm/xen/hypercall.h>

#include <xen/events.h>
#include <xen/features.h>
#include <xen/interface/xen.h>
#include <xen/interface/vcpu.h>

#include "xen-ops.h"

/* Xen may fire a timer up to this many ns early */

#define TIMER_SLOP	100000

#define NS_PER_TICK	(1000000000LL / HZ)

/* snapshots of runstate info */
static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate_snapshot);

/* unused ns of stolen time */
static DEFINE_PER_CPU(u64, xen_residual_stolen);


static void do_stolen_accounting(void) { struct vcpu_runstate_info state; struct vcpu_runstate_info *snap; s64 runnable, offline, stolen; cputime_t ticks; xen_get_runstate_snapshot(&state); WARN_ON(state.state != RUNSTATE_running); snap = this_cpu_ptr(&xen_runstate_snapshot); /* work out how much time the VCPU has not been runn*ing* */ runnable = state.time[RUNSTATE_runnable] - snap->time[RUNSTATE_runnable]; offline = state.time[RUNSTATE_offline] - snap->time[RUNSTATE_offline]; *snap = state; /* Add the appropriate number of ticks of stolen time, including any left-overs from last time. */ stolen = runnable + offline + __this_cpu_read(xen_residual_stolen); if (stolen < 0) stolen = 0; ticks = iter_div_u64_rem(stolen, NS_PER_TICK, &stolen); __this_cpu_write(xen_residual_stolen, stolen); account_steal_ticks(ticks); }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge12291.04%228.57%
christoph lameterchristoph lameter64.48%228.57%
tejun heotejun heo32.24%114.29%
martin schwidefskymartin schwidefsky21.49%114.29%
stefano stabellinistefano stabellini10.75%114.29%
Total134100.00%7100.00%

/* Get the TSC speed from Xen */
static unsigned long xen_tsc_khz(void) { struct pvclock_vcpu_time_info *info = &HYPERVISOR_shared_info->vcpu_info[0].time; return pvclock_tsc_khz(info); }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge2583.33%120.00%
glauber de oliveira costaglauber de oliveira costa26.67%120.00%
gerd hoffmanngerd hoffmann13.33%120.00%
alok katariaalok kataria13.33%120.00%
stefano stabellinistefano stabellini13.33%120.00%
Total30100.00%5100.00%


cycle_t xen_clocksource_read(void) { struct pvclock_vcpu_time_info *src; cycle_t ret; preempt_disable_notrace(); src = &__this_cpu_read(xen_vcpu)->time; ret = pvclock_clocksource_read(src); preempt_enable_notrace(); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge3073.17%350.00%
gerd hoffmanngerd hoffmann717.07%116.67%
boris ostrovskyboris ostrovsky37.32%116.67%
christoph lameterchristoph lameter12.44%116.67%
Total41100.00%6100.00%


static cycle_t xen_clocksource_get_cycles(struct clocksource *cs) { return xen_clocksource_read(); }

Contributors

PersonTokensPropCommitsCommitProp
magnus dammmagnus damm15100.00%1100.00%
Total15100.00%1100.00%


static void xen_read_wallclock(struct timespec *ts) { struct shared_info *s = HYPERVISOR_shared_info; struct pvclock_wall_clock *wall_clock = &(s->wc); struct pvclock_vcpu_time_info *vcpu_time; vcpu_time = &get_cpu_var(xen_vcpu)->time; pvclock_read_wallclock(wall_clock, vcpu_time, ts); put_cpu_var(xen_vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge3355.93%150.00%
gerd hoffmanngerd hoffmann2644.07%150.00%
Total59100.00%2100.00%


static void xen_get_wallclock(struct timespec *now) { xen_read_wallclock(now); }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge956.25%133.33%
david vrabeldavid vrabel637.50%133.33%
stefano stabellinistefano stabellini16.25%133.33%
Total16100.00%3100.00%


static int xen_set_wallclock(const struct timespec *now) { return -1; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge1168.75%133.33%
david vrabeldavid vrabel425.00%133.33%
stefano stabellinistefano stabellini16.25%133.33%
Total16100.00%3100.00%


static int xen_pvclock_gtod_notify(struct notifier_block *nb, unsigned long was_set, void *priv) { /* Protected by the calling core code serialization */ static struct timespec64 next_sync; struct xen_platform_op op; struct timespec64 now; struct timekeeper *tk = priv; static bool settime64_supported = true; int ret; now.tv_sec = tk->xtime_sec; now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); /* * We only take the expensive HV call when the clock was set * or when the 11 minutes RTC synchronization time elapsed. */ if (!was_set && timespec64_compare(&now, &next_sync) < 0) return NOTIFY_OK; again: if (settime64_supported) { op.cmd = XENPF_settime64; op.u.settime64.mbz = 0; op.u.settime64.secs = now.tv_sec; op.u.settime64.nsecs = now.tv_nsec; op.u.settime64.system_time = xen_clocksource_read(); } else { op.cmd = XENPF_settime32; op.u.settime32.secs = now.tv_sec; op.u.settime32.nsecs = now.tv_nsec; op.u.settime32.system_time = xen_clocksource_read(); } ret = HYPERVISOR_platform_op(&op); if (ret == -ENOSYS && settime64_supported) { settime64_supported = false; goto again; } if (ret < 0) return NOTIFY_BAD; /* * Move the next drift compensation time 11 minutes * ahead. That's emulating the sync_cmos_clock() update for * the hardware RTC. */ next_sync = now; next_sync.tv_sec += 11 * 60; return NOTIFY_OK; }

Contributors

PersonTokensPropCommitsCommitProp
stefano stabellinistefano stabellini14155.95%450.00%
jeremy fitzhardingejeremy fitzhardinge5722.62%225.00%
david vrabeldavid vrabel5421.43%225.00%
Total252100.00%8100.00%

static struct notifier_block xen_pvclock_gtod_notifier = { .notifier_call = xen_pvclock_gtod_notify, }; static struct clocksource xen_clocksource __read_mostly = { .name = "xen", .rating = 400, .read = xen_clocksource_get_cycles, .mask = ~0, .flags = CLOCK_SOURCE_IS_CONTINUOUS, }; /* Xen clockevent implementation Xen has two clockevent implementations: The old timer_op one works with all released versions of Xen prior to version 3.0.4. This version of the hypervisor provides a single-shot timer with nanosecond resolution. However, sharing the same event channel is a 100Hz tick which is delivered while the vcpu is running. We don't care about or use this tick, but it will cause the core time code to think the timer fired too soon, and will end up resetting it each time. It could be filtered, but doing so has complications when the ktime clocksource is not yet the xen clocksource (ie, at boot time). The new vcpu_op-based timer interface allows the tick timer period to be changed or turned off. The tick timer is not useful as a periodic timer because events are only delivered to running vcpus. The one-shot timer can report when a timeout is in the past, so set_next_event is capable of returning -ETIME when appropriate. This interface is used when available. */ /* Get a hypervisor absolute time. In theory we could maintain an offset between the kernel's time and the hypervisor's time, and apply that to a kernel's absolute timeout. Unfortunately the hypervisor and kernel times can drift even if the kernel is using the Xen clocksource, because ntp can warp the kernel's clocksource. */
static s64 get_abs_timeout(unsigned long delta) { return xen_clocksource_read() + delta; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge16100.00%1100.00%
Total16100.00%1100.00%


static int xen_timerop_shutdown(struct clock_event_device *evt) { /* cancel timeout */ HYPERVISOR_set_timer_op(0); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge1470.00%150.00%
viresh kumarviresh kumar630.00%150.00%
Total20100.00%2100.00%


static int xen_timerop_set_next_event(unsigned long delta, struct clock_event_device *evt) { WARN_ON(!clockevent_state_oneshot(evt)); if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0) BUG(); /* We may have missed the deadline, but there's no real way of knowing for sure. If the event was in the past, then we'll get an immediate interrupt. */ return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge3990.70%150.00%
viresh kumarviresh kumar49.30%150.00%
Total43100.00%2100.00%

static const struct clock_event_device xen_timerop_clockevent = { .name = "xen", .features = CLOCK_EVT_FEAT_ONESHOT, .max_delta_ns = 0xffffffff, .min_delta_ns = TIMER_SLOP, .mult = 1, .shift = 0, .rating = 500, .set_state_shutdown = xen_timerop_shutdown, .set_next_event = xen_timerop_set_next_event, };
static int xen_vcpuop_shutdown(struct clock_event_device *evt) { int cpu = smp_processor_id(); if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, cpu, NULL) || HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL)) BUG(); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge2967.44%150.00%
viresh kumarviresh kumar1432.56%150.00%
Total43100.00%2100.00%


static int xen_vcpuop_set_oneshot(struct clock_event_device *evt) { int cpu = smp_processor_id(); if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL)) BUG(); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
viresh kumarviresh kumar1955.88%150.00%
jeremy fitzhardingejeremy fitzhardinge1544.12%150.00%
Total34100.00%2100.00%


static int xen_vcpuop_set_next_event(unsigned long delta, struct clock_event_device *evt) { int cpu = smp_processor_id(); struct vcpu_set_singleshot_timer single; int ret; WARN_ON(!clockevent_state_oneshot(evt)); single.timeout_abs_ns = get_abs_timeout(delta); /* Get an event anyway, even if the timeout is already expired */ single.flags = 0; ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, cpu, &single); BUG_ON(ret != 0); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge6992.00%133.33%
viresh kumarviresh kumar45.33%133.33%
stefano stabellinistefano stabellini22.67%133.33%
Total75100.00%3100.00%

static const struct clock_event_device xen_vcpuop_clockevent = { .name = "xen", .features = CLOCK_EVT_FEAT_ONESHOT, .max_delta_ns = 0xffffffff, .min_delta_ns = TIMER_SLOP, .mult = 1, .shift = 0, .rating = 500, .set_state_shutdown = xen_vcpuop_shutdown, .set_state_oneshot = xen_vcpuop_set_oneshot, .set_next_event = xen_vcpuop_set_next_event, }; static const struct clock_event_device *xen_clockevent = &xen_timerop_clockevent; struct xen_clock_event_device { struct clock_event_device evt; char name[16]; }; static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
static irqreturn_t xen_timer_interrupt(int irq, void *dev_id) { struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt); irqreturn_t ret; ret = IRQ_NONE; if (evt->event_handler) { evt->event_handler(evt); ret = IRQ_HANDLED; } do_stolen_accounting(); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge5391.38%250.00%
christoph lameterchristoph lameter35.17%125.00%
konrad rzeszutek wilkkonrad rzeszutek wilk23.45%125.00%
Total58100.00%4100.00%


void xen_teardown_timer(int cpu) { struct clock_event_device *evt; BUG_ON(cpu == 0); evt = &per_cpu(xen_clock_events, cpu).evt; if (evt->irq >= 0) { unbind_from_irqhandler(evt->irq, NULL); evt->irq = -1; } }

Contributors

PersonTokensPropCommitsCommitProp
konrad rzeszutek wilkkonrad rzeszutek wilk58100.00%1100.00%
Total58100.00%1100.00%


void xen_setup_timer(int cpu) { struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu); struct clock_event_device *evt = &xevt->evt; int irq; WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu); if (evt->irq >= 0) xen_teardown_timer(cpu); printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu); snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu); irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt, IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER| IRQF_FORCE_RESUME|IRQF_EARLY_RESUME, xevt->name, NULL); (void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX); memcpy(evt, xen_clockevent, sizeof(*evt)); evt->cpumask = cpumask_of(cpu); evt->irq = irq; }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge7246.45%218.18%
konrad rzeszutek wilkkonrad rzeszutek wilk3824.52%327.27%
vitaly kuznetsovvitaly kuznetsov2818.06%19.09%
david vrabeldavid vrabel127.74%218.18%
ian campbellian campbell42.58%218.18%
rusty russellrusty russell10.65%19.09%
Total155100.00%11100.00%


void xen_setup_cpu_clockevents(void) { clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt)); }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge1372.22%250.00%
christoph lameterchristoph lameter316.67%125.00%
konrad rzeszutek wilkkonrad rzeszutek wilk211.11%125.00%
Total18100.00%4100.00%


void xen_timer_resume(void) { int cpu; pvclock_resume(); if (xen_clockevent != &xen_vcpuop_clockevent) return; for_each_online_cpu(cpu) { if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL)) BUG(); } }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge41100.00%2100.00%
Total41100.00%2100.00%

static const struct pv_time_ops xen_time_ops __initconst = { .sched_clock = xen_clocksource_read, };
static void __init xen_time_init(void) { int cpu = smp_processor_id(); struct timespec tp; /* As Dom0 is never moved, no penalty on using TSC there */ if (xen_initial_domain()) xen_clocksource.rating = 275; clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC); if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL) == 0) { /* Successfully turned off 100Hz tick, so we have the vcpuop-based timer interface */ printk(KERN_DEBUG "Xen: using vcpuop timer interface\n"); xen_clockevent = &xen_vcpuop_clockevent; } /* Set initial system time with full resolution */ xen_read_wallclock(&tp); do_settimeofday(&tp); setup_force_cpu_cap(X86_FEATURE_TSC); xen_setup_runstate_info(cpu); xen_setup_timer(cpu); xen_setup_cpu_clockevents(); if (xen_initial_domain()) pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier); }

Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge6459.26%327.27%
imre palikimre palik1211.11%19.09%
david vrabeldavid vrabel1110.19%19.09%
john stultzjohn stultz109.26%218.18%
ian campbellian campbell54.63%19.09%
andi kleenandi kleen43.70%19.09%
stefano stabellinistefano stabellini10.93%19.09%
daniel kiperdaniel kiper10.93%19.09%
Total108100.00%11100.00%


void __init xen_init_time_ops(void) { pv_time_ops = xen_time_ops; x86_init.timers.timer_init = xen_time_init; x86_init.timers.setup_percpu_clockev = x86_init_noop; x86_cpuinit.setup_percpu_clockev = x86_init_noop; x86_platform.calibrate_tsc = xen_tsc_khz; x86_platform.get_wallclock = xen_get_wallclock; /* Dom0 uses the native method to set the hardware RTC. */ if (!xen_initial_domain()) x86_platform.set_wallclock = xen_set_wallclock; }

Contributors

PersonTokensPropCommitsCommitProp
stefano stabellinistefano stabellini5186.44%133.33%
david vrabeldavid vrabel711.86%133.33%
daniel kiperdaniel kiper11.69%133.33%
Total59100.00%3100.00%

#ifdef CONFIG_XEN_PVHVM
static void xen_hvm_setup_cpu_clockevents(void) { int cpu = smp_processor_id(); xen_setup_runstate_info(cpu); /* * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence * doing it xen_hvm_cpu_notify (which gets called by smp_init during * early bootup and also during CPU hotplug events). */ xen_setup_cpu_clockevents(); }

Contributors

PersonTokensPropCommitsCommitProp
stefano stabellinistefano stabellini2295.65%150.00%
konrad rzeszutek wilkkonrad rzeszutek wilk14.35%150.00%
Total23100.00%2100.00%


void __init xen_hvm_init_time_ops(void) { /* vector callback is needed otherwise we cannot receive interrupts * on cpu > 0 and at this point we don't know how many cpus are * available */ if (!xen_have_vector_callback) return; if (!xen_feature(XENFEAT_hvm_safe_pvclock)) { printk(KERN_INFO "Xen doesn't support pvclock on HVM," "disable pv timer\n"); return; } pv_time_ops = xen_time_ops; x86_init.timers.setup_percpu_clockev = xen_time_init; x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents; x86_platform.calibrate_tsc = xen_tsc_khz; x86_platform.get_wallclock = xen_get_wallclock; x86_platform.set_wallclock = xen_set_wallclock; }

Contributors

PersonTokensPropCommitsCommitProp
stefano stabellinistefano stabellini6898.55%266.67%
daniel kiperdaniel kiper11.45%133.33%
Total69100.00%3100.00%

#endif

Overall Contributors

PersonTokensPropCommitsCommitProp
jeremy fitzhardingejeremy fitzhardinge93055.06%918.00%
stefano stabellinistefano stabellini31218.47%918.00%
konrad rzeszutek wilkkonrad rzeszutek wilk1277.52%510.00%
david vrabeldavid vrabel1096.45%510.00%
viresh kumarviresh kumar563.32%12.00%
gerd hoffmanngerd hoffmann372.19%12.00%
vitaly kuznetsovvitaly kuznetsov311.84%12.00%
magnus dammmagnus damm160.95%12.00%
christoph lameterchristoph lameter130.77%24.00%
imre palikimre palik120.71%12.00%
john stultzjohn stultz100.59%24.00%
ian campbellian campbell90.53%36.00%
tejun heotejun heo80.47%24.00%
daniel kiperdaniel kiper40.24%12.00%
andi kleenandi kleen40.24%12.00%
boris ostrovskyboris ostrovsky30.18%12.00%
alok katariaalok kataria20.12%12.00%
martin schwidefskymartin schwidefsky20.12%12.00%
glauber de oliveira costaglauber de oliveira costa20.12%12.00%
laszlo erseklaszlo ersek10.06%12.00%
rusty russellrusty russell10.06%12.00%
Total1689100.00%50100.00%
Directory: arch/x86/xen
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}