Table of Contents
This post is about the sequence and the amount of work happens under the hood when computer is switched on and the login prompt appears on the screen.
Bird Eye View
- Boot Command starts by initializing
- CPU
- Virtual address translations is turned off
- Disable hardware interrupts
- CPU
-
Boot command loads FreeBSD kernel
- FreeBSD kernel now goes through different stages of hardware and software initialization
- Set up initial stages of CPU
- Setup run time stack
- Setup virtual memory
- Machine dependent initialization
• Setting up mutexes
• Setting virtual memory page tables
• Configure I/O devices - Machine independent initialization
• Mounting root file system
• Initializing myriad system data structures.
- Set up initial stages of CPU
- System processes are created and executed
- User level programs are brought in to execute
- System is now ready to run normal applications
Detailed View
- When computer is powered on the first thing gets invoked is BIOS.
- Startup procedure stored in BIOS, reads a general purpose standalone program in boot disk.
- This general purpose standalone program executes FreeBSD boot program.
- FreeBSD boot program loads default device and program name to well known memory location.
- FreeBSD boot program blocks all interrupts.
- FreeBSD boot program disables hardware address-translation facility so that all memory references are to physical memory locations.
- Now FreeBSD kernel is started and it will starts the preparations.
- FreeBSD kernel initilization process is divided into 3 stages
- Assembly language code to set up the hardware
- Load and initialize kernel modules
- Start system resident processes and execute user level startup scripts
- Assembly language code to set up the hardware
Assembly language startup
- Machine dependent.
- Setting up the run-time stack.
- Identifying the type of CPU on which the system is executing.
- Calculating the amount of physical memory on the machine.
- Enabling the virtual-address-translation hardware.
- Initializing the memory-management hardware.
- Setting up tables for SMP operation, if necessary.
- Crafting the hardware context for process 0.
- Invoking the initial C-based entry point of the system.
- The kernel is usually loaded into contiguous physical memory, so the translation is simply a constant offset that can be saved in an index register.
- Identify CPU. For older CPU the kernel must emulate the missing hardware instructions in software.
Kernel Module Load/Initialization
After the assembly-language code has completed its work, it calls the first kernel routine written in C: the mi_startup() routine. The mi_startup() routine first sorts the list of modules that need to be started and then calls the function routine of each.
FreeBSD set up following services
- Static mutex and then dynamic mutexes
- Lock manager
- Virtual memory system - All memory allocations by the kernel or processes are for virtual addresses that are translated by the memory management hardware into physical addresses. Memory subsystem initialization is to set limits on the resources used by the kernel virtual memory system.
- Event handler module to register functions to be called by the kernel when an event occurs.
- Kernel module loader - uses event handler to load dynamic kernel modules into the system at boot or run time.
- Kernel Thread Initialization
3 processes are created
- Swapper - First process with PID 0.
- Init - Second process with PID 1.
- Idle - Created after Init. Halts the CPU when no work for it to do.
- Swapper - First process with PID 0.
- Device module initialization
- Network interfaces - Setup mbufs i.e. small mbufs and mbufs clusters.
- Interrupt handler - So far hardware interrupts are disabled now kernel set
up interrupt threads to handle interrupts when the system begins to run. - Device file system.
- Network interfaces - Setup mbufs i.e. small mbufs and mbufs clusters.
- Initialize VFS (Virtual file system)
- Initialize vnode subsystem.
- Initialize name cache in VFS.
- Initialize Pathname translation sub system - maps pathname to inodes.
- Initialize names pipes as part of the VFS.
- Initialize clocks.
- Initialize vnode subsystem.
- Start rest of kernel threads
- pagezero
- pagedaemon, bufdaemon, vnlru etc.
- pagezero
- Run scheduler to start scheduling kernel threads and user-level processes.
User Level Initialization
After kernel initialization, at user level init program is executed. This is not the Init process it is init program located in /sbin/init.
- Init calls fsck to check disk consistency if required and other user level scripts/processes are loaded like /etc/rc.conf, cron, getty.
- Getty finally reads a login name and invokes the /usr/bin/login program to complete a login sequence.