Linux Core Source Reading method

Source: Internet
Author: User

The first time you read the Linux4.0.5 kernel source code, a blank face, its code is not only complex, and large, can not find where to start reading.

typically Linux will have the following directories
  • The Arch subdirectory includes all the core code associated with the architecture. It also has a deeper subdirectory, each representing a supported architecture
  • the Include subdirectory includes most of the include files needed to compile the core. It also has a deeper subdirectory, one for each supported architecture. Include/asm is a soft link to the real include directory required by this architecture, such as include/asm-i386. To change the architecture, you need to edit the core makefile and re-run the Linux core configuration program
  • Init This directory contains the core initialization code, which is a very good starting point for studying how the core works.
  • MM This directory includes all the memory management code. Architecture-related memory management code in arch/*/mm/
  • Drivers system All the device drivers in this directory. They are divided into device driver classes
  • IPC This directory contains code for the core interprocess communication
  • Modules This is just a directory to store the built-in modules.
  • FS All the file system code. is divided into sub-directories, each supporting file system one
  • Kernel the main core code. Similarly, the core code associated with the system is placed in the Arch/*/kernel
  • NET core Network Code
  • Lib This directory to place the core library code. Architecture-related library code in arch/*/lib/
  • Scripts This directory contains scripts (for example, awk and TK scripts) for configuring the core
It's easy to read the source code in the following order.
    1. Core functions (Kernel)
    2. Memory Management (MM)
    3. File System (FS)
    4. Process Communication (IPC)
    5. Network (NET)
    6. System startup and initialization (Init/main and head. S
    7. others, etc.

System startup and initialization (systems startup and initialization)

On an Intel system, the core starts when Loadlin.exe or LILO loads the core into memory and gives it control over it. This part looks arch/i386/kernel/head. S Head. S performs some architecture-related setup work and jumps to the main () routine in init/main.c.

Memory Management (RAM management)

The code is mostly in mm but the architecture-related code is arch/*/mm. Page fault handles code in MM/MEMORY.C, memory-mapped and page-cache code in MM/FILEMAP.C. Buffer cache is implemented in MM/BUFFER.C, and swap caches are in mm/swap_state.c and mm/swapfile.c.

Kernel

Most of the relatively generic code in kernel, and architecture-related code in Arch/*/kernel. The scheduler is in KERNEL/SCHED.C, and the fork code is in KERNEL/FORK.C. Bottom half processing code in include/linux/interrupt.h. TASK_STRUCT data structures can be found in the Include/linux/sched.h

Pci

PCI pseudo driver in Drivers/pci/pci.c, system-wide definition in include/linux/pci.h. Each architecture has some special PCI BIOS code, the Alpha AXP is located in the arch/alpha/kernel/bios32.c

interprocess communication

All in the IPC directory. All system V IPC objects include the Ipc_perm data structure, which can be found in the include/linux/ipc.h. The system V message is implemented in IPC/MSG.C, and the IPC/SHM.C is present in the share, and the semaphore is ipc/sem.c. Pipelines are implemented in IPC/PIPE.C.

Interrupt Handling

The core interrupt handling code is almost always related to microprocessors (usually also the peace table). Intel interrupts the processing code in arch/i386/kernel/irq.c its definition in incude/asm-i386/irq.h.

Device Drivers (unit driver)

Most of the code lines of the Linux core source code are in its device drivers. All Linux device driver source codes are in drivers, but they are further categorized:
    • /block block device drivers such as the IDE (IDE.C). If you want to see how all the devices that might contain the file system are initialized, you can look at the Device_setup () in DRIVERS/BLOCK/GENHD.C. It not only initializes the hard disk, but also initializes the network, because you need the network when you install the NFS file system. Block devices include IDE-based and SCSI devices.  
    • /cdrom all cdrom code for Linux. Special CDROM devices (such as SoundBlaster CDROM) can be found here. Note The IDE CD driver is IDE-CD.C in Drivers/block, and the SCSI CD driver   in DRIVERS/SCSI/SCSI.C;
    • /PCI PCI pseudo driver. This is a good place to observe how the PCI subsystem is mapped and initialized. The Alpha AXP PCI grooming code is also worth viewing in arch/alpha/kernel/bios32.c  
    • /scsi Here you can find not only all Linux supported SCSI device drivers, but also all SCSI code  
    • /net Here you can find network device drivers such as DEC Chip 21040 PCI Ethernet driver in tulip.c  
    • /sound The location of all sound card drivers is  

File Systems (FileSystem)

The source program of the EXT2 file system is in the fs/ext2/subdirectory, and the data structure is defined in include/linux/ext2_fs.h,ext2_fs_i.h and ext2_fs_sb.h. The data structure of the virtual file system is described in Include/linux/fs.h, and the code is fs/*. The Buffer cache and update core daemons are all implemented with FS/BUFFER.C

Network (Internet)

The network code is placed in the net subdirectory, and most of the include files are in Include/net. BSD socket code in NET/SOCKET.C, Ipv4 INET socket code in NET/IPV4/AF_INET.C. The support code for the Universal protocol (including the Sk_buff processing routines) is in Net/core, and the TCP/IP network code is NET/IPV4. Network device drivers in Drivers/net

Modules (module)

The core module code is part of the core, partly in the modules package. The core code is all in KERNEL/MODULES.C, and the data results and core daemon Kerneld messages are in Include/linux/module.h and Include/linux/kerneld.h respectively. You may also want to see the structure of an elf target file in include/linux/elf.h

Reference

Linux kernel source code: http://www.kernel.org/deep distraction Linux kernel source code: Http://oss.org.cn/kernel-book/Linux Related reference:/HTTP Www.oldlinux.org/index_cn.html

Linux Core Source Reading method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.