cregit-Linux how code gets into the kernel

Release 4.17 virt/kvm/arm/arch_timer.c

Directory: virt/kvm/arm
/*
 * Copyright (C) 2012 ARM Ltd.
 * Author: Marc Zyngier <marc.zyngier@arm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/cpu.h>
#include <linux/kvm.h>
#include <linux/kvm_host.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/uaccess.h>

#include <clocksource/arm_arch_timer.h>
#include <asm/arch_timer.h>
#include <asm/kvm_hyp.h>

#include <kvm/arm_vgic.h>
#include <kvm/arm_arch_timer.h>

#include "trace.h"


static struct timecounter *timecounter;

static unsigned int host_vtimer_irq;

static u32 host_vtimer_irq_flags;

static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);


static const struct kvm_irq_level default_ptimer_irq = {
	.irq	= 30,
	.level	= 1,
};


static const struct kvm_irq_level default_vtimer_irq = {
	.irq	= 27,
	.level	= 1,
};

static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
				 struct arch_timer_context *timer_ctx);
static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);


u64 kvm_phys_timer_read(void) { return timecounter->cc->read(timecounter->cc); }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier1894.74%150.00%
Thomas Gleixner15.26%150.00%
Total19100.00%2100.00%


static inline bool userspace_irqchip(struct kvm *kvm) { return static_branch_unlikely(&userspace_irqchip_in_use) && unlikely(!irqchip_in_kernel(kvm)); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall28100.00%1100.00%
Total28100.00%1100.00%


static void soft_timer_start(struct hrtimer *hrt, u64 ns) { hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns), HRTIMER_MODE_ABS); }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier2586.21%150.00%
Christoffer Dall413.79%150.00%
Total29100.00%2100.00%


static void soft_timer_cancel(struct hrtimer *hrt, struct work_struct *work) { hrtimer_cancel(hrt); if (work) cancel_work_sync(work); }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier1653.33%133.33%
Christoffer Dall1446.67%266.67%
Total30100.00%3100.00%


static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id) { struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id; struct arch_timer_context *vtimer; /* * We may see a timer interrupt after vcpu_put() has been called which * sets the CPU's vcpu pointer to NULL, because even though the timer * has been disabled in vtimer_save_state(), the hardware interrupt * signal may not have been retired from the interrupt controller yet. */ if (!vcpu) return IRQ_HANDLED; vtimer = vcpu_vtimer(vcpu); if (kvm_timer_should_fire(vtimer)) kvm_timer_update_irq(vcpu, true, vtimer); if (userspace_irqchip(vcpu->kvm) && !static_branch_unlikely(&has_gic_active_state)) disable_percpu_irq(host_vtimer_irq); return IRQ_HANDLED; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall5562.50%480.00%
Marc Zyngier3337.50%120.00%
Total88100.00%5100.00%

/* * Work function for handling the backup timer that we schedule when a vcpu is * no longer running, but had a timer programmed to fire in the future. */
static void kvm_timer_inject_irq_work(struct work_struct *work) { struct kvm_vcpu *vcpu; vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired); /* * If the vcpu is blocked we want to wake it up so that it will see * the timer has expired when entering the guest. */ kvm_vcpu_wake_up(vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier3694.74%133.33%
Andrew Jones12.63%133.33%
Christoffer Dall12.63%133.33%
Total38100.00%3100.00%


static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx) { u64 cval, now; cval = timer_ctx->cnt_cval; now = kvm_phys_timer_read() - timer_ctx->cntvoff; if (now < cval) { u64 ns; ns = cyclecounter_cyc2ns(timecounter->cc, cval - now, timecounter->mask, &timecounter->frac); return ns; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier6288.57%120.00%
Jintack Lim710.00%360.00%
Thomas Gleixner11.43%120.00%
Total70100.00%5100.00%


static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx) { return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) && (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE); }

Contributors

PersonTokensPropCommitsCommitProp
Jintack Lim29100.00%1100.00%
Total29100.00%1100.00%

