The Master said, “An army can take away its general, but not its general.” The Analects of Confucius: A rare chapter

A hundred blog series. This paper is: the v40. Xx HongMeng kernel source code analysis (assembly summary) | assembly and lovely as the girl next door

Hardware architecture:

  • V22. Xx HongMeng kernel source code analysis (assembly) | where the CPU clock in
  • V23. Xx HongMeng kernel source code analysis (assembly parameter) | how to transfer the parameters of the complex
  • V36. Xx HongMeng kernel source code analysis (operating mode) | CPU is inspired, seven wives
  • V38. Xx HongMeng kernel source code analysis (register) | jack Bauer is the busiest storage universe
  • Abnormal v39. Xx HongMeng kernel source code analysis (over) | society is very simple, complex people
  • V40. Xx HongMeng kernel source code analysis (assembly summary) | assembly and lovely as the girl next door
  • V42. Xx HongMeng kernel source code analysis (interrupt switch) | system dynamic due to interrupt
  • V43. Xx HongMeng kernel source code analysis (interrupt concept) | sea father-in-law’s daily work
  • V44. Xx HongMeng kernel source code analysis (interrupt management) | henceforth no longer afraid of interruption

Compilation is actually pretty cute

  • Most IT practitioners do not touch the assembly for a lifetime, IT sounds like the ancient times distant call, always feel far but can hear the sound, assembly is really 01110011, assembly instructions are basically one by one corresponding to the machine instructions.
  • The so-called kernel is the driver of the hardware, and the orderly management of the resources behind the driver. The resources here are CPU(single-core/multi-core), memory, disk, AND I/O devices. Layers of encapsulation, step by step cover, to the application layer, I do not know the Han, no matter wei Jin just good. Good is good, but there is a saying, in fact, there is no quiet time, but someone for you to carry on. Don’t you want to know how others carry their weight?
  • The higher the language is the more close to the human thinking mode, the lower the language is the more close to the level of logic and nand gate fluctuation. Assembly is attached to the hardware flight, to study the kernel around assembly, but feel mysterious is from do not understand, fear is from not close.
  • In fact, after in-depth analysis of the kernel source will find that assembly is actually very lovely, very easy, much easier than C/C ++/ Java, really is very silly very simple.

HongMeng kernel source code analysis series has been at least five papers to be involved in the assembly, please make your own reading, but still not enough, should write article 15, completely through, now has just begun, we first arrange HongMeng kernel compiled all files and about the role of the subsequent piece to peel, don’t don’t give up the assembly strip to the ground.

Assembler directory

Hongmeng all assembly files as follows: direct click can view the annotated source code, some sites will remove the link, no way, can go directly to the major site search “Hongmeng kernel source code analysis”, find the source code annotations.

  • \arch\arm\arm\src
    • startupStart the related
      • Reset_vector_mp. S Multi-core CPU startup code, large file
      • Reset_vector_up. S Single-core CPU startup code, large file
    • armv7a
      • Cache.s Two cache-related functions
    • Los_dispatch. S Abnormal distribution processing, large files.
    • Los_hw_exc.S Hardware exception related, large file.
    • los_hw_runstop.S OsSRSaveRegisterOsSRRestoreRegisterAssembly implementation
    • Jmp. S Two simple jump functions
    • Hw_user_get. S copies user-space data to the kernel space
    • Hw_user_put. S Copies kernel space data to user space

hw_user_get.S

Copy user-space data SRC to kernel space DST

// errno_t _arm_get_user(void * DST, const void * SRC, size_t dstTypeLen, size_t srcTypeLen)
FUNCTION(_arm_get_user) stmdb sp! , {r0, r1, r2, r3, lr} @ four parameters in the stack, save LR CMP R2, #0@ r2 and0Lget_user_return @ = beq.Lget_user_return @ = Lget_user_return @ = bne.Lget_user_err @ = R31@ r2 and1Compare bhi.lget_user_half @if(dstTypeLen>1Jump to Lget_user_half.Lget_user_byte: @ copy data by byte0:  ldrbt   r3, [r1], #0 @r3=*r1
1STRB r3, [r0], #0@*r0=r3 b. Lget_user_return.Lget_user_half: CMP r2, #2@ r2 and2Compare bhi.lget_user_word @if(dstTypeLen>2) Lget_user_word
2:  ldrht   r3, [r1], #0@ completes the copy of the last byte3:  strh    r3, [r0], #0B. lget_user_return. Lget_user_word: CMP R2, #4@ r2 and4Compare bhi.lget_user_err @if(dstTypeLen>4Jump to Lget_user_err4:  ldrt   r3, [r1], #0
5:  str    r3, [r0], #0.lget_user_return: @return anchor ldmia sp! , {r0, r1, r2, r3, lr} @ save the contents of the stack, restore each register value mov r0,0@r0 save the return value is0Bx lr @ jumps back to call function to continue execution, _arm_get_user ends here! .Lget_user_err: ldmia sp! , {r0, r1, R2, r3, lr} @ save the contents of the stack, restore each register value mov r0, #- 14@r0 save the return value is- 14Bx lr @ jumps back to call function to continue execution, _arm_get_user ends here! . Pushsection __exc_table,"a"
    .long   0b. Lget_user_err.long   1b. Lget_user_err.long   2b. Lget_user_err.long   3b. Lget_user_err.long   4b. Lget_user_err.long   5b,. Lget_user_err. PopsectionCopy the code

