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 }