Skip to content

Comments

boot-4: Improve description for kernel relocation and paging#904

Open
0xAX wants to merge 1 commit intomasterfrom
boot-4/improvements
Open

boot-4: Improve description for kernel relocation and paging#904
0xAX wants to merge 1 commit intomasterfrom
boot-4/improvements

Conversation

@0xAX
Copy link
Owner

@0xAX 0xAX commented Feb 14, 2026

Description

The PR should provide way more better description of the preparation for kernel relocation and some tries to improve paging description.

@0xAX 0xAX requested a review from klaudiagrz as a code owner February 14, 2026 13:56
@github-actions
Copy link

E-books generated for this pull request available at: https://github.com/0xAX/linux-insides/actions/runs/22018627185

@0xAX 0xAX force-pushed the boot-4/improvements branch from 8eef110 to 95bb0d5 Compare February 14, 2026 14:13
@github-actions
Copy link

E-books generated for this pull request available at: https://github.com/0xAX/linux-insides/actions/runs/22018847289

@0xAX 0xAX force-pushed the boot-4/improvements branch from 95bb0d5 to 09af390 Compare February 14, 2026 16:30
@github-actions
Copy link

E-books generated for this pull request available at: https://github.com/0xAX/linux-insides/actions/runs/22020648645

@0xAX 0xAX added the boot label Feb 15, 2026
@0xAX 0xAX force-pushed the boot-4/improvements branch from 09af390 to 2b95b75 Compare February 15, 2026 11:44
@github-actions
Copy link

E-books generated for this pull request available at: https://github.com/0xAX/linux-insides/actions/runs/22035098613

@0xAX 0xAX force-pushed the boot-4/improvements branch from 2b95b75 to 73bd829 Compare February 16, 2026 16:14
Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
@0xAX 0xAX force-pushed the boot-4/improvements branch from 73bd829 to 771204b Compare February 16, 2026 16:16
@github-actions
Copy link

E-books generated for this pull request available at: https://github.com/0xAX/linux-insides/actions/runs/22070033196

@github-actions
Copy link

E-books generated for this pull request available at: https://github.com/0xAX/linux-insides/actions/runs/22070119113

### CPU verification

Before we the kernel can switch to long mode, it needs to check that it runs on the suitable `x86_64` processor. This is done by the next piece of code:
Before the the kernel can switch to long mode, it needs to check that it runs on the suitable `x86_64` processor. This is done by the next piece of code:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Before the the kernel can switch to long mode, it needs to check that it runs on the suitable `x86_64` processor. This is done by the next piece of code:
Before the kernel can switch to long mode, it checks that it runs on a suitable `x86_64` processor by running this piece of code:

```

The `verify_cpu` function defined in the [arch/x86/kernel/verify_cpu.S](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/verify_cpu.S) and executes the [cpuid](https://en.wikipedia.org/wiki/CPUID) instruction to check the details of the processors on which kernel is running on. In our case, the most crucial check is for `long mode` and [SSE](http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) support. and sets the `eax` register to `0` on success and `1` on failure. If the long mode is not supported by the current processor, the kernel jumps to the `no_longmode` label which just stops the CPU with the `hlt` instruction:
The `verify_cpu` function defined in the [arch/x86/kernel/verify_cpu.S](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/verify_cpu.S) and executes the [cpuid](https://en.wikipedia.org/wiki/CPUID) instruction to check the details of the processors on which kernel is running on. In our case, the most crucial check is for `long mode` and [SSE](http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) support. This function returns the result in the `eax` register which value will be `0` on success and `1` on failure. If the long mode is not supported by the current processor, the kernel jumps to the `no_longmode` label which just stops the CPU with the `hlt` instruction:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `verify_cpu` function defined in the [arch/x86/kernel/verify_cpu.S](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/verify_cpu.S) and executes the [cpuid](https://en.wikipedia.org/wiki/CPUID) instruction to check the details of the processors on which kernel is running on. In our case, the most crucial check is for `long mode` and [SSE](http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) support. This function returns the result in the `eax` register which value will be `0` on success and `1` on failure. If the long mode is not supported by the current processor, the kernel jumps to the `no_longmode` label which just stops the CPU with the `hlt` instruction:
The `verify_cpu` function is defined in [arch/x86/kernel/verify_cpu.S](https://github.com/torvalds/linux/blob/master/arch/x86/kernel/verify_cpu.S) and executes the [CPUID](https://en.wikipedia.org/wiki/CPUID) instruction to check the details of the processors on which the kernel is running. In our case, the most crucial check is for long mode and [SSE](http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) support. This function returns the result in the `eax` register. Its value is `0` on success and `1` on failure. If long mode is not supported by the current processor, the kernel jumps to the `no_longmode` label, which stops the CPU with the `hlt` instruction:

- Chunk of compressed kernel code

Obviously, the final decompressed kernel code will be bigger than compressed image. The memory area where the decompressed kernel should locate may overlap with the area where the compressed image is located. In this case, the compressed image could be overwritten during decompression process. To avoid this, the the kernel will copy the compressed part for safe decompression. This is done by the following code:
We may see it looking at the [arch/x86/boot/compressed/vmlinux.lds.S](https://github.com/torvalds/linux/blob/master/arch/x86/boot/compressed/vmlinux.lds.S) linker script:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We may see it looking at the [arch/x86/boot/compressed/vmlinux.lds.S](https://github.com/torvalds/linux/blob/master/arch/x86/boot/compressed/vmlinux.lds.S) linker script:
We can see it looking at the [arch/x86/boot/compressed/vmlinux.lds.S](https://github.com/torvalds/linux/blob/master/arch/x86/boot/compressed/vmlinux.lds.S) linker script:

}
```

