From 68d07bdfaae35cb954f55c88366755eb2e387c0b Mon Sep 17 00:00:00 2001 From: Yury Popov Date: Thu, 23 May 2019 19:55:57 +0300 Subject: [PATCH] Fix stack-related exception on process exit --- src/platform/syscall.cpp | 7 +++++-- src/process/process.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/platform/syscall.cpp b/src/platform/syscall.cpp index b38d018..7b57af1 100644 --- a/src/platform/syscall.cpp +++ b/src/platform/syscall.cpp @@ -17,11 +17,14 @@ static void syscall_puts(uintptr_t strptr) { static void syscall_exit(int code) { ProcessManager *manager = ProcessManager::getManager(); Process *process = manager->currentProcess(); + void *rsp; asm volatile("mov %%rsp, %q0; and $~0xFFF, %q0":"=r"(rsp)); + Pagetable::Entry *pt; asm volatile("mov %%cr3, %q0":"=r"(pt)); + Pagetable::Entry *pte = Pagetable::Entry::find(rsp, pt); asm volatile( "callq _ZN7Process4exitEi;" - "sti;" + "sti; movq $0, %q2;" "jmp _ZN14ProcessManager12process_loopEv" - ::"D"(process), "S"(code) + ::"D"(process), "S"(code), "a"(pte) ); } diff --git a/src/process/process.cpp b/src/process/process.cpp index d89b373..683bcab 100644 --- a/src/process/process.cpp +++ b/src/process/process.cpp @@ -16,6 +16,7 @@ Process::Process() { _aslrStack = RAND::get(0x40000000llu, 0x80000000llu) << 12; } Process::~Process() { + void *rsp; asm volatile("mov %%rsp, %q0; and $~0xFFF, %q0":"=r"(rsp)); if (pagetable != nullptr) { PTE addr; for (uintptr_t ptx = 0; ptx < 512; ptx++) { @@ -38,10 +39,11 @@ Process::~Process() { if (!addr.present) continue; void *page = addr.getPtr(); - if (uintptr_t(page) == ((ptx << (12 + 9 + 9 + 9)) + uintptr_t ptaddr = ((ptx << (12 + 9 + 9 + 9)) | (pdx << (12 + 9 + 9)) - | (pdpx << (12 + 9)) | (pml4x << (12)))) - continue; + | (pdpx << (12 + 9)) | (pml4x << (12))); + if (page == rsp) continue; + if (uintptr_t(page) == ptaddr) continue; Pagetable::free(page); } Pagetable::free(ppml4e);