kern

diff src/proc.c @ 51:b1e8c8251884

lalalala
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Aug 2011 06:45:29 +0300
parents 1d8877d12de0
children fa65b4f45366
line diff
     1.1 --- a/src/proc.c	Sat Jul 30 07:35:53 2011 +0300
     1.2 +++ b/src/proc.c	Mon Aug 01 06:45:29 2011 +0300
     1.3 @@ -5,6 +5,8 @@
     1.4  #include "segm.h"
     1.5  #include "intr.h"
     1.6  #include "panic.h"
     1.7 +#include "syscall.h"
     1.8 +#include "sched.h"
     1.9  
    1.10  
    1.11  /* defined in test_proc.S */
    1.12 @@ -18,50 +20,50 @@
    1.13  {
    1.14  	int proc_size_pg, img_start_pg, stack_pg;
    1.15  	void *img_start;
    1.16 -	cur_pid = -1;
    1.17 +	cur_pid = 0;
    1.18 +
    1.19 +	init_syscall();
    1.20  
    1.21  	/* prepare the first process */
    1.22 +	proc[1].id = 1;
    1.23 +	proc[1].parent = 0;
    1.24  
    1.25  	/* allocate a chunk of memory for the process image
    1.26  	 * and copy the code of test_proc there.
    1.27  	 * (should be mapped at a fixed address)
    1.28  	 */
    1.29 -	/*proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
    1.30 +	proc_size_pg = (test_proc_end - test_proc) / PGSIZE + 1;
    1.31  	if((img_start_pg = pgalloc(proc_size_pg, MEM_USER)) == -1) {
    1.32  		panic("failed to allocate space for the init process image\n");
    1.33  	}
    1.34  	img_start = (void*)PAGE_TO_ADDR(img_start_pg);
    1.35 -	memcpy(img_start, test_proc, proc_size_pg * PGSIZE);*/
    1.36 -	img_start = test_proc;
    1.37 +	memcpy(img_start, test_proc, proc_size_pg * PGSIZE);
    1.38  
    1.39  	/* instruction pointer at the beginning of the process image */
    1.40 -	proc[0].ctx.instr_ptr = (uint32_t)img_start;
    1.41 +	proc[1].ctx.instr_ptr = (uint32_t)img_start;
    1.42  
    1.43  	/* allocate the first page of the process stack */
    1.44  	stack_pg = ADDR_TO_PAGE(KMEM_START) - 1;
    1.45  	if(pgalloc_vrange(stack_pg, 1) == -1) {
    1.46  		panic("failed to allocate user stack page\n");
    1.47  	}
    1.48 -	proc[0].ctx.stack_ptr = PAGE_TO_ADDR(stack_pg) + PGSIZE;
    1.49 +	proc[1].ctx.stack_ptr = PAGE_TO_ADDR(stack_pg) + PGSIZE;
    1.50  
    1.51  	/* create the virtual address space for this process */
    1.52 -	proc[0].ctx.pgtbl_paddr = clone_vm();
    1.53 +	proc[1].ctx.pgtbl_paddr = clone_vm();
    1.54  
    1.55  	/* we don't need the image and the stack in this address space */
    1.56 -	/*unmap_page_range(img_start_pg, proc_size_pg);
    1.57 -	pgfree(img_start_pg, proc_size_pg);*/
    1.58 +	unmap_page_range(img_start_pg, proc_size_pg);
    1.59 +	pgfree(img_start_pg, proc_size_pg);
    1.60  
    1.61  	unmap_page(stack_pg);
    1.62  	pgfree(stack_pg, 1);
    1.63  
    1.64 +	/* add it to the scheduler queues */
    1.65 +	//add_proc(1, STATE_RUNNING);
    1.66  
    1.67 -	/* switch to it by calling a function that takes the context
    1.68 -	 * of the current process, plugs the values into the interrupt
    1.69 -	 * stack, and calls iret.
    1.70 -	 * (should also set ss0/sp0 in TSS before returning)
    1.71 -	 */
    1.72 -	context_switch(0);
    1.73 -	/* XXX this will never return */
    1.74 +	/* switch to the initial process, this never returns */
    1.75 +	context_switch(1);
    1.76  }
    1.77  
    1.78  
    1.79 @@ -76,7 +78,7 @@
    1.80  	ifrm.inum = ifrm.err = 0;
    1.81  
    1.82  	ifrm.regs = ctx->regs;
    1.83 -	ifrm.eflags = ctx->flags;
    1.84 +	ifrm.eflags = ctx->flags | (1 << 9);
    1.85  
    1.86  	ifrm.eip = ctx->instr_ptr;
    1.87  	ifrm.cs = selector(SEGM_KCODE, 0);	/* XXX change this when we setup the TSS */
    1.88 @@ -89,3 +91,18 @@
    1.89  
    1.90  	intr_ret(ifrm);
    1.91  }
    1.92 +
    1.93 +int get_current_pid(void)
    1.94 +{
    1.95 +	return cur_pid;
    1.96 +}
    1.97 +
    1.98 +struct process *get_current_proc(void)
    1.99 +{
   1.100 +	return cur_pid ? &proc[cur_pid] : 0;
   1.101 +}
   1.102 +
   1.103 +struct process *get_process(int pid)
   1.104 +{
   1.105 +	return &proc[pid];
   1.106 +}