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
,
336 Worker function that returns a bit field from Operand.
338 Returns the bitfield specified by the StartBit and the EndBit from Operand.
340 @param Operand Operand on which to perform the bitfield operation.
341 @param StartBit The ordinal of the least significant bit in the bit field.
342 @param EndBit The ordinal of the most significant bit in the bit field.
344 @return The bit field read.
356 Worker function that reads a bit field from Operand, performs a bitwise OR,
357 and returns the result.
359 Performs a bitwise OR between the bit field specified by StartBit and EndBit
360 in Operand and the value specified by AndData. All other bits in Operand are
361 preserved. The new value is returned.
363 @param Operand Operand on which to perform the bitfield operation.
364 @param StartBit The ordinal of the least significant bit in the bit field.
365 @param EndBit The ordinal of the most significant bit in the bit field.
366 @param OrData The value to OR with the read value from the value
368 @return The new value.
381 Worker function that reads a bit field from Operand, performs a bitwise AND,
382 and returns the result.
384 Performs a bitwise AND between the bit field specified by StartBit and EndBit
385 in Operand and the value specified by AndData. All other bits in Operand are
386 preserved. The new value is returned.
388 @param Operand Operand on which to perform the bitfield operation.
389 @param StartBit The ordinal of the least significant bit in the bit field.
390 @param EndBit The ordinal of the most significant bit in the bit field.
391 @param AndData The value to And with the read value from the value
393 @return The new value.
406 Worker function that checks ASSERT condition for JumpBuffer
408 Checks ASSERT condition for JumpBuffer.
410 If JumpBuffer is NULL, then ASSERT().
411 For IPF CPUs, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT().
413 @param JumpBuffer A pointer to CPU context buffer.
418 InternalAssertJumpBuffer (
419 IN BASE_LIBRARY_JUMP_BUFFER
*JumpBuffer
423 Restores the CPU context that was saved with SetJump().
425 Restores the CPU context from the buffer specified by JumpBuffer.
426 This function never returns to the caller.
427 Instead is resumes execution based on the state of JumpBuffer.
429 @param JumpBuffer A pointer to CPU context buffer.
430 @param Value The value to return when the SetJump() context is restored.
436 IN BASE_LIBRARY_JUMP_BUFFER
*JumpBuffer
,
441 Check if a Unicode character is a decimal character.
443 This internal function checks if a Unicode character is a
444 decimal character. The valid decimal character is from
447 @param Char The character to check against.
449 @retval TRUE If the Char is a decmial character.
450 @retval FALSE If the Char is not a decmial character.
455 InternalIsDecimalDigitCharacter (
460 Convert a Unicode character to numerical value.
462 This internal function only deal with Unicode character
463 which maps to a valid hexadecimal ASII character, i.e.
464 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
465 Unicode character, the value returned does not make sense.
467 @param Char The character to convert.
469 @return The numerical value converted.
474 InternalHexCharToUintn (
479 Check if a Unicode character is a hexadecimal character.
481 This internal function checks if a Unicode character is a
482 decimal character. The valid hexadecimal character is
483 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
486 @param Char The character to check against.
488 @retval TRUE If the Char is a hexadecmial character.
489 @retval FALSE If the Char is not a hexadecmial character.
494 InternalIsHexaDecimalDigitCharacter (
499 Check if a ASCII character is a decimal character.
501 This internal function checks if a Unicode character is a
502 decimal character. The valid decimal character is from
505 @param Char The character to check against.
507 @retval TRUE If the Char is a decmial character.
508 @retval FALSE If the Char is not a decmial character.
513 InternalAsciiIsDecimalDigitCharacter (
518 Check if a ASCII character is a hexadecimal character.
520 This internal function checks if a ASCII character is a
521 decimal character. The valid hexadecimal character is
522 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
525 @param Char The character to check against.
527 @retval TRUE If the Char is a hexadecmial character.
528 @retval FALSE If the Char is not a hexadecmial character.
533 InternalAsciiIsHexaDecimalDigitCharacter (
538 Convert a ASCII character to numerical value.
540 This internal function only deal with Unicode character
541 which maps to a valid hexadecimal ASII character, i.e.
542 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
543 ASCII character, the value returned does not make sense.
545 @param Char The character to convert.
547 @return The numerical value converted.
552 InternalAsciiHexCharToUintn (
557 // Ia32 and x64 specific functions
559 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
562 Reads the current Global Descriptor Table Register(GDTR) descriptor.
564 Reads and returns the current GDTR descriptor and returns it in Gdtr. This
565 function is only available on IA-32 and x64.
567 @param Gdtr The pointer to a GDTR descriptor.
572 InternalX86ReadGdtr (
573 OUT IA32_DESCRIPTOR
*Gdtr
577 Writes the current Global Descriptor Table Register (GDTR) descriptor.
579 Writes and the current GDTR descriptor specified by Gdtr. This function is
580 only available on IA-32 and x64.
582 @param Gdtr The pointer to a GDTR descriptor.
587 InternalX86WriteGdtr (
588 IN CONST IA32_DESCRIPTOR
*Gdtr
592 Reads the current Interrupt Descriptor Table Register(GDTR) descriptor.
594 Reads and returns the current IDTR descriptor and returns it in Idtr. This
595 function is only available on IA-32 and x64.
597 @param Idtr The pointer to an IDTR descriptor.
602 InternalX86ReadIdtr (
603 OUT IA32_DESCRIPTOR
*Idtr
607 Writes the current Interrupt Descriptor Table Register(GDTR) descriptor.
609 Writes the current IDTR descriptor and returns it in Idtr. This function is
610 only available on IA-32 and x64.
612 @param Idtr The pointer to an IDTR descriptor.
617 InternalX86WriteIdtr (
618 IN CONST IA32_DESCRIPTOR
*Idtr
622 Save the current floating point/SSE/SSE2 context to a buffer.
624 Saves the current floating point/SSE/SSE2 state to the buffer specified by
625 Buffer. Buffer must be aligned on a 16-byte boundary. This function is only
626 available on IA-32 and x64.
628 @param Buffer The pointer to a buffer to save the floating point/SSE/SSE2 context.
634 OUT IA32_FX_BUFFER
*Buffer
638 Restores the current floating point/SSE/SSE2 context from a buffer.
640 Restores the current floating point/SSE/SSE2 state from the buffer specified
641 by Buffer. Buffer must be aligned on a 16-byte boundary. This function is
642 only available on IA-32 and x64.
644 @param Buffer The pointer to a buffer to save the floating point/SSE/SSE2 context.
649 InternalX86FxRestore (
650 IN CONST IA32_FX_BUFFER
*Buffer
654 Enables the 32-bit paging mode on the CPU.
656 Enables the 32-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
657 must be properly initialized prior to calling this service. This function
658 assumes the current execution mode is 32-bit protected mode. This function is
659 only available on IA-32. After the 32-bit paging mode is enabled, control is
660 transferred to the function specified by EntryPoint using the new stack
661 specified by NewStack and passing in the parameters specified by Context1 and
662 Context2. Context1 and Context2 are optional and may be NULL. The function
663 EntryPoint must never return.
665 There are a number of constraints that must be followed before calling this
667 1) Interrupts must be disabled.
668 2) The caller must be in 32-bit protected mode with flat descriptors. This
669 means all descriptors must have a base of 0 and a limit of 4GB.
670 3) CR0 and CR4 must be compatible with 32-bit protected mode with flat
672 4) CR3 must point to valid page tables that will be used once the transition
673 is complete, and those page tables must guarantee that the pages for this
674 function and the stack are identity mapped.
676 @param EntryPoint A pointer to function to call with the new stack after
678 @param Context1 A pointer to the context to pass into the EntryPoint
679 function as the first parameter after paging is enabled.
680 @param Context2 A pointer to the context to pass into the EntryPoint
681 function as the second parameter after paging is enabled.
682 @param NewStack A pointer to the new stack to use for the EntryPoint
683 function after paging is enabled.
688 InternalX86EnablePaging32 (
689 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
690 IN VOID
*Context1 OPTIONAL
,
691 IN VOID
*Context2 OPTIONAL
,
696 Disables the 32-bit paging mode on the CPU.
698 Disables the 32-bit paging mode on the CPU and returns to 32-bit protected
699 mode. This function assumes the current execution mode is 32-paged protected
700 mode. This function is only available on IA-32. After the 32-bit paging mode
701 is disabled, control is transferred to the function specified by EntryPoint
702 using the new stack specified by NewStack and passing in the parameters
703 specified by Context1 and Context2. Context1 and Context2 are optional and
704 may be NULL. The function EntryPoint must never return.
706 There are a number of constraints that must be followed before calling this
708 1) Interrupts must be disabled.
709 2) The caller must be in 32-bit paged mode.
710 3) CR0, CR3, and CR4 must be compatible with 32-bit paged mode.
711 4) CR3 must point to valid page tables that guarantee that the pages for
712 this function and the stack are identity mapped.
714 @param EntryPoint A pointer to function to call with the new stack after
716 @param Context1 A pointer to the context to pass into the EntryPoint
717 function as the first parameter after paging is disabled.
718 @param Context2 A pointer to the context to pass into the EntryPoint
719 function as the second parameter after paging is
721 @param NewStack A pointer to the new stack to use for the EntryPoint
722 function after paging is disabled.
727 InternalX86DisablePaging32 (
728 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
729 IN VOID
*Context1 OPTIONAL
,
730 IN VOID
*Context2 OPTIONAL
,
735 Enables the 64-bit paging mode on the CPU.
737 Enables the 64-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
738 must be properly initialized prior to calling this service. This function
739 assumes the current execution mode is 32-bit protected mode with flat
740 descriptors. This function is only available on IA-32. After the 64-bit
741 paging mode is enabled, control is transferred to the function specified by
742 EntryPoint using the new stack specified by NewStack and passing in the
743 parameters specified by Context1 and Context2. Context1 and Context2 are
744 optional and may be 0. The function EntryPoint must never return.
746 @param Cs The 16-bit selector to load in the CS before EntryPoint
747 is called. The descriptor in the GDT that this selector
748 references must be setup for long mode.
749 @param EntryPoint The 64-bit virtual address of the function to call with
750 the new stack after paging is enabled.
751 @param Context1 The 64-bit virtual address of the context to pass into
752 the EntryPoint function as the first parameter after
754 @param Context2 The 64-bit virtual address of the context to pass into
755 the EntryPoint function as the second parameter after
757 @param NewStack The 64-bit virtual address of the new stack to use for
758 the EntryPoint function after paging is enabled.
763 InternalX86EnablePaging64 (
765 IN UINT64 EntryPoint
,
766 IN UINT64 Context1 OPTIONAL
,
767 IN UINT64 Context2 OPTIONAL
,
772 Disables the 64-bit paging mode on the CPU.
774 Disables the 64-bit paging mode on the CPU and returns to 32-bit protected
775 mode. This function assumes the current execution mode is 64-paging mode.
776 This function is only available on x64. After the 64-bit paging mode is
777 disabled, control is transferred to the function specified by EntryPoint
778 using the new stack specified by NewStack and passing in the parameters
779 specified by Context1 and Context2. Context1 and Context2 are optional and
780 may be 0. The function EntryPoint must never return.
782 @param Cs The 16-bit selector to load in the CS before EntryPoint
783 is called. The descriptor in the GDT that this selector
784 references must be setup for 32-bit protected mode.
785 @param EntryPoint The 64-bit virtual address of the function to call with
786 the new stack after paging is disabled.
787 @param Context1 The 64-bit virtual address of the context to pass into
788 the EntryPoint function as the first parameter after
790 @param Context2 The 64-bit virtual address of the context to pass into
791 the EntryPoint function as the second parameter after
793 @param NewStack The 64-bit virtual address of the new stack to use for
794 the EntryPoint function after paging is disabled.
799 InternalX86DisablePaging64 (
801 IN UINT32 EntryPoint
,
802 IN UINT32 Context1 OPTIONAL
,
803 IN UINT32 Context2 OPTIONAL
,
808 Generates a 16-bit random number through RDRAND instruction.
810 @param[out] Rand Buffer pointer to store the random result.
812 @retval TRUE RDRAND call was successful.
813 @retval FALSE Failed attempts to call RDRAND.
818 InternalX86RdRand16 (
823 Generates a 32-bit random number through RDRAND instruction.
825 @param[out] Rand Buffer pointer to store the random result.
827 @retval TRUE RDRAND call was successful.
828 @retval FALSE Failed attempts to call RDRAND.
833 InternalX86RdRand32 (
838 Generates a 64-bit random number through RDRAND instruction.
841 @param[out] Rand Buffer pointer to store the random result.
843 @retval TRUE RDRAND call was successful.
844 @retval FALSE Failed attempts to call RDRAND.
849 InternalX86RdRand64 (