Linux Kernel Analysis 24th study Report

Source: Internet
Author: User
Tags volatile

    • Student Bérénice Angremy
    1. Course Content

Three magic weapon of computer

• Stored program computer working model, the most basic logical structure of computer system;

• Function call stack, the basis for high-level language operation, only machine language and assembly language 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;

• Interrupts, the base point of the multi-channel program operating system, no interrupt mechanism programs can start running other programs only after they have been run from scratch.

One, function call stack 1. Stack

A stack-type C language program must run with a record call path and parameter space. Including:

 函数调用框架 传递参数 保存返回地址(如eax) 提供局部变量空间
2. Stack Register

(1) ESP stack pointer

(2) EBP base point pointer (represents the current function call base address in C language)

3. Stack operations

(1) Push stack top pointer reduces 4 bytes (32 bits)

(2) Pop stack top pointer increased by 4 bytes

4. Other key registers:

Cs:eipcall

Ret

5. Parameter passing and local variables

(1) Establishing the framework (equivalent to the call command)

push %ebpmovl %esp,%ebp

(2) Frame removal (equivalent to RET instruction)

movl %ebp,%esppop  %ebp

When the function returns, the frame must be removed, and the build and dismantle are one by one corresponding.

(3) Transfer parameters

Before the framework of a child function is established, the value of the local variable is saved in the caller's stack frame, so the value of the variable can be put into the stack before the child function framework is established.

! The return value of the function is passed through the EAX register

6. In-depth understanding of the working mechanism of function call PvP

7. Example analysis of stack changes when function calls second, experimental mypcb.h

Header files that are referenced in other. c Files

/* * LINUX/MYKERNEL/MYPCB.H * * Kernel Internal PCB Types * * Copyright (C) mengning * * * *#Define Max_task_num 4#Define Kernel_stack_size 1024*8/* Cpu-specific State of this task */struct Thread {UnsignedLong IP;Save EIPUnsignedLong 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 functions                   
MYMAIN.C:

Kernel initialization and number NO. 0 process Startup

/* * LINUX/MYKERNEL/MYMAIN.C * * Kernel Internal My_start_kernel * * Copyright (c) mengning * */#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];Declares a task array, TPCB struct type has a definition in mypcb.h TPCB * My_current_task =NULL;Declares the current task pointerVolatileint my_need_sched =0;Flags that define whether a schedule is requiredvoid My_process (void);void __init My_start_kernel (void) {int PID =0;int i;/* Initialize Process 0*/The initialization of process data structure of number No. 0 task[pid].pid = pid; Task[pid].state =0;/*-1 unrunnable, 0 runnable, >0 stopped */task[pid].task_entry = Task[pid].thread.ip = (UnsignedLong) my_process;To define the entry for process 0 as my_process, see the my_process function Task[pid].thread.sp = at the beginning of line 58th = (UnsignedLong) &task[pid].stack[kernel_stack_size-1];Stack top for definition stacks task[pid].next = &task[pid];Because only process 0 in the beginning of the system has no other processes, so the PID of the next still point to their own/*fork More Process */Create additional processes that can directly copy code for process No. 0 when initializing these processesfor (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 = (UnsignedLong) &task[i].stack[kernel_stack_size-1]; Task[i].next = Task[i-1].next; Task[i-1].next = &task[i]; }/* Starting from process # NO. 0 * * pid =0; My_current_task = &task[pid];AsmVolatile"Movl%1,%%esp\n\t"/* Set the value of ESP */"Pushl%1\n\t"/* Convert EBP to Stack (at this time esp=ebp),%1 equals task[pid].thread.sp*/"Pushl%0\n\t"/* Press the EIP on the stack,%0 equals task[pid].thread.ip*/"Ret\n\t"/* equivalent to the EIP stack */"Popl%%ebp\n\t"/* Process No. 0 is just starting */::"C" (Task[pid].thread.ip), "D" (TASK[PID].THREAD.SP) /* input C or D mean%ecx/%edx*/);} void my_process (void) // All processes take this as a starting point {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) //execution 10 000 000 times to determine if the schedule needs to be dispatched {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) mengning * */#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;externVolatileint my_need_sched;Volatileint 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 My_timer_handler (void) {#if 1if (time_count%1000 = =0 && my_need_sched! =1)Used to set the size of the time slice, set the schedule flag when the time slice runs out. Meet the clock interrupt occurs 1000 times, and when My_need_sched!=1, the my_need_sched is assigned to 1. When the process discovers the my_need_sched=1, it executes the my_schedule, and the process is dispatched. {PRINTK (Kern_notice">>>my_timer_handler here<<<\n"); my_need_sched =1; } Time_count + +;#endifReturn }void My_schedule (void) {TPCB * next; TPCB * prev;if (My_current_task = =NullReturn when an error occurs | | My_current_task->next = =NULL) {Return } PRINTK (Kern_notice">>>my_schedule<<<\n");/* Schedule */next = my_current_task->next;Assigns the next process of the current process to next prev = My_current_task;Prev Current Processif (next->state = =0)/*-1 unrunnable, 0 runnable, >0 stopped */{/* Process Toggle jump to next process */AsmVolatile"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 is prev->thread.ip*/"Pushl%3\n\t""Ret\n\t"/* Record the eip,%3 to jump process to next->thread.ip*/"1:\t"/* The next process starts executing */"Popl%%ebp\n\t":"=m" (PREV-&GT;THREAD.SP),"=m" (PREV-&GT;THREAD.IP):"M" (NEXT-&GT;THREAD.SP),"M" (Next->thread.ip)); My_current_task = Next; PRINTK (Kern_notice">>>switch%d to%d<<<\n", prev->pid,next->pid); }ElseThe process is a new process {next->state =0; My_current_task = Next; PRINTK (Kern_notice">>>switch%d to%d<<<\n", prev->pid,next->pid);/* Switch to New process */AsmVolatile"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*/ "mo VL%2,%%ebp\n\t "/ * re-record 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 the label 1: The Code stores the address in memory */ "PUSHL%3\n\t" " ret\n\t"/ * re-record the eip,%3 of the jump process to NEXT->THREAD.IP */: "= M "(PREV->THREAD.SP)," =m "(PREV->THREAD.IP): " M "(NEXT->THREAD.SP)," M "(NEXT->THREAD.IP) ); } return;               

Iv. Learning to summarize and understand

The three key weapons of computer work are mentioned in the course: the storage program computer working model, stack, interrupt.

    • The storage program computer working model, simply means that the CPU interprets and executes computer instructions, memory is used to store data and programs.

    • The stack mechanism (the function call stack), which is not so important in machine and assembly language, has a high-level language, especially a function call, which makes the stack an important basis for computer work;

    • Interrupts, introducing an interrupt mechanism that enables the kernel to handle hardware peripheral I/O. The interrupt source has I/O requests, clocks, and system calls. Interrupts allow the computer to process multiple programs at the same time, improving computer efficiency.

Linux Kernel Analysis 24th study Report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.