函数调用堆栈
堆栈并不是一开始就有的,计算机没有高级语言的时候,只有机器语言时候,因为汇编可以跳转,没有太多函数的概念,有了高级语言,有了函数,就要借助堆栈了
- 堆栈是c语言程序运行时必须的一个记录调用路径和参数的空间
- 函数调用框架
- 传递参数
- 保存返回地址
- 提供局部变量
- 等等
- 堆栈相关的寄存器
- esp 堆栈指针 (stack pointer)
- ebp 基址指针 (base pointer)
- 堆栈操作
- push 栈顶地址减少4个字节
- pop 栈顶地址增加4个字节
- ebp在c语言中用作记录当前函数调用基址
- 其他关键寄存器
- cs:eip:总是指向下一条的指令地址
- 顺序执行:总是指向地址连续的下一条指令
- 跳转/分支:执行这样的指令的时候,cs:eip的值会根据程序被修改
- call:将当前的cs:eip的值压入栈顶,cs:eip指向被调用函数的入口地址
- ret:从栈顶弹出原来的保存在这里的cd:eip的值,放入cs:eip中
- cs:eip:总是指向下一条的指令地址
操作系统很重要的两个要素
- 中断上下文(保存现场和恢复现场)
- 进程上下文的切换
32位x86堆栈框架
调用者
call x
call指令:- 将eip中下一跳指令的地址保存在在栈顶
- 设置eip指向被调用程序代码开始处
#下面两句是建立被调用者函数的堆栈框架
pushl %ebp
movl %esp, %ebp
-----------
solve
---------
#拆除被调用者函数的框架
movl %ebp, %esp
pop; %ebp
ret
#define MAX_TASK_NUM 4
#define KERNEL_STACK_SIZE 1024*8
/* CPU-specific state of this task */
struct Thread {
unsigned long ip; // eip
unsigned long sp; //esp
};
typedef struct PCB{
int pid;//进程的id
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; //入口,类似于main函数
struct PCB *next; //用链表将进程连起来
}tPCB;
void my_schedule(void); //调度器
#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,初始化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; //入口是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,当前栈是空的,要使ebp等于esp*/
"pushl %0\n\t" /* push task[pid].thread.ip */
"ret\n\t" /* pop task[pid].thread.ip to eip,ret之后0号进程正式启动了 */
"popl %%ebp\n\t"
:
: "c" (task[pid].thread.ip),"d" (task[pid].thread.sp) /* input c or d mean %ecx/%edx*/
);
/*内核初始化完成了,启动了0号进程*/
}
void my_process(void)
{
int i = 0;
while(1)
{
i++;
if(i%10000000 == 0) //循环1000万次才有一次机会判断一下是否需要调度
{
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);
}
}
}
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 ,$1f是指接下来的标号1:的位置*/
"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;
}
总结
操作系统是如何工作的? 操作系统是连接软硬件的桥梁,一方面它管理硬件资源(配置内存,控制输入输出设备,操作网络与文件系统),最大限度地发挥计算机资源;另一方面,他负责进程调度和作业管理,为软件运行提供库支持,屏蔽硬件的不同。通过中断对外界做出反映,通过进程切换使CPU时间合理分配,保证计算机的性能发挥。