2 Declaration of internal functions in BaseLib.
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #ifndef __BASE_LIB_INTERNALS__
10 #define __BASE_LIB_INTERNALS__
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/PcdLib.h>
23 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
24 are filled with zeros. The shifted value is returned.
26 This function shifts the 64-bit value Operand to the left by Count bits. The
27 low Count bits are set to zero. The shifted value is returned.
29 @param Operand The 64-bit operand to shift left.
30 @param Count The number of bits to shift left.
32 @return Operand << Count
37 InternalMathLShiftU64 (
43 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
44 are filled with zeros. The shifted value is returned.
46 This function shifts the 64-bit value Operand to the right by Count bits. The
47 high Count bits are set to zero. The shifted value is returned.
49 @param Operand The 64-bit operand to shift right.
50 @param Count The number of bits to shift right.
52 @return Operand >> Count
57 InternalMathRShiftU64 (
63 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
64 are filled with original integer's bit 63. The shifted value is returned.
66 This function shifts the 64-bit value Operand to the right by Count bits. The
67 high Count bits are set to bit 63 of Operand. The shifted value is returned.
69 @param Operand The 64-bit operand to shift right.
70 @param Count The number of bits to shift right.
72 @return Operand arithmetically shifted right by Count
77 InternalMathARShiftU64 (
83 Rotates a 64-bit integer left between 0 and 63 bits, filling
84 the low bits with the high bits that were rotated.
86 This function rotates the 64-bit value Operand to the left by Count bits. The
87 low Count bits are filled with the high Count bits of Operand. The rotated
90 @param Operand The 64-bit operand to rotate left.
91 @param Count The number of bits to rotate left.
93 @return Operand <<< Count
104 Rotates a 64-bit integer right between 0 and 63 bits, filling
105 the high bits with the high low bits that were rotated.
107 This function rotates the 64-bit value Operand to the right by Count bits.
108 The high Count bits are filled with the low Count bits of Operand. The rotated
111 @param Operand The 64-bit operand to rotate right.
112 @param Count The number of bits to rotate right.
114 @return Operand >>> Count
119 InternalMathRRotU64 (
125 Switches the endianess of a 64-bit integer.
127 This function swaps the bytes in a 64-bit unsigned value to switch the value
128 from little endian to big endian or vice versa. The byte swapped value is
131 @param Operand A 64-bit unsigned value.
133 @return The byte swapped Operand.
138 InternalMathSwapBytes64 (
143 Multiplies a 64-bit unsigned integer by a 32-bit unsigned integer
144 and generates a 64-bit unsigned result.
146 This function multiplies the 64-bit unsigned value Multiplicand by the 32-bit
147 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
148 bit unsigned result is returned.
150 @param Multiplicand A 64-bit unsigned value.
151 @param Multiplier A 32-bit unsigned value.
153 @return Multiplicand * Multiplier
158 InternalMathMultU64x32 (
159 IN UINT64 Multiplicand
,
164 Multiplies a 64-bit unsigned integer by a 64-bit unsigned integer
165 and generates a 64-bit unsigned result.
167 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
168 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
169 bit unsigned result is returned.
171 @param Multiplicand A 64-bit unsigned value.
172 @param Multiplier A 64-bit unsigned value.
174 @return Multiplicand * Multiplier
179 InternalMathMultU64x64 (
180 IN UINT64 Multiplicand
,
185 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
186 generates a 64-bit unsigned result.
188 This function divides the 64-bit unsigned value Dividend by the 32-bit
189 unsigned value Divisor and generates a 64-bit unsigned quotient. This
190 function returns the 64-bit unsigned quotient.
192 @param Dividend A 64-bit unsigned value.
193 @param Divisor A 32-bit unsigned value.
195 @return Dividend / Divisor
200 InternalMathDivU64x32 (
206 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
207 generates a 32-bit unsigned remainder.
209 This function divides the 64-bit unsigned value Dividend by the 32-bit
210 unsigned value Divisor and generates a 32-bit remainder. This function
211 returns the 32-bit unsigned remainder.
213 @param Dividend A 64-bit unsigned value.
214 @param Divisor A 32-bit unsigned value.
216 @return Dividend % Divisor
221 InternalMathModU64x32 (
227 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
228 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
230 This function divides the 64-bit unsigned value Dividend by the 32-bit
231 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
232 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
233 This function returns the 64-bit unsigned quotient.
235 @param Dividend A 64-bit unsigned value.
236 @param Divisor A 32-bit unsigned value.
237 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
238 optional and may be NULL.
240 @return Dividend / Divisor
245 InternalMathDivRemU64x32 (
248 OUT UINT32
*Remainder OPTIONAL
252 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
253 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
255 This function divides the 64-bit unsigned value Dividend by the 64-bit
256 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
257 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
258 This function returns the 64-bit unsigned quotient.
260 @param Dividend A 64-bit unsigned value.
261 @param Divisor A 64-bit unsigned value.
262 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
263 optional and may be NULL.
265 @return Dividend / Divisor
270 InternalMathDivRemU64x64 (
273 OUT UINT64
*Remainder OPTIONAL
277 Divides a 64-bit signed integer by a 64-bit signed integer and
278 generates a 64-bit signed result and an optional 64-bit signed remainder.
280 This function divides the 64-bit signed value Dividend by the 64-bit
281 signed value Divisor and generates a 64-bit signed quotient. If Remainder
282 is not NULL, then the 64-bit signed remainder is returned in Remainder.
283 This function returns the 64-bit signed quotient.
285 @param Dividend A 64-bit signed value.
286 @param Divisor A 64-bit signed value.
287 @param Remainder A pointer to a 64-bit signed value. This parameter is
288 optional and may be NULL.
290 @return Dividend / Divisor
295 InternalMathDivRemS64x64 (
298 OUT INT64
*Remainder OPTIONAL
302 Transfers control to a function starting with a new stack.
304 Transfers control to the function specified by EntryPoint using the
305 new stack specified by NewStack and passing in the parameters specified
306 by Context1 and Context2. Context1 and Context2 are optional and may
307 be NULL. The function EntryPoint must never return.
308 Marker will be ignored on IA-32, x64, and EBC.
309 IPF CPUs expect one additional parameter of type VOID * that specifies
310 the new backing store pointer.
312 If EntryPoint is NULL, then ASSERT().
313 If NewStack is NULL, then ASSERT().
315 @param EntryPoint A pointer to function to call with the new stack.
316 @param Context1 A pointer to the context to pass into the EntryPoint
318 @param Context2 A pointer to the context to pass into the EntryPoint
320 @param NewStack A pointer to the new stack to use for the EntryPoint
322 @param Marker VA_LIST marker for the variable argument list.
327 InternalSwitchStack (
328 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
329 IN VOID
*Context1
, OPTIONAL
330 IN VOID
*Context2
, OPTIONAL
337 Worker function that returns a bit field from Operand.
339 Returns the bitfield specified by the StartBit and the EndBit from Operand.
341 @param Operand Operand on which to perform the bitfield operation.
342 @param StartBit The ordinal of the least significant bit in the bit field.
343 @param EndBit The ordinal of the most significant bit in the bit field.
345 @return The bit field read.
358 Worker function that reads a bit field from Operand, performs a bitwise OR,
359 and returns the result.
361 Performs a bitwise OR between the bit field specified by StartBit and EndBit
362 in Operand and the value specified by AndData. All other bits in Operand are
363 preserved. The new value is returned.
365 @param Operand Operand on which to perform the bitfield operation.
366 @param StartBit The ordinal of the least significant bit in the bit field.
367 @param EndBit The ordinal of the most significant bit in the bit field.
368 @param OrData The value to OR with the read value from the value
370 @return The new value.
384 Worker function that reads a bit field from Operand, performs a bitwise AND,
385 and returns the result.
387 Performs a bitwise AND between the bit field specified by StartBit and EndBit
388 in Operand and the value specified by AndData. All other bits in Operand are
389 preserved. The new value is returned.
391 @param Operand Operand on which to perform the bitfield operation.
392 @param StartBit The ordinal of the least significant bit in the bit field.
393 @param EndBit The ordinal of the most significant bit in the bit field.
394 @param AndData The value to And with the read value from the value
396 @return The new value.
410 Worker function that checks ASSERT condition for JumpBuffer
412 Checks ASSERT condition for JumpBuffer.
414 If JumpBuffer is NULL, then ASSERT().
415 For IPF CPUs, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT().
417 @param JumpBuffer A pointer to CPU context buffer.
422 InternalAssertJumpBuffer (
423 IN BASE_LIBRARY_JUMP_BUFFER
*JumpBuffer
428 Restores the CPU context that was saved with SetJump().
430 Restores the CPU context from the buffer specified by JumpBuffer.
431 This function never returns to the caller.
432 Instead is resumes execution based on the state of JumpBuffer.
434 @param JumpBuffer A pointer to CPU context buffer.
435 @param Value The value to return when the SetJump() context is restored.
441 IN BASE_LIBRARY_JUMP_BUFFER
*JumpBuffer
,
447 Check if a Unicode character is a decimal character.
449 This internal function checks if a Unicode character is a
450 decimal character. The valid decimal character is from
453 @param Char The character to check against.
455 @retval TRUE If the Char is a decmial character.
456 @retval FALSE If the Char is not a decmial character.
461 InternalIsDecimalDigitCharacter (
467 Convert a Unicode character to numerical value.
469 This internal function only deal with Unicode character
470 which maps to a valid hexadecimal ASII character, i.e.
471 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
472 Unicode character, the value returned does not make sense.
474 @param Char The character to convert.
476 @return The numerical value converted.
481 InternalHexCharToUintn (
487 Check if a Unicode character is a hexadecimal character.
489 This internal function checks if a Unicode character is a
490 decimal character. The valid hexadecimal character is
491 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
494 @param Char The character to check against.
496 @retval TRUE If the Char is a hexadecmial character.
497 @retval FALSE If the Char is not a hexadecmial character.
502 InternalIsHexaDecimalDigitCharacter (
508 Check if a ASCII character is a decimal character.
510 This internal function checks if a Unicode character is a
511 decimal character. The valid decimal character is from
514 @param Char The character to check against.
516 @retval TRUE If the Char is a decmial character.
517 @retval FALSE If the Char is not a decmial character.
522 InternalAsciiIsDecimalDigitCharacter (
528 Check if a ASCII character is a hexadecimal character.
530 This internal function checks if a ASCII character is a
531 decimal character. The valid hexadecimal character is
532 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
535 @param Char The character to check against.
537 @retval TRUE If the Char is a hexadecmial character.
538 @retval FALSE If the Char is not a hexadecmial character.
543 InternalAsciiIsHexaDecimalDigitCharacter (
549 Convert a ASCII character to numerical value.
551 This internal function only deal with Unicode character
552 which maps to a valid hexadecimal ASII character, i.e.
553 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
554 ASCII character, the value returned does not make sense.
556 @param Char The character to convert.
558 @return The numerical value converted.
563 InternalAsciiHexCharToUintn (
569 // Ia32 and x64 specific functions
571 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
574 Reads the current Global Descriptor Table Register(GDTR) descriptor.
576 Reads and returns the current GDTR descriptor and returns it in Gdtr. This
577 function is only available on IA-32 and x64.
579 @param Gdtr The pointer to a GDTR descriptor.
584 InternalX86ReadGdtr (
585 OUT IA32_DESCRIPTOR
*Gdtr
589 Writes the current Global Descriptor Table Register (GDTR) descriptor.
591 Writes and the current GDTR descriptor specified by Gdtr. This function is
592 only available on IA-32 and x64.
594 @param Gdtr The pointer to a GDTR descriptor.
599 InternalX86WriteGdtr (
600 IN CONST IA32_DESCRIPTOR
*Gdtr
604 Reads the current Interrupt Descriptor Table Register(GDTR) descriptor.
606 Reads and returns the current IDTR descriptor and returns it in Idtr. This
607 function is only available on IA-32 and x64.
609 @param Idtr The pointer to an IDTR descriptor.
614 InternalX86ReadIdtr (
615 OUT IA32_DESCRIPTOR
*Idtr
619 Writes the current Interrupt Descriptor Table Register(GDTR) descriptor.
621 Writes the current IDTR descriptor and returns it in Idtr. This function is
622 only available on IA-32 and x64.
624 @param Idtr The pointer to an IDTR descriptor.
629 InternalX86WriteIdtr (
630 IN CONST IA32_DESCRIPTOR
*Idtr
634 Save the current floating point/SSE/SSE2 context to a buffer.
636 Saves the current floating point/SSE/SSE2 state to the buffer specified by
637 Buffer. Buffer must be aligned on a 16-byte boundary. This function is only
638 available on IA-32 and x64.
640 @param Buffer The pointer to a buffer to save the floating point/SSE/SSE2 context.
646 OUT IA32_FX_BUFFER
*Buffer
650 Restores the current floating point/SSE/SSE2 context from a buffer.
652 Restores the current floating point/SSE/SSE2 state from the buffer specified
653 by Buffer. Buffer must be aligned on a 16-byte boundary. This function is
654 only available on IA-32 and x64.
656 @param Buffer The pointer to a buffer to save the floating point/SSE/SSE2 context.
661 InternalX86FxRestore (
662 IN CONST IA32_FX_BUFFER
*Buffer
666 Enables the 32-bit paging mode on the CPU.
668 Enables the 32-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
669 must be properly initialized prior to calling this service. This function
670 assumes the current execution mode is 32-bit protected mode. This function is
671 only available on IA-32. After the 32-bit paging mode is enabled, control is
672 transferred to the function specified by EntryPoint using the new stack
673 specified by NewStack and passing in the parameters specified by Context1 and
674 Context2. Context1 and Context2 are optional and may be NULL. The function
675 EntryPoint must never return.
677 There are a number of constraints that must be followed before calling this
679 1) Interrupts must be disabled.
680 2) The caller must be in 32-bit protected mode with flat descriptors. This
681 means all descriptors must have a base of 0 and a limit of 4GB.
682 3) CR0 and CR4 must be compatible with 32-bit protected mode with flat
684 4) CR3 must point to valid page tables that will be used once the transition
685 is complete, and those page tables must guarantee that the pages for this
686 function and the stack are identity mapped.
688 @param EntryPoint A pointer to function to call with the new stack after
690 @param Context1 A pointer to the context to pass into the EntryPoint
691 function as the first parameter after paging is enabled.
692 @param Context2 A pointer to the context to pass into the EntryPoint
693 function as the second parameter after paging is enabled.
694 @param NewStack A pointer to the new stack to use for the EntryPoint
695 function after paging is enabled.
700 InternalX86EnablePaging32 (
701 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
702 IN VOID
*Context1
, OPTIONAL
703 IN VOID
*Context2
, OPTIONAL
708 Disables the 32-bit paging mode on the CPU.
710 Disables the 32-bit paging mode on the CPU and returns to 32-bit protected
711 mode. This function assumes the current execution mode is 32-paged protected
712 mode. This function is only available on IA-32. After the 32-bit paging mode
713 is disabled, control is transferred to the function specified by EntryPoint
714 using the new stack specified by NewStack and passing in the parameters
715 specified by Context1 and Context2. Context1 and Context2 are optional and
716 may be NULL. The function EntryPoint must never return.
718 There are a number of constraints that must be followed before calling this
720 1) Interrupts must be disabled.
721 2) The caller must be in 32-bit paged mode.
722 3) CR0, CR3, and CR4 must be compatible with 32-bit paged mode.
723 4) CR3 must point to valid page tables that guarantee that the pages for
724 this function and the stack are identity mapped.
726 @param EntryPoint A pointer to function to call with the new stack after
728 @param Context1 A pointer to the context to pass into the EntryPoint
729 function as the first parameter after paging is disabled.
730 @param Context2 A pointer to the context to pass into the EntryPoint
731 function as the second parameter after paging is
733 @param NewStack A pointer to the new stack to use for the EntryPoint
734 function after paging is disabled.
739 InternalX86DisablePaging32 (
740 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
741 IN VOID
*Context1
, OPTIONAL
742 IN VOID
*Context2
, OPTIONAL
747 Enables the 64-bit paging mode on the CPU.
749 Enables the 64-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
750 must be properly initialized prior to calling this service. This function
751 assumes the current execution mode is 32-bit protected mode with flat
752 descriptors. This function is only available on IA-32. After the 64-bit
753 paging mode is enabled, control is transferred to the function specified by
754 EntryPoint using the new stack specified by NewStack and passing in the
755 parameters specified by Context1 and Context2. Context1 and Context2 are
756 optional and may be 0. The function EntryPoint must never return.
758 @param Cs The 16-bit selector to load in the CS before EntryPoint
759 is called. The descriptor in the GDT that this selector
760 references must be setup for long mode.
761 @param EntryPoint The 64-bit virtual address of the function to call with
762 the new stack after paging is enabled.
763 @param Context1 The 64-bit virtual address of the context to pass into
764 the EntryPoint function as the first parameter after
766 @param Context2 The 64-bit virtual address of the context to pass into
767 the EntryPoint function as the second parameter after
769 @param NewStack The 64-bit virtual address of the new stack to use for
770 the EntryPoint function after paging is enabled.
775 InternalX86EnablePaging64 (
777 IN UINT64 EntryPoint
,
778 IN UINT64 Context1
, OPTIONAL
779 IN UINT64 Context2
, OPTIONAL
784 Disables the 64-bit paging mode on the CPU.
786 Disables the 64-bit paging mode on the CPU and returns to 32-bit protected
787 mode. This function assumes the current execution mode is 64-paging mode.
788 This function is only available on x64. After the 64-bit paging mode is
789 disabled, control is transferred to the function specified by EntryPoint
790 using the new stack specified by NewStack and passing in the parameters
791 specified by Context1 and Context2. Context1 and Context2 are optional and
792 may be 0. The function EntryPoint must never return.
794 @param Cs The 16-bit selector to load in the CS before EntryPoint
795 is called. The descriptor in the GDT that this selector
796 references must be setup for 32-bit protected mode.
797 @param EntryPoint The 64-bit virtual address of the function to call with
798 the new stack after paging is disabled.
799 @param Context1 The 64-bit virtual address of the context to pass into
800 the EntryPoint function as the first parameter after
802 @param Context2 The 64-bit virtual address of the context to pass into
803 the EntryPoint function as the second parameter after
805 @param NewStack The 64-bit virtual address of the new stack to use for
806 the EntryPoint function after paging is disabled.
811 InternalX86DisablePaging64 (
813 IN UINT32 EntryPoint
,
814 IN UINT32 Context1
, OPTIONAL
815 IN UINT32 Context2
, OPTIONAL
820 Generates a 16-bit random number through RDRAND instruction.
822 @param[out] Rand Buffer pointer to store the random result.
824 @retval TRUE RDRAND call was successful.
825 @retval FALSE Failed attempts to call RDRAND.
830 InternalX86RdRand16 (
835 Generates a 32-bit random number through RDRAND instruction.
837 @param[out] Rand Buffer pointer to store the random result.
839 @retval TRUE RDRAND call was successful.
840 @retval FALSE Failed attempts to call RDRAND.
845 InternalX86RdRand32 (
850 Generates a 64-bit random number through RDRAND instruction.
853 @param[out] Rand Buffer pointer to store the random result.
855 @retval TRUE RDRAND call was successful.
856 @retval FALSE Failed attempts to call RDRAND.
861 InternalX86RdRand64 (