| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) 2022 SiFive |
| 4 |
* |
| 5 |
* Authors: |
| 6 |
* Vincent Chen <[email protected]> |
| 7 |
* Greentime Hu <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/errno.h> |
| 11 |
#include <linux/err.h> |
| 12 |
#include <linux/kvm_host.h> |
| 13 |
#include <linux/uaccess.h> |
| 14 |
#include <asm/cpufeature.h> |
| 15 |
#include <asm/kvm_vcpu_vector.h> |
| 16 |
#include <asm/vector.h> |
| 17 |
|
| 18 |
#ifdef CONFIG_RISCV_ISA_V |
| 19 |
void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu) |
| 20 |
{ |
| 21 |
unsigned long *isa = vcpu->arch.isa; |
| 22 |
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; |
| 23 |
|
| 24 |
cntx->sstatus &= ~SR_VS; |
| 25 |
|
| 26 |
cntx->vector.vlenb = riscv_v_vsize / 32; |
| 27 |
|
| 28 |
if (riscv_isa_extension_available(isa, v)) { |
| 29 |
cntx->sstatus |= SR_VS_INITIAL; |
| 30 |
WARN_ON(!cntx->vector.datap); |
| 31 |
memset(cntx->vector.datap, 0, riscv_v_vsize); |
| 32 |
} else { |
| 33 |
cntx->sstatus |= SR_VS_OFF; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
static void kvm_riscv_vcpu_vector_clean(struct kvm_cpu_context *cntx) |
| 38 |
{ |
| 39 |
cntx->sstatus &= ~SR_VS; |
| 40 |
cntx->sstatus |= SR_VS_CLEAN; |
| 41 |
} |
| 42 |
|
| 43 |
void kvm_riscv_vcpu_guest_vector_save(struct kvm_cpu_context *cntx, |
| 44 |
unsigned long *isa) |
| 45 |
{ |
| 46 |
if ((cntx->sstatus & SR_VS) == SR_VS_DIRTY) { |
| 47 |
if (riscv_isa_extension_available(isa, v)) |
| 48 |
__kvm_riscv_vector_save(cntx); |
| 49 |
kvm_riscv_vcpu_vector_clean(cntx); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, |
| 54 |
unsigned long *isa) |
| 55 |
{ |
| 56 |
if ((cntx->sstatus & SR_VS) != SR_VS_OFF) { |
| 57 |
if (riscv_isa_extension_available(isa, v)) |
| 58 |
__kvm_riscv_vector_restore(cntx); |
| 59 |
kvm_riscv_vcpu_vector_clean(cntx); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx) |
| 64 |
{ |
| 65 |
/* No need to check host sstatus as it can be modified outside */ |
| 66 |
if (riscv_isa_extension_available(NULL, v)) |
| 67 |
__kvm_riscv_vector_save(cntx); |
| 68 |
} |
| 69 |
|
| 70 |
void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx) |
| 71 |
{ |
| 72 |
if (riscv_isa_extension_available(NULL, v)) |
| 73 |
__kvm_riscv_vector_restore(cntx); |
| 74 |
} |
| 75 |
|
| 76 |
int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu) |
| 77 |
{ |
| 78 |
vcpu->arch.guest_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL); |
| 79 |
if (!vcpu->arch.guest_context.vector.datap) |
| 80 |
return -ENOMEM; |
| 81 |
|
| 82 |
vcpu->arch.host_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL); |
| 83 |
if (!vcpu->arch.host_context.vector.datap) { |
| 84 |
kfree(vcpu->arch.guest_context.vector.datap); |
| 85 |
vcpu->arch.guest_context.vector.datap = NULL; |
| 86 |
return -ENOMEM; |
| 87 |
} |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu) |
| 93 |
{ |
| 94 |
kfree(vcpu->arch.guest_context.vector.datap); |
| 95 |
kfree(vcpu->arch.host_context.vector.datap); |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
static int kvm_riscv_vcpu_vreg_addr(struct kvm_vcpu *vcpu, |
| 100 |
unsigned long reg_num, |
| 101 |
size_t reg_size, |
| 102 |
void **reg_addr) |
| 103 |
{ |
| 104 |
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; |
| 105 |
size_t vlenb = riscv_v_vsize / 32; |
| 106 |
|
| 107 |
if (reg_num < KVM_REG_RISCV_VECTOR_REG(0)) { |
| 108 |
if (reg_size != sizeof(unsigned long)) |
| 109 |
return -EINVAL; |
| 110 |
switch (reg_num) { |
| 111 |
case KVM_REG_RISCV_VECTOR_CSR_REG(vstart): |
| 112 |
*reg_addr = &cntx->vector.vstart; |
| 113 |
break; |
| 114 |
case KVM_REG_RISCV_VECTOR_CSR_REG(vl): |
| 115 |
*reg_addr = &cntx->vector.vl; |
| 116 |
break; |
| 117 |
case KVM_REG_RISCV_VECTOR_CSR_REG(vtype): |
| 118 |
*reg_addr = &cntx->vector.vtype; |
| 119 |
break; |
| 120 |
case KVM_REG_RISCV_VECTOR_CSR_REG(vcsr): |
| 121 |
*reg_addr = &cntx->vector.vcsr; |
| 122 |
break; |
| 123 |
case KVM_REG_RISCV_VECTOR_CSR_REG(vlenb): |
| 124 |
*reg_addr = &cntx->vector.vlenb; |
| 125 |
break; |
| 126 |
case KVM_REG_RISCV_VECTOR_CSR_REG(datap): |
| 127 |
default: |
| 128 |
return -ENOENT; |
| 129 |
} |
| 130 |
} else if (reg_num <= KVM_REG_RISCV_VECTOR_REG(31)) { |
| 131 |
if (reg_size != vlenb) |
| 132 |
return -EINVAL; |
| 133 |
*reg_addr = cntx->vector.datap + |
| 134 |
(reg_num - KVM_REG_RISCV_VECTOR_REG(0)) * vlenb; |
| 135 |
} else { |
| 136 |
return -ENOENT; |
| 137 |
} |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
int kvm_riscv_vcpu_get_reg_vector(struct kvm_vcpu *vcpu, |
| 143 |
const struct kvm_one_reg *reg) |
| 144 |
{ |
| 145 |
unsigned long *isa = vcpu->arch.isa; |
| 146 |
unsigned long __user *uaddr = |
| 147 |
(unsigned long __user *)(unsigned long)reg->addr; |
| 148 |
unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | |
| 149 |
KVM_REG_SIZE_MASK | |
| 150 |
KVM_REG_RISCV_VECTOR); |
| 151 |
size_t reg_size = KVM_REG_SIZE(reg->id); |
| 152 |
void *reg_addr; |
| 153 |
int rc; |
| 154 |
|
| 155 |
if (!riscv_isa_extension_available(isa, v)) |
| 156 |
return -ENOENT; |
| 157 |
|
| 158 |
rc = kvm_riscv_vcpu_vreg_addr(vcpu, reg_num, reg_size, ®_addr); |
| 159 |
if (rc) |
| 160 |
return rc; |
| 161 |
|
| 162 |
if (copy_to_user(uaddr, reg_addr, reg_size)) |
| 163 |
return -EFAULT; |
| 164 |
|
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu, |
| 169 |
const struct kvm_one_reg *reg) |
| 170 |
{ |
| 171 |
unsigned long *isa = vcpu->arch.isa; |
| 172 |
unsigned long __user *uaddr = |
| 173 |
(unsigned long __user *)(unsigned long)reg->addr; |
| 174 |
unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | |
| 175 |
KVM_REG_SIZE_MASK | |
| 176 |
KVM_REG_RISCV_VECTOR); |
| 177 |
size_t reg_size = KVM_REG_SIZE(reg->id); |
| 178 |
void *reg_addr; |
| 179 |
int rc; |
| 180 |
|
| 181 |
if (!riscv_isa_extension_available(isa, v)) |
| 182 |
return -ENOENT; |
| 183 |
|
| 184 |
if (reg_num == KVM_REG_RISCV_VECTOR_CSR_REG(vlenb)) { |
| 185 |
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; |
| 186 |
unsigned long reg_val; |
| 187 |
|
| 188 |
if (reg_size != sizeof(reg_val)) |
| 189 |
return -EINVAL; |
| 190 |
if (copy_from_user(®_val, uaddr, reg_size)) |
| 191 |
return -EFAULT; |
| 192 |
if (reg_val != cntx->vector.vlenb) |
| 193 |
return -EINVAL; |
| 194 |
|
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
rc = kvm_riscv_vcpu_vreg_addr(vcpu, reg_num, reg_size, ®_addr); |
| 199 |
if (rc) |
| 200 |
return rc; |
| 201 |
|
| 202 |
if (copy_from_user(reg_addr, uaddr, reg_size)) |
| 203 |
return -EFAULT; |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |