xref: /openbmc/linux/arch/riscv/kvm/vcpu_sbi.c (revision c94b731d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Western Digital Corporation or its affiliates.
4  *
5  * Authors:
6  *     Atish Patra <atish.patra@wdc.com>
7  */
8 
9 #include <linux/errno.h>
10 #include <linux/err.h>
11 #include <linux/kvm_host.h>
12 #include <asm/sbi.h>
13 #include <asm/kvm_vcpu_sbi.h>
14 
15 static int kvm_linux_err_map_sbi(int err)
16 {
17 	switch (err) {
18 	case 0:
19 		return SBI_SUCCESS;
20 	case -EPERM:
21 		return SBI_ERR_DENIED;
22 	case -EINVAL:
23 		return SBI_ERR_INVALID_PARAM;
24 	case -EFAULT:
25 		return SBI_ERR_INVALID_ADDRESS;
26 	case -EOPNOTSUPP:
27 		return SBI_ERR_NOT_SUPPORTED;
28 	case -EALREADY:
29 		return SBI_ERR_ALREADY_AVAILABLE;
30 	default:
31 		return SBI_ERR_FAILURE;
32 	};
33 }
34 
35 #ifdef CONFIG_RISCV_SBI_V01
36 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;
37 #else
38 static const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01 = {
39 	.extid_start = -1UL,
40 	.extid_end = -1UL,
41 	.handler = NULL,
42 };
43 #endif
44 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_base;
45 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_time;
46 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_ipi;
47 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence;
48 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm;
49 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental;
50 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor;
51 
52 static const struct kvm_vcpu_sbi_extension *sbi_ext[] = {
53 	&vcpu_sbi_ext_v01,
54 	&vcpu_sbi_ext_base,
55 	&vcpu_sbi_ext_time,
56 	&vcpu_sbi_ext_ipi,
57 	&vcpu_sbi_ext_rfence,
58 	&vcpu_sbi_ext_hsm,
59 	&vcpu_sbi_ext_experimental,
60 	&vcpu_sbi_ext_vendor,
61 };
62 
63 void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run)
64 {
65 	struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
66 
67 	vcpu->arch.sbi_context.return_handled = 0;
68 	vcpu->stat.ecall_exit_stat++;
69 	run->exit_reason = KVM_EXIT_RISCV_SBI;
70 	run->riscv_sbi.extension_id = cp->a7;
71 	run->riscv_sbi.function_id = cp->a6;
72 	run->riscv_sbi.args[0] = cp->a0;
73 	run->riscv_sbi.args[1] = cp->a1;
74 	run->riscv_sbi.args[2] = cp->a2;
75 	run->riscv_sbi.args[3] = cp->a3;
76 	run->riscv_sbi.args[4] = cp->a4;
77 	run->riscv_sbi.args[5] = cp->a5;
78 	run->riscv_sbi.ret[0] = cp->a0;
79 	run->riscv_sbi.ret[1] = cp->a1;
80 }
81 
82 int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
83 {
84 	struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
85 
86 	/* Handle SBI return only once */
87 	if (vcpu->arch.sbi_context.return_handled)
88 		return 0;
89 	vcpu->arch.sbi_context.return_handled = 1;
90 
91 	/* Update return values */
92 	cp->a0 = run->riscv_sbi.ret[0];
93 	cp->a1 = run->riscv_sbi.ret[1];
94 
95 	/* Move to next instruction */
96 	vcpu->arch.guest_context.sepc += 4;
97 
98 	return 0;
99 }
100 
101 const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(unsigned long extid)
102 {
103 	int i = 0;
104 
105 	for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) {
106 		if (sbi_ext[i]->extid_start <= extid &&
107 		    sbi_ext[i]->extid_end >= extid)
108 			return sbi_ext[i];
109 	}
110 
111 	return NULL;
112 }
113 
114 int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run)
115 {
116 	int ret = 1;
117 	bool next_sepc = true;
118 	bool userspace_exit = false;
119 	struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
120 	const struct kvm_vcpu_sbi_extension *sbi_ext;
121 	struct kvm_cpu_trap utrap = { 0 };
122 	unsigned long out_val = 0;
123 	bool ext_is_v01 = false;
124 
125 	sbi_ext = kvm_vcpu_sbi_find_ext(cp->a7);
126 	if (sbi_ext && sbi_ext->handler) {
127 #ifdef CONFIG_RISCV_SBI_V01
128 		if (cp->a7 >= SBI_EXT_0_1_SET_TIMER &&
129 		    cp->a7 <= SBI_EXT_0_1_SHUTDOWN)
130 			ext_is_v01 = true;
131 #endif
132 		ret = sbi_ext->handler(vcpu, run, &out_val, &utrap, &userspace_exit);
133 	} else {
134 		/* Return error for unsupported SBI calls */
135 		cp->a0 = SBI_ERR_NOT_SUPPORTED;
136 		goto ecall_done;
137 	}
138 
139 	/* Handle special error cases i.e trap, exit or userspace forward */
140 	if (utrap.scause) {
141 		/* No need to increment sepc or exit ioctl loop */
142 		ret = 1;
143 		utrap.sepc = cp->sepc;
144 		kvm_riscv_vcpu_trap_redirect(vcpu, &utrap);
145 		next_sepc = false;
146 		goto ecall_done;
147 	}
148 
149 	/* Exit ioctl loop or Propagate the error code the guest */
150 	if (userspace_exit) {
151 		next_sepc = false;
152 		ret = 0;
153 	} else {
154 		/**
155 		 * SBI extension handler always returns an Linux error code. Convert
156 		 * it to the SBI specific error code that can be propagated the SBI
157 		 * caller.
158 		 */
159 		ret = kvm_linux_err_map_sbi(ret);
160 		cp->a0 = ret;
161 		ret = 1;
162 	}
163 ecall_done:
164 	if (next_sepc)
165 		cp->sepc += 4;
166 	if (!ext_is_v01)
167 		cp->a1 = out_val;
168 
169 	return ret;
170 }
171