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