/* * Returns the earliest expiration time in ns among guest timers. * Note that it will return 0 if none of timers can fire. */
static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu) { u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); if (kvm_timer_irq_can_fire(vtimer)) min_virt = kvm_timer_compute_delta(vtimer); if (kvm_timer_irq_can_fire(ptimer)) min_phys = kvm_timer_compute_delta(ptimer); /* If none of timers can fire, then return 0 */ if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX)) return 0; return min(min_virt, min_phys); }

Contributors

PersonTokensPropCommitsCommitProp
Jintack Lim94100.00%1100.00%
Total94100.00%1100.00%


static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt) { struct arch_timer_cpu *timer; struct kvm_vcpu *vcpu; u64 ns; timer = container_of(hrt, struct arch_timer_cpu, bg_timer); vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu); /* * Check that the timer has really expired from the guest's * PoV (NTP on the host may have forced it to expire * early). If we should have slept longer, restart it. */ ns = kvm_timer_earliest_exp(vcpu); if (unlikely(ns)) { hrtimer_forward_now(hrt, ns_to_ktime(ns)); return HRTIMER_RESTART; } schedule_work(&timer->expired); return HRTIMER_NORESTART; }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier8794.57%233.33%
Christoffer Dall22.17%116.67%
Jintack Lim22.17%233.33%
Bhaktipriya Shridhar11.09%116.67%
Total92100.00%6100.00%


static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt) { struct arch_timer_context *ptimer; struct arch_timer_cpu *timer; struct kvm_vcpu *vcpu; u64 ns; timer = container_of(hrt, struct arch_timer_cpu, phys_timer); vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu); ptimer = vcpu_ptimer(vcpu); /* * Check that the timer has really expired from the guest's * PoV (NTP on the host may have forced it to expire * early). If not ready, schedule for a later time. */ ns = kvm_timer_compute_delta(ptimer); if (unlikely(ns)) { hrtimer_forward_now(hrt, ns_to_ktime(ns)); return HRTIMER_RESTART; } kvm_timer_update_irq(vcpu, true, ptimer); return HRTIMER_NORESTART; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall105100.00%2100.00%
Total105100.00%2100.00%


static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx) { u64 cval, now; if (timer_ctx->loaded) { u32 cnt_ctl; /* Only the virtual timer can be loaded so far */ cnt_ctl = read_sysreg_el0(cntv_ctl); return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) && (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) && !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK); } if (!kvm_timer_irq_can_fire(timer_ctx)) return false; cval = timer_ctx->cnt_cval; now = kvm_phys_timer_read() - timer_ctx->cntvoff; return cval <= now; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall7991.86%457.14%
Jintack Lim66.98%228.57%
Thomas Gleixner11.16%114.29%
Total86100.00%7100.00%


bool kvm_timer_is_pending(struct kvm_vcpu *vcpu) { struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); if (kvm_timer_should_fire(vtimer)) return true; return kvm_timer_should_fire(ptimer); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall46100.00%1100.00%
Total46100.00%1100.00%

/* * Reflect the timer output level into the kvm_run structure */
void kvm_timer_update_run(struct kvm_vcpu *vcpu) { struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); struct kvm_sync_regs *regs = &vcpu->run->s.regs; /* Populate the device bitmap with the timer states */ regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER | KVM_ARM_DEV_EL1_PTIMER); if (kvm_timer_should_fire(vtimer)) regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER; if (kvm_timer_should_fire(ptimer)) regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER; }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf7692.68%150.00%
Christoffer Dall67.32%150.00%
Total82100.00%2100.00%


static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, struct arch_timer_context *timer_ctx) { int ret; timer_ctx->irq.level = new_level; trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq, timer_ctx->irq.level); if (!userspace_irqchip(vcpu->kvm)) { ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, timer_ctx->irq.irq, timer_ctx->irq.level, timer_ctx); WARN_ON(ret); } }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall6772.04%666.67%
Jintack Lim1010.75%111.11%
Alexander Graf1010.75%111.11%
Andre Przywara66.45%111.11%
Total93100.00%9100.00%

