How does linux start up?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sunnysideup
Member
Member
Posts: 106
Joined: Sat Feb 08, 2020 11:11 am
Libera.chat IRC: sunnysideup

How does linux start up?

Post by sunnysideup »

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?
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: How does linux start up?

Post by nexos »

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.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How does linux start up?

Post by iansjack »

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.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: How does linux start up?

Post by PeterX »

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
User avatar
Thomas
Member
Member
Posts: 281
Joined: Thu Jun 04, 2009 11:12 pm

Re: How does linux start up?

Post by Thomas »

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!. . This is more about putting a linux system together form scratch. But if you are starting out, this might be a great start.
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
nullplan
Member
Member
Posts: 1792
Joined: Wed Aug 30, 2017 8:24 am

Re: How does linux start up?

Post by nullplan »

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?
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.

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!
Post Reply