0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Linux内核结构介绍

CHANBAEK 来源:南山府嵌入式 作者:小秋秋 2023-04-14 11:59 次阅读

说明

通常情况下,Linux内核的结构被认为包含以下11个主要层次:

硬件抽象层

提供了与硬件交互的接口,包括设备驱动程序和中断控制器等。 HAL层的主要功能是隐藏硬件细节,为其他层提供一个硬件无关的接口,使内核能够在不同的硬件平台上运行。

系统调用接口层

提供了与用户空间程序交互的接口,包括系统调用和进程管理等。 SCI层是内核与用户空间之间的接口,它允许用户空间程序向内核发出请求,以获取系统资源或执行某些操作。

进程管理层

管理进程和线程,包括调度、同步和通信等。 PM层负责调度进程和线程的执行,控制进程之间的同步和通信,以及提供进程间共享资源的机制。

进程调度层负责管理系统中的进程,包括进程的创建、销毁、调度等操作。 它是内核的一个核心模块,也是系统性能的关键因素之一。

进程调度层的代码位于 kernel/sched/ 目录下,主要文件包括 sched.ctask.ccgroup_sched.c 等。

内存管理层

管理系统的物理内存和虚拟内存,包括内存分配和释放等。 MM层的主要任务是为进程提供内存,同时保护进程的内存空间不被其他进程破坏。

它是内核的一个核心模块,也是系统性能的关键因素之一。

内存管理层的代码位于 mm/ 目录下,主要文件包括 mmap.cpage_alloc.cvmalloc.c 等。

文件系统层

提供了对文件系统的支持,包括EXT4、FAT32等文件系统的实现。 FS层的主要功能是管理文件和目录,提供对文件的读写访问,并控制文件的权限和安全性。

文件系统层的代码位于 fs/ 目录下,主要文件包括 file.cnamei.csuper.c 等。

网络

实现网络协议栈,包括TCP/IP、UDP等协议的实现。 NET层提供了网络通信的功能,包括数据包的发送和接收、网络连接的管理以及网络安全等。

网络协议栈层的代码位于 net/ 目录下,主要文件包括 core.cipv4/ipv6/ 等。

设备驱动层

提供与硬件设备交互的接口,包括输入/输出设备驱动程序、网络设备驱动程序等。 DD层负责将内核与硬件设备连接起来,允许内核对硬件设备进行访问和控制。

设备驱动层的代码位于 drivers/ 目录下,根据设备类型的不同,代码被组织到不同的子目录中,例如网络设备的驱动代码位于 drivers/net/ 目录下。

中断处理层

处理硬件中断,包括中断的注册、响应和处理等。 IH层负责管理中断处理程序,确保系统能够快速、准确地响应硬件中断。

虚拟化层

实现虚拟化技术,包括KVM、Xen等虚拟化平台的实现。 VIRT层提供了虚拟化的能力,允许在同一物理主机上运行多个虚拟机,并提供虚拟机管理和资源调度的功能。

虚拟化层的代码位于 virt/ 目录下,主要文件包括 virtio.ckvm/xen/

安全子系统层

提供了安全机制和策略的实现,包括SELinux、AppArmor等安全子系统的实现。 SEC层为内核提供了安全的保护机制,确保系统资源的安全性和机密性。

安全模块层的代码位于 security/ 目录下,主要文件包括 security.ccapability.cselinux/ 等。

调试和诊断层

提供了内核调试和诊断工具的支持,包括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;
 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 */

设备驱动层

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);
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 内核
    +关注

    关注

    3

    文章

    1309

    浏览量

    39846
  • 接口
    +关注

    关注

    33

    文章

    7639

    浏览量

    148494
  • Linux
    +关注

    关注

    87

    文章

    10990

    浏览量

    206735
  • 文件
    +关注

    关注

    1

    文章

    540

    浏览量

    24402
  • 线程
    +关注

    关注

    0

    文章

    489

    浏览量

    19495
