Linux内核结构介绍

说明
通常情况下,linux内核的结构被认为包含以下11个主要层次:
硬件抽象层 :
提供了与硬件交互的接口,包括设备驱动程序和中断控制器等。 hal层的主要功能是隐藏硬件细节,为其他层提供一个硬件无关的接口,使内核能够在不同的硬件平台上运行。
系统调用接口层 :
提供了与用户空间程序交互的接口,包括系统调用和进程管理等。 sci层是内核与用户空间之间的接口,它允许用户空间程序向内核发出请求,以获取系统资源或执行某些操作。
进程管理层 :
管理进程和线程,包括调度、同步和通信等。 pm层负责调度进程和线程的执行,控制进程之间的同步和通信,以及提供进程间共享资源的机制。
进程调度层负责管理系统中的进程,包括进程的创建、销毁、调度等操作。 它是内核的一个核心模块,也是系统性能的关键因素之一。
进程调度层的代码位于 kernel/sched/ 目录下,主要文件包括 sched.c、task.c 和 cgroup_sched.c 等。
内存管理层
管理系统的物理内存和虚拟内存,包括内存分配和释放等。 mm层的主要任务是为进程提供内存,同时保护进程的内存空间不被其他进程破坏。
它是内核的一个核心模块,也是系统性能的关键因素之一。
内存管理层的代码位于 mm/ 目录下,主要文件包括 mmap.c、page_alloc.c 和 vmalloc.c 等。
文件系统层
提供了对文件系统的支持,包括ext4、fat32等文件系统的实现。 fs层的主要功能是管理文件和目录,提供对文件的读写访问,并控制文件的权限和安全性。
文件系统层的代码位于 fs/ 目录下,主要文件包括 file.c、namei.c 和 super.c 等。
网络层
实现网络协议栈,包括tcp/ip、udp等协议的实现。 net层提供了网络通信的功能,包括数据包的发送和接收、网络连接的管理以及网络安全等。
网络协议栈层的代码位于 net/ 目录下,主要文件包括 core.c、ipv4/ 和 ipv6/ 等。
设备驱动层
提供与硬件设备交互的接口,包括输入/输出设备驱动程序、网络设备驱动程序等。 dd层负责将内核与硬件设备连接起来,允许内核对硬件设备进行访问和控制。
设备驱动层的代码位于 drivers/ 目录下,根据设备类型的不同,代码被组织到不同的子目录中,例如网络设备的驱动代码位于 drivers/net/ 目录下。
中断处理层
处理硬件中断,包括中断的注册、响应和处理等。 ih层负责管理中断处理程序,确保系统能够快速、准确地响应硬件中断。
虚拟化层
实现虚拟化技术,包括kvm、xen等虚拟化平台的实现。 virt层提供了虚拟化的能力,允许在同一物理主机上运行多个虚拟机,并提供虚拟机管理和资源调度的功能。
虚拟化层的代码位于 virt/ 目录下,主要文件包括 virtio.c、kvm/ 和 xen/ 等
安全子系统层
提供了安全机制和策略的实现,包括selinux、apparmor等安全子系统的实现。 sec层为内核提供了安全的保护机制,确保系统资源的安全性和机密性。
安全模块层的代码位于 security/ 目录下,主要文件包括 security.c、capability.c 和 selinux/ 等。
调试和诊断层
提供了内核调试和诊断工具的支持,包括kdump、crash等工具的实现。 d&d层为开发人员提供了内核调试和诊断的功能,以便快速识别和修复内核问题。
其实分层不一定是这样的,因为linux内核之间是相互交错的,所以分层不一定,从网上也可以看到有的是分了4层,有的七层不等。 当然也可以按照模块来分,其实都差不多,可能是我刚接触有些还不太明白。
硬件抽象层
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; 910/**11 * struct platform_device - platform-level device structure12 * @name: name of device (mandatory)13 * @id: id of the device, usually derived from acpi or device tree14 * @dev: associated device structure (optional)15 * @num_resources: number of resources associated with the device16 * @resource: resource configuration of the device17 * @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 data23 * @fwnode: firmware node pointer for the device node24 * @pm_domain: power management domain of the device25 * @extcon_dev: external connector device associated with platform device26 *27 * note: @id_entry is for the use of platform bus only; other bus types should28 * 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_extcon46 struct extcon_dev *extcon_dev;47#endif48};4950/**51 * platform_device_register() - register a platform-level device52 * @pdev: platform-level device structure to register53 *54 * this function registers a platform-level device with the kernel. the device55 * 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);6061/**62 * platform_device_unregister() - unregister a platform-level device63 * @pdev: platform-level device structure to unregister64 *65 * this function unregisters a platform-level device from the kernel. if the66 * device was bound to a driver, the driver will be unbound from the device.67 */68void platform_device_unregister(struct platform_device *pdev);6970#endif /* _linux_platform_device_h */设备驱动层
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 device10 * @remove: reverses the effect of probe11 * @shutdown: tear down a device prior to system shutdown12 * @suspend: prepares a device for power saving mode13 * @resume: wake up a device from power saving mode14 * @groups: optional sysfs attribute groups15 * @of_match_table: matching table for of devices16 * @acpi_match_table: matching table for acpi devices17 * @pm: device power management operations18 * @probe_type: type of probe to be used19 * @suppress_bind_attrs: suppress the binding/unbinding attributes20 * @driverfs_dev: optional driverfs device link21 * @percpu_ref: optional percpu reference count22 * @p: private driver data (of the driver core)23 * @fwnode: firmware node pointer for the device node24 * @legacy: if true, a driver bound by of style match will match legacy platform devices25 * @no_driver_policy: policy for devices with missing driver26 * @bus_rescan_devices: pointer to bus specific rescan devices function27 * @dev_groups: optional device specific sysfs attribute groups28 * @sriov_configure: optional callback for sr-iov pf driver to configure vfs29 * @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 be32 * 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;4849 /* 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;5455 /* for driver core */56 struct device_driver *next;57 struct driver_attribute *dyn_attrs;58#ifdef config_sysfs59 struct kobject kobj;60#endif61#ifdef config_debug_driver62 unsigned long _priv[4];63#endif64 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;6970#ifdef config_pci_iov71 int (*sriov_configure)(struct pci_dev *dev, int num_vfs);72#endif73 const u64 *coherent_dma_masks;74};7576/**77 * driver_register - register a device driver with the system.78 * @drv: driver structure79 *80 * returns zero on success, or a negative error code.81 */82int driver_register(struct device_driver *drv);8384/**85 * driver_unregister - unregister a driver from the driver core.86 * @drv: driver structure to unregister87 */88void driver_unregister(struct device_driver *drv);