/* Schedule the background timer for the emulated timer. */
static void phys_timer_emulate(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); /* * If the timer can fire now we have just raised the IRQ line and we * don't need to have a soft timer scheduled for the future. If the * timer cannot fire at all, then we also don't need a soft timer. */ if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) { soft_timer_cancel(&timer->phys_timer, NULL); return; } soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer)); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall73100.00%2100.00%
Total73100.00%2100.00%

/* * Check if there was a change in the timer state, so that we should either * raise or lower the line level to the GIC or schedule a background timer to * emulate the physical timer. */
static void kvm_timer_update_state(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); bool level; if (unlikely(!timer->enabled)) return; /* * The vtimer virtual interrupt is a 'mapped' interrupt, meaning part * of its lifecycle is offloaded to the hardware, and we therefore may * not have lowered the irq.level value before having to signal a new * interrupt, but have to signal an interrupt every time the level is * asserted. */ level = kvm_timer_should_fire(vtimer); kvm_timer_update_irq(vcpu, level, vtimer); if (kvm_timer_should_fire(ptimer) != ptimer->irq.level) kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer); phys_timer_emulate(vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall5350.00%555.56%
Jintack Lim5047.17%333.33%
Alexander Graf32.83%111.11%
Total106100.00%9100.00%


static void vtimer_save_state(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); unsigned long flags; local_irq_save(flags); if (!vtimer->loaded) goto out; if (timer->enabled) { vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl); vtimer->cnt_cval = read_sysreg_el0(cntv_cval); } /* Disable the virtual timer */ write_sysreg_el0(0, cntv_ctl); isb(); vtimer->loaded = false; out: local_irq_restore(flags); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall102100.00%4100.00%
Total102100.00%4100.00%

/* * Schedule the background timer before calling kvm_vcpu_block, so that this * thread is removed from its waitqueue and made runnable when there's a timer * interrupt to handle. */
void kvm_timer_schedule(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); vtimer_save_state(vcpu); /* * No need to schedule a background timer if any guest timer has * already expired, because kvm_vcpu_block will return before putting * the thread to sleep. */ if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer)) return; /* * If both timers are not capable of raising interrupts (disabled or * masked), then there's no more work for us to do. */ if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer)) return; /* * The guest timers have not yet expired, schedule a background timer. * Set the earliest expiration time among the guest timers. */ soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu)); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall5156.04%457.14%
Jintack Lim3841.76%228.57%
Marc Zyngier22.20%114.29%
Total91100.00%7100.00%


static void vtimer_restore_state(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); unsigned long flags; local_irq_save(flags); if (vtimer->loaded) goto out; if (timer->enabled) { write_sysreg_el0(vtimer->cnt_cval, cntv_cval); isb(); write_sysreg_el0(vtimer->cnt_ctl, cntv_ctl); } vtimer->loaded = true; out: local_irq_restore(flags); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall93100.00%2100.00%
Total93100.00%2100.00%