收藏 人收藏

    评论

    相关推荐

    Linux内核开发工具介绍

    进行嵌入式Linux产品开发,往往需要对内核进行裁剪和定制,以满足嵌入式产品的功能和性能需求。本文介绍几种阅读Linux内核源码的工具和方法
    发表于 12-29 15:20 4552次阅读
    <b class='flag-5'>Linux</b><b class='flag-5'>内核</b>开发工具<b class='flag-5'>介绍</b>

    一文详解Linux内核源码组织结构

    概要:本文内容包含Linux源码树结构分析、Linux Makefile分析、Kconfig文件分析、Linux内核配置选项分析。这些知识是
    的头像 发表于 05-10 19:28 5337次阅读

    带你了解Linux内核体系结构

    图1说明Linux内核的发展简史:图1 Linux内核发展简史图2是Linux系统的层次结构:图
    发表于 08-27 10:31

    Linux内核结构详解

    ,打印警告或错误信息的过程,还有系统的调试例程等等。系统数据结构linux内核的实现中,有一些数据结构使用频度较高,他们是:task_struct.
    发表于 07-11 16:59

    Linux内核源码目录结构

    Linux体系结构Linux内核结构Linux内核
    发表于 12-30 07:22

    简单分析linux内核中的结构体使用方法

    所谓linux驱动编程可以理解为linux内核的编程。既然在内核编程那就必须要符合内核的逻辑和各种规定好的框架。
    发表于 01-19 08:26

    linux 5.4.31为例来介绍一下linux内核目录结构

    ,它是Linux内核的概述和编译命令说明。readme的说明更加针对X86等通用的平台,对于某些特殊的体系结构,可能有些特殊的地方。内核源码很复杂,包含多级目录,形成一个庞大的树状
    发表于 02-16 07:30

    Linux内核教程

    本章学习目标掌握LINUX内核版本的含义理解并掌握进程的概念掌握管道的概念及实现了解内核的数据结构了解LINUX
    发表于 04-10 16:59 0次下载

    《深入Linux内核架构》 莫尔勒著

    电子发烧友为您提供了免费下载,《深入Linux内核架构》一书讨论了Linux内核的概念、结构和实现。内核
    发表于 07-10 11:24 0次下载

    Linux 内核数据结构:位图(Bitmap)

    除了各种链式和树形数据结构Linux内核还提供了位图接口。位图在Linux内核中大量使用。下面的源代码文件包含这些
    发表于 05-14 17:24 3234次阅读

    你知道Linux内核数据结构中双向链表的作用?

    Linux 内核提供一套双向链表的实现,你可以在 include/linux/list.h 中找到。我们以双向链表着手开始介绍 Linux
    发表于 05-14 17:27 1756次阅读

    如何使用Linux内核实现USB驱动程序框架

    Linux内核提供了完整的USB驱动程序框架。USB总线采用树形结构,在一条总线上只能有唯一的主机设备。 Linux内核从主机和设备两个角度
    发表于 11-06 17:59 19次下载
    如何使用<b class='flag-5'>Linux</b><b class='flag-5'>内核</b>实现USB驱动程序框架

    STM32MP157 Linux系统移植开发篇7:Linux内核目录结构详解

    ,它是Linux内核的概述和编译命令说明。readme的说明更加针对X86等通用的平台,对于某些特殊的体系结构,可能有些特殊的地方。内核源码很复杂,包含多级目录,形成一个庞大的树状
    发表于 12-17 18:29 10次下载
    STM32MP157 <b class='flag-5'>Linux</b>系统移植开发篇7:<b class='flag-5'>Linux</b><b class='flag-5'>内核</b>目录<b class='flag-5'>结构</b>详解

    Linux内核的链表数据结构

    Linux内核实现了自己的链表数据结构,它的设计与传统的方式不同,非常巧妙也很通用。
    的头像 发表于 03-24 11:34 593次阅读
    <b class='flag-5'>Linux</b><b class='flag-5'>内核</b>的链表数据<b class='flag-5'>结构</b>

    Linux内核如何使用结构体和函数指针?

    我将结合具体的Linux内核驱动框架代码来展示Linux内核如何使用结构体和函数指针。
    的头像 发表于 09-06 14:17 564次阅读
    <b class='flag-5'>Linux</b><b class='flag-5'>内核</b>如何使用<b class='flag-5'>结构</b>体和函数指针?