Reputation: 143
I am on the trail of how Linux starts from the call from BIOS up until start_kernel
. To preface my question and give a little background for better understanding, first it's important to understand how memory is laid out. The processor starts in 'real mode' and this means that there is no virtual memory. The addressable space of the chip running the code is typically going to be 1Mb. This is shown below. There is a sector (512 bytes) for the BIOS program, then an MBR (master boot record). There is a bit of space for more MBR/BIOS. Then at 0x001000 starts the boot loader. (tldr; skip down to bottom to see quick questions)
~ ~
| Protected-mode kernel |
100000 +------------------------+
| I/O memory hole |
0A0000 +------------------------+
| Reserved for BIOS | Leave as much as possible unused
~ ~
| Command line | (Can also be below the X+10000 mark)
X+10000 +------------------------+
| Stack/heap | For use by the kernel real-mode code.
X+08000 +------------------------+
| Kernel setup | The kernel real-mode code.
| Kernel boot sector | The kernel legacy boot sector.
X +------------------------+
| Boot loader | <- Boot sector entry point 0000:7C00
001000 +------------------------+
| Reserved for MBR/BIOS |
000800 +------------------------+
| Typically used by MBR |
000600 +------------------------+
| BIOS use only |
000000 +------------------------+
I have been looking at the source tree for a week or two and I have figured most of it out. I'm sure that there is a boot loader image stored in a certain special place that the BIOS loads in when the OS is chosen. This image represents the assembly code defined in the files arch/x86/boot/compressed/head_64.S
and arch/x86/boot/header.S
as well as the other assembly definitions needed to start the routines for starting the kernel.
When the instruction pointer is loaded with the pointer from the MBR the routines start execution and everything loads as needed.
What determines the order of execution of all the routines, assembly, compiled code, and definitions in this image is a certain special file called vmlinux.lds.S
.
If you take a closer look at this file, arch/x86/boot/compressed/vmlinux.lds.S
, it has many definitions and macros it uses to put together an assembly file. One example is ENTRY(startup_32)
on line 16. I have searched for this macro definition in the source tree and I cannot find it. I can't find any of the macros defined in the vmlinux.lds.S
file.
My first assumption is that they are defined in the file included on line 2: #include <asm-generic/vmlinux.lds.h>
.
If you take a look in that file you'll see there is a commented section showing these needed macros, but that This is a sample
.
I have 2 questions:
Is this (vmlinux.lds.S
) assembly executed in compilation time to place the components in the right places or is this the the first set of assembly instructions executed, stored at the location the MBR has a pointer for?
Where are the macros definitions coming from if this vmlinux.lds.S
is executed at run-time?
Upvotes: 2
Views: 12