内核自定义启动代码01

发布时间:2022-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了内核自定义启动代码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)&amp;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, %%espnt"    /* 将task[pid].thread.sp加载到[esp] */
44         "pushl %1nt"        /* 保存当前任务堆栈基地址 */
45         "pushl %0nt"        /* 保存当前任务指令地址 */
46         "retnt"            /* 将上1行代码保存的指令地址加载到[eip] */
47         "popl %%ebpnt"
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 }

 

脚本宝典总结

以上是脚本宝典为你收集整理的内核自定义启动代码01全部内容,希望文章能够帮你解决内核自定义启动代码01所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。