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