Interpretation of the

  • Why does user space and kernel space need to be copied? This is a classic question. I read some answers on the Internet, and it’s fine:

    The kernel cannot trust any Pointers to user-space. The data to which a pointer to user space points must be validated. If you do validation without copying, you are always at risk of other incoming/threads modifying user-space data during subsequent runs. So you have to make a copy.

    In memory series article has repeatedly said that each user process has its own independent user space, but the user space is through the MMU mapping, is on the surface of the carpet of flowers behind are Shared with the real physical memory, so in the process of the task of high frequency switch, the original user space address content is easy to override. Here’s an example:

    • User A has A beauty Xi Shi in Wancong Hotel room 21 said to dedicate to the kernel big guy, if the kernel does not directly take the beauty home, but just make A record, write Xi Shi in Wancong hotel room 21, the kernel big guy immediately went to, will not be wrong to take the right person, but if other things to delay?
    • In this time of delay, the scheduling algorithm gives room 21 of Wancong Hotel to user B. Of course, before user B uses it, the hotel management staff will replace Xishi (so that when user A returns to the hotel, how to restore the original things). When room 21 was empty, B certainly did not know that the original room was used by A, and there had been A beauty Xi Shi in it, and it is impossible to know that A dedicated Xi Shi to the kernel big guy. Due to B’s business needs, it is likely that they have made a reservation to room 21.
    • At this time, if the kernel big guy is busy, think of the user A beauty, it is time. Because only recorded the address, went directly to wancong hotel room 21 arrest people, but this will be arrested that is our Miss Dongshi ah. This doesn’t mess things up.
    • So we need to copy it across space, just take the girl home and lock her up somewhere.

Reset_vector_mp. S and reset_vector_up. S

Hongmeng startup code according to the CPU multi-core or single-core divided into two independent file processing. Mp is multi-processor (multiprocessing) meaning: multi-CPU core operating system three processing modes (SMP+AMP+BMP) Hongmeng is to achieve the SMP way

  • Asymmetric multiprocessing (AMP) per CPU core

Run a separate operating system or separate instances of the same operating system.

  • Symmetric Multiprocessing (SMP) An instance of an operating system

All CPU cores can be managed at the same time, and the application is not bound to any one kernel.

  • Bound Multiprocessing (BMP) an operating system instance can

All CPU cores are managed simultaneously, but each application is locked to a specific core.

Up (Unit processing) means a single CPU, although not as complex as MP, but the file is also large, 500 line assembly, more than one section, need a separate chapter on reset_vector

The startup codes are listed only in the up case

Reset_vector: @hongmeng single-core CPU boot code/* Do some early CPU setup: I /d cache disable, mmu disabled */MRC p15.0R0, c1, c0,0Bic r0, # (1<<12(I) c % b # C1<<2 | 1<<0) MCR p15,0R0, c1, c0,0

    /* r11: delta of physical address and virtual address */Adr r11, PA_vA_offset LDR r0, [r11] sub R11, r11, r0/* if we need to relocate to proper location or not */Adr r4, __exception_handlers/* r4: base of load address */LDR r5, = SYS_MEM_BASE/* r5: base of physical address */Subs R12, R4, r5/* r12: delta of load address and physical address */
    beq     reloc_img_to_bottom_done            /* if we load image at the bottom of physical address */

    /* we need to relocate image at the bottom of physical address */LDR r7. = __exception_handlers/* r7: base of linked address (or vm address) */LDR r6, = __bss_start/* r6: end of linked address (or vm address) */Sub r6, r7/* r6: delta of linked address (or vm address) */The add r6, r4/* r6: end of load address */
Copy the code

Los_dispatch. S and los_hw_exc. S

The exception pattern handling entry and unified distribution reality, as mentioned earlier, is very complex, with more than 1000 lines, and will be detailed separately later.

jmp.S

Two simple function longjMP setjMP implementation, with annotations please go to the Hongmeng kernel source annotations analysis view

FUNCTION(longjmp) LDMFD r0, {r4-r12} add r0, #(4 * 9) LDR r13, [r0] add r0, #4LDR R14, [R0] CMP R1, #0Moveq r1, #1Mov r0, r1 mov PC,lr

FUNCTION(setjmp)Stmea r0,{r4-r12} add r0, #(4 * 9[r0] add r0, #4STR r14, [r0] mov r0, #0Mov PC, lrCopy the code

los_hw_runstop.S

    .global  OsSRSaveRegister
    .global  OsSRRestoreRegister
Copy the code

The assembly of two functions is a bit more complicated and will be explained separately.

cache.S

These are the two function implementations of the cache section, not annotated here, and try to understand the implementation of these two functions. Add annotations please go to the hongmeng kernel source code annotation analysis view

.macro DCACHE_LINE_SIZE, reg, TMP MRC P15,0, \ TMP, c0, c0,1LSR \ TMP, \ TMP, #16
    and\ TMP, \ TMP, #0xfMov \ reg #4Mov \reg, \reg, LSL \ TMP.endm


FUNCTION(arm_inv_cache_range)
    push    {r2, r3}
    DCACHE_LINE_SIZE r2, r3
    sub    r3, r2, #1TST r0, r3 BIC r0, r0, r3 McRne p150R0, c7, c14,1TST R1, R3 BIC R1, R1, R3 McRne p15,0, R1, C7, C14,1
1: the MCR p15.0R0, c7, c6,1Add R0, r0, R2 CMP r0, R1 BLo1bDSB pop {r2, r3} mov PC,lr

FUNCTION(arm_clean_cache_range)
    push   {r2, r3}
    DCACHE_LINE_SIZE r2, r3
    sub    r3, r2, #1Bic R0, R0, R31: the MCR p15.0R0, c7, c10,1Add R0, r0, R2 CMP r0, R1 BLo1bDSB POP {R2, R3} mov PC, LRCopy the code

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.