基于mykernel完成多进程的简单内核
原创作品转载请注明出处https://github.com/mengning/linuxkernel/
一、实验环境
虚拟机为VMware Workstation,系统为Ubuntu 18.04
部署平台
sudo apt-get install qemu # install QEMU
sudo ln -s /usr/bin/qemu-system-i386 /usr/bin/qemu
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.4.tar.xz # download Linux Kernel 3.9.4 source code
wget https://raw.github.com/mengning/mykernel/master/mykernel_for_linux3.9.4sc.patch # download mykernel_for_linux3.9.4sc.patch
xz -d linux-3.9.4.tar.xz
tar -xvf linux-3.9.4.tar
cd linux-3.9.4
patch -p1 < ../mykernel_for_linux3.9.4sc.patch
make allnoconfig
make
qemu -kernel arch/x86/boot/bzImage
一切顺利地话,可以看到如下的QEMU窗口:
可能的问题
系统自带的gcc版本较高,导致不兼容原来的代码。
解决方法:
# 安装低版本的gcc,并将系统默认编译器设为低版本
sudo apt-get install gcc-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 100
二、实验步骤
- 获取实验的源代码,分别为mypcb.h,myinterrupt.c和mymain.c
- 将以上文件拷贝至linux-3.9.4/mykernel文件下。
- 重新编译内核文件
- 再启动QEMU窗口,即可看到如下界面:
三、源码分析
mypcb.h
#define MAX_TASK_NUM 4 // 支持最多的进程数
#define KERNEL_STACK_SIZE 1024*2 // 内核堆栈的大小
/* CPU-specific state of this task */
struct Thread {
unsigned long ip; // eip指针
unsigned long sp; // esp指针
};
typedef struct PCB{
int pid; // 进程号
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
unsigned long stack[KERNEL_STACK_SIZE]; // 进程堆栈
/* CPU-specific state of this task */
struct Thread thread; // 用于记录指针状态
unsigned long task_entry; // 入口事件
struct PCB *next; // 指向下一个pcb块
}tPCB;
void my_schedule(void);
myinterrupt.c
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/tty.h>
#include <linux/vmalloc.h>
#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; // 计数器
/*
* 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
// 通过计数对my_need_sched进行设置,当time_count计数器达到1000并且全局变量my_need_sched不为1时,将其设置为1
if(time_count%1000 == 0 && my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
my_need_sched = 1;
}
time_count ++ ;
#endif
return;
}
// 调度实现
void my_schedule(void)
{
tPCB * next;
tPCB * prev;
// 若当前任务为空或者下一个任务为空直接返回
if(my_current_task == NULL || my_current_task->next == NULL)
{
return;
}
printk(KERN_NOTICE ">>>my_schedule<<<\n");
/* schedule */
next = my_current_task->next;
prev = my_current_task;
if(next->state == 0)/* -1 unrunnable, 0 runnable, >0 stopped */
{
// 切换当前任务
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->pid,next->pid);
/* switch to next process */
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)
);
}
return;
}
$1f 是一种前向引用,其中1代表标签1,f代表forward。这里指令movl $1f, %1\n\t
是执行标签1之前的一句即ret指令,将ip信息弹出,并保存在prev的ip信息里。
mymain.c
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/tty.h>
#include <linux/vmalloc.h>
#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;
/* Initialize process 0*/
task[pid].pid = pid;
task[pid].state = 0;/* -1 unrunnable, 0 runnable, >0 stopped */
task[pid].task_entry = task[pid].thread.ip = (unsigned long)my_process;
// 模拟堆栈,使sp指向栈顶位置
task[pid].thread.sp = (unsigned long)&task[pid].stack[KERNEL_STACK_SIZE-1];
task[pid].next = &task[pid];
/*fork more process */
for(i=1;i<MAX_TASK_NUM;i++)
{
// 复制0号进程的内容到task[i],并设置相应信息
memcpy(&task[i],&task[0],sizeof(tPCB));
task[i].pid = i;
//*(&task[i].stack[KERNEL_STACK_SIZE-1] - 1) = (unsigned long)&task[i].stack[KERNEL_STACK_SIZE-1];
//重设栈指针
task[i].thread.sp = (unsigned long)(&task[i].stack[KERNEL_STACK_SIZE-1]);
task[i].next = task[i-1].next;
task[i-1].next = &task[i];
}
/* start process 0 by task[0] */
pid = 0;
my_current_task = &task[pid];
// 根据pcb中的sp值,设置当前task的栈顶指针
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) /* input c or d mean %ecx/%edx*/
);
}
int i = 0;
void my_process(void)
{
// 由于之前设置了0号进程的entry是my_process,从0号进程开始
while(1)
{
i++;
//每循环10000000次,打印信息
if(i%10000000 == 0)
{
printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid);
// 当my_need_sched == 1时进程调度
if(my_need_sched == 1)
{
my_need_sched = 0;
my_schedule();
}
printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
}
}
}
mymain.c文件首先对进程进行初始化,并从0号进程开始调度。通过my_timer_handle函数实现对于my_process函数的控制,进行中断与控制,防止死循环。
四、实验总结
本次实验实现了Linux进程简单时间片的调度机制。通过对于mykernel源码的分析,可以了解到进程调度过程中发生中断的原因与过程,以及如何通过%esp,%ebp,%eip的指针变化将中断现场进行保护与恢复。最后进一步加深了对存储程序计算机,堆栈和中断的理解。