Boot FreeBSD

| Comments

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

  1. Boot Command starts by initializing
    • CPU
    • Virtual address translations is turned off
    • Disable hardware interrupts
  2. Boot command loads FreeBSD kernel

  3. 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.
  4. System processes are created and executed
  5. User level programs are brought in to execute
  6. System is now ready to run normal applications



Detailed View

  1. When computer is powered on the first thing gets invoked is BIOS.
  2. Startup procedure stored in BIOS, reads a general purpose standalone program in boot disk.
  3. This general purpose standalone program executes FreeBSD boot program.
  4. FreeBSD boot program loads default device and program name to well known memory location.
  5. FreeBSD boot program blocks all interrupts.
  6. FreeBSD boot program disables hardware address-translation facility so that all memory references are to physical memory locations.
  7. Now FreeBSD kernel is started and it will starts the preparations.
  8. FreeBSD kernel initilization process is divided into 3 stages



Assembly language startup

  1. Machine dependent.
  2. Setting up the run-time stack.
  3. Identifying the type of CPU on which the system is executing.
  4. Calculating the amount of physical memory on the machine.
  5. Enabling the virtual-address-translation hardware.
  6. Initializing the memory-management hardware.
  7. Setting up tables for SMP operation, if necessary.
  8. Crafting the hardware context for process 0.
  9. Invoking the initial C-based entry point of the system.
  10. 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.
  11. 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

  1. Static mutex and then dynamic mutexes
  2. Lock manager
  3. 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.
  4. Event handler module to register functions to be called by the kernel when an event occurs.
  5. Kernel module loader - uses event handler to load dynamic kernel modules into the system at boot or run time.
  6. 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.
  7. 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.
  8. 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.
  9. Start rest of kernel threads
    • pagezero
    • pagedaemon, bufdaemon, vnlru etc.
  10. 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.

  1. Init calls fsck to check disk consistency if required and other user level scripts/processes are loaded like /etc/rc.conf, cron, getty.
  2. Getty finally reads a login name and invokes the /usr/bin/login program to complete a login sequence.

Comments