At this point the threads are really processes...
This commit is contained in:
		
							parent
							
								
									6523b10984
								
							
						
					
					
						commit
						e868b83f50
					
				| @ -5,17 +5,17 @@ | ||||
| #include <multiboot.h> | ||||
| #include <cpu.h> | ||||
| #include <interrupts.h> | ||||
| #include <thread.h> | ||||
| #include <process.h> | ||||
| #include <scheduler.h> | ||||
| 
 | ||||
| int *thread_id = (int *)0x20000; | ||||
| int *pid = (int *)0x20000; | ||||
| void thread_function() | ||||
| { | ||||
|   *thread_id = thread()->tid; | ||||
|   *pid = process()->pid; | ||||
| 
 | ||||
|   while(1) | ||||
|   { | ||||
|     debug("Thread %d\n", *thread_id); | ||||
|     debug("Process %d\n", *pid); | ||||
|     yield(); | ||||
|   } | ||||
| } | ||||
| @ -35,16 +35,16 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data) | ||||
| 
 | ||||
|   cpu_init(); | ||||
| 
 | ||||
|   struct thread *th1 = new_thread(thread_function); | ||||
|   vmm_set_page(th1->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); | ||||
|   struct thread *th2 = new_thread(thread_function); | ||||
|   vmm_set_page(th2->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); | ||||
|   struct thread *th3 = new_thread(thread_function); | ||||
|   vmm_set_page(th3->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); | ||||
|   struct process *p1 = new_process(thread_function); | ||||
|   vmm_set_page(p1->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); | ||||
|   struct process *p2 = new_process(thread_function); | ||||
|   vmm_set_page(p2->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); | ||||
|   struct process *p3 = new_process(thread_function); | ||||
|   vmm_set_page(p3->P4, 0x20000, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT); | ||||
| 
 | ||||
|   ready(th1); | ||||
|   ready(th2); | ||||
|   ready(th3); | ||||
|   ready(p1); | ||||
|   ready(p2); | ||||
|   ready(p3); | ||||
| 
 | ||||
|   debug_ok("Boot \"Complete\"\n"); | ||||
| 
 | ||||
|  | ||||
| @ -3,9 +3,9 @@ | ||||
| #include <scheduler.h> | ||||
| #include <stdint.h> | ||||
| 
 | ||||
| struct thread | ||||
| struct process | ||||
| { | ||||
|   uint64_t tid; | ||||
|   uint64_t pid; | ||||
|   void *stack_ptr; | ||||
|   uint64_t state; | ||||
|   uint64_t P4; | ||||
| @ -13,9 +13,9 @@ struct thread | ||||
|   uint8_t stack[]; | ||||
| }; | ||||
| 
 | ||||
| struct thread *thread(); | ||||
| struct process *process(); | ||||
| 
 | ||||
| struct thread *new_thread(void (*function)(void)); | ||||
| struct process *new_process(void (*function)(void)); | ||||
| void yield(); | ||||
| void start_scheduler(); | ||||
| 
 | ||||
| @ -1,8 +1,8 @@ | ||||
| #pragma once | ||||
| #include <queue.h> | ||||
| 
 | ||||
| #define runQ readyQ, readyQ_next, struct thread | ||||
| #define runQ readyQ, readyQ_next, struct process | ||||
| QUEUE_DECLARE(runQ); | ||||
| 
 | ||||
| void ready(struct thread *th); | ||||
| struct thread *scheduler_next(); | ||||
| void ready(struct process *proc); | ||||
| struct process *scheduler_next(); | ||||
|  | ||||
							
								
								
									
										69
									
								
								src/kernel/proc/process.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/kernel/proc/process.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,69 @@ | ||||
| #include <process.h> | ||||
| #include <scheduler.h> | ||||
| #include <memory.h> | ||||
| #include <cpu.h> | ||||
| 
 | ||||
| struct swtch_stack | ||||
| { | ||||
|   uint64_t RBP; | ||||
|   uint64_t RBX; | ||||
|   uint64_t R12; | ||||
|   uint64_t R13; | ||||
|   uint64_t R14; | ||||
|   uint64_t R15; | ||||
|   uint64_t RBP2; | ||||
|   uint64_t ret; | ||||
| }__attribute__((packed)); | ||||
| 
 | ||||
| struct process *sched_proc = 0; | ||||
| struct process *_proc = 0; | ||||
| 
 | ||||
