LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 01-12-2025, 05:01 PM   #1
Giacomo93
LQ Newbie
 
Registered: Oct 2024
Posts: 7

Rep: Reputation: 0
Linux X86-64 Protected Mode Kernel


As described in the Linux boot protocol for the X86 architecture, the boot loader should load the protected mode kernel at the address 0x100000 of the physical memory.

As I understood reading the code, the protected mode kernel should be made up of:

- The 32 bit code starting from the label startup_32 in the file head_64.S which does the transition to 64 bit mode and the 64 bit code which sets everything up for the kernel decompression(This part, as one can read in the linker script "vmlinux.lds.S" should be in the .head.text section at the very beginning of the protected mode kernel which is compiled starting from the address 0).

- The compressed kernel. This should be an object file with just the ".rodata..compressed" section so it can be linked in the ".rodata..compressed" section of the protected mode kernel, according to the "vmlinux.lds.S" script. Right?

- The kernel decompression code.

I would like to know:

- Where can I find (In the source tree after compilation) each of the object files of the 3 sections of the protected mode kernel I mentioned above? Which are these object file names?

- Where are they put/linked together in the Makefile to get the protected mode kernel that the boot loader can load?

- Which file in the source tree (After compilation) is the protected mode kernel made up of the 3 section I mentioned above?

Thank you very much in advance.

Giacomo

Last edited by Giacomo93; 01-12-2025 at 05:02 PM.
 
Old 01-14-2025, 07:03 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,271

Rep: Reputation: 1314Reputation: 1314Reputation: 1314Reputation: 1314Reputation: 1314Reputation: 1314Reputation: 1314Reputation: 1314Reputation: 1314Reputation: 1314
Since these structures are all architecture-dependent, the code in the kernel source tree is under /arch. Specifically it is in /arch/x86/boot and /arch/x86/boot/compressed. For some reason the kernel developers refer to the compressed, packed vmlinuz as "piggy" and have mkpiggy.c to generate the section information. /arch/x86/boot/compressed/Makefile makes the target in the $(obj) directory which depends on the O= argument to the root Makefile. My Fedora kernel uses something called zstd for compression.
 
Old 01-20-2025, 05:02 PM   #3
Giacomo93
LQ Newbie
 
Registered: Oct 2024
Posts: 7

Original Poster
Rep: Reputation: 0
First of all, thank you for your prompt response.

I had a look at what you wrote and at kernel code and I would have some other questions.

Looking at a compiled kernel for x86-64 architecture I found out that the file bzImage contains:

1) The file setup.bin (Kernel Boot Sector + Setup Code) which has a size of 17980 bytes (At least in my case) starting from offset 0 (setup.bin is at the beginning of bzImage)
2) A padding of 2500 bytes all set to 0 from offset 17980 (0x463C), right after the setup.bin content.
3) The section .head.text of the object file head_64.o which has a size of 716 bytes from offset 20480 (0x5000), right after the padding.
4) The compressed kernel vmlinux.bin.xz from offset 21196, right after the .head.text section.
5) Other code including the kernel decompression code

As described in the Linux/X86 Boot Protocol, the boot loader should load the setup.bin content at an address X of the physical memory and the rest of bzImage
starting from the .head.text section described above at the address 0x100000 of the physical memory. In order to do this it must know the offset of .head.text section
in the bzImage file. The only thing I found is the 1-byte setup_sects field of the kernel header which can be found at offset 0x1F1 of setup.bin/bzImage.
This field should contain the size of the setup code in 512 bytes sectors.

Looking at this field the boot loader should know the end of the setup code (and so the beginning of .head.text section) and can load the .head.text section of head_64.o at the address 0x100000.

The problem I had is that the setup_sects field of the kernel header of my setup.bin file contains the value 39 (0x27), which gives a size of the setup code equal to 19968 bytes(39*512).
This is neither my setup.bin file size (17980) nor the setup.bin + the 2500 bytes of padding which would be 20480 (0x5000).
From my point of view the setup_sects field should contain the number 40 (0x28) which would give a setup code size of 20480 bytes (File setup.bin + the padding), so the boot loader could load the setup.bin + the 0-padding at an address X and the .head.text section at address 0x100000.

If the value of the setup_sects field is 39 (As I found out in my file) the boot loader should believe that the setup code size is 19968 bytes. How can it then know that there are other 512bytes of 0-padding before finding the offset of the .head.text section which must be loaded at address 0x100000?

I have not been able to understand how this works. Could anyone explain it to me?

Thank you very much! I think the kernel is very fascinating but it can also be very difficult to fully understand its code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[kernel-headers-3.2.45-x86-3] OR [kernel-headers-3.2.45_smp-x86-3]? Sefid par Slackware 3 07-24-2013 10:59 AM
Porting real mode OS to protected mode Hosenfeld Programming 1 08-07-2012 03:18 AM
[SOLVED] Displaying the contents of buffers in a protected and non-protected critical section mashhype Programming 5 11-22-2010 10:41 PM
How to implement memory mapping between protected mode and real mode? me_too Linux - Newbie 0 05-13-2008 02:12 AM
Error in switching from protected mode to real mode in Fedora East Linux - General 0 04-02-2008 03:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 07:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration