Linux Kernel Analysis Summary

Source: Internet
Author: User

Zhou Zixuan Original works reproduced annotated source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Notes:
    • The core idea of the von Neumann architecture is to store program computers. There are two kinds of instructions in the computer, one is the user instruction, the other is the system call.
    • Linux is a POSIX and UNIX-based multi-user, multitasking, multi-threaded and multi-CPU operating system.
    • When the kernel is running, the system enters kernel space in kernel state. When executing a normal user program, the system will enter user space to execute.
    • Applications running in the system communicate with the kernel through system calls.
    • Three magic: 存储程序计算机、函数调用堆栈、中断机制    Two swords:中断上下文、进程上下文的切换
    • When a hardware device wants to communicate with the system, it first sends out an asynchronous interrupt signal to interrupt the execution of the processor, which in turn interrupts the execution of the kernel. Interrupts usually correspond to an interrupt number, which is used by the kernel to find the appropriate interrupt service and call the program to respond and handle interrupts.
    • Linux is a portable operating system. That is, most C code is architecture-independent and can be compiled and executed on many computers of different architectures.
Blog directory:
    • Linux kernel analysis 1
      •   von Neumann the core idea of the architecture is to store the program computer. There are two kinds of instructions in the computer, one is the user instruction, the other is the system call.

      • When a user is using a computer, the computer runs in accordance with the instructions it assembles, and then returns to user mode after using the system call to ensure the stability of the system. Stack change for call in

      • program
      • assemble basic General Register

        16-bit  32-bit

        AX    EAX accumulator
        BX    EBX Base Register
        CX    ecx count Register
        DX    EDX data register
        BP    EBP stack base address pointer
        SI     ESI Forwarding Register
        DI     EDI address register
        SP     ESP stack top pointer

  • Linux kernel Analysis 2
    • function calling convention The code of the
      function calling convention parameter Passing order responsible for cleaning up the stack occupied by parameters
      __pascal left-to-right caller
      __stdcall right-to-left tuned function
      __cdecl right-to-left caller
      • Call function and the called function must take the same function as the calling convention for the program to run correctly.

         windows the default function calling convention for a C + + program is __cdecllinux the default rule for GCC is __stdcall  
    • Three magic weapon

        store program computer, function call stack, interrupt mechanism  
    • Two sword

        interrupt context, process context toggle  
  • Linux kernel Analysis 3
    • computer startup process Overview
      • x86 The first action cs:eip=ffff:0000h the CPU starts (converted to physical address 000ffff0h, because 16-bit CPUs have 20 address lines), That is, the location of the BIOS program. After the
      • BIOS routine detects the hardware and completes the corresponding initialization, it looks for the bootable media, and after locating the bootloader into the specified memory area, the control is given to the boot program. This is usually the first sector of the hard disk MBR and the active partition of the boot program loaded into memory (that is, load bootloader), the full load after the control to bootloader. The
      • bootstrapper bootloader begins to take charge of operating system initialization and then starts the operating system. The kernel, INITRD, and root partitions and directories are typically specified when the operating system is started, such as Root (hd0,0), kernel (hd0,0)/bzimage Root=/dev/ram init=/bin/ash,initrd ( hd0,0)/myinitrd4m.img
      • The kernel boot process consists of start_kernel before and after all the assembly instructions that were initialized before starting the operating system initialization of C code and finally performing the first user-state process init. The
      • typically starts in two stages, first using the INITRD memory file system and then switching to the hard disk file system to continue booting. The
        • INITRD file has two main functions:
        • 1, the driver module (modules) not provided by the kernel file (i.e., Vmlinuz), which is required to power on, and is responsible for loading the root text on the hard disk System and execute the/SBIN/INIT program to continue the boot process
  • Linux Kernel Analysis 4
    • System_call is the entry point for all system calls in Linux, with at least one parameter per system call, which is the system call number passed by eax
      • An application calls the fork () encapsulation routine, the value of the EAX register is set to 2 (that is, __nr_fork) before the int $0x80 is executed.
      • The number of parameters cannot be more than 6 (EBX,ECX, edx, ESI, EDI, EBP) beyond the system call number (EAX)
    • When the user-state process invokes a system call, the CPU switches to the kernel state and starts executing a kernel function.
      • In Linux, which executes a system call by executing an int $0x80, this assembly instruction produces a programming exception with a vector of 128
  • Linux Kernel Analysis 5
      • Add time and Time-asm commands to Menuos (four-step Operation command)
      • Interrupt handling procedure for system calls

    • Linux Kernel Analysis 6
    The role of the
    • process: Linking signals, interprocess communication, memory management, and file systems
    • The three main functions of the operating system: process management, memory management, file system
    • kernel distinguishes each process with a unique process identity PID
    • Fork, Vfork, and clone three system calls can create a new process, and all are created by calling Do_fork to create the process   the
      1th process is the ancestor of all user-state processes, and the No. 0 process is the ancestor of all kernel threads
  • Linux kernel Analysis 7
    • execve system call
      • Shell calls Execve pass command line arguments and environment parameters to the executable program Mai n function

        • int EXECV E (const char * filename,char * CONST argv[],CHAR * Const envp[]);

          The
          • execve () is used to execute the file path represented by the argument filename string, the second parameter is passed to the execution file using an array of pointers, and it needs to end with a null pointer (null), and the last parameter is an array of new environment variables passed to the execution file.
        • Library functions exec* are EXECVE package routines

          Execve and Fork are a special point of system invocation: The general is to fall into the kernel state and return to the user state. Fork parent process and general process scheduling, the child process returns to a specific point ret_from_fork, the child process starts from ret_from_fork and then returns to the user state ; Execve Special: Execute to executable program--fall into kernel--construct new executable file--Overwrite the original executables--return to the new executor, as a starting point (that is, main function), need to construct his execution environment;  
  • Linux Kernel Analysis 8
    • Process switching is implemented in the kernel at the time of the following three opportunities :
        • Interrupt processing (including clock interrupts, I/O interrupts, system calls, and exceptions), call schedule () directly, or call schedule () based on the need_resched tag when returning to the user state;

        • Kernel threads can directly call schedule () for process switching, or in the process of interrupt processing, which means that kernel threads as a special kind of process can be active scheduling, but also can be passively dispatched;

        • The user-state process cannot implement the active scheduling, but can only be dispatched by a point in time after the kernel state, that is, scheduling during interrupt processing.

Linux Kernel Analysis Summary

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.