]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/X86Msr.c
2 IA-32/x64 MSR functions.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "BaseLibInternals.h"
14 Returns the lower 32-bits of a Machine Specific Register(MSR).
16 Reads and returns the lower 32-bits of the MSR specified by Index.
17 No parameter checking is performed on Index, and some Index values may cause
18 CPU exceptions. The caller must either guarantee that Index is valid, or the
19 caller must set up exception handlers to catch the exceptions. This function
20 is only available on IA-32 and x64.
22 @param Index The 32-bit MSR index to read.
24 @return The lower 32 bits of the MSR identified by Index.
33 return (UINT32
)AsmReadMsr64 (Index
);
37 Writes a 32-bit value to a Machine Specific Register(MSR), and returns the value.
38 The upper 32-bits of the MSR are set to zero.
40 Writes the 32-bit value specified by Value to the MSR specified by Index. The
41 upper 32-bits of the MSR write are set to zero. The 32-bit value written to
42 the MSR is returned. No parameter checking is performed on Index or Value,
43 and some of these may cause CPU exceptions. The caller must either guarantee
44 that Index and Value are valid, or the caller must establish proper exception
45 handlers. This function is only available on IA-32 and x64.
47 @param Index The 32-bit MSR index to write.
48 @param Value The 32-bit value to write to the MSR.
60 return (UINT32
)AsmWriteMsr64 (Index
, Value
);
64 Reads a 64-bit MSR, performs a bitwise OR on the lower 32-bits, and
65 writes the result back to the 64-bit MSR.
67 Reads the 64-bit MSR specified by Index, performs a bitwise OR
68 between the lower 32-bits of the read result and the value specified by
69 OrData, and writes the result to the 64-bit MSR specified by Index. The lower
70 32-bits of the value written to the MSR is returned. No parameter checking is
71 performed on Index or OrData, and some of these may cause CPU exceptions. The
72 caller must either guarantee that Index and OrData are valid, or the caller
73 must establish proper exception handlers. This function is only available on
76 @param Index The 32-bit MSR index to write.
77 @param OrData The value to OR with the read value from the MSR.
79 @return The lower 32-bit value written to the MSR.
89 return (UINT32
)AsmMsrOr64 (Index
, OrData
);
93 Reads a 64-bit MSR, performs a bitwise AND on the lower 32-bits, and writes
94 the result back to the 64-bit MSR.
96 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
97 lower 32-bits of the read result and the value specified by AndData, and
98 writes the result to the 64-bit MSR specified by Index. The lower 32-bits of
99 the value written to the MSR is returned. No parameter checking is performed
100 on Index or AndData, and some of these may cause CPU exceptions. The caller
101 must either guarantee that Index and AndData are valid, or the caller must
102 establish proper exception handlers. This function is only available on IA-32
105 @param Index The 32-bit MSR index to write.
106 @param AndData The value to AND with the read value from the MSR.
108 @return The lower 32-bit value written to the MSR.
118 return (UINT32
)AsmMsrAnd64 (Index
, AndData
);
122 Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise OR
123 on the lower 32-bits, and writes the result back to the 64-bit MSR.
125 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
126 lower 32-bits of the read result and the value specified by AndData
127 preserving the upper 32-bits, performs a bitwise OR between the
128 result of the AND operation and the value specified by OrData, and writes the
129 result to the 64-bit MSR specified by Address. The lower 32-bits of the value
130 written to the MSR is returned. No parameter checking is performed on Index,
131 AndData, or OrData, and some of these may cause CPU exceptions. The caller
132 must either guarantee that Index, AndData, and OrData are valid, or the
133 caller must establish proper exception handlers. This function is only
134 available on IA-32 and x64.
136 @param Index The 32-bit MSR index to write.
137 @param AndData The value to AND with the read value from the MSR.
138 @param OrData The value to OR with the result of the AND operation.
140 @return The lower 32-bit value written to the MSR.
151 return (UINT32
)AsmMsrAndThenOr64 (Index
, AndData
, OrData
);
155 Reads a bit field of an MSR.
157 Reads the bit field in the lower 32-bits of a 64-bit MSR. The bit field is
158 specified by the StartBit and the EndBit. The value of the bit field is
159 returned. The caller must either guarantee that Index is valid, or the caller
160 must set up exception handlers to catch the exceptions. This function is only
161 available on IA-32 and x64.
163 If StartBit is greater than 31, then ASSERT().
164 If EndBit is greater than 31, then ASSERT().
165 If EndBit is less than StartBit, then ASSERT().
167 @param Index The 32-bit MSR index to read.
168 @param StartBit The ordinal of the least significant bit in the bit field.
170 @param EndBit The ordinal of the most significant bit in the bit field.
173 @return The bit field read from the MSR.
178 AsmMsrBitFieldRead32 (
184 return BitFieldRead32 (AsmReadMsr32 (Index
), StartBit
, EndBit
);
188 Writes a bit field to an MSR.
190 Writes Value to a bit field in the lower 32-bits of a 64-bit MSR. The bit
191 field is specified by the StartBit and the EndBit. All other bits in the
192 destination MSR are preserved. The lower 32-bits of the MSR written is
193 returned. The caller must either guarantee that Index and the data written
194 is valid, or the caller must set up exception handlers to catch the exceptions.
195 This function is only available on IA-32 and x64.
197 If StartBit is greater than 31, then ASSERT().
198 If EndBit is greater than 31, then ASSERT().
199 If EndBit is less than StartBit, then ASSERT().
200 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
202 @param Index The 32-bit MSR index to write.
203 @param StartBit The ordinal of the least significant bit in the bit field.
205 @param EndBit The ordinal of the most significant bit in the bit field.
207 @param Value The new value of the bit field.
209 @return The lower 32-bit of the value written to the MSR.
214 AsmMsrBitFieldWrite32 (
221 ASSERT (EndBit
< sizeof (Value
) * 8);
222 ASSERT (StartBit
<= EndBit
);
223 return (UINT32
)AsmMsrBitFieldWrite64 (Index
, StartBit
, EndBit
, Value
);
227 Reads a bit field in a 64-bit MSR, performs a bitwise OR, and writes the
228 result back to the bit field in the 64-bit MSR.
230 Reads the 64-bit MSR specified by Index, performs a bitwise OR
231 between the read result and the value specified by OrData, and writes the
232 result to the 64-bit MSR specified by Index. The lower 32-bits of the value
233 written to the MSR are returned. Extra left bits in OrData are stripped. The
234 caller must either guarantee that Index and the data written is valid, or
235 the caller must set up exception handlers to catch the exceptions. This
236 function is only available on IA-32 and x64.
238 If StartBit is greater than 31, then ASSERT().
239 If EndBit is greater than 31, then ASSERT().
240 If EndBit is less than StartBit, then ASSERT().
241 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
243 @param Index The 32-bit MSR index to write.
244 @param StartBit The ordinal of the least significant bit in the bit field.
246 @param EndBit The ordinal of the most significant bit in the bit field.
248 @param OrData The value to OR with the read value from the MSR.
250 @return The lower 32-bit of the value written to the MSR.
262 ASSERT (EndBit
< sizeof (OrData
) * 8);
263 ASSERT (StartBit
<= EndBit
);
264 return (UINT32
)AsmMsrBitFieldOr64 (Index
, StartBit
, EndBit
, OrData
);
268 Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
269 result back to the bit field in the 64-bit MSR.
271 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
272 read result and the value specified by AndData, and writes the result to the
273 64-bit MSR specified by Index. The lower 32-bits of the value written to the
274 MSR are returned. Extra left bits in AndData are stripped. The caller must
275 either guarantee that Index and the data written is valid, or the caller must
276 set up exception handlers to catch the exceptions. This function is only
277 available on IA-32 and x64.
279 If StartBit is greater than 31, then ASSERT().
280 If EndBit is greater than 31, then ASSERT().
281 If EndBit is less than StartBit, then ASSERT().
282 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
284 @param Index The 32-bit MSR index to write.
285 @param StartBit The ordinal of the least significant bit in the bit field.
287 @param EndBit The ordinal of the most significant bit in the bit field.
289 @param AndData The value to AND with the read value from the MSR.
291 @return The lower 32-bit of the value written to the MSR.
296 AsmMsrBitFieldAnd32 (
303 ASSERT (EndBit
< sizeof (AndData
) * 8);
304 ASSERT (StartBit
<= EndBit
);
305 return (UINT32
)AsmMsrBitFieldAnd64 (Index
, StartBit
, EndBit
, AndData
);
309 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
310 bitwise OR, and writes the result back to the bit field in the
313 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by a
314 bitwise OR between the read result and the value specified by
315 AndData, and writes the result to the 64-bit MSR specified by Index. The
316 lower 32-bits of the value written to the MSR are returned. Extra left bits
317 in both AndData and OrData are stripped. The caller must either guarantee
318 that Index and the data written is valid, or the caller must set up exception
319 handlers to catch the exceptions. This function is only available on IA-32
322 If StartBit is greater than 31, then ASSERT().
323 If EndBit is greater than 31, then ASSERT().
324 If EndBit is less than StartBit, then ASSERT().
325 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
326 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
328 @param Index The 32-bit MSR index to write.
329 @param StartBit The ordinal of the least significant bit in the bit field.
331 @param EndBit The ordinal of the most significant bit in the bit field.
333 @param AndData The value to AND with the read value from the MSR.
334 @param OrData The value to OR with the result of the AND operation.
336 @return The lower 32-bit of the value written to the MSR.
341 AsmMsrBitFieldAndThenOr32 (
349 ASSERT (EndBit
< sizeof (AndData
) * 8);
350 ASSERT (StartBit
<= EndBit
);
351 return (UINT32
)AsmMsrBitFieldAndThenOr64 (
361 Reads a 64-bit MSR, performs a bitwise OR, and writes the result
362 back to the 64-bit MSR.
364 Reads the 64-bit MSR specified by Index, performs a bitwise OR
365 between the read result and the value specified by OrData, and writes the
366 result to the 64-bit MSR specified by Index. The value written to the MSR is
367 returned. No parameter checking is performed on Index or OrData, and some of
368 these may cause CPU exceptions. The caller must either guarantee that Index
369 and OrData are valid, or the caller must establish proper exception handlers.
370 This function is only available on IA-32 and x64.
372 @param Index The 32-bit MSR index to write.
373 @param OrData The value to OR with the read value from the MSR.
375 @return The value written back to the MSR.
385 return AsmWriteMsr64 (Index
, AsmReadMsr64 (Index
) | OrData
);
389 Reads a 64-bit MSR, performs a bitwise AND, and writes the result back to the
392 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
393 read result and the value specified by OrData, and writes the result to the
394 64-bit MSR specified by Index. The value written to the MSR is returned. No
395 parameter checking is performed on Index or OrData, and some of these may
396 cause CPU exceptions. The caller must either guarantee that Index and OrData
397 are valid, or the caller must establish proper exception handlers. This
398 function is only available on IA-32 and x64.
400 @param Index The 32-bit MSR index to write.
401 @param AndData The value to AND with the read value from the MSR.
403 @return The value written back to the MSR.
413 return AsmWriteMsr64 (Index
, AsmReadMsr64 (Index
) & AndData
);
417 Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise
418 OR, and writes the result back to the 64-bit MSR.
420 Reads the 64-bit MSR specified by Index, performs a bitwise AND between read
421 result and the value specified by AndData, performs a bitwise OR
422 between the result of the AND operation and the value specified by OrData,
423 and writes the result to the 64-bit MSR specified by Index. The value written
424 to the MSR is returned. No parameter checking is performed on Index, AndData,
425 or OrData, and some of these may cause CPU exceptions. The caller must either
426 guarantee that Index, AndData, and OrData are valid, or the caller must
427 establish proper exception handlers. This function is only available on IA-32
430 @param Index The 32-bit MSR index to write.
431 @param AndData The value to AND with the read value from the MSR.
432 @param OrData The value to OR with the result of the AND operation.
434 @return The value written back to the MSR.
445 return AsmWriteMsr64 (Index
, (AsmReadMsr64 (Index
) & AndData
) | OrData
);
449 Reads a bit field of an MSR.
451 Reads the bit field in the 64-bit MSR. The bit field is specified by the
452 StartBit and the EndBit. The value of the bit field is returned. The caller
453 must either guarantee that Index is valid, or the caller must set up
454 exception handlers to catch the exceptions. This function is only available
457 If StartBit is greater than 63, then ASSERT().
458 If EndBit is greater than 63, then ASSERT().
459 If EndBit is less than StartBit, then ASSERT().
461 @param Index The 32-bit MSR index to read.
462 @param StartBit The ordinal of the least significant bit in the bit field.
464 @param EndBit The ordinal of the most significant bit in the bit field.
467 @return The value read from the MSR.
472 AsmMsrBitFieldRead64 (
478 return BitFieldRead64 (AsmReadMsr64 (Index
), StartBit
, EndBit
);
482 Writes a bit field to an MSR.
484 Writes Value to a bit field in a 64-bit MSR. The bit field is specified by
485 the StartBit and the EndBit. All other bits in the destination MSR are
486 preserved. The MSR written is returned. The caller must either guarantee
487 that Index and the data written is valid, or the caller must set up exception
488 handlers to catch the exceptions. This function is only available on IA-32 and x64.
490 If StartBit is greater than 63, then ASSERT().
491 If EndBit is greater than 63, then ASSERT().
492 If EndBit is less than StartBit, then ASSERT().
493 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
495 @param Index The 32-bit MSR index to write.
496 @param StartBit The ordinal of the least significant bit in the bit field.
498 @param EndBit The ordinal of the most significant bit in the bit field.
500 @param Value The new value of the bit field.
502 @return The value written back to the MSR.
507 AsmMsrBitFieldWrite64 (
514 return AsmWriteMsr64 (
516 BitFieldWrite64 (AsmReadMsr64 (Index
), StartBit
, EndBit
, Value
)
521 Reads a bit field in a 64-bit MSR, performs a bitwise OR, and
522 writes the result back to the bit field in the 64-bit MSR.
524 Reads the 64-bit MSR specified by Index, performs a bitwise OR
525 between the read result and the value specified by OrData, and writes the
526 result to the 64-bit MSR specified by Index. The value written to the MSR is
527 returned. Extra left bits in OrData are stripped. The caller must either
528 guarantee that Index and the data written is valid, or the caller must set up
529 exception handlers to catch the exceptions. This function is only available
532 If StartBit is greater than 63, then ASSERT().
533 If EndBit is greater than 63, then ASSERT().
534 If EndBit is less than StartBit, then ASSERT().
535 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
537 @param Index The 32-bit MSR index to write.
538 @param StartBit The ordinal of the least significant bit in the bit field.
540 @param EndBit The ordinal of the most significant bit in the bit field.
542 @param OrData The value to OR with the read value from the bit field.
544 @return The value written back to the MSR.
556 return AsmWriteMsr64 (
558 BitFieldOr64 (AsmReadMsr64 (Index
), StartBit
, EndBit
, OrData
)
563 Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
564 result back to the bit field in the 64-bit MSR.
566 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
567 read result and the value specified by AndData, and writes the result to the
568 64-bit MSR specified by Index. The value written to the MSR is returned.
569 Extra left bits in AndData are stripped. The caller must either guarantee
570 that Index and the data written is valid, or the caller must set up exception
571 handlers to catch the exceptions. This function is only available on IA-32
574 If StartBit is greater than 63, then ASSERT().
575 If EndBit is greater than 63, then ASSERT().
576 If EndBit is less than StartBit, then ASSERT().
577 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
579 @param Index The 32-bit MSR index to write.
580 @param StartBit The ordinal of the least significant bit in the bit field.
582 @param EndBit The ordinal of the most significant bit in the bit field.
584 @param AndData The value to AND with the read value from the bit field.
586 @return The value written back to the MSR.
591 AsmMsrBitFieldAnd64 (
598 return AsmWriteMsr64 (
600 BitFieldAnd64 (AsmReadMsr64 (Index
), StartBit
, EndBit
, AndData
)
605 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
606 bitwise OR, and writes the result back to the bit field in the
609 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by
610 a bitwise OR between the read result and the value specified by
611 AndData, and writes the result to the 64-bit MSR specified by Index. The
612 value written to the MSR is returned. Extra left bits in both AndData and
613 OrData are stripped. The caller must either guarantee that Index and the data
614 written is valid, or the caller must set up exception handlers to catch the
615 exceptions. This function is only available on IA-32 and x64.
617 If StartBit is greater than 63, then ASSERT().
618 If EndBit is greater than 63, then ASSERT().
619 If EndBit is less than StartBit, then ASSERT().
620 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
621 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
623 @param Index The 32-bit MSR index to write.
624 @param StartBit The ordinal of the least significant bit in the bit field.
626 @param EndBit The ordinal of the most significant bit in the bit field.
628 @param AndData The value to AND with the read value from the bit field.
629 @param OrData The value to OR with the result of the AND operation.
631 @return The value written back to the MSR.
636 AsmMsrBitFieldAndThenOr64 (
644 return AsmWriteMsr64 (
646 BitFieldAndThenOr64 (
647 AsmReadMsr64 (Index
),