]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/X86Msr.c
Update the text to use "x64" instead of "X64" in MdePkg.
[mirror_edk2.git] / MdePkg / Library / BaseLib / X86Msr.c
1 /** @file
2 IA-32/x64 MSR functions.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "BaseLibInternals.h"
17
18
19 /**
20 Returns the lower 32-bits of a Machine Specific Register(MSR).
21
22 Reads and returns the lower 32-bits of the MSR specified by Index.
23 No parameter checking is performed on Index, and some Index values may cause
24 CPU exceptions. The caller must either guarantee that Index is valid, or the
25 caller must set up exception handlers to catch the exceptions. This function
26 is only available on IA-32 and x64.
27
28 @param Index The 32-bit MSR index to read.
29
30 @return The lower 32 bits of the MSR identified by Index.
31
32 **/
33 UINT32
34 EFIAPI
35 AsmReadMsr32 (
36 IN UINT32 Index
37 )
38 {
39 return (UINT32)AsmReadMsr64 (Index);
40 }
41
42 /**
43 Zero-extend a 32-bit value and writes it to a Machine Specific Register(MSR).
44
45 Writes the 32-bit value specified by Value to the MSR specified by Index. The
46 upper 32-bits of the MSR write are set to zero. The 32-bit value written to
47 the MSR is returned. No parameter checking is performed on Index or Value,
48 and some of these may cause CPU exceptions. The caller must either guarantee
49 that Index and Value are valid, or the caller must establish proper exception
50 handlers. This function is only available on IA-32 and x64.
51
52 @param Index The 32-bit MSR index to write.
53 @param Value The 32-bit value to write to the MSR.
54
55 @return Value
56
57 **/
58 UINT32
59 EFIAPI
60 AsmWriteMsr32 (
61 IN UINT32 Index,
62 IN UINT32 Value
63 )
64 {
65 return (UINT32)AsmWriteMsr64 (Index, Value);
66 }
67
68 /**
69 Reads a 64-bit MSR, performs a bitwise inclusive OR on the lower 32-bits, and
70 writes the result back to the 64-bit MSR.
71
72 Reads the 64-bit MSR specified by Index, performs a bitwise inclusive OR
73 between the lower 32-bits of the read result and the value specified by
74 OrData, and writes the result to the 64-bit MSR specified by Index. The lower
75 32-bits of the value written to the MSR is returned. No parameter checking is
76 performed on Index or OrData, and some of these may cause CPU exceptions. The
77 caller must either guarantee that Index and OrData are valid, or the caller
78 must establish proper exception handlers. This function is only available on
79 IA-32 and x64.
80
81 @param Index The 32-bit MSR index to write.
82 @param OrData The value to OR with the read value from the MSR.
83
84 @return The lower 32-bit value written to the MSR.
85
86 **/
87 UINT32
88 EFIAPI
89 AsmMsrOr32 (
90 IN UINT32 Index,
91 IN UINT32 OrData
92 )
93 {
94 return (UINT32)AsmMsrOr64 (Index, OrData);
95 }
96
97 /**
98 Reads a 64-bit MSR, performs a bitwise AND on the lower 32-bits, and writes
99 the result back to the 64-bit MSR.
100
101 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
102 lower 32-bits of the read result and the value specified by AndData, and
103 writes the result to the 64-bit MSR specified by Index. The lower 32-bits of
104 the value written to the MSR is returned. No parameter checking is performed
105 on Index or AndData, and some of these may cause CPU exceptions. The caller
106 must either guarantee that Index and AndData are valid, or the caller must
107 establish proper exception handlers. This function is only available on IA-32
108 and x64.
109
110 @param Index The 32-bit MSR index to write.
111 @param AndData The value to AND with the read value from the MSR.
112
113 @return The lower 32-bit value written to the MSR.
114
115 **/
116 UINT32
117 EFIAPI
118 AsmMsrAnd32 (
119 IN UINT32 Index,
120 IN UINT32 AndData
121 )
122 {
123 return (UINT32)AsmMsrAnd64 (Index, AndData);
124 }
125
126 /**
127 Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise inclusive OR
128 on the lower 32-bits, and writes the result back to the 64-bit MSR.
129
130 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
131 lower 32-bits of the read result and the value specified by AndData
132 preserving the upper 32-bits, performs a bitwise inclusive OR between the
133 result of the AND operation and the value specified by OrData, and writes the
134 result to the 64-bit MSR specified by Address. The lower 32-bits of the value
135 written to the MSR is returned. No parameter checking is performed on Index,
136 AndData, or OrData, and some of these may cause CPU exceptions. The caller
137 must either guarantee that Index, AndData, and OrData are valid, or the
138 caller must establish proper exception handlers. This function is only
139 available on IA-32 and x64.
140
141 @param Index The 32-bit MSR index to write.
142 @param AndData The value to AND with the read value from the MSR.
143 @param OrData The value to OR with the result of the AND operation.
144
145 @return The lower 32-bit value written to the MSR.
146
147 **/
148 UINT32
149 EFIAPI
150 AsmMsrAndThenOr32 (
151 IN UINT32 Index,
152 IN UINT32 AndData,
153 IN UINT32 OrData
154 )
155 {
156 return (UINT32)AsmMsrAndThenOr64 (Index, AndData, OrData);
157 }
158
159 /**
160 Reads a bit field of an MSR.
161
162 Reads the bit field in the lower 32-bits of a 64-bit MSR. The bit field is
163 specified by the StartBit and the EndBit. The value of the bit field is
164 returned. The caller must either guarantee that Index is valid, or the caller
165 must set up exception handlers to catch the exceptions. This function is only
166 available on IA-32 and x64.
167
168 If StartBit is greater than 31, then ASSERT().
169 If EndBit is greater than 31, then ASSERT().
170 If EndBit is less than StartBit, then ASSERT().
171
172 @param Index The 32-bit MSR index to read.
173 @param StartBit The ordinal of the least significant bit in the bit field.
174 Range 0..31.
175 @param EndBit The ordinal of the most significant bit in the bit field.
176 Range 0..31.
177
178 @return The bit field read from the MSR.
179
180 **/
181 UINT32
182 EFIAPI
183 AsmMsrBitFieldRead32 (
184 IN UINT32 Index,
185 IN UINTN StartBit,
186 IN UINTN EndBit
187 )
188 {
189 return BitFieldRead32 (AsmReadMsr32 (Index), StartBit, EndBit);
190 }
191
192 /**
193 Writes a bit field to an MSR.
194
195 Writes Value to a bit field in the lower 32-bits of a 64-bit MSR. The bit
196 field is specified by the StartBit and the EndBit. All other bits in the
197 destination MSR are preserved. The lower 32-bits of the MSR written is
198 returned. Extra left bits in Value are stripped. The caller must either
199 guarantee that Index and the data written is valid, or the caller must set up
200 exception handlers to catch the exceptions. This function is only available
201 on IA-32 and x64.
202
203 If StartBit is greater than 31, then ASSERT().
204 If EndBit is greater than 31, then ASSERT().
205 If EndBit is less than StartBit, then ASSERT().
206
207 @param Index The 32-bit MSR index to write.
208 @param StartBit The ordinal of the least significant bit in the bit field.
209 Range 0..31.
210 @param EndBit The ordinal of the most significant bit in the bit field.
211 Range 0..31.
212 @param Value New value of the bit field.
213
214 @return The lower 32-bit of the value written to the MSR.
215
216 **/
217 UINT32
218 EFIAPI
219 AsmMsrBitFieldWrite32 (
220 IN UINT32 Index,
221 IN UINTN StartBit,
222 IN UINTN EndBit,
223 IN UINT32 Value
224 )
225 {
226 ASSERT (EndBit < sizeof (Value) * 8);
227 ASSERT (StartBit <= EndBit);
228 return (UINT32)AsmMsrBitFieldWrite64 (Index, StartBit, EndBit, Value);
229 }
230
231 /**
232 Reads a bit field in a 64-bit MSR, performs a bitwise OR, and writes the
233 result back to the bit field in the 64-bit MSR.
234
235 Reads the 64-bit MSR specified by Index, performs a bitwise inclusive OR
236 between the read result and the value specified by OrData, and writes the
237 result to the 64-bit MSR specified by Index. The lower 32-bits of the value
238 written to the MSR are returned. Extra left bits in OrData are stripped. The
239 caller must either guarantee that Index and the data written is valid, or
240 the caller must set up exception handlers to catch the exceptions. This
241 function is only available on IA-32 and x64.
242
243 If StartBit is greater than 31, then ASSERT().
244 If EndBit is greater than 31, then ASSERT().
245 If EndBit is less than StartBit, then ASSERT().
246
247 @param Index The 32-bit MSR index to write.
248 @param StartBit The ordinal of the least significant bit in the bit field.
249 Range 0..31.
250 @param EndBit The ordinal of the most significant bit in the bit field.
251 Range 0..31.
252 @param OrData The value to OR with the read value from the MSR.
253
254 @return The lower 32-bit of the value written to the MSR.
255
256 **/
257 UINT32
258 EFIAPI
259 AsmMsrBitFieldOr32 (
260 IN UINT32 Index,
261 IN UINTN StartBit,
262 IN UINTN EndBit,
263 IN UINT32 OrData
264 )
265 {
266 ASSERT (EndBit < sizeof (OrData) * 8);
267 ASSERT (StartBit <= EndBit);
268 return (UINT32)AsmMsrBitFieldOr64 (Index, StartBit, EndBit, OrData);
269 }
270
271 /**
272 Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
273 result back to the bit field in the 64-bit MSR.
274
275 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
276 read result and the value specified by AndData, and writes the result to the
277 64-bit MSR specified by Index. The lower 32-bits of the value written to the
278 MSR are returned. Extra left bits in AndData are stripped. The caller must
279 either guarantee that Index and the data written is valid, or the caller must
280 set up exception handlers to catch the exceptions. This function is only
281 available on IA-32 and x64.
282
283 If StartBit is greater than 31, then ASSERT().
284 If EndBit is greater than 31, then ASSERT().
285 If EndBit is less than StartBit, then ASSERT().
286
287 @param Index The 32-bit MSR index to write.
288 @param StartBit The ordinal of the least significant bit in the bit field.
289 Range 0..31.
290 @param EndBit The ordinal of the most significant bit in the bit field.
291 Range 0..31.
292 @param AndData The value to AND with the read value from the MSR.
293
294 @return The lower 32-bit of the value written to the MSR.
295
296 **/
297 UINT32
298 EFIAPI
299 AsmMsrBitFieldAnd32 (
300 IN UINT32 Index,
301 IN UINTN StartBit,
302 IN UINTN EndBit,
303 IN UINT32 AndData
304 )
305 {
306 ASSERT (EndBit < sizeof (AndData) * 8);
307 ASSERT (StartBit <= EndBit);
308 return (UINT32)AsmMsrBitFieldAnd64 (Index, StartBit, EndBit, AndData);
309 }
310
311 /**
312 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
313 bitwise inclusive OR, and writes the result back to the bit field in the
314 64-bit MSR.
315
316 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by a
317 bitwise inclusive OR between the read result and the value specified by
318 AndData, and writes the result to the 64-bit MSR specified by Index. The
319 lower 32-bits of the value written to the MSR are returned. Extra left bits
320 in both AndData and OrData are stripped. The caller must either guarantee
321 that Index and the data written is valid, or the caller must set up exception
322 handlers to catch the exceptions. This function is only available on IA-32
323 and x64.
324
325 If StartBit is greater than 31, then ASSERT().
326 If EndBit is greater than 31, then ASSERT().
327 If EndBit is less than StartBit, then ASSERT().
328
329 @param Index The 32-bit MSR index to write.
330 @param StartBit The ordinal of the least significant bit in the bit field.
331 Range 0..31.
332 @param EndBit The ordinal of the most significant bit in the bit field.
333 Range 0..31.
334 @param AndData The value to AND with the read value from the MSR.
335 @param OrData The value to OR with the result of the AND operation.
336
337 @return The lower 32-bit of the value written to the MSR.
338
339 **/
340 UINT32
341 EFIAPI
342 AsmMsrBitFieldAndThenOr32 (
343 IN UINT32 Index,
344 IN UINTN StartBit,
345 IN UINTN EndBit,
346 IN UINT32 AndData,
347 IN UINT32 OrData
348 )
349 {
350 ASSERT (EndBit < sizeof (AndData) * 8);
351 ASSERT (StartBit <= EndBit);
352 return (UINT32)AsmMsrBitFieldAndThenOr64 (
353 Index,
354 StartBit,
355 EndBit,
356 AndData,
357 OrData
358 );
359 }
360
361 /**
362 Reads a 64-bit MSR, performs a bitwise inclusive OR, and writes the result
363 back to the 64-bit MSR.
364
365 Reads the 64-bit MSR specified by Index, performs a bitwise inclusive OR
366 between the read result and the value specified by OrData, and writes the
367 result to the 64-bit MSR specified by Index. The value written to the MSR is
368 returned. No parameter checking is performed on Index or OrData, and some of
369 these may cause CPU exceptions. The caller must either guarantee that Index
370 and OrData are valid, or the caller must establish proper exception handlers.
371 This function is only available on IA-32 and x64.
372
373 @param Index The 32-bit MSR index to write.
374 @param OrData The value to OR with the read value from the MSR.
375
376 @return The value written back to the MSR.
377
378 **/
379 UINT64
380 EFIAPI
381 AsmMsrOr64 (
382 IN UINT32 Index,
383 IN UINT64 OrData
384 )
385 {
386 return AsmWriteMsr64 (Index, AsmReadMsr64 (Index) | OrData);
387 }
388
389 /**
390 Reads a 64-bit MSR, performs a bitwise AND, and writes the result back to the
391 64-bit MSR.
392
393 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
394 read result and the value specified by OrData, and writes the result to the
395 64-bit MSR specified by Index. The value written to the MSR is returned. No
396 parameter checking is performed on Index or OrData, and some of these may
397 cause CPU exceptions. The caller must either guarantee that Index and OrData
398 are valid, or the caller must establish proper exception handlers. This
399 function is only available on IA-32 and x64.
400
401 @param Index The 32-bit MSR index to write.
402 @param AndData The value to AND with the read value from the MSR.
403
404 @return The value written back to the MSR.
405
406 **/
407 UINT64
408 EFIAPI
409 AsmMsrAnd64 (
410 IN UINT32 Index,
411 IN UINT64 AndData
412 )
413 {
414 return AsmWriteMsr64 (Index, AsmReadMsr64 (Index) & AndData);
415 }
416
417 /**
418 Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise inclusive
419 OR, and writes the result back to the 64-bit MSR.
420
421 Reads the 64-bit MSR specified by Index, performs a bitwise AND between read
422 result and the value specified by AndData, performs a bitwise inclusive OR
423 between the result of the AND operation and the value specified by OrData,
424 and writes the result to the 64-bit MSR specified by Index. The value written
425 to the MSR is returned. No parameter checking is performed on Index, AndData,
426 or OrData, and some of these may cause CPU exceptions. The caller must either
427 guarantee that Index, AndData, and OrData are valid, or the caller must
428 establish proper exception handlers. This function is only available on IA-32
429 and x64.
430
431 @param Index The 32-bit MSR index to write.
432 @param AndData The value to AND with the read value from the MSR.
433 @param OrData The value to OR with the result of the AND operation.
434
435 @return The value written back to the MSR.
436
437 **/
438 UINT64
439 EFIAPI
440 AsmMsrAndThenOr64 (
441 IN UINT32 Index,
442 IN UINT64 AndData,
443 IN UINT64 OrData
444 )
445 {
446 return AsmWriteMsr64 (Index, (AsmReadMsr64 (Index) & AndData) | OrData);
447 }
448
449 /**
450 Reads a bit field of an MSR.
451
452 Reads the bit field in the 64-bit MSR. The bit field is specified by the
453 StartBit and the EndBit. The value of the bit field is returned. The caller
454 must either guarantee that Index is valid, or the caller must set up
455 exception handlers to catch the exceptions. This function is only available
456 on IA-32 and x64.
457
458 If StartBit is greater than 63, then ASSERT().
459 If EndBit is greater than 63, then ASSERT().
460 If EndBit is less than StartBit, then ASSERT().
461
462 @param Index The 32-bit MSR index to read.
463 @param StartBit The ordinal of the least significant bit in the bit field.
464 Range 0..63.
465 @param EndBit The ordinal of the most significant bit in the bit field.
466 Range 0..63.
467
468 @return The value written back to the MSR.
469
470 **/
471 UINT64
472 EFIAPI
473 AsmMsrBitFieldRead64 (
474 IN UINT32 Index,
475 IN UINTN StartBit,
476 IN UINTN EndBit
477 )
478 {
479 return BitFieldRead64 (AsmReadMsr64 (Index), StartBit, EndBit);
480 }
481
482 /**
483 Writes a bit field to an MSR.
484
485 Writes Value to a bit field in a 64-bit MSR. The bit field is specified by
486 the StartBit and the EndBit. All other bits in the destination MSR are
487 preserved. The MSR written is returned. Extra left bits in Value are
488 stripped. The caller must either guarantee that Index and the data written is
489 valid, or the caller must set up exception handlers to catch the exceptions.
490 This function is only available on IA-32 and x64.
491
492 If StartBit is greater than 63, then ASSERT().
493 If EndBit is greater than 63, then ASSERT().
494 If EndBit is less than StartBit, then ASSERT().
495
496 @param Index The 32-bit MSR index to write.
497 @param StartBit The ordinal of the least significant bit in the bit field.
498 Range 0..63.
499 @param EndBit The ordinal of the most significant bit in the bit field.
500 Range 0..63.
501 @param Value New value of the bit field.
502
503 @return The value written back to the MSR.
504
505 **/
506 UINT64
507 EFIAPI
508 AsmMsrBitFieldWrite64 (
509 IN UINT32 Index,
510 IN UINTN StartBit,
511 IN UINTN EndBit,
512 IN UINT64 Value
513 )
514 {
515 return AsmWriteMsr64 (
516 Index,
517 BitFieldWrite64 (AsmReadMsr64 (Index), StartBit, EndBit, Value)
518 );
519 }
520
521 /**
522 Reads a bit field in a 64-bit MSR, performs a bitwise inclusive OR, and
523 writes the result back to the bit field in the 64-bit MSR.
524
525 Reads the 64-bit MSR specified by Index, performs a bitwise inclusive OR
526 between the read result and the value specified by OrData, and writes the
527 result to the 64-bit MSR specified by Index. The value written to the MSR is
528 returned. Extra left bits in OrData are stripped. The caller must either
529 guarantee that Index and the data written is valid, or the caller must set up
530 exception handlers to catch the exceptions. This function is only available
531 on IA-32 and x64.
532
533 If StartBit is greater than 63, then ASSERT().
534 If EndBit is greater than 63, then ASSERT().
535 If EndBit is less than StartBit, then ASSERT().
536
537 @param Index The 32-bit MSR index to write.
538 @param StartBit The ordinal of the least significant bit in the bit field.
539 Range 0..63.
540 @param EndBit The ordinal of the most significant bit in the bit field.
541 Range 0..63.
542 @param OrData The value to OR with the read value from the bit field.
543
544 @return The value written back to the MSR.
545
546 **/
547 UINT64
548 EFIAPI
549 AsmMsrBitFieldOr64 (
550 IN UINT32 Index,
551 IN UINTN StartBit,
552 IN UINTN EndBit,
553 IN UINT64 OrData
554 )
555 {
556 return AsmWriteMsr64 (
557 Index,
558 BitFieldOr64 (AsmReadMsr64 (Index), StartBit, EndBit, OrData)
559 );
560 }
561
562 /**
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.
565
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
572 and x64.
573
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
578 @param Index The 32-bit MSR index to write.
579 @param StartBit The ordinal of the least significant bit in the bit field.
580 Range 0..63.
581 @param EndBit The ordinal of the most significant bit in the bit field.
582 Range 0..63.
583 @param AndData The value to AND with the read value from the bit field.
584
585 @return The value written back to the MSR.
586
587 **/
588 UINT64
589 EFIAPI
590 AsmMsrBitFieldAnd64 (
591 IN UINT32 Index,
592 IN UINTN StartBit,
593 IN UINTN EndBit,
594 IN UINT64 AndData
595 )
596 {
597 return AsmWriteMsr64 (
598 Index,
599 BitFieldAnd64 (AsmReadMsr64 (Index), StartBit, EndBit, AndData)
600 );
601 }
602
603 /**
604 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
605 bitwise inclusive OR, and writes the result back to the bit field in the
606 64-bit MSR.
607
608 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by
609 a bitwise inclusive OR between the read result and the value specified by
610 AndData, and writes the result to the 64-bit MSR specified by Index. The
611 value written to the MSR is returned. Extra left bits in both AndData and
612 OrData are stripped. The caller must either guarantee that Index and the data
613 written is valid, or the caller must set up exception handlers to catch the
614 exceptions. This function is only available on IA-32 and x64.
615
616 If StartBit is greater than 63, then ASSERT().
617 If EndBit is greater than 63, then ASSERT().
618 If EndBit is less than StartBit, then ASSERT().
619
620 @param Index The 32-bit MSR index to write.
621 @param StartBit The ordinal of the least significant bit in the bit field.
622 Range 0..63.
623 @param EndBit The ordinal of the most significant bit in the bit field.
624 Range 0..63.
625 @param AndData The value to AND with the read value from the bit field.
626 @param OrData The value to OR with the result of the AND operation.
627
628 @return The value written back to the MSR.
629
630 **/
631 UINT64
632 EFIAPI
633 AsmMsrBitFieldAndThenOr64 (
634 IN UINT32 Index,
635 IN UINTN StartBit,
636 IN UINTN EndBit,
637 IN UINT64 AndData,
638 IN UINT64 OrData
639 )
640 {
641 return AsmWriteMsr64 (
642 Index,
643 BitFieldAndThenOr64 (
644 AsmReadMsr64 (Index),
645 StartBit,
646 EndBit,
647 AndData,
648 OrData
649 )
650 );
651 }