1
0
mirror of https://github.com/0xAX/linux-insides.git synced 2024-12-22 22:58:08 +00:00
linux-insides/Initialization/linux-initialization-2.md

505 lines
26 KiB
Markdown
Raw Normal View History

2015-03-14 16:51:32 +00:00
Kernel initialization. Part 2.
================================================================================
2015-03-17 11:43:46 +00:00
Early interrupt and exception handling
2015-03-14 16:51:32 +00:00
--------------------------------------------------------------------------------
2018-03-23 19:06:57 +00:00
In the previous [part](https://0xax.gitbooks.io/linux-insides/content/Initialization/linux-initialization-1.html) we stopped before setting of early interrupt handlers. At this moment we are in the decompressed Linux kernel, we have basic [paging](https://en.wikipedia.org/wiki/Page_table) structure for early boot and our current goal is to finish early preparation before the main kernel code will start to work.
2018-03-23 19:06:57 +00:00
We already started to do this preparation in the previous [first](https://0xax.gitbooks.io/linux-insides/content/Initialization/linux-initialization-1.html) part of this [chapter](https://0xax.gitbooks.io/linux-insides/content/Initialization/index.html). We continue in this part and will know more about interrupt and exception handling.
2015-03-14 16:51:32 +00:00
2015-03-17 11:43:46 +00:00
Remember that we stopped before following loop:
2015-03-14 16:51:32 +00:00
```C
for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
set_intr_gate(i, early_idt_handler_array[i]);
2015-03-14 16:51:32 +00:00
```
from the [arch/x86/kernel/head64.c](https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/arch/x86/kernel/head64.c) source code file. But before we started to sort out this code, we need to know about interrupts and handlers.
2015-03-14 16:51:32 +00:00
Some theory
--------------------------------------------------------------------------------
2016-03-30 17:49:00 +00:00
An interrupt is an event caused by software or hardware to the CPU. For example a user have pressed a key on keyboard. On interrupt, CPU stops the current task and transfer control to the special routine which is called - [interrupt handler](https://en.wikipedia.org/wiki/Interrupt_handler). An interrupt handler handles and interrupt and transfer control back to the previously stopped task. We can split interrupts on three types:
2015-03-14 16:51:32 +00:00
* Software interrupts - when a software signals CPU that it needs kernel attention. These interrupts are generally used for system calls;
* Hardware interrupts - when a hardware event happens, for example button is pressed on a keyboard;
2015-03-14 16:51:32 +00:00
* Exceptions - interrupts generated by CPU, when the CPU detects error, for example division by zero or accessing a memory page which is not in RAM.
Every interrupt and exception is assigned a unique number which called - `vector number`. `Vector number` can be any number from `0` to `255`. There is common practice to use first `32` vector numbers for exceptions, and vector numbers from `32` to `255` are used for user-defined interrupts. We can see it in the code above - `NUM_EXCEPTION_VECTORS`, which defined as:
2015-03-14 16:51:32 +00:00
```C
#define NUM_EXCEPTION_VECTORS 32
```
2015-03-17 11:43:46 +00:00
CPU uses vector number as an index in the `Interrupt Descriptor Table` (we will see description of it soon). CPU catch interrupts from the [APIC](http://en.wikipedia.org/wiki/Advanced_Programmable_Interrupt_Controller) or through it's pins. Following table shows `0-31` exceptions:
2015-03-14 16:51:32 +00:00
```
----------------------------------------------------------------------------------------------
|Vector|Mnemonic|Description |Type |Error Code|Source |
2015-03-14 16:51:32 +00:00
----------------------------------------------------------------------------------------------
|0 | #DE |Divide Error |Fault|NO |DIV and IDIV |
|---------------------------------------------------------------------------------------------
|1 | #DB |Reserved |F/T |NO | |
|---------------------------------------------------------------------------------------------
|2 | --- |NMI |INT |NO |external NMI |
|---------------------------------------------------------------------------------------------
|3 | #BP |Breakpoint |Trap |NO |INT 3 |
|---------------------------------------------------------------------------------------------
|4 | #OF |Overflow |Trap |NO |INTO instruction |
|---------------------------------------------------------------------------------------------
|5 | #BR |Bound Range Exceeded|Fault|NO |BOUND instruction |
|---------------------------------------------------------------------------------------------
|6 | #UD |Invalid Opcode |Fault|NO |UD2 instruction |
|---------------------------------------------------------------------------------------------
|7 | #NM |Device Not Available|Fault|NO |Floating point or [F]WAIT |
|---------------------------------------------------------------------------------------------
2016-06-28 09:29:14 +00:00
|8 | #DF |Double Fault |Abort|YES |An instruction which can generate NMI |
2015-03-14 16:51:32 +00:00
|---------------------------------------------------------------------------------------------
|9 | --- |Reserved |Fault|NO | |
|---------------------------------------------------------------------------------------------
|10 | #TS |Invalid TSS |Fault|YES |Task switch or TSS access |
|---------------------------------------------------------------------------------------------
|11 | #NP |Segment Not Present |Fault|NO |Accessing segment register |
|---------------------------------------------------------------------------------------------
|12 | #SS |Stack-Segment Fault |Fault|YES |Stack operations |
|---------------------------------------------------------------------------------------------
|13 | #GP |General Protection |Fault|YES |Memory reference |
|---------------------------------------------------------------------------------------------
|14 | #PF |Page fault |Fault|YES |Memory reference |
|---------------------------------------------------------------------------------------------
|15 | --- |Reserved | |NO | |
|---------------------------------------------------------------------------------------------
|16 | #MF |x87 FPU fp error |Fault|NO |Floating point or [F]Wait |
|---------------------------------------------------------------------------------------------
|17 | #AC |Alignment Check |Fault|YES |Data reference |
|---------------------------------------------------------------------------------------------
|18 | #MC |Machine Check |Abort|NO | |
|---------------------------------------------------------------------------------------------
|19 | #XM |SIMD fp exception |Fault|NO |SSE[2,3] instructions |
|---------------------------------------------------------------------------------------------
|20 | #VE |Virtualization exc. |Fault|NO |EPT violations |
|---------------------------------------------------------------------------------------------
|21-31 | --- |Reserved |INT |NO |External interrupts |
----------------------------------------------------------------------------------------------
```
To react on interrupt CPU uses special structure - Interrupt Descriptor Table or IDT. IDT is an array of 8-byte descriptors like Global Descriptor Table, but IDT entries are called `gates`. CPU multiplies vector number on 8 to find index of the IDT entry. But in 64-bit mode IDT is an array of 16-byte descriptors and CPU multiplies vector number on 16 to find index of the entry in the IDT. We remember from the previous part that CPU uses special `GDTR` register to locate Global Descriptor Table, so CPU uses special register `IDTR` for Interrupt Descriptor Table and `lidt` instruction for loading base address of the table into this register.
2015-03-14 16:51:32 +00:00
64-bit mode IDT entry has following structure:
```
127 96
--------------------------------------------------------------------------------
| |
| Reserved |
| |
--------------------------------------------------------------------------------
95 64
--------------------------------------------------------------------------------
| |
| Offset 63..32 |
| |
--------------------------------------------------------------------------------
63 48 47 46 44 42 39 34 32
--------------------------------------------------------------------------------
| | | D | | | | | | |
| Offset 31..16 | P | P | 0 |Type |0 0 0 | 0 | 0 | IST |
| | | L | | | | | | |
--------------------------------------------------------------------------------
2017-03-28 18:25:22 +00:00
31 16 15 0
2015-03-14 16:51:32 +00:00
--------------------------------------------------------------------------------
| | |
| Segment Selector | Offset 15..0 |
| | |
--------------------------------------------------------------------------------
```
Where:
* `Offset` - is offset to entry point of an interrupt handler;
* `DPL` - Descriptor Privilege Level;
* `P` - Segment Present flag;
* `Segment selector` - a code segment selector in GDT or LDT
* `IST` - provides ability to switch to a new stack for interrupts handling.
2015-03-14 16:51:32 +00:00
And the last `Type` field describes type of the `IDT` entry. There are three different kinds of handlers for interrupts:
* Task descriptor
* Interrupt descriptor
* Trap descriptor
2015-03-17 11:43:46 +00:00
Interrupt and trap descriptors contain a far pointer to the entry point of the interrupt handler. Only one difference between these types is how CPU handles `IF` flag. If interrupt handler was accessed through interrupt gate, CPU clear the `IF` flag to prevent other interrupts while current interrupt handler executes. After that current interrupt handler executes, CPU sets the `IF` flag again with `iret` instruction.
2015-03-14 16:51:32 +00:00
Other bits in the interrupt gate reserved and must be 0. Now let's look how CPU handles interrupts:
2015-03-14 16:51:32 +00:00
2015-03-17 11:43:46 +00:00
* CPU save flags register, `CS`, and instruction pointer on the stack.
2015-03-14 16:51:32 +00:00
* If interrupt causes an error code (like `#PF` for example), CPU saves an error on the stack after instruction pointer;
* After interrupt handler executed, `iret` instruction used to return from it.
Now let's back to code.
Fill and load IDT
--------------------------------------------------------------------------------
2015-03-17 11:43:46 +00:00
We stopped at the following point:
2015-03-14 16:51:32 +00:00
```C
for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
set_intr_gate(i, early_idt_handler_array[i]);
2015-03-14 16:51:32 +00:00
```
Here we call `set_intr_gate` in the loop, which takes two parameters:
* Number of an interrupt or `vector number`;
2015-03-14 16:51:32 +00:00
* Address of the idt handler.
and inserts an interrupt gate to the `IDT` table which is represented by the `&idt_descr` array. First of all let's look on the `early_idt_handler_array` array. It is an array which is defined in the [arch/x86/include/asm/segment.h](https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/arch/x86/include/asm/segment.h) header file contains addresses of the first `32` exception handlers:
2015-03-14 16:51:32 +00:00
```C
#define EARLY_IDT_HANDLER_SIZE 9
#define NUM_EXCEPTION_VECTORS 32
extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
2015-03-14 16:51:32 +00:00
```
The `early_idt_handler_array` is `288` bytes array which contains address of exception entry points every nine bytes. Every nine bytes of this array consist of two bytes optional instruction for pushing dummy error code if an exception does not provide it, two bytes instruction for pushing vector number to the stack and five bytes of `jump` to the common exception handler code.
2018-08-25 11:28:54 +00:00
As we can see, We're filling only first 32 `IDT` entries in the loop, because all of the early setup runs with interrupts disabled, so there is no need to set up interrupt handlers for vectors greater than `32`. The `early_idt_handler_array` array contains generic idt handlers and we can find its definition in the [arch/x86/kernel/head_64.S](https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/arch/x86/kernel/head_64.S) assembly file. For now we will skip it, but will look it soon. Before this we will look on the implementation of the `set_intr_gate` function.
2015-03-14 16:51:32 +00:00
2018-08-25 11:28:54 +00:00
The `set_intr_gate` function is defined in the [arch/x86/kernel/idt.c](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/idt.c) source file and looks:
2015-03-14 16:51:32 +00:00
```C
2018-08-25 11:28:54 +00:00
static void set_intr_gate(unsigned int n, const void *addr)
2015-03-14 16:51:32 +00:00
{
2018-08-25 11:28:54 +00:00
struct idt_data data;
2015-03-14 16:51:32 +00:00
2018-08-25 11:28:54 +00:00
BUG_ON(n > 0xFF);
2015-03-14 16:51:32 +00:00
2018-08-25 11:28:54 +00:00
memset(&data, 0, sizeof(data));
data.vector = n;
data.addr = addr;
data.segment = __KERNEL_CS;
data.bits.type = GATE_INTERRUPT;
data.bits.p = 1;
2015-03-14 16:51:32 +00:00
2018-08-25 11:28:54 +00:00
idt_setup_from_table(idt_table, &data, 1, false);
}
2015-03-14 16:51:32 +00:00
```
2018-08-25 11:28:54 +00:00
First of all it checks with that passed interrupt number is not greater than `255` with `BUG_ON` macro. We need to do this check because we can have only `256` interrupts. After this, we setup the idt data with the given values. And then we call `idt_setup_from_table` function which looks like:
2015-03-14 16:51:32 +00:00
```C
2018-08-25 11:28:54 +00:00
static void
idt_setup_from_table(gate_desc *idt, const struct idt_data *t, int size, bool sys)
2015-03-14 16:51:32 +00:00
{
2018-08-25 11:28:54 +00:00
gate_desc desc;
for (; size > 0; t++, size--) {
desc.offset_low = (u16) t->addr;
desc.segment = (u16) t->segment
desc.bits = t->bits;
desc.offset_middle = (u16) (t->addr >> 16);
desc.offset_high = (u32) (t->addr >> 32);
desc.reserved = 0;
memcpy(&idt[t->vector], &desc, sizeof(desc));
if (sys)
set_bit(t->vector, system_vectors);
}
2015-03-14 16:51:32 +00:00
}
```
2018-08-25 11:28:54 +00:00
which fill three parts of the address of the interrupt handler with the address which we got in the main loop (address of the interrupt handler entry point). And then we just copy the gate descriptor to the idt entry.
After that main loop will finished, we will have filled `idt_table` array of `gate_desc` structures and we can load `Interrupt Descriptor table` with the call of the:
2015-03-14 16:51:32 +00:00
```C
load_idt((const struct desc_ptr *)&idt_descr);
```
Where `idt_descr` is:
```C
2018-08-25 13:55:13 +00:00
struct desc_ptr idt_descr __ro_after_init = {
.size = (IDT_ENTRIES * 2 * sizeof(unsigned long)) - 1,
.address = (unsigned long) idt_table,
};
2015-03-14 16:51:32 +00:00
```
and `load_idt` just executes `lidt` instruction:
```C
asm volatile("lidt %0"::"m" (*dtr));
```
You can note that there are calls of the `_trace_*` functions in the `_set_gate` and other functions. These functions fills `IDT` gates in the same manner that `_set_gate` but with one difference. These functions use `trace_idt_table` the `Interrupt Descriptor Table` instead of `idt_table` for tracepoints (we will cover this theme in the another part).
2015-03-14 16:51:32 +00:00
Okay, now we have filled and loaded `Interrupt Descriptor Table`, we know how the CPU acts during an interrupt. So now time to deal with interrupts handlers.
2015-03-14 16:51:32 +00:00
Early interrupts handlers
--------------------------------------------------------------------------------
As you can read above, we filled `IDT` with the address of the `early_idt_handler_array`. We can find it in the [arch/x86/kernel/head_64.S](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/head_64.S) assembly file:
2015-03-14 16:51:32 +00:00
```assembly
ENTRY(early_idt_handler_array)
2015-03-14 16:51:32 +00:00
i = 0
.rept NUM_EXCEPTION_VECTORS
.if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
UNWIND_HINT_IRET_REGS
pushq $0 # Dummy error code, to make stack frame uniform
.else
UNWIND_HINT_IRET_REGS offset=8
2015-03-14 16:51:32 +00:00
.endif
pushq $i # 72(%rsp) Vector number
jmp early_idt_handler_common
UNWIND_HINT_IRET_REGS
2015-03-14 16:51:32 +00:00
i = i + 1
.fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
2015-03-14 16:51:32 +00:00
.endr
UNWIND_HINT_IRET_REGS offset=16
END(early_idt_handler_array)
2015-03-14 16:51:32 +00:00
```
Functions which tend to not be called directly by other functions, such as syscall and interrupt handlers, often do unusual non-C-function-type things with the stack pointer. Such code needs to be annotated by using `UNWIND_HINT_IRET_REGS` macro such that objtool can understand it. We can see here, interrupt handlers generation for the first `32` exceptions. We check here, if exception has an error code then we do nothing, if exception does not return error code, we push zero to the stack. We do it for that would stack was uniform. After that we push exception number on the stack and jump on the `early_idt_handler_array` which is generic interrupt handler for now. As we may see above, every nine bytes of the `early_idt_handler_array` array consists from optional push of an error code, push of `vector number` and jump instruction. We can see it in the output of the `objdump` util:
```
$ objdump -D vmlinux
...
...
...
ffffffff81fe5000 <early_idt_handler_array>:
ffffffff81fe5000: 6a 00 pushq $0x0
ffffffff81fe5002: 6a 00 pushq $0x0
ffffffff81fe5004: e9 17 01 00 00 jmpq ffffffff81fe5120 <early_idt_handler_common>
ffffffff81fe5009: 6a 00 pushq $0x0
ffffffff81fe500b: 6a 01 pushq $0x1
ffffffff81fe500d: e9 0e 01 00 00 jmpq ffffffff81fe5120 <early_idt_handler_common>
ffffffff81fe5012: 6a 00 pushq $0x0
ffffffff81fe5014: 6a 02 pushq $0x2
...
...
...
```
As i wrote above, CPU pushes flag register, `CS` and `RIP` on the stack. So before `early_idt_handler` will be executed, stack will contain following data:
2015-03-14 16:51:32 +00:00
```
|--------------------|
| %rflags |
| %cs |
| %rip |
| error code | <-- %rsp
2015-03-14 16:51:32 +00:00
|--------------------|
```
Now let's look on the `early_idt_handler_common` implementation. It locates in the same [arch/x86/kernel/head_64.S](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/head_64.S) assembly file and first of all we increment `early_recursion_flag` to prevent recursion in the `early_idt_handler_common`:
2015-03-14 16:51:32 +00:00
```assembly
incl early_recursion_flag(%rip)
2015-03-14 16:51:32 +00:00
```
Next we save general registers on the stack:
2015-03-14 16:51:32 +00:00
```assembly
pushq %rsi
movq 8(%rsp), %rsi
movq %rdi, 8(%rsp)
pushq %rdx
pushq %rcx
pushq %rax
2015-03-14 16:51:32 +00:00
pushq %r8
pushq %r9
pushq %r10
pushq %r11
pushq %rbx
pushq %rbp
pushq %r12
pushq %r13
pushq %r14
pushq %r15
UNWIND_HINT_REGS
2015-03-14 16:51:32 +00:00
```
We need to do it to prevent wrong values of registers when we return from the interrupt handler. After this we check the vector number, and if it is `#PF` or [Page Fault](https://en.wikipedia.org/wiki/Page_fault), we put value from the `cr2` to the `rdi` register and call `early_make_pgtable` (we'll see it soon):
2015-03-14 16:51:32 +00:00
```assembly
cmpq $14,%rsi
2015-03-14 16:51:32 +00:00
jnz 10f
GET_CR2_INTO(%rdi)
call early_make_pgtable
andl %eax,%eax
jz 20f
```
If vector number is not `#PF`, we call `early_fixup_exception` function with passing kernel stack pointer. (refer to [x86-64 calling convention](https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions)):
```assembly
10:
movq %rsp,%rdi
call early_fixup_exception
```
We'll see the implementaion of the `early_fixup_exception` function later.
2015-03-14 16:51:32 +00:00
```assembly
20:
decl early_recursion_flag(%rip)
jmp restore_regs_and_return_to_kernel
2015-03-14 16:51:32 +00:00
```
After we decrement the `early_recursion_flag`, we restore registers which we saved earlier from the stack and return from the handler with `iretq`.
2015-03-14 16:51:32 +00:00
It is the end of the first interrupt handler. Note that it is very early interrupt handler, so it handles only Page Fault now. We will see handlers for the other interrupts, but now let's look on the page fault handler.
Page fault handling
--------------------------------------------------------------------------------
In the previous paragraph we saw first early interrupt handler which checks interrupt number for page fault and calls `early_make_pgtable` for building new page tables if it is. We need to have `#PF` handler in this step because there are plans to add ability to load kernel above `4G` and make access to `boot_params` structure above the 4G.
2015-03-14 16:51:32 +00:00
2018-09-08 10:23:36 +00:00
You can find implementation of the `early_make_pgtable` in the [arch/x86/kernel/head64.c](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/head64.c) and takes one parameter - address from the `cr2` register, which caused Page Fault. Let's look on it:
2015-03-14 16:51:32 +00:00
```C
int __init early_make_pgtable(unsigned long address)
{
unsigned long physaddr = address - __PAGE_OFFSET;
2018-09-08 10:23:36 +00:00
pmdval_t pmd;
pmd = (physaddr & PMD_MASK) + early_pmd_flags;
return __early_make_pgtable(address, pmd);
}
```
Next we call `__early_make_pgtable` function which is defined in the same file as `early_make_pgtable` function as following:
```C
int __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
{
unsigned long physaddr = address - __PAGE_OFFSET;
2015-03-14 16:51:32 +00:00
pgdval_t pgd, *pgd_p;
2018-09-08 10:23:36 +00:00
p4dval_t p4d, *p4d_p;
2015-03-14 16:51:32 +00:00
pudval_t pud, *pud_p;
2018-09-08 10:23:36 +00:00
pmdval_t *pmd_p;
2015-03-14 16:51:32 +00:00
...
...
...
}
```
It starts from the definition of some variables which have `*val_t` types. All of these types are just:
```C
typedef unsigned long pgdval_t;
```
2018-09-08 10:23:36 +00:00
Also we will operate with the `*_t` (not val) types, for example `pgd_t` and etc... All of these types are defined in the [arch/x86/include/asm/pgtable_types.h](https://github.com/torvalds/linux/blob/master/arch/x86/include/asm/pgtable_types.h) and represent structures like this:
2015-03-14 16:51:32 +00:00
```C
typedef struct { pgdval_t pgd; } pgd_t;
```
2015-03-17 11:43:46 +00:00
For example,
2015-03-14 16:51:32 +00:00
```C
2018-09-08 10:23:36 +00:00
extern pgd_t early_top_pgt[PTRS_PER_PGD];
2015-03-14 16:51:32 +00:00
```
2018-09-08 10:23:36 +00:00
Here `early_top_pgt` presents early top-level page table directory which consists of an array of `pgd_t` types and `pgd` points to low-level page entries.
2015-03-14 16:51:32 +00:00
2018-09-08 10:23:36 +00:00
After we made the check that we have no invalid address, we're getting the address of the Page Global Directory entry which contains `#PF` address and put its value to the `pgd` variable:
2015-03-14 16:51:32 +00:00
```C
2018-09-08 10:23:36 +00:00
pgd_p = &early_top_pgt[pgd_index(address)].pgd;
2015-03-14 16:51:32 +00:00
pgd = *pgd_p;
```
2018-09-08 10:23:36 +00:00
Next we check if five-layer paging is enabled:
```C
if (!pgtable_l5_enabled())
p4d_p = pgd_p;
```
In most cases five-layer paging is not enabled, so `p4d_p` most likely equals to `pgd_p`.
After this we fix up address of the p4d with:
```C
p4d_p += p4d_index(address);
p4d = *p4d_p;
```
In the next step we check `p4d`, if it contains correct p4d entry we put physical address of the p4d entry and put it to the `pud_p` with:
2015-03-14 16:51:32 +00:00
```C
2018-09-08 10:23:36 +00:00
pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
2015-03-14 16:51:32 +00:00
```
where `PTE_PFN_MASK` is a macro:
```C
#define PTE_PFN_MASK ((pteval_t)PHYSICAL_PAGE_MASK)
```
which expands to:
```C
2018-09-08 10:23:36 +00:00
(signed long)(~(PAGE_SIZE-1)) & ((1 << 52) - 1)
2015-03-14 16:51:32 +00:00
```
2018-09-08 10:23:36 +00:00
Here [sign-extension](https://en.wikipedia.org/wiki/Sign_extension) is used. To be more expanded:
2015-03-14 16:51:32 +00:00
```
2018-09-08 10:23:36 +00:00
0b1111111111111111111111111111111111111111111111111111
2015-03-14 16:51:32 +00:00
```
2018-09-08 10:23:36 +00:00
which is 52 bits to mask page frame.
2015-03-14 16:51:32 +00:00
2018-09-08 10:23:36 +00:00
If `p4d` does not contain correct address we check that `next_early_pgt` is not greater than `EARLY_DYNAMIC_PAGE_TABLES` which is `64` and present a fixed number of buffers to set up new page tables on demand. If `next_early_pgt` is greater than `EARLY_DYNAMIC_PAGE_TABLES` we reset page tables and start again. If `next_early_pgt` is less than `EARLY_DYNAMIC_PAGE_TABLES`, we create new page upper directory pointer which points to the current dynamic page table and writes its physical address with the `_KERNPG_TABLE` access rights to the p4d:
2015-03-14 16:51:32 +00:00
```C
if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
reset_early_page_tables();
goto again;
}
pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
for (i = 0; i < PTRS_PER_PUD; i++)
pud_p[i] = 0;
2018-09-08 10:23:36 +00:00
*p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
2015-03-14 16:51:32 +00:00
```
2018-09-08 10:23:36 +00:00
As we did above, we fix up address of the page upper directory with:
2015-03-14 16:51:32 +00:00
```C
pud_p += pud_index(address);
pud = *pud_p;
```
In the next step we do the same actions as we did before, but with the page middle directory. In the end we fix address of the page middle directory which contains maps kernel text+data virtual addresses:
```C
pmd_p[pmd_index(address)] = pmd;
```
2018-09-08 10:23:36 +00:00
After page fault handler finished its work and as result our `early_top_pgt` contains entries which point to the valid addresses.
2015-03-14 16:51:32 +00:00
Conclusion
--------------------------------------------------------------------------------
This is the end of the second part about linux kernel insides. If you have questions or suggestions, ping me in twitter [0xAX](https://twitter.com/0xAX), drop me [email](anotherworldofworld@gmail.com) or just create [issue](https://github.com/0xAX/linux-insides/issues/new). In the next part we will see all steps before kernel entry point - `start_kernel` function.
2015-03-14 16:51:32 +00:00
**Please note that English is not my first language and I am really sorry for any inconvenience. If you found any mistakes please send me PR to [linux-insides](https://github.com/0xAX/linux-insides).**
2015-03-14 16:51:32 +00:00
Links
--------------------------------------------------------------------------------
* [GNU assembly .rept](https://sourceware.org/binutils/docs-2.23/as/Rept.html)
* [APIC](http://en.wikipedia.org/wiki/Advanced_Programmable_Interrupt_Controller)
* [NMI](http://en.wikipedia.org/wiki/Non-maskable_interrupt)
* [Page table](https://en.wikipedia.org/wiki/Page_table)
* [Interrupt handler](https://en.wikipedia.org/wiki/Interrupt_handler)
* [Page Fault](https://en.wikipedia.org/wiki/Page_fault),
2018-03-23 19:06:57 +00:00
* [Previous part](https://0xax.gitbooks.io/linux-insides/content/Initialization/linux-initialization-1.html)