Linux Kernel second week

Source: Internet
Author: User

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;

      • 1234567 enterpushl %ebpmovl %esp,%ebpleavemovl %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

12 pop:从高地址向低地址push:从低地址向高地址

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

12345 顺序执行:总是指向地址连续的下一条指令跳转/分支:执行这样的指令的时候,cs : eip的值会 根据程序需要被修改call:将当前cs : eip的值压入栈顶,cs : eip指向被 调用函数的入口地址ret:从栈顶弹出原来保存在这里的cs : eip的值,放 入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

12345678910111213141516171819202122232425262728 /*     *  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 this task */    struct Thread {        unsigned longip;  //保存eip        unsigned longsp;  //保存esp    };     typedef structPCB{        intpid;        volatilelongstate;    /* 记录进程状态,-1 未运行, 0 运行中, >0 阻塞停止 */        charstack[KERNEL_STACK_SIZE];  /* 定义堆栈结构*/        structThread thread;        unsigned longtask_entry;   /* 定义程序入口,通常是main函数*/        struct PCB *next;    }tPCB;    voidmy_schedule(void);//调度器函数

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-&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);            } 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-&GT;THREAD.SP       */"MOVL%2,%%ebp\n\t"/* re-record the ebp,%2 to jump process to NEXT-&GT;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-&GT;THREAD.IP */: "=m" (PREV-&GT;THREAD.SP), "=m" (prev->tHREAD.IP): "M" (NEXT-&GT;THREAD.SP), "M" (Next->thread.ip));     } return; }

Chapter 3 Summary

Fundamentals of operating system work:

The operating system's kernel has a starting position, starting at this starting position. At the beginning of the work, the CPU is assigned to the first process, the first process is executed, and then a certain scheduling algorithm, such as time slice rotation, interrupts after a time slice.

, the first process is blocked, and the CPU is allocated to the next process after the save site is completed and the next process is executed. In this way, the operating system completes the basic process scheduling function. The stored program computer, stack mechanism, and interrupt mechanism. Operating system is to manage the computer

Hardware and software resources, but also the core of the computer system and the cornerstone. Operating system is the management of computer systems of all hardware resources, including software resources and data resources, control procedures run, improve the human machine interface, and other applications to provide support, etc., so that the calculation

Machine system to maximize the role of all resources to provide users with a 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-and advanced-level structures in the TASK_STRUCT structure

Information, including a link from the register copy of some hardware device 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. After the system starts,

The kernel is typically represented as a process. A pointer to task_struct global pointer variable current is used to record the running.

::::: Hao original works reprint please indicate the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Linux Kernel second week

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.