#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <machine/sbi.h>
#include "riscv.h"
#include "vmm_fence.h"
static int
vmm_sbi_handle_rfnc(struct vcpu *vcpu, struct hypctx *hypctx)
{
struct vmm_fence fence;
cpuset_t active_cpus;
uint64_t hart_mask;
uint64_t hart_mask_base;
uint64_t func_id;
struct hyp *hyp;
uint16_t maxcpus;
cpuset_t cpus;
int i;
func_id = hypctx->guest_regs.hyp_a[6];
hart_mask = hypctx->guest_regs.hyp_a[0];
hart_mask_base = hypctx->guest_regs.hyp_a[1];
fence.start = hypctx->guest_regs.hyp_a[2];
fence.size = hypctx->guest_regs.hyp_a[3];
fence.asid = hypctx->guest_regs.hyp_a[4];
switch (func_id) {
case SBI_RFNC_REMOTE_FENCE_I:
fence.type = VMM_RISCV_FENCE_I;
break;
case SBI_RFNC_REMOTE_SFENCE_VMA:
fence.type = VMM_RISCV_FENCE_VMA;
break;
case SBI_RFNC_REMOTE_SFENCE_VMA_ASID:
fence.type = VMM_RISCV_FENCE_VMA_ASID;
break;
default:
return (SBI_ERR_NOT_SUPPORTED);
}
CPU_ZERO(&cpus);
hyp = hypctx->hyp;
active_cpus = vm_active_cpus(hyp->vm);
maxcpus = vm_get_maxcpus(hyp->vm);
for (i = 0; i < maxcpus; i++) {
vcpu = vm_vcpu(hyp->vm, i);
if (vcpu == NULL)
continue;
if (hart_mask_base != -1UL) {
if (i < hart_mask_base)
continue;
if (!(hart_mask & (1UL << (i - hart_mask_base))))
continue;
}
if (!CPU_ISSET(i, &active_cpus))
return (SBI_ERR_INVALID_PARAM);
CPU_SET(i, &cpus);
}
if (CPU_EMPTY(&cpus))
return (SBI_ERR_INVALID_PARAM);
vmm_fence_add(hyp->vm, &cpus, &fence);
return (SBI_SUCCESS);
}
static int
vmm_sbi_handle_time(struct vcpu *vcpu, struct hypctx *hypctx)
{
uint64_t func_id;
uint64_t next_val;
func_id = hypctx->guest_regs.hyp_a[6];
next_val = hypctx->guest_regs.hyp_a[0];
switch (func_id) {
case SBI_TIME_SET_TIMER:
vtimer_set_timer(hypctx, next_val);
break;
default:
return (SBI_ERR_NOT_SUPPORTED);
}
return (SBI_SUCCESS);
}
static int
vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx)
{
cpuset_t active_cpus;
struct hyp *hyp;
uint64_t hart_mask;
uint64_t hart_mask_base;
uint64_t func_id;
cpuset_t cpus;
int hart_id;
int bit;
func_id = hypctx->guest_regs.hyp_a[6];
hart_mask = hypctx->guest_regs.hyp_a[0];
hart_mask_base = hypctx->guest_regs.hyp_a[1];
dprintf("%s: hart_mask %lx\n", __func__, hart_mask);
hyp = hypctx->hyp;
active_cpus = vm_active_cpus(hyp->vm);
CPU_ZERO(&cpus);
switch (func_id) {
case SBI_IPI_SEND_IPI:
while ((bit = ffs(hart_mask))) {
hart_id = (bit - 1);
hart_mask &= ~(1u << hart_id);
if (hart_mask_base != -1)
hart_id += hart_mask_base;
if (!CPU_ISSET(hart_id, &active_cpus))
return (SBI_ERR_INVALID_PARAM);
CPU_SET(hart_id, &cpus);
}
break;
default:
dprintf("%s: unknown func %ld\n", __func__, func_id);
return (SBI_ERR_NOT_SUPPORTED);
}
if (CPU_EMPTY(&cpus))
return (SBI_ERR_INVALID_PARAM);
riscv_send_ipi(hyp, &cpus);
return (SBI_SUCCESS);
}
bool
vmm_sbi_ecall(struct vcpu *vcpu)
{
int sbi_extension_id;
struct hypctx *hypctx;
int error;
hypctx = riscv_get_active_vcpu();
sbi_extension_id = hypctx->guest_regs.hyp_a[7];
dprintf("%s: args %lx %lx %lx %lx %lx %lx %lx %lx\n", __func__,
hypctx->guest_regs.hyp_a[0],
hypctx->guest_regs.hyp_a[1],
hypctx->guest_regs.hyp_a[2],
hypctx->guest_regs.hyp_a[3],
hypctx->guest_regs.hyp_a[4],
hypctx->guest_regs.hyp_a[5],
hypctx->guest_regs.hyp_a[6],
hypctx->guest_regs.hyp_a[7]);
switch (sbi_extension_id) {
case SBI_EXT_ID_RFNC:
error = vmm_sbi_handle_rfnc(vcpu, hypctx);
break;
case SBI_EXT_ID_TIME:
error = vmm_sbi_handle_time(vcpu, hypctx);
break;
case SBI_EXT_ID_IPI:
error = vmm_sbi_handle_ipi(vcpu, hypctx);
break;
default:
return (false);
}
hypctx->guest_regs.hyp_a[0] = error;
return (true);
}