目录
通过实验楼配置mykernel环境
初始运行结果
添加mypcb.h
修改mymain.c与汇编语言分析
修改myinterrupt.c与汇编语言分析
修改代码后运行结果
总结
通过实验楼配置mykernel环境
# 注意路径是区分大小的
$ 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
初始运行结果
添加mypcb.h
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2
struct Thread {
unsigned long ip;
unsigned long sp;
};
typedef struct PCB{
int pid;
volatile long state;
unsigned long stack[KERNEL_STACK_SIZE];
struct Thread thread;
unsigned long task_entry;
struct PCB *next;
}tPCB;
void my_schedule(void);
修改mymain.c与汇编语言分析
#include
#include
#include
#include
#include
#include "mypcb.h"
tPCB task[MAX_TASK_NUM];
tPCB * my_current_task = NULL;
volatile int my_need_sched = 0;
void my_process(void);
void __init my_start_kernel(void)
{
int pid = 0;
int i;
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];
for(i=1;ipid);
if(my_need_sched == 1)
{
my_need_sched = 0;
my_schedule();
}
printk(KERN_NOTICE "this is process %d +n",my_current_task->pid);
}
}
}
修改myinterrupt.c与汇编语言分析
#include
#include
#include
#include
#include
#include "mypcb.h"
extern tPCB task[MAX_TASK_NUM];
extern tPCB * my_current_task;
extern volatile int my_need_sched;
volatile int time_count = 0;
void my_timer_handler(void)
{
if(time_count%1000 == 0 && my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handler here<<next == NULL)
{
return;
}
printk(KERN_NOTICE ">>>my_schedule<<next;
prev = my_current_task;
if(next->state == 0)
{
asm volatile(
"pushl %%ebpnt"
"movl %%esp,%0nt"
"movl %2,%%espnt"
"movl $1f,%1nt"
"pushl %3nt"
"retnt"
"1:t"
"popl %%ebpnt"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<pid,next->pid);
}
else
{
next->state = 0;
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<pid,next->pid);
asm volatile(
"pushl %%ebpnt"
"movl %%esp,%0nt"
"movl %2,%%espnt"
"movl %2,%%ebpnt"
"movl $1f,%1nt"
"pushl %3nt"
"retnt"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2
struct Thread {
unsigned long ip;
unsigned long sp;
};
typedef struct PCB{
int pid;
volatile long state;
unsigned long stack[KERNEL_STACK_SIZE];
struct Thread thread;
unsigned long task_entry;
struct PCB *next;
}tPCB;
void my_schedule(void);
修改mymain.c与汇编语言分析
#include
#include
#include
#include
#include
#include "mypcb.h"
tPCB task[MAX_TASK_NUM];
tPCB * my_current_task = NULL;
volatile int my_need_sched = 0;
void my_process(void);
void __init my_start_kernel(void)
{
int pid = 0;
int i;
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];
for(i=1;ipid);
if(my_need_sched == 1)
{
my_need_sched = 0;
my_schedule();
}
printk(KERN_NOTICE "this is process %d +n",my_current_task->pid);
}
}
}
修改myinterrupt.c与汇编语言分析
#include
#include
#include
#include
#include
#include "mypcb.h"
extern tPCB task[MAX_TASK_NUM];
extern tPCB * my_current_task;
extern volatile int my_need_sched;
volatile int time_count = 0;
void my_timer_handler(void)
{
if(time_count%1000 == 0 && my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handler here<<next == NULL)
{
return;
}
printk(KERN_NOTICE ">>>my_schedule<<next;
prev = my_current_task;
if(next->state == 0)
{
asm volatile(
"pushl %%ebpnt"
"movl %%esp,%0nt"
"movl %2,%%espnt"
"movl $1f,%1nt"
"pushl %3nt"
"retnt"
"1:t"
"popl %%ebpnt"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<pid,next->pid);
}
else
{
next->state = 0;
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<pid,next->pid);
asm volatile(
"pushl %%ebpnt"
"movl %%esp,%0nt"
"movl %2,%%espnt"
"movl %2,%%ebpnt"
"movl $1f,%1nt"
"pushl %3nt"
"retnt"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
#include#include #include #include #include #include "mypcb.h" extern tPCB task[MAX_TASK_NUM]; extern tPCB * my_current_task; extern volatile int my_need_sched; volatile int time_count = 0; void my_timer_handler(void) { if(time_count%1000 == 0 && my_need_sched != 1) { printk(KERN_NOTICE ">>>my_timer_handler here<< next == NULL) { return; } printk(KERN_NOTICE ">>>my_schedule<< next; prev = my_current_task; if(next->state == 0) { asm volatile( "pushl %%ebpnt" "movl %%esp,%0nt" "movl %2,%%espnt" "movl $1f,%1nt" "pushl %3nt" "retnt" "1:t" "popl %%ebpnt" : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<< pid,next->pid); } else { next->state = 0; my_current_task = next; printk(KERN_NOTICE ">>>switch %d to %d<< pid,next->pid); asm volatile( "pushl %%ebpnt" "movl %%esp,%0nt" "movl %2,%%espnt" "movl %2,%%ebpnt" "movl $1f,%1nt" "pushl %3nt" "retnt" : "=m" (prev->thread.sp),"=m" (prev->thread.ip) : "m" (next->thread.sp),"m" (next->thread.ip) ); } return; }
修改代码后运行结果
总结
操作系统的三大法宝是存储程序结构、函数调用、中断,本实验中点探究了操作系统实现终端的过程。



