Kernel initialization. Part 2.

Early interrupt and exception handling

In the previous part we stopped before setting of early interrupt handlers. At this moment we are in the decompressed Linux kernel, we have basic paging structure for early boot and our current goal is to finish early preparation before the main kernel code will start to work.

We already started to do this preparation in the previous first part of this chapter. We continue in this part and will know more about interrupt and exception handling.

Remember that we stopped before following loop:

  1. for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
  2. set_intr_gate(i, early_idt_handler_array[i]);

from the 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.

Some theory

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. An interrupt handler handles and interrupt and transfer control back to the previously stopped task. We can split interrupts on three types:

  • 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;
  • 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:

  1. #define NUM_EXCEPTION_VECTORS 32

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 or through it’s pins. Following table shows 0-31 exceptions:

  1. ----------------------------------------------------------------------------------------------
  2. |Vector|Mnemonic|Description |Type |Error Code|Source |
  3. ----------------------------------------------------------------------------------------------
  4. |0 | #DE |Divide Error |Fault|NO |DIV and IDIV |
  5. |---------------------------------------------------------------------------------------------
  6. |1 | #DB |Reserved |F/T |NO | |
  7. |---------------------------------------------------------------------------------------------
  8. |2 | --- |NMI |INT |NO |external NMI |
  9. |---------------------------------------------------------------------------------------------
  10. |3 | #BP |Breakpoint |Trap |NO |INT 3 |
  11. |---------------------------------------------------------------------------------------------
  12. |4 | #OF |Overflow |Trap |NO |INTO instruction |
  13. |---------------------------------------------------------------------------------------------
  14. |5 | #BR |Bound Range Exceeded|Fault|NO |BOUND instruction |
  15. |---------------------------------------------------------------------------------------------
  16. |6 | #UD |Invalid Opcode |Fault|NO |UD2 instruction |
  17. |---------------------------------------------------------------------------------------------
  18. |7 | #NM |Device Not Available|Fault|NO |Floating point or [F]WAIT |
  19. |---------------------------------------------------------------------------------------------
  20. |8 | #DF |Double Fault |Abort|YES |An instruction which can generate NMI |
  21. |---------------------------------------------------------------------------------------------
  22. |9 | --- |Reserved |Fault|NO | |
  23. |---------------------------------------------------------------------------------------------
  24. |10 | #TS |Invalid TSS |Fault|YES |Task switch or TSS access |
  25. |---------------------------------------------------------------------------------------------
  26. |11 | #NP |Segment Not Present |Fault|NO |Accessing segment register |
  27. |---------------------------------------------------------------------------------------------
  28. |12 | #SS |Stack-Segment Fault |Fault|YES |Stack operations |
  29. |---------------------------------------------------------------------------------------------
  30. |13 | #GP |General Protection |Fault|YES |Memory reference |
  31. |---------------------------------------------------------------------------------------------
  32. |14 | #PF |Page fault |Fault|YES |Memory reference |
  33. |---------------------------------------------------------------------------------------------
  34. |15 | --- |Reserved | |NO | |
  35. |---------------------------------------------------------------------------------------------
  36. |16 | #MF |x87 FPU fp error |Fault|NO |Floating point or [F]Wait |
  37. |---------------------------------------------------------------------------------------------
  38. |17 | #AC |Alignment Check |Fault|YES |Data reference |
  39. |---------------------------------------------------------------------------------------------
  40. |18 | #MC |Machine Check |Abort|NO | |
  41. |---------------------------------------------------------------------------------------------
  42. |19 | #XM |SIMD fp exception |Fault|NO |SSE[2,3] instructions |
  43. |---------------------------------------------------------------------------------------------
  44. |20 | #VE |Virtualization exc. |Fault|NO |EPT violations |
  45. |---------------------------------------------------------------------------------------------
  46. |21-31 | --- |Reserved |INT |NO |External interrupts |
  47. ----------------------------------------------------------------------------------------------

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.

64-bit mode IDT entry has following structure:

  1. 127 96
  2. --------------------------------------------------------------------------------
  3. | |
  4. | Reserved |
  5. | |
  6. --------------------------------------------------------------------------------
  7. 95 64
  8. --------------------------------------------------------------------------------
  9. | |
  10. | Offset 63..32 |
  11. | |
  12. --------------------------------------------------------------------------------
  13. 63 48 47 46 44 42 39 34 32
  14. --------------------------------------------------------------------------------
  15. | | | D | | | | | | |
  16. | Offset 31..16 | P | P | 0 |Type |0 0 0 | 0 | 0 | IST |
  17. | | | L | | | | | | |
  18. --------------------------------------------------------------------------------
  19. 31 16 15 0
  20. --------------------------------------------------------------------------------
  21. | | |
  22. | Segment Selector | Offset 15..0 |
  23. | | |
  24. --------------------------------------------------------------------------------

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.

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

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.