YunOS 2016目标破亿,助力小企业打造大品牌
Marvell成后起之秀:聆听需求是芯片商制胜之道
噪声系数测量—超量程方法
宇树科技工业级机器狗去宝钢“上班”了
为何联想会选择在这么短时间内提交就撤回申请
Linux内核结构介绍
加西贝拉即将推出的新产品采用意法半导体的先进电源技术
如何快速检查电动机控制电路_检查线路步骤及方法
招投标资讯|2022年联通数科物联网事业部南京南部新城智慧灯杆建设(二期)公开比选项目
关于电声配件行业防尘防水透声膜的解决方案
从AI玩具到AI工具,钉钉大模型改造的200天
MB39C601:恒流AC-DC LED驱动器快速入门及特性介绍
三星s8/华硕手机还在在围绕骁龙835首发做文章?小米6都小批量了!
2011-2016年流程产业的以太网节点有望增长近一倍
为什么CPU时钟频率在过去5年里没有增加?
新一代可验证安全区块链,可为客户提供有效、可靠的数据信息保护
扁线的壁垒、定价模式和发展空间测算
区块链与其他技术的融合还存在哪些挑战
PLC工控物联网平台要上5G吗?有什么功能?
新疆电网用电负荷达到3252万千瓦 创下历史最高纪录