1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ucall support. A ucall is a "hypercall to userspace".
4  *
5  * Copyright (C) 2021 Western Digital Corporation or its affiliates.
6  */
7 
8 #include <linux/kvm.h>
9 
10 #include "kvm_util.h"
11 #include "processor.h"
12 
13 struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
14 			unsigned long arg1, unsigned long arg2,
15 			unsigned long arg3, unsigned long arg4,
16 			unsigned long arg5)
17 {
18 	register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);
19 	register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);
20 	register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);
21 	register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);
22 	register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);
23 	register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);
24 	register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);
25 	register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);
26 	struct sbiret ret;
27 
28 	asm volatile (
29 		"ecall"
30 		: "+r" (a0), "+r" (a1)
31 		: "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
32 		: "memory");
33 	ret.error = a0;
34 	ret.value = a1;
35 
36 	return ret;
37 }
38 
39 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
40 {
41 	struct kvm_run *run = vcpu->run;
42 
43 	if (run->exit_reason == KVM_EXIT_RISCV_SBI &&
44 	    run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) {
45 		switch (run->riscv_sbi.function_id) {
46 		case KVM_RISCV_SELFTESTS_SBI_UCALL:
47 			return (void *)run->riscv_sbi.args[0];
48 		case KVM_RISCV_SELFTESTS_SBI_UNEXP:
49 			vcpu_dump(stderr, vcpu, 2);
50 			TEST_ASSERT(0, "Unexpected trap taken by guest");
51 			break;
52 		default:
53 			break;
54 		}
55 	}
56 	return NULL;
57 }
58