20135313 Wu Ziyi. Beijing Institute of Electronic Technology
Chapter 1 Knowledge points carding
(a) How does the computer work? (summary)--Three magic weapon
① stored program Computer working model, the most basic logical structure of computer system;
② function Call stack, high-level language to run the foundation, only the machine language and assembly languages when the stack mechanism for the computer is not so important, but with high-level language and functions, the stack becomes the basic function of the computer;
Enter PUSHL%ebpmovl%esp,%ebpleave movl%ebp,%esppopl%EBP
function parameter passing mechanism and local variable storage
③ interrupt, multi-channel program operating system base point, no interrupt mechanism program can only run from the beginning to the end of the possibility to start running other programs.
(ii) function call stack
Stack
1. The stack is a space that the C language program must run with a record call path and parameters.
2. The purpose of the stack exists: function call frame, pass parameter, save return address, provide local variable space, etc.
The 3.C language compiler has a set of rules for using stacks.
4. Understanding the purpose of the stack and the rules used by the compiler on the stack are fundamental to understanding some of the key code of the operating system.
Stack registers and stack operations
Pop: from high address to low address push: from low address to high address
1. Stack-related registers: ESP, stack pointer (stacks pointer): EBP, base address pointer (base pointer)
2. Stack operation: Push stack top address reduced by 4 bytes (32 bit) pop stack top address increased by 4 bytes
3.EBP record the current function call base in C language
Other key registers
CS:EIP: Always point to the next instruction address
Sequential execution: Always point to the next instruction of the address continuous jump/branch: When executing such an instruction, the value of CS:EIP will be modified according to the program call: The value of the current CS:EIP is pressed into the top of the stack, Cs:eip points to the The entry address of the calling function Ret: pops up from the top of the stack the value of the CS:EIP that was originally saved here, when the interrupt occurred in the CS:EIP
(iii) Simulation of the storage program computer working model and clock interruption with the help of the Linux kernel part source code
When an interrupt signal occurs, the CPU presses the current EIP,ESP,EBP into the kernel stack and points the EIP to the entry of the interrupt handler.
Embed assembly code in C code
(iv) Constructing a simple operating system Kernel Chapter 2 experiment on the basis of Mykernel
1. Experiment Step 1:
Each time the My_ start_ kernel function is executed two or once, the My_ time_ hander function executes once.
2. Experimental steps 2:MYMAIN.C
3. Experimental steps 3:myinterrupt.c
4. Program Analysis
Mypcb.h
/* * linux/mykernel/mypcb.h * * Kernel Internal PCB Types * * Copyright (C) 2013 mengning * */ #define MAX_TASK_NUM 4 #define Kernel_stack_size 1024*8/ * Cpu-specific State of the This task */ struct Thread { unsigned long IP; Save Eip unsigned long sp; Save ESP }; typedef struct pcb{ int pid; volatile long state; /* Record process status,-1 not running, 0 running, >0 blocking stop */ char stack[kernel_stack_size]; /* Define the stack structure */ struct thread thread; unsigned long task_entry; /* Define the program entry, usually the main function */ struct PCB *next; } TPCB; void My_schedule (void);//Scheduler function
Mymain.c
/* * LINUX/MYKERNEL/MYMAIN.C * * Kernel Internal My_start_kernel * * Copyright (c) mengning * */#include <linux/types.h> #include <linux/string.h> #include <linux/ctype.h> #in Clude <linux/tty.h> #include <linux/vmalloc.h> #include "mypcb.h" TPCB Task[max_task_num]; Declares a PCB array TPCB * my_current_task = NULL; Declares the current task pointer, volatile int my_need_sched = 0; Whether a dispatch flag is required void my_process (void); void __init My_start_kernel (void) {int pid = 0; int i; /* Initialize Process # No. 0 */task[pid].pid = PID; Task[pid].state = 0;/*-1 unrunnable, 0 runnable, >0 stopped */task[pid].task_entry = Task[pid].thread.ip = (UN Signed long) my_process; /* is actually my_process*/task[pid].thread.sp = (unsigned long) &task[pid].stack[KERNEL_STACK_SIZE-1]; Task[pid].next = &task[pid]; Define stack Top//Create more sub-processes */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 from process No. 0 */pid = 0; My_current_task = &task[pid]; ASM volatile ("MOVL%1,%%esp\n\t"/* Sets the value of ESP */"PUSHL%1\n\t"/* converts EBP to stack (at this time ESP=EBP), %1 is equivalent to task[pid].thread.sp*/"PUSHL%0\n\t"/* will stack EIP,%0 equals task[pid].thread.ip*/"ret\n\t" /* Equivalent to the EIP out of the stack */"POPL%%ebp\n\t"/* # NO. 0 Process is started * *:: "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
/* * LINUX/MYKERNEL/MYINTERRUPT.C * * Kernel Internal My_timer_handler * * Copyright (c) Men gning * */#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 the current running process, * so it is kernel stack of current running process */void M Y_timer_handler (void) {#if 1 if (time_count%1000 = = 0 && my_need_sched! = 1) {Prin TK (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 */* process switch jump to next process */ASM Volatile ("PUSHL%%ebp\n\t"/* Save current EBP */"MOVL%%esp,%0\n\t"/* Save current ESP */ "Movl%2,%%esp\n\t"/* re-record the esp,%2 to jump process to next->thread.sp*/"MOVL $1f,%1\n\t"/* Save current EIP,%1 prev->thread.ip*/"PUSHL%3\n\t" "ret\n\t"/* record ei for jump process p,%3 for next->thread.ip*/"1:\t"///Next process Start execution */"POPL%%ebp\n\t" : "=m" (PREV->THREAD.SP), "=m" (PREV->THREAD.IP): "M" (NEXT->THREAD.SP), "M "(Next->thread.ip)); My_current_task = Next; PRINTK (kern_notice ">>>switch%d to%d<<<\n", prev->pid,next->pid); } 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 current EBP */ "Movl%%esp,%0\n\t"/* Save current ESP */"MOVL%2,%%esp\n\t"/* re-record ESP to jump process,%2 to NEXT->THREAD.SP */"MOVL%2,%%ebp\n\t"/* re-record the ebp,%2 to jump process to NEXT->THREAD.SP */"MOVL $1f,%1\n\t" /* Save current EIP,%1 is prev->thread.ip,%1f refers to label 1: The code is stored in memory address */"PUSHL%3\n\t" "ret\n\t /* Re-record the eip,%3 to jump process to NEXT->THREAD.IP */: "=m" (PREV->THREAD.SP), "=m" (prev->tHREAD.IP): "M" (NEXT->THREAD.SP), "M" (Next->thread.ip)); } return; }
Chapter 3 summarizes the understanding of how the operating system works.
The basis of operating system work: Stored program computer, stack mechanism, interrupt mechanism. The operating system is the program to manage computer hardware and software resources, but also the core and cornerstone of the computer system. Operating system is to manage all the hardware resources of computer system including software resources and data resources, control program operation, improve human-machine interface, and support other application software, so as to maximize the function of all resources of computer system and provide users with convenient, effective and friendly service interface. The operating system is a large management control program, which roughly includes 5 management functions: Process and processor management, job management, storage management, device management, file management. The Linux system has a process table, and a process is one of them. Each item in the Process Control table is a TASK_STRUCT structure that stores a variety of low-level and advanced information in the TASK_STRUCT structure, including links from the registers of some hardware devices to the working directory of the process. The Process Control table is both an array, a doubly linked list, and a tree, and its physical implementation is a static array that includes multiple pointers. Once the system is started, the kernel is typically represented as a process. A global pointer variable, pointing to Task_struct, is used to record a running process.
Chapter 4 Appendix
Wu Ziyi
Study No.: 20135313
Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
Linux Kernel Analysis-second week learning notes