Other bits in the interrupt gate reserved and must be 0. Now let’s look how CPU handles interrupts:

  • CPU save flags register, CS, and instruction pointer on the stack.
  • 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

We stopped at the following point:

  1. for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
  2. set_intr_gate(i, early_idt_handler_array[i]);

Here we call set_intr_gate in the loop, which takes two parameters:

  • Number of an interrupt or vector number;
  • 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 header file contains addresses of the first 32 exception handlers:

  1. #define EARLY_IDT_HANDLER_SIZE 9
  2. #define NUM_EXCEPTION_VECTORS 32
  3. extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];

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.

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 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 macro.

The set_intr_gate macro is defined in the arch/x86/include/asm/desc.h header file and looks:

  1. #define set_intr_gate(n, addr) \
  2. do { \
  3. BUG_ON((unsigned)n > 0xFF); \
  4. _set_gate(n, GATE_INTERRUPT, (void *)addr, 0, 0, \
  5. __KERNEL_CS); \
  6. _trace_set_gate(n, GATE_INTERRUPT, (void *)trace_##addr,\
  7. 0, 0, __KERNEL_CS); \
  8. } while (0)

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, it make a call of the _set_gate function which writes address of an interrupt gate to the IDT:

  1. static inline void _set_gate(int gate, unsigned type, void *addr,
  2. unsigned dpl, unsigned ist, unsigned seg)
  3. {
  4. gate_desc s;
  5. pack_gate(&s, type, (unsigned long)addr, dpl, ist, seg);
  6. write_idt_entry(idt_table, gate, &s);
  7. write_trace_idt_entry(gate, &s);
  8. }

At the start of _set_gate function we can see call of the pack_gate function which fills gate_desc structure with the given values:

  1. static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
  2. unsigned dpl, unsigned ist, unsigned seg)
  3. {
  4. gate->offset_low = PTR_LOW(func);
  5. gate->segment = __KERNEL_CS;
  6. gate->ist = ist;
  7. gate->p = 1;
  8. gate->dpl = dpl;
  9. gate->zero0 = 0;
  10. gate->zero1 = 0;
  11. gate->type = type;
  12. gate->offset_middle = PTR_MIDDLE(func);
  13. gate->offset_high = PTR_HIGH(func);
  14. }

As I mentioned above, we fill gate descriptor in this function. We 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). We are using three following macros to split address on three parts:

  1. #define PTR_LOW(x) ((unsigned long long)(x) & 0xFFFF)
  2. #define PTR_MIDDLE(x) (((unsigned long long)(x) >> 16) & 0xFFFF)
  3. #define PTR_HIGH(x) ((unsigned long long)(x) >> 32)

With the first PTR_LOW macro we get the first 2 bytes of the address, with the second PTR_MIDDLE we get the second 2 bytes of the address and with the third PTR_HIGH macro we get the last 4 bytes of the address. Next we setup the segment selector for interrupt handler, it will be our kernel code segment - __KERNEL_CS. In the next step we fill Interrupt Stack Table and Descriptor Privilege Level (highest privilege level) with zeros. And we set GAT_INTERRUPT type in the end.

Now we have filled IDT entry and we can call native_write_idt_entry function which just copies filled IDT entry to the IDT:

  1. static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate)
  2. {
  3. memcpy(&idt[entry], gate, sizeof(*gate));
  4. }

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:

  1. load_idt((const struct desc_ptr *)&idt_descr);

Where idt_descr is:

  1. struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };

and load_idt just executes lidt instruction:

  1. 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).

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.

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 assembly file:

  1. .globl early_idt_handler_array
  2. early_idt_handlers:
  3. i = 0
  4. .rept NUM_EXCEPTION_VECTORS
  5. .if (EXCEPTION_ERRCODE_MASK >> i) & 1
  6. pushq $0
  7. .endif
  8. pushq $i
  9. jmp early_idt_handler_common
  10. i = i + 1
  11. .fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
  12. .endr

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:

  1. $ objdump -D vmlinux
  2. ...
  3. ...
  4. ...
  5. ffffffff81fe5000 <early_idt_handler_array>:
  6. ffffffff81fe5000: 6a 00 pushq $0x0
  7. ffffffff81fe5002: 6a 00 pushq $0x0
  8. ffffffff81fe5004: e9 17 01 00 00 jmpq ffffffff81fe5120 <early_idt_handler_common>
  9. ffffffff81fe5009: 6a 00 pushq $0x0
  10. ffffffff81fe500b: 6a 01 pushq $0x1
  11. ffffffff81fe500d: e9 0e 01 00 00 jmpq ffffffff81fe5120 <early_idt_handler_common>
  12. ffffffff81fe5012: 6a 00 pushq $0x0
  13. ffffffff81fe5014: 6a 02 pushq $0x2
  14. ...
  15. ...
  16. ...

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:

  1. |--------------------|
  2. | %rflags |
  3. | %cs |
  4. | %rip |
  5. | rsp --> error code |
  6. |--------------------|

