]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/X86Msr.c
Make comments match the Spec.
[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 "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 Zero-extend a 32-bit value and writes it to a Machine Specific Register(MSR).
44
45 Writes the 32-bit value specified by Value to the MSR specified by Index. The
46 upper 32-bits of the MSR write are set to zero. The 32-bit value written to
47 the MSR is returned. No parameter checking is performed on Index or Value,
48 and some of these may cause CPU exceptions. The caller must either guarantee
49 that Index and Value are valid, or the caller must establish proper exception
50 handlers. This function is only available on IA-32 and x64.
51
52 @param Index The 32-bit MSR index to write.
53 @param Value The 32-bit value to write to the MSR.
54
55 @return Value
56
57 **/
58 UINT32
59 EFIAPI
60 AsmWriteMsr32 (
61 IN UINT32 Index,
62 IN UINT32 Value
63 )
64 {
65 return (UINT32)AsmWriteMsr64 (Index, Value);
66 }
67
68 /**
69 Reads a 64-bit MSR, performs a bitwise OR on the lower 32-bits, and
70 writes the result back to the 64-bit MSR.
71
72 Reads the 64-bit MSR specified by Index, performs a bitwise OR
73 between the lower 32-bits of the read result and the value specified by
74 OrData, and writes the result to the 64-bit MSR specified by Index. The lower
75 32-bits of the value written to the MSR is returned. No parameter checking is
76 performed on Index or OrData, and some of these may cause CPU exceptions. The
77 caller must either guarantee that Index and OrData are valid, or the caller
78 must establish proper exception handlers. This function is only available on
79 IA-32 and x64.
80
81 @param Index The 32-bit MSR index to write.
82 @param OrData The value to OR with the read value from the MSR.
83
84 @return The lower 32-bit value written to the MSR.
85
86 **/
87 UINT32
88 EFIAPI
89 AsmMsrOr32 (
90 IN UINT32 Index,
91 IN UINT32 OrData
92 )
93 {
94 return (UINT32)AsmMsrOr64 (Index, OrData);
95 }
96
97 /**
98 Reads a 64-bit MSR, performs a bitwise AND on the lower 32-bits, and writes
99 the result back to the 64-bit MSR.
100
101 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
102 lower 32-bits of the read result and the value specified by AndData, and
103 writes the result to the 64-bit MSR specified by Index. The lower 32-bits of
104 the value written to the MSR is returned. No parameter checking is performed
105 on Index or AndData, and some of these may cause CPU exceptions. The caller
106 must either guarantee that Index and AndData are valid, or the caller must
107 establish proper exception handlers. This function is only available on IA-32
108 and x64.
109
110 @param Index The 32-bit MSR index to write.
111 @param AndData The value to AND with the read value from the MSR.
112
113 @return The lower 32-bit value written to the MSR.
114
115 **/
116 UINT32
117 EFIAPI
118 AsmMsrAnd32 (
119 IN UINT32 Index,
120 IN UINT32 AndData
121 )
122 {
123 return (UINT32)AsmMsrAnd64 (Index, AndData);
124 }
125
126 /**
127 Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise OR
128 on the lower 32-bits, and writes the result back to the 64-bit MSR.
129
130 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
131 lower 32-bits of the read result and the value specified by AndData
132 preserving the upper 32-bits, performs a bitwise OR between the
133 result of the AND operation and the value specified by OrData, and writes the
134 result to the 64-bit MSR specified by Address. The lower 32-bits of the value
135 written to the MSR is returned. No parameter checking is performed on Index,
136 AndData, or OrData, and some of these may cause CPU exceptions. The caller
137 must either guarantee that Index, AndData, and OrData are valid, or the
138 caller must establish proper exception handlers. This function is only
139 available on IA-32 and x64.
140
141 @param Index The 32-bit MSR index to write.
142 @param AndData The value to AND with the read value from the MSR.
143 @param OrData The value to OR with the result of the AND operation.
144
145 @return The lower 32-bit value written to the MSR.
146
147 **/
148 UINT32
149 EFIAPI
150 AsmMsrAndThenOr32 (
151 IN UINT32 Index,
152 IN UINT32 AndData,
153 IN UINT32 OrData
154 )
155 {
156 return (UINT32)AsmMsrAndThenOr64 (Index, AndData, OrData);
157 }
158
159 /**
160 Reads a bit field of an MSR.
161
162 Reads the bit field in the lower 32-bits of a 64-bit MSR. The bit field is
163 specified by the StartBit and the EndBit. The value of the bit field is
164 returned. The caller must either guarantee that Index is valid, or the caller
165 must set up exception handlers to catch the exceptions. This function is only
166 available on IA-32 and x64.
167
168 If StartBit is greater than 31, then ASSERT().
169 If EndBit is greater than 31, then ASSERT().
170 If EndBit is less than StartBit, then ASSERT().
171
172 @param Index The 32-bit MSR index to read.
173 @param StartBit The ordinal of the least significant bit in the bit field.
174 Range 0..31.
175 @param EndBit The ordinal of the most significant bit in the bit field.
176 Range 0..31.
177
178 @return The bit field read from the MSR.
179
180 **/
181 UINT32
182 EFIAPI
183 AsmMsrBitFieldRead32 (
184 IN UINT32 Index,
185 IN UINTN StartBit,
186 IN UINTN EndBit
187 )
188 {
189 return BitFieldRead32 (AsmReadMsr32 (Index), StartBit, EndBit);
190 }
191
192 /**
193 Writes a bit field to an MSR.
194
195 Writes Value to a bit field in the lower 32-bits of a 64-bit MSR. The bit
196 field is specified by the StartBit and the EndBit. All other bits in the
197 destination MSR are preserved. The lower 32-bits of the MSR written is
198 returned. The caller must either guarantee that Index and the data written
199 is valid, or the caller must set up exception handlers to catch the exceptions.
200 This function is only available on IA-32 and x64.
201
202 If StartBit is greater than 31, then ASSERT().
203 If EndBit is greater than 31, then ASSERT().
204 If EndBit is less than StartBit, then ASSERT().
205
206 @param Index The 32-bit MSR index to write.
207 @param StartBit The ordinal of the least significant bit in the bit field.
208 Range 0..31.
209 @param EndBit The ordinal of the most significant bit in the bit field.
210 Range 0..31.
211 @param Value New value of the bit field.
212
213 @return The lower 32-bit of the value written to the MSR.
214
215 **/
216 UINT32
217 EFIAPI
218 AsmMsrBitFieldWrite32 (
219 IN UINT32 Index,
220 IN UINTN StartBit,
221 IN UINTN EndBit,
222 IN UINT32 Value
223 )
224 {
225 ASSERT (EndBit < sizeof (Value) * 8);
226 ASSERT (StartBit <= EndBit);
227 return (UINT32)AsmMsrBitFieldWrite64 (Index, StartBit, EndBit, Value);
228 }
229
230 /**
231 Reads a bit field in a 64-bit MSR, performs a bitwise OR, and writes the
232 result back to the bit field in the 64-bit MSR.
233
234 Reads the 64-bit MSR specified by Index, performs a bitwise OR
235 between the read result and the value specified by OrData, and writes the
236 result to the 64-bit MSR specified by Index. The lower 32-bits of the value
237 written to the MSR are returned. Extra left bits in OrData are stripped. The
238 caller must either guarantee that Index and the data written is valid, or
239 the caller must set up exception handlers to catch the exceptions. This
240 function is only available on IA-32 and x64.
241
242 If StartBit is greater than 31, then ASSERT().
243 If EndBit is greater than 31, then ASSERT().
244 If EndBit is less than StartBit, then ASSERT().
245
246 @param Index The 32-bit MSR index to write.
247 @param StartBit The ordinal of the least significant bit in the bit field.
248 Range 0..31.
249 @param EndBit The ordinal of the most significant bit in the bit field.
250 Range 0..31.
251 @param OrData The value to OR with the read value from the MSR.
252
253 @return The lower 32-bit of the value written to the MSR.
254
255 **/
256 UINT32
257 EFIAPI
258 AsmMsrBitFieldOr32 (
259 IN UINT32 Index,
260 IN UINTN StartBit,
261 IN UINTN EndBit,
262 IN UINT32 OrData
263 )
264 {
265 ASSERT (EndBit < sizeof (OrData) * 8);
266 ASSERT (StartBit <= EndBit);
267 return (UINT32)AsmMsrBitFieldOr64 (Index, StartBit, EndBit, OrData);
268 }
269
270 /**
271 Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
272 result back to the bit field in the 64-bit MSR.
273
274 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
275 read result and the value specified by AndData, and writes the result to the
276 64-bit MSR specified by Index. The lower 32-bits of the value written to the
277 MSR are returned. Extra left bits in AndData are stripped. The caller must
278 either guarantee that Index and the data written is valid, or the caller must
279 set up exception handlers to catch the exceptions. This function is only
280 available on IA-32 and x64.
281
282 If StartBit is greater than 31, then ASSERT().
283 If EndBit is greater than 31, then ASSERT().
284 If EndBit is less than StartBit, then ASSERT().
285
286 @param Index The 32-bit MSR index to write.
287 @param StartBit The ordinal of the least significant bit in the bit field.
288 Range 0..31.
289 @param EndBit The ordinal of the most significant bit in the bit field.
290 Range 0..31.
291 @param AndData The value to AND with the read value from the MSR.
292
293 @return The lower 32-bit of the value written to the MSR.
294
295 **/
296 UINT32
297 EFIAPI
298 AsmMsrBitFieldAnd32 (
299 IN UINT32 Index,
300 IN UINTN StartBit,
301 IN UINTN EndBit,
302 IN UINT32 AndData
303 )
304 {
305 ASSERT (EndBit < sizeof (AndData) * 8);
306 ASSERT (StartBit <= EndBit);
307 return (UINT32)AsmMsrBitFieldAnd64 (Index, StartBit, EndBit, AndData);
308 }
309
310 /**
311 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
312 bitwise OR, and writes the result back to the bit field in the
313 64-bit MSR.
314
315 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by a
316 bitwise OR between the read result and the value specified by
317 AndData, and writes the result to the 64-bit MSR specified by Index. The
318 lower 32-bits of the value written to the MSR are returned. Extra left bits
319 in both AndData and OrData are stripped. The caller must either guarantee
320 that Index and the data written is valid, or the caller must set up exception
321 handlers to catch the exceptions. This function is only available on IA-32
322 and x64.
323
324 If StartBit is greater than 31, then ASSERT().
325 If EndBit is greater than 31, then ASSERT().
326 If EndBit is less than StartBit, 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 written back to 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
494 @param Index The 32-bit MSR index to write.
495 @param StartBit The ordinal of the least significant bit in the bit field.
496 Range 0..63.
497 @param EndBit The ordinal of the most significant bit in the bit field.
498 Range 0..63.
499 @param Value New value of the bit field.
500
501 @return The value written back to the MSR.
502
503 **/
504 UINT64
505 EFIAPI
506 AsmMsrBitFieldWrite64 (
507 IN UINT32 Index,
508 IN UINTN StartBit,
509 IN UINTN EndBit,
510 IN UINT64 Value
511 )
512 {
513 return AsmWriteMsr64 (
514 Index,
515 BitFieldWrite64 (AsmReadMsr64 (Index), StartBit, EndBit, Value)
516 );
517 }
518
519 /**
520 Reads a bit field in a 64-bit MSR, performs a bitwise OR, and
521 writes the result back to the bit field in the 64-bit MSR.
522
523 Reads the 64-bit MSR specified by Index, performs a bitwise OR
524 between the read result and the value specified by OrData, and writes the
525 result to the 64-bit MSR specified by Index. The value written to the MSR is
526 returned. Extra left bits in OrData are stripped. The caller must either
527 guarantee that Index and the data written is valid, or the caller must set up
528 exception handlers to catch the exceptions. This function is only available
529 on IA-32 and x64.
530
531 If StartBit is greater than 63, then ASSERT().
532 If EndBit is greater than 63, then ASSERT().
533 If EndBit is less than StartBit, then ASSERT().
534
535 @param Index The 32-bit MSR index to write.
536 @param StartBit The ordinal of the least significant bit in the bit field.
537 Range 0..63.
538 @param EndBit The ordinal of the most significant bit in the bit field.
539 Range 0..63.
540 @param OrData The value to OR with the read value from the bit field.
541
542 @return The value written back to the MSR.
543
544 **/
545 UINT64
546 EFIAPI
547 AsmMsrBitFieldOr64 (
548 IN UINT32 Index,
549 IN UINTN StartBit,
550 IN UINTN EndBit,
551 IN UINT64 OrData
552 )
553 {
554 return AsmWriteMsr64 (
555 Index,
556 BitFieldOr64 (AsmReadMsr64 (Index), StartBit, EndBit, OrData)
557 );
558 }
559
560 /**
561 Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
562 result back to the bit field in the 64-bit MSR.
563
564 Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
565 read result and the value specified by AndData, and writes the result to the
566 64-bit MSR specified by Index. The value written to the MSR is returned.
567 Extra left bits in AndData are stripped. The caller must either guarantee
568 that Index and the data written is valid, or the caller must set up exception
569 handlers to catch the exceptions. This function is only available on IA-32
570 and x64.
571
572 If StartBit is greater than 63, then ASSERT().
573 If EndBit is greater than 63, then ASSERT().
574 If EndBit is less than StartBit, then ASSERT().
575
576 @param Index The 32-bit MSR index to write.
577 @param StartBit The ordinal of the least significant bit in the bit field.
578 Range 0..63.
579 @param EndBit The ordinal of the most significant bit in the bit field.
580 Range 0..63.
581 @param AndData The value to AND with the read value from the bit field.
582
583 @return The value written back to the MSR.
584
585 **/
586 UINT64
587 EFIAPI
588 AsmMsrBitFieldAnd64 (
589 IN UINT32 Index,
590 IN UINTN StartBit,
591 IN UINTN EndBit,
592 IN UINT64 AndData
593 )
594 {
595 return AsmWriteMsr64 (
596 Index,
597 BitFieldAnd64 (AsmReadMsr64 (Index), StartBit, EndBit, AndData)
598 );
599 }
600
601 /**
602 Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
603 bitwise OR, and writes the result back to the bit field in the
604 64-bit MSR.
605
606 Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by
607 a bitwise OR between the read result and the value specified by
608 AndData, and writes the result to the 64-bit MSR specified by Index. The
609 value written to the MSR is returned. Extra left bits in both AndData and
610 OrData are stripped. The caller must either guarantee that Index and the data
611 written is valid, or the caller must set up exception handlers to catch the
612 exceptions. This function is only available on IA-32 and x64.
613
614 If StartBit is greater than 63, then ASSERT().
615 If EndBit is greater than 63, then ASSERT().
616 If EndBit is less than StartBit, then ASSERT().
617
618 @param Index The 32-bit MSR index to write.
619 @param StartBit The ordinal of the least significant bit in the bit field.
620 Range 0..63.
621 @param EndBit The ordinal of the most significant bit in the bit field.
622 Range 0..63.
623 @param AndData The value to AND with the read value from the bit field.
624 @param OrData The value to OR with the result of the AND operation.
625
626 @return The value written back to the MSR.
627
628 **/
629 UINT64
630 EFIAPI
631 AsmMsrBitFieldAndThenOr64 (
632 IN UINT32 Index,
633 IN UINTN StartBit,
634 IN UINTN EndBit,
635 IN UINT64 AndData,
636 IN UINT64 OrData
637 )
638 {
639 return AsmWriteMsr64 (
640 Index,
641 BitFieldAndThenOr64 (
642 AsmReadMsr64 (Index),
643 StartBit,
644 EndBit,
645 AndData,
646 OrData
647 )
648 );
649 }