2019-2020-1 20199301 "Linux kernel principle and Analysis" in the third week of work

How the operating system works

The goal of this chapter is to write a simple kernel on the basis of mykernel

First, the study notes

1. The three magic computer: a stored program computer; B function call stack; C interrupt.

  • Stored program computer (computer logic framework for all fundamental);
  • Function call stack (C language runtime function calls must use the path and the recording parameter storage space);
    • Stack specific role:
      • Record function call frame
      • Pass arguments
      • Save the address of the return value
      • Provide storage of local variables inside a function
    • Stack registers related to:
      • ESP: Stack Pointer
      • EBP: base pointer
    • Stack operations:
      • push: 4 bytes reduce stack address, and the operand stack into a storage unit
      • pop: 4 bytes stack address is incremented, and the top of stack operand storage unit into
    • Other key registers:
      - CS: EIP : always points to the next instruction address continuously
      - jump / branch: this command is executed, CS: EIP values may need to be modified according to the procedure
      - Call : the current CS: EIP of value onto the stack, CS: EIP points to the entry address of the called function
      - RET : pop from the stack previously saved here CS: EIP values, into the CS: EIP in

      - the stack is particularly critical in the function call stack frame

Second, the experimental

  • LinuxKernel / linux-3.9.4 / directory under mykernel can see the qemu window
    • Code
    cd LinuxKernel/linux-3.9.4
    qemu -kernel arch/x86/boot/bzImage

    • QEMU window

    • View mymain.c

      per cycle 10,000 times, print a word.

    • View myinterrupt.c

      Each time, it will perform a clock interrupt.

The following is a simple round-robin mechanism kernel code:

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);
- 定义一个进程控制块pcb结构体
- task_entry:进程入口函数
- thread:保存eip和esp
- state:进程状态,用于判断是否调度

mymain.c

The main function is to allow the program to run from the beginning of the process number 0, only listed the following code is the core.

asm volatile(

//%0表示参数thread.ip,%1表示参数thread.sp。

"movl %1,%%esp\n\t"     /* set task[pid].thread.sp to esp 把参数thread.sp放到esp中*/

"pushl %1\n\t"    /* push ebp 由于当前栈是空的,esp与ebp指向相同,所以等价于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*/

); 

myinterrupt.c

The main function is used to clock interrupt handling and process scheduling algorithm.

asm volatile(

"pushl %%ebp\n\t" /* save ebp 保存当前进程的ebp*/

"movl %%esp,%0\n\t" /* save esp 把当前进程的esp赋给%0(指的是thread.sp),即保存当前进程的esp*/

"movl %2,%%esp\n\t" /* restore esp 把%2(指下一个进程的sp)放入esp中*/

"movl $1f,%1\n\t" /* save eip $1f是接下来的标号“1:”的位置,把eip保存下来*/

"pushl %3\n\t" /*把下一个进程eip压栈*/

"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)

);

Third, the summary

(1) process and interrupt the operating system is a very important part two, we need to master.
(2) EIP register storing the current execution of the code, the code can be changed by changing the value of the currently executing EIP register, thereby realizing the process of switching. For security reasons, the value of the EIP register can not be changed directly, but indirectly ret instruction can be changed by push +.
(3) a process during execution, when a time slice expires after the handover process needs to save the current execution context, the next scheduled time, the process needs to respond to the context.

How the operating system works?

Operating system executed by the stored program computer program instructions mechanisms in turn, recorded a call path and parameters to provide space for the stack to run a program, the interrupt for exception handling and process scheduling.

Guess you like

Origin www.cnblogs.com/lsqz/p/11601160.html