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