說明
通常情況下,Linux內(nèi)核的結(jié)構(gòu)被認(rèn)為包含以下11個(gè)主要層次:
硬件抽象層 :
提供了與硬件交互的接口,包括設(shè)備驅(qū)動程序和中斷控制器等。 HAL層的主要功能是隱藏硬件細(xì)節(jié),為其他層提供一個(gè)硬件無關(guān)的接口,使內(nèi)核能夠在不同的硬件平臺上運(yùn)行。
系統(tǒng)調(diào)用接口層 :
提供了與用戶空間程序交互的接口,包括系統(tǒng)調(diào)用和進(jìn)程管理等。 SCI層是內(nèi)核與用戶空間之間的接口,它允許用戶空間程序向內(nèi)核發(fā)出請求,以獲取系統(tǒng)資源或執(zhí)行某些操作。
進(jìn)程管理層 :
管理進(jìn)程和線程,包括調(diào)度、同步和通信等。 PM層負(fù)責(zé)調(diào)度進(jìn)程和線程的執(zhí)行,控制進(jìn)程之間的同步和通信,以及提供進(jìn)程間共享資源的機(jī)制。
進(jìn)程調(diào)度層負(fù)責(zé)管理系統(tǒng)中的進(jìn)程,包括進(jìn)程的創(chuàng)建、銷毀、調(diào)度等操作。 它是內(nèi)核的一個(gè)核心模塊,也是系統(tǒng)性能的關(guān)鍵因素之一。
進(jìn)程調(diào)度層的代碼位于 kernel/sched/ 目錄下,主要文件包括 sched.c、task.c 和 cgroup_sched.c 等。
內(nèi)存管理層
管理系統(tǒng)的物理內(nèi)存和虛擬內(nèi)存,包括內(nèi)存分配和釋放等。 MM層的主要任務(wù)是為進(jìn)程提供內(nèi)存,同時(shí)保護(hù)進(jìn)程的內(nèi)存空間不被其他進(jìn)程破壞。
它是內(nèi)核的一個(gè)核心模塊,也是系統(tǒng)性能的關(guān)鍵因素之一。
內(nèi)存管理層的代碼位于 mm/ 目錄下,主要文件包括 mmap.c、page_alloc.c 和 vmalloc.c 等。
文件系統(tǒng)層
提供了對文件系統(tǒng)的支持,包括EXT4、FAT32等文件系統(tǒng)的實(shí)現(xiàn)。 FS層的主要功能是管理文件和目錄,提供對文件的讀寫訪問,并控制文件的權(quán)限和安全性。
文件系統(tǒng)層的代碼位于 fs/ 目錄下,主要文件包括 file.c、namei.c 和 super.c 等。
網(wǎng)絡(luò)層
實(shí)現(xiàn)網(wǎng)絡(luò)協(xié)議棧,包括TCP/IP、UDP等協(xié)議的實(shí)現(xiàn)。 NET層提供了網(wǎng)絡(luò)通信的功能,包括數(shù)據(jù)包的發(fā)送和接收、網(wǎng)絡(luò)連接的管理以及網(wǎng)絡(luò)安全等。
網(wǎng)絡(luò)協(xié)議棧層的代碼位于 net/ 目錄下,主要文件包括 core.c、ipv4/ 和 ipv6/ 等。
設(shè)備驅(qū)動層
提供與硬件設(shè)備交互的接口,包括輸入/輸出設(shè)備驅(qū)動程序、網(wǎng)絡(luò)設(shè)備驅(qū)動程序等。 DD層負(fù)責(zé)將內(nèi)核與硬件設(shè)備連接起來,允許內(nèi)核對硬件設(shè)備進(jìn)行訪問和控制。
設(shè)備驅(qū)動層的代碼位于 drivers/ 目錄下,根據(jù)設(shè)備類型的不同,代碼被組織到不同的子目錄中,例如網(wǎng)絡(luò)設(shè)備的驅(qū)動代碼位于 drivers/net/ 目錄下。
中斷處理層
處理硬件中斷,包括中斷的注冊、響應(yīng)和處理等。 IH層負(fù)責(zé)管理中斷處理程序,確保系統(tǒng)能夠快速、準(zhǔn)確地響應(yīng)硬件中斷。
虛擬化層
實(shí)現(xiàn)虛擬化技術(shù),包括KVM、Xen等虛擬化平臺的實(shí)現(xiàn)。 VIRT層提供了虛擬化的能力,允許在同一物理主機(jī)上運(yùn)行多個(gè)虛擬機(jī),并提供虛擬機(jī)管理和資源調(diào)度的功能。
虛擬化層的代碼位于 virt/ 目錄下,主要文件包括 virtio.c、kvm/ 和 xen/ 等
安全子系統(tǒng)層
提供了安全機(jī)制和策略的實(shí)現(xiàn),包括SELinux、AppArmor等安全子系統(tǒng)的實(shí)現(xiàn)。 SEC層為內(nèi)核提供了安全的保護(hù)機(jī)制,確保系統(tǒng)資源的安全性和機(jī)密性。
安全模塊層的代碼位于 security/ 目錄下,主要文件包括 security.c、capability.c 和 selinux/ 等。
調(diào)試和診斷層
提供了內(nèi)核調(diào)試和診斷工具的支持,包括kdump、crash等工具的實(shí)現(xiàn)。 D&D層為開發(fā)人員提供了內(nèi)核調(diào)試和診斷的功能,以便快速識別和修復(fù)內(nèi)核問題。
其實(shí)分層不一定是這樣的,因?yàn)長inux內(nèi)核之間是相互交錯(cuò)的,所以分層不一定,從網(wǎng)上也可以看到有的是分了4層,有的七層不等。 當(dāng)然也可以按照模塊來分,其實(shí)都差不多,可能是我剛接觸有些還不太明白。
硬件抽象層
1// include/linux/platform_device.h
2
3#ifndef _LINUX_PLATFORM_DEVICE_H
4#define _LINUX_PLATFORM_DEVICE_H
5
6struct resource;
7struct platform_device_id;
8struct device;
9
10/**
11 * struct platform_device - platform-level device structure
12 * @name: name of device (mandatory)
13 * @id: id of the device, usually derived from ACPI or device tree
14 * @dev: associated device structure (optional)
15 * @num_resources: number of resources associated with the device
16 * @resource: resource configuration of the device
17 * @dev.parent: parent device (optional)
18 * @driver_override: driver override name (optional)
19 * @dma_mask: dma mask (optional)
20 * @coherent_dma_mask: coherent dma mask (optional)
21 * @id_entry: identity of the device (optional)
22 * @driver_data: driver specific data
23 * @fwnode: firmware node pointer for the device node
24 * @pm_domain: power management domain of the device
25 * @extcon_dev: external connector device associated with platform device
26 *
27 * NOTE: @id_entry is for the use of platform bus only; other bus types should
28 * use their own means to associate a driver with a device.
29 */
30struct platform_device {
31 const char *name;
32 int id;
33 struct device dev;
34 u32 num_resources;
35 struct resource *resource;
36 struct device_node *of_node;
37 struct device *parent;
38 const char *driver_override;
39 const u64 *dma_mask;
40 const u64 *coherent_dma_mask;
41 const struct platform_device_id *id_entry;
42 void *driver_data;
43 struct fwnode_handle *fwnode;
44 struct pm_domain *pm_domain;
45#ifdef CONFIG_EXTCON
46 struct extcon_dev *extcon_dev;
47#endif
48};
49
50/**
51 * platform_device_register() - register a platform-level device
52 * @pdev: platform-level device structure to register
53 *
54 * This function registers a platform-level device with the kernel. The device
55 * will be bound to an appropriate driver if one is available.
56 *
57 * Return: 0 on success, error code on failure.
58 */
59int platform_device_register(struct platform_device *pdev);
60
61/**
62 * platform_device_unregister() - unregister a platform-level device
63 * @pdev: platform-level device structure to unregister
64 *
65 * This function unregisters a platform-level device from the kernel. If the
66 * device was bound to a driver, the driver will be unbound from the device.
67 */
68void platform_device_unregister(struct platform_device *pdev);
69
70#endif /* _LINUX_PLATFORM_DEVICE_H */
設(shè)備驅(qū)動層
1// include/linux/device.h
2
3/**
4 * struct device_driver - The basic device driver structure
5 * @name: Name of the device driver
6 * @bus: Type of bus device is on
7 * @owner: Module owner
8 * @mod_name: Used for built-in modules
9 * @probe: Initializes a given device
10 * @remove: Reverses the effect of probe
11 * @shutdown: Tear down a device prior to system shutdown
12 * @suspend: Prepares a device for power saving mode
13 * @resume: Wake up a device from power saving mode
14 * @groups: Optional sysfs attribute groups
15 * @of_match_table: Matching table for OF devices
16 * @acpi_match_table: Matching table for ACPI devices
17 * @pm: Device power management operations
18 * @probe_type: Type of probe to be used
19 * @suppress_bind_attrs: Suppress the binding/unbinding attributes
20 * @driverfs_dev: Optional driverfs device link
21 * @percpu_ref: Optional percpu reference count
22 * @p: private driver data (of the driver core)
23 * @fwnode: firmware node pointer for the device node
24 * @legacy: if true, a driver bound by OF style match will match legacy platform devices
25 * @no_driver_policy: policy for devices with missing driver
26 * @bus_rescan_devices: pointer to bus specific rescan devices function
27 * @dev_groups: Optional device specific sysfs attribute groups
28 * @sriov_configure: Optional callback for SR-IOV PF driver to configure VFs
29 * @coherent_dma_masks: Optional list of DMA masks this driver supports.
30 * The list should be terminated with a mask of 0.
31 * If the driver sets a dma_mask, it should be
32 * included in this list.
33 */
34struct device_driver {
35 const char *name;
36 struct bus_type *bus;
37 struct module *owner;
38 const char *mod_name; /* used for built-in modules */
39 const struct of_device_id *of_match_table;
40 const struct acpi_device_id *acpi_match_table;
41 int (*probe) (struct device *dev);
42 int (*remove) (struct device *dev);
43 void (*shutdown) (struct device *dev);
44 int (*suspend) (struct device *dev, pm_message_t state);
45 int (*resume) (struct device *dev);
46 const struct attribute_group **groups;
47 const struct dev_pm_ops *pm;
48
49 /* Set of flags used to determine driver state */
50 unsigned int driver_features;
51 enum probe_type probe_type:2;
52 unsigned int suppress_bind_attrs:1;
53 struct driver_private *p;
54
55 /* For driver core */
56 struct device_driver *next;
57 struct driver_attribute *dyn_attrs;
58#ifdef CONFIG_SYSFS
59 struct kobject kobj;
60#endif
61#ifdef CONFIG_DEBUG_DRIVER
62 unsigned long _priv[4];
63#endif
64 struct fwnode_handle *fwnode;
65 unsigned int legacy:1;
66 enum no_driver_policy no_driver_policy:2;
67 void (*bus_rescan_devices)(struct device_driver *drv);
68 const struct attribute_group **dev_groups;
69
70#ifdef CONFIG_PCI_IOV
71 int (*sriov_configure)(struct pci_dev *dev, int num_vfs);
72#endif
73 const u64 *coherent_dma_masks;
74};
75
76/**
77 * driver_register - register a device driver with the system.
78 * @drv: driver structure
79 *
80 * Returns zero on success, or a negative error code.
81 */
82int driver_register(struct device_driver *drv);
83
84/**
85 * driver_unregister - unregister a driver from the driver core.
86 * @drv: driver structure to unregister
87 */
88void driver_unregister(struct device_driver *drv);
-
內(nèi)核
+關(guān)注
關(guān)注
4文章
1467瀏覽量
42872 -
接口
+關(guān)注
關(guān)注
33文章
9519瀏覽量
157019 -
Linux
+關(guān)注
關(guān)注
88文章
11758瀏覽量
219009 -
文件
+關(guān)注
關(guān)注
1文章
594瀏覽量
26054 -
線程
+關(guān)注
關(guān)注
0文章
509瀏覽量
20826
發(fā)布評論請先 登錄
Linux內(nèi)核開發(fā)工具介紹
一文詳解Linux內(nèi)核源碼組織結(jié)構(gòu)
Linux內(nèi)核結(jié)構(gòu)詳解
Linux內(nèi)核源碼目錄結(jié)構(gòu)
以linux 5.4.31為例來介紹一下linux內(nèi)核目錄結(jié)構(gòu)
Linux的內(nèi)核教程
Linux 內(nèi)核數(shù)據(jù)結(jié)構(gòu):位圖(Bitmap)
你知道Linux內(nèi)核數(shù)據(jù)結(jié)構(gòu)中雙向鏈表的作用?
如何使用Linux內(nèi)核實(shí)現(xiàn)USB驅(qū)動程序框架
Linux的內(nèi)核結(jié)構(gòu)詳細(xì)說明
STM32MP157 Linux系統(tǒng)移植開發(fā)篇7:Linux內(nèi)核目錄結(jié)構(gòu)詳解
Linux內(nèi)核的鏈表數(shù)據(jù)結(jié)構(gòu)
Linux內(nèi)核如何使用結(jié)構(gòu)體和函數(shù)指針?
Linux內(nèi)核中使用的數(shù)據(jù)結(jié)構(gòu)
Linux內(nèi)核結(jié)構(gòu)介紹
評論