实验要求
完成一个简单的时间片轮转多道程序内核代码
实验过程
使用实验楼的虚拟机打开 shell,并执行如下语句。
# 注意路径是区分大小的
$ 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
实验结果如下所示
查看 mymain.c
和 myinterrupt.c的内容
可以看到,在my_start_kernel函数中,不停地循环执行一条输出语句,打印出了在QEMU窗口中看到的>>>my_timer_handler here <<<<<
这是一个被时钟中断状态下调用的函数:my_timer_handler。
完成一个简单的时间片轮转多道程序内核代码
实验结果如下所示
代码分析
mykernel/mypcb.h
首先在mykernel目录下增加一个mypcb.h 头文件,用来定义进程控制块(Process Control Block),也就是进程结构体的定义,在Linux内核中是struct tast_struct结构体。
/*
* linux/mykernel/mypcb.h
*
* Kernel internal PCB types
*
* Copyright (C) 2013 Mengning
*
*/
#define MAX_TASK_NUM 4 /* 最大任务数定义为4 */
#define KERNEL_STACK_SIZE 1024*2 # unsigned long /* 内核堆栈数目定义为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);
mymain.c修改后的文件
对mymain.c进行修改,这是mykernel内核代码的入口,负责初始化内核的各个组成部分。在Linux内核源代码中,实际的内核入口是init/main.c中的start_kernel(void)函数。
/*
* linux/mykernel/mymain.c
*/
#include "mypcb.h"
#include <linux/tty.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].state = -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];
asm volatile(
"movq %1,%%rsp\n\t" /* set task[pid].thread.sp to rsp 将RSP寄存器指向进程0的堆栈栈底*/
"pushq %1\n\t" /* push rbp */
"pushq %0\n\t" /* push task[pid].thread.ip 将当前进程的RIP(这里是初始化的值my_process(void)函数的位置)入栈 */
"ret\n\t" /* pop task[pid].thread.ip to rip 让压栈的进程RIP保存到RIP寄存器中 */
"popq %%rbp\n\t"
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
}
void my_process(void)
{
int i = 0;
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);
}
}
}
对myinterrupt.c修改后的文件
对myinterrupt.c中修改my_timer_handler用来记录时间片.对myinterrupt.c进行修改,主要是增加了进程切换的代码my_schedule(void)函数,在Linux内核源代码中对应的是schedule(void)函数
/*
* linux/mykernel/myinterrupt.c
*/
#include "mypcb.h"
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/tty.h>
#include <linux/vmalloc.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.
*/
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" /* 保存prev进程(本例中指进程0)当前RBP寄存器的值到prev进程的堆栈 */
"movq %%rsp,%0\n\t" /* 保存prev进程(本例中指进程0)当前RSP寄存器的值到prev->thread.sp,这时RSP寄存器指向进程的栈顶地址,实际上就是将prev进程的栈顶地址保存;%0、%1...指这段汇编代码下面输入输出部分的编号 */
"movq %2,%%rsp\n\t" /*将next进程的栈顶地址next->thread.sp放入RSP寄存器,完成了进程0和进程1的堆栈切换 */
"movq $1f,%1\n\t" /*保存prev进程当前RIP寄存器值到prev->thread.ip,这里$1f是指标号1 */
"pushq %3\n\t" /*把即将执行的next进程的指令地址next->thread.ip入栈*/
"ret\n\t" /* 就是将压入栈中的next->thread.ip放入RIP寄存器 */
"1:\t" /* next process start here 标号1是一个特殊的地址位置,该位置的地址是$1f */
"popq %%rbp\n\t" /*将next进程堆栈基地址从堆栈中恢复到RBP寄存器中*/
: "=m" (prev->thread.sp),"=m" (prev->thread.ip)
: "m" (next->thread.sp),"m" (next->thread.ip)
);
}
else
{
next->state = 0;
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"
"movq %2,%%rbp\n\t" /* restore rbp 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;
}
总结
进程是操作系统的核心。在Linux系统中,进程和线程不做特别区分,线程是一种特殊的进程。进程存放在每一项类型为 task_struct(进程描述符,包括进程的地址空间、挂起的信号、状态等)的双向循环链表中,在内核栈的尾部创建 thread_info 结构,通过计算偏移间接查找进程描述符。父进程通过调用fork()复制本进程来创建新进程,exec()读取可执行文件并将其载入地址空间开始执行,最终,程序通过exit()系统调用退出执行。
系统调用在用户空间进程和硬件设备之间添加了一个中间层。程序员只需和API打交道,内核只和系统调用打交道。访问系统调用,通常用C库中定义的函数调用来进行。每个系统调用被赋予了一个独一无二且不能更改的系统调用号,执行系统调用后陷入内核,传递系统调用号和参数,执行系统调用函数,并把返回值带回用户空间。“提供机制而不是策略”,系统调用抽象出用于完成某种目的的函数,至于函数如何使用不需要关心。