Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix stack-related exception on process exit
  • Loading branch information
djphoenix committed May 23, 2019
1 parent 05d9ba7 commit 68d07bd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/platform/syscall.cpp
Expand Up @@ -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)
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/process/process.cpp
Expand Up @@ -16,6 +16,7 @@ Process::Process() {
_aslrStack = RAND::get<uintptr_t>(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++) {
Expand All @@ -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);
Expand Down

0 comments on commit 68d07bd

Please sign in to comment.