一.实验要求
按照https://github.com/mengning/mykernel 的说明配置mykernel 2.0,熟悉Linux内核的编译;
基于mykernel 2.0编写一个操作系统内核,参照https://github.com/mengning/mykernel 提供的范例代码
简要分析操作系统内核核心功能及运行工作机制。
二.实验环境配置
实验环境:采用的是虚拟机上的ubuntu16.04。
依次在终端输入以下代码:
wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch (此步安装失败,故从群里直接下载)
sudo apt install axel
axel -n 20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz
xz -d linux-5.4.34.tar.xz
tar -xvf linux-5.4.34.tar
cd linux-5.4.34
打补丁
patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch
安装相关库
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
编译
make defconfig
make -j
sudo apt install qemu-system-x86_64
运行内核
qemu-system-x86_64 -kernel arch/x86/boot/bzImage
运行结果:
从qemu窗口中看到my_start_kernel在执行,同时my_timer_handler时钟中断处理程序周期性执行。
三.编写内核
学习得知mypcb.h、mymain.c和myinterrupt.c共同实现了一个简单的时间片轮转调度进程的简单内核。
本次任务即为在linux-5.4.34/mykernel文件夹下新增mypcb.h文件,修改linux-5.4.34/mykernel文件夹下的mymain.c文件,修改linux-5.4.34/mykernel文件夹下的myinterrypt.c文件。
1.创建mypcb.h头文件,该头文件用于定义pcb块,包括进程id、进程状态、栈和进程入口以及Thread结构等。
// mypcb.h
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*2
/* CPU-specific state of this task */
struct Thread {
unsigned long ip;
unsigned long sp;
};
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;
}tPCB;
void my_schedule(void);
2.修改mymain.c文件,重写my_start_kernel(void)方法。
系统从my_start_kernel处最先执行,对进程列表task进行初始化,并将函数my_process()设置为进程的入口地址,将其堆栈指针指向栈底,同时指向下一个进程的指针设置为指向自己。栈顶指针为数组的最高地址,栈向下增长。进入循环后,继续创建初始化其他进程并通过链表连接起来。启动进程0,让rsp寄存器指向进程0的栈顶地址,push rbp,由于程序员无法直接操作rip寄存器所以先将ip入栈,再将ip出栈到rip寄存器,跳转到程序入口运行进程。最后执行my_process()函数,此方法块中,当前进程用完一个时间片后,让出CPU,然后通过判断 my_need_sched是否为1,决定是否切换进程。
//修改后的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;
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++)
{
memcpy(&task[i],&task[0],sizeof(tPCB));
task[i].pid = i;
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];
asm volatile(
"movq %1,%%rsp\n\t" /* set task[pid].thread.sp to rsp */
"pushq %1\n\t" /* push rbp */
"pushq %0\n\t" /* push task[pid].thread.ip */
"ret\n\t" /* pop task[pid].thread.ip to rip */
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
}
int i = 0;
void my_process(void)
{
while(1)
{
i++;
if(i%10000000 == 0)
{
printk(KERN_NOTICE "this is process %d -\n",my_current_task->pid);
if(my_need_sched == 1)
{
my_need_sched = 0;
my_schedule();
}
printk(KERN_NOTICE "this is process %d +\n",my_current_task->pid);
}
}
}
3.修改myinterrypt.c
my_shedule函数实现了进程的切换:
首先将rbp寄存器的值压入栈中,保存正在运行的进程的栈底地址;将rsp值保存在当前进程的sp字段,保存正在运行的进程的栈顶。将下一个进程的sp字段放入rsp中,保存正在运行的进程的ip指针。将寄存器中的ip修改为目的切换进程的ip指针。
//修改后的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(time_count%1000 == 0 && my_need_sched != 1)
{
printk(KERN_NOTICE ">>>my_timer_handler here<<<\n");
my_need_sched = 1;
}
time_count ++ ;
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(
"pushq %%rbp\n\t" /* save rbp of prev */
"movq %%rsp,%0\n\t" /* save rsp of prev */
"movq %2,%%rsp\n\t" /* restore rsp of next */
"movq $1f,%1\n\t" /* save rip of prev */
"pushq %3\n\t"
"ret\n\t" /* restore rip of next */
"1:\t" /* next process start here */
"popq %%rbp\n\t"
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
return;
}
4.实验结果
重新编译后,再次运行程序,结果如下:
四.总结
通过本次实验,对Linux内核中进程调度和进程切换的知识有了更深入的了解。
原文:https://www.cnblogs.com/Teresa-Chenchen/p/12882245.html