Does the kernel have a main() function?

In user space programs, main() is the entry point to the program that is called by the libc initialization code when the binary is executed. Kernel code does not have the luxury to rely on libc, as libc itself relies on the kernel syscall interface for memory allocation, I/O, process managements etc.

That said, the equivalent of main() in kernel code is start_kernel(), which is called by the bootloader after having loaded the kernel image, decompressed it into memory and setup essential hardware and memory paging. start_kernel() performs the majority of the system setup and eventually spawns the init process.

The entry point to Linux kernel modules is an init function that is registered with the kernel by calling the module_init() macro. The registered module init function is then called by kernel code through the do_initcalls() function during kernel startup.


The kernel does not have a main function. main is a concept of the C language. The kernel is written in C and assembly. The entry code of the kernel is written by assembly.

The boot sequence is organized as follows:

  1. The BIOS usually loads a boot loader from a boot block device. A popular boot loader right now is grub.
  2. Grub loads a kernel image into ram, possible with an initial root device (initrd). Then code at some address is executed.
  3. The kernel image has some kernel modules, for example: filesystem modules, device drivers. The kernel image use the filesystem module to mount the root filesystem. Now the kernel can load and run all kernel modules from disk.
  4. The kernel runs initialization tasks. For example: traverse PCI bus and find all PCI devices, initialize all device drivers.
  5. Finally the kernel creates process 0 and process 1 (the init process), switches the context of CPU from ring 0 to ring 3, and starts the init process (the process id is 1). Now the kernel boot is finished!
  6. The init program runs all init scripts. All services are started. Shell is called. Users can login.

The main function is a C function. Actually main method is not the entry point of C programs. The C runtime calls many functions before main. GCC has a extend feature: constructors. Functions declared "constructor" are called before main.

For example:

/* This should not be used directly. Use block_init etc. instead. */ 
#define module_init(function, type) \
    static void _attribute__((constructor)) do_qemu_init ## function(void) { \
    register_module_init(function, type); \
} 

This macro is from the qemu project.


There is e.g. a main() function in arch/x86/boot/main.c for preparing the system to switch from the real to the protected mode but other architectures don't have such a code. There is a nice overview how booting of Linux kernel 2.6.x on x86 platform works. It's really worth reading it.

According the document HOWTO do Linux kernel development, the Linux kernel is

a freestanding C environment, with no reliance on the standard C library, so some portions of the C standard are not supported.

what according the C standard BTW means that

It is implementation-defined whether a program in a freestanding environment is required to define a 'main' function.