OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Nov 25, 2020 5:39 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: How does linux start up?
PostPosted: Sun May 10, 2020 7:40 am 
Offline
Member
Member

Joined: Sat Feb 08, 2020 11:11 am
Posts: 105
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?


Top
 Profile  
 
 Post subject: Re: How does linux start up?
PostPosted: Sun May 10, 2020 8:59 am 
Offline
Member
Member
User avatar

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 431
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.

_________________
Working on NexBoot, a simple and lightweight GRUB replacement. and NexKe, a stable and fast microkernel. Part of the Nexware Project.


Top
 Profile  
 
 Post subject: Re: How does linux start up?
PostPosted: Sun May 10, 2020 9:08 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3993
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: How does linux start up?
PostPosted: Sun May 10, 2020 9:15 am 
Online
Member
Member
User avatar

Joined: Fri Nov 22, 2019 5:46 am
Posts: 432
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://wiki.osdev.org/User:PeterX


Top
 Profile  
 
 Post subject: Re: How does linux start up?
PostPosted: Sun May 10, 2020 11:34 am 
Offline
Member
Member
User avatar

Joined: Thu Jun 04, 2009 11:12 pm
Posts: 271
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


Top
 Profile  
 
 Post subject: Re: How does linux start up?
PostPosted: Sun May 10, 2020 1:13 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 706
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.

_________________
Thou hast outraged, not insulted me, sir; but for that I ask thee not to beware of Starbuck; thou wouldst but laugh; but let Ahab beware of Ahab; beware of thyself, old man.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group