1. 程式人生 > 其它 >核心自定義啟動程式碼01

核心自定義啟動程式碼01

 1 /*
 2 * linux-3.9.4/mykernel/mymain.c
 3 * 核心自定義啟動程式碼
 4 */
 5 
 6 #include <linux/types.h>
 7 #include <linux/string.h>
 8 #include <linux/ctypes.h>
 9 #include <linux/tty.h>
10 #include <linux/vmalloc.h>
11 
12 #include "mypcb.h"
13 
14 tPCB task[MAX_TASK_NUM];
15 tPCB* my_current_task = NULL;
16 volatile int my_need_sched = 0; 17 18 void my_process(void); 19 20 void __init my_start_kernel(void) 21 { 22 int pid = 0; 23 int i; 24 task[pid].pid = pid; 25 task[pid].state = 0; /*-1 暫停, 0 可執行, >0 已停止*/ 26 task[pid].task_entry = task[pid].thread.ip = (unsigned long)myprocess; 27
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE - 1]; 28 task[pid].next = &task[pid]; 29 30 for (i = 1; i < MAX_TASK_NUM; i++) 31 { 32 memcpy(&task[i], &task[0], sizeof(tPCB)); 33 task[i].pid = i; 34 task[i].state = -1
; 35 task[i].thread.sp = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE - 1]; 36 task[i].next = task[i - 1].next; 37 task[i - 1].next = &task[i]; 38 } 39 40 pid = 0; 41 my_current_task = &task[pid]; 42 asm volatile( 43 "mov %1, %%esp\n\t" /* 將task[pid].thread.sp載入到[esp] */ 44 "pushl %1\n\t" /* 儲存當前任務堆疊基地址 */ 45 "pushl %0\n\t" /* 儲存當前任務指令地址 */ 46 "ret\n\t" /* 將上1行程式碼儲存的指令地址載入到[eip] */ 47 "popl %%ebp\n\t" 48 : 49 : "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) 50 ); 51 } 52 53 void my_process(void) 54 { 55 int i = 0; 56 while (1) 57 { 58 i++; 59 if (i % 10000000 == 0) 60 { 61 printk(KERN_NOTICE "this is process %d -\n", my_current_task->pid); 62 /* 如果需要排程 */ 63 if (my_need_sched == 1) 64 { 65 my_need_sched = 0; 66 my_schedule(); 67 } 68 printk(KERN_NOTICE "this is process %d+\n", my_current_task->pid); 69 } 70 } 71 }