| 1 |
/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 |
/* |
| 3 |
* Copyright (C) 2019 Western Digital Corporation or its affiliates. |
| 4 |
* |
| 5 |
* Authors: |
| 6 |
* Anup Patel <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef __RISCV_KVM_HOST_H__ |
| 10 |
#define __RISCV_KVM_HOST_H__ |
| 11 |
|
| 12 |
#include <linux/types.h> |
| 13 |
#include <linux/kvm.h> |
| 14 |
#include <linux/kvm_types.h> |
| 15 |
#include <linux/spinlock.h> |
| 16 |
#include <asm/hwcap.h> |
| 17 |
#include <asm/kvm_aia.h> |
| 18 |
#include <asm/ptrace.h> |
| 19 |
#include <asm/kvm_tlb.h> |
| 20 |
#include <asm/kvm_vmid.h> |
| 21 |
#include <asm/kvm_vcpu_fp.h> |
| 22 |
#include <asm/kvm_vcpu_insn.h> |
| 23 |
#include <asm/kvm_vcpu_sbi.h> |
| 24 |
#include <asm/kvm_vcpu_sbi_fwft.h> |
| 25 |
#include <asm/kvm_vcpu_timer.h> |
| 26 |
#include <asm/kvm_vcpu_pmu.h> |
| 27 |
|
| 28 |
#define KVM_MAX_VCPUS 1024 |
| 29 |
|
| 30 |
#define KVM_HALT_POLL_NS_DEFAULT 500000 |
| 31 |
|
| 32 |
#define KVM_VCPU_MAX_FEATURES 0 |
| 33 |
|
| 34 |
#define KVM_IRQCHIP_NUM_PINS 1024 |
| 35 |
|
| 36 |
#define KVM_REQ_SLEEP \ |
| 37 |
KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) |
| 38 |
#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(1) |
| 39 |
#define KVM_REQ_UPDATE_HGATP KVM_ARCH_REQ(2) |
| 40 |
#define KVM_REQ_FENCE_I \ |
| 41 |
KVM_ARCH_REQ_FLAGS(3, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) |
| 42 |
#define KVM_REQ_HFENCE_VVMA_ALL \ |
| 43 |
KVM_ARCH_REQ_FLAGS(4, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) |
| 44 |
#define KVM_REQ_HFENCE \ |
| 45 |
KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) |
| 46 |
#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6) |
| 47 |
|
| 48 |
#define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE |
| 49 |
|
| 50 |
#define KVM_HEDELEG_DEFAULT (BIT(EXC_INST_MISALIGNED) | \ |
| 51 |
BIT(EXC_INST_ILLEGAL) | \ |
| 52 |
BIT(EXC_BREAKPOINT) | \ |
| 53 |
BIT(EXC_SYSCALL) | \ |
| 54 |
BIT(EXC_INST_PAGE_FAULT) | \ |
| 55 |
BIT(EXC_LOAD_PAGE_FAULT) | \ |
| 56 |
BIT(EXC_STORE_PAGE_FAULT)) |
| 57 |
|
| 58 |
#define KVM_HIDELEG_DEFAULT (BIT(IRQ_VS_SOFT) | \ |
| 59 |
BIT(IRQ_VS_TIMER) | \ |
| 60 |
BIT(IRQ_VS_EXT)) |
| 61 |
|
| 62 |
#define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ |
| 63 |
KVM_DIRTY_LOG_INITIALLY_SET) |
| 64 |
|
| 65 |
struct kvm_vm_stat { |
| 66 |
struct kvm_vm_stat_generic generic; |
| 67 |
}; |
| 68 |
|
| 69 |
struct kvm_vcpu_stat { |
| 70 |
struct kvm_vcpu_stat_generic generic; |
| 71 |
u64 ecall_exit_stat; |
| 72 |
u64 wfi_exit_stat; |
| 73 |
u64 wrs_exit_stat; |
| 74 |
u64 mmio_exit_user; |
| 75 |
u64 mmio_exit_kernel; |
| 76 |
u64 csr_exit_user; |
| 77 |
u64 csr_exit_kernel; |
| 78 |
u64 signal_exits; |
| 79 |
u64 exits; |
| 80 |
u64 instr_illegal_exits; |
| 81 |
u64 load_misaligned_exits; |
| 82 |
u64 store_misaligned_exits; |
| 83 |
u64 load_access_exits; |
| 84 |
u64 store_access_exits; |
| 85 |
}; |
| 86 |
|
| 87 |
struct kvm_arch_memory_slot { |
| 88 |
}; |
| 89 |
|
| 90 |
struct kvm_arch { |
| 91 |
/* G-stage vmid */ |
| 92 |
struct kvm_vmid vmid; |
| 93 |
|
| 94 |
/* G-stage page table */ |
| 95 |
pgd_t *pgd; |
| 96 |
phys_addr_t pgd_phys; |
| 97 |
|
| 98 |
/* Guest Timer */ |
| 99 |
struct kvm_guest_timer timer; |
| 100 |
|
| 101 |
/* AIA Guest/VM context */ |
| 102 |
struct kvm_aia aia; |
| 103 |
|
| 104 |
/* KVM_CAP_RISCV_MP_STATE_RESET */ |
| 105 |
bool mp_state_reset; |
| 106 |
}; |
| 107 |
|
| 108 |
struct kvm_cpu_trap { |
| 109 |
unsigned long sepc; |
| 110 |
unsigned long scause; |
| 111 |
unsigned long stval; |
| 112 |
unsigned long htval; |
| 113 |
unsigned long htinst; |
| 114 |
}; |
| 115 |
|
| 116 |
struct kvm_cpu_context { |
| 117 |
unsigned long zero; |
| 118 |
unsigned long ra; |
| 119 |
unsigned long sp; |
| 120 |
unsigned long gp; |
| 121 |
unsigned long tp; |
| 122 |
unsigned long t0; |
| 123 |
unsigned long t1; |
| 124 |
unsigned long t2; |
| 125 |
unsigned long s0; |
| 126 |
unsigned long s1; |
| 127 |
unsigned long a0; |
| 128 |
unsigned long a1; |
| 129 |
unsigned long a2; |
| 130 |
unsigned long a3; |
| 131 |
unsigned long a4; |
| 132 |
unsigned long a5; |
| 133 |
unsigned long a6; |
| 134 |
unsigned long a7; |
| 135 |
unsigned long s2; |
| 136 |
unsigned long s3; |
| 137 |
unsigned long s4; |
| 138 |
unsigned long s5; |
| 139 |
unsigned long s6; |
| 140 |
unsigned long s7; |
| 141 |
unsigned long s8; |
| 142 |
unsigned long s9; |
| 143 |
unsigned long s10; |
| 144 |
unsigned long s11; |
| 145 |
unsigned long t3; |
| 146 |
unsigned long t4; |
| 147 |
unsigned long t5; |
| 148 |
unsigned long t6; |
| 149 |
unsigned long sepc; |
| 150 |
unsigned long sstatus; |
| 151 |
unsigned long hstatus; |
| 152 |
union __riscv_fp_state fp; |
| 153 |
struct __riscv_v_ext_state vector; |
| 154 |
}; |
| 155 |
|
| 156 |
struct kvm_vcpu_csr { |
| 157 |
unsigned long vsstatus; |
| 158 |
unsigned long vsie; |
| 159 |
unsigned long vstvec; |
| 160 |
unsigned long vsscratch; |
| 161 |
unsigned long vsepc; |
| 162 |
unsigned long vscause; |
| 163 |
unsigned long vstval; |
| 164 |
unsigned long hvip; |
| 165 |
unsigned long vsatp; |
| 166 |
unsigned long scounteren; |
| 167 |
unsigned long senvcfg; |
| 168 |
}; |
| 169 |
|
| 170 |
struct kvm_vcpu_config { |
| 171 |
u64 henvcfg; |
| 172 |
u64 hstateen0; |
| 173 |
unsigned long hedeleg; |
| 174 |
}; |
| 175 |
|
| 176 |
struct kvm_vcpu_smstateen_csr { |
| 177 |
unsigned long sstateen0; |
| 178 |
}; |
| 179 |
|
| 180 |
struct kvm_vcpu_reset_state { |
| 181 |
spinlock_t lock; |
| 182 |
unsigned long pc; |
| 183 |
unsigned long a1; |
| 184 |
}; |
| 185 |
|
| 186 |
struct kvm_vcpu_arch { |
| 187 |
/* VCPU ran at least once */ |
| 188 |
bool ran_atleast_once; |
| 189 |
|
| 190 |
/* Last Host CPU on which Guest VCPU exited */ |
| 191 |
int last_exit_cpu; |
| 192 |
|
| 193 |
/* ISA feature bits (similar to MISA) */ |
| 194 |
DECLARE_BITMAP(isa, RISCV_ISA_EXT_MAX); |
| 195 |
|
| 196 |
/* Vendor, Arch, and Implementation details */ |
| 197 |
unsigned long mvendorid; |
| 198 |
unsigned long marchid; |
| 199 |
unsigned long mimpid; |
| 200 |
|
| 201 |
/* SSCRATCH, STVEC, and SCOUNTEREN of Host */ |
| 202 |
unsigned long host_sscratch; |
| 203 |
unsigned long host_stvec; |
| 204 |
unsigned long host_scounteren; |
| 205 |
unsigned long host_senvcfg; |
| 206 |
unsigned long host_sstateen0; |
| 207 |
|
| 208 |
/* CPU context of Host */ |
| 209 |
struct kvm_cpu_context host_context; |
| 210 |
|
| 211 |
/* CPU context of Guest VCPU */ |
| 212 |
struct kvm_cpu_context guest_context; |
| 213 |
|
| 214 |
/* CPU CSR context of Guest VCPU */ |
| 215 |
struct kvm_vcpu_csr guest_csr; |
| 216 |
|
| 217 |
/* CPU Smstateen CSR context of Guest VCPU */ |
| 218 |
struct kvm_vcpu_smstateen_csr smstateen_csr; |
| 219 |
|
| 220 |
/* CPU reset state of Guest VCPU */ |
| 221 |
struct kvm_vcpu_reset_state reset_state; |
| 222 |
|
| 223 |
/* |
| 224 |
* VCPU interrupts |
| 225 |
* |
| 226 |
* We have a lockless approach for tracking pending VCPU interrupts |
| 227 |
* implemented using atomic bitops. The irqs_pending bitmap represent |
| 228 |
* pending interrupts whereas irqs_pending_mask represent bits changed |
| 229 |
* in irqs_pending. Our approach is modeled around multiple producer |
| 230 |
* and single consumer problem where the consumer is the VCPU itself. |
| 231 |
*/ |
| 232 |
#define KVM_RISCV_VCPU_NR_IRQS 64 |
| 233 |
DECLARE_BITMAP(irqs_pending, KVM_RISCV_VCPU_NR_IRQS); |
| 234 |
DECLARE_BITMAP(irqs_pending_mask, KVM_RISCV_VCPU_NR_IRQS); |
| 235 |
|
| 236 |
/* VCPU Timer */ |
| 237 |
struct kvm_vcpu_timer timer; |
| 238 |
|
| 239 |
/* HFENCE request queue */ |
| 240 |
spinlock_t hfence_lock; |
| 241 |
unsigned long hfence_head; |
| 242 |
unsigned long hfence_tail; |
| 243 |
struct kvm_riscv_hfence hfence_queue[KVM_RISCV_VCPU_MAX_HFENCE]; |
| 244 |
|
| 245 |
/* MMIO instruction details */ |
| 246 |
struct kvm_mmio_decode mmio_decode; |
| 247 |
|
| 248 |
/* CSR instruction details */ |
| 249 |
struct kvm_csr_decode csr_decode; |
| 250 |
|
| 251 |
/* SBI context */ |
| 252 |
struct kvm_vcpu_sbi_context sbi_context; |
| 253 |
|
| 254 |
/* AIA VCPU context */ |
| 255 |
struct kvm_vcpu_aia aia_context; |
| 256 |
|
| 257 |
/* Cache pages needed to program page tables with spinlock held */ |
| 258 |
struct kvm_mmu_memory_cache mmu_page_cache; |
| 259 |
|
| 260 |
/* VCPU power state */ |
| 261 |
struct kvm_mp_state mp_state; |
| 262 |
spinlock_t mp_state_lock; |
| 263 |
|
| 264 |
/* Don't run the VCPU (blocked) */ |
| 265 |
bool pause; |
| 266 |
|
| 267 |
/* Performance monitoring context */ |
| 268 |
struct kvm_pmu pmu_context; |
| 269 |
|
| 270 |
/* Firmware feature SBI extension context */ |
| 271 |
struct kvm_sbi_fwft fwft_context; |
| 272 |
|
| 273 |
/* 'static' configurations which are set only once */ |
| 274 |
struct kvm_vcpu_config cfg; |
| 275 |
|
| 276 |
/* SBI steal-time accounting */ |
| 277 |
struct { |
| 278 |
gpa_t shmem; |
| 279 |
u64 last_steal; |
| 280 |
} sta; |
| 281 |
}; |
| 282 |
|
| 283 |
/* |
| 284 |
* Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, |
| 285 |
* arrived in guest context. For riscv, any event that arrives while a vCPU is |
| 286 |
* loaded is considered to be "in guest". |
| 287 |
*/ |
| 288 |
static inline bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu) |
| 289 |
{ |
| 290 |
return IS_ENABLED(CONFIG_GUEST_PERF_EVENTS) && !!vcpu; |
| 291 |
} |
| 292 |
|
| 293 |
static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {} |
| 294 |
static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {} |
| 295 |
|
| 296 |
int kvm_riscv_setup_default_irq_routing(struct kvm *kvm, u32 lines); |
| 297 |
|
| 298 |
void __kvm_riscv_unpriv_trap(void); |
| 299 |
|
| 300 |
unsigned long kvm_riscv_vcpu_unpriv_read(struct kvm_vcpu *vcpu, |
| 301 |
bool read_insn, |
| 302 |
unsigned long guest_addr, |
| 303 |
struct kvm_cpu_trap *trap); |
| 304 |
void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, |
| 305 |
struct kvm_cpu_trap *trap); |
| 306 |
int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, |
| 307 |
struct kvm_cpu_trap *trap); |
| 308 |
|
| 309 |
void __kvm_riscv_switch_to(struct kvm_vcpu_arch *vcpu_arch); |
| 310 |
|
| 311 |
void kvm_riscv_vcpu_setup_isa(struct kvm_vcpu *vcpu); |
| 312 |
unsigned long kvm_riscv_vcpu_num_regs(struct kvm_vcpu *vcpu); |
| 313 |
int kvm_riscv_vcpu_copy_reg_indices(struct kvm_vcpu *vcpu, |
| 314 |
u64 __user *uindices); |
| 315 |
int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, |
| 316 |
const struct kvm_one_reg *reg); |
| 317 |
int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, |
| 318 |
const struct kvm_one_reg *reg); |
| 319 |
|
| 320 |
int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq); |
| 321 |
int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq); |
| 322 |
void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu); |
| 323 |
void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu); |
| 324 |
bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, u64 mask); |
| 325 |
void __kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu); |
| 326 |
void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu); |
| 327 |
void __kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu); |
| 328 |
void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu); |
| 329 |
bool kvm_riscv_vcpu_stopped(struct kvm_vcpu *vcpu); |
| 330 |
|
| 331 |
void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu); |
| 332 |
|
| 333 |
/* Flags representing implementation specific details */ |
| 334 |
DECLARE_STATIC_KEY_FALSE(kvm_riscv_vsstage_tlb_no_gpa); |
| 335 |
|
| 336 |
#endif /* __RISCV_KVM_HOST_H__ */ |