msr.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =================
  3. KVM-specific MSRs
  4. =================
  5. :Author: Glauber Costa <glommer@redhat.com>, 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. This memory is expected to hold the
  150. 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. };
  158. Bits 5-4 of the MSR are reserved and should be zero. Bit 0 is set to 1
  159. when asynchronous page faults are enabled on the vcpu, 0 when disabled.
  160. Bit 1 is 1 if asynchronous page faults can be injected when vcpu is in
  161. cpl == 0. Bit 2 is 1 if asynchronous page faults are delivered to L1 as
  162. #PF vmexits. Bit 2 can be set only if KVM_FEATURE_ASYNC_PF_VMEXIT is
  163. present in CPUID. Bit 3 enables interrupt based delivery of 'page ready'
  164. events. Bit 3 can only be set if KVM_FEATURE_ASYNC_PF_INT is present in
  165. CPUID.
  166. 'Page not present' events are currently always delivered as synthetic
  167. #PF exception. During delivery of these events APF CR2 register contains
  168. a token that will be used to notify the guest when missing page becomes
  169. available. Also, to make it possible to distinguish between real #PF and
  170. APF, first 4 bytes of 64 byte memory location ('flags') will be written
  171. to by the hypervisor at the time of injection. Only first bit of 'flags'
  172. is currently supported, when set, it indicates that the guest is dealing
  173. with asynchronous 'page not present' event. If during a page fault APF
  174. 'flags' is '0' it means that this is regular page fault. Guest is
  175. supposed to clear 'flags' when it is done handling #PF exception so the
  176. next event can be delivered.
  177. Note, since APF 'page not present' events use the same exception vector
  178. as regular page fault, guest must reset 'flags' to '0' before it does
  179. something that can generate normal page fault.
  180. Bytes 4-7 of 64 byte memory location ('token') will be written to by the
  181. hypervisor at the time of APF 'page ready' event injection. The content
  182. of these bytes is a token which was previously delivered in CR2 as
  183. 'page not present' event. The event indicates the page is now available.
  184. Guest is supposed to write '0' to 'token' when it is done handling
  185. 'page ready' event and to write '1' to MSR_KVM_ASYNC_PF_ACK after
  186. clearing the location; writing to the MSR forces KVM to re-scan its
  187. queue and deliver the next pending notification.
  188. Note, MSR_KVM_ASYNC_PF_INT MSR specifying the interrupt vector for 'page
  189. ready' APF delivery needs to be written to before enabling APF mechanism
  190. in MSR_KVM_ASYNC_PF_EN or interrupt #0 can get injected. The MSR is
  191. available if KVM_FEATURE_ASYNC_PF_INT is present in CPUID.
  192. Note, previously, 'page ready' events were delivered via the same #PF
  193. exception as 'page not present' events but this is now deprecated. If
  194. bit 3 (interrupt based delivery) is not set APF events are not delivered.
  195. If APF is disabled while there are outstanding APFs, they will
  196. not be delivered.
  197. Currently 'page ready' APF events will be always delivered on the
  198. same vcpu as 'page not present' event was, but guest should not rely on
  199. that.
  200. MSR_KVM_STEAL_TIME:
  201. 0x4b564d03
  202. data:
  203. 64-byte alignment physical address of a memory area which must be
  204. in guest RAM, plus an enable bit in bit 0. This memory is expected to
  205. hold a copy of the following structure::
  206. struct kvm_steal_time {
  207. __u64 steal;
  208. __u32 version;
  209. __u32 flags;
  210. __u8 preempted;
  211. __u8 u8_pad[3];
  212. __u32 pad[11];
  213. }
  214. whose data will be filled in by the hypervisor periodically. Only one
  215. write, or registration, is needed for each VCPU. The interval between
  216. updates of this structure is arbitrary and implementation-dependent.
  217. The hypervisor may update this structure at any time it sees fit until
  218. anything with bit0 == 0 is written to it. Guest is required to make sure
  219. this structure is initialized to zero.
  220. Fields have the following meanings:
  221. version:
  222. a sequence counter. In other words, guest has to check
  223. this field before and after grabbing time information and make
  224. sure they are both equal and even. An odd version indicates an
  225. in-progress update.
  226. flags:
  227. At this point, always zero. May be used to indicate
  228. changes in this structure in the future.
  229. steal:
  230. the amount of time in which this vCPU did not run, in
  231. nanoseconds. Time during which the vcpu is idle, will not be
  232. reported as steal time.
  233. preempted:
  234. indicate the vCPU who owns this struct is running or
  235. not. Non-zero values mean the vCPU has been preempted. Zero
  236. means the vCPU is not preempted. NOTE, it is always zero if the
  237. the hypervisor doesn't support this field.
  238. MSR_KVM_EOI_EN:
  239. 0x4b564d04
  240. data:
  241. Bit 0 is 1 when PV end of interrupt is enabled on the vcpu; 0
  242. when disabled. Bit 1 is reserved and must be zero. When PV end of
  243. interrupt is enabled (bit 0 set), bits 63-2 hold a 4-byte aligned
  244. physical address of a 4 byte memory area which must be in guest RAM and
  245. must be zeroed.
  246. The first, least significant bit of 4 byte memory location will be
  247. written to by the hypervisor, typically at the time of interrupt
  248. injection. Value of 1 means that guest can skip writing EOI to the apic
  249. (using MSR or MMIO write); instead, it is sufficient to signal
  250. EOI by clearing the bit in guest memory - this location will
  251. later be polled by the hypervisor.
  252. Value of 0 means that the EOI write is required.
  253. It is always safe for the guest to ignore the optimization and perform
  254. the APIC EOI write anyway.
  255. Hypervisor is guaranteed to only modify this least
  256. significant bit while in the current VCPU context, this means that
  257. guest does not need to use either lock prefix or memory ordering
  258. primitives to synchronise with the hypervisor.
  259. However, hypervisor can set and clear this memory bit at any time:
  260. therefore to make sure hypervisor does not interrupt the
  261. guest and clear the least significant bit in the memory area
  262. in the window between guest testing it to detect
  263. whether it can skip EOI apic write and between guest
  264. clearing it to signal EOI to the hypervisor,
  265. guest must both read the least significant bit in the memory area and
  266. clear it using a single CPU instruction, such as test and clear, or
  267. compare and exchange.
  268. MSR_KVM_POLL_CONTROL:
  269. 0x4b564d05
  270. Control host-side polling.
  271. data:
  272. Bit 0 enables (1) or disables (0) host-side HLT polling logic.
  273. KVM guests can request the host not to poll on HLT, for example if
  274. they are performing polling themselves.
  275. MSR_KVM_ASYNC_PF_INT:
  276. 0x4b564d06
  277. data:
  278. Second asynchronous page fault (APF) control MSR.
  279. Bits 0-7: APIC vector for delivery of 'page ready' APF events.
  280. Bits 8-63: Reserved
  281. Interrupt vector for asynchnonous 'page ready' notifications delivery.
  282. The vector has to be set up before asynchronous page fault mechanism
  283. is enabled in MSR_KVM_ASYNC_PF_EN. The MSR is only available if
  284. KVM_FEATURE_ASYNC_PF_INT is present in CPUID.
  285. MSR_KVM_ASYNC_PF_ACK:
  286. 0x4b564d07
  287. data:
  288. Asynchronous page fault (APF) acknowledgment.
  289. When the guest is done processing 'page ready' APF event and 'token'
  290. field in 'struct kvm_vcpu_pv_apf_data' is cleared it is supposed to
  291. write '1' to bit 0 of the MSR, this causes the host to re-scan its queue
  292. and check if there are more notifications pending. The MSR is available
  293. if KVM_FEATURE_ASYNC_PF_INT is present in CPUID.
  294. MSR_KVM_MIGRATION_CONTROL:
  295. 0x4b564d08
  296. data:
  297. This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in
  298. CPUID. Bit 0 represents whether live migration of the guest is allowed.
  299. When a guest is started, bit 0 will be 0 if the guest has encrypted
  300. memory and 1 if the guest does not have encrypted memory. If the
  301. guest is communicating page encryption status to the host using the
  302. ``KVM_HC_MAP_GPA_RANGE`` hypercall, it can set bit 0 in this MSR to
  303. allow live migration of the guest.