void kvm_timer_unschedule(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; vtimer_restore_state(vcpu); soft_timer_cancel(&timer->bg_timer, &timer->expired); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall40100.00%4100.00%
Total40100.00%4100.00%


static void set_cntvoff(u64 cntvoff) { u32 low = lower_32_bits(cntvoff); u32 high = upper_32_bits(cntvoff); /* * Since kvm_call_hyp doesn't fully support the ARM PCS especially on * 32-bit systems, but rather passes register by register shifted one * place (we put the function address in r0/x0), we cannot simply pass * a 64-bit value as an argument, but have to split the value in two * 32-bit halves. */ kvm_call_hyp(__kvm_timer_set_cntvoff, low, high); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall35100.00%1100.00%
Total35100.00%1100.00%


static inline void set_vtimer_irq_phys_active(struct kvm_vcpu *vcpu, bool active) { int r; r = irq_set_irqchip_state(host_vtimer_irq, IRQCHIP_STATE_ACTIVE, active); WARN_ON(r); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall3294.12%133.33%
Alexander Graf12.94%133.33%
Marc Zyngier12.94%133.33%
Total34100.00%3100.00%


static void kvm_timer_vcpu_load_gic(struct kvm_vcpu *vcpu) { struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); bool phys_active; if (irqchip_in_kernel(vcpu->kvm)) phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq); else phys_active = vtimer->irq.level; set_vtimer_irq_phys_active(vcpu, phys_active); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall3658.06%337.50%
Marc Zyngier1422.58%112.50%
Jintack Lim69.68%112.50%
Andre Przywara58.06%225.00%
Alexander Graf11.61%112.50%
Total62100.00%8100.00%


static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu) { struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); /* * When using a userspace irqchip with the architected timers and a * host interrupt controller that doesn't support an active state, we * must still prevent continuously exiting from the guest, and * therefore mask the physical interrupt by disabling it on the host * interrupt controller when the virtual level is high, such that the * guest can make forward progress. Once we detect the output level * being de-asserted, we unmask the interrupt again so that we exit * from the guest when the timer fires. */ if (vtimer->irq.level) disable_percpu_irq(host_vtimer_irq); else enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall3376.74%266.67%
Alexander Graf1023.26%133.33%
Total43100.00%3100.00%


void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); if (unlikely(!timer->enabled)) return; if (static_branch_likely(&has_gic_active_state)) kvm_timer_vcpu_load_gic(vcpu); else kvm_timer_vcpu_load_nogic(vcpu); set_cntvoff(vtimer->cntvoff); vtimer_restore_state(vcpu); /* Set the background timer for the physical timer emulation. */ phys_timer_emulate(vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall6986.25%375.00%
Alexander Graf1113.75%125.00%
Total80100.00%4100.00%


bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu) { struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); struct kvm_sync_regs *sregs = &vcpu->run->s.regs; bool vlevel, plevel; if (likely(irqchip_in_kernel(vcpu->kvm))) return false; vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER; plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER; return kvm_timer_should_fire(vtimer) != vlevel || kvm_timer_should_fire(ptimer) != plevel; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall5355.79%266.67%
Alexander Graf4244.21%133.33%
Total95100.00%3100.00%


void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; if (unlikely(!timer->enabled)) return; vtimer_save_state(vcpu); /* * Cancel the physical timer emulation, because the only case where we * need it after a vcpu_put is in the context of a sleeping VCPU, and * in that case we already factor in the deadline for the physical * timer when scheduling the bg_timer. * * In any case, we re-schedule the hrtimer for the physical timer when * coming back to the VCPU thread in kvm_timer_vcpu_load(). */ soft_timer_cancel(&timer->phys_timer, NULL); /* * The kernel may decide to run userspace after calling vcpu_put, so * we reset cntvoff to 0 to ensure a consistent read between user * accesses to the virtual counter and kernel access to the physical * counter of non-VHE case. For VHE, the virtual counter uses a fixed * virtual offset of zero, so no need to zero CNTVOFF_EL2 register. */ if (!has_vhe()) set_cntvoff(0); }

Contributors

PersonTokensPropCommitsCommitProp
Alexander Graf3659.02%125.00%
Christoffer Dall1829.51%250.00%
Shanker Donthineni711.48%125.00%
Total61100.00%4100.00%

/* * With a userspace irqchip we have to check if the guest de-asserted the * timer and if so, unmask the timer irq signal on the host interrupt * controller to ensure that we see future timer signals. */
static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu) { struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); if (!kvm_timer_should_fire(vtimer)) { kvm_timer_update_irq(vcpu, false, vtimer); if (static_branch_likely(&has_gic_active_state)) set_vtimer_irq_phys_active(vcpu, false); else enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); } }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall4876.19%480.00%
Alexander Graf1523.81%120.00%
Total63100.00%5100.00%


void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; if (unlikely(!timer->enabled)) return; if (unlikely(!irqchip_in_kernel(vcpu->kvm))) unmask_vtimer_irq_user(vcpu); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall4180.39%375.00%
Marc Zyngier1019.61%125.00%
Total51100.00%4100.00%


