kern

changeset 45:b8f02479e3f4

mainly additions to the VM to support processes etc. not complete
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Jul 2011 02:41:33 +0300
parents 7bc74736c7e8
children b793b8fcba7d
files src/proc.c src/vm.c
diffstat 2 files changed, 72 insertions(+), 4 deletions(-) [+]
line diff
     1.1 --- a/src/proc.c	Mon Jul 25 11:29:57 2011 +0300
     1.2 +++ b/src/proc.c	Tue Jul 26 02:41:33 2011 +0300
     1.3 @@ -1,22 +1,36 @@
     1.4  #include "proc.h"
     1.5  #include "tss.h"
     1.6 +#include "vm.h"
     1.7  
     1.8  static struct process proc[MAX_PROC];
     1.9  static int cur_pid;
    1.10  
    1.11  void init_proc(void)
    1.12  {
    1.13 +	int proc_size_pg, img_start_pg;
    1.14 +	void *img_start;
    1.15  	cur_pid = -1;
    1.16  
    1.17  	/* prepare the first process */
    1.18  
    1.19 -	/* create the virtual address space for this process */
    1.20 -	proc[0].ctx.pgtbl_paddr = clone_vmem();
    1.21 -
    1.22  	/* allocate a chunk of memory for the process image
    1.23  	 * and copy the code of test_proc there.
    1.24  	 * (should be mapped at a fixed address)
    1.25  	 */
    1.26 +	proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
    1.27 +	if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
    1.28 +		panic("failed to allocate space for the init process image\n");
    1.29 +	}
    1.30 +	img_start = (void*)PAGE_TO_ADDR(img_start_pg);
    1.31 +	memcpy(img_start, test_proc, proc_size_pg * PGSIZE);
    1.32 +
    1.33 +	/* create the virtual address space for this process */
    1.34 +	proc[0].ctx.pgtbl_paddr = clone_vmem();
    1.35 +
    1.36 +	/* we don't need the image in this address space */
    1.37 +	unmap_pa
    1.38 +	pgfree(img_start_pg, proc_size_pg);
    1.39 +
    1.40  
    1.41  	/* fill in the proc[0].ctx with the appropriate process stack
    1.42  	 * and instruction pointers
     2.1 --- a/src/vm.c	Mon Jul 25 11:29:57 2011 +0300
     2.2 +++ b/src/vm.c	Tue Jul 26 02:41:33 2011 +0300
     2.3 @@ -250,6 +250,7 @@
     2.4  {
     2.5  	int intr_state, ret = -1;
     2.6  	struct page_range *node, *prev, dummy;
     2.7 +	unsigned int attr = 0;	/* TODO */
     2.8  
     2.9  	intr_state = get_intr_state();
    2.10  	disable_intr();
    2.11 @@ -281,7 +282,60 @@
    2.12  
    2.13  	if(ret >= 0) {
    2.14  		/* allocate physical storage and map */
    2.15 -		if(map_page_range(ret, num, -1, 0) == -1) {
    2.16 +		if(map_page_range(ret, num, -1, attr) == -1) {
    2.17 +			ret = -1;
    2.18 +		}
    2.19 +	}
    2.20 +
    2.21 +	set_intr_state(intr_state);
    2.22 +	return ret;
    2.23 +}
    2.24 +
    2.25 +int pgalloc_vrange(int start, int num)
    2.26 +{
    2.27 +	struct page_range *node, *prev, dummy;
    2.28 +	int area, intr_state, ret = -1;
    2.29 +	unsigned int attr = 0;	/* TODO */
    2.30 +
    2.31 +	area = (start >= ADDR_TO_PAGE(KMEM_START)) ? MEM_KERNEL : MEM_USER;
    2.32 +	if(area == KMEM_USER && start + num > ADDR_TO_PAGE(KMEM_START)) {
    2.33 +		printf("pgalloc_vrange: invalid range request crossing user/kernel split\n");
    2.34 +		return -1;
    2.35 +	}
    2.36 +
    2.37 +	intr_state = get_intr_state();
    2.38 +	disable_intr();
    2.39 +
    2.40 +	dummy.next = pglist[area];
    2.41 +	node = pglist[area];
    2.42 +	prev = &dummy;
    2.43 +
    2.44 +	/* check to see if the requested VM range is available */
    2.45 +	node = pglist[area];
    2.46 +	while(node) {
    2.47 +		if(start >= node->start && start + num <= node->end) {
    2.48 +			ret = node->start;
    2.49 +			node->start += num;
    2.50 +
    2.51 +			if(node->start == node->end) {
    2.52 +				prev->next = node->next;
    2.53 +				node->next = 0;
    2.54 +
    2.55 +				if(node == pglist[area]) {
    2.56 +					pglist[area] = 0;
    2.57 +				}
    2.58 +				free_node(node);
    2.59 +			}
    2.60 +			break;
    2.61 +		}
    2.62 +
    2.63 +		prev = node;
    2.64 +		node = node->next;
    2.65 +	}
    2.66 +
    2.67 +	if(ret >= 0) {
    2.68 +		/* allocate physical storage and map */
    2.69 +		if(map_page_range(ret, num, -1, attr) == -1) {
    2.70  			ret = -1;
    2.71  		}
    2.72  	}