arch/riscv/kvm/vcpu_timer.c
Source file repositories/reference/linux-study-clean/arch/riscv/kvm/vcpu_timer.c
File Facts
- System
- Linux kernel
- Corpus path
arch/riscv/kvm/vcpu_timer.c- Extension
.c- Size
- 9842 bytes
- Lines
- 384
- Domain
- Architecture Layer
- Bucket
- arch/riscv
- Inferred role
- Architecture Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/errno.hlinux/err.hlinux/kvm_host.hlinux/uaccess.hclocksource/timer-riscv.hasm/delay.hasm/kvm_isa.hasm/kvm_nacl.hasm/kvm_vcpu_timer.h
Detected Declarations
function Copyrightfunction kvm_riscv_delta_cycles2nsfunction kvm_riscv_vcpu_hrtimer_expiredfunction kvm_riscv_vcpu_timer_cancelfunction kvm_riscv_vcpu_update_vstimecmpfunction kvm_riscv_vcpu_update_hrtimerfunction kvm_riscv_vcpu_timer_next_eventfunction kvm_riscv_vcpu_vstimer_expiredfunction kvm_riscv_vcpu_timer_pendingfunction kvm_riscv_vcpu_timer_blockingfunction kvm_riscv_vcpu_timer_unblockingfunction kvm_riscv_vcpu_get_reg_timerfunction kvm_riscv_vcpu_set_reg_timerfunction kvm_riscv_vcpu_timer_initfunction kvm_riscv_vcpu_timer_deinitfunction kvm_riscv_vcpu_timer_resetfunction kvm_riscv_vcpu_update_timedeltafunction kvm_riscv_vcpu_timer_restorefunction kvm_riscv_vcpu_timer_syncfunction kvm_riscv_vcpu_timer_savefunction kvm_riscv_guest_timer_init
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2019 Western Digital Corporation or its affiliates.
*
* Authors:
* Atish Patra <atish.patra@wdc.com>
*/
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/kvm_host.h>
#include <linux/uaccess.h>
#include <clocksource/timer-riscv.h>
#include <asm/delay.h>
#include <asm/kvm_isa.h>
#include <asm/kvm_nacl.h>
#include <asm/kvm_vcpu_timer.h>
static u64 kvm_riscv_current_cycles(struct kvm_guest_timer *gt)
{
return get_cycles64() + gt->time_delta;
}
static u64 kvm_riscv_delta_cycles2ns(u64 cycles,
struct kvm_guest_timer *gt,
struct kvm_vcpu_timer *t)
{
unsigned long flags;
u64 cycles_now, cycles_delta, delta_ns;
local_irq_save(flags);
cycles_now = kvm_riscv_current_cycles(gt);
if (cycles_now < cycles)
cycles_delta = cycles - cycles_now;
else
cycles_delta = 0;
delta_ns = (cycles_delta * gt->nsec_mult) >> gt->nsec_shift;
local_irq_restore(flags);
return delta_ns;
}
static enum hrtimer_restart kvm_riscv_vcpu_hrtimer_expired(struct hrtimer *h)
{
u64 delta_ns;
struct kvm_vcpu_timer *t = container_of(h, struct kvm_vcpu_timer, hrt);
struct kvm_vcpu *vcpu = container_of(t, struct kvm_vcpu, arch.timer);
struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
if (kvm_riscv_current_cycles(gt) < t->next_cycles) {
delta_ns = kvm_riscv_delta_cycles2ns(t->next_cycles, gt, t);
hrtimer_forward_now(&t->hrt, ktime_set(0, delta_ns));
return HRTIMER_RESTART;
}
t->next_set = false;
kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_VS_TIMER);
return HRTIMER_NORESTART;
}
static int kvm_riscv_vcpu_timer_cancel(struct kvm_vcpu_timer *t)
{
if (!t->init_done || !t->next_set)
return -EINVAL;
hrtimer_cancel(&t->hrt);
t->next_set = false;
return 0;
}
static int kvm_riscv_vcpu_update_vstimecmp(struct kvm_vcpu *vcpu, u64 ncycles)
{
#if defined(CONFIG_32BIT)
ncsr_write(CSR_VSTIMECMP, ULONG_MAX);
ncsr_write(CSR_VSTIMECMPH, ncycles >> 32);
ncsr_write(CSR_VSTIMECMP, (u32)ncycles);
#else
ncsr_write(CSR_VSTIMECMP, ncycles);
#endif
return 0;
}
static int kvm_riscv_vcpu_update_hrtimer(struct kvm_vcpu *vcpu, u64 ncycles)
{
struct kvm_vcpu_timer *t = &vcpu->arch.timer;
struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer;
u64 delta_ns;
Annotation
- Immediate include surface: `linux/errno.h`, `linux/err.h`, `linux/kvm_host.h`, `linux/uaccess.h`, `clocksource/timer-riscv.h`, `asm/delay.h`, `asm/kvm_isa.h`, `asm/kvm_nacl.h`.
- Detected declarations: `function Copyright`, `function kvm_riscv_delta_cycles2ns`, `function kvm_riscv_vcpu_hrtimer_expired`, `function kvm_riscv_vcpu_timer_cancel`, `function kvm_riscv_vcpu_update_vstimecmp`, `function kvm_riscv_vcpu_update_hrtimer`, `function kvm_riscv_vcpu_timer_next_event`, `function kvm_riscv_vcpu_vstimer_expired`, `function kvm_riscv_vcpu_timer_pending`, `function kvm_riscv_vcpu_timer_blocking`.
- Atlas domain: Architecture Layer / arch/riscv.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.