How does linux start up?
-
- Member
- Posts: 106
- Joined: Sat Feb 08, 2020 11:11 am
- Libera.chat IRC: sunnysideup
How does linux start up?
I've been doing a bit of operating systems development, and want to start looking at the linux kernel source code for inspiration and ideas. While doing OsDev, I realized that the kernel is only called during syscalls/interrupts, and thus I can view the kernel as a set of functions that may or may not be called. However, I want to know how linux handles the startup (grub loads the kernel and passes control to it), how the kernel is initialized and how it switches to user mode / what is the first process it executes. I kind of want to know the 'main()' function of the kernel before it takes the backstage and simply acts as a set of functions to be called by user mode. Is this a good way to start reading the linux source code?
Re: How does linux start up?
I am not sure what exactly the main() function does, but I do know the first app it runs is /sbin/init, /etc/init, /bin/init, or /bin/sh, and which one it finds first, it executes. Most initialization takes place inside of init/main.c, in the start_kernel, kernel_init, and the kernel_init_freeable functions. Hopefully this was of help.
Re: How does linux start up?
There are a number of books about the Linux kernel. Perhaps you should read one as it is impossible to detail the startup of the OS in a forum post.
Re: How does linux start up?
Did you look at this:
https://en.wikipedia.org/wiki/Linux_sta ... rtup_stage
If you are already familiar with OS dev, then you are doing right. The code you are looking for is in the arch directory, but you probably guessed that.
I personally have looked at the boot source code of Linux (x86) and I don't think looking at Linux source code is good for the OS dev beginners or even worse for programming beginners. (I don't know if you are a beginner).
Also google (or duckduckgo) is your friend.
Happy hacking
Peter
https://en.wikipedia.org/wiki/Linux_sta ... rtup_stage
If you are already familiar with OS dev, then you are doing right. The code you are looking for is in the arch directory, but you probably guessed that.
I personally have looked at the boot source code of Linux (x86) and I don't think looking at Linux source code is good for the OS dev beginners or even worse for programming beginners. (I don't know if you are a beginner).
Also google (or duckduckgo) is your friend.
Happy hacking
Peter
Re: How does linux start up?
Hi sunnysideup,
Apart from the resources other people have pointed out, TLDP is a good resource too. Here are the resources that might help you. They are however based on older versions of Linux ( 2.2 , 2.4 etc ). The information provided is good!.
Reading supplemented with hands on experience should help you understand what is going on. Use bochs or qemu to boot linux, set breakpoints where you think is interesting. Trace the flow of a common system process etc.
--Thomas
PS: Short answer look at init code
Apart from the resources other people have pointed out, TLDP is a good resource too. Here are the resources that might help you. They are however based on older versions of Linux ( 2.2 , 2.4 etc ). The information provided is good!.
- The Linux Kernel . This is based on linux 2.03 code base.
- The Linux Kernel Hackers Guide. Also based on an older code base.
- Linux Kernel internals 2.4
- Pocket Linux Guide
Reading supplemented with hands on experience should help you understand what is going on. Use bochs or qemu to boot linux, set breakpoints where you think is interesting. Trace the flow of a common system process etc.
--Thomas
PS: Short answer look at init code
Re: How does linux start up?
High level overview: The code in arch/xxx/boot contains the decompressor. Linux' main image is always compressed and has to be decompressed before it can be started. Sort of like UPX handles it. Decompressor decompresses image, then jumps to its startup code.The code that runs then is somewhere in the relevant arch directory. Where exactly depends on the arch. For x86, the code is in arch/x86/kernel/head*.S. That code will perform arch-specific initialization, and usually initialize the memblocks, too. When the normal kernel environment has been created (for X86: Page tables are initialized as they should be, GS points to a CPU structure), the function kernel_start() is called. That function will initialize the arch-independent blocks in the kernel, initialize tasking, set itself up as the idle task, and then create the init task, before hanging itself into a halting loop. The init task will wait for kernel initialization to be completed, then initialize rootfs and do all that good stuff to locate the actual root disk. Then run /sbin/init from there. Then userspace takes over.sunnysideup wrote:I've been doing a bit of operating systems development, and want to start looking at the linux kernel source code for inspiration and ideas. While doing OsDev, I realized that the kernel is only called during syscalls/interrupts, and thus I can view the kernel as a set of functions that may or may not be called. However, I want to know how linux handles the startup (grub loads the kernel and passes control to it), how the kernel is initialized and how it switches to user mode / what is the first process it executes. I kind of want to know the 'main()' function of the kernel before it takes the backstage and simply acts as a set of functions to be called by user mode. Is this a good way to start reading the linux source code?
Whether this is a good way to read the Linux code depends on what you want to get out of it. If your goal is to understand Linux startup, then yeah, sure. If you want to understand a specific aspect of the kernel, maybe read about that other aspect directly.
Carpe diem!