在实验楼上按步骤
cd LinuxKernel/linux-3.9.4
rm -rf mykernel
patch -p1 < ../mykernel_for_linux3.9.4sc.patch
make allnoconfig
make #编译内核请耐心等待
qemu -kernel arch/x86/boot/bzImage
可获得一个不断打印my_start_kernel here %d的运行结果。
进入到mykernel下新建文件mypcb.h定义进程结构或者点mykernel下载
代码及分析
首先,新建一个头文件mypcb.h定义进程的基本信息
#define MAX_TASK_NUM 10 // max num of task in system
#define KERNEL_STACK_SIZE 1024*8
/* CPU-specific state of this stask */
struct Thread {
unsigned long ip;//指向CPU的运行地址
unsigned long sp;//指向当前thread的堆栈栈顶
};
//PCB Struct 进程控制块结构
typedef struct PCB{
int pid; //进程id
volatile long state; //进程状态
char stack[KERNEL_STACK_SIZE];// 每个进程分配的堆栈
/* CPU-specific state of this task */
struct Thread thread;
unsigned long task_entry;//the task execute entry memory address
struct PCB *next;//指向下一个进程
}tPCB;
void my_schedule(void);
修改之前的mymain.c
#include <linux/types.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/stackprotector.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/initrd.h>
#include <linux/bootmem.h>
#include <linux/acpi.h>
#include <linux/tty.h>
#include <linux/percpu.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/kernel_stat.h>
#include <linux/start_kernel.h>
#include <linux/security.h>
#include <linux/smp.h>
#include <linux/profile.h>
#include <linux/rcupdate.h>
#include <linux/moduleparam.h>
#include <linux/kallsyms.h>
#include <linux/writeback.h>
#include <linux/cpu.h>
#include <linux/cpuset.h>
#include <linux/cgroup.h>
#include <linux/efi.h>
#include <linux/tick.h>
#include <linux/interrupt.h>
#include <linux/taskstats_kern.h>
#include <linux/delayacct.h>
#include <linux/unistd.h>
#include <linux/rmap.h>
#include <linux/mempolicy.h>
#include <linux/key.h>
#include <linux/buffer_head.h>
#include <linux/page_cgroup.h>
#include <linux/debug_locks.h>
#include <linux/debugobjects.h>
#include <linux/lockdep.h>
#include <linux/kmemleak.h>
#include <linux/pid_namespace.h>
#include <linux/device.h>
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/idr.h>
#include <linux/kgdb.h>
#include <linux/ftrace.h>
#include <linux/async.h>
#include <linux/kmemcheck.h>
#include <linux/sfi.h>
#include <linux/shmem_fs.h>
#include <linux/slab.h>
#include <linux/perf_event.h>
#include <linux/file.h>
#include <linux/ptrace.h>
#include <linux/blkdev.h>
#include <linux/elevator.h>
#include <asm/io.h>
#include <asm/bugs.h>
#include <asm/setup.h>
#include <asm/sections.h>
#include <asm/cacheflush.h>
#include <linux/vmalloc.h>
#include <linux/module.h>
#include <linux/kernel.h>
#ifdef CONFIG_X86_LOCAL_APIC
#include <asm/smp.h>
#endif
#include "mypcb.h"
tPCB task[MAX_TASK_NUM];
tPCB * my_current_task = NULL;
int my_need_sched = 0;
void my_process(void);
void __init my_start_kernel(void)
{
int pid = 0;
/* Initialize process 0 */
task[pid].pid = pid;
task[pid].state = 0;
//set task 0 execute entry to my_process
task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid].next = &task[pid];
/* fork more process */
for(pid=1;pid<MAX_TASK_NUM;pid++)
{
memcpy(&task[pid],&task[0],sizeof(tPCB));
task[pid].pid = pid;
task[pid].state = -1;
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid-1].next =&task[pid];
}
task[MAX_TASK_NUM-1].next = &task[0];
printk(KERN_NOTICE "\n\n\n\n\n\n system begin :>>> process 0 runing!!1 <<\n\n");
/* start process 0 by task[0] */
pid = 0;
my_current_task =&task[pid];
asm volatile(
"movl %1,%%esp\n\t" /* set task[pid].thread.sp to esp */
"pushl %1\n\t" /*push ebp*/
"pushl %0\n\t" /*push task[pid].thread.ip*/
"ret\n\t"/*pop task[pid].thread.ip to eip*/
"popl %%ebp\n\t"
:
:"c" (task[pid].thread.ip),"d" (task[pid].thread.sp)
);
}
void my_process(void)
{
int i = 0;
while(1){
i++;
if(i%100000000 ==0)
{
printk(KERN_NOTICE "process %d ++\n",my_current_task->pid);
if(my_need_sched == 1){
my_need_sched =0;
my_schedule();
}
printk(KERN_NOTICE "process %d --\n",my_current_task->pid);
}
}
}
修改myinterrupt.c
#include <linux/kernel_stat.h>
#include <linux/export.h>
#include <linux/interrupt.h>
#include <linux/percpu.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/swap.h>
#include <linux/pid_namespace.h>
#include <linux/notifier.h>
#include <linux/thread_info.h>
#include <linux/time.h>
#include <linux/jiffies.h>
#include <linux/posix-timers.h>
#include <linux/cpu.h>
#include <linux/syscalls.h>
#include <linux/delay.h>
#include <linux/tick.h>
#include <linux/kallsyms.h>
#include <linux/irq_work.h>
#include <linux/sched.h>
#include <linux/sched/sysctl.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>
#include <asm/div64.h>
#include <asm/timex.h>
#include <asm/io.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/tty.h>
#include <linux/vmalloc.h>
#include "mypcb.h"
#define CREATE_TRACE_POINTS
#include <trace/events/timer.h>
extern tPCB task[MAX_TASK_NUM];
extern tPCB * my_current_task;
extern volatile int my_need_sched;
volatile int time_count =0;
/*
* Called by timer interrupt.
* it runs in the name of current running process
* so it use kernel stack of current running process
*/
void my_timer_handler(void)
{
#if 1
//make sure need schedule after system circle 2000 times.
if(time_count%2000 == 0 && my_need_sched !=1)
{
my_need_sched = 1;
}
time_count++;
#endif
return;
}
void my_schedule(void)
{
tPCB * next;
tPCB * prev;
// if there no task running or only a task ,it shouldn't need schedule
if(my_current_task == NULL || my_current_task->next ==NULL)
{
printk(KERN_NOTICE " time out!!!but no more than 2 task,need no schedule\n");
return;
}
/* schedule */
next = my_current_task->next;
prev = my_current_task;
printk(KERN_NOTICE " the next task is %d\n",next->pid);
if(next->state == 0)
{
asm volatile(
"pushl %%ebp\n\t" /* save ebp */
"movl %%esp,%0\n\t" /* save esp */
"movl %2,%%esp\n\t" /* restore esp */
"movl $1f,%1\n\t" /* save eip */
"pushl %3\n\t"
"ret\n\t" /* restore eip */
"1:\t" /* next process start here */
"popl %%ebp\n\t"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
my_current_task = next;
printk(KERN_NOTICE " switch from %d process to %d process\n >>>process %d running!!!<<<\n\n",prev->pid,next->pid,next->pid);
}
else{
next->state = 0;
my_current_task = next;
printk(KERN_NOTICE " switch from %d process to %d process\n >>>process %d running!!!<<<\n\n\n",prev->pid,next->pid,next->pid);
/* switch to new process */
asm volatile(
"pushl %%ebp\n\t" /* save ebp */
"movl %%esp,%0\n\t" /* save esp */
"movl %2,%%esp\n\t" /* restore esp */
"movl %2,%%ebp\n\t" /* restore ebp */
"movl $1f,%1\n\t" /* save eip */
"pushl %3\n\t"
"ret\n\t" /* restore eip */
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
程序从mymain.c的my_start_kernel开始运行
//初始化0号进程,设置0号程序的入口为my_process的地址,并设置0号进程控制的下一个进程指向自己
int pid = 0;
task[pid].pid = pid;
task[pid].state = 0;
task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid].next = &task[pid];
//通过复制0号进程,来初始化其它进程。
for(pid=1;pid<MAX_TASK_NUM;pid++)
{
memcpy(&task[pid],&task[0],sizeof(tPCB));
task[pid].pid = pid;
task[pid].state = -1;
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid-1].next = &task[pid];
}
task[MAX_TASK_NUM-1].next = &task[0];
//至此初始化完成,进程的信息构成一个环链表。
//启动0号进程,将0号进程信息所在的地址赋给my_current_task
pid = 0;
my_current_task =&task[pid];
asm volatile(
"movl %1,%%esp\n\t" //把0号进程的thread.sp赋给cpu esp
"pushl %1\n\t" //将0号的堆栈栈顶压栈(此时为空栈ebp=esp)
"pushl %0\n\t" //将0号进程的入口压栈
"ret\n\t"//将0号进程的入口出栈到eip
"popl %%ebp\n\t"//将0号进程的栈顶出栈到ebp
:
:"c" (task[pid].thread.ip),"d" (task[pid].thread.sp)
);
//这是所有进程运行的程序
void my_process(void)
{
int i = 0;
while(1){
i++;
if(i%100000000 ==0)
{
printk(KERN_NOTICE "process %d\n++",my_current_task->pid)
if(my_need_sched == 1){
my_need_sched =0;
my_schedule();
}
printk(KERN_NOTICE "process %d\n--",my_current_task->pid)
}
}
}
tPCB * next;//定义两个tPCB结构体,一个指向当前运行的进程,一个指向调度的下一进程。
tPCB * prev;
//进行调度,下一个进程处于运行中
if(next->state == 0)
{
asm volatile(
"pushl %%ebp\n\t" //将当前进程的ebp压栈
"movl %%esp,%0\n\t" //保存当前进程的esp到当前进程信息的thread.sp
"movl %2,%%esp\n\t" //将要切换的进程的esp恢复
"movl $1f,%1\n\t" //保存当前eip
"pushl %3\n\t"//将要切换的进程的eip压栈
"ret\n\t" //将要切换的进程eip出栈给eip
"1:\t" /* next process start here */
"popl %%ebp\n\t"//将要切换的进程的ebp出栈给ebp
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
//如果要切换的下一个进程没有运行
next->state = 0;//将进程置为启动
my_current_task = next;
printk(KERN_NOTICE " switch from %d process to %d process\n >>>process %d running!!!<<<\n\n\n",prev->pid,next->pid,next->pid);
/* switch to new process */
asm volatile(
"pushl %%ebp\n\t" //将当前进程的ebp压栈
"movl %%esp,%0\n\t" //保存当前进程的esp到当前进程信息的thread.sp
"movl %2,%%esp\n\t" //新建进程栈为空栈。esp=ebp
"movl %2,%%ebp\n\t" //将新建进程的栈顶和栈底赋给esp和ebp
"movl $1f,%1\n\t" //将1f赋给当前进程的thread.ip
"pushl %3\n\t" //将新建进程的入口压栈
"ret\n\t" //将要切换的进程入口出栈给eip
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
总结
对于上面的代码,进程间的调度没有优先级,只是按顺序循环运行。而调度的代码有疑问的地方是$1f,在下一个进程已运行的状态下,很明显是为了恢复ebp的操作,
而下一个进程没有运行时,没有1标号的位置,不知道这个语句的所指向的位置。