book3s_rtas.c 6.1 KB

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