There are three sections in the beginning of the linker script above:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
There are three sections in the beginning of the linker script above:
There are three sections at the beginning of the linker script above:

- `.rodaya..compressed` - section with the compressed kernel image
- `.text` - section with the decompressor code

The kernel decompression happens in place. This means that the parts of the decompressed kernel image will overwrite the parts of the compressed image during the decompression process. It may sound dangerous as if the decompressed part will overwrite the decompressor code or the part of the compressed kernel image which is not decompressed yet, this will lead to the code or image corruption.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The kernel decompression happens in place. This means that the parts of the decompressed kernel image will overwrite the parts of the compressed image during the decompression process. It may sound dangerous as if the decompressed part will overwrite the decompressor code or the part of the compressed kernel image which is not decompressed yet, this will lead to the code or image corruption.
The kernel decompression happens in-place, which is the same place where the compressed kernel is. This means that the parts of the decompressed kernel image will overwrite the parts of the compressed image during the decompression process. It may sound dangerous if the decompressed part overwrites the decompressor code or the part of the compressed kernel image that is not decompressed yet, this will lead to code or image corruption.

```

This is the `Long Mode Enable` bit and it is mandatory action to set this bit to enable `64-bit` mode.
This is the `Long Mode Enable` bit and it is mandatory action to set this bit to enable long mode.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is the `Long Mode Enable` bit and it is mandatory action to set this bit to enable long mode.
This is the `Long Mode Enable` bit, and it is mandatory to set this bit to enable long mode.

This is the `Long Mode Enable` bit and it is mandatory action to set this bit to enable `64-bit` mode.
This is the `Long Mode Enable` bit and it is mandatory action to set this bit to enable long mode.

In the next step, we may see the preparation of the jump on the long mode entrypoint. To do this jump, the kernel stores the base address of the kernel segment code along with the address of the long mode entrypoint on the stack:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the next step, we may see the preparation of the jump on the long mode entrypoint. To do this jump, the kernel stores the base address of the kernel segment code along with the address of the long mode entrypoint on the stack:
In the next step, we can see the preparation for the jump on the long mode entrypoint. To do this jump, the kernel stores the base address of the kernel segment code along with the address of the long mode entrypoint on the stack:

```

Everything is ready. Since our stack contains the base of the kernel code segment and the address of the entrypoint, kernel executes the last instruction in protected mode:
Since the stack contains the base of the kernel code segment and the address of the entrypoint, kernel executes the last instruction in protected mode:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Since the stack contains the base of the kernel code segment and the address of the entrypoint, kernel executes the last instruction in protected mode:
Since the stack contains the base of the kernel code segment and the address of the entrypoint, the kernel executes the last instruction in protected mode:

```

The CPU extracts the address of the `startup_64` from the stack and jumps there:
The CPU extracts the address of the `startup_64` which is the long mode entrypoint from the stack and jumps there:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The CPU extracts the address of the `startup_64` which is the long mode entrypoint from the stack and jumps there:
The CPU extracts the address of `startup_64`, which is the long mode entrypoint from the stack, and jumps there:


The Linux kernel now in 64-bit mode 🎉

The Linux kernel now is in 64-bit mode 🎉
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The Linux kernel now is in 64-bit mode 🎉
The Linux kernel is now in 64-bit mode! 🎉

Copy link
Collaborator

@klaudiagrz klaudiagrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants