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