| uint64_t next_pid = 1; | ||||
| struct process *new_process(void (*function)(void)) | ||||
| { | ||||
|   struct process *proc = P2V(pmm_calloc()); | ||||
|   proc->pid = next_pid++; | ||||
|   proc->stack_ptr = incptr(proc, PAGE_SIZE - sizeof(struct swtch_stack)); | ||||
|   proc->P4 = new_P4(); | ||||
| 
 | ||||
|   struct swtch_stack *stk = proc->stack_ptr; | ||||
|   stk->RBP = (uint64_t)&stk->RBP2; | ||||
|   stk->ret = (uint64_t)function; | ||||
| 
 | ||||
|   return proc; | ||||
| } | ||||
| 
 | ||||
| struct process *process() | ||||
| { | ||||
|   return _proc; | ||||
| } | ||||
| 
 | ||||
| void yield() | ||||
| { | ||||
|   switch_stack(&_proc->stack_ptr, &sched_proc->stack_ptr); | ||||
| } | ||||
| 
 | ||||
| void scheduler() | ||||
| { | ||||
|   while(1) | ||||
|   { | ||||
|     struct process *new = 0; | ||||
|     while(!(new = scheduler_next())); | ||||
| 
 | ||||
|     _proc = new; | ||||
|     write_cr3(new->P4); | ||||
|     switch_stack(&sched_proc->stack_ptr, &new->stack_ptr); | ||||
| 
 | ||||
|     ready(_proc); | ||||
|     _proc = 0; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void start_scheduler() | ||||
| { | ||||
|   sched_proc = new_process(scheduler); | ||||
|   sched_proc->pid = (uint64_t)-1; | ||||
| 
 | ||||
|   uint64_t stack; | ||||
|   switch_stack(&stack, &sched_proc->stack_ptr); | ||||
| } | ||||
| @ -1,15 +1,15 @@ | ||||
| #include <scheduler.h> | ||||
| #include <queue.h> | ||||
| #include <thread.h> | ||||
| #include <process.h> | ||||
| 
 | ||||
| QUEUE_DEFINE(runQ); | ||||
| 
 | ||||
| void ready(struct thread *th) | ||||
| void ready(struct process *proc) | ||||
| { | ||||
|   queue_add(runQ, th); | ||||
|   queue_add(runQ, proc); | ||||
| } | ||||
| 
 | ||||
| struct thread *scheduler_next() | ||||
| struct process *scheduler_next() | ||||
| { | ||||
|   return queue_pop(runQ); | ||||
| } | ||||
|  | ||||
| @ -1,69 +0,0 @@ | ||||
| #include <thread.h> | ||||
| #include <scheduler.h> | ||||
| #include <memory.h> | ||||
| #include <cpu.h> | ||||
| 
 | ||||
| struct swtch_stack | ||||
| { | ||||
|   uint64_t RBP; | ||||
|   uint64_t RBX; | ||||
|   uint64_t R12; | ||||
|   uint64_t R13; | ||||
|   uint64_t R14; | ||||
|   uint64_t R15; | ||||
|   uint64_t RBP2; | ||||
|   uint64_t ret; | ||||
| }__attribute__((packed)); | ||||
| 
 | ||||
| struct thread *sched_th = 0; | ||||
| struct thread *_thread = 0; | ||||
| 
 | ||||
| uint64_t next_tid = 1; | ||||
| struct thread *new_thread(void (*function)(void)) | ||||
| { | ||||
|   struct thread *th = P2V(pmm_calloc()); | ||||
|   th->tid = next_tid++; | ||||
|   th->stack_ptr = incptr(th, PAGE_SIZE - sizeof(struct swtch_stack)); | ||||
|   th->P4 = new_P4(); | ||||
| 
 | ||||
|   struct swtch_stack *stk = th->stack_ptr; | ||||
|   stk->RBP = (uint64_t)&stk->RBP2; | ||||
|   stk->ret = (uint64_t)function; | ||||
| 
 | ||||
|   return th; | ||||
| } | ||||
| 
 | ||||
| struct thread *thread() | ||||
| { | ||||
|   return _thread; | ||||
| } | ||||
| 
 | ||||
| void yield() | ||||
| { | ||||
|   switch_stack(&_thread->stack_ptr, &sched_th->stack_ptr); | ||||
| } | ||||
| 
 | ||||
| void scheduler() | ||||
| { | ||||
|   while(1) | ||||
|   { | ||||
|     struct thread *new = 0; | ||||
|     while(!(new = scheduler_next())); | ||||
| 
 | ||||
|     _thread = new; | ||||
|     write_cr3(new->P4); | ||||
|     switch_stack(&sched_th->stack_ptr, &new->stack_ptr); | ||||
| 
 | ||||
|     ready(_thread); | ||||
|     _thread = 0; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void start_scheduler() | ||||
| { | ||||
|   sched_th = new_thread(scheduler); | ||||
|   sched_th->tid = (uint64_t)-1; | ||||
| 
 | ||||
|   uint64_t stack; | ||||
|   switch_stack(&stack, &sched_th->stack_ptr); | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user