]>
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
9 #include "BaseLibInternals.h"
12 Returns the lower 32-bits of a Machine Specific Register(MSR).
14 Reads and returns the lower 32-bits of the MSR specified by Index.
15 No parameter checking is performed on Index, and some Index values may cause
16 CPU exceptions. The caller must either guarantee that Index is valid, or the
17 caller must set up exception handlers to catch the exceptions. This function
18 is only available on IA-32 and x64.
20 @param Index The 32-bit MSR index to read.
22 @return The lower 32 bits of the MSR identified by Index.
31 return (UINT32
)AsmReadMsr64 (Index
);
35 Writes a 32-bit value to a Machine Specific Register(MSR), and returns the value.
36 The upper 32-bits of the MSR are set to zero.
38 Writes the 32-bit value specified by Value to the MSR specified by Index. The
39 upper 32-bits of the MSR write are set to zero. The 32-bit value written to
40 the MSR is returned. No parameter checking is performed on Index or Value,
41 and some of these may cause CPU exceptions. The caller must either guarantee
42 that Index and Value are valid, or the caller must establish proper exception
43 handlers. This function is only available on IA-32 and x64.
45 @param Index The 32-bit MSR index to write.
46 @param Value The 32-bit value to write to the MSR.
58 return (UINT32
)AsmWriteMsr64 (Index
, Value
);
62 Reads a 64-bit MSR, performs a bitwise OR on the lower 32-bits, and
63 writes the result back to the 64-bit MSR.
65 Reads the 64-bit MSR specified by Index, performs a bitwise OR
66 between the lower 32-bits of the read result and the value specified by
67 OrData, and writes the result to the 64-bit MSR specified by Index. The lower
68 32-bits of the value written to the MSR is returned. No parameter checking is
69 performed on Index or OrData, and some of these may cause CPU exceptions. The
70 caller must either guarantee that Index and OrData are valid, or the caller
71 must establish proper exception handlers. This function is only available on
74 @param Index The 32-bit MSR index to write.
75 @param OrData The value to OR with the read value from the MSR.
77 @return The lower 32-bit value written to the MSR.
87 return (UINT32
)AsmMsrOr64 (Index
, OrData
);
91 Reads a 64-bit MSR, performs a bitwise AND on the lower 32-bits, and writes
92 the result back to the 64-bit MSR.
94 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
95 lower 32-bits of the read result and the value specified by AndData, and
96 writes the result to the 64-bit MSR specified by Index. The lower 32-bits of
97 the value written to the MSR is returned. No parameter checking is performed
98 on Index or AndData, and some of these may cause CPU exceptions. The caller
99 must either guarantee that Index and AndData are valid, or the caller must
100 establish proper exception handlers. This function is only available on IA-32
103 @param Index The 32-bit MSR index to write.
104 @param AndData The value to AND with the read value from the MSR.
106 @return The lower 32-bit value written to the MSR.
116 return (UINT32
)AsmMsrAnd64 (Index
, AndData
);
120 Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise OR
121 on the lower 32-bits, and writes the result back to the 64-bit MSR.
123 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
124 lower 32-bits of the read result and the value specified by AndData
125 preserving the upper 32-bits, performs a bitwise OR between the
126 result of the AND operation and the value specified by OrData, and writes the
127 result to the 64-bit MSR specified by Address. The lower 32-bits of the value
128 written to the MSR is returned. No parameter checking is performed on Index,
129 AndData, or OrData, and some of these may cause CPU exceptions. The caller
130 must either guarantee that Index, AndData, and OrData are valid, or the
131 caller must establish proper exception handlers. This function is only
132 available on IA-32 and x64.
134 @param Index The 32-bit MSR index to write.
135 @param AndData The value to AND with the read value from the MSR.
136 @param OrData The value to OR with the result of the AND operation.
138 @return The lower 32-bit value written to the MSR.
149 return (UINT32
)AsmMsrAndThenOr64 (Index
, AndData
, OrData
);
153 Reads a bit field of an MSR.
155 Reads the bit field in the lower 32-bits of a 64-bit MSR. The bit field is
156 specified by the StartBit and the EndBit. The value of the bit field is
157 returned. The caller must either guarantee that Index is valid, or the caller
158 must set up exception handlers to catch the exceptions. This function is only
159 available on IA-32 and x64.
161 If StartBit is greater than 31, then ASSERT().
162 If EndBit is greater than 31, then ASSERT().
163 If EndBit is less than StartBit, then ASSERT().
165 @param Index The 32-bit MSR index to read.
166 @param StartBit The ordinal of the least significant bit in the bit field.
168 @param EndBit The ordinal of the most significant bit in the bit field.
171 @return The bit field read from the MSR.
176 AsmMsrBitFieldRead32 (
182 return BitFieldRead32 (AsmReadMsr32 (Index
), StartBit
, EndBit
);
186 Writes a bit field to an MSR.
188 Writes Value to a bit field in the lower 32-bits of a 64-bit MSR. The bit
189 field is specified by the StartBit and the EndBit. All other bits in the
190 destination MSR are preserved. The lower 32-bits of the MSR written is
191 returned. The caller must either guarantee that Index and the data written
192 is valid, or the caller must set up exception handlers to catch the exceptions.
193 This function is only available on IA-32 and x64.
195 If StartBit is greater than 31, then ASSERT().
196 If EndBit is greater than 31, then ASSERT().
197 If EndBit is less than StartBit, then ASSERT().
198 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
200 @param Index The 32-bit MSR index to write.
201 @param StartBit The ordinal of the least significant bit in the bit field.
203 @param EndBit The ordinal of the most significant bit in the bit field.
205 @param Value The new value of the bit field.
207 @return The lower 32-bit of the value written to the MSR.
212 AsmMsrBitFieldWrite32 (
219 ASSERT (EndBit
< sizeof (Value
) * 8);
220 ASSERT (StartBit
<= EndBit
);
221 return (UINT32
)AsmMsrBitFieldWrite64 (Index
, StartBit
, EndBit
, Value
);
225 Reads a bit field in a 64-bit MSR, performs a bitwise OR, and writes the
226 result back to the bit field in the 64-bit MSR.
228 Reads the 64-bit MSR specified by Index, performs a bitwise OR
229 between the read result and the value specified by OrData, and writes the
230 result to the 64-bit MSR specified by Index. The lower 32-bits of the value
231 written to the MSR are returned. Extra left bits in OrData are stripped. The
232 caller must either guarantee that Index and the data written is valid, or
233 the caller must set up exception handlers to catch the exceptions. This
234 function is only available on IA-32 and x64.
236 If StartBit is greater than 31, then ASSERT().
237 If EndBit is greater than 31, then ASSERT().
238 If EndBit is less than StartBit, then ASSERT().
239 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
241 @param Index The 32-bit MSR index to write.
242 @param StartBit The ordinal of the least significant bit in the bit field.
244 @param EndBit The ordinal of the most significant bit in the bit field.
246 @param OrData The value to OR with the read value from the MSR.
248 @return The lower 32-bit of the value written to the MSR.
260 ASSERT (EndBit
< sizeof (OrData
) * 8);
261 ASSERT (StartBit
<= EndBit
);
262 return (UINT32
)AsmMsrBitFieldOr64 (Index
, StartBit
, EndBit
, OrData
);
266 Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
267 result back to the bit field in the 64-bit MSR.
269 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
270 read result and the value specified by AndData, and writes the result to the
271 64-bit MSR specified by Index. The lower 32-bits of the value written to the
272 MSR are returned. Extra left bits in AndData are stripped. The caller must
273 either guarantee that Index and the data written is valid, or the caller must
274 set up exception handlers to catch the exceptions. This function is only
275 available on IA-32 and x64.
277 If StartBit is greater than 31, then ASSERT().
278 If EndBit is greater than 31, then ASSERT().
279 If EndBit is less than StartBit, then ASSERT().
280 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
282 @param Index The 32-bit MSR index to write.
283 @param StartBit The ordinal of the least significant bit in the bit field.
285 @param EndBit The ordinal of the most significant bit in the bit field.
287 @param AndData The value to AND with the read value from the MSR.
289 @return The lower 32-bit of the value written to the MSR.
294 AsmMsrBitFieldAnd32 (
301 ASSERT (EndBit
< sizeof (AndData
) * 8);
302 ASSERT (StartBit
<= EndBit
);
303 return (UINT32
)AsmMsrBitFieldAnd64 (Index
, StartBit
, EndBit
, AndData
);
307 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
308 bitwise OR, and writes the result back to the bit field in the
311 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by a
312 bitwise OR between the read result and the value specified by
313 AndData, and writes the result to the 64-bit MSR specified by Index. The
314 lower 32-bits of the value written to the MSR are returned. Extra left bits
315 in both AndData and OrData are stripped. The caller must either guarantee
316 that Index and the data written is valid, or the caller must set up exception
317 handlers to catch the exceptions. This function is only available on IA-32
320 If StartBit is greater than 31, then ASSERT().
321 If EndBit is greater than 31, then ASSERT().
322 If EndBit is less than StartBit, then ASSERT().
323 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
324 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
326 @param Index The 32-bit MSR index to write.
327 @param StartBit The ordinal of the least significant bit in the bit field.
329 @param EndBit The ordinal of the most significant bit in the bit field.
331 @param AndData The value to AND with the read value from the MSR.
332 @param OrData The value to OR with the result of the AND operation.
334 @return The lower 32-bit of the value written to the MSR.
339 AsmMsrBitFieldAndThenOr32 (
347 ASSERT (EndBit
< sizeof (AndData
) * 8);
348 ASSERT (StartBit
<= EndBit
);
349 return (UINT32
)AsmMsrBitFieldAndThenOr64 (
359 Reads a 64-bit MSR, performs a bitwise OR, and writes the result
360 back to the 64-bit MSR.
362 Reads the 64-bit MSR specified by Index, performs a bitwise OR
363 between the read result and the value specified by OrData, and writes the
364 result to the 64-bit MSR specified by Index. The value written to the MSR is
365 returned. No parameter checking is performed on Index or OrData, and some of
366 these may cause CPU exceptions. The caller must either guarantee that Index
367 and OrData are valid, or the caller must establish proper exception handlers.
368 This function is only available on IA-32 and x64.
370 @param Index The 32-bit MSR index to write.
371 @param OrData The value to OR with the read value from the MSR.
373 @return The value written back to the MSR.
383 return AsmWriteMsr64 (Index
, AsmReadMsr64 (Index
) | OrData
);
387 Reads a 64-bit MSR, performs a bitwise AND, and writes the result back to the
390 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
391 read result and the value specified by OrData, and writes the result to the
392 64-bit MSR specified by Index. The value written to the MSR is returned. No
393 parameter checking is performed on Index or OrData, and some of these may
394 cause CPU exceptions. The caller must either guarantee that Index and OrData
395 are valid, or the caller must establish proper exception handlers. This
396 function is only available on IA-32 and x64.
398 @param Index The 32-bit MSR index to write.
399 @param AndData The value to AND with the read value from the MSR.
401 @return The value written back to the MSR.
411 return AsmWriteMsr64 (Index
, AsmReadMsr64 (Index
) & AndData
);
415 Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise
416 OR, and writes the result back to the 64-bit MSR.
418 Reads the 64-bit MSR specified by Index, performs a bitwise AND between read
419 result and the value specified by AndData, performs a bitwise OR
420 between the result of the AND operation and the value specified by OrData,
421 and writes the result to the 64-bit MSR specified by Index. The value written
422 to the MSR is returned. No parameter checking is performed on Index, AndData,
423 or OrData, and some of these may cause CPU exceptions. The caller must either
424 guarantee that Index, AndData, and OrData are valid, or the caller must
425 establish proper exception handlers. This function is only available on IA-32
428 @param Index The 32-bit MSR index to write.
429 @param AndData The value to AND with the read value from the MSR.
430 @param OrData The value to OR with the result of the AND operation.
432 @return The value written back to the MSR.
443 return AsmWriteMsr64 (Index
, (AsmReadMsr64 (Index
) & AndData
) | OrData
);
447 Reads a bit field of an MSR.
449 Reads the bit field in the 64-bit MSR. The bit field is specified by the
450 StartBit and the EndBit. The value of the bit field is returned. The caller
451 must either guarantee that Index is valid, or the caller must set up
452 exception handlers to catch the exceptions. This function is only available
455 If StartBit is greater than 63, then ASSERT().
456 If EndBit is greater than 63, then ASSERT().
457 If EndBit is less than StartBit, then ASSERT().
459 @param Index The 32-bit MSR index to read.
460 @param StartBit The ordinal of the least significant bit in the bit field.
462 @param EndBit The ordinal of the most significant bit in the bit field.
465 @return The value read from the MSR.
470 AsmMsrBitFieldRead64 (
476 return BitFieldRead64 (AsmReadMsr64 (Index
), StartBit
, EndBit
);
480 Writes a bit field to an MSR.
482 Writes Value to a bit field in a 64-bit MSR. The bit field is specified by
483 the StartBit and the EndBit. All other bits in the destination MSR are
484 preserved. The MSR written is returned. The caller must either guarantee
485 that Index and the data written is valid, or the caller must set up exception
486 handlers to catch the exceptions. This function is only available on IA-32 and x64.
488 If StartBit is greater than 63, then ASSERT().
489 If EndBit is greater than 63, then ASSERT().
490 If EndBit is less than StartBit, then ASSERT().
491 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
493 @param Index The 32-bit MSR index to write.
494 @param StartBit The ordinal of the least significant bit in the bit field.
496 @param EndBit The ordinal of the most significant bit in the bit field.
498 @param Value The new value of the bit field.
500 @return The value written back to the MSR.
505 AsmMsrBitFieldWrite64 (
512 return AsmWriteMsr64 (
514 BitFieldWrite64 (AsmReadMsr64 (Index
), StartBit
, EndBit
, Value
)
519 Reads a bit field in a 64-bit MSR, performs a bitwise OR, and
520 writes the result back to the bit field in the 64-bit MSR.
522 Reads the 64-bit MSR specified by Index, performs a bitwise OR
523 between the read result and the value specified by OrData, and writes the
524 result to the 64-bit MSR specified by Index. The value written to the MSR is
525 returned. Extra left bits in OrData are stripped. The caller must either
526 guarantee that Index and the data written is valid, or the caller must set up
527 exception handlers to catch the exceptions. This function is only available
530 If StartBit is greater than 63, then ASSERT().
531 If EndBit is greater than 63, then ASSERT().
532 If EndBit is less than StartBit, then ASSERT().
533 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
535 @param Index The 32-bit MSR index to write.
536 @param StartBit The ordinal of the least significant bit in the bit field.
538 @param EndBit The ordinal of the most significant bit in the bit field.
540 @param OrData The value to OR with the read value from the bit field.
542 @return The value written back to the MSR.
554 return AsmWriteMsr64 (
556 BitFieldOr64 (AsmReadMsr64 (Index
), StartBit
, EndBit
, OrData
)
561 Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
562 result back to the bit field in the 64-bit MSR.
564 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
565 read result and the value specified by AndData, and writes the result to the
566 64-bit MSR specified by Index. The value written to the MSR is returned.
567 Extra left bits in AndData are stripped. The caller must either guarantee
568 that Index and the data written is valid, or the caller must set up exception
569 handlers to catch the exceptions. This function is only available on IA-32
572 If StartBit is greater than 63, then ASSERT().
573 If EndBit is greater than 63, then ASSERT().
574 If EndBit is less than StartBit, then ASSERT().
575 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
577 @param Index The 32-bit MSR index to write.
578 @param StartBit The ordinal of the least significant bit in the bit field.
580 @param EndBit The ordinal of the most significant bit in the bit field.
582 @param AndData The value to AND with the read value from the bit field.
584 @return The value written back to the MSR.
589 AsmMsrBitFieldAnd64 (
596 return AsmWriteMsr64 (
598 BitFieldAnd64 (AsmReadMsr64 (Index
), StartBit
, EndBit
, AndData
)
603 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
604 bitwise OR, and writes the result back to the bit field in the
607 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by
608 a bitwise OR between the read result and the value specified by
609 AndData, and writes the result to the 64-bit MSR specified by Index. The
610 value written to the MSR is returned. Extra left bits in both AndData and
611 OrData are stripped. The caller must either guarantee that Index and the data
612 written is valid, or the caller must set up exception handlers to catch the
613 exceptions. This function is only available on IA-32 and x64.
615 If StartBit is greater than 63, then ASSERT().
616 If EndBit is greater than 63, then ASSERT().
617 If EndBit is less than StartBit, then ASSERT().
618 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
619 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
621 @param Index The 32-bit MSR index to write.
622 @param StartBit The ordinal of the least significant bit in the bit field.
624 @param EndBit The ordinal of the most significant bit in the bit field.
626 @param AndData The value to AND with the read value from the bit field.
627 @param OrData The value to OR with the result of the AND operation.
629 @return The value written back to the MSR.
634 AsmMsrBitFieldAndThenOr64 (
642 return AsmWriteMsr64 (
644 BitFieldAndThenOr64 (
645 AsmReadMsr64 (Index
),