feat: allow stack to grow for process up to 8MB in size

This commit is contained in:
EDiasAlberto
2024-11-26 04:43:25 +00:00
parent 605050e38d
commit 3ef5264b6e
4 changed files with 57 additions and 0 deletions

38
src/vm/stackgrowth.c Normal file
View File

@@ -0,0 +1,38 @@
#include <stdio.h>
#include "stackgrowth.h"
#include "threads/palloc.h"
#include "threads/thread.h"
#include "threads/vaddr.h"
#include "userprog/pagedir.h"
/* Validates a given address for being <=32 bytes away from the stack pointer or
above the stack */
bool needs_new_page (void *addr, void *esp)
{
return (is_user_vaddr (addr) &&
(uint32_t*)addr >= ((uint32_t*)esp - 32) &&
((PHYS_BASE - pg_round_down (addr))
<= MAX_STACK_SIZE));
}
/* Extends the stack by the necessary number of pages */
bool grow_stack (void *addr)
{
struct thread *t = thread_current ();
void *last_page = pg_round_down (addr);
uint8_t *new_page = palloc_get_page (PAL_USER | PAL_ZERO);
if ( new_page == NULL)
return false;
bool added_page = pagedir_get_page (t->pagedir, last_page) == NULL
&& pagedir_set_page (t->pagedir, last_page, new_page, true);
if (!added_page) {
palloc_free_page (new_page);
return false;
}
return true;
}