Add a macro to run a block of code in a different address space

This commit is contained in:
pjht 2020-07-29 08:58:45 -05:00
parent dda2885fd9
commit 96d7a839fb
3 changed files with 36 additions and 18 deletions

View File

@ -3,19 +3,16 @@
#include <stddef.h>
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);
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);
load_address_space(old_address_space);
});
return virt_addr;
}

View File

@ -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);
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);
}

View File

@ -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
*/