book3s_rtas.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. struct rtas_handler {
  18. void (*handler)(struct kvm_vcpu *vcpu, struct rtas_args *args);
  19. char *name;
  20. };
  21. static struct rtas_handler rtas_handlers[] = { };
  22. struct rtas_token_definition {
  23. struct list_head list;
  24. struct rtas_handler *handler;
  25. u64 token;
  26. };
  27. static int rtas_name_matches(char *s1, char *s2)
  28. {
  29. struct kvm_rtas_token_args args;
  30. return !strncmp(s1, s2, sizeof(args.name));
  31. }
  32. static int rtas_token_undefine(struct kvm *kvm, char *name)
  33. {
  34. struct rtas_token_definition *d, *tmp;
  35. lockdep_assert_held(&kvm->lock);
  36. list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
  37. if (rtas_name_matches(d->handler->name, name)) {
  38. list_del(&d->list);
  39. kfree(d);
  40. return 0;
  41. }
  42. }
  43. /* It's not an error to undefine an undefined token */
  44. return 0;
  45. }
  46. static int rtas_token_define(struct kvm *kvm, char *name, u64 token)
  47. {
  48. struct rtas_token_definition *d;
  49. struct rtas_handler *h = NULL;
  50. bool found;
  51. int i;
  52. lockdep_assert_held(&kvm->lock);
  53. list_for_each_entry(d, &kvm->arch.rtas_tokens, list) {
  54. if (d->token == token)
  55. return -EEXIST;
  56. }
  57. found = false;
  58. for (i = 0; i < ARRAY_SIZE(rtas_handlers); i++) {
  59. h = &rtas_handlers[i];
  60. if (rtas_name_matches(h->name, name)) {
  61. found = true;
  62. break;
  63. }
  64. }
  65. if (!found)
  66. return -ENOENT;
  67. d = kzalloc(sizeof(*d), GFP_KERNEL);
  68. if (!d)
  69. return -ENOMEM;
  70. d->handler = h;
  71. d->token = token;
  72. list_add_tail(&d->list, &kvm->arch.rtas_tokens);
  73. return 0;
  74. }
  75. int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp)
  76. {
  77. struct kvm_rtas_token_args args;
  78. int rc;
  79. if (copy_from_user(&args, argp, sizeof(args)))
  80. return -EFAULT;
  81. mutex_lock(&kvm->lock);
  82. if (args.token)
  83. rc = rtas_token_define(kvm, args.name, args.token);
  84. else
  85. rc = rtas_token_undefine(kvm, args.name);
  86. mutex_unlock(&kvm->lock);
  87. return rc;
  88. }
  89. int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu)
  90. {
  91. struct rtas_token_definition *d;
  92. struct rtas_args args;
  93. rtas_arg_t *orig_rets;
  94. gpa_t args_phys;
  95. int rc;
  96. /* r4 contains the guest physical address of the RTAS args */
  97. args_phys = kvmppc_get_gpr(vcpu, 4);
  98. rc = kvm_read_guest(vcpu->kvm, args_phys, &args, sizeof(args));
  99. if (rc)
  100. goto fail;
  101. /*
  102. * args->rets is a pointer into args->args. Now that we've
  103. * copied args we need to fix it up to point into our copy,
  104. * not the guest args. We also need to save the original
  105. * value so we can restore it on the way out.
  106. */
  107. orig_rets = args.rets;
  108. args.rets = &args.args[args.nargs];
  109. mutex_lock(&vcpu->kvm->lock);
  110. rc = -ENOENT;
  111. list_for_each_entry(d, &vcpu->kvm->arch.rtas_tokens, list) {
  112. if (d->token == args.token) {
  113. d->handler->handler(vcpu, &args);
  114. rc = 0;
  115. break;
  116. }
  117. }
  118. mutex_unlock(&vcpu->kvm->lock);
  119. if (rc == 0) {
  120. args.rets = orig_rets;
  121. rc = kvm_write_guest(vcpu->kvm, args_phys, &args, sizeof(args));
  122. if (rc)
  123. goto fail;
  124. }
  125. return rc;
  126. fail:
  127. /*
  128. * We only get here if the guest has called RTAS with a bogus
  129. * args pointer. That means we can't get to the args, and so we
  130. * can't fail the RTAS call. So fail right out to userspace,
  131. * which should kill the guest.
  132. */
  133. return rc;
  134. }
  135. void kvmppc_rtas_tokens_free(struct kvm *kvm)
  136. {
  137. struct rtas_token_definition *d, *tmp;
  138. lockdep_assert_held(&kvm->lock);
  139. list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
  140. list_del(&d->list);
  141. kfree(d);
  142. }
  143. }