操作系统是如何工作的

除了存储程序计算机和函数调用堆栈机制,还有一个非常基础的概念就是中断,这三个关键性的方法机制可以称作计算机的三个法宝:程序存储计算机、函数调用、中断

堆栈的作用:记录函数调用框架、传递函数参数、保存返回值地址、提供函数内部局部便量的存储空间。

堆栈相关的寄存器 ESP:堆栈指针,指向堆栈栈顶 EBP:基址指针,指向堆栈栈底

堆栈操作:push pop

实验过程

依次输入

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可以每100000次输出一次my start kernel here ,周期性地产生时钟中断信号,同时执行myinterrupt.c中的代码 。

然后按实验要求修改代码如下所示

mypcb.h:

#define MAX_TASK_NUM 4

#define KERNEL_STACK_SIZE 1024*8

/* 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 */
char 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:

#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].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(
"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*/
);
}
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:

#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
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)
); }
else
{
next->state = 0;
my_current_task = next;
printk(KERN_NOTICE ">>>switch %d to %d<<<\n",prev->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;
}

完成后的目录为

运行的结果为



在my_process(void)函数中,每10000000次打印“this is process %d”,如果my_need_sched为1时,执行myinerrupt中的my_schedule()触发中断。

最新文章

  1. BZOJ 2086: [Poi2010]Blocks
  2. 饿了么基于Vue2.0的通用组件开发之路(分享会记录)
  3. CAD规划成果入库GIS_SDE转换之分析
  4. RegExp正则校验之Java及R测试
  5. java正则表达式 --简单认识
  6. HTML解析组件HtmlAgilityPack使用
  7. 多余的Using Namespaces或引用会影响程序的执行效率么?
  8. jQuery - AJAX get() 和 post() 方法
  9. Linux GDB Debug
  10. Linux使用本地iso作为yum源
  11. 2016CCPC 中南地区邀请赛 A 矩阵快速幂
  12. java mysql驱动
  13. CI框架中遇见的一些错误和解决方法 笔记
  14. jitpack让使用第三方依赖库更简单
  15. Otacle表查询
  16. 5.15 pymysql 模块
  17. 关于QList&lt;T&gt;的内存释放
  18. 【NOI2015】【BZOJ4196】软件包管理器 - 题解
  19. 2018/7/26号碰到了个奇怪的问题(http有问题,但是ftp没毛病)
  20. windows的网上邻居

热门文章

  1. Java网络编程之InetAddress浅析
  2. 学习笔记之Java队列Queue中offer/add函数,poll/remove函数,peek/element函数的区别
  3. PHP识别简单的图片上面的数字(可扩展)
  4. MSIL实用指南-数学运算
  5. Oracle - 获取当前周别函数
  6. [python]创建文本文件,并读取
  7. CF_EDU51 E. Vasya and Big Integers
  8. 编写一个函数来找出所有不带歧义的函数名,也就是 那些只在一个模块里出现过的函数名(erlang)
  9. redis在windows下安装设置密码及主从数据库
  10. 什么是Json,Json如何使用