Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2012 Michael Ellerman, IBM Corporation.
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/kvm_host.h>
0008 #include <linux/kvm.h>
0009 #include <linux/err.h>
0010 
0011 #include <linux/uaccess.h>
0012 #include <asm/kvm_book3s.h>
0013 #include <asm/kvm_ppc.h>
0014 #include <asm/hvcall.h>
0015 #include <asm/rtas.h>
0016 #include <asm/xive.h>
0017 
0018 #ifdef CONFIG_KVM_XICS
0019 static void kvm_rtas_set_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
0020 {
0021     u32 irq, server, priority;
0022     int rc;
0023 
0024     if (be32_to_cpu(args->nargs) != 3 || be32_to_cpu(args->nret) != 1) {
0025         rc = -3;
0026         goto out;
0027     }
0028 
0029     irq = be32_to_cpu(args->args[0]);
0030     server = be32_to_cpu(args->args[1]);
0031     priority = be32_to_cpu(args->args[2]);
0032 
0033     if (xics_on_xive())
0034         rc = kvmppc_xive_set_xive(vcpu->kvm, irq, server, priority);
0035     else
0036         rc = kvmppc_xics_set_xive(vcpu->kvm, irq, server, priority);
0037     if (rc)
0038         rc = -3;
0039 out:
0040     args->rets[0] = cpu_to_be32(rc);
0041 }
0042 
0043 static void kvm_rtas_get_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
0044 {
0045     u32 irq, server, priority;
0046     int rc;
0047 
0048     if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 3) {
0049         rc = -3;
0050         goto out;
0051     }
0052 
0053     irq = be32_to_cpu(args->args[0]);
0054 
0055     server = priority = 0;
0056     if (xics_on_xive())
0057         rc = kvmppc_xive_get_xive(vcpu->kvm, irq, &server, &priority);
0058     else
0059         rc = kvmppc_xics_get_xive(vcpu->kvm, irq, &server, &priority);
0060     if (rc) {
0061         rc = -3;
0062         goto out;
0063     }
0064 
0065     args->rets[1] = cpu_to_be32(server);
0066     args->rets[2] = cpu_to_be32(priority);
0067 out:
0068     args->rets[0] = cpu_to_be32(rc);
0069 }
0070 
0071 static void kvm_rtas_int_off(struct kvm_vcpu *vcpu, struct rtas_args *args)
0072 {
0073     u32 irq;
0074     int rc;
0075 
0076     if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
0077         rc = -3;
0078         goto out;
0079     }
0080 
0081     irq = be32_to_cpu(args->args[0]);
0082 
0083     if (xics_on_xive())
0084         rc = kvmppc_xive_int_off(vcpu->kvm, irq);
0085     else
0086         rc = kvmppc_xics_int_off(vcpu->kvm, irq);
0087     if (rc)
0088         rc = -3;
0089 out:
0090     args->rets[0] = cpu_to_be32(rc);
0091 }
0092 
0093 static void kvm_rtas_int_on(struct kvm_vcpu *vcpu, struct rtas_args *args)
0094 {
0095     u32 irq;
0096     int rc;
0097 
0098     if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
0099         rc = -3;
0100         goto out;
0101     }
0102 
0103     irq = be32_to_cpu(args->args[0]);
0104 
0105     if (xics_on_xive())
0106         rc = kvmppc_xive_int_on(vcpu->kvm, irq);
0107     else
0108         rc = kvmppc_xics_int_on(vcpu->kvm, irq);
0109     if (rc)
0110         rc = -3;
0111 out:
0112     args->rets[0] = cpu_to_be32(rc);
0113 }
0114 #endif /* CONFIG_KVM_XICS */
0115 
0116 struct rtas_handler {
0117     void (*handler)(struct kvm_vcpu *vcpu, struct rtas_args *args);
0118     char *name;
0119 };
0120 
0121 static struct rtas_handler rtas_handlers[] = {
0122 #ifdef CONFIG_KVM_XICS
0123     { .name = "ibm,set-xive", .handler = kvm_rtas_set_xive },
0124     { .name = "ibm,get-xive", .handler = kvm_rtas_get_xive },
0125     { .name = "ibm,int-off",  .handler = kvm_rtas_int_off },
0126     { .name = "ibm,int-on",   .handler = kvm_rtas_int_on },
0127 #endif
0128 };
0129 
0130 struct rtas_token_definition {
0131     struct list_head list;
0132     struct rtas_handler *handler;
0133     u64 token;
0134 };
0135 
0136 static int rtas_name_matches(char *s1, char *s2)
0137 {
0138     struct kvm_rtas_token_args args;
0139     return !strncmp(s1, s2, sizeof(args.name));
0140 }
0141 
0142 static int rtas_token_undefine(struct kvm *kvm, char *name)
0143 {
0144     struct rtas_token_definition *d, *tmp;
0145 
0146     lockdep_assert_held(&kvm->arch.rtas_token_lock);
0147 
0148     list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
0149         if (rtas_name_matches(d->handler->name, name)) {
0150             list_del(&d->list);
0151             kfree(d);
0152             return 0;
0153         }
0154     }
0155 
0156     /* It's not an error to undefine an undefined token */
0157     return 0;
0158 }
0159 
0160 static int rtas_token_define(struct kvm *kvm, char *name, u64 token)
0161 {
0162     struct rtas_token_definition *d;
0163     struct rtas_handler *h = NULL;
0164     bool found;
0165     int i;
0166 
0167     lockdep_assert_held(&kvm->arch.rtas_token_lock);
0168 
0169     list_for_each_entry(d, &kvm->arch.rtas_tokens, list) {
0170         if (d->token == token)
0171             return -EEXIST;
0172     }
0173 
0174     found = false;
0175     for (i = 0; i < ARRAY_SIZE(rtas_handlers); i++) {
0176         h = &rtas_handlers[i];
0177         if (rtas_name_matches(h->name, name)) {
0178             found = true;
0179             break;
0180         }
0181     }
0182 
0183     if (!found)
0184         return -ENOENT;
0185 
0186     d = kzalloc(sizeof(*d), GFP_KERNEL);
0187     if (!d)
0188         return -ENOMEM;
0189 
0190     d->handler = h;
0191     d->token = token;
0192 
0193     list_add_tail(&d->list, &kvm->arch.rtas_tokens);
0194 
0195     return 0;
0196 }
0197 
0198 int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp)
0199 {
0200     struct kvm_rtas_token_args args;
0201     int rc;
0202 
0203     if (copy_from_user(&args, argp, sizeof(args)))
0204         return -EFAULT;
0205 
0206     mutex_lock(&kvm->arch.rtas_token_lock);
0207 
0208     if (args.token)
0209         rc = rtas_token_define(kvm, args.name, args.token);
0210     else
0211         rc = rtas_token_undefine(kvm, args.name);
0212 
0213     mutex_unlock(&kvm->arch.rtas_token_lock);
0214 
0215     return rc;
0216 }
0217 
0218 int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu)
0219 {
0220     struct rtas_token_definition *d;
0221     struct rtas_args args;
0222     rtas_arg_t *orig_rets;
0223     gpa_t args_phys;
0224     int rc;
0225 
0226     /*
0227      * r4 contains the guest physical address of the RTAS args
0228      * Mask off the top 4 bits since this is a guest real address
0229      */
0230     args_phys = kvmppc_get_gpr(vcpu, 4) & KVM_PAM;
0231 
0232     kvm_vcpu_srcu_read_lock(vcpu);
0233     rc = kvm_read_guest(vcpu->kvm, args_phys, &args, sizeof(args));
0234     kvm_vcpu_srcu_read_unlock(vcpu);
0235     if (rc)
0236         goto fail;
0237 
0238     /*
0239      * args->rets is a pointer into args->args. Now that we've
0240      * copied args we need to fix it up to point into our copy,
0241      * not the guest args. We also need to save the original
0242      * value so we can restore it on the way out.
0243      */
0244     orig_rets = args.rets;
0245     if (be32_to_cpu(args.nargs) >= ARRAY_SIZE(args.args)) {
0246         /*
0247          * Don't overflow our args array: ensure there is room for
0248          * at least rets[0] (even if the call specifies 0 nret).
0249          *
0250          * Each handler must then check for the correct nargs and nret
0251          * values, but they may always return failure in rets[0].
0252          */
0253         rc = -EINVAL;
0254         goto fail;
0255     }
0256     args.rets = &args.args[be32_to_cpu(args.nargs)];
0257 
0258     mutex_lock(&vcpu->kvm->arch.rtas_token_lock);
0259 
0260     rc = -ENOENT;
0261     list_for_each_entry(d, &vcpu->kvm->arch.rtas_tokens, list) {
0262         if (d->token == be32_to_cpu(args.token)) {
0263             d->handler->handler(vcpu, &args);
0264             rc = 0;
0265             break;
0266         }
0267     }
0268 
0269     mutex_unlock(&vcpu->kvm->arch.rtas_token_lock);
0270 
0271     if (rc == 0) {
0272         args.rets = orig_rets;
0273         rc = kvm_write_guest(vcpu->kvm, args_phys, &args, sizeof(args));
0274         if (rc)
0275             goto fail;
0276     }
0277 
0278     return rc;
0279 
0280 fail:
0281     /*
0282      * We only get here if the guest has called RTAS with a bogus
0283      * args pointer or nargs/nret values that would overflow the
0284      * array. That means we can't get to the args, and so we can't
0285      * fail the RTAS call. So fail right out to userspace, which
0286      * should kill the guest.
0287      *
0288      * SLOF should actually pass the hcall return value from the
0289      * rtas handler call in r3, so enter_rtas could be modified to
0290      * return a failure indication in r3 and we could return such
0291      * errors to the guest rather than failing to host userspace.
0292      * However old guests that don't test for failure could then
0293      * continue silently after errors, so for now we won't do this.
0294      */
0295     return rc;
0296 }
0297 EXPORT_SYMBOL_GPL(kvmppc_rtas_hcall);
0298 
0299 void kvmppc_rtas_tokens_free(struct kvm *kvm)
0300 {
0301     struct rtas_token_definition *d, *tmp;
0302 
0303     list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
0304         list_del(&d->list);
0305         kfree(d);
0306     }
0307 }