kvm.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
  3. *
  4. * Authors:
  5. * Alexander Graf <agraf@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2, as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <linux/init.h>
  22. #include <linux/kvm_para.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <asm/reg.h>
  26. #include <asm/kvm_ppc.h>
  27. #include <asm/sections.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/disassemble.h>
  30. #define KVM_MAGIC_PAGE (-4096L)
  31. #define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x)
  32. #define KVM_MASK_RT 0x03e00000
  33. static bool kvm_patching_worked = true;
  34. static inline void kvm_patch_ins(u32 *inst, u32 new_inst)
  35. {
  36. *inst = new_inst;
  37. flush_icache_range((ulong)inst, (ulong)inst + 4);
  38. }
  39. static void kvm_map_magic_page(void *data)
  40. {
  41. kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
  42. KVM_MAGIC_PAGE, /* Physical Address */
  43. KVM_MAGIC_PAGE); /* Effective Address */
  44. }
  45. static void kvm_check_ins(u32 *inst)
  46. {
  47. u32 _inst = *inst;
  48. u32 inst_no_rt = _inst & ~KVM_MASK_RT;
  49. u32 inst_rt = _inst & KVM_MASK_RT;
  50. switch (inst_no_rt) {
  51. }
  52. switch (_inst) {
  53. }
  54. }
  55. static void kvm_use_magic_page(void)
  56. {
  57. u32 *p;
  58. u32 *start, *end;
  59. u32 tmp;
  60. /* Tell the host to map the magic page to -4096 on all CPUs */
  61. on_each_cpu(kvm_map_magic_page, NULL, 1);
  62. /* Quick self-test to see if the mapping works */
  63. if (__get_user(tmp, (u32*)KVM_MAGIC_PAGE)) {
  64. kvm_patching_worked = false;
  65. return;
  66. }
  67. /* Now loop through all code and find instructions */
  68. start = (void*)_stext;
  69. end = (void*)_etext;
  70. for (p = start; p < end; p++)
  71. kvm_check_ins(p);
  72. printk(KERN_INFO "KVM: Live patching for a fast VM %s\n",
  73. kvm_patching_worked ? "worked" : "failed");
  74. }
  75. unsigned long kvm_hypercall(unsigned long *in,
  76. unsigned long *out,
  77. unsigned long nr)
  78. {
  79. unsigned long register r0 asm("r0");
  80. unsigned long register r3 asm("r3") = in[0];
  81. unsigned long register r4 asm("r4") = in[1];
  82. unsigned long register r5 asm("r5") = in[2];
  83. unsigned long register r6 asm("r6") = in[3];
  84. unsigned long register r7 asm("r7") = in[4];
  85. unsigned long register r8 asm("r8") = in[5];
  86. unsigned long register r9 asm("r9") = in[6];
  87. unsigned long register r10 asm("r10") = in[7];
  88. unsigned long register r11 asm("r11") = nr;
  89. unsigned long register r12 asm("r12");
  90. asm volatile("bl kvm_hypercall_start"
  91. : "=r"(r0), "=r"(r3), "=r"(r4), "=r"(r5), "=r"(r6),
  92. "=r"(r7), "=r"(r8), "=r"(r9), "=r"(r10), "=r"(r11),
  93. "=r"(r12)
  94. : "r"(r3), "r"(r4), "r"(r5), "r"(r6), "r"(r7), "r"(r8),
  95. "r"(r9), "r"(r10), "r"(r11)
  96. : "memory", "cc", "xer", "ctr", "lr");
  97. out[0] = r4;
  98. out[1] = r5;
  99. out[2] = r6;
  100. out[3] = r7;
  101. out[4] = r8;
  102. out[5] = r9;
  103. out[6] = r10;
  104. out[7] = r11;
  105. return r3;
  106. }
  107. EXPORT_SYMBOL_GPL(kvm_hypercall);
  108. static int kvm_para_setup(void)
  109. {
  110. extern u32 kvm_hypercall_start;
  111. struct device_node *hyper_node;
  112. u32 *insts;
  113. int len, i;
  114. hyper_node = of_find_node_by_path("/hypervisor");
  115. if (!hyper_node)
  116. return -1;
  117. insts = (u32*)of_get_property(hyper_node, "hcall-instructions", &len);
  118. if (len % 4)
  119. return -1;
  120. if (len > (4 * 4))
  121. return -1;
  122. for (i = 0; i < (len / 4); i++)
  123. kvm_patch_ins(&(&kvm_hypercall_start)[i], insts[i]);
  124. return 0;
  125. }
  126. static int __init kvm_guest_init(void)
  127. {
  128. if (!kvm_para_available())
  129. return 0;
  130. if (kvm_para_setup())
  131. return 0;
  132. if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE))
  133. kvm_use_magic_page();
  134. return 0;
  135. }
  136. postcore_initcall(kvm_guest_init);