The Master said, “I have nothing to do with those who do not know. Hear more and choose its good and from it, see more and know, know the second also.” Analects of Confucius: A review

A hundred blog series. This is:

V20. Xx HongMeng kernel source code analysis (stack way) | who provides the application site

The basic tools are:

  • V01. Xx HongMeng kernel source code analysis (two-way chain table) | who is the most important kernel structure
  • V19. Xx HongMeng kernel source code analysis (bitmap management) | who can be divided into two and a half to spend a penny
  • V20. Xx HongMeng kernel source code analysis (using stack) | programs run site provided by the who
  • V31. Xx HongMeng kernel source code analysis (timer) | which task of top priority
  • V34. Xx HongMeng kernel source code analysis (atomic) | who escort for atomic operations
  • V35. Xx HongMeng kernel source code analysis (time management) | who is a basic unit of time the kernel

Intensive reading kernel source code around assembly language, hongmeng kernel has six assembly files, do not read them it is really difficult to understand the following problems.

1. How is the system call implemented?

2. How does the CPU switch task and process context?

3. How are hardware interrupts handled?

4. Where does the main function come from?

5. What happens at the beginning of startup?

6. What happens at the end of the shutdown?

Here is a very simple C file compiled into assembly code annotations. Reading these notes will reveal that assembly is cute, even addictive, not as scary as you might think, and will change your understanding of assembly and stack.

#include <stdio.h>
#include <math.h>

int square(intA,int b){
    return a*b;
}

int fp(int b)
{
    int a = 1;
    return square(a + b, a + b); }int main(a)
{
    int sum = 1;
    for(int a = 0; a <100; a++){
        sum = sum + fp(a);
    }
    return sum;
}
Copy the code
// compiler: armv7-a clang (trunk)
square(int.int):
        sub     sp, sp, #8@ sp minus8To allocate stack space to square, only2STR r0, [sp, #4[sp] @ldr R1, [sp, #4[sp] @ fetch the first parameter to R1 LDR R2, [sp] @ fetch the second parameter to R2 mul r0, R1, r2 @ execute a*b to r0, return the work is always to r0 add sp, sp, #8The @ subroutine returns, equivalent to mov PC, lr, that is, jumps to the call pointfp(int):
        push    {r11, lr}      @r11(fp)/ LR, save main mov r11, sp @r11 used to save sp value, function stack start position sub sp, sp, #8@ sp minus8To allocate stack space to FP, only2STR r0, [sp, #4] @ save the parameter value and put it in SP+4Mov r0, #1         @r0=1STR r0, [sp] @1[SP] @ give the value of SP to r0 LDR r1, [SP, #4] @ SP +4Add R1, r0, R1 @ execute R1 =a+b mov r0, R1 @r0= R1, pass the parameter bl with r0, R1square(int.intMov LR, PC, mov PCsquare(int.intPop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @ Push {r11, lr} @r11(fp)/ LR, mov r11, sp @r11 used to save sp value, function stack start position sub sp, sp, #16@ sp minus8Allocate stack space to main2Stack space to complete the calculation mov R0, #0@ initialize r0 STR r0, [r11, #4 -STR r0, [sp, #8] @sum will always occupy SP+8STR r0, [sp, #4@a will always occupy SP+4LBB1_1: @ loop start entry LDR r0, [sp, #4] @ fetch a value to r0 CMP r0, #99@ with99Compare BGT. LBB1_4@ greater than99Mov pc.lbb1_4b.LBB1_2 @ To continue the loop, directly mov PC.lbb1_2.LBB1_2: @ to meet the loop condition8R0, sp+8LDR r0, [sp, #] LDR r0, [sp, #4[@r0 is used to pass the parameter, fetching the value of A to r0 as the parameter bl of fpfp(intMov LR, PC, mov PCfp(int[sp] @ add r0, r0, r1 @ calculate the new SUM, r0 save STR r0, [sp, #8Save the new sum to SP+8Mov pc.lbb1_3.LBB1_3: @ to complete a++ operation entry4]   @SP+4The record is the value of a, and assign r0 add r0, r0, #1@ r0 increase1}, p, p4@ puts the new a value back into SP+4B.LBB1_1 @ jump to compare a <100At.lbb1_4: @ loop end entry LDR r0, [sp, #8Pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @pop {r11, lr} @ Jumping to LR is equivalent to MOV PC, LRCopy the code

