Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright 2012 Michael Ellerman, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kvm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/kvm_book3s.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/kvm_ppc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/hvcall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/rtas.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/xive.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #ifdef CONFIG_KVM_XICS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static void kvm_rtas_set_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	u32 irq, server, priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	if (be32_to_cpu(args->nargs) != 3 || be32_to_cpu(args->nret) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	irq = be32_to_cpu(args->args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	server = be32_to_cpu(args->args[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	priority = be32_to_cpu(args->args[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (xics_on_xive())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		rc = kvmppc_xive_set_xive(vcpu->kvm, irq, server, priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		rc = kvmppc_xics_set_xive(vcpu->kvm, irq, server, priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	args->rets[0] = cpu_to_be32(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static void kvm_rtas_get_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	u32 irq, server, priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	irq = be32_to_cpu(args->args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	server = priority = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (xics_on_xive())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		rc = kvmppc_xive_get_xive(vcpu->kvm, irq, &server, &priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		rc = kvmppc_xics_get_xive(vcpu->kvm, irq, &server, &priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	args->rets[1] = cpu_to_be32(server);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	args->rets[2] = cpu_to_be32(priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	args->rets[0] = cpu_to_be32(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static void kvm_rtas_int_off(struct kvm_vcpu *vcpu, struct rtas_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	u32 irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	irq = be32_to_cpu(args->args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (xics_on_xive())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		rc = kvmppc_xive_int_off(vcpu->kvm, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		rc = kvmppc_xics_int_off(vcpu->kvm, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	args->rets[0] = cpu_to_be32(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void kvm_rtas_int_on(struct kvm_vcpu *vcpu, struct rtas_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u32 irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	irq = be32_to_cpu(args->args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (xics_on_xive())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		rc = kvmppc_xive_int_on(vcpu->kvm, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		rc = kvmppc_xics_int_on(vcpu->kvm, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		rc = -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	args->rets[0] = cpu_to_be32(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #endif /* CONFIG_KVM_XICS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct rtas_handler {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	void (*handler)(struct kvm_vcpu *vcpu, struct rtas_args *args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static struct rtas_handler rtas_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #ifdef CONFIG_KVM_XICS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	{ .name = "ibm,set-xive", .handler = kvm_rtas_set_xive },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	{ .name = "ibm,get-xive", .handler = kvm_rtas_get_xive },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	{ .name = "ibm,int-off",  .handler = kvm_rtas_int_off },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	{ .name = "ibm,int-on",   .handler = kvm_rtas_int_on },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct rtas_token_definition {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct rtas_handler *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	u64 token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int rtas_name_matches(char *s1, char *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct kvm_rtas_token_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return !strncmp(s1, s2, sizeof(args.name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int rtas_token_undefine(struct kvm *kvm, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct rtas_token_definition *d, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	lockdep_assert_held(&kvm->arch.rtas_token_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (rtas_name_matches(d->handler->name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			list_del(&d->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			kfree(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* It's not an error to undefine an undefined token */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int rtas_token_define(struct kvm *kvm, char *name, u64 token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct rtas_token_definition *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct rtas_handler *h = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	lockdep_assert_held(&kvm->arch.rtas_token_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	list_for_each_entry(d, &kvm->arch.rtas_tokens, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (d->token == token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	for (i = 0; i < ARRAY_SIZE(rtas_handlers); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		h = &rtas_handlers[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if (rtas_name_matches(h->name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	d = kzalloc(sizeof(*d), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	d->handler = h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	d->token = token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	list_add_tail(&d->list, &kvm->arch.rtas_tokens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct kvm_rtas_token_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (copy_from_user(&args, argp, sizeof(args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	mutex_lock(&kvm->arch.rtas_token_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (args.token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		rc = rtas_token_define(kvm, args.name, args.token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		rc = rtas_token_undefine(kvm, args.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	mutex_unlock(&kvm->arch.rtas_token_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct rtas_token_definition *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct rtas_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	rtas_arg_t *orig_rets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	gpa_t args_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	 * r4 contains the guest physical address of the RTAS args
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * Mask off the top 4 bits since this is a guest real address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	args_phys = kvmppc_get_gpr(vcpu, 4) & KVM_PAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	rc = kvm_read_guest(vcpu->kvm, args_phys, &args, sizeof(args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	 * args->rets is a pointer into args->args. Now that we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	 * copied args we need to fix it up to point into our copy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	 * not the guest args. We also need to save the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	 * value so we can restore it on the way out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	orig_rets = args.rets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (be32_to_cpu(args.nargs) >= ARRAY_SIZE(args.args)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		 * Don't overflow our args array: ensure there is room for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		 * at least rets[0] (even if the call specifies 0 nret).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		 * Each handler must then check for the correct nargs and nret
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		 * values, but they may always return failure in rets[0].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	args.rets = &args.args[be32_to_cpu(args.nargs)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	mutex_lock(&vcpu->kvm->arch.rtas_token_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	rc = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	list_for_each_entry(d, &vcpu->kvm->arch.rtas_tokens, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		if (d->token == be32_to_cpu(args.token)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			d->handler->handler(vcpu, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	mutex_unlock(&vcpu->kvm->arch.rtas_token_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		args.rets = orig_rets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		rc = kvm_write_guest(vcpu->kvm, args_phys, &args, sizeof(args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	 * We only get here if the guest has called RTAS with a bogus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * args pointer or nargs/nret values that would overflow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * array. That means we can't get to the args, and so we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * fail the RTAS call. So fail right out to userspace, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * should kill the guest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * SLOF should actually pass the hcall return value from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * rtas handler call in r3, so enter_rtas could be modified to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * return a failure indication in r3 and we could return such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * errors to the guest rather than failing to host userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 * However old guests that don't test for failure could then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * continue silently after errors, so for now we won't do this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) EXPORT_SYMBOL_GPL(kvmppc_rtas_hcall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) void kvmppc_rtas_tokens_free(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	struct rtas_token_definition *d, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		list_del(&d->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		kfree(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }