ppc-pv.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. The PPC KVM paravirtual interface
  2. =================================
  3. The basic execution principle by which KVM on PowerPC works is to run all kernel
  4. space code in PR=1 which is user space. This way we trap all privileged
  5. instructions and can emulate them accordingly.
  6. Unfortunately that is also the downfall. There are quite some privileged
  7. instructions that needlessly return us to the hypervisor even though they
  8. could be handled differently.
  9. This is what the PPC PV interface helps with. It takes privileged instructions
  10. and transforms them into unprivileged ones with some help from the hypervisor.
  11. This cuts down virtualization costs by about 50% on some of my benchmarks.
  12. The code for that interface can be found in arch/powerpc/kernel/kvm*
  13. Querying for existence
  14. ======================
  15. To find out if we're running on KVM or not, we leverage the device tree. When
  16. Linux is running on KVM, a node /hypervisor exists. That node contains a
  17. compatible property with the value "linux,kvm".
  18. Once you determined you're running under a PV capable KVM, you can now use
  19. hypercalls as described below.
  20. KVM hypercalls
  21. ==============
  22. Inside the device tree's /hypervisor node there's a property called
  23. 'hypercall-instructions'. This property contains at most 4 opcodes that make
  24. up the hypercall. To call a hypercall, just call these instructions.
  25. The parameters are as follows:
  26. Register IN OUT
  27. r0 - volatile
  28. r3 1st parameter Return code
  29. r4 2nd parameter 1st output value
  30. r5 3rd parameter 2nd output value
  31. r6 4th parameter 3rd output value
  32. r7 5th parameter 4th output value
  33. r8 6th parameter 5th output value
  34. r9 7th parameter 6th output value
  35. r10 8th parameter 7th output value
  36. r11 hypercall number 8th output value
  37. r12 - volatile
  38. Hypercall definitions are shared in generic code, so the same hypercall numbers
  39. apply for x86 and powerpc alike with the exception that each KVM hypercall
  40. also needs to be ORed with the KVM vendor code which is (42 << 16).
  41. Return codes can be as follows:
  42. Code Meaning
  43. 0 Success
  44. 12 Hypercall not implemented
  45. <0 Error
  46. The magic page
  47. ==============
  48. To enable communication between the hypervisor and guest there is a new shared
  49. page that contains parts of supervisor visible register state. The guest can
  50. map this shared page using the KVM hypercall KVM_HC_PPC_MAP_MAGIC_PAGE.
  51. With this hypercall issued the guest always gets the magic page mapped at the
  52. desired location. The first parameter indicates the effective address when the
  53. MMU is enabled. The second parameter indicates the address in real mode, if
  54. applicable to the target. For now, we always map the page to -4096. This way we
  55. can access it using absolute load and store functions. The following
  56. instruction reads the first field of the magic page:
  57. ld rX, -4096(0)
  58. The interface is designed to be extensible should there be need later to add
  59. additional registers to the magic page. If you add fields to the magic page,
  60. also define a new hypercall feature to indicate that the host can give you more
  61. registers. Only if the host supports the additional features, make use of them.
  62. The magic page layout is described by struct kvm_vcpu_arch_shared
  63. in arch/powerpc/include/asm/kvm_para.h.
  64. Magic page features
  65. ===================
  66. When mapping the magic page using the KVM hypercall KVM_HC_PPC_MAP_MAGIC_PAGE,
  67. a second return value is passed to the guest. This second return value contains
  68. a bitmap of available features inside the magic page.
  69. The following enhancements to the magic page are currently available:
  70. KVM_MAGIC_FEAT_SR Maps SR registers r/w in the magic page
  71. KVM_MAGIC_FEAT_MAS0_TO_SPRG7 Maps MASn, ESR, PIR and high SPRGs
  72. For enhanced features in the magic page, please check for the existence of the
  73. feature before using them!
  74. Magic page flags
  75. ================
  76. In addition to features that indicate whether a host is capable of a particular
  77. feature we also have a channel for a guest to tell the guest whether it's capable
  78. of something. This is what we call "flags".
  79. Flags are passed to the host in the low 12 bits of the Effective Address.
  80. The following flags are currently available for a guest to expose:
  81. MAGIC_PAGE_FLAG_NOT_MAPPED_NX Guest handles NX bits correctly wrt magic page
  82. MSR bits
  83. ========
  84. The MSR contains bits that require hypervisor intervention and bits that do
  85. not require direct hypervisor intervention because they only get interpreted
  86. when entering the guest or don't have any impact on the hypervisor's behavior.
  87. The following bits are safe to be set inside the guest:
  88. MSR_EE
  89. MSR_RI
  90. If any other bit changes in the MSR, please still use mtmsr(d).
  91. Patched instructions
  92. ====================
  93. The "ld" and "std" instructions are transformed to "lwz" and "stw" instructions
  94. respectively on 32 bit systems with an added offset of 4 to accommodate for big
  95. endianness.
  96. The following is a list of mapping the Linux kernel performs when running as
  97. guest. Implementing any of those mappings is optional, as the instruction traps
  98. also act on the shared page. So calling privileged instructions still works as
  99. before.
  100. From To
  101. ==== ==
  102. mfmsr rX ld rX, magic_page->msr
  103. mfsprg rX, 0 ld rX, magic_page->sprg0
  104. mfsprg rX, 1 ld rX, magic_page->sprg1
  105. mfsprg rX, 2 ld rX, magic_page->sprg2
  106. mfsprg rX, 3 ld rX, magic_page->sprg3
  107. mfsrr0 rX ld rX, magic_page->srr0
  108. mfsrr1 rX ld rX, magic_page->srr1
  109. mfdar rX ld rX, magic_page->dar
  110. mfdsisr rX lwz rX, magic_page->dsisr
  111. mtmsr rX std rX, magic_page->msr
  112. mtsprg 0, rX std rX, magic_page->sprg0
  113. mtsprg 1, rX std rX, magic_page->sprg1
  114. mtsprg 2, rX std rX, magic_page->sprg2
  115. mtsprg 3, rX std rX, magic_page->sprg3
  116. mtsrr0 rX std rX, magic_page->srr0
  117. mtsrr1 rX std rX, magic_page->srr1
  118. mtdar rX std rX, magic_page->dar
  119. mtdsisr rX stw rX, magic_page->dsisr
  120. tlbsync nop
  121. mtmsrd rX, 0 b <special mtmsr section>
  122. mtmsr rX b <special mtmsr section>
  123. mtmsrd rX, 1 b <special mtmsrd section>
  124. [Book3S only]
  125. mtsrin rX, rY b <special mtsrin section>
  126. [BookE only]
  127. wrteei [0|1] b <special wrteei section>
  128. Some instructions require more logic to determine what's going on than a load
  129. or store instruction can deliver. To enable patching of those, we keep some
  130. RAM around where we can live translate instructions to. What happens is the
  131. following:
  132. 1) copy emulation code to memory
  133. 2) patch that code to fit the emulated instruction
  134. 3) patch that code to return to the original pc + 4
  135. 4) patch the original instruction to branch to the new code
  136. That way we can inject an arbitrary amount of code as replacement for a single
  137. instruction. This allows us to check for pending interrupts when setting EE=1
  138. for example.
  139. Hypercall ABIs in KVM on PowerPC
  140. =================================
  141. 1) KVM hypercalls (ePAPR)
  142. These are ePAPR compliant hypercall implementation (mentioned above). Even
  143. generic hypercalls are implemented here, like the ePAPR idle hcall. These are
  144. available on all targets.
  145. 2) PAPR hypercalls
  146. PAPR hypercalls are needed to run server PowerPC PAPR guests (-M pseries in QEMU).
  147. These are the same hypercalls that pHyp, the POWER hypervisor implements. Some of
  148. them are handled in the kernel, some are handled in user space. This is only
  149. available on book3s_64.
  150. 3) OSI hypercalls
  151. Mac-on-Linux is another user of KVM on PowerPC, which has its own hypercall (long
  152. before KVM). This is supported to maintain compatibility. All these hypercalls get
  153. forwarded to user space. This is only useful on book3s_32, but can be used with
  154. book3s_64 as well.