msr.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =================
  3. KVM-specific MSRs
  4. =================
  5. :Author: Glauber Costa <[email protected]>, Red Hat Inc, 2010
  6. KVM makes use of some custom MSRs to service some requests.
  7. Custom MSRs have a range reserved for them, that goes from
  8. 0x4b564d00 to 0x4b564dff. There are MSRs outside this area,
  9. but they are deprecated and their use is discouraged.
  10. Custom MSR list
  11. ---------------
  12. The current supported Custom MSR list is:
  13. MSR_KVM_WALL_CLOCK_NEW:
  14. 0x4b564d00
  15. data:
  16. 4-byte alignment physical address of a memory area which must be
  17. in guest RAM. This memory is expected to hold a copy of the following
  18. structure::
  19. struct pvclock_wall_clock {
  20. u32 version;
  21. u32 sec;
  22. u32 nsec;
  23. } __attribute__((__packed__));
  24. whose data will be filled in by the hypervisor. The hypervisor is only
  25. guaranteed to update this data at the moment of MSR write.
  26. Users that want to reliably query this information more than once have
  27. to write more than once to this MSR. Fields have the following meanings:
  28. version:
  29. guest has to check version before and after grabbing
  30. time information and check that they are both equal and even.
  31. An odd version indicates an in-progress update.
  32. sec:
  33. number of seconds for wallclock at time of boot.
  34. nsec:
  35. number of nanoseconds for wallclock at time of boot.
  36. In order to get the current wallclock time, the system_time from
  37. MSR_KVM_SYSTEM_TIME_NEW needs to be added.
  38. Note that although MSRs are per-CPU entities, the effect of this
  39. particular MSR is global.
  40. Availability of this MSR must be checked via bit 3 in 0x4000001 cpuid
  41. leaf prior to usage.
  42. MSR_KVM_SYSTEM_TIME_NEW:
  43. 0x4b564d01
  44. data:
  45. 4-byte aligned physical address of a memory area which must be in
  46. guest RAM, plus an enable bit in bit 0. This memory is expected to hold
  47. a copy of the following structure::
  48. struct pvclock_vcpu_time_info {
  49. u32 version;
  50. u32 pad0;
  51. u64 tsc_timestamp;
  52. u64 system_time;
  53. u32 tsc_to_system_mul;
  54. s8 tsc_shift;
  55. u8 flags;
  56. u8 pad[2];
  57. } __attribute__((__packed__)); /* 32 bytes */
  58. whose data will be filled in by the hypervisor periodically. Only one
  59. write, or registration, is needed for each VCPU. The interval between
  60. updates of this structure is arbitrary and implementation-dependent.
  61. The hypervisor may update this structure at any time it sees fit until
  62. anything with bit0 == 0 is written to it.
  63. Fields have the following meanings:
  64. version:
  65. guest has to check version before and after grabbing
  66. time information and check that they are both equal and even.
  67. An odd version indicates an in-progress update.
  68. tsc_timestamp:
  69. the tsc value at the current VCPU at the time
  70. of the update of this structure. Guests can subtract this value
  71. from current tsc to derive a notion of elapsed time since the
  72. structure update.
  73. system_time:
  74. a host notion of monotonic time, including sleep
  75. time at the time this structure was last updated. Unit is
  76. nanoseconds.
  77. tsc_to_system_mul:
  78. multiplier to be used when converting
  79. tsc-related quantity to nanoseconds
  80. tsc_shift:
  81. shift to be used when converting tsc-related
  82. quantity to nanoseconds. This shift will ensure that
  83. multiplication with tsc_to_system_mul does not overflow.
  84. A positive value denotes a left shift, a negative value
  85. a right shift.
  86. The conversion from tsc to nanoseconds involves an additional
  87. right shift by 32 bits. With this information, guests can
  88. derive per-CPU time by doing::
  89. time = (current_tsc - tsc_timestamp)
  90. if (tsc_shift >= 0)
  91. time <<= tsc_shift;
  92. else
  93. time >>= -tsc_shift;
  94. time = (time * tsc_to_system_mul) >> 32
  95. time = time + system_time
  96. flags:
  97. bits in this field indicate extended capabilities
  98. coordinated between the guest and the hypervisor. Availability
  99. of specific flags has to be checked in 0x40000001 cpuid leaf.
  100. Current flags are:
  101. +-----------+--------------+----------------------------------+
  102. | flag bit | cpuid bit | meaning |
  103. +-----------+--------------+----------------------------------+
  104. | | | time measures taken across |
  105. | 0 | 24 | multiple cpus are guaranteed to |
  106. | | | be monotonic |
  107. +-----------+--------------+----------------------------------+
  108. | | | guest vcpu has been paused by |
  109. | 1 | N/A | the host |
  110. | | | See 4.70 in api.txt |
  111. +-----------+--------------+----------------------------------+
  112. Availability of this MSR must be checked via bit 3 in 0x4000001 cpuid
  113. leaf prior to usage.
  114. MSR_KVM_WALL_CLOCK:
  115. 0x11
  116. data and functioning:
  117. same as MSR_KVM_WALL_CLOCK_NEW. Use that instead.
  118. This MSR falls outside the reserved KVM range and may be removed in the
  119. future. Its usage is deprecated.
  120. Availability of this MSR must be checked via bit 0 in 0x4000001 cpuid
  121. leaf prior to usage.
  122. MSR_KVM_SYSTEM_TIME:
  123. 0x12
  124. data and functioning:
  125. same as MSR_KVM_SYSTEM_TIME_NEW. Use that instead.
  126. This MSR falls outside the reserved KVM range and may be removed in the
  127. future. Its usage is deprecated.
  128. Availability of this MSR must be checked via bit 0 in 0x4000001 cpuid
  129. leaf prior to usage.
  130. The suggested algorithm for detecting kvmclock presence is then::
  131. if (!kvm_para_available()) /* refer to cpuid.txt */
  132. return NON_PRESENT;
  133. flags = cpuid_eax(0x40000001);
  134. if (flags & 3) {
  135. msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
  136. msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
  137. return PRESENT;
  138. } else if (flags & 0) {
  139. msr_kvm_system_time = MSR_KVM_SYSTEM_TIME;
  140. msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK;
  141. return PRESENT;
  142. } else
  143. return NON_PRESENT;
  144. MSR_KVM_ASYNC_PF_EN:
  145. 0x4b564d02
  146. data:
  147. Asynchronous page fault (APF) control MSR.
  148. Bits 63-6 hold 64-byte aligned physical address of a 64 byte memory area
  149. which must be in guest RAM and must be zeroed. This memory is expected
  150. to hold a copy of the following structure::
  151. struct kvm_vcpu_pv_apf_data {
  152. /* Used for 'page not present' events delivered via #PF */
  153. __u32 flags;
  154. /* Used for 'page ready' events delivered via interrupt notification */
  155. __u32 token;
  156. __u8 pad[56];
  157. __u32 enabled;
  158. };
  159. Bits 5-4 of the MSR are reserved and should be zero. Bit 0 is set to 1
  160. when asynchronous page faults are enabled on the vcpu, 0 when disabled.
  161. Bit 1 is 1 if asynchronous page faults can be injected when vcpu is in
  162. cpl == 0. Bit 2 is 1 if asynchronous page faults are delivered to L1 as
  163. #PF vmexits. Bit 2 can be set only if KVM_FEATURE_ASYNC_PF_VMEXIT is
  164. present in CPUID. Bit 3 enables interrupt based delivery of 'page ready'
  165. events. Bit 3 can only be set if KVM_FEATURE_ASYNC_PF_INT is present in
  166. CPUID.
  167. 'Page not present' events are currently always delivered as synthetic
  168. #PF exception. During delivery of these events APF CR2 register contains
  169. a token that will be used to notify the guest when missing page becomes
  170. available. Also, to make it possible to distinguish between real #PF and
  171. APF, first 4 bytes of 64 byte memory location ('flags') will be written
  172. to by the hypervisor at the time of injection. Only first bit of 'flags'
  173. is currently supported, when set, it indicates that the guest is dealing
  174. with asynchronous 'page not present' event. If during a page fault APF
  175. 'flags' is '0' it means that this is regular page fault. Guest is
  176. supposed to clear 'flags' when it is done handling #PF exception so the
  177. next event can be delivered.
  178. Note, since APF 'page not present' events use the same exception vector
  179. as regular page fault, guest must reset 'flags' to '0' before it does
  180. something that can generate normal page fault.
  181. Bytes 5-7 of 64 byte memory location ('token') will be written to by the
  182. hypervisor at the time of APF 'page ready' event injection. The content
  183. of these bytes is a token which was previously delivered as 'page not
  184. present' event. The event indicates the page in now available. Guest is
  185. supposed to write '0' to 'token' when it is done handling 'page ready'
  186. event and to write 1' to MSR_KVM_ASYNC_PF_ACK after clearing the location;
  187. writing to the MSR forces KVM to re-scan its queue and deliver the next
  188. pending notification.
  189. Note, MSR_KVM_ASYNC_PF_INT MSR specifying the interrupt vector for 'page
  190. ready' APF delivery needs to be written to before enabling APF mechanism
  191. in MSR_KVM_ASYNC_PF_EN or interrupt #0 can get injected. The MSR is
  192. available if KVM_FEATURE_ASYNC_PF_INT is present in CPUID.
  193. Note, previously, 'page ready' events were delivered via the same #PF
  194. exception as 'page not present' events but this is now deprecated. If
  195. bit 3 (interrupt based delivery) is not set APF events are not delivered.
  196. If APF is disabled while there are outstanding APFs, they will
  197. not be delivered.
  198. Currently 'page ready' APF events will be always delivered on the
  199. same vcpu as 'page not present' event was, but guest should not rely on
  200. that.
  201. MSR_KVM_STEAL_TIME:
  202. 0x4b564d03
  203. data:
  204. 64-byte alignment physical address of a memory area which must be
  205. in guest RAM, plus an enable bit in bit 0. This memory is expected to
  206. hold a copy of the following structure::
  207. struct kvm_steal_time {
  208. __u64 steal;
  209. __u32 version;
  210. __u32 flags;
  211. __u8 preempted;
  212. __u8 u8_pad[3];
  213. __u32 pad[11];
  214. }
  215. whose data will be filled in by the hypervisor periodically. Only one
  216. write, or registration, is needed for each VCPU. The interval between
  217. updates of this structure is arbitrary and implementation-dependent.
  218. The hypervisor may update this structure at any time it sees fit until
  219. anything with bit0 == 0 is written to it. Guest is required to make sure
  220. this structure is initialized to zero.
  221. Fields have the following meanings:
  222. version:
  223. a sequence counter. In other words, guest has to check
  224. this field before and after grabbing time information and make
  225. sure they are both equal and even. An odd version indicates an
  226. in-progress update.
  227. flags:
  228. At this point, always zero. May be used to indicate
  229. changes in this structure in the future.
  230. steal:
  231. the amount of time in which this vCPU did not run, in
  232. nanoseconds. Time during which the vcpu is idle, will not be
  233. reported as steal time.
  234. preempted:
  235. indicate the vCPU who owns this struct is running or
  236. not. Non-zero values mean the vCPU has been preempted. Zero
  237. means the vCPU is not preempted. NOTE, it is always zero if the
  238. the hypervisor doesn't support this field.
  239. MSR_KVM_EOI_EN:
  240. 0x4b564d04
  241. data:
  242. Bit 0 is 1 when PV end of interrupt is enabled on the vcpu; 0
  243. when disabled. Bit 1 is reserved and must be zero. When PV end of
  244. interrupt is enabled (bit 0 set), bits 63-2 hold a 4-byte aligned
  245. physical address of a 4 byte memory area which must be in guest RAM and
  246. must be zeroed.
  247. The first, least significant bit of 4 byte memory location will be
  248. written to by the hypervisor, typically at the time of interrupt
  249. injection. Value of 1 means that guest can skip writing EOI to the apic
  250. (using MSR or MMIO write); instead, it is sufficient to signal
  251. EOI by clearing the bit in guest memory - this location will
  252. later be polled by the hypervisor.
  253. Value of 0 means that the EOI write is required.
  254. It is always safe for the guest to ignore the optimization and perform
  255. the APIC EOI write anyway.
  256. Hypervisor is guaranteed to only modify this least
  257. significant bit while in the current VCPU context, this means that
  258. guest does not need to use either lock prefix or memory ordering
  259. primitives to synchronise with the hypervisor.
  260. However, hypervisor can set and clear this memory bit at any time:
  261. therefore to make sure hypervisor does not interrupt the
  262. guest and clear the least significant bit in the memory area
  263. in the window between guest testing it to detect
  264. whether it can skip EOI apic write and between guest
  265. clearing it to signal EOI to the hypervisor,
  266. guest must both read the least significant bit in the memory area and
  267. clear it using a single CPU instruction, such as test and clear, or
  268. compare and exchange.
  269. MSR_KVM_POLL_CONTROL:
  270. 0x4b564d05
  271. Control host-side polling.
  272. data:
  273. Bit 0 enables (1) or disables (0) host-side HLT polling logic.
  274. KVM guests can request the host not to poll on HLT, for example if
  275. they are performing polling themselves.
  276. MSR_KVM_ASYNC_PF_INT:
  277. 0x4b564d06
  278. data:
  279. Second asynchronous page fault (APF) control MSR.
  280. Bits 0-7: APIC vector for delivery of 'page ready' APF events.
  281. Bits 8-63: Reserved
  282. Interrupt vector for asynchnonous 'page ready' notifications delivery.
  283. The vector has to be set up before asynchronous page fault mechanism
  284. is enabled in MSR_KVM_ASYNC_PF_EN. The MSR is only available if
  285. KVM_FEATURE_ASYNC_PF_INT is present in CPUID.
  286. MSR_KVM_ASYNC_PF_ACK:
  287. 0x4b564d07
  288. data:
  289. Asynchronous page fault (APF) acknowledgment.
  290. When the guest is done processing 'page ready' APF event and 'token'
  291. field in 'struct kvm_vcpu_pv_apf_data' is cleared it is supposed to
  292. write '1' to bit 0 of the MSR, this causes the host to re-scan its queue
  293. and check if there are more notifications pending. The MSR is available
  294. if KVM_FEATURE_ASYNC_PF_INT is present in CPUID.
  295. MSR_KVM_MIGRATION_CONTROL:
  296. 0x4b564d08
  297. data:
  298. This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in
  299. CPUID. Bit 0 represents whether live migration of the guest is allowed.
  300. When a guest is started, bit 0 will be 0 if the guest has encrypted
  301. memory and 1 if the guest does not have encrypted memory. If the
  302. guest is communicating page encryption status to the host using the
  303. ``KVM_HC_MAP_GPA_RANGE`` hypercall, it can set bit 0 in this MSR to
  304. allow live migration of the guest.