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