Now let’s look on the early_idt_handler_common implementation. It locates in the same arch/x86/kernel/head_64.S assembly file and first of all we can see check for NMI. We don’t need to handle it, so just ignore it in the early_idt_handler_common:

  1. cmpl $2,(%rsp)
  2. je .Lis_nmi

where is_nmi:

  1. is_nmi:
  2. addq $16,%rsp
  3. INTERRUPT_RETURN

drops an error code and vector number from the stack and call INTERRUPT_RETURN which is just expands to the iretq instruction. As we checked the vector number and it is not NMI, we check early_recursion_flag to prevent recursion in the early_idt_handler_common and if it’s correct we save general registers on the stack:

  1. pushq %rax
  2. pushq %rcx
  3. pushq %rdx
  4. pushq %rsi
  5. pushq %rdi
  6. pushq %r8
  7. pushq %r9
  8. pushq %r10
  9. pushq %r11

We need to do it to prevent wrong values of registers when we return from the interrupt handler. After this we check segment selector in the stack:

  1. cmpl $__KERNEL_CS,96(%rsp)
  2. jne 11f

which must be equal to the kernel code segment and if it is not we jump on label 11 which prints PANIC message and makes stack dump.

After the code segment was checked, we check the vector number, and if it is #PF or Page Fault, we put value from the cr2 to the rdi register and call early_make_pgtable (well see it soon):

  1. cmpl $14,72(%rsp)
  2. jnz 10f
  3. GET_CR2_INTO(%rdi)
  4. call early_make_pgtable
  5. andl %eax,%eax
  6. jz 20f

If vector number is not #PF, we restore general purpose registers from the stack:

  1. popq %r11
  2. popq %r10
  3. popq %r9
  4. popq %r8
  5. popq %rdi
  6. popq %rsi
  7. popq %rdx
  8. popq %rcx
  9. popq %rax

and exit from the handler with iret.

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.

You can find implementation of the early_make_pgtable in the arch/x86/kernel/head64.c and takes one parameter - address from the cr2 register, which caused Page Fault. Let’s look on it:

  1. int __init early_make_pgtable(unsigned long address)
  2. {
  3. unsigned long physaddr = address - __PAGE_OFFSET;
  4. unsigned long i;
  5. pgdval_t pgd, *pgd_p;
  6. pudval_t pud, *pud_p;
  7. pmdval_t pmd, *pmd_p;
  8. ...
  9. ...
  10. ...
  11. }

It starts from the definition of some variables which have *val_t types. All of these types are just:

  1. typedef unsigned long pgdval_t;

Also we will operate with the *_t (not val) types, for example pgd_t and etc… All of these types defined in the arch/x86/include/asm/pgtable_types.h and represent structures like this:

  1. typedef struct { pgdval_t pgd; } pgd_t;

For example,

  1. extern pgd_t early_level4_pgt[PTRS_PER_PGD];

Here early_level4_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.

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 it’s value to the pgd variable:

  1. pgd_p = &early_level4_pgt[pgd_index(address)].pgd;
  2. pgd = *pgd_p;

In the next step we check pgd, if it contains correct page global directory entry we put physical address of the page global directory entry and put it to the pud_p with:

  1. pud_p = (pudval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);

where PTE_PFN_MASK is a macro:

  1. #define PTE_PFN_MASK ((pteval_t)PHYSICAL_PAGE_MASK)

which expands to:

  1. (~(PAGE_SIZE-1)) & ((1 << 46) - 1)

or

  1. 0b1111111111111111111111111111111111111111111111

which is 46 bits to mask page frame.

If pgd 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 it’s physical address with the _KERPG_TABLE access rights to the page global directory:

  1. if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
  2. reset_early_page_tables();
  3. goto again;
  4. }
  5. pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
  6. for (i = 0; i < PTRS_PER_PUD; i++)
  7. pud_p[i] = 0;
  8. *pgd_p = (pgdval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;

After this we fix up address of the page upper directory with:

  1. pud_p += pud_index(address);
  2. 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:

  1. pmd = (physaddr & PMD_MASK) + early_pmd_flags;
  2. pmd_p[pmd_index(address)] = pmd;

After page fault handler finished it’s work and as result our early_level4_pgt contains entries which point to the valid addresses.

Conclusion

This is the end of the second part about linux kernel insides. If you have questions or suggestions, ping me in twitter 0xAX, drop me anotherworldofworld@gmail.com">email or just create issue. In the next part we will see all steps before kernel entry point - start_kernel function.

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.