Introduction

Features

Two modes in Linux

What’s a kernel

Kernel design goal

performance, stability, capability, security and protection, portability, extensibility

Linux source tree (directories)

linux/arch

linux/drivers

linux/fs

linux/include

linux/kernel

linux/lib

linux/mm

linux/scripts

scripts for:

Summary

Booting

How computer startup?

Booting sequence

BIOS (Basic Input/Output System)

MBR (Master Boot Record)

MBR

Boot loader

Kernel Image

Task of kernel

Init process

Runlevels

rc#.d files

init.d

实模式下的系统初始化

保护模式下的系统初始化

Q&A

  1. Q: 在i386中,内核可执行代码在内存中的首地址是否可随意选择?
    A: 从原理上讲是可以的,但实际上要考虑许多因素。例如微机内存的低端1MB的地址空间是不连续的。所以要把内核代码放在低端就要看是否能放的下。如果内核代码模块太大就不能将内核放在低端。
  2. Q: 主引导扇区位于硬盘的什么位置,如果一个硬盘的主引导扇区有故障此硬盘是否还可以使用?
    A: 主引导扇区位于硬盘的0面0道1扇区。一般来讲如果一个硬盘的主引导扇区有故障,此硬盘虽然可以使用,但不能作为引导盘使用了,因为它的主引导扇区不能读出内容。

/proc File System and Kernel Module Programming

What is a kernel module?

current kernel modules

cd /lib/modules/2.6.32-22-generic/
find . -name "*.ko"

current loaded modules

lsmod or cat /proc/modules

/proc

Tweak kernel parameters

Details of some files in /proc

The numerical named directories

A typical process directory

Other subdirectories in /proc

/proc/sys subdirectories

Advantages and disadvantages of the /proc file system

/proc file system entries

Tips

Process Management

Processes, Lightweight Processes and Threads

Process descriptor

task_struct data structure:

Process state

Identifying a process

The process list

Parenthood relationships among processes

Pidhash table and chained lists

PID

pids field of the process descriptor: the pid data structure

How processes are organized

Process switch, task switch, context switch

Performing the process switch

Task State Segment

TSS: a specific segment type in x86 architecture to store hardware contexts

Creating processes

clone(), fork() and vfork() system calls

Kernel threads

Destrying processes

Scheduling policy

Process preemption

How long should a quantum last

Scheduling algorithm

Scheduling of conventional processes

Static priority

Dynamic priority and average sleep time

Determine the status of a process

To determine whether a process is considered to be interactive or batch

Active and expired processes

Scheduling of real-time processes

Implementation Support of Scheduling

Data structures used by the scheduler

Functions used by the scheduler

Runqueue balancing in multiprocessor systems

Scheduling domains

A set of CPUs whose workloads are kept balanced by the kernel

Completely Fair Scheduler (CFS)

Kernel synchronization

Kernel control paths

Kernel preemption

When synchronization is necessary

When synchronization is not necessary

Per-CPU variables

Memory Barriers

Spin locks

Read/Write Spin Locks

Seqlock

Read-Copy Update

Semaphores

Read/Write semaphores

Completions

Local interrupt disabling

Disabling/Enabling deferrable functions

Synchronizing accesses to kernel data structures

Examples of race condition

Symmetric multiprocessing (SMP)

Traditional View

Categories of computer systems

Symmetric multiprocessing

SMP

Linux Support for SMP

NUMA

SMP 系统启动过程

  1. BIOS初始化:屏蔽AP(Application Processor),建立系统配置表
  2. MBR里面的引导程序(LILO,GRUB等)将内核载入内存
  3. 执行 head.S中的 startup_32函数,最后调用 start_kernel
  4. 执行 start_kernel,这个函数相当于应用程序里面的 main 函数
  5. start_kernel 进行一系列的初始化工作,最后将执行:
  6. 1号进程 init 完成剩下的工作

reschedule_idle 工作过程

  1. 先检查p进程上一次运行的cpu是否空闲,如果空闲,这是最好的cpu,直接返回。
  2. 找一个合适的cpu,查看SMP中的每个CPU上运行的进程,与p进程相比的抢先权,把具有最高的抢先权值的进程记录在target_task中,该进程运行的cpu为最合适的CPU。
  3. 如target_task为空,说明没有找到合适的cpu,直接返回。
  4. 如果target_task不为空,则说明找到了合适的cpu,因此将target_task->need_resched置为1,如果运行target_task的cpu不是当前运行的cpu,则向运行target_task的cpu发送一个IPI中断,让它重新调度。

Memory Management - Addressing

Memory Addresses

Virtual File System (VFS)

Role of VFS

Common file system interface

Terminology

Physical file representation

The common file model

VFS operations

VFS Data Structures

Superblock object

Inode object

Power Management - From Linux Kernel to Android

Linux Power Management

Android Power Management

Android PM State Machine

System Sleep

Battery Service