1 /*
2 * linux-3.9.4/mykernel/myinterrupt.c
3 * 内核自定义启动代码2-定时处理程序和任务调度器
4 */
5
6 #include <linux/types.h>
7 #include <linux/string.h>
8 #include <linux/ctype.h>
9 #include <linux/tty.h>
10 #include <linux/vmalloc.h>
11 #include "mypcb.h"
12
13 extern tPCB task[MAX_TASK_NUM];
14 extern tPCB* my_current_task;
15 extern volatile int my_need_sched;
16 volatile int time_count = 0;
17
18 //定时处理程序
19 void my_timer_handler(void)
20 {
21 #if 1
22 if (time_count % 1000 == 0 && my_need_sched != 1)
23 {
24 printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
25 my_need_sched = 1;
26 }
27 time_count++;
28 #endif
29 return;
30 }
31
32 //任务调度器
33 void my_schedule(void)
34 {
35 tPCB* next;
36 tPCB* prev;
37 //如果当前任务为空 或 没有后续任务
38 if (my_current_task == NULL || my_current_task->next == NULL)
39 {
40 return;
41 }
42
43 printk(KERN_NOTICE ">>>my_shcedule<<\n");
44
45 //更新任务链表指针
46 next = my_current_task->next;
47 prev = my_current_task;
48 //如果任务的状态表明可运行,则调度
49 if (next->state == 0)
50 {
51 //保存当前任务的ebp和esp到stack中
52 //将ebp和esp切换到后续任务
53 __asm__ volatile
54 (
55 "pushl %%ebp\n\t" /* save current task's base stack address [edp] to stack */
56 "mov1 %%esp, %0\n\t" /* save esp to prev->thread.sp */
57 "mov1 %2, %%esp\n\t" /* move next->thread.sp to esp */
58 /* 1f表示在后面的代码中搜寻标识为1的代码段 */
59 /* 1b标识在前面的代码中搜寻标识为1的代码段 */
60 /* 数字1可变化 */
61 "mov1 $1f, %1\n\t" /* save current task's eip to pre->thread.ip, [f] means search label before;[b] means search label behind */
62 "push1 %3,\n\t" /* switch to next->thread.ip */
63 "ret\n\t"
64 "1:\t" /* switch to next process start */
65 "pop1 %%edp\n\t" /* */
66 : "=m" (prev->thread.sp), "=m" (prev->thread.ip)
67 : "=m" (next->thread.sp), "=m" (next->thread.ip)
68 );
69
70 my_current_task = next;
71 printk(KERN_NOTICE ">>>switch %d to %d<<<\n", prev->pid, next->pid);
72 }
73 else //启动新任务
74 {
75 next->state = 0;
76 my_current_task = next;
77 printk(KERN_NOTICE ">>>switch %d to %d<<<\n", prev->pid, next->pid);
78
79 //功能同上
80 __asm__ volatile
81 (
82 "push1 %%ebp\n\t" /* save stack base address [ebp]*/
83 "mov1 %%esp, %0\n\t" /* save [esp] to prev->thread.sp */
84 "mov1 %2, %%esp\n\t" /* mov next->thread.sp to [esp] */
85 "mov1 %2, %%ebp\n\t" /* update [ebp] */
86 "mov1 %1f, %1\n\t" /* save [eip] to prev->thread.ip */
87 "push1 %3\n\t" /* save next->thread.ip to [stack] */
88 "ret\n\t" /* pop next->thead.ip to [eip] */
89 : "=m" (prev->thread.sp), "=m" (prev->thread.ip)
90 : "=m" (next->thread.sp), "=m" (next->thread.ip)
91 );
92
93 return;
94 }
95 }