diff --git a/xtldr/amd64/memory.c b/xtldr/amd64/memory.c index 5f77925..4a3c8c4 100644 --- a/xtldr/amd64/memory.c +++ b/xtldr/amd64/memory.c @@ -9,6 +9,33 @@ #include +VOID +BlCreateStack(IN PVOID *StackPtr, + IN ULONG StackSize, + IN PVOID Callback) +{ + EFI_PHYSICAL_ADDRESS Address; + PVOID StackEnd; + + /* Allocate pages for new stack and calculate its end */ + BlEfiMemoryAllocatePages(StackSize, &Address); + *StackPtr = (PVOID)(UINT_PTR)Address; + StackEnd = (PUINT8)*StackPtr + (StackSize * EFI_PAGE_SIZE) - EFI_PAGE_SIZE; + + /* Create new stack and switch to it immediatelly by calling callback function */ + asm volatile("mov %1, %%rax\n" + "mov %%rsp, %%rbx\n" + "mov %0, %%rsp\n" + "push %%rbp\n" + "mov %%rsp, %%rbp\n" + "push %%rbx\n" + "sub $32, %%rsp\n" + "call *%%rax\n" + : + : "m" (StackEnd), "m" (Callback) + : "rax", "rbx"); +} + /** * This routine does the actual virtual memory mapping. * diff --git a/xtldr/includes/xtbl.h b/xtldr/includes/xtbl.h index 7c19eee..59c09e4 100644 --- a/xtldr/includes/xtbl.h +++ b/xtldr/includes/xtbl.h @@ -54,6 +54,11 @@ BlConsoleInitialize(); VOID BlConsolePutChar(IN USHORT Character); +VOID +BlCreateStack(IN PVOID *StackPtr, + IN ULONG StackSize, + IN PVOID Callback); + VOID BlDbgPrint(IN PUINT16 Format, IN ...);