book3s_rtas.c 5.4 KB

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