1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Michael Ellerman, IBM Corporation.
6 #include <linux/kernel.h>
7 #include <linux/kvm_host.h>
11 #include <linux/uaccess.h>
12 #include <asm/kvm_book3s.h>
13 #include <asm/kvm_ppc.h>
14 #include <asm/hvcall.h>
18 #ifdef CONFIG_KVM_XICS
19 static void kvm_rtas_set_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
21 u32 irq, server, priority;
24 if (be32_to_cpu(args->nargs) != 3 || be32_to_cpu(args->nret) != 1) {
29 irq = be32_to_cpu(args->args[0]);
30 server = be32_to_cpu(args->args[1]);
31 priority = be32_to_cpu(args->args[2]);
34 rc = kvmppc_xive_set_xive(vcpu->kvm, irq, server, priority);
36 rc = kvmppc_xics_set_xive(vcpu->kvm, irq, server, priority);
40 args->rets[0] = cpu_to_be32(rc);
43 static void kvm_rtas_get_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
45 u32 irq, server, priority;
48 if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 3) {
53 irq = be32_to_cpu(args->args[0]);
55 server = priority = 0;
57 rc = kvmppc_xive_get_xive(vcpu->kvm, irq, &server, &priority);
59 rc = kvmppc_xics_get_xive(vcpu->kvm, irq, &server, &priority);
65 args->rets[1] = cpu_to_be32(server);
66 args->rets[2] = cpu_to_be32(priority);
68 args->rets[0] = cpu_to_be32(rc);
71 static void kvm_rtas_int_off(struct kvm_vcpu *vcpu, struct rtas_args *args)
76 if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
81 irq = be32_to_cpu(args->args[0]);
84 rc = kvmppc_xive_int_off(vcpu->kvm, irq);
86 rc = kvmppc_xics_int_off(vcpu->kvm, irq);
90 args->rets[0] = cpu_to_be32(rc);
93 static void kvm_rtas_int_on(struct kvm_vcpu *vcpu, struct rtas_args *args)
98 if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
103 irq = be32_to_cpu(args->args[0]);
106 rc = kvmppc_xive_int_on(vcpu->kvm, irq);
108 rc = kvmppc_xics_int_on(vcpu->kvm, irq);
112 args->rets[0] = cpu_to_be32(rc);
114 #endif /* CONFIG_KVM_XICS */
116 struct rtas_handler {
117 void (*handler)(struct kvm_vcpu *vcpu, struct rtas_args *args);
121 static struct rtas_handler rtas_handlers[] = {
122 #ifdef CONFIG_KVM_XICS
123 { .name = "ibm,set-xive", .handler = kvm_rtas_set_xive },
124 { .name = "ibm,get-xive", .handler = kvm_rtas_get_xive },
125 { .name = "ibm,int-off", .handler = kvm_rtas_int_off },
126 { .name = "ibm,int-on", .handler = kvm_rtas_int_on },
130 struct rtas_token_definition {
131 struct list_head list;
132 struct rtas_handler *handler;
136 static int rtas_name_matches(char *s1, char *s2)
138 struct kvm_rtas_token_args args;
139 return !strncmp(s1, s2, sizeof(args.name));
142 static int rtas_token_undefine(struct kvm *kvm, char *name)
144 struct rtas_token_definition *d, *tmp;
146 lockdep_assert_held(&kvm->arch.rtas_token_lock);
148 list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
149 if (rtas_name_matches(d->handler->name, name)) {
156 /* It's not an error to undefine an undefined token */
160 static int rtas_token_define(struct kvm *kvm, char *name, u64 token)
162 struct rtas_token_definition *d;
163 struct rtas_handler *h = NULL;
167 lockdep_assert_held(&kvm->arch.rtas_token_lock);
169 list_for_each_entry(d, &kvm->arch.rtas_tokens, list) {
170 if (d->token == token)
175 for (i = 0; i < ARRAY_SIZE(rtas_handlers); i++) {
176 h = &rtas_handlers[i];
177 if (rtas_name_matches(h->name, name)) {
186 d = kzalloc(sizeof(*d), GFP_KERNEL);
193 list_add_tail(&d->list, &kvm->arch.rtas_tokens);
198 int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp)
200 struct kvm_rtas_token_args args;
203 if (copy_from_user(&args, argp, sizeof(args)))
206 mutex_lock(&kvm->arch.rtas_token_lock);
209 rc = rtas_token_define(kvm, args.name, args.token);
211 rc = rtas_token_undefine(kvm, args.name);
213 mutex_unlock(&kvm->arch.rtas_token_lock);
218 int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu)
220 struct rtas_token_definition *d;
221 struct rtas_args args;
222 rtas_arg_t *orig_rets;
227 * r4 contains the guest physical address of the RTAS args
228 * Mask off the top 4 bits since this is a guest real address
230 args_phys = kvmppc_get_gpr(vcpu, 4) & KVM_PAM;
232 rc = kvm_read_guest(vcpu->kvm, args_phys, &args, sizeof(args));
237 * args->rets is a pointer into args->args. Now that we've
238 * copied args we need to fix it up to point into our copy,
239 * not the guest args. We also need to save the original
240 * value so we can restore it on the way out.
242 orig_rets = args.rets;
243 args.rets = &args.args[be32_to_cpu(args.nargs)];
245 mutex_lock(&vcpu->kvm->arch.rtas_token_lock);
248 list_for_each_entry(d, &vcpu->kvm->arch.rtas_tokens, list) {
249 if (d->token == be32_to_cpu(args.token)) {
250 d->handler->handler(vcpu, &args);
256 mutex_unlock(&vcpu->kvm->arch.rtas_token_lock);
259 args.rets = orig_rets;
260 rc = kvm_write_guest(vcpu->kvm, args_phys, &args, sizeof(args));
269 * We only get here if the guest has called RTAS with a bogus
270 * args pointer. That means we can't get to the args, and so we
271 * can't fail the RTAS call. So fail right out to userspace,
272 * which should kill the guest.
276 EXPORT_SYMBOL_GPL(kvmppc_rtas_hcall);
278 void kvmppc_rtas_tokens_free(struct kvm *kvm)
280 struct rtas_token_definition *d, *tmp;
282 list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {