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