int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); /* * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8 * and to 0 for ARMv7. We provide an implementation that always * resets the timer to be disabled and unmasked and is compliant with * the ARMv7 architecture. */ vtimer->cnt_ctl = 0; ptimer->cnt_ctl = 0; kvm_timer_update_state(vcpu); if (timer->enabled && irqchip_in_kernel(vcpu->kvm)) kvm_vgic_reset_mapped_irq(vcpu, vtimer->irq.irq); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall4855.17%555.56%
Jintack Lim2326.44%222.22%
Anup Patel1416.09%111.11%
Marc Zyngier22.30%111.11%
Total87100.00%9100.00%

/* Make the updates of cntvoff for all vtimer contexts atomic */
static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff) { int i; struct kvm *kvm = vcpu->kvm; struct kvm_vcpu *tmp; mutex_lock(&kvm->lock); kvm_for_each_vcpu(i, tmp, kvm) vcpu_vtimer(tmp)->cntvoff = cntvoff; /* * When called from the vcpu create path, the CPU being created is not * included in the loop above, so we just set it here as well. */ vcpu_vtimer(vcpu)->cntvoff = cntvoff; mutex_unlock(&kvm->lock); }

Contributors

PersonTokensPropCommitsCommitProp
Jintack Lim74100.00%1100.00%
Total74100.00%1100.00%


void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); /* Synchronize cntvoff across all vtimers of a VM. */ update_vtimer_cntvoff(vcpu, kvm_phys_timer_read()); vcpu_ptimer(vcpu)->cntvoff = 0; INIT_WORK(&timer->expired, kvm_timer_inject_irq_work); hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); timer->bg_timer.function = kvm_bg_timer_expire; hrtimer_init(&timer->phys_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); timer->phys_timer.function = kvm_phys_timer_expire; vtimer->irq.irq = default_vtimer_irq.irq; ptimer->irq.irq = default_ptimer_irq.irq; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall6348.46%350.00%
Marc Zyngier4937.69%116.67%
Jintack Lim1813.85%233.33%
Total130100.00%6100.00%


static void kvm_timer_init_interrupt(void *info) { enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier1694.12%266.67%
Anup Patel15.88%133.33%
Total17100.00%3100.00%


int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) { struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); switch (regid) { case KVM_REG_ARM_TIMER_CTL: vtimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT; break; case KVM_REG_ARM_TIMER_CNT: update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value); break; case KVM_REG_ARM_TIMER_CVAL: vtimer->cnt_cval = value; break; case KVM_REG_ARM_PTIMER_CTL: ptimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT; break; case KVM_REG_ARM_PTIMER_CVAL: ptimer->cnt_cval = value; break; default: return -1; } kvm_timer_update_state(vcpu); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andre Przywara6153.04%120.00%
Christoffer Dall4135.65%240.00%
Jintack Lim1311.30%240.00%
Total115100.00%5100.00%


static u64 read_timer_ctl(struct arch_timer_context *timer) { /* * Set ISTATUS bit if it's expired. * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit * regardless of ENABLE bit for our implementation convenience. */ if (!kvm_timer_compute_delta(timer)) return timer->cnt_ctl | ARCH_TIMER_CTRL_IT_STAT; else return timer->cnt_ctl; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall33100.00%1100.00%
Total33100.00%1100.00%


u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid) { struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); switch (regid) { case KVM_REG_ARM_TIMER_CTL: return read_timer_ctl(vtimer); case KVM_REG_ARM_TIMER_CNT: return kvm_phys_timer_read() - vtimer->cntvoff; case KVM_REG_ARM_TIMER_CVAL: return vtimer->cnt_cval; case KVM_REG_ARM_PTIMER_CTL: return read_timer_ctl(ptimer); case KVM_REG_ARM_PTIMER_CVAL: return ptimer->cnt_cval; case KVM_REG_ARM_PTIMER_CNT: return kvm_phys_timer_read(); } return (u64)-1; }

