diff --git a/kernel/address_spaces.c b/kernel/address_spaces.c index adeadb6..828801e 100644 --- a/kernel/address_spaces.c +++ b/kernel/address_spaces.c @@ -3,19 +3,16 @@ #include void address_spaces_copy_data(void* address_space, void* data,size_t size,void* virt_addr) { - void* old_address_space=get_address_space(); void* phys_addr=virt_to_phys(data); - load_address_space(address_space); - map_pages(virt_addr,phys_addr,(size/PAGE_SZ)+1,1,1); - load_address_space(old_address_space); + RUN_IN_ADDRESS_SPACE(address_space,map_pages(virt_addr,phys_addr,(size/PAGE_SZ)+1,1,1)); } void* address_spaces_put_data(void* address_space, void* data,size_t size) { - void* old_address_space=get_address_space(); void* phys_addr=virt_to_phys(data); - load_address_space(address_space); - void* virt_addr=find_free_pages((size/PAGE_SZ)+1); - map_pages(virt_addr,phys_addr,(size/PAGE_SZ)+1,1,1); - load_address_space(old_address_space); + void* virt_addr; + RUN_IN_ADDRESS_SPACE(address_space,{ + virt_addr=find_free_pages((size/PAGE_SZ)+1); + map_pages(virt_addr,phys_addr,(size/PAGE_SZ)+1,1,1); + }); return virt_addr; } diff --git a/kernel/cpu/i386/tasking_helpers_c.c b/kernel/cpu/i386/tasking_helpers_c.c index 6c9a5a3..d93e800 100644 --- a/kernel/cpu/i386/tasking_helpers_c.c +++ b/kernel/cpu/i386/tasking_helpers_c.c @@ -54,9 +54,7 @@ static int new_kstack() { } void setup_kstack(Thread* thread,void* param1,void* param2,char kmode,void* eip) { - void* old_address_space=get_address_space(); size_t kstack_num=new_kstack(); - load_address_space(thread->address_space); if (kmode) { size_t top_idx=(1024*(kstack_num+1)); thread->kernel_esp=((void*)(&kstacks[top_idx-5])); @@ -66,13 +64,15 @@ void setup_kstack(Thread* thread,void* param1,void* param2,char kmode,void* eip) size_t top_idx=(1024*(kstack_num+1)); thread->kernel_esp=((void*)(&kstacks[top_idx-7])); thread->kernel_esp_top=thread->kernel_esp; - void** user_stack=(void**)(((char*)alloc_pages(2))+0x2000); - user_stack-=2; - user_stack[0]=param1; - user_stack[1]=param2; + void** user_stack; + RUN_IN_ADDRESS_SPACE(thread->address_space,{ + user_stack=(void**)(((char*)alloc_pages(2))+0x2000); + user_stack-=2; + user_stack[0]=param1; + user_stack[1]=param2; + }); kstacks[top_idx-3]=(void*)task_init; kstacks[top_idx-2]=(void*)user_stack; kstacks[top_idx-1]=(void*)eip; } - load_address_space(old_address_space); } diff --git a/kernel/cpu/paging.h b/kernel/cpu/paging.h index b25bdf0..e850f11 100644 --- a/kernel/cpu/paging.h +++ b/kernel/cpu/paging.h @@ -5,6 +5,20 @@ #ifndef PAGING_H #define PAGING_H + +/** + * Run a block of code in a different address space + * \param addr_space the address space to use + * \param codeblock the block of code to run +*/ +#define RUN_IN_ADDRESS_SPACE(addr_space,codeblock) do { \ + void* old_address_space=get_address_space(); \ + load_address_space(addr_space); \ + codeblock; \ + load_address_space(old_address_space); \ +} while(0); + + /** * Map virtual pages to physical frames. * \param virt_addr_ptr The start of the virtual range to map. @@ -21,17 +35,24 @@ void map_pages(void* virt_addr_ptr,void* phys_addr_ptr,int num_pages,char usr,ch */ void unmap_pages(void* start_virt,int num_pages); /** - * Allocate virtual pages & map them to physical memory. + * Allocate virtual pages & map them to newly allocated physical memory. * \param num_pages The number of pages to allocate. * \return a pointer to the allocated pages. */ void* alloc_pages(int num_pages); /** - * Allocate virtual pages at a specific address & map them to physical memory. + * Allocate virtual pages at a specific address & map them to newly allocated physical memory. * \param num_pages The number of pages to allocate. * \param addr The adress to start allocation at. */ void alloc_pages_virt(int num_pages,void* addr); +/** + * Deallocate the physical memory for pages and unmap them + * \param num_pages The number of pages to deallocate. + * \param addr The adress to start deallocation at. +*/ +void dealloc_pages(int num_pages,void* addr); + /** * Initialize paging */