核心自定義啟動程式碼2-定時處理程式和任務排程器
阿新 • • 發佈:2021-10-20
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 }