Contributors

PersonTokensPropCommitsCommitProp
Andre Przywara5253.06%125.00%
Christoffer Dall3737.76%125.00%
Jintack Lim99.18%250.00%
Total98100.00%4100.00%


static int kvm_timer_starting_cpu(unsigned int cpu) { kvm_timer_init_interrupt(NULL); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier1266.67%150.00%
Richard Cochran633.33%150.00%
Total18100.00%2100.00%


static int kvm_timer_dying_cpu(unsigned int cpu) { disable_percpu_irq(host_vtimer_irq); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Richard Cochran1055.56%133.33%
Marc Zyngier738.89%133.33%
Anup Patel15.56%133.33%
Total18100.00%3100.00%


int kvm_timer_hyp_init(bool has_gic) { struct arch_timer_kvm_info *info; int err; info = arch_timer_get_kvm_info(); timecounter = &info->timecounter; if (!timecounter->cc) { kvm_err("kvm_arch_timer: uninitialized timecounter\n"); return -ENODEV; } if (info->virtual_irq <= 0) { kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n", info->virtual_irq); return -ENODEV; } host_vtimer_irq = info->virtual_irq; host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq); if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH && host_vtimer_irq_flags != IRQF_TRIGGER_LOW) { kvm_err("Invalid trigger for IRQ%d, assuming level low\n", host_vtimer_irq); host_vtimer_irq_flags = IRQF_TRIGGER_LOW; } err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler, "kvm guest timer", kvm_get_running_vcpus()); if (err) { kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n", host_vtimer_irq, err); return err; } if (has_gic) { err = irq_set_vcpu_affinity(host_vtimer_irq, kvm_get_running_vcpus()); if (err) { kvm_err("kvm_arch_timer: error setting vcpu affinity\n"); goto out_free_irq; } static_branch_enable(&has_gic_active_state); } kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq); cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING, "kvm/arm/timer:starting", kvm_timer_starting_cpu, kvm_timer_dying_cpu); return 0; out_free_irq: free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus()); return err; }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier11053.14%327.27%
Christoffer Dall6129.47%327.27%
Julien Grall2612.56%19.09%
Richard Cochran52.42%19.09%
Paolo Bonzini31.45%19.09%
Thomas Gleixner10.48%19.09%
Ard Biesheuvel10.48%19.09%
Total207100.00%11100.00%


void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); soft_timer_cancel(&timer->bg_timer, &timer->expired); soft_timer_cancel(&timer->phys_timer, NULL); kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq); }

Contributors

PersonTokensPropCommitsCommitProp
Marc Zyngier3350.00%228.57%
Christoffer Dall1928.79%342.86%
Jintack Lim1116.67%114.29%
Andre Przywara34.55%114.29%
Total66100.00%7100.00%


static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu) { int vtimer_irq, ptimer_irq; int i, ret; vtimer_irq = vcpu_vtimer(vcpu)->irq.irq; ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu)); if (ret) return false; ptimer_irq = vcpu_ptimer(vcpu)->irq.irq; ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu)); if (ret) return false; kvm_for_each_vcpu(i, vcpu, vcpu->kvm) { if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq || vcpu_ptimer(vcpu)->irq.irq != ptimer_irq) return false; } return true; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall125100.00%2100.00%
Total125100.00%2100.00%