This simple assembly is not hongmeng assembly, but first lay a bottom, from shallow to deep, but understand it basic understanding hongmeng assembly code no problem, the follow-up will be detailed analysis of the role of hongmeng kernel each assembly file. Start analyzing the assembly code above.

First: the above code and the hongmeng kernel stack way, have used decrement full stack, what is decrement full stack? Decrement means that the address at the bottom of the stack is higher than the address at the top, and full means that the SP pointer is always at the top of the stack. You must understand decrement full stack, otherwise you can’t read kernel assembly code. For example:

square(int.int):
        sub     sp, sp, #8@ sp minus8To allocate stack space to square, only2STR r0, [sp, #4[sp] @ldr R1, [sp, #4[sp] @ fetch the first parameter to R1 LDR R2, [sp] @ fetch the second parameter to R2 mul r0, R1, r2 @ execute a*b to r0, return the work is always to r0 add sp, sp, #8The @ subroutine returns, equivalent to mov PC, lr, that is, jumps to the call pointCopy the code

Sp = sp + 8; sp = sp + 8; sp = sp + 8; sp = sp + 8; sp = sp + 8; sp = sp + 8; sp = sp + 8 For example, LDR R1, [sp, #4] means to fetch the value of sp +4, which is the virtual address of r1 register, sp does not change, why +, because SP points to the top of the stack, the address is the smallest. Full stack means that the operation on the address cannot exceed SP, so you will rarely see the value of the SP-4 address assigned to a register in the calculation process, unless it is a special instruction, which is not possible.

Add sp, sp, #8 = add sp, sp, #8 = add sp, sp, #8 = add sp, sp The size of the space is the stack frame, which is the essence of the stack frame.

The third: Push {r11, lr} and pop {r11, LR} also come in pairs, mainly used for function calls, such as A -> B, where B stores A stack frame range and instruction position, lr stores which instruction position is executed by A function, R11 does the work of FP, In this way, when B returns to A after execution, the LR kernel will know which instruction of A to execute and also know the top position of A.

Fourth: the role of frequent R0 registers used to pass and return values, before A call to B, if there are two parameters, the parameters to the R0, r1, acted as A variable, to B, let R0, r1 into the stack, the purpose is to save the parameter values, because B with R0, r1, they become B variables used. All return values are saved to R0 by default. B returns the value to R0, goes back to A and retrives the value of R0 and for A this is the return value of B.

This is the above analysis for assembly code, asking two questions

First: What if it’s a mutable parameter? Can there be more than one return value?

Intensive reading of the kernel source code

Four code stores synchronous annotation kernel source code, >> view the Gitee repository

Analysis of 100 blogs. Dig deep into the core

Add comments to hongmeng kernel source code process, sort out the following article. Content based on the source code, often in life scene analogy as much as possible into the kernel knowledge of a scene, with a pictorial sense, easy to understand memory. It’s important to speak in a way that others can understand! The 100 blogs are by no means a bunch of ridiculously difficult concepts being put forward by Baidu. That’s not interesting. More hope to make the kernel become lifelike, feel more intimate. It’s hard, it’s hard, but there’s no turning back. 😛 and code bugs need to be constantly debug, there will be many mistakes and omissions in the article and annotation content, please forgive, but will be repeatedly amended, continuous update. Xx represents the number of modifications, refined, concise and comprehensive, and strive to create high-quality content.

Compile build The fundamental tools Loading operation Process management
Compile environment

The build process

Environment script

Build tools

Designed.the gn application

Ninja ninja

Two-way linked list

Bitmap management

In the stack way

The timer

Atomic operation

Time management

The ELF format

The ELF parsing

Static link

relocation

Process image

Process management

Process concept

Fork

Special process

Process recycling

Signal production

Signal consumption

Shell editor

Shell parsing

Process of communication Memory management Ins and outs Task management
spinlocks

The mutex

Process of communication

A semaphore

Incident control

The message queue

Memory allocation

Memory management

Memory assembly

The memory mapping

Rules of memory

Physical memory

Total directory

Scheduling the story

Main memory slave

The source code comments

Source structure

Static site

The clock task

Task scheduling

Task management

The scheduling queue

Scheduling mechanism

Thread concept

Concurrent parallel

The system calls

Task switching

The file system Hardware architecture
File concept

The file system

The index node

Mount the directory

Root file system

Character device

VFS

File handle

Pipeline file

Compilation basis

Assembly and the cords

Working mode

register

Anomaly over

Assembly summary

Interrupt switch

Interrupt concept

Interrupt management

HongMeng station | into a little bit every day, the original is not easy, welcome to reprint, please indicate the source.