xref: /linux/tools/testing/selftests/kvm/lib/riscv/ucall.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
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 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
14 {
15 	struct kvm_run *run = vcpu->run;
16 
17 	if (run->exit_reason == KVM_EXIT_RISCV_SBI &&
18 	    run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) {
19 		switch (run->riscv_sbi.function_id) {
20 		case KVM_RISCV_SELFTESTS_SBI_UCALL:
21 			return (void *)run->riscv_sbi.args[0];
22 		case KVM_RISCV_SELFTESTS_SBI_UNEXP:
23 			vcpu_dump(stderr, vcpu, 2);
24 			TEST_ASSERT(0, "Unexpected trap taken by guest");
25 			break;
26 		default:
27 			break;
28 		}
29 	}
30 	return NULL;
31 }
32