Search code examples
linuxmemory-managementlinux-kernelbootpage-fault

Why is this piece of code in linux kernel NOT causing dead loop?


I'm reading the linux source code(4.4.45, but should be same in other version) about the page table initialization when booting, and the code below has confused me.

Specifically, I'm reading about how the early_level4_pgt is initialized. Here is part of the code in x86_64, the link is here.

        leaq    level2_kernel_pgt(%rip), %rdi
        leaq    4096(%rdi), %r8
        /* See if it is a valid page table entry */
1:      testb   $1, 0(%rdi)
        jz      2f
        addq    %rbp, 0(%rdi)
        /* Go to the next page */
2:      addq    $8, %rdi
        cmp     %r8, %rdi
        jne     1b

        /* Fixup phys_base */
        addq    %rbp, phys_base(%rip)

        movq    $(early_level4_pgt - __START_KERNEL_map), %rax
        jmp 1f
ENTRY(secondary_startup_64)
(below is omitted...)

Starting from 1:, the flow will go to 2: no matter what; and inside 2:, the flow will jump back to 1:no matter what!!

I'm really confused, how does this loop end and when does the kernel enter secondary_startup_64? My guess is that when it visits an invalid pmd entry, which causes a page fault, the handler will deal with the rest of the startup code. But I'm not sure about it and I don't know where to find the corresponding code.

Could anyone give me some clues? Any help is appreciated.


Solution

  • https://stackoverflow.com/a/27353169/2422527

    jmp 1f means jump to label 1 forward (after this instruction).

    jmp 1b means jump to label 1 backward (before this instruction).

    So the line jmp 1f jump to label 1 after it, not causing a dead loop.