Looking at the source files, where does Linux start executing?

I would like to read a bit of the source code to try and understand how it all fits together, but cannot see where to start. Which file in the Linux source code is the main file used to compile the kernel?

I was half expecting to find a kernel/main.c, but there are many files under kernel/ and I cannot see which one is the main one? Is it kernel/sys.c?

Asked By: neelsg

||

Try looking at start_kernel() in /init/main.c. This is the function that is called by the boot-loader after it has setup some basic facilities such as memory paging.

For more context: wikipedia Linux startup process.

Answered By: superdesk

The handover from the bootloader to the kernel necessarily involves some architecture-specific considerations such as memory addresses and register use. Consequently, the place to look is in the architecture-specific directories (arch/*). Furthermore, handover from the bootloader involves a precise register usage protocol which is likely to be implemented in assembler. The kernel even has different entry points for different bootloaders on some architectures.

For example, on x86, the entry point is in arch/x86/boot/header.S (I don’t know of other entry points, but I’m not sure that there aren’t any). The real entry point is the _start label at offset 512 in the binary. The 512 bytes before that can be used to make a master boot record for an IBM PC-compatible BIOS (in the old days, a kernel could boot that way, but now this part only displays an error message). The _start label starts some fairly long processing, in real mode, first in assembly and then in main.c. At some point the initialization code switches to protected mode. I think this is the point where decompression happens if the kernel is compressed; then control reaches startup_32 or startup_64 in arch/x86/kernel/head_*.S depending on whether this is a 32-bit or 64-bit kernel. After more assembly, i386_start_kernel in head32.c or x86_64_start_kernel in head64.c is invoked. Finally, the architecture-independent start_kernel function in init/main.c is invoked.

start_kernel is where the kernel starts preparing for the real world. When it starts, there is only a single CPU and some memory (with virtual memory, the MMU is already switched on at that point). The code there sets up memory mappins, initializes all the subsystems, sets up interrupt handlers, starts the scheduler so that threads can be created, starts interacting with peripherals, etc.

The kernel has other entry points than the bootloader: entry points when enabling a core on a multi-core CPU, interrupt handlers, system call handlers, fault handlers, …

Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.