SwitchStack.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*++
  2. Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. --*/
  6. #include "Host.h"
  7. /**
  8. Transfers control to a function starting with a new stack.
  9. Transfers control to the function specified by EntryPoint using the new stack
  10. specified by NewStack and passing in the parameters specified by Context1 and
  11. Context2. Context1 and Context2 are optional and may be NULL. The function
  12. EntryPoint must never return.
  13. If EntryPoint is NULL, then ASSERT().
  14. If NewStack is NULL, then ASSERT().
  15. @param EntryPoint A pointer to function to call with the new stack.
  16. @param Context1 A pointer to the context to pass into the EntryPoint
  17. function.
  18. @param Context2 A pointer to the context to pass into the EntryPoint
  19. function.
  20. @param NewStack A pointer to the new stack to use for the EntryPoint
  21. function.
  22. **/
  23. VOID
  24. EFIAPI
  25. PeiSwitchStacks (
  26. IN SWITCH_STACK_ENTRY_POINT EntryPoint,
  27. IN VOID *Context1 OPTIONAL,
  28. IN VOID *Context2 OPTIONAL,
  29. IN VOID *NewStack
  30. )
  31. {
  32. BASE_LIBRARY_JUMP_BUFFER JumpBuffer;
  33. ASSERT (EntryPoint != NULL);
  34. ASSERT (NewStack != NULL);
  35. //
  36. // Stack should be aligned with CPU_STACK_ALIGNMENT
  37. //
  38. ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);
  39. JumpBuffer.Eip = (UINTN)EntryPoint;
  40. JumpBuffer.Esp = (UINTN)NewStack - sizeof (VOID *);
  41. JumpBuffer.Esp -= sizeof (Context1) + sizeof (Context2);
  42. ((VOID **)JumpBuffer.Esp)[1] = Context1;
  43. ((VOID **)JumpBuffer.Esp)[2] = Context2;
  44. LongJump (&JumpBuffer, (UINTN)-1);
  45. //
  46. // PeiSwitchStacks () will never return
  47. //
  48. ASSERT (FALSE);
  49. }