Fix Bug: Grow stack if necessary in case of a page fault in the kernel context

This commit is contained in:
sBubshait
2024-12-05 01:15:46 +00:00
parent f171a05108
commit 52ec8fe779

View File

@@ -157,27 +157,30 @@ page_fault (struct intr_frame *f)
write = (f->error_code & PF_W) != 0; write = (f->error_code & PF_W) != 0;
user = (f->error_code & PF_U) != 0; user = (f->error_code & PF_U) != 0;
if (!user || !not_present) /* Select the appropriate stack pointer based on the context of the fault. */
{ void *esp = user ? f->esp : thread_current()->curr_esp;
f->eip = (void *)f->eax;
f->eax = 0xffffffff;
return;
}
/* If the fault address is in a user page that is not present, then it might /* If the fault address is in a user page that is not present, then it might
be just that the stack needs to grow or that it needs to be lazily loaded. be just that the stack needs to grow or that it needs to be lazily loaded.
So we attempt to grow the stack. If this does not work, we check our SPT to see if the page So we attempt to grow the stack. If this does not work, we check our SPT to
is expected to have data loaded in memory. */ see if the page is expected to have data loaded in memory. */
void *upage = pg_round_down (fault_addr); void *upage = pg_round_down (fault_addr);
if (not_present && is_user_vaddr (upage) && upage != NULL) if (not_present && is_user_vaddr (upage) && upage != NULL)
{ {
if (is_valid_stack_access (fault_addr, f->esp)) if (is_valid_stack_access (fault_addr, esp))
{
if (grow_stack (upage)) if (grow_stack (upage))
return; return;
if (try_fetch_page (upage, write))
return;
} }
if (try_fetch_page (upage, write)) /* If the page fault occurred in kernel mode, then we intentionally indicate
a fault (for get_user() etc). */
if (!user)
{
f->eip = (void *)f->eax;
f->eax = 0xffffffff;
return; return;
} }