2 Declaration of internal functions in BaseLib.
4 Copyright (c) 2006 - 2007, 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
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.
15 #ifndef __BASE_LIB_INTERNALS__
16 #define __BASE_LIB_INTERNALS__
19 #include <Library/BaseLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/TimerLib.h>
23 #include <Library/PcdLib.h>
26 #define QUIENT_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 / 10)
27 #define REMINDER_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 % 10)
29 #define QUIENT_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 / 16)
30 #define REMINDER_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 % 16)
32 #define QUIENT_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 / 10)
33 #define REMINDER_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 % 10)
35 #define QUIENT_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 / 16)
36 #define REMINDER_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 % 16)
43 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
44 are filled with zeros. The shifted value is returned.
46 This function shifts the 64-bit value Operand to the left by Count bits. The
47 low Count bits are set to zero. The shifted value is returned.
49 @param Operand The 64-bit operand to shift left.
50 @param Count The number of bits to shift left.
52 @return Operand << Count
57 InternalMathLShiftU64 (
63 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
64 are filled with zeros. 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 zero. 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 >> Count
77 InternalMathRShiftU64 (
83 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
84 are filled with original integer's bit 63. The shifted value is returned.
86 This function shifts the 64-bit value Operand to the right by Count bits. The
87 high Count bits are set to bit 63 of Operand. The shifted value is returned.
89 @param Operand The 64-bit operand to shift right.
90 @param Count The number of bits to shift right.
92 @return Operand arithmetically shifted right by Count
97 InternalMathARShiftU64 (
103 Rotates a 64-bit integer left between 0 and 63 bits, filling
104 the low bits with the high bits that were rotated.
106 This function rotates the 64-bit value Operand to the left by Count bits. The
107 low Count bits are fill with the high Count bits of Operand. The rotated
110 @param Operand The 64-bit operand to rotate left.
111 @param Count The number of bits to rotate left.
113 @return Operand <<< Count
118 InternalMathLRotU64 (
124 Rotates a 64-bit integer right between 0 and 63 bits, filling
125 the high bits with the high low bits that were rotated.
127 This function rotates the 64-bit value Operand to the right by Count bits.
128 The high Count bits are fill with the low Count bits of Operand. The rotated
131 @param Operand The 64-bit operand to rotate right.
132 @param Count The number of bits to rotate right.
134 @return Operand >>> Count
139 InternalMathRRotU64 (
145 Switches the endianess of a 64-bit integer.
147 This function swaps the bytes in a 64-bit unsigned value to switch the value
148 from little endian to big endian or vice versa. The byte swapped value is
151 @param Operand A 64-bit unsigned value.
153 @return The byte swaped Operand.
158 InternalMathSwapBytes64 (
163 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
164 and generates a 64-bit unsigned result.
166 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
167 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
168 bit unsigned result is returned.
170 @param Multiplicand A 64-bit unsigned value.
171 @param Multiplier A 32-bit unsigned value.
173 @return Multiplicand * Multiplier
178 InternalMathMultU64x32 (
179 IN UINT64 Multiplicand
,
184 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
185 and generates a 64-bit unsigned result.
187 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
188 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
189 bit unsigned result is returned.
191 @param Multiplicand A 64-bit unsigned value.
192 @param Multiplier A 64-bit unsigned value.
194 @return Multiplicand * Multiplier
199 InternalMathMultU64x64 (
200 IN UINT64 Multiplicand
,
205 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
206 generates a 64-bit unsigned result.
208 This function divides the 64-bit unsigned value Dividend by the 32-bit
209 unsigned value Divisor and generates a 64-bit unsigned quotient. This
210 function returns the 64-bit unsigned quotient.
212 @param Dividend A 64-bit unsigned value.
213 @param Divisor A 32-bit unsigned value.
215 @return Dividend / Divisor
220 InternalMathDivU64x32 (
226 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
227 generates a 32-bit unsigned remainder.
229 This function divides the 64-bit unsigned value Dividend by the 32-bit
230 unsigned value Divisor and generates a 32-bit remainder. This function
231 returns the 32-bit unsigned remainder.
233 @param Dividend A 64-bit unsigned value.
234 @param Divisor A 32-bit unsigned value.
236 @return Dividend % Divisor
241 InternalMathModU64x32 (
247 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
248 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
250 This function divides the 64-bit unsigned value Dividend by the 32-bit
251 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
252 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
253 This function returns the 64-bit unsigned quotient.
255 @param Dividend A 64-bit unsigned value.
256 @param Divisor A 32-bit unsigned value.
257 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
258 optional and may be NULL.
260 @return Dividend / Divisor
265 InternalMathDivRemU64x32 (
268 OUT UINT32
*Remainder OPTIONAL
272 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
273 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
275 This function divides the 64-bit unsigned value Dividend by the 64-bit
276 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
277 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
278 This function returns the 64-bit unsigned quotient.
280 @param Dividend A 64-bit unsigned value.
281 @param Divisor A 64-bit unsigned value.
282 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
283 optional and may be NULL.
285 @return Dividend / Divisor
290 InternalMathDivRemU64x64 (
293 OUT UINT64
*Remainder OPTIONAL
297 Divides a 64-bit signed integer by a 64-bit signed integer and
298 generates a 64-bit signed result and a optional 64-bit signed remainder.
300 This function divides the 64-bit unsigned value Dividend by the 64-bit
301 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
302 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
303 This function returns the 64-bit unsigned quotient.
305 @param Dividend A 64-bit signed value.
306 @param Divisor A 64-bit signed value.
307 @param Remainder A pointer to a 64-bit signed value. This parameter is
308 optional and may be NULL.
310 @return Dividend / Divisor
315 InternalMathDivRemS64x64 (
318 OUT INT64
*Remainder OPTIONAL
322 Transfers control to a function starting with a new stack.
324 Transfers control to the function specified by EntryPoint using the
325 new stack specified by NewStack and passing in the parameters specified
326 by Context1 and Context2. Context1 and Context2 are optional and may
327 be NULL. The function EntryPoint must never return.
328 Marker will be ignored on IA-32, x64, and EBC.
329 IPF CPUs expect one additional parameter of type VOID * that specifies
330 the new backing store pointer.
332 If EntryPoint is NULL, then ASSERT().
333 If NewStack is NULL, then ASSERT().
335 @param EntryPoint A pointer to function to call with the new stack.
336 @param Context1 A pointer to the context to pass into the EntryPoint
338 @param Context2 A pointer to the context to pass into the EntryPoint
340 @param NewStack A pointer to the new stack to use for the EntryPoint
342 @param Marker VA_LIST marker for the variable argument list.
347 InternalSwitchStack (
348 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
349 IN VOID
*Context1
, OPTIONAL
350 IN VOID
*Context2
, OPTIONAL
357 Worker function that locates the Node in the List
359 By searching the List, finds the location of the Node in List. At the same time,
360 verifies the validity of this list.
362 If List is NULL, then ASSERT().
363 If List->ForwardLink is NULL, then ASSERT().
364 If List->backLink is NULL, then ASSERT().
365 If Node is NULL, then ASSERT();
366 If PcdMaximumLinkedListLenth is not zero, and prior to insertion the number
367 of nodes in ListHead, including the ListHead node, is greater than or
368 equal to PcdMaximumLinkedListLength, then ASSERT().
370 @param List A pointer to a node in a linked list.
371 @param Node A pointer to one nod.
373 @retval TRUE Node is in List
374 @retval FALSE Node isn't in List, or List is invalid
380 IN CONST LIST_ENTRY
*List
,
381 IN CONST LIST_ENTRY
*Node
386 Performs an atomic increment of an 32-bit unsigned integer.
388 Performs an atomic increment of the 32-bit unsigned integer specified by
389 Value and returns the incremented value. The increment operation must be
390 performed using MP safe mechanisms. The state of the return value is not
391 guaranteed to be MP safe.
393 @param Value A pointer to the 32-bit value to increment.
395 @return The incremented value.
400 InternalSyncIncrement (
401 IN
volatile UINT32
*Value
406 Performs an atomic decrement of an 32-bit unsigned integer.
408 Performs an atomic decrement of the 32-bit unsigned integer specified by
409 Value and returns the decrement value. The decrement operation must be
410 performed using MP safe mechanisms. The state of the return value is not
411 guaranteed to be MP safe.
413 @param Value A pointer to the 32-bit value to decrement.
415 @return The decrement value.
420 InternalSyncDecrement (
421 IN
volatile UINT32
*Value
426 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
428 Performs an atomic compare exchange operation on the 32-bit unsigned integer
429 specified by Value. If Value is equal to CompareValue, then Value is set to
430 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
431 then Value is returned. The compare exchange operation must be performed using
434 @param Value A pointer to the 32-bit value for the compare exchange
436 @param CompareValue 32-bit value used in compare operation.
437 @param ExchangeValue 32-bit value used in exchange operation.
439 @return The original *Value before exchange.
444 InternalSyncCompareExchange32 (
445 IN
volatile UINT32
*Value
,
446 IN UINT32 CompareValue
,
447 IN UINT32 ExchangeValue
452 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
454 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
455 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
456 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
457 The compare exchange operation must be performed using MP safe mechanisms.
459 @param Value A pointer to the 64-bit value for the compare exchange
461 @param CompareValue 64-bit value used in compare operation.
462 @param ExchangeValue 64-bit value used in exchange operation.
464 @return The original *Value before exchange.
469 InternalSyncCompareExchange64 (
470 IN
volatile UINT64
*Value
,
471 IN UINT64 CompareValue
,
472 IN UINT64 ExchangeValue
477 Worker function that returns a bit field from Operand
479 Returns the bitfield specified by the StartBit and the EndBit from Operand.
481 @param Operand Operand on which to perform the bitfield operation.
482 @param StartBit The ordinal of the least significant bit in the bit field.
483 @param EndBit The ordinal of the most significant bit in the bit field.
485 @return The bit field read.
491 IN
unsigned int Operand
,
498 Worker function that reads a bit field from Operand, performs a bitwise OR,
499 and returns the result.
501 Performs a bitwise OR between the bit field specified by StartBit and EndBit
502 in Operand and the value specified by AndData. All other bits in Operand are
503 preserved. The new value is returned.
505 @param Operand Operand on which to perform the bitfield operation.
506 @param StartBit The ordinal of the least significant bit in the bit field.
507 @param EndBit The ordinal of the most significant bit in the bit field.
508 @param OrData The value to OR with the read value from the value
510 @return The new value.
516 IN
unsigned int Operand
,
519 IN
unsigned int OrData
524 Worker function that reads a bit field from Operand, performs a bitwise AND,
525 and returns the result.
527 Performs a bitwise AND between the bit field specified by StartBit and EndBit
528 in Operand and the value specified by AndData. All other bits in Operand are
529 preserved. The new value is returned.
531 @param Operand Operand on which to perform the bitfield operation.
532 @param StartBit The ordinal of the least significant bit in the bit field.
533 @param EndBit The ordinal of the most significant bit in the bit field.
534 @param AndData The value to And with the read value from the value
536 @return The new value.
542 IN
unsigned int Operand
,
545 IN
unsigned int AndData
550 Worker function that checks ASSERT condition for JumpBuffer
552 Checks ASSERT condition for JumpBuffer.
554 If JumpBuffer is NULL, then ASSERT().
555 For IPF CPUs, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT().
557 @param JumpBuffer A pointer to CPU context buffer.
562 InternalAssertJumpBuffer (
563 IN BASE_LIBRARY_JUMP_BUFFER
*JumpBuffer
568 Restores the CPU context that was saved with SetJump().
570 Restores the CPU context from the buffer specified by JumpBuffer.
571 This function never returns to the caller.
572 Instead is resumes execution based on the state of JumpBuffer.
574 @param JumpBuffer A pointer to CPU context buffer.
575 @param Value The value to return when the SetJump() context is restored.
581 IN BASE_LIBRARY_JUMP_BUFFER
*JumpBuffer
,
587 // Ia32 and x64 specific functions
589 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
592 Reads the current Global Descriptor Table Register(GDTR) descriptor.
594 Reads and returns the current GDTR descriptor and returns it in Gdtr. This
595 function is only available on IA-32 and X64.
597 @param Gdtr Pointer to a GDTR descriptor.
602 InternalX86ReadGdtr (
603 OUT IA32_DESCRIPTOR
*Gdtr
607 Writes the current Global Descriptor Table Register (GDTR) descriptor.
609 Writes and the current GDTR descriptor specified by Gdtr. This function is
610 only available on IA-32 and X64.
612 @param Gdtr Pointer to a GDTR descriptor.
617 InternalX86WriteGdtr (
618 IN CONST IA32_DESCRIPTOR
*Gdtr
622 Reads the current Interrupt Descriptor Table Register(GDTR) descriptor.
624 Reads and returns the current IDTR descriptor and returns it in Idtr. This
625 function is only available on IA-32 and X64.
627 @param Idtr Pointer to a IDTR descriptor.
632 InternalX86ReadIdtr (
633 OUT IA32_DESCRIPTOR
*Idtr
637 Writes the current Interrupt Descriptor Table Register(GDTR) descriptor.
639 Writes the current IDTR descriptor and returns it in Idtr. This function is
640 only available on IA-32 and X64.
642 @param Idtr Pointer to a IDTR descriptor.
647 InternalX86WriteIdtr (
648 IN CONST IA32_DESCRIPTOR
*Idtr
652 Save the current floating point/SSE/SSE2 context to a buffer.
654 Saves the current floating point/SSE/SSE2 state to the buffer specified by
655 Buffer. Buffer must be aligned on a 16-byte boundary. This function is only
656 available on IA-32 and X64.
658 @param Buffer Pointer to a buffer to save the floating point/SSE/SSE2 context.
664 OUT IA32_FX_BUFFER
*Buffer
668 Restores the current floating point/SSE/SSE2 context from a buffer.
670 Restores the current floating point/SSE/SSE2 state from the buffer specified
671 by Buffer. Buffer must be aligned on a 16-byte boundary. This function is
672 only available on IA-32 and X64.
674 @param Buffer Pointer to a buffer to save the floating point/SSE/SSE2 context.
679 InternalX86FxRestore (
680 IN CONST IA32_FX_BUFFER
*Buffer
684 Enables the 32-bit paging mode on the CPU.
686 Enables the 32-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
687 must be properly initialized prior to calling this service. This function
688 assumes the current execution mode is 32-bit protected mode. This function is
689 only available on IA-32. After the 32-bit paging mode is enabled, control is
690 transferred to the function specified by EntryPoint using the new stack
691 specified by NewStack and passing in the parameters specified by Context1 and
692 Context2. Context1 and Context2 are optional and may be NULL. The function
693 EntryPoint must never return.
695 There are a number of constraints that must be followed before calling this
697 1) Interrupts must be disabled.
698 2) The caller must be in 32-bit protected mode with flat descriptors. This
699 means all descriptors must have a base of 0 and a limit of 4GB.
700 3) CR0 and CR4 must be compatible with 32-bit protected mode with flat
702 4) CR3 must point to valid page tables that will be used once the transition
703 is complete, and those page tables must guarantee that the pages for this
704 function and the stack are identity mapped.
706 @param EntryPoint A pointer to function to call with the new stack after
708 @param Context1 A pointer to the context to pass into the EntryPoint
709 function as the first parameter after paging is enabled.
710 @param Context2 A pointer to the context to pass into the EntryPoint
711 function as the second parameter after paging is enabled.
712 @param NewStack A pointer to the new stack to use for the EntryPoint
713 function after paging is enabled.
718 InternalX86EnablePaging32 (
719 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
720 IN VOID
*Context1
, OPTIONAL
721 IN VOID
*Context2
, OPTIONAL
726 Disables the 32-bit paging mode on the CPU.
728 Disables the 32-bit paging mode on the CPU and returns to 32-bit protected
729 mode. This function assumes the current execution mode is 32-paged protected
730 mode. This function is only available on IA-32. After the 32-bit paging mode
731 is disabled, control is transferred to the function specified by EntryPoint
732 using the new stack specified by NewStack and passing in the parameters
733 specified by Context1 and Context2. Context1 and Context2 are optional and
734 may be NULL. The function EntryPoint must never return.
736 There are a number of constraints that must be followed before calling this
738 1) Interrupts must be disabled.
739 2) The caller must be in 32-bit paged mode.
740 3) CR0, CR3, and CR4 must be compatible with 32-bit paged mode.
741 4) CR3 must point to valid page tables that guarantee that the pages for
742 this function and the stack are identity mapped.
744 @param EntryPoint A pointer to function to call with the new stack after
746 @param Context1 A pointer to the context to pass into the EntryPoint
747 function as the first parameter after paging is disabled.
748 @param Context2 A pointer to the context to pass into the EntryPoint
749 function as the second parameter after paging is
751 @param NewStack A pointer to the new stack to use for the EntryPoint
752 function after paging is disabled.
757 InternalX86DisablePaging32 (
758 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
759 IN VOID
*Context1
, OPTIONAL
760 IN VOID
*Context2
, OPTIONAL
765 Enables the 64-bit paging mode on the CPU.
767 Enables the 64-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
768 must be properly initialized prior to calling this service. This function
769 assumes the current execution mode is 32-bit protected mode with flat
770 descriptors. This function is only available on IA-32. After the 64-bit
771 paging mode is enabled, control is transferred to the function specified by
772 EntryPoint using the new stack specified by NewStack and passing in the
773 parameters specified by Context1 and Context2. Context1 and Context2 are
774 optional and may be 0. The function EntryPoint must never return.
776 @param Cs The 16-bit selector to load in the CS before EntryPoint
777 is called. The descriptor in the GDT that this selector
778 references must be setup for long mode.
779 @param EntryPoint The 64-bit virtual address of the function to call with
780 the new stack after paging is enabled.
781 @param Context1 The 64-bit virtual address of the context to pass into
782 the EntryPoint function as the first parameter after
784 @param Context2 The 64-bit virtual address of the context to pass into
785 the EntryPoint function as the second parameter after
787 @param NewStack The 64-bit virtual address of the new stack to use for
788 the EntryPoint function after paging is enabled.
793 InternalX86EnablePaging64 (
795 IN UINT64 EntryPoint
,
796 IN UINT64 Context1
, OPTIONAL
797 IN UINT64 Context2
, OPTIONAL
802 Disables the 64-bit paging mode on the CPU.
804 Disables the 64-bit paging mode on the CPU and returns to 32-bit protected
805 mode. This function assumes the current execution mode is 64-paging mode.
806 This function is only available on X64. After the 64-bit paging mode is
807 disabled, control is transferred to the function specified by EntryPoint
808 using the new stack specified by NewStack and passing in the parameters
809 specified by Context1 and Context2. Context1 and Context2 are optional and
810 may be 0. The function EntryPoint must never return.
812 @param Cs The 16-bit selector to load in the CS before EntryPoint
813 is called. The descriptor in the GDT that this selector
814 references must be setup for 32-bit protected mode.
815 @param EntryPoint The 64-bit virtual address of the function to call with
816 the new stack after paging is disabled.
817 @param Context1 The 64-bit virtual address of the context to pass into
818 the EntryPoint function as the first parameter after
820 @param Context2 The 64-bit virtual address of the context to pass into
821 the EntryPoint function as the second parameter after
823 @param NewStack The 64-bit virtual address of the new stack to use for
824 the EntryPoint function after paging is disabled.
829 InternalX86DisablePaging64 (
831 IN UINT32 EntryPoint
,
832 IN UINT32 Context1
, OPTIONAL
833 IN UINT32 Context2
, OPTIONAL
838 #elif defined (MDE_CPU_IPF)
841 // IPF specific functions
845 Transfers control to a function starting with a new stack.
847 Transfers control to the function specified by EntryPoint using the new stack
848 specified by NewStack and passing in the parameters specified by Context1 and
849 Context2. Context1 and Context2 are optional and may be NULL. The function
850 EntryPoint must never return.
852 If EntryPoint is NULL, then ASSERT().
853 If NewStack is NULL, then ASSERT().
855 @param EntryPoint A pointer to function to call with the new stack.
856 @param Context1 A pointer to the context to pass into the EntryPoint
858 @param Context2 A pointer to the context to pass into the EntryPoint
860 @param NewStack A pointer to the new stack to use for the EntryPoint
862 @param NewBsp A pointer to the new memory location for RSE backing
868 AsmSwitchStackAndBackingStore (
869 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
870 IN VOID
*Context1
, OPTIONAL
871 IN VOID
*Context2
, OPTIONAL