book3s_rtas.c 6.9 KB

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