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