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