bool kvm_arch_timer_get_input_level(int vintid) { struct kvm_vcpu *vcpu = kvm_arm_get_running_vcpu(); struct arch_timer_context *timer; if (vintid == vcpu_vtimer(vcpu)->irq.irq) timer = vcpu_vtimer(vcpu); else BUG(); /* We only map the vtimer so far */ return kvm_timer_should_fire(timer); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall52100.00%1100.00%
Total52100.00%1100.00%


int kvm_timer_enable(struct kvm_vcpu *vcpu) { struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); int ret; if (timer->enabled) return 0; /* Without a VGIC we do not map virtual IRQs to physical IRQs */ if (!irqchip_in_kernel(vcpu->kvm)) goto no_vgic; if (!vgic_initialized(vcpu->kvm)) return -ENODEV; if (!timer_irqs_are_valid(vcpu)) { kvm_debug("incorrectly configured timer irqs\n"); return -EINVAL; } ret = kvm_vgic_map_phys_irq(vcpu, host_vtimer_irq, vtimer->irq.irq, kvm_arch_timer_get_input_level); if (ret) return ret; no_vgic: timer->enabled = 1; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall7660.32%555.56%
Alexander Graf3023.81%111.11%
Jintack Lim107.94%111.11%
Marc Zyngier86.35%111.11%
Eric Auger21.59%111.11%
Total126100.00%9100.00%

/* * On VHE system, we only need to configure trap on physical timer and counter * accesses in EL0 and EL1 once, not for every world switch. * The host kernel runs at EL2 with HCR_EL2.TGE == 1, * and this makes those bits have no effect for the host kernel execution. */
void kvm_timer_init_vhe(void) { /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */ u32 cnthctl_shift = 10; u64 val; /* * Disallow physical timer access for the guest. * Physical counter access is allowed. */ val = read_sysreg(cnthctl_el2); val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift); val |= (CNTHCTL_EL1PCTEN << cnthctl_shift); write_sysreg(val, cnthctl_el2); }

Contributors

PersonTokensPropCommitsCommitProp
Jintack Lim48100.00%1100.00%
Total48100.00%1100.00%


static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq) { struct kvm_vcpu *vcpu; int i; kvm_for_each_vcpu(i, vcpu, kvm) { vcpu_vtimer(vcpu)->irq.irq = vtimer_irq; vcpu_ptimer(vcpu)->irq.irq = ptimer_irq; } }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall57100.00%1100.00%
Total57100.00%1100.00%


int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) { int __user *uaddr = (int __user *)(long)attr->addr; struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); int irq; if (!irqchip_in_kernel(vcpu->kvm)) return -EINVAL; if (get_user(irq, uaddr)) return -EFAULT; if (!(irq_is_ppi(irq))) return -EINVAL; if (vcpu->arch.timer_cpu.enabled) return -EBUSY; switch (attr->attr) { case KVM_ARM_VCPU_TIMER_IRQ_VTIMER: set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq); break; case KVM_ARM_VCPU_TIMER_IRQ_PTIMER: set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq); break; default: return -ENXIO; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall164100.00%1100.00%
Total164100.00%1100.00%


int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) { int __user *uaddr = (int __user *)(long)attr->addr; struct arch_timer_context *timer; int irq; switch (attr->attr) { case KVM_ARM_VCPU_TIMER_IRQ_VTIMER: timer = vcpu_vtimer(vcpu); break; case KVM_ARM_VCPU_TIMER_IRQ_PTIMER: timer = vcpu_ptimer(vcpu); break; default: return -ENXIO; } irq = timer->irq.irq; return put_user(irq, uaddr); }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall91100.00%1100.00%
Total91100.00%1100.00%


int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) { switch (attr->attr) { case KVM_ARM_VCPU_TIMER_IRQ_VTIMER: case KVM_ARM_VCPU_TIMER_IRQ_PTIMER: return 0; } return -ENXIO; }

Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall36100.00%1100.00%
Total36100.00%1100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Christoffer Dall217459.38%3553.85%
Marc Zyngier58215.90%710.77%
Jintack Lim45412.40%710.77%
Alexander Graf2366.45%11.54%
Andre Przywara1273.47%34.62%
Julien Grall260.71%11.54%
Richard Cochran210.57%11.54%
Anup Patel190.52%11.54%
Shanker Donthineni70.19%11.54%
Thomas Gleixner40.11%23.08%
Paolo Bonzini30.08%11.54%
Mark Rutland30.08%11.54%
Eric Auger20.05%11.54%
Ard Biesheuvel10.03%11.54%
Andrew Jones10.03%11.54%
Bhaktipriya Shridhar10.03%11.54%
Total3661100.00%65100.00%
Directory: virt/kvm/arm
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.