diff --git a/images/kernel/i2c-arch.png b/images/kernel/i2c-arch.png new file mode 100644 index 00000000..c19eead7 Binary files /dev/null and b/images/kernel/i2c-arch.png differ diff --git a/images/kernel/i2c-bus.png b/images/kernel/i2c-bus.png new file mode 100644 index 00000000..da1b2793 Binary files /dev/null and b/images/kernel/i2c-bus.png differ diff --git a/images/kernel/i2c-signal01.png b/images/kernel/i2c-signal01.png new file mode 100644 index 00000000..72586eaa Binary files /dev/null and b/images/kernel/i2c-signal01.png differ diff --git a/images/kernel/i2c-write.png b/images/kernel/i2c-write.png new file mode 100644 index 00000000..ac83c58c Binary files /dev/null and b/images/kernel/i2c-write.png differ diff --git a/linux/drivers/gpio/index.html b/linux/drivers/gpio/index.html index 868c4227..a2b01631 100644 --- a/linux/drivers/gpio/index.html +++ b/linux/drivers/gpio/index.html @@ -2311,17 +2311,17 @@

GPIO子系统

GPIO全称“General Purpose Input/Output”,通用输入输出。GPIO可能是芯片自带的,也可能通过I2C、SPI接口扩展。

设备树描述

-
/*rk3568.dtsi*/
-gpio0: gpio@fdd60000 {
-            compatible = "rockchip,gpio-bank";
-            reg = <0x0 0xfdd60000 0x0 0x100>;
-            interrupts = <GIC_SPI 33 IRQ_TYPE_LEVEL_HIGH>;
-            clocks = <&pmucru PCLK_GPIO0>;
-            gpio-controller;
-            #gpio-cells = <2>;
-            interrupt-controller;
-            #interrupt-cells = <2>;
-};
+
/*rk3568.dtsi*/
+gpio0: gpio@fdd60000 {
+    compatible = "rockchip,gpio-bank";
+    reg = <0x0 0xfdd60000 0x0 0x100>;
+    interrupts = <GIC_SPI 33 IRQ_TYPE_LEVEL_HIGH>;
+    clocks = <&pmucru PCLK_GPIO0>;
+    gpio-controller;
+    #gpio-cells = <2>;
+    interrupt-controller;
+    #interrupt-cells = <2>;
+};
 

数据结构

struct gpio_device {
diff --git a/linux/drivers/i2c/index.html b/linux/drivers/i2c/index.html
index a0c1abeb..dc03354f 100644
--- a/linux/drivers/i2c/index.html
+++ b/linux/drivers/i2c/index.html
@@ -1038,6 +1038,17 @@
         
       
       
+        
+      
       
         
   
@@ -1048,6 +1059,61 @@
 
       
       
+        
+
+
+      
     
   
 
@@ -2182,6 +2248,50 @@
     
   
   
+    
+    
+  
 
                   
@@ -2199,6 +2309,235 @@

I2C子系统

+

I2C协议

+

i2c物理总线示意图如下所示:

+

I2C物理总线

+

i2c支持主从模式,其中一个主设备、多个从设备,各设备地址独立。主设备负责调度总线,决定某个事件和其中一个从设备通信,其他从设备要想通信只能等待。当SCL与SDA同为高电平时,为空闲态。

+

i2c物理总线使用两条总线线路:

+ +

通信特征:

+ +

i2c的通信过程:

+
    +
  1. 开始信号(S):SDA线从高电平到低电平跳变,同时SCL线保持高电平。
  2. +
  3. 发送地址和读/写位:主设备通过SDA线发送一个7位设备地址,后面跟着一个读/写位。
  4. +
  5. 响应信号(ACK):每一次传输必须伴有一个响应信号,在SCL为高时,通过拉低SDA并保持低来实现。如果从设备忙,它可以使SCL保持在低电平,这会强制使主设备进入等待状态。当从设备空闲后,并且释放时钟线,原来的数据传输才会继续。
  6. +
  7. 数据传输:SDA上传输的数据必须在SCL为高电平期间保持稳定,SDA上的数据只能在SCL为低电平时变化。
  8. +
  9. 停止信号(P):数据传输结束时,SDA线从低电平到高电平的跳变,同时SCL线保持高电平,表示通信结束。
  10. +
+

对于第4点解释一下:

+

SDA上传输的数据必须在SCL为高电平期间保持稳定:

+ +

SDA上的数据只能在SCL为低电平时变化:

+ +

起始和停止

+

连续字节的写入如下图所示:

+

连续字节写入

+

设备树描述

+
i2c0: i2c@fdd40000 {
+    compatible = "rockchip,rk3568-i2c", "rockchip,rk3399-i2c";
+    reg = <0x0 0xfdd40000 0x0 0x1000>;
+    interrupts = <GIC_SPI 46 IRQ_TYPE_LEVEL_HIGH>;
+    clocks = <&pmucru CLK_I2C0>, <&pmucru PCLK_I2C0>;
+    clock-names = "i2c", "pclk";
+    pinctrl-0 = <&i2c0_xfer>;
+    pinctrl-names = "default";
+    #address-cells = <1>;
+    #size-cells = <0>;
+    status = "disabled";
+};
+
+

数据结构

+

i2c框架

+ +

i2c驱动中最核心的四个数据结构:

+ +

struct i2c_adapter对应一个i2c controller,所有的设备驱动都需要经过struct i2c_adapter对象的处理才能与物理设备通信:

+
struct i2c_adapter {
+    struct module *owner;
+    unsigned int class;       /* classes to allow probing for */
+    const struct i2c_algorithm *algo; /* the algorithm to access the bus */
+    void *algo_data;
+
+    /* data fields that are valid for all devices   */
+    const struct i2c_lock_operations *lock_ops;
+    struct rt_mutex bus_lock;
+    struct rt_mutex mux_lock;
+
+    int timeout;            /* in jiffies */
+    int retries;
+    struct device dev;      /* the adapter device */
+    unsigned long locked_flags; /* owned by the I2C core */
+#define I2C_ALF_IS_SUSPENDED        0
+#define I2C_ALF_SUSPEND_REPORTED    1
+
+    int nr;
+    char name[48];
+    struct completion dev_released;
+
+    struct mutex userspace_clients_lock;
+    struct list_head userspace_clients;
+
+    struct i2c_bus_recovery_info *bus_recovery_info;
+    const struct i2c_adapter_quirks *quirks;
+
+    struct irq_domain *host_notify_domain;
+    struct regulator *bus_regulator;
+};
+
+

struct i2c_algorithm定义了i2c控制器与i2c设备之间通信的算法,最重要的成员是master_xfer()函数,用来将数据发送到i2c controller: +

struct i2c_algorithm {
+    /*
+     * If an adapter algorithm can't do I2C-level access, set master_xfer
+     * to NULL. If an adapter algorithm can do SMBus access, set
+     * smbus_xfer. If set to NULL, the SMBus protocol is simulated
+     * using common I2C messages.
+     *
+     * master_xfer should return the number of messages successfully
+     * processed, or a negative value on error
+     */
+    int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
+               int num);
+    int (*master_xfer_atomic)(struct i2c_adapter *adap,
+                   struct i2c_msg *msgs, int num);
+    int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,
+              unsigned short flags, char read_write,
+              u8 command, int size, union i2c_smbus_data *data);
+    int (*smbus_xfer_atomic)(struct i2c_adapter *adap, u16 addr,
+                 unsigned short flags, char read_write,
+                 u8 command, int size, union i2c_smbus_data *data);
+
+    /* To determine what the adapter supports */
+    u32 (*functionality)(struct i2c_adapter *adap);
+
+#if IS_ENABLED(CONFIG_I2C_SLAVE)
+    int (*reg_slave)(struct i2c_client *client);
+    int (*unreg_slave)(struct i2c_client *client);
+#endif
+};
+

+
+

master_xfer:作为主设备时的发送函数

+

smbus_xfer:作为从设备时的接收函数

+
+

struct i2c_client表示连接到i2c总线上的设备,是具体硬件设备的抽象:

+
struct i2c_client {
+    unsigned short flags;       /* div., see below      */
+    unsigned short addr;        /* chip address - NOTE: 7bit    */
+    char name[I2C_NAME_SIZE];
+    struct i2c_adapter *adapter;    /* the adapter we sit on    */
+    struct device dev;      /* the device structure     */
+    int init_irq;           /* irq set at initialization    */
+    int irq;            /* irq issued by device     */
+    struct list_head detected;
+#if IS_ENABLED(CONFIG_I2C_SLAVE)
+    i2c_slave_cb_t slave_cb;    /* callback for slave mode  */
+#endif
+    void *devres_group_id;      /* ID of probe devres group */
+};
+
+

struct i2c_driver表示i2c设备所对应的驱动程序:

+
struct i2c_driver {
+    unsigned int class;
+
+    /* Standard driver model interfaces */
+    int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);
+    int (*remove)(struct i2c_client *client);
+
+    /* New driver model interface to aid the seamless removal of the
+     * current probe()'s, more commonly unused than used second parameter.
+     */
+    int (*probe_new)(struct i2c_client *client);
+
+    /* driver model interfaces that don't relate to enumeration  */
+    void (*shutdown)(struct i2c_client *client);
+
+    /* Alert callback, for example for the SMBus alert protocol.
+     * The format and meaning of the data value depends on the protocol.
+     * For the SMBus alert protocol, there is a single bit of data passed
+     * as the alert response's low bit ("event flag").
+     * For the SMBus Host Notify protocol, the data corresponds to the
+     * 16-bit payload data reported by the slave device acting as master.
+     */
+    void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,
+              unsigned int data);
+
+    /* a ioctl like command that can be used to perform specific functions
+     * with the device.
+     */
+    int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
+
+    struct device_driver driver;
+    const struct i2c_device_id *id_table;
+
+    /* Device detection callback for automatic device creation */
+    int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
+    const unsigned short *address_list;
+    struct list_head clients;
+};
+
+

平台设备驱动一章中我们描述了平台总线的概念,i2c总线有类似的功能,用来管理i2c设备和i2c驱动的匹配和删除操作:

+
/*drivers/i2c/i2c-core.c*/
+struct bus_type i2c_bus_type = {
+    .name       = "i2c",
+    .match      = i2c_device_match,
+    .probe      = i2c_device_probe,
+    .remove     = i2c_device_remove,
+    .shutdown   = i2c_device_shutdown,
+};
+
+

多个设备可以挂在同一个i2c总线上,i2c总线驱动由芯片厂商提供。

+

struct i2c_msg结构体表示i2c传输的消息。

+
struct i2c_msg {
+    __u16 addr;
+    __u16 flags;
+    ...
+    __u16 len;
+    __u8 *buf;
+};
+
+
+

addr:从设备地址

+

flags:标志位

+

len:传输数据的长度

+

buf:传输数据缓冲区

+
+

I2C函数接口

+

向内核注册/注销一个i2c_adapter: +

int i2c_add_adapter(struct i2c_adapter *adap)
+int i2c_del_adapter(struct i2c_adapter *adap)
+

+

注册/注销一个i2c驱动: +

int i2c_add_driver(struct i2c_driver *driver)
+int i2c_del_driver(struct i2c_driver *driver)
+

+

传输数据: +

int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
+

diff --git a/linux/drivers/pinctrl/index.html b/linux/drivers/pinctrl/index.html index dd4cda1c..fc492b8f 100644 --- a/linux/drivers/pinctrl/index.html +++ b/linux/drivers/pinctrl/index.html @@ -2450,43 +2450,43 @@

源文件列表设备树描述

引脚配置的设备树描述如下:

-
/*s5pv210-pinctrl.dtsi*/
-&pinctrl0 {
-    gpa0: gpa0 {
-        gpio-controller;        /*这是一个GPIO控制器*/
-        #gpio-cells = <2>;      /*需要2个32位的数来描述这个GPIO*/
+
/*s5pv210-pinctrl.dtsi*/
+&pinctrl0 {
+    gpa0: gpa0 {
+        gpio-controller;        /*这是一个GPIO控制器*/
+        #gpio-cells = <2>;      /*需要2个32位的数来描述这个GPIO*/
 
-        interrupt-controller;   /*这是一个中断控制器*/
-        #interrupt-cells = <2>;
-    };
+        interrupt-controller;   /*这是一个中断控制器*/
+        #interrupt-cells = <2>;
+    };
 
-    gpa1: gpa1 {
-        gpio-controller;
-        #gpio-cells = <2>;
+    gpa1: gpa1 {
+        gpio-controller;
+        #gpio-cells = <2>;
 
-        interrupt-controller;
-        #interrupt-cells = <2>;
-    };
-};
+        interrupt-controller;
+        #interrupt-cells = <2>;
+    };
+};
 
-/*省略其他引脚配置*/
+/*省略其他引脚配置*/
 

除了单个引脚的描述,还可以将多个引脚组合在一起以实现特定的功能,比如SPI接口、I2C接口等,这被称为引脚组(pin group),其设备树描述如下:

-
/*s5pv210-pinctrl.dtsi*/
-uart0_data: uart0-data {
-    samsung,pins = "gpa0-0", "gpa0-1";
-    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;
-    samsung,pin-pud = <S3C64XX_PIN_PULL_NONE>;
-    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
-};
+
/*s5pv210-pinctrl.dtsi*/
+uart0_data: uart0-data {
+    samsung,pins = "gpa0-0", "gpa0-1";
+    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;
+    samsung,pin-pud = <S3C64XX_PIN_PULL_NONE>;
+    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
+};
 
-...
-i2c0_bus: i2c0-bus {
-    samsung,pins = "gpd1-0", "gpd1-1";
-    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;
-    samsung,pin-pud = <S3C64XX_PIN_PULL_UP>;
-    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
-};
+...
+i2c0_bus: i2c0-bus {
+    samsung,pins = "gpd1-0", "gpd1-1";
+    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;
+    samsung,pin-pud = <S3C64XX_PIN_PULL_UP>;
+    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;
+};
 

在这个例子中,uart0的引脚组配置需要用到"gpa0-0, gpa0-1",i2c0的引脚组配置需要用到"gpd1-0", "gpd1-1"。一旦选择了某个功能,pins中定义的所有引脚都需要在pin-function中做相应的功能设定,具体设定的值需要在芯片手册中查找。

还有一些引脚配置属性比如:

@@ -2499,12 +2499,12 @@

设备树描述
device-node-name {  
-    pinctrl-names = "default", "init", "sleep";   
-    pinctrl-0 = <pin-config-0-a>; 
-    pinctrl-1 = <pin-config-1-b>; 
-    pinctrl-2 = <pin-config-2-c>;        
-};
+
device-node-name {  
+    pinctrl-names = "default", "init", "sleep";   
+    pinctrl-0 = <pin-config-0-a>; 
+    pinctrl-1 = <pin-config-1-b>; 
+    pinctrl-2 = <pin-config-2-c>;        
+};
 

这里pinctrl-names就表示引脚的state——default, init, sleep等。对于某个client device,它使用的一组引脚应该同时处于某种state下,state的定义与电源管理系统相关(Power Management)。比如当设备进入睡眠状态时,我们可以精确控制引脚状态以节省功耗。每个state由下面的0、1、2配置对应。pinctrl-x是一个句柄(phandle)列表,每个句柄指向一个pin configuration。

由设备树的知识我们知道,每个设备树描述的device node最终会形成一个树状结构,在内核初始化的过程中,会扫描这个树状结构,并根据每个device node的配置信息,初始化对应的设备并加入到内核中。类似地,pin controller driver的初始化也是从设备树节点开始的:

@@ -2542,19 +2542,19 @@

pin controller device
struct pinctrl_desc {
-        const char *name;
-        const struct pinctrl_pin_desc *pins;
-        unsigned int npins;
-        const struct pinctrl_ops *pctlops;
-        const struct pinmux_ops *pmxops;
-        const struct pinconf_ops *confops;
-        struct module *owner;
+    const char *name;
+    const struct pinctrl_pin_desc *pins;
+    unsigned int npins;
+    const struct pinctrl_ops *pctlops;
+    const struct pinmux_ops *pmxops;
+    const struct pinconf_ops *confops;
+    struct module *owner;
 };
 

@@ -2565,7 +2565,7 @@

pin controller device
struct pinctrl_pin_desc {
     unsigned number;
     const char *name;
@@ -2577,7 +2577,7 @@ 

pin controller device
        A   B   C   D   E   
 
@@ -2617,7 +2617,7 @@ 

pin controller device pinctrl_enable(pctl);

-

在SOC系统中,为了实现特定的功能,需要将多个引脚进行组合。因此pinctrl子系统必须以group为单位,同时地访问和控制多个引脚,这就是pin group的概念。相应地,pinctrl子系统必须提供一些机制,来获取系统中的有多少个group以及每个group有哪些引脚,这些操作定义在struct pinctrl_ops结构体中:

+

在SoC系统中,为了实现特定的功能,需要将多个引脚进行组合。因此pinctrl子系统提供以group为单位,同时地访问和控制多个引脚的功能,这就是pin group的概念,这些操作定义在struct pinctrl_ops结构体中:

struct pinctrl_ops {
     int (*get_groups_count) (struct pinctrl_dev *pctldev);
     const char *(*get_group_name) (struct pinctrl_dev *pctldev, unsigned selector);
@@ -2635,10 +2635,10 @@ 

pin controller device
struct pinmux_ops {
     int (*request) (struct pinctrl_dev *pctldev, unsigned offset);
     int (*free) (struct pinctrl_dev *pctldev, unsigned offset);
diff --git a/linux/kernel/interrupt/index.html b/linux/kernel/interrupt/index.html
index 586ab9cc..8b2b8fc7 100644
--- a/linux/kernel/interrupt/index.html
+++ b/linux/kernel/interrupt/index.html
@@ -2560,23 +2560,19 @@ 

中断和中断处理中断处理程序

在响应一个特定的中断时,内核会执行一个函数,该函数叫中断处理程序(interrupt handler)或中断服务例程(interrupt service routine, ISR)。产生中断的每个设备都有一个响应的中断处理程序,一个设备的中断处理程序是它驱动程序的一部分。

-

中断处理程序处于特殊的中断上下文中。在解释中断上下文之前,我们先回忆一下进程上下文。进程上下文时一种内核所处的操作模式,此时进程代表进程执行。在进程上下文中,可以使用current宏关联当前进程,可以睡眠,也可以调用调度程序。与之相反,中断上下文与进程没有什么关联。因为中断的触发是随时到来的,并不知道当前处于哪个进程。因为没有后备进程,所以中断上下文不可睡眠,也不能调用任何可以引起睡眠的函数。中断上下文的代码必须快速且简洁。

-

中断随时有可能发生,因此中断处理程序必须快速执行。但是,中断处理程序往往还要完成大量其他的工作。比如一个网络设备,除了对硬件应答之外,还需要把来自硬件的网络数据拷贝到内存。显然这种工作量不会小。

-

又想中断处理程序运行得快,又要其完成的工作多。鉴于这两者之间的矛盾关系,我们一般把中断处理的过程切分为两个部分:上半部和下半部。中断处理程序是上半部——当接收到一个中断后,它立刻被执行,但只做有严格时限的工作。例如对接收到的中断进行应答。而稍后需要完成的工作则会被推迟到下半部去,在合适的时机执行。内核提供了多种下半部的机制,稍后会做说明。

+

中断处理程序处于特殊的中断上下文中。在解释中断上下文之前,我们先回忆一下进程上下文。在进程上下文中,可以使用current宏关联当前进程,可以睡眠,也可以调用调度程序。与之相反,中断上下文与进程没有什么关联。因为中断的触发是随时到来的,并不知道当前处于哪个进程。因为没有后备进程,所以中断上下文不可睡眠,也不能调用任何可以引起睡眠的函数。

+

中断随时有可能发生,因此中断处理程序必须快速执行。但是,中断处理程序往往还要完成大量其他的工作。尤其体现在网络设备上,除了对硬件应答之外,还需要把来自硬件的网络数据拷贝到内存,显然这种工作量不会小。

+

又想中断处理程序运行得快,又要其完成的工作多。鉴于这两者之间的矛盾关系,我们一般把中断处理的过程切分为两个部分:上半部和下半部。通常所说的中断处理程序是指上半部——当接收到一个中断后,它立刻被执行,但只做有严格时限的工作。例如对接收到的中断进行应答。而稍后需要完成的工作则会被推迟到下半部去,在合适的时机执行。内核提供了多种下半部的机制,稍后会做说明。

设备驱动程序可以通过request_irq()函数注册一个中断处理程序,并且激活给定的中断线:

-
int request_irq(unsigned int irq,
-                irq_handler_t handler,
-                unsigned long flags,
-                const char *name,
-                void *dev)
+
int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
+                const char *name, void *dev)
 

第一个参数irq表示要分配的中断号。

第二个参数handler是一个函数指针,指向处理这个中断的实际中断处理程序。

typedef irqreturn_t (*irq_handler_t)(int, void*);
 
-

第三个参数flags可以为0,也可以是下列一个或多个标志的位掩码:

+

第三个参数flags可以为0,也可以是下列一个或多个标志的位掩码,更多的定义在<linux/interrupt.h>:

    -
  • IRQF_DISABLED——禁止所有其他中断,新内核已移除。
  • IRQF_TIMER——系统定时器标志
  • IRQF_SHARED——在多个中断处理程序之间共享中断线。如果中断处理程序在同一条线上注册,那么必须指定这个标志。
diff --git a/linux/kernel/sched/index.html b/linux/kernel/sched/index.html index 7cbbf2aa..03b01408 100644 --- a/linux/kernel/sched/index.html +++ b/linux/kernel/sched/index.html @@ -2700,7 +2700,7 @@

进程状态

TASK_STOPPED表示进程被暂停,当接收到SIGSTOP、SIGTSTP、SIGTTIN或SIGTTOU信号后,进入该状态。

TASK_TRACED表示进程被跟踪。这往往是调试程序暂时终止了进程的运行。

内核经常需要调整当前进程的状态,可以使用set_task_state(task, state)函数,该函数将指定进程设置为指定状态。

-

exit_state的取值有两种:

+

exit_state的取值有两种:

EXIT_ZOMBIE表示僵死状态。一个进程执行被终止,但是其父进程还没有调用wait4()或者waitpid()系统调用来回收资源时,进入该状态。

EXIT_DEAD是进程的最终状态。父进程回收子进程资源之后,进程由系统删除。

进程的一些其他状态信息由flags控制,使用位掩码来表示不同的标记。一些常见的标记包括:

@@ -2775,7 +2775,11 @@

等待队列}; typedef struct __wait_queue wait_queue_t;

-

等待队列链表中的每个元素都代表一个睡眠中的进程,它的描述符存入task字段中。task_list负责将每一个元素链接到链表中。func表示等待队列中睡眠进程应该用什么方式唤醒。flags表示该进程是互斥进程还是非互斥进程。互斥进程表示多个进程在等待相同的事件,因此产生了竞争关系,此时内核只需要唤醒其中一个进程即可。而非互斥进程在发生指定事件后总是被唤醒。

+
+

flags:表示该进程是互斥还是非互斥进程。互斥进程表示多个进程在等待相同的事件,因此产生了竞争关系,此时内核只需要唤醒其中一个进程即可。而非互斥进程在发生指定事件后总是被唤醒。

+

func:负责唤醒睡眠进程的函数。

+

task_list:等待队列链表,链表中的每个元素都代表一个睡眠中的进程。

+

等待队列的操作比较复杂,这里不详细展开,只讲一个比较重要的函数sleep_on():这个函数将当前进程加入等待队列,并启动调度程序。

void sleep_on(wait_queue_head_t *wq)
 {
@@ -2794,7 +2798,7 @@ 

进程生命周期进程创建

Linux的进程创建涉及到两个函数:fork()exec()fork()拷贝当前进程创建一个子进程。exec()负责读取可执行文件并将其载入地址空间开始运行。

Linux的fork()使用了写时拷贝(copy-on-write)机制。这意味着在创建进程时,内核并不复制整个进程地址空间,而是让父子进程共享。只有在需要写入时,数据才会被复制,从而使得各个进程拥有自己的地址空间。这种优化可以避免大量根本就不会被使用的数据,从而使得进程的创建非常迅速。

-

fork()函数由clone()系统调用实现。该系统调用通过一系列CLONE_FLAGS参数来指明父子进程需要共享的资源。Linux中进程与线程的区别主要是在执行clone()系统调用时指定的共享资源有所不同而已。线程在创建时传递的参数如下:

+

fork()函数由clone()系统调用实现。该系统调用通过一系列CLONE_FLAGS参数来指明父子进程需要共享的资源。Linux中进程与线程的区别主要是在执行clone()系统调用时指定的共享资源有所不同而已。线程在创建时传递的参数如下:

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
 

上述代码指明了创建线程时需要共享地址空间、文件系统资源、文件描述符和信号处理程序。

@@ -2806,11 +2810,11 @@

进程终结进程切换

-

为了控制进程的运行,内核必须有能力挂起正在运行的进程,或者恢复以前挂起的进程。这种行为被称为进程切换(process switch)或上下文切换(context switch)。理解了进程切换,才有可能理解内核是如何对进程进行调度的。

+

为了控制进程的运行,内核必须有能力挂起正在运行的进程,或者恢复以前挂起的进程。这种行为被称为上下文切换(context switch)。理解了进程切换,才有可能理解内核是如何对进程进行调度的。

硬件上下文

尽管每个进程拥有独立的虚拟地址空间,但所有进程都共享CPU的寄存器,因此在恢复一个进程执行之前,内核必须保证寄存器装入了挂起进程时的值。进程恢复执行前必须装入寄存器的一组数据称为硬件上下文(hardware context)。硬件上下文是进程执行上下文的一个子集,因为执行上下文包含进程执行需要的所有信息。

thread字段

-

每个进程描述符包含一个类型为thread_struct的thread字段,只要进程被切换出去,内核就把其硬件上下文保存在这个结构中。这个数据结构包含了大量CPU寄存器信息。

+

每个进程描述符包含一个类型为thread_struct的thread字段,只要进程被切换出去,内核就把其硬件上下文保存在这个结构中。这个数据结构包含了大量CPU寄存器信息。

执行进程切换

进程切换只会发生在以下两种情况:

    diff --git a/linux/kernel/vma/index.html b/linux/kernel/vma/index.html index b4407710..15387c6c 100644 --- a/linux/kernel/vma/index.html +++ b/linux/kernel/vma/index.html @@ -2369,7 +2369,7 @@

    地址空间的概念伙伴系统的内存分配算法。

    堆空间上方是待分配区域,用来扩展堆空间的使用。接下来是内存映射区域。任何应用程序都可以通过mmap()系统调用映射至此区域。内存映射可以用来加载动态库,比如 ld-linux.so 就被加载于此,另外,如果你通过malloc()申请了超过128K内存,内核将直接为你分配一块映射区域作为内存,而不是使用堆内存。

    最后一块区域是栈空间,在这里保存函数运行过程需要的局部变量以及函数参数等信息。栈空间的地址是从高到低增长的。内核使用start_stack标识栈的起始位置。SP寄存器保存栈顶指针,BP寄存器保存栈基地址。

    在命令行模式下,可以使用cat /proc/[pid]/maps查看一个进程的内存布局。

    @@ -2397,10 +2397,14 @@

    内存描述符mm_struct unsigned long stack_vm; //栈中映射的页数目 };

-

mm_users记录正在使用该地址的进程数目,比如如果有两个线程共享该地址空间,那么mm_users的值便等于2。mm_count是mm_struct结构体的主引用计数,当值为0时,该结构体会被释放。

-

mmap使用单独链表连接所有的内存区域对象。每一个vm_area_struct结构体通过自身的vm_next指针被连入链表。mmap指向链表中第一个节点。

-

mm_rb则使用红黑树。mm_rb指向根节点,每一个vm_area_struct结构体通过自身的vm_rb连接到树中。

-

进程管理与调度中我们介绍了mm变量,用来存放该进程使用的内存描述符,current->mm就指向当前进程的内存描述符。fork()函数利用copy_mm()函数复制父进程的内存描述符。如果父子进程共享地址空间,则在调用clone()时,设置CLONE_VM标志,这样的进程就是线程。在Linux环境下,是否共享地址空间几乎是进程和线程本质上的唯一区别。如果指定了CLONE_VM,线程就不需要另外分配地址空间了,而是直接将mm域直接指向父进程的内存描述符即可。

+
+

mm_users:正在使用该地址的进程数。

+

mm_count:mm_struct结构体的主引用计数,该值为0时,释放结构体。

+

mmap:指向一个vm_area_struct结构体的链表头。

+

mm_rb:指向一个vm_area_struct结构体的红黑树根节点。

+

mmlist:指向一个mm_struct结构体的链表头。

+
+

进程管理与调度中我们介绍了mm变量,用来存放该进程使用的内存描述符,current->mm就指向当前进程的内存描述符。fork()函数利用copy_mm()函数复制父进程的内存描述符。如果父子进程共享地址空间,则在调用clone()时,设置CLONE_VM标志,这样的进程就是线程。在Linux环境下,是否共享地址空间几乎是进程和线程本质上的唯一区别。如果指定了CLONE_VM,线程就不需要另外分配地址空间了,而是直接将mm域直接指向父进程的内存描述符即可。

内核线程没有进程地址空间,也没有相关的内存描述符,其mm域为NULL。当一个内核线程被调度时,它会直接使用前一个进程的内存描述符。

虚拟内存区域

虚拟内存区域(Virtual Memory Area, VMA)在内核中用vm_area_struct结构体描述。每个vm_area_struct结构都对应于指定地址空间上某块连续的内存区域。vm_start指向了这块虚拟内存区域的起始地址,vm_end指向了这块虚拟内存区域的结束地址。vm_area_struct结构体描述的是[vm_start,vm_end)这样一段左闭右开的虚拟内存区域。

@@ -2426,7 +2430,8 @@

虚拟内存区域VMA标志

vm_flags定义了VMA的标志,它表示内存区域的行为和信息。一些比较重要的标志比如:VM_READ、VM_WRITE和VM_EXEC标志了内存区域中页面的可读、可写、可执行权限。当访问VMA时,需要查看其访问权限。

VM_SHARED表示内存区域包含的映射是否可以在多进程之间共享,如果设置了该标志位,我们称其位共享映射,否则就是私有映射。

-

VM_IO标志内存区域中对设备I/O空间的映射。该标志通常在设备驱动程序执行mmap()函数时才被设置。VM_RESERVED标志规定了内存区域不能被换出。

+

VM_IO标志内存区域中对设备I/O空间的映射。该标志通常在设备驱动程序执行mmap()函数时才被设置。

+

VM_RESERVED标志规定了内存区域不能被换出。

VMA操作

vm_ops域指向与内存区域相关的操作函数,由vm_operations_struct结构体表示:

struct vm_operations_struct {
diff --git a/search/search_index.json b/search/search_index.json
index 4252fc5d..343fce7c 100644
--- a/search/search_index.json
+++ b/search/search_index.json
@@ -1 +1 @@
-{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Home","text":""},{"location":"#_1","title":"\u6b22\u8fce\u6765\u5230\u6211\u7684\u9891\u9053","text":"

\u6ca1\u6709\u5b66\u672f\u7684\u6c11\u4e3b\u548c\u601d\u60f3\u7684\u81ea\u7531\uff0c\u79d1\u5b66\u5c31\u4e0d\u80fd\u7e41\u8363\u3002 \u2014\u2014\u2014\u20142003\u7248\u9ad8\u4e00\u7269\u7406\u5fc5\u4fee\u4e00\u8bfe\u672c

\u8fd9\u91cc\u4e3b\u8981\u5206\u4eab\u6211\u7684\u5b66\u4e60\u7b14\u8bb0\u548c\u5fc3\u5f97\u4f53\u4f1a\uff0c\u53c2\u8003\u8d44\u6599\u4e8e\u6bcf\u4e2a\u4e3b\u9875\u9762\u5217\u51fa\u3002

"},{"location":"#_2","title":"\u5feb\u901f\u5bfc\u822a","text":"
  • Linux

  • RTOS

  • Arm

  • \u7f16\u7a0b\u8bed\u8a00

  • \u4e91\u8ba1\u7b97

  • Tools

  • \u5176\u4ed6

"},{"location":"#inspired-by","title":"Inspired by:","text":"
  • CS\u81ea\u5b66\u6307\u5357
  • \u4e0a\u6d77\u4ea4\u901a\u5927\u5b66\u751f\u5b58\u624b\u518c
"},{"location":"arm/","title":"ARM","text":""},{"location":"arm/#table-of-contents","title":"Table of Contents","text":"
  • ARM\u67b6\u6784\u57fa\u7840\u77e5\u8bc6
  • A64\u6307\u4ee4\u96c6
"},{"location":"arm/#_1","title":"\u53c2\u8003\u8d44\u6599","text":"
  • ARM64\u4f53\u7cfb\u7ed3\u6784\u7f16\u7a0b\u4e0e\u5b9e\u8df5
  • ARMv8 Arch
  • Arm Developer Documentation
  • ARM GCC Inline Assembler CookBook
"},{"location":"arm/aarch64/","title":"ARM\u67b6\u6784\u57fa\u7840\u77e5\u8bc6","text":"

ARM\u4f53\u7cfb\u7ed3\u6784\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u5206\u4e3a\u5982\u4e0b\u4e09\u79cd\u7cfb\u5217\uff1a

  • A\u7cfb\u5217\uff1a\u9762\u5411\u6027\u80fd\u5bc6\u96c6\u578b\u7cfb\u7edf

  • R\u7cfb\u5217\uff1a\u9762\u5411\u5b9e\u65f6\u6027\u5e94\u7528

  • M\u7cfb\u5217\uff1a\u9762\u5411\u5d4c\u5165\u5f0f\u5e94\u7528

ARMv8\u662fARM\u516c\u53f8\u53d1\u5e03\u7684\u7b2c\u4e00\u4ee3\u652f\u630164\u4f4d\u5904\u7406\u5668\u7684\u6307\u4ee4\u96c6\u548c\u4f53\u7cfb\u7ed3\u6784\u3002\u5728\u6269\u514564\u4f4d\u6307\u4ee4\u96c6\u7684\u540c\u65f6\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5bf932\u4f4d\u6307\u4ee4\u96c6\u7684\u652f\u6301\u3002\u5b83\u8fd8\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7684\u7279\u6027\uff1a

  • \u8d85\u5927\u7269\u7406\u5730\u5740\u7a7a\u95f4\uff0c\u63d0\u4f9b\u8d85\u8fc74GB\u7269\u7406\u5185\u5b58\u7684\u8bbf\u95ee\u3002

  • 64\u4f4d\u5bbd\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u3002

  • \u81ea\u52a8\u4e8b\u4ef6\u4fe1\u53f7\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8282\u80fd\u3001\u9ad8\u6027\u80fd\u7684\u81ea\u65cb\u9501\u3002

  • 31\u4e2a64\u4f4d\u5bbd\u7684\u901a\u7528\u5bc4\u5b58\u5668\uff0c\u53ef\u4ee5\u51cf\u5c11\u5bf9\u6808\u7684\u8bbf\u95ee\u3002

  • \u63d0\u4f9b16KB\u548c64KB\u7684\u9875\uff0c\u6709\u52a9\u4e8e\u964d\u4f4eTLB\u7684\u4e3a\u547d\u4e2d\u7387\u3002

  • \u5168\u65b0\u7684\u5f02\u5e38\u5904\u7406\u6a21\u578b\uff0c\u964d\u4f4e\u4e86\u64cd\u4f5c\u7cfb\u7edf\u548c\u865a\u62df\u5316\u7684\u5b9e\u73b0\u590d\u6742\u5ea6\u3002

  • \u5168\u65b0\u7684\u52a0\u8f7d-\u83b7\u53d6\u6307\u4ee4\u3001\u5b58\u50a8-\u91ca\u653e\u6307\u4ee4\uff0c\u4e13\u95e8\u4e3aC++11\u3001C11\u4ee5\u53caJAVA\u5185\u5b58\u6a21\u578b\u8bbe\u8ba1\u3002

  • NEON\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u9ad8\u7ea7SIMD\uff0c\u4f7f\u5f97SIMD\u77e2\u91cf\u5316\u80fd\u591f\u5e94\u7528\u4e8e\u66f4\u5e7f\u6cdb\u7684\u7b97\u6cd5\u96c6\uff0c\u4f8b\u5982\u79d1\u5b66\u8ba1\u7b97\u3001\u9ad8\u6027\u80fd\u8ba1\u7b97(HPC)\u548c\u8d85\u7ea7\u8ba1\u7b97\u673a\u3002

\u5e38\u89c1\u7684\u91c7\u7528ARMv8\u4f53\u7cfb\u7ed3\u6784\u7684\u5904\u7406\u5668\u5185\u6838\u6709\uff1a

  • Cortex-A53

  • Cortex-A57

  • Cortex-A72

ARMv8\u4f53\u7cfb\u5b9a\u4e49\u4e86\u4e24\u79cd\u6267\u884c\u72b6\u6001\uff1a

AArch64\uff1a64\u4f4d\u7684\u6267\u884c\u72b6\u6001\u3002

  • \u63d0\u4f9b31\u4e2a64\u4f4d\u7684\u901a\u7528\u5bc4\u5b58\u5668\u3002

  • \u63d0\u4f9b64\u4f4d\u7684\u7a0b\u5e8f\u8ba1\u6570\u6307\u9488\u5bc4\u5b58\u5668(PC)\u3001\u6808\u6307\u9488\u5bc4\u5b58\u5668\uff08SP\uff09\u4ee5\u53ca\u5f02\u5e38\u94fe\u63a5\u5bc4\u5b58\u5668(ELR)\u3002

  • \u63d0\u4f9bA64\u6307\u4ee4\u96c6\u3002

  • \u5b9a\u4e49ARMv8\u5f02\u5e38\u6a21\u578b\uff0c\u652f\u63014\u4e2a\u5f02\u5e38\u7b49\u7ea7\u2014\u2014EL0 ~ EL3\u3002

  • \u63d0\u4f9b\u4e00\u7ec4\u5904\u7406\u5668\u72b6\u6001\u5bc4\u5b58\u5668(PSTATE)\u4fdd\u5b58PE\u7684\u72b6\u6001\u3002

AArch32\uff1a32\u4f4d\u7684\u6267\u884c\u72b6\u6001\u3002

  • \u63d0\u4f9b13\u4e2a32\u4f4d\u7684\u901a\u7528\u5bc4\u5b58\u5668\uff0c\u518d\u52a0\u4e0aPC\u3001SP\u3001LR\u3002

  • \u652f\u6301A32\u548cT32(Thumb\u6307\u4ee4\u96c6)\u6307\u4ee4\u96c6\u3002

  • \u652f\u6301ARMv7-A\u5f02\u5e38\u6a21\u578b\u3002

  • \u5b9a\u4e49\u4e00\u7ec4PSTATE\u4fdd\u5b58PE\u7684\u72b6\u6001\u3002

ARMv8\u4f53\u7cfb\u7ed3\u6784\u6839\u636e\u4e0d\u540c\u7684\u6267\u884c\u72b6\u6001\u63d0\u4f9b\u4e0d\u540c\u6307\u4ee4\u96c6\u7684\u652f\u6301\uff1a

  • A64\u6307\u4ee4\u96c6\uff1a\u8fd0\u884c\u5728AArch64\u72b6\u6001\u4e0b\uff0c\u63d0\u4f9b64\u4f4d\u6307\u4ee4\u96c6

  • A32\u6307\u4ee4\u96c6\uff1a\u8fd0\u884c\u5728AArch32\u72b6\u6001\u4e0b\uff0c\u63d0\u4f9b32\u4f4d\u6307\u4ee4\u96c6

  • T32\u6307\u4ee4\u96c6\uff1a\u8fd0\u884c\u5728AArch32\u72b6\u6001\u4e0b\uff0c\u63d0\u4f9b16\u4f4d\u548c32\u4f4d\u6307\u4ee4\u96c6\u3002

\u5728AArch64\u72b6\u6001\u4e0b\uff0c\u7cfb\u7edf\u5bc4\u5b58\u5668\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u7b49\u7ea7\u63d0\u4f9b\u4e0d\u540c\u7684\u53d8\u79cd\u5bc4\u5b58\u5668\u2014\u2014<register_name>_ELx\u3002

"},{"location":"arm/aarch64/#armv8","title":"ARMv8\u5bc4\u5b58\u5668","text":"

AArch64\u6267\u884c\u72b6\u6001\u652f\u630131\u4e2a64\u4f4d\u7684\u901a\u7528\u5bc4\u5b58\u5668\uff1aX0 ~ X30\u3002\u53ef\u4ee5\u4f7f\u7528W\u6765\u8868\u793a\u4f4e32\u4f4d\u7684\u6570\u636e\uff0c\u5982W0\u8868\u793aX0\u5bc4\u5b58\u5668\u7684\u4f4e32\u4f4d\u3002\u4eceW\u5bc4\u5b58\u5668\u8bfb\u53d6\u65f6\uff0c\u5ffd\u7565\u76f8\u5e94X\u5bc4\u5b58\u5668\u9ad832\u4f4d\uff0c\u5e76\u4fdd\u6301\u5176\u5b83\u4e0d\u53d8\u3002\u5199\u5165W\u5bc4\u5b58\u5668\u65f6\uff0c\u5c06X\u5bc4\u5b58\u5668\u7684\u9ad832\u4f4d\u8bbe\u7f6e\u4e3a\u96f6\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5c060xFFFFFFFF\u5199\u5165W0\u4f1a\u5c06X0\u8bbe\u7f6e\u4e3a0x00000000FFFFFFFF\u3002

PSTATE\u5bc4\u5b58\u5668\u7528\u6765\u8868\u793a\u5f53\u524d\u5904\u7406\u5668\u72b6\u6001\u3002

\u7279\u6b8a\u5bc4\u5b58\u5668\uff1a

1.\u96f6\u5bc4\u5b58\u5668

ARMv8\u4f53\u7cfb\u7ed3\u6784\u63d0\u4f9b\u4e24\u4e2a\u96f6\u5bc4\u5b58\u5668\u2014\u2014WZR\uff0cXZR\uff0c\u5bc4\u5b58\u5668\u7684\u5185\u5bb9\u5168\u662f0\u3002

2.PC\u6307\u9488\u5bc4\u5b58\u5668

\u7528\u6765\u6307\u5411\u5f53\u524d\u8fd0\u884c\u6307\u4ee4\u7684\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u5730\u5740\u3002

3.SP\u6307\u9488\u5bc4\u5b58\u5668

\u7528\u6765\u6307\u5411\u5f53\u524d\u6808\u5e27\u7684\u6808\u9876\u3002\u6bcf\u4e2a\u5f02\u5e38\u7b49\u7ea7\u90fd\u6709\u4e00\u4e2a\u4e13\u95e8\u7684SP\u5bc4\u5b58\u5668\u3002

4.\u5907\u4efd\u7a0b\u5e8f\u72b6\u6001\u5bc4\u5b58\u5668(SPSR)

\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u5904\u7406\u5668\u4f1a\u5c06PSTATE\u5bc4\u5b58\u5668\u7684\u503c\u6682\u65f6\u4fdd\u5b58\u5230SPSR\u4e2d\u3002

5.\u5f02\u5e38\u94fe\u63a5\u5bc4\u5b58\u5668(ELR)

\u5b58\u653e\u5f02\u5e38\u8fd4\u56de\u5730\u5740\u3002

6.CuurentEL\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2dEL\u5b57\u6bb5\u7684\u503c\uff0c\u4fdd\u5b58\u4e86\u5f53\u524d\u5f02\u5e38\u7b49\u7ea7\uff0c\u53ef\u4ee5\u901a\u8fc7MRS\u6307\u4ee4\u8bfb\u53d6\u3002

7.DAIF\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2d{D, A,I,F}\u5b57\u6bb5\u7684\u503c\u3002

8.SPSel\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2dSP\u5b57\u6bb5\u7684\u503c\u3002

9.PAN\u5bc4\u5b58\u5668

\u8868\u793aPSTAET\u5bc4\u5b58\u5668\u4e2dPAN\u5b57\u6bb5\u7684\u503c\u3002

10.UAO\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2dUAO\u5b57\u6bb5\u7684\u503c\u3002

11.NZCV\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2d{N, Z, C, V}\u5b57\u6bb5\u7684\u503c\u3002

"},{"location":"arm/aarch64/#_1","title":"\u5f02\u5e38\u7b49\u7ea7","text":"
  • EL0\uff1a\u7528\u6237\u7279\u6743\uff0c\u8fd0\u884c\u666e\u901a\u7528\u6237\u7a0b\u5e8f

  • EL1\uff1a\u7cfb\u7edf\u7279\u6743\uff0c\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838

  • EL3\uff1a\u8fd0\u884c\u865a\u62df\u673a\u76d1\u89c6\u5668

  • EL4\uff1a\u8fd0\u884c\u5b89\u5168\u76d1\u63a7\u5668

\u5f02\u5e38\u7ea7\u522b\u4e4b\u95f4\u7684\u79fb\u52a8\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a

  • \u79fb\u52a8\u5230\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\uff0c\u4f8b\u5982\u4eceEL0\u5230EL1\uff0c\u8868\u793a\u8f6f\u4ef6\u589e\u52a0\u6267\ufa08\u7279\u6743\u3002

  • \u4e0d\u80fd\u5c06\u5f02\u5e38\u5904\u7406\u964d\u4f4e\u5230\u8f83\u4f4e\u7684\u5f02\u5e38\u7ea7\u522b\u3002

  • EL0\u7ea7\u522b\u6ca1\u6709\u5f02\u5e38\u5904\uf9e4\uff0c\u5fc5\u987b\u5728\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\u5904\uf9e4\u5f02\u5e38\u3002

  • \u5f02\u5e38\u5bfc\u81f4\u7a0b\u5e8f\u6d41\u7a0b\u53d1\u751f\u53d8\u5316\u3002\u5f02\u5e38\u5904\uf9e4\u7a0b\u5e8f\u7684\u6267\ufa08\u4ee5\u9ad8\u4e8eEL0\u7684\u5f02\u5e38\u7ea7\u522b\u4ece\u4e0e\u6240\u91c7\u53d6\u7684\u5f02\u5e38\u76f8\u5173\u7684\u5df2\u5b9a\u4e49\u5411\uf97e\u5f00\u59cb\u3002\u4f8b\u5916\u60c5\u51b5\u5305\u62ec\uff1aIRQ\u548cFIQ\u7b49\u4e2d\u65ad\u3002\u5185\u5b58\u7cfb\u7edf\u4e2d\u6b62\u3002\u672a\u5b9a\u4e49\u7684\u6307\u4ee4\u3002\u7cfb\u7edf\u8c03\u7528\u3002\u8fd9\u4e9b\u5141\u8bb8\u975e\u7279\u6743\u8f6f\u4ef6\u5bf9\u64cd\u4f5c\u7cfb\u7edf \u5b89\u5168\u76d1\u89c6\u5668\u6216\u7ba1\uf9e4\u7a0b\u5e8f\u9677\u9631\u3002

  • \u901a\u8fc7\u6267\ufa08ERET\u6307\u4ee4\u6765\u7ed3\u675f\u5f02\u5e38\u5904\uf9e4\u5e76\u8fd4\u56de\u5230\u4e0a\u4e00\u4e2a\u5f02\u5e38\u7ea7\u522b\u3002

  • \u4ece\u5f02\u5e38\u8fd4\u56de\u53ef\u4ee5\u4fdd\u6301\u76f8\u540c\u7684\u5f02\u5e38\u7ea7\u522b\u6216\u8fdb\u5165\u8f83\u4f4e\u7684\u5f02\u5e38\u7ea7\u522b\u3002\u5b83\uf967\u80fd\u79fb\u52a8\u5230\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\u3002

  • \u5b89\u5168\u72b6\u6001\u786e\u5b9e\u4f1a\u968f\u7740\u5f02\u5e38\u7ea7\u522b\u7684\u53d8\u5316\u800c\u53d8\u5316\uff0c\u9664\u975e\u4eceEL3\u91cd\u65b0\u8c03\u6574\u5230\u975e\u5b89\u5168\u72b6\u6001

\u8981\u5728\u76f8\u540c\u7684\u5f02\u5e38\u7ea7\u522b\u4e4b\u95f4\u5207\u6362\u6267\ufa08\u72b6\u6001\uff0c\u5fc5\u987b\u5207\u6362\u5230\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\uff0c\u7136\u540e\u8fd4\u56de\u5230\u539f\u59cb\u7684\u5f02\u5e38\u7ea7\u522b\u3002\u4f8b\u5982\uff0c\u670932\u4f4d\u548c64\u4f4d\u5e94\u7528\u7a0b\u5e8f\u572864\u4f4d\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8fd0\ufa08\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c32\u4f4d\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u6267\ufa08\u5e76\u751f\u6210\u4e3b\u7ba1\u8c03\u7528(SVC)\u6307\u4ee4\uff0c\u6216\u63a5\u6536\u4e2d\u65ad\uff0c\u4ece\u800c\u5bfc\u81f4\u5207\u6362\u5230L1\u548cAArch64\u3002\u7136\u540e\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u6267\ufa08\u4efb\u52a1\u5207\u6362\u5e76\u8fd4\u56de\u5230AArch64\u4e2d\u7684EL0\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u610f\u5473\u7740\u60a8\uf967\u80fd\u62e5\u6709\u6df7\u5408\u768432\u4f4d\u548c64\u4f4d\u5e94\u7528\u7a0b\u5e8f\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u76f4\u63a5\u7684\u8c03\u7528\u65b9\u5f0f\u3002

"},{"location":"arm/barrier/","title":"\u5185\u5b58\u5c4f\u969c","text":""},{"location":"arm/instructions/","title":"A64\u6307\u4ee4\u96c6","text":"

ARMv8\u589e\u52a0\u4e86\u65b0\u768464\u4f4d\u6307\u4ee4\u96c6\uff0c\u5b83\u53ef\u4ee5\u5904\u740664\u4f4d\u5bbd\u7684\u5bc4\u5b58\u5668\u548c\u6570\u636e\u5e76\u4e14\u4f7f\u752864\u4f4d\u7684\u6307\u9488\u6765\u8bbf\u95ee\u5185\u5b58\uff0c\u4f46\u662f\u5176\u6307\u4ee4\u96c6\u7684\u6307\u4ee4\u5bbd\u5ea6\u662f32\u4f4d\u3002ARMv8\u517c\u5bb9A32\u6307\u4ee4\u96c6\uff0c\u4f46A64\u6307\u4ee4\u96c6\u548cA32\u6307\u4ee4\u96c6\u662f\u4e0d\u517c\u5bb9\u7684\u3002

ARMv8\u652f\u6301\u4e24\u79cd\u6267\u884c\u72b6\u6001\u2014\u2014AArch64\u548cAArch32\u3002AArch64\u72b6\u6001\u7528\u4e8e\u6267\u884c64\u4f4d\u6307\u4ee4\uff0c\u800cAArch32\u72b6\u6001\u7528\u4e8e\u6267\u884c32\u4f4d\u6307\u4ee4\u3002

"},{"location":"cloud/","title":"\u4e91\u8ba1\u7b97","text":""},{"location":"cloud/docker/","title":"\u5bb9\u5668\u6280\u672f","text":""},{"location":"linux/","title":"Linux\u5185\u6838\u4f53\u7cfb","text":""},{"location":"linux/#table-of-contents","title":"Table of Contents","text":"
  • \u5185\u6838\u5256\u6790
  • \u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
  • \u6027\u80fd\u8c03\u4f18
  • ebpf
"},{"location":"linux/#_1","title":"\u53c2\u8003\u8d44\u6599","text":"
  • \u8da3\u8c08Linux\u64cd\u4f5c\u7cfb\u7edf

  • \u6df1\u5165Linux\u5185\u6838\u67b6\u6784

  • \u7cbe\u901a\u5d4c\u5165\u5f0fLinux\u7f16\u7a0b

  • \u6df1\u5165\u7406\u89e3LINUX\u5185\u6838(\u7b2c3\u7248)

  • Linux\u5185\u6838\u8bbe\u8ba1\u4e0e\u5b9e\u73b0(\u7b2c3\u7248)

  • UNIX\u73af\u5883\u9ad8\u7ea7\u7f16\u7a0b\uff08\u7b2c3\u7248)

  • Linux\u547d\u4ee4\u884c\u4e0eshell\u811a\u672c\u7f16\u7a0b\u5927\u5168\uff08\u7b2c4\u7248)

  • \u91ce\u706bLinux\u5f00\u53d1\u5b9e\u6218\u6307\u5357

  • \u8717\u58f3\u79d1\u6280

  • Linux\u6027\u80fd\u4f18\u5316\u5b9e\u6218

"},{"location":"linux/drivers/","title":"\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f","text":""},{"location":"linux/drivers/#table-of-contents","title":"Table of Contents","text":""},{"location":"linux/drivers/#_2","title":"\u57fa\u7840\u77e5\u8bc6","text":"
  • \u8bbe\u5907\u6811
  • \u8bbe\u5907\u6a21\u578b
  • \u5e73\u53f0\u8bbe\u5907\u9a71\u52a8
  • \u76f4\u63a5\u5185\u5b58\u8bbf\u95ee
"},{"location":"linux/drivers/#_3","title":"\u5b57\u7b26\u3001\u5757\u548c\u7f51\u7edc","text":"
  • \u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
  • \u5757\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
  • \u7f51\u7edc\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
"},{"location":"linux/drivers/#_4","title":"\u4f4e\u901f\u8bbe\u5907\u9a71\u52a8","text":"
  • Pinctrl\u5b50\u7cfb\u7edf
  • GPIO\u5b50\u7cfb\u7edf
  • I2C\u5b50\u7cfb\u7edf
  • SPI\u5b50\u7cfb\u7edf
  • UART\u5b50\u7cfb\u7edf
  • Input\u5b50\u7cfb\u7edf
"},{"location":"linux/drivers/#_5","title":"\u9ad8\u901f\u8bbe\u5907\u9a71\u52a8","text":"
  • \u4ee5\u592a\u7f51
  • PCI
  • USB
  • MMC
  • WIFI
  • \u84dd\u7259
"},{"location":"linux/drivers/blkdev/","title":"\u5757\u8bbe\u5907\u9a71\u52a8","text":""},{"location":"linux/drivers/chardev/","title":"\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f","text":"

\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u6846\u67b6\u5982\u56fe\u6240\u793a\uff1a

\u5728\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\u7684\u65f6\u5019\uff0c\u9996\u5148\u5e94\u8be5\u5411\u5185\u6838\u7533\u8bf7\u4e00\u4e2a\u8bbe\u5907\u53f7\u3002\u62ff\u5230\u8bbe\u5907\u53f7\u4e4b\u540e\uff0c\u9700\u8981\u624b\u52a8\u5b9e\u73b0file_operation\u7ed3\u6784\u4f53\u4e2d\u7684\u51fd\u6570\u6307\u9488\uff0c\u5e76\u4fdd\u5b58\u5230cdev\u7ed3\u6784\u4f53\u4e2d\u3002\u7136\u540e\u4f7f\u7528cdev_add()\u51fd\u6570\u6ce8\u518ccdev\u3002

\u6ce8\u9500\u8bbe\u5907\u65f6\u9700\u8981\u91ca\u653e\u5185\u6838\u4e2d\u7684cdev\uff0c\u5e76\u4e14\u5f52\u8fd8\u7533\u8bf7\u7684\u8bbe\u5907\u53f7\u3002

"},{"location":"linux/drivers/chardev/#_2","title":"\u5feb\u901f\u53c2\u8003","text":"
#include <linux/types.h>\n\ndev_t devID\n\nint MAJOR(dev_t dev)\n\nint MINOR(dev_t dev)\n\ndev_t MKDEV(unsigned int major, unsigned int minor)\n
#include <linux/fs.h>\n\nint register_chrdev_region(dev_t first, unsigned int count, char *name)\n\nint alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name)\n\nvoid unregister_chrdev_region(dev_t first, unsigned int count)\n\nint register_chrdev(unsigned int major, const char *name, struct file_operations *fops)\n\nint unregister_chrdev(unsigned int major, const char *name)\n\nstruct file_operations\n\nstruct file\n\nstruct inode\n
#include <linux/cdev.h>\n\nstruct cdev *cdev_alloc(void)\n\nvoid cdev_init(struct cdev *dev, dev_t num, unsigned int count)\n\nint cdev_add(struct cdev *cdev, dev_t num, unsigned int count)\n\nvoid cdev_del(struct cdev *dev)\n
#include <linux/kernel.h>\n\ncontainer_of(pointer, type, field)\n\n#include <asm/uaccess.h>\n\nunsigned long copy_from_user(void *to, const void *from, unsigned long count)\n\nunsigned long copy_to_user(void *to, const void *from, unsigned long count)\n
"},{"location":"linux/drivers/chardev/#_3","title":"\u8bbe\u5907\u53f7\u521d\u59cb\u5316","text":"

\u4e3b\u8bbe\u5907\u53f7\u6807\u8bc6\u8bbe\u5907\u5bf9\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u73b0\u4ee3Linux\u5185\u6838\u5141\u8bb8\u591a\u4e2a\u9a71\u52a8\u5171\u4eab\u4e3b\u8bbe\u5907\u53f7\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u6b21\u8bbe\u5907\u53f7\u7528\u4e8e\u6b63\u786e\u786e\u5b9a\u8bbe\u5907\u6587\u4ef6\u6240\u6307\u5411\u7684\u8bbe\u5907\u3002\u5185\u6838\u7528dev_t\u7c7b\u578b\u6765\u8868\u6751\u8bbe\u5907\u7f16\u53f7\u2014\u2014\u5305\u62ec\u4e3b\u8bbe\u5907\u53f7\u548c\u6b21\u8bbe\u5907\u53f7\u3002\u8981\u83b7\u53d6\u4e3b\u3001\u6b21\u8bbe\u5907\u53f7\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b8fMAJOR\u3001MINOR\u6765\u64cd\u4f5c\u3002

"},{"location":"linux/drivers/chardev/#_4","title":"\u8bbe\u5907\u53f7\u7684\u6ce8\u518c\u4e0e\u5378\u8f7d(\u4e0d\u63a8\u8350\u7684\u505a\u6cd5)","text":"
  • \u6ce8\u518c
    int register_chrdev(unsigned int major, const char *name, struct file_operations *fops)\n

\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6ce8\u518c\u65f6\u9700\u8981\u624b\u52a8\u6307\u5b9a\u4e3b\u8bbe\u5907\u53f7\uff0c\u56e0\u6b64\u4f60\u5fc5\u987b\u4e8b\u5148\u77e5\u9053\u54ea\u4e2a\u4e3b\u8bbe\u5907\u53f7\u6ca1\u6709\u88ab\u5360\u7528\uff0c\u5b9e\u9645\u5f00\u53d1\u4e2d\u5e76\u4e0d\u63a8\u8350\uff0c\u5e76\u4e14\u8be5\u51fd\u6570\u8fd8\u4e00\u6b21\u6027\u5360\u7528\u4e86\u4e3b\u8bbe\u5907\u53f7\u4e0b\u7684\u5168\u90e8\u6b21\u8bbe\u5907\u53f7\u3002

\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u5fc5\u987b\u8981\u68c0\u67e5\u4fdd\u5b58\u5728inode\u7ed3\u6784\u4e2d\u7684\u6b21\u8bbe\u5907\u53f7\u662f\u5426\u88ab\u4f7f\u7528\u3002

  • \u5378\u8f7d
    int unregister_chrdev(unsigned int major, const char *name)\n
"},{"location":"linux/drivers/chardev/#_5","title":"\u8bbe\u5907\u53f7\u7684\u6ce8\u518c\u4e0e\u5378\u8f7d(\u63a8\u8350\u7684\u505a\u6cd5)","text":"
  • \u6ce8\u518c
int register_chrdev_region(dev_t first, unsigned count, const char *name)\n

first\uff1a\u8981\u5206\u914d\u7684\u8bbe\u5907\u7f16\u53f7\u7684\u8d77\u59cb\u503c\u3002

count\uff1a\u8bf7\u6c42\u7684\u8fde\u7eed\u8bbe\u5907\u7f16\u53f7\u4e2a\u6570\u3002

name\uff1a\u4e0e\u8be5\u7f16\u53f7\u76f8\u5173\u7684\u8bbe\u5907\u540d\u79f0\uff0c\u5b83\u5c06\u51fa\u73b0\u5728/proc/devices\u548csysfs\u4e2d\u3002

\u5982\u679c\u6211\u4eec\u63d0\u524d\u5c31\u77e5\u9053\u6240\u9700\u8981\u7684\u8bbe\u5907\u7f16\u53f7\uff0c\u90a3\u4e48\u4f7f\u7528register_chrdev_region()\u5c31\u591f\u4e86\u3002\u4f46\u662f\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a\uff0c\u800c\u5e94\u8be5\u4f7f\u7528\u52a8\u6001\u5206\u914d\u51fd\u6570\uff1a

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)\n

dev\uff1a\u4fdd\u5b58\u7528\u6765\u7533\u8bf7\u7684\u8bbe\u5907\u53f7\u3002

baseminor\uff1a\u6b21\u8bbe\u5907\u53f7\u7684\u8d77\u59cb\u503c\uff0c\u4e00\u822c\u4e3a0\u3002

\u9a71\u52a8\u7a0b\u5e8f\u5e94\u8be5\u59cb\u7ec8\u4f7f\u7528alloc_chrdev_region\u800c\u4e0d\u662fregister_chrdev_region \u2014\u2014\u2014\u2014\u300aLinux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8fP50\u300b

\u52a8\u6001\u5206\u914d\u7684\u7f3a\u70b9\u662f\u65e0\u6cd5\u9884\u5148\u521b\u5efa\u8bbe\u5907\u8282\u70b9\uff08mknod\uff09\uff0c\u4e0d\u8fc7\u8fd9\u662f\u5c0f\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7528awk\u5de5\u5177\u4ece/proc/devices\u4e2d\u8bfb\u53d6\u4fe1\u606f\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u52a8\u6001\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u3002

  • \u5378\u8f7d

\u4e0a\u9762\u4e24\u79cd\u65b9\u6cd5\u6ce8\u518c\u7684\u8bbe\u5907\u7edf\u4e00\u7528\u4ee5\u4e0b\u51fd\u6570\u5378\u8f7d\uff0c\u8be5\u51fd\u6570\u901a\u5e38\u5728\u6e05\u7406\u51fd\u6570\u4e2d\u8c03\u7528\uff1a

void unregister_chrdev_region(dev_t first, unsigned count)\n

\u8fd9\u91cc\u6211\u4eec\u7ed9\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u540d\u5b57\u4e3a\"test\"\u7684\u8bbe\u5907\u3002

int major;\nint minor;\ndev_t devid;\n\nif(major){\n    /*\u5982\u679c\u6307\u5b9a\u4e86major*/\n    devid = MKDEV(major, 0);\n    register_chrdev_region(devid, 1, \"test\");\n}else {\n    /*\u5982\u679c\u6ca1\u6709\u6307\u5b9a*/\n    alloc_chrdev_region(&devid, 0, 1, \"test\");\n    major = MAJOR(devid);\n    minor = MINOR(devid);\n}\n

\u5bf9\u8bbe\u5907\u53f7\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u5230\u4e86\u4e09\u4e2a\u5b8f\u5b9a\u4e49\uff0cMKDEV, MAJOR, MINOR\u3002\u5f53\u7ed9\u5b9a\u4e3b\u8bbe\u5907\u53f7\u65f6\uff0c\u4f7f\u7528MKDEV\u6765\u6784\u5efa\u5b8c\u6574\u7684devID\uff0c\u6b21\u8bbe\u5907\u53f7\u5219\u4e00\u822c\u9009\u62e90\u3002

"},{"location":"linux/drivers/chardev/#_6","title":"\u5b57\u7b26\u8bbe\u5907\u7684\u6ce8\u518c","text":"

\u5185\u6838\u4f7f\u7528struct cdev\u7ed3\u6784\u6765\u8868\u793a\u5b57\u7b26\u8bbe\u5907\uff0c\u5728\u5b9e\u73b0\u4e86file_operations\u7ed3\u6784\u4f53\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528cdev_init()\u51fd\u6570\u6765\u5c06\u6587\u4ef6\u64cd\u4f5c\u6307\u9488\u4e0e\u4f60\u6ce8\u518c\u7684\u5b57\u7b26\u8bbe\u5907\u76f8\u5173\u8054\u3002

void cdev_init(struct cdev *cdev, const struct file_operations *fops)\n

\u6ce8\u518c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u8c03\u7528cdev_add()\u51fd\u6570\u5411\u5185\u6838\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u8bbe\u5907\uff0ccdev_del()\u51fd\u6570\u7528\u6765\u5220\u9664\u3002

int cdev_add(struct cdev *p, dev_t dev, unsigned count)\nvoid cdev_del(struct cdev *p)\n
"},{"location":"linux/drivers/chardev/#_7","title":"\u5185\u5b58\u6620\u5c04","text":""},{"location":"linux/drivers/chardev/#_8","title":"\u9a71\u52a8\u5c42\u7684\u64cd\u4f5c","text":"

\u7531\u4e8eLinux\u6709MMU\u6a21\u5757\uff0c\u56e0\u6b64\u65e0\u6cd5\u8bbf\u95ee\u771f\u5b9e\u7684\u7269\u7406\u5730\u5740\uff0c\u53ea\u80fd\u901a\u8fc7\u865a\u62df\u5730\u5740\u8bbf\u95ee\u786c\u4ef6\u5916\u8bbe\u3002\u6240\u4ee5\u9700\u8981\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u5728\u7269\u7406\u5730\u5740\u548c\u865a\u62df\u5730\u5740\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002

\u5173\u4e8e\u5982\u4f55\u83b7\u53d6\u786c\u4ef6\u7684\u7269\u7406\u5730\u5740\uff0c\u8bf7\u53c2\u8003\u8bbe\u5907\u6811\u3002

\u5730\u5740\u6620\u5c04\u51fd\u6570\u5982\u4e0b\uff1a

void __iomem *ioremap(phys_addr_t paddr, unsigned long size)\n

paddr\uff1a\u9700\u8981\u8bbf\u95ee\u7684\u7269\u7406\u5730\u5740\u3002

size\uff1a\u9700\u8981\u8f6c\u5316\u7684\u5927\u5c0f\u3002

\u8fd4\u56de\u503c\uff1a\u865a\u62df\u5730\u5740\u3002

\u53d6\u6d88\u5730\u5740\u6620\u5c04\u51fd\u6570\uff1a

void iounmap(void *addr)\n
"},{"location":"linux/drivers/chardev/#io","title":"I/O\u5185\u5b58\u8bbf\u95ee\u51fd\u6570","text":"

\u4f7f\u7528ioremap\u51fd\u6570\u5c06\u5bc4\u5b58\u5668\u7269\u7406\u5730\u5740\u6620\u5c04\u5230\u865a\u62df\u5730\u5740\u540e\uff0cLinux\u63a8\u8350\u4f7f\u7528\u5185\u6838\u81ea\u5e26\u7684\u8bfb\u5199\u64cd\u4f5c\u51fd\u6570\u5bf9\u6620\u5c04\u540e\u7684\u5185\u5b58\u8fdb\u884c\u8bfb\u5199\u3002

unsigned int ioread8(void __iomem *addr)\nunsigned int iorea16(void __iomem *addr)\nunsigned int ioread32(void __iomem *addr)\n\nvoid iowrite8(u8 b, void __iomem *addr)\nvoid iowrite16(u16 b, void __iomem *addr)\nvoid iowrite32(u32 b, void __iomem *addr)\n

\u5bf9\u4e8e\u8bfbI/O\u800c\u8a00\uff0c\u8f93\u5165\u53c2\u6570\u4e3a__iomem\u7c7b\u578b\u7684\u6307\u9488\uff0c\u6307\u5411\u88ab\u6620\u5c04\u540e\u7684\u5730\u5740\uff0c\u8fd4\u56de\u503c\u4e3a\u8bfb\u53d6\u5230\u7684\u6570\u636e\uff1b\u5bf9\u4e8e\u5199I/O\u800c\u8a00\uff0c\u8f93\u5165\u53c2\u6570\u7b2c\u4e00\u4e2a\u4e3a\u8981\u5199\u5165\u7684\u6570\u636e\uff0c\u7b2c\u4e8c\u4e2a\u4e3a\u8981\u5199\u5165\u7684\u5730\u5740\uff0c\u8fd4\u56de\u503c\u4e3a\u7a7a\u3002

\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u5730\u5740\u6620\u5c04\u7684\u76f8\u5173\u64cd\u4f5c\uff1a

unsigned long pa_dr = 0x20A8000 + 0x00;\nunsigned int __iomem *va_dr;\nunsigned int val;\nva_dr = ioremap(pa_dr, 4);\nval = ioread32(va_dr);\nval &= ~(0x01 << 19);\niowrite32(val, va_dr);\n
"},{"location":"linux/drivers/chardev/#_9","title":"\u5e94\u7528\u5c42\u7684\u64cd\u4f5c","text":"

\u7531\u4e8eLinux\u5185\u6838\u7981\u6b62\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u76f4\u63a5\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\uff0c\u6211\u4eec\u53ea\u80fd\u901a\u8fc7\u5185\u6838\u63d0\u4f9b\u7684API\u51fd\u6570\u8fdb\u884c\u3002

unsigned long copy_from_user(void *to, const void *from, unsigned long count)\n\nunsigned long copy_to_user(void *to, const void *from, unsigned long count)\n
"},{"location":"linux/drivers/device_model/","title":"\u8bbe\u5907\u6a21\u578b","text":"

\u7531\u4e8eLinux\u652f\u6301\u4e16\u754c\u4e0a\u51e0\u4e4e\u6240\u6709\u7684\u3001\u4e0d\u540c\u529f\u80fd\u7684\u786c\u4ef6\u8bbe\u5907\uff0c\u5bfc\u81f4Linux\u5185\u6838\u4e2d\u6709\u4e00\u534a\u7684\u4ee3\u7801\u90fd\u662f\u8bbe\u5907\u9a71\u52a8\u3002\u968f\u7740\u786c\u4ef6\u7684\u5feb\u901f\u8fed\u4ee3\uff0c\u8bbe\u5907\u9a71\u52a8\u7684\u4ee3\u7801\u4e5f\u5728\u5feb\u901f\u589e\u957f\u3002

\u4e3a\u4e86\u964d\u4f4e\u8bbe\u5907\u7684\u591a\u6837\u6027\u5e26\u6765\u7684\u9a71\u52a8\u5f00\u53d1\u7684\u590d\u6742\u5ea6\uff0cLinux\u63d0\u51fa\u4e86\u8bbe\u5907\u6a21\u578b\uff08device model\uff09\u7684\u6982\u5ff5\uff0c\u8be5\u6a21\u578b\u5c06\u8bbe\u5907\u548c\u9a71\u52a8\u5206\u5c42\uff0c\u628a\u6211\u4eec\u7f16\u5199\u7684\u9a71\u52a8\u4ee3\u7801\u5206\u6210\u4e86\u4e24\u5757\uff1a\u8bbe\u5907\u4e0e\u9a71\u52a8\u3002\u8bbe\u5907\u8d1f\u8d23\u63d0\u4f9b\u786c\u4ef6\u8d44\u6e90\u800c\u9a71\u52a8\u8d1f\u8d23\u53bb\u4f7f\u7528\u8bbe\u5907\u63d0\u4f9b\u7684\u786c\u4ef6\u8d44\u6e90\u3002\u4e8c\u8005\u7531\u603b\u7ebf\u5173\u8054\u8d77\u6765\u3002

\u8bbe\u5907\u6a21\u578b\u901a\u8fc7\u51e0\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u53cd\u6620\u5f53\u524d\u7cfb\u7edf\u4e2d\u603b\u7ebf\u3001\u8bbe\u5907\u4ee5\u53ca\u9a71\u52a8\u5de5\u4f5c\u7684\u60c5\u51b5\uff1a

  • \u603b\u7ebf(bus)\uff1a\u603b\u7ebf\u662fCPU\u548c\u8bbe\u5907\u4e4b\u95f4\u4fe1\u606f\u4ea4\u4e92\u7684\u901a\u9053\uff0c\u6240\u6709\u7684\u8bbe\u5907\u90fd\u5e94\u8fde\u63a5\u5230\u603b\u7ebf\u4e0a\uff0c\u65e0\u8bba\u662fCPU\u5185\u90e8\u603b\u7ebf\u8fd8\u662f\u865a\u62df\u603b\u7ebf\u3002

  • \u7c7b\uff08class\uff09\uff1a\u9762\u5411\u5bf9\u8c61\u601d\u60f3\uff0c\u5c06\u76f8\u540c\u529f\u80fd\u7684\u8bbe\u5907\uff0c\u5f52\u7ed3\u5230\u4e00\u79cdclass\u7edf\u4e00\u7ba1\u7406\u3002

  • \u8bbe\u5907\uff08device\uff09\uff1a\u6302\u8f7d\u5728\u603b\u7ebf\u7684\u7269\u7406\u8bbe\u5907\u3002

  • \u9a71\u52a8\uff08driver\uff09\uff1a\u786c\u4ef6\u8bbe\u5907\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u8d1f\u8d23\u521d\u59cb\u5316\u8bbe\u5907\u4ee5\u53ca\u5b9e\u73b0\u8be5\u8bbe\u5907\u7684\u4e00\u4e9b\u63a5\u53e3\u51fd\u6570\u3002

platform bus\u662f\u5185\u6838\u4e2d\u7684\u4e00\u79cd\u865a\u62df\u603b\u7ebf\u7c7b\u578b\uff0c\u5b83\u4e0d\u662f\u7269\u7406\u4e0a\u5b58\u5728\u7684\u603b\u7ebf\uff0c\u800c\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u603b\u7ebf\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u4ee5\u4e00\u79cd\u6807\u51c6\u7684\u65b9\u5f0f\u6765\u63cf\u8ff0\u548c\u7ba1\u7406\u90a3\u4e9b\u4e0d\u901a\u8fc7\u4f20\u7edf\u7269\u7406\u603b\u7ebf\u8fde\u63a5\u7684\u8bbe\u5907\u3002

Linux\u5185\u6838\u4f7f\u7528sysfs\u6587\u4ef6\u7cfb\u7edf\u5c06\u5185\u6838\u7684\u8bbe\u5907\u9a71\u52a8\u5bfc\u51fa\u5230\u7528\u6237\u7a7a\u95f4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee/sys\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff0c\u6765\u67e5\u770b\u751a\u81f3\u63a7\u5236\u5185\u6838\u7684\u4e00\u4e9b\u9a71\u52a8\u8bbe\u5907\u3002

/sys\u6587\u4ef6\u76ee\u5f55\u8bb0\u5f55\u4e86\u5404\u4e2a\u8bbe\u5907\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5176\u4e2d\uff0c/sys/bus\u76ee\u5f55\u4e0b\u7684\u6bcf\u4e2a\u5b50\u76ee\u5f55\u90fd\u662f\u5df2\u7ecf\u6ce8\u518c\u7684\u603b\u7ebf\u7c7b\u578b\u3002\u6bcf\u4e2a\u603b\u7ebf\u7c7b\u578b\u4e0b\u8fd8\u6709\u4e24\u4e2a\u6587\u4ef6\u5939\u2014\u2014devices\u548cdrivers\uff1bdevices\u662f\u8be5\u603b\u7ebf\u7c7b\u578b\u4e0b\u7684\u6240\u6709\u8bbe\u5907\uff0c\u4ee5\u7b26\u53f7\u94fe\u63a5\u7684\u5f62\u5f0f\u6307\u5411\u771f\u6b63\u7684\u8bbe\u5907\uff08/sys/devices/\uff09\u3002\u800cdrivers\u662f\u6240\u6709\u6ce8\u518c\u5728\u8fd9\u4e2a\u603b\u7ebf\u7c7b\u578b\u4e0a\u7684\u9a71\u52a8\u3002

/sys/devices\u76ee\u5f55\u4e0b\u662f\u5168\u5c40\u7684\u8bbe\u5907\u7ed3\u6784\u4f53\u7cfb\uff0c\u5305\u542b\u4e86\u6240\u6709\u6ce8\u518c\u5728\u5404\u7c7b\u603b\u7ebf\u4e0a\u7684\u7269\u7406\u8bbe\u5907\u3002\u6240\u6709\u7684\u7269\u7406\u8bbe\u5907\u4ee5\u603b\u7ebf\u62d3\u6251\u7684\u7ed3\u6784\u6765\u663e\u793a\u3002

/sys/class\u76ee\u5f55\u4e0b\u662f\u5305\u542b\u6240\u6709\u6ce8\u518c\u5728\u5185\u6838\u4e2d\u7684\u8bbe\u5907\u7c7b\u578b\uff0c\u6309\u7167\u8bbe\u5907\u7684\u529f\u80fd\u8fdb\u884c\u5206\u7c7b\u3002\u6bd4\u5982\u9f20\u6807\u7684\u529f\u80fd\u662f\u4f5c\u4e3a\u4eba\u673a\u4ea4\u4e92\u7684\u8f93\u5165\uff0c\u4e8e\u662f\u88ab\u5f52\u7c7b\u5230/sys/class/input\u76ee\u5f55\u4e0b\u3002

\u90a3\u4e48\u201c\u603b\u7ebf-\u8bbe\u5907-\u9a71\u52a8\u201d\u662f\u5982\u4f55\u914d\u5408\u5de5\u4f5c\u7684\u5462\uff1f

\u5728\u603b\u7ebf\u4e0a\u6302\u8f7d\u4e86\u4e24\u4e2a\u94fe\u8868\uff0c\u5206\u522b\u7ba1\u7406\u8bbe\u5907\u6a21\u578b\u548c\u9a71\u52a8\u6a21\u578b\u3002\u5f53\u6211\u4eec\u5411\u7cfb\u7edf\u6ce8\u518c\u4e00\u4e2a\u8bbe\u5907\u65f6\uff0c\u4fbf\u4f1a\u5728\u8bbe\u5907\u7684\u94fe\u8868\u4e2d\u63d2\u5165\u65b0\u7684\u8bbe\u5907\u3002\u5728\u63d2\u5165\u7684\u540c\u65f6\u603b\u7ebf\u4f1a\u6267\u884cmatch()\u65b9\u6cd5\u5bf9\u65b0\u63d2\u5165\u7684\u8bbe\u5907/\u9a71\u52a8\u8fdb\u884c\u914d\u5bf9\u3002\u82e5\u914d\u5bf9\u6210\u529f\u5219\u8c03\u7528probe()\u65b9\u6cd5\u83b7\u53d6\u8bbe\u5907\u8d44\u6e90\uff0c\u5728\u79fb\u9664\u8bbe\u5907/\u9a71\u52a8\u65f6\uff0c\u8c03\u7528remove()\u65b9\u6cd5\u3002

"},{"location":"linux/drivers/device_model/#kobjectktypekset","title":"kobject\u3001ktype\u548ckset","text":"

kobject\u662fLinux\u8bbe\u5907\u6a21\u578b\u7684\u57fa\u7840\uff0c\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u3001\u7edf\u4e00\u7684\u5bf9\u5927\u91cf\u786c\u4ef6\u8bbe\u5907\u7684\u63cf\u8ff0\u3002\u5b83\u4e3b\u8981\u63d0\u4f9b\u4ee5\u4e0b\u529f\u80fd\uff1a

  1. \u901a\u8fc7parent\u6307\u9488\uff0c\u5c06\u6240\u6709kobject\u4ee5\u6811\u72b6\u7ed3\u6784\u7684\u5f62\u5f0f\u7ec4\u5408\u8d77\u6765\u3002
  2. \u4f7f\u7528\u5f15\u7528\u8ba1\u6570kref\uff0c\u6765\u8bb0\u5f55kobject\u88ab\u5f15\u7528\u7684\u6b21\u6570\uff0c\u5728\u8ba1\u6570\u4e3a0\u65f6\u91ca\u653e\u5b83\u3002
  3. \u548csysfs\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u914d\u5408\uff0c\u5c06\u6bcf\u4e00\u4e2akobject\u7684\u7279\u6027\uff0c\u4ee5\u6587\u4ef6\u7684\u5f62\u5f0f\u5f00\u653e\u7ed9\u7528\u6237\u7a7a\u95f4\u67e5\u8be2\u3002
  4. \u901a\u8fc7uevent\u673a\u5236\uff0c\u5c06\u70ed\u63d2\u62d4\u4e8b\u4ef6\uff08\u6bd4\u5982\u4e00\u4e2a\u8bbe\u5907\u901a\u8fc7USB\u8fde\u63a5\u5230\u7cfb\u7edf\uff09\u901a\u77e5\u7528\u6237\u7a7a\u95f4\u3002

\u5185\u6838\u5f88\u5c11\u5355\u72ec\u521b\u5efakobject\u5bf9\u8c61\uff0c\u800c\u662f\u5c06\u5176\u4f5c\u4e3a\u9876\u5c42\u57fa\u7c7b\uff08C\u8bed\u8a00\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u7684\u673a\u5236\uff09\uff0c\u5d4c\u5165\u5230\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u3002\u6bd4\u5982struct cdev\u7ed3\u6784\u4f53\uff1a

struct cdev {\n    struct kobject kobj;\n    struct module *owner;\n    struct file_opeartions *ops;\n    struct list_head list;\n    dev_t dev;\n    unsigned int count;\n};\n

\u5982\u6b64\u4e00\u6765\uff0c\u8981\u4f7f\u7528kobject\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8bbf\u95eecdev.kobj\u5c31\u53ef\u4ee5\u3002\u5f53\u6211\u4eec\u5df2\u77e5\u4e00\u4e2akobject\u6307\u9488\uff0c\u53ef\u4ee5\u901a\u8fc7container_of\u5b8f\u7684\u65b9\u5f0f\u83b7\u53d6\u4e0a\u5c42\u6570\u636e\u7ed3\u6784\u7684\u6307\u9488\uff1a

struct cdev *device = container_of(kp, struct cdev, kobj);\n

\u8bbe\u5907\u9a71\u52a8\u6a21\u578b\u7684\u57fa\u672c\u5143\u7d20\u6709\u4e09\u4e2a\uff1a

  • kobject\uff1asysfs\u4e2d\u7684\u4e00\u4e2a\u76ee\u5f55\uff0c\u8868\u793a\u57fa\u672c\u9a71\u52a8\u5bf9\u8c61\u3002
  • kset\uff1a\u4e00\u4e2a\u7279\u6b8a\u7684kobject\uff0c\u7528\u6765\u7ba1\u7406\u7c7b\u4f3c\u7684kobject\u3002
  • ktype\uff1a\u76ee\u5f55\u4e0bkobject\u5c5e\u6027\u6587\u4ef6\u64cd\u4f5c\u7684\u63a5\u53e3\u3002

kobject\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct kobject {\n    const char      *name;\n    struct list_head    entry;\n    struct kobject      *parent;\n    struct kset     *kset;\n    struct kobj_type    *ktype;\n    struct kernfs_node  *sd; /* sysfs directory entry */\n    struct kref     kref;\n    unsigned int state_initialized:1;\n    unsigned int state_in_sysfs:1;\n    unsigned int state_add_uevent_sent:1;\n    unsigned int state_remove_uevent_sent:1;\n    unsigned int uevent_suppress:1;\n};\n

name\uff1akobject\u7684\u540d\u79f0\uff0c\u540c\u65f6\u4e5f\u662fsysfs\u4e2d\u7684\u76ee\u5f55\u540d\u79f0\u3002\u5f53kobject\u6dfb\u52a0\u5230\u5185\u6838\u65f6\uff0c\u9700\u8981\u6839\u636ename\u6ce8\u518c\u5230sysfs\u4e2d\u3002

entry\uff1a\u7528\u4e8e\u5c06kobject\u52a0\u5165\u5230\u94fe\u8868\u4e2d\u3002

parent\uff1a\u6307\u5411\u7236kobject\u7684\u6307\u9488\uff0c\u5728sysfs\u4e2d\u8868\u793a\u4e0a\u4e00\u5c42\u7684\u8282\u70b9\u3002

kset\uff1a\u8be5kobject\u5c5e\u4e8e\u7684kset\u3002\u82e5\u8be5kobject\u672a\u6307\u5b9aparent\uff0c\u5219\u4f1a\u628akset\u4f5c\u4e3aparent\u3002

ktype\uff1a\u8be5kobject\u5c5e\u4e8e\u7684kobj_type\uff0c\u6bcf\u4e2akobject\u5fc5\u987b\u6709\u4e00\u4e2aktype\u3002

sd\uff1a\u8be5kobject\u5728sysfs\u4e2d\u7684\u5bf9\u5e94\u76ee\u5f55\u9879\u3002

kref\uff1a\u539f\u5b50\u5f15\u7528\u8ba1\u6570\u3002

state_initialized\uff1a\u6307\u793a\u8be5kobject\u662f\u5426\u5df2\u7ecf\u521d\u59cb\u5316\u3002

state_in_sysfs\uff1a\u6307\u793a\u8be5kobject\u662f\u5426\u5df2\u5728sysfs\u4e2d\u5efa\u7acb\u76ee\u5f55\u3002

state_add_uevent_sent/state_remove_uevent_sent\uff1a\u8bb0\u5f55\u662f\u5426\u5df2\u5411\u7528\u6237\u7a7a\u95f4\u53d1\u9001add uevent\u3002

uevent_suppress\uff1a\u5982\u679c\u8be5\u5b57\u6bb5\u4e3a1\uff0c\u5219\u8868\u793a\u5ffd\u7565\u6240\u6709\u4e0a\u62a5\u7684uevent\u4e8b\u4ef6\u3002

kobject\u6838\u5fc3\u673a\u5236\uff1a

\u5185\u5d4c\u5728\u522b\u7684\u6570\u636e\u7ed3\u6784\uff08\u6bd4\u5982device_driver\uff09\u4e2d\uff0c\u5f53kobject\u4e2d\u7684\u5f15\u7528\u8ba1\u6570\u5f52\u96f6\u65f6\uff0c\u91ca\u653ekobject\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u540c\u65f6\u901a\u8fc7ktype\u4e2d\u7684release()\u56de\u8c03\u51fd\u6570\uff0c\u91ca\u653e\u5185\u5d4c\u6570\u636e\u7ed3\u6784\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u6bcf\u4e00\u4e2a\u5185\u5d4ckobject\u7684\u6570\u636e\u7ed3\u6784\u90fd\u9700\u8981\u81ea\u5df1\u5b9e\u73b0ktype\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u3002

ktype\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct kobj_type {\n    void (*release)(struct kobject *kobj);\n    const struct sysfs_ops *sysfs_ops;\n    const struct attribute_group **default_groups;\n    /*\u7701\u7565\u5176\u4ed6\u6210\u5458*/  \n};\n

release\uff1a\u5f53kobject\u5f15\u7528\u8ba1\u6570\u5f52\u96f6\u65f6\u8c03\u7528\u8be5\u6790\u6784\u51fd\u6570\uff0c\u8d1f\u8d23\u91ca\u653ekobject\u7684\u5185\u5b58\u3002

sysfs_ops\uff1asysfs\u6587\u4ef6\u7cfb\u7edf\u8bfb\u5199\u65f6\u7684\u7279\u6027\u3002

default_groups\uff1a\u5b9a\u4e49\u4e86kobject\u7684\u5c5e\u6027\uff0c\u7531struct attritube\u548cstruct bin_attribute\u6784\u6210\u3002

ktype\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u63cf\u8ff0\u4e00\u65cfkobject\u6240\u5177\u6709\u7684\u666e\u904d\u7279\u6027\u3002\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u6bcf\u4e2akobject\u5b9a\u4e49\u81ea\u5df1\u7684\u7279\u6027\uff0c\u800c\u662f\u5728ktype\u4e2d\u7edf\u4e00\u5b9a\u4e49\u3002

kset\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct kset {\n    struct list_head list;\n    spinlock_t list_lock;\n    struct kobject kobj;\n    const struct kset_uevent_ops *uevent_ops;\n};\n

list/list_lock\uff1a\u7528\u4e8e\u4fdd\u5b58\u8be5kset\u4e0b\u6240\u6709kobject\u5bf9\u8c61\u3002

kobj\uff1a\u8be5kset\u81ea\u5df1\u7684kobject\u3002

uevent_ops\uff1auevent\u662f\u7528\u6237\u7a7a\u95f4\u7684\u7f29\u5199\uff0c\u63d0\u4f9b\u4e86\u4e0e\u7528\u6237\u7a7a\u95f4\u70ed\u63d2\u62d4\u8fdb\u884c\u901a\u4fe1\u7684\u673a\u5236\u3002\u5f53\u4efb\u4f55kobject\u9700\u8981\u4e0a\u62a5uevent\u65f6\uff0c\u90fd\u8981\u8c03\u7528\u6240\u5c5e\u7684kset\u4e2duevent_ops\u4e2d\u7684\u51fd\u6570\u3002uevent\u7684\u6982\u5ff5\u7a0d\u540e\u8bf4\u660e\u3002

kset\u662fkobject\u5bf9\u8c61\u7684\u96c6\u5408\u4f53\u3002\u5b83\u4e0ektype\u7684\u533a\u522b\u5728\u4e8e\uff1a\u5177\u6709\u76f8\u540cktype\u7684kobject\u53ef\u4ee5\u88ab\u5206\u7ec4\u5230\u4e0d\u540c\u7684kset\u3002

\u5f53\u8bbe\u7f6e\u4e86kset\u5e76\u628a\u5b83\u6dfb\u52a0\u5230\u7cfb\u7edf\u4e2d\uff0c\u5c06\u5728sysfs\u4e2d\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u3002kobject\u7684\u6dfb\u52a0\u4e0e\u5220\u9664\u4e3b\u8981\u662fkobject_regsiter()\u51fd\u6570\u548ckobject_unregister()\u51fd\u6570\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0ckobject\u4f1a\u5728\u5176parent\u6307\u9488\u4e2d\u4fdd\u5b58kset\u7684\u6307\u9488\u3002

"},{"location":"linux/drivers/device_model/#sysfs","title":"sysfs","text":"

sysfs\u6587\u4ef6\u7cfb\u7edf\u662f\u4e00\u4e2a\u5904\u4e8e\u5185\u5b58\u4e2d\u7684\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff0c\u5b83\u63d0\u4f9b\u4e86kobject\u5bf9\u8c61\u7684\u5c42\u6b21\u7ed3\u6784\u89c6\u56fe\u3002\u7528\u6237\u67e5\u8be2\u7cfb\u7edf\u4e2d\u5404\u79cd\u8bbe\u5907\u7684\u62d3\u6251\u7ed3\u6784\uff0c\u5c31\u50cf\u67e5\u8be2\u6587\u4ef6\u76ee\u5f55\u4e00\u6837\u7b80\u5355\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u51fa\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u5c06\u5185\u6838\u53d8\u91cf\u63d0\u4f9b\u7ed9\u7528\u6237\u8bfb\u53d6\u6216\u8005\u5199\u5165\u3002

\u76ee\u5f55 \u529f\u80fd block \u5757\u8bbe\u5907 bus \u603b\u7ebf class \u5c06\u8bbe\u5907\u6309\u7167\u529f\u80fd\u5206\u7c7b dev block\u548cchar\u4e24\u4e2a\u76ee\u5f55\uff0c\u5bf9\u5e94\u90e8\u5206\u5757\u8bbe\u5907\u548c\u5b57\u7b26\u8bbe\u5907\u7b26\u53f7\u94fe\u63a5 devices \u6240\u6709\u8bbe\u5907 firmwares \u56fa\u4ef6\u4fe1\u606f fs \u6587\u4ef6\u7cfb\u7edf hypervisor \u7a7a\u76ee\u5f55 kernel \u5185\u6838\u914d\u7f6e\u53c2\u6570 module \u6a21\u5757\u4fe1\u606f power \u7535\u6e90\u7ba1\u7406

\u5176\u4e2d\u6700\u91cd\u8981\u7684\u76ee\u5f55\u662fdevices\uff0cLinux\u7cfb\u7edf\u4e2d\u6240\u6709\u8bbe\u5907\u90fd\u53ef\u4ee5\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u627e\u5230\u3002\u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u8bbe\u5907\u5176\u5b9e\u90fd\u662fdevices\u76ee\u5f55\u4e2d\u7684\u7b26\u53f7\u94fe\u63a5\u3002

sysfs\u7684\u6838\u5fc3\u662f\u628akobject\u5bf9\u8c61\u4e0e\u76ee\u5f55\u9879\uff08directory entries\uff09\u5173\u8054\u8d77\u6765\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u67e5\u770b\u6302\u8f7d\u4e8e/sys\u76ee\u5f55\u4e0b\u7684\u6574\u4e2a\u6587\u4ef6\u7cfb\u7edf\u89c6\u56fe\u3002\u5411sysfs\u4e2d\u6dfb\u52a0kobject\uff0c\u53ef\u4ee5\u4f7f\u7528kobject_add()\u548ckobject_create_and_add()\u51fd\u6570\u3002

\u6211\u4eec\u5df2\u7ecf\u77e5\u9053kobject\u53ef\u4ee5\u88ab\u6620\u5c04\u4e3a\u67d0\u4e2a\u6587\u4ef6\u76ee\u5f55\uff0c\u4ec5\u6709\u5982\u6b64\u8fd8\u4e0d\u591f\u3002\u56e0\u4e3a\u8fd9\u6837\u7684sysfs\u4ec5\u4ec5\u53ea\u662f\u4e00\u9897\u6811\uff0c\u4f46\u6ca1\u6709\u63d0\u4f9b\u5b9e\u9645\u6570\u636e\u7684\u6587\u4ef6\u3002\u4e3a\u4e86\u80fd\u591f\u8bfb\u5199\u8fd9\u9897\u6811\uff0c\u5185\u6838\u63d0\u4f9b\u4e86attribute\u548cbin_attribute\u4e24\u79cd\u5c5e\u6027\u3002

\u5728ktype\u4e2d\u7528struct attribute_group\u63cf\u8ff0\u8fd9\u4e24\u4e2a\u5c5e\u6027\uff1a

struct attribute_group {\n    const char *name;\n    struct attribute **attrs;\n    struct bin_attribute **bin_attrs;\n};\n\n/*attribute\u548cbin_attribute\u7684\u5b9a\u4e49\u5982\u4e0b*/\n\nstruct attribute {\n    const char *name;   //\u5c5e\u6027\u7684\u540d\u5b57\n    umode_t mode;       //\u5c5e\u6027\u7684\u6743\u9650\n};\n\nstruct bin_attribute {\n    struct attribute attr;\n    size_t size;\n    void *private;\n    struct address_space *(*f_mapping)(void);\n    ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,\n            char *, loff_t, size_t);\n    ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,\n             char *, loff_t, size_t);\n    int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,\n            struct vm_area_struct *vma);\n};\n

\u5bf9\u4e8e\u9ed8\u8ba4\u5c5e\u6027\u7684\u5b9e\u73b0\uff0c\u7531ktype->sysfs_ops\u6210\u5458\u63cf\u8ff0\uff1a

struct sysfs_ops {\n    ssize_t (*show)(struct kobject *, struct attribute *, char *);\n    ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);\n};\n

\u5f53\u7528\u6237\u7a7a\u95f4\u8bfb\u53d6\u4e00\u4e2a\u5c5e\u6027\u65f6\uff0c\u5185\u6838\u4f1a\u8c03\u7528show()\u65b9\u6cd5\uff1b\u5f53\u5199\u4e00\u4e2a\u5c5e\u6027\u65f6\uff0c\u8c03\u7528store()\u65b9\u6cd5\u3002

struct attribute\u4e3a\u666e\u901a\u7684attribute\uff0c\u4f7f\u7528\u8be5attribute\u751f\u6210\u7684sysfs\u6587\u4ef6\uff0c\u53ea\u80fd\u7528\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u8bfb\u5199\u3002\u800cstruct bin_attribute\u5728struct attribute\u7684\u57fa\u7840\u4e0a\uff0c\u589e\u52a0\u4e86read()\u3001write()\u7b49\u51fd\u6570\uff0c\u56e0\u6b64\u5b83\u6240\u751f\u6210\u7684sysfs\u6587\u4ef6\u53ef\u4ee5\u7528\u4efb\u4f55\u65b9\u5f0f\u8bfb\u5199\u3002

"},{"location":"linux/drivers/device_model/#uevent","title":"uevent","text":"

uevent\u662fkobject\u529f\u80fd\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u5728kobject\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u6bd4\u5982\u6dfb\u52a0\u3001\u79fb\u9664\uff0c\u901a\u77e5\u7528\u6237\u7a7a\u95f4\u3002\u7528\u6237\u7a7a\u95f4\u6536\u5230\u8baf\u606f\u540e\uff0c\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002

\u8be5\u673a\u5236\u901a\u5e38\u662f\u7528\u6765\u652f\u6301\u70ed\u63d2\u62d4\uff08hotplug\uff09\u8bbe\u5907\u7684\uff0c\u4f8b\u5982\u5f53U\u76d8\u63d2\u5165\u540e\uff0cUSB\u76f8\u5173\u7684\u9a71\u52a8\u4f1a\u52a8\u6001\u521b\u5efa\u7528\u4e8e\u8868\u793a\u8be5U\u76d8\u7684device\u7ed3\u6784\uff0c\u5e76\u544a\u77e5\u7528\u6237\u7a7a\u95f4\u4e3a\u8be5U\u76d8\u52a8\u6001\u521b\u5efa/dev/\u76ee\u5f55\u4e0b\u7684\u8bbe\u5907\u8282\u70b9\u3002

uevent\u673a\u5236\u6bd4\u8f83\u7b80\u5355\uff0c\u5f53\u8bbe\u5907\u6a21\u578b\u4e2d\u4efb\u4f55\u8bbe\u5907\u6709\u4e8b\u4ef6\u9700\u8981\u4e0a\u62a5\u65f6\uff0c\u90fd\u4f1a\u89e6\u53d1uevent\u63d0\u4f9b\u7684\u63a5\u53e3\u3002uevent\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e2a\u9014\u5f84\u628a\u4e8b\u4ef6\u4e0a\u62a5\u5230\u7528\u6237\u7a7a\u95f4\uff1a\u4e00\u79cd\u662f\u901a\u8fc7kmod\u6a21\u5757\uff0c\u53e6\u4e00\u79cd\u662f\u901a\u8fc7netlink\u901a\u4fe1\u673a\u5236\u3002

uevent\u7684\u4ee3\u7801\u4e3b\u8981\u4f4d\u4e8ekobject.h\u548ckobject_uevent.c\u4e24\u4e2a\u6587\u4ef6\u3002

kobject_action\u5b9a\u4e49\u4e86uevent\u7684\u7c7b\u578b\uff1a

enum kobject_action {\n    KOBJ_ADD,\n    KOBJ_REMOVE,\n    KOBJ_CHANGE,\n    KOBJ_MOVE,\n    KOBJ_ONLINE,\n    KOBJ_OFFLINE,\n    KOBJ_BIND,\n    KOBJ_UNBIND,\n};\n

ADD/REMOVE\uff1akobject\u7684\u6dfb\u52a0/\u79fb\u9664\u4e8b\u4ef6\u3002

CHANGE\uff1akobject\u72b6\u6001\u6216\u8005\u5185\u5bb9\u53d1\u751f\u6539\u53d8\u3002

MOVE\uff1akobject\u66f4\u6539\u540d\u79f0\u6216\u8005\u66f4\u6539\u4e86parent\u3002

ONLINE/OFFLINE\uff1akobject\u7684\u4e0a\u7ebf/\u4e0b\u7ebf\u4e8b\u4ef6

BIND/UNBIND\uff1akobject\u7684\u7ed1\u5b9a/\u89e3\u7ed1\u4e8b\u4ef6

kobj_uevent_env\u5b9a\u4e49\u4e86\u4e8b\u4ef6\u4e0a\u62a5\u65f6\u7684\u73af\u5883\u53d8\u91cf\uff1a

struct kobj_uevent_env {\n    char *argv[3];\n    char *envp[UEVENT_NUM_ENVP];\n    int envp_idx;\n    char buf[UEVENT_BUFFER_SIZE];\n    int buflen;\n};\n

argv\uff1a\u6307\u9488\u6570\u7ec4\uff0c\u53ef\u4ee5\u4fdd\u5b58\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u6700\u5927\u4e3a3\u4e2a\u3002

envp\uff1a\u6307\u9488\u6570\u7ec4\uff0c\u7528\u4e8e\u4fdd\u5b58\u6bcf\u4e2a\u73af\u5883\u53d8\u91cf\u7684\u5730\u5740\u3002

envp_idx\uff1a\u8bbf\u95eeenvp\u6570\u7ec4\u7684\u7d22\u5f15\u3002

buf\uff1a\u4fdd\u5b58uevent\u6d88\u606f\u7684\u7f13\u51b2\u533a

buflen\uff1a\u5b58\u50a8\u7f13\u51b2\u533a\u7684\u5927\u5c0f

kset_uevent_ops\u5b9a\u4e49\u4e86kset\u7684uevent\u63a5\u53e3\u64cd\u4f5c\uff1a

struct kset_uevent_ops {\n    int (* const filter)(struct kset *kset, struct kobject *kobj);\n    const char *(* const name)(struct kset *kset, struct kobject *kobj);\n    int (* const uevent)(struct kset *kset, struct kobject *kobj, struct kobj_uevent_env *env);\n};\n

filter\uff1a\u5f53kobject\u9700\u8981\u4e0a\u62a5uevent\u65f6\uff0c\u5b83\u6240\u5c5e\u7684kset\u53ef\u4ee5\u901a\u8fc7\u6b64\u63a5\u53e3\u8fc7\u6ee4\u6389\u4e0d\u5e0c\u671b\u4e0a\u62a5\u7684uevent\u3002

name\uff1a\u7528\u4e8e\u83b7\u53d6kset\u4e2dkobject\u7684uevent\u540d\u79f0\uff0c\u8fd9\u4e2a\u540d\u79f0\u901a\u5e38\u4e0euevent\u4e2d\u7684ACTION\u5b57\u6bb5\u76f8\u5bf9\u5e94\u3002

uevent\uff1a\u5f53\u4e00\u4e2akobject\u9700\u8981\u4e0a\u62a5uevent\u65f6\uff0cuevent\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u5b83\u53ef\u4ee5\u4e3auevent\u6dfb\u52a0\u73af\u5883\u53d8\u91cf\u3002

uevent\u7684\u4e00\u4e9b\u64cd\u4f5cAPI\uff1a

int kobject_uevent(struct kobject *kobj, enum kobject_action action);\nint kobject_uevent_env(struct kobject *kobj, enum kobject_action action, char *envp[]);\nint kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count);\nint add_uevent_var(struct kobj_uevent_env *env, const char *format, ...);\n

kobject_uevent_env\uff1a\u4ee5envp\u4e3a\u73af\u5883\u53d8\u91cf\uff0c\u4e0a\u62a5\u4e00\u4e2a\u6307\u5b9aaction\u7684uevent\u3002

kobject_synth_uevent\uff1a\u5141\u8bb8\u53d1\u9001\u4e00\u4e2a\u5408\u6210\u7684uevent\u3002

add_uevent_var\uff1a\u4ee5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\uff0c\u5411uevent\u6dfb\u52a0\u65b0\u7684\u73af\u5883\u53d8\u91cf\u3002

"},{"location":"linux/drivers/device_model/#devicedevice_driver","title":"device\u548cdevice_driver","text":"

device\u548cdevice_driver\u662fLinux\u9a71\u52a8\u5f00\u53d1\u7684\u57fa\u672c\u6982\u5ff5\u3002\u9a71\u52a8\u5f00\u53d1\uff0c\u5176\u5b9e\u5c31\u662f\u5f00\u53d1\u6307\u5b9a\u7684\u8f6f\u4ef6\uff08driver\uff09\u4ee5\u53ca\u9a71\u52a8\u6307\u5b9a\u7684\u8bbe\u5907\uff08device\uff09\u3002\u5185\u6838\u4e3a\u6b64\u5b9a\u4e49\u4e86\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u662fstruct device\u548cstruct device_driver\u3002\u5728<include/linux/device.h>\u4e2d\u53ef\u4ee5\u627e\u5230\u8fd9\u4e24\u4e2a\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u3002

\u5185\u6838\u7528struct device\u7ed3\u6784\u4f53\u6765\u8868\u793a\u4e00\u4e2a\u8bbe\u5907\uff1a

struct device {\n    struct kobject kobj;\n    struct device *parent;\n\n    struct device_private *p;\n\n    const char *init_name; \n    const struct device_type *type;\n\n    struct bus_type *bus;       \n    struct device_driver *driver;   \n    void *platform_data;    \n    void *driver_data;  \n\n    void (*release)(struct device *dev);\n};\n

kobj\uff1a\u8868\u793a\u8be5\u8bbe\u5907\u5e76\u628a\u5b83\u8fde\u63a5\u5230\u7ed3\u6784\u4f53\u7cfb\u4e2d\u7684kobject\u3002

parent\uff1a\u8bbe\u5907\u7684\u7236\u8bbe\u5907\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7236\u8bbe\u5907\u662f\u67d0\u79cdbus\u6216\u8005\u662fhost controller\u3002

p\uff1a\u8bbe\u5907\u7684\u79c1\u6709\u6570\u636e\u3002

init_name\uff1a\u8bbe\u5907\u7684\u521d\u59cb\u540d\u79f0\u3002

type\uff1a\u8bbe\u5907\u7684\u7c7b\u578b\u3002

bus\uff1abus\u7684\u7c7b\u578b\u3002

driver\uff1a\u5bf9\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002

platform_data\uff1a\u8bbe\u5907\u79c1\u6709\u7684\u5e73\u53f0\u6570\u636e\u3002

driver_data\uff1a\u9a71\u52a8\u7684\u79c1\u6709\u6570\u636e\u3002

release\uff1a\u8bbe\u5907\u5378\u8f7d\u65f6\uff0c\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\u3002

\u8bbe\u5907\u7684\u6ce8\u518c\u548c\u6ce8\u9500\u51fd\u6570\u662f\uff1a

init device_register(struct device *dev);\n\nvoid device_unregister(struct device *dev);\n

\u5185\u6838\u7528struct device_driver\u6765\u8868\u793a\u9a71\u52a8\u7a0b\u5e8f\uff1a

struct device_driver {\n    const char *name;\n    struct bus_type *bus;\n    struct module *owner;   \n\n    enum probe_type probe_type;\n\n    int (*probe) (struct device *dev);\n    int (*remove) (struct device *dev);\n\n    const struct attribute_group **groups;\n    const struct attribute_group **dev_groups;\n\n    struct driver_private *p;\n};\n

probe_type\uff1a\u6307\u5b9a\u4ee5\u4ec0\u4e48\u65b9\u5f0f\u6267\u884cprobe\uff08\u5f02\u6b65\u6216\u540c\u6b65\uff09\u3002

probe/remove\uff1a\u5728\u6ce8\u518c\u4e0e\u6ce8\u9500\u65f6\u88ab\u8c03\u7528\u3002

\u9a71\u52a8\u7a0b\u5e8f\u7684\u6ce8\u518c\u548c\u6ce8\u9500\u51fd\u6570\u662f\uff1a

int driver_register(struct device_driver *drv);\n\nvoid driver_unregister(struct device_drvier *drv);\n

Linux\u8bbe\u5907\u6a21\u578b\u6846\u67b6\u4f53\u7cfb\u4e0b\u5f00\u53d1\uff0c\u4e3b\u8981\u5305\u62ec\u4e24\u4e2a\u6b65\u9aa4\uff1a

  1. \u5206\u914d\u4e00\u4e2astruct device\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u586b\u5145\u4fe1\u606f\uff0c\u7136\u540e\u5c06\u5176\u6ce8\u518c\u5230\u5185\u6838\u3002

  2. \u5206\u914d\u4e00\u4e2astruct device_driver\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u586b\u5145\u4fe1\u606f\uff0c\u7136\u540e\u5c06\u5176\u6ce8\u518c\u5230\u5185\u6838\u3002

\u5185\u6838\u4f1a\u5728\u5408\u9002\u7684\u65f6\u673a\uff0c\u8c03\u7528struct device_driver\u4e2d\u7684\u5404\u7c7b\u56de\u8c03\u51fd\u6570\uff0c\u4ece\u800c\u89e6\u53d1\u540e\u8005\u8bbe\u5907\u9a71\u52a8\u7684\u6267\u884c\u3002\u800c\u6240\u6709\u7684\u9a71\u52a8\u7a0b\u5e8f\u903b\u8f91\uff0c\u5176\u5b9e\u90fd\u662f\u7531\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002

\u5f53\u7136\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cLinux\u9a71\u52a8\u5f00\u53d1\u5f88\u5c11\u76f4\u63a5\u64cd\u4f5c\u4e0a\u9762\u4e24\u4e2a\u7ed3\u6784\u4f53\uff0c\u56e0\u4e3a\u5185\u6838\u53c8\u5c01\u88c5\u4e86\u4e00\u5c42\uff0c\u6bd4\u5982platform_device\uff0c\u5c01\u88c5\u540e\u7684\u63a5\u53e3\u66f4\u4e3a\u7b80\u5355\u6613\u7528\u3002device\u548cdevice_driver\u5fc5\u987b\u6302\u5728\u5728\u540c\u4e00\u4e2abus\u4e4b\u4e0b\uff0c\u540d\u79f0\u4e5f\u5fc5\u987b\u4e00\u6837\uff0c\u5185\u6838\u624d\u80fd\u5b8c\u6210\u5339\u914d\u64cd\u4f5c\u3002

\u5982\u679c\u5339\u914d\u5230\u4e86\u76f8\u540c\u540d\u79f0\u7684device\u548cdevice_driver\uff0c\u5185\u6838\u5c31\u4f1a\u6267\u884cdevice_driver\u4e2d\u7684probe()\u56de\u8c03\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u662f\u6240\u6709driver\u7684\u5165\u53e3\u51fd\u6570\uff0c\u7528\u6765\u6267\u884c\u8bf8\u5982\u786c\u4ef6\u8bbe\u5907\u521d\u59cb\u5316\u3001\u5b57\u7b26\u8bbe\u5907\u6ce8\u518c\u3001\u6587\u4ef6\u64cd\u4f5cops\u6ce8\u518c\u7b49\u52a8\u4f5c\uff08\u5bf9\u5e94remove()\u51fd\u6570\uff09\u3002

"},{"location":"linux/drivers/device_model/#bus","title":"bus","text":"

\u603b\u7ebf\u662f\u8fde\u63a5\u5904\u7406\u5668\u548c\u8bbe\u5907\u4e4b\u95f4\u7684\u901a\u9053\u3002\u4e3a\u4e86\u65b9\u4fbf\u8bbe\u5907\u6a21\u578b\u7684\u5b9e\u73b0\uff0c\u7cfb\u7edf\u4e2d\u7684\u6bcf\u4e2a\u8bbe\u5907\u90fd\u9700\u8981\u8fde\u63a5\u5728\u4e00\u4e2a\u603b\u7ebf\u4e0a\uff0c\u8fd9\u4e2a\u603b\u7ebf\u53ef\u4ee5\u662f\u5185\u90e8\u603b\u7ebf\u3001\u865a\u62df\u603b\u7ebf\u6216\u8005\u662f\u5e73\u53f0\u603b\u7ebf\u3002

\u603b\u7ebf\u9a71\u52a8\u5219\u8d1f\u8d23\u5b9e\u73b0\u603b\u7ebf\u7684\u5404\u79cd\u884c\u4e3a\uff0c\u5176\u7ba1\u7406\u7740\u4e24\u4e2a\u94fe\u8868\uff0c\u5206\u522b\u662f\u6dfb\u52a0\u5230\u8be5\u603b\u7ebf\u7684\u8bbe\u5907\u94fe\u8868\u4ee5\u53ca\u6ce8\u518c\u5230\u8be5\u603b\u7ebf\u7684\u9a71\u52a8\u94fe\u8868\u3002\u5f53\u4f60\u5411\u603b\u7ebf\u6dfb\u52a0\uff08\u79fb\u9664\uff09\u4e00\u4e2a\u8bbe\u5907\uff08\u9a71\u52a8\uff09\u65f6\uff0c\u4fbf\u4f1a\u5728\u5bf9\u5e94\u7684\u5217\u8868\u4e0a\u6dfb\u52a0\u65b0\u7684\u8282\u70b9\uff0c \u540c\u65f6\u5bf9\u6302\u8f7d\u5728\u8be5\u603b\u7ebf\u7684\u9a71\u52a8\u4ee5\u53ca\u8bbe\u5907\u8fdb\u884c\u5339\u914d\uff0c\u5728\u5339\u914d\u8fc7\u7a0b\u4e2d\u4f1a\u5ffd\u7565\u6389\u90a3\u4e9b\u5df2\u7ecf\u6709\u9a71\u52a8\u5339\u914d\u7684\u8bbe\u5907\u3002

\u5185\u6838\u7528struct bus_type\u7ed3\u6784\u4f53\u62bd\u8c61\u51fa\u603b\u7ebf\uff1a

struct bus_type {\n    const char *name;\n    const char *dev_name;\n    struct device *dev_root;\n    const struct attribute_group **bus_groups;\n    const struct attribute_group **dev_groups;\n    const struct attribute_group **drv_groups;\n\n    int (*match)(struct device *dev, struct device_driver *drv);\n    int (*uevent)(struct device *dev, struct kobj_uevent_env *env);\n    int (*probe)(struct device *dev);\n    int (*remove)(struct device *dev);\n\n    struct subsys_private *p;\n};\n

\u6211\u4eec\u53d1\u73b0bus_type\u7ed3\u6784\u4f53\u4e2d\u7684\u5927\u90e8\u5206\u6210\u5458\u90fd\u4e0edevice\u6709\u5173\uff0c\u8bf4\u660e\u5b83\u4e3b\u8981\u8d1f\u8d23\u8bbe\u5907\u7684\u6ce8\u518c\u548c\u6ce8\u9500\u7b49\u64cd\u4f5c\u3002

name\uff1a\u8be5bus\u7684\u540d\u79f0\uff0c\u5728sysfs\u4e2d\u4ee5\u76ee\u5f55\u5f62\u5f0f\u5b58\u5728\uff0c\u6bd4\u5982platform bus\u8868\u73b0\u4e3a/sys/bus/platform\u3002

dev_name\uff1a\u6ce8\u518c\u5230bus\u7684\u8bbe\u5907\u540d\u79f0\u3002

dev_root\uff1a\u6839\u8bbe\u5907\u3002

bus_groups\uff1abus\u7684\u9ed8\u8ba4\u5c5e\u6027\u3002

dev_groups\uff1abus\u4e0adevice\u7684\u9ed8\u8ba4\u5c5e\u6027\u3002

drv_groups\uff1abus\u4e0adevice_driver\u7684\u9ed8\u8ba4\u5c5e\u6027\u3002

match\uff1a\u5f53\u5c5e\u4e8e\u8be5bus\u7684device\u6216\u8005device_driver\u6dfb\u52a0\u5230\u5185\u6838\u65f6\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u3002

uevent\uff1a\u5f53\u5c5e\u4e8e\u8be5bus\u7684device\uff0c\u53d1\u751f\u6dfb\u52a0\u3001\u79fb\u9664\u6216\u8005\u5176\u4ed6\u52a8\u4f5c\u65f6\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u3002

probe\u3001remove\uff1a\u5f53\u5c5e\u4e8e\u8be5bus\u7684device\uff0c\u53d1\u751f\u521d\u59cb\u5316\u548c\u79fb\u9664\u65f6\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u3002

p\uff1a\u4fdd\u5b58\u4e86bus\u6a21\u5757\u7684\u4e00\u4e9b\u79c1\u6709\u6570\u636e\u3002

bus\u7684\u5c5e\u6027\u4ee5struct bus_attribute\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct bus_attribute{\n    struct attribute attr;\n    ssize_t (*show)(struct bus_type *bus, char *buf);\n    ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);\n};\n

bus\u6a21\u5757\u7684\u4e3b\u8981\u529f\u80fd\u662f\uff1a

  • bus\u7684\u6ce8\u518c\u548c\u6ce8\u9500
  • \u672cbus\u4e0b\u6709device\u6216\u8005device_driver\u6ce8\u518c\u5230\u5185\u6838\u65f6\u7684\u5904\u7406
  • \u672cbus\u4e0b\u6709device\u6216\u8005device_driver\u4ece\u5185\u6838\u6ce8\u9500\u65f6\u7684\u5904\u7406
  • device_driver\u7684probe
  • \u7ba1\u7406bus\u4e0b\u6240\u6709\u7684device\u548cdevice_driver

\u5185\u6838\u63d0\u4f9b\u4e86bus_register()\u51fd\u6570\u6765\u6ce8\u518c\u603b\u7ebf\uff0cbus_unregister()\u51fd\u6570\u6765\u6ce8\u9500\u603b\u7ebf\u3002

int bus_register(struct bus_type *bus);\n\nvoid bus_unregister(struct bus_type *bus);\n

\u5f53\u6211\u4eec\u6210\u529f\u6ce8\u518c\u603b\u7ebf\u65f6\uff0c\u4f1a\u5728/sys/bus/\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff0c\u76ee\u5f55\u540d\u4e3a\u6211\u4eec\u65b0\u6ce8\u518c\u7684\u603b\u7ebf\u540d\u3002bus\u76ee\u5f55\u4e2d\u5305\u542b\u4e86\u5f53\u524d\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u6ce8\u518c\u4e86\u7684\u6240\u6709\u603b\u7ebf\uff0c\u4f8b\u5982i2c\uff0cspi\uff0cplatform\u7b49\u3002

"},{"location":"linux/drivers/device_model/#class","title":"class","text":"

\u6700\u540e\u4e00\u4e2a\u8bbe\u5907\u6a21\u578b\u6982\u5ff5\u662fclass\u3002class\u662f\u4e00\u4e2a\u8bbe\u5907\u7684\u9ad8\u7ea7\u89c6\u56fe\uff0c\u5b83\u62bd\u8c61\u51fa\u4e86\u5e95\u5c42\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u9a71\u52a8\u7a0b\u5e8f\u53ef\u4ee5\u770b\u5230\u56fa\u6001\u786c\u76d8\u6216\u5149\u76d8\uff0c\u4f46\u662f\u5728class\u7684\u5c42\u6b21\u4e0a\uff0c\u5b83\u4eec\u90fd\u53ea\u662f\u78c1\u76d8\u800c\u5df2\u3002class\u5141\u8bb8\u7528\u6237\u7a7a\u95f4\u4f7f\u7528\u8bbe\u5907\u6240\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u5173\u5fc3\u8bbe\u5907\u662f\u5982\u4f55\u8fde\u63a5\u7684\uff0c\u4ee5\u53ca\u5982\u4f55\u5de5\u4f5c\u7684\u3002

\u51e0\u4e4e\u6240\u6709\u7684class\u90fd\u663e\u793a\u5728/sys/class\u76ee\u5f55\u4e2d\u3002\u6bd4\u5982\u6240\u6709\u7684\u7f51\u7edc\u63a5\u53e3\u90fd\u51e0\u79cd\u5728/sys/class/net\u4e0b\uff0c\u8f93\u5165\u8bbe\u5907\u5728/sys/class/input\u4e0b\uff0c\u4e32\u884c\u8bbe\u5907\u5728/sys/class/tty\u4e0b\u3002

class\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a

struct class {\n    const char *name;\n\n    const struct attribute_group **class_groups;\n    const struct attribute_group **dev_groups;\n\n    struct kobject *dev_kobj;\n\n    int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);\n    char *(*devnode)(struct device *dev, umode_t *mode);\n\n    void (*class_release)(struct class *class);\n    void (*dev_release)(struct device *dev);\n\n    struct subsys_private *p;\n};\n

name\uff1aclass\u7684\u540d\u79f0\uff0c\u4f53\u73b0\u5728/sys/class\u76ee\u5f55\u4e0b\u3002

class_groups\uff1aclass\u5c5e\u6027\u3002

dev_groups\uff1adev\u5c5e\u6027\u3002

dev_uevent\uff1a\u8bbe\u5907\u7684uevent\u56de\u8c03\u51fd\u6570\u3002

class_release/dev_release\uff1arelease\u56de\u8c03\u51fd\u6570\u3002

p\uff1a\u5b50\u7cfb\u7edf\u7684\u79c1\u6709\u6570\u636e\u3002

\u5bf9\u4e8e\u5b50\u7cfb\u7edf\u8fd9\u91cc\u89e3\u91ca\u4e00\u4e0b\u3002/sys/class\u548cdevice_name\u4e4b\u95f4\u7684\u90a3\u90e8\u5206\u76ee\u5f55\u79f0\u4e3asubsystem\u3002\u4e5f\u5c31\u662f\u6bcf\u4e2adev\u5c5e\u6027\u6587\u4ef6\u6240\u5728\u7684\u8def\u5f84\u90fd\u53ef\u8868\u793a\u4e3a/sys/class/subsystem/device_name/dev\u3002\u4f8b\u5982\uff0ccat /sys/class/tty/tty0/dev\u4f1a\u5f97\u52304:0\uff0c\u8fd9\u91ccsubsystem\u4e3atty,device_name\u4e3atty0\u3002

class\u7684\u6ce8\u518c/\u6ce8\u9500\u51fd\u6570\u5982\u4e0b\uff1a

int __must_check __class_register(struct class *class, struct lock_class_key *key);\nvoid class_unregister(struct class *class);\n

class\u7684\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u540e\u7eed\uff0c\u5728\u5404\u7c7b\u5b50\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u80fd\u770b\u5230\u8bb8\u591aclass\u7684\u7528\u4f8b\u3002

"},{"location":"linux/drivers/dma/","title":"\u76f4\u63a5\u5185\u5b58\u8bbf\u95ee","text":"

\u76f4\u63a5\u5185\u5b58\u8bbf\u95ee(DMA)\u662f\u4e00\u79cd\u9ad8\u7ea7\u7684\u786c\u4ef6\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u5916\u56f4\u8bbe\u5907\u548c\u5185\u5b58\u4e4b\u95f4\u76f4\u63a5\u4f20\u8f93\u5b83\u4eec\u7684I/O\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981CPU\u7684\u53c2\u4e0e\u3002\u4f7f\u7528\u8fd9\u79cd\u673a\u5236\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4e0e\u8bbe\u5907\u901a\u4fe1\u7684\u6548\u7387\u3002

"},{"location":"linux/drivers/dts/","title":"\u8bbe\u5907\u6811","text":""},{"location":"linux/drivers/dts/#_2","title":"\u8bbe\u5907\u6811\u7684\u7531\u6765","text":"

\u8d77\u56e0\u662f\u5728<arch/arm/mach-xxx>\u76ee\u5f55\u4e0b\u5145\u65a5\u7740\u5927\u91cf\u91cd\u590d\u7684board specific\u7684\u4ee3\u7801\uff0c\u6bcf\u6b21Linux\u5185\u6838merge window\u671f\u95f4\uff0cARM\u7684\u4ee3\u7801\u53d8\u5316\u5360\u6574\u4e2aarch\u76ee\u5f55\u7684\u4e00\u534a\u4ee5\u4e0a\uff0c\u5bfc\u81f4\u5185\u6838\u5341\u5206\u7684\u81c3\u80bf\u3002\u7ecf\u8fc7\u793e\u533a\u8ba8\u8bba\u540e\u51b3\u5b9a\uff1a

  1. ARM\u7684\u6838\u5fc3\u4ee3\u7801\u4ecd\u7136\u4fdd\u5b58\u5728<arch/arm>\u76ee\u5f55\u4e0b
  2. ARM SOC\u5468\u8fb9\u5916\u8bbe\u6a21\u5757\u7684\u9a71\u52a8\u4ee3\u7801\u4fdd\u5b58\u5728drivers\u76ee\u5f55\u4e0b
  3. ARM SOC board specific\u7684\u4ee3\u7801\u88ab\u79fb\u9664\uff0c\u7531Device Tree\u673a\u5236\u6765\u8d1f\u8d23\u4f20\u9012\u786c\u4ef6\u8d44\u6e90\u4fe1\u606f\u3002

\u672c\u8d28\u4e0a\uff0cDevice Tree\u6539\u53d8\u4e86\u539f\u6765\u7528hardcode\u65b9\u5f0f\u5c06\u786c\u4ef6\u914d\u7f6e\u4fe1\u606f\u5d4c\u5165\u5230\u5185\u6838\u4ee3\u7801\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf\uff0c\u5728\u7cfb\u7edf\u542f\u52a8\u9636\u6bb5\uff0c\u7531bootloader\u901a\u8fc7bootm\u547d\u4ee4\u5c06\u8bbe\u5907\u6811\u4fe1\u606f\u4f20\u9012\u7ed9\u5185\u6838\uff0c\u7136\u540e\u7531\u5185\u6838\u6765\u8bc6\u522b\uff0c\u5e76\u6839\u636e\u5b83\u5c55\u5f00\u51fa\u5185\u6838\u4e2d\u7684platform_device\u3001i2c_client\u3001spi_device\u7b49\u8bbe\u5907\uff0c\u8fd9\u4e9b\u8bbe\u5907\u7528\u5230\u7684\u5185\u5b58\u3001IRQ\u7b49\u8d44\u6e90\u4e5f\u4f1a\u88ab\u4f20\u9012\u7ed9\u5185\u6838\u3002

\u8bbe\u5907\u6811\u6587\u4ef6.dts\u7528\u6765\u63cf\u8ff0\u786c\u4ef6\u4fe1\u606f\u3002\u4e00\u4e2aSOC\u53ef\u4ee5\u5236\u4f5c\u5f88\u591a\u5f00\u53d1\u677f\uff0c\u5c06\u8fd9\u4e9b\u5f00\u53d1\u677f\u7684\u901a\u7528\u4fe1\u606f\u63d0\u53d6\u51fa\u6765\uff0c\u53d8\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684.dtsi\u6587\u4ef6\u3002\u5176\u4ed6\u7684.dts\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u901a\u7528\u6587\u4ef6\uff0c\u5c31\u50cfC\u8bed\u8a00\u4e2d\u7684\u5934\u6587\u4ef6\u3002

\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c.dtsi\u63cf\u8ff0SOC\u4fe1\u606f\uff0c\u6bd4\u5982CPU\u67b6\u6784\uff0c\u4e3b\u9891\u3002.dts\u6587\u4ef6\u63cf\u8ff0\u677f\u7ea7\u4fe1\u606f\uff0c\u6bd4\u5982\u5f00\u53d1\u677f\u4e0a\u6709\u54ea\u4e9bIIC\u8bbe\u5907\u3001SPI\u8bbe\u5907\u7b49\u3002

\u5f00\u53d1\u677f\u7684\u8bbe\u5907\u6811\u6587\u4ef6\u4e00\u822c\u4f4d\u4e8e/boot/dtb/[SOC_NAME]/[BOARD_NAME.dtb]\u3002

\u5982\u679c\u8981\u67e5\u770b\u5f00\u53d1\u677f\u4e2d\u7684\u8bbe\u5907\u6811\u7ed3\u6784\u53ef\u4ee5\u4f7f\u7528\uff1a

ls /sys/firmware/devicetree/base\n

\u6216\u8005

ls /proc/device-tree\n
"},{"location":"linux/drivers/dts/#dtsdtbdtc","title":"DTS\u3001DTB\u548cDTC\u7684\u5173\u7cfb","text":"

dts\u662f\u8bbe\u5907\u6811\u6e90\u7801\u6587\u4ef6\uff0c\u5305\u542b\u7684\u5934\u6587\u4ef6\u4e3adtsi\u3002dts\u6587\u4ef6\u662f\u4eba\u7c7b\u53ef\u4ee5\u770b\u61c2\u7684\uff0c\u4f46\u662fuboot\u548clinux\u65e0\u6cd5\u8bc6\u522b\uff0c\u56e0\u6b64\u9700\u8981dtc\u5c06dts\u6587\u4ef6\u7f16\u8bd1\u6210dtb\u6587\u4ef6\u3002dtb\u6587\u4ef6\u662fuboot\u548clinux\u53ef\u4ee5\u8bc6\u522b\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u5728Linux\u6e90\u7801\u76ee\u5f55\u4e0b/scripts/dtc\u76ee\u5f55\u5305\u542b\u4e86dtc\u5de5\u5177\u7684\u6e90\u7801\u3002

dtc\u7684\u4f7f\u7528\u65b9\u6cd5\u662f\uff1a

dtc -I dts -O dtb -o [output].dtb [input].dts\n

\u53cd\u8fc7\u6765\u53ef\u4ee5\u751f\u6210dts\u6587\u4ef6\uff1a

dtc -I dtb -O dts -o [output].dts [input].dtb\n

make dtbs\u4f1a\u7f16\u8bd1\u6240\u6709\u7684dts\u6587\u4ef6\uff0c\u5982\u679c\u8981\u7f16\u8bd1\u6307\u5b9a\u7684dtb\uff0c\u8bf7\u4f7f\u7528make board_name.dtb\u3002

"},{"location":"linux/drivers/dts/#dts","title":"DTS\u57fa\u672c\u8bed\u6cd5","text":"

\u8bbe\u5907\u6811\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u7531\u4e00\u7cfb\u5217\u7684node\u548cproperty\u7ec4\u6210\u3002\u8fd9\u4e9bnode\u88ab\u7ec4\u7ec7\u6210\u6811\u72b6\u7ed3\u6784\uff0c\u9664\u4e86root node\uff0c\u6bcf\u4e2anode\u90fd\u6709\u4e00\u4e2aparent node\u3002\u6bcf\u4e2anode\u90fd\u5305\u542b\u4e00\u7cfb\u5217\u7684property\uff0c\u7528\u6765\u63cf\u8ff0\u8be5node\u7684\u4e00\u4e9b\u4fe1\u606f\u3002node\u8fd8\u53ef\u4ee5\u5d4c\u5957child node\u3002

label:node-name@unit-address{\n    \u5c5e\u60271 = ...\n    \u5c5e\u60272 = ...\n    \u5c5e\u60273 = ...\n    \u5b50\u8282\u70b9...\n}\n
  • label\uff1a\u7528\u6765\u6307\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u7b7e\uff0c\u65b9\u4fbf\u5f15\u7528\u3002
  • node-name\uff1a\u7528\u6765\u6307\u5b9a\u8282\u70b9\u7684\u540d\u79f0\u3002
  • unit-address\uff1a\u7528\u6765\u6307\u5b9a\u5730\u5740\uff0c\u548c\u6b64\u8282\u70b9\u7684reg\u5c5e\u6027\u7684\u5f00\u59cb\u5730\u5740\u5fc5\u987b\u4e00\u81f4\u3002

\u6ce8\u610f\uff1a\u5982\u679cnode\u4e2d\u6ca1\u6709reg\u5c5e\u6027\uff0c\u5219\u8282\u70b9\u540d\u5b57\u4e2d\u4e0d\u80fd\u6709unit-address\u3002unit-address\u7684\u5177\u4f53\u683c\u5f0f\u548c\u8bbe\u5907\u6302\u5728\u54ea\u4e2abus\u76f8\u5173\u3002\u4f8b\u5982\u5bf9\u4e8eCPU\uff0c\u5176unit-address\u5c31\u662f\u4ece0\u5f00\u59cb\u7f16\u5740\u3002\u800c\u5177\u4f53\u7684\u8bbe\u5907\uff0c\u4f8b\u5982\u4ee5\u592a\u7f51\u63a7\u5236\u5668\uff0c\u5176unit-address\u5c31\u662f\u5bc4\u5b58\u5668\u5730\u5740\u3002

\u5c5e\u6027\u503c\u6807\u8bc6\u4e86\u8bbe\u5907\u7684\u7279\u6027\uff0c\u5b83\u7684\u503c\u53ef\u4ee5\u662f\u4ee5\u4e0b\u51e0\u79cd\uff1a

  1. \u53ef\u80fd\u4e3a\u7a7a\uff0c\u4e5f\u5c31\u662f\u6ca1\u6709\u503c\u7684\u5b9a\u4e49\u3002
  2. \u53ef\u80fd\u662f\u4e00\u4e2au32\u3001u64\u7684\u6570\u503c\uff0c\u4e5f\u53ef\u4ee5\u662f\u6570\u7ec4\u3002
  3. \u53ef\u80fd\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6216\u8005\u662fstring list\u3002
"},{"location":"linux/drivers/dts/#_3","title":"\u7279\u6b8a\u8282\u70b9","text":"

\u522b\u540d\u8282\u70b9aliases\uff1a\u7528\u6765\u7ed9device-node\u5b9a\u4e49\u522b\u540d\uff0c\u56e0\u4e3a\u6bcf\u6b21\u5199\u4e00\u957f\u4e32\u8def\u5f84\u6bd4\u8f83\u9ebb\u70e6\u3002

memory\u8282\u70b9\uff1a\u7528\u6765\u63cf\u8ff0\u7cfb\u7edf\u7269\u7406\u5185\u5b58\u7684layout\u3002device_type\u5c5e\u6027\u5b9a\u4e49\u4e86\u8be5node\u7684\u8bbe\u5907\u7c7b\u578b\uff0c\u4f8b\u5982cpu\u3001serial\u7b49\u3002\u5bf9\u4e8ememory node\uff0c\u5176device_type\u5fc5\u987b\u7b49\u4e8ememory\u3002reg\u5c5e\u6027\u5b9a\u4e49\u4e86\u8bbf\u95ee\u8be5device node\u7684\u5730\u5740\u4fe1\u606f\u2014\u2014\u8d77\u59cb\u5730\u5740\u548c\u957f\u5ea6\u3002

chosen\u8282\u70b9\uff1a\u7528\u6765\u5b9a\u4e49\u542f\u52a8\u53c2\u6570\uff0c\u5176\u7236\u8282\u70b9\u5fc5\u987b\u662f\u6839\u8282\u70b9\u3002\u5185\u6838\u7684\u4e00\u4e9b\u542f\u52a8\u53c2\u6570\u53ef\u4ee5\u901a\u8fc7chosen\u8282\u70b9\u4e0b\u7684bootargs\u5c5e\u6027\u6765\u8bbe\u7f6e\uff0c\u5b83\u53ef\u4ee5\u88abbootloader\u8bfb\u53d6\u3002

"},{"location":"linux/drivers/dts/#_4","title":"\u5c5e\u6027","text":"

\u8282\u70b9\u7531\u4e00\u5806\u5c5e\u6027\u7ec4\u6210\uff0c\u8282\u70b9\u662f\u5177\u4f53\u7684\u8bbe\u5907\uff0c\u4f46\u662f\u4e0d\u540c\u7684\u8bbe\u5907\u6709\u4e0d\u540c\u7684\u5c5e\u6027\uff0c\u4e0d\u8fc7\u6709\u4e00\u4e9b\u662f\u6807\u51c6\u5c5e\u6027\u3002

1.compatible\u5c5e\u6027

compatible\u5c5e\u6027\u7528\u6765\u8868\u793adevice\u548cdriver\u7684\u9002\u914d\uff1a

compatible = \"rockchip, rk3399\";\n

\u8868\u793a\u5382\u5546\u548cSOC\u540d\u3002\u5728\u9a71\u52a8\u6587\u4ef6\u4e2d\u6709\u4e00\u4e2aof\u5339\u914d\u8868\uff0c\u7528\u6765\u5339\u914d\u8bbe\u5907\u8282\u70b9\u548c\u9a71\u52a8\u8282\u70b9\u3002\u5982\u679c\u8bbe\u5907\u8282\u70b9\u7684compatible\u5c5e\u6027\u503c\u548cof\u5339\u914d\u8868\u4e2d\u4efb\u4f55\u4e00\u4e2a\u503c\u76f8\u7b49\uff0c\u90a3\u4e48\u5c31\u8868\u793a\u8be5\u8bbe\u5907\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u9a71\u52a8\u3002

static const struct of_device_id rockchip_rk3399_match[] = {\n    { .compatible = \"rockchip,rk3399\" },\n};\n

compatible\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\u5c5e\u6027\u503c\uff0c\u6309\u7167\u4f18\u5148\u7ea7\u7684\u987a\u5e8f\u8fdb\u884c\u5339\u914d\u3002

2.model\u5c5e\u6027

model\u5c5e\u6027\u7528\u6765\u8868\u793a\u8bbe\u5907\u7684\u578b\u53f7\u3002

3.status\u5c5e\u6027

status\u5c5e\u6027\u7684\u503c\u4e0e\u8bbe\u5907\u72b6\u6001\u6709\u5173\uff0c\u901a\u8fc7\u8bbe\u7f6estatus\u5c5e\u6027\u53ef\u4ee5\u7981\u7528\u6216\u8005\u542f\u7528\u8bbe\u5907\u3002

4.reg\u5c5e\u6027

reg\u5c5e\u6027\u7684\u503c\u4e00\u822c\u662f\u4ee5(address, length)\u5bf9\u7684\u5f62\u5f0f\u51fa\u73b0\u3002\u7528\u4e8e\u63cf\u8ff0\u8bbe\u5907\u8d44\u6e90\u5728\u5176\u7236\u603b\u7ebf\u5b9a\u4e49\u7684\u5730\u5740\u7a7a\u95f4\u5185\u7684\u5730\u5740\u3002

reg = <0x4000e000 0x400>  //\u8d77\u59cb\u5730\u5740+\u5927\u5c0f\n

5.#address-cells\u548c#size-cells\u5c5e\u6027

\u5982\u679c\u4e00\u4e2adevice node\u7684sub node\u6709\u5bfb\u5740\u9700\u6c42\uff08\u5373\u9700\u8981\u5b9a\u4e49reg\u5c5e\u6027\uff09\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5c5e\u6027\u5c31\u5fc5\u987b\u8981\u5b9a\u4e49\uff0c\u7528\u4e8e\u63cf\u8ff0sub node\u7684reg\u5c5e\u6027\u7684\u4fe1\u606f\u3002

#address-cells: \u51b3\u5b9a\u4e86\u5b50\u8282\u70b9reg\u5c5e\u6027\u7684\u5730\u5740\u4fe1\u606f\u6240\u5360\u7528\u7684\u5b57\u957f\n#size-cells\uff1a\u51b3\u5b9a\u4e86\u5b50\u8282\u70b9reg\u5c5e\u6027\u4e2d\u957f\u5ea6\u4fe1\u606f\u6240\u5360\u7684\u5b57\u957f\n

6.ranges\u5c5e\u6027

ranges\u5c5e\u6027\u7684\u503c\u6309\u7167(child-bus-address, parent-bus-address, lenght)\u683c\u5f0f\u7f16\u5199\u3002ranges\u5c5e\u6027\u7528\u6765\u6307\u5b9a\u67d0\u4e2a\u8bbe\u5907\u7684\u5730\u5740\u8303\u56f4\u6216\u8005IO\u8303\u56f4\uff0c\u8fd9\u662f\u5bf9\u8bbe\u5907\u8fdb\u884c\u5bfb\u5740\u7684\u91cd\u8981\u4fe1\u606f\u3002\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7ranges\u5c5e\u6027\u83b7\u77e5\u54ea\u4e9b\u5185\u5b58\u533a\u57df\u6216\u8005IO\u7aef\u53e3\u662f\u88ab\u786c\u4ef6\u8bbe\u5907\u6240\u5360\u7528\u7684\u3002

  • child-bus-address\uff1a\u5b50\u603b\u7ebf\u5730\u5740\u7a7a\u95f4\u7684\u7269\u7406\u5730\u5740\uff0c\u7531\u7236\u8282\u70b9\u7684#address-cells\u786e\u5b9a\u6b64\u7269\u7406\u5730\u5740\u5360\u7528\u7684\u5b57\u957f
  • parent-bus-address\uff1a\u7236\u603b\u7ebf\u5730\u5740\u7a7a\u95f4\u7684\u7269\u7406\u5730\u5740\uff0c\u540c\u6837\u7531\u7236\u8282\u70b9\u7684#address-cells\u786e\u5b9a\u6b64\u7269\u7406\u5730\u5740\u5360\u7528\u7684\u5b57\u957f
  • length\uff1a\u5b50\u5730\u5740\u7a7a\u95f4\u7684\u957f\u5ea6\uff0c\u7531\u7236\u8282\u70b9\u7684#size-cells\u786e\u5b9a\u6b64\u5730\u5740\u957f\u5ea6\u5360\u7528\u7684\u5b57\u957f

\u5982\u679cranges\u5c5e\u6027\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u5b50\u5730\u5740\u7a7a\u95f4\u548c\u7236\u5730\u5740\u7a7a\u95f4\u76f8\u540c\uff0c\u4e0d\u9700\u8981\u8fdb\u884c\u8f6c\u6362\u3002

Arm\u4f53\u7cfb\u6b64\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u7a7a\u3002

7.intc\u5c5e\u6027

\u7528\u4e8e\u8868\u793a\u4e2d\u65ad\u63a7\u5236\u5668\u7684\u76f8\u5173\u4fe1\u606f\u3002

  • interrupt-controller\uff1a
  • interrupt-cells\uff1a
  • interrupt-parent\uff1a
  • interrupts\uff1a

\u6839\u8282\u70b9\u5fc5\u987b\u8981\u6709\u7684\u5c5e\u6027\u6709\uff1a

#address-cells:\u5b50\u8282\u70b9reg\u5c5e\u6027\u4e2d\uff0c\u7528\u591a\u5c11\u4e2au32\u6574\u6570\u6765\u63cf\u8ff0\u5730\u5740\n#size-cells\uff1a\u5b50\u8282\u70b9reg\u5c5e\u6027\u4e2d\uff0c\u7528\u591a\u5c11\u4e2au32\u6574\u6570\u6765\u63cf\u8ff0\u5927\u5c0f\ncompatible\uff1a\u6307\u5b9a\u677f\u5b50\u517c\u5bb9\u7684\u5e73\u53f0\nmodel\uff1a\u677f\u5b50\u540d\u79f0\n
"},{"location":"linux/drivers/dts/#of","title":"\u5185\u6838\u7684of\u51fd\u6570","text":"

\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u6765\u64cd\u4f5c\u8bbe\u5907\u6811\u4e2d\u7684\u8282\u70b9\u548c\u5c5e\u6027\u4fe1\u606f\uff0c\u8fd9\u4e9b\u51fd\u6570\u7edf\u4e00\u4ee5of\u5f00\u5934\uff0c\u5b9a\u4e49\u5728<include/linux/of.h>\u4e2d\u3002

\u5185\u6838\u4f7f\u7528device_node\u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u4e00\u4e2a\u8282\u70b9

struct device_node {\n    const char *name;\n    phandle phandle;\n    const char *full_name;\n    struct fwnode_handle fwnode;\n\n    struct  property *properties;\n    struct  property *deadprops;    /* removed properties */\n    struct  device_node *parent;\n    struct  device_node *child;\n    struct  device_node *sibling;\n#if defined(CONFIG_OF_KOBJ)\n    struct  kobject kobj;\n#endif\n    unsigned long _flags;\n    void    *data;\n#if defined(CONFIG_SPARC)\n    unsigned int unique_id;\n    struct of_irq_controller *irq_trans;\n#endif\n};\n
\u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u5173\u7684of\u51fd\u6570\u67095\u4e2a\uff1a

1.\u901a\u8fc7\u8282\u70b9\u540d\u5b57\u67e5\u627e\u6307\u5b9a\u8282\u70b9

struct device_node *of_find_node_by_name(struct device_node *from, const char *name);\n

from\uff1a\u5f00\u59cb\u67e5\u627e\u7684\u8282\u70b9\uff0cNULL\u5219\u8868\u793a\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u67e5\u627e

name\uff1a\u8981\u67e5\u627e\u7684\u8282\u70b9\u540d

\u8fd4\u56de\u503c\uff1a\u627e\u5230\u7684\u8282\u70b9\uff0cNULL\u8868\u793a\u5931\u8d25

2.\u901a\u8fc7device_type\u5c5e\u6027\u67e5\u627e\u6307\u5b9a\u8282\u70b9\uff08X\uff09

struct device_node *of_find_node_by_type(struct device_node *from, const char *type)\n

type\uff1a\u8981\u67e5\u627e\u7684\u8282\u70b9\u7684device_type\u5c5e\u6027\u503c

\u7531\u4e8edevice_type\u5df2\u7ecf\u88ab\u5e9f\u5f03\uff0c\u6240\u4ee5\u8fd9\u4e2a\u51fd\u6570\u5df2\u7ecf\u4e0d\u7528\u4e86\u3002

3.\u901a\u8fc7device_type\u548ccompatible\u4e24\u4e2a\u5c5e\u6027\u6765\u67e5\u627e\u6307\u5b9a\u8282\u70b9

struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compatible)\n

4.\u901a\u8fc7of_device_id\u5339\u914d\u8868\u6765\u67e5\u627e\u6307\u5b9a\u8282\u70b9

struct device_node *of_find_matching_node_and_match(struct device_node *from, const struct of_device_id *matches, const struct of_device_id **match)\n

5.\u901a\u8fc7\u8def\u5f84\u6765\u67e5\u627e\u6307\u5b9a\u8282\u70b9

inline struct device_node *of_find_node_by_path(const char *path)\n

path\uff1a\u5e26\u6709\u5168\u8def\u5f84\u7684\u8282\u70b9\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528\u8282\u70b9\u7684\u522b\u540d

\u63a8\u8350\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u67e5\u627e\u8282\u70b9\u3002

\u8282\u70b9\u7684\u5c5e\u6027\u4fe1\u606f\u91cc\u4fdd\u5b58\u4e86\u9a71\u52a8\u6240\u9700\u8981\u7684\u5185\u5bb9\uff0c\u5185\u6838\u4e2d\u4f7f\u7528\u7ed3\u6784\u4f53property\u8868\u793a\u5c5e\u6027\u3002

struct property{\n    char *name;    //\u5c5e\u6027\u540d\n    int length;    //\u5c5e\u6027\u503c\u7684\u957f\u5ea6\n    void *value;   //\u6307\u5411\u5c5e\u6027\u503c\u7684\u6307\u9488\n    struct property *next;      //\u6307\u5411\u4e0b\u4e00\u4e2a\u5c5e\u6027\u7684\u6307\u9488\n    unsigned long _flags;       //\u8868\u793a\u5c5e\u6027\u7684\u7c7b\u522b\n    unsigned int unique_id;     //\u6807\u8bc6\u8bbe\u5907\u7684\u552f\u4e00\u5c5e\u6027\n    struct bin_attribute attr;  //\u8868\u793a\u5c5e\u6027\u7684\u4e00\u4e9b\u5143\u6570\u636e\n};\n

\u901a\u8fc7\u4eb2\u7f18\u5173\u7cfb\u6765\u67e5\u627e\u8282\u70b9\uff1a

struct device_node *of_get_parent(const struct device_node *node)\n\nstruct device_node *of_get_next_child(const struct device_node *node, struct device_node *prev)\n\nstruct device_node *of_get_child_by_name(const struct device_node *node, const char *name)\n

\u540c\u65f6\u5185\u6838\u4e5f\u63d0\u4f9b\u4e86\u63d0\u53d6\u5c5e\u6027\u503c\u7684of\u51fd\u6570\u3002

1.\u67e5\u627e\u6307\u5b9a\u7684\u5c5e\u6027

property *of_find_property(const struct device_node *np, const char *name, int *lenp)\n

np\uff1a\u8bbe\u5907\u8282\u70b9

name\uff1a\u5c5e\u6027\u540d\u5b57

lenp\uff1a\u5c5e\u6027\u503c\u7684\u5b57\u8282\u6570

2.\u83b7\u53d6\u5c5e\u6027\u4e2d\u5143\u7d20\u7684\u6570\u91cf

int of_property_count_elems_of_size(const struct device_node *np, const char *propname, int elem_size)\n

3.\u4ece\u5c5e\u6027\u4e2d\u83b7\u53d6\u6307\u5b9a\u6807\u53f7\u7684u32\u7c7b\u578b\u6570\u636e

int of_property_read_u32_index(const struct device_node *np, const char *propname, u32 index, u32 *out_value)\n

propname\uff1a\u8981\u8bfb\u53d6\u7684\u5c5e\u6027\u540d

index\uff1a\u8981\u8bfb\u53d6\u7684\u503c\u6807\u53f7

out_value\uff1a\u8bfb\u53d6\u5230\u7684\u503c

4.\u8bfb\u53d6u8\u3001u16\u3001u32\u3001u64\u6570\u7ec4\u7c7b\u578b\u7684\u6570\u636e

int of_property_read_u8_array(const struct device_node *np, const char *propname, u8 *out_values, size_t sz)\n

\u5c06\u51fd\u6570\u540d\u4e2d\u7684u8\u66ff\u6362\u6210\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u5373\u53ef\u3002

5.\u8bfb\u53d6u8\u3001u16\u3001u32\u3001u64\u7c7b\u578b\u5c5e\u6027\u503c

int of_property_read_u8(const struct device_node *np, const char *propname, u8 *out_value)\n

6.\u8bfb\u53d6\u5c5e\u6027\u4e2d\u5b57\u7b26\u4e32\u7684\u503c

int of_property_read_string(struct device_node *np, const char *propname, const char **out_string)\n

\u8fd9\u4e2a\u51fd\u6570\u4f7f\u7528\u6bd4\u8f83\u7e41\u7410\uff0c\u5efa\u8bae\u4f7f\u7528\u4ee5\u4e0b\u51fd\u6570\uff1a

int of_property_read_string_index(const struct device_node *np,const char *propname, int index,const char **out_string)\n

\u76f8\u6bd4\u524d\u9762\u7684\u51fd\u6570\u589e\u52a0\u4e86\u53c2\u6570index\uff0c\u5b83\u7528\u4e8e\u6307\u5b9a\u8bfb\u53d6\u5c5e\u6027\u503c\u4e2d\u7b2c\u51e0\u4e2a\u5b57\u7b26\u4e32\uff0cindex\u4ece\u96f6\u5f00\u59cb\u8ba1\u6570\u3002 \u7b2c\u4e00\u4e2a\u51fd\u6570\u53ea\u80fd\u5f97\u5230\u5c5e\u6027\u503c\u6240\u5728\u5730\u5740\uff0c\u4e5f\u5c31\u662f\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5730\u5740\uff0c\u5176\u4ed6\u5b57\u7b26\u4e32\u9700\u8981\u6211\u4eec\u624b\u52a8\u4fee\u6539\u79fb\u52a8\u5730\u5740\uff0c\u975e\u5e38\u9ebb\u70e6\uff0c\u63a8\u8350\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u51fd\u6570\u3002

\u73b0\u5728\u5185\u6838\u63d0\u4f9b\u4e86\u5185\u5b58\u6620\u5c04\u76f8\u5173\u7684of\u51fd\u6570\uff0c\u53ef\u4ee5\u81ea\u52a8\u5b8c\u6210\u7269\u7406\u5730\u5740\u5230\u865a\u62df\u5730\u5740\u7684\u8f6c\u6362\uff1a

void __iomem *of_iomap(struct device_node *np, int index)\n

``` gpio1 { gpio-ranges = <&pinctrl 1 0 4>; };

"},{"location":"linux/drivers/gpio/","title":"GPIO\u5b50\u7cfb\u7edf","text":"

GPIO\u5168\u79f0\u201cGeneral Purpose Input/Output\u201d\uff0c\u901a\u7528\u8f93\u5165\u8f93\u51fa\u3002GPIO\u53ef\u80fd\u662f\u82af\u7247\u81ea\u5e26\u7684\uff0c\u4e5f\u53ef\u80fd\u901a\u8fc7I2C\u3001SPI\u63a5\u53e3\u6269\u5c55\u3002

"},{"location":"linux/drivers/gpio/#_1","title":"\u8bbe\u5907\u6811\u63cf\u8ff0","text":"
/*rk3568.dtsi*/\ngpio0: gpio@fdd60000 {\n            compatible = \"rockchip,gpio-bank\";\n            reg = <0x0 0xfdd60000 0x0 0x100>;\n            interrupts = <GIC_SPI 33 IRQ_TYPE_LEVEL_HIGH>;\n            clocks = <&pmucru PCLK_GPIO0>;\n            gpio-controller;\n            #gpio-cells = <2>;\n            interrupt-controller;\n            #interrupt-cells = <2>;\n};\n
"},{"location":"linux/drivers/gpio/#_2","title":"\u6570\u636e\u7ed3\u6784","text":"
struct gpio_device {\n    int         id;\n    struct device       dev;\n    struct cdev     chrdev;\n    struct device       *mockdev;\n    struct module       *owner;\n    struct gpio_chip    *chip;\n    struct gpio_desc    *descs;\n    int         base;\n    u16         ngpio;\n    const char      *label;\n    void            *data;\n    struct list_head        list;\n    struct blocking_notifier_head notifier;\n    struct rw_semaphore sem;\n\n#ifdef CONFIG_PINCTRL\n    /*\n     * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally\n     * describe the actual pin range which they serve in an SoC. This\n     * information would be used by pinctrl subsystem to configure\n     * corresponding pins for gpio usage.\n     */\n    struct list_head pin_ranges;\n#endif\n};\n

\u6bcf\u4e2aGPIO controller\u90fd\u4f1a\u7528\u4e00\u4e2astruct gpio_device\u7ed3\u6784\u4f53\u6765\u8868\u793a\uff0c\u5176\u4e2d\uff1a

  • \u5728struct gpio_chip\u4e2d\u63d0\u4f9b\u5f15\u811a\u64cd\u4f5c\u51fd\u6570

  • \u5728struct gpio_desc\u4e2d\u63d0\u4f9b\u6bcf\u4e2a\u5f15\u811a\u7684\u4fe1\u606f

struct gpio_chip {\n    const char      *label;\n    struct gpio_device  *gpiodev;\n    struct device       *parent;\n    struct module       *owner;\n\n    int         (*request)(struct gpio_chip *gc,\n                        unsigned int offset);\n    void            (*free)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*get_direction)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*direction_input)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*direction_output)(struct gpio_chip *gc,\n                        unsigned int offset, int value);\n    int         (*get)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*get_multiple)(struct gpio_chip *gc,\n                        unsigned long *mask,\n                        unsigned long *bits);\n    void            (*set)(struct gpio_chip *gc,\n                        unsigned int offset, int value);\n    void            (*set_multiple)(struct gpio_chip *gc,\n                        unsigned long *mask,\n                        unsigned long *bits);\n    int         (*set_config)(struct gpio_chip *gc,\n                          unsigned int offset,\n                          unsigned long config);\n    int         (*to_irq)(struct gpio_chip *gc,\n                        unsigned int offset);\n\n    void            (*dbg_show)(struct seq_file *s,\n                        struct gpio_chip *gc);\n\n    int         (*init_valid_mask)(struct gpio_chip *gc,\n                           unsigned long *valid_mask,\n                           unsigned int ngpios);\n\n    int         (*add_pin_ranges)(struct gpio_chip *gc);\n\n    int         base;\n    u16         ngpio;\n    u16         offset;\n    const char      *const *names;\n    bool            can_sleep;\n};\n

label\uff1aGPIO\u63a7\u5236\u5668\u540d\u79f0

gpiodevice\uff1aGPIO\u63a7\u5236\u5668\u8bbe\u5907

parent\uff1aGPIO\u63a7\u5236\u5668\u7236\u8bbe\u5907

base\uff1aGPIO\u5f15\u811a\u57fa\u503c

ngpio\uff1aGPIO\u5f15\u811a\u6570\u91cf

offset\uff1aGPIO\u5f15\u811a\u504f\u79fb

names\uff1aGPIO\u5f15\u811a\u540d\u79f0

can_sleep\uff1a\u662f\u5426\u53ef\u4ee5\u5728\u7761\u7720\u72b6\u6001\u4e0b\u8bbf\u95eeGPIO

\u4f7f\u7528gpiochip_add_data()\u5b8f\u6765\u6ce8\u518cstruct gpio_chip\u3002

\u6bcf\u4e2a\u5f15\u811a\u90fd\u5bf9\u5e94\u4e00\u4e2astruct gpio_desc\uff0c\u5f15\u811a\u4fe1\u606f\u88ab\u4fdd\u5b58\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\uff1a

struct gpio_desc {\n    struct gpio_device  *gdev;\n    unsigned long       flags;\n/* flag symbols are bit numbers */\n#define FLAG_REQUESTED  0\n#define FLAG_IS_OUT 1\n#define FLAG_EXPORT 2   /* protected by sysfs_lock */\n#define FLAG_SYSFS  3   /* exported via /sys/class/gpio/control */\n#define FLAG_ACTIVE_LOW 6   /* value has active low */\n#define FLAG_OPEN_DRAIN 7   /* Gpio is open drain type */\n#define FLAG_OPEN_SOURCE 8  /* Gpio is open source type */\n#define FLAG_USED_AS_IRQ 9  /* GPIO is connected to an IRQ */\n#define FLAG_IRQ_IS_ENABLED 10  /* GPIO is connected to an enabled IRQ */\n#define FLAG_IS_HOGGED  11  /* GPIO is hogged */\n#define FLAG_TRANSITORY 12  /* GPIO may lose value in sleep or reset */\n#define FLAG_PULL_UP    13  /* GPIO has pull up enabled */\n#define FLAG_PULL_DOWN  14  /* GPIO has pull down enabled */\n#define FLAG_BIAS_DISABLE    15 /* GPIO has pull disabled */\n#define FLAG_EDGE_RISING     16 /* GPIO CDEV detects rising edge events */\n#define FLAG_EDGE_FALLING    17 /* GPIO CDEV detects falling edge events */\n#define FLAG_EVENT_CLOCK_REALTIME   18 /* GPIO CDEV reports REALTIME timestamps in events */\n\n    /* Connection label */\n    const char      *label;\n    /* Name of the GPIO */\n    const char      *name;\n#ifdef CONFIG_OF_DYNAMIC\n    struct device_node  *hog;\n#endif\n#ifdef CONFIG_GPIO_CDEV\n    /* debounce period in microseconds */\n    unsigned int        debounce_period_us;\n#endif\n};\n

gdev\uff1a\u5c5e\u4e8e\u54ea\u4e2aGPIO controller

flags\uff1a\u6807\u5fd7\u4f4d\uff0c\u8868\u793a\u5f15\u811a\u7684\u72b6\u6001

label\uff1a\u4e00\u822c\u7b49\u4e8estruct gpio_chip\u7684label

name\uff1a\u5f15\u811a\u540d

"},{"location":"linux/drivers/gpio/#gpio_1","title":"GPIO\u51fd\u6570\u63a5\u53e3","text":"

\u5b9a\u4e49\u5728<include/linux/gpio/consumer.h>\u4e2d\uff0c\u5e38\u7528\u51fd\u6570\u6709\uff1a

\u83b7\u5f97GPIO\uff1a

struct gpio_desc *__must_check gpiod_get(struct device *dev,\n                     const char *con_id,\n                     enum gpiod_flags flags);\nstruct gpio_desc *__must_check gpiod_get_index(struct device *dev,\n                           const char *con_id,\n                           unsigned int idx,\n                           enum gpiod_flags flags);\nstruct gpio_descs *__must_check gpiod_get_array(struct device *dev,\n                        const char *con_id,\n                        enum gpiod_flags flags);\n\nstruct gpio_desc *__must_check devm_gpiod_get(struct device *dev,\n                          const char *con_id,\n                          enum gpiod_flags flags);\nstruct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,\n                            const char *con_id,\n                            unsigned int idx,\n                            enum gpiod_flags flags);\nstruct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,\n                             const char *con_id,\n                             enum gpiod_flags flags);\n

\u8bbe\u7f6e\u65b9\u5411\uff1a

int gpiod_direction_input(struct gpio_desc *desc);\nint gpiod_direction_output(struct gpio_desc *desc, int value);\n

\u8bfb\u5199\u503c\uff1a

int gpiod_get_value(const struct gpio_desc *desc);\nvoid gpiod_set_value(struct gpio_desc *desc, int value);\n\n\n\u91ca\u653eGPIO\uff1a\n```C\nvoid gpiod_put(struct gpio_desc *desc);\nvoid gpiod_put_array(struct gpio_descs *descs);\n

GPIO\u63cf\u8ff0\u7b26\u6807\u5fd7\u4f4d\uff1a

enum gpiod_flags {\n    GPIOD_ASIS  = 0,\n    GPIOD_IN    = GPIOD_FLAGS_BIT_DIR_SET,\n    GPIOD_OUT_LOW   = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,\n    GPIOD_OUT_HIGH  = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |\n              GPIOD_FLAGS_BIT_DIR_VAL,\n    GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,\n    GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,\n};\n

\u8981\u64cd\u4f5c\u4e00\u4e2a\u5f15\u811a\uff0c\u9996\u5148\u8981\u83b7\u53d6\u8fd9\u4e2a\u5f15\u811a\uff0c\u7136\u540e\u8bbe\u7f6e\u65b9\u5411\uff0c\u8bfb\u503c\u6216\u8005\u5199\u503c\u3002

"},{"location":"linux/drivers/gpio/#pinctrl","title":"\u4e0ePinctrl\u5b50\u7cfb\u7edf\u4ea4\u4e92","text":"
  1. \u5728GPIO\u8bbe\u5907\u6811\u4e2d\u4f7f\u7528gpio-ranges\u6765\u63cf\u8ff0\u5b83\u4eec\u4e4b\u95f4\u7684\u8054\u7cfb
  2. \u89e3\u6790\u8fd9\u4e9b\u8054\u7cfb\uff0c\u5728\u6ce8\u518cstruct gpio-chip\u65f6\u81ea\u52a8\u8c03\u7528
  3. \u5728GPIO\u9a71\u52a8\u7a0b\u5e8f\u4e2d\uff0c\u63d0\u4f9bgpio_chip->request\u51fd\u6570\uff1b\u5728Pinctrl\u5b50\u7cfb\u7edf\u4e2d\uff0c\u63d0\u4f9bpmxops->gpio_request_enable\u51fd\u6570\u6216\u8005pmxops->request\u51fd\u6570
"},{"location":"linux/drivers/i2c/","title":"I2C\u5b50\u7cfb\u7edf","text":""},{"location":"linux/drivers/input/","title":"Input\u5b50\u7cfb\u7edf","text":""},{"location":"linux/drivers/netdev/","title":"\u7f51\u7edc\u8bbe\u5907\u9a71\u52a8","text":""},{"location":"linux/drivers/pinctrl/","title":"Pinctrl\u5b50\u7cfb\u7edf","text":"

Pinctrl\uff1aPin Controller\uff0c\u5373\u5f15\u811a\u63a7\u5236\uff0c\u901a\u4fd7\u5730\u6765\u8bf4\u5c31\u662f\u4e00\u7ec4\u53ef\u4ee5\u63a7\u5236\u5f15\u811a\u7684\u5bc4\u5b58\u5668\u96c6\u5408\u3002\u5b83\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e09\u79cd\u529f\u80fd\uff1a

  • \u5f15\u811a\u679a\u4e3e\u4e0e\u547d\u540d\uff0c\u6bcf\u4e2apin\u7684ID\u552f\u4e00
  • \u5f15\u811a\u590d\u7528\uff0c\u6bd4\u5982\u5355\u4e2a\u5f15\u811a\u53ef\u4ee5\u914d\u7f6e\u6210GPIO\uff0c\u591a\u4e2a\u5f15\u811a\u8fd8\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2apin group\uff0c\u5f62\u6210\u7279\u5b9a\u7684\u529f\u80fd
  • \u5f15\u811a\u914d\u7f6e\uff0c\u6bd4\u5982\u4f7f\u80fd\u6216\u7981\u6b62\u5f15\u811a\u7684\u4e0a\u62c9\u3001\u4e0b\u62c9\u7535\u963b

pinctrl\u5b50\u7cfb\u7edf\u6d89\u53ca\u5230\u4e24\u4e2a\u5bf9\u8c61\uff1a

  • pin controller device\uff1a\u63d0\u4f9b\u5f15\u811a\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u7531\u539f\u5382\u5de5\u7a0b\u5e08\u63d0\u4f9b\u3002

  • client device\uff1a\u4f7f\u7528pinctrl\u5b50\u7cfb\u7edf\u7684\u8bbe\u5907\uff0c\u9700\u8981\u63cf\u8ff0\u4f7f\u7528\u7684\u5f15\u811a\uff0c\u7531\u9a71\u52a8\u5de5\u7a0b\u5e08\u7f16\u5199\u3002

\u5185\u6838\u5206\u522b\u62bd\u8c61\u51fastruct pinctrl_dev\u548cstruct device\u6765\u8868\u793a\u4e0a\u9762\u4e24\u4e2a\u5bf9\u8c61\u4fe1\u606f\u3002

"},{"location":"linux/drivers/pinctrl/#_1","title":"\u6e90\u6587\u4ef6\u5217\u8868","text":"

pinctrl\u5b50\u7cfb\u7edf\u76f8\u5173\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u5982\u4e0b\uff1a

1.\u6e90\u6587\u4ef6\u5217\u8868

\u6587\u4ef6\u540d \u63cf\u8ff0 core.h \u6838\u5fc3\u6570\u636e\u7ed3\u6784\u5934\u6587\u4ef6 pinctrl.h \u63a5\u53e3\u6587\u4ef6 pinmux.h \u5f15\u811a\u590d\u7528\u5934\u6587\u4ef6 pinconf.h \u5f15\u811a\u914d\u7f6e\u5934\u6587\u4ef6

2.\u548c\u5176\u4ed6\u5185\u6838\u6a21\u5757\u63a5\u53e3\u6587\u4ef6\u5217\u8868

\u6587\u4ef6\u540d \u63cf\u8ff0 consumer.h \u5176\u4ed6\u7684driver\u8981\u4f7f\u7528pinctrl\u5b50\u7cfb\u7edf\u65f6\uff0c\u9700\u8981\u5305\u542b\u8fd9\u4e2a\u5934\u6587\u4ef6 devinfo.h linux\u5185\u6838\u9a71\u52a8\u8bbe\u5907\u6a21\u578b\u6a21\u5757\u4f7f\u7528\u7684\u63a5\u53e3 machine.h \u548cmachine\u6a21\u5757\u7684\u63a5\u53e3

3.low level pin controller driver\u63a5\u53e3\u6587\u4ef6\u5217\u8868

\u6587\u4ef6\u540d \u63cf\u8ff0 pinconf-generic.h \u5f15\u811a\u914d\u7f6e\u7684\u901a\u7528\u63a5\u53e3 pinconf.h \u5f15\u811a\u914d\u7f6e\u7684\u63a5\u53e3 pinctrl-state.h \u4fdd\u5b58pin controller\u7684\u72b6\u6001"},{"location":"linux/drivers/pinctrl/#_2","title":"\u8bbe\u5907\u6811\u63cf\u8ff0","text":"

\u5f15\u811a\u914d\u7f6e\u7684\u8bbe\u5907\u6811\u63cf\u8ff0\u5982\u4e0b\uff1a

/*s5pv210-pinctrl.dtsi*/\n&pinctrl0 {\n    gpa0: gpa0 {\n        gpio-controller;        /*\u8fd9\u662f\u4e00\u4e2aGPIO\u63a7\u5236\u5668*/\n        #gpio-cells = <2>;      /*\u9700\u89812\u4e2a32\u4f4d\u7684\u6570\u6765\u63cf\u8ff0\u8fd9\u4e2aGPIO*/\n\n        interrupt-controller;   /*\u8fd9\u662f\u4e00\u4e2a\u4e2d\u65ad\u63a7\u5236\u5668*/\n        #interrupt-cells = <2>;\n    };\n\n    gpa1: gpa1 {\n        gpio-controller;\n        #gpio-cells = <2>;\n\n        interrupt-controller;\n        #interrupt-cells = <2>;\n    };\n};\n\n/*\u7701\u7565\u5176\u4ed6\u5f15\u811a\u914d\u7f6e*/\n

\u9664\u4e86\u5355\u4e2a\u5f15\u811a\u7684\u63cf\u8ff0\uff0c\u8fd8\u53ef\u4ee5\u5c06\u591a\u4e2a\u5f15\u811a\u7ec4\u5408\u5728\u4e00\u8d77\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u6bd4\u5982SPI\u63a5\u53e3\u3001I2C\u63a5\u53e3\u7b49\uff0c\u8fd9\u88ab\u79f0\u4e3a\u5f15\u811a\u7ec4\uff08pin group\uff09\uff0c\u5176\u8bbe\u5907\u6811\u63cf\u8ff0\u5982\u4e0b\uff1a

/*s5pv210-pinctrl.dtsi*/\nuart0_data: uart0-data {\n    samsung,pins = \"gpa0-0\", \"gpa0-1\";\n    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;\n    samsung,pin-pud = <S3C64XX_PIN_PULL_NONE>;\n    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;\n};\n\n...\ni2c0_bus: i2c0-bus {\n    samsung,pins = \"gpd1-0\", \"gpd1-1\";\n    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;\n    samsung,pin-pud = <S3C64XX_PIN_PULL_UP>;\n    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;\n};\n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cuart0\u7684\u5f15\u811a\u7ec4\u914d\u7f6e\u9700\u8981\u7528\u5230\"gpa0-0, gpa0-1\"\uff0ci2c0\u7684\u5f15\u811a\u7ec4\u914d\u7f6e\u9700\u8981\u7528\u5230\"gpd1-0\", \"gpd1-1\"\u3002\u4e00\u65e6\u9009\u62e9\u4e86\u67d0\u4e2a\u529f\u80fd\uff0cpins\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u5f15\u811a\u90fd\u9700\u8981\u5728pin-function\u4e2d\u505a\u76f8\u5e94\u7684\u529f\u80fd\u8bbe\u5b9a\uff0c\u5177\u4f53\u8bbe\u5b9a\u7684\u503c\u9700\u8981\u5728\u82af\u7247\u624b\u518c\u4e2d\u67e5\u627e\u3002

\u8fd8\u6709\u4e00\u4e9b\u5f15\u811a\u914d\u7f6e\u5c5e\u6027\u6bd4\u5982\uff1a

  • samsung,pin-val\uff1a\u5f15\u811a\u8f93\u51fa\u7f13\u51b2\u533a\u7684\u521d\u59cb\u503c
  • samsung,pin-pud\uff1a\u4e0a\u4e0b\u62c9\u914d\u7f6e
  • samsung,pin-drv\uff1a\u9a71\u52a8\u5668\u5f3a\u5ea6\u914d\u7f6e
  • samsung,pin-pud-pdn\uff1a\u4f4e\u529f\u8017\u6a21\u5f0f\u4e0b\u7684\u4e0a\u4e0b\u62c9\u914d\u7f6e
  • samsung,pin-drv-pdn\uff1a\u4f4e\u529f\u8017\u6a21\u5f0f\u4e0b\u7684\u9a71\u52a8\u5668\u5f3a\u5ea6\u914d\u7f6e

\u5177\u4f53\u7684\u914d\u7f6e\u4fe1\u606f\u6bcf\u4e2aSOC\u5382\u5546\u90fd\u6709\u4e0d\u540c\u7684\u6807\u51c6\uff0c\u9664\u975e\u4f60\u662f\u539f\u5382\u5de5\u7a0b\u5e08\uff0c\u5426\u5219\u53ea\u9700\u8981\u4f9d\u846b\u82a6\u753b\u74e2\u5373\u53ef\u3002

\u4e00\u4e2a\u5178\u578b\u7684client device\u5f15\u7528\u5f15\u811a\u914d\u7f6e\u7684\u5b9e\u4f8b\u5982\u4e0b\uff1a

device-node-name {  \n    pinctrl-names = \"default\", \"init\", \"sleep\";   \n    pinctrl-0 = <pin-config-0-a>; \n    pinctrl-1 = <pin-config-1-b>; \n    pinctrl-2 = <pin-config-2-c>;        \n};\n

\u8fd9\u91ccpinctrl-names\u5c31\u8868\u793a\u5f15\u811a\u7684state\u2014\u2014default, init, sleep\u7b49\u3002\u5bf9\u4e8e\u67d0\u4e2aclient device\uff0c\u5b83\u4f7f\u7528\u7684\u4e00\u7ec4\u5f15\u811a\u5e94\u8be5\u540c\u65f6\u5904\u4e8e\u67d0\u79cdstate\u4e0b\uff0cstate\u7684\u5b9a\u4e49\u4e0e\u7535\u6e90\u7ba1\u7406\u7cfb\u7edf\u76f8\u5173\uff08Power Management\uff09\u3002\u6bd4\u5982\u5f53\u8bbe\u5907\u8fdb\u5165\u7761\u7720\u72b6\u6001\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u5f15\u811a\u72b6\u6001\u4ee5\u8282\u7701\u529f\u8017\u3002\u6bcf\u4e2astate\u7531\u4e0b\u9762\u76840\u30011\u30012\u914d\u7f6e\u5bf9\u5e94\u3002pinctrl-x\u662f\u4e00\u4e2a\u53e5\u67c4\uff08phandle\uff09\u5217\u8868\uff0c\u6bcf\u4e2a\u53e5\u67c4\u6307\u5411\u4e00\u4e2apin configuration\u3002

\u7531\u8bbe\u5907\u6811\u7684\u77e5\u8bc6\u6211\u4eec\u77e5\u9053\uff0c\u6bcf\u4e2a\u8bbe\u5907\u6811\u63cf\u8ff0\u7684device node\u6700\u7ec8\u4f1a\u5f62\u6210\u4e00\u4e2a\u6811\u72b6\u7ed3\u6784\uff0c\u5728\u5185\u6838\u521d\u59cb\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u626b\u63cf\u8fd9\u4e2a\u6811\u72b6\u7ed3\u6784\uff0c\u5e76\u6839\u636e\u6bcf\u4e2adevice node\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u521d\u59cb\u5316\u5bf9\u5e94\u7684\u8bbe\u5907\u5e76\u52a0\u5165\u5230\u5185\u6838\u4e2d\u3002\u7c7b\u4f3c\u5730\uff0cpin controller driver\u7684\u521d\u59cb\u5316\u4e5f\u662f\u4ece\u8bbe\u5907\u6811\u8282\u70b9\u5f00\u59cb\u7684\uff1a

pinctrl@56000000{\n    reg=<0x56000000 0x1000=\"\">;\n    compatible=\"samsung, s3c2416-pinctrl\";\n}\n

compatible\u5c5e\u6027\u7528\u6765\u6307\u793a\u5185\u6838\u9009\u7528\u54ea\u4e00\u4e2apin controller driver\u6765\u9a71\u52a8\u8be5\u8bbe\u5907\u3002pinctrl\u5b50\u7cfb\u7edf\u8981\u60f3\u8fdb\u884c\u63a7\u5236\uff0c\u5fc5\u987b\u6709\u4e00\u4e2a\u63a7\u5236\u5bf9\u8c61\uff0c\u5728\u8f6f\u4ef6\u5c42\u9762\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u65b9\u6cd5\u5c06\u786c\u4ef6\u4fe1\u606f\u6ce8\u518c\u5230pinctrl\u5b50\u7cfb\u7edf\u4e2d\u53bb\u3002

"},{"location":"linux/drivers/pinctrl/#_3","title":"\u6570\u636e\u7ed3\u6784","text":"

\u5b66\u4e60pinctrl\u5b50\u7cfb\u7edf\uff0c\u9996\u5148\u8981\u4e86\u89e3\u5176\u5185\u90e8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u518d\u53bb\u7814\u7a76\u6e90\u7801\u5c31\u6bd4\u8f83\u8f7b\u677e\u3002

\u4e3b\u8981\u6d89\u53ca\u5230\u7684\u6570\u636e\u7ed3\u6784\u662f\uff1a

  • pin controller device\u76f8\u5173\uff1apinctrl_desc, pinctrl_ops, pinmux_ops, pinconf_ops, pinctrl_dev
  • client device\u76f8\u5173\uff1apinctrl, pinctrl_state, pinctrl_setting, pinctrl_map, pinctrl_dt_map

\u7531\u4e8e\u6d89\u53ca\u5230\u7684\u7ed3\u6784\u4f53\u6bd4\u8f83\u591a\uff0c\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684\u6210\u5458\u4e5f\u975e\u5e38\u590d\u6742\uff0c\u8fd9\u91cc\u7701\u7565\u4e86\u4e00\u90e8\u5206\u4e0d\u592a\u91cd\u8981\u7684\u5185\u5bb9\uff0c\u5bf9\u4e8e\u91cd\u590d\u6027\u7684\u5185\u5bb9\u4e5f\u4e0d\u518d\u8bf4\u660e\uff0c\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u81ea\u884c\u9605\u8bfb\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002

"},{"location":"linux/drivers/pinctrl/#pin-controller-device","title":"pin controller device","text":"

struct pinctrl_dev\u7ed3\u6784\u4f53\u7528\u6765\u8868\u793a\u4e00\u4e2apin controller device\uff0c\u8d1f\u8d23\u7ba1\u7406\u5f15\u811a\uff0c\u540e\u7eed\u7684\u8bb8\u591a\u6570\u636e\u7ed3\u6784\u90fd\u9700\u8981\u4e0e\u6b64\u5173\u8054\uff1a

struct pinctrl_dev {\n    struct list_head node;\n    struct pinctrl_desc *desc;\n    struct list_head gpio_ranges;\n    struct device *dev;\n    struct module *owner;\n    void *driver_data;\n    struct pinctrl *p;\n    struct pinctrl_state *hog_default;\n    struct pinctrl_state *hog_sleep;\n};\n

desc\uff1apin controller\u63cf\u8ff0\u7b26\uff0c\u7528\u4e8e\u63cf\u8ff0\u5f15\u811a\u4fe1\u606f\u3002

gpio_range\uff1a\u6b64pin controller\u5904\u7406\u7684GPIO\u8303\u56f4\u5217\u8868\u3002

dev\uff1apin controller\u7684\u7236\u8bbe\u5907\uff0c\u4e00\u822c\u8bbe\u7f6e\u4e3a\u5e73\u53f0\u8bbe\u5907\u7684dev\u6210\u5458\u3002

owner\uff1a\u6240\u6709\u8005\u3002

driver_data\uff1a\u9a71\u52a8\u7a0b\u5e8f\u7684\u79c1\u6709\u6570\u636e\u3002

p\uff1a\u4fdd\u5b58pinctrl_get(dev)\u7684\u7ed3\u679c\u3002

hog_default\uff1a\u6b64\u8bbe\u5907\u5360\u7528\u7684\u5f15\u811a\u7684\u9ed8\u8ba4\u72b6\u6001\u3002

hog_sleep\uff1a\u6b64\u8bbe\u5907\u5360\u7528\u7684\u5f15\u811a\u7684\u7761\u7720\u72b6\u6001\u3002

struct pinctrl_desc\u7528\u6765\u5b9a\u4e49\u548c\u914d\u7f6epin controller\u7684\u63cf\u8ff0\u4fe1\u606f\uff1a

struct pinctrl_desc {\n        const char *name;\n        const struct pinctrl_pin_desc *pins;\n        unsigned int npins;\n        const struct pinctrl_ops *pctlops;\n        const struct pinmux_ops *pmxops;\n        const struct pinconf_ops *confops;\n        struct module *owner;\n};\n

name\uff1apin controller\u7684\u540d\u79f0\u3002

pins\uff1a\u8be5pin controller\u5904\u7406\u7684\u6240\u6709\u5f15\u811a\u7684\u63cf\u8ff0\u7b26\u6570\u7ec4\u3002

npins\uff1a\u6570\u7ec4\u4e2d\u63cf\u8ff0\u7b26\u7684\u6570\u91cf\uff0c\u4e00\u822c\u7b49\u4e8eARRAY_SIZE(pins)\u3002

pctlops\uff1a\u5f15\u811a\u63a7\u5236\u64cd\u4f5c\u3002

pmxops\uff1a\u5f15\u811a\u590d\u7528\u64cd\u4f5c\u3002

confops\uff1a\u5f15\u811a\u914d\u7f6e\u64cd\u4f5c\u3002

\u5bf9\u4e8e\u67d0\u4e2apin controller device\u6765\u8bf4\uff0c\u5b83\u8981\u641e\u660e\u767d\u81ea\u5df1\u7ba1\u7406\u591a\u5c11\u5f15\u811a\uff0c\u5e76\u4f7f\u7528\u81ea\u7136\u6570\u4e3a\u8fd9\u4e9b\u5f15\u811a\u7f16\u53f7\u3002\u6240\u4ee5\u7cfb\u7edf\u4e2d\u7684\u5f15\u811a\u4fe1\u606f\uff0c\u90fd\u7531struct pinctrl_pin_desc\u6765\u63cf\u8ff0\uff0c\u5305\u62ec\u7f16\u53f7\u3001\u540d\u5b57\u548c\u6570\u636e\uff1a

struct pinctrl_pin_desc {\n    unsigned number;\n    const char *name;\n    void *drv_data;\n};\n

number\uff1a\u5f15\u811a\u7f16\u53f7\u3002

name\uff1a\u5f15\u811a\u540d\u79f0\u3002

drv_data\uff1a\u5f15\u811a\u7684\u79c1\u6709\u6570\u636e\u3002

struct pinctrl_pin_desc\u7ed3\u6784\u4f53\u4e2d\u7684\u7f16\u53f7\u548c\u540d\u79f0\u5b8c\u5168\u7531\u9a71\u52a8\u5f00\u53d1\u4eba\u5458\u81ea\u5df1\u51b3\u5b9a\u3002

\u8fd9\u4e48\u8bf4\u6709\u70b9\u67af\u71e5\uff0c\u6211\u4eec\u770b\u5b98\u65b9\u6587\u6863\u7684\u4e00\u4e2a\u793a\u4f8b\u3002\u5047\u8bbe\u5f15\u811a\u9635\u5217\u5982\u4e0b\u56fe\u6240\u793a\uff1a

        A   B   C   D   E   \n\n   5    o   o   o   o   o  \n\n   4    o   o   o   o   o  \n\n   3    o   o   o   o   o  \n\n   2    o   o   o   o   o  \n\n   1    o   o   o   o   o  \n

\u8981\u5728\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u6ce8\u518c\u4e00\u4e2a\u5f15\u811a\u63a7\u5236\u5668\u5e76\u4e3a\u8fd9\u4e2a\u5c01\u88c5\u4e0a\u7684\u6240\u6709\u5f15\u811a\u547d\u540d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a

#include <linux/pinctrl/pinctrl.h>\nconst struct pinctrl_pin_desc foo_pins[] = {\n    PINCTRL_PIN(0, \"A5\"),\n    PINCTRL_PIN(1, \"B5\"),\n    PINCTRL_PIN(2, \"C5\"),\n    ...\n    PINCTRL_PIN(23, \"C1\"),\n    PINCTRL_PIN(24, \"D1\"),\n    PINCTRL_PIN(25, \"E1\"),\n};\n

\u5728\u5b9a\u4e49\u4e86\u5f15\u811a\u914d\u7f6e\u6570\u7ec4\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u544a\u8bc9struct pinctrl_desc\u7ed3\u6784\u4f53\u5f15\u811a\u7684\u4fe1\u606f\uff0c\u7136\u540e\u8c03\u7528pinctrl_register_and_init()\u51fd\u6570\u6ce8\u518c\u3002

static struct pinctrl_desc foo_desc = {\n    .name = \"foo\",\n    .pins = foo_pins,\n    .npins = ARRAY_SIZE(foo_pins),\n    .owner = THIS_MODULE,\n};\n\nstruct pinctrl_dev *pctl;\n\npinctrl_register_and_init(&foo_desc, <PARENT>, NULL, &pctl);\n\npinctrl_enable(pctl);\n

\u5728SOC\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u9700\u8981\u5c06\u591a\u4e2a\u5f15\u811a\u8fdb\u884c\u7ec4\u5408\u3002\u56e0\u6b64pinctrl\u5b50\u7cfb\u7edf\u5fc5\u987b\u4ee5group\u4e3a\u5355\u4f4d\uff0c\u540c\u65f6\u5730\u8bbf\u95ee\u548c\u63a7\u5236\u591a\u4e2a\u5f15\u811a\uff0c\u8fd9\u5c31\u662fpin group\u7684\u6982\u5ff5\u3002\u76f8\u5e94\u5730\uff0cpinctrl\u5b50\u7cfb\u7edf\u5fc5\u987b\u63d0\u4f9b\u4e00\u4e9b\u673a\u5236\uff0c\u6765\u83b7\u53d6\u7cfb\u7edf\u4e2d\u7684\u6709\u591a\u5c11\u4e2agroup\u4ee5\u53ca\u6bcf\u4e2agroup\u6709\u54ea\u4e9b\u5f15\u811a\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u5b9a\u4e49\u5728struct pinctrl_ops\u7ed3\u6784\u4f53\u4e2d\uff1a

struct pinctrl_ops {\n    int (*get_groups_count) (struct pinctrl_dev *pctldev);\n    const char *(*get_group_name) (struct pinctrl_dev *pctldev, unsigned selector);\n    int (*get_group_pins) (struct pinctrl_dev *pctldev, unsigned selector,\n                           const unsigned **pins, unsigned *num_pins);\n    void (*pin_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                          unsigned offset);\n    int (*dt_node_to_map) (struct pinctrl_dev *pctldev, struct device_node *np_config,\n                           struct pinctrl_map **map, unsigned *num_maps);\n    void (*dt_free_map) (struct pinctrl_dev *pctldev, struct pinctrl_map *map, \n                         unsigned num_maps);\n};\n

get_groups_count()\uff1a\u83b7\u53d6pin group\u7684\u6570\u91cf\u3002

get_group_name()\uff1a\u83b7\u53d6pin group\u7684\u540d\u79f0\u3002

get_group_pins()\uff1a\u83b7\u53d6pin group\u7684\u5f15\u811a\u4fe1\u606f\u3002

dt_node_to_map()\uff1a\u5c06\u8bbe\u5907\u6811\u4e2d\u7684pin controller\u5b50\u8282\u70b9\u521b\u5efa\u6620\u5c04\uff0c\u5373\u5c06devic_node\u8f6c\u6362\u4e3a\u4e00\u7cfb\u5217\u7684struct pinctrl_map\u3002

dt_free_map()\uff1a\u91ca\u653edt_node_to_map()\u521b\u5efa\u7684\u6620\u5c04\u3002

SOC\u4e2d\u7684\u5f88\u591a\u5f15\u811a\u53ef\u4ee5\u914d\u7f6e\u4e3a\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u8fd9\u88ab\u79f0\u4e3a\u5f15\u811a\u7684\u590d\u7528\uff08pinmux\uff09\uff0cpinctrl\u5b50\u7cfb\u7edf\u4f7f\u7528struct pinmux_ops\u7ed3\u6784\u4f53\u6765\u62bd\u8c61pinmux\u6709\u5173\u7684\u64cd\u4f5c\uff1a

struct pinmux_ops {\n    int (*request) (struct pinctrl_dev *pctldev, unsigned offset);\n    int (*free) (struct pinctrl_dev *pctldev, unsigned offset);\n    int (*get_functions_count) (struct pinctrl_dev *pctldev);\n    const char *(*get_function_name) (struct pinctrl_dev *pctldev, unsigned selector);\n    int (*get_function_groups) (struct pinctrl_dev *pctldev, unsigned selector,\n                                const char * const **groups, unsigned *num_groups);\n    int (*set_mux) (struct pinctrl_dev *pctldev, unsigned func_selector,\n                    unsigned group_selector);\n    int (*gpio_request_enable) (struct pinctrl_dev *pctldev, \n                                struct pinctrl_gpio_range *range, unsigned offset);\n    void (*gpio_disable_free) (struct pinctrl_dev *pctldev, \n                               struct pinctrl_gpio_range *range, unsigned offset);\n    int (*gpio_set_direction) (struct pinctrl_dev *pctldev,\n                               struct pinctrl_gpio_range *range, unsigned offset,\n                               bool input);\n    bool strict;\n};\n

request\uff1a\u7531\u6838\u5fc3\u8c03\u7528\uff0c\u4ee5\u67e5\u770b\u7279\u5b9a\u5f15\u811a\u662f\u5426\u53ef\u4ee5\u7528\u4e8e\u5f15\u811a\u590d\u7528\u3002\u5728\u9009\u62e9\u4efb\u4f55\u5b9e\u9645\u7684\u590d\u7528\u8bbe\u7f6e\u4e4b\u524d\uff0c\u5e94\u7531\u6838\u5fc3\u8c03\u7528\u6b64\u51fd\u6570\u6765\u83b7\u53d6\u5f15\u811a\u3002\u5982\u679c\u8be5\u9a71\u52a8\u7a0b\u5e8f\u65e0\u6cd5\u5904\u7406\u8868\u8ff0\u7684\u5f15\u811a\uff0c\u5219\u5e94\u8fd4\u56de\u8d1f\u9519\u8bef\u4ee3\u7801\u6765\u62d2\u7edd\u8bf7\u6c42\u3002

free\uff1a\u5728\u7533\u8bf7\u540e\u91ca\u653e\u5f15\u811a\u3002

get_functions_count\uff1a\u8fd4\u56depin controller device\u652f\u6301\u7684function\u6570\u76ee\u3002

get_function_name\uff1a\u7ed9\u5b9a\u4e00\u4e2afunction selector\uff0c\u8fd4\u56de\u6307\u5b9afunction\u7684\u540d\u79f0\u3002

get_function_groups\uff1a\u7ed9\u5b9a\u4e00\u4e2afunction selector\uff0c\u8fd4\u56de\u6307\u5b9afunction\u7684pin group\u4fe1\u606f\u3002

set_mux\uff1a\u542f\u7528\u67d0\u4e2a\u590d\u7528\u529f\u80fd\u4e0e\u7279\u5b9a\u7684\u5f15\u811a\u7ec4\u3002\u9a71\u52a8\u7a0b\u5e8f\u4e0d\u9700\u8981\u5f04\u6e05\u695a\u542f\u7528\u8fd9\u4e2a\u529f\u80fd\u662f\u5426\u4e0e\u8be5\u7ec4\u5f15\u811a\u7684\u5176\u4ed6\u7528\u9014\u51b2\u7a81\uff0c\u8fd9\u6837\u7684\u51b2\u7a81\u7531\u5f15\u811a\u590d\u7528\u5b50\u7cfb\u7edf\u5904\u7406\u3002func_selector\u9009\u62e9\u4e00\u4e2a\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u800cgroup_selector\u9009\u62e9\u4e00\u7ec4\u8981\u4f7f\u7528\u7684\u7279\u5b9a\u5f15\u811a\u3002\u5728\u7b80\u5355\u7684\u63a7\u5236\u5668\u4e0a\uff0c\u540e\u8005\u53c2\u6570\u53ef\u80fd\u88ab\u5ffd\u7565\u3002

gpio_request_enable\uff1a\u5728\u67d0\u4e9b\u5f15\u811a\u4e0a\u8bf7\u6c42\u5e76\u542f\u7528GPIO\u3002\u4ec5\u5f53\u4f60\u80fd\u591f\u5c06\u6bcf\u4e2a\u5f15\u811a\u5355\u72ec\u590d\u7528\u4e3aGPIO\u65f6\uff0c\u624d\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u53d7\u5f71\u54cd\u7684GPIO\u8303\u56f4\u8fde\u540c\u504f\u79fb\u91cf\uff08\u5f15\u811a\u53f7\u7801\uff09\u4e00\u8d77\u4f20\u9012\u5230\u7279\u5b9a\u7684GPIO\u8303\u56f4\u2014\u2014\u529f\u80fd\u9009\u62e9\u5668\u548c\u5f15\u811a\u7ec4\u4e0e\u6b64\u72ec\u7acb\uff0c\u4f46\u662f\u6838\u5fc3\u5c06\u786e\u4fdd\u5f15\u811a\u4e0d\u4f1a\u51b2\u7a81\u3002

gpio_disable_free\uff1a\u5728\u67d0\u4e2a\u5f15\u811a\u4e0a\u91ca\u653eGPIO\u590d\u7528\u3002

gpio_set_direction\uff1a\u8bbe\u7f6eGPIO\u7684\u8f93\u5165\u6216\u8f93\u51fa\u65b9\u5411\u3002

strict\uff1a\u4e3atrue\u65f6\uff0c\u4e0d\u5141\u8bb8\u67d0\u4e2a\u5f15\u811a\u4f5c\u4e3aGPIO\u548c\u5176\u4ed6\u529f\u80fd\u540c\u65f6\u4f7f\u7528\u3002

\u4ec0\u4e48\u662ffunction\uff1ffunction\u662f\u5f15\u811a\u529f\u80fd\u7684\u62bd\u8c61\uff0cSPI\u662f\u4e00\u4e2afunction\uff0cI2C\u4e5f\u662f\u4e00\u4e2afunction\u3002\u4f46\u662f\u5373\u4fbf\u77e5\u9053\u5177\u4f53\u7684function name\uff0c\u6211\u4eec\u4e5f\u4e0d\u80fd\u786e\u5b9a\u5176\u4f7f\u7528\u5f15\u811a\u7684\u60c5\u51b5\u3002\u6bd4\u5982\u4e00\u4e2aSPI0\u7684\u529f\u80fd\u53ef\u80fd\u4f7f\u7528\u4e86\u5f15\u811a\u7ec4{A8, A7, A6, A5}\uff0c\u4e5f\u53ef\u80fd\u4f7f\u7528\u4e86\u5f15\u811a\u7ec4{G4, G3, G2, G1}\u3002\u4f46\u6beb\u65e0\u7591\u95ee\uff0c\u8fd9\u4e24\u4e2a\u5f15\u811a\u7ec4\u4e0d\u80fd\u540c\u65f6\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\uff0c\u56e0\u4e3a\u82af\u7247\u5185\u90e8SPI0\u7684\u903b\u8f91\u529f\u80fd\u7535\u8def\u53ea\u6709\u4e00\u4e2a\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u7ed9\u51fafunction selector\uff08\u5c31\u50cf\u6570\u7ec4\u7684\u4e00\u4e2a\u7d22\u5f15\uff09\u548cfunction\u7684pin group selector\u624d\u80fd\u8fdb\u884cfunction mux\u7684\u8bbe\u5b9a\u3002

\u5f15\u811a\u7684\u914d\u7f6e\uff0c\u6bd4\u5982\u4e0a\u62c9\u3001\u4e0b\u62c9\u3001\u9ad8\u963b\u6297\u7b49\uff0cpinctrl\u5b50\u7cfb\u7edf\u4f7f\u7528struct pinconf_ops\u7ed3\u6784\u4f53\u6765\u62bd\u8c61\u914d\u7f6e\u7684\u64cd\u4f5c\uff1a

struct pinconf_ops {\n#ifdef CONFIG_GENERIC_PINCONF\n    bool is_generic;\n#endif\n    int (*pin_config_get) (struct pinctrl_dev *pctldev, unsigned pin,\n                           unsigned long *config);\n    int (*pin_config_set) (struct pinctrl_dev *pctldev, unsigned pin,\n                           unsigned long *configs, unsigned num_configs);\n    int (*pin_config_group_get) (struct pinctrl_dev *pctldev, unsigned selector,\n                                 unsigned long *config);\n    int (*pin_config_group_set) (struct pinctrl_dev *pctldev, unsigned selector,\n                                 unsigned long *configs, unsigned num_configs);\n    void (*pin_config_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                                 unsigned offset);\n    void (*pin_config_group_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                                       unsigned selector);\n    void (*pin_config_config_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                                        unsigned long config);\n};\n

is_generic\uff1a\u5bf9\u4e8e\u5e0c\u671b\u4f7f\u7528\u901a\u7528\u63a5\u53e3\u7684\u5f15\u811a\u63a7\u5236\u5668\uff0c\u8be5\u6807\u5fd7\u544a\u8bc9\u6846\u67b6\u5b83\u662f\u901a\u7528\u7684\u3002

pin_config_get\uff1a\u83b7\u53d6\u67d0\u4e2a\u5f15\u811a\u7684\u914d\u7f6e\u3002

pin_config_set\uff1a\u914d\u7f6e\u5355\u4e2a\u5f15\u811a\u3002

pin_config_group_get\uff1a\u83b7\u53d6\u67d0\u4e2a\u5f15\u811a\u7ec4\u7684\u914d\u7f6e\u3002

pin_config_group_set\uff1a\u914d\u7f6e\u67d0\u4e2a\u5f15\u811a\u7ec4\u4e2d\u7684\u6240\u6709\u5f15\u811a\u3002

\u4e0b\u56fe\u63cf\u8ff0\u4e86pin controller device\u7c7b\u522b\u4e0b\u51e0\u4e2a\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u7684\u5173\u7cfb\uff1a

"},{"location":"linux/drivers/pinctrl/#client-device","title":"client device","text":"

\u5728\u5185\u6838\u542f\u52a8\u9636\u6bb5\uff0cdevice_node\u4e00\u822c\u4f1a\u88ab\u8f6c\u6362\u4e3aplatform_device\u7ed3\u6784\u4f53\uff0c\u6216\u8005\u5176\u4ed6\u7ed3\u6784\u4f53\u6bd4\u5982i2c_client\u3001spi_device\uff0c\u5b83\u4eec\u5185\u90e8\u90fd\u6709\u4e00\u4e2astruct device\u6210\u5458\uff08\u4e5f\u5373\u7ee7\u627f\u4e86struct device\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\u3002

\u5728struct device\u7ed3\u6784\u4f53\u91cc\u6709\u4e00\u4e2astruct dev_pin_info\u7ed3\u6784\u4f53\uff0c\u7528\u6765\u4fdd\u5b58\u8bbe\u5907\u7684\u5f15\u811a\u4fe1\u606f\uff1a

struct device {\n    struct kobject kobj;\n    struct device *parent;\n    struct device_private *p;\n    ...\n#ifdef CONFIG_PINCTRL\n    struct dev_pin_info *pins;\n#endif\n    ....\n};\n

struct dev_pin_info\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8client device\u5f15\u811a\u4fe1\u606f\u7684\u7ed3\u6784\u4f53\uff1a

struct dev_pin_info {\n    struct pinctrl *p;\n    struct pinctrl_state *default_state;\n    struct pinctrl_state *init_state;\n#ifdef CONFIG_PM\n    struct pinctrl_state *sleep_state;\n    struct pinctrl_state *idle_state;\n#endif\n};\n

p\uff1a\u4fdd\u5b58\u8be5\u8bbe\u5907\u7684\u6240\u6709\u72b6\u6001\u4fe1\u606f\uff0c\u5305\u62ec\u81ea\u5b9a\u4e49\u7684\u72b6\u6001\u3002

default_state\uff1a\u8868\u793a\u8be5\u8bbe\u5907\u4e0a\u5f15\u811a\u7684\u9ed8\u8ba4\u72b6\u6001\u3002

init_state\uff1a\u8868\u793a\u521d\u59cb\u5316\u5f15\u811a\u7684\u72b6\u6001\u3002

sleep_state/idle_state\uff1a\u5982\u679c\u542f\u7528\u4e86\u7535\u6e90\u7ba1\u7406\u6a21\u5757\uff08Power Management\uff09\uff0c\u5206\u522b\u8868\u793a\u7761\u7720\u548c\u7a7a\u95f2\u72b6\u6001\u3002

pinctrl\u5b50\u7cfb\u7edf\u53c8\u62bd\u8c61\u4e86struct pinctrl\u6765\u63cf\u8ff0\u4e00\u4e2aclient device\u7684\u6240\u6709\u72b6\u6001\uff1a

struct pinctrl {\n    struct list_head node;\n    struct device *dev;\n    struct list_head states;\n    struct pinctrl_state *state;\n    struct list_head dt_maps;\n    struct kref users;\n};\n

node\uff1a\u7528\u4e8e\u5c06\u8be5pin controller\u6302\u5165\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002

dev\uff1a\u8be5pin controller\u5bf9\u5e94\u7684device\u3002

states\uff1a\u4fdd\u5b58\u8be5pin controller\u7684\u6240\u6709\u72b6\u6001\u4fe1\u606f\uff0c\u8fd9\u4e9b\u72b6\u6001\u4fe1\u606f\u90fd\u4fdd\u5b58\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002

state\uff1a\u5f53\u524dpin controller\u7684\u72b6\u6001\u3002

dt_maps\uff1a\u4fdd\u5b58\u8be5pin controller\u5bf9\u5e94\u7684\u6240\u6709\u8bbe\u5907\u8282\u70b9\u4fe1\u606f\uff0c\u8fd9\u4e9b\u8bbe\u5907\u8282\u70b9\u4fe1\u606f\u90fd\u4fdd\u5b58\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002

users\uff1a\u5f15\u7528\u8ba1\u6570\u3002

\u7cfb\u7edf\u4e2d\u7684\u6bcf\u4e00\u4e2a\u9700\u8981\u548cpinctrl\u5b50\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u8bbe\u5907\u5728\u8fdb\u884c\u914d\u7f6e\u4e4b\u524d\u90fd\u9700\u8981\u9996\u5148\u83b7\u53d6\u8fd9\u4e2astruct pinctrl\u7ed3\u6784\u4f53\uff0c\u5c5e\u4e8e\u8be5\u8bbe\u5907\u7684\u6240\u6709\u7684\u72b6\u6001\u90fd\u5c06\u88ab\u6302\u5165\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u94fe\u8868\u5934\u5c31\u662fstates\u6210\u5458\u3002

\u5355\u4e2a\u72b6\u6001\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

struct pinctrl_state {\n    struct list_head node;\n    const char *name;\n    struct list_head settings;\n};\n

name\uff1a\u6b64\u72b6\u6001\u7684\u540d\u79f0\u3002

settings\uff1a\u5c5e\u4e8e\u8be5\u72b6\u6001\u7684\u6240\u6709settings\u3002

\u8bbe\u5907\u5728\u5355\u4e2a\u72b6\u6001\u4e0b\u7684pinctrl\u63a7\u5236\u4fe1\u606f\uff0c\u7531struct pinctrl_setting\u7ed3\u6784\u4f53\u63cf\u8ff0\uff1a

struct pinctrl_setting { \n    struct list_head node; \n    enum pinctrl_map_type type; \n    struct pinctrl_dev *pctldev;    //\u5bf9\u5e94\u7684pin controller device\n    const char *dev_name;           //\u8bbe\u5907\u540d\u79f0\n    union { \n        struct pinctrl_setting_mux mux;             //mux\u914d\u7f6e\u6570\u636e\n        struct pinctrl_setting_configs configs;     //config\u914d\u7f6e\u6570\u636e\n    } data; \n};\n

\u5f53\u67d0\u4e2adriver\u8bbe\u5b9a\u4e00\u4e2apin state\u7684\u65f6\u5019\uff0cpinctrl\u5b50\u7cfb\u7edf\u5185\u90e8\u4f1a\u904d\u5386\u8be5state\u7684settings\u94fe\u8868\uff0c\u5c06\u4e00\u4e2a\u4e00\u4e2a\u7684setting\u8fdb\u884c\u8bbe\u5b9a\u3002\u8fd9\u4e9bsettings\u6709\u5404\u79cd\u7c7b\u578b\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a

enum pinctrl_map_type { \n    PIN_MAP_TYPE_INVALID, \n    PIN_MAP_TYPE_DUMMY_STATE, \n    PIN_MAP_TYPE_MUX_GROUP,     // \u529f\u80fd\u590d\u7528\u7684setting\n    PIN_MAP_TYPE_CONFIGS_PIN,   // \u8bbe\u5b9a\u5355\u4e00\u4e00\u4e2apin\u7684\u7535\u6c14\u7279\u6027 \n    PIN_MAP_TYPE_CONFIGS_GROUP, // \u8bbe\u5b9a\u5355pin group\u7684\u7535\u6c14\u7279\u6027 \n};\n

struct pinctrl_setting_mux\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin mux\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_MUX_GROUP\uff09\uff1a

struct pinctrl_setting_mux {\n    unsigned group;\n    unsigned func;\n};\n

group\uff1apin group selector\u3002

function\uff1apin function selector\u3002

struct pinctrl_setting_config\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin config\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_CONFIGS_PIN\u548cPIN_MAP_TYPE_CONFIGS_GROUP\uff09\uff1a

struct pinctrl_setting_configs {\n    unsigned group_or_pin;\n    unsigned long *configs;\n    unsigned num_configs;\n};\n

group_or_pin\uff1a\u8981\u914d\u7f6e\u7684pin\u7f16\u53f7\u6216\u8005pin group selector\u3002

configs\uff1a\u6307\u5411\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u4e86\u4e00\u7ec4\u914d\u7f6e\u53c2\u6570\u6216\u503c\uff0c\u7528\u4e8e\u8bbe\u7f6e\u7279\u5b9a\u5f15\u811a\u6216\u5f15\u811a\u7ec4\u7684\u786c\u4ef6\u53c2\u6570\u3002

num_configs\uff1a\u9700\u8981\u5199\u5165\u7684\u914d\u7f6e\u53c2\u6570\u4e2a\u6570\u3002

struct pinctrl_map\u7528\u4e8e\u63cf\u8ff0client device\u7684\u6620\u5c04\u914d\u7f6e\uff0c\u4f7f\u7528pin controller device\u7684pinctrl_desc->pctlops->dt_node_to_map\u6765\u5904\u7406\u8bbe\u5907\u6811\u4e2d\u7684\u5f15\u811a\u914d\u7f6e\u8282\u70b9\u3002\u4f8b\u5982\u67d0\u914d\u7f6e\u8282\u70b9\u2014\u2014pinctrl-0 = <&uart0_xfer &uart0_cts &uart0_rts>\uff0c\u90a3\u4e48uart0_xfer \u3001uart0_cts \u3001uart0_rts\u8282\u70b9\u5747\u4f1a\u88abdt_node_to_map()\u51fd\u6570\u89e3\u6790\u4e3a\u4e00\u7cfb\u5217\u7684struct pinctrl_map\uff0c\u7136\u540e\u88ab\u8f6c\u6362\u4e3astruct pinctrl_setting\uff0c\u5b58\u5165struct pinctrl_state.settings\u94fe\u8868\u4e2d\u3002

struct pinctrl_map {\n    const char *dev_name;\n    const char *name;\n    enum pinctrl_map_type type;\n    const char *ctrl_dev_name;\n    union {\n        struct pinctrl_map_mux mux;\n        struct pinctrl_map_configs configs;\n    } data;\n};\n

dev_name\uff1a\u8bbe\u5907\u540d\u79f0\uff0c\u9700\u8981\u4e0e\u8be5client device\u4e2dstruct device\u4e2d\u7684\u540d\u79f0\u4e00\u81f4\u3002

name\uff1a\u72b6\u6001\u540d\u79f0\u3002

type\uff1a\u6620\u5c04\u9879\u7684\u7c7b\u578b\u3002

ctrl_dev_name\uff1a\u8bbe\u5907\u540d\u79f0\u3002

data\uff1a\u8be5\u6620\u5c04\u9879\u7684\u5177\u4f53\u6570\u636e\uff0c\u5305\u62ecmux\u548cconfigs\u4e24\u79cd\u7c7b\u578b\uff0c\u5206\u522b\u4ee3\u8868\u590d\u7528\u9009\u62e9\u548c\u5f15\u811a\u914d\u7f6e\u3002

struct pinctrl_map_mux\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin mux\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_MUX_GROUP\uff09\uff1a

struct pinctrl_map_mux {\n    const char *group;\n    const char *function;\n};\n

group\uff1a\u9700\u8981\u914d\u7f6e\u590d\u7528\u529f\u80fd\u7684pin group\u7684\u540d\u79f0\u3002

function:\u590d\u7528\u529f\u80fd\u7684\u540d\u79f0\u3002

struct pinctrl_map_configs\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin config\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_CONFIGS_PIN\u548cPIN_MAP_TYPE_CONFIGS_GROUP\uff09\uff1a

struct pinctrl_map_configs {\n    const char *group_or_pin;\n    unsigned long *configs;\n    unsigned num_configs;\n};\n

group_or_pin\uff1a\u9700\u8981\u914d\u7f6e\u7684pin\u6216\u8005pin group\u7684\u540d\u79f0\u3002

configs\uff1a\u6307\u5411\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u4e86\u4e00\u7ec4\u914d\u7f6e\u53c2\u6570\u6216\u503c\uff0c\u7528\u4e8e\u8bbe\u7f6e\u7279\u5b9a\u5f15\u811a\u6216\u5f15\u811a\u7ec4\u7684\u786c\u4ef6\u53c2\u6570\u3002

num_configs\uff1a\u9700\u8981\u5199\u5165\u7684\u914d\u7f6e\u53c2\u6570\u4e2a\u6570\u3002

struct pinctrl_maps\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0\u6620\u5c04\u8868\uff08mapping table\uff09\u7684\u90e8\u5206\u9879\uff1a

struct pinctrl_maps {\n    struct list_head node;\n    const struct pinctrl_map *maps;\n    unsigned num_maps;\n};\n

maps\uff1a\u6307\u5411\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u4e86\u4e00\u7ec4\u6620\u5c04\u8868\u9879\u3002

num_maps\uff1a\u9700\u8981\u5199\u5165\u7684\u6620\u5c04\u8868\u9879\u4e2a\u6570\u3002

\u5728\u524d\u9762\u6211\u4eec\u8bf4\u8fc7\uff0c\u5f15\u811a\u914d\u7f6e\u8282\u70b9\u4f1a\u88abdt_node_to_map()\u51fd\u6570\u89e3\u6790\u4e3astruct pinctrl_map\uff0c\u5e76\u4ee5\u6570\u7ec4\u6307\u9488\u5f62\u5f0f\u8fd4\u56de\u3002 \u800cstruct pinctrl_dt_map\u5c31\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u4ece\u8bbe\u5907\u6811\u89e3\u6790\u51fa\u6765\u7684\u6620\u5c04\u8868\u6570\u636e\u7684\u7ed3\u6784\u4f53\uff0c\u5176\u6210\u5458map\u6307\u5411\u8fd9\u4e2astruct pinctrl_map\u6570\u7ec4\uff1a

struct pinctrl_dt_map {\n    struct list_head node;\n    struct pinctrl_dev *pctldev;\n    struct pinctrl_map *map;\n    unsigned num_maps;\n};\n

pctldev\uff1a\u6307\u5411\u5206\u914d\u8be5\u6620\u5c04\u8868\u7684pin controller device\u3002

map\uff1a\u5b9e\u9645\u6620\u5c04\u8868\u6570\u636e\u3002

num_maps\uff1a\u6620\u5c04\u8868\u4e2d\u7684\u6570\u76ee\u3002

\u4e0b\u56fe\u63cf\u8ff0\u4e86client device\u7c7b\u522b\u4e0b\u51e0\u4e2a\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u7684\u5173\u7cfb\uff1a

"},{"location":"linux/drivers/pinctrl/#gpio","title":"\u4e0eGPIO\u5b50\u7cfb\u7edf\u4ea4\u4e92","text":"

\u4f5c\u4e3a\u8f6f\u4ef6\u5de5\u7a0b\u5e08\uff0c\u6211\u4eec\u671f\u671b\u7684\u786c\u4ef6\u8bbe\u8ba1\u5e94\u8be5\u5982\u4e0b\u56fe\u6240\u793a\uff1a

GPIO\u7684HW block\u5e94\u8be5\u4e0e\u5176\u4ed6\u5b50\u7cfb\u7edf\u7684block\u662f\u5e73\u7ea7\u5173\u7cfb\uff0c\u5b83\u4eec\u5171\u540c\u8f93\u5165\u5230\u4e00\u4e2a\u590d\u7528\u5668block\uff0c\u7531\u8be5block\u7684\u5bc4\u5b58\u5668\u63a7\u5236\u54ea\u4e00\u6761\u7535\u8def\u88ab\u63a5\u901a\u3002\u800cpin configuration\u5e94\u8be5\u662f\u5168\u5c40\u7684\uff0c\u4e0d\u8bba\u54ea\u79cdblock\u5904\u4e8eactive\u72b6\u6001\uff0c\u53ea\u8981\u63d0\u4f9b\u7edf\u4e00\u7684\u63a5\u53e3\u4f9b\u5b83\u4eec\u4f7f\u7528\u5373\u53ef\u3002\u5728\u8f6f\u4ef6\u5c42\u9762\uff0c\u6211\u4eec\u5f53\u7136\u53ef\u4ee5\u8ba9pinctrl\u5b50\u7cfb\u7edf\u548cGPIO\u5b50\u7cfb\u7edf\u5b8c\u5168\u72ec\u7acb\uff0c\u5404\u81ea\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f46\u662f\u5b9e\u9645\u7684SOC\u8bbe\u8ba1\u5e76\u975e\u603b\u662f\u548c\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u6240\u671f\u671b\u7684\u90a3\u6837\uff0c\u6bd4\u5982\u6709\u7684SOC\u8bbe\u8ba1\u6846\u67b6\u5982\u4e0b\u56fe\u6240\u793a\uff1a

\u6b64\u65f6\u7684GPIO block\u5e76\u6ca1\u6709\u548c\u5176\u4ed6block\u72ec\u7acb\uff0c\u800c\u662f\u6c38\u8fdc\u5904\u4e8eactive\u72b6\u6001\uff0c\u7ea2\u8272\u8fb9\u6846\u7684\u4e09\u4e2ablock\u88ab\u7d27\u5bc6\u5730\u8054\u7cfb\u5728\u4e00\u8d77\uff0c\u5b83\u4eec\u7684\u5bc4\u5b58\u5668\u5360\u636e\u4e86\u4e00\u4e2amemory range\u3002\u8fd9\u4e2a\u65f6\u5019\uff0c\u5bf9\u4e8e\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u6765\u8bf4\uff0c\u4e0d\u5f97\u4e0d\u5728pinctrl\u5b50\u7cfb\u7edf\u4e2d\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\uff0c\u624d\u80fd\u6b63\u786e\u5730\u63a7\u5236GPIO block\u3002\u4efb\u4f55\u4e00\u4e2agpio chip\uff0c\u5728\u4f7f\u7528GPIO\u529f\u80fd\u65f6\uff0c\u90fd\u5fc5\u987b\u8981\u5148\u5411\u7cfb\u7edf\u7684pinctrl\u5b50\u7cfb\u7edf\u7533\u8bf7\u5f15\u811a\uff0c\u5e76\u5c06\u5f15\u811a\u914d\u7f6e\u4e3aGPIO\u529f\u80fd\u3002

pinctrl\u5b50\u7cfb\u7edf\u4e3aGPIO\u63d0\u4f9b\u7684\u63a5\u53e3\u5b9a\u4e49\u5728<linux/pinctrl/consumer.h>\u4e2d\uff1a

int pinctrl_gpio_request(unsigned gpio);\nvoid pinctrl_gpio_free(unsigned gpio);\nint pinctrl_gpio_direction_input(unsigned gpio);\nint pinctrl_gpio_direction_output(unsigned gpio);\nint pinctrl_gpio_set_config(unsigned gpio, unsigned long config);\n

pinctrl_gpio_request\uff1a\u5411pinctrl\u5b50\u7cfb\u7edf\u7533\u8bf7\u4e00\u4e2agpio\u3002

pinctrl_gpio_free\uff1a\u91ca\u653e\u4e00\u4e2agpio\u3002

pinctrl_gpio_direction_input\uff1a\u5c06\u4e00\u4e2agpio\u8bbe\u7f6e\u4e3a\u8f93\u5165\u6a21\u5f0f\u3002

pinctrl_gpio_direction_output\uff1a\u5c06\u4e00\u4e2agpio\u8bbe\u7f6e\u4e3a\u8f93\u51fa\u6a21\u5f0f\u3002

pinctrl_gpio_set_config\uff1a\u8bbe\u7f6e\u4e00\u4e2agpio\u7684\u914d\u7f6e\u3002

\u5f53GPIO\u9a71\u52a8\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528\u67d0\u4e2a\u5f15\u811a\u65f6\uff0c\u76f4\u63a5\u8c03\u7528pinctrl_gpio_request()\u51fd\u6570\uff0c\u5411pinctrl\u5b50\u7cfb\u7edf\u7533\u8bf7\u3002pinctrl\u5b50\u7cfb\u7edf\u4f1a\u7ef4\u62a4\u4e00\u4e2agpio number\u5230pin number\u7684\u6620\u5c04\uff0c\u5c06GPIO\u5b50\u7cfb\u7edf\u7684gpio number\u8f6c\u6362\u4e3apin number\uff0c\u5e76\u8c03\u7528struct pinmux_ops\u7ed3\u6784\u4f53\u4e2d\u6709\u5173GPIO\u7684\u56de\u8c03\u51fd\u6570\u5373\u53ef\u3002

\u6beb\u65e0\u7591\u95ee\uff0c\u7533\u8bf7GPIO\u8d44\u6e90\u672c\u5e94\u8be5\u662fGPIO\u5b50\u7cfb\u7edf\u7684\u8d23\u4efb\uff0c\u4f46\u662f\u7531\u4e8e\u4e4b\u524d\u63cf\u8ff0\u7684\u7f18\u7531\uff0cpinctrl\u5b50\u7cfb\u7edf\u4e0d\u5f97\u4e0d\u63d0\u4f9b\u63a5\u53e3\u51fd\u6570\u4f9bGPIO\u9a71\u52a8\u7a0b\u5e8f\u4f7f\u7528\u3002\u5f53\u7136\uff0c\u5176\u4ed6\u7684\u9a71\u52a8\u7a0b\u5e8f\u4e0d\u5e94\u8be5\u8c03\u7528\u6b64\u63a5\u53e3\u3002

\u5982\u4f55\u63d0\u4f9bgpio number\u5230pin number\u7684\u6620\u5c04\u5462\uff1f\u662f\u901a\u8fc7\u4e00\u4e2astruct pinctrl_gpio_range\u7ed3\u6784\u4f53\u6765\u5b9e\u73b0\u7684\uff1a

struct pinctrl_gpio_range {\n    struct list_head node;\n    const char *name;\n    unsigned int id;\n    unsigned int base;\n    unsigned int pin_base;\n    unsigned int npins;\n    unsigned const *pins;\n    struct gpio_chip *gc;\n};\n

name\uff1a\u533a\u57df\u7684\u540d\u5b57\u3002

id\uff1a\u533a\u57dfID\u3002

base\uff1a\u533a\u57df\u8d77\u59cb\u7684GPIO ID\u3002

pin_base\uff1a\u533a\u57df\u8d77\u59cb\u7684pin ID

npins\uff1a\u533a\u57df\u5305\u542b\u7684\u5f15\u811a\u6570\u3002

pins\uff1a\u533a\u57df\u5305\u542b\u7684\u5f15\u811a\u5217\u8868\u3002

gc\uff1a\u533a\u57df\u5bf9\u5e94\u7684gpio_chip\u3002

\u4e00\u4e2astruct gpio_chip\u7ed3\u6784\u4f53\u53ef\u4ee5\u4ee5\u5982\u4e0b\u7684\u65b9\u5f0f\u6dfb\u52a0\u5230pin controller device\u4e2d\uff1a

struct gpio_chip chip_a;\nstruct gpio_chip chip_b;\n\nstatic struct pinctrl_gpio_range gpio_range_a = {\n    .name = \"chip a\",\n    .id = 0,\n    .base = 32,\n    .pin_base = 32,\n    .npins = 16,\n    .gc = &chip_a;\n};\n\nstatic struct pinctrl_gpio_range gpio_range_b = {\n    .name = \"chip b\",\n    .id = 0,\n    .base = 48,\n    .pin_base = 64,\n    .npins = 8,\n    .gc = &chip_b;\n};\n\n{\n    struct pinctrl_dev *pctl;\n    ...\n    pinctrl_add_gpio_range(pctl, &gpio_range_a);\n    pinctrl_add_gpio_range(pctl, &gpio_range_b)\n}\n

\u4ee5\u4e0a\u662f\u4e00\u4e2apin controller device\u7684\u793a\u4f8b\uff0c\u5b83\u5305\u542b\u4e24\u4e2agpio_chip\u7ed3\u6784\u4f53\uff0c\u5206\u522b\u5bf9\u5e94\u4e24\u4e2agpio_chip\u5b9e\u4f8b\u3002\"chip a\"\u548c\"chip b\"\u7684\u8d77\u59cbGPIO ID\u5206\u522b\u4e3a32\u548c48\uff0c\u8d77\u59cbpin ID\u5206\u522b\u4e3a32\u548c64\u3002\"chip a\"\u670916\u4e2a\u5f15\u811a\uff0c\"chip b\"\u67098\u4e2a\u5f15\u811a\u3002

chip a: - GPIO range: 32-47 - Pin range: 32-47

chip b: - GPIO range: 48-55 - Pin range: 64-71

\u4e0a\u8ff0\u4f8b\u5b50\u5047\u5b9aGPIO\u548cpin\u7684\u6620\u5c04\u662f\u7ebf\u6027\u7684\uff0c\u5426\u5219\u6211\u4eec\u5fc5\u987b\u5f97\u624b\u52a8\u5b9a\u4e49\u5f15\u811a\u4fe1\u606f\uff1a

static const unsigned range_pins[] = {14, 1, 22, 17, 10, 8, 6, 2};\n\nstatic struct pinctrl_gpio_range gpio_range_ = {\n    .name = \"chip\",\n    .id = 0,\n    .base = 32,\n    .pins = &range_pins,\n    .npins = ARRAY_SIZE(range_pins),\n    .gc = &chip;\n};\n

\u5728\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cpin_base\u5c5e\u6027\u4f1a\u88ab\u5ffd\u7565\u3002

"},{"location":"linux/drivers/platform/","title":"\u5e73\u53f0\u8bbe\u5907\u9a71\u52a8","text":"

\u5bf9\u4e8eI2C\u3001SPI\u3001USB\u8fd9\u4e9b\u5e38\u89c1\u7684\u8bbe\u5907\u6765\u8bf4\uff0cLinux\u5185\u6838\u90fd\u4f1a\u521b\u5efa\u4e0e\u4e4b\u76f8\u5bf9\u5e94\u7684\u9a71\u52a8\u603b\u7ebf\u3002\u4f46\u662f\u6709\u4e9b\u7ed3\u6784\u7b80\u5355\u7684\u8bbe\u5907\uff0c\u6bd4\u5982led\u3001rtc\u65f6\u949f\u3001\u8702\u9e23\u5668\u7b49\uff0c\u5185\u6838\u5c31\u4e0d\u4f1a\u81ea\u5df1\u521b\u5efa\u9a71\u52a8\u603b\u7ebf\u3002\u4e3a\u4e86\u4f7f\u8fd9\u90e8\u5206\u8bbe\u5907\u7684\u9a71\u52a8\u5f00\u53d1\u4e5f\u80fd\u9075\u5faa\u8bbe\u5907\u9a71\u52a8\u6a21\u578b\uff0cLinux\u5185\u6838\u5f15\u5165\u4e86\u865a\u62df\u7684\u603b\u7ebf\u2014\u2014\u5e73\u53f0\u603b\u7ebf\uff08platform bus\uff09\u3002\u5e73\u53f0\u603b\u7ebf\u7528\u4e8e\u7ba1\u7406\u548c\u6302\u8f7d\u90a3\u4e9b\u6ca1\u6709\u76f8\u5e94\u7269\u7406\u603b\u7ebf\u7684\u8bbe\u5907\uff0c\u8fd9\u4e9b\u8bbe\u5907\u88ab\u79f0\u4e3a\u5e73\u53f0\u8bbe\u5907\uff0c\u5bf9\u5e94\u7684\u8bbe\u5907\u9a71\u52a8\u88ab\u79f0\u4e3a\u5e73\u53f0\u9a71\u52a8\u3002\u5e73\u53f0\u8bbe\u5907\u5bf9\u4e8eLinux\u9a71\u52a8\u5de5\u7a0b\u5e08\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u7684\u9a71\u52a8\u4ee3\u7801\uff0c\u5b9e\u9645\u5c31\u662f\u4e3a\u4e86\u9a71\u52a8\u5e73\u53f0\u8bbe\u5907\u3002

Platform\u67b6\u6784\u56fe\u5982\u4e0b\u6240\u793a\uff1a

\u4ece\u56fe\u7247\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff1a

  • platform bus\uff1a\u7ee7\u627f\u81eabus\u6a21\u5757\uff0c\u7528\u4e8e\u6302\u8f7dplatform\u8bbe\u5907\u3002
  • platform device\uff1a\u7ee7\u627f\u81eadevice\u6a21\u5757\uff0c\u7528\u4e8e\u63cf\u8ff0platform\u8bbe\u5907\u3002
  • platform drvier\uff1a\u7ee7\u627f\u81eadevice_driver\u6a21\u5757\uff0c\u7528\u4e8e\u9a71\u52a8platform\u8bbe\u5907\u3002
"},{"location":"linux/drivers/platform/#_2","title":"\u5e73\u53f0\u603b\u7ebf","text":"

\u5728Linux\u5e73\u53f0\u8bbe\u5907\u9a71\u52a8\u6a21\u578b\u4e2d\uff0c\u603b\u7ebf\u662f\u6700\u91cd\u8981\u7684\u4e00\u73af\uff0c\u8d1f\u8d23\u5339\u914d\u8bbe\u5907\u548c\u9a71\u52a8\u3002\u5b83\u7ef4\u62a4\u7740\u4e24\u4e2a\u94fe\u8868\uff0c\u91cc\u9762\u8bb0\u5f55\u7740\u5404\u4e2a\u5df2\u7ecf\u6ce8\u518c\u7684\u5e73\u53f0\u8bbe\u5907\u548c\u5e73\u53f0\u9a71\u52a8\u3002\u6bcf\u5f53\u6709\u65b0\u7684\u8bbe\u5907\u6216\u8005\u662f\u9a71\u52a8\u52a0\u5165\u5230\u603b\u7ebf\u65f6\uff0c\u4fbf\u4f1a\u8c03\u7528platform_match()\u51fd\u6570\u5bf9\u65b0\u589e\u7684\u8bbe\u5907\u6216\u9a71\u52a8\u8fdb\u884c\u914d\u5bf9\u3002\u5185\u6838\u4f7f\u7528struct bus_type platform_bus_type\u6765\u63cf\u8ff0\u5e73\u53f0\u603b\u7ebf\uff0c\u8be5\u603b\u7ebf\u5728\u5185\u6838\u521d\u59cb\u5316\u7684\u65f6\u5019\u6ce8\u518c\uff1a

sturct bus_type platform_bus_type{\n    .name = \"platform\",\n    .dev_groups = platform_dev_groups,\n    .match = platform_match,\n    .uevent = platform_uevent,\n    .pm = &platform_dev_pm_ops,\n};\n

\u5bf9platform_bus_type\u7684\u521d\u59cb\u5316\u6765\u8bf4\uff0cmatch\u51fd\u6570\u6307\u9488\u6700\u4e3a\u91cd\u8981\uff0c\u5b83\u6307\u5411\u7684\u51fd\u6570\u8d1f\u8d23\u5b9e\u73b0\u5e73\u53f0\u603b\u7ebf\u548c\u5e73\u53f0\u8bbe\u5907\u7684\u5339\u914d\u8fc7\u7a0b\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u9a71\u52a8\u603b\u7ebf\uff0c\u90fd\u5fc5\u987b\u5b9e\u4f8b\u5316\u8be5\u51fd\u6570\u6307\u9488\u3002

"},{"location":"linux/drivers/platform/#_3","title":"\u5e73\u53f0\u8bbe\u5907","text":"

\u5185\u6838\u4f7f\u7528platform_device\u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u5e73\u53f0\u8bbe\u5907\uff1a

 struct platform_device {\n     const char *name;    //\u8bbe\u5907\u540d\u79f0\uff0c\u5339\u914d\u65f6\u4f1a\u6bd4\u8f83\u9a71\u52a8\u7684\u540d\u5b57\n     int id;              //\u5185\u6838\u5141\u8bb8\u5b58\u5728\u591a\u4e2a\n     struct device dev;   //\u7ee7\u627f\u7684device\u7ed3\u6784\u4f53\n     u32 num_resources;   //\u8bb0\u5f55\u8d44\u6e90\u7684\u6570\u76ee\n     struct resource *resource;    //\u5e73\u53f0\u8bbe\u5907\u63d0\u4f9b\u7ed9\u9a71\u52a8\u7684\u8d44\u6e90\n     const struct platform_device_id *id_entry;    \n };\n

\u5e73\u53f0\u8bbe\u5907\u7684\u5de5\u4f5c\u662f\u4e3a\u9a71\u52a8\u7a0b\u5e8f\u63d0\u4f9b\u8bbe\u5907\u4fe1\u606f,\u8bbe\u5907\u4fe1\u606f\u5305\u62ec\u786c\u4ef6\u4fe1\u606f\u548c\u8f6f\u4ef6\u4fe1\u606f\u4e24\u90e8\u5206\u3002

  1. \u786c\u4ef6\u4fe1\u606f\uff1a\u9a71\u52a8\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528\u5230\u4ec0\u4e48\u5bc4\u5b58\u5668\uff0c\u5360\u7528\u54ea\u4e9b\u4e2d\u65ad\u53f7\u3001\u5185\u5b58\u8d44\u6e90\u3001IO\u53e3\u7b49\u7b49

  2. \u8f6f\u4ef6\u4fe1\u606f\uff1a\u4ee5\u592a\u7f51\u5361\u8bbe\u5907\u4e2d\u7684MAC\u5730\u5740\u3001I2C\u8bbe\u5907\u4e2d\u7684\u8bbe\u5907\u5730\u5740\u3001SPI\u8bbe\u5907\u7684\u7247\u9009\u4fe1\u53f7\u7ebf\u7b49\u7b49

\u5bf9\u4e8e\u786c\u4ef6\u4fe1\u606f\uff0c\u4f7f\u7528\u7ed3\u6784\u4f53struct resource\u6765\u4fdd\u5b58\u8bbe\u5907\u6240\u63d0\u4f9b\u7684\u8d44\u6e90\uff0c\u6bd4\u5982\u8bbe\u5907\u4f7f\u7528\u7684\u4e2d\u65ad\u7f16\u53f7\uff0c\u5bc4\u5b58\u5668\u7269\u7406\u5730\u5740\u7b49\uff0c\u7ed3\u6784\u4f53\u539f\u578b\u5982\u4e0b\uff1a

struct resource {\n    resource_size_t start;\n    resource_size_t end;\n    const char *name;\n    unsigned long flags;\n};\n

name\uff1a \u6307\u5b9a\u8d44\u6e90\u7684\u540d\u5b57\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e3aNULL

start\u3001end\uff1a \u6307\u5b9a\u8d44\u6e90\u7684\u8d77\u59cb\u5730\u5740\u4ee5\u53ca\u7ed3\u675f\u5730\u5740

flags\uff1a \u7528\u4e8e\u6307\u5b9a\u8be5\u8d44\u6e90\u7684\u7c7b\u578b\uff0c\u5728Linux\u4e2d\uff0c\u8d44\u6e90\u5305\u62ecI/O\u3001Memory\u3001Register\u3001IRQ\u3001DMA\u3001Bus\u7b49\u591a\u79cd\u7c7b\u578b\uff0c\u6700\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a

\u8d44\u6e90\u5b8f\u5b9a\u4e49 \u63cf\u8ff0 IORESOURCE_IO \u7528\u4e8eIO\u5730\u5740\u7a7a\u95f4\uff0c\u5bf9\u5e94\u4e8eIO\u7aef\u53e3\u6620\u5c04\u65b9\u5f0f IORESOURCE_MEM \u7528\u4e8e\u5916\u8bbe\u7684\u53ef\u76f4\u63a5\u5bfb\u5740\u7684\u5730\u5740\u7a7a\u95f4 IORESOURCE_IRQ \u7528\u4e8e\u6307\u5b9a\u8be5\u8bbe\u5907\u4f7f\u7528\u67d0\u4e2a\u4e2d\u65ad IORESOURCE_DMA \u7528\u4e8e\u6307\u5b9a\u4f7f\u7528\u7684DMA\u901a\u9053

\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u7684\u4e3b\u8981\u76ee\u7684\u662f\u64cd\u4f5c\u8bbe\u5907\u7684\u5bc4\u5b58\u5668\u3002\u4e0d\u540c\u67b6\u6784\u7684\u8ba1\u7b97\u673a\u63d0\u4f9b\u4e0d\u540c\u7684\u64cd\u4f5c\u63a5\u53e3\uff0c\u4e3b\u8981\u6709IO\u7aef\u53e3\u6620\u5c04\u548cIO\u5167\u5b58\u6620\u5c04\u4e24\u79cd\u65b9\u5f0f\u3002\u5bf9\u5e94\u4e8eIO\u7aef\u53e3\u6620\u5c04\u65b9\u5f0f\uff0c\u53ea\u80fd\u901a\u8fc7\u4e13\u95e8\u7684\u63a5\u53e3\u51fd\u6570\uff08\u5982inb\u3001outb\uff09\u624d\u80fd\u8bbf\u95ee\uff1b\u91c7\u7528IO\u5185\u5b58\u6620\u5c04\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u50cf\u8bbf\u95ee\u5185\u5b58\u4e00\u6837\uff0c\u53bb\u8bfb\u5199\u5bc4\u5b58\u5668\u3002\u5728\u5d4c\u5165\u5f0f\u4e2d\uff0c\u57fa\u672c\u4e0a\u6ca1\u6709IO\u5730\u5740\u7a7a\u95f4\uff0c\u6240\u4ee5\u901a\u5e38\u4f7f\u7528IORESOURCE_MEM\u3002

\u5728\u8d44\u6e90\u7684\u8d77\u59cb\u5730\u5740\u548c\u7ed3\u675f\u5730\u5740\u4e2d\uff0c\u5bf9\u4e8eIORESOURCE_IO\u6216\u8005\u662fIORESOURCE_MEM\uff0c\u4ed6\u4eec\u8868\u793a\u8981\u4f7f\u7528\u7684\u5185\u5b58\u7684\u8d77\u59cb\u4f4d\u7f6e\u4ee5\u53ca\u7ed3\u675f\u4f4d\u7f6e\uff1b\u82e5\u662f\u53ea\u7528\u4e00\u4e2a\u4e2d\u65ad\u5f15\u811a\u6216\u8005\u662f\u4e00\u4e2a\u901a\u9053\uff0c\u5219\u5b83\u4eec\u7684start\u548cend\u6210\u5458\u503c\u5fc5\u987b\u662f\u76f8\u7b49\u7684\u3002

\u6ce8\u518c/\u6ce8\u9500\u5e73\u53f0\u8bbe\u5907\u7528\u5230\u7684\u51fd\u6570\u5982\u4e0b\uff1a

int platform_device_register(struct platform_device *pdev);\nvoid platform_device_unregister(struct platform_device *pdev);\n

\u8fd9\u4e24\u4e2a\u51fd\u6570\u5e94\u8be5\u5728\u6a21\u5757\u7684\u8fdb\u5165\u4e0e\u9000\u51fa\u51fd\u6570\u4e2d\u88ab\u8c03\u7528\u3002

"},{"location":"linux/drivers/platform/#_4","title":"\u5e73\u53f0\u9a71\u52a8","text":"

\u5185\u6838\u4f7f\u7528platform_driver\u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u5e73\u53f0\u9a71\u52a8\uff1a

struct platform_driver {\n    int (*probe)(struct platform_device *);\n    int (*remove)(structg platform_device *);\n    int (*suspend)(struct platform_device *, pm_message_t state);\n    int (*resume)(struct platform_device *);\n    struct device_driver driver;\n    const struct platform_device_id *id_table;\n};\n

\u9664\u4e86\u63d0\u4f9b\u4e00\u4e9b\u56de\u8c03\u51fd\u6570\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2aid_table\u7684\u6307\u9488\u3002\u8fd9\u4e2a\u6307\u9488\u7528\u6765\u8868\u793a\u8be5\u9a71\u52a8\u80fd\u591f\u517c\u5bb9\u7684platform_device\u3002

\u6211\u4eec\u6765\u770b\u4e00\u4e0bstruct platform_device_id\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\uff1a

struct platform_device_id {\n    char name[PLATFORM_NAME_SIZE];\n    kernel_ulong_t driver_data;\n};\n

name\u7528\u4e8e\u6307\u5b9a\u9a71\u52a8\u7684\u540d\u79f0\uff0c\u603b\u7ebf\u8fdb\u884c\u5339\u914d\u65f6\uff0c\u4f1a\u6839\u636e\u8be5name\u6210\u5458\u4e0eplatform_device\u4e2d\u7684name\u6210\u5458\u8fdb\u884c\u5339\u914d\uff0c\u4e00\u65e6\u5339\u914d\u5c31\u4f1a\u8c03\u7528probe()\u51fd\u6570\u3002driver_data\u7528\u6765\u4fdd\u5b58\u8bbe\u5907\u7684\u914d\u7f6e\u3002\u4e3a\u4e86\u51cf\u5c11\u4ee3\u7801\u7684\u5197\u4f59\uff0c\u4e00\u4e2a\u9a71\u52a8\u53ef\u4ee5\u5339\u914d\u591a\u4e2a\u8bbe\u5907\u3002

\u8fd9\u91cc\u63d2\u4e2a\u9898\u5916\u8bdd\uff0c\u5f53\u624b\u52a8\u5b9e\u73b0probe()\u51fd\u6570\u65f6\u6d89\u53ca\u5230\u5185\u5b58\u5206\u914d\u7684\u95ee\u9898\u3002\u5f88\u591a\u9a71\u52a8\u7a0b\u5e8f\u90fd\u4f7f\u7528devm_kzalloc()\u51fd\u6570\u6765\u5206\u914d\u5185\u5b58\u3002\u6211\u4eec\u63a5\u89e6\u6bd4\u8f83\u591a\u7684\u662fkmalloc()\u6216\u8005kzalloc()\u6765\u5206\u914d\u5185\u5b58\uff0c\u4f46\u662f\u8fd9\u4f1a\u5e26\u6765\u4e00\u4e9b\u6f5c\u5728\u7684\u95ee\u9898\u3002\u6bd4\u5982\u5728\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\u5982\u679c\u5931\u8d25\u4e86\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5f00\u53d1\u4eba\u5458\u5c0f\u5fc3\u5730\u91ca\u653e\u5185\u5b58\u3002\u800cdevm_kzalloc()\u51fd\u6570\u5219\u53ef\u4ee5\u81ea\u52a8\u91ca\u653e\u5185\u5b58\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u5f00\u53d1\u4eba\u5458\u62c5\u5fc3\u5185\u5b58\u91ca\u653e\u7684\u95ee\u9898\u4e86\u3002\u5176\u8bbe\u8ba1\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f\u8d44\u6e90\u7531\u8bbe\u5907\u7ba1\u7406\uff0c\u4e00\u65e6\u4e0d\u9700\u8981\u4e5f\u7531\u8bbe\u5907\u6765\u91ca\u653e\uff0c\u8fd9\u5176\u5b9e\u6709\u70b9C++\u4e2dRAII\u7684\u601d\u60f3\u3002

\u6ce8\u518c/\u6ce8\u9500\u5e73\u53f0\u9a71\u52a8\u7684\u51fd\u6570\u5982\u4e0b\uff1a

int platform_driver_register(struct platform_device *drv);\nvoid platform_driver_unregister(struct platform_device *drv);\n

\u5728\u5e73\u53f0\u8bbe\u5907\u4e2d\uff0cstruct resource\u7ed3\u6784\u4f53\u7528\u6765\u8868\u793a\u786c\u4ef6\u4fe1\u606f\uff0c\u800c\u8f6f\u4ef6\u4fe1\u606f\u5219\u53ef\u4ee5\u7528\u8bbe\u5907\u7ed3\u6784\u4f53device\u4e2d\u7684\u6210\u5458platform_data\u6765\u4fdd\u5b58\u3002

platform_get_resource()\u51fd\u6570\u901a\u5e38\u4f1a\u5728\u9a71\u52a8\u7684probe()\u51fd\u6570\u4e2d\u6267\u884c\uff0c\u7528\u4e8e\u83b7\u53d6platform_device\u4e2d\u7684resource\u7ed3\u6784\u4f53\uff1a

struct resource *platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);\n

dev\uff1a\u6307\u5b9a\u8981\u83b7\u53d6\u7684\u5e73\u53f0\u8bbe\u5907\u3002

type\uff1a\u6307\u5b9a\u83b7\u53d6\u8d44\u6e90\u7684\u7c7b\u578b\uff0c\u6bd4\u5982IORESOURCE_IO\u3002

num\uff1a\u6307\u5b9a\u8981\u83b7\u53d6\u7684\u8d44\u6e90\u7f16\u53f7\u3002

\u5982\u679c\u8d44\u6e90\u7c7b\u578b\u662fIORESOURCE_IRQ\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u63a5\u53e3\u8fd8\u83b7\u53d6\u4e2d\u65ad\u5f15\u811a\uff1a

int platform_get_irq(struct platform_device *pdev, unsigned int num);\n

\u5bf9\u4e8e\u5b58\u653e\u5728device\u7ed3\u6784\u4f53\u4e2dplatform_data\u7684\u8f6f\u4ef6\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528dev_get_platdata()\u51fd\u6570\u6765\u83b7\u53d6\uff1a

static inline void *dev_get_platdata(const struct device *dev)\n{\n    return dev->platform_data;\n}\n

\u603b\u7ed3\u4e00\u4e0b\u5e73\u53f0\u9a71\u52a8\uff1a\u9700\u8981\u624b\u52a8\u5b9e\u73b0probe()\u51fd\u6570\uff0c\u5f53\u5e73\u53f0\u603b\u7ebf\u6210\u529f\u5339\u914dplatform_device\u548cplatform_driver\u65f6\uff0c\u5219\u4f1a\u8c03\u7528\u9a71\u52a8\u7684probe()\u51fd\u6570\uff0c\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u7684\u51fd\u6570\u63a5\u53e3\u6765\u83b7\u53d6\u8d44\u6e90\uff0c\u4ee5\u521d\u59cb\u5316\u8bbe\u5907\uff0c\u6700\u540e\u586b\u5145\u7ed3\u6784\u4f53platform_driver\uff0c\u8c03\u7528platform_driver_register()\u8fdb\u884c\u6ce8\u518c\u3002

"},{"location":"linux/drivers/spi/","title":"SPI\u5b50\u7cfb\u7edf","text":""},{"location":"linux/drivers/uart/","title":"UART\u5b50\u7cfb\u7edf","text":""},{"location":"linux/ebpf/","title":"ebpf\u6280\u672f","text":"

\u5b89\u88c5bcc\u8f6f\u4ef6\u5305\uff1a

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4052245BD4284CDD\necho \"deb https://repo.iovisor.org/apt/xenial xenial main\" | sudo tee /etc/apt/sources.list.d/iovisor.list\nsudo apt-get update\nsudo apt-get install -y bcc-tools libbcc-examples linux-headers-$(uname -r)\n
"},{"location":"linux/kernel/","title":"\u5185\u6838\u57fa\u7840\u77e5\u8bc6","text":""},{"location":"linux/kernel/#_2","title":"\u5185\u6838\u6e90\u7801","text":"

\u8981\u83b7\u53d6\u5185\u6838\u6e90\u7801\uff0c\u8bf7\u4f7f\u7528git\u3002

git clone git://git.ernel.org/pub/scm/linux/kernel/git/torvalds/[linux\u7248\u672c\u53f7].git\n

\u5185\u6838\u6e90\u7801\u6811\u7531\u8bb8\u591a\u76ee\u5f55\u7ec4\u6210\uff0c\u4e00\u4e9b\u6bd4\u8f83\u91cd\u8981\u7684\u76ee\u5f55\u7684\u63cf\u8ff0\u5982\u4e0b\uff1a

\u76ee\u5f55\u540d \u63cf\u8ff0 arch \u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\u4ee3\u7801 block \u5757\u8bbe\u5907I/O\u5c42 drivers \u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f fs \u6587\u4ef6\u7cfb\u7edf include \u5185\u6838\u5934\u6587\u4ef6 init \u521d\u59cb\u5316\u4ee3\u7801 ipc \u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ee3\u7801 kernel \u5185\u6838\u6838\u5fc3\u4ee3\u7801 lib \u5185\u6838\u5e93\u51fd\u6570 mm \u5185\u5b58\u7ba1\u7406 net \u7f51\u7edc\u7cfb\u7edf

\u8981\u7f16\u8bd1\u5185\u6838\uff0c\u8bf7\u4f7f\u7528make menuconfig\u3002\u5185\u6838\u7684\u5404\u79cd\u914d\u7f6e\uff0c\u4ee5CONFIG_FEATURE\u7684\u5f62\u5f0f\u5199\u5165.config\u6587\u4ef6\u3002\u914d\u7f6e\u9009\u9879\u6709\u4e09\u79cd\uff1ayes\u3001no\u6216module\u3002\u5206\u522b\u5bf9\u5e94\u7f16\u8bd1\u3001\u4e0d\u7f16\u8bd1\u3001\u4ee5\u6a21\u5757\u5f62\u5f0f\u7f16\u8bd1\u3002

\u5185\u6838\u5f00\u53d1\u7684\u7279\u70b9\u5982\u4e0b\uff1a

  • \u4e0d\u80fd\u4f7f\u7528\u6807\u51c6C\u5e93\u5934\u6587\u4ef6\uff0c\u53ea\u80fd\u4f7f\u7528\u5185\u6838\u63d0\u4f9b\u7684\u5934\u6587\u4ef6\u3002\u4e0e\u4f53\u7cfb\u7ed3\u6784\u65e0\u5173\u7684\u5934\u6587\u4ef6\u4f4d\u4e8e\u5185\u6838\u6e90\u7801\u6839\u76ee\u5f55\u4e0b\u7684include\u76ee\u5f55\u3002\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\u5934\u6587\u4ef6\u4f4d\u4e8e<arch/architecture/include/asm>\u76ee\u5f55\uff0c\u5185\u6838\u4ee3\u7801\u4ee5asm\u524d\u7f00\u7684\u5f62\u5f0f\u5305\u542b\u8fd9\u4e9b\u5934\u6587\u4ef6\u3002

  • \u5fc5\u987b\u4f7f\u7528GNU C\u3002gcc\u7f16\u8bd1\u5668\u652f\u6301\u4ee5asm()\u6307\u4ee4\u5f00\u5934\u5d4c\u5165\u6c47\u7f16\u4ee3\u7801\u3002

  • \u6ca1\u6709\u5185\u5b58\u4fdd\u62a4\u673a\u5236\u3002\u5982\u679c\u662f\u5185\u6838\u8bbf\u95ee\u4e86\u975e\u6cd5\u5185\u5b58\uff0c\u540e\u679c\u4e0d\u582a\u8bbe\u60f3\u3002

  • \u65e0\u6cd5\u6267\u884c\u6d6e\u70b9\u8fd0\u7b97\u3002\u5185\u6838\u5bf9\u4e8e\u6d6e\u70b9\u8fd0\u7b97\u7684\u652f\u6301\u5ea6\u4e0d\u591f\u3002

  • \u5fc5\u987b\u8003\u8651\u540c\u6b65\u4e0e\u5e76\u53d1\u3002Linux\u5185\u6838\u662f\u62a2\u5360\u5f0f\u591a\u4efb\u52a1\u5904\u7406\u7cfb\u7edf\uff0c\u5f53\u524d\u4efb\u52a1\u968f\u65f6\u6709\u53ef\u80fd\u88ab\u53e6\u4e00\u4e2a\u4efb\u52a1\u62a2\u5360\u3002

  • \u5fc5\u987b\u8003\u8651\u53ef\u79fb\u690d\u6027\u3002\u6bd4\u5982\u4fdd\u6301\u5b57\u8282\u5e8f\u300164 \u4f4d\u5bf9\u9f50\u3001\u4e0d\u5047\u5b9a\u5b57\u957f\u548c\u9875\u9762\u957f\u5ea6\u7b49\u4e00\u7cfb\u5217\u51c6\u5219\u3002

"},{"location":"linux/kernel/block/","title":"\u5757I/O","text":"

\u7cfb\u7edf\u4e2d\u968f\u673a\u8bbf\u95ee\u56fa\u5b9a\u5927\u5c0f\u6570\u636e\u7247\u7684\u786c\u4ef6\u8bbe\u5907\u88ab\u79f0\u4e3a\u5757\u8bbe\u5907\uff0c\u8fd9\u4e9b\u56fa\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u7247\u5c31\u88ab\u79f0\u4e3a\u5757\uff0c\u6700\u5e38\u89c1\u7684\u5757\u8bbe\u5907\u662f\u786c\u76d8\u3002

\u53e6\u4e00\u79cd\u57fa\u672c\u7684\u8bbe\u5907\u7c7b\u578b\u662f\u5b57\u7b26\u8bbe\u5907\uff0c\u6bd4\u5982\u4e32\u53e3\u548c\u952e\u76d8\u3002\u5b83\u6309\u7167\u5b57\u7b26\u6d41\u7684\u65b9\u5f0f\u88ab\u6709\u5e8f\u8bbf\u95ee\uff0c\u968f\u673a\u7684\u8bbf\u95ee\u662f\u65e0\u610f\u4e49\u7684\u3002\u5f53\u5728\u952e\u76d8\u4e0a\u6253\u51fa\u201cchar\u201d\u8fd9\u4e2a\u5355\u8bcd\u65f6\uff0c\u5c4f\u5e55\u5fc5\u987b\u6309\u7167\u8f93\u5165\u987a\u5e8f\u663e\u793a\u51fa\u6765\u3002

\u76f8\u6bd4\u8f83\u4e4b\u4e0b\uff0c\u56e0\u4e3a\u5757\u8bbe\u5907\u652f\u6301\u968f\u673a\u7684I/O\uff0c\u6240\u4ee5\u5185\u6838\u5bf9\u4e8e\u5757\u8bbe\u5907\u7684\u7ba1\u7406\u6bd4\u5b57\u7b26\u8bbe\u5907\u8981\u590d\u6742\u8bb8\u591a\u3002\u66f4\u91cd\u8981\u7684\u662f\u5757\u8bbe\u5907\u5bf9\u4e8e\u6267\u884c\u6027\u80fd\u7684\u8981\u6c42\u5f88\u9ad8\uff0c\u5bf9\u4e8e\u786c\u76d8\u8bfb\u5199\u7684\u4f18\u5316\u53ef\u4ee5\u5e26\u6765\u6574\u4e2a\u7cfb\u7edf\u6027\u80fd\u7684\u63d0\u5347\u3002

"},{"location":"linux/kernel/block/#_1","title":"\u78c1\u76d8","text":"

\u78c1\u76d8\u662f\u5757\u8bbe\u5907\u7684\u4e00\u79cd\uff0c\u5e38\u89c1\u7684\u78c1\u76d8\u6709\u673a\u68b0\u78c1\u76d8\u548c\u56fa\u6001\u78c1\u76d8\uff0c\u5176\u4e2d\u673a\u68b0\u78c1\u76d8\u7531\u4e8e\u9700\u8981\u78c1\u9053\u5bfb\u5740\uff0c\u6240\u4ee5\u901f\u5ea6\u6bd4\u56fa\u6001\u78c1\u76d8\u8981\u6162\u5f88\u591a\u3002\u78c1\u76d8\u7684\u8bfb\u5199\u6709\u4e24\u79cd\u65b9\u5f0f\u2014\u2014\u968f\u673aI/O\u548c\u8fde\u7eedI/O\uff1a\u968f\u673aI/O\u662f\u6307\u6bcf\u6b21\u8bfb\u5199\u64cd\u4f5c\u90fd\u4ece\u78c1\u76d8\u7684\u67d0\u4e2a\u4f4d\u7f6e\u91cd\u65b0\u5f00\u59cb\uff0c\u800c\u8fde\u7eedI/O\u662f\u6307\u6bcf\u6b21\u8bfb\u5199\u64cd\u4f5c\u90fd\u4ece\u540c\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u9884\u8bfb\u7684\u65b9\u5f0f\uff0c\u51cf\u5c11I/O\u8bf7\u6c42\u7684\u6b21\u6570\u3002

\u78c1\u76d8\u8bfb\u5199\u7684\u6700\u5c0f\u5355\u4f4d\u662f\u6247\u533a\uff0c\u4f46\u662f\u6247\u533a\u53ea\u6709512B\u5927\u5c0f\u3002\u4e3a\u4e86\u63d0\u9ad8\u8bfb\u5199\u7684\u6548\u7387\uff0c\u6587\u4ef6\u7cfb\u7edf\u5c06\u8fde\u7eed\u7684\u6247\u533a\u7ec4\u5408\u6210\u903b\u8f91\u5757\uff0c\u6bcf\u4e2a\u903b\u8f91\u5757\u7684\u5927\u5c0f\u6700\u5e38\u89c1\u7684\u662f4KB\u2014\u2014\u75318\u4e2a\u6247\u533a\u7ec4\u6210\u3002

\u76ee\u5f55\u9879\u3001\u7d22\u5f15\u8282\u70b9\u4ee5\u53ca\u78c1\u76d8\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

"},{"location":"linux/kernel/block/#_2","title":"\u901a\u7528\u5757\u5c42","text":"

\u4e3a\u4e86\u5c4f\u853d\u4e0d\u540c\u5757\u8bbe\u5907\u5e26\u6765\u7684\u5dee\u5f02\uff0c\u5185\u6838\u5f15\u5165\u4e86\u901a\u7528\u5757\u5c42\uff0c\u6765\u7ba1\u7406\u4e0d\u540c\u7684\u5757\u8bbe\u5907\u3002

\u901a\u7528\u5757\u66fe\u662f\u6587\u4ef6\u7cfb\u7edf\u548c\u78c1\u76d8\u9a71\u52a8\u4e2d\u95f4\u7684\u4e00\u4e2a\u5757\u8bbe\u5907\u62bd\u8c61\u5c42\uff0c\u5b83\u6709\u4e24\u4e2a\u529f\u80fd\uff1a

  1. \u5411\u4e0a\uff0c\u4e3a\u6587\u4ef6\u7cfb\u7edf\u548c\u5e94\u7528\u7a0b\u5e8f\uff0c\u63d0\u4f9b\u8bbf\u95ee\u5757\u8bbe\u5907\u7684\u6807\u51c6\u63a5\u53e3\uff1b\u5411\u4e0b\uff0c\u628a\u4e0d\u540c\u7684\u5757\u8bbe\u5907\u62bd\u8c61\u4e3a\u7edf\u4e00\u4e2a\u5757\u8bbe\u5907\u3002
  2. \u7ba1\u7406\u5757\u8bbe\u5907\u7684I/O\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7\u91cd\u65b0\u6392\u5e8f\u3001\u8bf7\u6c42\u5408\u5e76\u7b49\u65b9\u5f0f\uff0c\u4f18\u5316\u8bfb\u5199\u78c1\u76d8\u7684\u6548\u7387\u3002

\u5bf9I/O\u8bf7\u6c42\u6392\u5e8f\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u6240\u8c13\u7684I/O\u8c03\u5ea6\u3002\u5185\u6838\u652f\u6301\u56db\u79cd\u8c03\u5ea6\u7b97\u6cd5\uff0c\u5206\u522b\u662fNONE\u3001NOOP\u3001CFQ\u4ee5\u53caDeadlLine\u3002

  • NONE\uff1a\u4e0d\u505a\u4efb\u4f55\u5904\u7406\uff0c\u5e38\u7528\u5728\u865a\u62df\u673a\u4e2d\u3002
  • NOOP\uff1a\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5148\u5165\u5148\u51fa\u7684\u961f\u5217\uff0c\u5236\u4f5c\u4e00\u4e9b\u6700\u57fa\u672c\u7684\u8bf7\u6c42\u5408\u5e76\uff0c\u5e38\u7528\u4e8eSSD\u78c1\u76d8\u3002
  • CFQ\uff1a\u9ed8\u8ba4I/O\u8c03\u5ea6\u5668\uff0c\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u7ef4\u62a4\u4e86\u4e00\u4e2aI/O\u8c03\u5ea6\u961f\u5217\uff0c\u5e76\u6309\u7167\u65f6\u95f4\u7247\u6765\u5747\u5300\u5206\u5e03\u6bcf\u4e2a\u8fdb\u7a0b\u7684I/O\u8bf7\u6c42\u3002\u9002\u7528\u4e8e\u8fd0\u884c\u5927\u91cf\u8fdb\u7a0b\u7684\u7cfb\u7edf\u3002
  • DeadLine\uff1a\u4e3a\u8bfb\u3001\u5199\u8bf7\u6c42\u521b\u5efa\u4e0d\u540c\u7684I/O\u961f\u5217\uff0c\u786e\u4fdd\u8fbe\u5230deadline\u7684\u8bf7\u6c42\u88ab\u4f18\u5148\u5904\u7406\u3002\u5e38\u7528\u4e8e\u6570\u636e\u5e93\u4e2d\u3002
"},{"location":"linux/kernel/interrupt/","title":"\u4e2d\u65ad\u548c\u4e2d\u65ad\u5904\u7406","text":"

\u4e2d\u65ad\u662f\u4e00\u79cd\u4e8b\u4ef6\uff0c\u8be5\u4e8b\u4ef6\u6539\u53d8\u5904\u7406\u5668\u6267\u884c\u6307\u4ee4\u7684\u987a\u5e8f\u3002\u8fd9\u79cd\u65b9\u5f0f\u6709\u540c\u6b65\u548c\u5f02\u6b65\u4e4b\u5206\u3002\u540c\u6b65\u4e2d\u65ad\u4e5f\u88ab\u79f0\u4e3a\u5f02\u5e38\u3002\u5728\u5904\u7406\u5668\u6267\u884c\u5230\u7531\u4e8e\u7f16\u7a0b\u5931\u8bef\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u6307\u4ee4\uff08\u6bd4\u5982\u9664\u4ee50\uff09\u7684\u65f6\u5019\uff0c\u6216\u8005\u662f\u5728\u6267\u884c\u671f\u95f4\u51fa\u73b0\u7279\u6b8a\u60c5\u51b5\uff08\u6bd4\u5982\u7f3a\u9875\uff09\uff0c\u5fc5\u987b\u9760\u5185\u6838\u5904\u7406\u7684\u65f6\u5019\uff0c\u5904\u7406\u5668\u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5f02\u5e38\u3002\u5f02\u6b65\u4e2d\u65ad\u5219\u662f\u7531\u786c\u4ef6\u5f15\u8d77\u7684\u3002

\u4e2d\u65ad\u53c8\u53ef\u4ee5\u5206\u4e3a\u53ef\u5c4f\u853d\u4e2d\u65ad\u548c\u4e0d\u53ef\u5c4f\u853d\u4e2d\u65ad\u3002\u901a\u5e38\uff0cI/O\u8bbe\u5907\u53d1\u51fa\u7684\u6240\u6709\u4e2d\u65ad\u8bf7\u6c42\u90fd\u662f\u53ef\u5c4f\u853d\u7684\uff0c\u800c\u90e8\u5206\u5371\u6025\u4e8b\u4ef6\uff08\u6bd4\u5982\u786c\u4ef6\u6545\u969c\uff09\u662f\u4e0d\u53ef\u5c4f\u853d\u7684\u3002\u6bcf\u4e2a\u4e2d\u65ad\u548c\u5f02\u5e38\u90fd\u662f\u75310 ~ 255\u4e4b\u95f4\u7684\u4e00\u4e2a\u6570\u6765\u6807\u8bc6\uff0c\u8fd9\u4e2a\u6570\u88ab\u79f0\u4f5c\u4e3a\u662f\u5411\u91cf\uff08vector\uff09\u3002\u4e0d\u53ef\u5c4f\u853d\u4e2d\u65ad\u7684\u5411\u91cf\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u53ef\u5c4f\u853d\u4e2d\u65ad\u7684\u5411\u91cf\u53ef\u4ee5\u901a\u8fc7\u5bf9\u4e2d\u65ad\u63a7\u5236\u5668\u7684\u7f16\u7a0b\u6765\u6539\u53d8\u3002

\u663e\u7136\uff0c\u4e2d\u65ad\u4fe1\u53f7\u63d0\u4f9b\u4e86\u4e00\u79cd\u7279\u6b8a\u7684\u65b9\u5f0f\uff0c\u8ba9\u5904\u7406\u5668\u53bb\u6267\u884c\u6b63\u5e38\u8fd0\u884c\u4ee3\u7801\u4e4b\u5916\u7684\u903b\u8f91\u3002\u5f53\u4e00\u4e2a\u4e2d\u65ad\u4fe1\u53f7\u5230\u6765\u65f6\uff0cCPU\u5fc5\u987b\u505c\u4e0b\u5b83\u5f53\u524d\u6b63\u5728\u505a\u7684\u4e8b\u60c5\uff0c\u8f6c\u800c\u53bb\u5904\u7406\u4e2d\u65ad\u4ee3\u7801\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u5c31\u9700\u8981\u5728\u5185\u6838\u6001\u5806\u6808\u4fdd\u5b58\u5f53\u524d\u4e0a\u4e0b\u6587\u73af\u5883\uff0c\u5e76\u4e14\u628a\u89e6\u53d1\u4e86\u4f55\u79cd\u4e2d\u65ad\u4e5f\u653e\u8fdb\u5bc4\u5b58\u5668\u4e2d\u3002

\u4f60\u53ef\u80fd\u4f1a\u628a\u4e2d\u65ad\u5904\u7406\u4e0e\u8fdb\u7a0b\u5207\u6362\u8054\u7cfb\u8d77\u6765\u3002\u786e\u5b9e\u8fd9\u4e24\u8005\u6709\u90e8\u5206\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46\u662f\u4e2d\u65ad\u6267\u884c\u7684\u4ee3\u7801\u4e0d\u662f\u8fdb\u7a0b\u73af\u5883\u3002\u5b83\u662f\u4e00\u4e2a\u5185\u6838\u63a7\u5236\u7684\u7279\u6b8a\u8def\u5f84\uff0c\u5fc5\u987b\u5feb\u901f\u5904\u7406\uff0c\u4e0d\u80fd\u505c\u7559\u3002\u7531\u4e8e\u4e2d\u65ad\u5904\u7406\u662f\u5185\u6838\u6700\u4e3a\u654f\u611f\u7684\u4efb\u52a1\u4e4b\u4e00\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u5b83\u5fc5\u987b\u6709\u4ee5\u4e0b\u7ea6\u675f\uff1a

  • \u4e2d\u65ad\u5fc5\u987b\u5c3d\u53ef\u80fd\u5feb\u5730\u5904\u7406\u3002\u4e3a\u6b64\uff0c\u4e2d\u65ad\u5904\u7406\u901a\u5e38\u5206\u4e3a\u4e24\u90e8\u5206\uff1a\u5173\u952e\u800c\u7d27\u6025\u7684\u90e8\u5206\uff0c\u5185\u6838\u7acb\u5373\u6267\u884c\uff1b\u5176\u4f59\u90e8\u5206\u63a8\u8fdf\u6267\u884c\u3002

  • \u4e2d\u65ad\u5fc5\u987b\u53ef\u4ee5\u5d4c\u5957\u6267\u884c\u3002\u7531\u4e8e\u4e2d\u65ad\u968f\u65f6\u4f1a\u5230\u6765\uff0c\u5728\u5185\u6838\u5904\u7406\u5176\u4e2d\u4e00\u4e2a\u4e2d\u65ad\u65f6\uff0c\u53e6\u4e00\u4e2a\u4e2d\u65ad\u4e5f\u4f1a\u53d1\u751f\u3002\u5f53\u4e2d\u65ad\u5904\u7406\u5b8c\u6210\u540e\uff0c\u5185\u6838\u5fc5\u987b\u80fd\u6062\u590d\u88ab\u4e2d\u65ad\u8fdb\u7a0b\u7684\u6267\u884c\u3002

  • \u5728\u4e34\u754c\u533a\u4e2d\uff0c\u4e2d\u65ad\u5fc5\u987b\u88ab\u7981\u6b62\u3002

"},{"location":"linux/kernel/interrupt/#_2","title":"\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f","text":"

\u5728\u54cd\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u4e2d\u65ad\u65f6\uff0c\u5185\u6838\u4f1a\u6267\u884c\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53eb\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff08interrupt handler\uff09\u6216\u4e2d\u65ad\u670d\u52a1\u4f8b\u7a0b\uff08interrupt service routine, ISR\uff09\u3002\u4ea7\u751f\u4e2d\u65ad\u7684\u6bcf\u4e2a\u8bbe\u5907\u90fd\u6709\u4e00\u4e2a\u54cd\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u4e00\u4e2a\u8bbe\u5907\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u662f\u5b83\u9a71\u52a8\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u3002

\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5904\u4e8e\u7279\u6b8a\u7684\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e2d\u3002\u5728\u89e3\u91ca\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u56de\u5fc6\u4e00\u4e0b\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u3002\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u65f6\u4e00\u79cd\u5185\u6838\u6240\u5904\u7684\u64cd\u4f5c\u6a21\u5f0f\uff0c\u6b64\u65f6\u8fdb\u7a0b\u4ee3\u8868\u8fdb\u7a0b\u6267\u884c\u3002\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528current\u5b8f\u5173\u8054\u5f53\u524d\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u7761\u7720\uff0c\u4e5f\u53ef\u4ee5\u8c03\u7528\u8c03\u5ea6\u7a0b\u5e8f\u3002\u4e0e\u4e4b\u76f8\u53cd\uff0c\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e0e\u8fdb\u7a0b\u6ca1\u6709\u4ec0\u4e48\u5173\u8054\u3002\u56e0\u4e3a\u4e2d\u65ad\u7684\u89e6\u53d1\u662f\u968f\u65f6\u5230\u6765\u7684\uff0c\u5e76\u4e0d\u77e5\u9053\u5f53\u524d\u5904\u4e8e\u54ea\u4e2a\u8fdb\u7a0b\u3002\u56e0\u4e3a\u6ca1\u6709\u540e\u5907\u8fdb\u7a0b\uff0c\u6240\u4ee5\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e0d\u53ef\u7761\u7720\uff0c\u4e5f\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55\u53ef\u4ee5\u5f15\u8d77\u7761\u7720\u7684\u51fd\u6570\u3002\u4e2d\u65ad\u4e0a\u4e0b\u6587\u7684\u4ee3\u7801\u5fc5\u987b\u5feb\u901f\u4e14\u7b80\u6d01\u3002

\u4e2d\u65ad\u968f\u65f6\u6709\u53ef\u80fd\u53d1\u751f\uff0c\u56e0\u6b64\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5fc5\u987b\u5feb\u901f\u6267\u884c\u3002\u4f46\u662f\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5f80\u5f80\u8fd8\u8981\u5b8c\u6210\u5927\u91cf\u5176\u4ed6\u7684\u5de5\u4f5c\u3002\u6bd4\u5982\u4e00\u4e2a\u7f51\u7edc\u8bbe\u5907\uff0c\u9664\u4e86\u5bf9\u786c\u4ef6\u5e94\u7b54\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u628a\u6765\u81ea\u786c\u4ef6\u7684\u7f51\u7edc\u6570\u636e\u62f7\u8d1d\u5230\u5185\u5b58\u3002\u663e\u7136\u8fd9\u79cd\u5de5\u4f5c\u91cf\u4e0d\u4f1a\u5c0f\u3002

\u53c8\u60f3\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u5f97\u5feb\uff0c\u53c8\u8981\u5176\u5b8c\u6210\u7684\u5de5\u4f5c\u591a\u3002\u9274\u4e8e\u8fd9\u4e24\u8005\u4e4b\u95f4\u7684\u77db\u76fe\u5173\u7cfb\uff0c\u6211\u4eec\u4e00\u822c\u628a\u4e2d\u65ad\u5904\u7406\u7684\u8fc7\u7a0b\u5207\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff1a\u4e0a\u534a\u90e8\u548c\u4e0b\u534a\u90e8\u3002\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u662f\u4e0a\u534a\u90e8\u2014\u2014\u5f53\u63a5\u6536\u5230\u4e00\u4e2a\u4e2d\u65ad\u540e\uff0c\u5b83\u7acb\u523b\u88ab\u6267\u884c\uff0c\u4f46\u53ea\u505a\u6709\u4e25\u683c\u65f6\u9650\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\u5bf9\u63a5\u6536\u5230\u7684\u4e2d\u65ad\u8fdb\u884c\u5e94\u7b54\u3002\u800c\u7a0d\u540e\u9700\u8981\u5b8c\u6210\u7684\u5de5\u4f5c\u5219\u4f1a\u88ab\u63a8\u8fdf\u5230\u4e0b\u534a\u90e8\u53bb\uff0c\u5728\u5408\u9002\u7684\u65f6\u673a\u6267\u884c\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0b\u534a\u90e8\u7684\u673a\u5236\uff0c\u7a0d\u540e\u4f1a\u505a\u8bf4\u660e\u3002

\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7request_irq()\u51fd\u6570\u6ce8\u518c\u4e00\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u5e76\u4e14\u6fc0\u6d3b\u7ed9\u5b9a\u7684\u4e2d\u65ad\u7ebf\uff1a

int request_irq(unsigned int irq,\n                irq_handler_t handler,\n                unsigned long flags,\n                const char *name,\n                void *dev)\n

\u7b2c\u4e00\u4e2a\u53c2\u6570irq\u8868\u793a\u8981\u5206\u914d\u7684\u4e2d\u65ad\u53f7\u3002

\u7b2c\u4e8c\u4e2a\u53c2\u6570handler\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u6307\u5411\u5904\u7406\u8fd9\u4e2a\u4e2d\u65ad\u7684\u5b9e\u9645\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002

typedef irqreturn_t (*irq_handler_t)(int, void*);\n

\u7b2c\u4e09\u4e2a\u53c2\u6570flags\u53ef\u4ee5\u4e3a0\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e0b\u5217\u4e00\u4e2a\u6216\u591a\u4e2a\u6807\u5fd7\u7684\u4f4d\u63a9\u7801\uff1a

  • IRQF_DISABLED\u2014\u2014\u7981\u6b62\u6240\u6709\u5176\u4ed6\u4e2d\u65ad\uff0c\u65b0\u5185\u6838\u5df2\u79fb\u9664\u3002
  • IRQF_TIMER\u2014\u2014\u7cfb\u7edf\u5b9a\u65f6\u5668\u6807\u5fd7
  • IRQF_SHARED\u2014\u2014\u5728\u591a\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\u4e2d\u65ad\u7ebf\u3002\u5982\u679c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5728\u540c\u4e00\u6761\u7ebf\u4e0a\u6ce8\u518c\uff0c\u90a3\u4e48\u5fc5\u987b\u6307\u5b9a\u8fd9\u4e2a\u6807\u5fd7\u3002

\u7b2c\u56db\u4e2a\u53c2\u6570name\u662f\u4e0e\u4e2d\u65ad\u76f8\u5173\u7684\u8bbe\u5907\u7684ASCII\u6587\u672c\u8868\u793a\u3002\u8fd9\u4e9b\u540d\u5b57\u4f1a\u88ab/proc/irq\u548c/proc/interrupts\u6587\u4ef6\u4f7f\u7528\u3002

\u7b2c\u4e94\u4e2a\u53c2\u6570dev\u7528\u4e8e\u786e\u5b9a\u5171\u4eab\u4e2d\u65ad\u7ebf\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002\u5f53\u591a\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5171\u4eab\u67d0\u6761\u4e2d\u65ad\u7ebf\uff0c\u5176\u4e2d\u67d0\u4e2a\u9700\u8981\u91ca\u653e\u65f6\uff0cdev\u5c06\u63d0\u4f9b\u552f\u4e00\u7684\u6807\u5fd7\u4fe1\u606f\u4ee5\u4fbf\u786e\u5b9a\u662f\u54ea\u4e2a\u7a0b\u5e8f\u9700\u8981\u5220\u9664\u3002

request_irq()\u6210\u529f\u8fd4\u56de0\uff0c\u9519\u8bef\u8fd4\u56de\u975e0\u3002\u6700\u5e38\u89c1\u7684\u9519\u8bef\u662f-EBUSY\uff0c\u8868\u793a\u7ed9\u5b9a\u7684\u4e2d\u65ad\u7ebf\u5df2\u7ecf\u88ab\u4f7f\u7528\u3002

request_irq()\u53ef\u80fd\u4f1a\u7761\u7720\uff0c\u56e0\u6b64\u4e0d\u80fd\u5728\u4e0d\u5141\u8bb8\u963b\u585e\u4ee3\u7801\u4e2d\u8c03\u7528\u8be5\u51fd\u6570\u3002

\u5378\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u65f6\uff0c\u9700\u8981\u6ce8\u9500\u54cd\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u5e76\u91ca\u653e\u4e2d\u65ad\u7ebf\uff1a

void free_irq(unsigned int irq, void *dev)\n

\u5982\u679c\u6307\u5b9a\u7684\u4e2d\u65ad\u7ebf\u4e0d\u662f\u5171\u4eab\u7684\uff0c\u90a3\u4e48\u8be5\u51fd\u6570\u5220\u9664\u5904\u7406\u7a0b\u5e8f\u7684\u540c\u65f6\u5c06\u7981\u7528\u8fd9\u6761\u4e2d\u65ad\u7ebf\u3002\u5982\u679c\u662f\u5171\u4eab\u7684\uff0c\u90a3\u4e48\u4ec5\u5220\u9664\u6240\u5bf9\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u800c\u8fd9\u6761\u4e2d\u65ad\u7ebf\u53ea\u6709\u5728\u5220\u9664\u4e86\u6700\u540e\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u65f6\u624d\u4f1a\u88ab\u7981\u7528\u3002

\u8981\u7f16\u5199\u81ea\u5df1\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u9700\u8981\u7528\u5230\u4ee5\u4e0b\u51fd\u6570\uff1a

static irqreturn_t intr_handler(int irq, void *dev)\n

\u53c2\u6570irq\u5df2\u7ecf\u6ca1\u6709\u4ec0\u4e48\u7528\u4e86\uff0c\u53c2\u6570dev\u7528\u6765\u5411\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4f20\u9012\u6570\u636e\u7ed3\u6784\u3002

Linux\u4e0a\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u65e0\u9700\u8981\u6c42\u53ef\u91cd\u5165\u3002\u56e0\u4e3a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u65f6\u76f8\u5e94\u4e2d\u65ad\u7ebf\u4e0a\u7684\u4e2d\u65ad\u662f\u5c4f\u853d\u7684\uff0c\u4e5f\u5c31\u662f\u4e0d\u4f1a\u53d1\u751f\u76f8\u540c\u4e2d\u65ad\u7684\u5d4c\u5957\u3002\u4f46\u4e0d\u540c\u4e2d\u65ad\u7684\u5d4c\u5957\u6709\u53ef\u80fd\u53d1\u751f\u3002

\u5bf9\u4e8e\u5171\u4eab\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u9664\u4e86\u8981\u6307\u5b9arequest_irq()\u51fd\u6570\u4e2d\u7684\u53c2\u6570flags\u4e3aIRQF_SHARED\u6807\u5fd7\uff0c\u8fd8\u9700\u8981\u4fdd\u8bc1\u53c2\u6570dev\u7684\u552f\u4e00\u6027\u3002\u5f53\u5185\u6838\u63a5\u6536\u4e00\u4e2a\u4e2d\u65ad\u540e\uff0c\u5b83\u5c06\u4f9d\u6b21\u8c03\u7528\u5728\u8be5\u4e2d\u65ad\u7ebf\u4e0a\u6ce8\u518c\u7684\u6bcf\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u3002\u56e0\u6b64\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u5fc5\u987b\u77e5\u9053\u5b83\u662f\u5426\u8be5\u4e3a\u8fd9\u4e2a\u4e2d\u65ad\u8d1f\u8d23\uff0c\u5982\u679c\u4e0d\u662f\u5c31\u8981\u7acb\u5373\u9000\u51fa\u3002

"},{"location":"linux/kernel/interrupt/#_3","title":"\u4e2d\u65ad\u5904\u7406\u673a\u5236","text":"

\u4e2d\u65ad\u5904\u7406\u7cfb\u7edf\u5728Linux\u4e2d\u7684\u5b9e\u73b0\u4f9d\u8d56\u4e8e\u4f53\u7cfb\u7ed3\u6784\u3002\u4e0a\u56fe\u662f\u4e2d\u65ad\u4ece\u786c\u4ef6\u5230\u5185\u6838\u6267\u884c\u7684\u8fc7\u7a0b\u3002\u5f53\u4e00\u4e2a\u8bbe\u5907\u4ea7\u751f\u4e2d\u65ad\u540e\uff0c\u901a\u8fc7\u603b\u7ebf\u628a\u7535\u4fe1\u53f7\u53d1\u9001\u7ed9\u4e2d\u65ad\u63a7\u5236\u5668\u3002\u5982\u679c\u4e2d\u65ad\u7ebf\u662f\u6fc0\u6d3b\u7684\uff0c\u90a3\u4e48\u4e2d\u65ad\u63a7\u5236\u5668\u5c31\u4f1a\u628a\u4e2d\u65ad\u53d1\u9001\u7ed9\u5904\u7406\u5668\u3002\u5728\u5927\u591a\u6570\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u5c31\u662f\u901a\u8fc7\u7535\u4fe1\u53f7\u7ed9\u5904\u7406\u5668\u7684\u7279\u5b9a\u7ba1\u811a\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u3002\u9664\u975e\u5904\u7406\u5668\u7981\u6b62\u4e86\u8be5\u4e2d\u65ad\uff0c\u5426\u5219\u5b83\u4f1a\u7acb\u523b\u505c\u4e0b\u5f53\u524d\u6267\u884c\u7684\u4ee3\u7801\uff0c\u5173\u95ed\u4e2d\u65ad\u7cfb\u7edf\uff0c\u7136\u540e\u8df3\u5230\u5185\u5b58\u4e2d\u9884\u5b9a\u4e49\u7684\u4f4d\u7f6e\u5f00\u59cb\u6267\u884c\u90a3\u91cc\u7684\u4ee3\u7801\u3002\u8fd9\u4e2a\u9884\u5b9a\u4e49\u7684\u4f4d\u7f6e\u7531\u5185\u6838\u8bbe\u7f6e\uff0c\u662f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002

\u5bf9\u4e8e\u6bcf\u6761\u4e2d\u65ad\u7ebf\uff0c\u5904\u7406\u5668\u90fd\u4f1a\u8df3\u5230\u5bf9\u5e94\u7684\u4e00\u4e2a\u552f\u4e00\u7684\u4f4d\u7f6e\u3002\u8fd9\u6837\uff0c\u5185\u6838\u5c31\u53ef\u4ee5\u77e5\u9053\u6240\u63a5\u6536\u4e2d\u65ad\u7684IRQ\u53f7\u4e86\u3002\u521d\u59cb\u5165\u53e3\u70b9\u5728\u6808\u4e2d\u4fdd\u5b58IRQ\u53f7\uff0c\u5e76\u5b58\u653e\u5f53\u524d\u5bc4\u5b58\u5668\u7684\u503c\uff08\u8fd9\u4e9b\u503c\u5c5e\u4e8e\u88ab\u4e2d\u65ad\u7684\u4efb\u52a1\uff09\u3002\u7136\u540e\uff0c\u7531\u5185\u6838\u8c03\u7528do_IRQ()\u51fd\u6570\uff1a

unsigned int do_IRQ(struct pt_regs *regs)\n

\u7531\u4e8e\u521d\u59cb\u5165\u53e3\u70b9\u5df2\u7ecf\u5c06IRQ\u53f7\u548c\u5bc4\u5b58\u5668\u7684\u503c\u4fdd\u5b58\u5728\u6808\u4e2d\u4e86\uff0c\u6240\u4ee5\u51fd\u6570do_IRQ()\u53ef\u4ee5\u76f4\u63a5\u63d0\u53d6\u6808\u4e2d\u7684\u53c2\u6570\u3002

do_IRQ()\u6267\u884c\u5b8c\u4e2d\u65ad\u670d\u52a1\u5668\u7a0b\u5e8f\u540e\uff0c\u4f1a\u68c0\u67e5\u662f\u5426\u9700\u8981\u91cd\u65b0\u8c03\u5ea6\uff08need_resched\u6807\u5fd7\u4f4d\uff09\uff0c\u7136\u540e\u5224\u65ad\u8fd4\u56de\u54ea\u4e2a\u7a7a\u95f4\uff1a

  • \u8fd4\u56de\u7528\u6237\u7a7a\u95f4\uff1a\u8c03\u7528schedule()\u51fd\u6570\u3002

  • \u8fd4\u56de\u5185\u6838\u7a7a\u95f4\uff1a\u5982\u679cpreempt_count\u6807\u5fd7\u4e3a0\uff0c\u8c03\u7528schedule()\u51fd\u6570\uff0c\u5426\u5219\u4e0d\u4f1a\u89e6\u53d1\u8c03\u5ea6\u3002

procfs\u662f\u4e00\u4e2a\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb/proc/interrupts\u6587\u4ef6\u8bfb\u53d6\u7cfb\u7edf\u4e2d\u4e0e\u4e2d\u65ad\u76f8\u5173\u7684\u7edf\u8ba1\u4fe1\u606f\u3002

"},{"location":"linux/kernel/interrupt/#_4","title":"\u4e2d\u65ad\u63a7\u5236","text":"

Linux\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7ec4\u63a5\u53e3\u7528\u4e8e\u64cd\u4f5c\u673a\u5668\u4e0a\u7684\u4e2d\u65ad\u72b6\u6001\u3002\u8fd9\u4e9b\u63a5\u53e3\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u80fd\u591f\u7981\u6b62\u5f53\u524d\u5904\u7406\u5668\u7684\u4e2d\u65ad\u7cfb\u7edf\uff0c\u6216\u5c4f\u853d\u6389\u6574\u4e2a\u673a\u5668\u7684\u4e00\u6761\u4e2d\u65ad\u7ebf\u7684\u80fd\u529b\u3002\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u662f\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u63a7\u5236\u4e2d\u65ad\u7cfb\u7edf\u7684\u539f\u56e0\u5f52\u6839\u5230\u5e95\u662f\u9700\u8981\u540c\u6b65\u3002\u901a\u8fc7\u7981\u6b62\u4e2d\u65ad\uff0c\u53ef\u4ee5\u786e\u4fdd\u67d0\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e0d\u4f1a\u88ab\u62a2\u5360\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7981\u6b62\u5185\u6838\u62a2\u5360\u3002\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u7f16\u5199\u9a71\u52a8\u7a0b\u5e8f\u65f6\uff0c\u5e94\u8be5\u5c3d\u91cf\u51cf\u5c11\u4e2d\u65ad\uff0c\u540c\u65f6\u4e0d\u8981\u628a\u8fd9\u79cd\u6280\u672f\u5f53\u505a\u4e92\u65a5\u673a\u5236\u6765\u4f7f\u7528\u3002

\u7981\u6b62\u548c\u6fc0\u6d3b\u672c\u5730\u4e2d\u65ad\u7684\u51fd\u6570\u4e3a\uff1a

local_irq_disable();\n\nlocal_irq_enable();\n

\u5982\u679c\u5728\u8c03\u7528local_irq_disable()\u51fd\u6570\u524d\u5df2\u7ecf\u7981\u6b62\u4e86\u4e2d\u65ad\uff0c\u90a3\u4e48\u8be5\u64cd\u4f5c\u4f1a\u5e26\u6765\u6f5c\u5728\u7684\u5371\u9669\u3002\u540c\u6837\uff0clocal_irq_enable()\u51fd\u6570\u4e5f\u5b58\u5728\u5371\u9669\uff0c\u56e0\u4e3a\u5b83\u4f1a\u65e0\u6761\u4ef6\u6fc0\u6d3b\u4e2d\u65ad\u3002\u5f53\u5185\u6838\u4ee3\u7801\u53d8\u5f97\u590d\u6742\u65f6\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u5f53\u524d\u5904\u7406\u5668\u4e2d\u65ad\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e00\u79cd\u673a\u5236\uff0c\u4ec5\u4ec5\u65f6\u628a\u4e2d\u65ad\u6062\u590d\u5230\u4ee5\u524d\u7684\u72b6\u6001\u800c\u4e0d\u662f\u7b80\u5355\u5730\u7981\u6b62\u6216\u8005\u662f\u6fc0\u6d3b\u3002

unsigned long flags;\nlocal_irq_save(flags);\nlocal_irq_restore(flags);\n

\u5728\u524d\u9762\u7684\u5185\u5bb9\u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u4e86\u7981\u6b62\u6574\u4e2a\u5904\u7406\u5668\u4e0a\u6240\u6709\u4e2d\u65ad\u7684\u51fd\u6570\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u7981\u6b62\u7279\u5b9a\u7684\u4e2d\u65ad\u7ebf\u3002\u76f8\u5173\u51fd\u6570\u5982\u4e0b\uff1a

void disable_irq(unsigned int irq);\nvoid disable_irq_nosync(unsigned int irq);\nvoid enable_irq(unsigned int irq);\nvoid synchronize_irq(unsigned int irq);\n

\u7531\u4e8e\u73b0\u4ee3\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5f80\u5f80\u5171\u4eab\u67d0\u4e2a\u4e2d\u65ad\u7ebf\uff0c\u7981\u6b62\u4e86\u67d0\u6761\u4e2d\u65ad\u7ebf\u4e5f\u5c31\u7981\u6b62\u4e86\u8fd9\u6761\u7ebf\u4e0a\u6240\u6709\u8bbe\u5907\u7684\u4e2d\u65ad\u4f20\u9012\u3002\u56e0\u6b64\u8fd9\u4e9b\u63a5\u53e3\u51fd\u6570\u4e0d\u5e94\u8be5\u88ab\u4f7f\u7528\u3002

"},{"location":"linux/kernel/interrupt/#_5","title":"\u4e0b\u534a\u90e8\u673a\u5236","text":"

\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u6709\u4e00\u4e2a\u5feb\u901f\u3001\u5f02\u6b65\u4e14\u7b80\u5355\u7684\u673a\u5236\u8d1f\u8d23\u5bf9\u786c\u4ef6\u8fc5\u901f\u505a\u51fa\u76f8\u5e94\u5e76\u5b8c\u6210\u90a3\u4e9b\u65f6\u95f4\u8981\u6c42\u5f88\u4e25\u683c\u7684\u64cd\u4f5c\u3002\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5f88\u9002\u5408\u4e8e\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002\u800c\u5bf9\u4e8e\u90a3\u4e9b\u65f6\u95f4\u8981\u6c42\u76f8\u5bf9\u5bbd\u677e\u7684\u4efb\u52a1\uff0c\u5e94\u5f53\u653e\u5230\u4e0b\u534a\u90e8\u53bb\u5904\u7406\u3002

\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u8ba9\u6240\u6709\u7684\u5de5\u4f5c\u90fd\u4ea4\u7ed9\u4e0b\u534a\u90e8\u53bb\u5904\u7406\u3002\u4f46\u662f\uff0c\u603b\u6709\u4e00\u4e9b\u5de5\u4f5c\u9700\u8981\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u6765\u5b8c\u6210\uff0c\u6bd4\u5982\u5bf9\u4e2d\u65ad\u7684\u5230\u8fbe\u8fdb\u884c\u786e\u8ba4\uff0c\u4ece\u786c\u4ef6\u62f7\u8d1d\u6570\u636e\u7b49\u3002\u5982\u4f55\u533a\u5206\u4e0a\u534a\u90e8\u548c\u4e0b\u534a\u90e8\uff0c\u5b8c\u5168\u7531\u9a71\u52a8\u7a0b\u5e8f\u5f00\u53d1\u8005\u81ea\u884c\u5224\u65ad\u3002\u5982\u679c\u4efb\u52a1\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42\uff0c\u5219\u9002\u5408\u653e\u5230\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u3002

  • \u5982\u679c\u8fd9\u4e2a\u4efb\u52a1\u5bf9\u65f6\u95f4\u975e\u5e38\u654f\u611f\u3002
  • \u5982\u679c\u8fd9\u4e2a\u4efb\u52a1\u548c\u786c\u4ef6\u76f8\u5173\u3002
  • \u5982\u679c\u8fd9\u4e2a\u4efb\u52a1\u4e0d\u80fd\u88ab\u5176\u4ed6\u4e2d\u65ad\u6253\u65ad\u3002

\u5176\u4ed6\u6240\u6709\u4efb\u52a1\uff0c\u8003\u8651\u653e\u5728\u4e0b\u534a\u90e8\u6267\u884c\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4ec5\u6267\u884c\u6700\u5173\u952e\u7684\u7d27\u6025\u4efb\u52a1\uff0c\u968f\u540e\u4fbf\u4f1a\u89e6\u53d1\u4e0b\u534a\u90e8\u673a\u5236\u4ee5\u5904\u7406\u540e\u7eed\u7684\u975e\u7d27\u6025\u4efb\u52a1\u3002

"},{"location":"linux/kernel/interrupt/#_6","title":"\u8f6f\u4e2d\u65ad","text":"

\u8f6f\u4e2d\u65ad\u7684\u82f1\u6587\u540d\u662fsoftirq\uff0c\u4e0e\u8f6f\u4ef6\u4e2d\u65ad\uff08software irq\uff09\u6ca1\u6709\u5173\u7cfb\u3002

\u8f6f\u4e2d\u65ad\u4f7f\u7528\u6bd4\u8f83\u5c11\uff0ctasklet\u662f\u4e0b\u534a\u90e8\u66f4\u5e38\u7528\u8fc7\u7684\u4e00\u79cd\u5f62\u5f0f\u3002\u4f46\u662f\uff0c\u7531\u4e8etasklet\u662f\u901a\u8fc7\u8f6f\u4e2d\u65ad\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u4e5f\u6709\u5fc5\u8981\u5148\u7814\u7a76\u8f6f\u4e2d\u65ad\u3002

"},{"location":"linux/kernel/interrupt/#_7","title":"\u8f6f\u4e2d\u65ad\u7684\u5b9e\u73b0","text":"

\u8f6f\u4e2d\u65ad\u5728\u7f16\u8bd1\u671f\u95f4\u9759\u6001\u5206\u914d\uff0c\u7531softirq_action\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct softirq_action{\n    void (*action)(struct softirq_action*);\n};\n

\u5728<kernel/softirq.c>\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u670932\u4e2a\u8be5\u7ed3\u6784\u4f53\u7684\u6570\u7ec4\uff1a

static struct softirq_action softirq_vec[NR_SOFTIRQS];\n

\u6bcf\u4e2a\u88ab\u6ce8\u518c\u7684\u8f6f\u4e2d\u65ad\u90fd\u5360\u636e\u8be5\u6570\u7ec4\u7684\u4e00\u9879\uff0c\u56e0\u6b64\u6700\u5927\u670932\u4e2a\u8f6f\u4e2d\u65ad\u3002

\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8faction\u7684\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a

void softirq_handler(struct softirq_action *)\n

\u5f53\u5185\u6838\u8fd0\u884c\u4e00\u4e2a\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u65f6\uff0c\u5c31\u4f1a\u6267\u884c\u8fd9\u4e2aaction\u51fd\u6570\uff0c\u5176\u552f\u4e00\u7684\u53c2\u6570\u662f\u6307\u5411\u76f8\u5e94softirq_action\u7ed3\u6784\u4f53\u7684\u6307\u9488\u3002\u4f8b\u5982\uff0c\u5982\u679cmy_softirq\u6307\u5411softirq_vec\u6570\u7ec4\u7684\u67d0\u9879\uff0c\u90a3\u4e48\u5185\u6838\u5c31\u4f1a\u7528\u5982\u4e0b\u7684\u65b9\u5f0f\u8c03\u7528\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u51fd\u6570\uff1a

my_softirq->action(my_softirq);\n

\u4e00\u4e2a\u6ce8\u518c\u7684\u8f6f\u4e2d\u65ad\u5fc5\u987b\u5728\u88ab\u6807\u8bb0\u540e\u624d\u4f1a\u6267\u884c\uff0c\u8fd9\u88ab\u79f0\u4e3a\u89e6\u53d1\u8f6f\u4e2d\u65ad\uff08raising the softirq\uff09\u3002\u901a\u5e38\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4f1a\u5728\u8fd4\u56de\u524d\u6807\u8bb0\u5b83\u7684\u8f6f\u4e2d\u65ad\uff0c\u4f7f\u5176\u5728\u7a0d\u540e\u88ab\u6267\u884c\u3002do_softirq()\u51fd\u6570\u4f1a\u68c0\u67e5softirq_vec\u6570\u7ec4\u4e2d\u6240\u6709\u5f85\u5904\u7406\u7684\u8f6f\u4e2d\u65ad\uff0c\u8c03\u7528\u5b83\u4eec\u7684\u5904\u7406\u7a0b\u5e8f\u3002

"},{"location":"linux/kernel/interrupt/#_8","title":"\u4f7f\u7528\u8f6f\u4e2d\u65ad","text":"

\u8f6f\u4e2d\u65ad\u4fdd\u7559\u7ed9\u7cfb\u7edf\u4e2d\u5bf9\u65f6\u95f4\u8981\u6c42\u6700\u4e25\u683c\u4ee5\u53ca\u6700\u91cd\u8981\u7684\u4e0b\u534a\u90e8\u4f7f\u7528\u3002\u5185\u6838\u5728<linux/interrupt.h>\u4e2d\u5df2\u7ecf\u5b9a\u4e49\u4e86\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u6765\u9759\u6001\u5730\u58f0\u660e\u8f6f\u4e2d\u65ad\uff0c\u7d22\u5f15\u53f7\u8868\u793a\u5176\u4f18\u5148\u7ea7\u3002\u8981\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u8f6f\u4e2d\u65ad\u5fc5\u987b\u5728\u6b64\u679a\u4e3e\u7c7b\u578b\u4e2d\u52a0\u5165\u65b0\u7684\u9879\uff0c\u800c\u4e14\u4f60\u5fc5\u987b\u6839\u636e\u5b83\u7684\u4f18\u5148\u7ea7\u6765\u51b3\u5b9a\u52a0\u5165\u7684\u4f4d\u7f6e\u3002\u4e0b\u8868\u5217\u51fa\u4e86\u5df2\u6709\u7684tasklet\u7c7b\u578b\u3002

tasklet \u4f18\u5148\u7ea7 \u8f6f\u4e2d\u65ad\u63cf\u8ff0 HI_SOFTIRQ 0 \u4f18\u5148\u7ea7\u9ad8\u7684tasklets TIMER_SOFTIRQ 1 \u5b9a\u65f6\u5668\u7684\u4e0b\u534a\u90e8 NET_TX_SOFTIRQ 2 \u53d1\u9001\u7f51\u7edc\u6570\u636e\u5305 NET_RX_SOFTIRQ 3 \u63a5\u6536\u7f51\u7edc\u6570\u636e\u5305 BLOCK_SOFTIRQ 4 BLOCK\u88c5\u7f6e TASKLET_SOFTIRQ 5 \u6b63\u5e38\u4f18\u5148\u6743\u7684tasklets SCHED_SOFTIRQ 6 \u8c03\u5ea6\u7a0b\u5e8f HRTIMER_SOFTIRQ 7 \u9ad8\u5206\u8fa8\u7387\u5b9a\u65f6\u5668 RCU_SOFTIRQ 8 RCU\u9501\u5b9a

\u63a5\u7740\uff0c\u5728\u8fd0\u884c\u65f6\u901a\u8fc7\u8c03\u7528open_softirq()\u51fd\u6570\u6ce8\u518c\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u8be5\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u8f6f\u4e2d\u65ad\u7d22\u5f15\u53f7\u548c\u5904\u7406\u51fd\u6570\uff1a

open_softirq(NET_TX_SOFTIRQ, net_tx_action)\n

\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u7684\u65f6\u5019\uff0c\u5141\u8bb8\u54cd\u5e94\u4e2d\u65ad\uff0c\u4f46\u5b83\u81ea\u5df1\u4e0d\u80fd\u4f11\u7720\u3002\u5728\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5f53\u524d\u5904\u7406\u5668\u4e0a\u7684\u8f6f\u4e2d\u65ad\u88ab\u7981\u6b62\u3002\u4f46\u5176\u4ed6\u5904\u7406\u5668\u4ecd\u7136\u53ef\u4ee5\u6267\u884c\u522b\u7684\u8f6f\u4e2d\u65ad\u3002\u8fd9\u610f\u5473\u7740\u5171\u4eab\u6570\u636e\u9700\u8981\u9501\u7684\u4fdd\u62a4\u3002\u56e0\u6b64\u5927\u90e8\u5206\u8f6f\u4e2d\u65ad\u5904\u7406\uff0c\u90fd\u901a\u8fc7\u91c7\u53d6\u5355\u5904\u7406\u5668\u6570\u636e\u6765\u907f\u514d\u52a0\u9501\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u51fa\u8272\u7684\u6027\u80fd\u3002

\u5728\u6ce8\u518c\u5b8c\u4e4b\u540e\uff0c\u65b0\u7684\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5c31\u80fd\u591f\u8fd0\u884c\u3002raise_softirq()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8f6f\u4e2d\u65ad\u8bbe\u7f6e\u4e3a\u6302\u8d77\u72b6\u6001\uff0c\u8ba9\u5b83\u5728\u4e0b\u6b21\u8c03\u7528do_softirq()\u51fd\u6570\u65f6\u6295\u5165\u8fd0\u884c\u3002

\u5728\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\u89e6\u53d1\u8f6f\u4e2d\u65ad\u662f\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\u3002\u5185\u6838\u5728\u6267\u884c\u5b8c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e4b\u540e\uff0c\u9a6c\u4e0a\u5c31\u4f1a\u8c03\u7528do_softirq()\u51fd\u6570\uff0c\u4e8e\u662f\u8f6f\u4e2d\u65ad\u5f00\u59cb\u6267\u884c\u7ed9\u5b9a\u7684\u4efb\u52a1\u3002

"},{"location":"linux/kernel/interrupt/#tasklet","title":"tasklet","text":"

tasklet\u662f\u5229\u7528\u8f6f\u4e2d\u65ad\u5b9e\u73b0\u7684\u4e00\u79cd\u4e0b\u534a\u90e8\u673a\u5236\uff0c\u4f46\u662f\u5b83\u7684\u63a5\u53e3\u66f4\u7b80\u5355\uff0c\u9501\u4fdd\u62a4\u8981\u6c42\u8f83\u4f4e\u3002\u5927\u591a\u6570\u60c5\u51b5\u90fd\u53ef\u4ee5\u4f7f\u7528tasklet\u6765\u5b8c\u6210\u4f60\u9700\u8981\u7684\u5de5\u4f5c\u3002

tasklet\u6709\u4e00\u4e9b\u6bd4\u8f83\u6709\u610f\u601d\u7684\u7279\u6027\uff1a

  • \u4e00\u4e2atasklet\u53ef\u5728\u7a0d\u540e\u88ab\u7981\u6b62\u6216\u8005\u91cd\u65b0\u542f\u7528\uff1b\u53ea\u6709\u542f\u7528\u7684\u6b21\u6570\u548c\u7981\u6b62\u7684\u6b21\u6570\u76f8\u540c\u65f6\uff0ctasklet\u624d\u4f1a\u88ab\u6267\u884c\u3002

  • tasklet\u53ef\u4ee5\u6ce8\u518c\u81ea\u5df1\u672c\u8eab\u3002

  • tasklet\u53ef\u88ab\u8c03\u5ea6\u5728\u6b63\u5e38\u4f18\u5148\u7ea7\u6216\u8005\u66f4\u9ad8\u4f18\u5148\u7ea7\u6267\u884c\u3002

  • \u5f53\u7cfb\u7edf\u8d1f\u8f7d\u4f4e\u65f6\uff0ctasklet\u4f1a\u88ab\u7acb\u523b\u6267\u884c\uff0c\u4f46\u518d\u665a\u4e0d\u4f1a\u665a\u4e8e\u4e0b\u4e00\u4e2a\u5b9a\u65f6\u5668tick\u3002

  • \u4e00\u4e2atasklet\u53ef\u4ee5\u4e0e\u5176\u4ed6tasklet\u5e76\u53d1\uff0c\u4f46\u662f\u540c\u4e00\u4e2atasklet\u6c38\u8fdc\u4e0d\u4f1a\u5728\u591a\u4e2aCPU\u4e0a\u540c\u65f6\u8fd0\u884c\uff0c\u4e14\u53ea\u4f1a\u5728\u8c03\u5ea6\u81ea\u5df1\u7684\u540c\u4e00CPU\u4e0a\u8fd0\u884c\u3002

"},{"location":"linux/kernel/interrupt/#tasklet_1","title":"tasklet\u7684\u5b9e\u73b0","text":"

tasklet\u7531\u4e24\u7c7b\u8f6f\u4e2d\u65ad\u4ee3\u8868\uff1aHI_SOFTIRQ\u548cTASKLET_SOFTIRQ\u3002\u524d\u8005\u4f18\u5148\u7ea7\u6bd4\u540e\u8005\u9ad8\u3002

tasklet\u7ed3\u6784\u4f53\u5982\u4e0b\uff1a

struct tasklet_struct {\n    struct tasklet_struct *next;    //\u94fe\u8868\u4e2d\u4e0b\u4e00\u4e2atasklet\n    unsigned long state;            //tasklet\u7684\u72b6\u6001\n    atomic_t count;                 //\u5f15\u7528\u8ba1\u6570\u5668\n    void (*func)(unsigned long);    //\u5904\u7406\u51fd\u6570\n    unsigned long data;             //\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\n};\n

state\u6210\u5458\u53ea\u80fd\u57280\u3001TASKLET_STATE_SCHED\u548cTASKLET_STATE_RUN\u4e4b\u95f4\u53d6\u503cTASKLET_STATE_SCHED\u8868\u660e tasklet\u5df2\u88ab\u8c03\u5ea6\uff0c\u6b63\u51c6\u5907\u6295\u5165\u8fd0\u884c\uff0cTASKLET_STATE_RUN\u8868\u660e\u8be5tasklet\u6b63\u5728\u8fd0\u884c\u3002TASKLET_STATE_RUN\u53ea\u6709\u5728\u591a\u5904\u7406\u5668\u7684\u7cfb\u7edf\u4e0a\u624d\u4f1a\u4f5c\u4e3a\u4e00\u79cd\u4f18\u5316\u6765\u4f7f\u7528\uff0c\u5355\u5904\u7406\u5668\u7cfb\u7edf\u4efb\u4f55\u65f6\u5019\u90fd\u6e05\u695a\u5355\u4e2atasklet\u662f\u4e0d\u662f\u6b63\u5728\u8fd0\u884c(\u5b83\u8981\u4e48\u5c31\u662f\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\uff0c\u8981\u4e48\u4e0d\u662f)\u3002

count\u6210\u5458\u662ftasklet\u7684\u5f15\u7528\u8ba1\u6570\u5668\u3002\u5982\u679c\u5b83\u4e0d\u4e3a0\uff0c\u5219tasklet\u88ab\u7981\u6b62\uff1b\u53ea\u6709\u5f53\u5b83\u4e3a0\u65f6\uff0ctasklet\u624d\u88ab\u6fc0\u6d3b\u3002

\u5df2\u8c03\u5ea6\uff08\u6216\u8005\u53eb\u5df2\u6fc0\u6d3b\uff09\u7684tasklet\u5b58\u653e\u5728tasklet_vec\uff08\u666e\u901atasklet\uff09\u548ctasklet_hi_vec\uff08\u9ad8\u4f18\u5148\u7ea7\u7684tasklet\uff09\u6570\u7ec4\u4e2d\u3002\u8fd9\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531tasklet_struct\u7ed3\u6784\u4f53\u6784\u6210\u7684\u94fe\u8868\uff0c\u94fe\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u4e0d\u540c\u7684tasklet\u3002

tasklet\u7531tasklet_schedule()\u548ctasklet_hi_schedule()\u51fd\u6570\u8fdb\u884c\u8c03\u5ea6\uff0c\u5b83\u4eec\u63a5\u53d7\u4e00\u4e2a\u6307\u5411tasklet_struct\u7ed3\u6784\u7684\u6307\u9488\u4f5c\u4e3a\u53c2\u6570\u3002

"},{"location":"linux/kernel/interrupt/#tasklet_2","title":"\u4f7f\u7528tasklet","text":"

\u5982\u679c\u662f\u9759\u6001\u521b\u5efa\u4e00\u4e2atasklet\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u4e2a\u5b8f\u4e2d\u7684\u4e00\u4e2a\uff1a

DECLARE_TASKLET(name, func, data)\nDECLARE_TASKLET_DISABLED(name, func, data)\n

\u8fd9\u4e24\u4e2a\u5b8f\u90fd\u80fd\u9759\u6001\u521b\u5efa\u4e00\u4e2atasklet_struct\u7ed3\u6784\uff0c\u533a\u522b\u5728\u4e8e\u5f15\u7528\u8ba1\u6570count\u7684\u521d\u59cb\u503c\u4e0d\u540c\u3002\u7b2c\u4e00\u4e2a\u8bbe\u4e3a0\uff0c\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\uff1b\u7b2c\u4e8c\u4e2a\u8bbe\u4e3a1\uff0c\u5904\u4e8e\u7981\u6b62\u72b6\u6001\u3002

\u5982\u679c\u662f\u52a8\u6001\u521b\u5efa\uff1a

tasklet_init(t, tasklet_handler, dev);\n

\u7f16\u5199\u81ea\u5df1\u7684tasklet\u5904\u7406\u7a0b\u5e8f\uff1a

void tasklet_handler(unsigned long data)\n

tasklet\u4e0d\u80fd\u7761\u7720\uff0c\u4e24\u4e2a\u76f8\u540c\u7684tasklet\u4e0d\u4f1a\u540c\u65f6\u6267\u884c\uff0c\u4f46\u5982\u679c\u4e0e\u5176\u4ed6tasklet\u6216\u8005\u662f\u8f6f\u4e2d\u65ad\u5171\u4eab\u4e86\u6570\u636e\uff0c\u90a3\u4e48\u5fc5\u987b\u8fdb\u884c\u9501\u4fdd\u62a4\u3002

\u901a\u8fc7\u8c03\u7528tasklet_schedule()\u51fd\u6570\u5e76\u4f20\u9012\u7ed9\u5b83\u76f8\u5e94\u7684tasklet_struct\u6307\u9488\uff0c\u8be5tasklet\u5c31\u4f1a\u88ab\u8c03\u5ea6\u4ee5\u4fbf\u6267\u884c\uff1a

tasklet_schedule(&my_tasklet);\n

\u4e00\u4e2atasklet\u603b\u662f\u5728\u8c03\u5ea6\u5b83\u7684\u5904\u7406\u5668\u4e0a\u8fd0\u884c\u3002\u8981\u7981\u6b62/\u4f7f\u80fdtasklet\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\uff1a

tasklet_disable(&my_tasklet);   \ntasklet_disable_nosync(&my_tasklet);         \n

\u7b2c\u4e00\u4e2a\u51fd\u6570\u7981\u6b62\u6307\u5b9a\u7684tasklet\uff0c\u5982\u679c\u6b63\u5728\u8fd0\u884c\uff0c\u5219\u4f1a\u7b49\u5f85\u671f\u6267\u884c\u5b8c\u6bd5\u3002\u7b2c\u4e8c\u4e2a\u51fd\u6570\u4e0d\u4f1a\u7b49\u5f85\u4efb\u4f55\u6b63\u5728\u8fd0\u884c\u7684tasklet\u3002

\u4f7f\u80fdtasklet\uff1a

tasklet_enable(&my_tasklet);\n

\u79fb\u9664tasklet\uff0c\u901a\u5e38\u5728\u8bbe\u5907\u5173\u95ed\u6216\u8005\u6a21\u5757\u9000\u51fa\u65f6\u8c03\u7528\u8be5\u51fd\u6570\uff1a

tasklet_kill(&my_tasklet);\n
"},{"location":"linux/kernel/interrupt/#ksoftirqd","title":"ksoftirqd","text":"

\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u6709\u4e00\u7ec4\u8f85\u52a9\u5904\u7406\u8f6f\u4e2d\u65ad\uff08\u548ctasklet\uff09\u7684\u5185\u6838\u7ebf\u7a0b\u3002\u5f53\u5185\u6838\u4e2d\u51fa\u73b0\u5927\u91cf\u8f6f\u4e2d\u65ad\u7684\u65f6\u5019\uff0c\u5185\u6838\u7ebf\u7a0b\u5c31\u4f1a\u9009\u62e9\u5408\u9002\u7684\u65f6\u673a\u6765\u5904\u7406\u8f6f\u4e2d\u65ad\u3002

\u5728\u5927\u6d41\u91cf\u7684\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0c\u8f6f\u4e2d\u65ad\u7684\u89e6\u53d1\u9891\u7387\u53ef\u80fd\u5f88\u9ad8\uff0c\u751a\u81f3\u8fd8\u4f1a\u81ea\u884c\u91cd\u590d\u89e6\u53d1\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u65e0\u6cd5\u83b7\u5f97\u8db3\u591f\u7684\u5904\u7406\u5668\u65f6\u95f4\u3002\u5982\u679c\u8f6f\u4e2d\u65ad\u548c\u91cd\u590d\u89e6\u53d1\u7684\u8f6f\u4e2d\u65ad\u90fd\u88ab\u7acb\u5373\u5904\u7406\uff0c\u90a3\u4e48\u5f53\u8d1f\u8f7d\u5f88\u9ad8\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u5c31\u4f1a\u51fa\u73b0\u660e\u663e\u7684\u5361\u987f\u73b0\u8c61\u3002\u5982\u679c\u9009\u62e9\u4e0d\u5904\u7406\u91cd\u65b0\u89e6\u53d1\u7684\u8f6f\u4e2d\u65ad\uff0c\u53c8\u4f1a\u6d6a\u8d39\u95f2\u7f6e\u7684\u7cfb\u7edf\u8d44\u6e90\uff0c\u5bfc\u81f4\u8f6f\u4e2d\u65ad\u51fa\u73b0\u9965\u997f\u73b0\u8c61\u3002

\u5185\u6838\u4e2d\u7684\u65b9\u6848\u65f6\u4e0d\u4f1a\u7acb\u5373\u5904\u7406\u91cd\u590d\u89e6\u53d1\u7684\u8f6f\u4e2d\u65ad\u3002\u5f53\u5927\u91cf\u8f6f\u4e2d\u65ad\u51fa\u73b0\u7684\u65f6\u5019\uff0c\u5185\u6838\u4f1a\u5524\u9192\u4e00\u7ec4\u5185\u6838\u7ebf\u7a0b\u6765\u5904\u7406\u8fd9\u4e9b\u8d1f\u8f7d\u3002\u8fd9\u4e9b\u7ebf\u7a0b\u5728\u6700\u4f4e\u4f18\u5148\u7ea7\uff08nice=19\uff09\u8fd0\u884c\uff0c\u907f\u514d\u4e0e\u5176\u4ed6\u4efb\u52a1\u62a2\u5360\u8d44\u6e90\u3002

\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u7ebf\u7a0b\uff0c\u540d\u5b57\u4e3aksoftirqd/n\uff0cn\u4e3a\u5904\u7406\u5668\u7f16\u53f7\u3002\u53ea\u8981\u6709\u5f85\u5904\u7406\u7684\u8f6f\u4e2d\u65ad\uff0cksoftirqd\u5c31\u4f1a\u8c03\u7528do_softirq()\u51fd\u6570\u6765\u5904\u7406\u5b83\u4eec\u3002

"},{"location":"linux/kernel/interrupt/#_9","title":"\u5de5\u4f5c\u961f\u5217","text":"

\u5de5\u4f5c\u961f\u5217\u662f\u4e00\u79cd\u5ef6\u540e\u6267\u884c\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u5c06\u540e\u7eed\u7684\u5de5\u4f5c\u4ea4\u7ed9\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u6267\u884c\u2014\u2014\u8fd9\u4e2a\u4e0b\u534a\u90e8\u5206\u603b\u662f\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u3002\u8fd9\u6837\uff0c\u901a\u8fc7\u5de5\u4f5c\u961f\u5217\u5b9e\u73b0\u7684\u4ee3\u7801\u5c31\u80fd\u4eab\u53d7\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u7684\u6240\u6709\u4f18\u52bf\uff0c\u6bd4\u5982\u53ef\u4ee5\u91cd\u65b0\u8c03\u5ea6\u751a\u81f3\u662f\u7761\u7720\u3002

\u5982\u679c\u63a8\u540e\u6267\u884c\u7684\u4efb\u52a1\u9700\u8981\u7761\u7720\uff0c\u90a3\u4e48\u5c31\u9009\u62e9\u5de5\u4f5c\u961f\u5217\u3002\u5426\u5219\u5c31\u9009\u62e9\u8f6f\u4e2d\u65ad\u6216tasklet\u3002\u5de5\u4f5c\u961f\u5217\u5728\u4f60\u9700\u8981\u83b7\u5f97\u5927\u91cf\u5185\u5b58\u65f6\uff0c\u9700\u8981\u83b7\u53d6\u4fe1\u53f7\u91cf\u65f6\uff0c\u9700\u8981\u6267\u884c\u963b\u585e\u5f0f\u7684I/O\u64cd\u4f5c\u65f6\uff0c\u5b83\u90fd\u4f1a\u975e\u5e38\u6709\u7528\u3002

"},{"location":"linux/kernel/interrupt/#_10","title":"\u5de5\u4f5c\u961f\u5217\u7684\u5b9e\u73b0","text":"

\u5de5\u4f5c\u961f\u5217\u5b50\u7cfb\u7edf\u63d0\u4f9b\u4e86\u7f3a\u7701\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\uff08worker thread\uff09\u6765\u5904\u7406\u9700\u8981\u63a8\u540e\u7684\u5de5\u4f5c\u3002\u7f3a\u7701\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u53eb\u505aevents/n\uff0cn\u4e3a\u5904\u7406\u5668\u7684\u7f16\u53f7\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\uff0c\u4e0d\u8fc7\u4e00\u822c\u4f7f\u7528\u7f3a\u7701\u7ebf\u7a0b\u5373\u53ef\u3002\u7136\u800c\uff0c\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u9700\u8981\u7279\u522b\u7684\u5904\u7406\uff0c\u6216\u8005\u5b83\u5bf9\u6027\u80fd\u6709\u975e\u5e38\u4e25\u683c\u7684\u8981\u6c42\uff0c\u90a3\u4e48\u521b\u5efa\u4e00\u4e2a\u4e13\u7528\u7684\u5185\u6838\u7ebf\u7a0b\u53ef\u80fd\u5c31\u662f\u5fc5\u8981\u7684\u3002\u4e13\u7528\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u548c\u4f18\u5316\u4efb\u52a1\u7684\u6267\u884c\uff0c\u4f46\u4e5f\u9700\u8981\u66f4\u591a\u7684\u8d44\u6e90\u548c\u7ba1\u7406\u5f00\u9500\u3002

\u5de5\u4f5c\u8005\u7ebf\u7a0b\u7528workqueue_struct\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct workqueue_struct {\n    struct cpu_workqueue_struct cpu_wq[NR_CPUS]\uff1b\n    struct list_head list;\n    const char *name;\n    int singlethread;\n    int freezeable;\n    int rt;\n};\n

\u8be5\u7ed3\u6784\u4f53\u5185\u6709\u4e00\u4e2acpu_workqueue_struct\u7ed3\u6784\u7ec4\u6210\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\u5bf9\u5e94\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e2a\u5904\u7406\u5668\u3002\u4e5f\u5c31\u662f\u8bf4\u7cfb\u7edf\u4e2d\u6bcf\u4e2a\u5904\u7406\u5668\u5bf9\u5e94\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u3002

\u5de5\u4f5c\u7531work_struct\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct work_struct {\n    atomic_long_t data;\n    struct list_head entry;\n    work_func_t func;\n};\n

\u8fd9\u4e9b\u7ed3\u6784\u4f53\u88ab\u8fde\u63a5\u6210\u94fe\u8868\uff0c\u6bcf\u4e2a\u5904\u7406\u5668\u4e0a\u7684\u6bcf\u79cd\u7c7b\u578b\u7684\u961f\u5217\u90fd\u5bf9\u5e94\u8fd9\u6837\u4e00\u4e2a\u94fe\u8868\u3002\u5f53\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u88ab\u5524\u9192\u65f6\uff0c\u5b83\u4f1a\u6267\u884c\u94fe\u8868\u4e0a\u7684\u6240\u6709\u5de5\u4f5c\uff0c\u5f53\u6ca1\u6709\u5269\u4f59\u7684\u64cd\u4f5c\u65f6\uff0c\u5b83\u5c31\u4f1a\u7ee7\u7eed\u4f11\u7720\u3002

"},{"location":"linux/kernel/interrupt/#_11","title":"\u4f7f\u7528\u5de5\u4f5c\u961f\u5217","text":"

\u9759\u6001\u521b\u5efa\u4e00\u4e2awork_struct\u7ed3\u6784\u4f53\uff1a

DECLARE_WORK(name, void(*func)(void *), void *data);\n

\u5982\u679c\u662f\u52a8\u6001\u521b\u5efa\uff1a

INIT_WORK(struct work_struct *work, void (*func)(void *), void *data);\n

\u5de5\u4f5c\u961f\u5217\u5904\u7406\u51fd\u6570\u7684\u539f\u578b\u662f\uff1a

void work_handler(void *data);\n

\u8fd9\u4e2a\u51fd\u6570\u7531\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u6267\u884c\uff0c\u56e0\u6b64\u5904\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5141\u8bb8\u76f8\u5e94\u4e2d\u65ad\uff0c\u5e76\u4e14\u4e0d\u6301\u6709\u4efb\u4f55\u9501\u3002\u5c3d\u7ba1\u5904\u7406\u51fd\u6570\u4f4d\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\uff0c\u4f46\u662f\u5b83\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7528\u6237\u7a7a\u95f4\uff0c\u56e0\u4e3a\u5185\u6838\u7ebf\u7a0b\u5728\u7528\u6237\u7a7a\u95f4\u6ca1\u6709\u76f8\u5173\u7684\u5185\u5b58\u6620\u5c04\u3002

\u5c06\u5de5\u4f5c\u63d0\u4ea4\u5230\u5de5\u4f5c\u961f\u5217\uff1a

int queue_work(struct workqueue_struct *queue, struct work_struct *work);\nint queue_delayed_work(struct workqueue_struct *queue, struct work_struct *work, unsigned long delay);\n

\u5bf9\u5de5\u4f5c\u8fdb\u884c\u8c03\u5ea6\uff0c\u628a\u7ed9\u5b9a\u5de5\u4f5c\u7684\u5904\u7406\u51fd\u6570\u63d0\u4ea4\u7ed9\u7f3a\u7701\u7684events\u5de5\u4f5c\u7ebf\u7a0b\uff1a

schedule_work(&work);\n

\u4e00\u65e6\u5176\u6240\u5728\u5904\u7406\u5668\u4e0a\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u88ab\u5524\u9192\uff0c\u5c31\u4f1a\u7acb\u523b\u6267\u884c\u3002\u5982\u679c\u9700\u8981\u5de5\u4f5c\u5ef6\u8fdf\u6267\u884c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51fd\u6570\uff1a

schedule_delayed_work(&work, delay);\n

&work\u6307\u5411\u7684work_struct\u5219\u4f1a\u7b49\u5f85delay\u4e2a\u65f6\u949f\u8282\u62cd\u624d\u4f1a\u6267\u884c\u3002

\u5982\u679c\u7f3a\u7701\u7684\u961f\u5217\u4e0d\u80fd\u6ee1\u8db3\u4f60\u7684\u8981\u6c42\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u961f\u5217\u548c\u4e0e\u4e4b\u76f8\u5e94\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u3002\u8fd9\u4e48\u505a\u4f1a\u5728\u6bcf\u4e2a\u5904\u7406\u5668\u4e0a\u90fd\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u3002\u9664\u975e\u4f60\u786e\u5b9a\u5fc5\u987b\u8981\u9760\u81ea\u5df1\u7684\u7ebf\u7a0b\u624d\u80fd\u63d0\u9ad8\u6027\u80fd\uff0c\u5426\u5219\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a

struct workqueue_struct *create_workqueue(const char *name);\n

\u5728\u7ed3\u675f\u5bf9\u5de5\u4f5c\u961f\u5217\u7684\u4f7f\u7528\u540e\uff0c\u53ef\u4ee5\u8c03\u7528\u4ee5\u4e0b\u51fd\u6570\u6765\u91ca\u653e\u8d44\u6e90\uff1a

void destroy_workqueue(struct workqueue_struct *queue);\n
"},{"location":"linux/kernel/interrupt/#_12","title":"\u4e0b\u534a\u90e8\u7684\u540c\u6b65","text":"

\u5728\u4f7f\u7528\u4e0b\u534a\u90e8\u673a\u5236\u65f6\uff0c\u5373\u4fbf\u662f\u4e00\u4e2a\u5355\u5904\u7406\u5668\uff0c\u907f\u514d\u5171\u4eab\u6570\u636e\u88ab\u540c\u65f6\u8bbf\u95ee\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4f7f\u7528tasklet\u7684\u597d\u5904\u662f\uff1a\u4e24\u4e2a\u76f8\u540c\u7c7b\u578b\u7684tasklet\u4e0d\u5141\u8bb8\u540c\u65f6\u6267\u884c\uff0c\u5373\u4fbf\u662f\u4e0d\u540c\u7684\u5904\u7406\u5668\u4e5f\u4e0d\u884c\u3002tasklet\u4e4b\u95f4\u7684\u540c\u6b65\uff08\u5373\u4e0d\u540c\u7c7b\u578b\u7684tasklet\u5171\u4eab\u540c\u4e00\u6570\u636e\uff09\uff0c\u6b63\u786e\u4f7f\u7528\u9501\u673a\u5236\u5373\u53ef\u3002

\u5982\u679c\u662f\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u548c\u4e0b\u534a\u90e8\u5171\u4eab\u6570\u636e\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u7981\u6b62\u4e0b\u534a\u90e8\u7684\u5904\u7406\u5e76\u83b7\u5f97\u9501\u7684\u4f7f\u7528\u6743\uff0c\u4ee5\u9632\u6b62\u6b7b\u9501\u7684\u51fa\u73b0\u3002

\u5982\u679c\u662f\u4e2d\u65ad\u4e0a\u4e0b\u6587\u548c\u4e0b\u534a\u90e8\u5171\u4eab\u6570\u636e\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u7981\u6b62\u4e2d\u65ad\u5e76\u83b7\u5f97\u9501\u7684\u4f7f\u7528\u6743\uff0c\u4e5f\u662f\u4e3a\u4e86\u9632\u6b62\u6b7b\u9501\u7684\u51fa\u73b0\u3002

\u4efb\u4f55\u5728\u5de5\u4f5c\u961f\u5217\u4e2d\u88ab\u5171\u4eab\u7684\u6570\u636e\u4e5f\u9700\u8981\u4f7f\u7528\u9501\u673a\u5236\u3002

\u4e00\u822c\u5355\u7eaf\u7981\u6b62\u4e0b\u534a\u90e8\u662f\u4e0d\u591f\u7684\uff0c\u66f4\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u5148\u83b7\u5f97\u4e00\u4e2a\u9501\u518d\u7981\u6b62\u4e0b\u534a\u90e8\u7684\u5904\u7406\u3002\u5982\u679c\u9700\u8981\u7981\u6b62\u6240\u6709\u4e0b\u534a\u90e8\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u8c03\u7528local_bh_disable()\u51fd\u6570\u3002\u5141\u8bb8\u4e0b\u534a\u90e8\u5904\u7406\uff0c\u8c03\u7528local_bh_enable()\u51fd\u6570\u3002\u51fd\u6570\u901a\u8fc7preempt_count\uff08\u5185\u6838\u62a2\u5360\u4e5f\u662f\u8fd9\u4e2a\u8ba1\u6570\u5668\uff09\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u7ef4\u62a4\u4e00\u4e2a\u8ba1\u6570\u5668\u3002\u5f53\u8ba1\u6570\u5668\u4e3a0\u65f6\uff0c\u624d\u53ef\u4ee5\u5904\u7406\u4e0b\u534a\u90e8\u3002

"},{"location":"linux/kernel/mm/","title":"\u7269\u7406\u5185\u5b58\u7ba1\u7406","text":"

\u8fd9\u90e8\u5206\u5185\u5bb9\u8981\u548c\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u7ed3\u5408\u8d77\u6765\u770b\u3002

"},{"location":"linux/kernel/mm/#_2","title":"\u9875\u548c\u533a","text":"

\u5185\u6838\u5c06\u7269\u7406\u5185\u5b58\u5212\u5206\u4e3a\u4f17\u591a\u56fa\u5b9a\u5927\u5c0f\u7684\u9875\uff0c\u6bcf\u9875\u901a\u5e38\u5305\u542b4KB\u7684\u5b58\u50a8\u7a7a\u95f4\u3002\u8fd9\u79cd\u5206\u9875\u673a\u5236\u6781\u5927\u5730\u63d0\u5347\u4e86\u5185\u6838\u5728\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\u65b9\u9762\u7684\u6548\u7387\uff0c\u540c\u65f6\u4e5f\u4e3a\u865a\u62df\u5185\u5b58\u7ba1\u7406\u63d0\u4f9b\u4e86\u4fbf\u5229\u3002\u901a\u8fc7\u5c06\u5185\u5b58\u5212\u5206\u4e3a\u4e00\u81f4\u5927\u5c0f\u7684\u5355\u5143\uff0c\u5185\u6838\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u5185\u5b58\u8bf7\u6c42\uff0c\u4f18\u5316\u8d44\u6e90\u5206\u914d\uff0c\u5e76\u7b80\u5316\u4e86\u5185\u5b58\u5bfb\u5740\u548c\u6620\u5c04\u7684\u8fc7\u7a0b\u3002struct page\u7ed3\u6784\u4f53\u8868\u793a\u9875\uff0c\u8be5\u5b9a\u4e49\u7684\u7b80\u5316\u7248\u5982\u4e0b\uff1a

struct page {\n    unsigned long flags;\n    atomic_t _count;\n    atomic_t _mapcount;\n    unsigned long private;\n    struct address_space *mapping;\n    pgoff_t index;\n    struct list_head lru;\n    void *virtual;\n};\n

flags\uff1a\u5b58\u653e\u9875\u7684\u72b6\u6001\uff0c\u662f\u4e0d\u662f\u810f\u9875\uff0c\u662f\u4e0d\u662f\u9501\u5b9a\u5728\u5185\u5b58\u4e2d\u7b49\u3002

_count\uff1a\u5b58\u653e\u9875\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5f53\u9875\u88ab\u5206\u914d\u7ed9\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u8fd9\u4e2a\u8ba1\u6570\u5668\u4f1a\u589e\u52a0\u3002\u5f53\u9875\u88ab\u91ca\u653e\u65f6\uff0c\u8ba1\u6570\u5668\u51cf\u5c11\u3002\u76f4\u5230\u8ba1\u6570\u5668\u964d\u81f30\uff0c\u9875\u624d\u80fd\u88ab\u56de\u6536\u3002

_mapcount\uff1a\u7528\u4e8e\u8bb0\u5f55\u9875\u8868\u9879\u6620\u5c04\u5230\u8be5\u7269\u7406\u9875\u7684\u6b21\u6570\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u5171\u4eab\u5185\u5b58\u9875\uff0c\u591a\u4e2a\u8fdb\u7a0b\u53ef\u80fd\u6620\u5c04\u5230\u540c\u4e00\u4e2a\u7269\u7406\u9875\u3002

mapping\uff1a\u6307\u5411\u4e00\u4e2aaddress_space\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u7528\u4e8e\u63cf\u8ff0\u4e0e\u6587\u4ef6\u76f8\u5173\u7684\u5185\u5b58\u6620\u5c04\u4fe1\u606f\u3002\u5982\u679c\u9875\u5c5e\u4e8e\u6587\u4ef6\u7f13\u5b58\uff0c\u8fd9\u4e2a\u5b57\u6bb5\u5c06\u6307\u5411\u76f8\u5e94\u7684address_space\u7ed3\u6784\u3002

index\uff1a\u8868\u793a\u9875\u5728\u6620\u5c04\u7684\u6587\u4ef6\u4e2d\u7684\u504f\u79fb\u91cf\uff0c\u5373\u9875\u5728\u6587\u4ef6\u4e2d\u7684\u7d22\u5f15\u53f7\u3002

lru\uff1a\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\u8282\u70b9\uff0c\u7528\u4e8e\u5c06\u9875\u94fe\u5165\u5230\u5404\u79cd\u94fe\u8868\u4e2d\uff0c\u6bd4\u5982\u6d3b\u8dc3\u94fe\u8868\u3001\u4e0d\u6d3b\u8dc3\u94fe\u8868\u7b49\u3002\u8fd9\u662f\u5185\u6838\u5b9e\u73b0\u9875\u7f6e\u6362\u7b97\u6cd5\uff08\u5982LRU\uff09\u7684\u4e00\u90e8\u5206\u3002

virtual\uff1a\u9875\u5728\u865a\u62df\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002

\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5185\u5b58\u548c\u4f18\u5316\u5185\u5b58\u5206\u914d\u7b56\u7565\uff0c\u5185\u6838\u5c06\u9875\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u533a(zone)\uff0c\u901a\u5e38\u5212\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u533a\uff1a

  • ZONE_DMA\uff1a\u7528\u4e8eDMA\u64cd\u4f5c\u7684\u5185\u5b58\u533a\uff0c\u901a\u5e38\u4f4d\u4e8e\u7269\u7406\u5185\u5b58\u7684\u4f4e\u5730\u5740\u90e8\u5206\u3002

  • ZONE_DMA32\uff1a\u4e0eZONE_DMA\u7c7b\u4f3c\uff0c\u4f46\u9002\u7528\u4e8e32\u4f4d\u5730\u5740\u7684DMA\u64cd\u4f5c\u3002

  • ZONE_NORMAL\uff1a\u666e\u901a\u7684\u5185\u5b58\u533a\uff0c\u53ef\u4ee5\u7531\u5185\u6838\u548c\u7528\u6237\u7a7a\u95f4\u8fdb\u7a0b\u4f7f\u7528\u3002

  • ZONE_HIGHMEM\uff1a\u9ad8\u7aef\u5185\u5b58\u533a\uff0c\u7528\u4e8e\u5904\u7406\u8d85\u8fc7\u76f4\u63a5\u6620\u5c04\u8303\u56f4\u7684\u5185\u5b58\u3002

\u533a\u7684\u4f7f\u7528\u548c\u5206\u5e03\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\uff0c\u5185\u6838\u5c06\u9875\u5212\u5206\u4e3a\u533a\uff0c\u5c31\u53ef\u4ee5\u6839\u636e\u7528\u9014\u8fdb\u884c\u5206\u914d\u3002\u533a\u7684\u5212\u5206\u6ca1\u6709\u7269\u7406\u610f\u4e49\uff0c\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u7ba1\u7406\u800c\u91c7\u53d6\u7684\u4e00\u79cd\u903b\u8f91\u5206\u7ec4\u3002

"},{"location":"linux/kernel/mm/#_3","title":"\u9875\u64cd\u4f5c","text":"

\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4ee5\u9875\u4e3a\u5355\u4f4d\u7684\u5185\u5b58\u5206\u914d\u4e0e\u91ca\u653e\u7684\u51fd\u6570\uff0c\u8fd9\u91cc\u4e0d\u505a\u8be6\u7ec6\u8bf4\u660e\u3002

\u51fd\u6570 \u63cf\u8ff0 alloc_page(gfp_mask) \u53ea\u5206\u914d\u4e00\u9875\uff0c\u8fd4\u56de\u6307\u5411\u9875\u7684\u6307\u9488 alloc_pages(gfp_mask, order) \u5206\u914d2order\u4e2a\u9875\uff0c\u8fd4\u56de\u6307\u5411\u7b2c\u4e00\u9875\u7684\u6307\u9488 __get_free_page(gfp_mask) \u53ea\u5206\u914d\u4e00\u9875\uff0c\u8fd4\u56de\u6307\u5411\u5176\u903b\u8f91\u5730\u5740\u7684\u6307\u9488 __get_free_pages(gfp_mask, order) \u5206\u914d2order\u4e2a\u9875\uff0c\u8fd4\u56de\u6307\u5411\u7b2c\u4e00\u9875\u903b\u8f91\u5730\u5740\u7684\u6307\u9488 get_zero_page(gfp_mask) \u53ea\u5206\u914d\u4e00\u9875\uff0c\u586b\u5145\u4e3a0\uff0c\u8fd4\u56de\u6307\u5411\u5176\u903b\u8f91\u5730\u5740\u7684\u6307\u9488 __free_pages(page, order) \u4f20\u5165\u9875\u7684\u6307\u9488\uff0c\u91ca\u653e2order\u4e2a\u9875 free_pages(addr, order) \u4f20\u5165\u7b2c\u4e00\u9875\u7684\u903b\u8f91\u5730\u5740\uff0c\u91ca\u653e2order\u4e2a\u9875 free_page(addr) \u91ca\u653e\u5355\u5f20\u9875

\u91ca\u653e\u9875\u65f6\u8981\u8c28\u614e\uff0c\u4e00\u65e6\u4f20\u9012\u4e86\u9519\u8bef\u7684page\u6216\u8005address\uff0c\u7cfb\u7edf\u5c31\u4f1a\u5d29\u6e83\u3002\u5728\u83b7\u5f97\u9875\u4e4b\u540e\uff0c\u9700\u8981\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u68c0\u67e5\u4ee5\u786e\u8ba4\u5185\u6838\u6b63\u786e\u5730\u5206\u914d\u4e86\u9875\u3002

\u5bf9\u4e8e\u5e38\u7528\u7684\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u5185\u5b58\u5206\u914d\u6765\u8bf4\uff0c\u5185\u6838\u63d0\u4f9b\u7684\u51fd\u6570\u662fkmalloc()\u3002

"},{"location":"linux/kernel/mm/#kmalloc","title":"kmalloc()","text":"

kmalloc()\u4e0e\u7528\u6237\u7a7a\u95f4\u7684malloc()\u7c7b\u4f3c\uff0c\u90fd\u662f\u5206\u914d\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u4e00\u5757\u5185\u5b58\uff0c\u533a\u522b\u5728\u4e8ekmalloc()\u591a\u4e86\u4e00\u4e2aflags\u53c2\u6570\uff1a

\u6ce8\u610f\uff1a\u4f7f\u7528kmalloc()\u51fd\u6570\u5206\u914d\u7684\u5185\u5b58\u53ea\u80fd\u4f7f\u7528kfree()\u51fd\u6570\u91ca\u653e\u3002

void *kmalloc(size_t size, gfp_t flags)\n

\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5185\u5b58\u5757\u7684\u6307\u9488\uff0c\u81f3\u5c11\u6709size\u5927\u5c0f\u3002\u6240\u5206\u914d\u7684\u5185\u5b58\u533a\u5728\u7269\u7406\u4e0a\u662f\u8fde\u7eed\u7684\u3002\u9664\u975e\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5b58\u53ef\u7528\uff0c\u5426\u5219\u5185\u6838\u603b\u80fd\u5206\u914d\u6210\u529f\u3002\u5f53\u7136\uff0c\u5728\u5bf9kmalloc()\u8c03\u7528\u4e4b\u540e\uff0c\u4f60\u8fd8\u662f\u9700\u8981\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u4e3aNULL\uff1a

struct dog *p;\np = kmalloc(sizeof(struct dog), GFP_KERNEL)\uff1b\nif(!p)\n    /*\u5904\u7406\u9519\u8bef*/\n

\u4e0d\u7ba1\u662f\u5728\u9875\u5206\u914d\u51fd\u6570\u8fd8\u662f\u5728kmalloc()\u4e2d\uff0c\u90fd\u7528\u5230\u4e86\u5206\u914d\u5668\u6807\u5fd7\u3002\u6807\u5fd7\u5206\u4e3a\u4e09\u7c7b\uff1a\u884c\u4e3a\u4fee\u9970\u7b26\u3001\u533a\u4fee\u9970\u7b26\u548c\u7c7b\u578b\u6807\u5fd7\u3002\u7c7b\u578b\u6807\u5fd7\u7ec4\u5408\u4e86\u524d\u4e24\u8005\uff0c\u7b80\u5316\u4e86\u4fee\u9970\u7b26\u7684\u4f7f\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u9700\u8981\u77e5\u9053\u7c7b\u578b\u6807\u5fd7\u5373\u53ef\u3002\u5185\u6838\u4e2d\u6700\u5e38\u7528\u7684\u5c31\u662fGFP_KERNEL\u3002\u8fd9\u79cd\u5206\u914d\u65b9\u5f0f\u53ef\u80fd\u4f1a\u5f15\u8d77\u7761\u7720\uff0c\u6240\u4ee5\u53ea\u80fd\u7528\u5728\u53ef\u4ee5\u91cd\u65b0\u5b89\u5168\u8c03\u5ea6\u7684\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u3002\u53e6\u4e00\u4e2a\u622a\u7136\u76f8\u53cd\u7684\u6807\u5fd7\u662fGFP_ATOMIC\uff0c\u8fd9\u4e2a\u6807\u5fd7\u8868\u793a\u4e0d\u80fd\u7761\u7720\u7684\u5185\u5b58\u5206\u914d\u3002\u4e0eGFP_KERNEL\u76f8\u6bd4\uff0c\u5b83\u5206\u914d\u6210\u529f\u7684\u673a\u4f1a\u8f83\u5c0f\uff0c\u4f46\u662f\u5728\u4e00\u4e9b\u65e0\u6cd5\u7761\u7720\u7684\u4ee3\u7801\u4e2d\uff0c\u4e5f\u53ea\u80fd\u9009\u62e9GFP_ATOMIC\u3002GFP_DMA\u6807\u5fd7\u8868\u793a\u5206\u914d\u5668\u5fc5\u987b\u6ee1\u8db3\u4eceZONE_DMA\u8fdb\u884c\u5206\u914d\u7684\u8bf7\u6c42\uff0c\u8be5\u6807\u5fd7\u7528\u5728\u9700\u8981DMA\u7684\u5185\u5b58\u7684\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u3002\u5728\u7f16\u5199\u7684\u7edd\u5927\u591a\u6570\u4ee3\u7801\u4e2d\uff0c\u8981\u4e48\u662fGFP_KERNEL\uff0c\u8981\u4e48\u662fGFP_ATOMIC\uff0c\u5176\u4ed6\u6807\u5fd7\u7528\u5230\u7684\u60c5\u51b5\u6781\u5c11\uff0c\u5c31\u4e0d\u505a\u8bf4\u660e\u4e86\u3002\u4e0b\u9762\u8fd9\u5f20\u8868\u683c\u603b\u7ed3\u4e86\u6807\u5fd7\u7684\u4f7f\u7528\u573a\u666f\u3002

\u60c5\u5f62 \u76f8\u5e94\u6807\u5fd7 \u53ef\u4ee5\u7761\u7720\u7684\u8fdb\u7a0b\u4e0a\u4e0b\u6587 GFP_KERNEL \u4e0d\u53ef\u4ee5\u7761\u7720\u7684\u8fdb\u7a0b\u4e0a\u4e0b\u6587 GFP_ATOMIC \u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f GFP_ATOMIC \u8f6f\u4e2d\u65ad GFP_ATOMIC tasklet GFP_ATOMIC \u53ef\u4ee5\u7761\u7720\u7684DMA\u5185\u5b58 \uff08GFP_DMA | GFP_KERNEL\uff09 \u4e0d\u53ef\u4ee5\u7761\u7720\u7684DMA\u5185\u5b58 \uff08GFP_DMA\uff09| GFP_ATOMIC

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5c3d\u7ba1kmalloc()\u51fd\u6570\u5728\u6ca1\u6709\u7a7a\u95f2\u5185\u5b58\u65f6\u4f1a\u5931\u8d25\uff0c\u4f46\u662f\u5185\u6838\u603b\u4f1a\u5c3d\u53ef\u80fd\u6ee1\u8db3\u8fd9\u4e2a\u5185\u5b58\u5206\u914d\u8bf7\u6c42\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4e00\u4e0b\u5b50\u5206\u914d\u4e86\u5f88\u591a\u5185\u5b58\uff0c\u7cfb\u7edf\u7684\u54cd\u5e94\u6027\u80fd\u5c31\u4f1a\u964d\u4f4e\u2014\u2014\u5f53\u7cfb\u7edf\u4e3a\u4e86\u6ee1\u8db3kmalloc()\u7684\u5206\u914d\u8bf7\u6c42\uff0c\u4f1a\u8bd5\u56fe\u4e0e\u78c1\u76d8\u4ea4\u6362\u5c3d\u53ef\u80fd\u591a\u7684\u9875\u7a7a\u95f4\u5bfc\u81f4\u7cfb\u7edf\u5361\u6b7b\u3002

\u57fa\u4e8e\u9875\u7684\u5206\u914d\u7b56\u7565\u80fd\u591f\u66f4\u52a0\u6709\u6548\u5730\u5229\u7528\u5185\u5b58\u7a7a\u95f4\uff0c\u800ckmalloc()\u51fd\u6570\u4f1a\u56e0\u4e3a\u5206\u914d\u7c92\u5ea6\u7684\u95ee\u9898\u6d6a\u8d39\u4e00\u5b9a\u6570\u91cf\u7684\u5185\u5b58\u3002

"},{"location":"linux/kernel/mm/#vmalloc","title":"vmalloc()","text":"

vmalloc()\u51fd\u6570\u4e0ekmalloc()\u51fd\u6570\u5728\u5185\u5b58\u5206\u914d\u65b9\u9762\u5177\u6709\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5b83\u4eec\u5728\u7269\u7406\u5730\u5740\u8fde\u7eed\u6027\u65b9\u9762\u6709\u6240\u4e0d\u540c\u3002\u4e0ekmalloc()\u4fdd\u8bc1\u5206\u914d\u7684\u5185\u5b58\u9875\u5728\u7269\u7406\u5730\u5740\u4e0a\u8fde\u7eed\u4e0d\u540c\uff0cvmalloc()\u5206\u914d\u7684\u5185\u5b58\u9875\u5728\u7269\u7406\u5730\u5740\u4e0a\u53ef\u4ee5\u662f\u4e0d\u8fde\u7eed\u7684\u3002\u8fd9\u79cd\u73b0\u8c61\u4e0e\u7528\u6237\u7a7a\u95f4\u5185\u5b58\u5206\u914d\u51fd\u6570malloc()\u7c7b\u4f3c\uff0cmalloc()\u8fd4\u56de\u7684\u9875\u5728\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u662f\u8fde\u7eed\u7684\uff0c\u4f46\u5e76\u4e0d\u4fdd\u8bc1\u8fd9\u4e9b\u9875\u5728\u7269\u7406\u5730\u5740\u4e0a\u4e5f\u662f\u8fde\u7eed\u7684\u3002

\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u786c\u4ef6\u8bbe\u5907\u9700\u8981\u8bbf\u95ee\u7269\u7406\u5730\u5740\u8fde\u7eed\u7684\u5185\u5b58\u9875\u3002\u8fd9\u662f\u56e0\u4e3a\u8bb8\u591a\u4f53\u7cfb\u7ed3\u6784\u7684\u786c\u4ef6\u8bbe\u5907\u5b58\u5728\u4e8e\u5185\u5b58\u7ba1\u7406\u5355\u5143\uff08MMU\uff09\u4e4b\u5916\uff0c\u5b83\u4eec\u65e0\u6cd5\u5904\u7406\u865a\u62df\u5730\u5740\u3002\u56e0\u6b64\uff0c\u786c\u4ef6\u8bbe\u5907\u6240\u4f7f\u7528\u7684\u4efb\u4f55\u5185\u5b58\u533a\u57df\u90fd\u5fc5\u987b\u662f\u7269\u7406\u4e0a\u8fde\u7eed\u7684\u5185\u5b58\u5757\u3002

\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u8f6f\u4ef6\u4f7f\u7528\u7684\u5185\u5b58\u5757\u5219\u6ca1\u6709\u8fd9\u6837\u7684\u4e25\u683c\u8981\u6c42\u3002\u8f6f\u4ef6\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u865a\u62df\u5730\u5740\u6765\u8bbf\u95ee\u5185\u5b58\uff0c\u5373\u4f7f\u8fd9\u4e9b\u865a\u62df\u5730\u5740\u5bf9\u5e94\u7684\u7269\u7406\u5185\u5b58\u4e0d\u662f\u8fde\u7eed\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u9875\u8868\u7ba1\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u5c06\u4e0d\u8fde\u7eed\u7684\u7269\u7406\u5185\u5b58\u6620\u5c04\u5230\u8fde\u7eed\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u3002\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97\u8f6f\u4ef6\u53ef\u4ee5\u9ad8\u6548\u5730\u4f7f\u7528\u5206\u6563\u7684\u7269\u7406\u5185\u5b58\u8d44\u6e90\uff0c\u800c\u65e0\u9700\u5173\u5fc3\u5185\u5b58\u7684\u7269\u7406\u5e03\u5c40\u3002

\u7531\u4e8evmalloc()\u9700\u8981\u5bf9\u4e0d\u8fde\u7eed\u7684\u9875\u8fdb\u884c\u4e13\u95e8\u7684\u6620\u5c04\u5904\u7406\uff0c\u6240\u4ee5\u5176\u6027\u80fd\u4e0d\u5982kmalloc()\u3002\u4ec5\u5728\u4e0d\u5f97\u5df2\u7684\u60c5\u5f62\u4e0b\u624d\u4f1a\u4f7f\u7528\uff0c\u6bd4\u5982\u4e3a\u4e86\u83b7\u5f97\u5927\u5757\u5185\u5b58\u65f6\u3002\u56e0\u4e3akmalloc()\u5206\u914d\u7684\u5185\u5b58\u5757\u662f\u8fde\u7eed\u7684\uff0c\u5f53\u7cfb\u7edf\u4e2d\u7684\u5185\u5b58\u788e\u7247\u8f83\u591a\u65f6\uff0c\u5c1d\u8bd5\u5206\u914d\u5927\u5757\u5185\u5b58\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u800cvmalloc()\u5219\u4e0d\u4f1a\u3002

vmalloc()\u7684\u5206\u914d\u548c\u91ca\u653e\u63a5\u53e3\u5982\u4e0b\uff1a

void *vmalloc(unsinged long size)\n\nvoid vfree(const void *addr)\n

\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u4f1a\u7761\u7720\uff0c\u56e0\u6b64\u4e0d\u80fd\u5728\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u3002

"},{"location":"linux/kernel/mm/#_4","title":"\u4f19\u4f34\u7cfb\u7edf","text":"

\u4f19\u4f34\u7cfb\u7edf\uff08Buddy System\uff09\u662f\u4e00\u79cd\u5185\u5b58\u5206\u914d\u7b97\u6cd5\uff0c\u5b83\u88ab\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u7ba1\u7406\u3002\u5728\u8fd9\u79cd\u7cfb\u7edf\u4e2d\uff0c\u5185\u5b58\u88ab\u5206\u6210\u591a\u4e2a\u5927\u5c0f\u76f8\u7b49\u7684\u5757\uff0c\u6bcf\u4e2a\u5757\u7684\u5927\u5c0f\u90fd\u662f2\u7684\u5e42\u6b21\u65b9\u3002\u8fd9\u4e9b\u5757\u88ab\u7ec4\u7ec7\u6210\u4e00\u9897\u6811\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5757\u90fd\u662f\u53e6\u4e00\u4e2a\u5757\u7684\u201c\u4f19\u4f34\u201d\u3002

\u5185\u6838\u628a\u6240\u6709\u7684\u7a7a\u95f2\u9875\u5206\u7ec4\u4e3a11\u4e2a\u9875\u5757\u94fe\u8868\uff0c\u6bcf\u4e2a\u5757\u94fe\u8868\u5206\u522b\u5305\u542b\u5f88\u591a\u4e2a\u5927\u5c0f\u7684\u9875\u5757\uff0c\u6709 1\u30012\u30014\u30018\u300116\u300132\u300164\u3001128\u3001256\u3001512 \u548c 1024 \u4e2a\u8fde\u7eed\u9875\u7684\u9875\u5757\u3002\u6700\u5927\u53ef\u4ee5\u7533\u8bf71024\u4e2a\u8fde\u7eed\u9875\uff0c\u5bf9\u5e94 4MB\u5927\u5c0f\u7684\u8fde\u7eed\u5185\u5b58\u3002

\u5f53\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u5206\u914d\u5185\u5b58\u65f6\uff0c\u5b83\u4f1a\u6839\u636e\u8bf7\u6c42\u7684\u5927\u5c0f\u627e\u5230\u6700\u63a5\u8fd1\u7684\u4e14\u5927\u5c0f\u8db3\u591f\u7684\u5757\u8fdb\u884c\u5206\u914d\u3002\u5982\u679c\u5206\u914d\u7684\u5757\u6bd4\u6240\u9700\u7684\u5927\u5c0f\u5927\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5757\u4f1a\u88ab\u5206\u6210\u4e24\u4e2a\u5927\u5c0f\u76f8\u7b49\u7684\u5b50\u5757\uff0c\u5176\u4e2d\u4e00\u4e2a\u5b50\u5757\u88ab\u7528\u6765\u6ee1\u8db3\u5185\u5b58\u5206\u914d\u8bf7\u6c42\uff0c\u800c\u53e6\u4e00\u4e2a\u5219\u6210\u4e3a\u5b83\u7684\u201c\u4f19\u4f34\u201d\u3002

\u5728\u4f19\u4f34\u7cfb\u7edf\u4e2d\uff0c\u5f53\u9700\u8981\u5206\u914d\u4e00\u4e2a\u7279\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u5757\u65f6\uff0c\u7cfb\u7edf\u4f1a\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a

  1. \u9996\u5148\uff0c\u786e\u5b9a\u6240\u9700\u5185\u5b58\u5757\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981128\u4e2a\u9875\u7684\u5757\uff0c\u7cfb\u7edf\u4f1a\u68c0\u67e5128\u4e2a\u9875\u7684\u7a7a\u95f2\u5757\u94fe\u8868\u3002

  2. \u5982\u679c\u5728128\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u6ca1\u6709\u627e\u5230\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u5bfb\u627e\u4e0b\u4e00\u4e2a\u66f4\u5927\u7684\u5185\u5b58\u5757\u5c3a\u5bf8\uff0c\u5373256\u4e2a\u9875\u7684\u5757\u3002

  3. \u5982\u679c\u5728256\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u627e\u5230\u4e86\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u5c06\u8fd9\u4e2a\u5757\u5206\u6210\u4e24\u4e2a\u5927\u5c0f\u76f8\u7b49\u7684\u5757\uff0c\u6bcf\u4e2a\u5757\u5305\u542b128\u4e2a\u9875\u3002\u5176\u4e2d\u4e00\u4e2a\u5757\u88ab\u5206\u914d\u7ed9\u8bf7\u6c42\u8005\uff0c\u53e6\u4e00\u4e2a\u5757\u4f5c\u4e3a\u65b0\u7684\u7a7a\u95f2\u5757\u63d2\u5165\u5230128\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u3002

  4. \u5982\u679c256\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u4e5f\u6ca1\u6709\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u7ee7\u7eed\u68c0\u67e5\u66f4\u5927\u7684\u5757\uff0c\u6bd4\u5982512\u4e2a\u9875\u7684\u5757\u3002

  5. \u5982\u679c\u5728512\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u627e\u5230\u4e86\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u5c06\u8fd9\u4e2a\u5757\u5206\u88c2\u6210\u4e09\u4e2a\u5757\uff1a\u4e24\u4e2a128\u4e2a\u9875\u7684\u5757\u548c\u4e00\u4e2a256\u4e2a\u9875\u7684\u5757\u3002\u5176\u4e2d\u4e00\u4e2a128\u4e2a\u9875\u7684\u5757\u88ab\u5206\u914d\u51fa\u53bb\uff0c\u5176\u4f59\u4e24\u4e2a\u5757\u5206\u522b\u63d2\u5165\u5230\u76f8\u5e94\u7684\u94fe\u8868\u4e2d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f19\u4f34\u7cfb\u7edf\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u5185\u5b58\uff0c\u786e\u4fdd\u5185\u5b58\u5206\u914d\u65e2\u9ad8\u6548\u53c8\u7075\u6d3b\u3002

\u5f53\u5185\u5b58\u5757\u88ab\u91ca\u653e\u65f6\uff0c\u7cfb\u7edf\u4f1a\u68c0\u67e5\u662f\u5426\u6709\u76f8\u90bb\u7684\u4f19\u4f34\u5757\u540c\u6837\u7a7a\u95f2\uff0c\u5982\u679c\u4e24\u8005\u90fd\u662f\u7a7a\u95f2\u7684\uff0c\u5b83\u4eec\u4f1a\u88ab\u5408\u5e76\u56de\u539f\u6765\u7684\u5927\u5757\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u788e\u7247\u3002

"},{"location":"linux/kernel/mm/#slab","title":"slab\u5206\u914d\u5668","text":"

\u5728\u5185\u6838\u7f16\u7a0b\u4e2d\uff0c\u5206\u914d\u548c\u91ca\u653e\u6570\u636e\u7ed3\u6784\u662f\u4e00\u9879\u6781\u4e3a\u5e38\u89c1\u7684\u5de5\u4f5c\u3002\u4e3a\u4e86\u9ad8\u6548\u5730\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u7684\u9891\u7e41\u5206\u914d\u4e0e\u56de\u6536\uff0c\u5f00\u53d1\u8005\u4eec\u901a\u5e38\u4f1a\u5229\u7528\u7a7a\u95f2\u94fe\u8868\u6765\u4f18\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u7a7a\u95f2\u94fe\u8868\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u9884\u5148\u5206\u914d\u7684\u6570\u636e\u7ed3\u6784\u5757\u7684\u96c6\u5408\uff0c\u8fd9\u4e9b\u5757\u90fd\u5904\u4e8e\u53ef\u7528\u72b6\u6001\u3002\u5f53\u7cfb\u7edf\u9700\u8981\u65b0\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u4f8b\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u7a7a\u95f2\u94fe\u8868\u4e2d\u83b7\u53d6\u4e00\u4e2a\u73b0\u6210\u7684\u5757\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u5b9e\u65f6\u5185\u5b58\u5206\u914d\u7684\u6d88\u8017\u3002\u4f7f\u7528\u5b8c\u6bd5\u540e\uff0c\u8be5\u6570\u636e\u7ed3\u6784\u5b9e\u4f8b\u88ab\u5f52\u8fd8\u5230\u7a7a\u95f2\u94fe\u8868\u4e2d\uff0c\u800c\u4e0d\u662f\u88ab\u76f4\u63a5\u91ca\u653e\u3002\u5728\u8fd9\u79cd\u673a\u5236\u4e0b\uff0c\u7a7a\u95f2\u94fe\u8868\u5145\u5f53\u4e86\u4e00\u79cd\u7279\u6b8a\u7684\u5bf9\u8c61\u9ad8\u901f\u7f13\u5b58\uff0c\u5b83\u80fd\u591f\u5feb\u901f\u5730\u5b58\u50a8\u548c\u63d0\u4f9b\u90a3\u4e9b\u7ecf\u5e38\u88ab\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u578b\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u4e0d\u4ec5\u63d0\u5347\u4e86\u5185\u5b58\u7ba1\u7406\u7684\u6548\u7387\uff0c\u8fd8\u51cf\u5c11\u4e86\u5185\u5b58\u788e\u7247\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u5b9e\u7528\u7684\u7f16\u7a0b\u6280\u5de7\u3002

Linux\u5185\u6838\u63d0\u4f9b\u4e86slab\u5206\u914d\u5668\uff0c\u5b83\u901a\u8fc7\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u7ec4\u7ec7\u5230\u5404\u81ea\u7684\u9ad8\u901f\u7f13\u5b58\u7ec4\u4e2d\uff0c\u6765\u4f18\u5316\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5206\u914d\u548c\u56de\u6536\u3002\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u7ec4\u4e13\u95e8\u7528\u4e8e\u5b58\u50a8\u4e00\u79cd\u7279\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\u53ef\u80fd\u7528\u4e8e\u7ba1\u7406\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff08task_struct\u7ed3\u6784\uff09\uff0c\u800c\u53e6\u4e00\u4e2a\u5219\u7528\u4e8e\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\uff08struct inode\uff09\u3002

slab\u53ef\u4ee5\u5904\u4e8e\u4e09\u79cd\u72b6\u6001\u4e4b\u4e00\uff1a\u6ee1\u3001\u90e8\u5206\u6ee1\u6216\u7a7a\u3002\u6ee1\u7684slab\u610f\u5473\u7740\u6240\u6709\u5bf9\u8c61\u90fd\u5df2\u5206\u914d\u51fa\u53bb\uff0c\u7a7a\u7684slab\u5219\u8868\u793a\u6240\u6709\u5bf9\u8c61\u90fd\u672a\u88ab\u5206\u914d\uff0c\u800c\u90e8\u5206\u6ee1\u7684slab\u5219\u5305\u542b\u4e86\u5df2\u5206\u914d\u548c\u672a\u5206\u914d\u7684\u5bf9\u8c61\u3002\u5f53\u5185\u6838\u8bf7\u6c42\u65b0\u5bf9\u8c61\u65f6\uff0c\u4f18\u5148\u4ece\u90e8\u5206\u6ee1\u7684slab\u4e2d\u5206\u914d\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5219\u4ece\u7a7a\u7684slab\u4e2d\u5206\u914d\u3002\u5982\u679c\u8fde\u7a7a\u7684slab\u90fd\u6ca1\u6709\uff0c\u5c31\u4f1a\u521b\u5efa\u65b0\u7684slab\u3002

\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u90fd\u4f7f\u7528kmem_cache\u7ed3\u6784\u4f53\u8868\u793a\u3002\u8fd9\u4e2a\u7ed3\u6784\u5305\u542b\u4e09\u4e2a\u94fe\u8868\uff1aslabs_full\u3001slabs_partial\u3001slabs_empty\uff0c\u8fd9\u4e9b\u94fe\u8868\u5305\u542b\u4e86\u9ad8\u901f\u7f13\u5b58\u4e2d\u7684\u6240\u6709slab\u3002struct slab\u7528\u6765\u63cf\u8ff0\u6bcf\u4e2aslab\u3002

slab\u5c42\u7684\u7ba1\u7406\u662f\u5728\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u7684\u57fa\u7840\u4e0a\uff0c\u901a\u8fc7\u5185\u6838\u63d0\u4f9b\u7684\u7edf\u4e00\u63a5\u53e3\u6765\u5b8c\u6210\u7684\u3002\u521b\u5efa\u548c\u64a4\u9500\u9ad8\u901f\u7f13\u5b58\uff0c\u5e76\u5728\u9ad8\u901f\u7f13\u5b58\u5185\u5206\u914d\u548c\u91ca\u653e\u5bf9\u8c61\u3002\u590d\u6742\u7684\u9ad8\u901f\u7f13\u5b58\u673a\u5236\u548cslab\u7684\u7ba1\u7406\u5b8c\u5168\u7531\u5185\u90e8\u673a\u5236\u6765\u5904\u7406\uff0c\u7528\u6237\u65e0\u987b\u5173\u5fc3\u3002

\u9ad8\u901f\u7f13\u5b58\u7684\u4f7f\u7528\u7edf\u8ba1\u60c5\u51b5\u53ef\u4ee5\u4ece/proc/slabinfo\u4e2d\u83b7\u5f97\u3002

struct kmem_cache *kmem_cache_create(const char *name, size_t size,\n                                     size_t align, unsigned int flags,\n                                     void (*ctor)(void *))              //\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9ad8\u901f\u7f13\u5b58\nint kmem_cache_destroy(struct kmem_cache *cachep)                       //\u64a4\u9500\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\nvoid *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)          //\u4ece\u7f13\u5b58\u4e2d\u5206\u914d\u5bf9\u8c61\nvoid kmem_cache_free(struct kmem_cache *cachep, void *objp)             //\u91ca\u653e\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c06\u5b83\u8fd4\u56de\u7ed9\u539f\u5148\u7684slab\n

\u5728\u6587\u4ef6<kernel/fork.c>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8bb8\u591a\u9ad8\u901f\u7f13\u5b58\u7684\u5b9e\u73b0\uff1a

/* SLAB cache for signal_struct structures (tsk->signal) */\nstatic struct kmem_cache *signal_cachep;\n\n/* SLAB cache for sighand_struct structures (tsk->sighand) */\nstruct kmem_cache *sighand_cachep;\n\n/* SLAB cache for files_struct structures (tsk->files) */\nstruct kmem_cache *files_cachep;\n\n/* SLAB cache for fs_struct structures (tsk->fs) */\nstruct kmem_cache *fs_cachep;\n\n/* SLAB cache for vm_area_struct structures */\nstatic struct kmem_cache *vm_area_cachep;\n\n/* SLAB cache for mm_struct structures (tsk->mm) */\nstatic struct kmem_cache *mm_cachep;\n

\u7531\u4e8efork()\u7cfb\u7edf\u8c03\u7528\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684task_struct\u7ed3\u6784\u4f53\uff0c\u4e8e\u662f\u4f7f\u7528\u9ad8\u901f\u7f13\u5b58\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u7cfb\u7edf\u7684\u6027\u80fd\u3002

"},{"location":"linux/kernel/mm/#swap","title":"Swap\u673a\u5236","text":"

\u5f53\u7cfb\u7edf\u5185\u5b58\u8d44\u6e90\u7d27\u5f20\u65f6\uff0c\u5185\u6838\u4f1a\u5bf9\u4e00\u4e9b\u5185\u5b58\u8fdb\u884c\u56de\u6536\u3002\u5bf9\u4e8e\u6587\u4ef6\u9875\uff0c\u5927\u90e8\u5206\u53ef\u4ee5\u76f4\u63a5\u56de\u6536\uff0c\u5982\u679c\u662f\u810f\u9875\u5219\u9700\u8981\u5148\u5199\u5165\u78c1\u76d8\u3002\u5bf9\u4e8e\u533f\u540d\u9875\uff0c\u5185\u6838\u5f15\u5165\u4e86Swap\u673a\u5236\u6765\u56de\u6536\u3002

Swap\u673a\u5236\u662f\u5185\u6838\u4e2d\u7684\u4e00\u79cd\u5185\u5b58\u7ba1\u7406\u6280\u672f\uff0c\u7528\u4e8e\u5904\u7406\u5185\u5b58\u4e0d\u8db3\u7684\u60c5\u51b5\u3002\u5f53\u7cfb\u7edf\u4e2d\u7684\u5b9e\u9645\u7269\u7406\u5185\u5b58\u4e0d\u8db3\u4ee5\u6ee1\u8db3\u7a0b\u5e8f\u8fd0\u884c\u9700\u6c42\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u90e8\u5206\u6682\u65f6\u4e0d\u7528\u7684\u5185\u5b58\u9875\u9762\u79fb\u5230Swap\u7a7a\u95f4\u4e2d\uff0c\u4ece\u800c\u91ca\u653e\u7269\u7406\u5185\u5b58\uff0c\u4ee5\u4fbf\u7ee7\u7eed\u8fd0\u884c\u5176\u4ed6\u7a0b\u5e8f\u3002\u5b83\u5305\u62ec\u6362\u51fa\u548c\u6362\u5165\u4e24\u4e2a\u8fc7\u7a0b\u3002

  • \u6362\u51fa\uff0c\u5c31\u662f\u628a\u8fdb\u7a0b\u6682\u65f6\u4e0d\u7528\u7684\u5185\u5b58\u6570\u636e\u5b58\u50a8\u5230\u78c1\u76d8\u4e2d\uff0c\u5e76\u91ca\u653e\u8fd9\u4e9b\u6570\u636e\u5360\u7528\u7684\u5185\u5b58\u3002
  • \u6362\u5165\uff0c\u5c31\u662f\u5c06\u78c1\u76d8\u4e2d\u7684\u6570\u636e\u91cd\u65b0\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002

\u5f53\u6709\u65b0\u7684\u5927\u5757\u5185\u5b58\u5206\u914d\u8bf7\u6c42\u800c\u5269\u4f59\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u5185\u6838\u5c31\u4f1a\u56de\u6536\u4e00\u90e8\u5206\u5185\u5b58\uff0c\u5c3d\u53ef\u80fd\u5730\u6ee1\u8db3\u65b0\u5185\u5b58\u8bf7\u6c42\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u76f4\u63a5\u5185\u5b58\u56de\u6536\u3002

\u9664\u4e86\u76f4\u63a5\u5185\u5b58\u56de\u6536\uff0c\u8fd8\u6709\u4e00\u4e2a\u4e13\u95e8\u7684\u5185\u6838\u7ebf\u7a0bkswapd0\u7528\u6765\u5b9a\u671f\u56de\u6536\u5185\u5b58\u3002\u4e3a\u4e86\u8861\u91cf\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u5185\u6838\u5b9a\u4e49\u4e86\u4e09\u4e2a\u5185\u5b58\u9608\u503c\uff0c\u5206\u522b\u662f\u9875\u6700\u5c0f\u9608\u503c\uff08pages_min\uff09\u3001\u9875\u4f4e\u9608\u503c\uff08pages_low\uff09\u548c\u9875\u9ad8\u9608\u503c\uff08pages_high\uff09\uff0c\u5269\u4f59\u5185\u5b58\uff0c\u5219\u7528pages_free\u8868\u793a\u3002

\u4e00\u65e6\u5269\u4f59\u5185\u5b58\u5c0f\u4e8e\u9875\u4f4e\u9608\u503c\uff0c\u5c31\u4f1a\u89e6\u53d1\u5185\u5b58\u7684\u56de\u6536\u3002

\u5728NUMA\u67b6\u6784\u4e0b\uff0c\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u4f1a\u88ab\u5212\u5206\u5230\u4e0d\u540c\u7684Node\u4e0b\uff0c\u800c\u6bcf\u4e2aNode\u90fd\u6709\u81ea\u5df1\u7684\u672c\u5730\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u5185\u6838\u9700\u8981\u6839\u636e\u5f53\u524dNode\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u6765\u51b3\u5b9a\u56de\u6536\u7684\u76ee\u6807\u3002\u5f53\u67d0\u4e2aNode\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u5185\u6838\u53ef\u4ee5\u4ece\u5176\u4ed6Node\u5bfb\u627e\u7a7a\u95f2\u5185\u5b58\uff0c\u4e5f\u53ef\u4ee5\u4ece\u672c\u5730\u56de\u6536\u5185\u5b58\uff0c\u5177\u4f53\u56de\u6536\u6a21\u5f0f\u5728/proc/sys/vm/zone_reclaim_mode\u6587\u4ef6\u4e2d\u8fdb\u884c\u914d\u7f6e\u3002

/proc/sys/vm/swappiness\u9009\u9879\u7528\u6765\u8c03\u6574\u4f7f\u7528Swap\u7684\u79ef\u6781\u7a0b\u5ea6\uff0c\u4ece0~100\uff0c\u6570\u503c\u8d8a\u5927\u8d8a\u79ef\u6781\u4f7f\u7528Swap\uff0c\u4e5f\u5c31\u662f\u66f4\u503e\u5411\u4e8e\u56de\u6536\u533f\u540d\u9875\uff1b\u6570\u503c\u8d8a\u5c0f\u8d8a\u6d88\u6781\u4f7f\u7528Swap\uff0c\u4e5f\u5c31\u662f\u66f4\u503e\u5411\u4e8e\u56de\u6536\u6587\u4ef6\u9875\u3002

"},{"location":"linux/kernel/network/","title":"\u7f51\u7edc\u7cfb\u7edf","text":""},{"location":"linux/kernel/others/","title":"\u5176\u4ed6","text":"

\u8fd9\u91cc\u4e3b\u8981\u653e\u4e00\u4e9b\u96f6\u788e\u7684\u5185\u5bb9\u3002

"},{"location":"linux/kernel/others/#_2","title":"\u7cfb\u7edf\u8c03\u7528","text":"

\u5f53\u4f60\u7684\u7a0b\u5e8f\u8c03\u7528open, read, write, close\u7b49\u51fd\u6570\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1\u7cfb\u7edf\u8c03\u7528\uff08syscall\uff09\u3002\u7cfb\u7edf\u8c03\u7528\u672c\u8d28\u662f\u7528\u6237\u6001\u8fdb\u7a0b\u4e0e\u786c\u4ef6\u8bbe\u5907\u4ea4\u4e92\u7684\u63a5\u53e3\uff0c\u5185\u6838\u8d1f\u8d23\u68c0\u67e5\u7cfb\u7edf\u8c03\u7528\u7684\u6b63\u786e\u6027\uff0c\u7136\u540e\u53d1\u51fa\u6307\u4ee4\u7ed9\u786c\u4ef6\u3002\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u8005\uff0c\u4e0d\u7528\u5173\u5fc3\u5e95\u5c42\u786c\u4ef6\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u800c\u53ea\u9700\u8981\u8c03\u7528\u666e\u901a\u51fd\u6570\u5c31\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\u4e86\u3002glibc\u5e93\u8fdb\u4e00\u6b65\u5c01\u88c5\u4e86\u7ec6\u8282\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528glibc\u5e93\u66b4\u9732\u7684API\u5373\u53ef\u3002

x86\u4f53\u7cfb\u8fdb\u5165\u548c\u9000\u51fa\u7cfb\u7edf\u8c03\u7528\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a

  • int $0x80 \u548c iret
  • sysenter \u548c sysexit

\u7b2c\u4e8c\u79cd\u88ab\u79f0\u4e3a\u5feb\u901f\u7cfb\u7edf\u8c03\u7528\u3002

\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u6700\u7ec8\u7ed3\u679c\u90fd\u662f\u8df3\u8f6c\u5230\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u51fd\u6570\uff08system call handler\uff09\u3002\u7531\u4e8e\u5185\u6838\u5b9e\u73b0\u4e86\u5f88\u591a\u4e0d\u540c\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u56e0\u6b64\u8fdb\u7a0b\u5fc5\u987b\u4f20\u9012\u4e00\u4e2a\u540d\u4e3a\u7cfb\u7edf\u8c03\u7528\u53f7\uff08system call number\uff09\u7684\u53c2\u6570\u6765\u8bc6\u522b\u6240\u9700\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u8fd9\u4e2a\u53c2\u6570\u5b58\u653e\u5728eax\u5bc4\u5b58\u5668\u4e2d\u3002\u6267\u884c\u5b8c\u7cfb\u7edf\u8c03\u7528\u540e\u7684\u8fd4\u56de\u503c\u4e5f\u653e\u5728eax\u5bc4\u5b58\u5668\u4e2d\uff0c\u5176\u4e2d\u6b63\u6570\u62160\u8868\u793a\u7cfb\u7edf\u8c03\u7528\u6210\u529f\uff0c\u8d1f\u6570\u8868\u793a\u51fa\u9519\uff0c\u5b58\u653e\u4e8eerrno\u5168\u5c40\u53d8\u91cf\u4e2d\u3002

\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u6d41\u7a0b\u662f\uff1a

  • \u5c06\u7cfb\u7edf\u8c03\u7528\u7684\u53c2\u6570\u5199\u5165CPU\u5bc4\u5b58\u5668
  • \u68c0\u67e5\u6240\u6709\u7684\u7cfb\u7edf\u8c03\u7528\u53c2\u6570
  • \u5c06CPU\u4e2d\u7684\u53c2\u6570\u62f7\u8d1d\u81f3\u5185\u6838\u6001\u5806\u6808
  • \u8c03\u7528\u540d\u4e3a\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\uff08system call service routine\uff09\u7684C\u51fd\u6570\u6765\u5904\u7406\u7cfb\u7edf\u8c03\u7528
  • \u9000\u51fa\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\uff0c\u5c06\u5185\u6838\u6808\u4e2d\u7684\u503c\u52a0\u8f7d\u81f3\u5bc4\u5b58\u5668\uff0c\u5e76\u4ece\u5185\u6838\u6001\u5207\u6362\u56de\u7528\u6237\u6001

\u4e3a\u4e86\u5c06\u7cfb\u7edf\u8c03\u7528\u53f7\u4e0e\u5bf9\u5e94\u7684\u670d\u52a1\u4f8b\u7a0b\u8054\u7cfb\u8d77\u6765\uff0c\u5185\u6838\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u5206\u6d3e\u8868\uff08system call dispatch table\uff09\uff0c\u8fd9\u4e2a\u8868\u5b58\u653e\u5728sys_call_table\u6570\u7ec4\u4e2d\u3002\u5185\u6838\u62ff\u5230\u7cfb\u7edf\u8c03\u7528\u53f7\u4e4b\u540e\uff0c\u5c31\u53bbsys_call_table\u4e2d\u627e\u5230\u5bf9\u5e94\u7684\u7cfb\u7edf\u8c03\u7528\u5b9e\u73b0\u51fd\u6570\u53bb\u6267\u884c\u3002\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f7f\u7528\u8fd4\u56de\u6307\u4ee4\u4ece\u5185\u6838\u6001\u8fd4\u56de\u81f3\u7528\u6237\u6001\u3002

\u5185\u6838\u5728\u6267\u884c\u7cfb\u7edf\u8c03\u7528\u7684\u65f6\u5019\u5904\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u3002current\u6307\u9488\u6307\u5411\u5f53\u524d\u4efb\u52a1\uff0c\u5373\u5f15\u53d1\u7cfb\u7edf\u8c03\u7528\u7684\u90a3\u4e2a\u8fdb\u7a0b\u3002\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u5185\u6838\u53ef\u4ee5\u4f11\u7720\u5e76\u4e14\u53ef\u4ee5\u88ab\u62a2\u5360\u3002\u56e0\u6b64\u5fc5\u987b\u4fdd\u8bc1\u7cfb\u7edf\u8c03\u7528\u65f6\u53ef\u91cd\u5165\u7684\uff0c\u56e0\u4e3a\u65b0\u7684\u8fdb\u7a0b\u53ef\u80fd\u4f1a\u4f7f\u7528\u76f8\u540c\u7684\u7cfb\u7edf\u8c03\u7528\u3002

"},{"location":"linux/kernel/others/#_3","title":"\u4fe1\u53f7","text":"

\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u54cd\u5e94\u5404\u79cd\u4e8b\u4ef6\u5b9a\u4e49\u4e86\u975e\u5e38\u591a\u7684\u4fe1\u53f7\u3002\u6bd4\u5982\u5f53\u6211\u4eec\u53d1\u9001kill -9 ${pid}\u65f6\uff0c\u5176\u5b9e\u5c31\u662f\u53d1\u9001SIGKILL\u4fe1\u53f7\u7ed9\u6307\u5b9a\u8fdb\u7a0b\uff0c\u5c06\u5b83\u6740\u6b7b\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7kill -l\u547d\u4ee4\u67e5\u770b\u6240\u6709\u7684\u4fe1\u53f7\u3002\u6bcf\u4e2a\u4fe1\u53f7\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684ID\u548c\u5bf9\u5e94\u7684\u9ed8\u8ba4\u64cd\u4f5c\u3002

\u8fdb\u7a0b\u5bf9\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a

  1. \u6267\u884c\u9ed8\u8ba4\u64cd\u4f5c\u3002
  2. \u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002
  3. \u5ffd\u7565\u4fe1\u53f7\u3002\u6ce8\u610f\uff0cSIGKILL\u548cSIGSTOP\u65e0\u6cd5\u5ffd\u7565\u3002

Linux\u63a8\u8350\u4f7f\u7528sigaction()\u51fd\u6570\u6765\u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);\n

\u800csigaction\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

struct sigaction {\n  __sighandler_t sa_handler;\n  unsigned long sa_flags;\n  __sigrestore_t sa_restorer;\n  sigset_t sa_mask;   \n};\n

\u5176\u4e2dsa_handler\u5c31\u662f\u4f60\u8981\u5b9a\u4e49\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002

\u53d1\u9001\u4fe1\u53f7\u7531\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u53d1\u9001\u7ed9\u6574\u4e2a\u7ebf\u7a0b\u7ec4\u7684\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u53d1\u9001\u7ed9\u67d0\u4e2a\u5355\u72ec\u7ebf\u7a0b\u7684\u3002\u4fe1\u53f7\u5206\u4e3a\u4e0d\u53ef\u9760\u4fe1\u53f7\u548c\u53ef\u9760\u4fe1\u53f7\u3002\u5728task_struct\u4e2d\u6709\u4e00\u4e2a\u7ed3\u6784\u4f53sigpending\uff0c\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

struct sigpending{\n    struct list_head list;\n    sigset_t signal;\n};\n

\u5bf9\u4e8e\u4e0d\u53ef\u9760\u4fe1\u53f7\uff0c\u4e5f\u5c31\u662f\u7f16\u53f7\u5c0f\u4e8e32\u7684\u4fe1\u53f7\uff0c\u4f1a\u653e\u5728sigset_t\u96c6\u5408\u4e2d\uff0c\u4e0d\u8bba\u53d1\u9001\u591a\u5c11\u6b21\uff0c\u5728\u88ab\u5904\u7406\u524d\u90fd\u53ea\u4f1a\u4fdd\u7559\u4e00\u4efd\u3002\u5bf9\u4e8e\u53ef\u9760\u4fe1\u53f7\uff0c\u5219\u4f1a\u6302\u5728struct sigpending\u7684\u94fe\u8868\u4e2d\u6328\u4e2a\u5904\u7406\u3002

"},{"location":"linux/kernel/others/#_4","title":"\u5b9a\u65f6\u5668","text":"

\u5185\u6838\u4e2d\u6709\u5927\u91cf\u7684\u51fd\u6570\u662f\u57fa\u4e8e\u65f6\u95f4\u9a71\u52a8\u7684\u3002\u6709\u4e9b\u51fd\u6570\u9700\u8981\u5468\u671f\u6027\u6267\u884c\uff0c\u6709\u4e9b\u51fd\u6570\u9700\u8981\u7b49\u5f85\u4e00\u4e2a\u76f8\u5bf9\u65f6\u95f4\u540e\u624d\u8fd0\u884c\u3002\u5468\u671f\u6027\u7684\u4e8b\u4ef6\u7531\u7cfb\u7edf\u5b9a\u65f6\u5668\u9a71\u52a8\u3002\u7cfb\u7edf\u5b9a\u65f6\u5668\u80fd\u4ee5\u56fa\u5b9a\u7684\u9891\u7387\u4ea7\u751f\u4e2d\u65ad\uff0c\u8fd9\u79cd\u4e2d\u65ad\u88ab\u79f0\u4e3a\u5b9a\u65f6\u5668\u4e2d\u65ad\uff0c\u5b83\u6240\u5bf9\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8d1f\u8d23\u66f4\u65b0\u7cfb\u7edf\u65f6\u95f4\u548c\u6267\u884c\u5468\u671f\u6027\u4efb\u52a1\u3002

\u7cfb\u7edf\u5b9a\u65f6\u5668\u7684\u9891\u7387\u88ab\u79f0\u4e3a\u8282\u62cd\u7387\uff08tick rate\uff09\uff0c\u4ea7\u751f\u7684\u4e24\u6b21\u65f6\u949f\u4e2d\u65ad\u7684\u95f4\u9694\u5c31\u88ab\u79f0\u4e3a\u8282\u62cd\uff08tick\uff09\uff0c\u5b83\u7b49\u4e8e\u8282\u62cd\u7387\u5206\u4e4b\u4e00\u79d2\u3002\u5185\u6838\u901a\u8fc7\u65f6\u949f\u4e2d\u65ad\u95f4\u9694\u6765\u8ba1\u7b97\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4\u3002\u65f6\u949f\u4e2d\u65ad\u9700\u8981\u6267\u884c\u7684\u64cd\u4f5c\u6709\uff1a

  • \u66f4\u65b0\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4
  • \u66f4\u65b0\u5b9e\u9645\u65f6\u95f4
  • \u5728SMP\u7cfb\u7edf\u4e0a\uff0c\u5747\u8861\u8c03\u5ea6\u7a0b\u5e8f\u4e2d\u5404\u5904\u7406\u5668\u4e0a\u7684\u8fd0\u884c\u961f\u5217
  • \u68c0\u67e5\u5f53\u524d\u8fdb\u7a0b\u662f\u5426\u7528\u5c3d\u4e86\u81ea\u5df1\u7684\u65f6\u95f4\u7247
  • \u8fd0\u884c\u8d85\u65f6\u7684\u52a8\u6001\u5b9a\u65f6\u5668
  • \u66f4\u65b0\u8d44\u6e90\u6d88\u8017\u548c\u5904\u7406\u5668\u65f6\u95f4\u7684\u7edf\u8ba1\u503c
"},{"location":"linux/kernel/others/#jiffies","title":"\u8282\u62cd\u7387\u548cjiffies\u53d8\u91cf","text":"

\u7cfb\u7edf\u5b9a\u65f6\u5668\u9891\u7387\u662f\u901a\u8fc7\u9759\u6001\u9884\u5904\u7406\u5b9a\u4e49\u7684\uff0c\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u6309\u7167HZ\u7684\u503c\u5bf9\u786c\u4ef6\u8fdb\u884c\u8bbe\u7f6e\uff0c\u8be5\u503c\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u3002\u5185\u6838\u5728<asm/param.h>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002\u5728x86\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u7cfb\u7edf\u5b9a\u65f6\u5668\u9891\u7387\u9ed8\u8ba4\u4e3a1000\u3002\u4e5f\u5c31\u662f\u8bf4x86\u7684\u65f6\u949f\u4e2d\u65ad\u9891\u7387\u4e3a1000HZ\uff0c\u5373\u6bcf\u79d2\u4e2d\u65ad1000\u6b21\uff08\u6bcf1ms\u4ea7\u751f\u4e00\u6b21\uff09\u3002

\u65f6\u949f\u4e2d\u65ad\u9891\u7387\u5e94\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u7406\u60f3\u7684\u503c\uff0c\u9ad8HZ\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u4f7f\u5f97\u7531\u65f6\u95f4\u9a71\u52a8\u7684\u4e8b\u4ef6\u66f4\u4e3a\u7cbe\u786e\u3002\u4f46\u540c\u65f6\u4e5f\u5e26\u6765\u4e86\u989d\u5916\u7684\u7cfb\u7edf\u8d1f\u62c5\uff0c\u56e0\u4e3a\u5904\u7406\u5668\u4f1a\u88ab\u66f4\u9891\u7e41\u5730\u6253\u65ad\u53bb\u6267\u884c\u65f6\u949f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002

\u5168\u5c40\u53d8\u91cfjiffies\u7528\u6765\u8bb0\u5f55\u7cfb\u7edf\u542f\u52a8\u4ee5\u6765\u4ea7\u751f\u7684\u8282\u62cd\u603b\u6570\u3002\u5185\u6838\u5728\u542f\u52a8\u65f6\u5c06\u8be5\u53d8\u91cf\u521d\u59cb\u5316\u4e3a0\uff0c\u6b64\u540e\u6bcf\u4ea7\u751f\u4e00\u6b21\u65f6\u949f\u4e2d\u65ad\u8be5\u503c\u5c31+1\u3002\u56e0\u4e3a\u4e00\u79d2\u5185\u65f6\u949f\u4e2d\u65ad\u7684\u6b21\u6570\u7b49\u4e8eHZ\uff0c\u6240\u4ee5jiffies\u4e00\u79d2\u5185\u589e\u52a0\u7684\u503c\u4e5f\u5c31\u4e3aHZ\u3002\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4\u4ee5\u79d2\u4e3a\u8ba1\uff0c\u5176\u503c\u7b49\u4e8ejiffies/HZ\u3002

jiffies\u53d8\u91cf\u88ab\u5b9a\u4e49\u4e3aunsigned long\u7c7b\u578b\u3002\u572832\u4f4d\u4f53\u7cfb\u7ed3\u6784\u4e0a\u662f32\u4f4d\u3002\u5982\u679c\u65f6\u949f\u9891\u7387\u4e3a100HZ\uff0c\u90a3\u4e48497\u5929\u540e\u4f1a\u6ea2\u51fa\u3002\u5982\u679c\u662f64\u4f4d\u4f53\u7cfb\u7ed3\u6784\uff0c\u4efb\u4f55\u4eba\u90fd\u522b\u6307\u671b\u4f1a\u770b\u5230\u5b83\u6ea2\u51fa\u3002

\u5f53jiffies\u53d8\u91cf\u7684\u503c\u8d85\u8fc7\u5b83\u7684\u6700\u5927\u5b58\u653e\u8303\u56f4\u540e\u5c31\u4f1a\u53d1\u751f\u6ea2\u51fa\uff0c\u6b64\u65f6\u518d\u7ee7\u7eed\u589e\u52a0\u7684\u8bdd\uff0c\u5b83\u4f1a\u56de\u7ed5\u81f30\u3002\u4e3a\u4e86\u89e3\u51b3jiffies\u53d8\u91cf\u7684\u6ea2\u51fa\u95ee\u9898\uff0c\u5185\u6838\u5f15\u5165\u4e86jiffies_64\u53d8\u91cf\uff0c\u8fd9\u662f\u4e00\u4e2a64\u4f4d\u7684\u65e0\u7b26\u53f7\u6574\u6570\u3002\u4f46\u662f\u7528\u6237\u7a7a\u95f4\u8bbf\u95ee\u7684\u4f9d\u7136\u8fd8\u662fjiffies\u53d8\u91cf\u3002

"},{"location":"linux/kernel/others/#_5","title":"\u52a8\u6001\u5b9a\u65f6\u5668","text":"

\u5b9a\u65f6\u5668\u7684\u4f7f\u7528\u5f88\u7b80\u5355\u3002\u4f60\u53ea\u9700\u8981\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\uff0c\u6307\u5b9a\u8d85\u65f6\u53d1\u751f\u540e\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\uff0c\u7136\u540e\u6fc0\u6d3b\u5b9a\u65f6\u5668\u5c31\u53ef\u4ee5\u4e86\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7ec4\u4e0e\u5b9a\u65f6\u5668\u76f8\u5173\u7684\u63a5\u53e3\u7528\u6765\u7b80\u5316\u5b9a\u65f6\u5668\u7684\u64cd\u4f5c\u3002

\u521b\u5efa\u5b9a\u65f6\u5668\uff1a

struct timer_list my_timer;\n

\u521d\u59cb\u5316\u5b9a\u65f6\u5668\uff1a

init_timer(&my_timer);\n

\u586b\u5145\u5b9a\u65f6\u5668\u7ed3\u6784\u4e2d\u7684\u503c\uff1a

my_timer.expires = jiffies + delay;    //\u5b9a\u65f6\u5668\u8d85\u65f6\u8282\u62cd\u6570\nmy_timer.data = 0;                     //\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\u4f20\u5165\u7684\u503c\nmy_timer.function = my_function;       //\u5b9a\u65f6\u5668\u8d85\u65f6\u56de\u8c03\u51fd\u6570\n

\u5982\u679c\u5f53\u524djiffies\u8ba1\u6570\u5927\u4e8e\u6216\u7b49\u4e8emy_timer.expires\u7684\u503c\uff0c\u90a3\u4e48my_timer.function\u6307\u5411\u7684\u5904\u7406\u51fd\u6570\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5904\u7406\u51fd\u6570\u5fc5\u987b\u7b26\u5408\u4ee5\u4e0b\u7c7b\u578b\uff1a

void my_function(unsigned long data);\n

\u5982\u679c\u9700\u8981\u901a\u8fc7data\u53c2\u6570\u4f20\u9012\u591a\u4e2a\u6570\u636e\u9879\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6570\u636e\u9879\u6346\u7ed1\u6210\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c06\u7ed3\u6784\u4f53\u7684\u6307\u9488\u5f3a\u5236\u8f6c\u6362\u6210unsigned long\u4f20\u5165\u3002

\u6700\u540e\uff0c\u4f60\u5fc5\u987b\u624b\u52a8\u6fc0\u6d3b\u5b9a\u65f6\u5668\uff1a

add_timer(&my_timer);\n

\u4e00\u822c\u6765\u8bf4\uff0c\u5b9a\u65f6\u5668\u90fd\u4f1a\u5728\u8d85\u65f6\u540e\u9a6c\u4e0a\u6267\u884c\uff0c\u4f46\u4e5f\u6709\u53ef\u80fd\u4f1a\u63a8\u8fdf\u5230\u4e0b\u4e00\u4e2a\u65f6\u949f\u8282\u62cd\u65f6\u624d\u8fd0\u884c\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528\u5b9a\u65f6\u5668\u6765\u5b9e\u73b0\u4efb\u4f55\u786c\u5b9e\u65f6\u7684\u4efb\u52a1\u3002\u5982\u679c\u9700\u8981\u4fee\u6539\u5b9a\u65f6\u5668\u8d85\u65f6\u65f6\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7mod_timer()\u51fd\u6570\u6765\u5b9e\u73b0\uff1a

mod_timer(&my_timer, jiffies + new_delay);\n

\u5982\u679c\u5728\u5b9a\u65f6\u5668\u8d85\u65f6\u524d\u505c\u6b62\u5b9a\u65f6\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528del_timer()\u51fd\u6570\uff1a

del_timer(&my_timer);\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728SMP\u7cfb\u7edf\u4e2d\uff0c\u5220\u9664\u5b9a\u65f6\u5668\u65f6\u53ef\u80fd\u9700\u8981\u7b49\u5f85\u5728\u5176\u4ed6\u5904\u7406\u5668\u4e0a\u8fd0\u884c\u7684\u5b9a\u65f6\u5668\u5904\u7406\u7a0b\u5e8f\u90fd\u9000\u51fa\uff0c\u8fd9\u65f6\u9700\u8981\u7528\u5230del_timer_syn()\u51fd\u6570\u6765\u6267\u884c\u5220\u9664\u5de5\u4f5c\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u5e94\u8be5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u800c\u4e0d\u662fdel_timer()\u3002\u5728\u62e5\u6709\u9501\u7684\u65f6\u5019\uff0c\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u68c0\u67e5\uff0c\u4ee5\u9632\u6b62\u6b7b\u9501\u3002

"},{"location":"linux/kernel/others/#_6","title":"\u5ef6\u8fdf\u6267\u884c","text":"

\u5185\u6838\u4ee3\u7801\u5f80\u5f80\u9700\u8981\u63a8\u8fdf\u67d0\u4e9b\u4efb\u52a1\u7684\u6267\u884c\uff0c\u8fd9\u79cd\u63a8\u8fdf\u901a\u5e38\u53d1\u751f\u5728\u7b49\u5f85\u786c\u4ef6\u5b8c\u6210\u67d0\u4e9b\u5de5\u4f5c\uff0c\u800c\u4e14\u7b49\u5f85\u65f6\u95f4\u5f80\u5f80\u975e\u5e38\u77ed\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u591a\u79cd\u5ef6\u8fdf\u65b9\u6cd5\u6765\u5904\u7406\u5ef6\u8fdf\u8bf7\u6c42\u3002

\u6700\u7b80\u5355\u7684\u5ef6\u8fdf\u65b9\u6cd5\u5c31\u662f\u5fd9\u7b49\u5f85\u3002\u8be5\u65b9\u6cd5\u4ec5\u4ec5\u5728\u60f3\u8981\u5ef6\u8fdf\u7684\u65f6\u95f4\u662f\u8282\u62cd\u7684\u6574\u6570\u500d\uff0c\u6216\u8005\u7cbe\u786e\u7387\u8981\u6c42\u4e0d\u9ad8\u7684\u60c5\u51b5\u4e0b\u624d\u53ef\u4ee5\u4f7f\u7528\u3002\u5fd9\u7b49\u5f85\u7684\u5b9e\u73b0\u975e\u5e38\u7b80\u5355\u2014\u2014\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u4e0d\u65ad\u7b49\u5f85\u76f4\u5230\u5e0c\u671b\u7684\u65f6\u949f\u8282\u62cd\u6570\u8017\u5c3d\uff0c\u6bd4\u5982\uff1a

unsigned long timeout = jiffies + 10;    //\u7b49\u5f8510\u4e2a\u8282\u62cd\nwhile(time_before(jiffies, timeout))\n{}\n

\u8be5\u5faa\u73af\u5c06\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230jiffies\u5927\u4e8edelay\u4e3a\u6b62\u3002\u8fd9\u662f\u4e00\u79cd\u4f4e\u6548\u7684\u529e\u6cd5\uff0c \u56e0\u4e3a\u5904\u7406\u5668\u9664\u4e86\u7b49\u5f85\u4e0d\u4f1a\u505a\u4efb\u4f55\u4e8b\u60c5\uff0c\u66f4\u597d\u7684\u65b9\u6848\u662f\u5728\u7b49\u5f85\u65f6\uff0c\u5141\u8bb8\u5185\u6838\u91cd\u65b0\u8c03\u5ea6\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002

\u57fa\u4e8ejiffies\u7684\u5ef6\u8fdf\u65b9\u6cd5\u53d7\u9650\u4e8e\u65f6\u949f\u8282\u62cd\uff0c\u65e0\u6cd5\u63d0\u4f9b\u66f4\u77ed\u3001\u66f4\u7cbe\u786e\u7684\u5ef6\u8fdf\u8981\u6c42\u3002\u4e3a\u6b64\uff0c\u5185\u6838\u63d0\u4f9b\u4e86\u4e09\u4e2a\u53ef\u4ee5\u5904\u7406us\u3001us\u548cms\u7ea7\u522b\u7684\u77ed\u5ef6\u8fdf\u51fd\u6570\uff1a

void ndelay(unsigned long nsecs)\nvoid udelay(unsigned long usecs)\nvoid mdelay(unsigned long msecs)\n

\u8fd9\u4e9b\u51fd\u6570\u7684\u5b9e\u9645\u5b9e\u73b0\u5728<asm/delay.h>\u4e2d\uff0c\u5f88\u660e\u663e\uff0c\u8fd9\u4e9b\u662f\u4e0e\u4f53\u7cfb\u67b6\u6784\u76f8\u5173\u7684\u51fd\u6570\u3002\u8fd9\u4e09\u4e2a\u51fd\u6570\u90fd\u662f\u5fd9\u7b49\u5f85\u51fd\u6570\uff0c\u5728\u5ef6\u8fdf\u8fc7\u7a0b\u4e2d\u65e0\u6cd5\u8fd0\u884c\u5176\u4ed6\u4efb\u52a1\u3002\u4e0d\u6d89\u53ca\u5fd9\u7b49\u5f85\u7684\u51fd\u6570\u6709\u4ee5\u4e0b\u4e24\u4e2a\uff0c\u5b83\u4eec\u4f1a\u5c06\u5f53\u524d\u8fdb\u7a0b\u7f6e\u5165\u4f11\u7720\u72b6\u6001\uff1a

void msleep(unsigned int millisecs);\nvoid ssleep(unsigned int seconds);\n

\u66f4\u7406\u60f3\u7684\u5ef6\u8fdf\u6267\u884c\u65b9\u6cd5\u662f\u4f7f\u7528schedule_timeout()\u51fd\u6570\uff0c\u8be5\u65b9\u6cd5\u8ba9\u9700\u8981\u5ef6\u8fdf\u6267\u884c\u7684\u4efb\u52a1\u7761\u7720\u5230\u6307\u5b9a\u5ef6\u8fdf\u65f6\u95f4\u8017\u5c3d\u540e\u518d\u91cd\u65b0\u8fd0\u884c\u3002\u5176\u7528\u6cd5\u5982\u4e0b\uff1a

set_current_state(TASK_INTERRUPTIBLE);\nschedule_timeout(s * HZ);\n

\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5f53\u524d\u4efb\u52a1\u63a8\u5165\u53ef\u4e2d\u65ad\u7761\u7720\u961f\u5217\uff0c\u7761\u7720s\u79d2\u540e\u5524\u9192\u3002\u6ce8\u610f\uff0c\u7531\u4e8eschedule_timeout()\u51fd\u6570\u9700\u8981\u8c03\u7528\u8c03\u5ea6\u7a0b\u5e8f\uff0c\u6240\u4ee5\u8c03\u7528\u5b83\u7684\u4ee3\u7801\u5fc5\u987b\u4fdd\u8bc1\u80fd\u591f\u7761\u7720\u3002\u4e5f\u5c31\u662f\u8c03\u7528\u51fd\u6570\u5fc5\u987b\u4f4d\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4e14\u4e0d\u80fd\u6301\u6709\u9501\u3002

\u5f53\u4efb\u52a1\u88ab\u91cd\u65b0\u8c03\u5ea6\u65f6\uff0c\u5c06\u8fd4\u56de\u4ee3\u7801\u8fdb\u5165\u7761\u7720\u524d\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u5982\u679c\u4efb\u52a1\u63d0\u524d\u88ab\u5524\u9192\uff0c\u90a3\u4e48\u5b9a\u65f6\u5668\u88ab\u64a4\u9500\u3002

"},{"location":"linux/kernel/others/#_7","title":"\u9875\u9ad8\u901f\u7f13\u5b58\u4e0e\u9875\u56de\u5199","text":"

\u9875\u9ad8\u901f\u7f13\u5b58\u662f\u4e00\u79cd\u628a\u78c1\u76d8\u7684\u6570\u636e\u7f13\u5b58\u5230\u7269\u7406\u5185\u5b58\u4e2d\u7684\u673a\u5236\uff0c\u4f7f\u5f97\u5bf9\u78c1\u76d8\u7684\u8bbf\u95ee\u53d8\u4e3a\u5bf9\u7269\u7406\u5185\u5b58\u7684\u8bbf\u95ee\u3002\u9ad8\u901f\u7f13\u5b58\u7684\u91cd\u8981\u6027\u4f53\u73b0\u5728\uff1a

  1. \u8bbf\u95ee\u78c1\u76d8\u7684\u901f\u5ea6\u8fdc\u8fdc\u4f4e\u4e8e\u8bbf\u95ee\u5185\u5b58\u7684\u901f\u5ea6\u3002

  2. \u6570\u636e\u4e00\u65e6\u88ab\u8bbf\u95ee\uff0c\u5c31\u5f88\u6709\u53ef\u80fd\u5728\u77ed\u671f\u5185\u518d\u6b21\u88ab\u8bbf\u95ee\u3002\u8fd9\u5c31\u662f\u5c40\u90e8\u6027\u539f\u7406\u3002

\u5f53\u5185\u6838\u5f00\u59cb\u4e00\u4e2a\u8bfb\u64cd\u4f5c\uff0c\u5b83\u9996\u5148\u4f1a\u68c0\u67e5\u9700\u8981\u7684\u6570\u636e\u662f\u5426\u5728\u9875\u9ad8\u901f\u7f13\u5b58\u4e2d\u3002\u5982\u679c\u5728\uff0c\u653e\u5f03\u8bbf\u95ee\u78c1\u76d8\uff0c\u800c\u76f4\u63a5\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\u3002\u8fd9\u4e2a\u884c\u4e3a\u79f0\u4e3a\u7f13\u5b58\u547d\u4e2d\uff08cache hit\uff09\u3002\u5426\u5219\u79f0\u4e3a\u7f13\u5b58\u672a\u547d\u4e2d\uff08cache miss\uff09\uff0c\u90a3\u4e48\u5185\u6838\u5fc5\u987b\u8c03\u5ea6\u5757I/O\u64cd\u4f5c\u53bb\u78c1\u76d8\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u7136\u540e\u5c06\u6570\u636e\u653e\u5165\u9875\u7f13\u5b58\u4e2d\u3002\u5185\u6838\u91c7\u7528\u9884\u8bfb\u673a\u5236\uff1a\u5373\u5f53\u4ece\u78c1\u76d8\u4e2d\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u7531\u4e8e\u78c1\u76d8\u7684\u8bfb\u5199\u5355\u4f4d\u4e3ablock\uff084KB\uff09\uff0c\u51fa\u4e8e\u7a7a\u95f4\u5c40\u90e8\u6027\u539f\u7406\uff0c\u5185\u6838\u4f1a\u5c06\u591a\u4e2ablock\u7684\u6570\u636e\u90fd\u52a0\u5165\u5230\u7f13\u5b58\u4e2d\u3002

"},{"location":"linux/kernel/others/#_8","title":"\u5199\u7f13\u5b58\u7b56\u7565","text":"

\u5728\u8fdb\u7a0b\u5199\u78c1\u76d8\u65f6\uff0c\u7f13\u5b58\u7684\u4f7f\u7528\u4e00\u822c\u6709\u4ee5\u4e0b\u4e09\u79cd\uff1a

  1. \u4e0d\u7f13\u5b58\uff1a\u4e5f\u5c31\u662f\u8bf4\u9ad8\u901f\u7f13\u5b58\u4e0d\u7f13\u5b58\u4efb\u4f55\u5199\u64cd\u4f5c\uff0c\u800c\u662f\u76f4\u63a5\u5199\u5165\u78c1\u76d8\u3002\u8fd9\u79cd\u65b9\u5f0f\u5f88\u5c11\u91c7\u7528\uff0c\u56e0\u4e3a\u6027\u80fd\u975e\u5e38\u5dee\u3002

  2. \u5199\u64cd\u4f5c\u81ea\u52a8\u66f4\u65b0\u7f13\u5b58\uff0c\u540c\u65f6\u4e5f\u66f4\u65b0\u78c1\u76d8\u6587\u4ef6\uff1a\u8fd9\u79cd\u7b56\u7565\u5bf9\u7f13\u5b58\u4e00\u81f4\u6027\u975e\u5e38\u6709\u6548\uff0c\u56e0\u4e3a\u5185\u5b58\u7f13\u5b58\u59cb\u7ec8\u548c\u78c1\u76d8\u4e00\u81f4\u3002

  3. Linux\u91c7\u7528\u7684\u2014\u2014\u56de\u5199\uff1a\u5199\u64cd\u4f5c\u5199\u5165\u7f13\u5b58\uff0c\u4f46\u662f\u78c1\u76d8\u5e76\u4e0d\u7acb\u523b\u66f4\u65b0\uff0c\u800c\u662f\u5c06\u9875\u9ad8\u901f\u7f13\u5b58\u4e2d\u88ab\u5199\u5165\u7684\u9875\u9762\u6807\u8bb0\u4e3a\u201c\u810f\u201d\uff0c\u7136\u540e\u52a0\u5165\u5230\u810f\u9875\u5217\u8868\u4e2d\u3002\u7531\u4e00\u4e2a\u56de\u5199\u8fdb\u7a0b\u5468\u671f\u6027\u7684\u5c06\u810f\u9875\u94fe\u8868\u4e2d\u7684\u9875\u56de\u5199\u81f3\u78c1\u76d8\uff0c\u6700\u540e\u6e05\u7406\u810f\u9875\u6807\u8bc6\u3002

"},{"location":"linux/kernel/others/#_9","title":"\u7f13\u5b58\u56de\u6536\u7b56\u7565","text":"

\u7531\u4e8e\u5927\u91cf\u7684\u9875\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5fc5\u7136\u5bfc\u81f4\u53ef\u7528\u5185\u5b58\u7684\u4e0b\u964d\u3002\u4e3a\u4e86\u4fdd\u8bc1\u5185\u5b58\u7684\u6b63\u5e38\u4f7f\u7528\uff0c\u5fc5\u987b\u6709\u4e00\u79cd\u673a\u5236\u53ef\u4ee5\u56de\u6536\u7f13\u5b58\u3002Linux\u7684\u7f13\u5b58\u56de\u6536\u662f\u5979\u901a\u8fc7\u9009\u62e9\u5e72\u51c0\u9875\u8fdb\u884c\u7b80\u5355\u66ff\u6362\u3002\u5982\u679c\u7f13\u5b58\u4e2d\u6ca1\u6709\u8db3\u591f\u7684\u5e72\u51c0\u9875\uff0c\u5185\u6838\u5c06\u5f3a\u5236\u6027\u7684\u8fdb\u884c\u56de\u5199\u64cd\u4f5c\uff0c\u4ee5\u817e\u51fa\u66f4\u591a\u7684\u5e72\u51c0\u9875\u4f7f\u7528\u3002

\u4e00\u79cd\u56de\u6536\u7b56\u7565\u88ab\u79f0\u4e3a\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7b97\u6cd5\uff0c\u7b80\u79f0LRU\uff08Least Recently Used\uff09\u3002LRU\u7b97\u6cd5\u4e00\u822c\u662f\u7528\u94fe\u8868\u4f5c\u4e3a\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u7684\uff0c\u94fe\u8868\u5934\u90e8\u7684\u6570\u636e\u662f\u6700\u8fd1\u4f7f\u7528\u7684\uff0c\u800c\u94fe\u8868\u672b\u5c3e\u7684\u6570\u636e\u662f\u6700\u4e45\u6ca1\u88ab\u4f7f\u7528\u7684\u3002\u5f53\u7a7a\u95f4\u4e0d\u591f\u65f6\uff0c\u5c31\u6dd8\u6c70\u6700\u4e45\u6ca1\u88ab\u4f7f\u7528\u7684\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u94fe\u8868\u672b\u5c3e\u7684\u6570\u636e\uff0c\u4ece\u800c\u817e\u51fa\u5185\u5b58\u7a7a\u95f4\u3002\u4f20\u7edf\u7684LRU\u7b97\u6cd5\u5bf9\u4e8e\u90a3\u4e9b\u53ea\u4f1a\u88ab\u8bbf\u95ee\u4e00\u6b21\u7684\u6587\u4ef6\u6548\u679c\u5c24\u5176\u5dee\u3002

Linux\u5185\u6838\u5bf9LRU\u7b97\u6cd5\u8fdb\u884c\u4e86\u6539\u8fdb\uff0c\u88ab\u79f0\u4e3a\u53cc\u94fe\u7b56\u7565\u2014\u2014\u7ef4\u62a4\u4e24\u4e2a\u94fe\u8868\uff1a\u6d3b\u8dc3\u94fe\u8868\uff08active list\uff09\u548c\u4e0d\u6d3b\u8dc3\u94fe\u8868\uff08inactive list\uff09\u3002\u5904\u4e8e\u6d3b\u8dc3\u94fe\u8868\u4e0a\u7684\u9875\u88ab\u8ba4\u4e3a\u662f\u201c\u70ed\u201d\u7684\u4e14\u4e0d\u4f1a\u6362\u51fa\uff0c\u5904\u4e8e\u4e0d\u6d3b\u8dc3\u94fe\u8868\u4e0a\u7684\u9875\u88ab\u8ba4\u4e3a\u662f\u201c\u51b7\u201d\u7684\u53ef\u4ee5\u88ab\u6362\u51fa\u3002\u9875\u9762\u4ece\u5c3e\u90e8\u52a0\u5165\uff0c\u4ece\u5934\u90e8\u79fb\u9664\uff0c\u5c31\u50cf\u961f\u5217\u4e00\u6837\u3002\u6709\u4e86\u8fd9\u4e24\u4e2aLRU\u94fe\u8868\u540e\uff0c\u9884\u8bfb\u9875\u5c31\u53ea\u9700\u8981\u52a0\u5165\u5230\u4e0d\u6d3b\u8dc3\u94fe\u8868\u7684\u5934\u90e8\uff0c\u5f53\u9875\u88ab\u771f\u6b63\u8bbf\u95ee\u7684\u65f6\u5019\uff0c\u624d\u5c06\u9875\u63d2\u5165\u5230\u6d3b\u8dc3\u94fe\u8868\u7684\u5934\u90e8\u3002\u5982\u679c\u9884\u8bfb\u7684\u9875\u4e00\u76f4\u6ca1\u6709\u88ab\u8bbf\u95ee\uff0c\u5c31\u4f1a\u4ece\u4e0d\u6d3b\u8dc3\u94fe\u8868\u4e2d\u79fb\u9664\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u5f71\u54cd\u6d3b\u8dc3\u94fe\u8868\u4e2d\u7684\u70ed\u6570\u636e\u3002

"},{"location":"linux/kernel/others/#_10","title":"\u53ef\u79fb\u690d\u6027","text":""},{"location":"linux/kernel/sched/","title":"\u8fdb\u7a0b\u7ba1\u7406\u4e0e\u8c03\u5ea6","text":"

\u8fdb\u7a0b\u5c31\u662f\u5904\u4e8e\u6267\u884c\u671f\u7684\u7a0b\u5e8f\uff0c\u5f53\u6211\u4eec\u5c06\u4ee3\u7801\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u8f93\u5165./a.out\uff0c\u6211\u4eec\u5c31\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u3002\u8fdb\u7a0b\u4ece\u78c1\u76d8\u52a0\u8f7d\u5230\u5185\u5b58\u7684\u8fc7\u7a0b\u8bf7\u53c2\u8003\u7a0b\u5e8f\u7684\u88c5\u8f7d\u3002

\u53ef\u4ee5\u628a\u4e00\u4e2a\u8fdb\u7a0b\u60f3\u8c61\u6210\u4e00\u4e2a\u5e9e\u5927\u7684\u9879\u76ee\u7ec4\uff0c\u5b83\u5305\u542b\u4e86\u5404\u79cd\u8d44\u6e90\uff0c\u4f8b\u5982\u6253\u5f00\u7684\u6587\u4ef6\u3001\u5360\u7528\u7684\u5185\u5b58\u3001\u5904\u7406\u5668\u7684\u72b6\u6001\u7b49\u3002\u800c\u7ebf\u7a0b\uff0c\u5c31\u50cf\u662f\u9879\u76ee\u7ec4\u4e2d\u8d1f\u8d23\u6267\u884c\u5177\u4f53\u4efb\u52a1\u7684\u4e2a\u4eba\u3002\u6b63\u5982\u6bcf\u4e2a\u9879\u76ee\u7ec4\u81f3\u5c11\u9700\u8981\u4e00\u4e2a\u4eba\u6765\u6267\u884c\u4efb\u52a1\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u5c06\u6240\u6709\u4efb\u52a1\u90fd\u4ea4\u7ed9\u4e3b\u7ebf\u7a0b\u6765\u5b8c\u6210\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u628a\u4e00\u4e2a\u5927\u7684\u4efb\u52a1\u5206\u89e3\u6210\u591a\u4e2a\u5c0f\u4efb\u52a1\uff0c\u5e76\u521b\u5efa\u989d\u5916\u7684\u7ebf\u7a0b\u6765\u5e76\u884c\u5904\u7406\u8fd9\u4e9b\u5c0f\u4efb\u52a1\u3002\u90a3\u4e48\uff0c\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u521b\u5efa\u66f4\u591a\u7684\u8fdb\u7a0b\u6765\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u5462\uff1f\u4ece\u6280\u672f\u4e0a\u6765\u8bf4\uff0c\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u8fd9\u901a\u5e38\u5e76\u4e0d\u662f\u6700\u4f73\u9009\u62e9\u3002\u9996\u5148\uff0c\u8fdb\u7a0b\u5360\u7528\u7684\u8d44\u6e90\u6bd4\u7ebf\u7a0b\u591a\uff0c\u800c\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u521b\u5efa\u7684\u901f\u5ea6\u8981\u5feb\u5f97\u591a\u3002\u5176\u6b21\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u8d44\u6e90\u5171\u4eab\u76f8\u5bf9\u590d\u6742\uff0c\u800c\u7ebf\u7a0b\u7531\u4e8e\u5171\u4eab\u540c\u4e00\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u8bbf\u95ee\u8fd9\u4e9b\u8d44\u6e90\u3002\u8fd9\u5c31\u50cf\u662f\u5728\u4e00\u4e2a\u516c\u53f8\u7684\u4e0d\u540c\u9879\u76ee\u7ec4\u4e4b\u95f4\u8fdb\u884c\u5de5\u4f5c\u4ea4\u63a5\u53ef\u80fd\u4f1a\u5f88\u9ebb\u70e6\uff0c\u4f46\u5982\u679c\u5927\u5bb6\u90fd\u5c5e\u4e8e\u540c\u4e00\u4e2a\u9879\u76ee\u7ec4\uff0c\u5408\u4f5c\u5c31\u4f1a\u987a\u7545\u5f97\u591a\u3002

"},{"location":"linux/kernel/sched/#_2","title":"\u8fdb\u7a0b\u63cf\u8ff0\u7b26","text":"

\u4e3a\u4e86\u7ba1\u7406\u8fdb\u7a0b\uff0c\u5185\u6838\u5fc5\u987b\u6e05\u6670\u5730\u63cf\u8ff0\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u3002\u5728Linux\u7cfb\u7edf\u4e0b\uff0c\u4e0d\u7ba1\u662f\u8fdb\u7a0b\u8fd8\u662f\u7ebf\u7a0b\uff0c\u5185\u6838\u7edf\u4e00\u7528task_struct\u7ed3\u6784\u4f53\u7ba1\u7406\u3002

"},{"location":"linux/kernel/sched/#_3","title":"\u5185\u6838\u6808","text":"
void *stack;\n

\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u4e13\u7528\u7684\u5185\u6838\u6808\uff0c\u7528\u4e8e\u4fdd\u5b58\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u6267\u884c\u65f6\u7684\u4e34\u65f6\u6570\u636e\u548c\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5f53\u521b\u5efa\u65b0\u8fdb\u7a0b\u65f6\uff0c\u5185\u6838\u4f1a\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5408\u9002\u5927\u5c0f\u7684\u5185\u6838\u6808\u7a7a\u95f4\uff0c\u5e76\u5c06\u5176\u5730\u5740\u8d4b\u7ed9task_struct\u4e2d\u7684stack\u6210\u5458\u3002

"},{"location":"linux/kernel/sched/#_4","title":"\u7ebf\u7a0b\u63cf\u8ff0\u7b26","text":"

\u7ebf\u7a0b\u63cf\u8ff0\u7b26thread_info\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u653e\u7ebf\u7a0b\u76f8\u5173\u4fe1\u606f\u7684\u7ed3\u6784\u4f53\uff0c\u5b83\u5305\u542b\u4e86\u5f88\u591a\u4e0e\u7ebf\u7a0b\u76f8\u5173\u7684\u5b57\u6bb5\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u662ftask_struct *task\uff0c\u5b83\u6307\u5411\u5f53\u524d\u7ebf\u7a0b\u6240\u5c5e\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u3002\u4e3a\u4e86\u65b9\u4fbf\u8bbf\u95ee\u5f53\u524dCPU\u4e0a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u7684task_struct\u7ed3\u6784\uff0c\u5185\u6838\u63d0\u4f9b\u4e86current\u5b8f\uff0c\u8be5\u5b8f\u672c\u8d28\u4e0a\u7b49\u4e8e\uff1a

current_thread_info()->task\n

\u4e0a\u8ff0\u4ee3\u7801\u8fd4\u56de\u5f53\u524dtask_struct\u7684\u5730\u5740\u3002

"},{"location":"linux/kernel/sched/#_5","title":"\u8fdb\u7a0b\u72b6\u6001","text":"

\u5728task_struct\u4e2d\uff0c\u6d89\u53ca\u5230\u8fdb\u7a0b\u72b6\u6001\u7684\u662f\u8fd9\u51e0\u4e2a\u6210\u5458\uff1a

 volatile long state;   \n int exit_state;\n unsigned int flags;\n

state\u53ef\u4ee5\u53d6\u7684\u503c\u6709\uff1a

/* Used in tsk->state: */\n#define TASK_RUNNING                    0\n#define TASK_INTERRUPTIBLE              1\n#define TASK_UNINTERRUPTIBLE            2\n#define __TASK_STOPPED                  4\n#define __TASK_TRACED                   8\n/* Used in tsk->exit_state: */\n#define EXIT_DEAD                       16\n#define EXIT_ZOMBIE                     32\n#define EXIT_TRACE                      (EXIT_ZOMBIE | EXIT_DEAD)\n/* Used in tsk->state again: */\n#define TASK_DEAD                       64\n#define TASK_WAKEKILL                   128\n#define TASK_WAKING                     256\n#define TASK_PARKED                     512\n#define TASK_NOLOAD                     1024\n#define TASK_NEW                        2048\n#define TASK_STATE_MAX                  4096\n

state\u5b57\u6bb5\u7684\u503c\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u8bbe\u7f6e\u3002

p->state = TASK_RUNNING;\n

\u4e5f\u53ef\u4ee5\u4f7f\u7528set_task_state\u548cset_current_state\u5b8f\uff1a\u5b83\u4eec\u5206\u522b\u8bbe\u7f6e\u6307\u5b9a\u8fdb\u7a0b\u7684\u72b6\u6001\u548c\u5f53\u524d\u6267\u884c\u8fdb\u7a0b\u7684\u72b6\u6001\u3002

TASK_RUNNING\u8868\u793a\u8fdb\u7a0b\u6b63\u5728\u6267\u884c\u6216\u8005\u51c6\u5907\u6267\u884c\u3002

\u5f53\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u540e\uff0c\u6709\u4e24\u79cd\u72b6\u6001\uff1a

TASK_INTERRUPTIBLE\u8868\u793a\u8fdb\u7a0b\u53ef\u4ee5\u54cd\u5e94\u4fe1\u53f7\u3002\u4e5f\u5c31\u662f\u5f53\u4e00\u4e2a\u4fe1\u53f7\u6765\u4e34\u65f6\uff0c\u5524\u9192\u8be5\u8fdb\u7a0b\uff0c\u7136\u540e\u6267\u884c\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002

TASK_UNINTERRUPTIBLE\u8868\u793a\u8fdb\u7a0b\u4e0d\u53ef\u4ee5\u54cd\u5e94\u4fe1\u53f7\u3002\u5f53\u8fdb\u7a0b\u7b49\u5f85\u67d0\u4e2a\u4e8b\u4ef6\u5b8c\u6210\u800c\u8fdb\u5165\u6df1\u5ea6\u7761\u7720\u72b6\u6001\u65f6\uff0c\u5982\u679c\u8be5\u4e8b\u4ef6\u56e0\u4e3a\u67d0\u79cd\u539f\u56e0\u65e0\u6cd5\u5b8c\u6210\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8fdb\u7a0b\u4e5f\u5c06\u6c38\u8fdc\u7761\u7720\u4e0b\u53bb\u3002\u6700\u5178\u578b\u7684\u5c31\u662f\u7b49\u5f85\u78c1\u76d8I/O\u3002

\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5371\u9669\u7684\u4e8b\u60c5\uff0c\u9664\u975e\u5f88\u6709\u628a\u63e1\u4e0d\u7136\u4e0d\u4f1a\u8ba9\u8fdb\u7a0b\u8fdb\u5165\u6df1\u5ea6\u7761\u7720\u6001\u3002

\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u4e86\u4e00\u79cd\u65b0\u7684\u72b6\u6001\uff1aTASK_KILLABLE\uff0c\u53ef\u4ee5\u7ec8\u6b62\u7684\u6df1\u5ea6\u7761\u7720\u6001\u3002\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNITERRUPTIBLE)\n

\u5176\u4e2dTASK_WAKEKILL\u8868\u793a\u5728\u63a5\u6536\u5230\u81f4\u547d\u4fe1\u53f7\u65f6\uff0c\u5524\u9192\u8fdb\u7a0b\u3002

TASK_STOPPED\u8868\u793a\u8fdb\u7a0b\u88ab\u6682\u505c\uff0c\u5f53\u63a5\u6536\u5230SIGSTOP\u3001SIGTSTP\u3001SIGTTIN\u6216SIGTTOU\u4fe1\u53f7\u540e\uff0c\u8fdb\u5165\u8be5\u72b6\u6001\u3002

TASK_TRACED\u8868\u793a\u8fdb\u7a0b\u88ab\u8ddf\u8e2a\u3002\u8fd9\u5f80\u5f80\u662f\u8c03\u8bd5\u7a0b\u5e8f\u6682\u65f6\u7ec8\u6b62\u4e86\u8fdb\u7a0b\u7684\u8fd0\u884c\u3002

\u5185\u6838\u7ecf\u5e38\u9700\u8981\u8c03\u6574\u5f53\u524d\u8fdb\u7a0b\u7684\u72b6\u6001\uff0c\u53ef\u4ee5\u4f7f\u7528set_task_state(task, state)\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u6307\u5b9a\u8fdb\u7a0b\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u72b6\u6001\u3002

exit_state\u7684\u53d6\u503c\u6709\u4e24\u79cd\uff1a

EXIT_ZOMBIE\u8868\u793a\u50f5\u6b7b\u72b6\u6001\u3002\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c\u88ab\u7ec8\u6b62\uff0c\u4f46\u662f\u5176\u7236\u8fdb\u7a0b\u8fd8\u6ca1\u6709\u8c03\u7528wait4()\u6216\u8005waitpid()\u7cfb\u7edf\u8c03\u7528\u6765\u56de\u6536\u8d44\u6e90\u65f6\uff0c\u8fdb\u5165\u8be5\u72b6\u6001\u3002

EXIT_DEAD\u662f\u8fdb\u7a0b\u7684\u6700\u7ec8\u72b6\u6001\u3002\u7236\u8fdb\u7a0b\u56de\u6536\u5b50\u8fdb\u7a0b\u8d44\u6e90\u4e4b\u540e\uff0c\u8fdb\u7a0b\u7531\u7cfb\u7edf\u5220\u9664\u3002

\u8fdb\u7a0b\u7684\u4e00\u4e9b\u5176\u4ed6\u72b6\u6001\u4fe1\u606f\u7531flags\u63a7\u5236\uff0c\u4f7f\u7528\u4f4d\u63a9\u7801\u6765\u8868\u793a\u4e0d\u540c\u7684\u6807\u8bb0\u3002\u4e00\u4e9b\u5e38\u89c1\u7684\u6807\u8bb0\u5305\u62ec\uff1a

  • \u4efb\u52a1\u72b6\u6001\uff1a\u4f8b\u5982\u662f\u5426\u6b63\u5728\u8fd0\u884c\u3001\u505c\u6b62\u7b49
  • \u8c03\u5ea6\u5668\u76f8\u5173\uff1a\u4f8b\u5982\u8c03\u5ea6\u7b56\u7565\u3001\u4f18\u5148\u7ea7\u7b49
  • \u4fe1\u53f7\u5904\u7406\uff1a\u4f8b\u5982\u6302\u8d77\u4fe1\u53f7\u96c6
  • \u8fdb\u7a0b\u7279\u6027\uff1a\u662f\u5426\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u3001\u662f\u5426\u5141\u8bb8\u88ab\u8ffd\u8e2a\u7b49

flags\u6210\u5458\u7684\u4e00\u4e9b\u53d6\u503c\u4e3e\u4f8b\u5982\u4e0b\uff0c\u8fd9\u4e9b\u5b8f\u4ee5PF\u5f00\u5934\uff1a

#define PF_EXITING 0x00000004    //getting shut down\n#define PF_WQ_WORKER 0x00000020  //I'm a workqueue worker\n#define PF_KTHREAD 0x00200000    //I'm a kernel thread\n
"},{"location":"linux/kernel/sched/#_6","title":"\u8fdb\u7a0b\u6807\u8bc6","text":"

Linux\u7cfb\u7edf\u4f7f\u7528PID\u6765\u6807\u8bc6\u4e00\u4e2a\u8fdb\u7a0b\uff0cPID\u88ab\u987a\u5e8f\u7f16\u53f7\uff0c\u65b0\u521b\u5efa\u7684\u8fdb\u7a0b\u7684PID\u901a\u5e38\u662f\u524d\u4e00\u4e2a\u8fdb\u7a0b+1\u3002\u4f46\u662fPID\u7684\u503c\u6709\u4e00\u4e2a\u4e0a\u9650\uff0c\u7cfb\u7edf\u7ba1\u7406\u5458\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539/proc/sys/kernel/pid_max\u6587\u4ef6\u7684\u503c\u6765\u6539\u53d8\u8fd9\u4e2a\u4e0a\u9650\u3002\u8fd9\u4e48\u4e00\u770b\u4f3c\u4e4e\u4e00\u4e2aPID\u6765\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u8fdb\u7a0b\u5c31\u591f\u4e86\uff0c\u4f46\u662f\u5728task_struct\u4e2d\uff0c\u6d89\u53ca\u5230\u8fdb\u7a0b\u6807\u8bc6\u7684\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6210\u5458\u3002

pid_t pid;\npid_t tgid;\nstruct task_struct *group_leader; \n

\u4f60\u53ef\u80fd\u4f1a\u7591\u60d1\uff0c\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u7528\u5230\u8fd9\u4e48\u591a\u53d8\u91cf\uff1f\u8fd9\u662f\u56e0\u4e3a\u4e4b\u524d\u63d0\u5230\uff0c\u5728Linux\u7cfb\u7edf\u4e0b\uff0c\u8fdb\u7a0b\u4e0e\u7ebf\u7a0b\u5e76\u4e0d\u7279\u522b\u533a\u5206\uff0c\u90fd\u662f\u4e00\u4e2atask_struct\u7ed3\u6784\u4f53\u3002\u8fd9\u5c31\u7ed9\u7ba1\u7406\u5e26\u6765\u4e86\u4e00\u4e9b\u9ebb\u70e6\u3002\u6bd4\u5982\u6211\u4eec\u5e0c\u671b\u540c\u5c5e\u4e8e\u4e00\u4e2a\u8fdb\u7a0b\u7ec4\u7684\u7ebf\u7a0b\u62e5\u6709\u5171\u540c\u7684pid\u3002\u5f53\u6211\u4eec\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u7ed9\u6307\u5b9apid\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u8fd9\u4e2a\u4fe1\u53f7\u80fd\u4f5c\u7528\u4e8e\u8be5\u7ec4\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\u3002\u5f53\u6740\u6b7b\u67d0\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u5f53\u7136\u662f\u8fde\u540c\u8be5\u8fdb\u7a0b\u4e0b\u7684\u6240\u6709\u7ebf\u7a0b\u4e00\u8d77\u6740\u6b7b\uff0c\u800c\u4e0d\u662f\u53ea\u6740\u6b7b\u4e86\u4e3b\u8fdb\u7a0b\uff0c\u5176\u4ed6\u7ebf\u7a0b\u8fd8\u5728\u5de5\u4f5c\uff0c\u8fd9\u663e\u7136\u4e0d\u5408\u5e38\u7406\u3002\u4e8b\u5b9e\u4e0a\uff0cPOSIX\u6807\u51c6\u4e5f\u89c4\u5b9a\u4e00\u4e2a\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\u7684\u6240\u6709\u7ebf\u7a0b\u5fc5\u987b\u4eab\u6709\u5171\u540c\u7684pid\u3002

\u9075\u7167\u8fd9\u4e2a\u6807\u51c6\uff0cLinux\u5f15\u5165\u4e86\u7ebf\u7a0b\u7ec4\u7684\u6982\u5ff5\u3002\u4e00\u4e2a\u7ebf\u7a0b\u7ec4\u4e2d\u6240\u6709\u7ebf\u7a0b\u4f7f\u7528\u548c\u8be5\u7ebf\u7a0b\u7ec4\u9886\u5934\u7ebf\u7a0b\uff08thread group leader\uff09\u76f8\u540c\u7684pid\uff0c\u5b83\u88ab\u5b58\u5165task_struct\u7684tgid\u5b57\u6bb5\u3002\u5f53\u6211\u4eec\u4f7f\u7528getpid()\u7cfb\u7edf\u8c03\u7528\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u7684pid\u65f6\uff0c\u8fd4\u56de\u7684\u4e5f\u662ftgid\u7684\u503c\u3002\u4efb\u4f55\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5982\u679c\u53ea\u6709\u4e3b\u7ebf\u7a0b\uff0c\u90a3\u4e48pid = tgid\uff0c group_leader\u6307\u5411\u7684\u5c31\u662f\u81ea\u5df1\u3002\u5982\u679c\u4e3b\u7ebf\u7a0b\u521b\u5efa\u4e86\u5176\u4ed6\u7ebf\u7a0b\uff0c\u90a3\u4e48\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684pid\uff0c\u4f46\u662ftgid\u4ecd\u7136\u662f\u4e3b\u7ebf\u7a0b\u3002

"},{"location":"linux/kernel/sched/#_7","title":"\u8fdb\u7a0b\u95f4\u5173\u7cfb","text":"

Linux\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u662fPID\u4e3a1\u7684init\u8fdb\u7a0b\u7684\u540e\u4ee3\uff0c\u5185\u6838\u5728\u7cfb\u7edf\u542f\u52a8\u7684\u6700\u540e\u9636\u6bb5\u542f\u52a8init\u8fdb\u7a0b\u3002\u8be5\u8fdb\u7a0b\u8bfb\u53d6\u7cfb\u7edf\u7684\u521d\u59cb\u5316\u811a\u672c\u5e76\u6267\u884c\u5176\u4ed6\u7684\u76f8\u5173\u7a0b\u5e8f\uff0c\u6700\u540e\u5b8c\u6210\u6574\u4e2a\u7cfb\u7edf\u7684\u542f\u52a8\u3002

\u6bcf\u4e2a\u8fdb\u7a0b\u5fc5\u6709\u4e00\u4e2a\u7236\u8fdb\u7a0b\uff0c\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u521b\u5efa\u4e86\u591a\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u8fd8\u6709\u5144\u5f1f\u5173\u7cfb\u3002\u5728task_struct\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u5b57\u6bb5\u6765\u8868\u793a\u4eb2\u7f18\u5173\u7cfb\uff1a

struct task_struct __rcu *real_parent; /* real parent process */\nstruct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */\nstruct list_head children;      /* list of my children */\nstruct list_head sibling;       /* linkage in my parent's children list */\n

\u8fd9\u4e9b\u5b57\u6bb5\u6839\u636e\u540d\u5b57\u5c31\u80fd\u731c\u5230\u662f\u4ec0\u4e48\u610f\u601d\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0breal_parent\u548cparent\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5f53\u6211\u4eec\u542f\u52a8GDB\u76d1\u89c6\u67d0\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u8be5\u8fdb\u7a0b\u7684real_parent\u4e0d\u53d8\uff0c\u4f46\u662fparent\u53d8\u6210\u4e86GDB\u3002

\u8fdb\u7a0b\u4e4b\u95f4\u8fd8\u6709\u5176\u4ed6\u5173\u7cfb\uff0c\u8fd9\u91cc\u4e0d\u8be6\u7ec6\u5c55\u5f00\u3002

"},{"location":"linux/kernel/sched/#_8","title":"\u8fd0\u884c\u7edf\u8ba1","text":"
u64        utime;//\u7528\u6237\u6001\u6d88\u8017\u7684CPU\u65f6\u95f4\nu64        stime;//\u5185\u6838\u6001\u6d88\u8017\u7684CPU\u65f6\u95f4\nunsigned long      nvcsw;//\u81ea\u613f(voluntary)\u4e0a\u4e0b\u6587\u5207\u6362\u8ba1\u6570\nunsigned long      nivcsw;//\u975e\u81ea\u613f(involuntary)\u4e0a\u4e0b\u6587\u5207\u6362\u8ba1\u6570\nu64        start_time;//\u8fdb\u7a0b\u542f\u52a8\u65f6\u95f4\uff0c\u4e0d\u5305\u542b\u7761\u7720\u65f6\u95f4\nu64        real_start_time;//\u8fdb\u7a0b\u542f\u52a8\u65f6\u95f4\uff0c\u5305\u542b\u7761\u7720\u65f6\u95f4\n
"},{"location":"linux/kernel/sched/#_9","title":"\u5185\u5b58\u7ba1\u7406","text":"
struct mm_struct *mm;        \nstruct mm_struct *active_mm;            \n
"},{"location":"linux/kernel/sched/#_10","title":"\u6587\u4ef6\u4e0e\u6587\u4ef6\u7cfb\u7edf","text":"
/* Filesystem information: */\nstruct fs_struct *fs;\n/* Open file information: */\nstruct files_struct *files;\n

\u8be6\u60c5\u8bf7\u53c2\u8003\uff1a\u865a\u62df\u6587\u4ef6\u7cfb\u7edf

"},{"location":"linux/kernel/sched/#_11","title":"\u8fdb\u7a0b\u7ec4\u7ec7\u5f62\u5f0f","text":"

\u8981\u660e\u767d\u8fdb\u7a0b\u4e4b\u95f4\u662f\u5982\u4f55\u7ec4\u7ec7\u7684\uff0c\u9996\u5148\u8981\u77e5\u9053\u53cc\u5411\u94fe\u8868\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u3002\u53cc\u5411\u94fe\u8868\u5c31\u662f\u540c\u65f6\u6709prev\u548cnext\u6307\u9488\u7684\u94fe\u8868\uff0c\u5206\u522b\u6307\u5411\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u3002Linux\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5c06\u6240\u6709\u8fdb\u7a0b\u7684\u63cf\u8ff0\u7b26\u5168\u90e8\u8fde\u63a5\u8d77\u6765\u3002

"},{"location":"linux/kernel/sched/#_12","title":"\u8fd0\u884c\u961f\u5217","text":"

\u5f53\u5185\u6838\u9700\u8981\u5bfb\u627e\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u8fd0\u884c\u65f6\uff0c\u5fc5\u987b\u53ea\u8003\u8651\u5df2\u5904\u4e8eTASK_RUNNING\u72b6\u6001\u7684\u8fdb\u7a0b\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u8fd0\u884c\u961f\u5217\u3002\u4e3a\u4e86\u63d0\u9ad8\u8c03\u5ea6\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\uff0c\u5185\u6838\u4e3a\u6bcf\u4e2a\u4f18\u5148\u7ea7\u90fd\u7ef4\u62a4\u4e86\u4e00\u4e2a\u94fe\u8868\u3002\u5728\u591a\u5904\u7406\u5668\u4e2d\uff0c\u6bcf\u4e2aCPU\u90fd\u6709\u81ea\u5df1\u7684\u8fd0\u884c\u961f\u5217\u3002\u8fd0\u884c\u961f\u5217\u662fLinux\u8c03\u5ea6\u7b97\u6cd5\u7684\u57fa\u7840\u3002\u66f4\u8be6\u7ec6\u7684\u5185\u5bb9\u8bf7\u53c2\u8003\u8fdb\u7a0b\u8c03\u5ea6\u3002

"},{"location":"linux/kernel/sched/#_13","title":"\u7b49\u5f85\u961f\u5217","text":"

\u7b49\u5f85\u961f\u5217\u5728\u5185\u6838\u4e2d\u6709\u5f88\u591a\u7528\u9014\uff0c\u5c24\u5176\u65f6\u7528\u5728\u4e2d\u65ad\u5904\u7406\u3001\u8fdb\u7a0b\u540c\u6b65\u3002\u8fd0\u884c\u4e2d\u7684\u8fdb\u7a0b\u5f80\u5f80\u9700\u8981\u7b49\u5f85\u67d0\u4e9b\u4e8b\u4ef6\u7684\u53d1\u751f\uff0c\u5e0c\u671b\u7b49\u5f85\u7279\u5b9a\u4e8b\u4ef6\u7684\u8fdb\u7a0b\u5c06\u81ea\u5df1\u653e\u5165\u5408\u9002\u7684\u7b49\u5f85\u961f\u5217\uff0c\u7136\u540e\u8fdb\u5165\u7761\u7720\u72b6\u6001\u3002\u5f53\u4e8b\u4ef6\u53d1\u751f\u540e\uff0c\u7531\u5185\u6838\u8d1f\u8d23\u5524\u9192\u5b83\u4eec\u3002

\u7b49\u5f85\u961f\u5217\u7531\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff0c\u6bcf\u4e2a\u7b49\u5f85\u961f\u5217\u90fd\u6709\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u5934\uff08wait queue head\uff09\uff0c\u4e00\u4e2a\u7c7b\u578b\u4e3await_queue_head_t\u7684\u6570\u636e\u7ed3\u6784\uff1a

struct __wait_queue_head {\n    spinlock_t lock;\n    struct list_head task_list;\n};\n\ntypedef struct __wait_queue_head wait_queue_head_t;\n

\u56e0\u4e3a\u7b49\u5f85\u961f\u5217\u4e3b\u8981\u662f\u7531\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u548c\u5185\u6838\u51fd\u6570\u4fee\u6539\u7684\uff0c\u56e0\u6b64\u5fc5\u987b\u6709\u9501\u52a0\u4ee5\u4fdd\u62a4\u3002\u7b49\u5f85\u961f\u5217\u94fe\u8868\u4e2d\u7684\u5143\u7d20\u4e3a\uff1a

struct __wait_queue {\n    unsigned int flags;\n    struct task_struct *task;\n    wait_queue_func_t func;\n    struct list_head task_list;\n};\ntypedef struct __wait_queue wait_queue_t;\n

\u7b49\u5f85\u961f\u5217\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e00\u4e2a\u7761\u7720\u4e2d\u7684\u8fdb\u7a0b\uff0c\u5b83\u7684\u63cf\u8ff0\u7b26\u5b58\u5165task\u5b57\u6bb5\u4e2d\u3002task_list\u8d1f\u8d23\u5c06\u6bcf\u4e00\u4e2a\u5143\u7d20\u94fe\u63a5\u5230\u94fe\u8868\u4e2d\u3002func\u8868\u793a\u7b49\u5f85\u961f\u5217\u4e2d\u7761\u7720\u8fdb\u7a0b\u5e94\u8be5\u7528\u4ec0\u4e48\u65b9\u5f0f\u5524\u9192\u3002flags\u8868\u793a\u8be5\u8fdb\u7a0b\u662f\u4e92\u65a5\u8fdb\u7a0b\u8fd8\u662f\u975e\u4e92\u65a5\u8fdb\u7a0b\u3002\u4e92\u65a5\u8fdb\u7a0b\u8868\u793a\u591a\u4e2a\u8fdb\u7a0b\u5728\u7b49\u5f85\u76f8\u540c\u7684\u4e8b\u4ef6\uff0c\u56e0\u6b64\u4ea7\u751f\u4e86\u7ade\u4e89\u5173\u7cfb\uff0c\u6b64\u65f6\u5185\u6838\u53ea\u9700\u8981\u5524\u9192\u5176\u4e2d\u4e00\u4e2a\u8fdb\u7a0b\u5373\u53ef\u3002\u800c\u975e\u4e92\u65a5\u8fdb\u7a0b\u5728\u53d1\u751f\u6307\u5b9a\u4e8b\u4ef6\u540e\u603b\u662f\u88ab\u5524\u9192\u3002

\u7b49\u5f85\u961f\u5217\u7684\u64cd\u4f5c\u6bd4\u8f83\u590d\u6742\uff0c\u8fd9\u91cc\u4e0d\u8be6\u7ec6\u5c55\u5f00\uff0c\u53ea\u8bb2\u4e00\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u51fd\u6570sleep_on()\uff1a\u8fd9\u4e2a\u51fd\u6570\u5c06\u5f53\u524d\u8fdb\u7a0b\u52a0\u5165\u7b49\u5f85\u961f\u5217\uff0c\u5e76\u542f\u52a8\u8c03\u5ea6\u7a0b\u5e8f\u3002

void sleep_on(wait_queue_head_t *wq)\n{\n    wait_queue_t wait;  //\u58f0\u660e\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u5143\u7d20\n    init_waiqueue_entry(&wait, current);  //\u5c06current\u5f53\u524d\u8fdb\u7a0b\u52a0\u5165\u5230wait\u4e2d\n    current->state = TAKS_UNINTERRUPTIBLE; //\u8bbe\u7f6e\u5f53\u524d\u8fdb\u7a0b\u4e3a\u6df1\u5ea6\u7761\u7720\u6001\n    add_wait_queue(wq, &wait);  //\u5c06wait\u52a0\u5165\u5230\u7b49\u5f85\u961f\u5217wq\u4e2d\n    schedule();                //\u542f\u52a8\u8c03\u5ea6\u7a0b\u5e8f\n    remove_wait_queue(wq, &wait);   //\u628a\u5f53\u524d\u8fdb\u7a0b\u4ece\u7b49\u5f85\u961f\u5217\u4e2d\u5220\u9664\n}\n

\u6ce8\u610f\uff1a\u5728\u542f\u52a8\u8c03\u5ea6\u7a0b\u5e8f\u4e4b\u524d\uff0c\u8c03\u5ea6\u5668\u4f1a\u8bb0\u5f55\u5f53\u524d\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5e76\u4fdd\u5b58\u81f3\u5bc4\u5b58\u5668\u4e2d\uff0c\u5f53\u8be5\u7761\u7720\u8fdb\u7a0b\u88ab\u5524\u9192\u65f6\uff0c\u8c03\u5ea6\u7a0b\u5e8f\u4ecesleep_on()\u51fd\u6570\u4e4b\u524d\u505c\u6b62\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u2014\u2014\u628a\u8be5\u8fdb\u7a0b\u4ece\u7b49\u5f85\u961f\u5217\u5220\u9664\u3002

"},{"location":"linux/kernel/sched/#_14","title":"\u8fdb\u7a0b\u751f\u547d\u5468\u671f","text":"

\u8fd9\u91cc\u7ed9\u51fa\u4e00\u5f20\u793a\u610f\u56fe\u65b9\u4fbf\u7406\u89e3\u3002

"},{"location":"linux/kernel/sched/#_15","title":"\u8fdb\u7a0b\u521b\u5efa","text":"

Linux\u7684\u8fdb\u7a0b\u521b\u5efa\u6d89\u53ca\u5230\u4e24\u4e2a\u51fd\u6570\uff1afork()\u548cexec()\u3002fork()\u62f7\u8d1d\u5f53\u524d\u8fdb\u7a0b\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u3002exec()\u8d1f\u8d23\u8bfb\u53d6\u53ef\u6267\u884c\u6587\u4ef6\u5e76\u5c06\u5176\u8f7d\u5165\u5730\u5740\u7a7a\u95f4\u5f00\u59cb\u8fd0\u884c\u3002

Linux\u7684fork()\u4f7f\u7528\u4e86\u5199\u65f6\u62f7\u8d1d\uff08copy-on-write\uff09\u673a\u5236\u3002\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u8fdb\u7a0b\u65f6\uff0c\u5185\u6838\u5e76\u4e0d\u590d\u5236\u6574\u4e2a\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\uff0c\u800c\u662f\u8ba9\u7236\u5b50\u8fdb\u7a0b\u5171\u4eab\u3002\u53ea\u6709\u5728\u9700\u8981\u5199\u5165\u65f6\uff0c\u6570\u636e\u624d\u4f1a\u88ab\u590d\u5236\uff0c\u4ece\u800c\u4f7f\u5f97\u5404\u4e2a\u8fdb\u7a0b\u62e5\u6709\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u3002\u8fd9\u79cd\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u5927\u91cf\u6839\u672c\u5c31\u4e0d\u4f1a\u88ab\u4f7f\u7528\u7684\u6570\u636e\uff0c\u4ece\u800c\u4f7f\u5f97\u8fdb\u7a0b\u7684\u521b\u5efa\u975e\u5e38\u8fc5\u901f\u3002

fork()\u51fd\u6570\u7531clone()\u7cfb\u7edf\u8c03\u7528\u5b9e\u73b0\u3002\u8be5\u7cfb\u7edf\u8c03\u7528\u901a\u8fc7\u4e00\u7cfb\u5217CLONE_FLAGS\u53c2\u6570\u6765\u6307\u660e\u7236\u5b50\u8fdb\u7a0b\u9700\u8981\u5171\u4eab\u7684\u8d44\u6e90\u3002Linux\u4e2d\u8fdb\u7a0b\u4e0e\u7ebf\u7a0b\u7684\u533a\u522b\u4e3b\u8981\u662f\u5728\u6267\u884cclone()\u7cfb\u7edf\u8c03\u7528\u65f6\u6307\u5b9a\u7684\u5171\u4eab\u8d44\u6e90\u6709\u6240\u4e0d\u540c\u800c\u5df2\u3002\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6\u4f20\u9012\u7684\u53c2\u6570\u5982\u4e0b\uff1a

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);\n

\u4e0a\u8ff0\u4ee3\u7801\u6307\u660e\u4e86\u521b\u5efa\u7ebf\u7a0b\u65f6\u9700\u8981\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u7cfb\u7edf\u8d44\u6e90\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\u3002

\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4e00\u4e2a\u666e\u901a\u7684fork()\u7684\u5b9e\u73b0\uff1a

clone(SIGCHLD, 0);\n
"},{"location":"linux/kernel/sched/#_16","title":"\u8fdb\u7a0b\u7ec8\u7ed3","text":"

\u4e00\u4e2a\u8fdb\u7a0b\u7ec8\u7ed3\u65f6\uff0c\u5185\u6838\u5fc5\u987b\u91ca\u653e\u5b83\u6240\u5360\u7528\u7684\u8d44\u6e90\u5e76\u4e14\u544a\u77e5\u7236\u8fdb\u7a0b\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fdb\u7a0b\u7684\u7ec8\u7ed3\u662f\u81ea\u8eab\u5f15\u8d77\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u63a5\u6536\u4e86\u4e00\u4e2a\u4e0d\u53ef\u5ffd\u7565\u7684\u4fe1\u53f7\u6216\u8005\u662f\u5f02\u5e38\u3002

\u5f53\u8fdb\u7a0b\u76f8\u5173\u8054\u7684\u6240\u6709\u8d44\u6e90\u90fd\u88ab\u91ca\u653e\u540e\uff0c\u5b83\u5904\u4e8eEXIT_ZOMBIE\u72b6\u6001\u3002\u8be5\u72b6\u6001\u4e5f\u88ab\u79f0\u4e3a\u50f5\u6b7b\u72b6\u6001\uff0c\u5b83\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u88ab\u4fdd\u7559\uff0c\u7b49\u5f85\u7236\u8fdb\u7a0b\u4f7f\u7528wait()\u7cfb\u5217\u51fd\u6570\u56de\u6536\u3002

\u4f46\u662f\uff0c\u5982\u679c\u7236\u8fdb\u7a0b\u5728\u5b50\u8fdb\u7a0b\u4e4b\u524d\u5c31\u9000\u51fa\u4e86\uff0c\u90a3\u4e48\u5fc5\u987b\u6709\u4e00\u4e2a\u673a\u5236\u6765\u4fdd\u8bc1\u5b50\u8fdb\u7a0b\u627e\u5230\u4e00\u4e2a\u65b0\u7684\u7236\u8fdb\u7a0b\uff0c\u5426\u5219\u8fd9\u4e9b\u5b64\u513f\u8fdb\u7a0b\u4f1a\u6c38\u8fdc\u5904\u4e8e\u50f5\u6b7b\u72b6\u6001\uff0c\u767d\u767d\u8017\u8d39\u5185\u5b58\u3002\u5185\u6838\u7684\u89e3\u51b3\u529e\u6cd5\u662f\u5728\u5f53\u524d\u7ebf\u7a0b\u7ec4\u5185\u627e\u4e00\u4e2a\u7ebf\u7a0b\u4f5c\u4e3a\u7236\u8fdb\u7a0b\uff0c\u5982\u679c\u4e0d\u884c\uff0c\u5c31\u8ba9init\u8fdb\u7a0b\u505a\u7236\u8fdb\u7a0b\u3002

"},{"location":"linux/kernel/sched/#_17","title":"\u8fdb\u7a0b\u5207\u6362","text":"

\u4e3a\u4e86\u63a7\u5236\u8fdb\u7a0b\u7684\u8fd0\u884c\uff0c\u5185\u6838\u5fc5\u987b\u6709\u80fd\u529b\u6302\u8d77\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u6216\u8005\u6062\u590d\u4ee5\u524d\u6302\u8d77\u7684\u8fdb\u7a0b\u3002\u8fd9\u79cd\u884c\u4e3a\u88ab\u79f0\u4e3a\u8fdb\u7a0b\u5207\u6362\uff08process switch\uff09\u6216\u4e0a\u4e0b\u6587\u5207\u6362\uff08context switch\uff09\u3002\u7406\u89e3\u4e86\u8fdb\u7a0b\u5207\u6362\uff0c\u624d\u6709\u53ef\u80fd\u7406\u89e3\u5185\u6838\u662f\u5982\u4f55\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u8c03\u5ea6\u7684\u3002

"},{"location":"linux/kernel/sched/#_18","title":"\u786c\u4ef6\u4e0a\u4e0b\u6587","text":"

\u5c3d\u7ba1\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u4f46\u6240\u6709\u8fdb\u7a0b\u90fd\u5171\u4eabCPU\u7684\u5bc4\u5b58\u5668\uff0c\u56e0\u6b64\u5728\u6062\u590d\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c\u4e4b\u524d\uff0c\u5185\u6838\u5fc5\u987b\u4fdd\u8bc1\u5bc4\u5b58\u5668\u88c5\u5165\u4e86\u6302\u8d77\u8fdb\u7a0b\u65f6\u7684\u503c\u3002\u8fdb\u7a0b\u6062\u590d\u6267\u884c\u524d\u5fc5\u987b\u88c5\u5165\u5bc4\u5b58\u5668\u7684\u4e00\u7ec4\u6570\u636e\u79f0\u4e3a\u786c\u4ef6\u4e0a\u4e0b\u6587\uff08hardware context\uff09\u3002\u786c\u4ef6\u4e0a\u4e0b\u6587\u662f\u8fdb\u7a0b\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u56e0\u4e3a\u6267\u884c\u4e0a\u4e0b\u6587\u5305\u542b\u8fdb\u7a0b\u6267\u884c\u9700\u8981\u7684\u6240\u6709\u4fe1\u606f\u3002

"},{"location":"linux/kernel/sched/#thread","title":"thread\u5b57\u6bb5","text":"

\u6bcf\u4e2a\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u5305\u542b\u4e00\u4e2a\u7c7b\u578b\u4e3athread_struct\u7684thread\u5b57\u6bb5\uff0c\u53ea\u8981\u8fdb\u7a0b\u88ab\u5207\u6362\u51fa\u53bb\uff0c\u5185\u6838\u5c31\u628a\u5176\u786c\u4ef6\u4e0a\u4e0b\u6587\u4fdd\u5b58\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\u3002\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u5305\u542b\u4e86\u5927\u91cfCPU\u5bc4\u5b58\u5668\u4fe1\u606f\u3002

"},{"location":"linux/kernel/sched/#_19","title":"\u6267\u884c\u8fdb\u7a0b\u5207\u6362","text":"

\u8fdb\u7a0b\u5207\u6362\u53ea\u4f1a\u53d1\u751f\u5728\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a

  1. \u4e3b\u52a8\u653e\u5f03CPU\uff0c\u8c03\u7528schedule()\u51fd\u6570\u3002
  2. \u6b63\u5728\u8fd0\u884c\u65f6\uff0c\u88ab\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0b\u62a2\u5360\u3002

\u4ece\u672c\u8d28\u4e0a\u6765\u8bf4\uff0c\u8fdb\u7a0b\u5207\u6362\u6709\u4e24\u4e2a\u6b65\u9aa4\uff1a

  1. \u5207\u6362\u9875\u5168\u5c40\u76ee\u5f55\u4ee5\u5b89\u88c5\u4e00\u4e2a\u65b0\u7684\u5730\u5740\u7a7a\u95f4\u3002
  2. \u5207\u6362\u5185\u6838\u6001\u5806\u6808\u548c\u786c\u4ef6\u4e0a\u4e0b\u6587\u3002

\u8fdb\u7a0b\u5207\u6362\u7531switch_to\u5b8f\u6267\u884c\u3002\u8be5\u5b8f\u5b9a\u4e49\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\u3002

"},{"location":"linux/kernel/sched/#_20","title":"\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5","text":""},{"location":"linux/kernel/synch/","title":"\u5185\u6838\u540c\u6b65","text":"

\u53ea\u8981\u6709\u5171\u4eab\u8d44\u6e90\u7684\u5730\u65b9\uff0c\u7a0b\u5e8f\u5458\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u5c31\u9700\u8981\u7279\u522b\u6ce8\u610f\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u9632\u6b62\u5e76\u53d1\u8bbf\u95ee\u65f6\u9020\u6210\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u662f\u9020\u6210\u7cfb\u7edf\u4e0d\u7a33\u5b9a\u7684\u4e00\u7c7b\u9690\u60a3\uff0c\u800c\u8fd9\u7c7b\u9690\u60a3\u5f80\u5f80\u96be\u4ee5\u8ddf\u8e2a\u548c\u8c03\u8bd5\u3002\u8981\u505a\u5230\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u4fdd\u62a4\u76f8\u5f53\u56f0\u96be\u3002\u65e9\u5e74\uff0cLinux\u8fd8\u4e0d\u652f\u6301SMP\uff08\u5bf9\u79f0\u591a\u5904\u7406\u5668\uff09\uff0c\u907f\u514d\u5e76\u53d1\u8bbf\u95ee\u6570\u636e\u8fd8\u6bd4\u8f83\u5bb9\u6613\u3002\u5728\u5355\u4e00\u5904\u7406\u5668\u65f6\uff0c\u53ea\u6709\u5728\u4e2d\u65ad\u53d1\u751f\u7684\u65f6\u5019\uff0c\u6216\u8005\u7a0b\u5e8f\u660e\u786e\u8bf7\u6c42\u91cd\u65b0\u8c03\u5ea6\uff08scheduler\uff09\u7684\u65f6\u5019\uff0c\u6570\u636e\u624d\u6709\u53ef\u80fd\u88ab\u5e76\u53d1\u8bbf\u95ee\u3002\u4e8e\u662f\u5728\u65e9\u671f\u7684\u5355CPU\u67b6\u6784\u4e0b\uff0c\u7b80\u5355\u7684\u7981\u6b62\u4e2d\u65ad\u5373\u53ef\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u3002\u4f46\u662f\u591a\u5904\u7406\u5668\u65f6\u4ee3\u8fd9\u4e00\u65b9\u6cd5\u4e0d\u518d\u6709\u6548\u4e86\uff0c\u56e0\u4e3a\u7981\u6b62\u4e2d\u65ad\u53ea\u80fd\u7981\u6b62\u672c\u5730\u7684\u4e2d\u65ad\uff0c\u4f46\u662f\u65e0\u6cd5\u963b\u6b62\u5176\u4ed6CPU\u5e76\u53d1\u5730\u8bbf\u95ee\u6570\u636e\u3002\u968f\u77402.6\u5185\u6838\u7248\u672c\u7684\u51fa\u73b0\uff0cLinux\u5185\u6838\u5df2\u7ecf\u53d1\u5c55\u6210\u62a2\u5360\u5f0f\u5185\u6838\u3002\u8fd9\u610f\u5473\u7740\u8c03\u5ea6\u7a0b\u5e8f\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u523b\u62a2\u5360\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\uff0c\u91cd\u65b0\u8c03\u5ea6\u5176\u4ed6\u7684\u8fdb\u7a0b\u6267\u884c\uff0c\u8fd9\u5bf9\u6570\u636e\u7684\u540c\u6b65\u63d0\u51fa\u4e86\u66f4\u9ad8\u7684\u8981\u6c42\u3002\u5177\u4f53\u8bf7\u770b\u8fdb\u7a0b\u8c03\u5ea6\u3002

\u5185\u6838\u4e2d\u5e76\u53d1\u6267\u884c\u7684\u539f\u56e0\u6709\uff1a

  • \u4e2d\u65ad\u2014\u2014\u4e2d\u65ad\u662f\u5f02\u6b65\u7684\uff0c\u51e0\u4e4e\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u523b\u53d1\u751f\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u5728\u4efb\u4f55\u65f6\u523b\u6253\u65ad\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\u3002
  • \u8f6f\u4e2d\u65ad\u548ctasklet\u2014\u2014\u5185\u6838\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u523b\u5524\u9192\u6216\u8c03\u5ea6\u8f6f\u4e2d\u65ad\u548ctasklet\u3002
  • \u62a2\u5360\u2014\u2014\u5185\u6838\u662f\u62a2\u5360\u6027\u8d28\u7684\uff0c\u6b63\u5728\u6267\u884c\u7684\u4efb\u52a1\u53ef\u80fd\u4f1a\u88ab\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u62a2\u5360\u3002
  • \u7761\u7720\u2014\u2014\u5728\u5185\u6838\u6267\u884c\u7684\u8fdb\u7a0b\u53ef\u80fd\u7761\u7720\uff0c\u8fd9\u4f1a\u5524\u9192\u8c03\u5ea6\u7a0b\u5e8f\uff0c\u4ece\u800c\u8c03\u5ea6\u53e6\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u3002
  • SMP\u2014\u2014\u591a\u4e2aCPU\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u4ee3\u7801\u3002
"},{"location":"linux/kernel/synch/#_2","title":"\u540c\u6b65\u7684\u6982\u5ff5","text":""},{"location":"linux/kernel/synch/#_3","title":"\u4e34\u754c\u533a","text":"

\u6240\u8c13\u4e34\u754c\u533a\u5c31\u662f\u8bbf\u95ee\u548c\u64cd\u4f5c\u5171\u4eab\u6570\u636e\u7684\u4ee3\u7801\u6bb5\u3002\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u4e34\u754c\u533a\u4ee3\u7801\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u56e0\u6b64\u4e34\u754c\u533a\u4ee3\u7801\u5fc5\u987b\u4ee5\u539f\u5b50\u5730\u65b9\u5f0f\u6267\u884c\u3002\u8003\u8651\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfi\uff0c\u64cd\u4f5c\u4ec5\u4ec5\u662f\u5bf9\u5176\u52a01\u3002

i++;\n

\u5982\u6b64\u7b80\u5355\u7684\u64cd\u4f5c\u5728CPU\u6267\u884c\u7684\u65f6\u5019\u9700\u8981\u4e09\u6761\u6c47\u7f16\u6307\u4ee4\uff1a

  1. \u4ece\u5185\u5b58\u4e2d\u8bfb\u51fa\u53d8\u91cfi\u7684\u503c\u5e76\u653e\u5728\u4e00\u4e2a\u5bc4\u5b58\u5668\u5185\u3002
  2. \u5c06\u5bc4\u5b58\u5668\u4e2d\u7684\u503c+1\u3002
  3. \u628ai\u7684\u503c\u5199\u56de\u5230\u5185\u5b58\u3002

\u5b9e\u9645\u4e0a\uff0c\u5728\u591a\u7ebf\u7a0b\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b\uff0c\u5176\u4ed6\u7ebf\u7a0b\u6709\u53ef\u80fd\u5728\u8fd9\u4e09\u6761\u6307\u4ee4\u95f4\u9699\u7684\u4efb\u610f\u65f6\u523b\u201c\u63d2\u961f\u201d\u3002\u8fd9\u79cd\u6982\u7387\u867d\u7136\u5f88\u5c0f\uff0c\u4f46\u662f\u8ba1\u7b97\u673a\u6bcf\u79d2\u8fd0\u884c\u4e0a\u767e\u4e07\u6761\u6307\u4ee4\uff0c\u201c\u63d2\u961f\u201d\u53ef\u80fd\u6bcf\u8fc7\u51e0\u79d2\u5c31\u53d1\u751f\u4e00\u6b21\u3002\u5047\u8bbe\u6709\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u82e5i\u7684\u521d\u59cb\u503c\u4e3a1\uff0c\u90a3\u4e48\u6211\u4eec\u671f\u671b\u7684\u6700\u7ec8\u7ed3\u679c\u5e94\u8be5\u662f3\u3002\u4f46\u662f\u5047\u5982\u7b2c\u4e8c\u4e2a\u7ebf\u7a0b\u5728\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u6700\u540e\u4e00\u6b65\u4e4b\u524d\u5c31\u53bb\u5185\u5b58\u4e2d\u8bfb\u53d6\u4e86i\u7684\u503c\uff08\u6b64\u65f6i\u7684\u503c\u4ecd\u7136\u662f1\uff09\uff0c\u6211\u4eec\u6700\u540e\u5f97\u5230\u7684i\u7684\u503c\u5c31\u662f2\u800c\u4e0d\u662f3\uff0c\u8fd9\u4e0e\u6211\u4eec\u9884\u671f\u7684\u503c\u4e0d\u7b26\u3002

#include <stdio.h>\n#include <pthread.h>\n\nint i = 0;\n\nvoid *thread_func(void *arg)\n{\n    for(int j = 0 ;j < 1000000; j++)\n    {\n        i++;\n    }\n\n}\n\nint main(int argc, char *argv[])\n{\n\n    pthread_t tid1, tid2;\n    pthread_create(&tid1, NULL, thread_func, NULL);\n    pthread_create(&tid2, NULL, thread_func, NULL);\n\n    pthread_join(tid1, NULL);\n    pthread_join(tid2, NULL);\n\n    printf(\"i = %d\\n\", i);\n    return 0;\n}\n

\u4ee5\u4e0a\u662f\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff0c\u5728\u7f16\u8bd1\u65f6\u8bf7\u52a0\u4e0a-lpthread\uff0c\u4ee5\u94fe\u63a5\u6b63\u786e\u7684\u5e93\u3002\u591a\u6b21\u6267\u884c\u8be5\u7a0b\u5e8f\u540e\u4f60\u4f1a\u53d1\u73b0\u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e0d\u786e\u5b9a\u7684\u3002

\u8fd9\u662f\u6700\u7b80\u5355\u7684\u4e34\u754c\u533a\u4f8b\u5b50\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u7b80\u5355\u7684\u7ade\u4e89\u6761\u4ef6\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u7528\u5230\u590d\u6742\u7684\u9501\u673a\u5236\uff0c\u56e0\u4e3a\u9501\u5bf9\u4e8eCPU\u7684\u6027\u80fd\u6709\u5f88\u5927\u7684\u5f71\u54cd\u3002\u591a\u6570\u5904\u7406\u5668\u90fd\u63d0\u4f9b\u4e86\u6307\u4ee4\u6765\u539f\u5b50\u5730\u8bfb\u3001\u5199\u53d8\u91cf\u3002\u6211\u4eec\u79f0\u4e4b\u4e3a\u539f\u5b50\u6307\u4ee4\u3002\u4f7f\u7528\u539f\u5b50\u6307\u4ee4\u53ef\u4ee5\u89e3\u51b3\u4e00\u4e9b\u7b80\u5355\u7684\u5e76\u53d1\u95ee\u9898\u3002\u4e24\u6761\u539f\u5b50\u6307\u4ee4\u4e0d\u53ef\u80fd\u4ea4\u9519\u6267\u884c\uff0c\u56e0\u4e3a\u5904\u7406\u5668\u4f1a\u4ece\u786c\u4ef6\u4e0a\u7981\u6b62\u8fd9\u79cd\u53ef\u80fd\u6027\u3002

"},{"location":"linux/kernel/synch/#_4","title":"\u52a0\u9501","text":"

\u5f53\u5171\u4eab\u8d44\u6e90\u662f\u4e00\u4e2a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7684i++\u65f6\uff0c\u539f\u5b50\u6307\u4ee4\u5c31\u65e0\u80fd\u4e3a\u529b\u4e86\u3002\u6b64\u65f6\u6211\u4eec\u5fc5\u987b\u5f15\u5165\u9501\u673a\u5236\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3002\u540c\u4e00\u65f6\u523b\uff0c\u6211\u4eec\u53ea\u5141\u8bb8\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u7684\u8bbf\u95ee\u5fc5\u987b\u7b49\u5f85\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e\u9501\u4e4b\u540e\u624d\u80fd\u8fdb\u884c\u3002\u9501\u6709\u591a\u79cd\u591a\u6837\u7684\u5f62\u5f0f\uff0c\u9501\u7684\u7c92\u5ea6\u4e5f\u5404\u4e0d\u76f8\u540c\u3002Linux\u5185\u6838\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0d\u540c\u7684\u9501\u673a\u5236\uff0c\u4ed6\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff1a\u5f53\u9501\u88ab\u5176\u4ed6\u7ebf\u7a0b\u6301\u6709\u800c\u4e0d\u53ef\u7528\u65f6\u7684\u8868\u73b0\u5f62\u5f0f\u2014\u2014\u6709\u4e00\u4e9b\u9501\u4f1a\u5728\u539f\u5730\u7b49\u5f85\uff0c\u800c\u6709\u4e00\u4e9b\u9501\u4f1a\u76f4\u63a5\u7761\u7720\u3002\u9501\u6ca1\u6709\u4f18\u52a3\u4e4b\u5206\uff0c\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u7528\u4e0d\u540c\u7684\u9501\u662f\u7a0b\u5e8f\u5458\u5fc5\u5907\u7684\u6280\u80fd\u3002

\u5f53\u4e00\u4e2a\u9501\u6b63\u5728\u88ab\u5360\u7528\u65f6\uff0c\u6709\u5176\u4ed6\u7ebf\u7a0b\u8bd5\u56fe\u83b7\u5f97\u8be5\u9501\uff0c\u6211\u4eec\u79f0\u4e4b\u4e3a\u9501\u7684\u4e89\u7528\u3002\u7531\u4e8e\u9501\u662f\u8ba9\u7a0b\u5e8f\u4ee5\u4e32\u884c\u7684\u65b9\u5f0f\u5bf9\u8d44\u6e90\u8fdb\u884c\u8bbf\u95ee\uff0c\u88ab\u957f\u65f6\u95f4\u6301\u6709\u7684\u9501\u65e0\u7591\u4f1a\u964d\u4f4e\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u4e8e\u662f\u52a0\u9501\u7c92\u5ea6\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u5982\u679c\u662f\u4e00\u6bb5\u52a0\u9501\u7684\u4ee3\u7801\u88ab\u9891\u7e41\u7684\u8c03\u7528\uff0c\u8fd9\u5f80\u5f80\u4f1a\u6210\u4e3a\u7cfb\u7edf\u6027\u80fd\u7684\u74f6\u9888\u3002\u5728\u4e00\u4e9b\u5927\u578b\u673a\u5668\u4e0a\u53ef\u80fd\u8868\u73b0\u4e0d\u662f\u90a3\u4e48\u660e\u663e\uff0c\u4f46\u662f\u5728\u4e00\u4e9b\u5c0f\u578b\u673a\u5668\u4e0a\uff0c\u8fc7\u7c97\u7684\u9501\u7684\u7c92\u5ea6\u4f1a\u4e25\u91cd\u62d6\u7d2f\u7cfb\u7edf\u7684\u6027\u80fd\u3002

"},{"location":"linux/kernel/synch/#_5","title":"\u6b7b\u9501","text":"

\u6b7b\u9501\u662f\u7f16\u5199\u540c\u6b65\u4ee3\u7801\u65f6\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\u95ee\u9898\uff0c\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u56e0\u4e3a\u4e89\u593a\u8d44\u6e90\u800c\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e\u9501\u6216\u8d44\u6e90\u3002\u5982\u679c\u6ca1\u6709\u5916\u529b\u5e72\u6d89\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u5c06\u6c38\u8fdc\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u3002

\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u6b7b\u9501\u4f8b\u5b50\u5c31\u662f\u81ea\u6b7b\u9501\uff1a\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u8bd5\u56fe\u53bb\u83b7\u5f97\u4e00\u4e2a\u81ea\u5df1\u5df2\u7ecf\u6301\u6709\u7684\u9501\uff0c\u90a3\u4e48\u5b83\u5c06\u6c38\u8fdc\u7b49\u5f85\u4e0b\u53bb\u3002 \u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50\u53ebABBA\u6b7b\u9501\uff1a\u7ebf\u7a0b1\u6301\u6709\u9501A\uff0c\u7ebf\u7a0b2\u6301\u6709\u9501B\uff0c\u7ebf\u7a0b1\u8bd5\u56fe\u53bb\u83b7\u5f97\u9501B\uff0c\u800c\u7ebf\u7a0b2\u8bd5\u56fe\u53bb\u83b7\u5f97\u9501A\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e\u9501\uff0c\u4f46\u662f\u8c01\u90fd\u4e0d\u60f3\u91ca\u653e\u81ea\u5df1\u7684\u9501\uff0c\u4e8e\u662f\u5c31\u9020\u6210\u4e86\u6b7b\u9501\u3002\u9884\u9632\u6b7b\u9501\u7684\u53d1\u751f\u975e\u5e38\u91cd\u8981\uff0c\u867d\u7136\u4f60\u4e0d\u77e5\u9053\u81ea\u5df1\u7684\u4ee3\u7801\u4f1a\u4e0d\u4f1a\u53d1\u751f\u6b7b\u9501\uff0c\u4f46\u662f\u9075\u5faa\u4e00\u4e9b\u7b80\u5355\u7684\u89c4\u5219\u5bf9\u4e8e\u907f\u514d\u6b7b\u9501\u5927\u6709\u5e2e\u52a9\uff1a

  • \u6309\u987a\u5e8f\u52a0\u9501\u3002\u4f7f\u7528\u591a\u4e2a\u9501\u65f6\u5fc5\u987b\u4fdd\u8bc1\u4ee5\u76f8\u540c\u7684\u987a\u5e8f\u83b7\u53d6\u9501\uff0c\u5426\u5219\u5c31\u6709\u53ef\u80fd\u9020\u6210ABBA\u6b7b\u9501\u3002
  • \u9632\u6b62\u9965\u997f\u3002
  • \u4e0d\u8981\u91cd\u590d\u8bf7\u6c42\u540c\u4e00\u4e2a\u9501\u3002
  • \u8bbe\u8ba1\u8d8a\u7b80\u5355\u8d8a\u597d\u3002
"},{"location":"linux/kernel/synch/#_6","title":"\u540c\u6b65\u7684\u65b9\u6cd5","text":""},{"location":"linux/kernel/synch/#_7","title":"\u539f\u5b50\u64cd\u4f5c","text":"

\u539f\u5b50\u64cd\u4f5c\u53ef\u4ee5\u4fdd\u8bc1\u6307\u4ee4\u4ee5\u4e0d\u53ef\u5206\u5272\u7684\u5f62\u5f0f\u6267\u884c\u2014\u2014\u2014\u2014\u6267\u884c\u8fc7\u7a0b\u4e0d\u53ef\u88ab\u6253\u65ad\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u4e24\u7ec4\u539f\u5b50\u64cd\u4f5c\u63a5\u53e3\u2014\u2014\u2014\u2014\u4e00\u7ec4\u9488\u5bf9\u6574\u6570\uff0c\u4e00\u7ec4\u9488\u5bf9\u5355\u72ec\u7684\u4f4d\u3002\u5728Linux\u652f\u6301\u7684\u6240\u6709\u4f53\u7cfb\u7ed3\u6784\u4e0a\u90fd\u5b9e\u73b0\u4e86\u8fd9\u4e24\u7ec4\u63a5\u53e3\u3002\u867d\u7136\u5728\u4e0d\u540c\u4f53\u7cfb\u7ed3\u6784\u4e0a\u5b9e\u73b0\u7684\u65b9\u5f0f\u662f\u4e0d\u540c\u7684\uff0c\u4f46\u662f\u63a5\u53e3\u90fd\u662f\u7edf\u4e00\u7684\u3002

\u6709\u7684\u65f6\u5019\u6211\u4eec\u4f1a\u8981\u6c42\u67d0\u4e9b\u6307\u4ee4\u6309\u7167\u7279\u5b9a\u7684\u987a\u5e8f\u6267\u884c\uff0c\u8fd9\u88ab\u79f0\u4e3a\u987a\u5e8f\u6027\uff0c\u4ee5\u5c4f\u969c\uff08barrier\uff09\u6307\u4ee4\u6765\u5b9e\u73b0\u3002

1.\u539f\u5b50\u6574\u6570\u64cd\u4f5c

\u9488\u5bf9\u6574\u6570\u7684\u539f\u5b50\u64cd\u4f5c\u4f7f\u7528\u4e00\u4e2a\u7279\u6b8a\u7684atomic_t\u7c7b\u578b\u7684\u6570\u636e\u3002\u5b83\u7684\u5b9a\u4e49\u5728<linux/types.h>\u4e2d\uff1a

typedef struct{\n    volatile int counter;\n}atomic_t;\n

\u4f7f\u7528\u539f\u5b50\u6574\u6570\u64cd\u4f5c\u7684\u58f0\u660e\u5728<asm/atomic.h>\u4e2d\u5b9a\u4e49\u3002\u6709\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4f1a\u63d0\u4f9b\u4e00\u4e9b\u989d\u5916\u7684\u539f\u5b50\u64cd\u4f5c\uff0c\u4f46\u662f\u5185\u6838\u7684\u63a5\u53e3\u5728\u6240\u6709\u4f53\u7cfb\u7ed3\u6784\u4e0a\u90fd\u5b9e\u73b0\u4e86\u3002

\u4e00\u4e9b\u7b80\u5355\u7684\u64cd\u4f5c\u6bd4\u5982\uff1a

atomic_t v;   //\u5b9a\u4e49v\natomic_t u = ATOMIC_INIT(0); //\u5b9a\u4e49u\u5e76\u521d\u59cb\u5316\u4e3a0\natomic_set(&v, 4);          //\u8bbe\u7f6ev\u7684\u503c\u4e3a4\natomic_add(2, &v);          //\u5c06v\u7684\u503c\u589e\u52a02\nint w = atomic_read(&v);    //\u5c06\u539f\u5b50\u53d8\u91cfv\u8f6c\u53d8\u4e3aint\u53d8\u91cf\n

\u539f\u5b50\u64cd\u4f5c\u7684\u63a5\u53e3\u975e\u5e38\u7b80\u5355\u6613\u8bfb\uff0c\u6ca1\u6709\u5fc5\u8981\u5355\u72ec\u53bb\u8bb0\uff0c\u7528\u5230\u4ec0\u4e48\u5c31\u5b66\u4ec0\u4e48\u3002

\u539f\u5b50\u64cd\u4f5c\u901a\u5e38\u662f\u5185\u8054\u51fd\u6570\uff0c\u4e14\u662f\u7528\u5185\u5d4c\u6c47\u7f16\u6307\u4ee4\u6765\u5b9e\u73b0\u7684\u3002ARM\u4f53\u7cfb\u8bf7\u53c2\u8003\uff1aARM GCC Inline Assembler\u3002

atomic64_t\u7c7b\u578b\u662f64\u4f4d\u7684\u539f\u5b50\u53d8\u91cf\uff0c\u5176\u529f\u80fd\u548c32\u4f4d\u4e00\u81f4\uff0c\u51fd\u6570\u63a5\u53e3\u4ee5atomic64\u524d\u7f00\u547d\u540d\u3002

typedef struct {\n    volatile long counter;\n}atomic64_t;\n

2.\u539f\u5b50\u4f4d\u64cd\u4f5c

atomic_t\u7c7b\u578b\u5bf9\u6574\u6570\u7b97\u672f\u6765\u8bb2\u6bd4\u8f83\u6709\u7528\u3002\u4f46\u662f\u5f53\u9700\u8981\u4ee5\u539f\u5b50\u5f62\u5f0f\u6765\u64cd\u4f5c\u5355\u4e2a\u7684\u4f4d\u65f6\uff0c\u8fd9\u79cd\u7c7b\u578b\u5c31\u65e0\u6cd5\u6d3e\u4e0a\u7528\u573a\u4e86\u3002Linux\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5bf9\u4f4d\u7684\u539f\u5b50\u64cd\u4f5c\u3002

\u539f\u5b50\u4f4d\u7684\u64cd\u4f5c\u975e\u5e38\u5feb\uff0c\u53ea\u8981\u786c\u4ef6\u5e95\u5c42\u786c\u4ef6\u652f\u6301\uff0c\u8fd9\u79cd\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528\u5355\u4e2a\u673a\u5668\u6307\u4ee4\u6765\u6267\u884c\u3002\u8fd9\u4e9b\u51fd\u6570\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\uff0c\u5b9a\u4e49\u5728<asm/bitops.h>\u4e2d\u3002\u5373\u4f7f\u662f\u5728SMP\u8ba1\u7b97\u673a\u4e0a\uff0c\u8fd9\u4e9b\u51fd\u6570\u4f9d\u65e7\u53ef\u4ee5\u786e\u4fdd\u662f\u539f\u5b50\u7684\u3002\u539f\u5b50\u4f4d\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u4f4d\u53f7+\u6307\u9488\u3002\u53ef\u7528\u7684\u64cd\u4f5c\u5982\u4e0b\uff1a

void set_bit(nr, void *addr);     //\u8bbe\u7f6eaddr\u6307\u5411\u7684\u7b2cnr\u4f4d\nvoid clear_bit(nr, void *addr);   //\u6e05\u9664addr\u6307\u5411\u7684\u7b2cnr\u4f4d\nvoid change_bit(nr, void *addr);  //\u5207\u6362addr\u6307\u5411\u7684\u7b2cnr\u4f4d\ntest_bit(nr, void *addr);         //\u8fd4\u56de\u6307\u5b9a\u4f4d\u7684\u5f53\u524d\u503c\n
"},{"location":"linux/kernel/synch/#_8","title":"\u81ea\u65cb\u9501","text":"

\u539f\u5b50\u64cd\u4f5c\u53ea\u80fd\u9488\u5bf9\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u4fdd\u62a4\uff0c\u73b0\u5b9e\u60c5\u51b5\u91cc\uff0c\u4e00\u4e2a\u4e34\u754c\u533a\u91cc\u751a\u81f3\u6709\u591a\u4e2a\u51fd\u6570\u3002\u6bd4\u5982\u6211\u4eec\u6709\u8fd9\u6837\u4e00\u4e2a\u60c5\u51b5\uff1a\u4ece\u67d0\u4e2a\u51fd\u6570\u4e2d\u8bfb\u53d6\u67d0\u4e2astruct\u7c7b\u578b\u7684\u6570\u636e\uff0c\u7136\u540e\u6539\u53d8\u5176\u4e2d\u67d0\u4e2a\u6210\u5458\u7684\u503c\uff0c\u6700\u540e\u518d\u628a\u66f4\u65b0\u7684\u503c\u8d4b\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u5fc5\u987b\u662f\u4e0d\u53ef\u5206\u5272\u7684\uff0c\u5728\u6570\u636e\u66f4\u65b0\u5b8c\u6bd5\u524d\uff0c\u7edd\u5bf9\u4e0d\u5141\u8bb8\u4efb\u4f55\u5176\u4ed6\u7684\u4ee3\u7801\u8bfb\u53d6\u8fd9\u4e9b\u6570\u636e\u3002\u663e\u7136\uff0c\u6211\u4eec\u4e0d\u53ef\u80fd\u8981\u6c42\u6bcf\u4e2a\u4f53\u7cfb\u90fd\u652f\u6301\u5982\u6b64\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u7528\u5230\u66f4\u590d\u6742\u7684\u540c\u6b65\u65b9\u6cd5\u2014\u2014\u9501\u6765\u63d0\u4f9b\u4fdd\u62a4\u3002

Linux\u5185\u6838\u4e2d\u6700\u5e38\u89c1\u7684\u9501\u662f\u81ea\u65cb\u9501\uff08spin lock\uff09\u3002\u81ea\u65cb\u9501\u53ea\u80fd\u540c\u65f6\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\uff0c\u5982\u679c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8bd5\u56fe\u83b7\u5f97\u4e00\u4e2a\u5df2\u7ecf\u88ab\u5360\u7528\u7684\u81ea\u65cb\u9501\uff0c\u90a3\u4e48\u8be5\u7ebf\u7a0b\u5c06\u4f1a\u8fdb\u5165\u5fd9\u7b49\u5f85\uff0c\u76f4\u5230\u9501\u53ef\u7528\u3002\u901a\u4fd7\u7684\u89e3\u91ca\u5c31\u662f\u4e00\u6247\u95e8\u5bf9\u5e94\u4e00\u628a\u94a5\u5319\uff0c\u8981\u8fdb\u5165\u8fd9\u6247\u95e8\u5fc5\u987b\u5148\u68c0\u67e5\u95e8\u4e0a\u6709\u6ca1\u6709\u94a5\u5319\uff0c\u5982\u679c\u6ca1\u6709\u5c31\u53ea\u80fd\u5728\u5916\u9762\u7b49\u5f85\u76f4\u5230\u91cc\u9762\u7684\u4eba\u51fa\u6765\u3002\u7531\u4e8e\u81ea\u65cb\u4f1a\u4e00\u76f4\u5bfc\u81f4\u7ebf\u7a0b\u5fd9\u7b49\u5f85\uff0c\u767d\u767d\u5360\u7528\u4e86CPU\u8d44\u6e90\u800c\u4e0d\u4f1a\u53bb\u505a\u522b\u7684\u4e8b\u60c5\uff0c\u56e0\u6b64\u7279\u522b\u6d6a\u8d39\u8d44\u6e90\uff0c\u6240\u4ee5\u81ea\u65cb\u9501\u4e0d\u5e94\u8be5\u88ab\u957f\u65f6\u95f4\u5360\u6709\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f7f\u7528\u81ea\u65cb\u9501\u65f6\u4f60\u5fc5\u987b\u4fdd\u8bc1\u4e34\u754c\u533a\u4ee3\u7801\u6267\u884c\u8db3\u591f\u77ed\uff0c\u5426\u5219\u4f1a\u4e25\u91cd\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\u3002

\u81ea\u65cb\u9501\u7684\u5b9e\u73b0\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\uff0c\u4ee3\u7801\u5f80\u5f80\u901a\u8fc7\u6c47\u7f16\u5b9e\u73b0\u3002\u5176\u5b9a\u4e49\u5728<asm/spinlock.h>\u4e2d\uff0c\u5b9e\u9645\u7684\u63a5\u53e3\u5b9a\u4e49\u5728<linux/spinlock.h>\u3002\u81ea\u65cb\u9501\u7684\u57fa\u672c\u4f7f\u7528\u5982\u4e0b\uff1a

DEFINE_SPINLOCK(sp_lock);\nspin_lock(&sp_lock);\n/*\u4e34\u754c\u533a\u4ee3\u7801*/\nspin_unlock(&sp_lock);\n

\u5728\u5355\u5904\u7406\u5668\u4e0a\uff0c\u5982\u679c\u7981\u6b62\u5185\u6838\u62a2\u5360\uff0c\u90a3\u4e48\u81ea\u65cb\u9501\u4e0d\u4f1a\u88ab\u7f16\u8bd1\u8fdb\u5185\u6838\u3002

\u81ea\u65cb\u9501\u53ef\u4ee5\u4f7f\u7528\u5728\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\uff0c\u4f46\u83b7\u53d6\u9501\u4e4b\u524d\uff0c\u5fc5\u987b\u8981\u7981\u6b62\u672c\u5730\u4e2d\u65ad\uff0c\u5426\u5219\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4f1a\u6253\u65ad\u5f53\u524d\u6301\u6709\u9501\u7684\u4ee3\u7801\u3002\u7531\u4e8e\u9501\u7684\u6301\u6709\u548c\u91ca\u653e\u5fc5\u987b\u662f\u539f\u5b50\u7684\uff0c\u5982\u679c\u88ab\u6253\u65ad\u53ef\u80fd\u4f1a\u5f71\u54cd\u9501\u7684\u4e00\u81f4\u6027\uff0c\u8fd9\u4f1a\u5f15\u8d77\u4e0d\u53ef\u9884\u6d4b\u7684\u540e\u679c\u3002\u751a\u81f3\uff0c\u5982\u679c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u53bb\u4e89\u7528\u8fd9\u4e2a\u5df2\u7ecf\u88ab\u6301\u6709\u7684\u9501\uff0c\u90a3\u4e48\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002\u6ce8\u610f\uff0c\u8981\u5173\u95ed\u7684\u53ea\u662f\u5f53\u524d\u5904\u7406\u5668\u7684\u4e2d\u65ad\uff0c\u5176\u4ed6\u5904\u7406\u5668\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u81ea\u65cb\u5e76\u4e0d\u4f1a\u59a8\u788d\u5f53\u524d\u5904\u7406\u5668\u91ca\u653e\u9501\u3002

\u5185\u6838\u63d0\u4f9b\u4e86\u7981\u6b62\u4e2d\u65ad\u540c\u65f6\u8bf7\u6c42\u9501\u7684\u63a5\u53e3\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a

DEFINE_SPINLOCK(sp_lock);\nunsigned long flags;\nspin_lock_irqsave(&sp_lock, flags);\nspin_unlock_irqrestore(&sp_lock, flags);\n

\u51fd\u6570spin_lock_irqsave()\u4fdd\u5b58\u4e2d\u65ad\u72b6\u6001\uff0c\u5e76\u7981\u6b62\u672c\u5730\u4e2d\u65ad\uff0c\u7136\u540e\u518d\u53bb\u83b7\u53d6\u9501\u3002\u53cd\u8fc7\u6765spin_unlock_irqrestore()\u89e3\u9501\uff0c\u5e76\u8ba9\u4e2d\u65ad\u6062\u590d\u5230\u52a0\u9501\u524d\u7684\u72b6\u6001\u3002\u6240\u4ee5\u5373\u4fbf\u662f\u52a0\u9501\u524d\u4e2d\u65ad\u5df2\u7ecf\u88ab\u7981\u6b62\u4e86\uff0c\u4ee3\u7801\u4e5f\u4e0d\u4f1a\u9519\u8bef\u5730\u6fc0\u6d3b\u4e2d\u65ad\uff0c\u800c\u662f\u8ba9\u4e2d\u65ad\u4fdd\u6301\u7981\u6b62\u72b6\u6001\u3002

\u5982\u679c\u4f60\u80fd\u786e\u5b9a\u4e2d\u65ad\u5728\u52a0\u9501\u524d\u662f\u6fc0\u6d3b\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u914d\u5bf9\u4f7f\u7528spin_lock_irq()\u548cspin_unlock_irq()\uff0c\u4ece\u800c\u5728\u89e3\u9501\u65f6\u76f4\u63a5\u6fc0\u6d3b\u4e2d\u65ad\u3002\u4f46\u662f\u5185\u6838\u7684\u5e9e\u5927\u6027\u5f80\u5f80\u8ba9\u4eba\u5f88\u96be\u641e\u6e05\u695a\u4e2d\u65ad\u5728\u5f53\u524d\u6267\u884c\u4ee3\u7801\u524d\u662f\u5426\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\uff0c\u6240\u4ee5\u8be5\u65b9\u6cd5\u5e76\u4e0d\u63d0\u5021\u3002

\u7531\u4e8e\u4e2d\u65ad\u4e0b\u534a\u90e8\u53ef\u4ee5\u62a2\u5360\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u7684\u4ee3\u7801\uff0c\u56e0\u6b64\u52a0\u9501\u7684\u540c\u65f6\u8fd8\u9700\u8981\u7981\u6b62\u4e0b\u534a\u90e8\u6267\u884c\u3002\u51fd\u6570spin_lock_bh()\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u540c\u6837\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u53ef\u4ee5\u62a2\u5360\u4e0b\u534a\u90e8\uff0c\u56e0\u6b64\u5982\u679c\u4e0b\u534a\u90e8\u6709\u9700\u8981\u548c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5171\u4eab\u7684\u6570\u636e\u65f6\uff0c\u4e5f\u5fc5\u987b\u7981\u6b62\u4e2d\u65ad\u3002

\u4e2d\u65ad\u5904\u7406\u662f\u53e6\u4e00\u4e2a\u8bdd\u9898\uff0c\u8bf7\u53c2\u8003\u4e2d\u65ad\u548c\u4e2d\u65ad\u5904\u7406\u3002

"},{"location":"linux/kernel/synch/#_9","title":"\u8bfb/\u5199\u81ea\u65cb\u9501","text":"

\u6709\u65f6\uff0c\u9501\u7684\u7528\u9014\u53ef\u4ee5\u660e\u786e\u5730\u5206\u4e3a\u8bfb\u548c\u5199\u4e24\u4e2a\u573a\u666f\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u9700\u8981\u5927\u91cf\u8bfb\u64cd\u4f5c\uff0c\u800c\u5199\u64cd\u4f5c\u5f88\u5c11\u7684\u60c5\u51b5\u65f6\uff0c\u5f15\u5165\u8bfb\u5199\u9501\u53ef\u4ee5\u5f88\u5927\u5730\u6539\u5584\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u8bfb\u5199\u9501\u7684\u57fa\u672c\u903b\u8f91\u662f\uff1a\u8bfb\u6a21\u5f0f\u662f\u5171\u4eab\u7684\uff0c\u5199\u6a21\u5f0f\u662f\u72ec\u5360\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u5f53\u8fdb\u884c\u5199\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u53ea\u80fd\u7531\u5355\u4e2a\u4efb\u52a1\u8fdb\u884c\u3002\u800c\u8fdb\u884c\u8bfb\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5e76\u53d1\u7684\u6267\u884c\u800c\u4e0d\u7528\u62c5\u5fc3\u5b89\u5168\u95ee\u9898\u3002\u8bfb/\u5199\u9501\u7684\u57fa\u672c\u65b9\u6cd5\u5982\u4e0b\uff1a

DEFINE_RWLOCK(rw_lock);\n//\u8bfb\u64cd\u4f5c\nread_lock(&rw_lock);\nread_unlock(&rw_lock);\n//\u5199\u64cd\u4f5c\nwrite_lock(&rw_lock);\nwrite_unlock(&rw_lock);\n

\u901a\u5e38\uff0c\u8bfb\u5199\u64cd\u4f5c\u5e94\u5904\u4e8e\u5b8c\u5168\u5206\u5272\u5f00\u7684\u4ee3\u7801\u5206\u652f\u4e2d\uff0c\u5982\u679c\u8bfb\u5199\u64cd\u4f5c\u4e0d\u80fd\u6e05\u6670\u5730\u8fa8\u522b\uff0c\u4e0d\u8981\u4f7f\u7528\u8bfb\u5199\u9501\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002\u4f8b\u5982\u5728\u6267\u884c\u8bfb\u64cd\u4f5c\u4ee3\u7801\u65f6\uff0c\u4e5f\u52a0\u5165\u4e86\u5199\u64cd\u4f5c\u3002

\u5728\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\uff0c\u5982\u679c\u53ea\u6709\u8bfb\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528read_lock()\u6765\u4fdd\u62a4\u8bfb\u64cd\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u7981\u6b62\u4e2d\u65ad\u4e86\u3002\u4f46\u662f\uff0c\u5199\u64cd\u4f5c\u7684\u4e2d\u65ad\u5fc5\u987b\u7981\u6b62\uff0c\u5426\u5219\u5c31\u4f1a\u9020\u6210\u6b7b\u9501\u3002\u56e0\u4e3a\u5199\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\u8bfb\u64cd\u4f5c\u91ca\u653e\u9501\uff0c\u800c\u8bfb\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\u5199\u64cd\u4f5c\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd4\u56de\u3002

\u5728\u4f7f\u7528\u8bfb\u5199\u9501\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\uff0c\u5f53\u8bfb\u9501\u88ab\u6301\u6709\u65f6\uff0c\u5199\u9501\u53ea\u80fd\u7b49\u5f85\u3002\u7136\u800c\u8bfb\u9501\u5374\u53ef\u4ee5\u7ee7\u7eed\u6210\u529f\u5730\u5360\u7528\u9501\u3002\u8fd9\u4f1a\u9020\u6210\u5199\u9501\u9965\u997f\u73b0\u8c61\u3002

\u5982\u679c\u52a0\u9501\u65f6\u95f4\u5f88\u957f\u6216\u8005\u4ee3\u7801\u5728\u6301\u6709\u9501\u65f6\u6709\u53ef\u80fd\u7761\u7720\uff0c\u90a3\u4e48\u6700\u597d\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u5904\u7406\u3002

"},{"location":"linux/kernel/synch/#_10","title":"\u987a\u5e8f\u9501","text":"

\u987a\u5e8f\u9501\uff08seqlock\uff09\u5728struct fs_struct\u4e2d\u88ab\u4f7f\u7528\u3002

"},{"location":"linux/kernel/synch/#_11","title":"\u4fe1\u53f7\u91cf","text":"

Linux\u4e2d\u7684\u4fe1\u53f7\u91cf\u662f\u4e00\u79cd\u7761\u7720\u9501\u3002\u5f53\u6709\u4e00\u4e2a\u4efb\u52a1\u8bd5\u56fe\u83b7\u5f97\u4e00\u4e2a\u5df2\u7ecf\u88ab\u5360\u7528\u7684\u4fe1\u53f7\u91cf\u65f6\uff0c\u8be5\u4efb\u52a1\u4f1a\u52a0\u5165\u7b49\u5f85\u961f\u5217\uff0c\u7136\u540e\u7761\u7720\u3002\u7531\u4e8e\u91ca\u653e\u4e86\u8d44\u6e90\u5904\u7406\u5668\u53ef\u4ee5\u53bb\u6267\u884c\u5176\u4ed6\u7684\u4ee3\u7801\u3002\u5f53\u6301\u6709\u7684\u4fe1\u53f7\u91cf\u88ab\u91ca\u653e\u65f6\uff0c\u5904\u4e8e\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u90a3\u4e2a\u4efb\u52a1\u4f1a\u88ab\u5524\u9192\uff0c\u5e76\u83b7\u5f97\u8be5\u4fe1\u53f7\u91cf\u3002

\u5b9e\u9645\u4e0a\uff0cLinux\u63d0\u4f9b\u4e24\u79cd\u4fe1\u53f7\u91cf\uff1a

  • \u5185\u6838\u4fe1\u53f7\u91cf\uff0c\u7531\u5185\u6838\u6001\u4f7f\u7528
  • System V IPC\u4fe1\u53f7\u91cf\uff0c\u7531\u7528\u6237\u6001\u4f7f\u7528

\u7531\u4e8e\u4fe1\u53f7\u91cf\u4f1a\u7761\u7720\uff0c\u56e0\u6b64\u6709\u4ee5\u4e0b\u7ed3\u8bba\uff1a

  • \u4fe1\u53f7\u91cf\u9002\u7528\u4e8e\u9501\u4f1a\u88ab\u957f\u65f6\u95f4\u5360\u6709\u7684\u573a\u666f\u3002
  • \u4fe1\u53f7\u91cf\u53ea\u80fd\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u4e2d\u65ad\u4e0a\u4e0b\u6587\u7981\u6b62\u7761\u7720\u3002
  • \u591a\u4e2a\u8fdb\u7a0b\u8bd5\u56fe\u83b7\u5f97\u4fe1\u53f7\u91cf\u4e0d\u4f1a\u6b7b\u9501\u3002
  • \u5982\u679c\u5df2\u7ecf\u5360\u7528\u4e86\u4fe1\u53f7\u91cf\uff0c\u4e0d\u80fd\u518d\u4f7f\u7528\u81ea\u65cb\u9501\uff0c\u56e0\u4e3a\u81ea\u65cb\u9501\u7981\u6b62\u7761\u7720\u3002

\u4fe1\u53f7\u91cf\u76f8\u6bd4\u81ea\u65cb\u9501\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5730\u65b9\uff0c\u5c31\u662f\u5b83\u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2acount\u503c\uff0c\u8be5count\u503c\u7b49\u540c\u4e8e\u540c\u4e00\u65f6\u95f4\u80fd\u591f\u6301\u6709\u4fe1\u53f7\u91cf\u7684\u6570\u91cf\u3002\u5982\u679c\u8fd9\u4e2a\u503c\u662f1\uff0c\u90a3\u4e48\u4fe1\u53f7\u91cf\u53c8\u88ab\u79f0\u4e3a\u4e92\u65a5\u4fe1\u53f7\u91cf\u3002\u4fe1\u53f7\u91cf\u652f\u6301\u4e24\u4e2a\u64cd\u4f5c\uff1adown()\u64cd\u4f5c\u901a\u8fc7\u5bf9\u4fe1\u53f7\u91cf\u8ba1\u6570\u51cf1\u6765\u83b7\u5f97\u5b83\uff0c\u800cup()\u64cd\u4f5c\u52a01\u6765\u91ca\u653e\u5b83\u3002

\u4fe1\u53f7\u91cf\u662f\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\uff0c\u5b9a\u4e49\u5728<asm/semaphore.h>\u4e2d\u3002\u521b\u5efa\u65b9\u6cd5\u5982\u4e0b\uff1a

struct semaphore name;\nsema_init(&name, count);\n

\u51fd\u6570down_interruptible()\u8bd5\u56fe\u83b7\u53d6\u6307\u5b9a\u7684\u4fe1\u53f7\u91cf\uff0c\u5982\u679c\u4e0d\u53ef\u7528\uff0c\u5219\u5c06\u8fdb\u7a0b\u8bbe\u7f6e\u4e3aTASK_INTERRUPTIBLE\u3002\u8fd9\u79cd\u8fdb\u7a0b\u72b6\u6001\u610f\u5473\u7740\u4efb\u52a1\u53ef\u4ee5\u88ab\u4fe1\u53f7\u5524\u9192\u3002\u4f7f\u7528down_trylock()\u51fd\u6570\uff0c\u5728\u4fe1\u53f7\u91cf\u5df2\u7ecf\u88ab\u5360\u7528\u65f6\uff0c\u7acb\u5373\u8fd4\u56de\u800c\u4e0d\u662f\u8ba9\u8fdb\u7a0b\u7761\u7720\u3002

"},{"location":"linux/kernel/synch/#_12","title":"\u4e92\u65a5\u4f53","text":"

\u5728\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4fe1\u53f7\u91cf\u53ea\u662f\u4f5c\u4e3a\u4e00\u4e2a\u8ba1\u6570\u4e3a1\u7684\u5141\u8bb8\u7761\u7720\u7684\u81ea\u65cb\u9501\u5b58\u5728\u3002\u4e3a\u4e86\u627e\u5230\u4e00\u4e2a\u66f4\u7b80\u5355\u4e14\u53ef\u4ee5\u7761\u7720\u7684\u9501\uff0c\u5f00\u53d1\u8005\u4eec\u5f15\u5165\u4e86\u4e92\u65a5\u4f53\uff08mutex\uff09\u3002\u5176\u884c\u4e3a\u548c\u8ba1\u6570\u4e3a1\u7684\u4fe1\u53f7\u91cf\u7c7b\u4f3c\uff0c\u4f46\u662f\u64cd\u4f5c\u7684\u63a5\u53e3\u66f4\u7b80\u5355\uff0c\u5b9e\u73b0\u4e5f\u66f4\u9ad8\u6548\u3002\u57fa\u672c\u9002\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a

DEFINE_MUTEX(mutex);\nmutex_init(&mutex);\nmutex_lock(&mutex);\nmutex_unlock(&mutex);\n

mutex\u7684\u7b80\u4ecb\u4e0e\u9ad8\u6548\u6e90\u4e8e\u76f8\u6bd4\u4f7f\u7528\u4fe1\u53f7\u91cf\u66f4\u591a\u7684\u53d7\u9650\u6027\uff1a

  • \u4efb\u4f55\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u4efb\u52a1\u6301\u6709mutex\u3002
  • \u5fc5\u987b\u7531\u4e0a\u9501\u8005\u89e3\u9501\u2014\u2014\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e0a\u9501\uff0c\u800c\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u89e3\u9501\u3002
  • \u9012\u5f52\u5730\u4e0a\u9501\u548c\u89e3\u9501\u65f6\u4e0d\u88ab\u5141\u8bb8\u7684\u3002
  • \u5f53\u6301\u6709\u4e00\u4e2amutex\u65f6\uff0c\u8fdb\u7a0b\u4e0d\u80fd\u9000\u51fa\u3002
  • mutex\u4e0d\u80fd\u5728\u4e2d\u65ad\u6216\u8005\u4e0b\u534a\u90e8\u4e2d\u4f7f\u7528\u3002
\u9700\u6c42 \u5efa\u8bae\u7684\u52a0\u9501\u65b9\u5f0f \u4f4e\u5f00\u9500\u52a0\u9501 \u4f18\u5148\u4f7f\u7528\u81ea\u65cb\u9501 \u77ed\u671f\u52a0\u9501 \u4f18\u5148\u4f7f\u7528\u81ea\u65cb\u9501 \u957f\u671f\u52a0\u9501 \u4f18\u5148\u4f7f\u7528\u4e92\u65a5\u4f53 \u4e2d\u65ad\u4e0a\u4e0b\u6587\u52a0\u9501 \u4f7f\u7528\u81ea\u65cb\u9501 \u6301\u6709\u9501\u9700\u8981\u7761\u7720 \u4f7f\u7528\u4e92\u65a5\u4f53"},{"location":"linux/kernel/synch/#_13","title":"\u5b8c\u6210\u53d8\u91cf","text":"

\u5982\u679c\u5728\u5185\u6838\u4e2d\u4e00\u4e2a\u4efb\u52a1\u9700\u8981\u53d1\u9001\u4fe1\u53f7\u901a\u77e5\u53e6\u4e00\u4e2a\u4efb\u52a1\u53d1\u751f\u4e86\u67d0\u79cd\u7279\u5b9a\u4e8b\u4ef6\uff0c\u6b64\u65f6\u53ef\u4ee5\u7528\u5b8c\u6210\u53d8\u91cf\uff08completion variable\uff09\u3002\u5f53\u67d0\u4e2a\u4efb\u52a1\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u4f1a\u4f7f\u7528\u5b8c\u6210\u53d8\u91cf\u53bb\u5524\u9192\u6b63\u5728\u7b49\u5f85\u7684\u4efb\u52a1\u3002

\u5b8c\u6210\u53d8\u91cf\u7531\u7ed3\u6784\u4f53completion\u8868\u793a\uff0c\u5b9a\u4e49\u5728<linux/completion.h>\u4e2d\u3002\u5176\u521b\u5efa\u65b9\u6cd5\u5982\u4e0b\uff1a

DECLARE_COMPLETION(comp);\n

\u6216\u8005\u4f7f\u7528init_completion()\u52a8\u6001\u521b\u5efa\u3002\u9700\u8981\u7b49\u5f85\u7684\u4efb\u52a1\u8c03\u7528wait_for_completion()\u6765\u7b49\u5f85\u7279\u5b9a\u4e8b\u4ef6\u3002\u5f53\u4e8b\u4ef6\u53d1\u751f\u540e\uff0c\u4ea7\u751f\u4e8b\u4ef6\u7684\u4efb\u52a1\u8c03\u7528complete()\u6765\u53d1\u9001\u4fe1\u53f7\u5524\u9192\u6b63\u5728\u7b49\u5f85\u7684\u4efb\u52a1\u3002

"},{"location":"linux/kernel/synch/#rcu","title":"RCU\u673a\u5236","text":"

\u8bfb\u53d6-\u590d\u5236-\u66f4\u65b0\uff08read-copy-update\uff09\u662f\u4e00\u79cd\u9ad8\u7ea7\u4e92\u65a5\u673a\u5236\uff0c\u4e00\u822c\u7528\u4e0d\u5230\u4f46\u662f\u6211\u4eec\u5bf9\u8fd9\u4e2a\u6982\u5ff5\u5f97\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u4e86\u89e3\u3002RCU\u673a\u5236\u4e3b\u8981\u9488\u5bf9\u8bfb\u53d6\u7ecf\u5e38\u53d1\u751f\u3001\u4f46\u662f\u5199\u5165\u5f88\u5c11\u7684\u60c5\u51b5\u3002\u5728\u9700\u8981\u4fee\u6539\u6570\u636e\u65f6\uff0c\u5199\u5165\u7ebf\u7a0b\u9996\u5148\u590d\u5236\u4e00\u4efd\uff0c\u7136\u540e\u4fee\u6539\u526f\u672c\u3002\u5176\u4ed6\u7ebf\u7a0b\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u4ecd\u7136\u6307\u5411\u539f\u59cb\u7684\u5171\u4eab\u6570\u636e\u5730\u5740\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u5728\u5199\u64cd\u4f5c\u53d1\u751f\u524d\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4ecd\u7136\u53ef\u4ee5\u8bfb\u53d6\u5230\u4e00\u81f4\u7684\u6570\u636e\u3002\u76f4\u5230\u66f4\u65b0\u7ebf\u7a0b\u5b8c\u6210\u4e86\u6570\u636e\u7684\u4fee\u6539\uff0c\u5e76\u901a\u8fc7\u7279\u5b9a\u7684API\u5c06\u66f4\u65b0\u540e\u7684\u6570\u636e\u6307\u9488\u8d4b\u503c\u56de\u539f\u6765\u7684\u5171\u4eab\u6570\u636e\u4f4d\u7f6e\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u201c\u66f4\u65b0\u5b8c\u6210\u201d\u3002\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\u7528\u4e8e\u4fdd\u8bc1\u53ea\u6709\u5728\u6570\u636e\u7ed3\u6784\u88ab\u4fee\u6539\u4e4b\u540e\uff0c\u5df2\u66f4\u65b0\u7684\u6307\u9488\u624d\u5bf9\u5176\u4ed6CPU\u53ef\u89c1\u3002\u8bfb\u53d6\u7aef\u7684\u4ee3\u7801\u5fc5\u987b\u653e\u7f6e\u4e8ercu_read_lock()\u548crcu_read_unlock()\u4e4b\u95f4\u3002

\u4f7f\u7528RCU\u6280\u672f\u7684\u96be\u70b9\u5728\u4e8e\uff1a\u5199\u5165\u7aef\u4fee\u6539\u6307\u9488\u65f6\u4e0d\u80fd\u7acb\u523b\u91ca\u653e\u6570\u636e\u7ed3\u6784\u7684\u65e7\u6307\u9488\uff0c\u56e0\u4e3a\u8fd8\u6709\u5176\u4ed6\u7684\u8bfb\u53d6\u7aef\u5728\u4f7f\u7528\u3002\u53ea\u6709\u5f53\u6240\u6709\u7684\u8bfb\u53d6\u7aef\u6267\u884c\u5b8c\u5b8frcu_read_unlcok()\u4e4b\u540e\uff0c\u624d\u53ef\u4ee5\u91ca\u653e\u65e7\u6307\u9488\u3002\u5199\u5165\u7aef\u8c03\u7528\u51fd\u6570call_rcu()\u6765\u91ca\u653e\u65e7\u6307\u9488\u3002

"},{"location":"linux/kernel/synch/#_14","title":"\u7981\u6b62\u62a2\u5360","text":"

Linux\u662f\u62a2\u5360\u5f0f\u5185\u6838\uff0c\u5176\u4e3b\u8981\u7279\u70b9\u662f\uff1a\u4e00\u4e2a\u5728\u5185\u6838\u6001\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u53ef\u80fd\u5728\u6267\u884c\u5185\u6838\u6001\u51fd\u6570\u671f\u95f4\u88ab\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u62a2\u5360\u3002\u5728\u8fdb\u7a0bA\u6267\u884c\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u65f6\uff08\u6b21\u5403\u4f4d\u4e8e\u5185\u6838\u6001\uff09\uff0c\u4e00\u4e2a\u66f4\u6709\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0bB\u53d8\u4e3a\u53ef\u6267\u884c\u72b6\u6001\u3002\u5982\u679c\u5185\u6838\u662f\u53ef\u62a2\u5360\u7684\uff0c\u5c31\u4f1a\u53d1\u751f\u5f3a\u5236\u6027\u4efb\u52a1\u5207\u6362\uff0c\u8ba9B\u53d6\u4ee3A\u3002\u518d\u6bd4\u5982\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u5df2\u7ecf\u7528\u5b8c\u4e86\u5b83\u7684\u65f6\u95f4\u7247\u914d\u989d\uff0c\u62a2\u5360\u5f0f\u5185\u6838\u4f1a\u7acb\u523b\u8ba9\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u53d6\u4ee3\u5b83\u3002

\u5185\u6838\u4f7f\u7528thread_info\u4e2d\u7684preempt_count\u5b57\u6bb5\u8868\u793a\u62a2\u5360\u8ba1\u6570\u3002\u5f53\u8fd9\u4e2a\u503c\u5927\u4e8e0\u65f6\uff0c\u5c31\u7981\u6b62\u5185\u6838\u62a2\u5360\u3002\u5b83\u5728\u4ee5\u4e0b\u4efb\u4f55\u4e00\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u53d6\u503c\u90fd\u5927\u4e8e0\uff1a

  1. \u5185\u6838\u6b63\u5728\u6267\u884c\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f\u3002

  2. \u53ef\u5ef6\u8fdf\u51fd\u6570\u88ab\u7981\u6b62\uff08\u5f53\u5185\u6838\u6b63\u5728\u6267\u884c\u8f6f\u4e2d\u65ad\u6216tasklet\uff09\u3002

  3. \u663e\u793a\u8bbe\u7f6e\u62a2\u5360\u8ba1\u6570\u5668\u4e3a\u6b63\u6570\u3002

\u5b8f \u8bf4\u660e preempt_count() \u8fd4\u56de\u62a2\u5360\u8ba1\u6570\u503c preempt_disable() \u4f7f\u62a2\u5360\u8ba1\u6570+1 preempt_enable() \u4f7f\u62a2\u5360\u8ba1\u6570-1\uff0c\u5e76\u68c0\u67e5TIF_NEED_RESCHED\u6807\u5fd7 preempt_enable_no_resched() \u4f7f\u62a2\u5360\u8ba1\u6570-1

\u4e0a\u9762\u7b2c\u4e09\u4e2a\u5b8f\u9700\u8981\u8bf4\u660e\u4e00\u4e0b\u3002preempt_enable()\u5b8f\u9996\u5148\u9012\u51cf\u62a2\u5360\u8ba1\u6570\u5668\uff0c\u5e76\u4e14\u68c0\u67e5TIF_NEED_RESCHED\u6807\u5fd7\u662f\u5426\u88ab\u8bbe\u7f6e\u3002\u5f53\u8fd9\u4e2a\u6807\u5fd7\u4e3a1\u65f6\u8868\u793a\u9700\u8981\u6267\u884c\u8c03\u5ea6\u7a0b\u5e8f\u3002\u4e8e\u662f\u6211\u4eec\u8fd8\u4f1a\u8c03\u7528preempt_schedule()\u6765\u8c03\u7528schedule()\u9009\u62e9\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u8fd0\u884c\u3002

"},{"location":"linux/kernel/synch/#_15","title":"\u987a\u5e8f\u4e0e\u5c4f\u969c","text":"

\u4e3a\u4ec0\u4e48\u9700\u8981\u987a\u5e8f\u548c\u5c4f\u969c\u6307\u4ee4\uff1f

  • \u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\u5bfc\u81f4\u7684\u91cd\u6392\uff1a\u73b0\u4ee3\u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c\u5404\u79cd\u4f18\u5316\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u5305\u62ec\u6307\u4ee4\u91cd\u6392\u3002\u5728\u4e0d\u8003\u8651\u5185\u5b58\u64cd\u4f5c\u987a\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u6539\u53d8\u6307\u4ee4\u7684\u6267\u884c\u987a\u5e8f\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u7684\u884c\u4e3a\u4e0e\u9884\u671f\u4e0d\u7b26\u3002\u901a\u8fc7\u4f7f\u7528\u987a\u5e8f\u548c\u5c4f\u969c\u6307\u4ee4\uff0c\u53ef\u4ee5\u9650\u5236\u7f16\u8bd1\u5668\u5bf9\u67d0\u4e9b\u5173\u952e\u4ee3\u7801\u6bb5\u7684\u4f18\u5316\u3002

  • \u4fdd\u8bc1\u5e76\u53d1\u64cd\u4f5c\u7684\u4e00\u81f4\u6027\uff1a\u5728\u591a\u6838\u5904\u7406\u5668\u548c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8c03\u5ea6\u5668\u53ef\u80fd\u4f1a\u5728\u4e0d\u540c\u7684\u5904\u7406\u5668\u6838\u5fc3\u4e0a\u5e76\u884c\u6267\u884c\u591a\u4e2a\u7ebf\u7a0b\u3002\u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u9700\u8981\u786e\u4fdd\u6240\u6709\u6838\u5fc3\u4e0a\u7684\u5185\u5b58\u64cd\u4f5c\u90fd\u6309\u7167\u7a0b\u5e8f\u6307\u5b9a\u7684\u987a\u5e8f\u6267\u884c\u3002

  • \u89e3\u51b3CPU\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\uff1aCPU\u7f13\u5b58\u662f\u5904\u7406\u5668\u901f\u5ea6\u7684\u5173\u952e\u90e8\u5206\uff0c\u4f46\u5b83\u7684\u5b58\u5728\u4e5f\u5e26\u6765\u4e86\u7f13\u5b58\u4e00\u81f4\u6027\u7684\u95ee\u9898\u3002\u5f53\u4e00\u4e2aCPU\u6838\u5fc3\u5199\u5165\u6570\u636e\uff0c\u800c\u8fd9\u4e2a\u6570\u636e\u53c8\u88ab\u53e6\u4e00\u4e2a\u6838\u5fc3\u7684\u7f13\u5b58\u6240\u7f13\u5b58\u65f6\uff0c\u6ca1\u6709\u5c4f\u969c\u7684\u8bdd\uff0c\u53e6\u4e00\u4e2a\u6838\u5fc3\u53ef\u80fd\u4f1a\u8bfb\u53d6\u5230\u65e7\u7684\u6570\u636e\u7248\u672c\u3002\u5185\u5b58\u5c4f\u969c\u80fd\u591f\u786e\u4fdd\u6240\u6709\u6838\u5fc3\u770b\u5230\u5185\u5b58\u64cd\u4f5c\u7684\u6700\u7ec8\u4e00\u81f4\u6027\u3002

Linux\u4e2d\u7684\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\uff1a

\u5b8f \u8bf4\u660e mb() \u9002\u7528\u4e8eMP\u548cUP\u7684\u5185\u5b58\u5c4f\u969c rmb() \u9002\u7528\u4e8eMP\u548cUP\u7684\u8bfb\u5185\u5b58\u5c4f\u969c wmb() \u9002\u7528\u4e8eMP\u548cUP\u7684\u5199\u5185\u5b58\u5c4f\u969c smp_mb() \u4ec5\u9002\u7528\u4e8eMP\u7684\u5185\u5b58\u5c4f\u969c smp_rmb() \u4ec5\u9002\u7528\u4e8eMP\u7684\u8bfb\u5185\u5b58\u5c4f\u969c smp_wmb() \u4ec5\u9002\u7528\u4e8eMP\u7684\u5199\u5185\u5b58\u5c4f\u969c

\u6b64\u7c7b\u6307\u4ee4\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\uff0c\u8bf7\u53c2\u8003ARM\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\u3002

"},{"location":"linux/kernel/vfs/","title":"\u865a\u62df\u6587\u4ef6\u7cfb\u7edf","text":"

\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff08VFS\uff09\u662fLinux\u5185\u6838\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u7cfb\u7edf\uff0c\u5b83\u4e3a\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u4e00\u5957\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u4ee5\u5b9e\u73b0\u5bf9\u4e0d\u540c\u6587\u4ef6\u7cfb\u7edf\u7684\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd9\u4e9b\u63a5\u53e3\uff0c\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u53ef\u4ee5\u91c7\u7528\u6807\u51c6\u7684\u7cfb\u7edf\u8c03\u7528\u6765\u6267\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u5173\u5fc3\u8fd9\u4e9b\u64cd\u4f5c\u5b9e\u9645\u662f\u5728\u54ea\u79cd\u6587\u4ef6\u7cfb\u7edf\u4e0a\u6267\u884c\u7684\u3002VFS\u4f7f\u5f97\u5185\u6838\u80fd\u591f\u652f\u6301\u591a\u79cd\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b\uff0c\u5982ext4\u3001ReiserFS\u3001XFS\u7b49\u3002\u5bf9\u4e8e\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7cfb\u7edf\u770b\u8d77\u6765\u90fd\u662f\u4e00\u6837\u7684\uff0c\u56e0\u4e3aVFS\u9690\u85cf\u4e86\u5e95\u5c42\u6587\u4ef6\u7cfb\u7edf\u7684\u5dee\u5f02\u3002VFS\u7684\u8bbe\u8ba1\u4f7f\u5f97Linux\u5185\u6838\u80fd\u591f\u7075\u6d3b\u5730\u652f\u6301\u591a\u79cd\u6587\u4ef6\u7cfb\u7edf\uff0c\u540c\u65f6\u4fdd\u6301\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002\u8fd9\u5bf9\u4e8eLinux\u7cfb\u7edf\u7684\u53ef\u6269\u5c55\u6027\u548c\u7075\u6d3b\u6027\u81f3\u5173\u91cd\u8981\u3002

\u4e3a\u4e86\u652f\u6301\u591a\u6587\u4ef6\u7cfb\u7edf\uff0cVFS\u63d0\u4f9b\u4e86\u4e00\u4e2a\u901a\u7528\u6587\u4ef6\u7cfb\u7edf\u6a21\u578b\u7684\u62bd\u8c61\u5c42\uff0c\u8be5\u6a21\u578b\u56ca\u62ec\u4e86\u4efb\u4f55\u6587\u4ef6\u7cfb\u7edf\u5e38\u7528\u7684\u529f\u80fd\u548c\u884c\u4e3a\u3002\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u53ea\u9700\u8981\u4f7f\u7528VFS\u66b4\u9732\u7684\u63a5\u53e3\uff0c\u800c\u65e0\u987b\u5173\u5fc3\u5e95\u5c42\u6587\u4ef6\u7cfb\u7edf\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u6bd4\u5982\u8c03\u7528\u4e00\u4e2a\u7b80\u5355\u7684write()\u51fd\u6570\uff0c\u9996\u5148\u7531VFS\u8f6c\u6362\u4e3asys_write()\uff0c\u518d\u6839\u636efd\u6587\u4ef6\u63cf\u8ff0\u7b26\u6240\u5728\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u627e\u5230\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u6700\u540e\u518d\u6267\u884c\u8be5\u64cd\u4f5c\u3002

\u901a\u8fc7\u8fd9\u5f20\u56fe\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cVFS\u652f\u6301\u591a\u79cd\u4e0d\u540c\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u6309\u7167\u5b58\u50a8\u4f4d\u7f6e\u7684\u4e0d\u540c\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7cfb\u7edf\u53ef\u4ee5\u5206\u4e3a\u4e09\u7c7b\uff1a

  • \u57fa\u4e8e\u78c1\u76d8\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u4e5f\u5c31\u662f\u628a\u6570\u636e\u76f4\u63a5\u5b58\u50a8\u5728\u672c\u5730\u78c1\u76d8\u4e2d\u3002\u5e38\u89c1\u7684\u7531Ext4\u3001XFS\u3001OverlayFS\u7b49\u3002

  • \u57fa\u4e8e\u5185\u5b58\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u4e0d\u9700\u8981\u78c1\u76d8\u5206\u914d\u5b58\u50a8\u7a7a\u95f4\uff0c\u4f46\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u3002/proc\u6587\u4ef6\u7cfb\u7edf\u5c31\u5c5e\u4e8e\u8fd9\u4e00\u7c7b\u3002

  • \u57fa\u4e8e\u7f51\u7edc\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u7528\u6765\u8bbf\u95ee\u5176\u4ed6\u8ba1\u7b97\u673a\u6570\u636e\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u6bd4\u5982NFS\u3002

\u4e0d\u7ba1\u662f\u54ea\u7c7b\u6587\u4ef6\u7cfb\u7edf\uff0c\u90fd\u9700\u8981\u6302\u8f7d\u5230VFS\u76ee\u5f55\u6811\u4e2d\u7684\u67d0\u4e2a\u5b50\u76ee\u5f55\uff0c\u624d\u80fd\u88ab\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u8bbf\u95ee\u5230\u3002\u62ff\u57fa\u4e8e\u78c1\u76d8\u7684\u6587\u4ef6\u7cfb\u7edf\u4e3a\u4f8b\uff0c\u5728\u5b89\u88c5\u7cfb\u7edf\u65f6\uff0c\u5c31\u9700\u8981\u6307\u5b9a\u6302\u8f7d\u70b9\u2014\u2014\u6839\u76ee\u5f55\uff08/\uff09\uff0c\u5728\u6839\u76ee\u5f55\u4e0b\u518d\u628a\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u6302\u8f7d\u8fdb\u6765\u3002

Linux\u7cfb\u7edf\u603b\u5171\u91c7\u7528\u4e86\u56db\u79cd\u6838\u5fc3\u6982\u5ff5\u6765\u62bd\u8c61\u6587\u4ef6\u7cfb\u7edf\uff1a\u6587\u4ef6\u3001\u76ee\u5f55\u9879\u3001\u7d22\u5f15\u70b9\u548c\u6302\u8f7d\u70b9\u3002\u8fd9\u4e9b\u6982\u5ff5\u5171\u540c\u6784\u5efa\u4e86\u4e00\u4e2a\u5206\u5c42\u7684\u6570\u636e\u5b58\u50a8\u7ed3\u6784\uff0c\u7528\u4e8e\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u53ca\u76f8\u5173\u7684\u63a7\u5236\u4fe1\u606f\u3002Linux\u6587\u4ef6\u7cfb\u7edf\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u7edf\u4e00\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u6240\u6709\u7684\u6587\u4ef6\u88ab\u6302\u8f7d\u5230\u4e00\u4e2a\u5168\u5c40\u7684\u5c42\u6b21\u7ed3\u6784\u4e2d\uff0c\u5f62\u6210\u4e00\u4e2a\u7c7b\u4f3c\u6839\u6587\u4ef6\u7cfb\u7edf\u7684\u7ed3\u6784\u3002\u8fd9\u4e0eWindows\u7cfb\u7edf\u5c06\u6587\u4ef6\u7cfb\u7edf\u5212\u5206\u4e0d\u540c\u7684\u5206\u533a\u4e0d\u540c\u3002

Linux\u7cfb\u7edf\u533a\u5206\u6587\u4ef6\u672c\u8eab\u548c\u6587\u4ef6\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u8bbf\u95ee\u63a7\u5236\u6743\u9650\u3001\u5927\u5c0f\u3001\u6240\u6709\u8005\u3001\u521b\u5efa\u65f6\u95f4\u7b49\u3002\u8fd9\u4e9b\u76f8\u5173\u4fe1\u606f\uff0c\u6709\u65f6\u88ab\u79f0\u4f5c\u6587\u4ef6\u7684\u5143\u6570\u636e\uff0c\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u2014\u2014\u7d22\u5f15\u8282\u70b9\uff08inode\uff09\u3002

"},{"location":"linux/kernel/vfs/#vfs","title":"VFS\u5bf9\u8c61","text":"

VFS\u4f7f\u7528\u4e00\u7ec4\u6570\u636e\u7ed3\u6784\u6765\u4ee3\u8868\u901a\u7528\u6587\u4ef6\u5bf9\u8c61\uff0c\u5b83\u4eec\u5206\u522b\u662f\uff1a

  • \u8d85\u7ea7\u5757\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u5177\u4f53\u7684\u5df2\u5b89\u88c5\u7684\u6587\u4ef6\u7cfb\u7edf\u3002
  • \u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u5177\u4f53\u6587\u4ef6\u3002
  • \u76ee\u5f55\u9879\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u76ee\u5f55\u9879\uff0c\u662f\u4e00\u4e2a\u8def\u5f84\u7684\u7ec4\u6210\u90e8\u5206\u3002
  • \u6587\u4ef6\u5bf9\u8c61\uff0c\u4ee3\u8868\u7531\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u3002

\u6bcf\u4e2a\u901a\u7528\u5bf9\u8c61\u90fd\u5305\u542b\u4e00\u4e2a\u64cd\u4f5c\u5bf9\u8c61\uff0c\u63cf\u8ff0\u4e86\u5185\u6838\u5bf9\u901a\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u2014\u2014super_operations\u3001inode_operations\u3001dentry_operations\u3001file_operations\u3002

"},{"location":"linux/kernel/vfs/#_2","title":"\u8d85\u7ea7\u5757\u5bf9\u8c61","text":"

\u8d85\u7ea7\u5757\u5bf9\u8c61\u662f\u7528\u6765\u63cf\u8ff0\u4e00\u4e2a\u5df2\u5b89\u88c5\u7684\u6587\u4ef6\u7cfb\u7edf\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5305\u542b\u4e86\u6587\u4ef6\u7cfb\u7edf\u7684\u5173\u952e\u5143\u6570\u636e\uff0c\u8fd9\u4e9b\u5143\u6570\u636e\u7528\u4e8e\u5185\u6838\u7ba1\u7406\u548c\u7ef4\u62a4\u6587\u4ef6\u7cfb\u7edf\u3002\u8d85\u7ea7\u5757\u5bf9\u8c61\u662f\u6587\u4ef6\u7cfb\u7edf\u5728\u5185\u6838\u4e2d\u8868\u793a\u81ea\u5df1\u7684\u65b9\u5f0f\uff0c\u5b83\u63d0\u4f9b\u4e86\u6587\u4ef6\u7cfb\u7edf\u7684\u603b\u4f53\u89c6\u56fe\u3002\u8d85\u7ea7\u5757\u5bf9\u8c61\u7531super_block\u7ed3\u6784\u4f53\u8868\u793a\uff0c\u901a\u8fc7alloc_super()\u51fd\u6570\u521b\u5efa\u5e76\u521d\u59cb\u5316\u3002\u5728\u6587\u4ef6\u7cfb\u7edf\u5b89\u88c5\u65f6\uff0c\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u4ee5\u4fbf\u4ece\u78c1\u76d8\u4e2d\u8bfb\u53d6\u6587\u4ef6\u7cfb\u7edf\u8d85\u7ea7\u5757\u3002\u8d85\u7ea7\u5757\u64cd\u4f5c\u7531super_operations\u7ed3\u6784\u4f53\u8868\u793a\uff0c\u8be5\u7ed3\u6784\u4f53\u4e2d\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u4e2a\u6307\u5411\u8d85\u7ea7\u5757\u64cd\u4f5c\u51fd\u6570\u7684\u6307\u9488\u3002

"},{"location":"linux/kernel/vfs/#_3","title":"\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61","text":"

\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u4ee3\u8868\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u903b\u8f91\u8868\u793a\uff0c\u5305\u542b\u4e86\u5173\u4e8e\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5143\u6570\u636e\uff0c\u8fd9\u4e9b\u5143\u6570\u636e\u63cf\u8ff0\u4e86\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5c5e\u6027\uff0c\u5982\u6743\u9650\u3001\u6240\u6709\u8005\u3001\u6587\u4ef6\u5927\u5c0f\u3001\u6700\u540e\u8bbf\u95ee\u548c\u4fee\u6539\u65f6\u95f4\u7b49\u3002\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u7531struct inode\u7ed3\u6784\u4f53\u6765\u8868\u793a\u3002\u6bcf\u4e2a\u6587\u4ef6\u7cfb\u7edf\u90fd\u6709\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u8868\uff0c\u8be5\u8868\u5305\u542b\u4e86\u6240\u6709\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u5f53\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u901a\u8fc7\u6807\u51c6\u7684\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\uff08\u5982 open()\u3001read()\u3001write()\u7b49\uff09\u4e0e\u6587\u4ef6\u7cfb\u7edf\u4ea4\u4e92\u65f6\uff0c\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u5728\u4e0e\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u8fdb\u884c\u4ea4\u4e92\u3002

\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u4ee3\u8868\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6\uff0c\u5b83\u53ef\u4ee5\u662f\u8bbe\u5907\u6216\u7ba1\u9053\u8fd9\u6837\u7684\u7279\u6b8a\u6587\u4ef6\u3002\u5728struct inode\u7ed3\u6784\u4f53\u4e2d\u6709\u4e00\u4e9b\u548c\u7279\u6b8a\u6587\u4ef6\u76f8\u5173\u7684\u9879\uff0c\u6bd4\u5982i_pipe\u6307\u5411\u6709\u540d\u7ba1\u9053\uff0ci_bdev\u6307\u5411\u5757\u8bbe\u5907\uff0ci_cdev\u6307\u5411\u5b57\u7b26\u8bbe\u5907\u3002\u8fd9\u4e09\u4e2a\u6307\u9488\u88ab\u653e\u5728\u4e00\u4e2a\u5171\u7528\u4f53\u4e2d\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u6bcf\u6b21\u53ea\u80fd\u8868\u793a\u5176\u4e2d\u4e00\u79cd\u3002

\u4e0e\u8d85\u7ea7\u5757\u7c7b\u4f3c\uff0c\u7d22\u5f15\u8282\u70b9\u64cd\u4f5c\u7531inode_operations\u7ed3\u6784\u4f53\u8868\u793a\u3002

"},{"location":"linux/kernel/vfs/#_4","title":"\u76ee\u5f55\u9879\u5bf9\u8c61","text":"

\u5728VFS\u4e2d\uff0c\u76ee\u5f55\u88ab\u5f53\u4f5c\u6587\u4ef6\u6765\u5904\u7406\uff0c\u8fd9\u5141\u8bb8\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5bf9\u6587\u4ef6\u548c\u76ee\u5f55\u8fdb\u884c\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5728\u8def\u5f84/bin/vi\u4e2d\uff0cbin\u548cvi\u90fd\u88ab\u89c6\u4e3a\u6587\u4ef6\u3002\u8fd9\u91cc\u7684bin\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u76ee\u5f55\u6587\u4ef6\uff0c\u5b83\u5305\u542b\u4e86\u6307\u5411\u7cfb\u7edf\u4e2d\u53ef\u6267\u884c\u6587\u4ef6\u7684\u94fe\u63a5\uff0c\u800cvi\u662f\u4e00\u4e2a\u666e\u901a\u6587\u4ef6\uff0c\u5305\u542b\u4e86\u6587\u672c\u7f16\u8f91\u5668\u7684\u6e90\u4ee3\u7801\u3002

\u5728VFS\u4e2d\uff0c\u8def\u5f84\u4e2d\u7684\u6bcf\u4e2a\u7ec4\u6210\u90e8\u5206\uff08\u65e0\u8bba\u662f\u666e\u901a\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\uff09\u90fd\u662f\u7531\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u8868\u793a\u7684\u3002

\u4e3a\u4e86\u7b80\u5316\u8def\u5f84\u540d\u67e5\u627e\u7b49\u76ee\u5f55\u76f8\u5173\u7684\u64cd\u4f5c\uff0cVFS\u5f15\u5165\u4e86\u76ee\u5f55\u9879\u7684\u6982\u5ff5\u3002\u76ee\u5f55\u9879\u5bf9\u8c61\u662f\u8def\u5f84\u4e2d\u7684\u4e00\u4e2a\u7279\u5b9a\u90e8\u5206\uff0c\u5b83\u5305\u542b\u4e86\u5bf9\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u7684\u5f15\u7528\u4ee5\u53ca\u6587\u4ef6\u540d\u548c\u5176\u4ed6\u76f8\u5173\u4fe1\u606f\u3002\u8fd9\u6837\uff0cVFS\u5728\u8fdb\u884c\u8def\u5f84\u540d\u67e5\u627e\u65f6\uff0c\u5c31\u4e0d\u5fc5\u5bf9\u6574\u4e2a\u8def\u5f84\u8fdb\u884c\u9010\u5b57\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u800c\u662f\u53ef\u4ee5\u57fa\u4e8e\u76ee\u5f55\u9879\u5bf9\u8c61\u5feb\u901f\u5b9a\u4f4d\u5230\u76ee\u6807\u6587\u4ef6\u6216\u76ee\u5f55\u3002

\u76ee\u5f55\u9879\u5bf9\u8c61\u8fd8\u5305\u62ec\u4e86\u6302\u8f7d\u70b9\uff0c\u8fd9\u662f\u6587\u4ef6\u7cfb\u7edf\u6302\u8f7d\u65f6\u7684\u6839\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5728\u8def\u5f84/mnt/cdrom/foo \u4e2d\uff0c/\u3001mnt\u3001cdrom \u548cfoo\u90fd\u662f\u76ee\u5f55\u9879\u5bf9\u8c61\u3002VFS\u5728\u6267\u884c\u76ee\u5f55\u64cd\u4f5c\u65f6\u4f1a\u73b0\u573a\u521b\u5efa\u76ee\u5f55\u9879\u5bf9\u8c61\u3002

\u76ee\u5f55\u9879\u5bf9\u8c61\u7531struct dentry\u7ed3\u6784\u4f53\u8868\u793a\u3002

\u7531\u4e8e\u6587\u4ef6\u7684\u8bbf\u95ee\u5448\u73b0\u7a7a\u95f4\u548c\u65f6\u95f4\u7684\u5c40\u90e8\u6027\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u8bbf\u95ee\u6587\u4ef6\u7684\u6027\u80fd\uff0cVFS\u5bf9\u4e8e\u76ee\u5f55\u9879\u548c\u7d22\u5f15\u8282\u70b9\u8fdb\u884c\u4e86\u7f13\u5b58\uff0c\u4ee5\u51cf\u5c11\u5bf9\u4e8e\u78c1\u76d8\u7684\u8bbf\u95ee\u64cd\u4f5c\u3002\u76ee\u5f55\u9879\u7f13\u5b58\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a

  1. \u7f13\u5b58\u6570\u636e\uff1a\u5f53\u5185\u6838\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u68c0\u67e5\u76ee\u5f55\u9879\u7f13\u5b58\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u8be5\u76ee\u5f55\u9879\u7684\u6570\u636e\u3002\u5982\u679c\u6570\u636e\u5728\u7f13\u5b58\u4e2d\uff0c\u5185\u6838\u5c06\u76f4\u63a5\u4f7f\u7528\u7f13\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4ece\u78c1\u76d8\u8bfb\u53d6\u3002

  2. \u7f13\u5b58\u66f4\u65b0\uff1a\u5f53\u5185\u6838\u4fee\u6539\u4e86\u76ee\u5f55\u9879\u7684\u6570\u636e\uff08\u4f8b\u5982\uff0c\u521b\u5efa\u3001\u5220\u9664\u6216\u91cd\u547d\u540d\u6587\u4ef6\u6216\u76ee\u5f55\uff09\u65f6\uff0c\u5b83\u4f1a\u5c06\u4fee\u6539\u540e\u7684\u6570\u636e\u5199\u5165\u76ee\u5f55\u9879\u7f13\u5b58\u3002\u8fd9\u6837\uff0c\u4e0b\u4e00\u6b21\u8bbf\u95ee\u76f8\u540c\u7684\u6570\u636e\u65f6\uff0c\u5185\u6838\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u800c\u4e0d\u662f\u518d\u6b21\u8bbf\u95ee\u78c1\u76d8\u3002

  3. \u7f13\u5b58\u6e05\u7406\uff1a\u4e3a\u4e86\u4fdd\u6301\u7f13\u5b58\u7684\u6570\u636e\u662f\u6700\u65b0\u7684\uff0c\u5185\u6838\u4f1a\u5b9a\u671f\u6216\u6839\u636e\u9700\u8981\u6e05\u7406\u7f13\u5b58\uff0c\u79fb\u9664\u4e0d\u518d\u5f15\u7528\u7684\u76ee\u5f55\u9879\u6570\u636e\u3002

  4. \u7f13\u5b58\u4e00\u81f4\u6027\uff1a\u5728\u591a\u6838\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u4fdd\u6301\u7f13\u5b58\u7684\u4e00\u81f4\u6027\uff0c\u5185\u6838\u4f1a\u4f7f\u7528\u5404\u79cd\u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae\uff08\u5982MESI\u534f\u8bae\uff09\u6765\u540c\u6b65\u7f13\u5b58\u6570\u636e\u3002

struct dentry_operations\u7ed3\u6784\u4f53\u6307\u660e\u4e86VFS\u64cd\u4f5c\u76ee\u5f55\u9879\u7684\u6240\u6709\u65b9\u6cd5\u3002

"},{"location":"linux/kernel/vfs/#_5","title":"\u6587\u4ef6\u5bf9\u8c61","text":"

\u6587\u4ef6\u5bf9\u8c61\u662f\u8fdb\u7a0b\u5df2\u6253\u5f00\u7684\u6587\u4ef6\u5728\u5185\u5b58\u4e2d\u7684\u8868\u793a\uff0c\u8be5\u5bf9\u8c61\u7531open()\u521b\u5efa\uff0cclose()\u64a4\u9500\u3002\u4e0e\u6587\u4ef6\u6709\u5173\u7684\u8c03\u7528\u5b9e\u9645\u4e0a\u90fd\u662f\u6587\u4ef6\u64cd\u4f5c\u8868struct files_operations\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u6307\u9488\uff0c\u8fd9\u91cc\u5217\u51fa\u6765\u4f5c\u4e3a\u53c2\u8003\uff1a

struct file_operations {\n    struct module *owner;\n    loff_t (*llseek) (struct file *, loff_t, int);\n    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);\n    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);\n    ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);\n    ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);\n    int (*iopoll)(struct kiocb *kiocb, bool spin);\n    int (*iterate) (struct file *, struct dir_context *);\n    int (*iterate_shared) (struct file *, struct dir_context *);\n    __poll_t (*poll) (struct file *, struct poll_table_struct *);\n    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);\n    long (*compat_ioctl) (struct file *, unsigned int, unsigned long);\n    int (*mmap) (struct file *, struct vm_area_struct *);\n    unsigned long mmap_supported_flags;\n    int (*open) (struct inode *, struct file *);\n    int (*flush) (struct file *, fl_owner_t id);\n    int (*release) (struct inode *, struct file *);\n    int (*fsync) (struct file *, loff_t, loff_t, int datasync);\n    int (*fasync) (int, struct file *, int);\n    int (*lock) (struct file *, int, struct file_lock *);\n    ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);\n    unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);\n    int (*check_flags)(int);\n    int (*flock) (struct file *, int, struct file_lock *);\n    ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);\n    ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);\n    int (*setlease)(struct file *, long, struct file_lock **, void **);\n    long (*fallocate)(struct file *file, int mode, loff_t offset,\n              loff_t len);\n    void (*show_fdinfo)(struct seq_file *m, struct file *f);\n#ifndef CONFIG_MMU\n    unsigned (*mmap_capabilities)(struct file *);\n#endif\n    ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,\n            loff_t, size_t, unsigned int);\n    loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,\n                   struct file *file_out, loff_t pos_out,\n                   loff_t len, unsigned int remap_flags);\n    int (*fadvise)(struct file *, loff_t, loff_t, int);\n};\n

\u603b\u7684\u6765\u8bf4\uff0c\u5185\u6838\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u90fd\u5206\u914d\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u2014\u2014\u7d22\u5f15\u8282\u70b9\u548c\u76ee\u5f55\u9879\uff0c\u5206\u522b\u7528\u6765\u8bb0\u5f55\u6587\u4ef6\u7684\u5143\u6570\u636e\u548c\u76ee\u5f55\u7ed3\u6784\u3002\u7d22\u5f15\u8282\u70b9\u662f\u6587\u4ef6\u7684\u552f\u4e00\u6807\u5fd7\uff0c\u591a\u4e2a\u76ee\u5f55\u9879\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u3002\u6bd4\u5982\u901a\u8fc7\u786c\u94fe\u63a5\u4e3a\u6587\u4ef6\u521b\u5efa\u522b\u540d\uff0c\u867d\u7136\u76ee\u5f55\u4f4d\u7f6e\u4e0d\u540c\uff0c\u4f46\u662f\u94fe\u63a5\u7684\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u56e0\u6b64\u7d22\u5f15\u8282\u70b9\u4e00\u81f4\u3002

"},{"location":"linux/kernel/vfs/#_6","title":"\u8fdb\u7a0b\u4e2d\u7684\u6587\u4ef6\u6570\u636e\u7ed3\u6784","text":"

\u5728\u8fdb\u7a0b\u7ba1\u7406\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u4e86\u5728\u8fdb\u7a0b\u63cf\u8ff0\u7b26tast_struct\u4e2d\u6709\u4e24\u4e2a\u4e0e\u6587\u4ef6\u7cfb\u7edf\u76f8\u5173\u7684\u6210\u5458\u53d8\u91cf\uff1a

struct fs_struct *fs;\nstruct files_struct *files;\n

fs_struct\u5305\u542b\u6587\u4ef6\u7cfb\u7edf\u548c\u8fdb\u7a0b\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/fs_struct.h>\u4e2d\uff1a

struct fs_struct {\n    int users;\n    int umask;\n    int in_exec;\n    struct path root, pwd;\n};\n

user\uff1a\u5f53\u524d\u6253\u5f00\u8be5\u6587\u4ef6\u7684\u7528\u6237\u6570\u91cf\u3002

umask\uff1a\u7528\u6237\u6a21\u5f0f\u63a9\u7801\uff0c\u51b3\u5b9a\u4e86\u7cfb\u7edf\u5728\u521b\u5efa\u6587\u4ef6\u548c\u76ee\u5f55\u65f6\u7684\u9ed8\u8ba4\u6743\u9650\u63a9\u7801\u3002

in_exec\uff1a\u8868\u793a\u6587\u4ef6\u7cfb\u7edf\u662f\u5426\u5904\u4e8e\u6267\u884c\u6a21\u5f0f\u3002

root\uff0cpwd\uff1a\u6839\u76ee\u5f55\u8def\u5f84\u548c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u3002

files_struct\u6807\u8bc6\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u4fe1\u606f\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/fdtable.h>\u4e2d\uff1a

struct files_struct {\n    atomic_t count;\n\n    struct fdtable __rcu *fdt;\n    struct fdtable fdtab;\n\n    unsigned int next_fd;\n    unsigned long close_on_exec_init[1];\n    unsigned long open_fds_init[1];\n    unsigned long full_fds_bits_init[1];\n    struct file __rcu * fd_array[NR_OPEN_DEFAULT];\n};\n

count\uff1a\u8ddf\u8e2a\u5f53\u524d\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6570\u91cf\u3002

fd\uff1a\u7f13\u5b58\u4e0b\u4e00\u4e2a\u53ef\u7528\u7684fd\u3002

fd_array\uff1a\u6307\u5411\u4e00\u4e2astruct file\u7ed3\u6784\u4f53\u7684\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u572864\u4f4d\u4f53\u7cfb\u4e2d\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u9ed8\u8ba4\u53ef\u4ee5\u6253\u5f0064\u4e2a\u6587\u4ef6\u5bf9\u8c61\u3002

\u8fd8\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7ed3\u6784\u4f53\u662fnamespace\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/mount.h>\u4e2d\u3002\u5b83\u53ef\u4ee5\u4f7f\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u5728\u7cfb\u7edf\u4e2d\u90fd\u770b\u5230\u552f\u4e00\u7684\u6587\u4ef6\u7cfb\u7edf\u5c42\u6b21\u7ed3\u6784\uff0c\u662fLinux\u4e09\u5927\u9694\u79bb\u6280\u672f\u4e4b\u4e00\u3002\u5177\u4f53\u7684\u7ec6\u8282\u5c31\u4e0d\u5728\u8fd9\u91cc\u63a2\u8ba8\u4e86\uff0c\u5728\u5bb9\u5668\u6280\u672f\u4e00\u7ae0\u6211\u4f1a\u518d\u505a\u8be6\u7ec6\u4ecb\u7ecd\u3002

\u4e0a\u8ff0\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u90fd\u662f\u901a\u8fc7\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u94fe\u8868\u8fde\u63a5\u8d77\u6765\u7684\u3002\u5bf9\u4e8e\u591a\u6570\u8fdb\u7a0b\uff0c\u5b83\u4eec\u7684\u63cf\u8ff0\u7b26\u90fd\u6307\u5411\u552f\u4e00\u7684files_struct\u548cfs_struct\u7ed3\u6784\u4f53\u3002\u4f46\u662f\u5bf9\u4e8e\u90a3\u4e9b\u5728\u8c03\u7528clone()\u51fd\u6570\u65f6\u6307\u5b9a\u4e86\u514b\u9686\u6807\u5fd7CLONE_FILES\u6216\u8005CLONE_FS\u7684\u8fdb\u7a0b\uff0c\u4f1a\u5171\u4eab\u8fd9\u4e24\u4e2a\u7ed3\u6784\u4f53\u3002

namespace\u7ed3\u6784\u4f53\u7684\u4f7f\u7528\u65b9\u6cd5\u6bd4\u8f83\u7279\u6b8a\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u8fdb\u7a0b\u90fd\u662f\u5171\u4eab\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u7684\uff0c\u53ea\u6709\u5f53clone()\u51fd\u6570\u65f6\u6307\u5b9a\u4e86CLONE_NEWS\u6807\u5fd7\uff0c\u624d\u4f1a\u7ed9\u8fdb\u7a0b\u4e00\u4e2a\u552f\u4e00\u7684\u547d\u540d\u7a7a\u95f4\u7ed3\u6784\u4f53\u7684\u62f7\u8d1d\u3002

"},{"location":"linux/kernel/vfs/#proc","title":"proc\u6587\u4ef6\u7cfb\u7edf","text":"

proc\u6587\u4ef6\u7cfb\u7edf\u662f\u7279\u6b8a\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u5b83\u4eec\u7684\u4fe1\u606f\u5e76\u4e0d\u4fdd\u5b58\u5728\u78c1\u76d8\u4e0a\u800c\u662f\u5728\u6536\u5230\u8bf7\u6c42\u540e\u7531\u5185\u6838\u52a8\u6001\u751f\u6210\u3002proc\u6587\u4ef6\u7cfb\u7edf\u63d0\u4f9b\u4e86\u5185\u6838\u5404\u4e2a\u5b50\u7cfb\u7edf\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u8fdb\u7a0b\u4fe1\u606f\u3001\u5185\u5b58\u4fe1\u606f\u3001\u7f51\u7edc\u4fe1\u606f\u7b49\u3002\u5b83\u4f7f\u5f97\u7528\u6237\u901a\u8fc7\u7b80\u5355\u5730\u4f7f\u7528cat\u547d\u4ee4\u5c31\u53ef\u4ee5\u67e5\u770b\u7cfb\u7edf\u4fe1\u606f\uff0c\u751a\u8fd8\u53ef\u4ee5\u5728\u7cfb\u7edf\u8fd0\u884c\u65f6\u52a8\u6001\u4fee\u6539\u5185\u6838\u53c2\u6570\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u5185\u6838\u7684\u4ee3\u7801\u3002

\u5728proc\u6587\u4ef6\u7cfb\u7edf\u5f15\u5165\u4e4b\u524d\uff0c\u8981\u60f3\u83b7\u53d6\u6216\u4fee\u6539\u5185\u6838\u7684\u53c2\u6570\u65f6\uff0c\u9700\u8981\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u4e13\u95e8\u7684\u7279\u6743\u7ea7\u7a0b\u5e8f\uff0c\u8fd9\u9700\u8981\u5f00\u53d1\u4eba\u5458\u5177\u6709\u975e\u5e38\u4e13\u4e1a\u7684\u5185\u6838\u77e5\u8bc6\uff0c\u8fd9\u79cd\u505a\u6cd5\u5bf9\u4e8e\u5185\u6838\u6765\u8bf4\u5f88\u4e0d\u5b89\u5168\u3002\u6709\u4e86proc\u6587\u4ef6\u7cfb\u7edf\u4e4b\u540e\uff0c\u7528\u6237\u901a\u8fc7\u7b80\u5355\u7684read/write\u547d\u4ee4\u5c31\u53ef\u4ee5\u8bfb\u53d6\u548c\u4fee\u6539\u5185\u6838\u53c2\u6570\uff0c\u4e0d\u4ec5\u589e\u52a0\u4e86\u5e94\u7528\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u4e5f\u964d\u4f4e\u4e86\u5185\u6838\u7a33\u5b9a\u6027\u88ab\u5e94\u7528\u7a0b\u5e8f\u7834\u574f\u7684\u98ce\u9669\u3002

\u4e0b\u9762\u5bf9proc\u76ee\u5f55\u4e0b\u51e0\u4e2a\u5e38\u7528\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u548c\u4f7f\u7528\u573a\u666f\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u8bf4\u660e\u3002

"},{"location":"linux/kernel/vfs/#proccpuinfo","title":"/proc/cpuinfo","text":"

\u8be5\u6587\u4ef6\u5305\u542b\u4e86\u5f53\u524d\u7cfb\u7edfCPU\u7684\u53c2\u6570\u4fe1\u606f\uff0c\u5305\u62ecCPU\u7684\u6838\u5fc3\u6570\u91cf\u3001\u6bcf\u4e2a\u6838\u5fc3\u7684\u5de5\u4f5c\u9891\u7387\u3001\u7f13\u5b58\u5927\u5c0f\u7b49\u3002

"},{"location":"linux/kernel/vfs/#procmeminfo","title":"/proc/meminfo","text":"

\u8be5\u6587\u4ef6\u663e\u793a\u7684\u662f\u7cfb\u7edf\u4e2d\u5f53\u524d\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u6bd4\u5982\u7269\u7406\u5185\u5b58\u5bb9\u91cf\u3001\u5df2\u4f7f\u7528\u5185\u5b58\u3001\u7a7a\u95f2\u5185\u5b58\u3001\u5171\u4eab\u5185\u5b58\u3001\u4ea4\u6362\u5185\u5b58\u5927\u5c0f\u7b49\u3002\u65e5\u5e38\u5f00\u53d1\u8fc7\u7a0b\u4e2dtop\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u65f6\uff0c\u5c31\u662f\u901a\u8fc7\u8bfb\u53d6\u8be5\u6587\u4ef6\u83b7\u53d6\u7684\u3002\u76f8\u6bd4\u4e8etop\u547d\u4ee4\uff0c\u8be5\u6587\u4ef6\u63d0\u4f9b\u4e86\u66f4\u4e3a\u8be6\u7ec6\u7684\u5185\u5b58\u72b6\u6001\u4fe1\u606f\uff0c\u6bd4\u5982slab\u5185\u5b58\u5360\u7528\u91cf\u3001\u53ef\u56de\u6536\u5185\u5b58\u91cf\u3001\u5185\u5b58\u6808\u7a7a\u95f4\u5927\u5c0f\u3001\u4f7f\u7528malloc()\u5206\u914d\u7684\u5806\u5185\u5b58\u91cf\u3001\u4f7f\u7528mmap()\u6620\u5c04\u7684\u5185\u5b58\u91cf\u7b49\u3002

"},{"location":"linux/kernel/vfs/#prockallsyms","title":"/proc/kallsyms","text":"

\u8be5\u6587\u4ef6\u662f\u4e00\u4e2a\u7b26\u53f7\u8868\uff0c\u5305\u542b\u4e86\u5185\u6838\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002

\u8fd9\u662f\u4e00\u4e2a\u5e2e\u52a9\u5185\u6838\u5f00\u53d1\u8005\u8c03\u8bd5\u5185\u6838\u800c\u52a0\u5165\u7684\u6587\u4ef6\uff0c\u5728Linux\u7cfb\u7edf\u5d29\u6e83\u4ea7\u751f\u7684\u4fe1\u606f\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u5806\u6808\u4e2d\u663e\u793a\u51fa\u6765\u7684\u51fd\u6570\u540d\uff0c\u5c31\u662f\u5728\u8fd9\u4e2a\u6587\u4ef6\u7684\u5e2e\u52a9\u4e0b\u4ea7\u751f\u7684\u3002

"},{"location":"linux/kernel/vfs/#procinterrupts","title":"/proc/interrupts","text":"

\u8be5\u6587\u4ef6\u8bb0\u5f55\u4e86\u7cfb\u7edf\u5728\u6bcf\u4e2aCPU\u4e0a\u5904\u7406\u7684\u5404\u7c7b\u4e2d\u65ad\u7684\u7edf\u8ba1\u4fe1\u606f\u3002

"},{"location":"linux/kernel/vfs/#procloadavg","title":"/proc/loadavg","text":"

\u8be5\u6587\u4ef6\u663e\u793a\u7cfb\u7edf\u5728\u8fc7\u53bb\u4e00\u6bb5\u65f6\u95f4\u7684\u5e73\u5747\u8d1f\u8f7d\uff1a

\u4ece\u5de6\u5230\u53f3\u5206\u522b\u662f\uff1a

  • \u8fc7\u53bb\u4e00\u5206\u949f\u7684\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d
  • \u8fc7\u53bb\u4e94\u5206\u949f\u7684\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d
  • \u8fc7\u53bb\u5341\u4e94\u5206\u949f\u7684\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d
  • \u91c7\u6837\u65f6\u523b\u8fd0\u884c\u961f\u5217\u7684\u4efb\u52a1\u6570/\u7cfb\u7edf\u4e2d\u6d3b\u8dc3\u7684\u603b\u4efb\u52a1\u6570
  • \u91c7\u6837\u65f6\u523b\u5360\u7528\u6700\u5927\u7684\u7ebf\u7a0bID

\u8f93\u51fa\u7684\u524d\u4e09\u4e2a\u503c\u7ecf\u5e38\u88ab\u7528\u6765\u89c2\u5bdf\u7cfb\u7edf\u8d1f\u8f7d\u7684\u53d1\u5c55\u8d8b\u52bf\uff0c\u5982\u679c\u524d\u9762\u7684\u503c\u6bd4\u540e\u9762\u7684\u503c\u5927\uff0c\u8bf4\u660e\u7cfb\u7edf\u7684\u8d1f\u8f7d\u5448\u73b0\u4e0a\u5347\u8d8b\u52bf\u3002

"},{"location":"linux/kernel/vfs/#procpid","title":"/proc/PID/","text":"

\u8fd9\u662f\u5305\u542b\u8fdb\u7a0b\u8fd0\u884c\u4fe1\u606f\u7684\u76ee\u5f55\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u7ef4\u62a4\u4e00\u4e2a\u4ee5PID\u547d\u540d\u7684\u76ee\u5f55\uff0c\u5728\u8be5\u76ee\u5f55\u4e0b\u4fdd\u5b58\u8be5\u8fdb\u7a0b\u8fd0\u884c\u65f6\u7684\u5404\u79cd\u72b6\u6001\u6570\u636e\uff0c\u6bd4\u5982\u8fd0\u884c\u72b6\u6001\uff08status\uff09\u3001\u865a\u62df\u5185\u5b58\u6620\u5c04\u4fe1\u606f\uff08maps\uff09\u3001\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08fd\uff09\u3001\u521b\u5efa\u7684\u7f51\u7edc\u5957\u63a5\u5b57\u7b49\u3002

\u5176\u4e2d\u7528\u7684\u6bd4\u8f83\u591a\u7684\uff0c\u5e94\u8be5\u662f\u865a\u62df\u5185\u5b58\u6620\u5c04\u4fe1\u606f\uff08maps\uff09\uff1a

"},{"location":"linux/kernel/vfs/#procnet","title":"/proc/net/","text":"

\u8be5\u76ee\u5f55\u4e0b\u4fdd\u5b58\u4e86\u7cfb\u7edf\u7f51\u7edc\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u7f51\u7edc\u63a5\u53e3\u3001\u7f51\u7edc\u534f\u8bae\u6808\u3001\u7f51\u7edc\u8fde\u63a5\u3001\u7f51\u7edc\u8def\u7531\u8868\u7b49\u3002\u6700\u5e38\u7528\u7684/proc/net/dev\u4fdd\u5b58\u4e86\u901a\u8fc7\u5404\u7f51\u7edc\u63a5\u53e3\u4f20\u8f93\u7684\u6570\u636e\u7684\u7edf\u8ba1\u91cf\uff0c\u7ecf\u5e38\u7528\u5b83\u6765\u67e5\u770b\u7f51\u7edc\u6d41\u91cf\u548c\u68c0\u67e5\u7f51\u7edc\u4f20\u8f93\u8d28\u91cf\u3002

"},{"location":"linux/kernel/vma/","title":"\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4","text":"

\u5185\u6838\u9664\u4e86\u7ba1\u7406\u672c\u8eab\u7684\u5185\u5b58\u5916\uff0c\u8fd8\u5fc5\u987b\u7ba1\u7406\u7528\u6237\u7a7a\u95f4\u4e2d\u8fdb\u7a0b\u7684\u5185\u5b58\u3002\u6211\u4eec\u79f0\u8fd9\u4e2a\u5185\u5b58\u4e3a\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u3002Linux\u64cd\u4f5c\u7cfb\u7edf\u91c7\u7528\u865a\u62df\u5185\u5b58\u6280\u672f\uff0c\u5bf9\u4e00\u4e2a\u8fdb\u7a0b\u800c\u8a00\uff0c\u5b83\u4ee5\u865a\u62df\u7684\u65b9\u5f0f\u8bbf\u95ee\u6574\u4e2a\u7cfb\u7edf\u7684\u6240\u6709\u7269\u7406\u5185\u5b58\uff0c\u751a\u81f3\u5176\u865a\u62df\u7a7a\u95f4\u53ef\u4ee5\u8fdc\u8fdc\u5927\u4e8e\u7269\u7406\u5185\u5b58\u3002

\u4e3a\u4ec0\u4e48\u8981\u6709\u865a\u62df\u5185\u5b58\uff1f

\u5728\u8003\u8651\u4e24\u4e2a\u7a0b\u5e8f\u540c\u65f6\u64cd\u4f5c\u7269\u7406\u5185\u5b58\u7684\u573a\u666f\u65f6\uff0c\u6211\u4eec\u5fc5\u987b\u8c28\u614e\u5730\u4e3a\u5b83\u4eec\u5206\u914d\u5185\u5b58\u5730\u5740\uff0c\u5e76\u786e\u4fdd\u6bcf\u4e2a\u7a0b\u5e8f\u4ec5\u8bbf\u95ee\u5176\u88ab\u5206\u914d\u7684\u5185\u5b58\u533a\u57df\uff0c\u9632\u6b62\u5730\u5740\u51b2\u7a81\u3002\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u5730\u5740\u4f1a\u6781\u5927\u5730\u589e\u52a0\u7f16\u7a0b\u7684\u590d\u6742\u6027\u3002\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u5b9e\u73b0\u4e00\u79cd\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u63d0\u4f9b\u4e00\u5957\u9694\u79bb\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c06\u4e3a\u8fd9\u4e9b\u865a\u62df\u5730\u5740\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\uff0c\u4e14\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u72ec\u7acb\u4e8e\u5176\u4ed6\u8fdb\u7a0b\u64cd\u4f5c\uff0c\u90a3\u4e48\u8fd9\u5c06\u6781\u5927\u5730\u7b80\u5316\u7f16\u7a0b\u5de5\u4f5c\u3002\u8fd9\u79cd\u673a\u5236\u5c31\u662f\u865a\u62df\u5185\u5b58\u7ba1\u7406\uff0c\u800c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u5355\u5143\uff08MMU\uff09\u5219\u8d1f\u8d23\u5c06\u865a\u62df\u5730\u5740\u8f6c\u6362\u4e3a\u7269\u7406\u5730\u5740\u3002

Linux\u91c7\u7528\u4e86\u57fa\u4e8e\u5206\u9875\u7684\u865a\u62df\u5185\u5b58\u7ba1\u7406\u673a\u5236\uff0c\u5176\u4e2d\u5185\u5b58\u88ab\u5212\u5206\u4e3a\u5927\u5c0f\u56fa\u5b9a\u7684\u9875\uff0c\u6bcf\u4e00\u9875\u901a\u5e38\u662f4KB\u3002\u8fd9\u79cd\u5206\u9875\u673a\u5236\u4f7f\u5f97\u5185\u5b58\u7ba1\u7406\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002\u5728Linux\u4e2d\uff0c\u865a\u62df\u5730\u5740\u5230\u7269\u7406\u5730\u5740\u7684\u8f6c\u6362\u662f\u901a\u8fc7\u591a\u7ea7\u9875\u8868\u6765\u5b9e\u73b0\u7684\uff0c\u8fd9\u79cd\u5206\u7ea7\u7ed3\u6784\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u5927\u578b\u5730\u5740\u7a7a\u95f4\uff0c\u540c\u65f6\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u4e3a\u4e86\u63d0\u9ad8\u5730\u5740\u8f6c\u6362\u7684\u901f\u5ea6\uff0c\u73b0\u4ee3CPU\u8bbe\u8ba1\u901a\u5e38\u5305\u62ec\u4e00\u4e2a\u79f0\u4e3a\u8f6c\u6362\u540e\u63f4\u7f13\u51b2\u5668\uff08TLB\uff09\u7684\u786c\u4ef6\u7f13\u5b58\u3002TLB\u7f13\u5b58\u4e86\u6700\u8fd1\u4f7f\u7528\u8fc7\u7684\u9875\u8868\u9879\uff0c\u8fd9\u6837\u5f53CPU\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u65f6\uff0c\u5b83\u9996\u5148\u68c0\u67e5TLB\uff0c\u5982\u679c\u627e\u5230\u4e86\u5339\u914d\u7684\u6761\u76ee\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u88ab\u79f0\u4e3aTLB\u547d\u4e2d\uff0c\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u5730\u5740\u8f6c\u6362\uff0c\u907f\u514d\u8bbf\u95ee\u4e3b\u5185\u5b58\u4e2d\u7684\u9875\u8868\uff0c\u4ece\u800c\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002\u5f53\u53d1\u751fTLB\u672a\u547d\u4e2d\u7684\u60c5\u51b5\u65f6\uff0cCPU\u5fc5\u987b\u8bbf\u95ee\u4e3b\u5185\u5b58\u4e2d\u7684\u9875\u8868\u6765\u83b7\u53d6\u6b63\u786e\u7684\u7269\u7406\u5730\u5740\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8f83\u5927\u7684\u6027\u80fd\u5f00\u9500\u3002\u56e0\u6b64\uff0c\u64cd\u4f5c\u7cfb\u7edf\u548c\u786c\u4ef6\u8bbe\u8ba1\u8005\u90fd\u4f1a\u5c3d\u91cf\u4f18\u5316TLB\u7684\u547d\u4e2d\u7387\uff0c\u4ee5\u51cf\u5c11\u8fd9\u79cd\u5f00\u9500\u3002

"},{"location":"linux/kernel/vma/#_2","title":"\u5730\u5740\u7a7a\u95f4\u7684\u6982\u5ff5","text":"

\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u7531\u53ef\u5bfb\u5740\u7684\u865a\u62df\u5185\u5b58\u7ec4\u6210\uff0c\u572832\u4f4d\u4f53\u7cfb\u4e2d\uff08\u53ea\u8ba8\u8bba32\u4f4d\u67b6\u6784\uff0c64\u4f4d\u7c7b\u4f3c\uff09\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u53ef\u5bfb\u57404GB\u7684\u865a\u62df\u5185\u5b58\uff0c\u5176\u4e2d1GB\u5206\u914d\u7ed9\u5185\u6838\u7a7a\u95f4\uff0c\u5269\u4e0b3GB\u5206\u914d\u7ed9\u7528\u6237\u7a7a\u95f4\u3002\u4e00\u4e2a\u8fdb\u7a0b\u53ea\u80fd\u8bbf\u95ee\u6709\u6548\u5185\u5b58\u533a\u57df\u5185\u7684\u5185\u5b58\u5730\u5740\uff0c\u90e8\u5206\u5730\u5740\u662f\u975e\u6cd5\u7684\uff0c\u4e00\u65e6\u8bbf\u95ee\uff0c\u5c31\u4f1a\u7acb\u523b\u89e6\u53d1\u6bb5\u9519\u8bef\uff0c\u7531\u5185\u6838\u7ec8\u6b62\u8be5\u8fdb\u7a0b\u3002\u6bcf\u4e2a\u5185\u5b58\u533a\u57df\u4e5f\u6709\u76f8\u5173\u7684\u6743\u9650\uff0c\u6bd4\u5982\u53ef\u8bfb\u53ef\u5199\u53ef\u6267\u884c\u7b49\u3002

\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u5206\u5e03\u5982\u4e0b\u56fe\u6240\u793a\uff1a

\u4ece\u4e0a\u56fe\u4e2d\u6211\u4eec\u770b\u5230\uff0c0x0000 0000\u52300x0804 8000\u8fd9\u6bb5\u7a7a\u95f4\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684\u4fdd\u7559\u533a\uff0c\u5728\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u6570\u503c\u6bd4\u8f83\u5c0f\u7684\u5730\u5740\u662f\u975e\u6cd5\u5730\u5740\uff0c\u4e0d\u5141\u8bb8\u8bbf\u95ee\u7684\u3002\u6bd4\u5982NULL\u6307\u9488\u5c31\u4f1a\u6307\u5411\u8fd9\u7247\u533a\u57df\u3002

0x0804 8000\u81f30xC000 0000\u662f\u7528\u6237\u6001\u7a7a\u95f4\u5730\u5740\uff0c\u518d\u7f51\u4e0a\u5c31\u662f\u6240\u8c13\u7684\u9ad8\u7aef\u5730\u5740\u2014\u2014\u4f9b\u5185\u6838\u4f7f\u7528\u3002\u5185\u6838\u6001\u4e0e\u7528\u6237\u6001\u7684\u5206\u754c\u7ebf\u7531\u6210\u5458\u53d8\u91cftask_size\u5b9a\u4e49\u3002

\u4fdd\u7559\u533a\u4e0a\u65b9\u662f\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\uff0c\u5b83\u4eec\u662f\u4ece\u53ef\u6267\u884c\u6587\u4ef6\u76f4\u63a5\u52a0\u8f7d\u8fdb\u6765\u7684\u3002\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u653e\u5728\u4ee3\u7801\u6bb5\uff0c\u6570\u636e\u6bb5\u7528\u6765\u5b58\u653e\u5df2\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u3002

BSS\u6bb5\u7528\u6765\u5b58\u653e\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4ee50\u586b\u5145\u3002

BSS\u6bb5\u4e0a\u65b9\u662f\u5806\u7a7a\u95f4\uff0c\u5730\u5740\u7684\u589e\u957f\u65b9\u5411\u662f\u4ece\u4f4e\u5230\u9ad8\u3002\u5185\u6838\u4f7f\u7528start_brk\u6807\u8bc6\u5806\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0cbrk\u6807\u8bc6\u5806\u5f53\u524d\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002\u5f53\u5806\u7533\u8bf7\u4e00\u5757\u5f88\u5c0f\u7684\u5185\u5b58\uff08128K\u4ee5\u5185\uff09\uff0c\u53ea\u9700\u8981\u5c06brk\u6307\u9488\u589e\u52a0\u5bf9\u5e94\u7684\u5927\u5c0f\u5373\u53ef\u3002\u5806\u5185\u5b58\u7684\u7ba1\u7406\u6bd4\u8f83\u590d\u6742\uff0c\u5806\u5185\u5b58\u6700\u5927\u7684\u56f0\u96be\u5c31\u662f\u9891\u7e41\u5730\u5206\u914d\u548c\u91ca\u653e\u4f1a\u9020\u6210\u5185\u5b58\u788e\u7247\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u5185\u6838\u91c7\u7528\u4e86\u57fa\u4e8e\u4f19\u4f34\u7cfb\u7edf\u7684\u5185\u5b58\u5206\u914d\u7b97\u6cd5\u3002

\u5806\u7a7a\u95f4\u4e0a\u65b9\u662f\u5f85\u5206\u914d\u533a\u57df\uff0c\u7528\u6765\u6269\u5c55\u5806\u7a7a\u95f4\u7684\u4f7f\u7528\u3002\u63a5\u4e0b\u6765\u662f\u5185\u5b58\u6620\u5c04\u533a\u57df\u3002\u4efb\u4f55\u5e94\u7528\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u901a\u8fc7mmap()\u7cfb\u7edf\u8c03\u7528\u6620\u5c04\u81f3\u6b64\u533a\u57df\u3002\u5185\u5b58\u6620\u5c04\u53ef\u4ee5\u7528\u6765\u52a0\u8f7d\u52a8\u6001\u5e93\uff0c\u6bd4\u5982 ld-linux.so \u5c31\u88ab\u52a0\u8f7d\u4e8e\u6b64\uff0c\u53e6\u5916\uff0c\u5982\u679c\u4f60\u901a\u8fc7malloc()\u7533\u8bf7\u4e86\u8d85\u8fc7128K\u5185\u5b58\uff0c\u5185\u6838\u5c06\u76f4\u63a5\u4e3a\u4f60\u5206\u914d\u4e00\u5757\u6620\u5c04\u533a\u57df\u4f5c\u4e3a\u5185\u5b58\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u5806\u5185\u5b58\u3002

\u6700\u540e\u4e00\u5757\u533a\u57df\u662f\u6808\u7a7a\u95f4\uff0c\u5728\u8fd9\u91cc\u4fdd\u5b58\u51fd\u6570\u8fd0\u884c\u8fc7\u7a0b\u9700\u8981\u7684\u5c40\u90e8\u53d8\u91cf\u4ee5\u53ca\u51fd\u6570\u53c2\u6570\u7b49\u4fe1\u606f\u3002\u6808\u7a7a\u95f4\u7684\u5730\u5740\u662f\u4ece\u9ad8\u5230\u4f4e\u589e\u957f\u7684\u3002\u5185\u6838\u4f7f\u7528start_stack\u6807\u8bc6\u6808\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002SP\u5bc4\u5b58\u5668\u4fdd\u5b58\u6808\u9876\u6307\u9488\uff0cBP\u5bc4\u5b58\u5668\u4fdd\u5b58\u6808\u57fa\u5730\u5740\u3002

\u5728\u547d\u4ee4\u884c\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528cat /proc/[pid]/maps\u67e5\u770b\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5185\u5b58\u5e03\u5c40\u3002

"},{"location":"linux/kernel/vma/#mm_struct","title":"\u5185\u5b58\u63cf\u8ff0\u7b26mm_struct","text":"

mm_struct\u7528\u6765\u8868\u793a\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u7684\u4fe1\u606f\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/mm_types.h>\u4e2d\uff1a

struct mm_struct {\n    atomic_t mm_users;\n    atomic_t mm_count;\n    struct vm_area_struct *mmap;\n    struct rb_root mm_rb;\n    struct list_head mmlist;\n\n    unsigned long start_code, end_code;    //\u4ee3\u7801\u6bb5\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long start_data, end_data;    //\u6570\u636e\u6bb5\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long start_brk, brk,          //\u5806\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long start_stack;             //\u6808\u7684\u8d77\u59cb\u5730\u5740\n    unsigned long arg_start, arg_end,      //\u547d\u4ee4\u884c\u53c2\u6570\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long env_start, env_end;      //\u73af\u5883\u53d8\u91cf\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long mmap_base;               //\u5185\u5b58\u6620\u5c04\u533a\u7684\u8d77\u59cb\u5730\u5740\n    unsigned long total_vm;                //\u603b\u5171\u6620\u5c04\u7684\u9875\u6570\u76ee\n    unsigned long locked_vm;               //\u88ab\u9501\u5b9a\u4e0d\u80fd\u6362\u51fa\u7684\u9875\u6570\u76ee\n    unsigned long pinned_vm;               //\u65e2\u4e0d\u80fd\u6362\u51fa\uff0c\u4e5f\u4e0d\u80fd\u79fb\u52a8\u7684\u9875\u6570\u76ee\n    unsigned long data_vm;                 //\u6570\u636e\u6bb5\u4e2d\u6620\u5c04\u7684\u9875\u6570\u76ee\n    unsigned long exec_vm;                 //\u4ee3\u7801\u6bb5\u4e2d\u53ef\u6267\u884c\u6587\u4ef6\u7684\u9875\u6570\u76ee\n    unsigned long stack_vm;                //\u6808\u4e2d\u6620\u5c04\u7684\u9875\u6570\u76ee\n};\n

mm_users\u8bb0\u5f55\u6b63\u5728\u4f7f\u7528\u8be5\u5730\u5740\u7684\u8fdb\u7a0b\u6570\u76ee\uff0c\u6bd4\u5982\u5982\u679c\u6709\u4e24\u4e2a\u7ebf\u7a0b\u5171\u4eab\u8be5\u5730\u5740\u7a7a\u95f4\uff0c\u90a3\u4e48mm_users\u7684\u503c\u4fbf\u7b49\u4e8e2\u3002mm_count\u662fmm_struct\u7ed3\u6784\u4f53\u7684\u4e3b\u5f15\u7528\u8ba1\u6570\uff0c\u5f53\u503c\u4e3a0\u65f6\uff0c\u8be5\u7ed3\u6784\u4f53\u4f1a\u88ab\u91ca\u653e\u3002

mmap\u4f7f\u7528\u5355\u72ec\u94fe\u8868\u8fde\u63a5\u6240\u6709\u7684\u5185\u5b58\u533a\u57df\u5bf9\u8c61\u3002\u6bcf\u4e00\u4e2avm_area_struct\u7ed3\u6784\u4f53\u901a\u8fc7\u81ea\u8eab\u7684vm_next\u6307\u9488\u88ab\u8fde\u5165\u94fe\u8868\u3002mmap\u6307\u5411\u94fe\u8868\u4e2d\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002

mm_rb\u5219\u4f7f\u7528\u7ea2\u9ed1\u6811\u3002mm_rb\u6307\u5411\u6839\u8282\u70b9\uff0c\u6bcf\u4e00\u4e2avm_area_struct\u7ed3\u6784\u4f53\u901a\u8fc7\u81ea\u8eab\u7684vm_rb\u8fde\u63a5\u5230\u6811\u4e2d\u3002

\u5728\u8fdb\u7a0b\u7ba1\u7406\u4e0e\u8c03\u5ea6\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86mm\u53d8\u91cf\uff0c\u7528\u6765\u5b58\u653e\u8be5\u8fdb\u7a0b\u4f7f\u7528\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\uff0ccurrent->mm\u5c31\u6307\u5411\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u3002fork()\u51fd\u6570\u5229\u7528copy_mm()\u51fd\u6570\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u3002\u5982\u679c\u7236\u5b50\u8fdb\u7a0b\u5171\u4eab\u5730\u5740\u7a7a\u95f4\uff0c\u5219\u5728\u8c03\u7528clone()\u65f6\uff0c\u8bbe\u7f6eCLONE_VM\u6807\u5fd7\uff0c\u8fd9\u6837\u7684\u8fdb\u7a0b\u5c31\u662f\u7ebf\u7a0b\u3002\u5728Linux\u73af\u5883\u4e0b\uff0c\u662f\u5426\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u51e0\u4e4e\u662f\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u672c\u8d28\u4e0a\u7684\u552f\u4e00\u533a\u522b\u3002\u5982\u679c\u6307\u5b9a\u4e86CLONE_VM,\u7ebf\u7a0b\u5c31\u4e0d\u9700\u8981\u53e6\u5916\u5206\u914d\u5730\u5740\u7a7a\u95f4\u4e86\uff0c\u800c\u662f\u76f4\u63a5\u5c06mm\u57df\u76f4\u63a5\u6307\u5411\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u5373\u53ef\u3002

\u5185\u6838\u7ebf\u7a0b\u6ca1\u6709\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\uff0c\u4e5f\u6ca1\u6709\u76f8\u5173\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\uff0c\u5176mm\u57df\u4e3aNULL\u3002\u5f53\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u88ab\u8c03\u5ea6\u65f6\uff0c\u5b83\u4f1a\u76f4\u63a5\u4f7f\u7528\u524d\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u3002

"},{"location":"linux/kernel/vma/#_3","title":"\u865a\u62df\u5185\u5b58\u533a\u57df","text":"

\u865a\u62df\u5185\u5b58\u533a\u57df\uff08Virtual Memory Area, VMA\uff09\u5728\u5185\u6838\u4e2d\u7528vm_area_struct\u7ed3\u6784\u4f53\u63cf\u8ff0\u3002\u6bcf\u4e2avm_area_struct\u7ed3\u6784\u90fd\u5bf9\u5e94\u4e8e\u6307\u5b9a\u5730\u5740\u7a7a\u95f4\u4e0a\u67d0\u5757\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\u3002vm_start\u6307\u5411\u4e86\u8fd9\u5757\u865a\u62df\u5185\u5b58\u533a\u57df\u7684\u8d77\u59cb\u5730\u5740\uff0cvm_end\u6307\u5411\u4e86\u8fd9\u5757\u865a\u62df\u5185\u5b58\u533a\u57df\u7684\u7ed3\u675f\u5730\u5740\u3002vm_area_struct\u7ed3\u6784\u4f53\u63cf\u8ff0\u7684\u662f[vm_start\uff0cvm_end)\u8fd9\u6837\u4e00\u6bb5\u5de6\u95ed\u53f3\u5f00\u7684\u865a\u62df\u5185\u5b58\u533a\u57df\u3002

\u5185\u6838\u5c06\u6bcf\u4e2a\u5185\u5b58\u533a\u57df\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5185\u5b58\u5bf9\u8c61\u7ba1\u7406\uff0c\u6bcf\u4e2a\u533a\u57df\u53c8\u4e00\u81f4\u7684\u5c5e\u6027\u548c\u64cd\u4f5c\uff0c\u4e0b\u9762\u7ed9\u51favm_area_struct\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\uff1a

struct vm_area_struct {\n    struct mm_struct *vm_mm;                      //\u76f8\u5173\u7684mm_struct\u7ed3\u6784\u4f53\n    unsigned long vm_start;                       //\u533a\u95f4\u7684\u9996\u5730\u5740\n    unsigned long vm_end;                         //\u533a\u95f4\u7684\u5c3e\u5730\u5740\n    struct vm_area_struct *vm_next;               //VMA\u94fe\u8868\n    pgprot_t vm_page_prot;                        //\u8bbf\u95ee\u63a7\u5236\u6743\u9650\n    unsigned long vm_flags;                       //\u63cf\u8ff0\u8be5\u533a\u57df\u7684\u6807\u5fd7\uff0c\u91cd\u8981\u7684\u4e24\u4e2a\u662fVM_IO\u548cVM_RESERVED\n    struct rb_node vm_rb;                         //\u7ea2\u9ed1\u6811\u8282\u70b9\n    struct anon_vma *anon_vma;                    //\u533f\u540dVMA\u5bf9\u8c61\n    const struct vm_operations_struct *vm_ops;    //VMA\u7684\u64cd\u4f5c\u51fd\u6570\n    unsigned long vm_pgoff;                       //\u6587\u4ef6\u4e2d\u8be5\u533a\u57df\u7684\u504f\u79fb\u91cf\n    struct file *vm_file;                         //\u6307\u5411\u4e0e\u8be5\u533a\u57df\u76f8\u5173\u8054\u7684file\u7ed3\u6784\u6307\u9488\n    void *vm_private_data;                        //\u79c1\u6709\u6570\u636e\n};\n

\u6bcf\u4e2a\u5185\u5b58\u63cf\u8ff0\u7b26\u5bf9\u5e94\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u4e2d\u7684\u552f\u4e00\u7a7a\u95f4\uff0c\u8303\u56f4\u662f[vm_start, vm_end)\u3002vm_mm\u6307\u5411\u4e0eVMA\u76f8\u5173\u7684mm_struct\u7ed3\u6784\u4f53\uff0cvm_next\u8d1f\u8d23\u5c06VMA\u4e32\u8054\u6210\u94fe\u8868\u3002

"},{"location":"linux/kernel/vma/#vma","title":"VMA\u6807\u5fd7","text":"

vm_flags\u5b9a\u4e49\u4e86VMA\u7684\u6807\u5fd7\uff0c\u5b83\u8868\u793a\u5185\u5b58\u533a\u57df\u7684\u884c\u4e3a\u548c\u4fe1\u606f\u3002\u4e00\u4e9b\u6bd4\u8f83\u91cd\u8981\u7684\u6807\u5fd7\u6bd4\u5982\uff1aVM_READ\u3001VM_WRITE\u548cVM_EXEC\u6807\u5fd7\u4e86\u5185\u5b58\u533a\u57df\u4e2d\u9875\u9762\u7684\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u53ef\u6267\u884c\u6743\u9650\u3002\u5f53\u8bbf\u95eeVMA\u65f6\uff0c\u9700\u8981\u67e5\u770b\u5176\u8bbf\u95ee\u6743\u9650\u3002

VM_SHARED\u8868\u793a\u5185\u5b58\u533a\u57df\u5305\u542b\u7684\u6620\u5c04\u662f\u5426\u53ef\u4ee5\u5728\u591a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u8be5\u6807\u5fd7\u4f4d\uff0c\u6211\u4eec\u79f0\u5176\u4f4d\u5171\u4eab\u6620\u5c04\uff0c\u5426\u5219\u5c31\u662f\u79c1\u6709\u6620\u5c04\u3002

VM_IO\u6807\u5fd7\u5185\u5b58\u533a\u57df\u4e2d\u5bf9\u8bbe\u5907I/O\u7a7a\u95f4\u7684\u6620\u5c04\u3002\u8be5\u6807\u5fd7\u901a\u5e38\u5728\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u6267\u884cmmap()\u51fd\u6570\u65f6\u624d\u88ab\u8bbe\u7f6e\u3002VM_RESERVED\u6807\u5fd7\u89c4\u5b9a\u4e86\u5185\u5b58\u533a\u57df\u4e0d\u80fd\u88ab\u6362\u51fa\u3002

"},{"location":"linux/kernel/vma/#vma_1","title":"VMA\u64cd\u4f5c","text":"

vm_ops\u57df\u6307\u5411\u4e0e\u5185\u5b58\u533a\u57df\u76f8\u5173\u7684\u64cd\u4f5c\u51fd\u6570\uff0c\u7531vm_operations_struct\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct vm_operations_struct {\n    void (*open)(struct vm_area_struct *);\n    void (*close)(sturct vm_area_struct *);\n    int (*fault)(struct vm_area_struct *, struct vm_fault *);\n    int (*page_mkwrite)(struct vm_area_struct *vma, struct vm_fault *vmf);\n    int (*access)(struct vm_area_struct *, unsigned long, void *, int, int);\n};\n
"},{"location":"linux/kernel/vma/#malloc","title":"malloc()\u539f\u7406","text":"

malloc()\u662fC\u5e93\u91cc\u7684\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u51fd\u6570\uff0c\u7533\u8bf7\u5185\u5b58\u65f6\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a

  • \u65b9\u5f0f\u4e00\uff1a\u901a\u8fc7brk()\u7cfb\u7edf\u8c03\u7528\u4ece\u5806\u5206\u914d\u5185\u5b58\u3002

  • \u65b9\u5f0f\u4e8c\uff1a\u901a\u8fc7mmap()\u7cfb\u7edf\u8c03\u7528\u4ece\u6587\u4ef6\u6620\u5c04\u533a\u5206\u914d\u5185\u5b58\u3002

\u7b2c\u4e00\u79cd\u65b9\u5f0f\u5f88\u7b80\u5355\uff0c\u901a\u8fc7\u7b80\u5355\u5730\u8c03\u6574brk\u6307\u9488\u5411\u9ad8\u5730\u5740\u79fb\u52a8\uff0c\u5373\u5b8c\u6210\u4e86\u5185\u5b58\u5206\u914d\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u76f4\u63a5\u5728\u6587\u4ef6\u6620\u5c04\u533a\u6620\u5c04\u4e00\u5757\u5185\u5b58\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

malloc()\u6e90\u7801\u89c4\u5b9a\u4e86\u4e00\u4e2a\u9608\u503c\uff1a

  • \u5982\u679c\u5206\u914d\u7684\u5185\u5b58\u5c0f\u4e8e\u8fd9\u4e2a\u9608\u503c\uff0c\u5219\u901a\u8fc7brk()\u7533\u8bf7\u5185\u5b58\u3002

  • \u5982\u679c\u5206\u914d\u7684\u5185\u5b58\u5927\u4e8e\u8fd9\u4e2a\u9608\u503c\uff0c\u5219\u901a\u8fc7mmap()\u7533\u8bf7\u5185\u5b58\u3002

\u8bbe\u8ba1\u5185\u5b58\u5206\u914d\u7b56\u7565\u65f6\uff0c\u6838\u5fc3\u76ee\u6807\u662f\u5728\u6027\u80fd\u548c\u8d44\u6e90\u7ba1\u7406\u4e4b\u95f4\u627e\u5230\u6700\u4f73\u5e73\u8861\u70b9\u3002\u5bf9\u4e8e\u5c0f\u5185\u5b58\u5757\u7684\u9891\u7e41\u5206\u914d\u4e0e\u91ca\u653e\u64cd\u4f5c\uff0cmalloc() \u91c7\u7528 brk() \u7cfb\u7edf\u8c03\u7528\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3002\u91ca\u653e\u7684\u5185\u5b58\u5e76\u4e0d\u4f1a\u7acb\u5373\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u800c\u662f\u88ab\u7f13\u5b58\u81f3\u5185\u5b58\u6c60\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5206\u914d\u8bf7\u6c42\u80fd\u591f\u5feb\u901f\u91cd\u7528\u8fd9\u4e9b\u5df2\u91ca\u653e\u7684\u5185\u5b58\u5757\uff0c\u4ece\u800c\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570\u548c\u76f8\u5173\u7684\u6027\u80fd\u5f00\u9500\u3002

\u5bf9\u4e8e\u5927\u5185\u5b58\u5757\u7684\u5206\u914d\uff0cmalloc \u5219\u53ef\u80fd\u4f7f\u7528 mmap() \u7cfb\u7edf\u8c03\u7528\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u9996\u6b21\u8bbf\u95ee\u65f6\u53ef\u80fd\u4f1a\u89e6\u53d1\u7f3a\u9875\u4e2d\u65ad\uff0c\u56e0\u4e3a\u5206\u914d\u7684\u5185\u5b58\u521d\u59cb\u5904\u4e8e\u672a\u6620\u5c04\u72b6\u6001\uff0c\u9700\u8981\u64cd\u4f5c\u7cfb\u7edf\u4ecb\u5165\u4ee5\u5efa\u7acb\u865a\u62df\u5730\u5740\u5230\u7269\u7406\u5185\u5b58\u7684\u6620\u5c04\u3002\u8fd9\u4e00\u673a\u5236\u76f8\u8f83\u4e8e brk() \u7cfb\u7edf\u8c03\u7528\uff0c\u4f1a\u5e26\u6765\u66f4\u9ad8\u7684\u6027\u80fd\u6210\u672c\u3002

"},{"location":"linux/optimization/","title":"\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/#table-of-contents","title":"Table of Contents","text":"
  • CPU\u6027\u80fd
  • \u5185\u5b58\u6027\u80fd
  • \u78c1\u76d8I/O\u6027\u80fd
  • \u7f51\u7edc\u6027\u80fd
"},{"location":"linux/optimization/#_2","title":"\u53c2\u8003\u8d44\u6599","text":"
  • Linux\u6027\u80fd\u4f18\u5316\u5b9e\u6218
"},{"location":"linux/optimization/cpu/","title":"CPU\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/cpu/#cpu_1","title":"CPU\u6027\u80fd\u6307\u6807","text":""},{"location":"linux/optimization/cpu/#cpu_2","title":"CPU\u4f7f\u7528\u7387","text":"

CPU\u4f7f\u7528\u7387\uff0c\u662f\u6307\u5355\u4f4d\u65f6\u95f4\u5185CPU\u4f7f\u7528\u60c5\u51b5\u7684\u7edf\u8ba1\uff0c\u4ee5\u767e\u5206\u6bd4\u7684\u65b9\u5f0f\u5c55\u793a\u3002

\u4e0eCPU\u4f7f\u7528\u7387\u76f8\u5173\u7684\u6307\u6807\u6709\u8bb8\u591a\uff1a

  • user\uff1a\u7528\u6237\u6001\u65f6\u95f4
  • nice\uff1a\u4f4e\u4f18\u5148\u7ea7\u7528\u6237\u6001\u65f6\u95f4
  • system\uff1a\u5185\u6838\u6001\u65f6\u95f4
  • idle\uff1a\u7a7a\u95f2\u65f6\u95f4
  • iowait\uff1a\u7b49\u5f85I/O\u5b8c\u6210\u7684\u65f6\u95f4
  • irq\uff1a\u786c\u4e2d\u65ad\u65f6\u95f4
  • softirq\uff1a\u8f6f\u4e2d\u65ad\u65f6\u95f4
  • steal\uff1a\u7ba1\u7406\u7a0b\u5e8f\u65f6\u95f4
  • guest\uff1a\u8fd0\u884c\u865a\u62df\u5904\u7406\u5668\u7684\u65f6\u95f4
  • guest_nice\uff1a\u4f4e\u4f18\u5148\u7ea7\u8fd0\u884c\u865a\u62df\u5904\u7406\u5668\u7684\u65f6\u95f4

\u4f7f\u7528perf top\u5b9e\u65f6\u663e\u793a\u5360\u7528CPU\u65f6\u949f\u6700\u591a\u7684\u51fd\u6570\u6216\u8005\u6307\u4ee4\uff1a

$ perf top\nSamples: 833  of event 'cpu-clock', Event count (approx.): 97742399\nOverhead  Shared Object       Symbol\n   7.28%  perf                [.] 0x00000000001f78a4\n   4.72%  [kernel]            [k] vsnprintf\n   4.32%  [kernel]            [k] module_get_kallsym\n   3.65%  [kernel]            [k] _raw_spin_unlock_irqrestore\n...\n

\u8f93\u51fa\u7ed3\u679c\u7684\u7b2c\u4e00\u884c\u5206\u522b\u4ee3\u8868\u91c7\u6837\u6570\uff08Samples\uff09\u3001\u4e8b\u4ef6\u7c7b\u578b\uff08event\uff09\u3001\u4e8b\u4ef6\u8ba1\u6570\uff08Event count\uff09\u3002

\u4e0b\u65b9\u7684\u8868\u683c\u5404\u5217\u7684\u542b\u4e49\u662f\uff1a

  • Overhead\uff1a\u5f53\u524d\u51fd\u6570\u6216\u8005\u6307\u4ee4\u5360\u7528\u7684CPU\u65f6\u949f\u767e\u5206\u6bd4
  • Shared Object\uff1a\u51fd\u6570\u6216\u8005\u6307\u4ee4\u6240\u5728\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u540d
  • Symbol\uff1a\u51fd\u6570\u6216\u8005\u6307\u4ee4\u540d\uff0c16\u8fdb\u5236\u5730\u5740\u4ee3\u8868\u672a\u77e5\u7b26\u53f7

perf top\u4e0d\u4fdd\u5b58\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528perf record\u548cperf report\u547d\u4ee4\u6765\u8bb0\u5f55\u548c\u5206\u6790\u6570\u636e\uff0c\u540c\u65f6\u52a0\u4e0a-g\u53c2\u6570\u4ee5\u663e\u793a\u8c03\u7528\u5173\u7cfb\u94fe\u3002

"},{"location":"linux/optimization/cpu/#_1","title":"\u5e73\u5747\u8d1f\u8f7d","text":"

\u6bcf\u5f53\u7cfb\u7edf\u53d8\u6162\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f\u6267\u884ctop\u6216\u8005uptime\u547d\u4ee4\u6765\u4e86\u89e3\u4e0b\u7cfb\u7edf\u7684\u8d1f\u8f7d\u60c5\u51b5\uff0c\u6bd4\u5982\uff1a

$ uptime\n02:34:03 up 2 days, 20:14,  1 user,  load average: 0.63, 0.83, 0.88\n

\u5bf9\u5e94\u7684\u542b\u4e49\u662f\uff1a

02:34:03              //\u5f53\u524d\u65f6\u95f4\nup 2 days, 20:14      //\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4\n1 user                //\u6b63\u5728\u767b\u5f55\u7528\u6237\u6570\nload average: 0.63, 0.83, 0.88  //1\u5206\u949f\u30015\u5206\u949f\u300115\u5206\u949f\u7684\u5e73\u5747\u8d1f\u8f7d\n

\u4ec0\u4e48\u662f\u5e73\u5747\u8d1f\u8f7d\uff1f\u5e73\u5747\u8d1f\u8f7d\u662f\u6307\u5355\u4f4d\u65f6\u95f4\u5185\uff0c\u7cfb\u7edf\u5904\u4e8e\u53ef\u8fd0\u884c\u72b6\u6001\u548c\u4e0d\u53ef\u4e2d\u65ad\u72b6\u6001\u7684\u5e73\u5747\u8fdb\u7a0b\u6570\uff0c\u4e5f\u5c31\u662f\u5e73\u5747\u6d3b\u8dc3\u8fdb\u7a0b\u6570\uff0c\u5b83\u548cCPU\u4f7f\u7528\u7387\u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\u3002

\u6240\u8c13\u53ef\u8fd0\u884c\u72b6\u6001\u7684\u8fdb\u7a0b\uff0c\u662f\u6307\u6b63\u5728\u4f7f\u7528CPU\u6216\u8005\u6b63\u5728\u7b49\u5f85CPU\u7684\u8fdb\u7a0b\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u4f7f\u7528ps\u547d\u4ee4\u770b\u5230\u7684\uff0c\u5904\u4e8eR\u72b6\u6001\uff08Running\u6216Runnable\uff09\u7684\u8fdb\u7a0b\u3002

\u4e0d\u53ef\u4e2d\u65ad\u72b6\u6001\u7684\u8fdb\u7a0b\u5219\u662f\u6b63\u5904\u4e8e\u5185\u6838\u6001\u5173\u952e\u6d41\u7a0b\u4e2d\u7684\u8fdb\u7a0b\uff0c\u5e76\u4e14\u8fd9\u4e9b\u6d41\u7a0b\u4e0d\u80fd\u88ab\u6253\u65ad\u3002\u6bd4\u5982\u6700\u5e38\u89c1\u7684\u662f\u7b49\u5f85\u78c1\u76d8I/O\u7684\u8fdb\u7a0b\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u4f7f\u7528ps\u547d\u4ee4\u770b\u5230\u7684\uff0c\u5904\u4e8eD\u72b6\u6001\uff08Uninterruptible Sleep\uff0c\u4e5f\u88ab\u79f0\u4e3aDisk Sleep\uff09\u7684\u8fdb\u7a0b\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u5411\u78c1\u76d8\u8bfb\u5199\u6570\u636e\u65f6\uff0c\u5b83\u9700\u8981\u7b49\u5f85\u78c1\u76d8\u7684\u56de\u5e94\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u662f\u4e0d\u80fd\u88ab\u6253\u65ad\u7684\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u78c1\u76d8\u548c\u5185\u5b58\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002

\u6240\u4ee5\u5e73\u5747\u8d1f\u8f7d\u53ef\u4ee5\u7b80\u5355\u5730\u7406\u89e3\u4e3a\u662f\u5e73\u5747\u6d3b\u8dc3\u8fdb\u7a0b\u6570\uff0c\u6700\u7406\u60f3\u7684\u60c5\u51b5\u662f\u6bcf\u4e2aCPU\u521a\u597d\u8fd0\u884c\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u8fd9\u65f6\u7684\u5e73\u5747\u8d1f\u8f7d\u5c31\u7b49\u4e8eCPU\u7684\u4e2a\u6570\u3002\u6bd4\u5982\u5728\u53ea\u67092\u4e2aCPU\u7684\u7cfb\u7edf\u4e0a\uff0c\u5982\u679c\u5e73\u5747\u8d1f\u8f7d\u662f2\uff0c\u90a3\u4e48\u610f\u5473\u7740\u6240\u6709\u7684CPU\u90fd\u521a\u597d\u88ab\u5b8c\u5168\u5360\u7528\uff1b\u5982\u679c\u5e73\u5747\u8d1f\u8f7d\u662f4\uff0c\u610f\u5473\u7740\u6709\u4e00\u534a\u7684\u8fdb\u7a0b\u7ade\u4e89\u4e0d\u5230CPU\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2/proc/cpuoinfo\u6765\u83b7\u53d6\u5f53\u524d\u7cfb\u7edf\u6709\u51e0\u4e2aCPU\u3002

\u5e73\u5747\u8d1f\u8f7d\u6709\u4e09\u4e2a\u503c\uff0c\u6211\u4eec\u9700\u8981\u7efc\u5408\u67e5\u770b\uff0c\u5206\u6790\u7cfb\u7edf\u8d1f\u8f7d\u7684\u8d8b\u52bf\uff1a

  • \u5982\u679c\u8fd9\u4e09\u4e2a\u503c\u76f8\u5dee\u4e0d\u5927\uff0c\u8bf4\u660e\u7cfb\u7edf\u8fd0\u884c\u5e73\u7a33\u3002
  • \u5982\u679c1\u5206\u949f\u7684\u503c\u8fdc\u5c0f\u4e8e15\u5206\u949f\u7684\u503c\uff0c\u8bf4\u660e\u7cfb\u7edf\u6700\u8fd1\u7684\u8d1f\u8f7d\u5728\u51cf\u5c11\u3002
  • \u800c\u53cd\u8fc7\u6765\uff0c\u5982\u679c1\u5206\u949f\u7684\u503c\u8fdc\u5927\u4e8e15\u5206\u949f\u7684\u503c\uff0c\u8bf4\u660e\u7cfb\u7edf\u6700\u8fd1\u7684\u8d1f\u8f7d\u5728\u589e\u52a0\u3002

\u5e73\u5747\u8d1f\u8f7d\u548cCPU\u4f7f\u7528\u7387\u4e0d\u4e00\u5b9a\u5b8c\u5168\u5bf9\u5e94\uff0c\u6bd4\u5982\uff1a

  • CPU\u5bc6\u96c6\u578b\u8fdb\u7a0b\uff0c\u4f7f\u7528\u5927\u91cfCPU\u4e5f\u4f1a\u5bfc\u81f4\u5e73\u5747\u8d1f\u8f7d\u5347\u9ad8\uff0c\u6b64\u65f6\u4e24\u8005\u5bf9\u5e94\u3002
  • I/O\u5bc6\u96c6\u578b\u8fdb\u7a0b\uff0c\u7b49\u5f85I/O\u4f1a\u5bfc\u81f4\u5e73\u5747\u8d1f\u8f7d\u5347\u9ad8\uff0c\u4f46\u662fCPU\u4f7f\u7528\u7387\u4e0d\u4e00\u5b9a\u5f88\u9ad8\u3002
  • \u5927\u91cf\u7b49\u5f85CPU\u7684\u8fdb\u7a0b\u8c03\u5ea6\u4f1a\u5bfc\u81f4\u5e73\u5747\u8d1f\u8f7d\u5347\u9ad8\uff0c\u6b64\u65f6\u7684CPU\u4f7f\u7528\u7387\u4e5f\u4f1a\u6bd4\u8f83\u9ad8\u3002
"},{"location":"linux/optimization/cpu/#_2","title":"\u4e0a\u4e0b\u6587\u5207\u6362","text":"

\u4f17\u6240\u5468\u77e5\uff0cLinux\u662f\u4e00\u4e2a\u591a\u4efb\u52a1\u64cd\u4f5c\u7cfb\u7edf\uff0c\u652f\u6301\u8fdc\u5927\u4e8e\u5176CPU\u6838\u5fc3\u6570\u7684\u8fdb\u7a0b\u8fd0\u884c\u3002\u8fd9\u4e9b\u8fdb\u7a0b\u5e76\u975e\u540c\u65f6\u8fd0\u884c\uff0c\u800c\u662f\u4f7f\u7528CPU\u7684\u8c03\u5ea6\u7b97\u6cd5\u8f6e\u6d41\u6267\u884c\u3002\u5728\u6bcf\u4e2a\u4efb\u52a1\u8fd0\u884c\u524d\uff0cCPU\u9700\u8981\u77e5\u9053\u4efb\u52a1\u4ece\u54ea\u91cc\u52a0\u8f7d\u3001\u8fd0\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\u7cfb\u7edf\u9700\u8981\u63d0\u524d\u8bbe\u7f6e\u5bc4\u5b58\u5668\u4ee5\u53ca\u7a0b\u5e8f\u8ba1\u6570\u5668\u7684\u5185\u5bb9\uff0c\u8fd9\u79cd\u73af\u5883\u88ab\u79f0\u4e3aCPU\u4e0a\u4e0b\u6587\u3002

\u800cCPU\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u5c31\u662f\u628a\u4e0a\u4e00\u4e2a\u4efb\u52a1\u7684\u73af\u5883\u4fdd\u5b58\u8d77\u6765\uff0c\u7136\u540e\u52a0\u8f7d\u4e0b\u4e00\u4e2a\u4efb\u52a1\u7684\u73af\u5883\u3002\u6682\u65f6\u6302\u8d77\u7684\u4efb\u52a1\u4f1a\u5728\u672a\u6765\u7684\u67d0\u4e2a\u65f6\u95f4\u70b9\u4e0a\u7ee7\u7eed\u6267\u884c\u3002

\u7531\u4e8e\u8fd0\u884c\u4efb\u52a1\u7684\u4e0d\u540c\uff0cCPU\u4e0a\u4e0b\u6587\u5207\u6362\u4e5f\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684\u73af\u5883\u5206\u4e3a\u4e0d\u540c\u7684\u573a\u666f\uff1a

  • \u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\uff1a\u8fdb\u7a0b\u662fLinux\u7cfb\u7edf\u4e2d\u6700\u57fa\u672c\u7684\u6267\u884c\u5355\u5143\uff0c\u8fdb\u7a0b\u7684\u5207\u6362\u7531\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u6765\u5b8c\u6210\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u4fdd\u5b58\u548c\u6062\u590d\u8fdb\u7a0b\u7684\u5404\u79cd\u5bc4\u5b58\u5668\u72b6\u6001\u3001\u5185\u5b58\u9875\u7b49\u6570\u636e\uff0c\u56e0\u6b64\uff0c\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u6bd4\u8f83\u5927\u3002
  • \u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\uff1a\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u8fdb\u7a0b\uff0c\u540c\u4e00\u8fdb\u7a0b\u7684\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u662f\u5171\u4eab\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u7684\uff0c\u56e0\u6b64\uff0c\u7ebf\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u6bd4\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u8981\u5feb\u5f97\u591a\u3002
  • \u4e2d\u65ad\u4e0a\u4e0b\u6587\u5207\u6362\uff1a\u4e3a\u4e86\u5feb\u901f\u54cd\u5e94\u786c\u4ef6\u7684\u4e8b\u60c5\uff0c\u4e00\u4e2a\u786c\u4ef6\u4e2d\u65ad\u4f1a\u968f\u65f6\u6253\u65ad\u8fdb\u7a0b\u7684\u6b63\u5e38\u6267\u884c\uff0c\u8f6c\u800c\u6267\u884c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002\u5728\u6253\u65ad\u5176\u4ed6\u8fdb\u7a0b\u65f6\uff0c\u5c31\u9700\u8981\u5c06\u8fdb\u7a0b\u5f53\u524d\u7684\u72b6\u6001\u4fdd\u5b58\u4e0b\u6765\uff0c\u4ee5\u4fbf\u540e\u7eed\u6062\u590d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u53eb\u505a\u4e2d\u65ad\u4e0a\u4e0b\u6587\u5207\u6362\u3002

\u5bf9\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u8fd9\u91cc\u9700\u8981\u8bf4\u660e\u4e00\u4e0b\u3002\u4e00\u6b21\u7cfb\u7edf\u8c03\u7528\u6d89\u53ca\u5230\u4ece\u7528\u6237\u6001\u5230\u5185\u6838\u6001\uff0c\u518d\u4ece\u5185\u6838\u6001\u5230\u7528\u6237\u6001\uff0c\u4e00\u5171\u4e24\u6b21CPU\u4e0a\u4e0b\u6587\u5207\u6362\u3002

\u5bf9\u4e8e\u4e00\u822c\u7684\u8fdb\u7a0b\u4e4b\u95f4\u7684\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u53ea\u6709\u5728\u8fdb\u7a0b\u8c03\u5ea6\u7684\u65f6\u5019\u624d\u4f1a\u7528\u5230\u3002Linux\u4e3a\u6bcf\u4e2aCPU\u90fd\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5c31\u7eea\u961f\u5217\uff0c\u5c06\u6d3b\u8dc3\u8fdb\u7a0b\uff08\u6b63\u5728\u8fd0\u884c\u548c\u7b49\u5f85\u8fd0\u884c\u7684\u8fdb\u7a0b\uff09\u6309\u7167\u4f18\u5148\u7ea7\u548c\u7b49\u5f85\u65f6\u95f4\u6392\u5e8f\uff0c\u7136\u540e\u9009\u62e9\u6700\u9700\u8981CPU\u7684\u8fdb\u7a0b\uff0c\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u8bf7\u770b\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5\u3002\u8fdb\u7a0b\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\uff0c\u4f1a\u89e6\u53d1\u8fdb\u7a0b\u8c03\u5ea6\uff1a

  • \u5206\u914d\u4e2a\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u8017\u5c3d\u3002
  • \u8fdb\u7a0b\u5728\u7b49\u5f85\u67d0\u4e2a\u8d44\u6e90\u3002
  • \u8fdb\u7a0b\u8c03\u7528sleep()\u7b49\u51fd\u6570\u4e3b\u52a8\u6302\u8d77\u3002
  • \u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0b\u9700\u8981\u8fd0\u884c\u3002
  • \u53d1\u751f\u786c\u4ef6\u4e2d\u65ad\u3002

\u4e0a\u4e0b\u6587\u5207\u6362\u662f\u8861\u91cf\u6027\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u6307\u6807\uff0c\u5b83\u53ef\u4ee5\u662f\u81ea\u613f\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u975e\u81ea\u613f\u7684\uff1a

  • \u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u53d8\u591a\u4e86\uff0c\u8bf4\u660e\u8fdb\u7a0b\u90fd\u5728\u7b49\u5f85\u8d44\u6e90\uff0c\u53ef\u80fd\u53d1\u751f\u4e86I/O\u95ee\u9898\u3002
  • \u975e\u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u53d8\u591a\u4e86\uff0c\u8bf4\u660e\u8fdb\u7a0b\u90fd\u5728\u88ab\u5f3a\u5236\u8c03\u5ea6\uff0c\u53ef\u80fd\u53d1\u751f\u4e86CPU\u7ade\u4e89\u95ee\u9898\u3002
"},{"location":"linux/optimization/cpu/#cpu_3","title":"CPU\u7f13\u5b58\u547d\u4e2d\u7387","text":"

CPU\u5177\u6709L1\u3001L2\u3001L3\u4e09\u7ea7\u7f13\u5b58\uff0c\u5176\u4e2dL3\u662f\u5171\u4eab\u7684\u3002\u4eceL1\u5230L3\uff0c\u7f13\u5b58\u7684\u5927\u5c0f\u4f9d\u6b21\u589e\u52a0\uff0c\u4f46\u662f\u6027\u80fd\u4f9d\u6b21\u964d\u4f4e\u3002\u5b83\u4eec\u7684\u547d\u4ee4\u7387\uff0c\u8861\u91cf\u7684\u662fCPU\u7f13\u5b58\u7684\u590d\u7528\u60c5\u51b5\u3002

"},{"location":"linux/optimization/cpu/#cpu_4","title":"CPU\u6027\u80fd\u5206\u6790\u5de5\u5177","text":"

\u5176\u4e2dtop\u3001vmstat\u3001pidstat\u51e0\u4e4e\u6db5\u76d6\u4e86\u6240\u6709\u91cd\u8981\u7684CPU\u6027\u80fd\u6307\u6807\u3002

\u4f7f\u7528top\u547d\u4ee4\u52a8\u6001\u76d1\u6d4bCPU\uff1a

$ top\n  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND\n28961 root      20   0   43816   3148   4040 R   3.2  0.0   0:00.01 top\n  620 root      20   0   37280  33676    908 D   0.3  0.4   0:00.01 app\n    1 root      20   0  160072   9416   6752 S   0.0  0.1   0:37.64 systemd\n 1896 root      20   0       0      0      0 Z   0.0  0.0   0:00.00 devapp\n    2 root      20   0       0      0      0 S   0.0  0.0   0:00.10 kthreadd\n    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H\n    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq\n    7 root      20   0       0      0      0 S   0.0  0.0   0:06.37 ksoftirqd/0\n...\n
  • \u4f7f\u7528uptime\u67e5\u770b\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d\uff1a
$ uptime\n02:34:03 up 2 days, 20:14,  1 user,  load average: 0.63, 0.83, 0.88\n
  • \u4f7f\u7528mpstat\u67e5\u770bCPU\u4f7f\u7528\u7387\uff1a
# -P ALL \u8868\u793a\u76d1\u63a7\u6240\u6709CPU\uff0c\u540e\u9762\u6570\u5b575\u8868\u793a\u95f4\u96945\u79d2\u540e\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n$ mpstat -P ALL 5\nLinux 4.15.0 (ubuntu) 09/22/18 _x86_64_ (2 CPU)\n13:30:06     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle\n13:30:11     all   50.05    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   49.95\n13:30:11       0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00\n13:30:11       1  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00\n
  • \u4f7f\u7528pidstat\u67e5\u770b\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\uff1a
# \u95f4\u96945\u79d2\u540e\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n$ pidstat -u 5 1\n13:37:07      UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command\n13:37:12        0      2962  100.00    0.00    0.00    0.00  100.00     1  stress\n
  • \u4f7f\u7528vmstat\u67e5\u770b\u4e0a\u4e0b\u6587\u5207\u6362\u60c5\u51b5\uff1a
# \u6bcf\u96945\u79d2\u8f93\u51fa1\u7ec4\u6570\u636e\n$ vmstat 5\nprocs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----\n r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st\n 0  0      0 7005360  91564 818900    0    0     0     0   25   33  0  0 100  0  0\n

cs\uff1a\u6bcf\u79d2\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570\u3002

in\uff1a\u6bcf\u79d2\u4e2d\u65ad\u6b21\u6570\u3002

r\uff1a\u5c31\u7eea\u961f\u5217\u957f\u5ea6\u3002

b\uff1a\u7b49\u5f85\u8d44\u6e90\u7684\u8fdb\u7a0b\u6570\u3002

  • \u4f7f\u7528pidstat\u67e5\u770b\u6bcf\u4e2a\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u60c5\u51b5\uff1a
# \u6bcf\u96945\u79d2\u8f93\u51fa1\u7ec4\u6570\u636e\n$ pidstat -w 5\nLinux 4.15.0 (ubuntu)  09/23/18  _x86_64_  (2 CPU)\n\n08:18:26      UID       PID   cswch/s nvcswch/s  Command\n08:18:31        0         1      0.20      0.00  systemd\n08:18:31        0         8      5.40      0.00  rcu_sched\n...\n

cswch/s\uff1a\u6bcf\u79d2\u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570\u3002

nvcswch/s\uff1a\u6bcf\u79d2\u975e\u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570\u3002

  • \u4f7f\u7528dstat\u67e5\u770bCPU\u548cI/O\u4f7f\u7528\u60c5\u51b5\uff1a
# \u95f4\u96941\u79d2\u8f93\u51fa10\u7ec4\u6570\u636e\n$ dstat 1 10\nYou did not select any stats, using -cdngy by default.\n--total-cpu-usage-- -dsk/total- -net/total- ---paging-- ---system--\nusr sys idl wai stl| read  writ| recv  send|  in   out | int   csw\n  0   0  96   4   0|1219k  408k|   0     0 |   0     0 |  42   885\n  0   0   2  98   0|  34M    0 | 198B  790B|   0     0 |  42   138\n  0   0   0 100   0|  34M    0 |  66B  342B|   0     0 |  42   135\n  0   0  84  16   0|5633k    0 |  66B  342B|   0     0 |  52   177\n  0   3  39  58   0|  22M    0 |  66B  342B|   0     0 |  43   144\n  0   0   0 100   0|  34M    0 | 200B  450B|   0     0 |  46   147\n  0   0   2  98   0|  34M    0 |  66B  342B|   0     0 |  45   134\n  0   0   0 100   0|  34M    0 |  66B  342B|   0     0 |  39   131\n  0   0  83  17   0|5633k    0 |  66B  342B|   0     0 |  46   168\n  0   3  39  59   0|  22M    0 |  66B  342B|   0     0 |  37   134\n...\n
  • \u4f7f\u7528sar\u67e5\u770b\u7cfb\u7edf\u7684\u7f51\u7edc\u6536\u53d1\u60c5\u51b5\uff1a
# -n DEV \u8868\u793a\u663e\u793a\u7f51\u7edc\u6536\u53d1\u7684\u62a5\u544a\uff0c\u95f4\u96941\u79d2\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n$ sar -n DEV 1\n15:03:46        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil\n15:03:47         eth0  12607.00   6304.00    664.86    358.11      0.00      0.00      0.00      0.01\n15:03:47      docker0   6302.00  12604.00    270.79    664.66      0.00      0.00      0.00      0.00\n15:03:47           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00\n15:03:47    veth9f6bbcd   6302.00  12604.00    356.95    664.66      0.00      0.00      0.00      0.05\n

IFACE\uff1a\u7f51\u5361

rxpck/txpck\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u6570\u636e\u5305

rxkB/txkB\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u5343\u5b57\u8282

rxcmp/txcmp\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u538b\u7f29\u6570\u636e\u5305

rxmcst/txmcst\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u591a\u64ad\u6570\u636e\u5305

%ifutil\uff1a\u7f51\u7edc\u63a5\u53e3\u7684\u4f7f\u7528\u7387

  • \u4f7f\u7528tcpdump\u6293\u53d6\u7f51\u7edc\u5305\uff1a
# -i eth0 \u53ea\u6293\u53d6eth0\u7f51\u5361\uff0c-n\u4e0d\u89e3\u6790\u534f\u8bae\u540d\u548c\u4e3b\u673a\u540d\n# tcp port 80\u8868\u793a\u53ea\u6293\u53d6tcp\u534f\u8bae\u5e76\u4e14\u7aef\u53e3\u53f7\u4e3a80\u7684\u7f51\u7edc\u5e27\n$ tcpdump -i eth0 -n tcp port 80\n15:11:32.678966 IP 192.168.0.2.18238 > 192.168.0.30.80: Flags [S], seq 458303614, win 512, length 0\n...\n
"},{"location":"linux/optimization/disk/","title":"\u78c1\u76d8I/O\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/disk/#_1","title":"\u78c1\u76d8\u6027\u80fd\u6307\u6807","text":"
  • \u4f7f\u7528\u7387\uff1a\u78c1\u76d8\u5904\u7406I/O\u7684\u65f6\u95f4\u767e\u5206\u6bd4\u3002
  • \u9971\u548c\u5ea6\uff1a\u78c1\u76d8\u5904\u7406I/O\u7684\u7e41\u5fd9\u7a0b\u5ea6\u3002
  • IOPS\uff1a\u6bcf\u79d2\u7684I/O\u8bf7\u6c42\u6570\u3002
  • \u541e\u5410\u91cf\uff1a\u6bcf\u79d2\u7684I/O\u8bf7\u6c42\u6570\u636e\u91cf\u3002
  • \u54cd\u5e94\u65f6\u95f4\uff1aI/O\u8bf7\u6c42\u4ece\u53d1\u51fa\u5230\u6536\u5230\u54cd\u5e94\u7684\u95f4\u9694\u65f6\u95f4\u3002
"},{"location":"linux/optimization/disk/#_2","title":"\u78c1\u76d8\u6027\u80fd\u5206\u6790\u5de5\u5177","text":"
  • \u4f7f\u7528iostat\u89c2\u5bdf\u78c1\u76d8I/O\u4f7f\u7528\u60c5\u51b5\uff1a
# -d -x\u8868\u793a\u663e\u793a\u6240\u6709\u78c1\u76d8I/O\u7684\u6307\u6807\n$ iostat -d -x 1 \nDevice            r/s     w/s     rkB/s     wkB/s   rrqm/s   wrqm/s  %rrqm  %wrqm r_await w_await aqu-sz rareq-sz wareq-sz  svctm  %util \nloop0            0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \nloop1            0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \nsda              0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \nsdb              0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \n
  • \u4f7f\u7528pidstat\u89c2\u5bdf\u8fdb\u7a0bI/O\u4f7f\u7528\u60c5\u51b5\uff1a
$ pidstat -d 1 \n13:39:51      UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command \n13:39:52      102       916      0.00      4.00      0.00       0  rsyslogd\n
  • \u4f7f\u7528iotop\u5bf9\u8fdb\u7a0bI/O\u6392\u5e8f\uff1a
$ iotop\nTotal DISK READ :       0.00 B/s | Total DISK WRITE :       7.85 K/s \nActual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s \n  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND \n15055 be/3 root        0.00 B/s    7.85 K/s  0.00 %  0.00 % systemd-journald \n
  • \u4f7f\u7528filetop\u8ddf\u8e2a\u6587\u4ef6\u7684\u8bfb\u5199\u60c5\u51b5\uff1a
# -C \u9009\u9879\u8868\u793a\u8f93\u51fa\u65b0\u5185\u5bb9\u65f6\u4e0d\u6e05\u7a7a\u5c4f\u5e55 \n$ ./filetop -C \n\nTID    COMM             READS  WRITES R_Kb    W_Kb    T FILE \n514    python           0      1      0       2832    R 669.txt \n514    python           0      1      0       2490    R 667.txt \n514    python           0      1      0       2685    R 671.txt \n514    python           0      1      0       2392    R 670.txt \n514    python           0      1      0       2050    R 672.txt \n\n...\n\nTID    COMM             READS  WRITES R_Kb    W_Kb    T FILE \n514    python           2      0      5957    0       R 651.txt \n514    python           2      0      5371    0       R 112.txt \n514    python           2      0      4785    0       R 861.txt \n514    python           2      0      4736    0       R 213.txt \n514    python           2      0      4443    0       R 45.txt \n
"},{"location":"linux/optimization/memory/","title":"\u5185\u5b58\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/memory/#_2","title":"\u5185\u5b58\u6027\u80fd\u6307\u6807","text":""},{"location":"linux/optimization/memory/#buffercache","title":"Buffer/Cache","text":"

Buffer\u548cCache\u88ab\u8bbe\u8ba1\u7528\u6765\u63d0\u5347\u7cfb\u7edfI/O\u7684\u6027\u80fd\uff0c\u5b83\u4eec\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5145\u5f53\u8d77\u6162\u901f\u78c1\u76d8\u4e0e\u5feb\u901fCPU\u4e4b\u95f4\u7684\u6865\u6881\u3002

  • Buffers\u662f\u5bf9\u78c1\u76d8\u6570\u636e\u7684\u7f13\u5b58\uff0c\u901a\u5e38\u4e0d\u4f1a\u7279\u522b\u5927\u3002

  • Cached\u662f\u5bf9\u6587\u4ef6\u6570\u636e\u7684\u7f13\u5b58\u3002

\u4e0d\u8bba\u662f\u8bfb\u8fd8\u662f\u5199\uff0c\u5e94\u7528\u7a0b\u5e8f\u90fd\u4f18\u5148\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u8bbf\u95ee\u4f4e\u901f\u7684\u78c1\u76d8\uff0c\u4ece\u800c\u63d0\u9ad8\u7cfb\u7edf\u7684I/O\u6027\u80fd\u3002

"},{"location":"linux/optimization/memory/#_3","title":"\u5185\u5b58\u6027\u80fd\u5206\u6790\u5de5\u5177","text":"
  • \u4f7f\u7528free\u67e5\u770b\u7cfb\u7edf\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a
$ free\n              total        used        free      shared  buff/cache   available\nMem:        8169348      263524     6875352         668     1030472     7611064\nSwap:             0           0           0\n

total\uff1a\u603b\u5185\u5b58\u5927\u5c0f

used\uff1a\u5df2\u7ecf\u4f7f\u7528\u7684\u5185\u5b58\u5927\u5c0f

free\uff1a\u7a7a\u95f2\u7684\u5185\u5b58\u5927\u5c0f

shared\uff1a\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u7684\u5185\u5b58\u5927\u5c0f

buff/cache\uff1a\u7f13\u5b58\u548c\u7f13\u51b2\u533a\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f

available\uff1a\u53ef\u7528\u7684\u5185\u5b58\u5927\u5c0f

  • \u4f7f\u7528top\u67e5\u770b\u8fdb\u7a0b\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a
$ top\n...\nKiB Mem :  8169348 total,  6871440 free,   267096 used,  1030812 buff/cache\nKiB Swap:        0 total,        0 free,        0 used.  7607492 avail Mem\n\n\n  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND\n  430 root      19  -1  122360  35588  23748 S   0.0  0.4   0:32.17 systemd-journal\n 1075 root      20   0  771860  22744  11368 S   0.0  0.3   0:38.89 snapd\n 1048 root      20   0  170904  17292   9488 S   0.0  0.2   0:00.24 networkd-dispat\n    1 root      20   0   78020   9156   6644 S   0.0  0.1   0:22.92 systemd\n12376 azure     20   0   76632   7456   6420 S   0.0  0.1   0:00.01 systemd\n12374 root      20   0  107984   7312   6304 S   0.0  0.1   0:00.00 sshd\n...\n

VIRT\uff1a\u8fdb\u7a0b\u4f7f\u7528\u7684\u865a\u62df\u5185\u5b58\u5927\u5c0f\uff0c\u7533\u8bf7\u8fc7\u4f46\u5c1a\u672a\u5206\u914d\u7269\u7406\u5185\u5b58\u4e5f\u4f1a\u8ba1\u7b97\u5728\u5185\u3002

RES\uff1a\u8fdb\u7a0b\u5b9e\u9645\u4f7f\u7528\u7684\u7269\u7406\u5185\u5b58\u5927\u5c0f\u3002

SHR\uff1a\u8fdb\u7a0b\u4f7f\u7528\u7684\u5171\u4eab\u5185\u5b58\u5927\u5c0f\uff0c\u6bd4\u5982\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002

%MEM\uff1a\u8fdb\u7a0b\u4f7f\u7528\u7684\u7269\u7406\u5185\u5b58\u5360\u7cfb\u7edf\u603b\u5185\u5b58\u7684\u767e\u5206\u6bd4\u3002

  • \u4f7f\u7528cachestat\u548ccachetop\u67e5\u770b\u7f13\u5b58\u547d\u4e2d\u60c5\u51b5\uff1a

\u8fd9\u4e24\u4e2a\u5de5\u5177\u662fbcc\u8f6f\u4ef6\u5305\u7684\u4e00\u90e8\u5206\uff0c\u57fa\u4e8eebpf\u6280\u672f\u3002

$ cachestat 1 3\n   TOTAL   MISSES     HITS  DIRTIES   BUFFERS_MB  CACHED_MB\n       2        0        2        1           17        279\n       2        0        2        1           17        279\n       2        0        2        1           17        279 \n

TOTAL\uff1a\u603b\u7f13\u5b58\u6b21\u6570\u3002

MISSES\uff1a\u7f13\u5b58\u672a\u547d\u4e2d\u7684\u6b21\u6570\u3002

HITS\uff1a\u7f13\u5b58\u547d\u4e2d\u7684\u6b21\u6570\u3002

DIRTIES\uff1a\u7f13\u5b58\u4e2ddirty pages\u810f\u9875\uff09\u7684\u6b21\u6570\u3002

BUFFERS_MB\uff1a\u7f13\u5b58Buffers\u7684\u5927\u5c0f\uff0c\u5355\u4f4d\u662fMB\u3002

CACHED_MB\uff1a\u7f13\u5b58\u4e2dCache\u7684\u6570\u91cf\uff0c\u5355\u4f4d\u662fMB\u3002

$ cachetop\n11:58:50 Buffers MB: 258 / Cached MB: 347 / Sort: HITS / Order: ascending\nPID      UID      CMD              HITS     MISSES   DIRTIES  READ_HIT%  WRITE_HIT%\n   13029 root     python                  1        0        0     100.0%       0.0%\n

READ_HIT\uff1a\u8bfb\u7f13\u5b58\u547d\u4e2d\u7387\u3002

WRITE_HIT\uff1a\u5199\u7f13\u5b58\u547d\u4e2d\u7387\u3002

  • \u4f7f\u7528smem\u5c06\u8fdb\u7a0b\u6309\u7167swap\u4f7f\u7528\u91cf\u6392\u5e8f\uff1a
$ smem --sort swap\n  PID User     Command                         Swap      USS      PSS      RSS \n1168907 ubuntu   bash                               0      468     1091     3576 \n1169141 ubuntu   /bin/bash --init-file /home        0     1060     1693     4416 \n1207585 ubuntu   sleep 180                          0      120      268     1916 \n1208733 ubuntu   /usr/bin/python3 /usr/bin/s        0    10908    11197    13956 \n1784762 ubuntu   /bin/bash --init-file /home        0      976     1038     3104 \n2002992 ubuntu   /lib/systemd/systemd --user        0     2052     2620     6248 \n2003004 ubuntu   /usr/libexec/tracker-miner-        0    10288    10768    16148 \n2003010 ubuntu   /usr/bin/dbus-daemon --sess        0      776      903     3868\n...\n
  • \u4f7f\u7528sar\u67e5\u770b\u5185\u5b58\u5404\u6307\u6807\u53d8\u5316\u60c5\u51b5\uff1a
# \u95f4\u96941\u79d2\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n# -r\u8868\u793a\u663e\u793a\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c-S\u8868\u793a\u663e\u793aSwap\u4f7f\u7528\u60c5\u51b5\n$ sar -r -S 1\n04:39:56    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty\n04:39:57      6249676   6839824   1919632     23.50    740512     67316   1691736     10.22    815156    841868         4\n\n04:39:56    kbswpfree kbswpused  %swpused  kbswpcad   %swpcad\n04:39:57      8388604         0      0.00         0      0.00\n\n04:39:57    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty\n04:39:58      6184472   6807064   1984836     24.30    772768     67380   1691736     10.22    847932    874224        20\n\n04:39:57    kbswpfree kbswpused  %swpused  kbswpcad   %swpcad\n04:39:58      8388604         0      0.00         0      0.00\n\n\u2026\n\n\n04:44:06    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty\n04:44:07       152780   6525716   8016528     98.13   6530440     51316   1691736     10.22    867124   6869332         0\n\n04:44:06    kbswpfree kbswpused  %swpused  kbswpcad   %swpcad\n04:44:07      8384508      4096      0.05        52      1.27\n
"},{"location":"linux/optimization/network/","title":"\u7f51\u7edc\u6027\u80fd\u8c03\u4f18","text":""},{"location":"others/","title":"\u4e0d\u592a\u597d\u5206\u7c7b\u7684\u653e\u8fd9","text":""},{"location":"others/#_2","title":"\u53c2\u8003\u8d44\u6599","text":"
  • \u7a0b\u5e8f\u5458\u7684\u81ea\u6211\u4fee\u517b
"},{"location":"others/ld/","title":"\u4ece\u4ee3\u7801\u5230\u53ef\u6267\u884c\u6587\u4ef6","text":""},{"location":"others/ld/#gcc","title":"gcc\u7f16\u8bd1\u6b65\u9aa4","text":""},{"location":"others/ld/#_2","title":"\u7a0b\u5e8f\u7684\u94fe\u63a5\u4e0e\u88c5\u8f7d","text":""},{"location":"others/ld/#_3","title":"\u7a0b\u5e8f\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f","text":"

\u7ecf\u8fc7\u9884\u5904\u7406\u3001\u7f16\u8bd1\u3001\u6c47\u7f16\u4ee5\u540e\uff0c.c\u6587\u4ef6\u88ab\u7f16\u8bd1\u6210\u4e86.o\u6587\u4ef6\uff0c\u53c8\u53eb\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6\uff0c\u8fd9\u662fELF\u683c\u5f0f\u7684\u4e00\u79cd\u3002\u8fd9\u4e2a\u6587\u4ef6\u683c\u5f0f\u662f\u8fd9\u6837\u7684\u3002

\u5176\u4e2dELF Header\u8bb0\u5f55\u4e00\u4e2a\u4e2asection\u7684\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528readelf -h\u67e5\u770b\u4e00\u4e2a.o\u6587\u4ef6\u7684Header\u4fe1\u606f\u3002\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u91cc\u5b58\u653e\u7740\u7a0b\u5e8f\u7684\u4ee3\u7801\u4ee5\u53ca\u53d8\u91cf\u7b49\u7b49\u4fe1\u606f\u3002

.text\uff1a\u5b58\u653e\u7f16\u8bd1\u597d\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\u3002

.rodata\uff1a\u5b58\u653e\u53ea\u8bfb\u6570\u636e\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u5e38\u91cf\u3002

.data\uff1a\u5b58\u653e\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\u3002

.bss\uff1a\u5b58\u653e\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\uff0c\u5168\u90e8\u7f6e\u4e3a0.

.symtab\uff1a\u7b26\u53f7\u8868\uff0c\u5b58\u653e\u7684\u662f\u51fd\u6570\u548c\u53d8\u91cf\u7684\u7b26\u53f7\u4fe1\u606f\u3002

.rel.text\uff0c.rel.data\uff1a\u5b58\u653e\u9700\u8981\u91cd\u5b9a\u4f4d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002

.strtab\uff1a\u5b57\u7b26\u4e32\u8868\uff0c\u6bd4\u5982\u51fd\u6570\u540d\u3001\u53d8\u91cf\u540d\u3002

\u8fd9\u4e9bsection\u7684\u6570\u636e\u4fe1\u606f\u4fdd\u5b58\u5728Section Header Table\u91cc\u3002\u4e3a\u4ec0\u4e48\u53eb\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6\uff0c\u56e0\u4e3a\u7f16\u8bd1\u6210.o\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u4e86\u4e00\u4e9b\u5916\u90e8\u51fd\u6570\u6216\u8005\u53d8\u91cf\uff0c\u6b64\u65f6\u5e76\u4e0d\u77e5\u9053\u8fd9\u4e9b\u51fd\u6570\u6216\u53d8\u91cf\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\uff0c\u6240\u4ee5\u9700\u8981\u5728.o\u6587\u4ef6\u4e2d\u8bf4\u660e\u8fd9\u4e9b\u7b26\u53f7\u5c06\u6765\u662f\u9700\u8981\u91cd\u65b0\u5b9a\u4f4d\u7684\u3002

"},{"location":"others/ld/#_4","title":"\u9759\u6001\u5e93\u4e0e\u52a8\u6001\u5e93","text":"

\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u4e2a.o\u6587\u4ef6\u6253\u5305\u6210\u5e93\u6587\u4ef6\u7684\u5f62\u5f0f\uff0c\u4f9b\u5176\u4ed6\u7a0b\u5e8f\u8c03\u7528\u3002\u5f62\u6210\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u53eb\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u662fELF\u6587\u4ef6\u7684\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u3002\u5b83\u7684\u7ed3\u6784\u5982\u4e0b\u3002

\u8fd9\u4e2a\u683c\u5f0f\u4e0e\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u5728\u94fe\u63a5\u7684\u8fc7\u7a0b\u4e2d\uff0c\u90e8\u5206section\u5408\u5e76\u4e86\u3002\u6bd4\u5982\u90fd\u662f\u4e8c\u8fdb\u5236\u4ee3\u7801\uff0c\u90a3\u5c31\u4e00\u8d77\u5408\u5e76\u5230.text\u6bb5\u4e2d\u53bb\u3002\u5c0f\u7684section\u5408\u5e76\u6210\u4e3a\u4e86\u5927\u7684segment\u3002\u5728ELF\u5934\u91cc\u9762\uff0c\u6709\u4e2a\u53ebe_entry\u7684\u865a\u62df\u5730\u5740\uff0c\u662f\u7a0b\u5e8f\u7684\u5165\u53e3\u5730\u5740\u3002\u5f53\u9759\u6001\u5e93\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u94fe\u63a5\u5728\u4e00\u8d77\u65f6\uff0c\u4f1a\u628a\u9759\u6001\u5e93\u7684\u4fe1\u606f\u5408\u5e76\u8fdb\u53bb\uff0c\u5f62\u6210\u4e00\u4e2a\u66f4\u5927\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd9\u6837\u7684\u597d\u5904\u5c31\u662f\u8be5\u7a0b\u5e8f\u6267\u884c\u7684\u65f6\u5019\u4e0d\u4f9d\u8d56\u4e8e\u9759\u6001\u5e93\u6587\u4ef6\u3002\u574f\u5904\u5c31\u662f\uff0c\u6709\u5176\u4ed6\u9700\u8981\u94fe\u63a5\u7684\u6587\u4ef6\uff0c\u540c\u6837\u4f1a\u590d\u5236\u4e00\u4efd\u4fe1\u606f\u5230\u5185\u5b58\uff0c\u9020\u6210\u4e86\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002\u5f53\u9759\u6001\u5e93\u66f4\u65b0\u540e\uff0c\u5982\u679c\u4e0d\u91cd\u65b0\u8fdb\u884c\u7f16\u8bd1\uff0c\u7a0b\u5e8f\u5c31\u65e0\u6cd5\u8fd0\u884c\u4e86\u3002\u4e3a\u4e86\u89e3\u51b3\u7a7a\u95f4\u6d6a\u8d39\u4ee5\u53ca\u7a0b\u5e8f\u6269\u5c55\u6027\u5dee\u7684\u95ee\u9898\uff0c\u52a8\u6001\u5e93\u5c31\u51fa\u73b0\u4e86\u3002\u52a8\u6001\u5e93\u4e5f\u662fELF\u6587\u4ef6\u7684\u7b2c\u4e09\u79cd\u5f62\u5f0f\u2014\u2014\u5171\u4eab\u5bf9\u8c61\u6587\u4ef6\u3002

\u5f53\u52a8\u6001\u5e93\u88ab\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\u53bb\u65f6\uff0c\u53ea\u4fdd\u7559\u4e86\u5bf9\u52a8\u6001\u5e93\u7684\u5f15\u7528\uff0c\u5e76\u4e14\u4e00\u4e2a\u52a8\u6001\u5e93\u53ef\u4ee5\u88ab\u591a\u4e2a\u7a0b\u5e8f\u5f15\u7528\u3002\u800c\u4e3a\u4e86\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u51fd\u6570\u4f4d\u7f6e\uff0c\u57fa\u4e8e\u52a8\u6001\u5e93\u94fe\u63a5\u7684.so\u6587\u4ef6\u591a\u4e86\u4e00\u4e9b\u4fe1\u606f\u3002\u9996\u5148\u662f\u4e00\u4e2a\u53eb.interp\u7684\u6bb5\uff0c\u8fd9\u91cc\u9762\u662fld-linux.so\uff0c\u8fd9\u662f\u52a8\u6001\u94fe\u63a5\u5668\uff0c\u8fd0\u884c\u65f6\u7684\u94fe\u63a5\u52a8\u4f5c\u90fd\u662f\u5b83\u505a\u7684\u3002\u53e6\u5916\u8fd8\u591a\u4e86\u4e24\u4e2asection\uff0c\u4e00\u4e2a\u662f.plt\uff0c\u8fc7\u7a0b\u94fe\u63a5\u8868\uff0c\u4e00\u4e2a\u662f.got\uff0c\u5168\u5c40\u504f\u79fb\u91cf\u8868\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570func()\u9700\u8981\u91cd\u5b9a\u4f4d\uff0c\u7531\u4e8e\u4ee3\u7801\u6bb5\u662f\u4e0d\u53ef\u5199\u7684\uff0c\u6240\u4ee5\u5728\u94fe\u63a5\u8fc7\u7a0b\u4e2d\uff0c\u65e0\u6cd5\u5c06\u5176\u5b9e\u9645\u5730\u5740\u5199\u5165\u3002\u89e3\u51b3\u529e\u6cd5\u662f\u7528\u4e86\u4e00\u4e2a\u5c0f\u6280\u5de7\uff0c\u5728\u6c47\u7f16\u6307\u4ee4\u4e2d\uff0c\u5f53\u6267\u884c\u5230func()\u51fd\u6570\u65f6\uff0c\u544a\u8bc9\u7a0b\u5e8f\u4f60\u8981\u53bbPLT[x]\u4f4d\u7f6e\u5904\u53bb\u5bfb\u627efunc\u51fd\u6570\u7684\u4f4d\u7f6e\u3002\u800cPLT[X]\u53c8\u544a\u8bc9\u7a0b\u5e8f\u4f60\u8981\u53bbGOT[Y]\u7684\u5730\u65b9\u53bb\u627e\u51fd\u6570\u4f4d\u7f6e\u3002\u5f53\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\uff0cGOT[Y]\u91cc\u7684\u4f4d\u7f6e\u4fe1\u606f\u662f\u7a7a\u7684\uff0c\u4e8e\u662fGOT\u544a\u8bc9PLT\uff0c\u6211\u8fd9\u91cc\u6ca1\u6709\u5b9a\u4f4d\u4fe1\u606f\u3002PLT\u6536\u5230\u8fd9\u4e2a\u4fe1\u606f\u4e4b\u540e\uff0c\u8c03\u7528PLT[0]\uff0cPLT[0]\u8f6c\u800c\u8c03\u7528GOT[2]\uff0c\u8fd9\u91cc\u9762\u4fdd\u5b58\u7684\u662f\u94fe\u63a5\u5668\u7684\u5165\u53e3\u5730\u5740\u3002\u94fe\u63a5\u5668\u4f1a\u627e\u5230func()\u51fd\u6570\u7684\u5165\u53e3\u5730\u5740\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u5730\u5740\u5199\u5165\u5230GOT[Y]\u4e2d\uff0c\u90a3\u4e48\u4e0b\u6b21\u518d\u8c03\u7528func()\u51fd\u6570\u65f6\uff0c\u5c31\u4e0d\u9700\u8981\u4ee5\u4e0a\u8fd9\u4e48\u9ebb\u70e6\u7684\u6b65\u9aa4\u4e86\uff0c\u56e0\u4e3a\u51fd\u6570\u7684\u5730\u5740\u5df2\u7ecf\u5199\u5728\u4e86GOT\u8868\u4e2d\u3002

"},{"location":"others/ld/#_5","title":"\u7a0b\u5e8f\u7684\u88c5\u8f7d","text":"

\u901a\u8fc7execv()\u7cfb\u7edf\u8c03\u7528\uff0c\u6267\u884cload_elf_binary()\u51fd\u6570\uff0c\u5c06\u53ef\u6267\u884c\u6587\u4ef6\u88c5\u8f7d\u5230\u5185\u5b58\u4e2d\u3002

\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u5f85\u5b9a\u3002

"},{"location":"pl/","title":"\u7f16\u7a0b\u7684\u5fc3\u5f97\u4f53\u4f1a","text":""},{"location":"rtos/","title":"RTOS","text":""},{"location":"rtos/#table-of-contents","title":"Table of Contents","text":"
  • freertos
  • CMSIS\u6807\u51c6
  • rt-thread
"},{"location":"rtos/#_1","title":"\u53c2\u8003\u8d44\u6599","text":"
  • freertos doc
  • rt-thread\u6587\u6863\u4e2d\u5fc3
  • \u5d4c\u5165\u5f0f\u5b9e\u65f6\u64cd\u4f5c\u7cfb\u7edfRT-Thread\u8bbe\u8ba1\u4e0e\u5b9e\u73b0
"},{"location":"rtos/freertos/","title":"\u7b80\u660e\u5165\u95e8","text":"

\u5927\u591a\u6570\u7684\u64cd\u4f5c\u7cfb\u7edf\u90fd\u80fd\u540c\u65f6\u6267\u884c\u591a\u4e2a\u7a0b\u5e8f\uff0c\u8fd9\u88ab\u79f0\u4e3a\u591a\u4efb\u52a1\u5904\u7406\u3002\u5b9e\u9645\u4e0a\uff0c\u6bcf\u4e2a\u5904\u7406\u5668\u6838\u5fc3\u5728\u7ed9\u5b9a\u65f6\u95f4\u7247\u5185\u53ea\u80fd\u8fd0\u884c\u5355\u4e2a\u4efb\u52a1\u3002CPU\u901a\u8fc7\u5feb\u901f\u5207\u6362\u65f6\u95f4\u7247\u9020\u6210\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u6267\u884c\u7684\u5047\u8c61\u3002 \u5b9e\u65f6\u64cd\u4f5c\u7cfb\u7edf(RTOS)\u4e2d\u7684\u4efb\u52a1\u8c03\u5ea6\u5668\u65e8\u5728\u63d0\u4f9b\u53ef\u9884\u6d4b\u7684\u6267\u884c\u6a21\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u5bf9\u5d4c\u5165\u5f0f\u7cfb\u7edf\u610f\u4e49\u91cd\u5927\uff0c\u56e0\u4e3a\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f80\u5f80\u6709\u5b9e\u65f6\u6027\u7684\u8981\u6c42\uff0c\u6bd4\u5982\u6211\u8981\u6c42\u57283\u79d2\u5185\u53d1\u51fa\u8b66\u62a5\u58f0\u3002

\u4f7f\u7528STM32CubeMX\u521b\u5efa\u7684FreeRTOS\u5de5\u7a0b\u4e2d\uff0c\u6e90\u7801\u4e3b\u8981\u6d89\u53ca\u4e24\u4e2a\u76ee\u5f55\uff1a

  • Core
    • Inc\u76ee\u5f55\u4e0b\u7684FreeRTOSConfig.h\u662f\u914d\u7f6e\u6587\u4ef6
    • Src\u76ee\u5f55\u4e0b\u7684freertos.c\u662fSTM32CubeMX\u521b\u5efa\u7684\u9ed8\u8ba4\u4efb\u52a1
  • Middlewares/Thirt_Party/FreeRTOS/Source
    • \u6839\u76ee\u5f55\u4e0b\u662f\u6838\u5fc3\u6587\u4ef6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u662f\u901a\u7528\u7684
    • portable\u76ee\u5f55\u4e0b\u662f\u79fb\u690d\u65f6\u9700\u8981\u7684\u6587\u4ef6\uff1aport.c\u548cportmacro.h
      • \u76ee\u5f55\u540d\u4e3a\uff1a[compiler]/[architecture]
      • \u6bd4\u5982\uff1aRVDS/ARM-CM3\uff0c\u8fd9\u8868\u793acortexM3\u67b6\u6784\u5728RVDS\u5de5\u5177\u4e0a\u7684\u79fb\u690d\u6587\u4ef6
FreeRTOS/Srouce/ \u4f5c\u7528 tasks.c \u5fc5\u987b\uff0c\u4efb\u52a1\u64cd\u4f5c list.c \u5fc5\u987b\uff0c \u5217\u8868 queue.c \u57fa\u672c\u5fc5\u987b\uff0c\u63d0\u4f9b\u961f\u5217\u64cd\u4f5c\u3001\u4fe1\u53f7\u91cf timer.c \u53ef\u9009\uff0c\u63d0\u4f9b\u5b9a\u65f6\u5668 event_groups.c \u53ef\u9009\uff0c\u63d0\u4f9bevent group\u529f\u80fd croutine.c \u53ef\u9009\uff0c\u5df2\u8fc7\u65f6

\u5934\u6587\u4ef6\u76ee\u5f55\u9700\u8981\u5305\u542b\uff1a

  • FreeRTOS\u81ea\u8eab\u5934\u6587\u4ef6\uff1aMiddlewares/Third_Party/FreeRTOS/Source/include

  • \u914d\u7f6e\u6587\u4ef6\uff1aCore/Inc/FreeRTOSConfig.h

\u6570\u636e\u7c7b\u578b\uff1a

\u5728portmacro.h\u5934\u6587\u4ef6\u91cc\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a\u6570\u636e\u7c7b\u578b\uff1a

  • TickType_t\uff1a
    • FreeRTOS\u914d\u7f6e\u4e86\u4e00\u4e2a\u5468\u671f\u6027\u7684\u65f6\u949f\u4e2d\u65ad\uff1aTick Interrupt
    • \u6bcf\u53d1\u751f\u4e00\u6b21\u4e2d\u65ad\uff0c\u4e2d\u65ad\u6b21\u6570\u7d2f\u52a0\uff0c\u8fd9\u88ab\u79f0\u4e3atick count
    • tick count\u8fd9\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u5c31\u662fTickType_t
    • TickType_t\u53ef\u4ee5\u662f16\u4f4d\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f32\u4f4d\u7684
    • FreeRTOSConfig.h\u4e2d\u5b9a\u4e49configUSE_16_BIT_TICKS\u65f6\uff0cTickType_t\u5c31\u662fuint16_t
    • \u5426\u5219TickType_t\u5c31\u662fuint32_t
    • \u5bf9\u4e8e32\u4f4d\u67b6\u6784\uff0c\u5efa\u8bae\u628aTickType_t\u914d\u7f6e\u4e3auint32_t
  • BaseType_t\uff1a
    • \u8fd9\u662f\u8be5\u67b6\u6784\u6700\u9ad8\u6548\u7684\u6570\u636e\u7c7b\u578b
    • 32\u4f4d\u67b6\u6784\u4e2d\uff0c\u5b83\u5c31\u662fuint32_t
    • 16\u4f4d\u67b6\u6784\u4e2d\uff0c\u5b83\u5c31\u662fuint16_t
    • 8\u4f4d\u67b6\u6784\u4e2d\uff0c\u5b83\u5c31\u662fuint8_t
    • BaseType_t\u901a\u5e38\u7528\u4f5c\u7b80\u5355\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u8fd8\u6709\u903b\u8f91\u503c\uff0c\u6bd4\u5982pdTRUE/pdFALSE

\u547d\u540d\u89c4\u8303\uff1a

\u53d8\u91cf\u540d\u524d\u7f00 \u542b\u4e49 ul uint32_t us uint16_t uc uint8_t x \u975estdint\u7c7b\u578b\uff0c\u6bd4\u5982BaseType_t\u548cTickType_t\uff0c\u6216\u8005size_t ux UBaseType_t e enum p \u6307\u9488 c char \u51fd\u6570\u540d\u524d\u7f00 \u542b\u4e49 prv \u79c1\u6709\u51fd\u6570(file scope) v \u8fd4\u56de\u503cvoid task \u5b9a\u4e49\u5728task.c \u5b8f\u524d\u7f00 \u542b\u4e49 config \u5b9a\u4e49\u5728FreeRTOSConfig.h"},{"location":"rtos/freertos/#_2","title":"\u5185\u5b58\u7ba1\u7406","text":"

\u6bcf\u6b21\u521b\u5efa\u4efb\u52a1\u3001\u961f\u5217\u3001\u4e92\u65a5\u9501\u3001\u8f6f\u4ef6\u5b9a\u65f6\u5668\u3001\u4fe1\u53f7\u91cf\u6216\u4e8b\u4ef6\u7ec4\u65f6\uff0c\u5185\u6838\u90fd\u9700\u8981RAM\uff0cRAM\u53ef\u4ee5\u7531RTOS\u5806\u52a8\u6001\u5206\u914d\uff0c\u4e5f\u53ef\u4ee5\u7531\u7f16\u5199\u8005\u624b\u52a8\u63d0\u4f9b\u3002

\u5c3d\u7ba1C\u5e93\u63d0\u4f9b\u4e86malloc()\u548cfree()\u51fd\u6570\uff0c\u4f46\u662f\uff1a 1. \u5b83\u4eec\u5e76\u4e0d\u603b\u662f\u9002\u7528\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf 2. \u5b83\u4eec\u8017\u8d39\u65f6\u95f4\u6bd4\u8f83\u957f 3. \u5b83\u4eec\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684 4. \u8fd0\u884c\u7ed3\u679c\u4e0d\u786e\u5b9a

\u56e0\u6b64freertos\u5b9e\u73b0\u4e86\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u63a5\u53e3\u51fd\u6570\u3002

freertos\u4e2d\u6709\u4e94\u79cd\u5806\u5185\u5b58\uff0c\u6587\u4ef6\u5b9a\u4e49\u5728Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang\u4e0b\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fheap_4.c\uff0c\u5b83\u53ef\u4ee5\u5c06\u76f8\u90bb\u7684\u7a7a\u95f4\u5185\u5b58\u5757\u5408\u5e76\uff0c\u89e3\u51b3\u4e86\u5185\u5b58\u788e\u7247\u95ee\u9898\u3002\u800cheap_5.c\u5141\u8bb8\u591a\u4e2a\u4e0d\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\u3002\u9700\u8981\u7528\u5230\u4ee5\u4e0b\u7ed3\u6784\u4f53\u8fdb\u884c\u4e0d\u540c\u5185\u5b58\u5757\u7684\u521d\u59cb\u5316\uff1a

typedef struct HeapRegion\n{\n    uint8_t *pucStartAddress;   //\u8d77\u59cb\u5730\u5740\n    size_t xSizeInBytes;       //\u5927\u5c0f\n}HeapRegion_t;\n

\u5f53\u6307\u5b9a\u591a\u5757\u5185\u5b58\u65f6\uff0c\u9700\u8981\u7528\u5230HeapRegion_t\u7c7b\u578b\u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u90fd\u662f\u4e00\u4e2aHeapRegion_t\u5143\u7d20\u3002\u8fd9\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4f4e\u5730\u5740\u5728\u524d\uff0c\u9ad8\u5730\u5740\u5728\u540e\u3002

HeapRegion_t xHeapRegions[] = \n{\n    {(uint8_t *)0x80000000UL, 0x10000}, //\u8d77\u59cb\u5730\u5740\u548c\u5927\u5c0f\n    {(uint8_t *)0x90000000UL, 0x10000}, \n    {NULL, 0}  //\u8868\u793a\u6570\u7ec4\u7ed3\u675f\n};\n

\u5b9a\u4e49\u4e86\u5185\u5b58\u5757\u6570\u7ec4\u8fd8\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff1a

void vPortDefineHeapRegions(const HeapRegion_t *const pxHeapRegions);\n

Heap\u7684\u5206\u914d\u548c\u91ca\u653e\uff1a

void *pvPortMalloc(size_t xWantedSize);\nvoid vPortFree(void *pv);\n

\u83b7\u53d6\u5f53\u524d\u7a7a\u95f2\u5185\u5b58\u7a7a\u95f4\uff1a

size_t xPortGetFreeHeapSize(void);\n

\u83b7\u53d6\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7a7a\u95f2\u5185\u5b58\u7684\u6700\u5c0f\u503c\uff1a

size_t xPortGetMinimumEverFreeHeapSize(void);\n
"},{"location":"rtos/freertos/#_3","title":"\u4efb\u52a1\u7ba1\u7406","text":"

\u4e00\u4e2a\u4efb\u52a1\u6700\u57fa\u672c\u7684\u5143\u7d20\u6709\uff1a

  • \u4efb\u52a1\u72b6\u6001\uff1a\u6bd4\u5982\u963b\u585e\uff0c\u5c31\u7eea\uff0c\u6302\u8d77\u7b49
  • \u4f18\u5148\u7ea7\uff1a\u6bcf\u4e2a\u4efb\u52a1\u90fd\u5206\u914d\u4e86\u4ece0\u5230configMAX_PRIORITIES-1\u7684\u4f18\u5148\u7ea7
  • \u6808\uff1a\u7528\u6765\u4fdd\u5b58\u4efb\u52a1\u7684\u5c40\u90e8\u6570\u636e
  • \u4e8b\u4ef6\uff1a\u8868\u793a\u4efb\u52a1\u505a\u4e86\u4ec0\u4e48\u4e8b\u60c5

\u5bf9\u4e8e\u5355\u6838\u5904\u7406\u5668\u6765\u8bf4\uff0c\u4efb\u4f55\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u4efb\u52a1\u5f97\u5230\u8fd0\u884c\uff0c\u5f53\u8c03\u5ea6\u5668\u51b3\u5b9a\u5207\u6362\u4efb\u52a1\u65f6\uff0c\u9700\u8981\u5c06\u4e0a\u4e00\u4e2a\u4efb\u52a1\u7684\u4e0a\u4e0b\u6587\uff08\u5bc4\u5b58\u5668\uff0c\u6808\uff09\u4fdd\u5b58\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u51fa\u73b0\u4e86\u6b7b\u5faa\u73af\u64cd\u4f5c\uff0c\u90a3\u4e48\u6bd4\u8fd9\u4e2a\u4efb\u52a1\u4f18\u5148\u7ea7\u4f4e\u7684\u4efb\u52a1\u90fd\u5c06\u65e0\u6cd5\u6267\u884c\u3002\u6240\u4ee5\u5728\u8bbe\u8ba1\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u4fdd\u8bc1\u4efb\u52a1\u5728\u4e0d\u6d3b\u8dc3\u72b6\u6001\u65f6\u8fdb\u5165\u963b\u585e\u72b6\u6001\u4ee5\u8ba9\u51faCPU\u4f7f\u7528\u6743\uff0c\u4fdd\u8bc1\u5176\u4ed6\u4f4e\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u53ef\u4ee5\u6267\u884c\u3002

"},{"location":"rtos/freertos/#_4","title":"\u521b\u5efa\u4efb\u52a1","text":"

\u5728FreeRTOS\u4e2d\uff0c\u4efb\u52a1\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

void vATaskFunction(void *pvParameters)\n{\n    for(;;)\n    {\n        //RTOS\u63a8\u8350\u91c7\u7528\u4e8b\u4ef6\u9a71\u52a8\u578b\u4efb\u52a1\n        if(WaitForEvent(EventObject, Timeout) == pdPASS)\n        {\n            //\u5904\u7406\u4e8b\u4ef6\n        }\n        else\n        {\n\n        }\n    }\n\n    vTaskDelete(NULL);  //\u6267\u884c\u5b8c\u6bd5\u5220\u9664\u81ea\u5df1\n}\n
\u52a8\u6001\u521b\u5efa\u4efb\u52a1\u4f7f\u7528\u7684\u51fd\u6570\u5982\u4e0b\uff1a

BaseType_t xTaskCreate(TaskFunction_t pxTaskCode,\n                        const char *const pcName, \n                        const configSTACK_DEPTH_TYPE usStackDepth,\n                        void *const pvParameters,\n                        UBaseType_t uxPriority,\n                        TaskHandle_t *const pxCreatedTask);\n

\u53c2\u6570\u8bf4\u660e\uff1a

\u53c2\u6570 \u63cf\u8ff0 pvTaskCode \u4efb\u52a1\u51fd\u6570 pcName \u4efb\u52a1\u540d\u79f0 usStackDepth \u4efb\u52a1\u6808\u5927\u5c0f\uff0c\u5355\u4f4dword pvParameters \u4f20\u9012\u7ed9\u4efb\u52a1\u51fd\u6570\u7684\u53c2\u6570 uxPriority \u4efb\u52a1\u4f18\u5148\u7ea7 pxCreatedTask \u4efb\u52a1\u53e5\u67c4\uff0c\u7528\u6765\u64cd\u4f5c\u4efb\u52a1\u51fd\u6570 \u8fd4\u56de\u503c \u6210\u529f\u8fd4\u56depdPass\uff0c\u5931\u8d25\u901a\u5e38\u662f\u56e0\u4e3a\u5185\u5b58\u4e0d\u8db3

\u9759\u6001\u521b\u5efa\u4efb\u52a1\u4f7f\u7528\u7684\u51fd\u6570\u5982\u4e0b\uff1a

TaskHandle_t xTaskCreateStatic(TaskFunction_t pxTaskCode,\n                                const char *const pcName,\n                                const uint32_t ulStackDepth,\n                                void *const pvParameters,\n                                UBaseType_t uxPriority,\n                                StackType_t *const puxStackBuffer,\n                                StaticTask_t *const pxTaskBuffer);\n

\u76f8\u5bf9\u4e8e\u52a8\u6001\u5206\u914d\uff0c\u6709\u4e24\u4e2a\u53c2\u6570\u4e0d\u540c\uff1a

\u53c2\u6570 \u63cf\u8ff0 puxStackBuffer \u6808\u6570\u7ec4\uff0c\u7d22\u5f15\u5fc5\u987b\u4e0d\u5c0f\u4e8eulStackDepth pxTaskBuffer \u6307\u5411StaticTask_t\u7684\u6307\u9488\uff0c\u7528\u6765\u4fdd\u5b58\u4efb\u52a1\u7ed3\u6784\u4f53

\u6bcf\u4e2a\u4efb\u52a1\u90fd\u9700\u8981RAM\u6765\u4fdd\u5b58\u4efb\u52a1\u72b6\u6001\uff0c\u5e76\u7531\u4efb\u52a1\u7528\u4f5c\u5176\u5806\u6808\u3002\u5982\u679c\u4f7f\u7528\u52a8\u6001\u521b\u5efa\uff0c\u5219\u4f1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4eceFreeRTOS\u5806\u4e2d\u81ea\u52a8\u5206\u914dRAM\u3002\u5982\u679c\u662f\u9759\u6001\u521b\u5efa\uff0cRAM\u7531\u7f16\u5199\u8005\u624b\u52a8\u63d0\u4f9b\uff0c\u4f46\u5728\u7f16\u8bd1\u65f6\u5c31\u5df2\u786e\u5b9a\u3002

"},{"location":"rtos/freertos/#tick","title":"Tick","text":"

\u4efb\u52a1\u8fdb\u5165\u7761\u7720\u540e\u9700\u8981\u6307\u5b9a\u5524\u9192\u7684\u65f6\u95f4\uff0cFreeRTOS\u901a\u8fc7Tick\u53d8\u91cf\u6d4b\u91cf\u65f6\u95f4\u3002\u5b9a\u65f6\u5668\u4e2d\u65ad\u4ee5\u4e25\u683c\u7684\u65f6\u95f4\u7cbe\u5ea6\u589e\u52a0Tick count\u3002\u6bcf\u6b21Tick\u589e\u52a0\u65f6\uff0c\u5185\u6838\u5fc5\u987b\u68c0\u67e5\u73b0\u5728\u662f\u5426\u9700\u8981\u89e3\u9664\u963b\u585e\u6216\u8005\u5524\u9192\u4efb\u52a1\uff0c\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u5148\u88ab\u5524\u9192\u3002\u4efb\u52a1\u901a\u8fc7\u8c03\u7528vTaskDelay()\u51fd\u6570\u6765\u4e3b\u52a8\u7b49\u5f85\u4e00\u5b9a\u7684\u65f6\u95f4\u3002\u5b8fpdMS_TO_TICKS()\u5c06\u6beb\u79d2\u8f6c\u6362\u4e3aTick count\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5185\u6838\u672c\u8eab\u7684\u4efb\u52a1\u5207\u6362\u9700\u8981\u65f6\u95f4\uff0c\u5f53\u6307\u5b9aN\u4e2aTick\u7684\u5ef6\u8fdf\u540e\uff0c\u5b9e\u9645\u5ef6\u8fdf\u65f6\u95f4\u5c06\u5728(N-1)~(N)\u4e2aTick\u4e4b\u95f4\u3002

"},{"location":"rtos/freertos/#_5","title":"\u4efb\u52a1\u72b6\u6001","text":"

\u4efb\u52a1\u53ef\u4ee5\u6709\u4ee5\u4e0b\u72b6\u6001\uff1a

  • \u8fd0\u884c\uff1a\u6700\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u88ab\u6267\u884c

  • \u5c31\u7eea\uff1a\u80fd\u591f\u6267\u884c\uff0c\u7b49\u5f85\u8c03\u5ea6\u5668\u8c03\u5ea6

  • \u963b\u585e\uff1a\u6b63\u5728\u7b49\u5f85\u65f6\u95f4\u6216\u8005\u5916\u90e8\u4e8b\u4ef6

  • \u6302\u8d77\uff1a\u653e\u5f03CPU\u4f7f\u7528\u6743\uff0c\u4e0d\u53c2\u4e0e\u8c03\u5ea6\u3002

"},{"location":"rtos/freertos/#_6","title":"\u8c03\u5ea6\u7b97\u6cd5","text":"

\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a\u62a2\u5360\u548c\u8f6e\u8f6c\u3002\u901a\u8fc7\u914d\u7f6econfigUSE_PREEMPTION\u4f7f\u80fd\u62a2\u5360\uff0c\u5373\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u53ef\u4ee5\u62a2\u5360\u4f4e\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u914d\u7f6econfigUSE_TIME_SLICING\u4f7f\u80fd\u8f6e\u8f6c\uff0c\u5373\u76f8\u540c\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u8f6e\u6d41\u6267\u884c\u3002

"},{"location":"rtos/freertos/#_7","title":"\u7a7a\u95f2\u4efb\u52a1\u548c\u94a9\u5b50\u51fd\u6570","text":"

\u5728\u6ca1\u6709\u5176\u4ed6\u4efb\u52a1\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u6267\u884c\u7a7a\u95f2\u4efb\u52a1\u3002vTaskStartScheduler()\u51fd\u6570\u5185\u90e8\u4f1a\u521b\u5efa\u7a7a\u95f2\u4efb\u52a1\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u4e3a0\uff0c\u6c38\u8fdc\u4f4e\u4e8e\u7528\u6237\u4efb\u52a1\uff0c\u4e14\u8981\u4e48\u5904\u4e8e\u5c31\u7eea\u6001\uff0c\u8981\u4e48\u5904\u4e8e\u8fd0\u884c\u6001\u3002

\u6211\u4eec\u53ef\u4ee5\u5728\u7a7a\u95f2\u4efb\u52a1\u5185\u6dfb\u52a0\u4e00\u4e2a\u94a9\u5b50\u51fd\u6570\uff0c\u8fd9\u6837\u7a7a\u95f2\u4efb\u52a1\u6bcf\u6267\u884c\u4e00\u6b21\uff0c\u90fd\u4f1a\u8c03\u7528\u4e00\u6b21\u94a9\u5b50\u51fd\u6570\u3002\u94a9\u5b50\u51fd\u6570\u7684\u4f5c\u7528\u662f\uff1a

  • \u6267\u884c\u4e00\u4e9b\u540e\u53f0\u4efb\u52a1
  • \u6d4b\u91cf\u7cfb\u7edf\u7a7a\u95f2\u65f6\u95f4
  • \u4f7f\u7cfb\u7edf\u8fdb\u5165\u7701\u7535\u6a21\u5f0f

\u8981\u6ce8\u610f\u7684\u662f\uff0c\u94a9\u5b50\u51fd\u6570\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55\u4f7f\u5176\u5bfc\u81f4\u963b\u585e\u7684\u51fd\u6570\u3002\u7a7a\u95f2\u94a9\u5b50\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

void vApplicationIdleHook(void);\n
"},{"location":"rtos/freertos/#_8","title":"\u4efb\u52a1\u540c\u6b65\u4e0e\u901a\u4fe1","text":"

\u8fd9\u91cc\u9762\u7684\u5185\u5bb9\u4ee5\u7406\u89e3\u539f\u7406\u4e3a\u4e3b\uff0c\u6ca1\u6709\u5fc5\u8981\u53bb\u8bb0\u51fd\u6570\u63a5\u53e3\uff0c\u7528\u5230\u4ec0\u4e48\u5c31\u67e5\u4ec0\u4e48\u3002

"},{"location":"rtos/freertos/#_9","title":"\u6d88\u606f\u961f\u5217","text":"

\u6d88\u606f\u961f\u5217\u7b80\u79f0\u4e3a\u961f\u5217\uff0c\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u652f\u6301\u4efb\u52a1\u6216\u8005\u4e2d\u65ad\u7684\u8bfb\u5199\u3002\u5f53\u961f\u5217\u4e2d\u7684\u6d88\u606f\u4e3a\u7a7a\u65f6\uff0c\u8bfb\u53d6\u6d88\u606f\u7684\u4efb\u52a1\u5c06\u88ab\u963b\u585e\uff1b\u5f53\u961f\u5217\u5df2\u6ee1\uff0c\u53d1\u9001\u6d88\u606f\u7684\u4efb\u52a1\u5c06\u88ab\u963b\u585e\u3002\u53ef\u4ee5\u5c06\u4e00\u6761\u6216\u8005\u591a\u6761\u6d88\u606f\u653e\u5165\u961f\u5217\u4e2d\uff0c\u591a\u4e2a\u4efb\u52a1\u4e5f\u53ef\u4ee5\u4ece\u540c\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u8bfb\u5199\u6d88\u606f\u3002\u5982\u679c\u662f\u7d27\u6025\u6d88\u606f\uff0c\u53ef\u4ee5\u53d1\u9001\u81f3\u961f\u5217\u5934\u4ece\u800c\u63d0\u524d\u88ab\u63a5\u6536\u3002

\u4e3a\u4e86\u5b9e\u73b0\u5148\u8fdb\u5148\u51fa\u7684\u673a\u5236\uff0c\u961f\u5217\u4f7f\u7528\u4e86\u963b\u585e\u7684\u673a\u5236\u3002\u5f53\u4efb\u52a1A\u5bf9\u67d0\u4e2a\u961f\u5217\u8fdb\u884c\u8bfb\u64cd\u4f5c\u65f6\uff0c\u5982\u679c\u961f\u5217\u6b64\u65f6\u4e3a\u7a7a\uff0c\u90a3\u4e48\u5b83\u53ef\u4ee5\u9009\u62e9\uff1a

  1. \u76f4\u63a5\u653e\u5f03\u8bfb\u64cd\u4f5c\uff0c\u53bb\u5e72\u522b\u7684\u4e8b\u60c5\u3002
  2. \u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u3002\u5982\u679c\u5728\u7b49\u5f85\u671f\u95f4\u6d88\u606f\u6765\u4e86\uff0c\u5c31\u53d8\u4e3a\u5c31\u7eea\u72b6\u6001\uff1b\u5982\u679c\u7b49\u5f85\u65f6\u95f4\u8fc7\u4e86\u6d88\u606f\u90fd\u6ca1\u6765\uff0c\u90a3\u4e48\u5c31\u8fd4\u56de\u4e00\u4e2a\u672a\u6536\u5230\u6d88\u606f\u7684\u9519\u8bef\u7801\uff0c\u7136\u540e\u6267\u884c\u5176\u4ed6\u7a0b\u5e8f\u3002
  3. \u6b7b\u7b49\u76f4\u5230\u6d88\u606f\u5230\u6765\u4e3a\u6b62\u3002

\u5199\u64cd\u4f5c\u7684\u963b\u585e\u673a\u5236\u7c7b\u4f3c\u3002

\u6ce8\u610f\uff1a\u7531\u4e8e\u4e2d\u65ad\u51fd\u6570\u4e5f\u53ef\u4ee5\u5bf9\u961f\u5217\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u4f46\u662f\u4e2d\u65ad\u51fd\u6570\u4e0d\u5141\u8bb8\u963b\u585e\uff0c\u56e0\u6b64\u5728\u4e2d\u65ad\u5904\u7406\u6d41\u7a0b\u4e2d\u5fc5\u987b\u4f7f\u7528\u7279\u6b8a\u7684\u8bfb\u5199\u51fd\u6570\u63a5\u53e3\u3002

"},{"location":"rtos/freertos/#_10","title":"\u4fe1\u53f7\u91cf","text":"

\u4fe1\u53f7\u91cf\u5206\u4e3a\u4e8c\u503c\u4fe1\u53f7\u91cf\u548c\u8ba1\u6570\u4fe1\u53f7\u91cf\uff0c\u5176\u539f\u7406\u548cLinux\u5185\u6838\u76f8\u4f3c\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002

"},{"location":"rtos/freertos/#_11","title":"\u4e92\u65a5\u91cf","text":"

\u4e92\u65a5\u91cf\u662f\u7279\u6b8a\u7684\u4e8c\u503c\u4fe1\u53f7\u91cf\uff0c\u6709\u7740\u9012\u5f52\u8bbf\u95ee\u548c\u9632\u6b62\u4f18\u5148\u7ea7\u53cd\u8f6c\u7684\u7279\u6027\uff0c\u4e3b\u8981\u7528\u4e8e\u4e34\u754c\u8d44\u6e90\u7684\u4fdd\u62a4\u3002\u4e92\u65a5\u91cf\u5177\u6709\u7279\u6b8a\u7684\u4f18\u5148\u7ea7\u7ee7\u627f\u7684\u673a\u5236\uff0c\u8981\u7406\u89e3\u8fd9\u4e2a\u673a\u5236\uff0c\u6211\u4eec\u9996\u5148\u8981\u89e3\u91ca\u4e0b\u4ec0\u4e48\u662f\u4f18\u5148\u7ea7\u53cd\u8f6c\u3002

\u5728\u4e00\u4e2a\u7b80\u5316\u8fc7\u7684\u573a\u666f\u4e2d\uff0c\u6709\u4e09\u4e2a\u4efb\u52a1H\u3001M\u3001L\uff0c\u5b83\u4eec\u7684\u4f18\u5148\u7ea7\u4f9d\u6b21\u4e3aH>M>L\u3002H\u4e0eL\u5171\u4eab\u67d0\u4e2a\u8d44\u6e90\uff0cM\u4e0d\u5171\u4eab\u3002\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u6700\u9ad8\u4f18\u5148\u7ea7\u7684H\u5e94\u8be5\u80fd\u591f\u4f18\u5148\u6267\u884c\uff0c\u5e76\u4e14\u6709\u6743\u9650\u6253\u65adM\u6216\u8005L\u7684\u6267\u884c\u72b6\u6001\u4ece\u800c\u62a2\u5360CPU\u7684\u63a7\u5236\u6743\u3002\u7136\u800c\u5b58\u5728\u4e00\u79cd\u60c5\u51b5,\u4f1a\u5bfc\u81f4H\u4e00\u76f4\u88ab\u963b\u585e\uff0c\u7b49\u5f85M\u548cL\u7684\u5b8c\u6210\u8fd0\u884c\uff1a

\u5728\u67d0\u4e00\u65f6\u523b\uff0cL\u5df2\u7ecf\u83b7\u53d6\u4e86\u5171\u4eab\u8d44\u6e90\u7684\u4f7f\u7528\u6743\uff0c\u6b63\u5728\u6267\u884c\u81ea\u5df1\u7684\u4efb\u52a1\u3002\u5982\u679c\u6b64\u65f6H\u88ab\u5524\u9192\uff0c\u7531\u4e8eH\u7684\u4f18\u5148\u7ea7\u5927\u4e8eL\uff0c\u5b83\u5c1d\u8bd5\u83b7\u53d6CPU\u7684\u63a7\u5236\u6743\u3002\u4f46\u662f\u7531\u4e8e\u5171\u4eab\u8d44\u6e90\u88abL\u5360\u7528\uff0cH\u4e0d\u5f97\u4e0d\u7b49\u5f85L\u91ca\u653e\u8d44\u6e90\u800c\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u6709\u8da3\u7684\u662f\uff0c\u5982\u679cM\u88ab\u5524\u9192\uff0c\u7531\u4e8eM\u4e0eL\u5e76\u4e0d\u5171\u4eab\u8d44\u6e90\u4e14\u4f18\u5148\u7ea7\u5927\u4e8eL\uff0c\u5b83\u5c06\u7acb\u523b\u4e2d\u65adL\u7684\u6267\u884c\uff0c\u83b7\u53d6CPU\u7684\u63a7\u5236\u6743\u3002\u53ea\u6709\u5f53M\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u624d\u4f1a\u5c06CPU\u63a7\u5236\u6743\u5f52\u8fd8\u7ed9L\uff0c\u4f7f\u5176\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5269\u4f59\u7684\u4ee3\u7801\u3002\u4e00\u65e6L\u6267\u884c\u5b8c\u6bd5\u540e\u91ca\u653e\u4e86\u5171\u4eab\u8d44\u6e90\uff0cH\u4efb\u52a1\u624d\u80fd\u4ece\u963b\u585e\u72b6\u6001\u4e2d\u88ab\u5524\u9192\u3002\u6211\u4eec\u53d1\u73b0\uff0c\u6700\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1H\u53cd\u800c\u9700\u8981\u7b49\u5f85\u4e24\u4e2a\u4f4e\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u5148\u6267\u884c\u5b8c\u6bd5\uff0c\u5176\u963b\u585e\u65f6\u95f4\u7b49\u4e8eM\u548cL\u7684\u6267\u884c\u65f6\u95f4\u4e4b\u548c\u3002\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u201c\u4f18\u5148\u7ea7\u53cd\u8f6c\u201d\uff0c\u5728\u7cfb\u7edf\u4e2d\u53ef\u80fd\u5bfc\u81f4\u4e25\u91cd\u540e\u679c\uff0c\u751a\u81f3\u5d29\u6e83\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u8f86\u6c7d\u8f66\u7684\u5239\u8f66\u7cfb\u7edf\u662f\u6700\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\uff0c\u4f46\u9700\u8981\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u8fd9\u5c06\u5bfc\u81f4\u707e\u96be\u6027\u7684\u540e\u679c\u3002\u56e0\u6b64\u4e3a\u4e86\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u8fd0\u884c\uff0c\u5fc5\u987b\u89e3\u51b3\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898\u3002

\u4f46\u662f\u5047\u5982\u6211\u4eec\u5f15\u8fdb\u4e86\u4f18\u5148\u7ea7\u7ee7\u627f\u673a\u5236\u3002\u540c\u6837\u662fL\u83b7\u53d6\u5171\u4eab\u8d44\u6e90\uff0cH\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u7cfb\u7edf\u4e34\u65f6\u5c06L\u7684\u4f18\u5148\u7ea7\u63d0\u5347\u5230H\uff0c\u90a3\u4e48M\u5c31\u65e0\u6cd5\u6253\u65adL\u7684\u6267\u884c\u3002\u6240\u4ee5L\u6267\u884c\u5b8c\u5b83\u7684\u4efb\u52a1\u5c31\u4f1a\u7acb\u523b\u91ca\u653e\u8d44\u6e90\uff0c\u7136\u540e\u7531H\u6765\u62a2\u5360CPU\u7684\u4f7f\u7528\u6743\u3002\u5176\u963b\u585e\u65f6\u95f4\u4ec5\u4ec5\u662fL\u7684\u6267\u884c\u65f6\u95f4\uff0c\u8fd9\u5c06\u5927\u5927\u964d\u4f4e\u4f18\u5148\u7ea7\u53cd\u8f6c\u7684\u5371\u5bb3\u3002

"},{"location":"rtos/freertos/#_12","title":"\u4e8b\u4ef6\u7ec4","text":"

\u4e8b\u4ef6\u7ec4\u4e3b\u8981\u7528\u6765\u5b9e\u73b0\u591a\u4efb\u52a1\u95f4\u7684\u540c\u6b65\uff0c\u53ea\u80fd\u7528\u6765\u4e8b\u4ef6\u7684\u901a\u4fe1\uff0c\u4e0d\u80fd\u7528\u6765\u53d1\u9001\u6570\u636e\u3002\u53ef\u4ee5\u662f\u4e00\u5bf9\u591a\uff0c\u4e5f\u53ef\u4ee5\u662f\u591a\u5bf9\u4e00\u3002

\u4e8b\u4ef6\u7ec4\u7528\u4e00\u4e2aEventBits_t\u7c7b\u578b\u7684\u53d8\u91cf\u6765\u8868\u793a\uff0c\u5176\u4e2d\u9ad88\u4f4d\u7559\u7ed9\u5185\u6838\uff0c\u7528\u5176\u4ed6\u7684\u4f4d\u6765\u8868\u793a\u4e8b\u4ef6\u3002\u6bcf\u4e00\u4f4d\u90fd\u4ee3\u8868\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u542b\u4e49\u7531\u7f16\u5199\u8005\u51b3\u5b9a\uff0c\u6bd4\u5982\u7528bit0\u8868\u793a\u706f\u6ce1\u4eae\u706d\uff0cbit1\u8868\u793a\u6309\u952e\u662f\u5426\u6309\u4e0b\u3002\u8fd9\u4e9b\u4f4d\uff0c\u503c\u4e3a1\u8868\u793a\u4e8b\u4ef6\u53d1\u751f\uff0c\u503c\u4e3a0\u8868\u793a\u4e8b\u4ef6\u672a\u53d1\u751f\u3002\u4efb\u52a1\u901a\u8fc7\u201c\u903b\u8f91\u4e0e\u201d\u3001\u201c\u903b\u8f91\u6216\u201d\u4e0e\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u4e8b\u4ef6\u5efa\u7acb\u5173\u8054\u3002\u903b\u8f91\u4e0e\u8868\u793a\u4efb\u52a1\u611f\u5174\u8da3\u7684\u591a\u4e2a\u4e8b\u4ef6\u540c\u65f6\u53d1\u751f\u624d\u88ab\u5524\u9192\uff0c\u903b\u8f91\u6216\u8868\u793a\u4efb\u4e00\u4e8b\u4ef6\u53d1\u751f\u5373\u53ef\u88ab\u5524\u9192\u3002

\u4e8b\u4ef6\u7ec4\u7684\u64cd\u4f5c\u6d41\u7a0b\u5982\u4e0b\uff1a

  • \u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u7ec4
  • \u4efb\u52a1A\u3001B\u63a7\u5236\u4e8b\u4ef6\u4e2d\u7684\u67d0\u4e9b\u4f4d
  • \u4efb\u52a1C\u3001D\u7b49\u5f85\u4e8b\u4ef6\u4e2d\u7684\u4f4d\uff0c\u5e76\u4e14\u51b3\u5b9a\u53d1\u751f\u4e8b\u4ef6\u540e\u7684\u64cd\u4f5c

\u8fd9\u91cc\u5bf9\u4e24\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u51fd\u6570\u8bf4\u660e\u4e00\u4e0b\uff1a

\u7b49\u5f85\u4e8b\u4ef6\u51fd\u6570\uff1a

EventBits_t xEventGroupWaitBits(EventGroupsHandle_t xEventGroup,\n                                const EvetnBits_t uxBitsToWaitFor,\n                                const BaseType_t xClearOnExit,\n                                const BaseType_t xWaitForAllBits,\n                                TickType_t xTickToWait);\n
\u53c2\u6570 \u8bf4\u660e xEventGroup \u7b49\u5f85\u7684\u4e8b\u4ef6\u7ec4 uxBitsToWaitFor \u7b49\u5f85\u7684\u4f4d xClearOnExit pdTRUE:\u6e05\u9664\u4f4d xWaitForAllBits pdTRUE\uff1a\u7b49\u5f85\u7684\u4f4d\u5168\u90e8\u4e3a1\uff1bpdFALSE\uff1a\u67d0\u4e00\u4f4d\u4e3a1 xTicksToWait \u963b\u585e\u65f6\u95f4

\u4e3e\u4f8b\u8bf4\u660e\uff1a

\u4e8b\u4ef6\u7ec4\u7684\u503c uxBitsToWaitFor xWaitForAllBits \u8bf4\u660e 0100 0101 pdTrue \u671f\u671bbit0,bit2\u90fd\u4e3a1\uff0c\u4e0d\u6ee1\u8db3\u8fdb\u5165\u963b\u585e 0100 0110 pdFALSE \u671f\u671bbit0, bit2\u67d0\u4f4d\u4e3a1\uff0c\u6ee1\u8db3\u9000\u51fa

\u540c\u6b65\u4e8b\u4ef6\u51fd\u6570\uff1a

EventBits_t xEventGroupSync(EventGroupHandle_t xEventGroup,\n                            const EventBits_t uxBitsToSet,\n                            const EventBits_t uxBitsToWaitFor,\n                            TickType_t xTicksToWait);\n

\u8be5\u51fd\u6570\u7528\u6765\u534f\u540c\u591a\u4e2a\u4efb\u52a1\uff0c\u5f53\u671f\u671b\u7684\u4e8b\u4ef6\u4e00\u8d77\u53d1\u751f\u540e\uff0c\u624d\u4f1a\u6210\u529f\u8fd4\u56de\u3002

"},{"location":"rtos/freertos/#_13","title":"\u4efb\u52a1\u901a\u77e5","text":"

FreeRTOS\u4eceV8.2.0\u7248\u672c\u5f00\u59cb\u63d0\u4f9b\u4efb\u52a1\u901a\u77e5\u8fd9\u4e2a\u529f\u80fd\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4efb\u52a1\u901a\u77e5\u53ef\u4ee5\u66ff\u4ee3\u4fe1\u53f7\u91cf\u3001\u4e8b\u4ef6\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u66ff\u4ee3\u957f\u5ea6\u4e3a1\u7684\u961f\u5217\u3002\u4efb\u52a1\u901a\u77e5\u66f4\u52a0\u7075\u6d3b\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\uff0c\u5e76\u4e14\u66f4\u52a0\u8282\u7701RAM\u7a7a\u95f4\uff0c\u7f3a\u70b9\u662f\u4efb\u52a1\u901a\u77e5\u53ea\u80fd\u7531\u4e00\u4e2a\u4efb\u52a1\u6765\u63a5\u6536\u901a\u77e5\u6d88\u606f\u3002

FreeRTOS\u63d0\u4f9b\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u53d1\u9001\u901a\u77e5\u7ed9\u4efb\u52a1\uff1a

  • \u5982\u679c\u6709\u901a\u77e5\u672a\u8bfb\uff0c\u4e0d\u8986\u76d6\u901a\u77e5\u503c
  • \u76f4\u63a5\u8986\u76d6\u901a\u77e5\u503c
  • \u8bbe\u7f6e\u901a\u77e5\u503c\u7684\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u4f4d\uff0c\u7c7b\u4f3c\u4e8b\u4ef6\u7ec4
  • \u9012\u589e\u901a\u77e5\u503c\uff0c\u7c7b\u4f3c\u8ba1\u6570\u4fe1\u53f7\u91cf

\u4efb\u52a1\u901a\u77e5\u662f\u4efb\u52a1\u63a7\u5236\u5757\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u6b64\u4efb\u52a1\u5728\u88ab\u521b\u5efa\u7684\u65f6\u5019\uff0c\u4efb\u52a1\u901a\u77e5\u5c31\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u4e86\u3002\u4efb\u52a1\u901a\u77e5\u53ef\u4ee5\u7531\u4efb\u52a1\u53d1\u9001\uff0c\u4e5f\u53ef\u4ee5\u7531\u4e2d\u65ad\u53d1\u9001\u3002\u53ea\u6709\u5728\u4efb\u52a1\u4e2d\u624d\u53ef\u4ee5\u7b49\u5f85\u901a\u77e5\uff0c\u800c\u4e0d\u5141\u8bb8\u5728\u4e2d\u65ad\u4e2d\u7b49\u5f85\u901a\u77e5\u3002\u5982\u679c\u4efb\u52a1\u5728\u7b49\u5f85\u901a\u77e5\u7684\u65f6\u5019\u6682\u65f6\u65e0\u6548\uff0c\u5219\u4efb\u52a1\u4f1a\u6839\u636e\u7528\u6237\u6307\u5b9a\u7684\u963b\u585e\u65f6\u95f4\u5185\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u53ea\u8981\u6709\u4efb\u52a1\u6216\u8005\u4e2d\u65ad\u53d1\u9001\u4efb\u52a1\u901a\u77e5\uff0c\u4efb\u52a1\u5c31\u4f1a\u4ece\u963b\u585e\u72b6\u6001\u89e3\u9664\u3002

\u6bcf\u4e2a\u4efb\u52a1\u90fd\u6709\u4e00\u4e2a\u7ed3\u6784\u4f53:TCB\uff08Task Control Block\uff09\uff0c\u91cc\u9762\u6709\u4e24\u4e2a\u6210\u5458\uff0c\u4e00\u4e2a\u7528\u6765\u8868\u793a\u901a\u77e5\u72b6\u6001\uff0c\u4e00\u4e2a\u7528\u6765\u8868\u793a\u901a\u77e5\u503c\u3002

typedef struct tskTaskControlBlock\n{\n    ......\n    /* configTASK_NOTIFICATION_ARRAY_ENTRIES = 1 */\n    volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];\n    volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];\n    ......\n} tskTCB;\n

\u901a\u77e5\u72b6\u6001\u67093\u79cd\u53d6\u503c\uff1a

  • taskNOT_WAITING_NOTIFICATION\uff1a\u4efb\u52a1\u6ca1\u6709\u5728\u7b49\u5f85\u901a\u77e5
  • taskWAITING_NOTIFICATION\uff1a\u4efb\u52a1\u5728\u7b49\u5f85\u901a\u77e5
  • taskNOTIFICATION_RECEIVED\uff1a\u4efb\u52a1\u63a5\u6536\u5230\u4e86\u901a\u77e5
"},{"location":"rtos/freertos/#_14","title":"\u8f6f\u4ef6\u5b9a\u65f6\u5668","text":"

\u8f6f\u4ef6\u5b9a\u65f6\u5668\u5c31\u662f\u201c\u95f9\u949f\u201d\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u95f9\u949f\uff1a\u65e9\u4e0a7\u70b9\u53eb\u9192\u4f60\uff1b\u6216\u8005\uff0c\u6bcf\u5de5\u4f5c\u4e00\u5c0f\u65f6\u5c31\u4f11\u606f\u4e00\u4f1a\u3002\u8f6f\u4ef6\u5b9a\u65f6\u5668\u7684\u529f\u80fd\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u662f\u5728\u672a\u6765\u67d0\u4e2a\u65f6\u95f4\u70b9\u89e6\u53d1\u67d0\u4e2a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u5468\u671f\u6027\u5730\u8fd0\u884c\u51fd\u6570\u3002

\u8f6f\u4ef6\u5b9a\u65f6\u5668\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4e00\u7c7b\u63a5\u53e3\uff0c\u5b83\u6784\u5efa\u5728\u786c\u4ef6\u5b9a\u65f6\u5668\u57fa\u7840\u4e4b\u4e0a\uff0c\u4ee5\u7cfb\u7edf\u8282\u62cd\u5468\u671f\u4e3a\u5355\u4f4d\u3002\u7cfb\u7edf\u8282\u62cd\u5c31\u662f\u7cfb\u7edf\u65f6\u949f\u7684\u9891\u7387\uff0c\u914d\u7f6e\u4e3aconfigTICK_RATE_HZ\uff0c\u9ed8\u8ba4\u4e3a1000\u3002\u4e5f\u5c31\u662f\u8bf41s\u8df3\u52a81000\u6b21\uff0c\u90a3\u4e48\u7cfb\u7edf\u8857\u62cd\u5468\u671f\u5c31\u662f1ms\u3002\u8f6f\u4ef6\u5b9a\u65f6\u5668\u7684\u5b9a\u65f6\u6570\u503c\u5fc5\u987b\u662f\u8fd9\u4e2a\u8282\u62cd\u5468\u671f\u7684\u6574\u6570\u500d\u3002\u5f53\u7136\uff0c\u7cfb\u7edf\u8282\u62cd\u7cbe\u5ea6\u8d8a\u9ad8\uff0c\u8f6f\u4ef6\u5b9a\u65f6\u5668\u6240\u80fd\u8bbe\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5c31\u8d8a\u5c0f\uff0c\u4f46\u662f\u7cfb\u7edf\u7684\u5f00\u9500\u4e5f\u4f1a\u53d8\u5927\u3002

FreeRTOS\u901a\u8fc7\u4e00\u4e2aprvTimerTask\u7684\u5b88\u62a4\u4efb\u52a1\u6765\u7ba1\u7406\u8f6f\u4ef6\u5b9a\u65f6\u5668\uff0c\u5b83\u662f\u5728\u542f\u52a8\u8c03\u5ea6\u5668\u65f6\u81ea\u52a8\u521b\u5efa\u7684\u3002\u5b88\u62a4\u4efb\u52a1\u4f1a\u5b9a\u671f\u68c0\u67e5\u7528\u6237\u7f16\u5199\u7684\u5b9a\u65f6\u5668\uff0c\u5e76\u8c03\u7528\u5176\u56de\u8c03\u51fd\u6570\u3002\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u4efb\u52a1\u51fd\u6570\u8981\u8c03\u7528\u5b9a\u65f6\u5668\u51fd\u6570\u65f6\uff0c\u662f\u901a\u8fc7\u201c\u5b9a\u65f6\u5668\u547d\u4ee4\u961f\u5217\u201d\u548c\u5b88\u62a4\u4efb\u52a1\u4ea4\u4e92\u7684\u3002\u5b88\u62a4\u4efb\u52a1\u7684\u4e3b\u8981\u5de5\u4f5c\u5c31\u662f\u4ece\u547d\u4ee4\u961f\u5217\u4e2d\u53d6\u51fa\u547d\u4ee4\u3001\u5904\u7406\uff0c\u6267\u884c\u5b9a\u65f6\u5668\u7684\u56de\u8c03\u51fd\u6570\u3002\u80fd\u5426\u53ca\u65f6\u5904\u7406\u5b9a\u65f6\u5668\u547d\u4ee4\uff0c\u53d6\u51b3\u4e8e\u5b88\u62a4\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u3002\u5176\u4f18\u5148\u7ea7\u9ed8\u8ba4\u4e3aconfiTIMER_TASK_PRIORITY\uff0c\u4e3a\u4e86\u66f4\u597d\u5730\u54cd\u5e94\uff0c\u8be5\u4f18\u5148\u7ea7\u5e94\u8bbe\u7f6e\u4e3a\u6240\u6709\u4efb\u52a1\u4e2d\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\u3002

\u5b9a\u65f6\u5668\u56de\u8c03\u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b\uff1a

void ATimerCallback(TimerHandler_t xTimer);\n

\u5b9a\u65f6\u5668\u7684\u56de\u8c03\u51fd\u6570\u662f\u5728\u5b88\u62a4\u4efb\u52a1\u4e2d\u88ab\u8c03\u7528\u7684\uff0c\u56e0\u6b64\u8be5\u51fd\u6570\u4e0d\u80fd\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u4e5f\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55\u4f1a\u5f15\u8d77\u963b\u585e\u7684\u51fd\u6570\u3002

\u5b9a\u65f6\u5668\u7684\u590d\u4f4d\uff1a

BaseType_t xTimerReset(TimerHandle_t xTimer, TickType_t xTicksToWait);\n

\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u8ba9\u5b9a\u65f6\u5668\u7684\u72b6\u6001\u4ece\u51ac\u7720\u6001\u53d8\u4e3a\u8fd0\u884c\u6001\uff0c\u5982\u679c\u5b9a\u65f6\u5668\u5df2\u7ecf\u8fd0\u884c\uff0c\u5219\u76f8\u5f53\u4e8e\u91cd\u65b0\u786e\u5b9a\u8d85\u65f6\u65f6\u95f4\u3002

\u4fee\u6539\u5468\u671f\uff1a

BaseType_t xTimerChangePeriod(TimerHandle_t xTimer, TickType_t xNewPeriod, TickType_t xTicksToWait);\n
"},{"location":"rtos/freertos/#_15","title":"\u4e2d\u65ad\u7ba1\u7406","text":"

\u4e2d\u65ad\u5c5e\u4e8e\u5f02\u6b65\u5f02\u5e38\uff0c\u6765\u6e90\u4e8e\u5916\u90e8\u786c\u4ef6\u3002\u5f53\u6ca1\u6709\u4e2d\u65ad\u53d1\u751f\u65f6\uff0cCPU\u6267\u884c\u81ea\u5df1\u7684\u4efb\u52a1\u3002\u5f53\u4e2d\u65ad\u4ea7\u751f\u65f6\uff0cCPU\u5f53\u524d\u4efb\u52a1\u88ab\u6253\u65ad\uff0c\u8f6c\u800c\u53bb\u6267\u884c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002\u5916\u90e8\u8bbe\u5907\u4ea7\u751f\u4e00\u4e2a\u4e2d\u65ad\u4fe1\u53f7\u5e76\u53d1\u9001\u81f3\u4e2d\u65ad\u63a7\u5236\u5668\uff0c\u7531\u4e2d\u65ad\u63a7\u5236\u5668\u5c06\u4fe1\u53f7\u518d\u8f6c\u53d1\u7ed9CPU\uff0cCPU\u54cd\u5e94\u4e2d\u65ad\u6e90\u7684\u8bf7\u6c42\u3002\u5f53\u4e2d\u65ad\u4ea7\u751f\u65f6\uff0c\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u4e0a\u4e0b\u6587\u72b6\u6001\u3002

\u4e2d\u65ad\u7684\u73af\u5883\u6709\u4e24\u79cd\uff1a

  • \u4efb\u52a1\u4e0a\u4e0b\u6587\uff1a\u65e0\u8bba\u4e2d\u65ad\u7684\u4f18\u5148\u7ea7\u662f\u591a\u5927\uff0c\u90fd\u4f1a\u6253\u65ad\u5f53\u524d\u4efb\u52a1\u7684\u6267\u884c\u3002
  • \u4e2d\u65ad\u4e0a\u4e0b\u6587\uff1a\u4ec5\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u53ef\u4ee5\u6253\u65ad\u5f53\u524d\u4e2d\u65ad\u7684\u6267\u884c\uff0c\u8fd9\u5fc5\u987b\u8981\u7cfb\u7edf\u652f\u6301\u4e2d\u65ad\u5d4c\u5957\u3002
"},{"location":"rtos/freertos/CMSIS/","title":"CMSIS\u6807\u51c6","text":"

Cortex\u7cfb\u5217\u7684\u82af\u7247\u5185\u6838\u662f\u76f8\u540c\u7684\uff0c\u4f46\u662f\u4e0d\u540c\u7684\u5382\u5546\u5728\u751f\u4ea7\u7684\u65f6\u5019\u5916\u8bbe\u4f1a\u6709\u533a\u522b\uff0c\u8fd9\u4e9b\u5dee\u5f02\u5bfc\u81f4\u8f6f\u4ef6\u5728\u540c\u5185\u6838\uff0c\u4e0d\u540c\u5916\u8bbe\u7684\u82af\u7247\u4e0a\u79fb\u690d\u56f0\u96be\u3002\u4e3a\u4e86\u89e3\u51b3\u4e0d\u540c\u82af\u7247\u5382\u5546\u751f\u4ea7\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff0cARM\u4e0e\u5404\u82af\u7247\u5382\u5546\u5efa\u8bae\u4e86CMSIS\u6807\u51c6\u3002

\u6240\u8c13\u7684CMSIS\u6807\u51c6\uff0c\u5176\u5b9e\u5c31\u662f\u65b0\u5efa\u4e86\u4e00\u4e2a\u8f6f\u4ef6\u62bd\u8c61\u5c42\uff0c\u5c06\u4e0d\u540c\u7684RTOS\u7684\u63a5\u53e3\u7edf\u4e00\u8d77\u6765\uff0c\u63d0\u4f9bCMSIS\u7684\u63a5\u53e3\u4f9b\u4f7f\u7528\u8005\u4f7f\u7528\u3002

\u672c\u6587\u53ea\u7b80\u5355\u5217\u4e3e\u4e86\u90e8\u5206CMSIS\u6807\u51c6\u4e0b\u7684\u51fd\u6570\u63a5\u53e3\uff0c\u5176\u4ed6\u51fd\u6570\u53ca\u5176\u5b9e\u73b0\u7ec6\u8282\u8bf7\u53c2\u8003\uff1aCMSIS-RTOS2

"},{"location":"rtos/freertos/CMSIS/#_1","title":"\u4efb\u52a1\u7ba1\u7406","text":"
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);    //\u521b\u5efa\u4efb\u52a1\nosThreadId_t osThreadGetId(void);    //\u8fd4\u56de\u5f53\u524d\u4efb\u52a1ID\nosStatus_t osThreadSuspend(osThreadId_t thread_id);    //\u6302\u8d77\u4efb\u52a1\nosStatus_t osThreadResume(osThreadId_t thread_id);    //\u6062\u590d\u4efb\u52a1\nosStatus_t osThreadYield(void);    //\u4e3b\u52a8\u8ba9\u51fa\u8d44\u6e90\nvoid osThreadExit(void)    //\u7ec8\u6b62\u5f53\u524d\u4efb\u52a1\nosStatus_t osThreadTerminate(osThreadId_t thread_id);    //\u7ec8\u6b62\u5176\u4ed6\u4efb\u52a1\nosStatus_t osDelay(uint32_t ticks);    //\u5ef6\u65f6ticks\u65f6\u949f\u5468\u671f\n
"},{"location":"rtos/freertos/CMSIS/#_2","title":"\u6d88\u606f\u961f\u5217","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5730\u5740\n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n    void *mq_mem;        //\u6570\u636e\u5b58\u50a8\u5730\u5740\n    uint32_t mq_size;    //\u6570\u636e\u5b58\u50a8\u5927\u5c0f\n}osMessageQueueAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);    //\u521b\u5efa\nosStatus_t osMessageQueueDelete(osMessageQueueId_t mq_id);    //\u5220\u9664\nosStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);    //\u53d1\u9001\u6d88\u606f\nosStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);    //\u83b7\u53d6\u6d88\u606f\nosStatus_t osMessageQueueReset(osMessageQueueId_t mq_id);    //\u91cd\u7f6e\u6d88\u606f\u961f\u5217\n
"},{"location":"rtos/freertos/CMSIS/#_3","title":"\u4fe1\u53f7\u91cf","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;   //\u540d\u5b57\n    uint32_t attr_bits; //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;       //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5730\u5740\n    uint32_t cb_size;   //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osSemaphoreAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);    //\u521b\u5efa\nosStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout);    //\u83b7\u53d6\nosStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id);    //\u91ca\u653e\nuint32_t osSemaphoreGetCount(osSemaphoreID_t semaphore_id);    //\u83b7\u53d6\u8ba1\u6570\u91cf\nosStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_4","title":"\u4e92\u65a5\u91cf","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //\u4e92\u65a5\u91cf\u7c7b\u578b\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5185\u5b58 \n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osMutexAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osMutexId_t osMutexNew(const osMutexAttr_t *attr);    //\u521b\u5efa\nosStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout);    //\u83b7\u53d6\nosStatus_t osMutexRelease(osMutexId_t mutex_id);    //\u91ca\u653e\nosStatus_t osMutexDelete(osMutexId_t mutex_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_5","title":"\u4e8b\u4ef6","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5185\u5b58\n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osEventFlagsAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr);    //\u521b\u5efa\nuint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags);    //\u8bbe\u7f6e\u4f4d\nuint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags);    //\u6e05\u7a7a\u4f4d\nuint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout);    //\u7b49\u5f85\u4f4d\nosStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_6","title":"\u4efb\u52a1\u901a\u77e5","text":"

CMSIS\u6807\u51c6\u6ca1\u6709\u63d0\u4f9b\u4efb\u52a1\u901a\u77e5\u7684API\uff0c\u5c5e\u4e8eFreeRTOS\u7684\u7279\u6027\u3002

"},{"location":"rtos/freertos/CMSIS/#_7","title":"\u5b9a\u65f6\u5668","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5185\u5b58\n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osTimerAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);    //\u521b\u5efa\nosStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);    //\u542f\u52a8\nosStatus_t osTimerStop(osTimerId_t timer_id);   //\u505c\u6b62\nosStatus_t osTimerDelete(osTimerId_t timer_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_8","title":"\u5185\u5b58\u7ba1\u7406","text":"

\u5e38\u7528\u51fd\u6570\uff1a

osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, unit32_t block_size, const osMemoryPoolAttr_t *attr);    //\u521b\u5efa\nvoid *osMemoryPoolAlloc(osMemoryPoolId_t mp_id, unit32_t timeout);    //\u5206\u914d\nosStatut_t osMemoryPoolFree(osMemoryPoolId_t mp_id, void *block);    //\u91ca\u653e\nosStatus_t osMemoryPoolDelete(osMemoryPoolId_t mp_id);    //\u5220\u9664\n
"},{"location":"rtos/rt-thread/","title":"rtt\u5185\u6838\u7b80\u4ecb","text":"

\u7531\u4e8e\u4e0d\u540cRTOS\u7684\u5185\u6838\u5927\u5dee\u4e0d\u5dee\uff0c\u8fd9\u91cc\u53ea\u8bb0\u5f55\u4e0brtt\u5185\u6838\u6bd4\u8f83\u7279\u6b8a\u7684\u5730\u65b9\u3002

rtt\u5185\u6838\u67b6\u6784\u5982\u4e0b\u56fe\u6240\u793a\uff1a

rtt\u5185\u6838\u6a21\u5757\u4e3b\u8981\u5206\u4e3a\uff1a

  1. \u7ebf\u7a0b\u8c03\u5ea6
  2. \u65f6\u949f\u7ba1\u7406
  3. \u7ebf\u7a0b\u95f4\u540c\u6b65\u4e0e\u901a\u4fe1
  4. \u5185\u5b58\u7ba1\u7406
  5. \u4e2d\u65ad\u7ba1\u7406
  6. I/O\u8bbe\u5907\u7ba1\u7406
  7. \u5916\u8bbe\u63a5\u53e3

\u4e0b\u56fe\u663e\u793a\u4e86rtt\u4e2d\u5404\u7c7b\u5185\u6838\u5bf9\u8c61\u7684\u6d3e\u751f\u548c\u7ee7\u627f\u5173\u7cfb\u3002

\u5185\u6838\u5bf9\u8c61\u63a7\u5236\u5757\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct rt_object {\n    char name[RT_NAME_MAX];    //\u5185\u6838\u5bf9\u8c61\u540d\u79f0\n    rt_uint8_t type;           //\u5185\u6838\u5bf9\u8c61\u7c7b\u578b\n    rt_uint8_t flag;           //\u5185\u6838\u5bf9\u8c61\u53c2\u6570\n    rt_list_t list;            //\u5185\u6838\u5bf9\u8c61\u7ba1\u7406\u94fe\u8868\n};\n

\u5185\u6838\u5bf9\u8c61\u5bb9\u5668\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct rt_object_information {\n    enum rt_object_class_type type;    //\u5bf9\u8c61\u7c7b\u578b\n    rt_list_t object_list;             //\u5bf9\u8c61\u94fe\u8868\n    rt_size_t object_size;             //\u5bf9\u8c61\u5927\u5c0f\n};\n

\u540c\u4e00\u7c7b\u5bf9\u8c61\u7531\u4e00\u4e2art_object_information\u7ed3\u6784\u4f53\u6765\u7ba1\u7406\uff0c\u8fd9\u7c7b\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u4ee5\u94fe\u8868\u7684\u5f62\u5f0f\u6302\u5728object_list\u94fe\u8868\u4e0a\u3002\u5f53\u67d0\u4e2a\u5185\u6838\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u540e\uff0c\u8be5\u5bf9\u8c61\u5c31\u88ab\u6dfb\u52a0\u5230\u5bf9\u5e94\u5bb9\u5668\u4e2d\u7ba1\u7406\u3002

"},{"location":"rtos/rt-thread/#_1","title":"\u7ebf\u7a0b\u7ba1\u7406","text":"

rtt\u4e2d\u7ebf\u7a0b\u72b6\u6001\u591a\u4e86\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\uff0c\u5f53\u7ebf\u7a0b\u521b\u5efa\u5b8c\u6bd5\u8fd8\u6ca1\u5f00\u59cb\u8fd0\u884c\u65f6\u5c31\u5904\u4e8e\u521d\u59cb\u72b6\u6001\uff0c\u8be5\u72b6\u6001\u4e0b\u7ebf\u7a0b\u4e0d\u53c2\u4e0e\u8c03\u5ea6\uff0c\u5b8f\u5b9a\u4e49\u4e3aRT_THREAD_INIT\u3002\u521d\u59cb\u72b6\u6001\u7684\u7ebf\u7a0b\u9700\u8981\u8c03\u7528rt_thread_startup()\u8fdb\u5165\u5c31\u7eea\u72b6\u6001\u3002

rtt\u6700\u5927\u652f\u6301256\u4e2a\u4f18\u5148\u7ea7\uff0c\u5bf9\u4e8eARM Cortex-M\u7cfb\u5217\u4e00\u822c\u91c7\u752832\u4e2a\u4f18\u5148\u7ea7\u3002

\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u65f6\u95f4\u7247\u53c2\u6570\uff0c\u65f6\u95f4\u7247\u4ec5\u5bf9\u4f18\u5148\u7ea7\u76f8\u540c\u7684\u5c31\u7eea\u72b6\u6001\u7ebf\u7a0b\u6709\u6548\uff08\u5373\u540c\u4f18\u5148\u7ea7\u53c2\u4e0e\u8c03\u5ea6\uff09\u3002\u65f6\u95f4\u7247\u7684\u5355\u4f4d\u662f\u4e00\u4e2a\u7cfb\u7edf\u8282\u62cd\uff0c\u5176\u610f\u4e49\u662f\u7ebf\u7a0b\u5355\u6b21\u8fd0\u884c\u7684\u65f6\u957f\u3002

\u4f7f\u7528rt_thread_create()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u7ebf\u7a0b\uff0c\u4f7f\u7528rt_thread_delete()\u51fd\u6570\u5220\u9664\u4e00\u4e2a\u52a8\u6001\u7ebf\u7a0b\u3002\u9759\u6001\u7ebf\u7a0b\u7684API\u5206\u522b\u662frt_thread_init()\u51fd\u6570\u548crt_thread_detach()\u51fd\u6570\u3002\u4f7f\u7528rt_thread_startup()\u51fd\u6570\u542f\u52a8\u7ebf\u7a0b\uff0crt_thread_yield()\u51fd\u6570\u8ba9\u51fa\u7ebf\u7a0b\u8d44\u6e90\uff0crt_thread_delay()\u51fd\u6570\u4f7f\u7ebf\u7a0b\u7761\u7720\uff0crt_thread_resume()\u51fd\u6570\u6062\u590d\u7ebf\u7a0b\u3002

"},{"location":"rtos/rt-thread/#_2","title":"\u65f6\u949f\u7ba1\u7406","text":"

\u5168\u5c40\u53d8\u91cfrt_tick\u6bcf\u6b21\u7ecf\u8fc7\u4e00\u4e2a\u65f6\u949f\u8282\u62cd\uff0c\u503c\u5c31\u4f1a\u52a01\uff0c\u5b83\u8868\u793a\u7cfb\u7edf\u4ece\u542f\u52a8\u5f00\u59cb\u603b\u5171\u7ecf\u8fc7\u7684\u59cb\u7ec8\u8282\u62cd\u6570\u3002\u6b64\u5916\uff0crt_tick\u6bcf\u6b21\u52a01\u90fd\u4f1a\u68c0\u67e5\u5f53\u524d\u7ebf\u7a0b\u7684\u65f6\u95f4\u7247\u662f\u5426\u4f7f\u7528\u5b8c\uff0c\u4ee5\u53ca\u662f\u5426\u6709\u5b9a\u65f6\u5668\u8d85\u65f6\u3002

rtt\u7684\u5b9a\u65f6\u5668\u53ef\u4ee5\u5206\u4e3aHARD_TIMER\u6a21\u5f0f\u2014\u2014\u5728\u4e2d\u65ad\u73af\u5883\u4e2d\u6267\u884c\uff0c\u548cSOFT_TIMER\u6a21\u5f0f\u2014\u2014\u5728\u7ebf\u7a0b\u73af\u5883\u4e2d\u6267\u884c\u3002

\u65b0\u521b\u5efa\u5e76\u6fc0\u6d3b\u7684\u5b9a\u65f6\u5668\u90fd\u4f1a\u4ee5\u8d85\u65f6\u65f6\u95f4\u6392\u5e8f\u7684\u65b9\u5f0f\u63d2\u5165\u5230rt_timer_list\u94fe\u8868\u4e2d\u3002\u6240\u6709\u5b9a\u65f6\u5668\u5728\u5b9a\u65f6\u8d85\u65f6\u540e\u90fd\u4f1a\u4ece\u5b9a\u65f6\u5668\u94fe\u8868\u4e2d\u88ab\u79fb\u9664\uff0c\u800c\u5468\u671f\u6027\u5b9a\u65f6\u5668\u4f1a\u5728\u5b83\u518d\u6b21\u542f\u52a8\u65f6\u88ab\u52a0\u5165\u5b9a\u65f6\u5668\u94fe\u8868\u3002\u4e0e\u5b9a\u65f6\u5668\u6709\u5173\u7684\u64cd\u4f5c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

"},{"location":"rtos/rt-thread/#_3","title":"\u7ebf\u7a0b\u95f4\u540c\u6b65","text":""},{"location":"rtos/rt-thread/#_4","title":"\u4fe1\u53f7\u91cf","text":"

\u4fe1\u53f7\u91cf\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

rtt\u5185\u6838\u5728\u521b\u5efa\u4fe1\u53f7\u91cf\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u901a\u8fc7flag\u53c2\u6570\u6307\u5b9a\u4fe1\u53f7\u91cf\u4e0d\u53ef\u7528\u65f6\u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u7684\u6392\u961f\u65b9\u5f0f\u3002\u6bd4\u5982RT_IPC_FLAG_FIFO\u8868\u793a\u6309\u7167\u5148\u8fdb\u5148\u51fa\u7684\u65b9\u5f0f\u6392\u961f\uff0cRT_IPC_FLAG_PRIO\u8868\u793a\u6309\u7167\u4f18\u5148\u7ea7\u7684\u65b9\u5f0f\u8fdb\u884c\u6392\u961f\u3002\u4e00\u822c\u91c7\u7528\u4f18\u5148\u7ea7\u65b9\u5f0f\uff0c\u9664\u975e\u5e94\u7528\u7a0b\u5e8f\u975e\u5e38\u5728\u610f\u5148\u6765\u540e\u5230\u3002

"},{"location":"rtos/rt-thread/#_5","title":"\u4e92\u65a5\u91cf","text":"

\u4e92\u65a5\u91cf\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

\u4e92\u65a5\u91cf\u4e3b\u8981\u9002\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a

  1. \u7ebf\u7a0b\u591a\u6b21\u6301\u6709\u4e92\u65a5\u91cf\uff0c\u4e92\u65a5\u91cf\u53ef\u4ee5\u9012\u5f52\u6301\u6709\u800c\u4fe1\u53f7\u91cf\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002
  2. \u7531\u4e8e\u591a\u7ebf\u7a0b\u540c\u6b65\u800c\u9020\u6210\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898\u3002
"},{"location":"rtos/rt-thread/#_6","title":"\u4e8b\u4ef6\u7ec4","text":"

rtt\u5b9a\u4e49\u7684\u4e8b\u4ef6\u96c6\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a

  1. \u4e8b\u4ef6\u53ea\u4e0e\u7ebf\u7a0b\u76f8\u5173\uff0c\u4e8b\u4ef6\u95f4\u76f8\u4e92\u72ec\u7acb\uff1a\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u62e5\u670932\u4e2a\u4e8b\u4ef6\u6807\u5fd7\uff0c\u91c7\u7528\u4e00\u4e2a32bit\u65e0\u7b26\u53f7\u6574\u578b\u6570\u8fdb\u884c\u8bb0\u5f55\uff0c\u6bcf\u4e00\u4e2abit\u4ee3\u8868\u4e00\u4e2a\u4e8b\u4ef6\uff1b

  2. \u4e8b\u4ef6\u4ec5\u7528\u4e8e\u540c\u6b65\uff0c\u4e0d\u63d0\u4f9b\u6570\u636e\u4f20\u8f93\u529f\u80fd\uff1b

  3. \u4e8b\u4ef6\u65e0\u6392\u961f\u6027\uff0c\u5373\u591a\u6b21\u5411\u7ebf\u7a0b\u53d1\u9001\u540c\u4e00\u4e8b\u4ef6 (\u5982\u679c\u7ebf\u7a0b\u8fd8\u672a\u6765\u5f97\u53ca\u8bfb\u8d70)\uff0c\u5176\u6548\u679c\u7b49\u540c\u4e8e\u53ea\u53d1\u9001\u4e00\u6b21\u3002

\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u62e5\u6709\u4e00\u4e2a\u4e8b\u4ef6\u4fe1\u606f\u6807\u8bb0\uff0c\u5b83\u6709\u4e09\u4e2a\u5c5e\u6027\uff0c\u5206\u522b\u662fRT_EVENT_FLAG_AND(\u903b\u8f91\u4e0e)\uff0cRT_EVENT_FLAG_OR(\u903b\u8f91\u6216\uff09\u4ee5\u53caRT_EVENT_FLAG_CLEAR(\u6e05\u9664\u6807\u8bb0\uff09\u3002\u5f53\u7ebf\u7a0b\u7b49\u5f85\u4e8b\u4ef6\u540c\u6b65\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc732\u4e2a\u4e8b\u4ef6\u6807\u5fd7\u548c\u8fd9\u4e2a\u4e8b\u4ef6\u4fe1\u606f\u6807\u8bb0\u6765\u5224\u65ad\u5f53\u524d\u63a5\u6536\u7684\u4e8b\u4ef6\u662f\u5426\u6ee1\u8db3\u540c\u6b65\u6761\u4ef6\u3002

\u4e8b\u4ef6\u7ec4\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_7","title":"\u7ebf\u7a0b\u95f4\u901a\u4fe1","text":""},{"location":"rtos/rt-thread/#_8","title":"\u90ae\u7bb1","text":"

\u90ae\u7bb1\u7684\u7279\u70b9\u662f\u5f00\u9500\u6bd4\u8f83\u4f4e\uff0c\u6548\u7387\u8f83\u9ad8\u3002\u90ae\u7bb1\u4e2d\u7684\u6bcf\u4e00\u5c01\u90ae\u4ef6\u53ea\u80fd\u5bb9\u7eb3\u56fa\u5b9a\u76844\u5b57\u8282\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\u8bf4\u90ae\u7bb1\u53ea\u80fd\u7528\u6765\u4f20\u8f93\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5bb9\u3002\u90ae\u7bb1\u4f7f\u7528\u4e00\u4e2a\u56fa\u5b9a\u7f13\u51b2\u533a\u5927\u5c0f\u6765\u5b58\u50a8\u90ae\u4ef6\uff0c\u5176\u5927\u5c0f\u5728\u521b\u5efa\u65f6\u88ab\u5b9a\u4e49\u3002

\u975e\u963b\u585e\u65b9\u5f0f\u7684\u90ae\u4ef6\u53d1\u9001\u8fc7\u7a0b\u80fd\u591f\u5b89\u5168\u7684\u5e94\u7528\u4e8e\u4e2d\u65ad\u670d\u52a1\u4e2d\uff0c\u662f\u7ebf\u7a0b\u3001\u4e2d\u65ad\u670d\u52a1\u3001\u5b9a\u65f6\u5668\u5411\u7ebf\u7a0b\u53d1\u9001\u6d88\u606f\u7684\u6709\u6548\u624b\u6bb5\u3002\u6839\u636e\u5bf9\u5e94\u7684\u60c5\u51b5\uff0c\u90ae\u7bb1\u7684\u6536\u53d6\u6216\u8005\u53d1\u9001\u90fd\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u8d85\u65f6\u963b\u585e\uff0c\u8fdb\u5165\u963b\u585e\u72b6\u6001\u7684\u7ebf\u7a0b\u5c06\u7761\u7720\uff0c\u7b49\u5f85\u5bf9\u5e94\u6761\u4ef6\u6ee1\u8db3\u540e\u5524\u9192\u3002

\u90ae\u7bb1\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_9","title":"\u6d88\u606f\u961f\u5217","text":"

\u6d88\u606f\u961f\u5217\u662f\u90ae\u7bb1\u7684\u6269\u5c55\uff0c\u53ef\u4ee5\u7528\u6765\u4f20\u8f93\u4e0d\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5bb9\u3002

\u6d88\u606f\u961f\u5217\u9700\u8981\u5c06\u6d88\u606f\u7f13\u5b58\u5728\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u5185\u5b58\u5360\u7528\u6bd4\u90ae\u7bb1\u8981\u5927\u3002\u591a\u4e2a\u7ebf\u7a0b\u80fd\u591f\u8bfb\u53d6\u540c\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u3002\u800c\u5f53\u6d88\u606f\u961f\u5217\u662f\u7a7a\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u6302\u8d77\u8bfb\u53d6\u7ebf\u7a0b\u3002\u5f53\u6709\u65b0\u7684\u6d88\u606f\u5230\u8fbe\u65f6\uff0c\u6302\u8d77\u7684\u7ebf\u7a0b\u5c06\u88ab\u5524\u9192\u4ee5\u63a5\u6536\u5e76\u5904\u7406\u6d88\u606f\u3002\u6d88\u606f\u961f\u5217\u662f\u4e00\u79cd\u5f02\u6b65\u7684\u901a\u4fe1\u65b9\u5f0f\u3002

\u6d88\u606f\u961f\u5217\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_10","title":"\u4fe1\u53f7","text":"

\u4fe1\u53f7\u5176\u5b9e\u662f\u7528\u6237\u5c42\u9762\u5bf9\u786c\u4ef6\u4e2d\u65ad\u7684\u4e00\u79cd\u6a21\u62df\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u6536\u5230\u4fe1\u53f7\u4e4b\u540e\u4f1a\u8f6c\u53bb\u5bf9\u5e94\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002rtt\u4e2d\u4f7f\u7528rt_sigset_t\u7c7b\u578b\u6765\u5b9a\u4e49\u4fe1\u53f7\u96c6\u3002

\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6536\u5230\u4fe1\u53f7\u65f6\uff0c\u5982\u679c\u5b83\u5904\u4e8e\u6302\u8d77\u72b6\u6001\uff0c\u90a3\u4e48\u5b83\u4f1a\u6539\u4e3a\u5c31\u7eea\u72b6\u6001\u53bb\u5904\u7406\u5bf9\u5e94\u7684\u4fe1\u53f7\u3002

\u4fe1\u53f7\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

\u5728\u4fe1\u53f7\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5148\u5b9a\u4e49\u7ebf\u7a0b\u9700\u8981\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528rt_signal_install()\u51fd\u6570\u5c06signo\u4e0e\u5bf9\u5e94\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u76f8\u5173\u8054\u3002

"},{"location":"rtos/rt-thread/#_11","title":"\u5185\u5b58\u7ba1\u7406","text":"

\u5b9e\u65f6\u7cfb\u7edf\u8981\u6c42\u5185\u5b58\u5757\u7684\u5206\u914d\u8fc7\u7a0b\u5728\u53ef\u9884\u6d4b\u7684\u65f6\u95f4\u5185\u5b8c\u6210\uff0c\u5426\u5219\u5b9e\u65f6\u4efb\u52a1\u5bf9\u5916\u90e8\u4e8b\u4ef6\u7684\u54cd\u5e94\u5c06\u53d8\u5f97\u4e0d\u786e\u5b9a\u3002rtt\u5185\u6838\u5728\u5185\u5b58\u5206\u914d\u7ba1\u7406\u7b97\u6cd5\u4e0a\uff0c\u63d0\u4f9b\u4e86\u4e24\u5927\u7c7b\uff1a\u5185\u5b58\u5806\u7ba1\u7406\u4e0e\u5185\u5b58\u6c60\u7ba1\u7406\u3002\u800c\u5185\u5b58\u5bf9\u7ba1\u7406\u53c8\u5212\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a\u5c0f\u5185\u5b58\u7ba1\u7406\u7b97\u6cd5\uff0cslab\u7ba1\u7406\u7b97\u6cd5\u548cmemheap\u7ba1\u7406\u7b97\u6cd5\u3002

"},{"location":"rtos/rt-thread/#_12","title":"\u5185\u5b58\u5806\u7ba1\u7406","text":"

\u5c0f\u5185\u5b58\u7ba1\u7406\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5185\u5b58\u5206\u914d\u7b97\u6cd5\u3002\u521d\u59cb\u65f6\uff0c\u5b83\u662f\u4e00\u5757\u5927\u5185\u5b58\uff0c\u5f53\u9700\u8981\u5206\u914d\u5185\u5b58\u5757\u65f6\uff0c\u4ece\u8fd9\u5757\u5927\u5185\u5b58\u4e0a\u5206\u5272\u51fa\u54cd\u5e94\u7684\u5185\u5b58\u5757\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u5757\u5f52\u8fd8\u7ed9\u5806\u7ba1\u7406\u7cfb\u7edf\u3002\u6bcf\u4e2a\u5185\u5b58\u5757\u90fd\u7528\u53cc\u5411\u94fe\u8868\u7684\u65b9\u5f0f\u94fe\u63a5\u8d77\u6765\u3002\u901a\u8fc7*used\u6210\u5458\u53d8\u91cf\u6807\u8bb0\u5f53\u524d\u5185\u5b58\u5757\u662f\u5426\u5df2\u88ab\u4f7f\u7528\u3002

slab\u5206\u914d\u5668\u4f1a\u6839\u636e\u5bf9\u8c61\u7684\u5927\u5c0f\u5206\u6210\u591a\u4e2a\u533a\uff08zone\uff09\uff0c\u4e00\u4e2azone\u7684\u5927\u5c0f\u572832K\u5230128K\u5b57\u8282\u4e4b\u95f4\uff0c\u7cfb\u7edf\u4e2d\u7684zone\u6700\u591a\u5305\u62ec72\u79cd\u5bf9\u8c61\uff0c\u4e00\u6b21\u6027\u6700\u5927\u53ef\u4ee5\u5206\u914d16K\u7684\u7a7a\u95f4\u3002\u5982\u679c\u8d85\u51fa\u4e8616K\u5219\u76f4\u63a5\u4ece\u9875\u5206\u914d\u5668\u4e2d\u5206\u914d\u3002\u6bcf\u4e2azone\u4e0a\u90fd\u662f\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u5757\u94fe\u63a5\u5728\u4e00\u8d77\u3002\u800c72\u79cd\u5bf9\u8c61\u7684zone\u94fe\u8868\u5219\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7edf\u4e00\u7ba1\u7406\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

memheap\u7ba1\u7406\u7b97\u6cd5\u9002\u7528\u4e8e\u7cfb\u7edf\u6709\u591a\u4e2a\u4e0d\u53ef\u8fde\u7eed\u5730\u5740\u7684\u5185\u5b58\u5806\u3002\u7528\u6237\u53ea\u9700\u8981\u5728\u7cfb\u7edf\u521d\u59cb\u5316\u65f6\u5c06\u591a\u4e2a\u6240\u9700\u7684memheap\u521d\u59cb\u5316\uff0c\u5e76\u5f00\u542fmemheap\u529f\u80fd\u5c31\u53ef\u4ee5\u5c06\u591a\u4e2amemheap\u7c98\u5408\u8d77\u6765\u7edf\u4e00\u7528\u4f5c\u5185\u5b58\u5206\u914d\u3002

\u5728\u4f7f\u7528\u5185\u5b58\u5806\u65f6\uff0c\u5fc5\u987b\u5728\u7cfb\u7edf\u521d\u59cb\u5316\u7684\u65f6\u5019\u8fdb\u884c\u5806\u7684\u521d\u59cb\u5316\uff1a

void rt_system_heap_init(void *begin_addr, void *end_addr);\n

\u5185\u5b58\u5806\u7ba1\u7406\u7684\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_13","title":"\u5185\u5b58\u6c60\u7ba1\u7406","text":"

\u9891\u7e41\u5730\u8c03\u7528\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u51fd\u6570\u4f1a\u5bfc\u81f4\u5185\u5b58\u788e\u7247\uff0c\u56e0\u6b64rtt\u5f15\u8fdb\u4e86\u53e6\u5916\u4e00\u79cd\u5185\u5b58\u7ba1\u7406\u65b9\u6cd5\uff1a\u5185\u5b58\u6c60\u3002

\u5185\u5b58\u6c60\u7528\u4e8e\u5206\u914d\u5927\u91cf\u5927\u5c0f\u76f8\u540c\u7684\u5c0f\u5185\u5b58\u5757\uff0c\u5185\u5b58\u6c60\u5728\u521b\u5efa\u65f6\u5148\u5411\u7cfb\u7edf\u7533\u8bf7\u4e00\u5927\u5757\u5185\u5b58\uff0c\u7136\u540e\u5206\u6210\u540c\u6837\u5927\u5c0f\u7684\u591a\u4e2a\u5c0f\u5185\u5b58\u5757\uff0c\u5c0f\u5185\u5b58\u5757\u76f4\u63a5\u901a\u8fc7\u94fe\u8868\u8fde\u63a5\u8d77\u6765\uff08\u6b64\u94fe\u8868\u4e5f\u79f0\u4e3a\u7a7a\u95f2\u94fe\u8868\uff09\u3002\u6bcf\u6b21\u5206\u914d\u7684\u65f6\u5019\uff0c\u4ece\u7a7a\u95f2\u94fe\u8868\u4e2d\u53d6\u51fa\u94fe\u5934\u4e0a\u7b2c\u4e00\u4e2a\u5185\u5b58\u5757\uff0c\u63d0\u4f9b\u7ed9\u7533\u8bf7\u8005\u3002\u5185\u5b58\u6c60\u4e00\u65e6\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u5185\u90e8\u7684\u5185\u5b58\u5757\u5927\u5c0f\u5c06\u4e0d\u80fd\u518d\u505a\u8c03\u6574\u3002

\u5185\u5b58\u6c60\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_14","title":"\u4e2d\u65ad\u7ba1\u7406","text":""},{"location":"tools/","title":"\u5de5\u5177","text":"

\u5217\u4e3e\u4e00\u4e0b\u81ea\u5df1\u4f7f\u7528\u8fc7\u7684\u5de5\u5177\u3002

"},{"location":"tools/github/","title":"github","text":"

\u7531\u4e8e\u4f17\u6240\u5468\u77e5\u7684\u539f\u56e0\uff0cgithub\u5728\u56fd\u5185\u7684\u8bbf\u95ee\u4e0d\u5c3d\u5982\u4eba\u610f\uff0c\u4f46\u662f\u56fd\u5916\u5f00\u6e90\u9879\u76ee\u51e0\u4e4e\u90fd\u90e8\u7f72\u5728github\u4e0a\uff0c\u4e0d\u5f97\u4e0d\u7528\u3002\u5982\u679c\u662f\u81ea\u5df1\u7684\u865a\u62df\u673a\u8fde\u63a5\u7f51\u901f\u6bd4\u8f83\u6162\uff0c\u5efa\u8bae\u5f00\u4e2a\u68af\u5b50\u7136\u540e\u914d\u7f6e\u4e0b\u6d41\u91cf\u4ee3\u7406\uff1a

git config --global http.proxy http://127.0.0.1:[\u7aef\u53e3\u53f7]\n\ngit config --global https.proxy http://127.0.0.1:[\u7aef\u53e3\u53f7]\n

\u8fd9\u4e24\u884c\u547d\u4ee4\u7684\u610f\u601d\u662f\u8ba9http\u548chttps\u534f\u8bae\u7684\u6d41\u91cf\u5168\u90e8\u8d70\u4f60\u68af\u5b50\u7684\u4ee3\u7406\uff0c\u7aef\u53e3\u53f7\u6839\u636e\u68af\u5b50\u7684\u914d\u7f6e\u81ea\u884c\u8f93\u5165\u3002\u914d\u7f6e\u5b8c\u4ee5\u540egit clone\u5e94\u8be5\u4e0d\u4f1a\u5361\u4e86\u3002\u5982\u679c\u4f60\u8981\u53d6\u6d88\u5168\u5c40\u4ee3\u7406\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a

git config --global --unset http.proxy\n\ngit config --global --unset https.proxy\n

\u4e00\u822c\u6765\u8bf4\uff0cgithub\u7684SSH\u9ed8\u8ba4\u901a\u8fc7\u7aef\u53e322\u5efa\u7acb\u8fde\u63a5\uff0c\u9664\u975e\u4f60\u66f4\u6539\u4e86\u914d\u7f6e\u6587\u4ef6\u5f3a\u5236\u4f7f\u7528HTTPS\u7684443\u7aef\u53e3\u8fde\u63a5\u3002

\u8981\u6d4b\u8bd5\u901a\u8fc7HTTPS\u7aef\u53e3\u7684SSH\u8fde\u63a5\u662f\u5426\u53ef\u884c\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a

ssh -T -p 443 git@ssh.github.com\n\n> Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.\n
\u5982\u679c\u8f93\u51fa\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5219\u8bf4\u660e\u8fde\u63a5\u53ef\u884c\u3002\u4f60\u53ef\u4ee5\u66f4\u6539~/.ssh/config\u6587\u4ef6\uff0c\u5f3a\u5236\u4e0egithub\u7684\u8fde\u63a5\u90fd\u901a\u8fc7\u8be5\u670d\u52a1\u5668\u548c\u7aef\u53e3\uff1a

Host github.com\nHostname ssh.github.com\nPort 443\nUser git\n

\u901a\u8fc7\u518d\u6b21\u8fde\u63a5\u5230github\u6765\u6d4b\u8bd5\u662f\u5426\u6709\u6548\uff0c\u6ce8\u610f\u8fd9\u65f6\u5c31\u4e0d\u9700\u8981\u6307\u5b9a\u7aef\u53e3\u53f7\u4e86\uff1a

ssh -T git@github.com\n\n> Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.\n

\u6ce8\u610f\uff1aSSH\u901a\u8fc7\u516c\u94a5\u548c\u79c1\u94a5\u914d\u5bf9\u7684\u65b9\u5f0f\u6765\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u5b89\u5168\uff0c\u5f53\u4f60\u7b2c\u4e00\u6b21\u901a\u8fc7SSH\u8fde\u63a5\u65f6\uff0c\u4f1a\u8be2\u95ee\u4f60\u662f\u5426\u4fe1\u4efb\u8be5\u670d\u52a1\u5668\uff0c\u8f93\u5165yes\u5373\u53ef\u3002

"},{"location":"tools/github/#ssh","title":"\u6dfb\u52a0SSH\u5bc6\u94a5","text":"

\u6dfb\u52a0SSH\u5bc6\u94a5\u7684\u8fc7\u7a0b\u5f88\u7b80\u5355\uff0c\u6309\u7167\u6b65\u9aa4\u4e00\u6b65\u6b65\u6765\u5373\u53ef\u3002

1.\u9996\u5148\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5c06\u90ae\u4ef6\u5730\u5740\u6539\u4e3a\u4f60\u6ce8\u518cgithub\u7684\u90ae\u7bb1\uff1a

ssh-keygen -t ed25519 -C \"your_email@example.com\"\n

\u7cfb\u7edf\u4f1a\u8981\u6c42\u4f60\u8f93\u5165\u4e00\u4e9b\u4fe1\u606f\uff0c\u8fde\u7eed\u6309\u56de\u8f66\u8df3\u8fc7\u5373\u53ef\u3002

2.\u5c06SSH\u5bc6\u94a5\u6dfb\u52a0\u5230ssh-agent\uff1a

\u5148\u542f\u52a8ssh\u4ee3\u7406\uff1a

eval \"$(ssh-agent -s)\"\n

\u7136\u540e\u6dfb\u52a0\u79c1\u94a5\uff1a

ssh-add ~/.ssh/id_ed25519\n

3.\u5411\u4f60\u7684\u8d26\u6237\u6dfb\u52a0\u65b0\u7684SSH\u5bc6\u94a5

\u73b0\u5728\uff0c.ssh/\u76ee\u5f55\u4e0b\u6709\u4e24\u4e2a\u6587\u4ef6\uff0c\u4ee5pub\u7ed3\u5c3e\u7684\u5c31\u662f\u516c\u94a5\uff0c\u9700\u8981\u4f60\u4e0a\u4f20\u81f3github\u670d\u52a1\u5668\u3002

cat ~/.ssh/id_ed25519.pub\n

\u590d\u5236\u6253\u5370\u7684\u5185\u5bb9\uff0c\u7136\u540e\u5728github\u4e2a\u4eba\u8d44\u6599\u9762\u70b9\u51fb\u8bbe\u7f6e\uff0c\u627e\u5230SSH and GPG keys\uff0c\u70b9\u51fbNew SSH key\uff0c\u6700\u540e\u7c98\u8d34\u590d\u5236\u7684\u516c\u94a5\u5373\u53ef\u3002

"},{"location":"tools/github/#_1","title":"\u7ba1\u7406\u8fdc\u7a0b\u4ed3\u5e93","text":"

\u8981\u6dfb\u52a0\u4e00\u4e2a\u8fdc\u7a0b\u4ed3\u5e93\u94fe\u63a5\uff0c\u4f7f\u7528git remote add \u547d\u4ee4\uff0c\u8be5\u547d\u4ee4\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a

  • \u8fdc\u7a0b\u4ed3\u5e93\u540d\uff0c\u6bd4\u5982origin
  • \u8fdc\u7a0b\u4ed3\u5e93\u5730\u5740\uff0c\u6bd4\u5982https://github.com/OWNER/REPOSITORY.git
git remote add origin https://github.com/OWNER/REPOSITORY.git\n

\u5728\u6dfb\u52a0\u5b8c\u8fdc\u7a0b\u4ed3\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528git remote -v\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u4f60\u5f53\u524d\u6dfb\u52a0\u7684\u8fdc\u7a0b\u4ed3\u5e93\uff1a

git remote -v\n# Verify new remote\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n

\u5982\u679c\u8981\u66f4\u6539\u8fde\u63a5\u8fdc\u7a0b\u4ed3\u5e93\u7684\u65b9\u5f0f\uff0c\u6bd4\u5982\u4eceHTTPS\u6539\u4e3aSSH\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528git remote set-url\u547d\u4ee4\uff1a

git remote set-url origin git@github.com:OWNER/REPOSITORY.git\n

\u8fd9\u65f6\u8fdc\u7a0b\u4ed3\u5e93\u7684\u683c\u5f0f\u5e94\u8be5\u662f\uff1a

origin  git@github.com:OWNER/REPOSITORY.git (fetch)\norigin  git@github.com:OWNER/REPOSITORY.git (push)\n
"},{"location":"tools/github/#git","title":"git\u5e38\u7528\u547d\u4ee4","text":"

\u9879\u76ee\u521d\u59cb\u5316\uff1a

git init\n

\u521d\u59cb\u5316\u4e2a\u4eba\u4fe1\u606f\uff1a

git config --global user.name \"\u4f60\u7684\u540d\u5b57\"\ngit config --global user.email \"\u4f60\u7684\u90ae\u7bb1\"\n

\u5c06\u53d8\u52a8\u7684\u6587\u4ef6\u6dfb\u52a0\u5230\u6682\u5b58\u533a\uff1a

git add --all\n

\u63d0\u4ea4\u4ee3\u7801\uff1a

git commit -m \"\u63d0\u4ea4\u4fe1\u606f\"\n

\u63a8\u9001\u4ee3\u7801\uff1a

git push origin main\n

origin\u66ff\u6362\u4e3a\u8fdc\u7a0b\u4ed3\u5e93\u7684\u540d\u79f0\uff0cmain\u66ff\u6362\u4e3a\u63d0\u4ea4\u7684\u5206\u652f\u540d

\u62c9\u53d6\u4ee3\u7801\u5230\u672c\u5730\uff1a

git pull origin main\n

\u67e5\u770b\u5f53\u524dgit\u72b6\u6001\uff1a

git status\n

\u67e5\u770b\u63d0\u4ea4\u65e5\u5fd7\uff1a

git log\n

\u5207\u6362\u5206\u652f\uff1a

git check -b [\u65b0\u5206\u652f\u540d]\n

\u5408\u5e76\u4ee3\u7801\uff1a

git merge [\u53e6\u4e00\u4e2a\u5206\u652f\u540d]\n
"},{"location":"tools/markdown/","title":"Markdown","text":"

Markdown\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6807\u8bb0\u8bed\u8a00\uff0c\u6392\u7248\u8bed\u6cd5\u7b80\u6d01\uff0c\u8ba9\u4eba\u4eec\u66f4\u591a\u5730\u5173\u6ce8\u5185\u5bb9\u672c\u8eab\u800c\u975e\u6392\u7248\u3002\u5b83\u4f7f\u7528\u6613\u8bfb\u6613\u5199\u7684\u7eaf\u6587\u672c\u683c\u5f0f\u7f16\u5199\u6587\u6863\uff0c\u53ef\u4e0eHTML\u6df7\u7f16\uff0c\u53ef\u5bfc\u51faHTML\u3001PDF\u4ee5\u53ca\u672c\u8eab\u7684.md\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u56e0\u7b80\u6d01\u3001\u9ad8\u6548\u3001\u6613\u8bfb\u3001\u6613\u5199\uff0cMarkdown\u88ab\u5927\u91cf\u4f7f\u7528\uff0c\u5982Github\u3001Wikipedia\u3001\u7b80\u4e66\u7b49\u3002

\u53ef\u4ee5\u5148\u5728\u7ebf\u4f53\u9a8c\u4e00\u4e0bMarkdown\u5728\u7ebf\u7f16\u8f91\u5668\u3002

Markdown\u7684\u8bed\u6cd5\u5341\u5206\u7b80\u5355\uff0c\u534a\u5c0f\u65f6\u8db3\u4ee5\u638c\u63e1\u3002\u5b66\u5b8c\u4e4b\u540e\u53ef\u4ee5\u4e13\u5fc3\u4e8e\u5185\u5bb9\u8f93\u51fa\uff0c\u5927\u91cf\u63d0\u9ad8\u751f\u4ea7\u529b\uff0c\u975e\u5e38\u63a8\u8350\u3002

\u5b98\u65b9\u7f51\u7ad9\u7684\u5185\u5bb9\u5df2\u7ecf\u8db3\u591f\u8be6\u7ec6\uff0c\u8bf7\u76f4\u63a5\u67e5\u770b\u5b98\u65b9\u6587\u6863\u81ea\u5b66\uff1aMarkdown\u5b98\u65b9\u6559\u7a0b\u3002

"},{"location":"tools/vim/","title":"vim","text":"

\u8fd9\u4e0d\u662f\u65b0\u624b\u6559\u7a0b\uff0c\u53ea\u8bb0\u5f55\u4e00\u4e9b\u5e38\u7528\u64cd\u4f5c\u3002

vim\u547d\u4ee4\u53ef\u4ee5\u7531\u64cd\u4f5c\u7b26 + \u6b21\u6570 + \u52a8\u4f5c\u6765\u7ec4\u6210\uff0c\u6bd4\u5982d3w\u8868\u793a\u5220\u96643\u4e2a\u5355\u8bcd\u3002

u\uff1a\u64a4\u9500\u4fee\u6539

CTRL + R\uff1a\u6062\u590d\u4fee\u6539

"},{"location":"tools/vim/#_1","title":"\u79fb\u52a8","text":"

h, j, k, l\uff1a\u5de6\u4e0b\u4e0a\u53f3

e\uff1a\u8df3\u8f6c\u81f3\u4e0b\u4e00\u4e2a\u5355\u8bcd\u672b\u5c3e

b:\u8df3\u8f6c\u81f3\u4e0a\u4e00\u4e2a\u5355\u8bcd\u5f00\u5934

w\uff1a\u8df3\u8f6c\u81f3\u4e0b\u4e00\u4e2a\u5355\u8bcd\u5f00\u5934

$\uff1a\u8df3\u8f6c\u81f3\u884c\u672b

CTRL + O\uff1a\u8fd4\u56de\u4e0a\u6b21\u4f4d\u7f6e

CTRL + I\uff1a\u524d\u5f80\u4e0b\u6b21\u4f4d\u7f6e

0\uff1a\u8df3\u8f6c\u81f3\u884c\u9996

nG\uff1a\u8df3\u8f6c\u81f3\u7b2cn\u884c

GG\uff1a\u8df3\u8f6c\u81f3\u6587\u4ef6\u672b\u5c3e

gg\uff1a\u8df3\u8f6c\u81f3\u6587\u4ef6\u5f00\u5934

"},{"location":"tools/vim/#_2","title":"\u63d2\u5165","text":"

a\uff1a\u5149\u6807\u540e\u63d2\u5165

A\uff1a\u884c\u5c3e\u63d2\u5165

i\uff1a\u5149\u6807\u524d\u63d2\u5165

I\uff1a\u884c\u9996\u63d2\u5165

o\uff1a\u4e0b\u4e00\u884c\u63d2\u5165

O\uff1a\u4e0a\u4e00\u884c\u63d2\u5165

"},{"location":"tools/vim/#_3","title":"\u5220\u9664","text":"

x\uff1a\u5220\u9664\u5149\u6807\u5904\u5b57\u7b26

dd\uff1a\u5220\u9664\u6574\u884c

D\uff1a\u5220\u9664\u81f3\u884c\u672b

dw\uff1a\u4ece\u5f53\u524d\u5149\u6807\u4f4d\u7f6e\uff0c\u4e00\u76f4\u5220\u9664\u81f3\u4e0b\u4e00\u4e2a\u5355\u8bcd\u5f00\u5934

d$\uff1a\u4ece\u5f53\u524d\u5149\u6807\u4f4d\u7f6e\uff0c\u4e00\u76f4\u5220\u9664\u81f3\u884c\u672b

"},{"location":"tools/vim/#_4","title":"\u590d\u5236\u4e0e\u7c98\u8d34","text":"

p\uff1a\u5728\u5149\u6807\u4e0b\u4e00\u884c\u4f4d\u7f6e\u590d\u5236

y\uff1a\u590d\u5236\u9009\u4e2d\u5185\u5bb9\uff0c\u9700\u8981\u914d\u5408visual mode

yy\uff1a\u590d\u5236\u5f53\u524d\u884c

yw\uff1a\u590d\u5236\u4e00\u4e2a\u5355\u8bcd

"},{"location":"tools/vim/#_5","title":"\u4fee\u6539\u4e0e\u66ff\u6362","text":"

rx\uff1a\u5c06\u5f53\u524d\u5149\u6807\u5904\u5b57\u7b26\u4fee\u6539\u4e3ax

ce\uff1a\u4fee\u6539\u5355\u8bcd

:s/old/new/g\uff1a\u5c06\u5f53\u524d\u884c\u7684'old'\u5168\u90e8\u6539\u4e3a'new'

:%s/old/new/g\uff1a\u5c06\u5f53\u524d\u6587\u4ef6\u7684'old'\u5168\u90e8\u6539\u4e3a'new'

"},{"location":"tools/vim/#_6","title":"\u67e5\u627e","text":"

\u6309/\u8fdb\u5165\u67e5\u627e\u6a21\u5f0f\uff0c\u8f93\u5165\u4f60\u8981\u641c\u5bfb\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\uff1a

n\uff1a\u67e5\u627e\u4e0b\u4e00\u4e2a

N\uff1a\u67e5\u627e\u4e0a\u4e00\u4e2a

%\uff1a\u641c\u5bfb\u4e0b\u4e00\u4e2a\u5339\u914d\u7684), ], }

"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Home","text":""},{"location":"#_1","title":"\u6b22\u8fce\u6765\u5230\u6211\u7684\u9891\u9053","text":"

\u6ca1\u6709\u5b66\u672f\u7684\u6c11\u4e3b\u548c\u601d\u60f3\u7684\u81ea\u7531\uff0c\u79d1\u5b66\u5c31\u4e0d\u80fd\u7e41\u8363\u3002 \u2014\u2014\u2014\u20142003\u7248\u9ad8\u4e00\u7269\u7406\u5fc5\u4fee\u4e00\u8bfe\u672c

\u8fd9\u91cc\u4e3b\u8981\u5206\u4eab\u6211\u7684\u5b66\u4e60\u7b14\u8bb0\u548c\u5fc3\u5f97\u4f53\u4f1a\uff0c\u53c2\u8003\u8d44\u6599\u4e8e\u6bcf\u4e2a\u4e3b\u9875\u9762\u5217\u51fa\u3002

"},{"location":"#_2","title":"\u5feb\u901f\u5bfc\u822a","text":"
  • Linux

  • RTOS

  • Arm

  • \u7f16\u7a0b\u8bed\u8a00

  • \u4e91\u8ba1\u7b97

  • Tools

  • \u5176\u4ed6

"},{"location":"#inspired-by","title":"Inspired by:","text":"
  • CS\u81ea\u5b66\u6307\u5357
  • \u4e0a\u6d77\u4ea4\u901a\u5927\u5b66\u751f\u5b58\u624b\u518c
"},{"location":"arm/","title":"ARM","text":""},{"location":"arm/#table-of-contents","title":"Table of Contents","text":"
  • ARM\u67b6\u6784\u57fa\u7840\u77e5\u8bc6
  • A64\u6307\u4ee4\u96c6
"},{"location":"arm/#_1","title":"\u53c2\u8003\u8d44\u6599","text":"
  • ARM64\u4f53\u7cfb\u7ed3\u6784\u7f16\u7a0b\u4e0e\u5b9e\u8df5
  • ARMv8 Arch
  • Arm Developer Documentation
  • ARM GCC Inline Assembler CookBook
"},{"location":"arm/aarch64/","title":"ARM\u67b6\u6784\u57fa\u7840\u77e5\u8bc6","text":"

ARM\u4f53\u7cfb\u7ed3\u6784\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u5206\u4e3a\u5982\u4e0b\u4e09\u79cd\u7cfb\u5217\uff1a

  • A\u7cfb\u5217\uff1a\u9762\u5411\u6027\u80fd\u5bc6\u96c6\u578b\u7cfb\u7edf

  • R\u7cfb\u5217\uff1a\u9762\u5411\u5b9e\u65f6\u6027\u5e94\u7528

  • M\u7cfb\u5217\uff1a\u9762\u5411\u5d4c\u5165\u5f0f\u5e94\u7528

ARMv8\u662fARM\u516c\u53f8\u53d1\u5e03\u7684\u7b2c\u4e00\u4ee3\u652f\u630164\u4f4d\u5904\u7406\u5668\u7684\u6307\u4ee4\u96c6\u548c\u4f53\u7cfb\u7ed3\u6784\u3002\u5728\u6269\u514564\u4f4d\u6307\u4ee4\u96c6\u7684\u540c\u65f6\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5bf932\u4f4d\u6307\u4ee4\u96c6\u7684\u652f\u6301\u3002\u5b83\u8fd8\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7684\u7279\u6027\uff1a

  • \u8d85\u5927\u7269\u7406\u5730\u5740\u7a7a\u95f4\uff0c\u63d0\u4f9b\u8d85\u8fc74GB\u7269\u7406\u5185\u5b58\u7684\u8bbf\u95ee\u3002

  • 64\u4f4d\u5bbd\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u3002

  • \u81ea\u52a8\u4e8b\u4ef6\u4fe1\u53f7\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8282\u80fd\u3001\u9ad8\u6027\u80fd\u7684\u81ea\u65cb\u9501\u3002

  • 31\u4e2a64\u4f4d\u5bbd\u7684\u901a\u7528\u5bc4\u5b58\u5668\uff0c\u53ef\u4ee5\u51cf\u5c11\u5bf9\u6808\u7684\u8bbf\u95ee\u3002

  • \u63d0\u4f9b16KB\u548c64KB\u7684\u9875\uff0c\u6709\u52a9\u4e8e\u964d\u4f4eTLB\u7684\u4e3a\u547d\u4e2d\u7387\u3002

  • \u5168\u65b0\u7684\u5f02\u5e38\u5904\u7406\u6a21\u578b\uff0c\u964d\u4f4e\u4e86\u64cd\u4f5c\u7cfb\u7edf\u548c\u865a\u62df\u5316\u7684\u5b9e\u73b0\u590d\u6742\u5ea6\u3002

  • \u5168\u65b0\u7684\u52a0\u8f7d-\u83b7\u53d6\u6307\u4ee4\u3001\u5b58\u50a8-\u91ca\u653e\u6307\u4ee4\uff0c\u4e13\u95e8\u4e3aC++11\u3001C11\u4ee5\u53caJAVA\u5185\u5b58\u6a21\u578b\u8bbe\u8ba1\u3002

  • NEON\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u9ad8\u7ea7SIMD\uff0c\u4f7f\u5f97SIMD\u77e2\u91cf\u5316\u80fd\u591f\u5e94\u7528\u4e8e\u66f4\u5e7f\u6cdb\u7684\u7b97\u6cd5\u96c6\uff0c\u4f8b\u5982\u79d1\u5b66\u8ba1\u7b97\u3001\u9ad8\u6027\u80fd\u8ba1\u7b97(HPC)\u548c\u8d85\u7ea7\u8ba1\u7b97\u673a\u3002

\u5e38\u89c1\u7684\u91c7\u7528ARMv8\u4f53\u7cfb\u7ed3\u6784\u7684\u5904\u7406\u5668\u5185\u6838\u6709\uff1a

  • Cortex-A53

  • Cortex-A57

  • Cortex-A72

ARMv8\u4f53\u7cfb\u5b9a\u4e49\u4e86\u4e24\u79cd\u6267\u884c\u72b6\u6001\uff1a

AArch64\uff1a64\u4f4d\u7684\u6267\u884c\u72b6\u6001\u3002

  • \u63d0\u4f9b31\u4e2a64\u4f4d\u7684\u901a\u7528\u5bc4\u5b58\u5668\u3002

  • \u63d0\u4f9b64\u4f4d\u7684\u7a0b\u5e8f\u8ba1\u6570\u6307\u9488\u5bc4\u5b58\u5668(PC)\u3001\u6808\u6307\u9488\u5bc4\u5b58\u5668\uff08SP\uff09\u4ee5\u53ca\u5f02\u5e38\u94fe\u63a5\u5bc4\u5b58\u5668(ELR)\u3002

  • \u63d0\u4f9bA64\u6307\u4ee4\u96c6\u3002

  • \u5b9a\u4e49ARMv8\u5f02\u5e38\u6a21\u578b\uff0c\u652f\u63014\u4e2a\u5f02\u5e38\u7b49\u7ea7\u2014\u2014EL0 ~ EL3\u3002

  • \u63d0\u4f9b\u4e00\u7ec4\u5904\u7406\u5668\u72b6\u6001\u5bc4\u5b58\u5668(PSTATE)\u4fdd\u5b58PE\u7684\u72b6\u6001\u3002

AArch32\uff1a32\u4f4d\u7684\u6267\u884c\u72b6\u6001\u3002

  • \u63d0\u4f9b13\u4e2a32\u4f4d\u7684\u901a\u7528\u5bc4\u5b58\u5668\uff0c\u518d\u52a0\u4e0aPC\u3001SP\u3001LR\u3002

  • \u652f\u6301A32\u548cT32(Thumb\u6307\u4ee4\u96c6)\u6307\u4ee4\u96c6\u3002

  • \u652f\u6301ARMv7-A\u5f02\u5e38\u6a21\u578b\u3002

  • \u5b9a\u4e49\u4e00\u7ec4PSTATE\u4fdd\u5b58PE\u7684\u72b6\u6001\u3002

ARMv8\u4f53\u7cfb\u7ed3\u6784\u6839\u636e\u4e0d\u540c\u7684\u6267\u884c\u72b6\u6001\u63d0\u4f9b\u4e0d\u540c\u6307\u4ee4\u96c6\u7684\u652f\u6301\uff1a

  • A64\u6307\u4ee4\u96c6\uff1a\u8fd0\u884c\u5728AArch64\u72b6\u6001\u4e0b\uff0c\u63d0\u4f9b64\u4f4d\u6307\u4ee4\u96c6

  • A32\u6307\u4ee4\u96c6\uff1a\u8fd0\u884c\u5728AArch32\u72b6\u6001\u4e0b\uff0c\u63d0\u4f9b32\u4f4d\u6307\u4ee4\u96c6

  • T32\u6307\u4ee4\u96c6\uff1a\u8fd0\u884c\u5728AArch32\u72b6\u6001\u4e0b\uff0c\u63d0\u4f9b16\u4f4d\u548c32\u4f4d\u6307\u4ee4\u96c6\u3002

\u5728AArch64\u72b6\u6001\u4e0b\uff0c\u7cfb\u7edf\u5bc4\u5b58\u5668\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u7b49\u7ea7\u63d0\u4f9b\u4e0d\u540c\u7684\u53d8\u79cd\u5bc4\u5b58\u5668\u2014\u2014<register_name>_ELx\u3002

"},{"location":"arm/aarch64/#armv8","title":"ARMv8\u5bc4\u5b58\u5668","text":"

AArch64\u6267\u884c\u72b6\u6001\u652f\u630131\u4e2a64\u4f4d\u7684\u901a\u7528\u5bc4\u5b58\u5668\uff1aX0 ~ X30\u3002\u53ef\u4ee5\u4f7f\u7528W\u6765\u8868\u793a\u4f4e32\u4f4d\u7684\u6570\u636e\uff0c\u5982W0\u8868\u793aX0\u5bc4\u5b58\u5668\u7684\u4f4e32\u4f4d\u3002\u4eceW\u5bc4\u5b58\u5668\u8bfb\u53d6\u65f6\uff0c\u5ffd\u7565\u76f8\u5e94X\u5bc4\u5b58\u5668\u9ad832\u4f4d\uff0c\u5e76\u4fdd\u6301\u5176\u5b83\u4e0d\u53d8\u3002\u5199\u5165W\u5bc4\u5b58\u5668\u65f6\uff0c\u5c06X\u5bc4\u5b58\u5668\u7684\u9ad832\u4f4d\u8bbe\u7f6e\u4e3a\u96f6\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5c060xFFFFFFFF\u5199\u5165W0\u4f1a\u5c06X0\u8bbe\u7f6e\u4e3a0x00000000FFFFFFFF\u3002

PSTATE\u5bc4\u5b58\u5668\u7528\u6765\u8868\u793a\u5f53\u524d\u5904\u7406\u5668\u72b6\u6001\u3002

\u7279\u6b8a\u5bc4\u5b58\u5668\uff1a

1.\u96f6\u5bc4\u5b58\u5668

ARMv8\u4f53\u7cfb\u7ed3\u6784\u63d0\u4f9b\u4e24\u4e2a\u96f6\u5bc4\u5b58\u5668\u2014\u2014WZR\uff0cXZR\uff0c\u5bc4\u5b58\u5668\u7684\u5185\u5bb9\u5168\u662f0\u3002

2.PC\u6307\u9488\u5bc4\u5b58\u5668

\u7528\u6765\u6307\u5411\u5f53\u524d\u8fd0\u884c\u6307\u4ee4\u7684\u4e0b\u4e00\u6761\u6307\u4ee4\u7684\u5730\u5740\u3002

3.SP\u6307\u9488\u5bc4\u5b58\u5668

\u7528\u6765\u6307\u5411\u5f53\u524d\u6808\u5e27\u7684\u6808\u9876\u3002\u6bcf\u4e2a\u5f02\u5e38\u7b49\u7ea7\u90fd\u6709\u4e00\u4e2a\u4e13\u95e8\u7684SP\u5bc4\u5b58\u5668\u3002

4.\u5907\u4efd\u7a0b\u5e8f\u72b6\u6001\u5bc4\u5b58\u5668(SPSR)

\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u5904\u7406\u5668\u4f1a\u5c06PSTATE\u5bc4\u5b58\u5668\u7684\u503c\u6682\u65f6\u4fdd\u5b58\u5230SPSR\u4e2d\u3002

5.\u5f02\u5e38\u94fe\u63a5\u5bc4\u5b58\u5668(ELR)

\u5b58\u653e\u5f02\u5e38\u8fd4\u56de\u5730\u5740\u3002

6.CuurentEL\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2dEL\u5b57\u6bb5\u7684\u503c\uff0c\u4fdd\u5b58\u4e86\u5f53\u524d\u5f02\u5e38\u7b49\u7ea7\uff0c\u53ef\u4ee5\u901a\u8fc7MRS\u6307\u4ee4\u8bfb\u53d6\u3002

7.DAIF\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2d{D, A,I,F}\u5b57\u6bb5\u7684\u503c\u3002

8.SPSel\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2dSP\u5b57\u6bb5\u7684\u503c\u3002

9.PAN\u5bc4\u5b58\u5668

\u8868\u793aPSTAET\u5bc4\u5b58\u5668\u4e2dPAN\u5b57\u6bb5\u7684\u503c\u3002

10.UAO\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2dUAO\u5b57\u6bb5\u7684\u503c\u3002

11.NZCV\u5bc4\u5b58\u5668

\u8868\u793aPSTATE\u5bc4\u5b58\u5668\u4e2d{N, Z, C, V}\u5b57\u6bb5\u7684\u503c\u3002

"},{"location":"arm/aarch64/#_1","title":"\u5f02\u5e38\u7b49\u7ea7","text":"
  • EL0\uff1a\u7528\u6237\u7279\u6743\uff0c\u8fd0\u884c\u666e\u901a\u7528\u6237\u7a0b\u5e8f

  • EL1\uff1a\u7cfb\u7edf\u7279\u6743\uff0c\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838

  • EL3\uff1a\u8fd0\u884c\u865a\u62df\u673a\u76d1\u89c6\u5668

  • EL4\uff1a\u8fd0\u884c\u5b89\u5168\u76d1\u63a7\u5668

\u5f02\u5e38\u7ea7\u522b\u4e4b\u95f4\u7684\u79fb\u52a8\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219\uff1a

  • \u79fb\u52a8\u5230\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\uff0c\u4f8b\u5982\u4eceEL0\u5230EL1\uff0c\u8868\u793a\u8f6f\u4ef6\u589e\u52a0\u6267\ufa08\u7279\u6743\u3002

  • \u4e0d\u80fd\u5c06\u5f02\u5e38\u5904\u7406\u964d\u4f4e\u5230\u8f83\u4f4e\u7684\u5f02\u5e38\u7ea7\u522b\u3002

  • EL0\u7ea7\u522b\u6ca1\u6709\u5f02\u5e38\u5904\uf9e4\uff0c\u5fc5\u987b\u5728\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\u5904\uf9e4\u5f02\u5e38\u3002

  • \u5f02\u5e38\u5bfc\u81f4\u7a0b\u5e8f\u6d41\u7a0b\u53d1\u751f\u53d8\u5316\u3002\u5f02\u5e38\u5904\uf9e4\u7a0b\u5e8f\u7684\u6267\ufa08\u4ee5\u9ad8\u4e8eEL0\u7684\u5f02\u5e38\u7ea7\u522b\u4ece\u4e0e\u6240\u91c7\u53d6\u7684\u5f02\u5e38\u76f8\u5173\u7684\u5df2\u5b9a\u4e49\u5411\uf97e\u5f00\u59cb\u3002\u4f8b\u5916\u60c5\u51b5\u5305\u62ec\uff1aIRQ\u548cFIQ\u7b49\u4e2d\u65ad\u3002\u5185\u5b58\u7cfb\u7edf\u4e2d\u6b62\u3002\u672a\u5b9a\u4e49\u7684\u6307\u4ee4\u3002\u7cfb\u7edf\u8c03\u7528\u3002\u8fd9\u4e9b\u5141\u8bb8\u975e\u7279\u6743\u8f6f\u4ef6\u5bf9\u64cd\u4f5c\u7cfb\u7edf \u5b89\u5168\u76d1\u89c6\u5668\u6216\u7ba1\uf9e4\u7a0b\u5e8f\u9677\u9631\u3002

  • \u901a\u8fc7\u6267\ufa08ERET\u6307\u4ee4\u6765\u7ed3\u675f\u5f02\u5e38\u5904\uf9e4\u5e76\u8fd4\u56de\u5230\u4e0a\u4e00\u4e2a\u5f02\u5e38\u7ea7\u522b\u3002

  • \u4ece\u5f02\u5e38\u8fd4\u56de\u53ef\u4ee5\u4fdd\u6301\u76f8\u540c\u7684\u5f02\u5e38\u7ea7\u522b\u6216\u8fdb\u5165\u8f83\u4f4e\u7684\u5f02\u5e38\u7ea7\u522b\u3002\u5b83\uf967\u80fd\u79fb\u52a8\u5230\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\u3002

  • \u5b89\u5168\u72b6\u6001\u786e\u5b9e\u4f1a\u968f\u7740\u5f02\u5e38\u7ea7\u522b\u7684\u53d8\u5316\u800c\u53d8\u5316\uff0c\u9664\u975e\u4eceEL3\u91cd\u65b0\u8c03\u6574\u5230\u975e\u5b89\u5168\u72b6\u6001

\u8981\u5728\u76f8\u540c\u7684\u5f02\u5e38\u7ea7\u522b\u4e4b\u95f4\u5207\u6362\u6267\ufa08\u72b6\u6001\uff0c\u5fc5\u987b\u5207\u6362\u5230\uf901\u9ad8\u7684\u5f02\u5e38\u7ea7\u522b\uff0c\u7136\u540e\u8fd4\u56de\u5230\u539f\u59cb\u7684\u5f02\u5e38\u7ea7\u522b\u3002\u4f8b\u5982\uff0c\u670932\u4f4d\u548c64\u4f4d\u5e94\u7528\u7a0b\u5e8f\u572864\u4f4d\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8fd0\ufa08\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c32\u4f4d\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u6267\ufa08\u5e76\u751f\u6210\u4e3b\u7ba1\u8c03\u7528(SVC)\u6307\u4ee4\uff0c\u6216\u63a5\u6536\u4e2d\u65ad\uff0c\u4ece\u800c\u5bfc\u81f4\u5207\u6362\u5230L1\u548cAArch64\u3002\u7136\u540e\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u6267\ufa08\u4efb\u52a1\u5207\u6362\u5e76\u8fd4\u56de\u5230AArch64\u4e2d\u7684EL0\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u610f\u5473\u7740\u60a8\uf967\u80fd\u62e5\u6709\u6df7\u5408\u768432\u4f4d\u548c64\u4f4d\u5e94\u7528\u7a0b\u5e8f\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u76f4\u63a5\u7684\u8c03\u7528\u65b9\u5f0f\u3002

"},{"location":"arm/barrier/","title":"\u5185\u5b58\u5c4f\u969c","text":""},{"location":"arm/instructions/","title":"A64\u6307\u4ee4\u96c6","text":"

ARMv8\u589e\u52a0\u4e86\u65b0\u768464\u4f4d\u6307\u4ee4\u96c6\uff0c\u5b83\u53ef\u4ee5\u5904\u740664\u4f4d\u5bbd\u7684\u5bc4\u5b58\u5668\u548c\u6570\u636e\u5e76\u4e14\u4f7f\u752864\u4f4d\u7684\u6307\u9488\u6765\u8bbf\u95ee\u5185\u5b58\uff0c\u4f46\u662f\u5176\u6307\u4ee4\u96c6\u7684\u6307\u4ee4\u5bbd\u5ea6\u662f32\u4f4d\u3002ARMv8\u517c\u5bb9A32\u6307\u4ee4\u96c6\uff0c\u4f46A64\u6307\u4ee4\u96c6\u548cA32\u6307\u4ee4\u96c6\u662f\u4e0d\u517c\u5bb9\u7684\u3002

ARMv8\u652f\u6301\u4e24\u79cd\u6267\u884c\u72b6\u6001\u2014\u2014AArch64\u548cAArch32\u3002AArch64\u72b6\u6001\u7528\u4e8e\u6267\u884c64\u4f4d\u6307\u4ee4\uff0c\u800cAArch32\u72b6\u6001\u7528\u4e8e\u6267\u884c32\u4f4d\u6307\u4ee4\u3002

"},{"location":"cloud/","title":"\u4e91\u8ba1\u7b97","text":""},{"location":"cloud/docker/","title":"\u5bb9\u5668\u6280\u672f","text":""},{"location":"linux/","title":"Linux\u5185\u6838\u4f53\u7cfb","text":""},{"location":"linux/#table-of-contents","title":"Table of Contents","text":"
  • \u5185\u6838\u5256\u6790
  • \u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
  • \u6027\u80fd\u8c03\u4f18
  • ebpf
"},{"location":"linux/#_1","title":"\u53c2\u8003\u8d44\u6599","text":"
  • \u8da3\u8c08Linux\u64cd\u4f5c\u7cfb\u7edf

  • \u6df1\u5165Linux\u5185\u6838\u67b6\u6784

  • \u7cbe\u901a\u5d4c\u5165\u5f0fLinux\u7f16\u7a0b

  • \u6df1\u5165\u7406\u89e3LINUX\u5185\u6838(\u7b2c3\u7248)

  • Linux\u5185\u6838\u8bbe\u8ba1\u4e0e\u5b9e\u73b0(\u7b2c3\u7248)

  • UNIX\u73af\u5883\u9ad8\u7ea7\u7f16\u7a0b\uff08\u7b2c3\u7248)

  • Linux\u547d\u4ee4\u884c\u4e0eshell\u811a\u672c\u7f16\u7a0b\u5927\u5168\uff08\u7b2c4\u7248)

  • \u91ce\u706bLinux\u5f00\u53d1\u5b9e\u6218\u6307\u5357

  • \u8717\u58f3\u79d1\u6280

  • Linux\u6027\u80fd\u4f18\u5316\u5b9e\u6218

"},{"location":"linux/drivers/","title":"\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f","text":""},{"location":"linux/drivers/#table-of-contents","title":"Table of Contents","text":""},{"location":"linux/drivers/#_2","title":"\u57fa\u7840\u77e5\u8bc6","text":"
  • \u8bbe\u5907\u6811
  • \u8bbe\u5907\u6a21\u578b
  • \u5e73\u53f0\u8bbe\u5907\u9a71\u52a8
  • \u76f4\u63a5\u5185\u5b58\u8bbf\u95ee
"},{"location":"linux/drivers/#_3","title":"\u5b57\u7b26\u3001\u5757\u548c\u7f51\u7edc","text":"
  • \u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
  • \u5757\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
  • \u7f51\u7edc\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f
"},{"location":"linux/drivers/#_4","title":"\u4f4e\u901f\u8bbe\u5907\u9a71\u52a8","text":"
  • Pinctrl\u5b50\u7cfb\u7edf
  • GPIO\u5b50\u7cfb\u7edf
  • I2C\u5b50\u7cfb\u7edf
  • SPI\u5b50\u7cfb\u7edf
  • UART\u5b50\u7cfb\u7edf
  • Input\u5b50\u7cfb\u7edf
"},{"location":"linux/drivers/#_5","title":"\u9ad8\u901f\u8bbe\u5907\u9a71\u52a8","text":"
  • \u4ee5\u592a\u7f51
  • PCI
  • USB
  • MMC
  • WIFI
  • \u84dd\u7259
"},{"location":"linux/drivers/blkdev/","title":"\u5757\u8bbe\u5907\u9a71\u52a8","text":""},{"location":"linux/drivers/chardev/","title":"\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f","text":"

\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u6846\u67b6\u5982\u56fe\u6240\u793a\uff1a

\u5728\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u8bbe\u5907\u7684\u65f6\u5019\uff0c\u9996\u5148\u5e94\u8be5\u5411\u5185\u6838\u7533\u8bf7\u4e00\u4e2a\u8bbe\u5907\u53f7\u3002\u62ff\u5230\u8bbe\u5907\u53f7\u4e4b\u540e\uff0c\u9700\u8981\u624b\u52a8\u5b9e\u73b0file_operation\u7ed3\u6784\u4f53\u4e2d\u7684\u51fd\u6570\u6307\u9488\uff0c\u5e76\u4fdd\u5b58\u5230cdev\u7ed3\u6784\u4f53\u4e2d\u3002\u7136\u540e\u4f7f\u7528cdev_add()\u51fd\u6570\u6ce8\u518ccdev\u3002

\u6ce8\u9500\u8bbe\u5907\u65f6\u9700\u8981\u91ca\u653e\u5185\u6838\u4e2d\u7684cdev\uff0c\u5e76\u4e14\u5f52\u8fd8\u7533\u8bf7\u7684\u8bbe\u5907\u53f7\u3002

"},{"location":"linux/drivers/chardev/#_2","title":"\u5feb\u901f\u53c2\u8003","text":"
#include <linux/types.h>\n\ndev_t devID\n\nint MAJOR(dev_t dev)\n\nint MINOR(dev_t dev)\n\ndev_t MKDEV(unsigned int major, unsigned int minor)\n
#include <linux/fs.h>\n\nint register_chrdev_region(dev_t first, unsigned int count, char *name)\n\nint alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name)\n\nvoid unregister_chrdev_region(dev_t first, unsigned int count)\n\nint register_chrdev(unsigned int major, const char *name, struct file_operations *fops)\n\nint unregister_chrdev(unsigned int major, const char *name)\n\nstruct file_operations\n\nstruct file\n\nstruct inode\n
#include <linux/cdev.h>\n\nstruct cdev *cdev_alloc(void)\n\nvoid cdev_init(struct cdev *dev, dev_t num, unsigned int count)\n\nint cdev_add(struct cdev *cdev, dev_t num, unsigned int count)\n\nvoid cdev_del(struct cdev *dev)\n
#include <linux/kernel.h>\n\ncontainer_of(pointer, type, field)\n\n#include <asm/uaccess.h>\n\nunsigned long copy_from_user(void *to, const void *from, unsigned long count)\n\nunsigned long copy_to_user(void *to, const void *from, unsigned long count)\n
"},{"location":"linux/drivers/chardev/#_3","title":"\u8bbe\u5907\u53f7\u521d\u59cb\u5316","text":"

\u4e3b\u8bbe\u5907\u53f7\u6807\u8bc6\u8bbe\u5907\u5bf9\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u73b0\u4ee3Linux\u5185\u6838\u5141\u8bb8\u591a\u4e2a\u9a71\u52a8\u5171\u4eab\u4e3b\u8bbe\u5907\u53f7\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u6b21\u8bbe\u5907\u53f7\u7528\u4e8e\u6b63\u786e\u786e\u5b9a\u8bbe\u5907\u6587\u4ef6\u6240\u6307\u5411\u7684\u8bbe\u5907\u3002\u5185\u6838\u7528dev_t\u7c7b\u578b\u6765\u8868\u6751\u8bbe\u5907\u7f16\u53f7\u2014\u2014\u5305\u62ec\u4e3b\u8bbe\u5907\u53f7\u548c\u6b21\u8bbe\u5907\u53f7\u3002\u8981\u83b7\u53d6\u4e3b\u3001\u6b21\u8bbe\u5907\u53f7\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b8fMAJOR\u3001MINOR\u6765\u64cd\u4f5c\u3002

"},{"location":"linux/drivers/chardev/#_4","title":"\u8bbe\u5907\u53f7\u7684\u6ce8\u518c\u4e0e\u5378\u8f7d(\u4e0d\u63a8\u8350\u7684\u505a\u6cd5)","text":"
  • \u6ce8\u518c
    int register_chrdev(unsigned int major, const char *name, struct file_operations *fops)\n

\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6ce8\u518c\u65f6\u9700\u8981\u624b\u52a8\u6307\u5b9a\u4e3b\u8bbe\u5907\u53f7\uff0c\u56e0\u6b64\u4f60\u5fc5\u987b\u4e8b\u5148\u77e5\u9053\u54ea\u4e2a\u4e3b\u8bbe\u5907\u53f7\u6ca1\u6709\u88ab\u5360\u7528\uff0c\u5b9e\u9645\u5f00\u53d1\u4e2d\u5e76\u4e0d\u63a8\u8350\uff0c\u5e76\u4e14\u8be5\u51fd\u6570\u8fd8\u4e00\u6b21\u6027\u5360\u7528\u4e86\u4e3b\u8bbe\u5907\u53f7\u4e0b\u7684\u5168\u90e8\u6b21\u8bbe\u5907\u53f7\u3002

\u5982\u679c\u4e00\u5b9a\u8981\u4f7f\u7528\uff0c\u5fc5\u987b\u8981\u68c0\u67e5\u4fdd\u5b58\u5728inode\u7ed3\u6784\u4e2d\u7684\u6b21\u8bbe\u5907\u53f7\u662f\u5426\u88ab\u4f7f\u7528\u3002

  • \u5378\u8f7d
    int unregister_chrdev(unsigned int major, const char *name)\n
"},{"location":"linux/drivers/chardev/#_5","title":"\u8bbe\u5907\u53f7\u7684\u6ce8\u518c\u4e0e\u5378\u8f7d(\u63a8\u8350\u7684\u505a\u6cd5)","text":"
  • \u6ce8\u518c
int register_chrdev_region(dev_t first, unsigned count, const char *name)\n

first\uff1a\u8981\u5206\u914d\u7684\u8bbe\u5907\u7f16\u53f7\u7684\u8d77\u59cb\u503c\u3002

count\uff1a\u8bf7\u6c42\u7684\u8fde\u7eed\u8bbe\u5907\u7f16\u53f7\u4e2a\u6570\u3002

name\uff1a\u4e0e\u8be5\u7f16\u53f7\u76f8\u5173\u7684\u8bbe\u5907\u540d\u79f0\uff0c\u5b83\u5c06\u51fa\u73b0\u5728/proc/devices\u548csysfs\u4e2d\u3002

\u5982\u679c\u6211\u4eec\u63d0\u524d\u5c31\u77e5\u9053\u6240\u9700\u8981\u7684\u8bbe\u5907\u7f16\u53f7\uff0c\u90a3\u4e48\u4f7f\u7528register_chrdev_region()\u5c31\u591f\u4e86\u3002\u4f46\u662f\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a\uff0c\u800c\u5e94\u8be5\u4f7f\u7528\u52a8\u6001\u5206\u914d\u51fd\u6570\uff1a

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)\n

dev\uff1a\u4fdd\u5b58\u7528\u6765\u7533\u8bf7\u7684\u8bbe\u5907\u53f7\u3002

baseminor\uff1a\u6b21\u8bbe\u5907\u53f7\u7684\u8d77\u59cb\u503c\uff0c\u4e00\u822c\u4e3a0\u3002

\u9a71\u52a8\u7a0b\u5e8f\u5e94\u8be5\u59cb\u7ec8\u4f7f\u7528alloc_chrdev_region\u800c\u4e0d\u662fregister_chrdev_region \u2014\u2014\u2014\u2014\u300aLinux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8fP50\u300b

\u52a8\u6001\u5206\u914d\u7684\u7f3a\u70b9\u662f\u65e0\u6cd5\u9884\u5148\u521b\u5efa\u8bbe\u5907\u8282\u70b9\uff08mknod\uff09\uff0c\u4e0d\u8fc7\u8fd9\u662f\u5c0f\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7528awk\u5de5\u5177\u4ece/proc/devices\u4e2d\u8bfb\u53d6\u4fe1\u606f\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u52a8\u6001\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u3002

  • \u5378\u8f7d

\u4e0a\u9762\u4e24\u79cd\u65b9\u6cd5\u6ce8\u518c\u7684\u8bbe\u5907\u7edf\u4e00\u7528\u4ee5\u4e0b\u51fd\u6570\u5378\u8f7d\uff0c\u8be5\u51fd\u6570\u901a\u5e38\u5728\u6e05\u7406\u51fd\u6570\u4e2d\u8c03\u7528\uff1a

void unregister_chrdev_region(dev_t first, unsigned count)\n

\u8fd9\u91cc\u6211\u4eec\u7ed9\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u540d\u5b57\u4e3a\"test\"\u7684\u8bbe\u5907\u3002

int major;\nint minor;\ndev_t devid;\n\nif(major){\n    /*\u5982\u679c\u6307\u5b9a\u4e86major*/\n    devid = MKDEV(major, 0);\n    register_chrdev_region(devid, 1, \"test\");\n}else {\n    /*\u5982\u679c\u6ca1\u6709\u6307\u5b9a*/\n    alloc_chrdev_region(&devid, 0, 1, \"test\");\n    major = MAJOR(devid);\n    minor = MINOR(devid);\n}\n

\u5bf9\u8bbe\u5907\u53f7\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u5230\u4e86\u4e09\u4e2a\u5b8f\u5b9a\u4e49\uff0cMKDEV, MAJOR, MINOR\u3002\u5f53\u7ed9\u5b9a\u4e3b\u8bbe\u5907\u53f7\u65f6\uff0c\u4f7f\u7528MKDEV\u6765\u6784\u5efa\u5b8c\u6574\u7684devID\uff0c\u6b21\u8bbe\u5907\u53f7\u5219\u4e00\u822c\u9009\u62e90\u3002

"},{"location":"linux/drivers/chardev/#_6","title":"\u5b57\u7b26\u8bbe\u5907\u7684\u6ce8\u518c","text":"

\u5185\u6838\u4f7f\u7528struct cdev\u7ed3\u6784\u6765\u8868\u793a\u5b57\u7b26\u8bbe\u5907\uff0c\u5728\u5b9e\u73b0\u4e86file_operations\u7ed3\u6784\u4f53\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528cdev_init()\u51fd\u6570\u6765\u5c06\u6587\u4ef6\u64cd\u4f5c\u6307\u9488\u4e0e\u4f60\u6ce8\u518c\u7684\u5b57\u7b26\u8bbe\u5907\u76f8\u5173\u8054\u3002

void cdev_init(struct cdev *cdev, const struct file_operations *fops)\n

\u6ce8\u518c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u8c03\u7528cdev_add()\u51fd\u6570\u5411\u5185\u6838\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u8bbe\u5907\uff0ccdev_del()\u51fd\u6570\u7528\u6765\u5220\u9664\u3002

int cdev_add(struct cdev *p, dev_t dev, unsigned count)\nvoid cdev_del(struct cdev *p)\n
"},{"location":"linux/drivers/chardev/#_7","title":"\u5185\u5b58\u6620\u5c04","text":""},{"location":"linux/drivers/chardev/#_8","title":"\u9a71\u52a8\u5c42\u7684\u64cd\u4f5c","text":"

\u7531\u4e8eLinux\u6709MMU\u6a21\u5757\uff0c\u56e0\u6b64\u65e0\u6cd5\u8bbf\u95ee\u771f\u5b9e\u7684\u7269\u7406\u5730\u5740\uff0c\u53ea\u80fd\u901a\u8fc7\u865a\u62df\u5730\u5740\u8bbf\u95ee\u786c\u4ef6\u5916\u8bbe\u3002\u6240\u4ee5\u9700\u8981\u6709\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u5728\u7269\u7406\u5730\u5740\u548c\u865a\u62df\u5730\u5740\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002

\u5173\u4e8e\u5982\u4f55\u83b7\u53d6\u786c\u4ef6\u7684\u7269\u7406\u5730\u5740\uff0c\u8bf7\u53c2\u8003\u8bbe\u5907\u6811\u3002

\u5730\u5740\u6620\u5c04\u51fd\u6570\u5982\u4e0b\uff1a

void __iomem *ioremap(phys_addr_t paddr, unsigned long size)\n

paddr\uff1a\u9700\u8981\u8bbf\u95ee\u7684\u7269\u7406\u5730\u5740\u3002

size\uff1a\u9700\u8981\u8f6c\u5316\u7684\u5927\u5c0f\u3002

\u8fd4\u56de\u503c\uff1a\u865a\u62df\u5730\u5740\u3002

\u53d6\u6d88\u5730\u5740\u6620\u5c04\u51fd\u6570\uff1a

void iounmap(void *addr)\n
"},{"location":"linux/drivers/chardev/#io","title":"I/O\u5185\u5b58\u8bbf\u95ee\u51fd\u6570","text":"

\u4f7f\u7528ioremap\u51fd\u6570\u5c06\u5bc4\u5b58\u5668\u7269\u7406\u5730\u5740\u6620\u5c04\u5230\u865a\u62df\u5730\u5740\u540e\uff0cLinux\u63a8\u8350\u4f7f\u7528\u5185\u6838\u81ea\u5e26\u7684\u8bfb\u5199\u64cd\u4f5c\u51fd\u6570\u5bf9\u6620\u5c04\u540e\u7684\u5185\u5b58\u8fdb\u884c\u8bfb\u5199\u3002

unsigned int ioread8(void __iomem *addr)\nunsigned int iorea16(void __iomem *addr)\nunsigned int ioread32(void __iomem *addr)\n\nvoid iowrite8(u8 b, void __iomem *addr)\nvoid iowrite16(u16 b, void __iomem *addr)\nvoid iowrite32(u32 b, void __iomem *addr)\n

\u5bf9\u4e8e\u8bfbI/O\u800c\u8a00\uff0c\u8f93\u5165\u53c2\u6570\u4e3a__iomem\u7c7b\u578b\u7684\u6307\u9488\uff0c\u6307\u5411\u88ab\u6620\u5c04\u540e\u7684\u5730\u5740\uff0c\u8fd4\u56de\u503c\u4e3a\u8bfb\u53d6\u5230\u7684\u6570\u636e\uff1b\u5bf9\u4e8e\u5199I/O\u800c\u8a00\uff0c\u8f93\u5165\u53c2\u6570\u7b2c\u4e00\u4e2a\u4e3a\u8981\u5199\u5165\u7684\u6570\u636e\uff0c\u7b2c\u4e8c\u4e2a\u4e3a\u8981\u5199\u5165\u7684\u5730\u5740\uff0c\u8fd4\u56de\u503c\u4e3a\u7a7a\u3002

\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u5730\u5740\u6620\u5c04\u7684\u76f8\u5173\u64cd\u4f5c\uff1a

unsigned long pa_dr = 0x20A8000 + 0x00;\nunsigned int __iomem *va_dr;\nunsigned int val;\nva_dr = ioremap(pa_dr, 4);\nval = ioread32(va_dr);\nval &= ~(0x01 << 19);\niowrite32(val, va_dr);\n
"},{"location":"linux/drivers/chardev/#_9","title":"\u5e94\u7528\u5c42\u7684\u64cd\u4f5c","text":"

\u7531\u4e8eLinux\u5185\u6838\u7981\u6b62\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u76f4\u63a5\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\uff0c\u6211\u4eec\u53ea\u80fd\u901a\u8fc7\u5185\u6838\u63d0\u4f9b\u7684API\u51fd\u6570\u8fdb\u884c\u3002

unsigned long copy_from_user(void *to, const void *from, unsigned long count)\n\nunsigned long copy_to_user(void *to, const void *from, unsigned long count)\n
"},{"location":"linux/drivers/device_model/","title":"\u8bbe\u5907\u6a21\u578b","text":"

\u7531\u4e8eLinux\u652f\u6301\u4e16\u754c\u4e0a\u51e0\u4e4e\u6240\u6709\u7684\u3001\u4e0d\u540c\u529f\u80fd\u7684\u786c\u4ef6\u8bbe\u5907\uff0c\u5bfc\u81f4Linux\u5185\u6838\u4e2d\u6709\u4e00\u534a\u7684\u4ee3\u7801\u90fd\u662f\u8bbe\u5907\u9a71\u52a8\u3002\u968f\u7740\u786c\u4ef6\u7684\u5feb\u901f\u8fed\u4ee3\uff0c\u8bbe\u5907\u9a71\u52a8\u7684\u4ee3\u7801\u4e5f\u5728\u5feb\u901f\u589e\u957f\u3002

\u4e3a\u4e86\u964d\u4f4e\u8bbe\u5907\u7684\u591a\u6837\u6027\u5e26\u6765\u7684\u9a71\u52a8\u5f00\u53d1\u7684\u590d\u6742\u5ea6\uff0cLinux\u63d0\u51fa\u4e86\u8bbe\u5907\u6a21\u578b\uff08device model\uff09\u7684\u6982\u5ff5\uff0c\u8be5\u6a21\u578b\u5c06\u8bbe\u5907\u548c\u9a71\u52a8\u5206\u5c42\uff0c\u628a\u6211\u4eec\u7f16\u5199\u7684\u9a71\u52a8\u4ee3\u7801\u5206\u6210\u4e86\u4e24\u5757\uff1a\u8bbe\u5907\u4e0e\u9a71\u52a8\u3002\u8bbe\u5907\u8d1f\u8d23\u63d0\u4f9b\u786c\u4ef6\u8d44\u6e90\u800c\u9a71\u52a8\u8d1f\u8d23\u53bb\u4f7f\u7528\u8bbe\u5907\u63d0\u4f9b\u7684\u786c\u4ef6\u8d44\u6e90\u3002\u4e8c\u8005\u7531\u603b\u7ebf\u5173\u8054\u8d77\u6765\u3002

\u8bbe\u5907\u6a21\u578b\u901a\u8fc7\u51e0\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u53cd\u6620\u5f53\u524d\u7cfb\u7edf\u4e2d\u603b\u7ebf\u3001\u8bbe\u5907\u4ee5\u53ca\u9a71\u52a8\u5de5\u4f5c\u7684\u60c5\u51b5\uff1a

  • \u603b\u7ebf(bus)\uff1a\u603b\u7ebf\u662fCPU\u548c\u8bbe\u5907\u4e4b\u95f4\u4fe1\u606f\u4ea4\u4e92\u7684\u901a\u9053\uff0c\u6240\u6709\u7684\u8bbe\u5907\u90fd\u5e94\u8fde\u63a5\u5230\u603b\u7ebf\u4e0a\uff0c\u65e0\u8bba\u662fCPU\u5185\u90e8\u603b\u7ebf\u8fd8\u662f\u865a\u62df\u603b\u7ebf\u3002

  • \u7c7b\uff08class\uff09\uff1a\u9762\u5411\u5bf9\u8c61\u601d\u60f3\uff0c\u5c06\u76f8\u540c\u529f\u80fd\u7684\u8bbe\u5907\uff0c\u5f52\u7ed3\u5230\u4e00\u79cdclass\u7edf\u4e00\u7ba1\u7406\u3002

  • \u8bbe\u5907\uff08device\uff09\uff1a\u6302\u8f7d\u5728\u603b\u7ebf\u7684\u7269\u7406\u8bbe\u5907\u3002

  • \u9a71\u52a8\uff08driver\uff09\uff1a\u786c\u4ef6\u8bbe\u5907\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u8d1f\u8d23\u521d\u59cb\u5316\u8bbe\u5907\u4ee5\u53ca\u5b9e\u73b0\u8be5\u8bbe\u5907\u7684\u4e00\u4e9b\u63a5\u53e3\u51fd\u6570\u3002

platform bus\u662f\u5185\u6838\u4e2d\u7684\u4e00\u79cd\u865a\u62df\u603b\u7ebf\u7c7b\u578b\uff0c\u5b83\u4e0d\u662f\u7269\u7406\u4e0a\u5b58\u5728\u7684\u603b\u7ebf\uff0c\u800c\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u603b\u7ebf\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u4ee5\u4e00\u79cd\u6807\u51c6\u7684\u65b9\u5f0f\u6765\u63cf\u8ff0\u548c\u7ba1\u7406\u90a3\u4e9b\u4e0d\u901a\u8fc7\u4f20\u7edf\u7269\u7406\u603b\u7ebf\u8fde\u63a5\u7684\u8bbe\u5907\u3002

Linux\u5185\u6838\u4f7f\u7528sysfs\u6587\u4ef6\u7cfb\u7edf\u5c06\u5185\u6838\u7684\u8bbe\u5907\u9a71\u52a8\u5bfc\u51fa\u5230\u7528\u6237\u7a7a\u95f4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee/sys\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff0c\u6765\u67e5\u770b\u751a\u81f3\u63a7\u5236\u5185\u6838\u7684\u4e00\u4e9b\u9a71\u52a8\u8bbe\u5907\u3002

/sys\u6587\u4ef6\u76ee\u5f55\u8bb0\u5f55\u4e86\u5404\u4e2a\u8bbe\u5907\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5176\u4e2d\uff0c/sys/bus\u76ee\u5f55\u4e0b\u7684\u6bcf\u4e2a\u5b50\u76ee\u5f55\u90fd\u662f\u5df2\u7ecf\u6ce8\u518c\u7684\u603b\u7ebf\u7c7b\u578b\u3002\u6bcf\u4e2a\u603b\u7ebf\u7c7b\u578b\u4e0b\u8fd8\u6709\u4e24\u4e2a\u6587\u4ef6\u5939\u2014\u2014devices\u548cdrivers\uff1bdevices\u662f\u8be5\u603b\u7ebf\u7c7b\u578b\u4e0b\u7684\u6240\u6709\u8bbe\u5907\uff0c\u4ee5\u7b26\u53f7\u94fe\u63a5\u7684\u5f62\u5f0f\u6307\u5411\u771f\u6b63\u7684\u8bbe\u5907\uff08/sys/devices/\uff09\u3002\u800cdrivers\u662f\u6240\u6709\u6ce8\u518c\u5728\u8fd9\u4e2a\u603b\u7ebf\u7c7b\u578b\u4e0a\u7684\u9a71\u52a8\u3002

/sys/devices\u76ee\u5f55\u4e0b\u662f\u5168\u5c40\u7684\u8bbe\u5907\u7ed3\u6784\u4f53\u7cfb\uff0c\u5305\u542b\u4e86\u6240\u6709\u6ce8\u518c\u5728\u5404\u7c7b\u603b\u7ebf\u4e0a\u7684\u7269\u7406\u8bbe\u5907\u3002\u6240\u6709\u7684\u7269\u7406\u8bbe\u5907\u4ee5\u603b\u7ebf\u62d3\u6251\u7684\u7ed3\u6784\u6765\u663e\u793a\u3002

/sys/class\u76ee\u5f55\u4e0b\u662f\u5305\u542b\u6240\u6709\u6ce8\u518c\u5728\u5185\u6838\u4e2d\u7684\u8bbe\u5907\u7c7b\u578b\uff0c\u6309\u7167\u8bbe\u5907\u7684\u529f\u80fd\u8fdb\u884c\u5206\u7c7b\u3002\u6bd4\u5982\u9f20\u6807\u7684\u529f\u80fd\u662f\u4f5c\u4e3a\u4eba\u673a\u4ea4\u4e92\u7684\u8f93\u5165\uff0c\u4e8e\u662f\u88ab\u5f52\u7c7b\u5230/sys/class/input\u76ee\u5f55\u4e0b\u3002

\u90a3\u4e48\u201c\u603b\u7ebf-\u8bbe\u5907-\u9a71\u52a8\u201d\u662f\u5982\u4f55\u914d\u5408\u5de5\u4f5c\u7684\u5462\uff1f

\u5728\u603b\u7ebf\u4e0a\u6302\u8f7d\u4e86\u4e24\u4e2a\u94fe\u8868\uff0c\u5206\u522b\u7ba1\u7406\u8bbe\u5907\u6a21\u578b\u548c\u9a71\u52a8\u6a21\u578b\u3002\u5f53\u6211\u4eec\u5411\u7cfb\u7edf\u6ce8\u518c\u4e00\u4e2a\u8bbe\u5907\u65f6\uff0c\u4fbf\u4f1a\u5728\u8bbe\u5907\u7684\u94fe\u8868\u4e2d\u63d2\u5165\u65b0\u7684\u8bbe\u5907\u3002\u5728\u63d2\u5165\u7684\u540c\u65f6\u603b\u7ebf\u4f1a\u6267\u884cmatch()\u65b9\u6cd5\u5bf9\u65b0\u63d2\u5165\u7684\u8bbe\u5907/\u9a71\u52a8\u8fdb\u884c\u914d\u5bf9\u3002\u82e5\u914d\u5bf9\u6210\u529f\u5219\u8c03\u7528probe()\u65b9\u6cd5\u83b7\u53d6\u8bbe\u5907\u8d44\u6e90\uff0c\u5728\u79fb\u9664\u8bbe\u5907/\u9a71\u52a8\u65f6\uff0c\u8c03\u7528remove()\u65b9\u6cd5\u3002

"},{"location":"linux/drivers/device_model/#kobjectktypekset","title":"kobject\u3001ktype\u548ckset","text":"

kobject\u662fLinux\u8bbe\u5907\u6a21\u578b\u7684\u57fa\u7840\uff0c\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u3001\u7edf\u4e00\u7684\u5bf9\u5927\u91cf\u786c\u4ef6\u8bbe\u5907\u7684\u63cf\u8ff0\u3002\u5b83\u4e3b\u8981\u63d0\u4f9b\u4ee5\u4e0b\u529f\u80fd\uff1a

  1. \u901a\u8fc7parent\u6307\u9488\uff0c\u5c06\u6240\u6709kobject\u4ee5\u6811\u72b6\u7ed3\u6784\u7684\u5f62\u5f0f\u7ec4\u5408\u8d77\u6765\u3002
  2. \u4f7f\u7528\u5f15\u7528\u8ba1\u6570kref\uff0c\u6765\u8bb0\u5f55kobject\u88ab\u5f15\u7528\u7684\u6b21\u6570\uff0c\u5728\u8ba1\u6570\u4e3a0\u65f6\u91ca\u653e\u5b83\u3002
  3. \u548csysfs\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u914d\u5408\uff0c\u5c06\u6bcf\u4e00\u4e2akobject\u7684\u7279\u6027\uff0c\u4ee5\u6587\u4ef6\u7684\u5f62\u5f0f\u5f00\u653e\u7ed9\u7528\u6237\u7a7a\u95f4\u67e5\u8be2\u3002
  4. \u901a\u8fc7uevent\u673a\u5236\uff0c\u5c06\u70ed\u63d2\u62d4\u4e8b\u4ef6\uff08\u6bd4\u5982\u4e00\u4e2a\u8bbe\u5907\u901a\u8fc7USB\u8fde\u63a5\u5230\u7cfb\u7edf\uff09\u901a\u77e5\u7528\u6237\u7a7a\u95f4\u3002

\u5185\u6838\u5f88\u5c11\u5355\u72ec\u521b\u5efakobject\u5bf9\u8c61\uff0c\u800c\u662f\u5c06\u5176\u4f5c\u4e3a\u9876\u5c42\u57fa\u7c7b\uff08C\u8bed\u8a00\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u7684\u673a\u5236\uff09\uff0c\u5d4c\u5165\u5230\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u3002\u6bd4\u5982struct cdev\u7ed3\u6784\u4f53\uff1a

struct cdev {\n    struct kobject kobj;\n    struct module *owner;\n    struct file_opeartions *ops;\n    struct list_head list;\n    dev_t dev;\n    unsigned int count;\n};\n

\u5982\u6b64\u4e00\u6765\uff0c\u8981\u4f7f\u7528kobject\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8bbf\u95eecdev.kobj\u5c31\u53ef\u4ee5\u3002\u5f53\u6211\u4eec\u5df2\u77e5\u4e00\u4e2akobject\u6307\u9488\uff0c\u53ef\u4ee5\u901a\u8fc7container_of\u5b8f\u7684\u65b9\u5f0f\u83b7\u53d6\u4e0a\u5c42\u6570\u636e\u7ed3\u6784\u7684\u6307\u9488\uff1a

struct cdev *device = container_of(kp, struct cdev, kobj);\n

\u8bbe\u5907\u9a71\u52a8\u6a21\u578b\u7684\u57fa\u672c\u5143\u7d20\u6709\u4e09\u4e2a\uff1a

  • kobject\uff1asysfs\u4e2d\u7684\u4e00\u4e2a\u76ee\u5f55\uff0c\u8868\u793a\u57fa\u672c\u9a71\u52a8\u5bf9\u8c61\u3002
  • kset\uff1a\u4e00\u4e2a\u7279\u6b8a\u7684kobject\uff0c\u7528\u6765\u7ba1\u7406\u7c7b\u4f3c\u7684kobject\u3002
  • ktype\uff1a\u76ee\u5f55\u4e0bkobject\u5c5e\u6027\u6587\u4ef6\u64cd\u4f5c\u7684\u63a5\u53e3\u3002

kobject\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct kobject {\n    const char      *name;\n    struct list_head    entry;\n    struct kobject      *parent;\n    struct kset     *kset;\n    struct kobj_type    *ktype;\n    struct kernfs_node  *sd; /* sysfs directory entry */\n    struct kref     kref;\n    unsigned int state_initialized:1;\n    unsigned int state_in_sysfs:1;\n    unsigned int state_add_uevent_sent:1;\n    unsigned int state_remove_uevent_sent:1;\n    unsigned int uevent_suppress:1;\n};\n

name\uff1akobject\u7684\u540d\u79f0\uff0c\u540c\u65f6\u4e5f\u662fsysfs\u4e2d\u7684\u76ee\u5f55\u540d\u79f0\u3002\u5f53kobject\u6dfb\u52a0\u5230\u5185\u6838\u65f6\uff0c\u9700\u8981\u6839\u636ename\u6ce8\u518c\u5230sysfs\u4e2d\u3002

entry\uff1a\u7528\u4e8e\u5c06kobject\u52a0\u5165\u5230\u94fe\u8868\u4e2d\u3002

parent\uff1a\u6307\u5411\u7236kobject\u7684\u6307\u9488\uff0c\u5728sysfs\u4e2d\u8868\u793a\u4e0a\u4e00\u5c42\u7684\u8282\u70b9\u3002

kset\uff1a\u8be5kobject\u5c5e\u4e8e\u7684kset\u3002\u82e5\u8be5kobject\u672a\u6307\u5b9aparent\uff0c\u5219\u4f1a\u628akset\u4f5c\u4e3aparent\u3002

ktype\uff1a\u8be5kobject\u5c5e\u4e8e\u7684kobj_type\uff0c\u6bcf\u4e2akobject\u5fc5\u987b\u6709\u4e00\u4e2aktype\u3002

sd\uff1a\u8be5kobject\u5728sysfs\u4e2d\u7684\u5bf9\u5e94\u76ee\u5f55\u9879\u3002

kref\uff1a\u539f\u5b50\u5f15\u7528\u8ba1\u6570\u3002

state_initialized\uff1a\u6307\u793a\u8be5kobject\u662f\u5426\u5df2\u7ecf\u521d\u59cb\u5316\u3002

state_in_sysfs\uff1a\u6307\u793a\u8be5kobject\u662f\u5426\u5df2\u5728sysfs\u4e2d\u5efa\u7acb\u76ee\u5f55\u3002

state_add_uevent_sent/state_remove_uevent_sent\uff1a\u8bb0\u5f55\u662f\u5426\u5df2\u5411\u7528\u6237\u7a7a\u95f4\u53d1\u9001add uevent\u3002

uevent_suppress\uff1a\u5982\u679c\u8be5\u5b57\u6bb5\u4e3a1\uff0c\u5219\u8868\u793a\u5ffd\u7565\u6240\u6709\u4e0a\u62a5\u7684uevent\u4e8b\u4ef6\u3002

kobject\u6838\u5fc3\u673a\u5236\uff1a

\u5185\u5d4c\u5728\u522b\u7684\u6570\u636e\u7ed3\u6784\uff08\u6bd4\u5982device_driver\uff09\u4e2d\uff0c\u5f53kobject\u4e2d\u7684\u5f15\u7528\u8ba1\u6570\u5f52\u96f6\u65f6\uff0c\u91ca\u653ekobject\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u540c\u65f6\u901a\u8fc7ktype\u4e2d\u7684release()\u56de\u8c03\u51fd\u6570\uff0c\u91ca\u653e\u5185\u5d4c\u6570\u636e\u7ed3\u6784\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u6bcf\u4e00\u4e2a\u5185\u5d4ckobject\u7684\u6570\u636e\u7ed3\u6784\u90fd\u9700\u8981\u81ea\u5df1\u5b9e\u73b0ktype\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u3002

ktype\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct kobj_type {\n    void (*release)(struct kobject *kobj);\n    const struct sysfs_ops *sysfs_ops;\n    const struct attribute_group **default_groups;\n    /*\u7701\u7565\u5176\u4ed6\u6210\u5458*/  \n};\n

release\uff1a\u5f53kobject\u5f15\u7528\u8ba1\u6570\u5f52\u96f6\u65f6\u8c03\u7528\u8be5\u6790\u6784\u51fd\u6570\uff0c\u8d1f\u8d23\u91ca\u653ekobject\u7684\u5185\u5b58\u3002

sysfs_ops\uff1asysfs\u6587\u4ef6\u7cfb\u7edf\u8bfb\u5199\u65f6\u7684\u7279\u6027\u3002

default_groups\uff1a\u5b9a\u4e49\u4e86kobject\u7684\u5c5e\u6027\uff0c\u7531struct attritube\u548cstruct bin_attribute\u6784\u6210\u3002

ktype\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u63cf\u8ff0\u4e00\u65cfkobject\u6240\u5177\u6709\u7684\u666e\u904d\u7279\u6027\u3002\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u6bcf\u4e2akobject\u5b9a\u4e49\u81ea\u5df1\u7684\u7279\u6027\uff0c\u800c\u662f\u5728ktype\u4e2d\u7edf\u4e00\u5b9a\u4e49\u3002

kset\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct kset {\n    struct list_head list;\n    spinlock_t list_lock;\n    struct kobject kobj;\n    const struct kset_uevent_ops *uevent_ops;\n};\n

list/list_lock\uff1a\u7528\u4e8e\u4fdd\u5b58\u8be5kset\u4e0b\u6240\u6709kobject\u5bf9\u8c61\u3002

kobj\uff1a\u8be5kset\u81ea\u5df1\u7684kobject\u3002

uevent_ops\uff1auevent\u662f\u7528\u6237\u7a7a\u95f4\u7684\u7f29\u5199\uff0c\u63d0\u4f9b\u4e86\u4e0e\u7528\u6237\u7a7a\u95f4\u70ed\u63d2\u62d4\u8fdb\u884c\u901a\u4fe1\u7684\u673a\u5236\u3002\u5f53\u4efb\u4f55kobject\u9700\u8981\u4e0a\u62a5uevent\u65f6\uff0c\u90fd\u8981\u8c03\u7528\u6240\u5c5e\u7684kset\u4e2duevent_ops\u4e2d\u7684\u51fd\u6570\u3002uevent\u7684\u6982\u5ff5\u7a0d\u540e\u8bf4\u660e\u3002

kset\u662fkobject\u5bf9\u8c61\u7684\u96c6\u5408\u4f53\u3002\u5b83\u4e0ektype\u7684\u533a\u522b\u5728\u4e8e\uff1a\u5177\u6709\u76f8\u540cktype\u7684kobject\u53ef\u4ee5\u88ab\u5206\u7ec4\u5230\u4e0d\u540c\u7684kset\u3002

\u5f53\u8bbe\u7f6e\u4e86kset\u5e76\u628a\u5b83\u6dfb\u52a0\u5230\u7cfb\u7edf\u4e2d\uff0c\u5c06\u5728sysfs\u4e2d\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u3002kobject\u7684\u6dfb\u52a0\u4e0e\u5220\u9664\u4e3b\u8981\u662fkobject_regsiter()\u51fd\u6570\u548ckobject_unregister()\u51fd\u6570\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0ckobject\u4f1a\u5728\u5176parent\u6307\u9488\u4e2d\u4fdd\u5b58kset\u7684\u6307\u9488\u3002

"},{"location":"linux/drivers/device_model/#sysfs","title":"sysfs","text":"

sysfs\u6587\u4ef6\u7cfb\u7edf\u662f\u4e00\u4e2a\u5904\u4e8e\u5185\u5b58\u4e2d\u7684\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff0c\u5b83\u63d0\u4f9b\u4e86kobject\u5bf9\u8c61\u7684\u5c42\u6b21\u7ed3\u6784\u89c6\u56fe\u3002\u7528\u6237\u67e5\u8be2\u7cfb\u7edf\u4e2d\u5404\u79cd\u8bbe\u5907\u7684\u62d3\u6251\u7ed3\u6784\uff0c\u5c31\u50cf\u67e5\u8be2\u6587\u4ef6\u76ee\u5f55\u4e00\u6837\u7b80\u5355\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u51fa\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u5c06\u5185\u6838\u53d8\u91cf\u63d0\u4f9b\u7ed9\u7528\u6237\u8bfb\u53d6\u6216\u8005\u5199\u5165\u3002

\u76ee\u5f55 \u529f\u80fd block \u5757\u8bbe\u5907 bus \u603b\u7ebf class \u5c06\u8bbe\u5907\u6309\u7167\u529f\u80fd\u5206\u7c7b dev block\u548cchar\u4e24\u4e2a\u76ee\u5f55\uff0c\u5bf9\u5e94\u90e8\u5206\u5757\u8bbe\u5907\u548c\u5b57\u7b26\u8bbe\u5907\u7b26\u53f7\u94fe\u63a5 devices \u6240\u6709\u8bbe\u5907 firmwares \u56fa\u4ef6\u4fe1\u606f fs \u6587\u4ef6\u7cfb\u7edf hypervisor \u7a7a\u76ee\u5f55 kernel \u5185\u6838\u914d\u7f6e\u53c2\u6570 module \u6a21\u5757\u4fe1\u606f power \u7535\u6e90\u7ba1\u7406

\u5176\u4e2d\u6700\u91cd\u8981\u7684\u76ee\u5f55\u662fdevices\uff0cLinux\u7cfb\u7edf\u4e2d\u6240\u6709\u8bbe\u5907\u90fd\u53ef\u4ee5\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u627e\u5230\u3002\u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u8bbe\u5907\u5176\u5b9e\u90fd\u662fdevices\u76ee\u5f55\u4e2d\u7684\u7b26\u53f7\u94fe\u63a5\u3002

sysfs\u7684\u6838\u5fc3\u662f\u628akobject\u5bf9\u8c61\u4e0e\u76ee\u5f55\u9879\uff08directory entries\uff09\u5173\u8054\u8d77\u6765\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u67e5\u770b\u6302\u8f7d\u4e8e/sys\u76ee\u5f55\u4e0b\u7684\u6574\u4e2a\u6587\u4ef6\u7cfb\u7edf\u89c6\u56fe\u3002\u5411sysfs\u4e2d\u6dfb\u52a0kobject\uff0c\u53ef\u4ee5\u4f7f\u7528kobject_add()\u548ckobject_create_and_add()\u51fd\u6570\u3002

\u6211\u4eec\u5df2\u7ecf\u77e5\u9053kobject\u53ef\u4ee5\u88ab\u6620\u5c04\u4e3a\u67d0\u4e2a\u6587\u4ef6\u76ee\u5f55\uff0c\u4ec5\u6709\u5982\u6b64\u8fd8\u4e0d\u591f\u3002\u56e0\u4e3a\u8fd9\u6837\u7684sysfs\u4ec5\u4ec5\u53ea\u662f\u4e00\u9897\u6811\uff0c\u4f46\u6ca1\u6709\u63d0\u4f9b\u5b9e\u9645\u6570\u636e\u7684\u6587\u4ef6\u3002\u4e3a\u4e86\u80fd\u591f\u8bfb\u5199\u8fd9\u9897\u6811\uff0c\u5185\u6838\u63d0\u4f9b\u4e86attribute\u548cbin_attribute\u4e24\u79cd\u5c5e\u6027\u3002

\u5728ktype\u4e2d\u7528struct attribute_group\u63cf\u8ff0\u8fd9\u4e24\u4e2a\u5c5e\u6027\uff1a

struct attribute_group {\n    const char *name;\n    struct attribute **attrs;\n    struct bin_attribute **bin_attrs;\n};\n\n/*attribute\u548cbin_attribute\u7684\u5b9a\u4e49\u5982\u4e0b*/\n\nstruct attribute {\n    const char *name;   //\u5c5e\u6027\u7684\u540d\u5b57\n    umode_t mode;       //\u5c5e\u6027\u7684\u6743\u9650\n};\n\nstruct bin_attribute {\n    struct attribute attr;\n    size_t size;\n    void *private;\n    struct address_space *(*f_mapping)(void);\n    ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,\n            char *, loff_t, size_t);\n    ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,\n             char *, loff_t, size_t);\n    int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,\n            struct vm_area_struct *vma);\n};\n

\u5bf9\u4e8e\u9ed8\u8ba4\u5c5e\u6027\u7684\u5b9e\u73b0\uff0c\u7531ktype->sysfs_ops\u6210\u5458\u63cf\u8ff0\uff1a

struct sysfs_ops {\n    ssize_t (*show)(struct kobject *, struct attribute *, char *);\n    ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);\n};\n

\u5f53\u7528\u6237\u7a7a\u95f4\u8bfb\u53d6\u4e00\u4e2a\u5c5e\u6027\u65f6\uff0c\u5185\u6838\u4f1a\u8c03\u7528show()\u65b9\u6cd5\uff1b\u5f53\u5199\u4e00\u4e2a\u5c5e\u6027\u65f6\uff0c\u8c03\u7528store()\u65b9\u6cd5\u3002

struct attribute\u4e3a\u666e\u901a\u7684attribute\uff0c\u4f7f\u7528\u8be5attribute\u751f\u6210\u7684sysfs\u6587\u4ef6\uff0c\u53ea\u80fd\u7528\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u8bfb\u5199\u3002\u800cstruct bin_attribute\u5728struct attribute\u7684\u57fa\u7840\u4e0a\uff0c\u589e\u52a0\u4e86read()\u3001write()\u7b49\u51fd\u6570\uff0c\u56e0\u6b64\u5b83\u6240\u751f\u6210\u7684sysfs\u6587\u4ef6\u53ef\u4ee5\u7528\u4efb\u4f55\u65b9\u5f0f\u8bfb\u5199\u3002

"},{"location":"linux/drivers/device_model/#uevent","title":"uevent","text":"

uevent\u662fkobject\u529f\u80fd\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u5728kobject\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u6bd4\u5982\u6dfb\u52a0\u3001\u79fb\u9664\uff0c\u901a\u77e5\u7528\u6237\u7a7a\u95f4\u3002\u7528\u6237\u7a7a\u95f4\u6536\u5230\u8baf\u606f\u540e\uff0c\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002

\u8be5\u673a\u5236\u901a\u5e38\u662f\u7528\u6765\u652f\u6301\u70ed\u63d2\u62d4\uff08hotplug\uff09\u8bbe\u5907\u7684\uff0c\u4f8b\u5982\u5f53U\u76d8\u63d2\u5165\u540e\uff0cUSB\u76f8\u5173\u7684\u9a71\u52a8\u4f1a\u52a8\u6001\u521b\u5efa\u7528\u4e8e\u8868\u793a\u8be5U\u76d8\u7684device\u7ed3\u6784\uff0c\u5e76\u544a\u77e5\u7528\u6237\u7a7a\u95f4\u4e3a\u8be5U\u76d8\u52a8\u6001\u521b\u5efa/dev/\u76ee\u5f55\u4e0b\u7684\u8bbe\u5907\u8282\u70b9\u3002

uevent\u673a\u5236\u6bd4\u8f83\u7b80\u5355\uff0c\u5f53\u8bbe\u5907\u6a21\u578b\u4e2d\u4efb\u4f55\u8bbe\u5907\u6709\u4e8b\u4ef6\u9700\u8981\u4e0a\u62a5\u65f6\uff0c\u90fd\u4f1a\u89e6\u53d1uevent\u63d0\u4f9b\u7684\u63a5\u53e3\u3002uevent\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e2a\u9014\u5f84\u628a\u4e8b\u4ef6\u4e0a\u62a5\u5230\u7528\u6237\u7a7a\u95f4\uff1a\u4e00\u79cd\u662f\u901a\u8fc7kmod\u6a21\u5757\uff0c\u53e6\u4e00\u79cd\u662f\u901a\u8fc7netlink\u901a\u4fe1\u673a\u5236\u3002

uevent\u7684\u4ee3\u7801\u4e3b\u8981\u4f4d\u4e8ekobject.h\u548ckobject_uevent.c\u4e24\u4e2a\u6587\u4ef6\u3002

kobject_action\u5b9a\u4e49\u4e86uevent\u7684\u7c7b\u578b\uff1a

enum kobject_action {\n    KOBJ_ADD,\n    KOBJ_REMOVE,\n    KOBJ_CHANGE,\n    KOBJ_MOVE,\n    KOBJ_ONLINE,\n    KOBJ_OFFLINE,\n    KOBJ_BIND,\n    KOBJ_UNBIND,\n};\n

ADD/REMOVE\uff1akobject\u7684\u6dfb\u52a0/\u79fb\u9664\u4e8b\u4ef6\u3002

CHANGE\uff1akobject\u72b6\u6001\u6216\u8005\u5185\u5bb9\u53d1\u751f\u6539\u53d8\u3002

MOVE\uff1akobject\u66f4\u6539\u540d\u79f0\u6216\u8005\u66f4\u6539\u4e86parent\u3002

ONLINE/OFFLINE\uff1akobject\u7684\u4e0a\u7ebf/\u4e0b\u7ebf\u4e8b\u4ef6

BIND/UNBIND\uff1akobject\u7684\u7ed1\u5b9a/\u89e3\u7ed1\u4e8b\u4ef6

kobj_uevent_env\u5b9a\u4e49\u4e86\u4e8b\u4ef6\u4e0a\u62a5\u65f6\u7684\u73af\u5883\u53d8\u91cf\uff1a

struct kobj_uevent_env {\n    char *argv[3];\n    char *envp[UEVENT_NUM_ENVP];\n    int envp_idx;\n    char buf[UEVENT_BUFFER_SIZE];\n    int buflen;\n};\n

argv\uff1a\u6307\u9488\u6570\u7ec4\uff0c\u53ef\u4ee5\u4fdd\u5b58\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u6700\u5927\u4e3a3\u4e2a\u3002

envp\uff1a\u6307\u9488\u6570\u7ec4\uff0c\u7528\u4e8e\u4fdd\u5b58\u6bcf\u4e2a\u73af\u5883\u53d8\u91cf\u7684\u5730\u5740\u3002

envp_idx\uff1a\u8bbf\u95eeenvp\u6570\u7ec4\u7684\u7d22\u5f15\u3002

buf\uff1a\u4fdd\u5b58uevent\u6d88\u606f\u7684\u7f13\u51b2\u533a

buflen\uff1a\u5b58\u50a8\u7f13\u51b2\u533a\u7684\u5927\u5c0f

kset_uevent_ops\u5b9a\u4e49\u4e86kset\u7684uevent\u63a5\u53e3\u64cd\u4f5c\uff1a

struct kset_uevent_ops {\n    int (* const filter)(struct kset *kset, struct kobject *kobj);\n    const char *(* const name)(struct kset *kset, struct kobject *kobj);\n    int (* const uevent)(struct kset *kset, struct kobject *kobj, struct kobj_uevent_env *env);\n};\n

filter\uff1a\u5f53kobject\u9700\u8981\u4e0a\u62a5uevent\u65f6\uff0c\u5b83\u6240\u5c5e\u7684kset\u53ef\u4ee5\u901a\u8fc7\u6b64\u63a5\u53e3\u8fc7\u6ee4\u6389\u4e0d\u5e0c\u671b\u4e0a\u62a5\u7684uevent\u3002

name\uff1a\u7528\u4e8e\u83b7\u53d6kset\u4e2dkobject\u7684uevent\u540d\u79f0\uff0c\u8fd9\u4e2a\u540d\u79f0\u901a\u5e38\u4e0euevent\u4e2d\u7684ACTION\u5b57\u6bb5\u76f8\u5bf9\u5e94\u3002

uevent\uff1a\u5f53\u4e00\u4e2akobject\u9700\u8981\u4e0a\u62a5uevent\u65f6\uff0cuevent\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u5b83\u53ef\u4ee5\u4e3auevent\u6dfb\u52a0\u73af\u5883\u53d8\u91cf\u3002

uevent\u7684\u4e00\u4e9b\u64cd\u4f5cAPI\uff1a

int kobject_uevent(struct kobject *kobj, enum kobject_action action);\nint kobject_uevent_env(struct kobject *kobj, enum kobject_action action, char *envp[]);\nint kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count);\nint add_uevent_var(struct kobj_uevent_env *env, const char *format, ...);\n

kobject_uevent_env\uff1a\u4ee5envp\u4e3a\u73af\u5883\u53d8\u91cf\uff0c\u4e0a\u62a5\u4e00\u4e2a\u6307\u5b9aaction\u7684uevent\u3002

kobject_synth_uevent\uff1a\u5141\u8bb8\u53d1\u9001\u4e00\u4e2a\u5408\u6210\u7684uevent\u3002

add_uevent_var\uff1a\u4ee5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\uff0c\u5411uevent\u6dfb\u52a0\u65b0\u7684\u73af\u5883\u53d8\u91cf\u3002

"},{"location":"linux/drivers/device_model/#devicedevice_driver","title":"device\u548cdevice_driver","text":"

device\u548cdevice_driver\u662fLinux\u9a71\u52a8\u5f00\u53d1\u7684\u57fa\u672c\u6982\u5ff5\u3002\u9a71\u52a8\u5f00\u53d1\uff0c\u5176\u5b9e\u5c31\u662f\u5f00\u53d1\u6307\u5b9a\u7684\u8f6f\u4ef6\uff08driver\uff09\u4ee5\u53ca\u9a71\u52a8\u6307\u5b9a\u7684\u8bbe\u5907\uff08device\uff09\u3002\u5185\u6838\u4e3a\u6b64\u5b9a\u4e49\u4e86\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u662fstruct device\u548cstruct device_driver\u3002\u5728<include/linux/device.h>\u4e2d\u53ef\u4ee5\u627e\u5230\u8fd9\u4e24\u4e2a\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u3002

\u5185\u6838\u7528struct device\u7ed3\u6784\u4f53\u6765\u8868\u793a\u4e00\u4e2a\u8bbe\u5907\uff1a

struct device {\n    struct kobject kobj;\n    struct device *parent;\n\n    struct device_private *p;\n\n    const char *init_name; \n    const struct device_type *type;\n\n    struct bus_type *bus;       \n    struct device_driver *driver;   \n    void *platform_data;    \n    void *driver_data;  \n\n    void (*release)(struct device *dev);\n};\n

kobj\uff1a\u8868\u793a\u8be5\u8bbe\u5907\u5e76\u628a\u5b83\u8fde\u63a5\u5230\u7ed3\u6784\u4f53\u7cfb\u4e2d\u7684kobject\u3002

parent\uff1a\u8bbe\u5907\u7684\u7236\u8bbe\u5907\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7236\u8bbe\u5907\u662f\u67d0\u79cdbus\u6216\u8005\u662fhost controller\u3002

p\uff1a\u8bbe\u5907\u7684\u79c1\u6709\u6570\u636e\u3002

init_name\uff1a\u8bbe\u5907\u7684\u521d\u59cb\u540d\u79f0\u3002

type\uff1a\u8bbe\u5907\u7684\u7c7b\u578b\u3002

bus\uff1abus\u7684\u7c7b\u578b\u3002

driver\uff1a\u5bf9\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002

platform_data\uff1a\u8bbe\u5907\u79c1\u6709\u7684\u5e73\u53f0\u6570\u636e\u3002

driver_data\uff1a\u9a71\u52a8\u7684\u79c1\u6709\u6570\u636e\u3002

release\uff1a\u8bbe\u5907\u5378\u8f7d\u65f6\uff0c\u8c03\u7528\u8be5\u56de\u8c03\u51fd\u6570\u3002

\u8bbe\u5907\u7684\u6ce8\u518c\u548c\u6ce8\u9500\u51fd\u6570\u662f\uff1a

init device_register(struct device *dev);\n\nvoid device_unregister(struct device *dev);\n

\u5185\u6838\u7528struct device_driver\u6765\u8868\u793a\u9a71\u52a8\u7a0b\u5e8f\uff1a

struct device_driver {\n    const char *name;\n    struct bus_type *bus;\n    struct module *owner;   \n\n    enum probe_type probe_type;\n\n    int (*probe) (struct device *dev);\n    int (*remove) (struct device *dev);\n\n    const struct attribute_group **groups;\n    const struct attribute_group **dev_groups;\n\n    struct driver_private *p;\n};\n

probe_type\uff1a\u6307\u5b9a\u4ee5\u4ec0\u4e48\u65b9\u5f0f\u6267\u884cprobe\uff08\u5f02\u6b65\u6216\u540c\u6b65\uff09\u3002

probe/remove\uff1a\u5728\u6ce8\u518c\u4e0e\u6ce8\u9500\u65f6\u88ab\u8c03\u7528\u3002

\u9a71\u52a8\u7a0b\u5e8f\u7684\u6ce8\u518c\u548c\u6ce8\u9500\u51fd\u6570\u662f\uff1a

int driver_register(struct device_driver *drv);\n\nvoid driver_unregister(struct device_drvier *drv);\n

Linux\u8bbe\u5907\u6a21\u578b\u6846\u67b6\u4f53\u7cfb\u4e0b\u5f00\u53d1\uff0c\u4e3b\u8981\u5305\u62ec\u4e24\u4e2a\u6b65\u9aa4\uff1a

  1. \u5206\u914d\u4e00\u4e2astruct device\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u586b\u5145\u4fe1\u606f\uff0c\u7136\u540e\u5c06\u5176\u6ce8\u518c\u5230\u5185\u6838\u3002

  2. \u5206\u914d\u4e00\u4e2astruct device_driver\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u586b\u5145\u4fe1\u606f\uff0c\u7136\u540e\u5c06\u5176\u6ce8\u518c\u5230\u5185\u6838\u3002

\u5185\u6838\u4f1a\u5728\u5408\u9002\u7684\u65f6\u673a\uff0c\u8c03\u7528struct device_driver\u4e2d\u7684\u5404\u7c7b\u56de\u8c03\u51fd\u6570\uff0c\u4ece\u800c\u89e6\u53d1\u540e\u8005\u8bbe\u5907\u9a71\u52a8\u7684\u6267\u884c\u3002\u800c\u6240\u6709\u7684\u9a71\u52a8\u7a0b\u5e8f\u903b\u8f91\uff0c\u5176\u5b9e\u90fd\u662f\u7531\u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002

\u5f53\u7136\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cLinux\u9a71\u52a8\u5f00\u53d1\u5f88\u5c11\u76f4\u63a5\u64cd\u4f5c\u4e0a\u9762\u4e24\u4e2a\u7ed3\u6784\u4f53\uff0c\u56e0\u4e3a\u5185\u6838\u53c8\u5c01\u88c5\u4e86\u4e00\u5c42\uff0c\u6bd4\u5982platform_device\uff0c\u5c01\u88c5\u540e\u7684\u63a5\u53e3\u66f4\u4e3a\u7b80\u5355\u6613\u7528\u3002device\u548cdevice_driver\u5fc5\u987b\u6302\u5728\u5728\u540c\u4e00\u4e2abus\u4e4b\u4e0b\uff0c\u540d\u79f0\u4e5f\u5fc5\u987b\u4e00\u6837\uff0c\u5185\u6838\u624d\u80fd\u5b8c\u6210\u5339\u914d\u64cd\u4f5c\u3002

\u5982\u679c\u5339\u914d\u5230\u4e86\u76f8\u540c\u540d\u79f0\u7684device\u548cdevice_driver\uff0c\u5185\u6838\u5c31\u4f1a\u6267\u884cdevice_driver\u4e2d\u7684probe()\u56de\u8c03\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u662f\u6240\u6709driver\u7684\u5165\u53e3\u51fd\u6570\uff0c\u7528\u6765\u6267\u884c\u8bf8\u5982\u786c\u4ef6\u8bbe\u5907\u521d\u59cb\u5316\u3001\u5b57\u7b26\u8bbe\u5907\u6ce8\u518c\u3001\u6587\u4ef6\u64cd\u4f5cops\u6ce8\u518c\u7b49\u52a8\u4f5c\uff08\u5bf9\u5e94remove()\u51fd\u6570\uff09\u3002

"},{"location":"linux/drivers/device_model/#bus","title":"bus","text":"

\u603b\u7ebf\u662f\u8fde\u63a5\u5904\u7406\u5668\u548c\u8bbe\u5907\u4e4b\u95f4\u7684\u901a\u9053\u3002\u4e3a\u4e86\u65b9\u4fbf\u8bbe\u5907\u6a21\u578b\u7684\u5b9e\u73b0\uff0c\u7cfb\u7edf\u4e2d\u7684\u6bcf\u4e2a\u8bbe\u5907\u90fd\u9700\u8981\u8fde\u63a5\u5728\u4e00\u4e2a\u603b\u7ebf\u4e0a\uff0c\u8fd9\u4e2a\u603b\u7ebf\u53ef\u4ee5\u662f\u5185\u90e8\u603b\u7ebf\u3001\u865a\u62df\u603b\u7ebf\u6216\u8005\u662f\u5e73\u53f0\u603b\u7ebf\u3002

\u603b\u7ebf\u9a71\u52a8\u5219\u8d1f\u8d23\u5b9e\u73b0\u603b\u7ebf\u7684\u5404\u79cd\u884c\u4e3a\uff0c\u5176\u7ba1\u7406\u7740\u4e24\u4e2a\u94fe\u8868\uff0c\u5206\u522b\u662f\u6dfb\u52a0\u5230\u8be5\u603b\u7ebf\u7684\u8bbe\u5907\u94fe\u8868\u4ee5\u53ca\u6ce8\u518c\u5230\u8be5\u603b\u7ebf\u7684\u9a71\u52a8\u94fe\u8868\u3002\u5f53\u4f60\u5411\u603b\u7ebf\u6dfb\u52a0\uff08\u79fb\u9664\uff09\u4e00\u4e2a\u8bbe\u5907\uff08\u9a71\u52a8\uff09\u65f6\uff0c\u4fbf\u4f1a\u5728\u5bf9\u5e94\u7684\u5217\u8868\u4e0a\u6dfb\u52a0\u65b0\u7684\u8282\u70b9\uff0c \u540c\u65f6\u5bf9\u6302\u8f7d\u5728\u8be5\u603b\u7ebf\u7684\u9a71\u52a8\u4ee5\u53ca\u8bbe\u5907\u8fdb\u884c\u5339\u914d\uff0c\u5728\u5339\u914d\u8fc7\u7a0b\u4e2d\u4f1a\u5ffd\u7565\u6389\u90a3\u4e9b\u5df2\u7ecf\u6709\u9a71\u52a8\u5339\u914d\u7684\u8bbe\u5907\u3002

\u5185\u6838\u7528struct bus_type\u7ed3\u6784\u4f53\u62bd\u8c61\u51fa\u603b\u7ebf\uff1a

struct bus_type {\n    const char *name;\n    const char *dev_name;\n    struct device *dev_root;\n    const struct attribute_group **bus_groups;\n    const struct attribute_group **dev_groups;\n    const struct attribute_group **drv_groups;\n\n    int (*match)(struct device *dev, struct device_driver *drv);\n    int (*uevent)(struct device *dev, struct kobj_uevent_env *env);\n    int (*probe)(struct device *dev);\n    int (*remove)(struct device *dev);\n\n    struct subsys_private *p;\n};\n

\u6211\u4eec\u53d1\u73b0bus_type\u7ed3\u6784\u4f53\u4e2d\u7684\u5927\u90e8\u5206\u6210\u5458\u90fd\u4e0edevice\u6709\u5173\uff0c\u8bf4\u660e\u5b83\u4e3b\u8981\u8d1f\u8d23\u8bbe\u5907\u7684\u6ce8\u518c\u548c\u6ce8\u9500\u7b49\u64cd\u4f5c\u3002

name\uff1a\u8be5bus\u7684\u540d\u79f0\uff0c\u5728sysfs\u4e2d\u4ee5\u76ee\u5f55\u5f62\u5f0f\u5b58\u5728\uff0c\u6bd4\u5982platform bus\u8868\u73b0\u4e3a/sys/bus/platform\u3002

dev_name\uff1a\u6ce8\u518c\u5230bus\u7684\u8bbe\u5907\u540d\u79f0\u3002

dev_root\uff1a\u6839\u8bbe\u5907\u3002

bus_groups\uff1abus\u7684\u9ed8\u8ba4\u5c5e\u6027\u3002

dev_groups\uff1abus\u4e0adevice\u7684\u9ed8\u8ba4\u5c5e\u6027\u3002

drv_groups\uff1abus\u4e0adevice_driver\u7684\u9ed8\u8ba4\u5c5e\u6027\u3002

match\uff1a\u5f53\u5c5e\u4e8e\u8be5bus\u7684device\u6216\u8005device_driver\u6dfb\u52a0\u5230\u5185\u6838\u65f6\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u3002

uevent\uff1a\u5f53\u5c5e\u4e8e\u8be5bus\u7684device\uff0c\u53d1\u751f\u6dfb\u52a0\u3001\u79fb\u9664\u6216\u8005\u5176\u4ed6\u52a8\u4f5c\u65f6\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u3002

probe\u3001remove\uff1a\u5f53\u5c5e\u4e8e\u8be5bus\u7684device\uff0c\u53d1\u751f\u521d\u59cb\u5316\u548c\u79fb\u9664\u65f6\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u3002

p\uff1a\u4fdd\u5b58\u4e86bus\u6a21\u5757\u7684\u4e00\u4e9b\u79c1\u6709\u6570\u636e\u3002

bus\u7684\u5c5e\u6027\u4ee5struct bus_attribute\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct bus_attribute{\n    struct attribute attr;\n    ssize_t (*show)(struct bus_type *bus, char *buf);\n    ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);\n};\n

bus\u6a21\u5757\u7684\u4e3b\u8981\u529f\u80fd\u662f\uff1a

  • bus\u7684\u6ce8\u518c\u548c\u6ce8\u9500
  • \u672cbus\u4e0b\u6709device\u6216\u8005device_driver\u6ce8\u518c\u5230\u5185\u6838\u65f6\u7684\u5904\u7406
  • \u672cbus\u4e0b\u6709device\u6216\u8005device_driver\u4ece\u5185\u6838\u6ce8\u9500\u65f6\u7684\u5904\u7406
  • device_driver\u7684probe
  • \u7ba1\u7406bus\u4e0b\u6240\u6709\u7684device\u548cdevice_driver

\u5185\u6838\u63d0\u4f9b\u4e86bus_register()\u51fd\u6570\u6765\u6ce8\u518c\u603b\u7ebf\uff0cbus_unregister()\u51fd\u6570\u6765\u6ce8\u9500\u603b\u7ebf\u3002

int bus_register(struct bus_type *bus);\n\nvoid bus_unregister(struct bus_type *bus);\n

\u5f53\u6211\u4eec\u6210\u529f\u6ce8\u518c\u603b\u7ebf\u65f6\uff0c\u4f1a\u5728/sys/bus/\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff0c\u76ee\u5f55\u540d\u4e3a\u6211\u4eec\u65b0\u6ce8\u518c\u7684\u603b\u7ebf\u540d\u3002bus\u76ee\u5f55\u4e2d\u5305\u542b\u4e86\u5f53\u524d\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u6ce8\u518c\u4e86\u7684\u6240\u6709\u603b\u7ebf\uff0c\u4f8b\u5982i2c\uff0cspi\uff0cplatform\u7b49\u3002

"},{"location":"linux/drivers/device_model/#class","title":"class","text":"

\u6700\u540e\u4e00\u4e2a\u8bbe\u5907\u6a21\u578b\u6982\u5ff5\u662fclass\u3002class\u662f\u4e00\u4e2a\u8bbe\u5907\u7684\u9ad8\u7ea7\u89c6\u56fe\uff0c\u5b83\u62bd\u8c61\u51fa\u4e86\u5e95\u5c42\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u9a71\u52a8\u7a0b\u5e8f\u53ef\u4ee5\u770b\u5230\u56fa\u6001\u786c\u76d8\u6216\u5149\u76d8\uff0c\u4f46\u662f\u5728class\u7684\u5c42\u6b21\u4e0a\uff0c\u5b83\u4eec\u90fd\u53ea\u662f\u78c1\u76d8\u800c\u5df2\u3002class\u5141\u8bb8\u7528\u6237\u7a7a\u95f4\u4f7f\u7528\u8bbe\u5907\u6240\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u5173\u5fc3\u8bbe\u5907\u662f\u5982\u4f55\u8fde\u63a5\u7684\uff0c\u4ee5\u53ca\u5982\u4f55\u5de5\u4f5c\u7684\u3002

\u51e0\u4e4e\u6240\u6709\u7684class\u90fd\u663e\u793a\u5728/sys/class\u76ee\u5f55\u4e2d\u3002\u6bd4\u5982\u6240\u6709\u7684\u7f51\u7edc\u63a5\u53e3\u90fd\u51e0\u79cd\u5728/sys/class/net\u4e0b\uff0c\u8f93\u5165\u8bbe\u5907\u5728/sys/class/input\u4e0b\uff0c\u4e32\u884c\u8bbe\u5907\u5728/sys/class/tty\u4e0b\u3002

class\u7684\u7ed3\u6784\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a

struct class {\n    const char *name;\n\n    const struct attribute_group **class_groups;\n    const struct attribute_group **dev_groups;\n\n    struct kobject *dev_kobj;\n\n    int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);\n    char *(*devnode)(struct device *dev, umode_t *mode);\n\n    void (*class_release)(struct class *class);\n    void (*dev_release)(struct device *dev);\n\n    struct subsys_private *p;\n};\n

name\uff1aclass\u7684\u540d\u79f0\uff0c\u4f53\u73b0\u5728/sys/class\u76ee\u5f55\u4e0b\u3002

class_groups\uff1aclass\u5c5e\u6027\u3002

dev_groups\uff1adev\u5c5e\u6027\u3002

dev_uevent\uff1a\u8bbe\u5907\u7684uevent\u56de\u8c03\u51fd\u6570\u3002

class_release/dev_release\uff1arelease\u56de\u8c03\u51fd\u6570\u3002

p\uff1a\u5b50\u7cfb\u7edf\u7684\u79c1\u6709\u6570\u636e\u3002

\u5bf9\u4e8e\u5b50\u7cfb\u7edf\u8fd9\u91cc\u89e3\u91ca\u4e00\u4e0b\u3002/sys/class\u548cdevice_name\u4e4b\u95f4\u7684\u90a3\u90e8\u5206\u76ee\u5f55\u79f0\u4e3asubsystem\u3002\u4e5f\u5c31\u662f\u6bcf\u4e2adev\u5c5e\u6027\u6587\u4ef6\u6240\u5728\u7684\u8def\u5f84\u90fd\u53ef\u8868\u793a\u4e3a/sys/class/subsystem/device_name/dev\u3002\u4f8b\u5982\uff0ccat /sys/class/tty/tty0/dev\u4f1a\u5f97\u52304:0\uff0c\u8fd9\u91ccsubsystem\u4e3atty,device_name\u4e3atty0\u3002

class\u7684\u6ce8\u518c/\u6ce8\u9500\u51fd\u6570\u5982\u4e0b\uff1a

int __must_check __class_register(struct class *class, struct lock_class_key *key);\nvoid class_unregister(struct class *class);\n

class\u7684\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u540e\u7eed\uff0c\u5728\u5404\u7c7b\u5b50\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u80fd\u770b\u5230\u8bb8\u591aclass\u7684\u7528\u4f8b\u3002

"},{"location":"linux/drivers/dma/","title":"\u76f4\u63a5\u5185\u5b58\u8bbf\u95ee","text":"

\u76f4\u63a5\u5185\u5b58\u8bbf\u95ee(DMA)\u662f\u4e00\u79cd\u9ad8\u7ea7\u7684\u786c\u4ef6\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u5916\u56f4\u8bbe\u5907\u548c\u5185\u5b58\u4e4b\u95f4\u76f4\u63a5\u4f20\u8f93\u5b83\u4eec\u7684I/O\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981CPU\u7684\u53c2\u4e0e\u3002\u4f7f\u7528\u8fd9\u79cd\u673a\u5236\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4e0e\u8bbe\u5907\u901a\u4fe1\u7684\u6548\u7387\u3002

"},{"location":"linux/drivers/dts/","title":"\u8bbe\u5907\u6811","text":""},{"location":"linux/drivers/dts/#_2","title":"\u8bbe\u5907\u6811\u7684\u7531\u6765","text":"

\u8d77\u56e0\u662f\u5728<arch/arm/mach-xxx>\u76ee\u5f55\u4e0b\u5145\u65a5\u7740\u5927\u91cf\u91cd\u590d\u7684board specific\u7684\u4ee3\u7801\uff0c\u6bcf\u6b21Linux\u5185\u6838merge window\u671f\u95f4\uff0cARM\u7684\u4ee3\u7801\u53d8\u5316\u5360\u6574\u4e2aarch\u76ee\u5f55\u7684\u4e00\u534a\u4ee5\u4e0a\uff0c\u5bfc\u81f4\u5185\u6838\u5341\u5206\u7684\u81c3\u80bf\u3002\u7ecf\u8fc7\u793e\u533a\u8ba8\u8bba\u540e\u51b3\u5b9a\uff1a

  1. ARM\u7684\u6838\u5fc3\u4ee3\u7801\u4ecd\u7136\u4fdd\u5b58\u5728<arch/arm>\u76ee\u5f55\u4e0b
  2. ARM SOC\u5468\u8fb9\u5916\u8bbe\u6a21\u5757\u7684\u9a71\u52a8\u4ee3\u7801\u4fdd\u5b58\u5728drivers\u76ee\u5f55\u4e0b
  3. ARM SOC board specific\u7684\u4ee3\u7801\u88ab\u79fb\u9664\uff0c\u7531Device Tree\u673a\u5236\u6765\u8d1f\u8d23\u4f20\u9012\u786c\u4ef6\u8d44\u6e90\u4fe1\u606f\u3002

\u672c\u8d28\u4e0a\uff0cDevice Tree\u6539\u53d8\u4e86\u539f\u6765\u7528hardcode\u65b9\u5f0f\u5c06\u786c\u4ef6\u914d\u7f6e\u4fe1\u606f\u5d4c\u5165\u5230\u5185\u6838\u4ee3\u7801\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf\uff0c\u5728\u7cfb\u7edf\u542f\u52a8\u9636\u6bb5\uff0c\u7531bootloader\u901a\u8fc7bootm\u547d\u4ee4\u5c06\u8bbe\u5907\u6811\u4fe1\u606f\u4f20\u9012\u7ed9\u5185\u6838\uff0c\u7136\u540e\u7531\u5185\u6838\u6765\u8bc6\u522b\uff0c\u5e76\u6839\u636e\u5b83\u5c55\u5f00\u51fa\u5185\u6838\u4e2d\u7684platform_device\u3001i2c_client\u3001spi_device\u7b49\u8bbe\u5907\uff0c\u8fd9\u4e9b\u8bbe\u5907\u7528\u5230\u7684\u5185\u5b58\u3001IRQ\u7b49\u8d44\u6e90\u4e5f\u4f1a\u88ab\u4f20\u9012\u7ed9\u5185\u6838\u3002

\u8bbe\u5907\u6811\u6587\u4ef6.dts\u7528\u6765\u63cf\u8ff0\u786c\u4ef6\u4fe1\u606f\u3002\u4e00\u4e2aSOC\u53ef\u4ee5\u5236\u4f5c\u5f88\u591a\u5f00\u53d1\u677f\uff0c\u5c06\u8fd9\u4e9b\u5f00\u53d1\u677f\u7684\u901a\u7528\u4fe1\u606f\u63d0\u53d6\u51fa\u6765\uff0c\u53d8\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684.dtsi\u6587\u4ef6\u3002\u5176\u4ed6\u7684.dts\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u901a\u7528\u6587\u4ef6\uff0c\u5c31\u50cfC\u8bed\u8a00\u4e2d\u7684\u5934\u6587\u4ef6\u3002

\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c.dtsi\u63cf\u8ff0SOC\u4fe1\u606f\uff0c\u6bd4\u5982CPU\u67b6\u6784\uff0c\u4e3b\u9891\u3002.dts\u6587\u4ef6\u63cf\u8ff0\u677f\u7ea7\u4fe1\u606f\uff0c\u6bd4\u5982\u5f00\u53d1\u677f\u4e0a\u6709\u54ea\u4e9bIIC\u8bbe\u5907\u3001SPI\u8bbe\u5907\u7b49\u3002

\u5f00\u53d1\u677f\u7684\u8bbe\u5907\u6811\u6587\u4ef6\u4e00\u822c\u4f4d\u4e8e/boot/dtb/[SOC_NAME]/[BOARD_NAME.dtb]\u3002

\u5982\u679c\u8981\u67e5\u770b\u5f00\u53d1\u677f\u4e2d\u7684\u8bbe\u5907\u6811\u7ed3\u6784\u53ef\u4ee5\u4f7f\u7528\uff1a

ls /sys/firmware/devicetree/base\n

\u6216\u8005

ls /proc/device-tree\n
"},{"location":"linux/drivers/dts/#dtsdtbdtc","title":"DTS\u3001DTB\u548cDTC\u7684\u5173\u7cfb","text":"

dts\u662f\u8bbe\u5907\u6811\u6e90\u7801\u6587\u4ef6\uff0c\u5305\u542b\u7684\u5934\u6587\u4ef6\u4e3adtsi\u3002dts\u6587\u4ef6\u662f\u4eba\u7c7b\u53ef\u4ee5\u770b\u61c2\u7684\uff0c\u4f46\u662fuboot\u548clinux\u65e0\u6cd5\u8bc6\u522b\uff0c\u56e0\u6b64\u9700\u8981dtc\u5c06dts\u6587\u4ef6\u7f16\u8bd1\u6210dtb\u6587\u4ef6\u3002dtb\u6587\u4ef6\u662fuboot\u548clinux\u53ef\u4ee5\u8bc6\u522b\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u5728Linux\u6e90\u7801\u76ee\u5f55\u4e0b/scripts/dtc\u76ee\u5f55\u5305\u542b\u4e86dtc\u5de5\u5177\u7684\u6e90\u7801\u3002

dtc\u7684\u4f7f\u7528\u65b9\u6cd5\u662f\uff1a

dtc -I dts -O dtb -o [output].dtb [input].dts\n

\u53cd\u8fc7\u6765\u53ef\u4ee5\u751f\u6210dts\u6587\u4ef6\uff1a

dtc -I dtb -O dts -o [output].dts [input].dtb\n

make dtbs\u4f1a\u7f16\u8bd1\u6240\u6709\u7684dts\u6587\u4ef6\uff0c\u5982\u679c\u8981\u7f16\u8bd1\u6307\u5b9a\u7684dtb\uff0c\u8bf7\u4f7f\u7528make board_name.dtb\u3002

"},{"location":"linux/drivers/dts/#dts","title":"DTS\u57fa\u672c\u8bed\u6cd5","text":"

\u8bbe\u5907\u6811\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u7531\u4e00\u7cfb\u5217\u7684node\u548cproperty\u7ec4\u6210\u3002\u8fd9\u4e9bnode\u88ab\u7ec4\u7ec7\u6210\u6811\u72b6\u7ed3\u6784\uff0c\u9664\u4e86root node\uff0c\u6bcf\u4e2anode\u90fd\u6709\u4e00\u4e2aparent node\u3002\u6bcf\u4e2anode\u90fd\u5305\u542b\u4e00\u7cfb\u5217\u7684property\uff0c\u7528\u6765\u63cf\u8ff0\u8be5node\u7684\u4e00\u4e9b\u4fe1\u606f\u3002node\u8fd8\u53ef\u4ee5\u5d4c\u5957child node\u3002

label:node-name@unit-address{\n    \u5c5e\u60271 = ...\n    \u5c5e\u60272 = ...\n    \u5c5e\u60273 = ...\n    \u5b50\u8282\u70b9...\n}\n
  • label\uff1a\u7528\u6765\u6307\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u7b7e\uff0c\u65b9\u4fbf\u5f15\u7528\u3002
  • node-name\uff1a\u7528\u6765\u6307\u5b9a\u8282\u70b9\u7684\u540d\u79f0\u3002
  • unit-address\uff1a\u7528\u6765\u6307\u5b9a\u5730\u5740\uff0c\u548c\u6b64\u8282\u70b9\u7684reg\u5c5e\u6027\u7684\u5f00\u59cb\u5730\u5740\u5fc5\u987b\u4e00\u81f4\u3002

\u6ce8\u610f\uff1a\u5982\u679cnode\u4e2d\u6ca1\u6709reg\u5c5e\u6027\uff0c\u5219\u8282\u70b9\u540d\u5b57\u4e2d\u4e0d\u80fd\u6709unit-address\u3002unit-address\u7684\u5177\u4f53\u683c\u5f0f\u548c\u8bbe\u5907\u6302\u5728\u54ea\u4e2abus\u76f8\u5173\u3002\u4f8b\u5982\u5bf9\u4e8eCPU\uff0c\u5176unit-address\u5c31\u662f\u4ece0\u5f00\u59cb\u7f16\u5740\u3002\u800c\u5177\u4f53\u7684\u8bbe\u5907\uff0c\u4f8b\u5982\u4ee5\u592a\u7f51\u63a7\u5236\u5668\uff0c\u5176unit-address\u5c31\u662f\u5bc4\u5b58\u5668\u5730\u5740\u3002

\u5c5e\u6027\u503c\u6807\u8bc6\u4e86\u8bbe\u5907\u7684\u7279\u6027\uff0c\u5b83\u7684\u503c\u53ef\u4ee5\u662f\u4ee5\u4e0b\u51e0\u79cd\uff1a

  1. \u53ef\u80fd\u4e3a\u7a7a\uff0c\u4e5f\u5c31\u662f\u6ca1\u6709\u503c\u7684\u5b9a\u4e49\u3002
  2. \u53ef\u80fd\u662f\u4e00\u4e2au32\u3001u64\u7684\u6570\u503c\uff0c\u4e5f\u53ef\u4ee5\u662f\u6570\u7ec4\u3002
  3. \u53ef\u80fd\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6216\u8005\u662fstring list\u3002
"},{"location":"linux/drivers/dts/#_3","title":"\u7279\u6b8a\u8282\u70b9","text":"

\u522b\u540d\u8282\u70b9aliases\uff1a\u7528\u6765\u7ed9device-node\u5b9a\u4e49\u522b\u540d\uff0c\u56e0\u4e3a\u6bcf\u6b21\u5199\u4e00\u957f\u4e32\u8def\u5f84\u6bd4\u8f83\u9ebb\u70e6\u3002

memory\u8282\u70b9\uff1a\u7528\u6765\u63cf\u8ff0\u7cfb\u7edf\u7269\u7406\u5185\u5b58\u7684layout\u3002device_type\u5c5e\u6027\u5b9a\u4e49\u4e86\u8be5node\u7684\u8bbe\u5907\u7c7b\u578b\uff0c\u4f8b\u5982cpu\u3001serial\u7b49\u3002\u5bf9\u4e8ememory node\uff0c\u5176device_type\u5fc5\u987b\u7b49\u4e8ememory\u3002reg\u5c5e\u6027\u5b9a\u4e49\u4e86\u8bbf\u95ee\u8be5device node\u7684\u5730\u5740\u4fe1\u606f\u2014\u2014\u8d77\u59cb\u5730\u5740\u548c\u957f\u5ea6\u3002

chosen\u8282\u70b9\uff1a\u7528\u6765\u5b9a\u4e49\u542f\u52a8\u53c2\u6570\uff0c\u5176\u7236\u8282\u70b9\u5fc5\u987b\u662f\u6839\u8282\u70b9\u3002\u5185\u6838\u7684\u4e00\u4e9b\u542f\u52a8\u53c2\u6570\u53ef\u4ee5\u901a\u8fc7chosen\u8282\u70b9\u4e0b\u7684bootargs\u5c5e\u6027\u6765\u8bbe\u7f6e\uff0c\u5b83\u53ef\u4ee5\u88abbootloader\u8bfb\u53d6\u3002

"},{"location":"linux/drivers/dts/#_4","title":"\u5c5e\u6027","text":"

\u8282\u70b9\u7531\u4e00\u5806\u5c5e\u6027\u7ec4\u6210\uff0c\u8282\u70b9\u662f\u5177\u4f53\u7684\u8bbe\u5907\uff0c\u4f46\u662f\u4e0d\u540c\u7684\u8bbe\u5907\u6709\u4e0d\u540c\u7684\u5c5e\u6027\uff0c\u4e0d\u8fc7\u6709\u4e00\u4e9b\u662f\u6807\u51c6\u5c5e\u6027\u3002

1.compatible\u5c5e\u6027

compatible\u5c5e\u6027\u7528\u6765\u8868\u793adevice\u548cdriver\u7684\u9002\u914d\uff1a

compatible = \"rockchip, rk3399\";\n

\u8868\u793a\u5382\u5546\u548cSOC\u540d\u3002\u5728\u9a71\u52a8\u6587\u4ef6\u4e2d\u6709\u4e00\u4e2aof\u5339\u914d\u8868\uff0c\u7528\u6765\u5339\u914d\u8bbe\u5907\u8282\u70b9\u548c\u9a71\u52a8\u8282\u70b9\u3002\u5982\u679c\u8bbe\u5907\u8282\u70b9\u7684compatible\u5c5e\u6027\u503c\u548cof\u5339\u914d\u8868\u4e2d\u4efb\u4f55\u4e00\u4e2a\u503c\u76f8\u7b49\uff0c\u90a3\u4e48\u5c31\u8868\u793a\u8be5\u8bbe\u5907\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u9a71\u52a8\u3002

static const struct of_device_id rockchip_rk3399_match[] = {\n    { .compatible = \"rockchip,rk3399\" },\n};\n

compatible\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\u5c5e\u6027\u503c\uff0c\u6309\u7167\u4f18\u5148\u7ea7\u7684\u987a\u5e8f\u8fdb\u884c\u5339\u914d\u3002

2.model\u5c5e\u6027

model\u5c5e\u6027\u7528\u6765\u8868\u793a\u8bbe\u5907\u7684\u578b\u53f7\u3002

3.status\u5c5e\u6027

status\u5c5e\u6027\u7684\u503c\u4e0e\u8bbe\u5907\u72b6\u6001\u6709\u5173\uff0c\u901a\u8fc7\u8bbe\u7f6estatus\u5c5e\u6027\u53ef\u4ee5\u7981\u7528\u6216\u8005\u542f\u7528\u8bbe\u5907\u3002

4.reg\u5c5e\u6027

reg\u5c5e\u6027\u7684\u503c\u4e00\u822c\u662f\u4ee5(address, length)\u5bf9\u7684\u5f62\u5f0f\u51fa\u73b0\u3002\u7528\u4e8e\u63cf\u8ff0\u8bbe\u5907\u8d44\u6e90\u5728\u5176\u7236\u603b\u7ebf\u5b9a\u4e49\u7684\u5730\u5740\u7a7a\u95f4\u5185\u7684\u5730\u5740\u3002

reg = <0x4000e000 0x400>  //\u8d77\u59cb\u5730\u5740+\u5927\u5c0f\n

5.#address-cells\u548c#size-cells\u5c5e\u6027

\u5982\u679c\u4e00\u4e2adevice node\u7684sub node\u6709\u5bfb\u5740\u9700\u6c42\uff08\u5373\u9700\u8981\u5b9a\u4e49reg\u5c5e\u6027\uff09\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5c5e\u6027\u5c31\u5fc5\u987b\u8981\u5b9a\u4e49\uff0c\u7528\u4e8e\u63cf\u8ff0sub node\u7684reg\u5c5e\u6027\u7684\u4fe1\u606f\u3002

#address-cells: \u51b3\u5b9a\u4e86\u5b50\u8282\u70b9reg\u5c5e\u6027\u7684\u5730\u5740\u4fe1\u606f\u6240\u5360\u7528\u7684\u5b57\u957f\n#size-cells\uff1a\u51b3\u5b9a\u4e86\u5b50\u8282\u70b9reg\u5c5e\u6027\u4e2d\u957f\u5ea6\u4fe1\u606f\u6240\u5360\u7684\u5b57\u957f\n

6.ranges\u5c5e\u6027

ranges\u5c5e\u6027\u7684\u503c\u6309\u7167(child-bus-address, parent-bus-address, lenght)\u683c\u5f0f\u7f16\u5199\u3002ranges\u5c5e\u6027\u7528\u6765\u6307\u5b9a\u67d0\u4e2a\u8bbe\u5907\u7684\u5730\u5740\u8303\u56f4\u6216\u8005IO\u8303\u56f4\uff0c\u8fd9\u662f\u5bf9\u8bbe\u5907\u8fdb\u884c\u5bfb\u5740\u7684\u91cd\u8981\u4fe1\u606f\u3002\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7ranges\u5c5e\u6027\u83b7\u77e5\u54ea\u4e9b\u5185\u5b58\u533a\u57df\u6216\u8005IO\u7aef\u53e3\u662f\u88ab\u786c\u4ef6\u8bbe\u5907\u6240\u5360\u7528\u7684\u3002

  • child-bus-address\uff1a\u5b50\u603b\u7ebf\u5730\u5740\u7a7a\u95f4\u7684\u7269\u7406\u5730\u5740\uff0c\u7531\u7236\u8282\u70b9\u7684#address-cells\u786e\u5b9a\u6b64\u7269\u7406\u5730\u5740\u5360\u7528\u7684\u5b57\u957f
  • parent-bus-address\uff1a\u7236\u603b\u7ebf\u5730\u5740\u7a7a\u95f4\u7684\u7269\u7406\u5730\u5740\uff0c\u540c\u6837\u7531\u7236\u8282\u70b9\u7684#address-cells\u786e\u5b9a\u6b64\u7269\u7406\u5730\u5740\u5360\u7528\u7684\u5b57\u957f
  • length\uff1a\u5b50\u5730\u5740\u7a7a\u95f4\u7684\u957f\u5ea6\uff0c\u7531\u7236\u8282\u70b9\u7684#size-cells\u786e\u5b9a\u6b64\u5730\u5740\u957f\u5ea6\u5360\u7528\u7684\u5b57\u957f

\u5982\u679cranges\u5c5e\u6027\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u5b50\u5730\u5740\u7a7a\u95f4\u548c\u7236\u5730\u5740\u7a7a\u95f4\u76f8\u540c\uff0c\u4e0d\u9700\u8981\u8fdb\u884c\u8f6c\u6362\u3002

Arm\u4f53\u7cfb\u6b64\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u7a7a\u3002

7.intc\u5c5e\u6027

\u7528\u4e8e\u8868\u793a\u4e2d\u65ad\u63a7\u5236\u5668\u7684\u76f8\u5173\u4fe1\u606f\u3002

  • interrupt-controller\uff1a
  • interrupt-cells\uff1a
  • interrupt-parent\uff1a
  • interrupts\uff1a

\u6839\u8282\u70b9\u5fc5\u987b\u8981\u6709\u7684\u5c5e\u6027\u6709\uff1a

#address-cells:\u5b50\u8282\u70b9reg\u5c5e\u6027\u4e2d\uff0c\u7528\u591a\u5c11\u4e2au32\u6574\u6570\u6765\u63cf\u8ff0\u5730\u5740\n#size-cells\uff1a\u5b50\u8282\u70b9reg\u5c5e\u6027\u4e2d\uff0c\u7528\u591a\u5c11\u4e2au32\u6574\u6570\u6765\u63cf\u8ff0\u5927\u5c0f\ncompatible\uff1a\u6307\u5b9a\u677f\u5b50\u517c\u5bb9\u7684\u5e73\u53f0\nmodel\uff1a\u677f\u5b50\u540d\u79f0\n
"},{"location":"linux/drivers/dts/#of","title":"\u5185\u6838\u7684of\u51fd\u6570","text":"

\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u6765\u64cd\u4f5c\u8bbe\u5907\u6811\u4e2d\u7684\u8282\u70b9\u548c\u5c5e\u6027\u4fe1\u606f\uff0c\u8fd9\u4e9b\u51fd\u6570\u7edf\u4e00\u4ee5of\u5f00\u5934\uff0c\u5b9a\u4e49\u5728<include/linux/of.h>\u4e2d\u3002

\u5185\u6838\u4f7f\u7528device_node\u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u4e00\u4e2a\u8282\u70b9

struct device_node {\n    const char *name;\n    phandle phandle;\n    const char *full_name;\n    struct fwnode_handle fwnode;\n\n    struct  property *properties;\n    struct  property *deadprops;    /* removed properties */\n    struct  device_node *parent;\n    struct  device_node *child;\n    struct  device_node *sibling;\n#if defined(CONFIG_OF_KOBJ)\n    struct  kobject kobj;\n#endif\n    unsigned long _flags;\n    void    *data;\n#if defined(CONFIG_SPARC)\n    unsigned int unique_id;\n    struct of_irq_controller *irq_trans;\n#endif\n};\n
\u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u5173\u7684of\u51fd\u6570\u67095\u4e2a\uff1a

1.\u901a\u8fc7\u8282\u70b9\u540d\u5b57\u67e5\u627e\u6307\u5b9a\u8282\u70b9

struct device_node *of_find_node_by_name(struct device_node *from, const char *name);\n

from\uff1a\u5f00\u59cb\u67e5\u627e\u7684\u8282\u70b9\uff0cNULL\u5219\u8868\u793a\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u67e5\u627e

name\uff1a\u8981\u67e5\u627e\u7684\u8282\u70b9\u540d

\u8fd4\u56de\u503c\uff1a\u627e\u5230\u7684\u8282\u70b9\uff0cNULL\u8868\u793a\u5931\u8d25

2.\u901a\u8fc7device_type\u5c5e\u6027\u67e5\u627e\u6307\u5b9a\u8282\u70b9\uff08X\uff09

struct device_node *of_find_node_by_type(struct device_node *from, const char *type)\n

type\uff1a\u8981\u67e5\u627e\u7684\u8282\u70b9\u7684device_type\u5c5e\u6027\u503c

\u7531\u4e8edevice_type\u5df2\u7ecf\u88ab\u5e9f\u5f03\uff0c\u6240\u4ee5\u8fd9\u4e2a\u51fd\u6570\u5df2\u7ecf\u4e0d\u7528\u4e86\u3002

3.\u901a\u8fc7device_type\u548ccompatible\u4e24\u4e2a\u5c5e\u6027\u6765\u67e5\u627e\u6307\u5b9a\u8282\u70b9

struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compatible)\n

4.\u901a\u8fc7of_device_id\u5339\u914d\u8868\u6765\u67e5\u627e\u6307\u5b9a\u8282\u70b9

struct device_node *of_find_matching_node_and_match(struct device_node *from, const struct of_device_id *matches, const struct of_device_id **match)\n

5.\u901a\u8fc7\u8def\u5f84\u6765\u67e5\u627e\u6307\u5b9a\u8282\u70b9

inline struct device_node *of_find_node_by_path(const char *path)\n

path\uff1a\u5e26\u6709\u5168\u8def\u5f84\u7684\u8282\u70b9\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528\u8282\u70b9\u7684\u522b\u540d

\u63a8\u8350\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u67e5\u627e\u8282\u70b9\u3002

\u8282\u70b9\u7684\u5c5e\u6027\u4fe1\u606f\u91cc\u4fdd\u5b58\u4e86\u9a71\u52a8\u6240\u9700\u8981\u7684\u5185\u5bb9\uff0c\u5185\u6838\u4e2d\u4f7f\u7528\u7ed3\u6784\u4f53property\u8868\u793a\u5c5e\u6027\u3002

struct property{\n    char *name;    //\u5c5e\u6027\u540d\n    int length;    //\u5c5e\u6027\u503c\u7684\u957f\u5ea6\n    void *value;   //\u6307\u5411\u5c5e\u6027\u503c\u7684\u6307\u9488\n    struct property *next;      //\u6307\u5411\u4e0b\u4e00\u4e2a\u5c5e\u6027\u7684\u6307\u9488\n    unsigned long _flags;       //\u8868\u793a\u5c5e\u6027\u7684\u7c7b\u522b\n    unsigned int unique_id;     //\u6807\u8bc6\u8bbe\u5907\u7684\u552f\u4e00\u5c5e\u6027\n    struct bin_attribute attr;  //\u8868\u793a\u5c5e\u6027\u7684\u4e00\u4e9b\u5143\u6570\u636e\n};\n

\u901a\u8fc7\u4eb2\u7f18\u5173\u7cfb\u6765\u67e5\u627e\u8282\u70b9\uff1a

struct device_node *of_get_parent(const struct device_node *node)\n\nstruct device_node *of_get_next_child(const struct device_node *node, struct device_node *prev)\n\nstruct device_node *of_get_child_by_name(const struct device_node *node, const char *name)\n

\u540c\u65f6\u5185\u6838\u4e5f\u63d0\u4f9b\u4e86\u63d0\u53d6\u5c5e\u6027\u503c\u7684of\u51fd\u6570\u3002

1.\u67e5\u627e\u6307\u5b9a\u7684\u5c5e\u6027

property *of_find_property(const struct device_node *np, const char *name, int *lenp)\n

np\uff1a\u8bbe\u5907\u8282\u70b9

name\uff1a\u5c5e\u6027\u540d\u5b57

lenp\uff1a\u5c5e\u6027\u503c\u7684\u5b57\u8282\u6570

2.\u83b7\u53d6\u5c5e\u6027\u4e2d\u5143\u7d20\u7684\u6570\u91cf

int of_property_count_elems_of_size(const struct device_node *np, const char *propname, int elem_size)\n

3.\u4ece\u5c5e\u6027\u4e2d\u83b7\u53d6\u6307\u5b9a\u6807\u53f7\u7684u32\u7c7b\u578b\u6570\u636e

int of_property_read_u32_index(const struct device_node *np, const char *propname, u32 index, u32 *out_value)\n

propname\uff1a\u8981\u8bfb\u53d6\u7684\u5c5e\u6027\u540d

index\uff1a\u8981\u8bfb\u53d6\u7684\u503c\u6807\u53f7

out_value\uff1a\u8bfb\u53d6\u5230\u7684\u503c

4.\u8bfb\u53d6u8\u3001u16\u3001u32\u3001u64\u6570\u7ec4\u7c7b\u578b\u7684\u6570\u636e

int of_property_read_u8_array(const struct device_node *np, const char *propname, u8 *out_values, size_t sz)\n

\u5c06\u51fd\u6570\u540d\u4e2d\u7684u8\u66ff\u6362\u6210\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u5373\u53ef\u3002

5.\u8bfb\u53d6u8\u3001u16\u3001u32\u3001u64\u7c7b\u578b\u5c5e\u6027\u503c

int of_property_read_u8(const struct device_node *np, const char *propname, u8 *out_value)\n

6.\u8bfb\u53d6\u5c5e\u6027\u4e2d\u5b57\u7b26\u4e32\u7684\u503c

int of_property_read_string(struct device_node *np, const char *propname, const char **out_string)\n

\u8fd9\u4e2a\u51fd\u6570\u4f7f\u7528\u6bd4\u8f83\u7e41\u7410\uff0c\u5efa\u8bae\u4f7f\u7528\u4ee5\u4e0b\u51fd\u6570\uff1a

int of_property_read_string_index(const struct device_node *np,const char *propname, int index,const char **out_string)\n

\u76f8\u6bd4\u524d\u9762\u7684\u51fd\u6570\u589e\u52a0\u4e86\u53c2\u6570index\uff0c\u5b83\u7528\u4e8e\u6307\u5b9a\u8bfb\u53d6\u5c5e\u6027\u503c\u4e2d\u7b2c\u51e0\u4e2a\u5b57\u7b26\u4e32\uff0cindex\u4ece\u96f6\u5f00\u59cb\u8ba1\u6570\u3002 \u7b2c\u4e00\u4e2a\u51fd\u6570\u53ea\u80fd\u5f97\u5230\u5c5e\u6027\u503c\u6240\u5728\u5730\u5740\uff0c\u4e5f\u5c31\u662f\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5730\u5740\uff0c\u5176\u4ed6\u5b57\u7b26\u4e32\u9700\u8981\u6211\u4eec\u624b\u52a8\u4fee\u6539\u79fb\u52a8\u5730\u5740\uff0c\u975e\u5e38\u9ebb\u70e6\uff0c\u63a8\u8350\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u51fd\u6570\u3002

\u73b0\u5728\u5185\u6838\u63d0\u4f9b\u4e86\u5185\u5b58\u6620\u5c04\u76f8\u5173\u7684of\u51fd\u6570\uff0c\u53ef\u4ee5\u81ea\u52a8\u5b8c\u6210\u7269\u7406\u5730\u5740\u5230\u865a\u62df\u5730\u5740\u7684\u8f6c\u6362\uff1a

void __iomem *of_iomap(struct device_node *np, int index)\n

``` gpio1 { gpio-ranges = <&pinctrl 1 0 4>; };

"},{"location":"linux/drivers/gpio/","title":"GPIO\u5b50\u7cfb\u7edf","text":"

GPIO\u5168\u79f0\u201cGeneral Purpose Input/Output\u201d\uff0c\u901a\u7528\u8f93\u5165\u8f93\u51fa\u3002GPIO\u53ef\u80fd\u662f\u82af\u7247\u81ea\u5e26\u7684\uff0c\u4e5f\u53ef\u80fd\u901a\u8fc7I2C\u3001SPI\u63a5\u53e3\u6269\u5c55\u3002

"},{"location":"linux/drivers/gpio/#_1","title":"\u8bbe\u5907\u6811\u63cf\u8ff0","text":"
/*rk3568.dtsi*/\ngpio0: gpio@fdd60000 {\n    compatible = \"rockchip,gpio-bank\";\n    reg = <0x0 0xfdd60000 0x0 0x100>;\n    interrupts = <GIC_SPI 33 IRQ_TYPE_LEVEL_HIGH>;\n    clocks = <&pmucru PCLK_GPIO0>;\n    gpio-controller;\n    #gpio-cells = <2>;\n    interrupt-controller;\n    #interrupt-cells = <2>;\n};\n
"},{"location":"linux/drivers/gpio/#_2","title":"\u6570\u636e\u7ed3\u6784","text":"
struct gpio_device {\n    int         id;\n    struct device       dev;\n    struct cdev     chrdev;\n    struct device       *mockdev;\n    struct module       *owner;\n    struct gpio_chip    *chip;\n    struct gpio_desc    *descs;\n    int         base;\n    u16         ngpio;\n    const char      *label;\n    void            *data;\n    struct list_head        list;\n    struct blocking_notifier_head notifier;\n    struct rw_semaphore sem;\n\n#ifdef CONFIG_PINCTRL\n    /*\n     * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally\n     * describe the actual pin range which they serve in an SoC. This\n     * information would be used by pinctrl subsystem to configure\n     * corresponding pins for gpio usage.\n     */\n    struct list_head pin_ranges;\n#endif\n};\n

\u6bcf\u4e2aGPIO controller\u90fd\u4f1a\u7528\u4e00\u4e2astruct gpio_device\u7ed3\u6784\u4f53\u6765\u8868\u793a\uff0c\u5176\u4e2d\uff1a

  • \u5728struct gpio_chip\u4e2d\u63d0\u4f9b\u5f15\u811a\u64cd\u4f5c\u51fd\u6570

  • \u5728struct gpio_desc\u4e2d\u63d0\u4f9b\u6bcf\u4e2a\u5f15\u811a\u7684\u4fe1\u606f

struct gpio_chip {\n    const char      *label;\n    struct gpio_device  *gpiodev;\n    struct device       *parent;\n    struct module       *owner;\n\n    int         (*request)(struct gpio_chip *gc,\n                        unsigned int offset);\n    void            (*free)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*get_direction)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*direction_input)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*direction_output)(struct gpio_chip *gc,\n                        unsigned int offset, int value);\n    int         (*get)(struct gpio_chip *gc,\n                        unsigned int offset);\n    int         (*get_multiple)(struct gpio_chip *gc,\n                        unsigned long *mask,\n                        unsigned long *bits);\n    void            (*set)(struct gpio_chip *gc,\n                        unsigned int offset, int value);\n    void            (*set_multiple)(struct gpio_chip *gc,\n                        unsigned long *mask,\n                        unsigned long *bits);\n    int         (*set_config)(struct gpio_chip *gc,\n                          unsigned int offset,\n                          unsigned long config);\n    int         (*to_irq)(struct gpio_chip *gc,\n                        unsigned int offset);\n\n    void            (*dbg_show)(struct seq_file *s,\n                        struct gpio_chip *gc);\n\n    int         (*init_valid_mask)(struct gpio_chip *gc,\n                           unsigned long *valid_mask,\n                           unsigned int ngpios);\n\n    int         (*add_pin_ranges)(struct gpio_chip *gc);\n\n    int         base;\n    u16         ngpio;\n    u16         offset;\n    const char      *const *names;\n    bool            can_sleep;\n};\n

label\uff1aGPIO\u63a7\u5236\u5668\u540d\u79f0

gpiodevice\uff1aGPIO\u63a7\u5236\u5668\u8bbe\u5907

parent\uff1aGPIO\u63a7\u5236\u5668\u7236\u8bbe\u5907

base\uff1aGPIO\u5f15\u811a\u57fa\u503c

ngpio\uff1aGPIO\u5f15\u811a\u6570\u91cf

offset\uff1aGPIO\u5f15\u811a\u504f\u79fb

names\uff1aGPIO\u5f15\u811a\u540d\u79f0

can_sleep\uff1a\u662f\u5426\u53ef\u4ee5\u5728\u7761\u7720\u72b6\u6001\u4e0b\u8bbf\u95eeGPIO

\u4f7f\u7528gpiochip_add_data()\u5b8f\u6765\u6ce8\u518cstruct gpio_chip\u3002

\u6bcf\u4e2a\u5f15\u811a\u90fd\u5bf9\u5e94\u4e00\u4e2astruct gpio_desc\uff0c\u5f15\u811a\u4fe1\u606f\u88ab\u4fdd\u5b58\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\uff1a

struct gpio_desc {\n    struct gpio_device  *gdev;\n    unsigned long       flags;\n/* flag symbols are bit numbers */\n#define FLAG_REQUESTED  0\n#define FLAG_IS_OUT 1\n#define FLAG_EXPORT 2   /* protected by sysfs_lock */\n#define FLAG_SYSFS  3   /* exported via /sys/class/gpio/control */\n#define FLAG_ACTIVE_LOW 6   /* value has active low */\n#define FLAG_OPEN_DRAIN 7   /* Gpio is open drain type */\n#define FLAG_OPEN_SOURCE 8  /* Gpio is open source type */\n#define FLAG_USED_AS_IRQ 9  /* GPIO is connected to an IRQ */\n#define FLAG_IRQ_IS_ENABLED 10  /* GPIO is connected to an enabled IRQ */\n#define FLAG_IS_HOGGED  11  /* GPIO is hogged */\n#define FLAG_TRANSITORY 12  /* GPIO may lose value in sleep or reset */\n#define FLAG_PULL_UP    13  /* GPIO has pull up enabled */\n#define FLAG_PULL_DOWN  14  /* GPIO has pull down enabled */\n#define FLAG_BIAS_DISABLE    15 /* GPIO has pull disabled */\n#define FLAG_EDGE_RISING     16 /* GPIO CDEV detects rising edge events */\n#define FLAG_EDGE_FALLING    17 /* GPIO CDEV detects falling edge events */\n#define FLAG_EVENT_CLOCK_REALTIME   18 /* GPIO CDEV reports REALTIME timestamps in events */\n\n    /* Connection label */\n    const char      *label;\n    /* Name of the GPIO */\n    const char      *name;\n#ifdef CONFIG_OF_DYNAMIC\n    struct device_node  *hog;\n#endif\n#ifdef CONFIG_GPIO_CDEV\n    /* debounce period in microseconds */\n    unsigned int        debounce_period_us;\n#endif\n};\n

gdev\uff1a\u5c5e\u4e8e\u54ea\u4e2aGPIO controller

flags\uff1a\u6807\u5fd7\u4f4d\uff0c\u8868\u793a\u5f15\u811a\u7684\u72b6\u6001

label\uff1a\u4e00\u822c\u7b49\u4e8estruct gpio_chip\u7684label

name\uff1a\u5f15\u811a\u540d

"},{"location":"linux/drivers/gpio/#gpio_1","title":"GPIO\u51fd\u6570\u63a5\u53e3","text":"

\u5b9a\u4e49\u5728<include/linux/gpio/consumer.h>\u4e2d\uff0c\u5e38\u7528\u51fd\u6570\u6709\uff1a

\u83b7\u5f97GPIO\uff1a

struct gpio_desc *__must_check gpiod_get(struct device *dev,\n                     const char *con_id,\n                     enum gpiod_flags flags);\nstruct gpio_desc *__must_check gpiod_get_index(struct device *dev,\n                           const char *con_id,\n                           unsigned int idx,\n                           enum gpiod_flags flags);\nstruct gpio_descs *__must_check gpiod_get_array(struct device *dev,\n                        const char *con_id,\n                        enum gpiod_flags flags);\n\nstruct gpio_desc *__must_check devm_gpiod_get(struct device *dev,\n                          const char *con_id,\n                          enum gpiod_flags flags);\nstruct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,\n                            const char *con_id,\n                            unsigned int idx,\n                            enum gpiod_flags flags);\nstruct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,\n                             const char *con_id,\n                             enum gpiod_flags flags);\n

\u8bbe\u7f6e\u65b9\u5411\uff1a

int gpiod_direction_input(struct gpio_desc *desc);\nint gpiod_direction_output(struct gpio_desc *desc, int value);\n

\u8bfb\u5199\u503c\uff1a

int gpiod_get_value(const struct gpio_desc *desc);\nvoid gpiod_set_value(struct gpio_desc *desc, int value);\n\n\n\u91ca\u653eGPIO\uff1a\n```C\nvoid gpiod_put(struct gpio_desc *desc);\nvoid gpiod_put_array(struct gpio_descs *descs);\n

GPIO\u63cf\u8ff0\u7b26\u6807\u5fd7\u4f4d\uff1a

enum gpiod_flags {\n    GPIOD_ASIS  = 0,\n    GPIOD_IN    = GPIOD_FLAGS_BIT_DIR_SET,\n    GPIOD_OUT_LOW   = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,\n    GPIOD_OUT_HIGH  = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |\n              GPIOD_FLAGS_BIT_DIR_VAL,\n    GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,\n    GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,\n};\n

\u8981\u64cd\u4f5c\u4e00\u4e2a\u5f15\u811a\uff0c\u9996\u5148\u8981\u83b7\u53d6\u8fd9\u4e2a\u5f15\u811a\uff0c\u7136\u540e\u8bbe\u7f6e\u65b9\u5411\uff0c\u8bfb\u503c\u6216\u8005\u5199\u503c\u3002

"},{"location":"linux/drivers/gpio/#pinctrl","title":"\u4e0ePinctrl\u5b50\u7cfb\u7edf\u4ea4\u4e92","text":"
  1. \u5728GPIO\u8bbe\u5907\u6811\u4e2d\u4f7f\u7528gpio-ranges\u6765\u63cf\u8ff0\u5b83\u4eec\u4e4b\u95f4\u7684\u8054\u7cfb
  2. \u89e3\u6790\u8fd9\u4e9b\u8054\u7cfb\uff0c\u5728\u6ce8\u518cstruct gpio-chip\u65f6\u81ea\u52a8\u8c03\u7528
  3. \u5728GPIO\u9a71\u52a8\u7a0b\u5e8f\u4e2d\uff0c\u63d0\u4f9bgpio_chip->request\u51fd\u6570\uff1b\u5728Pinctrl\u5b50\u7cfb\u7edf\u4e2d\uff0c\u63d0\u4f9bpmxops->gpio_request_enable\u51fd\u6570\u6216\u8005pmxops->request\u51fd\u6570
"},{"location":"linux/drivers/i2c/","title":"I2C\u5b50\u7cfb\u7edf","text":""},{"location":"linux/drivers/i2c/#i2c_1","title":"I2C\u534f\u8bae","text":"

i2c\u7269\u7406\u603b\u7ebf\u793a\u610f\u56fe\u5982\u4e0b\u6240\u793a\uff1a

i2c\u652f\u6301\u4e3b\u4ece\u6a21\u5f0f\uff0c\u5176\u4e2d\u4e00\u4e2a\u4e3b\u8bbe\u5907\u3001\u591a\u4e2a\u4ece\u8bbe\u5907\uff0c\u5404\u8bbe\u5907\u5730\u5740\u72ec\u7acb\u3002\u4e3b\u8bbe\u5907\u8d1f\u8d23\u8c03\u5ea6\u603b\u7ebf\uff0c\u51b3\u5b9a\u67d0\u4e2a\u4e8b\u4ef6\u548c\u5176\u4e2d\u4e00\u4e2a\u4ece\u8bbe\u5907\u901a\u4fe1\uff0c\u5176\u4ed6\u4ece\u8bbe\u5907\u8981\u60f3\u901a\u4fe1\u53ea\u80fd\u7b49\u5f85\u3002\u5f53SCL\u4e0eSDA\u540c\u4e3a\u9ad8\u7535\u5e73\u65f6\uff0c\u4e3a\u7a7a\u95f2\u6001\u3002

i2c\u7269\u7406\u603b\u7ebf\u4f7f\u7528\u4e24\u6761\u603b\u7ebf\u7ebf\u8def\uff1a

  • SCL:\u65f6\u949f\u7ebf\uff0c\u4f20\u8f93CLK\u4fe1\u53f7
  • SDA\uff1a\u6570\u636e\u7ebf\uff0c\u901a\u4fe1\u6570\u636e\u90fd\u901a\u8fc7SDA

\u901a\u4fe1\u7279\u5f81\uff1a

  • \u4e32\u884c\uff1a\u53ea\u6709\u4e00\u6839\u6570\u636e\u7ebf
  • \u540c\u6b65\uff1a\u4e3b\u4ece\u8bbe\u5907\u5728\u540c\u4e00\u4e2a\u65f6\u949f\u9891\u7387\u4e0b\u5de5\u4f5c
  • \u975e\u5dee\u5206\uff1a\u56e0\u4e3aI2C\u901a\u4fe1\u901f\u7387\u4e0d\u9ad8\uff0c\u800c\u4e14\u901a\u4fe1\u53cc\u65b9\u8ddd\u79bb\u5f88\u8fd1\uff0c\u4e00\u822c\u662f\u677f\u7ea7\u901a\u4fe1\uff0c\u6240\u4ee5\u4f7f\u7528\u7535\u5e73\u4fe1\u53f7\u901a\u4fe1
  • \u4f4e\u901f\u7387\uff1a\u4f20\u8f93\u7684\u6570\u636e\u91cf\u4e0d\u5927\uff0c\u672c\u8eab\u901a\u4fe1\u901f\u7387\u5f88\u4f4e

i2c\u7684\u901a\u4fe1\u8fc7\u7a0b\uff1a

  1. \u5f00\u59cb\u4fe1\u53f7\uff08S\uff09\uff1aSDA\u7ebf\u4ece\u9ad8\u7535\u5e73\u5230\u4f4e\u7535\u5e73\u8df3\u53d8\uff0c\u540c\u65f6SCL\u7ebf\u4fdd\u6301\u9ad8\u7535\u5e73\u3002
  2. \u53d1\u9001\u5730\u5740\u548c\u8bfb/\u5199\u4f4d\uff1a\u4e3b\u8bbe\u5907\u901a\u8fc7SDA\u7ebf\u53d1\u9001\u4e00\u4e2a7\u4f4d\u8bbe\u5907\u5730\u5740\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u8bfb/\u5199\u4f4d\u3002
  3. \u54cd\u5e94\u4fe1\u53f7(ACK)\uff1a\u6bcf\u4e00\u6b21\u4f20\u8f93\u5fc5\u987b\u4f34\u6709\u4e00\u4e2a\u54cd\u5e94\u4fe1\u53f7\uff0c\u5728SCL\u4e3a\u9ad8\u65f6\uff0c\u901a\u8fc7\u62c9\u4f4eSDA\u5e76\u4fdd\u6301\u4f4e\u6765\u5b9e\u73b0\u3002\u5982\u679c\u4ece\u8bbe\u5907\u5fd9\uff0c\u5b83\u53ef\u4ee5\u4f7fSCL\u4fdd\u6301\u5728\u4f4e\u7535\u5e73\uff0c\u8fd9\u4f1a\u5f3a\u5236\u4f7f\u4e3b\u8bbe\u5907\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u3002\u5f53\u4ece\u8bbe\u5907\u7a7a\u95f2\u540e\uff0c\u5e76\u4e14\u91ca\u653e\u65f6\u949f\u7ebf\uff0c\u539f\u6765\u7684\u6570\u636e\u4f20\u8f93\u624d\u4f1a\u7ee7\u7eed\u3002
  4. \u6570\u636e\u4f20\u8f93\uff1aSDA\u4e0a\u4f20\u8f93\u7684\u6570\u636e\u5fc5\u987b\u5728SCL\u4e3a\u9ad8\u7535\u5e73\u671f\u95f4\u4fdd\u6301\u7a33\u5b9a\uff0cSDA\u4e0a\u7684\u6570\u636e\u53ea\u80fd\u5728SCL\u4e3a\u4f4e\u7535\u5e73\u65f6\u53d8\u5316\u3002
  5. \u505c\u6b62\u4fe1\u53f7(P)\uff1a\u6570\u636e\u4f20\u8f93\u7ed3\u675f\u65f6\uff0cSDA\u7ebf\u4ece\u4f4e\u7535\u5e73\u5230\u9ad8\u7535\u5e73\u7684\u8df3\u53d8\uff0c\u540c\u65f6SCL\u7ebf\u4fdd\u6301\u9ad8\u7535\u5e73\uff0c\u8868\u793a\u901a\u4fe1\u7ed3\u675f\u3002

\u5bf9\u4e8e\u7b2c4\u70b9\u89e3\u91ca\u4e00\u4e0b\uff1a

SDA\u4e0a\u4f20\u8f93\u7684\u6570\u636e\u5fc5\u987b\u5728SCL\u4e3a\u9ad8\u7535\u5e73\u671f\u95f4\u4fdd\u6301\u7a33\u5b9a\uff1a

  • \u5728I2C\u901a\u4fe1\u4e2d\uff0cSCL\u8d1f\u8d23\u540c\u6b65\u6570\u636e\u4f20\u8f93\u3002\u5f53SCL\u7ebf\u5904\u4e8e\u9ad8\u7535\u5e73\u65f6\uff0c\u8868\u793a\u6570\u636e\u7ebfSDA\u4e0a\u7684\u6570\u636e\u7a33\u5b9a\u6709\u6548\u3002
  • \u8fd9\u610f\u5473\u7740\u5728SCL\u4e3a\u9ad8\u7535\u5e73\u7684\u671f\u95f4\uff0cSDA\u7ebf\u4e0a\u7684\u6570\u636e\u5fc5\u987b\u4fdd\u6301\u4e0d\u53d8\uff0c\u4efb\u4f55\u8bd5\u56fe\u6539\u53d8SDA\u7ebf\u72b6\u6001\u7684\u64cd\u4f5c\u90fd\u5fc5\u987b\u7b49\u5230SCL\u7ebf\u53d8\u4e3a\u4f4e\u7535\u5e73\u3002

SDA\u4e0a\u7684\u6570\u636e\u53ea\u80fd\u5728SCL\u4e3a\u4f4e\u7535\u5e73\u65f6\u53d8\u5316\uff1a

  • \u5f53SCL\u7ebf\u5904\u4e8e\u4f4e\u7535\u5e73\u65f6\uff0c\u8868\u793a\u6570\u636e\u7ebfSDA\u53ef\u4ee5\u53d8\u5316\uff0c\u6b64\u65f6\u4e3b\u673a\u6216\u4ece\u8bbe\u5907\u53ef\u4ee5\u6539\u53d8SDA\u7ebf\u7684\u72b6\u6001\uff0c\u6bd4\u5982\u53d1\u9001\u6216\u63a5\u6536\u6570\u636e\u3002
  • \u6570\u636e\u7ebfSDA\u4e0a\u7684\u53d8\u5316\uff08\u6bd4\u5982\u4ece\u9ad8\u7535\u5e73\u5230\u4f4e\u7535\u5e73\uff0c\u6216\u4ece\u4f4e\u7535\u5e73\u5230\u9ad8\u7535\u5e73\uff09\u90fd\u5fc5\u987b\u53d1\u751f\u5728SCL\u7ebf\u7684\u4e0a\u5347\u6cbf\u6216\u4e0b\u964d\u6cbf\u3002

\u8fde\u7eed\u5b57\u8282\u7684\u5199\u5165\u5982\u4e0b\u56fe\u6240\u793a\uff1a

"},{"location":"linux/drivers/i2c/#_1","title":"\u8bbe\u5907\u6811\u63cf\u8ff0","text":"
i2c0: i2c@fdd40000 {\n    compatible = \"rockchip,rk3568-i2c\", \"rockchip,rk3399-i2c\";\n    reg = <0x0 0xfdd40000 0x0 0x1000>;\n    interrupts = <GIC_SPI 46 IRQ_TYPE_LEVEL_HIGH>;\n    clocks = <&pmucru CLK_I2C0>, <&pmucru PCLK_I2C0>;\n    clock-names = \"i2c\", \"pclk\";\n    pinctrl-0 = <&i2c0_xfer>;\n    pinctrl-names = \"default\";\n    #address-cells = <1>;\n    #size-cells = <0>;\n    status = \"disabled\";\n};\n
"},{"location":"linux/drivers/i2c/#_2","title":"\u6570\u636e\u7ed3\u6784","text":"
  • i2c\u6838\u5fc3\uff1a\u7ba1\u7406i2c\u9a71\u52a8\u548ci2c\u8bbe\u5907\u7684\u5339\u914d\u3001\u5220\u9664
  • i2c\u8bbe\u5907\uff1ai2c\u786c\u4ef6\u8bbe\u5907\u7684\u62bd\u8c61
  • i2c\u9a71\u52a8\uff1ai2c\u8bbe\u5907\u7684\u9a71\u52a8\u7a0b\u5e8f
  • i2c\u9002\u914d\u5668\uff1ai2c\u63a7\u5236\u5668\uff0c\u7528\u4e8e\u9a71\u52a8\u548c\u8bbe\u5907\u4e4b\u95f4\u7684\u901a\u4fe1

i2c\u9a71\u52a8\u4e2d\u6700\u6838\u5fc3\u7684\u56db\u4e2a\u6570\u636e\u7ed3\u6784\uff1a

  • struct i2c_adapter
  • struct i2c_algorithm
  • struct i2c_client
  • struct i2c-driver

struct i2c_adapter\u5bf9\u5e94\u4e00\u4e2ai2c controller\uff0c\u6240\u6709\u7684\u8bbe\u5907\u9a71\u52a8\u90fd\u9700\u8981\u7ecf\u8fc7struct i2c_adapter\u5bf9\u8c61\u7684\u5904\u7406\u624d\u80fd\u4e0e\u7269\u7406\u8bbe\u5907\u901a\u4fe1\uff1a

struct i2c_adapter {\n    struct module *owner;\n    unsigned int class;       /* classes to allow probing for */\n    const struct i2c_algorithm *algo; /* the algorithm to access the bus */\n    void *algo_data;\n\n    /* data fields that are valid for all devices   */\n    const struct i2c_lock_operations *lock_ops;\n    struct rt_mutex bus_lock;\n    struct rt_mutex mux_lock;\n\n    int timeout;            /* in jiffies */\n    int retries;\n    struct device dev;      /* the adapter device */\n    unsigned long locked_flags; /* owned by the I2C core */\n#define I2C_ALF_IS_SUSPENDED        0\n#define I2C_ALF_SUSPEND_REPORTED    1\n\n    int nr;\n    char name[48];\n    struct completion dev_released;\n\n    struct mutex userspace_clients_lock;\n    struct list_head userspace_clients;\n\n    struct i2c_bus_recovery_info *bus_recovery_info;\n    const struct i2c_adapter_quirks *quirks;\n\n    struct irq_domain *host_notify_domain;\n    struct regulator *bus_regulator;\n};\n

struct i2c_algorithm\u5b9a\u4e49\u4e86i2c\u63a7\u5236\u5668\u4e0ei2c\u8bbe\u5907\u4e4b\u95f4\u901a\u4fe1\u7684\u7b97\u6cd5\uff0c\u6700\u91cd\u8981\u7684\u6210\u5458\u662fmaster_xfer()\u51fd\u6570\uff0c\u7528\u6765\u5c06\u6570\u636e\u53d1\u9001\u5230i2c controller\uff1a

struct i2c_algorithm {\n    /*\n     * If an adapter algorithm can't do I2C-level access, set master_xfer\n     * to NULL. If an adapter algorithm can do SMBus access, set\n     * smbus_xfer. If set to NULL, the SMBus protocol is simulated\n     * using common I2C messages.\n     *\n     * master_xfer should return the number of messages successfully\n     * processed, or a negative value on error\n     */\n    int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,\n               int num);\n    int (*master_xfer_atomic)(struct i2c_adapter *adap,\n                   struct i2c_msg *msgs, int num);\n    int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,\n              unsigned short flags, char read_write,\n              u8 command, int size, union i2c_smbus_data *data);\n    int (*smbus_xfer_atomic)(struct i2c_adapter *adap, u16 addr,\n                 unsigned short flags, char read_write,\n                 u8 command, int size, union i2c_smbus_data *data);\n\n    /* To determine what the adapter supports */\n    u32 (*functionality)(struct i2c_adapter *adap);\n\n#if IS_ENABLED(CONFIG_I2C_SLAVE)\n    int (*reg_slave)(struct i2c_client *client);\n    int (*unreg_slave)(struct i2c_client *client);\n#endif\n};\n

master_xfer\uff1a\u4f5c\u4e3a\u4e3b\u8bbe\u5907\u65f6\u7684\u53d1\u9001\u51fd\u6570

smbus_xfer\uff1a\u4f5c\u4e3a\u4ece\u8bbe\u5907\u65f6\u7684\u63a5\u6536\u51fd\u6570

struct i2c_client\u8868\u793a\u8fde\u63a5\u5230i2c\u603b\u7ebf\u4e0a\u7684\u8bbe\u5907\uff0c\u662f\u5177\u4f53\u786c\u4ef6\u8bbe\u5907\u7684\u62bd\u8c61\uff1a

struct i2c_client {\n    unsigned short flags;       /* div., see below      */\n    unsigned short addr;        /* chip address - NOTE: 7bit    */\n    char name[I2C_NAME_SIZE];\n    struct i2c_adapter *adapter;    /* the adapter we sit on    */\n    struct device dev;      /* the device structure     */\n    int init_irq;           /* irq set at initialization    */\n    int irq;            /* irq issued by device     */\n    struct list_head detected;\n#if IS_ENABLED(CONFIG_I2C_SLAVE)\n    i2c_slave_cb_t slave_cb;    /* callback for slave mode  */\n#endif\n    void *devres_group_id;      /* ID of probe devres group */\n};\n

struct i2c_driver\u8868\u793ai2c\u8bbe\u5907\u6240\u5bf9\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\uff1a

struct i2c_driver {\n    unsigned int class;\n\n    /* Standard driver model interfaces */\n    int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);\n    int (*remove)(struct i2c_client *client);\n\n    /* New driver model interface to aid the seamless removal of the\n     * current probe()'s, more commonly unused than used second parameter.\n     */\n    int (*probe_new)(struct i2c_client *client);\n\n    /* driver model interfaces that don't relate to enumeration  */\n    void (*shutdown)(struct i2c_client *client);\n\n    /* Alert callback, for example for the SMBus alert protocol.\n     * The format and meaning of the data value depends on the protocol.\n     * For the SMBus alert protocol, there is a single bit of data passed\n     * as the alert response's low bit (\"event flag\").\n     * For the SMBus Host Notify protocol, the data corresponds to the\n     * 16-bit payload data reported by the slave device acting as master.\n     */\n    void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,\n              unsigned int data);\n\n    /* a ioctl like command that can be used to perform specific functions\n     * with the device.\n     */\n    int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);\n\n    struct device_driver driver;\n    const struct i2c_device_id *id_table;\n\n    /* Device detection callback for automatic device creation */\n    int (*detect)(struct i2c_client *client, struct i2c_board_info *info);\n    const unsigned short *address_list;\n    struct list_head clients;\n};\n

\u5728\u5e73\u53f0\u8bbe\u5907\u9a71\u52a8\u4e00\u7ae0\u4e2d\u6211\u4eec\u63cf\u8ff0\u4e86\u5e73\u53f0\u603b\u7ebf\u7684\u6982\u5ff5\uff0ci2c\u603b\u7ebf\u6709\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u7528\u6765\u7ba1\u7406i2c\u8bbe\u5907\u548ci2c\u9a71\u52a8\u7684\u5339\u914d\u548c\u5220\u9664\u64cd\u4f5c\uff1a

/*drivers/i2c/i2c-core.c*/\nstruct bus_type i2c_bus_type = {\n    .name       = \"i2c\",\n    .match      = i2c_device_match,\n    .probe      = i2c_device_probe,\n    .remove     = i2c_device_remove,\n    .shutdown   = i2c_device_shutdown,\n};\n

\u591a\u4e2a\u8bbe\u5907\u53ef\u4ee5\u6302\u5728\u540c\u4e00\u4e2ai2c\u603b\u7ebf\u4e0a\uff0ci2c\u603b\u7ebf\u9a71\u52a8\u7531\u82af\u7247\u5382\u5546\u63d0\u4f9b\u3002

struct i2c_msg\u7ed3\u6784\u4f53\u8868\u793ai2c\u4f20\u8f93\u7684\u6d88\u606f\u3002

struct i2c_msg {\n    __u16 addr;\n    __u16 flags;\n    ...\n    __u16 len;\n    __u8 *buf;\n};\n

addr\uff1a\u4ece\u8bbe\u5907\u5730\u5740

flags\uff1a\u6807\u5fd7\u4f4d

len\uff1a\u4f20\u8f93\u6570\u636e\u7684\u957f\u5ea6

buf\uff1a\u4f20\u8f93\u6570\u636e\u7f13\u51b2\u533a

"},{"location":"linux/drivers/i2c/#i2c_2","title":"I2C\u51fd\u6570\u63a5\u53e3","text":"

\u5411\u5185\u6838\u6ce8\u518c/\u6ce8\u9500\u4e00\u4e2ai2c_adapter\uff1a

int i2c_add_adapter(struct i2c_adapter *adap)\nint i2c_del_adapter(struct i2c_adapter *adap)\n

\u6ce8\u518c/\u6ce8\u9500\u4e00\u4e2ai2c\u9a71\u52a8\uff1a

int i2c_add_driver(struct i2c_driver *driver)\nint i2c_del_driver(struct i2c_driver *driver)\n

\u4f20\u8f93\u6570\u636e\uff1a

int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)\n

"},{"location":"linux/drivers/input/","title":"Input\u5b50\u7cfb\u7edf","text":""},{"location":"linux/drivers/netdev/","title":"\u7f51\u7edc\u8bbe\u5907\u9a71\u52a8","text":""},{"location":"linux/drivers/pinctrl/","title":"Pinctrl\u5b50\u7cfb\u7edf","text":"

Pinctrl\uff1aPin Controller\uff0c\u5373\u5f15\u811a\u63a7\u5236\uff0c\u901a\u4fd7\u5730\u6765\u8bf4\u5c31\u662f\u4e00\u7ec4\u53ef\u4ee5\u63a7\u5236\u5f15\u811a\u7684\u5bc4\u5b58\u5668\u96c6\u5408\u3002\u5b83\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e09\u79cd\u529f\u80fd\uff1a

  • \u5f15\u811a\u679a\u4e3e\u4e0e\u547d\u540d\uff0c\u6bcf\u4e2apin\u7684ID\u552f\u4e00
  • \u5f15\u811a\u590d\u7528\uff0c\u6bd4\u5982\u5355\u4e2a\u5f15\u811a\u53ef\u4ee5\u914d\u7f6e\u6210GPIO\uff0c\u591a\u4e2a\u5f15\u811a\u8fd8\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2apin group\uff0c\u5f62\u6210\u7279\u5b9a\u7684\u529f\u80fd
  • \u5f15\u811a\u914d\u7f6e\uff0c\u6bd4\u5982\u4f7f\u80fd\u6216\u7981\u6b62\u5f15\u811a\u7684\u4e0a\u62c9\u3001\u4e0b\u62c9\u7535\u963b

pinctrl\u5b50\u7cfb\u7edf\u6d89\u53ca\u5230\u4e24\u4e2a\u5bf9\u8c61\uff1a

  • pin controller device\uff1a\u63d0\u4f9b\u5f15\u811a\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u7531\u539f\u5382\u5de5\u7a0b\u5e08\u63d0\u4f9b\u3002

  • client device\uff1a\u4f7f\u7528pinctrl\u5b50\u7cfb\u7edf\u7684\u8bbe\u5907\uff0c\u9700\u8981\u63cf\u8ff0\u4f7f\u7528\u7684\u5f15\u811a\uff0c\u7531\u9a71\u52a8\u5de5\u7a0b\u5e08\u7f16\u5199\u3002

\u5185\u6838\u5206\u522b\u62bd\u8c61\u51fastruct pinctrl_dev\u548cstruct device\u6765\u8868\u793a\u4e0a\u9762\u4e24\u4e2a\u5bf9\u8c61\u4fe1\u606f\u3002

"},{"location":"linux/drivers/pinctrl/#_1","title":"\u6e90\u6587\u4ef6\u5217\u8868","text":"

pinctrl\u5b50\u7cfb\u7edf\u76f8\u5173\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u5982\u4e0b\uff1a

1.\u6e90\u6587\u4ef6\u5217\u8868

\u6587\u4ef6\u540d \u63cf\u8ff0 core.h \u6838\u5fc3\u6570\u636e\u7ed3\u6784\u5934\u6587\u4ef6 pinctrl.h \u63a5\u53e3\u6587\u4ef6 pinmux.h \u5f15\u811a\u590d\u7528\u5934\u6587\u4ef6 pinconf.h \u5f15\u811a\u914d\u7f6e\u5934\u6587\u4ef6

2.\u548c\u5176\u4ed6\u5185\u6838\u6a21\u5757\u63a5\u53e3\u6587\u4ef6\u5217\u8868

\u6587\u4ef6\u540d \u63cf\u8ff0 consumer.h \u5176\u4ed6\u7684driver\u8981\u4f7f\u7528pinctrl\u5b50\u7cfb\u7edf\u65f6\uff0c\u9700\u8981\u5305\u542b\u8fd9\u4e2a\u5934\u6587\u4ef6 devinfo.h linux\u5185\u6838\u9a71\u52a8\u8bbe\u5907\u6a21\u578b\u6a21\u5757\u4f7f\u7528\u7684\u63a5\u53e3 machine.h \u548cmachine\u6a21\u5757\u7684\u63a5\u53e3

3.low level pin controller driver\u63a5\u53e3\u6587\u4ef6\u5217\u8868

\u6587\u4ef6\u540d \u63cf\u8ff0 pinconf-generic.h \u5f15\u811a\u914d\u7f6e\u7684\u901a\u7528\u63a5\u53e3 pinconf.h \u5f15\u811a\u914d\u7f6e\u7684\u63a5\u53e3 pinctrl-state.h \u4fdd\u5b58pin controller\u7684\u72b6\u6001"},{"location":"linux/drivers/pinctrl/#_2","title":"\u8bbe\u5907\u6811\u63cf\u8ff0","text":"

\u5f15\u811a\u914d\u7f6e\u7684\u8bbe\u5907\u6811\u63cf\u8ff0\u5982\u4e0b\uff1a

/*s5pv210-pinctrl.dtsi*/\n&pinctrl0 {\n    gpa0: gpa0 {\n        gpio-controller;        /*\u8fd9\u662f\u4e00\u4e2aGPIO\u63a7\u5236\u5668*/\n        #gpio-cells = <2>;      /*\u9700\u89812\u4e2a32\u4f4d\u7684\u6570\u6765\u63cf\u8ff0\u8fd9\u4e2aGPIO*/\n\n        interrupt-controller;   /*\u8fd9\u662f\u4e00\u4e2a\u4e2d\u65ad\u63a7\u5236\u5668*/\n        #interrupt-cells = <2>;\n    };\n\n    gpa1: gpa1 {\n        gpio-controller;\n        #gpio-cells = <2>;\n\n        interrupt-controller;\n        #interrupt-cells = <2>;\n    };\n};\n\n/*\u7701\u7565\u5176\u4ed6\u5f15\u811a\u914d\u7f6e*/\n

\u9664\u4e86\u5355\u4e2a\u5f15\u811a\u7684\u63cf\u8ff0\uff0c\u8fd8\u53ef\u4ee5\u5c06\u591a\u4e2a\u5f15\u811a\u7ec4\u5408\u5728\u4e00\u8d77\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u6bd4\u5982SPI\u63a5\u53e3\u3001I2C\u63a5\u53e3\u7b49\uff0c\u8fd9\u88ab\u79f0\u4e3a\u5f15\u811a\u7ec4\uff08pin group\uff09\uff0c\u5176\u8bbe\u5907\u6811\u63cf\u8ff0\u5982\u4e0b\uff1a

/*s5pv210-pinctrl.dtsi*/\nuart0_data: uart0-data {\n    samsung,pins = \"gpa0-0\", \"gpa0-1\";\n    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;\n    samsung,pin-pud = <S3C64XX_PIN_PULL_NONE>;\n    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;\n};\n\n...\ni2c0_bus: i2c0-bus {\n    samsung,pins = \"gpd1-0\", \"gpd1-1\";\n    samsung,pin-function = <EXYNOS_PIN_FUNC_2>;\n    samsung,pin-pud = <S3C64XX_PIN_PULL_UP>;\n    samsung,pin-drv = <EXYNOS4_PIN_DRV_LV1>;\n};\n

\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cuart0\u7684\u5f15\u811a\u7ec4\u914d\u7f6e\u9700\u8981\u7528\u5230\"gpa0-0, gpa0-1\"\uff0ci2c0\u7684\u5f15\u811a\u7ec4\u914d\u7f6e\u9700\u8981\u7528\u5230\"gpd1-0\", \"gpd1-1\"\u3002\u4e00\u65e6\u9009\u62e9\u4e86\u67d0\u4e2a\u529f\u80fd\uff0cpins\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u5f15\u811a\u90fd\u9700\u8981\u5728pin-function\u4e2d\u505a\u76f8\u5e94\u7684\u529f\u80fd\u8bbe\u5b9a\uff0c\u5177\u4f53\u8bbe\u5b9a\u7684\u503c\u9700\u8981\u5728\u82af\u7247\u624b\u518c\u4e2d\u67e5\u627e\u3002

\u8fd8\u6709\u4e00\u4e9b\u5f15\u811a\u914d\u7f6e\u5c5e\u6027\u6bd4\u5982\uff1a

  • samsung,pin-val\uff1a\u5f15\u811a\u8f93\u51fa\u7f13\u51b2\u533a\u7684\u521d\u59cb\u503c
  • samsung,pin-pud\uff1a\u4e0a\u4e0b\u62c9\u914d\u7f6e
  • samsung,pin-drv\uff1a\u9a71\u52a8\u5668\u5f3a\u5ea6\u914d\u7f6e
  • samsung,pin-pud-pdn\uff1a\u4f4e\u529f\u8017\u6a21\u5f0f\u4e0b\u7684\u4e0a\u4e0b\u62c9\u914d\u7f6e
  • samsung,pin-drv-pdn\uff1a\u4f4e\u529f\u8017\u6a21\u5f0f\u4e0b\u7684\u9a71\u52a8\u5668\u5f3a\u5ea6\u914d\u7f6e

\u5177\u4f53\u7684\u914d\u7f6e\u4fe1\u606f\u6bcf\u4e2aSOC\u5382\u5546\u90fd\u6709\u4e0d\u540c\u7684\u6807\u51c6\uff0c\u9664\u975e\u4f60\u662f\u539f\u5382\u5de5\u7a0b\u5e08\uff0c\u5426\u5219\u53ea\u9700\u8981\u4f9d\u846b\u82a6\u753b\u74e2\u5373\u53ef\u3002

\u4e00\u4e2a\u5178\u578b\u7684client device\u5f15\u7528\u5f15\u811a\u914d\u7f6e\u7684\u5b9e\u4f8b\u5982\u4e0b\uff1a

device-node-name {  \n    pinctrl-names = \"default\", \"init\", \"sleep\";   \n    pinctrl-0 = <pin-config-0-a>; \n    pinctrl-1 = <pin-config-1-b>; \n    pinctrl-2 = <pin-config-2-c>;        \n};\n

\u8fd9\u91ccpinctrl-names\u5c31\u8868\u793a\u5f15\u811a\u7684state\u2014\u2014default, init, sleep\u7b49\u3002\u5bf9\u4e8e\u67d0\u4e2aclient device\uff0c\u5b83\u4f7f\u7528\u7684\u4e00\u7ec4\u5f15\u811a\u5e94\u8be5\u540c\u65f6\u5904\u4e8e\u67d0\u79cdstate\u4e0b\uff0cstate\u7684\u5b9a\u4e49\u4e0e\u7535\u6e90\u7ba1\u7406\u7cfb\u7edf\u76f8\u5173\uff08Power Management\uff09\u3002\u6bd4\u5982\u5f53\u8bbe\u5907\u8fdb\u5165\u7761\u7720\u72b6\u6001\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u5f15\u811a\u72b6\u6001\u4ee5\u8282\u7701\u529f\u8017\u3002\u6bcf\u4e2astate\u7531\u4e0b\u9762\u76840\u30011\u30012\u914d\u7f6e\u5bf9\u5e94\u3002pinctrl-x\u662f\u4e00\u4e2a\u53e5\u67c4\uff08phandle\uff09\u5217\u8868\uff0c\u6bcf\u4e2a\u53e5\u67c4\u6307\u5411\u4e00\u4e2apin configuration\u3002

\u7531\u8bbe\u5907\u6811\u7684\u77e5\u8bc6\u6211\u4eec\u77e5\u9053\uff0c\u6bcf\u4e2a\u8bbe\u5907\u6811\u63cf\u8ff0\u7684device node\u6700\u7ec8\u4f1a\u5f62\u6210\u4e00\u4e2a\u6811\u72b6\u7ed3\u6784\uff0c\u5728\u5185\u6838\u521d\u59cb\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u626b\u63cf\u8fd9\u4e2a\u6811\u72b6\u7ed3\u6784\uff0c\u5e76\u6839\u636e\u6bcf\u4e2adevice node\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u521d\u59cb\u5316\u5bf9\u5e94\u7684\u8bbe\u5907\u5e76\u52a0\u5165\u5230\u5185\u6838\u4e2d\u3002\u7c7b\u4f3c\u5730\uff0cpin controller driver\u7684\u521d\u59cb\u5316\u4e5f\u662f\u4ece\u8bbe\u5907\u6811\u8282\u70b9\u5f00\u59cb\u7684\uff1a

pinctrl@56000000{\n    reg=<0x56000000 0x1000=\"\">;\n    compatible=\"samsung, s3c2416-pinctrl\";\n}\n

compatible\u5c5e\u6027\u7528\u6765\u6307\u793a\u5185\u6838\u9009\u7528\u54ea\u4e00\u4e2apin controller driver\u6765\u9a71\u52a8\u8be5\u8bbe\u5907\u3002pinctrl\u5b50\u7cfb\u7edf\u8981\u60f3\u8fdb\u884c\u63a7\u5236\uff0c\u5fc5\u987b\u6709\u4e00\u4e2a\u63a7\u5236\u5bf9\u8c61\uff0c\u5728\u8f6f\u4ef6\u5c42\u9762\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u65b9\u6cd5\u5c06\u786c\u4ef6\u4fe1\u606f\u6ce8\u518c\u5230pinctrl\u5b50\u7cfb\u7edf\u4e2d\u53bb\u3002

"},{"location":"linux/drivers/pinctrl/#_3","title":"\u6570\u636e\u7ed3\u6784","text":"

\u5b66\u4e60pinctrl\u5b50\u7cfb\u7edf\uff0c\u9996\u5148\u8981\u4e86\u89e3\u5176\u5185\u90e8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u518d\u53bb\u7814\u7a76\u6e90\u7801\u5c31\u6bd4\u8f83\u8f7b\u677e\u3002

\u4e3b\u8981\u6d89\u53ca\u5230\u7684\u6570\u636e\u7ed3\u6784\u662f\uff1a

  • pin controller device\u76f8\u5173\uff1apinctrl_desc, pinctrl_ops, pinmux_ops, pinconf_ops, pinctrl_dev
  • client device\u76f8\u5173\uff1apinctrl, pinctrl_state, pinctrl_setting, pinctrl_map, pinctrl_dt_map

\u7531\u4e8e\u6d89\u53ca\u5230\u7684\u7ed3\u6784\u4f53\u6bd4\u8f83\u591a\uff0c\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684\u6210\u5458\u4e5f\u975e\u5e38\u590d\u6742\uff0c\u8fd9\u91cc\u7701\u7565\u4e86\u4e00\u90e8\u5206\u4e0d\u592a\u91cd\u8981\u7684\u5185\u5bb9\uff0c\u5bf9\u4e8e\u91cd\u590d\u6027\u7684\u5185\u5bb9\u4e5f\u4e0d\u518d\u8bf4\u660e\uff0c\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u81ea\u884c\u9605\u8bfb\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002

"},{"location":"linux/drivers/pinctrl/#pin-controller-device","title":"pin controller device","text":"

struct pinctrl_dev\u7ed3\u6784\u4f53\u7528\u6765\u8868\u793a\u4e00\u4e2apin controller device\uff0c\u8d1f\u8d23\u7ba1\u7406\u5f15\u811a\uff0c\u540e\u7eed\u7684\u8bb8\u591a\u6570\u636e\u7ed3\u6784\u90fd\u9700\u8981\u4e0e\u6b64\u5173\u8054\uff1a

struct pinctrl_dev {\n    struct list_head node;\n    struct pinctrl_desc *desc;\n    struct list_head gpio_ranges;\n    struct device *dev;\n    struct module *owner;\n    void *driver_data;\n    struct pinctrl *p;\n    struct pinctrl_state *hog_default;\n    struct pinctrl_state *hog_sleep;\n};\n

desc\uff1apin controller\u63cf\u8ff0\u7b26\uff0c\u7528\u4e8e\u63cf\u8ff0\u5f15\u811a\u4fe1\u606f\u3002

gpio_range\uff1a\u6b64pin controller\u5904\u7406\u7684GPIO\u8303\u56f4\u5217\u8868\u3002

dev\uff1apin controller\u7684\u7236\u8bbe\u5907\uff0c\u4e00\u822c\u8bbe\u7f6e\u4e3a\u5e73\u53f0\u8bbe\u5907\u7684dev\u6210\u5458\u3002

owner\uff1a\u6240\u6709\u8005\u3002

driver_data\uff1a\u9a71\u52a8\u7a0b\u5e8f\u7684\u79c1\u6709\u6570\u636e\u3002

p\uff1a\u8be5pin controller\u5bf9\u5e94\u7684client device\u3002

hog_default\uff1a\u6b64\u8bbe\u5907\u5360\u7528\u7684\u5f15\u811a\u7684\u9ed8\u8ba4\u72b6\u6001\u3002

hog_sleep\uff1a\u6b64\u8bbe\u5907\u5360\u7528\u7684\u5f15\u811a\u7684\u7761\u7720\u72b6\u6001\u3002

struct pinctrl_desc\u7528\u6765\u5b9a\u4e49\u548c\u914d\u7f6epin controller\u7684\u63cf\u8ff0\u4fe1\u606f\uff1a

struct pinctrl_desc {\n    const char *name;\n    const struct pinctrl_pin_desc *pins;\n    unsigned int npins;\n    const struct pinctrl_ops *pctlops;\n    const struct pinmux_ops *pmxops;\n    const struct pinconf_ops *confops;\n    struct module *owner;\n};\n

name\uff1apin controller\u7684\u540d\u79f0\u3002

pins\uff1a\u8be5pin controller\u5904\u7406\u7684\u6240\u6709\u5f15\u811a\u7684\u63cf\u8ff0\u7b26\u6570\u7ec4\u3002

npins\uff1a\u6570\u7ec4\u4e2d\u63cf\u8ff0\u7b26\u7684\u6570\u91cf\uff0c\u4e00\u822c\u7b49\u4e8eARRAY_SIZE(pins)\u3002

pctlops\uff1a\u5f15\u811a\u63a7\u5236\u64cd\u4f5c\u3002

pmxops\uff1a\u5f15\u811a\u590d\u7528\u64cd\u4f5c\u3002

confops\uff1a\u5f15\u811a\u914d\u7f6e\u64cd\u4f5c\u3002

\u5bf9\u4e8e\u67d0\u4e2apin controller device\u6765\u8bf4\uff0c\u5b83\u8981\u641e\u660e\u767d\u81ea\u5df1\u7ba1\u7406\u591a\u5c11\u5f15\u811a\uff0c\u5e76\u4f7f\u7528\u81ea\u7136\u6570\u4e3a\u8fd9\u4e9b\u5f15\u811a\u7f16\u53f7\u3002\u7cfb\u7edf\u4e2d\u7684\u5f15\u811a\u4fe1\u606f\uff0c\u90fd\u7531struct pinctrl_pin_desc\u6765\u63cf\u8ff0\uff0c\u5305\u62ec\u7f16\u53f7\u3001\u540d\u5b57\u548c\u6570\u636e\uff1a

struct pinctrl_pin_desc {\n    unsigned number;\n    const char *name;\n    void *drv_data;\n};\n

number\uff1a\u5f15\u811a\u7f16\u53f7\u3002

name\uff1a\u5f15\u811a\u540d\u79f0\u3002

drv_data\uff1a\u5f15\u811a\u7684\u79c1\u6709\u6570\u636e\u3002

struct pinctrl_pin_desc\u7ed3\u6784\u4f53\u4e2d\u7684\u7f16\u53f7\u548c\u540d\u79f0\u5b8c\u5168\u7531\u9a71\u52a8\u5f00\u53d1\u4eba\u5458\u81ea\u5df1\u51b3\u5b9a\uff0c\u5f53\u7136\uff0c\u9700\u8981\u7b26\u5408\u4e00\u5b9a\u7684\u89c4\u8303\u3002

\u8fd9\u4e48\u8bf4\u6709\u70b9\u67af\u71e5\uff0c\u6211\u4eec\u770b\u5b98\u65b9\u6587\u6863\u7684\u4e00\u4e2a\u793a\u4f8b\u3002\u5047\u8bbe\u5f15\u811a\u9635\u5217\u5982\u4e0b\u56fe\u6240\u793a\uff1a

        A   B   C   D   E   \n\n   5    o   o   o   o   o  \n\n   4    o   o   o   o   o  \n\n   3    o   o   o   o   o  \n\n   2    o   o   o   o   o  \n\n   1    o   o   o   o   o  \n

\u8981\u5728\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u6ce8\u518c\u4e00\u4e2a\u5f15\u811a\u63a7\u5236\u5668\u5e76\u4e3a\u8fd9\u4e2a\u5c01\u88c5\u4e0a\u7684\u6240\u6709\u5f15\u811a\u547d\u540d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a

#include <linux/pinctrl/pinctrl.h>\nconst struct pinctrl_pin_desc foo_pins[] = {\n    PINCTRL_PIN(0, \"A5\"),\n    PINCTRL_PIN(1, \"B5\"),\n    PINCTRL_PIN(2, \"C5\"),\n    ...\n    PINCTRL_PIN(23, \"C1\"),\n    PINCTRL_PIN(24, \"D1\"),\n    PINCTRL_PIN(25, \"E1\"),\n};\n

\u5728\u5b9a\u4e49\u4e86\u5f15\u811a\u914d\u7f6e\u6570\u7ec4\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u544a\u8bc9struct pinctrl_desc\u7ed3\u6784\u4f53\u5f15\u811a\u7684\u4fe1\u606f\uff0c\u7136\u540e\u8c03\u7528pinctrl_register_and_init()\u51fd\u6570\u6ce8\u518c\u3002

static struct pinctrl_desc foo_desc = {\n    .name = \"foo\",\n    .pins = foo_pins,\n    .npins = ARRAY_SIZE(foo_pins),\n    .owner = THIS_MODULE,\n};\n\nstruct pinctrl_dev *pctl;\n\npinctrl_register_and_init(&foo_desc, <PARENT>, NULL, &pctl);\n\npinctrl_enable(pctl);\n

\u5728SoC\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u9700\u8981\u5c06\u591a\u4e2a\u5f15\u811a\u8fdb\u884c\u7ec4\u5408\u3002\u56e0\u6b64pinctrl\u5b50\u7cfb\u7edf\u63d0\u4f9b\u4ee5group\u4e3a\u5355\u4f4d\uff0c\u540c\u65f6\u5730\u8bbf\u95ee\u548c\u63a7\u5236\u591a\u4e2a\u5f15\u811a\u7684\u529f\u80fd\uff0c\u8fd9\u5c31\u662fpin group\u7684\u6982\u5ff5\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u5b9a\u4e49\u5728struct pinctrl_ops\u7ed3\u6784\u4f53\u4e2d\uff1a

struct pinctrl_ops {\n    int (*get_groups_count) (struct pinctrl_dev *pctldev);\n    const char *(*get_group_name) (struct pinctrl_dev *pctldev, unsigned selector);\n    int (*get_group_pins) (struct pinctrl_dev *pctldev, unsigned selector,\n                           const unsigned **pins, unsigned *num_pins);\n    void (*pin_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                          unsigned offset);\n    int (*dt_node_to_map) (struct pinctrl_dev *pctldev, struct device_node *np_config,\n                           struct pinctrl_map **map, unsigned *num_maps);\n    void (*dt_free_map) (struct pinctrl_dev *pctldev, struct pinctrl_map *map, \n                         unsigned num_maps);\n};\n

get_groups_count()\uff1a\u83b7\u53d6pin group\u7684\u6570\u91cf\u3002

get_group_name()\uff1a\u83b7\u53d6pin group\u7684\u540d\u79f0\u3002

get_group_pins()\uff1a\u83b7\u53d6pin group\u7684\u5f15\u811a\u4fe1\u606f\u3002

dt_node_to_map()\uff1a\u4e3a\u8bbe\u5907\u6811\u4e2d\u7684pin controller\u5b50\u8282\u70b9\u521b\u5efa\u6620\u5c04\uff0c\u5373\u5c06devic_node\u8f6c\u6362\u4e3a\u4e00\u7cfb\u5217\u7684struct pinctrl_map\u3002

dt_free_map()\uff1a\u91ca\u653edt_node_to_map()\u521b\u5efa\u7684\u6620\u5c04\u3002

SoC\u4e2d\u7684\u5f88\u591a\u5f15\u811a\u53ef\u4ee5\u914d\u7f6e\u4e3a\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u8fd9\u88ab\u79f0\u4e3a\u5f15\u811a\u7684\u590d\u7528\uff08pinmux\uff09\uff0cpinctrl\u5b50\u7cfb\u7edf\u4f7f\u7528struct pinmux_ops\u7ed3\u6784\u4f53\u6765\u62bd\u8c61\u590d\u7528\u7684\u6709\u5173\u64cd\u4f5c\uff1a

struct pinmux_ops {\n    int (*request) (struct pinctrl_dev *pctldev, unsigned offset);\n    int (*free) (struct pinctrl_dev *pctldev, unsigned offset);\n    int (*get_functions_count) (struct pinctrl_dev *pctldev);\n    const char *(*get_function_name) (struct pinctrl_dev *pctldev, unsigned selector);\n    int (*get_function_groups) (struct pinctrl_dev *pctldev, unsigned selector,\n                                const char * const **groups, unsigned *num_groups);\n    int (*set_mux) (struct pinctrl_dev *pctldev, unsigned func_selector,\n                    unsigned group_selector);\n    int (*gpio_request_enable) (struct pinctrl_dev *pctldev, \n                                struct pinctrl_gpio_range *range, unsigned offset);\n    void (*gpio_disable_free) (struct pinctrl_dev *pctldev, \n                               struct pinctrl_gpio_range *range, unsigned offset);\n    int (*gpio_set_direction) (struct pinctrl_dev *pctldev,\n                               struct pinctrl_gpio_range *range, unsigned offset,\n                               bool input);\n    bool strict;\n};\n

request\uff1a\u7531\u6838\u5fc3\u8c03\u7528\uff0c\u4ee5\u67e5\u770b\u7279\u5b9a\u5f15\u811a\u662f\u5426\u53ef\u4ee5\u7528\u4e8e\u5f15\u811a\u590d\u7528\u3002\u5728\u9009\u62e9\u4efb\u4f55\u5b9e\u9645\u7684\u590d\u7528\u8bbe\u7f6e\u4e4b\u524d\uff0c\u5e94\u7531\u6838\u5fc3\u8c03\u7528\u6b64\u51fd\u6570\u6765\u83b7\u53d6\u5f15\u811a\u3002\u5982\u679c\u8be5\u9a71\u52a8\u7a0b\u5e8f\u65e0\u6cd5\u5904\u7406\u8868\u8ff0\u7684\u5f15\u811a\uff0c\u5219\u5e94\u8fd4\u56de\u8d1f\u9519\u8bef\u4ee3\u7801\u6765\u62d2\u7edd\u8bf7\u6c42\u3002

free\uff1a\u5728\u7533\u8bf7\u540e\u91ca\u653e\u5f15\u811a\u3002

get_functions_count\uff1a\u8fd4\u56depin controller device\u652f\u6301\u7684function\u6570\u76ee\u3002

get_function_name\uff1a\u7ed9\u5b9a\u4e00\u4e2afunction selector\uff0c\u8fd4\u56de\u6307\u5b9afunction\u7684\u540d\u79f0\u3002

get_function_groups\uff1a\u7ed9\u5b9a\u4e00\u4e2afunction selector\uff0c\u8fd4\u56de\u6307\u5b9afunction\u7684pin group\u4fe1\u606f\u3002

set_mux\uff1a\u542f\u7528\u67d0\u4e2a\u590d\u7528\u529f\u80fd\u4e0e\u7279\u5b9a\u7684\u5f15\u811a\u7ec4\u3002\u9a71\u52a8\u7a0b\u5e8f\u4e0d\u9700\u8981\u5f04\u6e05\u695a\u542f\u7528\u8fd9\u4e2a\u529f\u80fd\u662f\u5426\u4e0e\u8be5\u7ec4\u5f15\u811a\u7684\u5176\u4ed6\u7528\u9014\u51b2\u7a81\uff0c\u8fd9\u6837\u7684\u51b2\u7a81\u7531\u5f15\u811a\u590d\u7528\u5b50\u7cfb\u7edf\u5904\u7406\u3002func_selector\u9009\u62e9\u4e00\u4e2a\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u800cgroup_selector\u9009\u62e9\u4e00\u7ec4\u8981\u4f7f\u7528\u7684\u7279\u5b9a\u5f15\u811a\u3002\u5728\u7b80\u5355\u7684\u63a7\u5236\u5668\u4e0a\uff0c\u540e\u8005\u53c2\u6570\u53ef\u80fd\u88ab\u5ffd\u7565\u3002

gpio_request_enable\uff1a\u5728\u67d0\u4e9b\u5f15\u811a\u4e0a\u8bf7\u6c42\u5e76\u542f\u7528GPIO\u3002\u4ec5\u5f53\u4f60\u80fd\u591f\u5c06\u6bcf\u4e2a\u5f15\u811a\u5355\u72ec\u590d\u7528\u4e3aGPIO\u65f6\uff0c\u624d\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u53d7\u5f71\u54cd\u7684GPIO\u8303\u56f4\u8fde\u540c\u504f\u79fb\u91cf\uff08\u5f15\u811a\u53f7\u7801\uff09\u4e00\u8d77\u4f20\u9012\u5230\u7279\u5b9a\u7684GPIO\u8303\u56f4\u2014\u2014\u529f\u80fd\u9009\u62e9\u5668\u548c\u5f15\u811a\u7ec4\u4e0e\u6b64\u72ec\u7acb\uff0c\u4f46\u662f\u6838\u5fc3\u5c06\u786e\u4fdd\u5f15\u811a\u4e0d\u4f1a\u51b2\u7a81\u3002

gpio_disable_free\uff1a\u5728\u67d0\u4e2a\u5f15\u811a\u4e0a\u91ca\u653eGPIO\u590d\u7528\u3002

gpio_set_direction\uff1a\u8bbe\u7f6eGPIO\u7684\u8f93\u5165\u6216\u8f93\u51fa\u65b9\u5411\u3002

strict\uff1a\u4e3atrue\u65f6\uff0c\u4e0d\u5141\u8bb8\u67d0\u4e2a\u5f15\u811a\u4f5c\u4e3aGPIO\u548c\u5176\u4ed6\u529f\u80fd\u540c\u65f6\u4f7f\u7528\u3002

\u4ec0\u4e48\u662ffunction\uff1ffunction\u662f\u5f15\u811a\u529f\u80fd\u7684\u62bd\u8c61\uff0cSPI\u662f\u4e00\u4e2afunction\uff0cI2C\u4e5f\u662f\u4e00\u4e2afunction\u3002\u4f46\u662f\u5373\u4fbf\u77e5\u9053\u5177\u4f53\u7684function name\uff0c\u6211\u4eec\u4e5f\u4e0d\u80fd\u786e\u5b9a\u5176\u4f7f\u7528\u5f15\u811a\u7684\u60c5\u51b5\u3002\u6bd4\u5982\u4e00\u4e2aSPI0\u7684\u529f\u80fd\u53ef\u80fd\u4f7f\u7528\u4e86\u5f15\u811a\u7ec4{A8, A7, A6, A5}\uff0c\u4e5f\u53ef\u80fd\u4f7f\u7528\u4e86\u5f15\u811a\u7ec4{G4, G3, G2, G1}\u3002\u4f46\u6beb\u65e0\u7591\u95ee\uff0c\u8fd9\u4e24\u4e2a\u5f15\u811a\u7ec4\u4e0d\u80fd\u540c\u65f6\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\uff0c\u56e0\u4e3a\u82af\u7247\u5185\u90e8SPI0\u7684\u903b\u8f91\u529f\u80fd\u7535\u8def\u53ea\u6709\u4e00\u4e2a\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u7ed9\u51fafunction selector\uff08\u5c31\u50cf\u6570\u7ec4\u7684\u4e00\u4e2a\u7d22\u5f15\uff09\u548cfunction\u7684pin group selector\u624d\u80fd\u8fdb\u884cfunction mux\u7684\u8bbe\u5b9a\u3002

\u5f15\u811a\u7684\u914d\u7f6e\uff0c\u6bd4\u5982\u4e0a\u62c9\u3001\u4e0b\u62c9\u3001\u9ad8\u963b\u6297\u7b49\uff0cpinctrl\u5b50\u7cfb\u7edf\u4f7f\u7528struct pinconf_ops\u7ed3\u6784\u4f53\u6765\u62bd\u8c61\u914d\u7f6e\u7684\u64cd\u4f5c\uff1a

struct pinconf_ops {\n#ifdef CONFIG_GENERIC_PINCONF\n    bool is_generic;\n#endif\n    int (*pin_config_get) (struct pinctrl_dev *pctldev, unsigned pin,\n                           unsigned long *config);\n    int (*pin_config_set) (struct pinctrl_dev *pctldev, unsigned pin,\n                           unsigned long *configs, unsigned num_configs);\n    int (*pin_config_group_get) (struct pinctrl_dev *pctldev, unsigned selector,\n                                 unsigned long *config);\n    int (*pin_config_group_set) (struct pinctrl_dev *pctldev, unsigned selector,\n                                 unsigned long *configs, unsigned num_configs);\n    void (*pin_config_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                                 unsigned offset);\n    void (*pin_config_group_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                                       unsigned selector);\n    void (*pin_config_config_dbg_show) (struct pinctrl_dev *pctldev, struct seq_file *s,\n                                        unsigned long config);\n};\n

is_generic\uff1a\u5bf9\u4e8e\u5e0c\u671b\u4f7f\u7528\u901a\u7528\u63a5\u53e3\u7684\u5f15\u811a\u63a7\u5236\u5668\uff0c\u8be5\u6807\u5fd7\u544a\u8bc9\u6846\u67b6\u5b83\u662f\u901a\u7528\u7684\u3002

pin_config_get\uff1a\u83b7\u53d6\u67d0\u4e2a\u5f15\u811a\u7684\u914d\u7f6e\u3002

pin_config_set\uff1a\u914d\u7f6e\u5355\u4e2a\u5f15\u811a\u3002

pin_config_group_get\uff1a\u83b7\u53d6\u67d0\u4e2a\u5f15\u811a\u7ec4\u7684\u914d\u7f6e\u3002

pin_config_group_set\uff1a\u914d\u7f6e\u67d0\u4e2a\u5f15\u811a\u7ec4\u4e2d\u7684\u6240\u6709\u5f15\u811a\u3002

\u4e0b\u56fe\u63cf\u8ff0\u4e86pin controller device\u7c7b\u522b\u4e0b\u51e0\u4e2a\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u7684\u5173\u7cfb\uff1a

"},{"location":"linux/drivers/pinctrl/#client-device","title":"client device","text":"

\u5728\u5185\u6838\u542f\u52a8\u9636\u6bb5\uff0cdevice_node\u4e00\u822c\u4f1a\u88ab\u8f6c\u6362\u4e3aplatform_device\u7ed3\u6784\u4f53\uff0c\u6216\u8005\u5176\u4ed6\u7ed3\u6784\u4f53\u6bd4\u5982i2c_client\u3001spi_device\uff0c\u5b83\u4eec\u5185\u90e8\u90fd\u6709\u4e00\u4e2astruct device\u6210\u5458\uff08\u4e5f\u5373\u7ee7\u627f\u4e86struct device\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\u3002

\u5728struct device\u7ed3\u6784\u4f53\u91cc\u6709\u4e00\u4e2astruct dev_pin_info\u7ed3\u6784\u4f53\uff0c\u7528\u6765\u4fdd\u5b58\u8bbe\u5907\u7684\u5f15\u811a\u4fe1\u606f\uff1a

struct device {\n    struct kobject kobj;\n    struct device *parent;\n    struct device_private *p;\n    ...\n#ifdef CONFIG_PINCTRL\n    struct dev_pin_info *pins;\n#endif\n    ....\n};\n

struct dev_pin_info\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8client device\u5f15\u811a\u4fe1\u606f\u7684\u7ed3\u6784\u4f53\uff1a

struct dev_pin_info {\n    struct pinctrl *p;\n    struct pinctrl_state *default_state;\n    struct pinctrl_state *init_state;\n#ifdef CONFIG_PM\n    struct pinctrl_state *sleep_state;\n    struct pinctrl_state *idle_state;\n#endif\n};\n

p\uff1a\u4fdd\u5b58\u8be5\u8bbe\u5907\u7684\u6240\u6709\u72b6\u6001\u4fe1\u606f\uff0c\u5305\u62ec\u81ea\u5b9a\u4e49\u7684\u72b6\u6001\u3002

default_state\uff1a\u8868\u793a\u8be5\u8bbe\u5907\u4e0a\u5f15\u811a\u7684\u9ed8\u8ba4\u72b6\u6001\u3002

init_state\uff1a\u8868\u793a\u521d\u59cb\u5316\u5f15\u811a\u7684\u72b6\u6001\u3002

sleep_state/idle_state\uff1a\u5982\u679c\u542f\u7528\u4e86\u7535\u6e90\u7ba1\u7406\u6a21\u5757\uff08Power Management\uff09\uff0c\u5206\u522b\u8868\u793a\u7761\u7720\u548c\u7a7a\u95f2\u72b6\u6001\u3002

pinctrl\u5b50\u7cfb\u7edf\u53c8\u62bd\u8c61\u4e86struct pinctrl\u6765\u63cf\u8ff0\u4e00\u4e2aclient device\u7684\u6240\u6709\u72b6\u6001\uff1a

struct pinctrl {\n    struct list_head node;\n    struct device *dev;\n    struct list_head states;\n    struct pinctrl_state *state;\n    struct list_head dt_maps;\n    struct kref users;\n};\n

node\uff1a\u7528\u4e8e\u5c06\u8be5pin controller\u6302\u5165\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002

dev\uff1a\u8be5pin controller\u5bf9\u5e94\u7684device\u3002

states\uff1a\u4fdd\u5b58\u8be5pin controller\u7684\u6240\u6709\u72b6\u6001\u4fe1\u606f\uff0c\u8fd9\u4e9b\u72b6\u6001\u4fe1\u606f\u90fd\u4fdd\u5b58\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002

state\uff1a\u5f53\u524dpin controller\u7684\u72b6\u6001\u3002

dt_maps\uff1a\u4fdd\u5b58\u8be5pin controller\u5bf9\u5e94\u7684\u6240\u6709\u8bbe\u5907\u8282\u70b9\u4fe1\u606f\uff0c\u8fd9\u4e9b\u8bbe\u5907\u8282\u70b9\u4fe1\u606f\u90fd\u4fdd\u5b58\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002

users\uff1a\u5f15\u7528\u8ba1\u6570\u3002

\u7cfb\u7edf\u4e2d\u7684\u6bcf\u4e00\u4e2a\u9700\u8981\u548cpinctrl\u5b50\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u8bbe\u5907\u5728\u8fdb\u884c\u914d\u7f6e\u4e4b\u524d\u90fd\u9700\u8981\u9996\u5148\u83b7\u53d6\u8fd9\u4e2astruct pinctrl\u7ed3\u6784\u4f53\uff0c\u5c5e\u4e8e\u8be5\u8bbe\u5907\u7684\u6240\u6709\u7684\u72b6\u6001\u90fd\u5c06\u88ab\u6302\u5165\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u94fe\u8868\u5934\u5c31\u662fstates\u6210\u5458\u3002

\u5355\u4e2a\u72b6\u6001\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

struct pinctrl_state {\n    struct list_head node;\n    const char *name;\n    struct list_head settings;\n};\n

name\uff1a\u6b64\u72b6\u6001\u7684\u540d\u79f0\u3002

settings\uff1a\u5c5e\u4e8e\u8be5\u72b6\u6001\u7684\u6240\u6709settings\u3002

\u8bbe\u5907\u5728\u5355\u4e2a\u72b6\u6001\u4e0b\u7684pinctrl\u63a7\u5236\u4fe1\u606f\uff0c\u7531struct pinctrl_setting\u7ed3\u6784\u4f53\u63cf\u8ff0\uff1a

struct pinctrl_setting { \n    struct list_head node; \n    enum pinctrl_map_type type; \n    struct pinctrl_dev *pctldev;    //\u5bf9\u5e94\u7684pin controller device\n    const char *dev_name;           //\u8bbe\u5907\u540d\u79f0\n    union { \n        struct pinctrl_setting_mux mux;             //mux\u914d\u7f6e\u6570\u636e\n        struct pinctrl_setting_configs configs;     //config\u914d\u7f6e\u6570\u636e\n    } data; \n};\n

\u5f53\u67d0\u4e2adriver\u8bbe\u5b9a\u4e00\u4e2apin state\u7684\u65f6\u5019\uff0cpinctrl\u5b50\u7cfb\u7edf\u5185\u90e8\u4f1a\u904d\u5386\u8be5state\u7684settings\u94fe\u8868\uff0c\u5c06\u4e00\u4e2a\u4e00\u4e2a\u7684setting\u8fdb\u884c\u8bbe\u5b9a\u3002\u8fd9\u4e9bsettings\u6709\u5404\u79cd\u7c7b\u578b\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a

enum pinctrl_map_type { \n    PIN_MAP_TYPE_INVALID, \n    PIN_MAP_TYPE_DUMMY_STATE, \n    PIN_MAP_TYPE_MUX_GROUP,     // \u529f\u80fd\u590d\u7528\u7684setting\n    PIN_MAP_TYPE_CONFIGS_PIN,   // \u8bbe\u5b9a\u5355\u4e00\u4e00\u4e2apin\u7684\u7535\u6c14\u7279\u6027 \n    PIN_MAP_TYPE_CONFIGS_GROUP, // \u8bbe\u5b9a\u5355pin group\u7684\u7535\u6c14\u7279\u6027 \n};\n

struct pinctrl_setting_mux\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin mux\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_MUX_GROUP\uff09\uff1a

struct pinctrl_setting_mux {\n    unsigned group;\n    unsigned func;\n};\n

group\uff1apin group selector\u3002

function\uff1apin function selector\u3002

struct pinctrl_setting_config\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin config\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_CONFIGS_PIN\u548cPIN_MAP_TYPE_CONFIGS_GROUP\uff09\uff1a

struct pinctrl_setting_configs {\n    unsigned group_or_pin;\n    unsigned long *configs;\n    unsigned num_configs;\n};\n

group_or_pin\uff1a\u8981\u914d\u7f6e\u7684pin\u7f16\u53f7\u6216\u8005pin group selector\u3002

configs\uff1a\u6307\u5411\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u4e86\u4e00\u7ec4\u914d\u7f6e\u53c2\u6570\u6216\u503c\uff0c\u7528\u4e8e\u8bbe\u7f6e\u7279\u5b9a\u5f15\u811a\u6216\u5f15\u811a\u7ec4\u7684\u786c\u4ef6\u53c2\u6570\u3002

num_configs\uff1a\u9700\u8981\u5199\u5165\u7684\u914d\u7f6e\u53c2\u6570\u4e2a\u6570\u3002

struct pinctrl_map\u7528\u4e8e\u63cf\u8ff0client device\u7684\u6620\u5c04\u914d\u7f6e\uff0c\u4f7f\u7528pin controller device\u7684pinctrl_desc->pctlops->dt_node_to_map\u6765\u5904\u7406\u8bbe\u5907\u6811\u4e2d\u7684\u5f15\u811a\u914d\u7f6e\u8282\u70b9\u3002\u4f8b\u5982\u67d0\u914d\u7f6e\u8282\u70b9\u2014\u2014pinctrl-0 = <&uart0_xfer &uart0_cts &uart0_rts>\uff0c\u90a3\u4e48uart0_xfer \u3001uart0_cts \u3001uart0_rts\u8282\u70b9\u5747\u4f1a\u88abdt_node_to_map()\u51fd\u6570\u89e3\u6790\u4e3a\u4e00\u7cfb\u5217\u7684struct pinctrl_map\uff0c\u7136\u540e\u88ab\u8f6c\u6362\u4e3astruct pinctrl_setting\uff0c\u5b58\u5165struct pinctrl_state.settings\u94fe\u8868\u4e2d\u3002

struct pinctrl_map {\n    const char *dev_name;\n    const char *name;\n    enum pinctrl_map_type type;\n    const char *ctrl_dev_name;\n    union {\n        struct pinctrl_map_mux mux;\n        struct pinctrl_map_configs configs;\n    } data;\n};\n

dev_name\uff1a\u8bbe\u5907\u540d\u79f0\uff0c\u9700\u8981\u4e0e\u8be5client device\u4e2dstruct device\u4e2d\u7684\u540d\u79f0\u4e00\u81f4\u3002

name\uff1a\u72b6\u6001\u540d\u79f0\u3002

type\uff1a\u6620\u5c04\u9879\u7684\u7c7b\u578b\u3002

ctrl_dev_name\uff1a\u8bbe\u5907\u540d\u79f0\u3002

data\uff1a\u8be5\u6620\u5c04\u9879\u7684\u5177\u4f53\u6570\u636e\uff0c\u5305\u62ecmux\u548cconfigs\u4e24\u79cd\u7c7b\u578b\uff0c\u5206\u522b\u4ee3\u8868\u590d\u7528\u9009\u62e9\u548c\u5f15\u811a\u914d\u7f6e\u3002

struct pinctrl_map_mux\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin mux\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_MUX_GROUP\uff09\uff1a

struct pinctrl_map_mux {\n    const char *group;\n    const char *function;\n};\n

group\uff1a\u9700\u8981\u914d\u7f6e\u590d\u7528\u529f\u80fd\u7684pin group\u7684\u540d\u79f0\u3002

function:\u590d\u7528\u529f\u80fd\u7684\u540d\u79f0\u3002

struct pinctrl_map_configs\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0pin config\u76f8\u5173\u7684\u8bbe\u5b9a\uff08PIN_MAP_TYPE_CONFIGS_PIN\u548cPIN_MAP_TYPE_CONFIGS_GROUP\uff09\uff1a

struct pinctrl_map_configs {\n    const char *group_or_pin;\n    unsigned long *configs;\n    unsigned num_configs;\n};\n

group_or_pin\uff1a\u9700\u8981\u914d\u7f6e\u7684pin\u6216\u8005pin group\u7684\u540d\u79f0\u3002

configs\uff1a\u6307\u5411\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u4e86\u4e00\u7ec4\u914d\u7f6e\u53c2\u6570\u6216\u503c\uff0c\u7528\u4e8e\u8bbe\u7f6e\u7279\u5b9a\u5f15\u811a\u6216\u5f15\u811a\u7ec4\u7684\u786c\u4ef6\u53c2\u6570\u3002

num_configs\uff1a\u9700\u8981\u5199\u5165\u7684\u914d\u7f6e\u53c2\u6570\u4e2a\u6570\u3002

struct pinctrl_maps\u7ed3\u6784\u4f53\u7528\u6765\u63cf\u8ff0\u6620\u5c04\u8868\uff08mapping table\uff09\u7684\u90e8\u5206\u9879\uff1a

struct pinctrl_maps {\n    struct list_head node;\n    const struct pinctrl_map *maps;\n    unsigned num_maps;\n};\n

maps\uff1a\u6307\u5411\u4e00\u4e2a\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u4e86\u4e00\u7ec4\u6620\u5c04\u8868\u9879\u3002

num_maps\uff1a\u9700\u8981\u5199\u5165\u7684\u6620\u5c04\u8868\u9879\u4e2a\u6570\u3002

\u5728\u524d\u9762\u6211\u4eec\u8bf4\u8fc7\uff0c\u5f15\u811a\u914d\u7f6e\u8282\u70b9\u4f1a\u88abdt_node_to_map()\u51fd\u6570\u89e3\u6790\u4e3astruct pinctrl_map\uff0c\u5e76\u4ee5\u6570\u7ec4\u6307\u9488\u5f62\u5f0f\u8fd4\u56de\u3002 \u800cstruct pinctrl_dt_map\u5c31\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u4ece\u8bbe\u5907\u6811\u89e3\u6790\u51fa\u6765\u7684\u6620\u5c04\u8868\u6570\u636e\u7684\u7ed3\u6784\u4f53\uff0c\u5176\u6210\u5458map\u6307\u5411\u8fd9\u4e2astruct pinctrl_map\u6570\u7ec4\uff1a

struct pinctrl_dt_map {\n    struct list_head node;\n    struct pinctrl_dev *pctldev;\n    struct pinctrl_map *map;\n    unsigned num_maps;\n};\n

pctldev\uff1a\u6307\u5411\u5206\u914d\u8be5\u6620\u5c04\u8868\u7684pin controller device\u3002

map\uff1a\u5b9e\u9645\u6620\u5c04\u8868\u6570\u636e\u3002

num_maps\uff1a\u6620\u5c04\u8868\u4e2d\u7684\u6570\u76ee\u3002

\u4e0b\u56fe\u63cf\u8ff0\u4e86client device\u7c7b\u522b\u4e0b\u51e0\u4e2a\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u7684\u5173\u7cfb\uff1a

"},{"location":"linux/drivers/pinctrl/#gpio","title":"\u4e0eGPIO\u5b50\u7cfb\u7edf\u4ea4\u4e92","text":"

\u4f5c\u4e3a\u8f6f\u4ef6\u5de5\u7a0b\u5e08\uff0c\u6211\u4eec\u671f\u671b\u7684\u786c\u4ef6\u8bbe\u8ba1\u5e94\u8be5\u5982\u4e0b\u56fe\u6240\u793a\uff1a

GPIO\u7684HW block\u5e94\u8be5\u4e0e\u5176\u4ed6\u5b50\u7cfb\u7edf\u7684block\u662f\u5e73\u7ea7\u5173\u7cfb\uff0c\u5b83\u4eec\u5171\u540c\u8f93\u5165\u5230\u4e00\u4e2a\u590d\u7528\u5668block\uff0c\u7531\u8be5block\u7684\u5bc4\u5b58\u5668\u63a7\u5236\u54ea\u4e00\u6761\u7535\u8def\u88ab\u63a5\u901a\u3002\u800cpin configuration\u5e94\u8be5\u662f\u5168\u5c40\u7684\uff0c\u4e0d\u8bba\u54ea\u79cdblock\u5904\u4e8eactive\u72b6\u6001\uff0c\u53ea\u8981\u63d0\u4f9b\u7edf\u4e00\u7684\u63a5\u53e3\u4f9b\u5b83\u4eec\u4f7f\u7528\u5373\u53ef\u3002\u5728\u8f6f\u4ef6\u5c42\u9762\uff0c\u6211\u4eec\u5f53\u7136\u53ef\u4ee5\u8ba9pinctrl\u5b50\u7cfb\u7edf\u548cGPIO\u5b50\u7cfb\u7edf\u5b8c\u5168\u72ec\u7acb\uff0c\u5404\u81ea\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f46\u662f\u5b9e\u9645\u7684SOC\u8bbe\u8ba1\u5e76\u975e\u603b\u662f\u548c\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u6240\u671f\u671b\u7684\u90a3\u6837\uff0c\u6bd4\u5982\u6709\u7684SOC\u8bbe\u8ba1\u6846\u67b6\u5982\u4e0b\u56fe\u6240\u793a\uff1a

\u6b64\u65f6\u7684GPIO block\u5e76\u6ca1\u6709\u548c\u5176\u4ed6block\u72ec\u7acb\uff0c\u800c\u662f\u6c38\u8fdc\u5904\u4e8eactive\u72b6\u6001\uff0c\u7ea2\u8272\u8fb9\u6846\u7684\u4e09\u4e2ablock\u88ab\u7d27\u5bc6\u5730\u8054\u7cfb\u5728\u4e00\u8d77\uff0c\u5b83\u4eec\u7684\u5bc4\u5b58\u5668\u5360\u636e\u4e86\u4e00\u4e2amemory range\u3002\u8fd9\u4e2a\u65f6\u5019\uff0c\u5bf9\u4e8e\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u6765\u8bf4\uff0c\u4e0d\u5f97\u4e0d\u5728pinctrl\u5b50\u7cfb\u7edf\u4e2d\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\uff0c\u624d\u80fd\u6b63\u786e\u5730\u63a7\u5236GPIO block\u3002\u4efb\u4f55\u4e00\u4e2agpio chip\uff0c\u5728\u4f7f\u7528GPIO\u529f\u80fd\u65f6\uff0c\u90fd\u5fc5\u987b\u8981\u5148\u5411\u7cfb\u7edf\u7684pinctrl\u5b50\u7cfb\u7edf\u7533\u8bf7\u5f15\u811a\uff0c\u5e76\u5c06\u5f15\u811a\u914d\u7f6e\u4e3aGPIO\u529f\u80fd\u3002

pinctrl\u5b50\u7cfb\u7edf\u4e3aGPIO\u63d0\u4f9b\u7684\u63a5\u53e3\u5b9a\u4e49\u5728<linux/pinctrl/consumer.h>\u4e2d\uff1a

int pinctrl_gpio_request(unsigned gpio);\nvoid pinctrl_gpio_free(unsigned gpio);\nint pinctrl_gpio_direction_input(unsigned gpio);\nint pinctrl_gpio_direction_output(unsigned gpio);\nint pinctrl_gpio_set_config(unsigned gpio, unsigned long config);\n

pinctrl_gpio_request\uff1a\u5411pinctrl\u5b50\u7cfb\u7edf\u7533\u8bf7\u4e00\u4e2agpio\u3002

pinctrl_gpio_free\uff1a\u91ca\u653e\u4e00\u4e2agpio\u3002

pinctrl_gpio_direction_input\uff1a\u5c06\u4e00\u4e2agpio\u8bbe\u7f6e\u4e3a\u8f93\u5165\u6a21\u5f0f\u3002

pinctrl_gpio_direction_output\uff1a\u5c06\u4e00\u4e2agpio\u8bbe\u7f6e\u4e3a\u8f93\u51fa\u6a21\u5f0f\u3002

pinctrl_gpio_set_config\uff1a\u8bbe\u7f6e\u4e00\u4e2agpio\u7684\u914d\u7f6e\u3002

\u5f53GPIO\u9a71\u52a8\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528\u67d0\u4e2a\u5f15\u811a\u65f6\uff0c\u76f4\u63a5\u8c03\u7528pinctrl_gpio_request()\u51fd\u6570\uff0c\u5411pinctrl\u5b50\u7cfb\u7edf\u7533\u8bf7\u3002pinctrl\u5b50\u7cfb\u7edf\u4f1a\u7ef4\u62a4\u4e00\u4e2agpio number\u5230pin number\u7684\u6620\u5c04\uff0c\u5c06GPIO\u5b50\u7cfb\u7edf\u7684gpio number\u8f6c\u6362\u4e3apin number\uff0c\u5e76\u8c03\u7528struct pinmux_ops\u7ed3\u6784\u4f53\u4e2d\u6709\u5173GPIO\u7684\u56de\u8c03\u51fd\u6570\u5373\u53ef\u3002

\u6beb\u65e0\u7591\u95ee\uff0c\u7533\u8bf7GPIO\u8d44\u6e90\u672c\u5e94\u8be5\u662fGPIO\u5b50\u7cfb\u7edf\u7684\u8d23\u4efb\uff0c\u4f46\u662f\u7531\u4e8e\u4e4b\u524d\u63cf\u8ff0\u7684\u7f18\u7531\uff0cpinctrl\u5b50\u7cfb\u7edf\u4e0d\u5f97\u4e0d\u63d0\u4f9b\u63a5\u53e3\u51fd\u6570\u4f9bGPIO\u9a71\u52a8\u7a0b\u5e8f\u4f7f\u7528\u3002\u5f53\u7136\uff0c\u5176\u4ed6\u7684\u9a71\u52a8\u7a0b\u5e8f\u4e0d\u5e94\u8be5\u8c03\u7528\u6b64\u63a5\u53e3\u3002

\u5982\u4f55\u63d0\u4f9bgpio number\u5230pin number\u7684\u6620\u5c04\u5462\uff1f\u662f\u901a\u8fc7\u4e00\u4e2astruct pinctrl_gpio_range\u7ed3\u6784\u4f53\u6765\u5b9e\u73b0\u7684\uff1a

struct pinctrl_gpio_range {\n    struct list_head node;\n    const char *name;\n    unsigned int id;\n    unsigned int base;\n    unsigned int pin_base;\n    unsigned int npins;\n    unsigned const *pins;\n    struct gpio_chip *gc;\n};\n

name\uff1a\u533a\u57df\u7684\u540d\u5b57\u3002

id\uff1a\u533a\u57dfID\u3002

base\uff1a\u533a\u57df\u8d77\u59cb\u7684GPIO ID\u3002

pin_base\uff1a\u533a\u57df\u8d77\u59cb\u7684pin ID

npins\uff1a\u533a\u57df\u5305\u542b\u7684\u5f15\u811a\u6570\u3002

pins\uff1a\u533a\u57df\u5305\u542b\u7684\u5f15\u811a\u5217\u8868\u3002

gc\uff1a\u533a\u57df\u5bf9\u5e94\u7684gpio_chip\u3002

\u4e00\u4e2astruct gpio_chip\u7ed3\u6784\u4f53\u53ef\u4ee5\u4ee5\u5982\u4e0b\u7684\u65b9\u5f0f\u6dfb\u52a0\u5230pin controller device\u4e2d\uff1a

struct gpio_chip chip_a;\nstruct gpio_chip chip_b;\n\nstatic struct pinctrl_gpio_range gpio_range_a = {\n    .name = \"chip a\",\n    .id = 0,\n    .base = 32,\n    .pin_base = 32,\n    .npins = 16,\n    .gc = &chip_a;\n};\n\nstatic struct pinctrl_gpio_range gpio_range_b = {\n    .name = \"chip b\",\n    .id = 0,\n    .base = 48,\n    .pin_base = 64,\n    .npins = 8,\n    .gc = &chip_b;\n};\n\n{\n    struct pinctrl_dev *pctl;\n    ...\n    pinctrl_add_gpio_range(pctl, &gpio_range_a);\n    pinctrl_add_gpio_range(pctl, &gpio_range_b)\n}\n

\u4ee5\u4e0a\u662f\u4e00\u4e2apin controller device\u7684\u793a\u4f8b\uff0c\u5b83\u5305\u542b\u4e24\u4e2agpio_chip\u7ed3\u6784\u4f53\uff0c\u5206\u522b\u5bf9\u5e94\u4e24\u4e2agpio_chip\u5b9e\u4f8b\u3002\"chip a\"\u548c\"chip b\"\u7684\u8d77\u59cbGPIO ID\u5206\u522b\u4e3a32\u548c48\uff0c\u8d77\u59cbpin ID\u5206\u522b\u4e3a32\u548c64\u3002\"chip a\"\u670916\u4e2a\u5f15\u811a\uff0c\"chip b\"\u67098\u4e2a\u5f15\u811a\u3002

chip a: - GPIO range: 32-47 - Pin range: 32-47

chip b: - GPIO range: 48-55 - Pin range: 64-71

\u4e0a\u8ff0\u4f8b\u5b50\u5047\u5b9aGPIO\u548cpin\u7684\u6620\u5c04\u662f\u7ebf\u6027\u7684\uff0c\u5426\u5219\u6211\u4eec\u5fc5\u987b\u5f97\u624b\u52a8\u5b9a\u4e49\u5f15\u811a\u4fe1\u606f\uff1a

static const unsigned range_pins[] = {14, 1, 22, 17, 10, 8, 6, 2};\n\nstatic struct pinctrl_gpio_range gpio_range_ = {\n    .name = \"chip\",\n    .id = 0,\n    .base = 32,\n    .pins = &range_pins,\n    .npins = ARRAY_SIZE(range_pins),\n    .gc = &chip;\n};\n

\u5728\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cpin_base\u5c5e\u6027\u4f1a\u88ab\u5ffd\u7565\u3002

"},{"location":"linux/drivers/platform/","title":"\u5e73\u53f0\u8bbe\u5907\u9a71\u52a8","text":"

\u5bf9\u4e8eI2C\u3001SPI\u3001USB\u8fd9\u4e9b\u5e38\u89c1\u7684\u8bbe\u5907\u6765\u8bf4\uff0cLinux\u5185\u6838\u90fd\u4f1a\u521b\u5efa\u4e0e\u4e4b\u76f8\u5bf9\u5e94\u7684\u9a71\u52a8\u603b\u7ebf\u3002\u4f46\u662f\u6709\u4e9b\u7ed3\u6784\u7b80\u5355\u7684\u8bbe\u5907\uff0c\u6bd4\u5982led\u3001rtc\u65f6\u949f\u3001\u8702\u9e23\u5668\u7b49\uff0c\u5185\u6838\u5c31\u4e0d\u4f1a\u81ea\u5df1\u521b\u5efa\u9a71\u52a8\u603b\u7ebf\u3002\u4e3a\u4e86\u4f7f\u8fd9\u90e8\u5206\u8bbe\u5907\u7684\u9a71\u52a8\u5f00\u53d1\u4e5f\u80fd\u9075\u5faa\u8bbe\u5907\u9a71\u52a8\u6a21\u578b\uff0cLinux\u5185\u6838\u5f15\u5165\u4e86\u865a\u62df\u7684\u603b\u7ebf\u2014\u2014\u5e73\u53f0\u603b\u7ebf\uff08platform bus\uff09\u3002\u5e73\u53f0\u603b\u7ebf\u7528\u4e8e\u7ba1\u7406\u548c\u6302\u8f7d\u90a3\u4e9b\u6ca1\u6709\u76f8\u5e94\u7269\u7406\u603b\u7ebf\u7684\u8bbe\u5907\uff0c\u8fd9\u4e9b\u8bbe\u5907\u88ab\u79f0\u4e3a\u5e73\u53f0\u8bbe\u5907\uff0c\u5bf9\u5e94\u7684\u8bbe\u5907\u9a71\u52a8\u88ab\u79f0\u4e3a\u5e73\u53f0\u9a71\u52a8\u3002\u5e73\u53f0\u8bbe\u5907\u5bf9\u4e8eLinux\u9a71\u52a8\u5de5\u7a0b\u5e08\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5927\u591a\u6570\u7684\u9a71\u52a8\u4ee3\u7801\uff0c\u5b9e\u9645\u5c31\u662f\u4e3a\u4e86\u9a71\u52a8\u5e73\u53f0\u8bbe\u5907\u3002

Platform\u67b6\u6784\u56fe\u5982\u4e0b\u6240\u793a\uff1a

\u4ece\u56fe\u7247\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff1a

  • platform bus\uff1a\u7ee7\u627f\u81eabus\u6a21\u5757\uff0c\u7528\u4e8e\u6302\u8f7dplatform\u8bbe\u5907\u3002
  • platform device\uff1a\u7ee7\u627f\u81eadevice\u6a21\u5757\uff0c\u7528\u4e8e\u63cf\u8ff0platform\u8bbe\u5907\u3002
  • platform drvier\uff1a\u7ee7\u627f\u81eadevice_driver\u6a21\u5757\uff0c\u7528\u4e8e\u9a71\u52a8platform\u8bbe\u5907\u3002
"},{"location":"linux/drivers/platform/#_2","title":"\u5e73\u53f0\u603b\u7ebf","text":"

\u5728Linux\u5e73\u53f0\u8bbe\u5907\u9a71\u52a8\u6a21\u578b\u4e2d\uff0c\u603b\u7ebf\u662f\u6700\u91cd\u8981\u7684\u4e00\u73af\uff0c\u8d1f\u8d23\u5339\u914d\u8bbe\u5907\u548c\u9a71\u52a8\u3002\u5b83\u7ef4\u62a4\u7740\u4e24\u4e2a\u94fe\u8868\uff0c\u91cc\u9762\u8bb0\u5f55\u7740\u5404\u4e2a\u5df2\u7ecf\u6ce8\u518c\u7684\u5e73\u53f0\u8bbe\u5907\u548c\u5e73\u53f0\u9a71\u52a8\u3002\u6bcf\u5f53\u6709\u65b0\u7684\u8bbe\u5907\u6216\u8005\u662f\u9a71\u52a8\u52a0\u5165\u5230\u603b\u7ebf\u65f6\uff0c\u4fbf\u4f1a\u8c03\u7528platform_match()\u51fd\u6570\u5bf9\u65b0\u589e\u7684\u8bbe\u5907\u6216\u9a71\u52a8\u8fdb\u884c\u914d\u5bf9\u3002\u5185\u6838\u4f7f\u7528struct bus_type platform_bus_type\u6765\u63cf\u8ff0\u5e73\u53f0\u603b\u7ebf\uff0c\u8be5\u603b\u7ebf\u5728\u5185\u6838\u521d\u59cb\u5316\u7684\u65f6\u5019\u6ce8\u518c\uff1a

sturct bus_type platform_bus_type{\n    .name = \"platform\",\n    .dev_groups = platform_dev_groups,\n    .match = platform_match,\n    .uevent = platform_uevent,\n    .pm = &platform_dev_pm_ops,\n};\n

\u5bf9platform_bus_type\u7684\u521d\u59cb\u5316\u6765\u8bf4\uff0cmatch\u51fd\u6570\u6307\u9488\u6700\u4e3a\u91cd\u8981\uff0c\u5b83\u6307\u5411\u7684\u51fd\u6570\u8d1f\u8d23\u5b9e\u73b0\u5e73\u53f0\u603b\u7ebf\u548c\u5e73\u53f0\u8bbe\u5907\u7684\u5339\u914d\u8fc7\u7a0b\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u9a71\u52a8\u603b\u7ebf\uff0c\u90fd\u5fc5\u987b\u5b9e\u4f8b\u5316\u8be5\u51fd\u6570\u6307\u9488\u3002

"},{"location":"linux/drivers/platform/#_3","title":"\u5e73\u53f0\u8bbe\u5907","text":"

\u5185\u6838\u4f7f\u7528platform_device\u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u5e73\u53f0\u8bbe\u5907\uff1a

 struct platform_device {\n     const char *name;    //\u8bbe\u5907\u540d\u79f0\uff0c\u5339\u914d\u65f6\u4f1a\u6bd4\u8f83\u9a71\u52a8\u7684\u540d\u5b57\n     int id;              //\u5185\u6838\u5141\u8bb8\u5b58\u5728\u591a\u4e2a\n     struct device dev;   //\u7ee7\u627f\u7684device\u7ed3\u6784\u4f53\n     u32 num_resources;   //\u8bb0\u5f55\u8d44\u6e90\u7684\u6570\u76ee\n     struct resource *resource;    //\u5e73\u53f0\u8bbe\u5907\u63d0\u4f9b\u7ed9\u9a71\u52a8\u7684\u8d44\u6e90\n     const struct platform_device_id *id_entry;    \n };\n

\u5e73\u53f0\u8bbe\u5907\u7684\u5de5\u4f5c\u662f\u4e3a\u9a71\u52a8\u7a0b\u5e8f\u63d0\u4f9b\u8bbe\u5907\u4fe1\u606f,\u8bbe\u5907\u4fe1\u606f\u5305\u62ec\u786c\u4ef6\u4fe1\u606f\u548c\u8f6f\u4ef6\u4fe1\u606f\u4e24\u90e8\u5206\u3002

  1. \u786c\u4ef6\u4fe1\u606f\uff1a\u9a71\u52a8\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528\u5230\u4ec0\u4e48\u5bc4\u5b58\u5668\uff0c\u5360\u7528\u54ea\u4e9b\u4e2d\u65ad\u53f7\u3001\u5185\u5b58\u8d44\u6e90\u3001IO\u53e3\u7b49\u7b49

  2. \u8f6f\u4ef6\u4fe1\u606f\uff1a\u4ee5\u592a\u7f51\u5361\u8bbe\u5907\u4e2d\u7684MAC\u5730\u5740\u3001I2C\u8bbe\u5907\u4e2d\u7684\u8bbe\u5907\u5730\u5740\u3001SPI\u8bbe\u5907\u7684\u7247\u9009\u4fe1\u53f7\u7ebf\u7b49\u7b49

\u5bf9\u4e8e\u786c\u4ef6\u4fe1\u606f\uff0c\u4f7f\u7528\u7ed3\u6784\u4f53struct resource\u6765\u4fdd\u5b58\u8bbe\u5907\u6240\u63d0\u4f9b\u7684\u8d44\u6e90\uff0c\u6bd4\u5982\u8bbe\u5907\u4f7f\u7528\u7684\u4e2d\u65ad\u7f16\u53f7\uff0c\u5bc4\u5b58\u5668\u7269\u7406\u5730\u5740\u7b49\uff0c\u7ed3\u6784\u4f53\u539f\u578b\u5982\u4e0b\uff1a

struct resource {\n    resource_size_t start;\n    resource_size_t end;\n    const char *name;\n    unsigned long flags;\n};\n

name\uff1a \u6307\u5b9a\u8d44\u6e90\u7684\u540d\u5b57\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e3aNULL

start\u3001end\uff1a \u6307\u5b9a\u8d44\u6e90\u7684\u8d77\u59cb\u5730\u5740\u4ee5\u53ca\u7ed3\u675f\u5730\u5740

flags\uff1a \u7528\u4e8e\u6307\u5b9a\u8be5\u8d44\u6e90\u7684\u7c7b\u578b\uff0c\u5728Linux\u4e2d\uff0c\u8d44\u6e90\u5305\u62ecI/O\u3001Memory\u3001Register\u3001IRQ\u3001DMA\u3001Bus\u7b49\u591a\u79cd\u7c7b\u578b\uff0c\u6700\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a

\u8d44\u6e90\u5b8f\u5b9a\u4e49 \u63cf\u8ff0 IORESOURCE_IO \u7528\u4e8eIO\u5730\u5740\u7a7a\u95f4\uff0c\u5bf9\u5e94\u4e8eIO\u7aef\u53e3\u6620\u5c04\u65b9\u5f0f IORESOURCE_MEM \u7528\u4e8e\u5916\u8bbe\u7684\u53ef\u76f4\u63a5\u5bfb\u5740\u7684\u5730\u5740\u7a7a\u95f4 IORESOURCE_IRQ \u7528\u4e8e\u6307\u5b9a\u8be5\u8bbe\u5907\u4f7f\u7528\u67d0\u4e2a\u4e2d\u65ad IORESOURCE_DMA \u7528\u4e8e\u6307\u5b9a\u4f7f\u7528\u7684DMA\u901a\u9053

\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u7684\u4e3b\u8981\u76ee\u7684\u662f\u64cd\u4f5c\u8bbe\u5907\u7684\u5bc4\u5b58\u5668\u3002\u4e0d\u540c\u67b6\u6784\u7684\u8ba1\u7b97\u673a\u63d0\u4f9b\u4e0d\u540c\u7684\u64cd\u4f5c\u63a5\u53e3\uff0c\u4e3b\u8981\u6709IO\u7aef\u53e3\u6620\u5c04\u548cIO\u5167\u5b58\u6620\u5c04\u4e24\u79cd\u65b9\u5f0f\u3002\u5bf9\u5e94\u4e8eIO\u7aef\u53e3\u6620\u5c04\u65b9\u5f0f\uff0c\u53ea\u80fd\u901a\u8fc7\u4e13\u95e8\u7684\u63a5\u53e3\u51fd\u6570\uff08\u5982inb\u3001outb\uff09\u624d\u80fd\u8bbf\u95ee\uff1b\u91c7\u7528IO\u5185\u5b58\u6620\u5c04\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u50cf\u8bbf\u95ee\u5185\u5b58\u4e00\u6837\uff0c\u53bb\u8bfb\u5199\u5bc4\u5b58\u5668\u3002\u5728\u5d4c\u5165\u5f0f\u4e2d\uff0c\u57fa\u672c\u4e0a\u6ca1\u6709IO\u5730\u5740\u7a7a\u95f4\uff0c\u6240\u4ee5\u901a\u5e38\u4f7f\u7528IORESOURCE_MEM\u3002

\u5728\u8d44\u6e90\u7684\u8d77\u59cb\u5730\u5740\u548c\u7ed3\u675f\u5730\u5740\u4e2d\uff0c\u5bf9\u4e8eIORESOURCE_IO\u6216\u8005\u662fIORESOURCE_MEM\uff0c\u4ed6\u4eec\u8868\u793a\u8981\u4f7f\u7528\u7684\u5185\u5b58\u7684\u8d77\u59cb\u4f4d\u7f6e\u4ee5\u53ca\u7ed3\u675f\u4f4d\u7f6e\uff1b\u82e5\u662f\u53ea\u7528\u4e00\u4e2a\u4e2d\u65ad\u5f15\u811a\u6216\u8005\u662f\u4e00\u4e2a\u901a\u9053\uff0c\u5219\u5b83\u4eec\u7684start\u548cend\u6210\u5458\u503c\u5fc5\u987b\u662f\u76f8\u7b49\u7684\u3002

\u6ce8\u518c/\u6ce8\u9500\u5e73\u53f0\u8bbe\u5907\u7528\u5230\u7684\u51fd\u6570\u5982\u4e0b\uff1a

int platform_device_register(struct platform_device *pdev);\nvoid platform_device_unregister(struct platform_device *pdev);\n

\u8fd9\u4e24\u4e2a\u51fd\u6570\u5e94\u8be5\u5728\u6a21\u5757\u7684\u8fdb\u5165\u4e0e\u9000\u51fa\u51fd\u6570\u4e2d\u88ab\u8c03\u7528\u3002

"},{"location":"linux/drivers/platform/#_4","title":"\u5e73\u53f0\u9a71\u52a8","text":"

\u5185\u6838\u4f7f\u7528platform_driver\u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u5e73\u53f0\u9a71\u52a8\uff1a

struct platform_driver {\n    int (*probe)(struct platform_device *);\n    int (*remove)(structg platform_device *);\n    int (*suspend)(struct platform_device *, pm_message_t state);\n    int (*resume)(struct platform_device *);\n    struct device_driver driver;\n    const struct platform_device_id *id_table;\n};\n

\u9664\u4e86\u63d0\u4f9b\u4e00\u4e9b\u56de\u8c03\u51fd\u6570\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2aid_table\u7684\u6307\u9488\u3002\u8fd9\u4e2a\u6307\u9488\u7528\u6765\u8868\u793a\u8be5\u9a71\u52a8\u80fd\u591f\u517c\u5bb9\u7684platform_device\u3002

\u6211\u4eec\u6765\u770b\u4e00\u4e0bstruct platform_device_id\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\uff1a

struct platform_device_id {\n    char name[PLATFORM_NAME_SIZE];\n    kernel_ulong_t driver_data;\n};\n

name\u7528\u4e8e\u6307\u5b9a\u9a71\u52a8\u7684\u540d\u79f0\uff0c\u603b\u7ebf\u8fdb\u884c\u5339\u914d\u65f6\uff0c\u4f1a\u6839\u636e\u8be5name\u6210\u5458\u4e0eplatform_device\u4e2d\u7684name\u6210\u5458\u8fdb\u884c\u5339\u914d\uff0c\u4e00\u65e6\u5339\u914d\u5c31\u4f1a\u8c03\u7528probe()\u51fd\u6570\u3002driver_data\u7528\u6765\u4fdd\u5b58\u8bbe\u5907\u7684\u914d\u7f6e\u3002\u4e3a\u4e86\u51cf\u5c11\u4ee3\u7801\u7684\u5197\u4f59\uff0c\u4e00\u4e2a\u9a71\u52a8\u53ef\u4ee5\u5339\u914d\u591a\u4e2a\u8bbe\u5907\u3002

\u8fd9\u91cc\u63d2\u4e2a\u9898\u5916\u8bdd\uff0c\u5f53\u624b\u52a8\u5b9e\u73b0probe()\u51fd\u6570\u65f6\u6d89\u53ca\u5230\u5185\u5b58\u5206\u914d\u7684\u95ee\u9898\u3002\u5f88\u591a\u9a71\u52a8\u7a0b\u5e8f\u90fd\u4f7f\u7528devm_kzalloc()\u51fd\u6570\u6765\u5206\u914d\u5185\u5b58\u3002\u6211\u4eec\u63a5\u89e6\u6bd4\u8f83\u591a\u7684\u662fkmalloc()\u6216\u8005kzalloc()\u6765\u5206\u914d\u5185\u5b58\uff0c\u4f46\u662f\u8fd9\u4f1a\u5e26\u6765\u4e00\u4e9b\u6f5c\u5728\u7684\u95ee\u9898\u3002\u6bd4\u5982\u5728\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\u5982\u679c\u5931\u8d25\u4e86\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5f00\u53d1\u4eba\u5458\u5c0f\u5fc3\u5730\u91ca\u653e\u5185\u5b58\u3002\u800cdevm_kzalloc()\u51fd\u6570\u5219\u53ef\u4ee5\u81ea\u52a8\u91ca\u653e\u5185\u5b58\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u5f00\u53d1\u4eba\u5458\u62c5\u5fc3\u5185\u5b58\u91ca\u653e\u7684\u95ee\u9898\u4e86\u3002\u5176\u8bbe\u8ba1\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f\u8d44\u6e90\u7531\u8bbe\u5907\u7ba1\u7406\uff0c\u4e00\u65e6\u4e0d\u9700\u8981\u4e5f\u7531\u8bbe\u5907\u6765\u91ca\u653e\uff0c\u8fd9\u5176\u5b9e\u6709\u70b9C++\u4e2dRAII\u7684\u601d\u60f3\u3002

\u6ce8\u518c/\u6ce8\u9500\u5e73\u53f0\u9a71\u52a8\u7684\u51fd\u6570\u5982\u4e0b\uff1a

int platform_driver_register(struct platform_device *drv);\nvoid platform_driver_unregister(struct platform_device *drv);\n

\u5728\u5e73\u53f0\u8bbe\u5907\u4e2d\uff0cstruct resource\u7ed3\u6784\u4f53\u7528\u6765\u8868\u793a\u786c\u4ef6\u4fe1\u606f\uff0c\u800c\u8f6f\u4ef6\u4fe1\u606f\u5219\u53ef\u4ee5\u7528\u8bbe\u5907\u7ed3\u6784\u4f53device\u4e2d\u7684\u6210\u5458platform_data\u6765\u4fdd\u5b58\u3002

platform_get_resource()\u51fd\u6570\u901a\u5e38\u4f1a\u5728\u9a71\u52a8\u7684probe()\u51fd\u6570\u4e2d\u6267\u884c\uff0c\u7528\u4e8e\u83b7\u53d6platform_device\u4e2d\u7684resource\u7ed3\u6784\u4f53\uff1a

struct resource *platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);\n

dev\uff1a\u6307\u5b9a\u8981\u83b7\u53d6\u7684\u5e73\u53f0\u8bbe\u5907\u3002

type\uff1a\u6307\u5b9a\u83b7\u53d6\u8d44\u6e90\u7684\u7c7b\u578b\uff0c\u6bd4\u5982IORESOURCE_IO\u3002

num\uff1a\u6307\u5b9a\u8981\u83b7\u53d6\u7684\u8d44\u6e90\u7f16\u53f7\u3002

\u5982\u679c\u8d44\u6e90\u7c7b\u578b\u662fIORESOURCE_IRQ\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u63a5\u53e3\u8fd8\u83b7\u53d6\u4e2d\u65ad\u5f15\u811a\uff1a

int platform_get_irq(struct platform_device *pdev, unsigned int num);\n

\u5bf9\u4e8e\u5b58\u653e\u5728device\u7ed3\u6784\u4f53\u4e2dplatform_data\u7684\u8f6f\u4ef6\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528dev_get_platdata()\u51fd\u6570\u6765\u83b7\u53d6\uff1a

static inline void *dev_get_platdata(const struct device *dev)\n{\n    return dev->platform_data;\n}\n

\u603b\u7ed3\u4e00\u4e0b\u5e73\u53f0\u9a71\u52a8\uff1a\u9700\u8981\u624b\u52a8\u5b9e\u73b0probe()\u51fd\u6570\uff0c\u5f53\u5e73\u53f0\u603b\u7ebf\u6210\u529f\u5339\u914dplatform_device\u548cplatform_driver\u65f6\uff0c\u5219\u4f1a\u8c03\u7528\u9a71\u52a8\u7684probe()\u51fd\u6570\uff0c\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u7684\u51fd\u6570\u63a5\u53e3\u6765\u83b7\u53d6\u8d44\u6e90\uff0c\u4ee5\u521d\u59cb\u5316\u8bbe\u5907\uff0c\u6700\u540e\u586b\u5145\u7ed3\u6784\u4f53platform_driver\uff0c\u8c03\u7528platform_driver_register()\u8fdb\u884c\u6ce8\u518c\u3002

"},{"location":"linux/drivers/spi/","title":"SPI\u5b50\u7cfb\u7edf","text":""},{"location":"linux/drivers/uart/","title":"UART\u5b50\u7cfb\u7edf","text":""},{"location":"linux/ebpf/","title":"ebpf\u6280\u672f","text":"

\u5b89\u88c5bcc\u8f6f\u4ef6\u5305\uff1a

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4052245BD4284CDD\necho \"deb https://repo.iovisor.org/apt/xenial xenial main\" | sudo tee /etc/apt/sources.list.d/iovisor.list\nsudo apt-get update\nsudo apt-get install -y bcc-tools libbcc-examples linux-headers-$(uname -r)\n
"},{"location":"linux/kernel/","title":"\u5185\u6838\u57fa\u7840\u77e5\u8bc6","text":""},{"location":"linux/kernel/#_2","title":"\u5185\u6838\u6e90\u7801","text":"

\u8981\u83b7\u53d6\u5185\u6838\u6e90\u7801\uff0c\u8bf7\u4f7f\u7528git\u3002

git clone git://git.ernel.org/pub/scm/linux/kernel/git/torvalds/[linux\u7248\u672c\u53f7].git\n

\u5185\u6838\u6e90\u7801\u6811\u7531\u8bb8\u591a\u76ee\u5f55\u7ec4\u6210\uff0c\u4e00\u4e9b\u6bd4\u8f83\u91cd\u8981\u7684\u76ee\u5f55\u7684\u63cf\u8ff0\u5982\u4e0b\uff1a

\u76ee\u5f55\u540d \u63cf\u8ff0 arch \u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\u4ee3\u7801 block \u5757\u8bbe\u5907I/O\u5c42 drivers \u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f fs \u6587\u4ef6\u7cfb\u7edf include \u5185\u6838\u5934\u6587\u4ef6 init \u521d\u59cb\u5316\u4ee3\u7801 ipc \u8fdb\u7a0b\u95f4\u901a\u4fe1\u4ee3\u7801 kernel \u5185\u6838\u6838\u5fc3\u4ee3\u7801 lib \u5185\u6838\u5e93\u51fd\u6570 mm \u5185\u5b58\u7ba1\u7406 net \u7f51\u7edc\u7cfb\u7edf

\u8981\u7f16\u8bd1\u5185\u6838\uff0c\u8bf7\u4f7f\u7528make menuconfig\u3002\u5185\u6838\u7684\u5404\u79cd\u914d\u7f6e\uff0c\u4ee5CONFIG_FEATURE\u7684\u5f62\u5f0f\u5199\u5165.config\u6587\u4ef6\u3002\u914d\u7f6e\u9009\u9879\u6709\u4e09\u79cd\uff1ayes\u3001no\u6216module\u3002\u5206\u522b\u5bf9\u5e94\u7f16\u8bd1\u3001\u4e0d\u7f16\u8bd1\u3001\u4ee5\u6a21\u5757\u5f62\u5f0f\u7f16\u8bd1\u3002

\u5185\u6838\u5f00\u53d1\u7684\u7279\u70b9\u5982\u4e0b\uff1a

  • \u4e0d\u80fd\u4f7f\u7528\u6807\u51c6C\u5e93\u5934\u6587\u4ef6\uff0c\u53ea\u80fd\u4f7f\u7528\u5185\u6838\u63d0\u4f9b\u7684\u5934\u6587\u4ef6\u3002\u4e0e\u4f53\u7cfb\u7ed3\u6784\u65e0\u5173\u7684\u5934\u6587\u4ef6\u4f4d\u4e8e\u5185\u6838\u6e90\u7801\u6839\u76ee\u5f55\u4e0b\u7684include\u76ee\u5f55\u3002\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\u5934\u6587\u4ef6\u4f4d\u4e8e<arch/architecture/include/asm>\u76ee\u5f55\uff0c\u5185\u6838\u4ee3\u7801\u4ee5asm\u524d\u7f00\u7684\u5f62\u5f0f\u5305\u542b\u8fd9\u4e9b\u5934\u6587\u4ef6\u3002

  • \u5fc5\u987b\u4f7f\u7528GNU C\u3002gcc\u7f16\u8bd1\u5668\u652f\u6301\u4ee5asm()\u6307\u4ee4\u5f00\u5934\u5d4c\u5165\u6c47\u7f16\u4ee3\u7801\u3002

  • \u6ca1\u6709\u5185\u5b58\u4fdd\u62a4\u673a\u5236\u3002\u5982\u679c\u662f\u5185\u6838\u8bbf\u95ee\u4e86\u975e\u6cd5\u5185\u5b58\uff0c\u540e\u679c\u4e0d\u582a\u8bbe\u60f3\u3002

  • \u65e0\u6cd5\u6267\u884c\u6d6e\u70b9\u8fd0\u7b97\u3002\u5185\u6838\u5bf9\u4e8e\u6d6e\u70b9\u8fd0\u7b97\u7684\u652f\u6301\u5ea6\u4e0d\u591f\u3002

  • \u5fc5\u987b\u8003\u8651\u540c\u6b65\u4e0e\u5e76\u53d1\u3002Linux\u5185\u6838\u662f\u62a2\u5360\u5f0f\u591a\u4efb\u52a1\u5904\u7406\u7cfb\u7edf\uff0c\u5f53\u524d\u4efb\u52a1\u968f\u65f6\u6709\u53ef\u80fd\u88ab\u53e6\u4e00\u4e2a\u4efb\u52a1\u62a2\u5360\u3002

  • \u5fc5\u987b\u8003\u8651\u53ef\u79fb\u690d\u6027\u3002\u6bd4\u5982\u4fdd\u6301\u5b57\u8282\u5e8f\u300164 \u4f4d\u5bf9\u9f50\u3001\u4e0d\u5047\u5b9a\u5b57\u957f\u548c\u9875\u9762\u957f\u5ea6\u7b49\u4e00\u7cfb\u5217\u51c6\u5219\u3002

"},{"location":"linux/kernel/block/","title":"\u5757I/O","text":"

\u7cfb\u7edf\u4e2d\u968f\u673a\u8bbf\u95ee\u56fa\u5b9a\u5927\u5c0f\u6570\u636e\u7247\u7684\u786c\u4ef6\u8bbe\u5907\u88ab\u79f0\u4e3a\u5757\u8bbe\u5907\uff0c\u8fd9\u4e9b\u56fa\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u7247\u5c31\u88ab\u79f0\u4e3a\u5757\uff0c\u6700\u5e38\u89c1\u7684\u5757\u8bbe\u5907\u662f\u786c\u76d8\u3002

\u53e6\u4e00\u79cd\u57fa\u672c\u7684\u8bbe\u5907\u7c7b\u578b\u662f\u5b57\u7b26\u8bbe\u5907\uff0c\u6bd4\u5982\u4e32\u53e3\u548c\u952e\u76d8\u3002\u5b83\u6309\u7167\u5b57\u7b26\u6d41\u7684\u65b9\u5f0f\u88ab\u6709\u5e8f\u8bbf\u95ee\uff0c\u968f\u673a\u7684\u8bbf\u95ee\u662f\u65e0\u610f\u4e49\u7684\u3002\u5f53\u5728\u952e\u76d8\u4e0a\u6253\u51fa\u201cchar\u201d\u8fd9\u4e2a\u5355\u8bcd\u65f6\uff0c\u5c4f\u5e55\u5fc5\u987b\u6309\u7167\u8f93\u5165\u987a\u5e8f\u663e\u793a\u51fa\u6765\u3002

\u76f8\u6bd4\u8f83\u4e4b\u4e0b\uff0c\u56e0\u4e3a\u5757\u8bbe\u5907\u652f\u6301\u968f\u673a\u7684I/O\uff0c\u6240\u4ee5\u5185\u6838\u5bf9\u4e8e\u5757\u8bbe\u5907\u7684\u7ba1\u7406\u6bd4\u5b57\u7b26\u8bbe\u5907\u8981\u590d\u6742\u8bb8\u591a\u3002\u66f4\u91cd\u8981\u7684\u662f\u5757\u8bbe\u5907\u5bf9\u4e8e\u6267\u884c\u6027\u80fd\u7684\u8981\u6c42\u5f88\u9ad8\uff0c\u5bf9\u4e8e\u786c\u76d8\u8bfb\u5199\u7684\u4f18\u5316\u53ef\u4ee5\u5e26\u6765\u6574\u4e2a\u7cfb\u7edf\u6027\u80fd\u7684\u63d0\u5347\u3002

"},{"location":"linux/kernel/block/#_1","title":"\u78c1\u76d8","text":"

\u78c1\u76d8\u662f\u5757\u8bbe\u5907\u7684\u4e00\u79cd\uff0c\u5e38\u89c1\u7684\u78c1\u76d8\u6709\u673a\u68b0\u78c1\u76d8\u548c\u56fa\u6001\u78c1\u76d8\uff0c\u5176\u4e2d\u673a\u68b0\u78c1\u76d8\u7531\u4e8e\u9700\u8981\u78c1\u9053\u5bfb\u5740\uff0c\u6240\u4ee5\u901f\u5ea6\u6bd4\u56fa\u6001\u78c1\u76d8\u8981\u6162\u5f88\u591a\u3002\u78c1\u76d8\u7684\u8bfb\u5199\u6709\u4e24\u79cd\u65b9\u5f0f\u2014\u2014\u968f\u673aI/O\u548c\u8fde\u7eedI/O\uff1a\u968f\u673aI/O\u662f\u6307\u6bcf\u6b21\u8bfb\u5199\u64cd\u4f5c\u90fd\u4ece\u78c1\u76d8\u7684\u67d0\u4e2a\u4f4d\u7f6e\u91cd\u65b0\u5f00\u59cb\uff0c\u800c\u8fde\u7eedI/O\u662f\u6307\u6bcf\u6b21\u8bfb\u5199\u64cd\u4f5c\u90fd\u4ece\u540c\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u9884\u8bfb\u7684\u65b9\u5f0f\uff0c\u51cf\u5c11I/O\u8bf7\u6c42\u7684\u6b21\u6570\u3002

\u78c1\u76d8\u8bfb\u5199\u7684\u6700\u5c0f\u5355\u4f4d\u662f\u6247\u533a\uff0c\u4f46\u662f\u6247\u533a\u53ea\u6709512B\u5927\u5c0f\u3002\u4e3a\u4e86\u63d0\u9ad8\u8bfb\u5199\u7684\u6548\u7387\uff0c\u6587\u4ef6\u7cfb\u7edf\u5c06\u8fde\u7eed\u7684\u6247\u533a\u7ec4\u5408\u6210\u903b\u8f91\u5757\uff0c\u6bcf\u4e2a\u903b\u8f91\u5757\u7684\u5927\u5c0f\u6700\u5e38\u89c1\u7684\u662f4KB\u2014\u2014\u75318\u4e2a\u6247\u533a\u7ec4\u6210\u3002

\u76ee\u5f55\u9879\u3001\u7d22\u5f15\u8282\u70b9\u4ee5\u53ca\u78c1\u76d8\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

"},{"location":"linux/kernel/block/#_2","title":"\u901a\u7528\u5757\u5c42","text":"

\u4e3a\u4e86\u5c4f\u853d\u4e0d\u540c\u5757\u8bbe\u5907\u5e26\u6765\u7684\u5dee\u5f02\uff0c\u5185\u6838\u5f15\u5165\u4e86\u901a\u7528\u5757\u5c42\uff0c\u6765\u7ba1\u7406\u4e0d\u540c\u7684\u5757\u8bbe\u5907\u3002

\u901a\u7528\u5757\u66fe\u662f\u6587\u4ef6\u7cfb\u7edf\u548c\u78c1\u76d8\u9a71\u52a8\u4e2d\u95f4\u7684\u4e00\u4e2a\u5757\u8bbe\u5907\u62bd\u8c61\u5c42\uff0c\u5b83\u6709\u4e24\u4e2a\u529f\u80fd\uff1a

  1. \u5411\u4e0a\uff0c\u4e3a\u6587\u4ef6\u7cfb\u7edf\u548c\u5e94\u7528\u7a0b\u5e8f\uff0c\u63d0\u4f9b\u8bbf\u95ee\u5757\u8bbe\u5907\u7684\u6807\u51c6\u63a5\u53e3\uff1b\u5411\u4e0b\uff0c\u628a\u4e0d\u540c\u7684\u5757\u8bbe\u5907\u62bd\u8c61\u4e3a\u7edf\u4e00\u4e2a\u5757\u8bbe\u5907\u3002
  2. \u7ba1\u7406\u5757\u8bbe\u5907\u7684I/O\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7\u91cd\u65b0\u6392\u5e8f\u3001\u8bf7\u6c42\u5408\u5e76\u7b49\u65b9\u5f0f\uff0c\u4f18\u5316\u8bfb\u5199\u78c1\u76d8\u7684\u6548\u7387\u3002

\u5bf9I/O\u8bf7\u6c42\u6392\u5e8f\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u6240\u8c13\u7684I/O\u8c03\u5ea6\u3002\u5185\u6838\u652f\u6301\u56db\u79cd\u8c03\u5ea6\u7b97\u6cd5\uff0c\u5206\u522b\u662fNONE\u3001NOOP\u3001CFQ\u4ee5\u53caDeadlLine\u3002

  • NONE\uff1a\u4e0d\u505a\u4efb\u4f55\u5904\u7406\uff0c\u5e38\u7528\u5728\u865a\u62df\u673a\u4e2d\u3002
  • NOOP\uff1a\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5148\u5165\u5148\u51fa\u7684\u961f\u5217\uff0c\u5236\u4f5c\u4e00\u4e9b\u6700\u57fa\u672c\u7684\u8bf7\u6c42\u5408\u5e76\uff0c\u5e38\u7528\u4e8eSSD\u78c1\u76d8\u3002
  • CFQ\uff1a\u9ed8\u8ba4I/O\u8c03\u5ea6\u5668\uff0c\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u7ef4\u62a4\u4e86\u4e00\u4e2aI/O\u8c03\u5ea6\u961f\u5217\uff0c\u5e76\u6309\u7167\u65f6\u95f4\u7247\u6765\u5747\u5300\u5206\u5e03\u6bcf\u4e2a\u8fdb\u7a0b\u7684I/O\u8bf7\u6c42\u3002\u9002\u7528\u4e8e\u8fd0\u884c\u5927\u91cf\u8fdb\u7a0b\u7684\u7cfb\u7edf\u3002
  • DeadLine\uff1a\u4e3a\u8bfb\u3001\u5199\u8bf7\u6c42\u521b\u5efa\u4e0d\u540c\u7684I/O\u961f\u5217\uff0c\u786e\u4fdd\u8fbe\u5230deadline\u7684\u8bf7\u6c42\u88ab\u4f18\u5148\u5904\u7406\u3002\u5e38\u7528\u4e8e\u6570\u636e\u5e93\u4e2d\u3002
"},{"location":"linux/kernel/interrupt/","title":"\u4e2d\u65ad\u548c\u4e2d\u65ad\u5904\u7406","text":"

\u4e2d\u65ad\u662f\u4e00\u79cd\u4e8b\u4ef6\uff0c\u8be5\u4e8b\u4ef6\u6539\u53d8\u5904\u7406\u5668\u6267\u884c\u6307\u4ee4\u7684\u987a\u5e8f\u3002\u8fd9\u79cd\u65b9\u5f0f\u6709\u540c\u6b65\u548c\u5f02\u6b65\u4e4b\u5206\u3002\u540c\u6b65\u4e2d\u65ad\u4e5f\u88ab\u79f0\u4e3a\u5f02\u5e38\u3002\u5728\u5904\u7406\u5668\u6267\u884c\u5230\u7531\u4e8e\u7f16\u7a0b\u5931\u8bef\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u6307\u4ee4\uff08\u6bd4\u5982\u9664\u4ee50\uff09\u7684\u65f6\u5019\uff0c\u6216\u8005\u662f\u5728\u6267\u884c\u671f\u95f4\u51fa\u73b0\u7279\u6b8a\u60c5\u51b5\uff08\u6bd4\u5982\u7f3a\u9875\uff09\uff0c\u5fc5\u987b\u9760\u5185\u6838\u5904\u7406\u7684\u65f6\u5019\uff0c\u5904\u7406\u5668\u5c31\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5f02\u5e38\u3002\u5f02\u6b65\u4e2d\u65ad\u5219\u662f\u7531\u786c\u4ef6\u5f15\u8d77\u7684\u3002

\u4e2d\u65ad\u53c8\u53ef\u4ee5\u5206\u4e3a\u53ef\u5c4f\u853d\u4e2d\u65ad\u548c\u4e0d\u53ef\u5c4f\u853d\u4e2d\u65ad\u3002\u901a\u5e38\uff0cI/O\u8bbe\u5907\u53d1\u51fa\u7684\u6240\u6709\u4e2d\u65ad\u8bf7\u6c42\u90fd\u662f\u53ef\u5c4f\u853d\u7684\uff0c\u800c\u90e8\u5206\u5371\u6025\u4e8b\u4ef6\uff08\u6bd4\u5982\u786c\u4ef6\u6545\u969c\uff09\u662f\u4e0d\u53ef\u5c4f\u853d\u7684\u3002\u6bcf\u4e2a\u4e2d\u65ad\u548c\u5f02\u5e38\u90fd\u662f\u75310 ~ 255\u4e4b\u95f4\u7684\u4e00\u4e2a\u6570\u6765\u6807\u8bc6\uff0c\u8fd9\u4e2a\u6570\u88ab\u79f0\u4f5c\u4e3a\u662f\u5411\u91cf\uff08vector\uff09\u3002\u4e0d\u53ef\u5c4f\u853d\u4e2d\u65ad\u7684\u5411\u91cf\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u53ef\u5c4f\u853d\u4e2d\u65ad\u7684\u5411\u91cf\u53ef\u4ee5\u901a\u8fc7\u5bf9\u4e2d\u65ad\u63a7\u5236\u5668\u7684\u7f16\u7a0b\u6765\u6539\u53d8\u3002

\u663e\u7136\uff0c\u4e2d\u65ad\u4fe1\u53f7\u63d0\u4f9b\u4e86\u4e00\u79cd\u7279\u6b8a\u7684\u65b9\u5f0f\uff0c\u8ba9\u5904\u7406\u5668\u53bb\u6267\u884c\u6b63\u5e38\u8fd0\u884c\u4ee3\u7801\u4e4b\u5916\u7684\u903b\u8f91\u3002\u5f53\u4e00\u4e2a\u4e2d\u65ad\u4fe1\u53f7\u5230\u6765\u65f6\uff0cCPU\u5fc5\u987b\u505c\u4e0b\u5b83\u5f53\u524d\u6b63\u5728\u505a\u7684\u4e8b\u60c5\uff0c\u8f6c\u800c\u53bb\u5904\u7406\u4e2d\u65ad\u4ee3\u7801\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u5c31\u9700\u8981\u5728\u5185\u6838\u6001\u5806\u6808\u4fdd\u5b58\u5f53\u524d\u4e0a\u4e0b\u6587\u73af\u5883\uff0c\u5e76\u4e14\u628a\u89e6\u53d1\u4e86\u4f55\u79cd\u4e2d\u65ad\u4e5f\u653e\u8fdb\u5bc4\u5b58\u5668\u4e2d\u3002

\u4f60\u53ef\u80fd\u4f1a\u628a\u4e2d\u65ad\u5904\u7406\u4e0e\u8fdb\u7a0b\u5207\u6362\u8054\u7cfb\u8d77\u6765\u3002\u786e\u5b9e\u8fd9\u4e24\u8005\u6709\u90e8\u5206\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46\u662f\u4e2d\u65ad\u6267\u884c\u7684\u4ee3\u7801\u4e0d\u662f\u8fdb\u7a0b\u73af\u5883\u3002\u5b83\u662f\u4e00\u4e2a\u5185\u6838\u63a7\u5236\u7684\u7279\u6b8a\u8def\u5f84\uff0c\u5fc5\u987b\u5feb\u901f\u5904\u7406\uff0c\u4e0d\u80fd\u505c\u7559\u3002\u7531\u4e8e\u4e2d\u65ad\u5904\u7406\u662f\u5185\u6838\u6700\u4e3a\u654f\u611f\u7684\u4efb\u52a1\u4e4b\u4e00\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u5b83\u5fc5\u987b\u6709\u4ee5\u4e0b\u7ea6\u675f\uff1a

  • \u4e2d\u65ad\u5fc5\u987b\u5c3d\u53ef\u80fd\u5feb\u5730\u5904\u7406\u3002\u4e3a\u6b64\uff0c\u4e2d\u65ad\u5904\u7406\u901a\u5e38\u5206\u4e3a\u4e24\u90e8\u5206\uff1a\u5173\u952e\u800c\u7d27\u6025\u7684\u90e8\u5206\uff0c\u5185\u6838\u7acb\u5373\u6267\u884c\uff1b\u5176\u4f59\u90e8\u5206\u63a8\u8fdf\u6267\u884c\u3002

  • \u4e2d\u65ad\u5fc5\u987b\u53ef\u4ee5\u5d4c\u5957\u6267\u884c\u3002\u7531\u4e8e\u4e2d\u65ad\u968f\u65f6\u4f1a\u5230\u6765\uff0c\u5728\u5185\u6838\u5904\u7406\u5176\u4e2d\u4e00\u4e2a\u4e2d\u65ad\u65f6\uff0c\u53e6\u4e00\u4e2a\u4e2d\u65ad\u4e5f\u4f1a\u53d1\u751f\u3002\u5f53\u4e2d\u65ad\u5904\u7406\u5b8c\u6210\u540e\uff0c\u5185\u6838\u5fc5\u987b\u80fd\u6062\u590d\u88ab\u4e2d\u65ad\u8fdb\u7a0b\u7684\u6267\u884c\u3002

  • \u5728\u4e34\u754c\u533a\u4e2d\uff0c\u4e2d\u65ad\u5fc5\u987b\u88ab\u7981\u6b62\u3002

"},{"location":"linux/kernel/interrupt/#_2","title":"\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f","text":"

\u5728\u54cd\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u4e2d\u65ad\u65f6\uff0c\u5185\u6838\u4f1a\u6267\u884c\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53eb\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff08interrupt handler\uff09\u6216\u4e2d\u65ad\u670d\u52a1\u4f8b\u7a0b\uff08interrupt service routine, ISR\uff09\u3002\u4ea7\u751f\u4e2d\u65ad\u7684\u6bcf\u4e2a\u8bbe\u5907\u90fd\u6709\u4e00\u4e2a\u54cd\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u4e00\u4e2a\u8bbe\u5907\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u662f\u5b83\u9a71\u52a8\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u3002

\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5904\u4e8e\u7279\u6b8a\u7684\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e2d\u3002\u5728\u89e3\u91ca\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u56de\u5fc6\u4e00\u4e0b\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u3002\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528current\u5b8f\u5173\u8054\u5f53\u524d\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u7761\u7720\uff0c\u4e5f\u53ef\u4ee5\u8c03\u7528\u8c03\u5ea6\u7a0b\u5e8f\u3002\u4e0e\u4e4b\u76f8\u53cd\uff0c\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e0e\u8fdb\u7a0b\u6ca1\u6709\u4ec0\u4e48\u5173\u8054\u3002\u56e0\u4e3a\u4e2d\u65ad\u7684\u89e6\u53d1\u662f\u968f\u65f6\u5230\u6765\u7684\uff0c\u5e76\u4e0d\u77e5\u9053\u5f53\u524d\u5904\u4e8e\u54ea\u4e2a\u8fdb\u7a0b\u3002\u56e0\u4e3a\u6ca1\u6709\u540e\u5907\u8fdb\u7a0b\uff0c\u6240\u4ee5\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e0d\u53ef\u7761\u7720\uff0c\u4e5f\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55\u53ef\u4ee5\u5f15\u8d77\u7761\u7720\u7684\u51fd\u6570\u3002

\u4e2d\u65ad\u968f\u65f6\u6709\u53ef\u80fd\u53d1\u751f\uff0c\u56e0\u6b64\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5fc5\u987b\u5feb\u901f\u6267\u884c\u3002\u4f46\u662f\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5f80\u5f80\u8fd8\u8981\u5b8c\u6210\u5927\u91cf\u5176\u4ed6\u7684\u5de5\u4f5c\u3002\u5c24\u5176\u4f53\u73b0\u5728\u7f51\u7edc\u8bbe\u5907\u4e0a\uff0c\u9664\u4e86\u5bf9\u786c\u4ef6\u5e94\u7b54\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u628a\u6765\u81ea\u786c\u4ef6\u7684\u7f51\u7edc\u6570\u636e\u62f7\u8d1d\u5230\u5185\u5b58\uff0c\u663e\u7136\u8fd9\u79cd\u5de5\u4f5c\u91cf\u4e0d\u4f1a\u5c0f\u3002

\u53c8\u60f3\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u5f97\u5feb\uff0c\u53c8\u8981\u5176\u5b8c\u6210\u7684\u5de5\u4f5c\u591a\u3002\u9274\u4e8e\u8fd9\u4e24\u8005\u4e4b\u95f4\u7684\u77db\u76fe\u5173\u7cfb\uff0c\u6211\u4eec\u4e00\u822c\u628a\u4e2d\u65ad\u5904\u7406\u7684\u8fc7\u7a0b\u5207\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff1a\u4e0a\u534a\u90e8\u548c\u4e0b\u534a\u90e8\u3002\u901a\u5e38\u6240\u8bf4\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u662f\u6307\u4e0a\u534a\u90e8\u2014\u2014\u5f53\u63a5\u6536\u5230\u4e00\u4e2a\u4e2d\u65ad\u540e\uff0c\u5b83\u7acb\u523b\u88ab\u6267\u884c\uff0c\u4f46\u53ea\u505a\u6709\u4e25\u683c\u65f6\u9650\u7684\u5de5\u4f5c\u3002\u4f8b\u5982\u5bf9\u63a5\u6536\u5230\u7684\u4e2d\u65ad\u8fdb\u884c\u5e94\u7b54\u3002\u800c\u7a0d\u540e\u9700\u8981\u5b8c\u6210\u7684\u5de5\u4f5c\u5219\u4f1a\u88ab\u63a8\u8fdf\u5230\u4e0b\u534a\u90e8\u53bb\uff0c\u5728\u5408\u9002\u7684\u65f6\u673a\u6267\u884c\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0b\u534a\u90e8\u7684\u673a\u5236\uff0c\u7a0d\u540e\u4f1a\u505a\u8bf4\u660e\u3002

\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7request_irq()\u51fd\u6570\u6ce8\u518c\u4e00\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u5e76\u4e14\u6fc0\u6d3b\u7ed9\u5b9a\u7684\u4e2d\u65ad\u7ebf\uff1a

int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,\n                const char *name, void *dev)\n

\u7b2c\u4e00\u4e2a\u53c2\u6570irq\u8868\u793a\u8981\u5206\u914d\u7684\u4e2d\u65ad\u53f7\u3002

\u7b2c\u4e8c\u4e2a\u53c2\u6570handler\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u6307\u5411\u5904\u7406\u8fd9\u4e2a\u4e2d\u65ad\u7684\u5b9e\u9645\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002

typedef irqreturn_t (*irq_handler_t)(int, void*);\n

\u7b2c\u4e09\u4e2a\u53c2\u6570flags\u53ef\u4ee5\u4e3a0\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e0b\u5217\u4e00\u4e2a\u6216\u591a\u4e2a\u6807\u5fd7\u7684\u4f4d\u63a9\u7801\uff0c\u66f4\u591a\u7684\u5b9a\u4e49\u5728<linux/interrupt.h>\uff1a

  • IRQF_TIMER\u2014\u2014\u7cfb\u7edf\u5b9a\u65f6\u5668\u6807\u5fd7
  • IRQF_SHARED\u2014\u2014\u5728\u591a\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\u4e2d\u65ad\u7ebf\u3002\u5982\u679c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5728\u540c\u4e00\u6761\u7ebf\u4e0a\u6ce8\u518c\uff0c\u90a3\u4e48\u5fc5\u987b\u6307\u5b9a\u8fd9\u4e2a\u6807\u5fd7\u3002

\u7b2c\u56db\u4e2a\u53c2\u6570name\u662f\u4e0e\u4e2d\u65ad\u76f8\u5173\u7684\u8bbe\u5907\u7684ASCII\u6587\u672c\u8868\u793a\u3002\u8fd9\u4e9b\u540d\u5b57\u4f1a\u88ab/proc/irq\u548c/proc/interrupts\u6587\u4ef6\u4f7f\u7528\u3002

\u7b2c\u4e94\u4e2a\u53c2\u6570dev\u7528\u4e8e\u786e\u5b9a\u5171\u4eab\u4e2d\u65ad\u7ebf\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002\u5f53\u591a\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5171\u4eab\u67d0\u6761\u4e2d\u65ad\u7ebf\uff0c\u5176\u4e2d\u67d0\u4e2a\u9700\u8981\u91ca\u653e\u65f6\uff0cdev\u5c06\u63d0\u4f9b\u552f\u4e00\u7684\u6807\u5fd7\u4fe1\u606f\u4ee5\u4fbf\u786e\u5b9a\u662f\u54ea\u4e2a\u7a0b\u5e8f\u9700\u8981\u5220\u9664\u3002

request_irq()\u6210\u529f\u8fd4\u56de0\uff0c\u9519\u8bef\u8fd4\u56de\u975e0\u3002\u6700\u5e38\u89c1\u7684\u9519\u8bef\u662f-EBUSY\uff0c\u8868\u793a\u7ed9\u5b9a\u7684\u4e2d\u65ad\u7ebf\u5df2\u7ecf\u88ab\u4f7f\u7528\u3002

request_irq()\u53ef\u80fd\u4f1a\u7761\u7720\uff0c\u56e0\u6b64\u4e0d\u80fd\u5728\u4e0d\u5141\u8bb8\u963b\u585e\u4ee3\u7801\u4e2d\u8c03\u7528\u8be5\u51fd\u6570\u3002

\u5378\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u65f6\uff0c\u9700\u8981\u6ce8\u9500\u54cd\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u5e76\u91ca\u653e\u4e2d\u65ad\u7ebf\uff1a

void free_irq(unsigned int irq, void *dev)\n

\u5982\u679c\u6307\u5b9a\u7684\u4e2d\u65ad\u7ebf\u4e0d\u662f\u5171\u4eab\u7684\uff0c\u90a3\u4e48\u8be5\u51fd\u6570\u5220\u9664\u5904\u7406\u7a0b\u5e8f\u7684\u540c\u65f6\u5c06\u7981\u7528\u8fd9\u6761\u4e2d\u65ad\u7ebf\u3002\u5982\u679c\u662f\u5171\u4eab\u7684\uff0c\u90a3\u4e48\u4ec5\u5220\u9664\u6240\u5bf9\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u800c\u8fd9\u6761\u4e2d\u65ad\u7ebf\u53ea\u6709\u5728\u5220\u9664\u4e86\u6700\u540e\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u65f6\u624d\u4f1a\u88ab\u7981\u7528\u3002

\u8981\u7f16\u5199\u81ea\u5df1\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u9700\u8981\u7528\u5230\u4ee5\u4e0b\u51fd\u6570\uff1a

static irqreturn_t intr_handler(int irq, void *dev)\n

\u53c2\u6570irq\u5df2\u7ecf\u6ca1\u6709\u4ec0\u4e48\u7528\u4e86\uff0c\u53c2\u6570dev\u7528\u6765\u5411\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4f20\u9012\u6570\u636e\u7ed3\u6784\u3002

Linux\u4e0a\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u65e0\u9700\u8981\u6c42\u53ef\u91cd\u5165\u3002\u56e0\u4e3a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u65f6\u76f8\u5e94\u4e2d\u65ad\u7ebf\u4e0a\u7684\u4e2d\u65ad\u662f\u5c4f\u853d\u7684\uff0c\u4e5f\u5c31\u662f\u4e0d\u4f1a\u53d1\u751f\u76f8\u540c\u4e2d\u65ad\u7684\u5d4c\u5957\u3002\u4f46\u4e0d\u540c\u4e2d\u65ad\u7684\u5d4c\u5957\u6709\u53ef\u80fd\u53d1\u751f\u3002

\u5bf9\u4e8e\u5171\u4eab\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u9664\u4e86\u8981\u6307\u5b9arequest_irq()\u51fd\u6570\u4e2d\u7684\u53c2\u6570flags\u4e3aIRQF_SHARED\u6807\u5fd7\uff0c\u8fd8\u9700\u8981\u4fdd\u8bc1\u53c2\u6570dev\u7684\u552f\u4e00\u6027\u3002\u5f53\u5185\u6838\u63a5\u6536\u4e00\u4e2a\u4e2d\u65ad\u540e\uff0c\u5b83\u5c06\u4f9d\u6b21\u8c03\u7528\u5728\u8be5\u4e2d\u65ad\u7ebf\u4e0a\u6ce8\u518c\u7684\u6bcf\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u3002\u56e0\u6b64\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u5fc5\u987b\u77e5\u9053\u5b83\u662f\u5426\u8be5\u4e3a\u8fd9\u4e2a\u4e2d\u65ad\u8d1f\u8d23\uff0c\u5982\u679c\u4e0d\u662f\u5c31\u8981\u7acb\u5373\u9000\u51fa\u3002

"},{"location":"linux/kernel/interrupt/#_3","title":"\u4e2d\u65ad\u5904\u7406\u673a\u5236","text":"

\u4e2d\u65ad\u5904\u7406\u7cfb\u7edf\u5728Linux\u4e2d\u7684\u5b9e\u73b0\u4f9d\u8d56\u4e8e\u4f53\u7cfb\u7ed3\u6784\u3002\u4e0a\u56fe\u662f\u4e2d\u65ad\u4ece\u786c\u4ef6\u5230\u5185\u6838\u6267\u884c\u7684\u8fc7\u7a0b\u3002\u5f53\u4e00\u4e2a\u8bbe\u5907\u4ea7\u751f\u4e2d\u65ad\u540e\uff0c\u901a\u8fc7\u603b\u7ebf\u628a\u7535\u4fe1\u53f7\u53d1\u9001\u7ed9\u4e2d\u65ad\u63a7\u5236\u5668\u3002\u5982\u679c\u4e2d\u65ad\u7ebf\u662f\u6fc0\u6d3b\u7684\uff0c\u90a3\u4e48\u4e2d\u65ad\u63a7\u5236\u5668\u5c31\u4f1a\u628a\u4e2d\u65ad\u53d1\u9001\u7ed9\u5904\u7406\u5668\u3002\u5728\u5927\u591a\u6570\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u8fd9\u4e2a\u5de5\u4f5c\u5c31\u662f\u901a\u8fc7\u7535\u4fe1\u53f7\u7ed9\u5904\u7406\u5668\u7684\u7279\u5b9a\u7ba1\u811a\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u3002\u9664\u975e\u5904\u7406\u5668\u7981\u6b62\u4e86\u8be5\u4e2d\u65ad\uff0c\u5426\u5219\u5b83\u4f1a\u7acb\u523b\u505c\u4e0b\u5f53\u524d\u6267\u884c\u7684\u4ee3\u7801\uff0c\u5173\u95ed\u4e2d\u65ad\u7cfb\u7edf\uff0c\u7136\u540e\u8df3\u5230\u5185\u5b58\u4e2d\u9884\u5b9a\u4e49\u7684\u4f4d\u7f6e\u5f00\u59cb\u6267\u884c\u90a3\u91cc\u7684\u4ee3\u7801\u3002\u8fd9\u4e2a\u9884\u5b9a\u4e49\u7684\u4f4d\u7f6e\u7531\u5185\u6838\u8bbe\u7f6e\uff0c\u662f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002

\u5bf9\u4e8e\u6bcf\u6761\u4e2d\u65ad\u7ebf\uff0c\u5904\u7406\u5668\u90fd\u4f1a\u8df3\u5230\u5bf9\u5e94\u7684\u4e00\u4e2a\u552f\u4e00\u7684\u4f4d\u7f6e\u3002\u8fd9\u6837\uff0c\u5185\u6838\u5c31\u53ef\u4ee5\u77e5\u9053\u6240\u63a5\u6536\u4e2d\u65ad\u7684IRQ\u53f7\u4e86\u3002\u521d\u59cb\u5165\u53e3\u70b9\u5728\u6808\u4e2d\u4fdd\u5b58IRQ\u53f7\uff0c\u5e76\u5b58\u653e\u5f53\u524d\u5bc4\u5b58\u5668\u7684\u503c\uff08\u8fd9\u4e9b\u503c\u5c5e\u4e8e\u88ab\u4e2d\u65ad\u7684\u4efb\u52a1\uff09\u3002\u7136\u540e\uff0c\u7531\u5185\u6838\u8c03\u7528do_IRQ()\u51fd\u6570\uff1a

unsigned int do_IRQ(struct pt_regs *regs)\n

\u7531\u4e8e\u521d\u59cb\u5165\u53e3\u70b9\u5df2\u7ecf\u5c06IRQ\u53f7\u548c\u5bc4\u5b58\u5668\u7684\u503c\u4fdd\u5b58\u5728\u6808\u4e2d\u4e86\uff0c\u6240\u4ee5\u51fd\u6570do_IRQ()\u53ef\u4ee5\u76f4\u63a5\u63d0\u53d6\u6808\u4e2d\u7684\u53c2\u6570\u3002

do_IRQ()\u6267\u884c\u5b8c\u4e2d\u65ad\u670d\u52a1\u5668\u7a0b\u5e8f\u540e\uff0c\u4f1a\u68c0\u67e5\u662f\u5426\u9700\u8981\u91cd\u65b0\u8c03\u5ea6\uff08need_resched\u6807\u5fd7\u4f4d\uff09\uff0c\u7136\u540e\u5224\u65ad\u8fd4\u56de\u54ea\u4e2a\u7a7a\u95f4\uff1a

  • \u8fd4\u56de\u7528\u6237\u7a7a\u95f4\uff1a\u8c03\u7528schedule()\u51fd\u6570\u3002

  • \u8fd4\u56de\u5185\u6838\u7a7a\u95f4\uff1a\u5982\u679cpreempt_count\u6807\u5fd7\u4e3a0\uff0c\u8c03\u7528schedule()\u51fd\u6570\uff0c\u5426\u5219\u4e0d\u4f1a\u89e6\u53d1\u8c03\u5ea6\u3002

procfs\u662f\u4e00\u4e2a\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb/proc/interrupts\u6587\u4ef6\u8bfb\u53d6\u7cfb\u7edf\u4e2d\u4e0e\u4e2d\u65ad\u76f8\u5173\u7684\u7edf\u8ba1\u4fe1\u606f\u3002

"},{"location":"linux/kernel/interrupt/#_4","title":"\u4e2d\u65ad\u63a7\u5236","text":"

Linux\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7ec4\u63a5\u53e3\u7528\u4e8e\u64cd\u4f5c\u673a\u5668\u4e0a\u7684\u4e2d\u65ad\u72b6\u6001\u3002\u8fd9\u4e9b\u63a5\u53e3\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u80fd\u591f\u7981\u6b62\u5f53\u524d\u5904\u7406\u5668\u7684\u4e2d\u65ad\u7cfb\u7edf\uff0c\u6216\u5c4f\u853d\u6389\u6574\u4e2a\u673a\u5668\u7684\u4e00\u6761\u4e2d\u65ad\u7ebf\u7684\u80fd\u529b\u3002\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u662f\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u63a7\u5236\u4e2d\u65ad\u7cfb\u7edf\u7684\u539f\u56e0\u5f52\u6839\u5230\u5e95\u662f\u9700\u8981\u540c\u6b65\u3002\u901a\u8fc7\u7981\u6b62\u4e2d\u65ad\uff0c\u53ef\u4ee5\u786e\u4fdd\u67d0\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e0d\u4f1a\u88ab\u62a2\u5360\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7981\u6b62\u5185\u6838\u62a2\u5360\u3002\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u7f16\u5199\u9a71\u52a8\u7a0b\u5e8f\u65f6\uff0c\u5e94\u8be5\u5c3d\u91cf\u51cf\u5c11\u4e2d\u65ad\uff0c\u540c\u65f6\u4e0d\u8981\u628a\u8fd9\u79cd\u6280\u672f\u5f53\u505a\u4e92\u65a5\u673a\u5236\u6765\u4f7f\u7528\u3002

\u7981\u6b62\u548c\u6fc0\u6d3b\u672c\u5730\u4e2d\u65ad\u7684\u51fd\u6570\u4e3a\uff1a

local_irq_disable();\n\nlocal_irq_enable();\n

\u5982\u679c\u5728\u8c03\u7528local_irq_disable()\u51fd\u6570\u524d\u5df2\u7ecf\u7981\u6b62\u4e86\u4e2d\u65ad\uff0c\u90a3\u4e48\u8be5\u64cd\u4f5c\u4f1a\u5e26\u6765\u6f5c\u5728\u7684\u5371\u9669\u3002\u540c\u6837\uff0clocal_irq_enable()\u51fd\u6570\u4e5f\u5b58\u5728\u5371\u9669\uff0c\u56e0\u4e3a\u5b83\u4f1a\u65e0\u6761\u4ef6\u6fc0\u6d3b\u4e2d\u65ad\u3002\u5f53\u5185\u6838\u4ee3\u7801\u53d8\u5f97\u590d\u6742\u65f6\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u5f53\u524d\u5904\u7406\u5668\u4e2d\u65ad\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e00\u79cd\u673a\u5236\uff0c\u4ec5\u4ec5\u65f6\u628a\u4e2d\u65ad\u6062\u590d\u5230\u4ee5\u524d\u7684\u72b6\u6001\u800c\u4e0d\u662f\u7b80\u5355\u5730\u7981\u6b62\u6216\u8005\u662f\u6fc0\u6d3b\u3002

unsigned long flags;\nlocal_irq_save(flags);\nlocal_irq_restore(flags);\n

\u5728\u524d\u9762\u7684\u5185\u5bb9\u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u4e86\u7981\u6b62\u6574\u4e2a\u5904\u7406\u5668\u4e0a\u6240\u6709\u4e2d\u65ad\u7684\u51fd\u6570\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u7981\u6b62\u7279\u5b9a\u7684\u4e2d\u65ad\u7ebf\u3002\u76f8\u5173\u51fd\u6570\u5982\u4e0b\uff1a

void disable_irq(unsigned int irq);\nvoid disable_irq_nosync(unsigned int irq);\nvoid enable_irq(unsigned int irq);\nvoid synchronize_irq(unsigned int irq);\n

\u7531\u4e8e\u73b0\u4ee3\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5f80\u5f80\u5171\u4eab\u67d0\u4e2a\u4e2d\u65ad\u7ebf\uff0c\u7981\u6b62\u4e86\u67d0\u6761\u4e2d\u65ad\u7ebf\u4e5f\u5c31\u7981\u6b62\u4e86\u8fd9\u6761\u7ebf\u4e0a\u6240\u6709\u8bbe\u5907\u7684\u4e2d\u65ad\u4f20\u9012\u3002\u56e0\u6b64\u8fd9\u4e9b\u63a5\u53e3\u51fd\u6570\u4e0d\u5e94\u8be5\u88ab\u4f7f\u7528\u3002

"},{"location":"linux/kernel/interrupt/#_5","title":"\u4e0b\u534a\u90e8\u673a\u5236","text":"

\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u6709\u4e00\u4e2a\u5feb\u901f\u3001\u5f02\u6b65\u4e14\u7b80\u5355\u7684\u673a\u5236\u8d1f\u8d23\u5bf9\u786c\u4ef6\u8fc5\u901f\u505a\u51fa\u76f8\u5e94\u5e76\u5b8c\u6210\u90a3\u4e9b\u65f6\u95f4\u8981\u6c42\u5f88\u4e25\u683c\u7684\u64cd\u4f5c\u3002\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5f88\u9002\u5408\u4e8e\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002\u800c\u5bf9\u4e8e\u90a3\u4e9b\u65f6\u95f4\u8981\u6c42\u76f8\u5bf9\u5bbd\u677e\u7684\u4efb\u52a1\uff0c\u5e94\u5f53\u653e\u5230\u4e0b\u534a\u90e8\u53bb\u5904\u7406\u3002

\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u8ba9\u6240\u6709\u7684\u5de5\u4f5c\u90fd\u4ea4\u7ed9\u4e0b\u534a\u90e8\u53bb\u5904\u7406\u3002\u4f46\u662f\uff0c\u603b\u6709\u4e00\u4e9b\u5de5\u4f5c\u9700\u8981\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u6765\u5b8c\u6210\uff0c\u6bd4\u5982\u5bf9\u4e2d\u65ad\u7684\u5230\u8fbe\u8fdb\u884c\u786e\u8ba4\uff0c\u4ece\u786c\u4ef6\u62f7\u8d1d\u6570\u636e\u7b49\u3002\u5982\u4f55\u533a\u5206\u4e0a\u534a\u90e8\u548c\u4e0b\u534a\u90e8\uff0c\u5b8c\u5168\u7531\u9a71\u52a8\u7a0b\u5e8f\u5f00\u53d1\u8005\u81ea\u884c\u5224\u65ad\u3002\u5982\u679c\u4efb\u52a1\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42\uff0c\u5219\u9002\u5408\u653e\u5230\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u3002

  • \u5982\u679c\u8fd9\u4e2a\u4efb\u52a1\u5bf9\u65f6\u95f4\u975e\u5e38\u654f\u611f\u3002
  • \u5982\u679c\u8fd9\u4e2a\u4efb\u52a1\u548c\u786c\u4ef6\u76f8\u5173\u3002
  • \u5982\u679c\u8fd9\u4e2a\u4efb\u52a1\u4e0d\u80fd\u88ab\u5176\u4ed6\u4e2d\u65ad\u6253\u65ad\u3002

\u5176\u4ed6\u6240\u6709\u4efb\u52a1\uff0c\u8003\u8651\u653e\u5728\u4e0b\u534a\u90e8\u6267\u884c\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4ec5\u6267\u884c\u6700\u5173\u952e\u7684\u7d27\u6025\u4efb\u52a1\uff0c\u968f\u540e\u4fbf\u4f1a\u89e6\u53d1\u4e0b\u534a\u90e8\u673a\u5236\u4ee5\u5904\u7406\u540e\u7eed\u7684\u975e\u7d27\u6025\u4efb\u52a1\u3002

"},{"location":"linux/kernel/interrupt/#_6","title":"\u8f6f\u4e2d\u65ad","text":"

\u8f6f\u4e2d\u65ad\u7684\u82f1\u6587\u540d\u662fsoftirq\uff0c\u4e0e\u8f6f\u4ef6\u4e2d\u65ad\uff08software irq\uff09\u6ca1\u6709\u5173\u7cfb\u3002

\u8f6f\u4e2d\u65ad\u4f7f\u7528\u6bd4\u8f83\u5c11\uff0ctasklet\u662f\u4e0b\u534a\u90e8\u66f4\u5e38\u7528\u8fc7\u7684\u4e00\u79cd\u5f62\u5f0f\u3002\u4f46\u662f\uff0c\u7531\u4e8etasklet\u662f\u901a\u8fc7\u8f6f\u4e2d\u65ad\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u4e5f\u6709\u5fc5\u8981\u5148\u7814\u7a76\u8f6f\u4e2d\u65ad\u3002

"},{"location":"linux/kernel/interrupt/#_7","title":"\u8f6f\u4e2d\u65ad\u7684\u5b9e\u73b0","text":"

\u8f6f\u4e2d\u65ad\u5728\u7f16\u8bd1\u671f\u95f4\u9759\u6001\u5206\u914d\uff0c\u7531softirq_action\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct softirq_action{\n    void (*action)(struct softirq_action*);\n};\n

\u5728<kernel/softirq.c>\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u670932\u4e2a\u8be5\u7ed3\u6784\u4f53\u7684\u6570\u7ec4\uff1a

static struct softirq_action softirq_vec[NR_SOFTIRQS];\n

\u6bcf\u4e2a\u88ab\u6ce8\u518c\u7684\u8f6f\u4e2d\u65ad\u90fd\u5360\u636e\u8be5\u6570\u7ec4\u7684\u4e00\u9879\uff0c\u56e0\u6b64\u6700\u5927\u670932\u4e2a\u8f6f\u4e2d\u65ad\u3002

\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8faction\u7684\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a

void softirq_handler(struct softirq_action *)\n

\u5f53\u5185\u6838\u8fd0\u884c\u4e00\u4e2a\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u65f6\uff0c\u5c31\u4f1a\u6267\u884c\u8fd9\u4e2aaction\u51fd\u6570\uff0c\u5176\u552f\u4e00\u7684\u53c2\u6570\u662f\u6307\u5411\u76f8\u5e94softirq_action\u7ed3\u6784\u4f53\u7684\u6307\u9488\u3002\u4f8b\u5982\uff0c\u5982\u679cmy_softirq\u6307\u5411softirq_vec\u6570\u7ec4\u7684\u67d0\u9879\uff0c\u90a3\u4e48\u5185\u6838\u5c31\u4f1a\u7528\u5982\u4e0b\u7684\u65b9\u5f0f\u8c03\u7528\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u51fd\u6570\uff1a

my_softirq->action(my_softirq);\n

\u4e00\u4e2a\u6ce8\u518c\u7684\u8f6f\u4e2d\u65ad\u5fc5\u987b\u5728\u88ab\u6807\u8bb0\u540e\u624d\u4f1a\u6267\u884c\uff0c\u8fd9\u88ab\u79f0\u4e3a\u89e6\u53d1\u8f6f\u4e2d\u65ad\uff08raising the softirq\uff09\u3002\u901a\u5e38\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4f1a\u5728\u8fd4\u56de\u524d\u6807\u8bb0\u5b83\u7684\u8f6f\u4e2d\u65ad\uff0c\u4f7f\u5176\u5728\u7a0d\u540e\u88ab\u6267\u884c\u3002do_softirq()\u51fd\u6570\u4f1a\u68c0\u67e5softirq_vec\u6570\u7ec4\u4e2d\u6240\u6709\u5f85\u5904\u7406\u7684\u8f6f\u4e2d\u65ad\uff0c\u8c03\u7528\u5b83\u4eec\u7684\u5904\u7406\u7a0b\u5e8f\u3002

"},{"location":"linux/kernel/interrupt/#_8","title":"\u4f7f\u7528\u8f6f\u4e2d\u65ad","text":"

\u8f6f\u4e2d\u65ad\u4fdd\u7559\u7ed9\u7cfb\u7edf\u4e2d\u5bf9\u65f6\u95f4\u8981\u6c42\u6700\u4e25\u683c\u4ee5\u53ca\u6700\u91cd\u8981\u7684\u4e0b\u534a\u90e8\u4f7f\u7528\u3002\u5185\u6838\u5728<linux/interrupt.h>\u4e2d\u5df2\u7ecf\u5b9a\u4e49\u4e86\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b\u6765\u9759\u6001\u5730\u58f0\u660e\u8f6f\u4e2d\u65ad\uff0c\u7d22\u5f15\u53f7\u8868\u793a\u5176\u4f18\u5148\u7ea7\u3002\u8981\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u8f6f\u4e2d\u65ad\u5fc5\u987b\u5728\u6b64\u679a\u4e3e\u7c7b\u578b\u4e2d\u52a0\u5165\u65b0\u7684\u9879\uff0c\u800c\u4e14\u4f60\u5fc5\u987b\u6839\u636e\u5b83\u7684\u4f18\u5148\u7ea7\u6765\u51b3\u5b9a\u52a0\u5165\u7684\u4f4d\u7f6e\u3002\u4e0b\u8868\u5217\u51fa\u4e86\u5df2\u6709\u7684tasklet\u7c7b\u578b\u3002

tasklet \u4f18\u5148\u7ea7 \u8f6f\u4e2d\u65ad\u63cf\u8ff0 HI_SOFTIRQ 0 \u4f18\u5148\u7ea7\u9ad8\u7684tasklets TIMER_SOFTIRQ 1 \u5b9a\u65f6\u5668\u7684\u4e0b\u534a\u90e8 NET_TX_SOFTIRQ 2 \u53d1\u9001\u7f51\u7edc\u6570\u636e\u5305 NET_RX_SOFTIRQ 3 \u63a5\u6536\u7f51\u7edc\u6570\u636e\u5305 BLOCK_SOFTIRQ 4 BLOCK\u88c5\u7f6e TASKLET_SOFTIRQ 5 \u6b63\u5e38\u4f18\u5148\u6743\u7684tasklets SCHED_SOFTIRQ 6 \u8c03\u5ea6\u7a0b\u5e8f HRTIMER_SOFTIRQ 7 \u9ad8\u5206\u8fa8\u7387\u5b9a\u65f6\u5668 RCU_SOFTIRQ 8 RCU\u9501\u5b9a

\u63a5\u7740\uff0c\u5728\u8fd0\u884c\u65f6\u901a\u8fc7\u8c03\u7528open_softirq()\u51fd\u6570\u6ce8\u518c\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\uff0c\u8be5\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u8f6f\u4e2d\u65ad\u7d22\u5f15\u53f7\u548c\u5904\u7406\u51fd\u6570\uff1a

open_softirq(NET_TX_SOFTIRQ, net_tx_action)\n

\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u7684\u65f6\u5019\uff0c\u5141\u8bb8\u54cd\u5e94\u4e2d\u65ad\uff0c\u4f46\u5b83\u81ea\u5df1\u4e0d\u80fd\u4f11\u7720\u3002\u5728\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5f53\u524d\u5904\u7406\u5668\u4e0a\u7684\u8f6f\u4e2d\u65ad\u88ab\u7981\u6b62\u3002\u4f46\u5176\u4ed6\u5904\u7406\u5668\u4ecd\u7136\u53ef\u4ee5\u6267\u884c\u522b\u7684\u8f6f\u4e2d\u65ad\u3002\u8fd9\u610f\u5473\u7740\u5171\u4eab\u6570\u636e\u9700\u8981\u9501\u7684\u4fdd\u62a4\u3002\u56e0\u6b64\u5927\u90e8\u5206\u8f6f\u4e2d\u65ad\u5904\u7406\uff0c\u90fd\u901a\u8fc7\u91c7\u53d6\u5355\u5904\u7406\u5668\u6570\u636e\u6765\u907f\u514d\u52a0\u9501\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u51fa\u8272\u7684\u6027\u80fd\u3002

\u5728\u6ce8\u518c\u5b8c\u4e4b\u540e\uff0c\u65b0\u7684\u8f6f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5c31\u80fd\u591f\u8fd0\u884c\u3002raise_softirq()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8f6f\u4e2d\u65ad\u8bbe\u7f6e\u4e3a\u6302\u8d77\u72b6\u6001\uff0c\u8ba9\u5b83\u5728\u4e0b\u6b21\u8c03\u7528do_softirq()\u51fd\u6570\u65f6\u6295\u5165\u8fd0\u884c\u3002

\u5728\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\u89e6\u53d1\u8f6f\u4e2d\u65ad\u662f\u6700\u5e38\u89c1\u7684\u5f62\u5f0f\u3002\u5185\u6838\u5728\u6267\u884c\u5b8c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e4b\u540e\uff0c\u9a6c\u4e0a\u5c31\u4f1a\u8c03\u7528do_softirq()\u51fd\u6570\uff0c\u4e8e\u662f\u8f6f\u4e2d\u65ad\u5f00\u59cb\u6267\u884c\u7ed9\u5b9a\u7684\u4efb\u52a1\u3002

"},{"location":"linux/kernel/interrupt/#tasklet","title":"tasklet","text":"

tasklet\u662f\u5229\u7528\u8f6f\u4e2d\u65ad\u5b9e\u73b0\u7684\u4e00\u79cd\u4e0b\u534a\u90e8\u673a\u5236\uff0c\u4f46\u662f\u5b83\u7684\u63a5\u53e3\u66f4\u7b80\u5355\uff0c\u9501\u4fdd\u62a4\u8981\u6c42\u8f83\u4f4e\u3002\u5927\u591a\u6570\u60c5\u51b5\u90fd\u53ef\u4ee5\u4f7f\u7528tasklet\u6765\u5b8c\u6210\u4f60\u9700\u8981\u7684\u5de5\u4f5c\u3002

tasklet\u6709\u4e00\u4e9b\u6bd4\u8f83\u6709\u610f\u601d\u7684\u7279\u6027\uff1a

  • \u4e00\u4e2atasklet\u53ef\u5728\u7a0d\u540e\u88ab\u7981\u6b62\u6216\u8005\u91cd\u65b0\u542f\u7528\uff1b\u53ea\u6709\u542f\u7528\u7684\u6b21\u6570\u548c\u7981\u6b62\u7684\u6b21\u6570\u76f8\u540c\u65f6\uff0ctasklet\u624d\u4f1a\u88ab\u6267\u884c\u3002

  • tasklet\u53ef\u4ee5\u6ce8\u518c\u81ea\u5df1\u672c\u8eab\u3002

  • tasklet\u53ef\u88ab\u8c03\u5ea6\u5728\u6b63\u5e38\u4f18\u5148\u7ea7\u6216\u8005\u66f4\u9ad8\u4f18\u5148\u7ea7\u6267\u884c\u3002

  • \u5f53\u7cfb\u7edf\u8d1f\u8f7d\u4f4e\u65f6\uff0ctasklet\u4f1a\u88ab\u7acb\u523b\u6267\u884c\uff0c\u4f46\u518d\u665a\u4e0d\u4f1a\u665a\u4e8e\u4e0b\u4e00\u4e2a\u5b9a\u65f6\u5668tick\u3002

  • \u4e00\u4e2atasklet\u53ef\u4ee5\u4e0e\u5176\u4ed6tasklet\u5e76\u53d1\uff0c\u4f46\u662f\u540c\u4e00\u4e2atasklet\u6c38\u8fdc\u4e0d\u4f1a\u5728\u591a\u4e2aCPU\u4e0a\u540c\u65f6\u8fd0\u884c\uff0c\u4e14\u53ea\u4f1a\u5728\u8c03\u5ea6\u81ea\u5df1\u7684\u540c\u4e00CPU\u4e0a\u8fd0\u884c\u3002

"},{"location":"linux/kernel/interrupt/#tasklet_1","title":"tasklet\u7684\u5b9e\u73b0","text":"

tasklet\u7531\u4e24\u7c7b\u8f6f\u4e2d\u65ad\u4ee3\u8868\uff1aHI_SOFTIRQ\u548cTASKLET_SOFTIRQ\u3002\u524d\u8005\u4f18\u5148\u7ea7\u6bd4\u540e\u8005\u9ad8\u3002

tasklet\u7ed3\u6784\u4f53\u5982\u4e0b\uff1a

struct tasklet_struct {\n    struct tasklet_struct *next;    //\u94fe\u8868\u4e2d\u4e0b\u4e00\u4e2atasklet\n    unsigned long state;            //tasklet\u7684\u72b6\u6001\n    atomic_t count;                 //\u5f15\u7528\u8ba1\u6570\u5668\n    void (*func)(unsigned long);    //\u5904\u7406\u51fd\u6570\n    unsigned long data;             //\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\n};\n

state\u6210\u5458\u53ea\u80fd\u57280\u3001TASKLET_STATE_SCHED\u548cTASKLET_STATE_RUN\u4e4b\u95f4\u53d6\u503cTASKLET_STATE_SCHED\u8868\u660e tasklet\u5df2\u88ab\u8c03\u5ea6\uff0c\u6b63\u51c6\u5907\u6295\u5165\u8fd0\u884c\uff0cTASKLET_STATE_RUN\u8868\u660e\u8be5tasklet\u6b63\u5728\u8fd0\u884c\u3002TASKLET_STATE_RUN\u53ea\u6709\u5728\u591a\u5904\u7406\u5668\u7684\u7cfb\u7edf\u4e0a\u624d\u4f1a\u4f5c\u4e3a\u4e00\u79cd\u4f18\u5316\u6765\u4f7f\u7528\uff0c\u5355\u5904\u7406\u5668\u7cfb\u7edf\u4efb\u4f55\u65f6\u5019\u90fd\u6e05\u695a\u5355\u4e2atasklet\u662f\u4e0d\u662f\u6b63\u5728\u8fd0\u884c(\u5b83\u8981\u4e48\u5c31\u662f\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\uff0c\u8981\u4e48\u4e0d\u662f)\u3002

count\u6210\u5458\u662ftasklet\u7684\u5f15\u7528\u8ba1\u6570\u5668\u3002\u5982\u679c\u5b83\u4e0d\u4e3a0\uff0c\u5219tasklet\u88ab\u7981\u6b62\uff1b\u53ea\u6709\u5f53\u5b83\u4e3a0\u65f6\uff0ctasklet\u624d\u88ab\u6fc0\u6d3b\u3002

\u5df2\u8c03\u5ea6\uff08\u6216\u8005\u53eb\u5df2\u6fc0\u6d3b\uff09\u7684tasklet\u5b58\u653e\u5728tasklet_vec\uff08\u666e\u901atasklet\uff09\u548ctasklet_hi_vec\uff08\u9ad8\u4f18\u5148\u7ea7\u7684tasklet\uff09\u6570\u7ec4\u4e2d\u3002\u8fd9\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531tasklet_struct\u7ed3\u6784\u4f53\u6784\u6210\u7684\u94fe\u8868\uff0c\u94fe\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u4e0d\u540c\u7684tasklet\u3002

tasklet\u7531tasklet_schedule()\u548ctasklet_hi_schedule()\u51fd\u6570\u8fdb\u884c\u8c03\u5ea6\uff0c\u5b83\u4eec\u63a5\u53d7\u4e00\u4e2a\u6307\u5411tasklet_struct\u7ed3\u6784\u7684\u6307\u9488\u4f5c\u4e3a\u53c2\u6570\u3002

"},{"location":"linux/kernel/interrupt/#tasklet_2","title":"\u4f7f\u7528tasklet","text":"

\u5982\u679c\u662f\u9759\u6001\u521b\u5efa\u4e00\u4e2atasklet\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u4e2a\u5b8f\u4e2d\u7684\u4e00\u4e2a\uff1a

DECLARE_TASKLET(name, func, data)\nDECLARE_TASKLET_DISABLED(name, func, data)\n

\u8fd9\u4e24\u4e2a\u5b8f\u90fd\u80fd\u9759\u6001\u521b\u5efa\u4e00\u4e2atasklet_struct\u7ed3\u6784\uff0c\u533a\u522b\u5728\u4e8e\u5f15\u7528\u8ba1\u6570count\u7684\u521d\u59cb\u503c\u4e0d\u540c\u3002\u7b2c\u4e00\u4e2a\u8bbe\u4e3a0\uff0c\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\uff1b\u7b2c\u4e8c\u4e2a\u8bbe\u4e3a1\uff0c\u5904\u4e8e\u7981\u6b62\u72b6\u6001\u3002

\u5982\u679c\u662f\u52a8\u6001\u521b\u5efa\uff1a

tasklet_init(t, tasklet_handler, dev);\n

\u7f16\u5199\u81ea\u5df1\u7684tasklet\u5904\u7406\u7a0b\u5e8f\uff1a

void tasklet_handler(unsigned long data)\n

tasklet\u4e0d\u80fd\u7761\u7720\uff0c\u4e24\u4e2a\u76f8\u540c\u7684tasklet\u4e0d\u4f1a\u540c\u65f6\u6267\u884c\uff0c\u4f46\u5982\u679c\u4e0e\u5176\u4ed6tasklet\u6216\u8005\u662f\u8f6f\u4e2d\u65ad\u5171\u4eab\u4e86\u6570\u636e\uff0c\u90a3\u4e48\u5fc5\u987b\u8fdb\u884c\u9501\u4fdd\u62a4\u3002

\u901a\u8fc7\u8c03\u7528tasklet_schedule()\u51fd\u6570\u5e76\u4f20\u9012\u7ed9\u5b83\u76f8\u5e94\u7684tasklet_struct\u6307\u9488\uff0c\u8be5tasklet\u5c31\u4f1a\u88ab\u8c03\u5ea6\u4ee5\u4fbf\u6267\u884c\uff1a

tasklet_schedule(&my_tasklet);\n

\u4e00\u4e2atasklet\u603b\u662f\u5728\u8c03\u5ea6\u5b83\u7684\u5904\u7406\u5668\u4e0a\u8fd0\u884c\u3002\u8981\u7981\u6b62/\u4f7f\u80fdtasklet\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\uff1a

tasklet_disable(&my_tasklet);   \ntasklet_disable_nosync(&my_tasklet);         \n

\u7b2c\u4e00\u4e2a\u51fd\u6570\u7981\u6b62\u6307\u5b9a\u7684tasklet\uff0c\u5982\u679c\u6b63\u5728\u8fd0\u884c\uff0c\u5219\u4f1a\u7b49\u5f85\u671f\u6267\u884c\u5b8c\u6bd5\u3002\u7b2c\u4e8c\u4e2a\u51fd\u6570\u4e0d\u4f1a\u7b49\u5f85\u4efb\u4f55\u6b63\u5728\u8fd0\u884c\u7684tasklet\u3002

\u4f7f\u80fdtasklet\uff1a

tasklet_enable(&my_tasklet);\n

\u79fb\u9664tasklet\uff0c\u901a\u5e38\u5728\u8bbe\u5907\u5173\u95ed\u6216\u8005\u6a21\u5757\u9000\u51fa\u65f6\u8c03\u7528\u8be5\u51fd\u6570\uff1a

tasklet_kill(&my_tasklet);\n
"},{"location":"linux/kernel/interrupt/#ksoftirqd","title":"ksoftirqd","text":"

\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u6709\u4e00\u7ec4\u8f85\u52a9\u5904\u7406\u8f6f\u4e2d\u65ad\uff08\u548ctasklet\uff09\u7684\u5185\u6838\u7ebf\u7a0b\u3002\u5f53\u5185\u6838\u4e2d\u51fa\u73b0\u5927\u91cf\u8f6f\u4e2d\u65ad\u7684\u65f6\u5019\uff0c\u5185\u6838\u7ebf\u7a0b\u5c31\u4f1a\u9009\u62e9\u5408\u9002\u7684\u65f6\u673a\u6765\u5904\u7406\u8f6f\u4e2d\u65ad\u3002

\u5728\u5927\u6d41\u91cf\u7684\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0c\u8f6f\u4e2d\u65ad\u7684\u89e6\u53d1\u9891\u7387\u53ef\u80fd\u5f88\u9ad8\uff0c\u751a\u81f3\u8fd8\u4f1a\u81ea\u884c\u91cd\u590d\u89e6\u53d1\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u65e0\u6cd5\u83b7\u5f97\u8db3\u591f\u7684\u5904\u7406\u5668\u65f6\u95f4\u3002\u5982\u679c\u8f6f\u4e2d\u65ad\u548c\u91cd\u590d\u89e6\u53d1\u7684\u8f6f\u4e2d\u65ad\u90fd\u88ab\u7acb\u5373\u5904\u7406\uff0c\u90a3\u4e48\u5f53\u8d1f\u8f7d\u5f88\u9ad8\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u5c31\u4f1a\u51fa\u73b0\u660e\u663e\u7684\u5361\u987f\u73b0\u8c61\u3002\u5982\u679c\u9009\u62e9\u4e0d\u5904\u7406\u91cd\u65b0\u89e6\u53d1\u7684\u8f6f\u4e2d\u65ad\uff0c\u53c8\u4f1a\u6d6a\u8d39\u95f2\u7f6e\u7684\u7cfb\u7edf\u8d44\u6e90\uff0c\u5bfc\u81f4\u8f6f\u4e2d\u65ad\u51fa\u73b0\u9965\u997f\u73b0\u8c61\u3002

\u5185\u6838\u4e2d\u7684\u65b9\u6848\u65f6\u4e0d\u4f1a\u7acb\u5373\u5904\u7406\u91cd\u590d\u89e6\u53d1\u7684\u8f6f\u4e2d\u65ad\u3002\u5f53\u5927\u91cf\u8f6f\u4e2d\u65ad\u51fa\u73b0\u7684\u65f6\u5019\uff0c\u5185\u6838\u4f1a\u5524\u9192\u4e00\u7ec4\u5185\u6838\u7ebf\u7a0b\u6765\u5904\u7406\u8fd9\u4e9b\u8d1f\u8f7d\u3002\u8fd9\u4e9b\u7ebf\u7a0b\u5728\u6700\u4f4e\u4f18\u5148\u7ea7\uff08nice=19\uff09\u8fd0\u884c\uff0c\u907f\u514d\u4e0e\u5176\u4ed6\u4efb\u52a1\u62a2\u5360\u8d44\u6e90\u3002

\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u7ebf\u7a0b\uff0c\u540d\u5b57\u4e3aksoftirqd/n\uff0cn\u4e3a\u5904\u7406\u5668\u7f16\u53f7\u3002\u53ea\u8981\u6709\u5f85\u5904\u7406\u7684\u8f6f\u4e2d\u65ad\uff0cksoftirqd\u5c31\u4f1a\u8c03\u7528do_softirq()\u51fd\u6570\u6765\u5904\u7406\u5b83\u4eec\u3002

"},{"location":"linux/kernel/interrupt/#_9","title":"\u5de5\u4f5c\u961f\u5217","text":"

\u5de5\u4f5c\u961f\u5217\u662f\u4e00\u79cd\u5ef6\u540e\u6267\u884c\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u5c06\u540e\u7eed\u7684\u5de5\u4f5c\u4ea4\u7ed9\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u6267\u884c\u2014\u2014\u8fd9\u4e2a\u4e0b\u534a\u90e8\u5206\u603b\u662f\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u3002\u8fd9\u6837\uff0c\u901a\u8fc7\u5de5\u4f5c\u961f\u5217\u5b9e\u73b0\u7684\u4ee3\u7801\u5c31\u80fd\u4eab\u53d7\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u7684\u6240\u6709\u4f18\u52bf\uff0c\u6bd4\u5982\u53ef\u4ee5\u91cd\u65b0\u8c03\u5ea6\u751a\u81f3\u662f\u7761\u7720\u3002

\u5982\u679c\u63a8\u540e\u6267\u884c\u7684\u4efb\u52a1\u9700\u8981\u7761\u7720\uff0c\u90a3\u4e48\u5c31\u9009\u62e9\u5de5\u4f5c\u961f\u5217\u3002\u5426\u5219\u5c31\u9009\u62e9\u8f6f\u4e2d\u65ad\u6216tasklet\u3002\u5de5\u4f5c\u961f\u5217\u5728\u4f60\u9700\u8981\u83b7\u5f97\u5927\u91cf\u5185\u5b58\u65f6\uff0c\u9700\u8981\u83b7\u53d6\u4fe1\u53f7\u91cf\u65f6\uff0c\u9700\u8981\u6267\u884c\u963b\u585e\u5f0f\u7684I/O\u64cd\u4f5c\u65f6\uff0c\u5b83\u90fd\u4f1a\u975e\u5e38\u6709\u7528\u3002

"},{"location":"linux/kernel/interrupt/#_10","title":"\u5de5\u4f5c\u961f\u5217\u7684\u5b9e\u73b0","text":"

\u5de5\u4f5c\u961f\u5217\u5b50\u7cfb\u7edf\u63d0\u4f9b\u4e86\u7f3a\u7701\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\uff08worker thread\uff09\u6765\u5904\u7406\u9700\u8981\u63a8\u540e\u7684\u5de5\u4f5c\u3002\u7f3a\u7701\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u53eb\u505aevents/n\uff0cn\u4e3a\u5904\u7406\u5668\u7684\u7f16\u53f7\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\uff0c\u4e0d\u8fc7\u4e00\u822c\u4f7f\u7528\u7f3a\u7701\u7ebf\u7a0b\u5373\u53ef\u3002\u7136\u800c\uff0c\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u9700\u8981\u7279\u522b\u7684\u5904\u7406\uff0c\u6216\u8005\u5b83\u5bf9\u6027\u80fd\u6709\u975e\u5e38\u4e25\u683c\u7684\u8981\u6c42\uff0c\u90a3\u4e48\u521b\u5efa\u4e00\u4e2a\u4e13\u7528\u7684\u5185\u6838\u7ebf\u7a0b\u53ef\u80fd\u5c31\u662f\u5fc5\u8981\u7684\u3002\u4e13\u7528\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u548c\u4f18\u5316\u4efb\u52a1\u7684\u6267\u884c\uff0c\u4f46\u4e5f\u9700\u8981\u66f4\u591a\u7684\u8d44\u6e90\u548c\u7ba1\u7406\u5f00\u9500\u3002

\u5de5\u4f5c\u8005\u7ebf\u7a0b\u7528workqueue_struct\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct workqueue_struct {\n    struct cpu_workqueue_struct cpu_wq[NR_CPUS]\uff1b\n    struct list_head list;\n    const char *name;\n    int singlethread;\n    int freezeable;\n    int rt;\n};\n

\u8be5\u7ed3\u6784\u4f53\u5185\u6709\u4e00\u4e2acpu_workqueue_struct\u7ed3\u6784\u7ec4\u6210\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\u5bf9\u5e94\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e2a\u5904\u7406\u5668\u3002\u4e5f\u5c31\u662f\u8bf4\u7cfb\u7edf\u4e2d\u6bcf\u4e2a\u5904\u7406\u5668\u5bf9\u5e94\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u3002

\u5de5\u4f5c\u7531work_struct\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct work_struct {\n    atomic_long_t data;\n    struct list_head entry;\n    work_func_t func;\n};\n

\u8fd9\u4e9b\u7ed3\u6784\u4f53\u88ab\u8fde\u63a5\u6210\u94fe\u8868\uff0c\u6bcf\u4e2a\u5904\u7406\u5668\u4e0a\u7684\u6bcf\u79cd\u7c7b\u578b\u7684\u961f\u5217\u90fd\u5bf9\u5e94\u8fd9\u6837\u4e00\u4e2a\u94fe\u8868\u3002\u5f53\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u88ab\u5524\u9192\u65f6\uff0c\u5b83\u4f1a\u6267\u884c\u94fe\u8868\u4e0a\u7684\u6240\u6709\u5de5\u4f5c\uff0c\u5f53\u6ca1\u6709\u5269\u4f59\u7684\u64cd\u4f5c\u65f6\uff0c\u5b83\u5c31\u4f1a\u7ee7\u7eed\u4f11\u7720\u3002

"},{"location":"linux/kernel/interrupt/#_11","title":"\u4f7f\u7528\u5de5\u4f5c\u961f\u5217","text":"

\u9759\u6001\u521b\u5efa\u4e00\u4e2awork_struct\u7ed3\u6784\u4f53\uff1a

DECLARE_WORK(name, void(*func)(void *), void *data);\n

\u5982\u679c\u662f\u52a8\u6001\u521b\u5efa\uff1a

INIT_WORK(struct work_struct *work, void (*func)(void *), void *data);\n

\u5de5\u4f5c\u961f\u5217\u5904\u7406\u51fd\u6570\u7684\u539f\u578b\u662f\uff1a

void work_handler(void *data);\n

\u8fd9\u4e2a\u51fd\u6570\u7531\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u6267\u884c\uff0c\u56e0\u6b64\u5904\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5141\u8bb8\u76f8\u5e94\u4e2d\u65ad\uff0c\u5e76\u4e14\u4e0d\u6301\u6709\u4efb\u4f55\u9501\u3002\u5c3d\u7ba1\u5904\u7406\u51fd\u6570\u4f4d\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\uff0c\u4f46\u662f\u5b83\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7528\u6237\u7a7a\u95f4\uff0c\u56e0\u4e3a\u5185\u6838\u7ebf\u7a0b\u5728\u7528\u6237\u7a7a\u95f4\u6ca1\u6709\u76f8\u5173\u7684\u5185\u5b58\u6620\u5c04\u3002

\u5c06\u5de5\u4f5c\u63d0\u4ea4\u5230\u5de5\u4f5c\u961f\u5217\uff1a

int queue_work(struct workqueue_struct *queue, struct work_struct *work);\nint queue_delayed_work(struct workqueue_struct *queue, struct work_struct *work, unsigned long delay);\n

\u5bf9\u5de5\u4f5c\u8fdb\u884c\u8c03\u5ea6\uff0c\u628a\u7ed9\u5b9a\u5de5\u4f5c\u7684\u5904\u7406\u51fd\u6570\u63d0\u4ea4\u7ed9\u7f3a\u7701\u7684events\u5de5\u4f5c\u7ebf\u7a0b\uff1a

schedule_work(&work);\n

\u4e00\u65e6\u5176\u6240\u5728\u5904\u7406\u5668\u4e0a\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u88ab\u5524\u9192\uff0c\u5c31\u4f1a\u7acb\u523b\u6267\u884c\u3002\u5982\u679c\u9700\u8981\u5de5\u4f5c\u5ef6\u8fdf\u6267\u884c\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51fd\u6570\uff1a

schedule_delayed_work(&work, delay);\n

&work\u6307\u5411\u7684work_struct\u5219\u4f1a\u7b49\u5f85delay\u4e2a\u65f6\u949f\u8282\u62cd\u624d\u4f1a\u6267\u884c\u3002

\u5982\u679c\u7f3a\u7701\u7684\u961f\u5217\u4e0d\u80fd\u6ee1\u8db3\u4f60\u7684\u8981\u6c42\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u961f\u5217\u548c\u4e0e\u4e4b\u76f8\u5e94\u7684\u5de5\u4f5c\u8005\u7ebf\u7a0b\u3002\u8fd9\u4e48\u505a\u4f1a\u5728\u6bcf\u4e2a\u5904\u7406\u5668\u4e0a\u90fd\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u8005\u7ebf\u7a0b\u3002\u9664\u975e\u4f60\u786e\u5b9a\u5fc5\u987b\u8981\u9760\u81ea\u5df1\u7684\u7ebf\u7a0b\u624d\u80fd\u63d0\u9ad8\u6027\u80fd\uff0c\u5426\u5219\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a

struct workqueue_struct *create_workqueue(const char *name);\n

\u5728\u7ed3\u675f\u5bf9\u5de5\u4f5c\u961f\u5217\u7684\u4f7f\u7528\u540e\uff0c\u53ef\u4ee5\u8c03\u7528\u4ee5\u4e0b\u51fd\u6570\u6765\u91ca\u653e\u8d44\u6e90\uff1a

void destroy_workqueue(struct workqueue_struct *queue);\n
"},{"location":"linux/kernel/interrupt/#_12","title":"\u4e0b\u534a\u90e8\u7684\u540c\u6b65","text":"

\u5728\u4f7f\u7528\u4e0b\u534a\u90e8\u673a\u5236\u65f6\uff0c\u5373\u4fbf\u662f\u4e00\u4e2a\u5355\u5904\u7406\u5668\uff0c\u907f\u514d\u5171\u4eab\u6570\u636e\u88ab\u540c\u65f6\u8bbf\u95ee\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4f7f\u7528tasklet\u7684\u597d\u5904\u662f\uff1a\u4e24\u4e2a\u76f8\u540c\u7c7b\u578b\u7684tasklet\u4e0d\u5141\u8bb8\u540c\u65f6\u6267\u884c\uff0c\u5373\u4fbf\u662f\u4e0d\u540c\u7684\u5904\u7406\u5668\u4e5f\u4e0d\u884c\u3002tasklet\u4e4b\u95f4\u7684\u540c\u6b65\uff08\u5373\u4e0d\u540c\u7c7b\u578b\u7684tasklet\u5171\u4eab\u540c\u4e00\u6570\u636e\uff09\uff0c\u6b63\u786e\u4f7f\u7528\u9501\u673a\u5236\u5373\u53ef\u3002

\u5982\u679c\u662f\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u548c\u4e0b\u534a\u90e8\u5171\u4eab\u6570\u636e\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u7981\u6b62\u4e0b\u534a\u90e8\u7684\u5904\u7406\u5e76\u83b7\u5f97\u9501\u7684\u4f7f\u7528\u6743\uff0c\u4ee5\u9632\u6b62\u6b7b\u9501\u7684\u51fa\u73b0\u3002

\u5982\u679c\u662f\u4e2d\u65ad\u4e0a\u4e0b\u6587\u548c\u4e0b\u534a\u90e8\u5171\u4eab\u6570\u636e\uff0c\u5728\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u7981\u6b62\u4e2d\u65ad\u5e76\u83b7\u5f97\u9501\u7684\u4f7f\u7528\u6743\uff0c\u4e5f\u662f\u4e3a\u4e86\u9632\u6b62\u6b7b\u9501\u7684\u51fa\u73b0\u3002

\u4efb\u4f55\u5728\u5de5\u4f5c\u961f\u5217\u4e2d\u88ab\u5171\u4eab\u7684\u6570\u636e\u4e5f\u9700\u8981\u4f7f\u7528\u9501\u673a\u5236\u3002

\u4e00\u822c\u5355\u7eaf\u7981\u6b62\u4e0b\u534a\u90e8\u662f\u4e0d\u591f\u7684\uff0c\u66f4\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u5148\u83b7\u5f97\u4e00\u4e2a\u9501\u518d\u7981\u6b62\u4e0b\u534a\u90e8\u7684\u5904\u7406\u3002\u5982\u679c\u9700\u8981\u7981\u6b62\u6240\u6709\u4e0b\u534a\u90e8\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u8c03\u7528local_bh_disable()\u51fd\u6570\u3002\u5141\u8bb8\u4e0b\u534a\u90e8\u5904\u7406\uff0c\u8c03\u7528local_bh_enable()\u51fd\u6570\u3002\u51fd\u6570\u901a\u8fc7preempt_count\uff08\u5185\u6838\u62a2\u5360\u4e5f\u662f\u8fd9\u4e2a\u8ba1\u6570\u5668\uff09\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u7ef4\u62a4\u4e00\u4e2a\u8ba1\u6570\u5668\u3002\u5f53\u8ba1\u6570\u5668\u4e3a0\u65f6\uff0c\u624d\u53ef\u4ee5\u5904\u7406\u4e0b\u534a\u90e8\u3002

"},{"location":"linux/kernel/mm/","title":"\u7269\u7406\u5185\u5b58\u7ba1\u7406","text":"

\u8fd9\u90e8\u5206\u5185\u5bb9\u8981\u548c\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u7ed3\u5408\u8d77\u6765\u770b\u3002

"},{"location":"linux/kernel/mm/#_2","title":"\u9875\u548c\u533a","text":"

\u5185\u6838\u5c06\u7269\u7406\u5185\u5b58\u5212\u5206\u4e3a\u4f17\u591a\u56fa\u5b9a\u5927\u5c0f\u7684\u9875\uff0c\u6bcf\u9875\u901a\u5e38\u5305\u542b4KB\u7684\u5b58\u50a8\u7a7a\u95f4\u3002\u8fd9\u79cd\u5206\u9875\u673a\u5236\u6781\u5927\u5730\u63d0\u5347\u4e86\u5185\u6838\u5728\u5185\u5b58\u5206\u914d\u548c\u56de\u6536\u65b9\u9762\u7684\u6548\u7387\uff0c\u540c\u65f6\u4e5f\u4e3a\u865a\u62df\u5185\u5b58\u7ba1\u7406\u63d0\u4f9b\u4e86\u4fbf\u5229\u3002\u901a\u8fc7\u5c06\u5185\u5b58\u5212\u5206\u4e3a\u4e00\u81f4\u5927\u5c0f\u7684\u5355\u5143\uff0c\u5185\u6838\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u5185\u5b58\u8bf7\u6c42\uff0c\u4f18\u5316\u8d44\u6e90\u5206\u914d\uff0c\u5e76\u7b80\u5316\u4e86\u5185\u5b58\u5bfb\u5740\u548c\u6620\u5c04\u7684\u8fc7\u7a0b\u3002struct page\u7ed3\u6784\u4f53\u8868\u793a\u9875\uff0c\u8be5\u5b9a\u4e49\u7684\u7b80\u5316\u7248\u5982\u4e0b\uff1a

struct page {\n    unsigned long flags;\n    atomic_t _count;\n    atomic_t _mapcount;\n    unsigned long private;\n    struct address_space *mapping;\n    pgoff_t index;\n    struct list_head lru;\n    void *virtual;\n};\n

flags\uff1a\u5b58\u653e\u9875\u7684\u72b6\u6001\uff0c\u662f\u4e0d\u662f\u810f\u9875\uff0c\u662f\u4e0d\u662f\u9501\u5b9a\u5728\u5185\u5b58\u4e2d\u7b49\u3002

_count\uff1a\u5b58\u653e\u9875\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5f53\u9875\u88ab\u5206\u914d\u7ed9\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u8fd9\u4e2a\u8ba1\u6570\u5668\u4f1a\u589e\u52a0\u3002\u5f53\u9875\u88ab\u91ca\u653e\u65f6\uff0c\u8ba1\u6570\u5668\u51cf\u5c11\u3002\u76f4\u5230\u8ba1\u6570\u5668\u964d\u81f30\uff0c\u9875\u624d\u80fd\u88ab\u56de\u6536\u3002

_mapcount\uff1a\u7528\u4e8e\u8bb0\u5f55\u9875\u8868\u9879\u6620\u5c04\u5230\u8be5\u7269\u7406\u9875\u7684\u6b21\u6570\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u5171\u4eab\u5185\u5b58\u9875\uff0c\u591a\u4e2a\u8fdb\u7a0b\u53ef\u80fd\u6620\u5c04\u5230\u540c\u4e00\u4e2a\u7269\u7406\u9875\u3002

mapping\uff1a\u6307\u5411\u4e00\u4e2aaddress_space\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u7528\u4e8e\u63cf\u8ff0\u4e0e\u6587\u4ef6\u76f8\u5173\u7684\u5185\u5b58\u6620\u5c04\u4fe1\u606f\u3002\u5982\u679c\u9875\u5c5e\u4e8e\u6587\u4ef6\u7f13\u5b58\uff0c\u8fd9\u4e2a\u5b57\u6bb5\u5c06\u6307\u5411\u76f8\u5e94\u7684address_space\u7ed3\u6784\u3002

index\uff1a\u8868\u793a\u9875\u5728\u6620\u5c04\u7684\u6587\u4ef6\u4e2d\u7684\u504f\u79fb\u91cf\uff0c\u5373\u9875\u5728\u6587\u4ef6\u4e2d\u7684\u7d22\u5f15\u53f7\u3002

lru\uff1a\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\u8282\u70b9\uff0c\u7528\u4e8e\u5c06\u9875\u94fe\u5165\u5230\u5404\u79cd\u94fe\u8868\u4e2d\uff0c\u6bd4\u5982\u6d3b\u8dc3\u94fe\u8868\u3001\u4e0d\u6d3b\u8dc3\u94fe\u8868\u7b49\u3002\u8fd9\u662f\u5185\u6838\u5b9e\u73b0\u9875\u7f6e\u6362\u7b97\u6cd5\uff08\u5982LRU\uff09\u7684\u4e00\u90e8\u5206\u3002

virtual\uff1a\u9875\u5728\u865a\u62df\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002

\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5185\u5b58\u548c\u4f18\u5316\u5185\u5b58\u5206\u914d\u7b56\u7565\uff0c\u5185\u6838\u5c06\u9875\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u533a(zone)\uff0c\u901a\u5e38\u5212\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u533a\uff1a

  • ZONE_DMA\uff1a\u7528\u4e8eDMA\u64cd\u4f5c\u7684\u5185\u5b58\u533a\uff0c\u901a\u5e38\u4f4d\u4e8e\u7269\u7406\u5185\u5b58\u7684\u4f4e\u5730\u5740\u90e8\u5206\u3002

  • ZONE_DMA32\uff1a\u4e0eZONE_DMA\u7c7b\u4f3c\uff0c\u4f46\u9002\u7528\u4e8e32\u4f4d\u5730\u5740\u7684DMA\u64cd\u4f5c\u3002

  • ZONE_NORMAL\uff1a\u666e\u901a\u7684\u5185\u5b58\u533a\uff0c\u53ef\u4ee5\u7531\u5185\u6838\u548c\u7528\u6237\u7a7a\u95f4\u8fdb\u7a0b\u4f7f\u7528\u3002

  • ZONE_HIGHMEM\uff1a\u9ad8\u7aef\u5185\u5b58\u533a\uff0c\u7528\u4e8e\u5904\u7406\u8d85\u8fc7\u76f4\u63a5\u6620\u5c04\u8303\u56f4\u7684\u5185\u5b58\u3002

\u533a\u7684\u4f7f\u7528\u548c\u5206\u5e03\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\uff0c\u5185\u6838\u5c06\u9875\u5212\u5206\u4e3a\u533a\uff0c\u5c31\u53ef\u4ee5\u6839\u636e\u7528\u9014\u8fdb\u884c\u5206\u914d\u3002\u533a\u7684\u5212\u5206\u6ca1\u6709\u7269\u7406\u610f\u4e49\uff0c\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u7ba1\u7406\u800c\u91c7\u53d6\u7684\u4e00\u79cd\u903b\u8f91\u5206\u7ec4\u3002

"},{"location":"linux/kernel/mm/#_3","title":"\u9875\u64cd\u4f5c","text":"

\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4ee5\u9875\u4e3a\u5355\u4f4d\u7684\u5185\u5b58\u5206\u914d\u4e0e\u91ca\u653e\u7684\u51fd\u6570\uff0c\u8fd9\u91cc\u4e0d\u505a\u8be6\u7ec6\u8bf4\u660e\u3002

\u51fd\u6570 \u63cf\u8ff0 alloc_page(gfp_mask) \u53ea\u5206\u914d\u4e00\u9875\uff0c\u8fd4\u56de\u6307\u5411\u9875\u7684\u6307\u9488 alloc_pages(gfp_mask, order) \u5206\u914d2order\u4e2a\u9875\uff0c\u8fd4\u56de\u6307\u5411\u7b2c\u4e00\u9875\u7684\u6307\u9488 __get_free_page(gfp_mask) \u53ea\u5206\u914d\u4e00\u9875\uff0c\u8fd4\u56de\u6307\u5411\u5176\u903b\u8f91\u5730\u5740\u7684\u6307\u9488 __get_free_pages(gfp_mask, order) \u5206\u914d2order\u4e2a\u9875\uff0c\u8fd4\u56de\u6307\u5411\u7b2c\u4e00\u9875\u903b\u8f91\u5730\u5740\u7684\u6307\u9488 get_zero_page(gfp_mask) \u53ea\u5206\u914d\u4e00\u9875\uff0c\u586b\u5145\u4e3a0\uff0c\u8fd4\u56de\u6307\u5411\u5176\u903b\u8f91\u5730\u5740\u7684\u6307\u9488 __free_pages(page, order) \u4f20\u5165\u9875\u7684\u6307\u9488\uff0c\u91ca\u653e2order\u4e2a\u9875 free_pages(addr, order) \u4f20\u5165\u7b2c\u4e00\u9875\u7684\u903b\u8f91\u5730\u5740\uff0c\u91ca\u653e2order\u4e2a\u9875 free_page(addr) \u91ca\u653e\u5355\u5f20\u9875

\u91ca\u653e\u9875\u65f6\u8981\u8c28\u614e\uff0c\u4e00\u65e6\u4f20\u9012\u4e86\u9519\u8bef\u7684page\u6216\u8005address\uff0c\u7cfb\u7edf\u5c31\u4f1a\u5d29\u6e83\u3002\u5728\u83b7\u5f97\u9875\u4e4b\u540e\uff0c\u9700\u8981\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u68c0\u67e5\u4ee5\u786e\u8ba4\u5185\u6838\u6b63\u786e\u5730\u5206\u914d\u4e86\u9875\u3002

\u5bf9\u4e8e\u5e38\u7528\u7684\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u5185\u5b58\u5206\u914d\u6765\u8bf4\uff0c\u5185\u6838\u63d0\u4f9b\u7684\u51fd\u6570\u662fkmalloc()\u3002

"},{"location":"linux/kernel/mm/#kmalloc","title":"kmalloc()","text":"

kmalloc()\u4e0e\u7528\u6237\u7a7a\u95f4\u7684malloc()\u7c7b\u4f3c\uff0c\u90fd\u662f\u5206\u914d\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u4e00\u5757\u5185\u5b58\uff0c\u533a\u522b\u5728\u4e8ekmalloc()\u591a\u4e86\u4e00\u4e2aflags\u53c2\u6570\uff1a

\u6ce8\u610f\uff1a\u4f7f\u7528kmalloc()\u51fd\u6570\u5206\u914d\u7684\u5185\u5b58\u53ea\u80fd\u4f7f\u7528kfree()\u51fd\u6570\u91ca\u653e\u3002

void *kmalloc(size_t size, gfp_t flags)\n

\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5185\u5b58\u5757\u7684\u6307\u9488\uff0c\u81f3\u5c11\u6709size\u5927\u5c0f\u3002\u6240\u5206\u914d\u7684\u5185\u5b58\u533a\u5728\u7269\u7406\u4e0a\u662f\u8fde\u7eed\u7684\u3002\u9664\u975e\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5b58\u53ef\u7528\uff0c\u5426\u5219\u5185\u6838\u603b\u80fd\u5206\u914d\u6210\u529f\u3002\u5f53\u7136\uff0c\u5728\u5bf9kmalloc()\u8c03\u7528\u4e4b\u540e\uff0c\u4f60\u8fd8\u662f\u9700\u8981\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u4e3aNULL\uff1a

struct dog *p;\np = kmalloc(sizeof(struct dog), GFP_KERNEL)\uff1b\nif(!p)\n    /*\u5904\u7406\u9519\u8bef*/\n

\u4e0d\u7ba1\u662f\u5728\u9875\u5206\u914d\u51fd\u6570\u8fd8\u662f\u5728kmalloc()\u4e2d\uff0c\u90fd\u7528\u5230\u4e86\u5206\u914d\u5668\u6807\u5fd7\u3002\u6807\u5fd7\u5206\u4e3a\u4e09\u7c7b\uff1a\u884c\u4e3a\u4fee\u9970\u7b26\u3001\u533a\u4fee\u9970\u7b26\u548c\u7c7b\u578b\u6807\u5fd7\u3002\u7c7b\u578b\u6807\u5fd7\u7ec4\u5408\u4e86\u524d\u4e24\u8005\uff0c\u7b80\u5316\u4e86\u4fee\u9970\u7b26\u7684\u4f7f\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u9700\u8981\u77e5\u9053\u7c7b\u578b\u6807\u5fd7\u5373\u53ef\u3002\u5185\u6838\u4e2d\u6700\u5e38\u7528\u7684\u5c31\u662fGFP_KERNEL\u3002\u8fd9\u79cd\u5206\u914d\u65b9\u5f0f\u53ef\u80fd\u4f1a\u5f15\u8d77\u7761\u7720\uff0c\u6240\u4ee5\u53ea\u80fd\u7528\u5728\u53ef\u4ee5\u91cd\u65b0\u5b89\u5168\u8c03\u5ea6\u7684\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u3002\u53e6\u4e00\u4e2a\u622a\u7136\u76f8\u53cd\u7684\u6807\u5fd7\u662fGFP_ATOMIC\uff0c\u8fd9\u4e2a\u6807\u5fd7\u8868\u793a\u4e0d\u80fd\u7761\u7720\u7684\u5185\u5b58\u5206\u914d\u3002\u4e0eGFP_KERNEL\u76f8\u6bd4\uff0c\u5b83\u5206\u914d\u6210\u529f\u7684\u673a\u4f1a\u8f83\u5c0f\uff0c\u4f46\u662f\u5728\u4e00\u4e9b\u65e0\u6cd5\u7761\u7720\u7684\u4ee3\u7801\u4e2d\uff0c\u4e5f\u53ea\u80fd\u9009\u62e9GFP_ATOMIC\u3002GFP_DMA\u6807\u5fd7\u8868\u793a\u5206\u914d\u5668\u5fc5\u987b\u6ee1\u8db3\u4eceZONE_DMA\u8fdb\u884c\u5206\u914d\u7684\u8bf7\u6c42\uff0c\u8be5\u6807\u5fd7\u7528\u5728\u9700\u8981DMA\u7684\u5185\u5b58\u7684\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u4e2d\u3002\u5728\u7f16\u5199\u7684\u7edd\u5927\u591a\u6570\u4ee3\u7801\u4e2d\uff0c\u8981\u4e48\u662fGFP_KERNEL\uff0c\u8981\u4e48\u662fGFP_ATOMIC\uff0c\u5176\u4ed6\u6807\u5fd7\u7528\u5230\u7684\u60c5\u51b5\u6781\u5c11\uff0c\u5c31\u4e0d\u505a\u8bf4\u660e\u4e86\u3002\u4e0b\u9762\u8fd9\u5f20\u8868\u683c\u603b\u7ed3\u4e86\u6807\u5fd7\u7684\u4f7f\u7528\u573a\u666f\u3002

\u60c5\u5f62 \u76f8\u5e94\u6807\u5fd7 \u53ef\u4ee5\u7761\u7720\u7684\u8fdb\u7a0b\u4e0a\u4e0b\u6587 GFP_KERNEL \u4e0d\u53ef\u4ee5\u7761\u7720\u7684\u8fdb\u7a0b\u4e0a\u4e0b\u6587 GFP_ATOMIC \u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f GFP_ATOMIC \u8f6f\u4e2d\u65ad GFP_ATOMIC tasklet GFP_ATOMIC \u53ef\u4ee5\u7761\u7720\u7684DMA\u5185\u5b58 \uff08GFP_DMA | GFP_KERNEL\uff09 \u4e0d\u53ef\u4ee5\u7761\u7720\u7684DMA\u5185\u5b58 \uff08GFP_DMA\uff09| GFP_ATOMIC

\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5c3d\u7ba1kmalloc()\u51fd\u6570\u5728\u6ca1\u6709\u7a7a\u95f2\u5185\u5b58\u65f6\u4f1a\u5931\u8d25\uff0c\u4f46\u662f\u5185\u6838\u603b\u4f1a\u5c3d\u53ef\u80fd\u6ee1\u8db3\u8fd9\u4e2a\u5185\u5b58\u5206\u914d\u8bf7\u6c42\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4e00\u4e0b\u5b50\u5206\u914d\u4e86\u5f88\u591a\u5185\u5b58\uff0c\u7cfb\u7edf\u7684\u54cd\u5e94\u6027\u80fd\u5c31\u4f1a\u964d\u4f4e\u2014\u2014\u5f53\u7cfb\u7edf\u4e3a\u4e86\u6ee1\u8db3kmalloc()\u7684\u5206\u914d\u8bf7\u6c42\uff0c\u4f1a\u8bd5\u56fe\u4e0e\u78c1\u76d8\u4ea4\u6362\u5c3d\u53ef\u80fd\u591a\u7684\u9875\u7a7a\u95f4\u5bfc\u81f4\u7cfb\u7edf\u5361\u6b7b\u3002

\u57fa\u4e8e\u9875\u7684\u5206\u914d\u7b56\u7565\u80fd\u591f\u66f4\u52a0\u6709\u6548\u5730\u5229\u7528\u5185\u5b58\u7a7a\u95f4\uff0c\u800ckmalloc()\u51fd\u6570\u4f1a\u56e0\u4e3a\u5206\u914d\u7c92\u5ea6\u7684\u95ee\u9898\u6d6a\u8d39\u4e00\u5b9a\u6570\u91cf\u7684\u5185\u5b58\u3002

"},{"location":"linux/kernel/mm/#vmalloc","title":"vmalloc()","text":"

vmalloc()\u51fd\u6570\u4e0ekmalloc()\u51fd\u6570\u5728\u5185\u5b58\u5206\u914d\u65b9\u9762\u5177\u6709\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5b83\u4eec\u5728\u7269\u7406\u5730\u5740\u8fde\u7eed\u6027\u65b9\u9762\u6709\u6240\u4e0d\u540c\u3002\u4e0ekmalloc()\u4fdd\u8bc1\u5206\u914d\u7684\u5185\u5b58\u9875\u5728\u7269\u7406\u5730\u5740\u4e0a\u8fde\u7eed\u4e0d\u540c\uff0cvmalloc()\u5206\u914d\u7684\u5185\u5b58\u9875\u5728\u7269\u7406\u5730\u5740\u4e0a\u53ef\u4ee5\u662f\u4e0d\u8fde\u7eed\u7684\u3002\u8fd9\u79cd\u73b0\u8c61\u4e0e\u7528\u6237\u7a7a\u95f4\u5185\u5b58\u5206\u914d\u51fd\u6570malloc()\u7c7b\u4f3c\uff0cmalloc()\u8fd4\u56de\u7684\u9875\u5728\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u662f\u8fde\u7eed\u7684\uff0c\u4f46\u5e76\u4e0d\u4fdd\u8bc1\u8fd9\u4e9b\u9875\u5728\u7269\u7406\u5730\u5740\u4e0a\u4e5f\u662f\u8fde\u7eed\u7684\u3002

\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u786c\u4ef6\u8bbe\u5907\u9700\u8981\u8bbf\u95ee\u7269\u7406\u5730\u5740\u8fde\u7eed\u7684\u5185\u5b58\u9875\u3002\u8fd9\u662f\u56e0\u4e3a\u8bb8\u591a\u4f53\u7cfb\u7ed3\u6784\u7684\u786c\u4ef6\u8bbe\u5907\u5b58\u5728\u4e8e\u5185\u5b58\u7ba1\u7406\u5355\u5143\uff08MMU\uff09\u4e4b\u5916\uff0c\u5b83\u4eec\u65e0\u6cd5\u5904\u7406\u865a\u62df\u5730\u5740\u3002\u56e0\u6b64\uff0c\u786c\u4ef6\u8bbe\u5907\u6240\u4f7f\u7528\u7684\u4efb\u4f55\u5185\u5b58\u533a\u57df\u90fd\u5fc5\u987b\u662f\u7269\u7406\u4e0a\u8fde\u7eed\u7684\u5185\u5b58\u5757\u3002

\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u8f6f\u4ef6\u4f7f\u7528\u7684\u5185\u5b58\u5757\u5219\u6ca1\u6709\u8fd9\u6837\u7684\u4e25\u683c\u8981\u6c42\u3002\u8f6f\u4ef6\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u865a\u62df\u5730\u5740\u6765\u8bbf\u95ee\u5185\u5b58\uff0c\u5373\u4f7f\u8fd9\u4e9b\u865a\u62df\u5730\u5740\u5bf9\u5e94\u7684\u7269\u7406\u5185\u5b58\u4e0d\u662f\u8fde\u7eed\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u9875\u8868\u7ba1\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u5c06\u4e0d\u8fde\u7eed\u7684\u7269\u7406\u5185\u5b58\u6620\u5c04\u5230\u8fde\u7eed\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u3002\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97\u8f6f\u4ef6\u53ef\u4ee5\u9ad8\u6548\u5730\u4f7f\u7528\u5206\u6563\u7684\u7269\u7406\u5185\u5b58\u8d44\u6e90\uff0c\u800c\u65e0\u9700\u5173\u5fc3\u5185\u5b58\u7684\u7269\u7406\u5e03\u5c40\u3002

\u7531\u4e8evmalloc()\u9700\u8981\u5bf9\u4e0d\u8fde\u7eed\u7684\u9875\u8fdb\u884c\u4e13\u95e8\u7684\u6620\u5c04\u5904\u7406\uff0c\u6240\u4ee5\u5176\u6027\u80fd\u4e0d\u5982kmalloc()\u3002\u4ec5\u5728\u4e0d\u5f97\u5df2\u7684\u60c5\u5f62\u4e0b\u624d\u4f1a\u4f7f\u7528\uff0c\u6bd4\u5982\u4e3a\u4e86\u83b7\u5f97\u5927\u5757\u5185\u5b58\u65f6\u3002\u56e0\u4e3akmalloc()\u5206\u914d\u7684\u5185\u5b58\u5757\u662f\u8fde\u7eed\u7684\uff0c\u5f53\u7cfb\u7edf\u4e2d\u7684\u5185\u5b58\u788e\u7247\u8f83\u591a\u65f6\uff0c\u5c1d\u8bd5\u5206\u914d\u5927\u5757\u5185\u5b58\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u800cvmalloc()\u5219\u4e0d\u4f1a\u3002

vmalloc()\u7684\u5206\u914d\u548c\u91ca\u653e\u63a5\u53e3\u5982\u4e0b\uff1a

void *vmalloc(unsinged long size)\n\nvoid vfree(const void *addr)\n

\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u4f1a\u7761\u7720\uff0c\u56e0\u6b64\u4e0d\u80fd\u5728\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u3002

"},{"location":"linux/kernel/mm/#_4","title":"\u4f19\u4f34\u7cfb\u7edf","text":"

\u4f19\u4f34\u7cfb\u7edf\uff08Buddy System\uff09\u662f\u4e00\u79cd\u5185\u5b58\u5206\u914d\u7b97\u6cd5\uff0c\u5b83\u88ab\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u7ba1\u7406\u3002\u5728\u8fd9\u79cd\u7cfb\u7edf\u4e2d\uff0c\u5185\u5b58\u88ab\u5206\u6210\u591a\u4e2a\u5927\u5c0f\u76f8\u7b49\u7684\u5757\uff0c\u6bcf\u4e2a\u5757\u7684\u5927\u5c0f\u90fd\u662f2\u7684\u5e42\u6b21\u65b9\u3002\u8fd9\u4e9b\u5757\u88ab\u7ec4\u7ec7\u6210\u4e00\u9897\u6811\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5757\u90fd\u662f\u53e6\u4e00\u4e2a\u5757\u7684\u201c\u4f19\u4f34\u201d\u3002

\u5185\u6838\u628a\u6240\u6709\u7684\u7a7a\u95f2\u9875\u5206\u7ec4\u4e3a11\u4e2a\u9875\u5757\u94fe\u8868\uff0c\u6bcf\u4e2a\u5757\u94fe\u8868\u5206\u522b\u5305\u542b\u5f88\u591a\u4e2a\u5927\u5c0f\u7684\u9875\u5757\uff0c\u6709 1\u30012\u30014\u30018\u300116\u300132\u300164\u3001128\u3001256\u3001512 \u548c 1024 \u4e2a\u8fde\u7eed\u9875\u7684\u9875\u5757\u3002\u6700\u5927\u53ef\u4ee5\u7533\u8bf71024\u4e2a\u8fde\u7eed\u9875\uff0c\u5bf9\u5e94 4MB\u5927\u5c0f\u7684\u8fde\u7eed\u5185\u5b58\u3002

\u5f53\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u5206\u914d\u5185\u5b58\u65f6\uff0c\u5b83\u4f1a\u6839\u636e\u8bf7\u6c42\u7684\u5927\u5c0f\u627e\u5230\u6700\u63a5\u8fd1\u7684\u4e14\u5927\u5c0f\u8db3\u591f\u7684\u5757\u8fdb\u884c\u5206\u914d\u3002\u5982\u679c\u5206\u914d\u7684\u5757\u6bd4\u6240\u9700\u7684\u5927\u5c0f\u5927\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5757\u4f1a\u88ab\u5206\u6210\u4e24\u4e2a\u5927\u5c0f\u76f8\u7b49\u7684\u5b50\u5757\uff0c\u5176\u4e2d\u4e00\u4e2a\u5b50\u5757\u88ab\u7528\u6765\u6ee1\u8db3\u5185\u5b58\u5206\u914d\u8bf7\u6c42\uff0c\u800c\u53e6\u4e00\u4e2a\u5219\u6210\u4e3a\u5b83\u7684\u201c\u4f19\u4f34\u201d\u3002

\u5728\u4f19\u4f34\u7cfb\u7edf\u4e2d\uff0c\u5f53\u9700\u8981\u5206\u914d\u4e00\u4e2a\u7279\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u5757\u65f6\uff0c\u7cfb\u7edf\u4f1a\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a

  1. \u9996\u5148\uff0c\u786e\u5b9a\u6240\u9700\u5185\u5b58\u5757\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981128\u4e2a\u9875\u7684\u5757\uff0c\u7cfb\u7edf\u4f1a\u68c0\u67e5128\u4e2a\u9875\u7684\u7a7a\u95f2\u5757\u94fe\u8868\u3002

  2. \u5982\u679c\u5728128\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u6ca1\u6709\u627e\u5230\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u5bfb\u627e\u4e0b\u4e00\u4e2a\u66f4\u5927\u7684\u5185\u5b58\u5757\u5c3a\u5bf8\uff0c\u5373256\u4e2a\u9875\u7684\u5757\u3002

  3. \u5982\u679c\u5728256\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u627e\u5230\u4e86\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u5c06\u8fd9\u4e2a\u5757\u5206\u6210\u4e24\u4e2a\u5927\u5c0f\u76f8\u7b49\u7684\u5757\uff0c\u6bcf\u4e2a\u5757\u5305\u542b128\u4e2a\u9875\u3002\u5176\u4e2d\u4e00\u4e2a\u5757\u88ab\u5206\u914d\u7ed9\u8bf7\u6c42\u8005\uff0c\u53e6\u4e00\u4e2a\u5757\u4f5c\u4e3a\u65b0\u7684\u7a7a\u95f2\u5757\u63d2\u5165\u5230128\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u3002

  4. \u5982\u679c256\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u4e5f\u6ca1\u6709\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u7ee7\u7eed\u68c0\u67e5\u66f4\u5927\u7684\u5757\uff0c\u6bd4\u5982512\u4e2a\u9875\u7684\u5757\u3002

  5. \u5982\u679c\u5728512\u4e2a\u9875\u7684\u94fe\u8868\u4e2d\u627e\u5230\u4e86\u7a7a\u95f2\u5757\uff0c\u7cfb\u7edf\u4f1a\u5c06\u8fd9\u4e2a\u5757\u5206\u88c2\u6210\u4e09\u4e2a\u5757\uff1a\u4e24\u4e2a128\u4e2a\u9875\u7684\u5757\u548c\u4e00\u4e2a256\u4e2a\u9875\u7684\u5757\u3002\u5176\u4e2d\u4e00\u4e2a128\u4e2a\u9875\u7684\u5757\u88ab\u5206\u914d\u51fa\u53bb\uff0c\u5176\u4f59\u4e24\u4e2a\u5757\u5206\u522b\u63d2\u5165\u5230\u76f8\u5e94\u7684\u94fe\u8868\u4e2d\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f19\u4f34\u7cfb\u7edf\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u5185\u5b58\uff0c\u786e\u4fdd\u5185\u5b58\u5206\u914d\u65e2\u9ad8\u6548\u53c8\u7075\u6d3b\u3002

\u5f53\u5185\u5b58\u5757\u88ab\u91ca\u653e\u65f6\uff0c\u7cfb\u7edf\u4f1a\u68c0\u67e5\u662f\u5426\u6709\u76f8\u90bb\u7684\u4f19\u4f34\u5757\u540c\u6837\u7a7a\u95f2\uff0c\u5982\u679c\u4e24\u8005\u90fd\u662f\u7a7a\u95f2\u7684\uff0c\u5b83\u4eec\u4f1a\u88ab\u5408\u5e76\u56de\u539f\u6765\u7684\u5927\u5757\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u788e\u7247\u3002

"},{"location":"linux/kernel/mm/#slab","title":"slab\u5206\u914d\u5668","text":"

\u5728\u5185\u6838\u7f16\u7a0b\u4e2d\uff0c\u5206\u914d\u548c\u91ca\u653e\u6570\u636e\u7ed3\u6784\u662f\u4e00\u9879\u6781\u4e3a\u5e38\u89c1\u7684\u5de5\u4f5c\u3002\u4e3a\u4e86\u9ad8\u6548\u5730\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u7684\u9891\u7e41\u5206\u914d\u4e0e\u56de\u6536\uff0c\u5f00\u53d1\u8005\u4eec\u901a\u5e38\u4f1a\u5229\u7528\u7a7a\u95f2\u94fe\u8868\u6765\u4f18\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u7a7a\u95f2\u94fe\u8868\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u9884\u5148\u5206\u914d\u7684\u6570\u636e\u7ed3\u6784\u5757\u7684\u96c6\u5408\uff0c\u8fd9\u4e9b\u5757\u90fd\u5904\u4e8e\u53ef\u7528\u72b6\u6001\u3002\u5f53\u7cfb\u7edf\u9700\u8981\u65b0\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u4f8b\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u7a7a\u95f2\u94fe\u8868\u4e2d\u83b7\u53d6\u4e00\u4e2a\u73b0\u6210\u7684\u5757\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u5b9e\u65f6\u5185\u5b58\u5206\u914d\u7684\u6d88\u8017\u3002\u4f7f\u7528\u5b8c\u6bd5\u540e\uff0c\u8be5\u6570\u636e\u7ed3\u6784\u5b9e\u4f8b\u88ab\u5f52\u8fd8\u5230\u7a7a\u95f2\u94fe\u8868\u4e2d\uff0c\u800c\u4e0d\u662f\u88ab\u76f4\u63a5\u91ca\u653e\u3002\u5728\u8fd9\u79cd\u673a\u5236\u4e0b\uff0c\u7a7a\u95f2\u94fe\u8868\u5145\u5f53\u4e86\u4e00\u79cd\u7279\u6b8a\u7684\u5bf9\u8c61\u9ad8\u901f\u7f13\u5b58\uff0c\u5b83\u80fd\u591f\u5feb\u901f\u5730\u5b58\u50a8\u548c\u63d0\u4f9b\u90a3\u4e9b\u7ecf\u5e38\u88ab\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u578b\u3002\u8fd9\u6837\u7684\u8bbe\u8ba1\u4e0d\u4ec5\u63d0\u5347\u4e86\u5185\u5b58\u7ba1\u7406\u7684\u6548\u7387\uff0c\u8fd8\u51cf\u5c11\u4e86\u5185\u5b58\u788e\u7247\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u5b9e\u7528\u7684\u7f16\u7a0b\u6280\u5de7\u3002

Linux\u5185\u6838\u63d0\u4f9b\u4e86slab\u5206\u914d\u5668\uff0c\u5b83\u901a\u8fc7\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u7ec4\u7ec7\u5230\u5404\u81ea\u7684\u9ad8\u901f\u7f13\u5b58\u7ec4\u4e2d\uff0c\u6765\u4f18\u5316\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5206\u914d\u548c\u56de\u6536\u3002\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u7ec4\u4e13\u95e8\u7528\u4e8e\u5b58\u50a8\u4e00\u79cd\u7279\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\u53ef\u80fd\u7528\u4e8e\u7ba1\u7406\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff08task_struct\u7ed3\u6784\uff09\uff0c\u800c\u53e6\u4e00\u4e2a\u5219\u7528\u4e8e\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\uff08struct inode\uff09\u3002

slab\u53ef\u4ee5\u5904\u4e8e\u4e09\u79cd\u72b6\u6001\u4e4b\u4e00\uff1a\u6ee1\u3001\u90e8\u5206\u6ee1\u6216\u7a7a\u3002\u6ee1\u7684slab\u610f\u5473\u7740\u6240\u6709\u5bf9\u8c61\u90fd\u5df2\u5206\u914d\u51fa\u53bb\uff0c\u7a7a\u7684slab\u5219\u8868\u793a\u6240\u6709\u5bf9\u8c61\u90fd\u672a\u88ab\u5206\u914d\uff0c\u800c\u90e8\u5206\u6ee1\u7684slab\u5219\u5305\u542b\u4e86\u5df2\u5206\u914d\u548c\u672a\u5206\u914d\u7684\u5bf9\u8c61\u3002\u5f53\u5185\u6838\u8bf7\u6c42\u65b0\u5bf9\u8c61\u65f6\uff0c\u4f18\u5148\u4ece\u90e8\u5206\u6ee1\u7684slab\u4e2d\u5206\u914d\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5219\u4ece\u7a7a\u7684slab\u4e2d\u5206\u914d\u3002\u5982\u679c\u8fde\u7a7a\u7684slab\u90fd\u6ca1\u6709\uff0c\u5c31\u4f1a\u521b\u5efa\u65b0\u7684slab\u3002

\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u90fd\u4f7f\u7528kmem_cache\u7ed3\u6784\u4f53\u8868\u793a\u3002\u8fd9\u4e2a\u7ed3\u6784\u5305\u542b\u4e09\u4e2a\u94fe\u8868\uff1aslabs_full\u3001slabs_partial\u3001slabs_empty\uff0c\u8fd9\u4e9b\u94fe\u8868\u5305\u542b\u4e86\u9ad8\u901f\u7f13\u5b58\u4e2d\u7684\u6240\u6709slab\u3002struct slab\u7528\u6765\u63cf\u8ff0\u6bcf\u4e2aslab\u3002

slab\u5c42\u7684\u7ba1\u7406\u662f\u5728\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u7684\u57fa\u7840\u4e0a\uff0c\u901a\u8fc7\u5185\u6838\u63d0\u4f9b\u7684\u7edf\u4e00\u63a5\u53e3\u6765\u5b8c\u6210\u7684\u3002\u521b\u5efa\u548c\u64a4\u9500\u9ad8\u901f\u7f13\u5b58\uff0c\u5e76\u5728\u9ad8\u901f\u7f13\u5b58\u5185\u5206\u914d\u548c\u91ca\u653e\u5bf9\u8c61\u3002\u590d\u6742\u7684\u9ad8\u901f\u7f13\u5b58\u673a\u5236\u548cslab\u7684\u7ba1\u7406\u5b8c\u5168\u7531\u5185\u90e8\u673a\u5236\u6765\u5904\u7406\uff0c\u7528\u6237\u65e0\u987b\u5173\u5fc3\u3002

\u9ad8\u901f\u7f13\u5b58\u7684\u4f7f\u7528\u7edf\u8ba1\u60c5\u51b5\u53ef\u4ee5\u4ece/proc/slabinfo\u4e2d\u83b7\u5f97\u3002

struct kmem_cache *kmem_cache_create(const char *name, size_t size,\n                                     size_t align, unsigned int flags,\n                                     void (*ctor)(void *))              //\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9ad8\u901f\u7f13\u5b58\nint kmem_cache_destroy(struct kmem_cache *cachep)                       //\u64a4\u9500\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\nvoid *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)          //\u4ece\u7f13\u5b58\u4e2d\u5206\u914d\u5bf9\u8c61\nvoid kmem_cache_free(struct kmem_cache *cachep, void *objp)             //\u91ca\u653e\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c06\u5b83\u8fd4\u56de\u7ed9\u539f\u5148\u7684slab\n

\u5728\u6587\u4ef6<kernel/fork.c>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8bb8\u591a\u9ad8\u901f\u7f13\u5b58\u7684\u5b9e\u73b0\uff1a

/* SLAB cache for signal_struct structures (tsk->signal) */\nstatic struct kmem_cache *signal_cachep;\n\n/* SLAB cache for sighand_struct structures (tsk->sighand) */\nstruct kmem_cache *sighand_cachep;\n\n/* SLAB cache for files_struct structures (tsk->files) */\nstruct kmem_cache *files_cachep;\n\n/* SLAB cache for fs_struct structures (tsk->fs) */\nstruct kmem_cache *fs_cachep;\n\n/* SLAB cache for vm_area_struct structures */\nstatic struct kmem_cache *vm_area_cachep;\n\n/* SLAB cache for mm_struct structures (tsk->mm) */\nstatic struct kmem_cache *mm_cachep;\n

\u7531\u4e8efork()\u7cfb\u7edf\u8c03\u7528\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684task_struct\u7ed3\u6784\u4f53\uff0c\u4e8e\u662f\u4f7f\u7528\u9ad8\u901f\u7f13\u5b58\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u7cfb\u7edf\u7684\u6027\u80fd\u3002

"},{"location":"linux/kernel/mm/#swap","title":"Swap\u673a\u5236","text":"

\u5f53\u7cfb\u7edf\u5185\u5b58\u8d44\u6e90\u7d27\u5f20\u65f6\uff0c\u5185\u6838\u4f1a\u5bf9\u4e00\u4e9b\u5185\u5b58\u8fdb\u884c\u56de\u6536\u3002\u5bf9\u4e8e\u6587\u4ef6\u9875\uff0c\u5927\u90e8\u5206\u53ef\u4ee5\u76f4\u63a5\u56de\u6536\uff0c\u5982\u679c\u662f\u810f\u9875\u5219\u9700\u8981\u5148\u5199\u5165\u78c1\u76d8\u3002\u5bf9\u4e8e\u533f\u540d\u9875\uff0c\u5185\u6838\u5f15\u5165\u4e86Swap\u673a\u5236\u6765\u56de\u6536\u3002

Swap\u673a\u5236\u662f\u5185\u6838\u4e2d\u7684\u4e00\u79cd\u5185\u5b58\u7ba1\u7406\u6280\u672f\uff0c\u7528\u4e8e\u5904\u7406\u5185\u5b58\u4e0d\u8db3\u7684\u60c5\u51b5\u3002\u5f53\u7cfb\u7edf\u4e2d\u7684\u5b9e\u9645\u7269\u7406\u5185\u5b58\u4e0d\u8db3\u4ee5\u6ee1\u8db3\u7a0b\u5e8f\u8fd0\u884c\u9700\u6c42\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u90e8\u5206\u6682\u65f6\u4e0d\u7528\u7684\u5185\u5b58\u9875\u9762\u79fb\u5230Swap\u7a7a\u95f4\u4e2d\uff0c\u4ece\u800c\u91ca\u653e\u7269\u7406\u5185\u5b58\uff0c\u4ee5\u4fbf\u7ee7\u7eed\u8fd0\u884c\u5176\u4ed6\u7a0b\u5e8f\u3002\u5b83\u5305\u62ec\u6362\u51fa\u548c\u6362\u5165\u4e24\u4e2a\u8fc7\u7a0b\u3002

  • \u6362\u51fa\uff0c\u5c31\u662f\u628a\u8fdb\u7a0b\u6682\u65f6\u4e0d\u7528\u7684\u5185\u5b58\u6570\u636e\u5b58\u50a8\u5230\u78c1\u76d8\u4e2d\uff0c\u5e76\u91ca\u653e\u8fd9\u4e9b\u6570\u636e\u5360\u7528\u7684\u5185\u5b58\u3002
  • \u6362\u5165\uff0c\u5c31\u662f\u5c06\u78c1\u76d8\u4e2d\u7684\u6570\u636e\u91cd\u65b0\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002

\u5f53\u6709\u65b0\u7684\u5927\u5757\u5185\u5b58\u5206\u914d\u8bf7\u6c42\u800c\u5269\u4f59\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u5185\u6838\u5c31\u4f1a\u56de\u6536\u4e00\u90e8\u5206\u5185\u5b58\uff0c\u5c3d\u53ef\u80fd\u5730\u6ee1\u8db3\u65b0\u5185\u5b58\u8bf7\u6c42\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u76f4\u63a5\u5185\u5b58\u56de\u6536\u3002

\u9664\u4e86\u76f4\u63a5\u5185\u5b58\u56de\u6536\uff0c\u8fd8\u6709\u4e00\u4e2a\u4e13\u95e8\u7684\u5185\u6838\u7ebf\u7a0bkswapd0\u7528\u6765\u5b9a\u671f\u56de\u6536\u5185\u5b58\u3002\u4e3a\u4e86\u8861\u91cf\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u5185\u6838\u5b9a\u4e49\u4e86\u4e09\u4e2a\u5185\u5b58\u9608\u503c\uff0c\u5206\u522b\u662f\u9875\u6700\u5c0f\u9608\u503c\uff08pages_min\uff09\u3001\u9875\u4f4e\u9608\u503c\uff08pages_low\uff09\u548c\u9875\u9ad8\u9608\u503c\uff08pages_high\uff09\uff0c\u5269\u4f59\u5185\u5b58\uff0c\u5219\u7528pages_free\u8868\u793a\u3002

\u4e00\u65e6\u5269\u4f59\u5185\u5b58\u5c0f\u4e8e\u9875\u4f4e\u9608\u503c\uff0c\u5c31\u4f1a\u89e6\u53d1\u5185\u5b58\u7684\u56de\u6536\u3002

\u5728NUMA\u67b6\u6784\u4e0b\uff0c\u6bcf\u4e2a\u5904\u7406\u5668\u90fd\u4f1a\u88ab\u5212\u5206\u5230\u4e0d\u540c\u7684Node\u4e0b\uff0c\u800c\u6bcf\u4e2aNode\u90fd\u6709\u81ea\u5df1\u7684\u672c\u5730\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u5185\u6838\u9700\u8981\u6839\u636e\u5f53\u524dNode\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u6765\u51b3\u5b9a\u56de\u6536\u7684\u76ee\u6807\u3002\u5f53\u67d0\u4e2aNode\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u5185\u6838\u53ef\u4ee5\u4ece\u5176\u4ed6Node\u5bfb\u627e\u7a7a\u95f2\u5185\u5b58\uff0c\u4e5f\u53ef\u4ee5\u4ece\u672c\u5730\u56de\u6536\u5185\u5b58\uff0c\u5177\u4f53\u56de\u6536\u6a21\u5f0f\u5728/proc/sys/vm/zone_reclaim_mode\u6587\u4ef6\u4e2d\u8fdb\u884c\u914d\u7f6e\u3002

/proc/sys/vm/swappiness\u9009\u9879\u7528\u6765\u8c03\u6574\u4f7f\u7528Swap\u7684\u79ef\u6781\u7a0b\u5ea6\uff0c\u4ece0~100\uff0c\u6570\u503c\u8d8a\u5927\u8d8a\u79ef\u6781\u4f7f\u7528Swap\uff0c\u4e5f\u5c31\u662f\u66f4\u503e\u5411\u4e8e\u56de\u6536\u533f\u540d\u9875\uff1b\u6570\u503c\u8d8a\u5c0f\u8d8a\u6d88\u6781\u4f7f\u7528Swap\uff0c\u4e5f\u5c31\u662f\u66f4\u503e\u5411\u4e8e\u56de\u6536\u6587\u4ef6\u9875\u3002

"},{"location":"linux/kernel/network/","title":"\u7f51\u7edc\u7cfb\u7edf","text":""},{"location":"linux/kernel/others/","title":"\u5176\u4ed6","text":"

\u8fd9\u91cc\u4e3b\u8981\u653e\u4e00\u4e9b\u96f6\u788e\u7684\u5185\u5bb9\u3002

"},{"location":"linux/kernel/others/#_2","title":"\u7cfb\u7edf\u8c03\u7528","text":"

\u5f53\u4f60\u7684\u7a0b\u5e8f\u8c03\u7528open, read, write, close\u7b49\u51fd\u6570\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1\u7cfb\u7edf\u8c03\u7528\uff08syscall\uff09\u3002\u7cfb\u7edf\u8c03\u7528\u672c\u8d28\u662f\u7528\u6237\u6001\u8fdb\u7a0b\u4e0e\u786c\u4ef6\u8bbe\u5907\u4ea4\u4e92\u7684\u63a5\u53e3\uff0c\u5185\u6838\u8d1f\u8d23\u68c0\u67e5\u7cfb\u7edf\u8c03\u7528\u7684\u6b63\u786e\u6027\uff0c\u7136\u540e\u53d1\u51fa\u6307\u4ee4\u7ed9\u786c\u4ef6\u3002\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u8005\uff0c\u4e0d\u7528\u5173\u5fc3\u5e95\u5c42\u786c\u4ef6\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u800c\u53ea\u9700\u8981\u8c03\u7528\u666e\u901a\u51fd\u6570\u5c31\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\u4e86\u3002glibc\u5e93\u8fdb\u4e00\u6b65\u5c01\u88c5\u4e86\u7ec6\u8282\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528glibc\u5e93\u66b4\u9732\u7684API\u5373\u53ef\u3002

x86\u4f53\u7cfb\u8fdb\u5165\u548c\u9000\u51fa\u7cfb\u7edf\u8c03\u7528\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a

  • int $0x80 \u548c iret
  • sysenter \u548c sysexit

\u7b2c\u4e8c\u79cd\u88ab\u79f0\u4e3a\u5feb\u901f\u7cfb\u7edf\u8c03\u7528\u3002

\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u6700\u7ec8\u7ed3\u679c\u90fd\u662f\u8df3\u8f6c\u5230\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u51fd\u6570\uff08system call handler\uff09\u3002\u7531\u4e8e\u5185\u6838\u5b9e\u73b0\u4e86\u5f88\u591a\u4e0d\u540c\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u56e0\u6b64\u8fdb\u7a0b\u5fc5\u987b\u4f20\u9012\u4e00\u4e2a\u540d\u4e3a\u7cfb\u7edf\u8c03\u7528\u53f7\uff08system call number\uff09\u7684\u53c2\u6570\u6765\u8bc6\u522b\u6240\u9700\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u8fd9\u4e2a\u53c2\u6570\u5b58\u653e\u5728eax\u5bc4\u5b58\u5668\u4e2d\u3002\u6267\u884c\u5b8c\u7cfb\u7edf\u8c03\u7528\u540e\u7684\u8fd4\u56de\u503c\u4e5f\u653e\u5728eax\u5bc4\u5b58\u5668\u4e2d\uff0c\u5176\u4e2d\u6b63\u6570\u62160\u8868\u793a\u7cfb\u7edf\u8c03\u7528\u6210\u529f\uff0c\u8d1f\u6570\u8868\u793a\u51fa\u9519\uff0c\u5b58\u653e\u4e8eerrno\u5168\u5c40\u53d8\u91cf\u4e2d\u3002

\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u6d41\u7a0b\u662f\uff1a

  • \u5c06\u7cfb\u7edf\u8c03\u7528\u7684\u53c2\u6570\u5199\u5165CPU\u5bc4\u5b58\u5668
  • \u68c0\u67e5\u6240\u6709\u7684\u7cfb\u7edf\u8c03\u7528\u53c2\u6570
  • \u5c06CPU\u4e2d\u7684\u53c2\u6570\u62f7\u8d1d\u81f3\u5185\u6838\u6001\u5806\u6808
  • \u8c03\u7528\u540d\u4e3a\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\uff08system call service routine\uff09\u7684C\u51fd\u6570\u6765\u5904\u7406\u7cfb\u7edf\u8c03\u7528
  • \u9000\u51fa\u7cfb\u7edf\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\uff0c\u5c06\u5185\u6838\u6808\u4e2d\u7684\u503c\u52a0\u8f7d\u81f3\u5bc4\u5b58\u5668\uff0c\u5e76\u4ece\u5185\u6838\u6001\u5207\u6362\u56de\u7528\u6237\u6001

\u4e3a\u4e86\u5c06\u7cfb\u7edf\u8c03\u7528\u53f7\u4e0e\u5bf9\u5e94\u7684\u670d\u52a1\u4f8b\u7a0b\u8054\u7cfb\u8d77\u6765\uff0c\u5185\u6838\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u5206\u6d3e\u8868\uff08system call dispatch table\uff09\uff0c\u8fd9\u4e2a\u8868\u5b58\u653e\u5728sys_call_table\u6570\u7ec4\u4e2d\u3002\u5185\u6838\u62ff\u5230\u7cfb\u7edf\u8c03\u7528\u53f7\u4e4b\u540e\uff0c\u5c31\u53bbsys_call_table\u4e2d\u627e\u5230\u5bf9\u5e94\u7684\u7cfb\u7edf\u8c03\u7528\u5b9e\u73b0\u51fd\u6570\u53bb\u6267\u884c\u3002\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f7f\u7528\u8fd4\u56de\u6307\u4ee4\u4ece\u5185\u6838\u6001\u8fd4\u56de\u81f3\u7528\u6237\u6001\u3002

\u5185\u6838\u5728\u6267\u884c\u7cfb\u7edf\u8c03\u7528\u7684\u65f6\u5019\u5904\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u3002current\u6307\u9488\u6307\u5411\u5f53\u524d\u4efb\u52a1\uff0c\u5373\u5f15\u53d1\u7cfb\u7edf\u8c03\u7528\u7684\u90a3\u4e2a\u8fdb\u7a0b\u3002\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u5185\u6838\u53ef\u4ee5\u4f11\u7720\u5e76\u4e14\u53ef\u4ee5\u88ab\u62a2\u5360\u3002\u56e0\u6b64\u5fc5\u987b\u4fdd\u8bc1\u7cfb\u7edf\u8c03\u7528\u65f6\u53ef\u91cd\u5165\u7684\uff0c\u56e0\u4e3a\u65b0\u7684\u8fdb\u7a0b\u53ef\u80fd\u4f1a\u4f7f\u7528\u76f8\u540c\u7684\u7cfb\u7edf\u8c03\u7528\u3002

"},{"location":"linux/kernel/others/#_3","title":"\u4fe1\u53f7","text":"

\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u54cd\u5e94\u5404\u79cd\u4e8b\u4ef6\u5b9a\u4e49\u4e86\u975e\u5e38\u591a\u7684\u4fe1\u53f7\u3002\u6bd4\u5982\u5f53\u6211\u4eec\u53d1\u9001kill -9 ${pid}\u65f6\uff0c\u5176\u5b9e\u5c31\u662f\u53d1\u9001SIGKILL\u4fe1\u53f7\u7ed9\u6307\u5b9a\u8fdb\u7a0b\uff0c\u5c06\u5b83\u6740\u6b7b\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7kill -l\u547d\u4ee4\u67e5\u770b\u6240\u6709\u7684\u4fe1\u53f7\u3002\u6bcf\u4e2a\u4fe1\u53f7\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684ID\u548c\u5bf9\u5e94\u7684\u9ed8\u8ba4\u64cd\u4f5c\u3002

\u8fdb\u7a0b\u5bf9\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a

  1. \u6267\u884c\u9ed8\u8ba4\u64cd\u4f5c\u3002
  2. \u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002
  3. \u5ffd\u7565\u4fe1\u53f7\u3002\u6ce8\u610f\uff0cSIGKILL\u548cSIGSTOP\u65e0\u6cd5\u5ffd\u7565\u3002

Linux\u63a8\u8350\u4f7f\u7528sigaction()\u51fd\u6570\u6765\u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);\n

\u800csigaction\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

struct sigaction {\n  __sighandler_t sa_handler;\n  unsigned long sa_flags;\n  __sigrestore_t sa_restorer;\n  sigset_t sa_mask;   \n};\n

\u5176\u4e2dsa_handler\u5c31\u662f\u4f60\u8981\u5b9a\u4e49\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002

\u53d1\u9001\u4fe1\u53f7\u7531\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u53d1\u9001\u7ed9\u6574\u4e2a\u7ebf\u7a0b\u7ec4\u7684\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u53d1\u9001\u7ed9\u67d0\u4e2a\u5355\u72ec\u7ebf\u7a0b\u7684\u3002\u4fe1\u53f7\u5206\u4e3a\u4e0d\u53ef\u9760\u4fe1\u53f7\u548c\u53ef\u9760\u4fe1\u53f7\u3002\u5728task_struct\u4e2d\u6709\u4e00\u4e2a\u7ed3\u6784\u4f53sigpending\uff0c\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

struct sigpending{\n    struct list_head list;\n    sigset_t signal;\n};\n

\u5bf9\u4e8e\u4e0d\u53ef\u9760\u4fe1\u53f7\uff0c\u4e5f\u5c31\u662f\u7f16\u53f7\u5c0f\u4e8e32\u7684\u4fe1\u53f7\uff0c\u4f1a\u653e\u5728sigset_t\u96c6\u5408\u4e2d\uff0c\u4e0d\u8bba\u53d1\u9001\u591a\u5c11\u6b21\uff0c\u5728\u88ab\u5904\u7406\u524d\u90fd\u53ea\u4f1a\u4fdd\u7559\u4e00\u4efd\u3002\u5bf9\u4e8e\u53ef\u9760\u4fe1\u53f7\uff0c\u5219\u4f1a\u6302\u5728struct sigpending\u7684\u94fe\u8868\u4e2d\u6328\u4e2a\u5904\u7406\u3002

"},{"location":"linux/kernel/others/#_4","title":"\u5b9a\u65f6\u5668","text":"

\u5185\u6838\u4e2d\u6709\u5927\u91cf\u7684\u51fd\u6570\u662f\u57fa\u4e8e\u65f6\u95f4\u9a71\u52a8\u7684\u3002\u6709\u4e9b\u51fd\u6570\u9700\u8981\u5468\u671f\u6027\u6267\u884c\uff0c\u6709\u4e9b\u51fd\u6570\u9700\u8981\u7b49\u5f85\u4e00\u4e2a\u76f8\u5bf9\u65f6\u95f4\u540e\u624d\u8fd0\u884c\u3002\u5468\u671f\u6027\u7684\u4e8b\u4ef6\u7531\u7cfb\u7edf\u5b9a\u65f6\u5668\u9a71\u52a8\u3002\u7cfb\u7edf\u5b9a\u65f6\u5668\u80fd\u4ee5\u56fa\u5b9a\u7684\u9891\u7387\u4ea7\u751f\u4e2d\u65ad\uff0c\u8fd9\u79cd\u4e2d\u65ad\u88ab\u79f0\u4e3a\u5b9a\u65f6\u5668\u4e2d\u65ad\uff0c\u5b83\u6240\u5bf9\u5e94\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8d1f\u8d23\u66f4\u65b0\u7cfb\u7edf\u65f6\u95f4\u548c\u6267\u884c\u5468\u671f\u6027\u4efb\u52a1\u3002

\u7cfb\u7edf\u5b9a\u65f6\u5668\u7684\u9891\u7387\u88ab\u79f0\u4e3a\u8282\u62cd\u7387\uff08tick rate\uff09\uff0c\u4ea7\u751f\u7684\u4e24\u6b21\u65f6\u949f\u4e2d\u65ad\u7684\u95f4\u9694\u5c31\u88ab\u79f0\u4e3a\u8282\u62cd\uff08tick\uff09\uff0c\u5b83\u7b49\u4e8e\u8282\u62cd\u7387\u5206\u4e4b\u4e00\u79d2\u3002\u5185\u6838\u901a\u8fc7\u65f6\u949f\u4e2d\u65ad\u95f4\u9694\u6765\u8ba1\u7b97\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4\u3002\u65f6\u949f\u4e2d\u65ad\u9700\u8981\u6267\u884c\u7684\u64cd\u4f5c\u6709\uff1a

  • \u66f4\u65b0\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4
  • \u66f4\u65b0\u5b9e\u9645\u65f6\u95f4
  • \u5728SMP\u7cfb\u7edf\u4e0a\uff0c\u5747\u8861\u8c03\u5ea6\u7a0b\u5e8f\u4e2d\u5404\u5904\u7406\u5668\u4e0a\u7684\u8fd0\u884c\u961f\u5217
  • \u68c0\u67e5\u5f53\u524d\u8fdb\u7a0b\u662f\u5426\u7528\u5c3d\u4e86\u81ea\u5df1\u7684\u65f6\u95f4\u7247
  • \u8fd0\u884c\u8d85\u65f6\u7684\u52a8\u6001\u5b9a\u65f6\u5668
  • \u66f4\u65b0\u8d44\u6e90\u6d88\u8017\u548c\u5904\u7406\u5668\u65f6\u95f4\u7684\u7edf\u8ba1\u503c
"},{"location":"linux/kernel/others/#jiffies","title":"\u8282\u62cd\u7387\u548cjiffies\u53d8\u91cf","text":"

\u7cfb\u7edf\u5b9a\u65f6\u5668\u9891\u7387\u662f\u901a\u8fc7\u9759\u6001\u9884\u5904\u7406\u5b9a\u4e49\u7684\uff0c\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u6309\u7167HZ\u7684\u503c\u5bf9\u786c\u4ef6\u8fdb\u884c\u8bbe\u7f6e\uff0c\u8be5\u503c\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u3002\u5185\u6838\u5728<asm/param.h>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002\u5728x86\u4f53\u7cfb\u7ed3\u6784\u4e2d\uff0c\u7cfb\u7edf\u5b9a\u65f6\u5668\u9891\u7387\u9ed8\u8ba4\u4e3a1000\u3002\u4e5f\u5c31\u662f\u8bf4x86\u7684\u65f6\u949f\u4e2d\u65ad\u9891\u7387\u4e3a1000HZ\uff0c\u5373\u6bcf\u79d2\u4e2d\u65ad1000\u6b21\uff08\u6bcf1ms\u4ea7\u751f\u4e00\u6b21\uff09\u3002

\u65f6\u949f\u4e2d\u65ad\u9891\u7387\u5e94\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u7406\u60f3\u7684\u503c\uff0c\u9ad8HZ\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u4f7f\u5f97\u7531\u65f6\u95f4\u9a71\u52a8\u7684\u4e8b\u4ef6\u66f4\u4e3a\u7cbe\u786e\u3002\u4f46\u540c\u65f6\u4e5f\u5e26\u6765\u4e86\u989d\u5916\u7684\u7cfb\u7edf\u8d1f\u62c5\uff0c\u56e0\u4e3a\u5904\u7406\u5668\u4f1a\u88ab\u66f4\u9891\u7e41\u5730\u6253\u65ad\u53bb\u6267\u884c\u65f6\u949f\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002

\u5168\u5c40\u53d8\u91cfjiffies\u7528\u6765\u8bb0\u5f55\u7cfb\u7edf\u542f\u52a8\u4ee5\u6765\u4ea7\u751f\u7684\u8282\u62cd\u603b\u6570\u3002\u5185\u6838\u5728\u542f\u52a8\u65f6\u5c06\u8be5\u53d8\u91cf\u521d\u59cb\u5316\u4e3a0\uff0c\u6b64\u540e\u6bcf\u4ea7\u751f\u4e00\u6b21\u65f6\u949f\u4e2d\u65ad\u8be5\u503c\u5c31+1\u3002\u56e0\u4e3a\u4e00\u79d2\u5185\u65f6\u949f\u4e2d\u65ad\u7684\u6b21\u6570\u7b49\u4e8eHZ\uff0c\u6240\u4ee5jiffies\u4e00\u79d2\u5185\u589e\u52a0\u7684\u503c\u4e5f\u5c31\u4e3aHZ\u3002\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4\u4ee5\u79d2\u4e3a\u8ba1\uff0c\u5176\u503c\u7b49\u4e8ejiffies/HZ\u3002

jiffies\u53d8\u91cf\u88ab\u5b9a\u4e49\u4e3aunsigned long\u7c7b\u578b\u3002\u572832\u4f4d\u4f53\u7cfb\u7ed3\u6784\u4e0a\u662f32\u4f4d\u3002\u5982\u679c\u65f6\u949f\u9891\u7387\u4e3a100HZ\uff0c\u90a3\u4e48497\u5929\u540e\u4f1a\u6ea2\u51fa\u3002\u5982\u679c\u662f64\u4f4d\u4f53\u7cfb\u7ed3\u6784\uff0c\u4efb\u4f55\u4eba\u90fd\u522b\u6307\u671b\u4f1a\u770b\u5230\u5b83\u6ea2\u51fa\u3002

\u5f53jiffies\u53d8\u91cf\u7684\u503c\u8d85\u8fc7\u5b83\u7684\u6700\u5927\u5b58\u653e\u8303\u56f4\u540e\u5c31\u4f1a\u53d1\u751f\u6ea2\u51fa\uff0c\u6b64\u65f6\u518d\u7ee7\u7eed\u589e\u52a0\u7684\u8bdd\uff0c\u5b83\u4f1a\u56de\u7ed5\u81f30\u3002\u4e3a\u4e86\u89e3\u51b3jiffies\u53d8\u91cf\u7684\u6ea2\u51fa\u95ee\u9898\uff0c\u5185\u6838\u5f15\u5165\u4e86jiffies_64\u53d8\u91cf\uff0c\u8fd9\u662f\u4e00\u4e2a64\u4f4d\u7684\u65e0\u7b26\u53f7\u6574\u6570\u3002\u4f46\u662f\u7528\u6237\u7a7a\u95f4\u8bbf\u95ee\u7684\u4f9d\u7136\u8fd8\u662fjiffies\u53d8\u91cf\u3002

"},{"location":"linux/kernel/others/#_5","title":"\u52a8\u6001\u5b9a\u65f6\u5668","text":"

\u5b9a\u65f6\u5668\u7684\u4f7f\u7528\u5f88\u7b80\u5355\u3002\u4f60\u53ea\u9700\u8981\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\uff0c\u6307\u5b9a\u8d85\u65f6\u53d1\u751f\u540e\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\uff0c\u7136\u540e\u6fc0\u6d3b\u5b9a\u65f6\u5668\u5c31\u53ef\u4ee5\u4e86\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7ec4\u4e0e\u5b9a\u65f6\u5668\u76f8\u5173\u7684\u63a5\u53e3\u7528\u6765\u7b80\u5316\u5b9a\u65f6\u5668\u7684\u64cd\u4f5c\u3002

\u521b\u5efa\u5b9a\u65f6\u5668\uff1a

struct timer_list my_timer;\n

\u521d\u59cb\u5316\u5b9a\u65f6\u5668\uff1a

init_timer(&my_timer);\n

\u586b\u5145\u5b9a\u65f6\u5668\u7ed3\u6784\u4e2d\u7684\u503c\uff1a

my_timer.expires = jiffies + delay;    //\u5b9a\u65f6\u5668\u8d85\u65f6\u8282\u62cd\u6570\nmy_timer.data = 0;                     //\u5b9a\u65f6\u5668\u5904\u7406\u51fd\u6570\u4f20\u5165\u7684\u503c\nmy_timer.function = my_function;       //\u5b9a\u65f6\u5668\u8d85\u65f6\u56de\u8c03\u51fd\u6570\n

\u5982\u679c\u5f53\u524djiffies\u8ba1\u6570\u5927\u4e8e\u6216\u7b49\u4e8emy_timer.expires\u7684\u503c\uff0c\u90a3\u4e48my_timer.function\u6307\u5411\u7684\u5904\u7406\u51fd\u6570\u5c31\u4f1a\u88ab\u6267\u884c\u3002\u5904\u7406\u51fd\u6570\u5fc5\u987b\u7b26\u5408\u4ee5\u4e0b\u7c7b\u578b\uff1a

void my_function(unsigned long data);\n

\u5982\u679c\u9700\u8981\u901a\u8fc7data\u53c2\u6570\u4f20\u9012\u591a\u4e2a\u6570\u636e\u9879\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6570\u636e\u9879\u6346\u7ed1\u6210\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c06\u7ed3\u6784\u4f53\u7684\u6307\u9488\u5f3a\u5236\u8f6c\u6362\u6210unsigned long\u4f20\u5165\u3002

\u6700\u540e\uff0c\u4f60\u5fc5\u987b\u624b\u52a8\u6fc0\u6d3b\u5b9a\u65f6\u5668\uff1a

add_timer(&my_timer);\n

\u4e00\u822c\u6765\u8bf4\uff0c\u5b9a\u65f6\u5668\u90fd\u4f1a\u5728\u8d85\u65f6\u540e\u9a6c\u4e0a\u6267\u884c\uff0c\u4f46\u4e5f\u6709\u53ef\u80fd\u4f1a\u63a8\u8fdf\u5230\u4e0b\u4e00\u4e2a\u65f6\u949f\u8282\u62cd\u65f6\u624d\u8fd0\u884c\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528\u5b9a\u65f6\u5668\u6765\u5b9e\u73b0\u4efb\u4f55\u786c\u5b9e\u65f6\u7684\u4efb\u52a1\u3002\u5982\u679c\u9700\u8981\u4fee\u6539\u5b9a\u65f6\u5668\u8d85\u65f6\u65f6\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7mod_timer()\u51fd\u6570\u6765\u5b9e\u73b0\uff1a

mod_timer(&my_timer, jiffies + new_delay);\n

\u5982\u679c\u5728\u5b9a\u65f6\u5668\u8d85\u65f6\u524d\u505c\u6b62\u5b9a\u65f6\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528del_timer()\u51fd\u6570\uff1a

del_timer(&my_timer);\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728SMP\u7cfb\u7edf\u4e2d\uff0c\u5220\u9664\u5b9a\u65f6\u5668\u65f6\u53ef\u80fd\u9700\u8981\u7b49\u5f85\u5728\u5176\u4ed6\u5904\u7406\u5668\u4e0a\u8fd0\u884c\u7684\u5b9a\u65f6\u5668\u5904\u7406\u7a0b\u5e8f\u90fd\u9000\u51fa\uff0c\u8fd9\u65f6\u9700\u8981\u7528\u5230del_timer_syn()\u51fd\u6570\u6765\u6267\u884c\u5220\u9664\u5de5\u4f5c\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u5e94\u8be5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u800c\u4e0d\u662fdel_timer()\u3002\u5728\u62e5\u6709\u9501\u7684\u65f6\u5019\uff0c\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u68c0\u67e5\uff0c\u4ee5\u9632\u6b62\u6b7b\u9501\u3002

"},{"location":"linux/kernel/others/#_6","title":"\u5ef6\u8fdf\u6267\u884c","text":"

\u5185\u6838\u4ee3\u7801\u5f80\u5f80\u9700\u8981\u63a8\u8fdf\u67d0\u4e9b\u4efb\u52a1\u7684\u6267\u884c\uff0c\u8fd9\u79cd\u63a8\u8fdf\u901a\u5e38\u53d1\u751f\u5728\u7b49\u5f85\u786c\u4ef6\u5b8c\u6210\u67d0\u4e9b\u5de5\u4f5c\uff0c\u800c\u4e14\u7b49\u5f85\u65f6\u95f4\u5f80\u5f80\u975e\u5e38\u77ed\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u591a\u79cd\u5ef6\u8fdf\u65b9\u6cd5\u6765\u5904\u7406\u5ef6\u8fdf\u8bf7\u6c42\u3002

\u6700\u7b80\u5355\u7684\u5ef6\u8fdf\u65b9\u6cd5\u5c31\u662f\u5fd9\u7b49\u5f85\u3002\u8be5\u65b9\u6cd5\u4ec5\u4ec5\u5728\u60f3\u8981\u5ef6\u8fdf\u7684\u65f6\u95f4\u662f\u8282\u62cd\u7684\u6574\u6570\u500d\uff0c\u6216\u8005\u7cbe\u786e\u7387\u8981\u6c42\u4e0d\u9ad8\u7684\u60c5\u51b5\u4e0b\u624d\u53ef\u4ee5\u4f7f\u7528\u3002\u5fd9\u7b49\u5f85\u7684\u5b9e\u73b0\u975e\u5e38\u7b80\u5355\u2014\u2014\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u4e0d\u65ad\u7b49\u5f85\u76f4\u5230\u5e0c\u671b\u7684\u65f6\u949f\u8282\u62cd\u6570\u8017\u5c3d\uff0c\u6bd4\u5982\uff1a

unsigned long timeout = jiffies + 10;    //\u7b49\u5f8510\u4e2a\u8282\u62cd\nwhile(time_before(jiffies, timeout))\n{}\n

\u8be5\u5faa\u73af\u5c06\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230jiffies\u5927\u4e8edelay\u4e3a\u6b62\u3002\u8fd9\u662f\u4e00\u79cd\u4f4e\u6548\u7684\u529e\u6cd5\uff0c \u56e0\u4e3a\u5904\u7406\u5668\u9664\u4e86\u7b49\u5f85\u4e0d\u4f1a\u505a\u4efb\u4f55\u4e8b\u60c5\uff0c\u66f4\u597d\u7684\u65b9\u6848\u662f\u5728\u7b49\u5f85\u65f6\uff0c\u5141\u8bb8\u5185\u6838\u91cd\u65b0\u8c03\u5ea6\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002

\u57fa\u4e8ejiffies\u7684\u5ef6\u8fdf\u65b9\u6cd5\u53d7\u9650\u4e8e\u65f6\u949f\u8282\u62cd\uff0c\u65e0\u6cd5\u63d0\u4f9b\u66f4\u77ed\u3001\u66f4\u7cbe\u786e\u7684\u5ef6\u8fdf\u8981\u6c42\u3002\u4e3a\u6b64\uff0c\u5185\u6838\u63d0\u4f9b\u4e86\u4e09\u4e2a\u53ef\u4ee5\u5904\u7406us\u3001us\u548cms\u7ea7\u522b\u7684\u77ed\u5ef6\u8fdf\u51fd\u6570\uff1a

void ndelay(unsigned long nsecs)\nvoid udelay(unsigned long usecs)\nvoid mdelay(unsigned long msecs)\n

\u8fd9\u4e9b\u51fd\u6570\u7684\u5b9e\u9645\u5b9e\u73b0\u5728<asm/delay.h>\u4e2d\uff0c\u5f88\u660e\u663e\uff0c\u8fd9\u4e9b\u662f\u4e0e\u4f53\u7cfb\u67b6\u6784\u76f8\u5173\u7684\u51fd\u6570\u3002\u8fd9\u4e09\u4e2a\u51fd\u6570\u90fd\u662f\u5fd9\u7b49\u5f85\u51fd\u6570\uff0c\u5728\u5ef6\u8fdf\u8fc7\u7a0b\u4e2d\u65e0\u6cd5\u8fd0\u884c\u5176\u4ed6\u4efb\u52a1\u3002\u4e0d\u6d89\u53ca\u5fd9\u7b49\u5f85\u7684\u51fd\u6570\u6709\u4ee5\u4e0b\u4e24\u4e2a\uff0c\u5b83\u4eec\u4f1a\u5c06\u5f53\u524d\u8fdb\u7a0b\u7f6e\u5165\u4f11\u7720\u72b6\u6001\uff1a

void msleep(unsigned int millisecs);\nvoid ssleep(unsigned int seconds);\n

\u66f4\u7406\u60f3\u7684\u5ef6\u8fdf\u6267\u884c\u65b9\u6cd5\u662f\u4f7f\u7528schedule_timeout()\u51fd\u6570\uff0c\u8be5\u65b9\u6cd5\u8ba9\u9700\u8981\u5ef6\u8fdf\u6267\u884c\u7684\u4efb\u52a1\u7761\u7720\u5230\u6307\u5b9a\u5ef6\u8fdf\u65f6\u95f4\u8017\u5c3d\u540e\u518d\u91cd\u65b0\u8fd0\u884c\u3002\u5176\u7528\u6cd5\u5982\u4e0b\uff1a

set_current_state(TASK_INTERRUPTIBLE);\nschedule_timeout(s * HZ);\n

\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5f53\u524d\u4efb\u52a1\u63a8\u5165\u53ef\u4e2d\u65ad\u7761\u7720\u961f\u5217\uff0c\u7761\u7720s\u79d2\u540e\u5524\u9192\u3002\u6ce8\u610f\uff0c\u7531\u4e8eschedule_timeout()\u51fd\u6570\u9700\u8981\u8c03\u7528\u8c03\u5ea6\u7a0b\u5e8f\uff0c\u6240\u4ee5\u8c03\u7528\u5b83\u7684\u4ee3\u7801\u5fc5\u987b\u4fdd\u8bc1\u80fd\u591f\u7761\u7720\u3002\u4e5f\u5c31\u662f\u8c03\u7528\u51fd\u6570\u5fc5\u987b\u4f4d\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4e14\u4e0d\u80fd\u6301\u6709\u9501\u3002

\u5f53\u4efb\u52a1\u88ab\u91cd\u65b0\u8c03\u5ea6\u65f6\uff0c\u5c06\u8fd4\u56de\u4ee3\u7801\u8fdb\u5165\u7761\u7720\u524d\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u5982\u679c\u4efb\u52a1\u63d0\u524d\u88ab\u5524\u9192\uff0c\u90a3\u4e48\u5b9a\u65f6\u5668\u88ab\u64a4\u9500\u3002

"},{"location":"linux/kernel/others/#_7","title":"\u9875\u9ad8\u901f\u7f13\u5b58\u4e0e\u9875\u56de\u5199","text":"

\u9875\u9ad8\u901f\u7f13\u5b58\u662f\u4e00\u79cd\u628a\u78c1\u76d8\u7684\u6570\u636e\u7f13\u5b58\u5230\u7269\u7406\u5185\u5b58\u4e2d\u7684\u673a\u5236\uff0c\u4f7f\u5f97\u5bf9\u78c1\u76d8\u7684\u8bbf\u95ee\u53d8\u4e3a\u5bf9\u7269\u7406\u5185\u5b58\u7684\u8bbf\u95ee\u3002\u9ad8\u901f\u7f13\u5b58\u7684\u91cd\u8981\u6027\u4f53\u73b0\u5728\uff1a

  1. \u8bbf\u95ee\u78c1\u76d8\u7684\u901f\u5ea6\u8fdc\u8fdc\u4f4e\u4e8e\u8bbf\u95ee\u5185\u5b58\u7684\u901f\u5ea6\u3002

  2. \u6570\u636e\u4e00\u65e6\u88ab\u8bbf\u95ee\uff0c\u5c31\u5f88\u6709\u53ef\u80fd\u5728\u77ed\u671f\u5185\u518d\u6b21\u88ab\u8bbf\u95ee\u3002\u8fd9\u5c31\u662f\u5c40\u90e8\u6027\u539f\u7406\u3002

\u5f53\u5185\u6838\u5f00\u59cb\u4e00\u4e2a\u8bfb\u64cd\u4f5c\uff0c\u5b83\u9996\u5148\u4f1a\u68c0\u67e5\u9700\u8981\u7684\u6570\u636e\u662f\u5426\u5728\u9875\u9ad8\u901f\u7f13\u5b58\u4e2d\u3002\u5982\u679c\u5728\uff0c\u653e\u5f03\u8bbf\u95ee\u78c1\u76d8\uff0c\u800c\u76f4\u63a5\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\u3002\u8fd9\u4e2a\u884c\u4e3a\u79f0\u4e3a\u7f13\u5b58\u547d\u4e2d\uff08cache hit\uff09\u3002\u5426\u5219\u79f0\u4e3a\u7f13\u5b58\u672a\u547d\u4e2d\uff08cache miss\uff09\uff0c\u90a3\u4e48\u5185\u6838\u5fc5\u987b\u8c03\u5ea6\u5757I/O\u64cd\u4f5c\u53bb\u78c1\u76d8\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u7136\u540e\u5c06\u6570\u636e\u653e\u5165\u9875\u7f13\u5b58\u4e2d\u3002\u5185\u6838\u91c7\u7528\u9884\u8bfb\u673a\u5236\uff1a\u5373\u5f53\u4ece\u78c1\u76d8\u4e2d\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u7531\u4e8e\u78c1\u76d8\u7684\u8bfb\u5199\u5355\u4f4d\u4e3ablock\uff084KB\uff09\uff0c\u51fa\u4e8e\u7a7a\u95f4\u5c40\u90e8\u6027\u539f\u7406\uff0c\u5185\u6838\u4f1a\u5c06\u591a\u4e2ablock\u7684\u6570\u636e\u90fd\u52a0\u5165\u5230\u7f13\u5b58\u4e2d\u3002

"},{"location":"linux/kernel/others/#_8","title":"\u5199\u7f13\u5b58\u7b56\u7565","text":"

\u5728\u8fdb\u7a0b\u5199\u78c1\u76d8\u65f6\uff0c\u7f13\u5b58\u7684\u4f7f\u7528\u4e00\u822c\u6709\u4ee5\u4e0b\u4e09\u79cd\uff1a

  1. \u4e0d\u7f13\u5b58\uff1a\u4e5f\u5c31\u662f\u8bf4\u9ad8\u901f\u7f13\u5b58\u4e0d\u7f13\u5b58\u4efb\u4f55\u5199\u64cd\u4f5c\uff0c\u800c\u662f\u76f4\u63a5\u5199\u5165\u78c1\u76d8\u3002\u8fd9\u79cd\u65b9\u5f0f\u5f88\u5c11\u91c7\u7528\uff0c\u56e0\u4e3a\u6027\u80fd\u975e\u5e38\u5dee\u3002

  2. \u5199\u64cd\u4f5c\u81ea\u52a8\u66f4\u65b0\u7f13\u5b58\uff0c\u540c\u65f6\u4e5f\u66f4\u65b0\u78c1\u76d8\u6587\u4ef6\uff1a\u8fd9\u79cd\u7b56\u7565\u5bf9\u7f13\u5b58\u4e00\u81f4\u6027\u975e\u5e38\u6709\u6548\uff0c\u56e0\u4e3a\u5185\u5b58\u7f13\u5b58\u59cb\u7ec8\u548c\u78c1\u76d8\u4e00\u81f4\u3002

  3. Linux\u91c7\u7528\u7684\u2014\u2014\u56de\u5199\uff1a\u5199\u64cd\u4f5c\u5199\u5165\u7f13\u5b58\uff0c\u4f46\u662f\u78c1\u76d8\u5e76\u4e0d\u7acb\u523b\u66f4\u65b0\uff0c\u800c\u662f\u5c06\u9875\u9ad8\u901f\u7f13\u5b58\u4e2d\u88ab\u5199\u5165\u7684\u9875\u9762\u6807\u8bb0\u4e3a\u201c\u810f\u201d\uff0c\u7136\u540e\u52a0\u5165\u5230\u810f\u9875\u5217\u8868\u4e2d\u3002\u7531\u4e00\u4e2a\u56de\u5199\u8fdb\u7a0b\u5468\u671f\u6027\u7684\u5c06\u810f\u9875\u94fe\u8868\u4e2d\u7684\u9875\u56de\u5199\u81f3\u78c1\u76d8\uff0c\u6700\u540e\u6e05\u7406\u810f\u9875\u6807\u8bc6\u3002

"},{"location":"linux/kernel/others/#_9","title":"\u7f13\u5b58\u56de\u6536\u7b56\u7565","text":"

\u7531\u4e8e\u5927\u91cf\u7684\u9875\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5fc5\u7136\u5bfc\u81f4\u53ef\u7528\u5185\u5b58\u7684\u4e0b\u964d\u3002\u4e3a\u4e86\u4fdd\u8bc1\u5185\u5b58\u7684\u6b63\u5e38\u4f7f\u7528\uff0c\u5fc5\u987b\u6709\u4e00\u79cd\u673a\u5236\u53ef\u4ee5\u56de\u6536\u7f13\u5b58\u3002Linux\u7684\u7f13\u5b58\u56de\u6536\u662f\u5979\u901a\u8fc7\u9009\u62e9\u5e72\u51c0\u9875\u8fdb\u884c\u7b80\u5355\u66ff\u6362\u3002\u5982\u679c\u7f13\u5b58\u4e2d\u6ca1\u6709\u8db3\u591f\u7684\u5e72\u51c0\u9875\uff0c\u5185\u6838\u5c06\u5f3a\u5236\u6027\u7684\u8fdb\u884c\u56de\u5199\u64cd\u4f5c\uff0c\u4ee5\u817e\u51fa\u66f4\u591a\u7684\u5e72\u51c0\u9875\u4f7f\u7528\u3002

\u4e00\u79cd\u56de\u6536\u7b56\u7565\u88ab\u79f0\u4e3a\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7b97\u6cd5\uff0c\u7b80\u79f0LRU\uff08Least Recently Used\uff09\u3002LRU\u7b97\u6cd5\u4e00\u822c\u662f\u7528\u94fe\u8868\u4f5c\u4e3a\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u7684\uff0c\u94fe\u8868\u5934\u90e8\u7684\u6570\u636e\u662f\u6700\u8fd1\u4f7f\u7528\u7684\uff0c\u800c\u94fe\u8868\u672b\u5c3e\u7684\u6570\u636e\u662f\u6700\u4e45\u6ca1\u88ab\u4f7f\u7528\u7684\u3002\u5f53\u7a7a\u95f4\u4e0d\u591f\u65f6\uff0c\u5c31\u6dd8\u6c70\u6700\u4e45\u6ca1\u88ab\u4f7f\u7528\u7684\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u94fe\u8868\u672b\u5c3e\u7684\u6570\u636e\uff0c\u4ece\u800c\u817e\u51fa\u5185\u5b58\u7a7a\u95f4\u3002\u4f20\u7edf\u7684LRU\u7b97\u6cd5\u5bf9\u4e8e\u90a3\u4e9b\u53ea\u4f1a\u88ab\u8bbf\u95ee\u4e00\u6b21\u7684\u6587\u4ef6\u6548\u679c\u5c24\u5176\u5dee\u3002

Linux\u5185\u6838\u5bf9LRU\u7b97\u6cd5\u8fdb\u884c\u4e86\u6539\u8fdb\uff0c\u88ab\u79f0\u4e3a\u53cc\u94fe\u7b56\u7565\u2014\u2014\u7ef4\u62a4\u4e24\u4e2a\u94fe\u8868\uff1a\u6d3b\u8dc3\u94fe\u8868\uff08active list\uff09\u548c\u4e0d\u6d3b\u8dc3\u94fe\u8868\uff08inactive list\uff09\u3002\u5904\u4e8e\u6d3b\u8dc3\u94fe\u8868\u4e0a\u7684\u9875\u88ab\u8ba4\u4e3a\u662f\u201c\u70ed\u201d\u7684\u4e14\u4e0d\u4f1a\u6362\u51fa\uff0c\u5904\u4e8e\u4e0d\u6d3b\u8dc3\u94fe\u8868\u4e0a\u7684\u9875\u88ab\u8ba4\u4e3a\u662f\u201c\u51b7\u201d\u7684\u53ef\u4ee5\u88ab\u6362\u51fa\u3002\u9875\u9762\u4ece\u5c3e\u90e8\u52a0\u5165\uff0c\u4ece\u5934\u90e8\u79fb\u9664\uff0c\u5c31\u50cf\u961f\u5217\u4e00\u6837\u3002\u6709\u4e86\u8fd9\u4e24\u4e2aLRU\u94fe\u8868\u540e\uff0c\u9884\u8bfb\u9875\u5c31\u53ea\u9700\u8981\u52a0\u5165\u5230\u4e0d\u6d3b\u8dc3\u94fe\u8868\u7684\u5934\u90e8\uff0c\u5f53\u9875\u88ab\u771f\u6b63\u8bbf\u95ee\u7684\u65f6\u5019\uff0c\u624d\u5c06\u9875\u63d2\u5165\u5230\u6d3b\u8dc3\u94fe\u8868\u7684\u5934\u90e8\u3002\u5982\u679c\u9884\u8bfb\u7684\u9875\u4e00\u76f4\u6ca1\u6709\u88ab\u8bbf\u95ee\uff0c\u5c31\u4f1a\u4ece\u4e0d\u6d3b\u8dc3\u94fe\u8868\u4e2d\u79fb\u9664\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u5f71\u54cd\u6d3b\u8dc3\u94fe\u8868\u4e2d\u7684\u70ed\u6570\u636e\u3002

"},{"location":"linux/kernel/others/#_10","title":"\u53ef\u79fb\u690d\u6027","text":""},{"location":"linux/kernel/sched/","title":"\u8fdb\u7a0b\u7ba1\u7406\u4e0e\u8c03\u5ea6","text":"

\u8fdb\u7a0b\u5c31\u662f\u5904\u4e8e\u6267\u884c\u671f\u7684\u7a0b\u5e8f\uff0c\u5f53\u6211\u4eec\u5c06\u4ee3\u7801\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u8f93\u5165./a.out\uff0c\u6211\u4eec\u5c31\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u3002\u8fdb\u7a0b\u4ece\u78c1\u76d8\u52a0\u8f7d\u5230\u5185\u5b58\u7684\u8fc7\u7a0b\u8bf7\u53c2\u8003\u7a0b\u5e8f\u7684\u88c5\u8f7d\u3002

\u53ef\u4ee5\u628a\u4e00\u4e2a\u8fdb\u7a0b\u60f3\u8c61\u6210\u4e00\u4e2a\u5e9e\u5927\u7684\u9879\u76ee\u7ec4\uff0c\u5b83\u5305\u542b\u4e86\u5404\u79cd\u8d44\u6e90\uff0c\u4f8b\u5982\u6253\u5f00\u7684\u6587\u4ef6\u3001\u5360\u7528\u7684\u5185\u5b58\u3001\u5904\u7406\u5668\u7684\u72b6\u6001\u7b49\u3002\u800c\u7ebf\u7a0b\uff0c\u5c31\u50cf\u662f\u9879\u76ee\u7ec4\u4e2d\u8d1f\u8d23\u6267\u884c\u5177\u4f53\u4efb\u52a1\u7684\u4e2a\u4eba\u3002\u6b63\u5982\u6bcf\u4e2a\u9879\u76ee\u7ec4\u81f3\u5c11\u9700\u8981\u4e00\u4e2a\u4eba\u6765\u6267\u884c\u4efb\u52a1\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u5c06\u6240\u6709\u4efb\u52a1\u90fd\u4ea4\u7ed9\u4e3b\u7ebf\u7a0b\u6765\u5b8c\u6210\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u628a\u4e00\u4e2a\u5927\u7684\u4efb\u52a1\u5206\u89e3\u6210\u591a\u4e2a\u5c0f\u4efb\u52a1\uff0c\u5e76\u521b\u5efa\u989d\u5916\u7684\u7ebf\u7a0b\u6765\u5e76\u884c\u5904\u7406\u8fd9\u4e9b\u5c0f\u4efb\u52a1\u3002\u90a3\u4e48\uff0c\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u521b\u5efa\u66f4\u591a\u7684\u8fdb\u7a0b\u6765\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u5462\uff1f\u4ece\u6280\u672f\u4e0a\u6765\u8bf4\uff0c\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u8fd9\u901a\u5e38\u5e76\u4e0d\u662f\u6700\u4f73\u9009\u62e9\u3002\u9996\u5148\uff0c\u8fdb\u7a0b\u5360\u7528\u7684\u8d44\u6e90\u6bd4\u7ebf\u7a0b\u591a\uff0c\u800c\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u521b\u5efa\u7684\u901f\u5ea6\u8981\u5feb\u5f97\u591a\u3002\u5176\u6b21\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u8d44\u6e90\u5171\u4eab\u76f8\u5bf9\u590d\u6742\uff0c\u800c\u7ebf\u7a0b\u7531\u4e8e\u5171\u4eab\u540c\u4e00\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u8bbf\u95ee\u8fd9\u4e9b\u8d44\u6e90\u3002\u8fd9\u5c31\u50cf\u662f\u5728\u4e00\u4e2a\u516c\u53f8\u7684\u4e0d\u540c\u9879\u76ee\u7ec4\u4e4b\u95f4\u8fdb\u884c\u5de5\u4f5c\u4ea4\u63a5\u53ef\u80fd\u4f1a\u5f88\u9ebb\u70e6\uff0c\u4f46\u5982\u679c\u5927\u5bb6\u90fd\u5c5e\u4e8e\u540c\u4e00\u4e2a\u9879\u76ee\u7ec4\uff0c\u5408\u4f5c\u5c31\u4f1a\u987a\u7545\u5f97\u591a\u3002

"},{"location":"linux/kernel/sched/#_2","title":"\u8fdb\u7a0b\u63cf\u8ff0\u7b26","text":"

\u4e3a\u4e86\u7ba1\u7406\u8fdb\u7a0b\uff0c\u5185\u6838\u5fc5\u987b\u6e05\u6670\u5730\u63cf\u8ff0\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u3002\u5728Linux\u7cfb\u7edf\u4e0b\uff0c\u4e0d\u7ba1\u662f\u8fdb\u7a0b\u8fd8\u662f\u7ebf\u7a0b\uff0c\u5185\u6838\u7edf\u4e00\u7528task_struct\u7ed3\u6784\u4f53\u7ba1\u7406\u3002

"},{"location":"linux/kernel/sched/#_3","title":"\u5185\u6838\u6808","text":"
void *stack;\n

\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u4e13\u7528\u7684\u5185\u6838\u6808\uff0c\u7528\u4e8e\u4fdd\u5b58\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u6267\u884c\u65f6\u7684\u4e34\u65f6\u6570\u636e\u548c\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5f53\u521b\u5efa\u65b0\u8fdb\u7a0b\u65f6\uff0c\u5185\u6838\u4f1a\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u5408\u9002\u5927\u5c0f\u7684\u5185\u6838\u6808\u7a7a\u95f4\uff0c\u5e76\u5c06\u5176\u5730\u5740\u8d4b\u7ed9task_struct\u4e2d\u7684stack\u6210\u5458\u3002

"},{"location":"linux/kernel/sched/#_4","title":"\u7ebf\u7a0b\u63cf\u8ff0\u7b26","text":"

\u7ebf\u7a0b\u63cf\u8ff0\u7b26thread_info\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u653e\u7ebf\u7a0b\u76f8\u5173\u4fe1\u606f\u7684\u7ed3\u6784\u4f53\uff0c\u5b83\u5305\u542b\u4e86\u5f88\u591a\u4e0e\u7ebf\u7a0b\u76f8\u5173\u7684\u5b57\u6bb5\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u662ftask_struct *task\uff0c\u5b83\u6307\u5411\u5f53\u524d\u7ebf\u7a0b\u6240\u5c5e\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u3002\u4e3a\u4e86\u65b9\u4fbf\u8bbf\u95ee\u5f53\u524dCPU\u4e0a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\u7684task_struct\u7ed3\u6784\uff0c\u5185\u6838\u63d0\u4f9b\u4e86current\u5b8f\uff0c\u8be5\u5b8f\u672c\u8d28\u4e0a\u7b49\u4e8e\uff1a

current_thread_info()->task\n

\u4e0a\u8ff0\u4ee3\u7801\u8fd4\u56de\u5f53\u524dtask_struct\u7684\u5730\u5740\u3002

"},{"location":"linux/kernel/sched/#_5","title":"\u8fdb\u7a0b\u72b6\u6001","text":"

\u5728task_struct\u4e2d\uff0c\u6d89\u53ca\u5230\u8fdb\u7a0b\u72b6\u6001\u7684\u662f\u8fd9\u51e0\u4e2a\u6210\u5458\uff1a

 volatile long state;   \n int exit_state;\n unsigned int flags;\n

state\u53ef\u4ee5\u53d6\u7684\u503c\u6709\uff1a

/* Used in tsk->state: */\n#define TASK_RUNNING                    0\n#define TASK_INTERRUPTIBLE              1\n#define TASK_UNINTERRUPTIBLE            2\n#define __TASK_STOPPED                  4\n#define __TASK_TRACED                   8\n/* Used in tsk->exit_state: */\n#define EXIT_DEAD                       16\n#define EXIT_ZOMBIE                     32\n#define EXIT_TRACE                      (EXIT_ZOMBIE | EXIT_DEAD)\n/* Used in tsk->state again: */\n#define TASK_DEAD                       64\n#define TASK_WAKEKILL                   128\n#define TASK_WAKING                     256\n#define TASK_PARKED                     512\n#define TASK_NOLOAD                     1024\n#define TASK_NEW                        2048\n#define TASK_STATE_MAX                  4096\n

state\u5b57\u6bb5\u7684\u503c\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u8bbe\u7f6e\u3002

p->state = TASK_RUNNING;\n

\u4e5f\u53ef\u4ee5\u4f7f\u7528set_task_state\u548cset_current_state\u5b8f\uff1a\u5b83\u4eec\u5206\u522b\u8bbe\u7f6e\u6307\u5b9a\u8fdb\u7a0b\u7684\u72b6\u6001\u548c\u5f53\u524d\u6267\u884c\u8fdb\u7a0b\u7684\u72b6\u6001\u3002

TASK_RUNNING\u8868\u793a\u8fdb\u7a0b\u6b63\u5728\u6267\u884c\u6216\u8005\u51c6\u5907\u6267\u884c\u3002

\u5f53\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u540e\uff0c\u6709\u4e24\u79cd\u72b6\u6001\uff1a

TASK_INTERRUPTIBLE\u8868\u793a\u8fdb\u7a0b\u53ef\u4ee5\u54cd\u5e94\u4fe1\u53f7\u3002\u4e5f\u5c31\u662f\u5f53\u4e00\u4e2a\u4fe1\u53f7\u6765\u4e34\u65f6\uff0c\u5524\u9192\u8be5\u8fdb\u7a0b\uff0c\u7136\u540e\u6267\u884c\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002

TASK_UNINTERRUPTIBLE\u8868\u793a\u8fdb\u7a0b\u4e0d\u53ef\u4ee5\u54cd\u5e94\u4fe1\u53f7\u3002\u5f53\u8fdb\u7a0b\u7b49\u5f85\u67d0\u4e2a\u4e8b\u4ef6\u5b8c\u6210\u800c\u8fdb\u5165\u6df1\u5ea6\u7761\u7720\u72b6\u6001\u65f6\uff0c\u5982\u679c\u8be5\u4e8b\u4ef6\u56e0\u4e3a\u67d0\u79cd\u539f\u56e0\u65e0\u6cd5\u5b8c\u6210\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8fdb\u7a0b\u4e5f\u5c06\u6c38\u8fdc\u7761\u7720\u4e0b\u53bb\u3002\u6700\u5178\u578b\u7684\u5c31\u662f\u7b49\u5f85\u78c1\u76d8I/O\u3002

\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5371\u9669\u7684\u4e8b\u60c5\uff0c\u9664\u975e\u5f88\u6709\u628a\u63e1\u4e0d\u7136\u4e0d\u4f1a\u8ba9\u8fdb\u7a0b\u8fdb\u5165\u6df1\u5ea6\u7761\u7720\u6001\u3002

\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u4e86\u4e00\u79cd\u65b0\u7684\u72b6\u6001\uff1aTASK_KILLABLE\uff0c\u53ef\u4ee5\u7ec8\u6b62\u7684\u6df1\u5ea6\u7761\u7720\u6001\u3002\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNITERRUPTIBLE)\n

\u5176\u4e2dTASK_WAKEKILL\u8868\u793a\u5728\u63a5\u6536\u5230\u81f4\u547d\u4fe1\u53f7\u65f6\uff0c\u5524\u9192\u8fdb\u7a0b\u3002

TASK_STOPPED\u8868\u793a\u8fdb\u7a0b\u88ab\u6682\u505c\uff0c\u5f53\u63a5\u6536\u5230SIGSTOP\u3001SIGTSTP\u3001SIGTTIN\u6216SIGTTOU\u4fe1\u53f7\u540e\uff0c\u8fdb\u5165\u8be5\u72b6\u6001\u3002

TASK_TRACED\u8868\u793a\u8fdb\u7a0b\u88ab\u8ddf\u8e2a\u3002\u8fd9\u5f80\u5f80\u662f\u8c03\u8bd5\u7a0b\u5e8f\u6682\u65f6\u7ec8\u6b62\u4e86\u8fdb\u7a0b\u7684\u8fd0\u884c\u3002

\u5185\u6838\u7ecf\u5e38\u9700\u8981\u8c03\u6574\u5f53\u524d\u8fdb\u7a0b\u7684\u72b6\u6001\uff0c\u53ef\u4ee5\u4f7f\u7528set_task_state(task, state)\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u6307\u5b9a\u8fdb\u7a0b\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u72b6\u6001\u3002

exit_state\u7684\u53d6\u503c\u6709\u4e24\u79cd\uff1a

EXIT_ZOMBIE\u8868\u793a\u50f5\u6b7b\u72b6\u6001\u3002\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c\u88ab\u7ec8\u6b62\uff0c\u4f46\u662f\u5176\u7236\u8fdb\u7a0b\u8fd8\u6ca1\u6709\u8c03\u7528wait4()\u6216\u8005waitpid()\u7cfb\u7edf\u8c03\u7528\u6765\u56de\u6536\u8d44\u6e90\u65f6\uff0c\u8fdb\u5165\u8be5\u72b6\u6001\u3002

EXIT_DEAD\u662f\u8fdb\u7a0b\u7684\u6700\u7ec8\u72b6\u6001\u3002\u7236\u8fdb\u7a0b\u56de\u6536\u5b50\u8fdb\u7a0b\u8d44\u6e90\u4e4b\u540e\uff0c\u8fdb\u7a0b\u7531\u7cfb\u7edf\u5220\u9664\u3002

\u8fdb\u7a0b\u7684\u4e00\u4e9b\u5176\u4ed6\u72b6\u6001\u4fe1\u606f\u7531flags\u63a7\u5236\uff0c\u4f7f\u7528\u4f4d\u63a9\u7801\u6765\u8868\u793a\u4e0d\u540c\u7684\u6807\u8bb0\u3002\u4e00\u4e9b\u5e38\u89c1\u7684\u6807\u8bb0\u5305\u62ec\uff1a

  • \u4efb\u52a1\u72b6\u6001\uff1a\u4f8b\u5982\u662f\u5426\u6b63\u5728\u8fd0\u884c\u3001\u505c\u6b62\u7b49
  • \u8c03\u5ea6\u5668\u76f8\u5173\uff1a\u4f8b\u5982\u8c03\u5ea6\u7b56\u7565\u3001\u4f18\u5148\u7ea7\u7b49
  • \u4fe1\u53f7\u5904\u7406\uff1a\u4f8b\u5982\u6302\u8d77\u4fe1\u53f7\u96c6
  • \u8fdb\u7a0b\u7279\u6027\uff1a\u662f\u5426\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u3001\u662f\u5426\u5141\u8bb8\u88ab\u8ffd\u8e2a\u7b49

flags\u6210\u5458\u7684\u4e00\u4e9b\u53d6\u503c\u4e3e\u4f8b\u5982\u4e0b\uff0c\u8fd9\u4e9b\u5b8f\u4ee5PF\u5f00\u5934\uff1a

#define PF_EXITING 0x00000004    //getting shut down\n#define PF_WQ_WORKER 0x00000020  //I'm a workqueue worker\n#define PF_KTHREAD 0x00200000    //I'm a kernel thread\n
"},{"location":"linux/kernel/sched/#_6","title":"\u8fdb\u7a0b\u6807\u8bc6","text":"

Linux\u7cfb\u7edf\u4f7f\u7528PID\u6765\u6807\u8bc6\u4e00\u4e2a\u8fdb\u7a0b\uff0cPID\u88ab\u987a\u5e8f\u7f16\u53f7\uff0c\u65b0\u521b\u5efa\u7684\u8fdb\u7a0b\u7684PID\u901a\u5e38\u662f\u524d\u4e00\u4e2a\u8fdb\u7a0b+1\u3002\u4f46\u662fPID\u7684\u503c\u6709\u4e00\u4e2a\u4e0a\u9650\uff0c\u7cfb\u7edf\u7ba1\u7406\u5458\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539/proc/sys/kernel/pid_max\u6587\u4ef6\u7684\u503c\u6765\u6539\u53d8\u8fd9\u4e2a\u4e0a\u9650\u3002\u8fd9\u4e48\u4e00\u770b\u4f3c\u4e4e\u4e00\u4e2aPID\u6765\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u8fdb\u7a0b\u5c31\u591f\u4e86\uff0c\u4f46\u662f\u5728task_struct\u4e2d\uff0c\u6d89\u53ca\u5230\u8fdb\u7a0b\u6807\u8bc6\u7684\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6210\u5458\u3002

pid_t pid;\npid_t tgid;\nstruct task_struct *group_leader; \n

\u4f60\u53ef\u80fd\u4f1a\u7591\u60d1\uff0c\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u7528\u5230\u8fd9\u4e48\u591a\u53d8\u91cf\uff1f\u8fd9\u662f\u56e0\u4e3a\u4e4b\u524d\u63d0\u5230\uff0c\u5728Linux\u7cfb\u7edf\u4e0b\uff0c\u8fdb\u7a0b\u4e0e\u7ebf\u7a0b\u5e76\u4e0d\u7279\u522b\u533a\u5206\uff0c\u90fd\u662f\u4e00\u4e2atask_struct\u7ed3\u6784\u4f53\u3002\u8fd9\u5c31\u7ed9\u7ba1\u7406\u5e26\u6765\u4e86\u4e00\u4e9b\u9ebb\u70e6\u3002\u6bd4\u5982\u6211\u4eec\u5e0c\u671b\u540c\u5c5e\u4e8e\u4e00\u4e2a\u8fdb\u7a0b\u7ec4\u7684\u7ebf\u7a0b\u62e5\u6709\u5171\u540c\u7684pid\u3002\u5f53\u6211\u4eec\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u7ed9\u6307\u5b9apid\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u8fd9\u4e2a\u4fe1\u53f7\u80fd\u4f5c\u7528\u4e8e\u8be5\u7ec4\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\u3002\u5f53\u6740\u6b7b\u67d0\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u5f53\u7136\u662f\u8fde\u540c\u8be5\u8fdb\u7a0b\u4e0b\u7684\u6240\u6709\u7ebf\u7a0b\u4e00\u8d77\u6740\u6b7b\uff0c\u800c\u4e0d\u662f\u53ea\u6740\u6b7b\u4e86\u4e3b\u8fdb\u7a0b\uff0c\u5176\u4ed6\u7ebf\u7a0b\u8fd8\u5728\u5de5\u4f5c\uff0c\u8fd9\u663e\u7136\u4e0d\u5408\u5e38\u7406\u3002\u4e8b\u5b9e\u4e0a\uff0cPOSIX\u6807\u51c6\u4e5f\u89c4\u5b9a\u4e00\u4e2a\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\u7684\u6240\u6709\u7ebf\u7a0b\u5fc5\u987b\u4eab\u6709\u5171\u540c\u7684pid\u3002

\u9075\u7167\u8fd9\u4e2a\u6807\u51c6\uff0cLinux\u5f15\u5165\u4e86\u7ebf\u7a0b\u7ec4\u7684\u6982\u5ff5\u3002\u4e00\u4e2a\u7ebf\u7a0b\u7ec4\u4e2d\u6240\u6709\u7ebf\u7a0b\u4f7f\u7528\u548c\u8be5\u7ebf\u7a0b\u7ec4\u9886\u5934\u7ebf\u7a0b\uff08thread group leader\uff09\u76f8\u540c\u7684pid\uff0c\u5b83\u88ab\u5b58\u5165task_struct\u7684tgid\u5b57\u6bb5\u3002\u5f53\u6211\u4eec\u4f7f\u7528getpid()\u7cfb\u7edf\u8c03\u7528\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u7684pid\u65f6\uff0c\u8fd4\u56de\u7684\u4e5f\u662ftgid\u7684\u503c\u3002\u4efb\u4f55\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5982\u679c\u53ea\u6709\u4e3b\u7ebf\u7a0b\uff0c\u90a3\u4e48pid = tgid\uff0c group_leader\u6307\u5411\u7684\u5c31\u662f\u81ea\u5df1\u3002\u5982\u679c\u4e3b\u7ebf\u7a0b\u521b\u5efa\u4e86\u5176\u4ed6\u7ebf\u7a0b\uff0c\u90a3\u4e48\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684pid\uff0c\u4f46\u662ftgid\u4ecd\u7136\u662f\u4e3b\u7ebf\u7a0b\u3002

"},{"location":"linux/kernel/sched/#_7","title":"\u8fdb\u7a0b\u95f4\u5173\u7cfb","text":"

Linux\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u8fdb\u7a0b\u90fd\u662fPID\u4e3a1\u7684init\u8fdb\u7a0b\u7684\u540e\u4ee3\uff0c\u5185\u6838\u5728\u7cfb\u7edf\u542f\u52a8\u7684\u6700\u540e\u9636\u6bb5\u542f\u52a8init\u8fdb\u7a0b\u3002\u8be5\u8fdb\u7a0b\u8bfb\u53d6\u7cfb\u7edf\u7684\u521d\u59cb\u5316\u811a\u672c\u5e76\u6267\u884c\u5176\u4ed6\u7684\u76f8\u5173\u7a0b\u5e8f\uff0c\u6700\u540e\u5b8c\u6210\u6574\u4e2a\u7cfb\u7edf\u7684\u542f\u52a8\u3002

\u6bcf\u4e2a\u8fdb\u7a0b\u5fc5\u6709\u4e00\u4e2a\u7236\u8fdb\u7a0b\uff0c\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u521b\u5efa\u4e86\u591a\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u8fd8\u6709\u5144\u5f1f\u5173\u7cfb\u3002\u5728task_struct\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u5b57\u6bb5\u6765\u8868\u793a\u4eb2\u7f18\u5173\u7cfb\uff1a

struct task_struct __rcu *real_parent; /* real parent process */\nstruct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */\nstruct list_head children;      /* list of my children */\nstruct list_head sibling;       /* linkage in my parent's children list */\n

\u8fd9\u4e9b\u5b57\u6bb5\u6839\u636e\u540d\u5b57\u5c31\u80fd\u731c\u5230\u662f\u4ec0\u4e48\u610f\u601d\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0breal_parent\u548cparent\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5f53\u6211\u4eec\u542f\u52a8GDB\u76d1\u89c6\u67d0\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u8be5\u8fdb\u7a0b\u7684real_parent\u4e0d\u53d8\uff0c\u4f46\u662fparent\u53d8\u6210\u4e86GDB\u3002

\u8fdb\u7a0b\u4e4b\u95f4\u8fd8\u6709\u5176\u4ed6\u5173\u7cfb\uff0c\u8fd9\u91cc\u4e0d\u8be6\u7ec6\u5c55\u5f00\u3002

"},{"location":"linux/kernel/sched/#_8","title":"\u8fd0\u884c\u7edf\u8ba1","text":"
u64        utime;//\u7528\u6237\u6001\u6d88\u8017\u7684CPU\u65f6\u95f4\nu64        stime;//\u5185\u6838\u6001\u6d88\u8017\u7684CPU\u65f6\u95f4\nunsigned long      nvcsw;//\u81ea\u613f(voluntary)\u4e0a\u4e0b\u6587\u5207\u6362\u8ba1\u6570\nunsigned long      nivcsw;//\u975e\u81ea\u613f(involuntary)\u4e0a\u4e0b\u6587\u5207\u6362\u8ba1\u6570\nu64        start_time;//\u8fdb\u7a0b\u542f\u52a8\u65f6\u95f4\uff0c\u4e0d\u5305\u542b\u7761\u7720\u65f6\u95f4\nu64        real_start_time;//\u8fdb\u7a0b\u542f\u52a8\u65f6\u95f4\uff0c\u5305\u542b\u7761\u7720\u65f6\u95f4\n
"},{"location":"linux/kernel/sched/#_9","title":"\u5185\u5b58\u7ba1\u7406","text":"
struct mm_struct *mm;        \nstruct mm_struct *active_mm;            \n
"},{"location":"linux/kernel/sched/#_10","title":"\u6587\u4ef6\u4e0e\u6587\u4ef6\u7cfb\u7edf","text":"
/* Filesystem information: */\nstruct fs_struct *fs;\n/* Open file information: */\nstruct files_struct *files;\n

\u8be6\u60c5\u8bf7\u53c2\u8003\uff1a\u865a\u62df\u6587\u4ef6\u7cfb\u7edf

"},{"location":"linux/kernel/sched/#_11","title":"\u8fdb\u7a0b\u7ec4\u7ec7\u5f62\u5f0f","text":"

\u8981\u660e\u767d\u8fdb\u7a0b\u4e4b\u95f4\u662f\u5982\u4f55\u7ec4\u7ec7\u7684\uff0c\u9996\u5148\u8981\u77e5\u9053\u53cc\u5411\u94fe\u8868\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u3002\u53cc\u5411\u94fe\u8868\u5c31\u662f\u540c\u65f6\u6709prev\u548cnext\u6307\u9488\u7684\u94fe\u8868\uff0c\u5206\u522b\u6307\u5411\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u5143\u7d20\u3002Linux\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5c06\u6240\u6709\u8fdb\u7a0b\u7684\u63cf\u8ff0\u7b26\u5168\u90e8\u8fde\u63a5\u8d77\u6765\u3002

"},{"location":"linux/kernel/sched/#_12","title":"\u8fd0\u884c\u961f\u5217","text":"

\u5f53\u5185\u6838\u9700\u8981\u5bfb\u627e\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u8fd0\u884c\u65f6\uff0c\u5fc5\u987b\u53ea\u8003\u8651\u5df2\u5904\u4e8eTASK_RUNNING\u72b6\u6001\u7684\u8fdb\u7a0b\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u8fd0\u884c\u961f\u5217\u3002\u4e3a\u4e86\u63d0\u9ad8\u8c03\u5ea6\u7a0b\u5e8f\u7684\u8fd0\u884c\u901f\u5ea6\uff0c\u5185\u6838\u4e3a\u6bcf\u4e2a\u4f18\u5148\u7ea7\u90fd\u7ef4\u62a4\u4e86\u4e00\u4e2a\u94fe\u8868\u3002\u5728\u591a\u5904\u7406\u5668\u4e2d\uff0c\u6bcf\u4e2aCPU\u90fd\u6709\u81ea\u5df1\u7684\u8fd0\u884c\u961f\u5217\u3002\u8fd0\u884c\u961f\u5217\u662fLinux\u8c03\u5ea6\u7b97\u6cd5\u7684\u57fa\u7840\u3002\u66f4\u8be6\u7ec6\u7684\u5185\u5bb9\u8bf7\u53c2\u8003\u8fdb\u7a0b\u8c03\u5ea6\u3002

"},{"location":"linux/kernel/sched/#_13","title":"\u7b49\u5f85\u961f\u5217","text":"

\u7b49\u5f85\u961f\u5217\u5728\u5185\u6838\u4e2d\u6709\u5f88\u591a\u7528\u9014\uff0c\u5c24\u5176\u65f6\u7528\u5728\u4e2d\u65ad\u5904\u7406\u3001\u8fdb\u7a0b\u540c\u6b65\u3002\u8fd0\u884c\u4e2d\u7684\u8fdb\u7a0b\u5f80\u5f80\u9700\u8981\u7b49\u5f85\u67d0\u4e9b\u4e8b\u4ef6\u7684\u53d1\u751f\uff0c\u5e0c\u671b\u7b49\u5f85\u7279\u5b9a\u4e8b\u4ef6\u7684\u8fdb\u7a0b\u5c06\u81ea\u5df1\u653e\u5165\u5408\u9002\u7684\u7b49\u5f85\u961f\u5217\uff0c\u7136\u540e\u8fdb\u5165\u7761\u7720\u72b6\u6001\u3002\u5f53\u4e8b\u4ef6\u53d1\u751f\u540e\uff0c\u7531\u5185\u6838\u8d1f\u8d23\u5524\u9192\u5b83\u4eec\u3002

\u7b49\u5f85\u961f\u5217\u7531\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff0c\u6bcf\u4e2a\u7b49\u5f85\u961f\u5217\u90fd\u6709\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u5934\uff08wait queue head\uff09\uff0c\u4e00\u4e2a\u7c7b\u578b\u4e3await_queue_head_t\u7684\u6570\u636e\u7ed3\u6784\uff1a

struct __wait_queue_head {\n    spinlock_t lock;\n    struct list_head task_list;\n};\n\ntypedef struct __wait_queue_head wait_queue_head_t;\n

\u56e0\u4e3a\u7b49\u5f85\u961f\u5217\u4e3b\u8981\u662f\u7531\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u548c\u5185\u6838\u51fd\u6570\u4fee\u6539\u7684\uff0c\u56e0\u6b64\u5fc5\u987b\u6709\u9501\u52a0\u4ee5\u4fdd\u62a4\u3002\u7b49\u5f85\u961f\u5217\u94fe\u8868\u4e2d\u7684\u5143\u7d20\u4e3a\uff1a

struct __wait_queue {\n    unsigned int flags;\n    struct task_struct *task;\n    wait_queue_func_t func;\n    struct list_head task_list;\n};\ntypedef struct __wait_queue wait_queue_t;\n

flags\uff1a\u8868\u793a\u8be5\u8fdb\u7a0b\u662f\u4e92\u65a5\u8fd8\u662f\u975e\u4e92\u65a5\u8fdb\u7a0b\u3002\u4e92\u65a5\u8fdb\u7a0b\u8868\u793a\u591a\u4e2a\u8fdb\u7a0b\u5728\u7b49\u5f85\u76f8\u540c\u7684\u4e8b\u4ef6\uff0c\u56e0\u6b64\u4ea7\u751f\u4e86\u7ade\u4e89\u5173\u7cfb\uff0c\u6b64\u65f6\u5185\u6838\u53ea\u9700\u8981\u5524\u9192\u5176\u4e2d\u4e00\u4e2a\u8fdb\u7a0b\u5373\u53ef\u3002\u800c\u975e\u4e92\u65a5\u8fdb\u7a0b\u5728\u53d1\u751f\u6307\u5b9a\u4e8b\u4ef6\u540e\u603b\u662f\u88ab\u5524\u9192\u3002

func\uff1a\u8d1f\u8d23\u5524\u9192\u7761\u7720\u8fdb\u7a0b\u7684\u51fd\u6570\u3002

task_list\uff1a\u7b49\u5f85\u961f\u5217\u94fe\u8868\uff0c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e00\u4e2a\u7761\u7720\u4e2d\u7684\u8fdb\u7a0b\u3002

\u7b49\u5f85\u961f\u5217\u7684\u64cd\u4f5c\u6bd4\u8f83\u590d\u6742\uff0c\u8fd9\u91cc\u4e0d\u8be6\u7ec6\u5c55\u5f00\uff0c\u53ea\u8bb2\u4e00\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u51fd\u6570sleep_on()\uff1a\u8fd9\u4e2a\u51fd\u6570\u5c06\u5f53\u524d\u8fdb\u7a0b\u52a0\u5165\u7b49\u5f85\u961f\u5217\uff0c\u5e76\u542f\u52a8\u8c03\u5ea6\u7a0b\u5e8f\u3002

void sleep_on(wait_queue_head_t *wq)\n{\n    wait_queue_t wait;  //\u58f0\u660e\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u5143\u7d20\n    init_waiqueue_entry(&wait, current);  //\u5c06current\u5f53\u524d\u8fdb\u7a0b\u52a0\u5165\u5230wait\u4e2d\n    current->state = TAKS_UNINTERRUPTIBLE; //\u8bbe\u7f6e\u5f53\u524d\u8fdb\u7a0b\u4e3a\u6df1\u5ea6\u7761\u7720\u6001\n    add_wait_queue(wq, &wait);  //\u5c06wait\u52a0\u5165\u5230\u7b49\u5f85\u961f\u5217wq\u4e2d\n    schedule();                //\u542f\u52a8\u8c03\u5ea6\u7a0b\u5e8f\n    remove_wait_queue(wq, &wait);   //\u628a\u5f53\u524d\u8fdb\u7a0b\u4ece\u7b49\u5f85\u961f\u5217\u4e2d\u5220\u9664\n}\n

\u6ce8\u610f\uff1a\u5728\u542f\u52a8\u8c03\u5ea6\u7a0b\u5e8f\u4e4b\u524d\uff0c\u8c03\u5ea6\u5668\u4f1a\u8bb0\u5f55\u5f53\u524d\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5e76\u4fdd\u5b58\u81f3\u5bc4\u5b58\u5668\u4e2d\uff0c\u5f53\u8be5\u7761\u7720\u8fdb\u7a0b\u88ab\u5524\u9192\u65f6\uff0c\u8c03\u5ea6\u7a0b\u5e8f\u4ecesleep_on()\u51fd\u6570\u4e4b\u524d\u505c\u6b62\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u2014\u2014\u628a\u8be5\u8fdb\u7a0b\u4ece\u7b49\u5f85\u961f\u5217\u5220\u9664\u3002

"},{"location":"linux/kernel/sched/#_14","title":"\u8fdb\u7a0b\u751f\u547d\u5468\u671f","text":"

\u8fd9\u91cc\u7ed9\u51fa\u4e00\u5f20\u793a\u610f\u56fe\u65b9\u4fbf\u7406\u89e3\u3002

"},{"location":"linux/kernel/sched/#_15","title":"\u8fdb\u7a0b\u521b\u5efa","text":"

Linux\u7684\u8fdb\u7a0b\u521b\u5efa\u6d89\u53ca\u5230\u4e24\u4e2a\u51fd\u6570\uff1afork()\u548cexec()\u3002fork()\u62f7\u8d1d\u5f53\u524d\u8fdb\u7a0b\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u3002exec()\u8d1f\u8d23\u8bfb\u53d6\u53ef\u6267\u884c\u6587\u4ef6\u5e76\u5c06\u5176\u8f7d\u5165\u5730\u5740\u7a7a\u95f4\u5f00\u59cb\u8fd0\u884c\u3002

Linux\u7684fork()\u4f7f\u7528\u4e86\u5199\u65f6\u62f7\u8d1d\uff08copy-on-write\uff09\u673a\u5236\u3002\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u8fdb\u7a0b\u65f6\uff0c\u5185\u6838\u5e76\u4e0d\u590d\u5236\u6574\u4e2a\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\uff0c\u800c\u662f\u8ba9\u7236\u5b50\u8fdb\u7a0b\u5171\u4eab\u3002\u53ea\u6709\u5728\u9700\u8981\u5199\u5165\u65f6\uff0c\u6570\u636e\u624d\u4f1a\u88ab\u590d\u5236\uff0c\u4ece\u800c\u4f7f\u5f97\u5404\u4e2a\u8fdb\u7a0b\u62e5\u6709\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u3002\u8fd9\u79cd\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u5927\u91cf\u6839\u672c\u5c31\u4e0d\u4f1a\u88ab\u4f7f\u7528\u7684\u6570\u636e\uff0c\u4ece\u800c\u4f7f\u5f97\u8fdb\u7a0b\u7684\u521b\u5efa\u975e\u5e38\u8fc5\u901f\u3002

fork()\u51fd\u6570\u7531clone()\u7cfb\u7edf\u8c03\u7528\u5b9e\u73b0\u3002\u8be5\u7cfb\u7edf\u8c03\u7528\u901a\u8fc7\u4e00\u7cfb\u5217CLONE_FLAGS\u53c2\u6570\u6765\u6307\u660e\u7236\u5b50\u8fdb\u7a0b\u9700\u8981\u5171\u4eab\u7684\u8d44\u6e90\u3002Linux\u4e2d\u8fdb\u7a0b\u4e0e\u7ebf\u7a0b\u7684\u533a\u522b\u4e3b\u8981\u662f\u5728\u6267\u884cclone()\u7cfb\u7edf\u8c03\u7528\u65f6\u6307\u5b9a\u7684\u5171\u4eab\u8d44\u6e90\u6709\u6240\u4e0d\u540c\u800c\u5df2\u3002\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6\u4f20\u9012\u7684\u53c2\u6570\u5982\u4e0b\uff1a

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);\n

\u4e0a\u8ff0\u4ee3\u7801\u6307\u660e\u4e86\u521b\u5efa\u7ebf\u7a0b\u65f6\u9700\u8981\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u7cfb\u7edf\u8d44\u6e90\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\u3002

\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4e00\u4e2a\u666e\u901a\u7684fork()\u7684\u5b9e\u73b0\uff1a

clone(SIGCHLD, 0);\n
"},{"location":"linux/kernel/sched/#_16","title":"\u8fdb\u7a0b\u7ec8\u7ed3","text":"

\u4e00\u4e2a\u8fdb\u7a0b\u7ec8\u7ed3\u65f6\uff0c\u5185\u6838\u5fc5\u987b\u91ca\u653e\u5b83\u6240\u5360\u7528\u7684\u8d44\u6e90\u5e76\u4e14\u544a\u77e5\u7236\u8fdb\u7a0b\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fdb\u7a0b\u7684\u7ec8\u7ed3\u662f\u81ea\u8eab\u5f15\u8d77\u7684\uff0c\u4e5f\u53ef\u80fd\u662f\u63a5\u6536\u4e86\u4e00\u4e2a\u4e0d\u53ef\u5ffd\u7565\u7684\u4fe1\u53f7\u6216\u8005\u662f\u5f02\u5e38\u3002

\u5f53\u8fdb\u7a0b\u76f8\u5173\u8054\u7684\u6240\u6709\u8d44\u6e90\u90fd\u88ab\u91ca\u653e\u540e\uff0c\u5b83\u5904\u4e8eEXIT_ZOMBIE\u72b6\u6001\u3002\u8be5\u72b6\u6001\u4e5f\u88ab\u79f0\u4e3a\u50f5\u6b7b\u72b6\u6001\uff0c\u5b83\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u88ab\u4fdd\u7559\uff0c\u7b49\u5f85\u7236\u8fdb\u7a0b\u4f7f\u7528wait()\u7cfb\u5217\u51fd\u6570\u56de\u6536\u3002

\u4f46\u662f\uff0c\u5982\u679c\u7236\u8fdb\u7a0b\u5728\u5b50\u8fdb\u7a0b\u4e4b\u524d\u5c31\u9000\u51fa\u4e86\uff0c\u90a3\u4e48\u5fc5\u987b\u6709\u4e00\u4e2a\u673a\u5236\u6765\u4fdd\u8bc1\u5b50\u8fdb\u7a0b\u627e\u5230\u4e00\u4e2a\u65b0\u7684\u7236\u8fdb\u7a0b\uff0c\u5426\u5219\u8fd9\u4e9b\u5b64\u513f\u8fdb\u7a0b\u4f1a\u6c38\u8fdc\u5904\u4e8e\u50f5\u6b7b\u72b6\u6001\uff0c\u767d\u767d\u8017\u8d39\u5185\u5b58\u3002\u5185\u6838\u7684\u89e3\u51b3\u529e\u6cd5\u662f\u5728\u5f53\u524d\u7ebf\u7a0b\u7ec4\u5185\u627e\u4e00\u4e2a\u7ebf\u7a0b\u4f5c\u4e3a\u7236\u8fdb\u7a0b\uff0c\u5982\u679c\u4e0d\u884c\uff0c\u5c31\u8ba9init\u8fdb\u7a0b\u505a\u7236\u8fdb\u7a0b\u3002

"},{"location":"linux/kernel/sched/#_17","title":"\u8fdb\u7a0b\u5207\u6362","text":"

\u4e3a\u4e86\u63a7\u5236\u8fdb\u7a0b\u7684\u8fd0\u884c\uff0c\u5185\u6838\u5fc5\u987b\u6709\u80fd\u529b\u6302\u8d77\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u6216\u8005\u6062\u590d\u4ee5\u524d\u6302\u8d77\u7684\u8fdb\u7a0b\u3002\u8fd9\u79cd\u884c\u4e3a\u88ab\u79f0\u4e3a\u4e0a\u4e0b\u6587\u5207\u6362\uff08context switch\uff09\u3002\u7406\u89e3\u4e86\u8fdb\u7a0b\u5207\u6362\uff0c\u624d\u6709\u53ef\u80fd\u7406\u89e3\u5185\u6838\u662f\u5982\u4f55\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u8c03\u5ea6\u7684\u3002

"},{"location":"linux/kernel/sched/#_18","title":"\u786c\u4ef6\u4e0a\u4e0b\u6587","text":"

\u5c3d\u7ba1\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u4f46\u6240\u6709\u8fdb\u7a0b\u90fd\u5171\u4eabCPU\u7684\u5bc4\u5b58\u5668\uff0c\u56e0\u6b64\u5728\u6062\u590d\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c\u4e4b\u524d\uff0c\u5185\u6838\u5fc5\u987b\u4fdd\u8bc1\u5bc4\u5b58\u5668\u88c5\u5165\u4e86\u6302\u8d77\u8fdb\u7a0b\u65f6\u7684\u503c\u3002\u8fdb\u7a0b\u6062\u590d\u6267\u884c\u524d\u5fc5\u987b\u88c5\u5165\u5bc4\u5b58\u5668\u7684\u4e00\u7ec4\u6570\u636e\u79f0\u4e3a\u786c\u4ef6\u4e0a\u4e0b\u6587\uff08hardware context\uff09\u3002\u786c\u4ef6\u4e0a\u4e0b\u6587\u662f\u8fdb\u7a0b\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u56e0\u4e3a\u6267\u884c\u4e0a\u4e0b\u6587\u5305\u542b\u8fdb\u7a0b\u6267\u884c\u9700\u8981\u7684\u6240\u6709\u4fe1\u606f\u3002

"},{"location":"linux/kernel/sched/#thread","title":"thread\u5b57\u6bb5","text":"

\u6bcf\u4e2a\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u5305\u542b\u4e00\u4e2a\u7c7b\u578b\u4e3athread_struct\u7684thread\u5b57\u6bb5\uff0c\u53ea\u8981\u8fdb\u7a0b\u88ab\u5207\u6362\u51fa\u53bb\uff0c\u5185\u6838\u5c31\u628a\u5176\u786c\u4ef6\u4e0a\u4e0b\u6587\u4fdd\u5b58\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\u3002\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u5305\u542b\u4e86\u5927\u91cfCPU\u5bc4\u5b58\u5668\u4fe1\u606f\u3002

"},{"location":"linux/kernel/sched/#_19","title":"\u6267\u884c\u8fdb\u7a0b\u5207\u6362","text":"

\u8fdb\u7a0b\u5207\u6362\u53ea\u4f1a\u53d1\u751f\u5728\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a

  1. \u4e3b\u52a8\u653e\u5f03CPU\uff0c\u8c03\u7528schedule()\u51fd\u6570\u3002
  2. \u6b63\u5728\u8fd0\u884c\u65f6\uff0c\u88ab\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0b\u62a2\u5360\u3002

\u4ece\u672c\u8d28\u4e0a\u6765\u8bf4\uff0c\u8fdb\u7a0b\u5207\u6362\u6709\u4e24\u4e2a\u6b65\u9aa4\uff1a

  1. \u5207\u6362\u9875\u5168\u5c40\u76ee\u5f55\u4ee5\u5b89\u88c5\u4e00\u4e2a\u65b0\u7684\u5730\u5740\u7a7a\u95f4\u3002
  2. \u5207\u6362\u5185\u6838\u6001\u5806\u6808\u548c\u786c\u4ef6\u4e0a\u4e0b\u6587\u3002

\u8fdb\u7a0b\u5207\u6362\u7531switch_to\u5b8f\u6267\u884c\u3002\u8be5\u5b8f\u5b9a\u4e49\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\u3002

"},{"location":"linux/kernel/sched/#_20","title":"\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5","text":""},{"location":"linux/kernel/synch/","title":"\u5185\u6838\u540c\u6b65","text":"

\u53ea\u8981\u6709\u5171\u4eab\u8d44\u6e90\u7684\u5730\u65b9\uff0c\u7a0b\u5e8f\u5458\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u5c31\u9700\u8981\u7279\u522b\u6ce8\u610f\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u9632\u6b62\u5e76\u53d1\u8bbf\u95ee\u65f6\u9020\u6210\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u662f\u9020\u6210\u7cfb\u7edf\u4e0d\u7a33\u5b9a\u7684\u4e00\u7c7b\u9690\u60a3\uff0c\u800c\u8fd9\u7c7b\u9690\u60a3\u5f80\u5f80\u96be\u4ee5\u8ddf\u8e2a\u548c\u8c03\u8bd5\u3002\u8981\u505a\u5230\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u4fdd\u62a4\u76f8\u5f53\u56f0\u96be\u3002\u65e9\u5e74\uff0cLinux\u8fd8\u4e0d\u652f\u6301SMP\uff08\u5bf9\u79f0\u591a\u5904\u7406\u5668\uff09\uff0c\u907f\u514d\u5e76\u53d1\u8bbf\u95ee\u6570\u636e\u8fd8\u6bd4\u8f83\u5bb9\u6613\u3002\u5728\u5355\u4e00\u5904\u7406\u5668\u65f6\uff0c\u53ea\u6709\u5728\u4e2d\u65ad\u53d1\u751f\u7684\u65f6\u5019\uff0c\u6216\u8005\u7a0b\u5e8f\u660e\u786e\u8bf7\u6c42\u91cd\u65b0\u8c03\u5ea6\uff08scheduler\uff09\u7684\u65f6\u5019\uff0c\u6570\u636e\u624d\u6709\u53ef\u80fd\u88ab\u5e76\u53d1\u8bbf\u95ee\u3002\u4e8e\u662f\u5728\u65e9\u671f\u7684\u5355CPU\u67b6\u6784\u4e0b\uff0c\u7b80\u5355\u7684\u7981\u6b62\u4e2d\u65ad\u5373\u53ef\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u3002\u4f46\u662f\u591a\u5904\u7406\u5668\u65f6\u4ee3\u8fd9\u4e00\u65b9\u6cd5\u4e0d\u518d\u6709\u6548\u4e86\uff0c\u56e0\u4e3a\u7981\u6b62\u4e2d\u65ad\u53ea\u80fd\u7981\u6b62\u672c\u5730\u7684\u4e2d\u65ad\uff0c\u4f46\u662f\u65e0\u6cd5\u963b\u6b62\u5176\u4ed6CPU\u5e76\u53d1\u5730\u8bbf\u95ee\u6570\u636e\u3002\u968f\u77402.6\u5185\u6838\u7248\u672c\u7684\u51fa\u73b0\uff0cLinux\u5185\u6838\u5df2\u7ecf\u53d1\u5c55\u6210\u62a2\u5360\u5f0f\u5185\u6838\u3002\u8fd9\u610f\u5473\u7740\u8c03\u5ea6\u7a0b\u5e8f\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u523b\u62a2\u5360\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\uff0c\u91cd\u65b0\u8c03\u5ea6\u5176\u4ed6\u7684\u8fdb\u7a0b\u6267\u884c\uff0c\u8fd9\u5bf9\u6570\u636e\u7684\u540c\u6b65\u63d0\u51fa\u4e86\u66f4\u9ad8\u7684\u8981\u6c42\u3002\u5177\u4f53\u8bf7\u770b\u8fdb\u7a0b\u8c03\u5ea6\u3002

\u5185\u6838\u4e2d\u5e76\u53d1\u6267\u884c\u7684\u539f\u56e0\u6709\uff1a

  • \u4e2d\u65ad\u2014\u2014\u4e2d\u65ad\u662f\u5f02\u6b65\u7684\uff0c\u51e0\u4e4e\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u523b\u53d1\u751f\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u5728\u4efb\u4f55\u65f6\u523b\u6253\u65ad\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\u3002
  • \u8f6f\u4e2d\u65ad\u548ctasklet\u2014\u2014\u5185\u6838\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u523b\u5524\u9192\u6216\u8c03\u5ea6\u8f6f\u4e2d\u65ad\u548ctasklet\u3002
  • \u62a2\u5360\u2014\u2014\u5185\u6838\u662f\u62a2\u5360\u6027\u8d28\u7684\uff0c\u6b63\u5728\u6267\u884c\u7684\u4efb\u52a1\u53ef\u80fd\u4f1a\u88ab\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u62a2\u5360\u3002
  • \u7761\u7720\u2014\u2014\u5728\u5185\u6838\u6267\u884c\u7684\u8fdb\u7a0b\u53ef\u80fd\u7761\u7720\uff0c\u8fd9\u4f1a\u5524\u9192\u8c03\u5ea6\u7a0b\u5e8f\uff0c\u4ece\u800c\u8c03\u5ea6\u53e6\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u3002
  • SMP\u2014\u2014\u591a\u4e2aCPU\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u4ee3\u7801\u3002
"},{"location":"linux/kernel/synch/#_2","title":"\u540c\u6b65\u7684\u6982\u5ff5","text":""},{"location":"linux/kernel/synch/#_3","title":"\u4e34\u754c\u533a","text":"

\u6240\u8c13\u4e34\u754c\u533a\u5c31\u662f\u8bbf\u95ee\u548c\u64cd\u4f5c\u5171\u4eab\u6570\u636e\u7684\u4ee3\u7801\u6bb5\u3002\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u4e34\u754c\u533a\u4ee3\u7801\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u56e0\u6b64\u4e34\u754c\u533a\u4ee3\u7801\u5fc5\u987b\u4ee5\u539f\u5b50\u5730\u65b9\u5f0f\u6267\u884c\u3002\u8003\u8651\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfi\uff0c\u64cd\u4f5c\u4ec5\u4ec5\u662f\u5bf9\u5176\u52a01\u3002

i++;\n

\u5982\u6b64\u7b80\u5355\u7684\u64cd\u4f5c\u5728CPU\u6267\u884c\u7684\u65f6\u5019\u9700\u8981\u4e09\u6761\u6c47\u7f16\u6307\u4ee4\uff1a

  1. \u4ece\u5185\u5b58\u4e2d\u8bfb\u51fa\u53d8\u91cfi\u7684\u503c\u5e76\u653e\u5728\u4e00\u4e2a\u5bc4\u5b58\u5668\u5185\u3002
  2. \u5c06\u5bc4\u5b58\u5668\u4e2d\u7684\u503c+1\u3002
  3. \u628ai\u7684\u503c\u5199\u56de\u5230\u5185\u5b58\u3002

\u5b9e\u9645\u4e0a\uff0c\u5728\u591a\u7ebf\u7a0b\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b\uff0c\u5176\u4ed6\u7ebf\u7a0b\u6709\u53ef\u80fd\u5728\u8fd9\u4e09\u6761\u6307\u4ee4\u95f4\u9699\u7684\u4efb\u610f\u65f6\u523b\u201c\u63d2\u961f\u201d\u3002\u8fd9\u79cd\u6982\u7387\u867d\u7136\u5f88\u5c0f\uff0c\u4f46\u662f\u8ba1\u7b97\u673a\u6bcf\u79d2\u8fd0\u884c\u4e0a\u767e\u4e07\u6761\u6307\u4ee4\uff0c\u201c\u63d2\u961f\u201d\u53ef\u80fd\u6bcf\u8fc7\u51e0\u79d2\u5c31\u53d1\u751f\u4e00\u6b21\u3002\u5047\u8bbe\u6709\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u82e5i\u7684\u521d\u59cb\u503c\u4e3a1\uff0c\u90a3\u4e48\u6211\u4eec\u671f\u671b\u7684\u6700\u7ec8\u7ed3\u679c\u5e94\u8be5\u662f3\u3002\u4f46\u662f\u5047\u5982\u7b2c\u4e8c\u4e2a\u7ebf\u7a0b\u5728\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u6700\u540e\u4e00\u6b65\u4e4b\u524d\u5c31\u53bb\u5185\u5b58\u4e2d\u8bfb\u53d6\u4e86i\u7684\u503c\uff08\u6b64\u65f6i\u7684\u503c\u4ecd\u7136\u662f1\uff09\uff0c\u6211\u4eec\u6700\u540e\u5f97\u5230\u7684i\u7684\u503c\u5c31\u662f2\u800c\u4e0d\u662f3\uff0c\u8fd9\u4e0e\u6211\u4eec\u9884\u671f\u7684\u503c\u4e0d\u7b26\u3002

#include <stdio.h>\n#include <pthread.h>\n\nint i = 0;\n\nvoid *thread_func(void *arg)\n{\n    for(int j = 0 ;j < 1000000; j++)\n    {\n        i++;\n    }\n\n}\n\nint main(int argc, char *argv[])\n{\n\n    pthread_t tid1, tid2;\n    pthread_create(&tid1, NULL, thread_func, NULL);\n    pthread_create(&tid2, NULL, thread_func, NULL);\n\n    pthread_join(tid1, NULL);\n    pthread_join(tid2, NULL);\n\n    printf(\"i = %d\\n\", i);\n    return 0;\n}\n

\u4ee5\u4e0a\u662f\u4e00\u6bb5\u793a\u4f8b\u4ee3\u7801\uff0c\u5728\u7f16\u8bd1\u65f6\u8bf7\u52a0\u4e0a-lpthread\uff0c\u4ee5\u94fe\u63a5\u6b63\u786e\u7684\u5e93\u3002\u591a\u6b21\u6267\u884c\u8be5\u7a0b\u5e8f\u540e\u4f60\u4f1a\u53d1\u73b0\u6700\u7ec8\u7684\u7ed3\u679c\u662f\u4e0d\u786e\u5b9a\u7684\u3002

\u8fd9\u662f\u6700\u7b80\u5355\u7684\u4e34\u754c\u533a\u4f8b\u5b50\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u7b80\u5355\u7684\u7ade\u4e89\u6761\u4ef6\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u7528\u5230\u590d\u6742\u7684\u9501\u673a\u5236\uff0c\u56e0\u4e3a\u9501\u5bf9\u4e8eCPU\u7684\u6027\u80fd\u6709\u5f88\u5927\u7684\u5f71\u54cd\u3002\u591a\u6570\u5904\u7406\u5668\u90fd\u63d0\u4f9b\u4e86\u6307\u4ee4\u6765\u539f\u5b50\u5730\u8bfb\u3001\u5199\u53d8\u91cf\u3002\u6211\u4eec\u79f0\u4e4b\u4e3a\u539f\u5b50\u6307\u4ee4\u3002\u4f7f\u7528\u539f\u5b50\u6307\u4ee4\u53ef\u4ee5\u89e3\u51b3\u4e00\u4e9b\u7b80\u5355\u7684\u5e76\u53d1\u95ee\u9898\u3002\u4e24\u6761\u539f\u5b50\u6307\u4ee4\u4e0d\u53ef\u80fd\u4ea4\u9519\u6267\u884c\uff0c\u56e0\u4e3a\u5904\u7406\u5668\u4f1a\u4ece\u786c\u4ef6\u4e0a\u7981\u6b62\u8fd9\u79cd\u53ef\u80fd\u6027\u3002

"},{"location":"linux/kernel/synch/#_4","title":"\u52a0\u9501","text":"

\u5f53\u5171\u4eab\u8d44\u6e90\u662f\u4e00\u4e2a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7684i++\u65f6\uff0c\u539f\u5b50\u6307\u4ee4\u5c31\u65e0\u80fd\u4e3a\u529b\u4e86\u3002\u6b64\u65f6\u6211\u4eec\u5fc5\u987b\u5f15\u5165\u9501\u673a\u5236\u6765\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3002\u540c\u4e00\u65f6\u523b\uff0c\u6211\u4eec\u53ea\u5141\u8bb8\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u7684\u8bbf\u95ee\u5fc5\u987b\u7b49\u5f85\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e\u9501\u4e4b\u540e\u624d\u80fd\u8fdb\u884c\u3002\u9501\u6709\u591a\u79cd\u591a\u6837\u7684\u5f62\u5f0f\uff0c\u9501\u7684\u7c92\u5ea6\u4e5f\u5404\u4e0d\u76f8\u540c\u3002Linux\u5185\u6838\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0d\u540c\u7684\u9501\u673a\u5236\uff0c\u4ed6\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff1a\u5f53\u9501\u88ab\u5176\u4ed6\u7ebf\u7a0b\u6301\u6709\u800c\u4e0d\u53ef\u7528\u65f6\u7684\u8868\u73b0\u5f62\u5f0f\u2014\u2014\u6709\u4e00\u4e9b\u9501\u4f1a\u5728\u539f\u5730\u7b49\u5f85\uff0c\u800c\u6709\u4e00\u4e9b\u9501\u4f1a\u76f4\u63a5\u7761\u7720\u3002\u9501\u6ca1\u6709\u4f18\u52a3\u4e4b\u5206\uff0c\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u7528\u4e0d\u540c\u7684\u9501\u662f\u7a0b\u5e8f\u5458\u5fc5\u5907\u7684\u6280\u80fd\u3002

\u5f53\u4e00\u4e2a\u9501\u6b63\u5728\u88ab\u5360\u7528\u65f6\uff0c\u6709\u5176\u4ed6\u7ebf\u7a0b\u8bd5\u56fe\u83b7\u5f97\u8be5\u9501\uff0c\u6211\u4eec\u79f0\u4e4b\u4e3a\u9501\u7684\u4e89\u7528\u3002\u7531\u4e8e\u9501\u662f\u8ba9\u7a0b\u5e8f\u4ee5\u4e32\u884c\u7684\u65b9\u5f0f\u5bf9\u8d44\u6e90\u8fdb\u884c\u8bbf\u95ee\uff0c\u88ab\u957f\u65f6\u95f4\u6301\u6709\u7684\u9501\u65e0\u7591\u4f1a\u964d\u4f4e\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u4e8e\u662f\u52a0\u9501\u7c92\u5ea6\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u5982\u679c\u662f\u4e00\u6bb5\u52a0\u9501\u7684\u4ee3\u7801\u88ab\u9891\u7e41\u7684\u8c03\u7528\uff0c\u8fd9\u5f80\u5f80\u4f1a\u6210\u4e3a\u7cfb\u7edf\u6027\u80fd\u7684\u74f6\u9888\u3002\u5728\u4e00\u4e9b\u5927\u578b\u673a\u5668\u4e0a\u53ef\u80fd\u8868\u73b0\u4e0d\u662f\u90a3\u4e48\u660e\u663e\uff0c\u4f46\u662f\u5728\u4e00\u4e9b\u5c0f\u578b\u673a\u5668\u4e0a\uff0c\u8fc7\u7c97\u7684\u9501\u7684\u7c92\u5ea6\u4f1a\u4e25\u91cd\u62d6\u7d2f\u7cfb\u7edf\u7684\u6027\u80fd\u3002

"},{"location":"linux/kernel/synch/#_5","title":"\u6b7b\u9501","text":"

\u6b7b\u9501\u662f\u7f16\u5199\u540c\u6b65\u4ee3\u7801\u65f6\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\u95ee\u9898\uff0c\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u56e0\u4e3a\u4e89\u593a\u8d44\u6e90\u800c\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e\u9501\u6216\u8d44\u6e90\u3002\u5982\u679c\u6ca1\u6709\u5916\u529b\u5e72\u6d89\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u5c06\u6c38\u8fdc\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u3002

\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u6b7b\u9501\u4f8b\u5b50\u5c31\u662f\u81ea\u6b7b\u9501\uff1a\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u8bd5\u56fe\u53bb\u83b7\u5f97\u4e00\u4e2a\u81ea\u5df1\u5df2\u7ecf\u6301\u6709\u7684\u9501\uff0c\u90a3\u4e48\u5b83\u5c06\u6c38\u8fdc\u7b49\u5f85\u4e0b\u53bb\u3002 \u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50\u53ebABBA\u6b7b\u9501\uff1a\u7ebf\u7a0b1\u6301\u6709\u9501A\uff0c\u7ebf\u7a0b2\u6301\u6709\u9501B\uff0c\u7ebf\u7a0b1\u8bd5\u56fe\u53bb\u83b7\u5f97\u9501B\uff0c\u800c\u7ebf\u7a0b2\u8bd5\u56fe\u53bb\u83b7\u5f97\u9501A\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e\u9501\uff0c\u4f46\u662f\u8c01\u90fd\u4e0d\u60f3\u91ca\u653e\u81ea\u5df1\u7684\u9501\uff0c\u4e8e\u662f\u5c31\u9020\u6210\u4e86\u6b7b\u9501\u3002\u9884\u9632\u6b7b\u9501\u7684\u53d1\u751f\u975e\u5e38\u91cd\u8981\uff0c\u867d\u7136\u4f60\u4e0d\u77e5\u9053\u81ea\u5df1\u7684\u4ee3\u7801\u4f1a\u4e0d\u4f1a\u53d1\u751f\u6b7b\u9501\uff0c\u4f46\u662f\u9075\u5faa\u4e00\u4e9b\u7b80\u5355\u7684\u89c4\u5219\u5bf9\u4e8e\u907f\u514d\u6b7b\u9501\u5927\u6709\u5e2e\u52a9\uff1a

  • \u6309\u987a\u5e8f\u52a0\u9501\u3002\u4f7f\u7528\u591a\u4e2a\u9501\u65f6\u5fc5\u987b\u4fdd\u8bc1\u4ee5\u76f8\u540c\u7684\u987a\u5e8f\u83b7\u53d6\u9501\uff0c\u5426\u5219\u5c31\u6709\u53ef\u80fd\u9020\u6210ABBA\u6b7b\u9501\u3002
  • \u9632\u6b62\u9965\u997f\u3002
  • \u4e0d\u8981\u91cd\u590d\u8bf7\u6c42\u540c\u4e00\u4e2a\u9501\u3002
  • \u8bbe\u8ba1\u8d8a\u7b80\u5355\u8d8a\u597d\u3002
"},{"location":"linux/kernel/synch/#_6","title":"\u540c\u6b65\u7684\u65b9\u6cd5","text":""},{"location":"linux/kernel/synch/#_7","title":"\u539f\u5b50\u64cd\u4f5c","text":"

\u539f\u5b50\u64cd\u4f5c\u53ef\u4ee5\u4fdd\u8bc1\u6307\u4ee4\u4ee5\u4e0d\u53ef\u5206\u5272\u7684\u5f62\u5f0f\u6267\u884c\u2014\u2014\u2014\u2014\u6267\u884c\u8fc7\u7a0b\u4e0d\u53ef\u88ab\u6253\u65ad\u3002\u5185\u6838\u63d0\u4f9b\u4e86\u4e24\u7ec4\u539f\u5b50\u64cd\u4f5c\u63a5\u53e3\u2014\u2014\u2014\u2014\u4e00\u7ec4\u9488\u5bf9\u6574\u6570\uff0c\u4e00\u7ec4\u9488\u5bf9\u5355\u72ec\u7684\u4f4d\u3002\u5728Linux\u652f\u6301\u7684\u6240\u6709\u4f53\u7cfb\u7ed3\u6784\u4e0a\u90fd\u5b9e\u73b0\u4e86\u8fd9\u4e24\u7ec4\u63a5\u53e3\u3002\u867d\u7136\u5728\u4e0d\u540c\u4f53\u7cfb\u7ed3\u6784\u4e0a\u5b9e\u73b0\u7684\u65b9\u5f0f\u662f\u4e0d\u540c\u7684\uff0c\u4f46\u662f\u63a5\u53e3\u90fd\u662f\u7edf\u4e00\u7684\u3002

\u6709\u7684\u65f6\u5019\u6211\u4eec\u4f1a\u8981\u6c42\u67d0\u4e9b\u6307\u4ee4\u6309\u7167\u7279\u5b9a\u7684\u987a\u5e8f\u6267\u884c\uff0c\u8fd9\u88ab\u79f0\u4e3a\u987a\u5e8f\u6027\uff0c\u4ee5\u5c4f\u969c\uff08barrier\uff09\u6307\u4ee4\u6765\u5b9e\u73b0\u3002

1.\u539f\u5b50\u6574\u6570\u64cd\u4f5c

\u9488\u5bf9\u6574\u6570\u7684\u539f\u5b50\u64cd\u4f5c\u4f7f\u7528\u4e00\u4e2a\u7279\u6b8a\u7684atomic_t\u7c7b\u578b\u7684\u6570\u636e\u3002\u5b83\u7684\u5b9a\u4e49\u5728<linux/types.h>\u4e2d\uff1a

typedef struct{\n    volatile int counter;\n}atomic_t;\n

\u4f7f\u7528\u539f\u5b50\u6574\u6570\u64cd\u4f5c\u7684\u58f0\u660e\u5728<asm/atomic.h>\u4e2d\u5b9a\u4e49\u3002\u6709\u4e9b\u4f53\u7cfb\u7ed3\u6784\u4f1a\u63d0\u4f9b\u4e00\u4e9b\u989d\u5916\u7684\u539f\u5b50\u64cd\u4f5c\uff0c\u4f46\u662f\u5185\u6838\u7684\u63a5\u53e3\u5728\u6240\u6709\u4f53\u7cfb\u7ed3\u6784\u4e0a\u90fd\u5b9e\u73b0\u4e86\u3002

\u4e00\u4e9b\u7b80\u5355\u7684\u64cd\u4f5c\u6bd4\u5982\uff1a

atomic_t v;   //\u5b9a\u4e49v\natomic_t u = ATOMIC_INIT(0); //\u5b9a\u4e49u\u5e76\u521d\u59cb\u5316\u4e3a0\natomic_set(&v, 4);          //\u8bbe\u7f6ev\u7684\u503c\u4e3a4\natomic_add(2, &v);          //\u5c06v\u7684\u503c\u589e\u52a02\nint w = atomic_read(&v);    //\u5c06\u539f\u5b50\u53d8\u91cfv\u8f6c\u53d8\u4e3aint\u53d8\u91cf\n

\u539f\u5b50\u64cd\u4f5c\u7684\u63a5\u53e3\u975e\u5e38\u7b80\u5355\u6613\u8bfb\uff0c\u6ca1\u6709\u5fc5\u8981\u5355\u72ec\u53bb\u8bb0\uff0c\u7528\u5230\u4ec0\u4e48\u5c31\u5b66\u4ec0\u4e48\u3002

\u539f\u5b50\u64cd\u4f5c\u901a\u5e38\u662f\u5185\u8054\u51fd\u6570\uff0c\u4e14\u662f\u7528\u5185\u5d4c\u6c47\u7f16\u6307\u4ee4\u6765\u5b9e\u73b0\u7684\u3002ARM\u4f53\u7cfb\u8bf7\u53c2\u8003\uff1aARM GCC Inline Assembler\u3002

atomic64_t\u7c7b\u578b\u662f64\u4f4d\u7684\u539f\u5b50\u53d8\u91cf\uff0c\u5176\u529f\u80fd\u548c32\u4f4d\u4e00\u81f4\uff0c\u51fd\u6570\u63a5\u53e3\u4ee5atomic64\u524d\u7f00\u547d\u540d\u3002

typedef struct {\n    volatile long counter;\n}atomic64_t;\n

2.\u539f\u5b50\u4f4d\u64cd\u4f5c

atomic_t\u7c7b\u578b\u5bf9\u6574\u6570\u7b97\u672f\u6765\u8bb2\u6bd4\u8f83\u6709\u7528\u3002\u4f46\u662f\u5f53\u9700\u8981\u4ee5\u539f\u5b50\u5f62\u5f0f\u6765\u64cd\u4f5c\u5355\u4e2a\u7684\u4f4d\u65f6\uff0c\u8fd9\u79cd\u7c7b\u578b\u5c31\u65e0\u6cd5\u6d3e\u4e0a\u7528\u573a\u4e86\u3002Linux\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5bf9\u4f4d\u7684\u539f\u5b50\u64cd\u4f5c\u3002

\u539f\u5b50\u4f4d\u7684\u64cd\u4f5c\u975e\u5e38\u5feb\uff0c\u53ea\u8981\u786c\u4ef6\u5e95\u5c42\u786c\u4ef6\u652f\u6301\uff0c\u8fd9\u79cd\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528\u5355\u4e2a\u673a\u5668\u6307\u4ee4\u6765\u6267\u884c\u3002\u8fd9\u4e9b\u51fd\u6570\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\uff0c\u5b9a\u4e49\u5728<asm/bitops.h>\u4e2d\u3002\u5373\u4f7f\u662f\u5728SMP\u8ba1\u7b97\u673a\u4e0a\uff0c\u8fd9\u4e9b\u51fd\u6570\u4f9d\u65e7\u53ef\u4ee5\u786e\u4fdd\u662f\u539f\u5b50\u7684\u3002\u539f\u5b50\u4f4d\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u4f4d\u53f7+\u6307\u9488\u3002\u53ef\u7528\u7684\u64cd\u4f5c\u5982\u4e0b\uff1a

void set_bit(nr, void *addr);     //\u8bbe\u7f6eaddr\u6307\u5411\u7684\u7b2cnr\u4f4d\nvoid clear_bit(nr, void *addr);   //\u6e05\u9664addr\u6307\u5411\u7684\u7b2cnr\u4f4d\nvoid change_bit(nr, void *addr);  //\u5207\u6362addr\u6307\u5411\u7684\u7b2cnr\u4f4d\ntest_bit(nr, void *addr);         //\u8fd4\u56de\u6307\u5b9a\u4f4d\u7684\u5f53\u524d\u503c\n
"},{"location":"linux/kernel/synch/#_8","title":"\u81ea\u65cb\u9501","text":"

\u539f\u5b50\u64cd\u4f5c\u53ea\u80fd\u9488\u5bf9\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u4fdd\u62a4\uff0c\u73b0\u5b9e\u60c5\u51b5\u91cc\uff0c\u4e00\u4e2a\u4e34\u754c\u533a\u91cc\u751a\u81f3\u6709\u591a\u4e2a\u51fd\u6570\u3002\u6bd4\u5982\u6211\u4eec\u6709\u8fd9\u6837\u4e00\u4e2a\u60c5\u51b5\uff1a\u4ece\u67d0\u4e2a\u51fd\u6570\u4e2d\u8bfb\u53d6\u67d0\u4e2astruct\u7c7b\u578b\u7684\u6570\u636e\uff0c\u7136\u540e\u6539\u53d8\u5176\u4e2d\u67d0\u4e2a\u6210\u5458\u7684\u503c\uff0c\u6700\u540e\u518d\u628a\u66f4\u65b0\u7684\u503c\u8d4b\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u3002\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u5fc5\u987b\u662f\u4e0d\u53ef\u5206\u5272\u7684\uff0c\u5728\u6570\u636e\u66f4\u65b0\u5b8c\u6bd5\u524d\uff0c\u7edd\u5bf9\u4e0d\u5141\u8bb8\u4efb\u4f55\u5176\u4ed6\u7684\u4ee3\u7801\u8bfb\u53d6\u8fd9\u4e9b\u6570\u636e\u3002\u663e\u7136\uff0c\u6211\u4eec\u4e0d\u53ef\u80fd\u8981\u6c42\u6bcf\u4e2a\u4f53\u7cfb\u90fd\u652f\u6301\u5982\u6b64\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u7528\u5230\u66f4\u590d\u6742\u7684\u540c\u6b65\u65b9\u6cd5\u2014\u2014\u9501\u6765\u63d0\u4f9b\u4fdd\u62a4\u3002

Linux\u5185\u6838\u4e2d\u6700\u5e38\u89c1\u7684\u9501\u662f\u81ea\u65cb\u9501\uff08spin lock\uff09\u3002\u81ea\u65cb\u9501\u53ea\u80fd\u540c\u65f6\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\uff0c\u5982\u679c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8bd5\u56fe\u83b7\u5f97\u4e00\u4e2a\u5df2\u7ecf\u88ab\u5360\u7528\u7684\u81ea\u65cb\u9501\uff0c\u90a3\u4e48\u8be5\u7ebf\u7a0b\u5c06\u4f1a\u8fdb\u5165\u5fd9\u7b49\u5f85\uff0c\u76f4\u5230\u9501\u53ef\u7528\u3002\u901a\u4fd7\u7684\u89e3\u91ca\u5c31\u662f\u4e00\u6247\u95e8\u5bf9\u5e94\u4e00\u628a\u94a5\u5319\uff0c\u8981\u8fdb\u5165\u8fd9\u6247\u95e8\u5fc5\u987b\u5148\u68c0\u67e5\u95e8\u4e0a\u6709\u6ca1\u6709\u94a5\u5319\uff0c\u5982\u679c\u6ca1\u6709\u5c31\u53ea\u80fd\u5728\u5916\u9762\u7b49\u5f85\u76f4\u5230\u91cc\u9762\u7684\u4eba\u51fa\u6765\u3002\u7531\u4e8e\u81ea\u65cb\u4f1a\u4e00\u76f4\u5bfc\u81f4\u7ebf\u7a0b\u5fd9\u7b49\u5f85\uff0c\u767d\u767d\u5360\u7528\u4e86CPU\u8d44\u6e90\u800c\u4e0d\u4f1a\u53bb\u505a\u522b\u7684\u4e8b\u60c5\uff0c\u56e0\u6b64\u7279\u522b\u6d6a\u8d39\u8d44\u6e90\uff0c\u6240\u4ee5\u81ea\u65cb\u9501\u4e0d\u5e94\u8be5\u88ab\u957f\u65f6\u95f4\u5360\u6709\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f7f\u7528\u81ea\u65cb\u9501\u65f6\u4f60\u5fc5\u987b\u4fdd\u8bc1\u4e34\u754c\u533a\u4ee3\u7801\u6267\u884c\u8db3\u591f\u77ed\uff0c\u5426\u5219\u4f1a\u4e25\u91cd\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\u3002

\u81ea\u65cb\u9501\u7684\u5b9e\u73b0\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\uff0c\u4ee3\u7801\u5f80\u5f80\u901a\u8fc7\u6c47\u7f16\u5b9e\u73b0\u3002\u5176\u5b9a\u4e49\u5728<asm/spinlock.h>\u4e2d\uff0c\u5b9e\u9645\u7684\u63a5\u53e3\u5b9a\u4e49\u5728<linux/spinlock.h>\u3002\u81ea\u65cb\u9501\u7684\u57fa\u672c\u4f7f\u7528\u5982\u4e0b\uff1a

DEFINE_SPINLOCK(sp_lock);\nspin_lock(&sp_lock);\n/*\u4e34\u754c\u533a\u4ee3\u7801*/\nspin_unlock(&sp_lock);\n

\u5728\u5355\u5904\u7406\u5668\u4e0a\uff0c\u5982\u679c\u7981\u6b62\u5185\u6838\u62a2\u5360\uff0c\u90a3\u4e48\u81ea\u65cb\u9501\u4e0d\u4f1a\u88ab\u7f16\u8bd1\u8fdb\u5185\u6838\u3002

\u81ea\u65cb\u9501\u53ef\u4ee5\u4f7f\u7528\u5728\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\uff0c\u4f46\u83b7\u53d6\u9501\u4e4b\u524d\uff0c\u5fc5\u987b\u8981\u7981\u6b62\u672c\u5730\u4e2d\u65ad\uff0c\u5426\u5219\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4f1a\u6253\u65ad\u5f53\u524d\u6301\u6709\u9501\u7684\u4ee3\u7801\u3002\u7531\u4e8e\u9501\u7684\u6301\u6709\u548c\u91ca\u653e\u5fc5\u987b\u662f\u539f\u5b50\u7684\uff0c\u5982\u679c\u88ab\u6253\u65ad\u53ef\u80fd\u4f1a\u5f71\u54cd\u9501\u7684\u4e00\u81f4\u6027\uff0c\u8fd9\u4f1a\u5f15\u8d77\u4e0d\u53ef\u9884\u6d4b\u7684\u540e\u679c\u3002\u751a\u81f3\uff0c\u5982\u679c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u53bb\u4e89\u7528\u8fd9\u4e2a\u5df2\u7ecf\u88ab\u6301\u6709\u7684\u9501\uff0c\u90a3\u4e48\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002\u6ce8\u610f\uff0c\u8981\u5173\u95ed\u7684\u53ea\u662f\u5f53\u524d\u5904\u7406\u5668\u7684\u4e2d\u65ad\uff0c\u5176\u4ed6\u5904\u7406\u5668\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u81ea\u65cb\u5e76\u4e0d\u4f1a\u59a8\u788d\u5f53\u524d\u5904\u7406\u5668\u91ca\u653e\u9501\u3002

\u5185\u6838\u63d0\u4f9b\u4e86\u7981\u6b62\u4e2d\u65ad\u540c\u65f6\u8bf7\u6c42\u9501\u7684\u63a5\u53e3\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a

DEFINE_SPINLOCK(sp_lock);\nunsigned long flags;\nspin_lock_irqsave(&sp_lock, flags);\nspin_unlock_irqrestore(&sp_lock, flags);\n

\u51fd\u6570spin_lock_irqsave()\u4fdd\u5b58\u4e2d\u65ad\u72b6\u6001\uff0c\u5e76\u7981\u6b62\u672c\u5730\u4e2d\u65ad\uff0c\u7136\u540e\u518d\u53bb\u83b7\u53d6\u9501\u3002\u53cd\u8fc7\u6765spin_unlock_irqrestore()\u89e3\u9501\uff0c\u5e76\u8ba9\u4e2d\u65ad\u6062\u590d\u5230\u52a0\u9501\u524d\u7684\u72b6\u6001\u3002\u6240\u4ee5\u5373\u4fbf\u662f\u52a0\u9501\u524d\u4e2d\u65ad\u5df2\u7ecf\u88ab\u7981\u6b62\u4e86\uff0c\u4ee3\u7801\u4e5f\u4e0d\u4f1a\u9519\u8bef\u5730\u6fc0\u6d3b\u4e2d\u65ad\uff0c\u800c\u662f\u8ba9\u4e2d\u65ad\u4fdd\u6301\u7981\u6b62\u72b6\u6001\u3002

\u5982\u679c\u4f60\u80fd\u786e\u5b9a\u4e2d\u65ad\u5728\u52a0\u9501\u524d\u662f\u6fc0\u6d3b\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u914d\u5bf9\u4f7f\u7528spin_lock_irq()\u548cspin_unlock_irq()\uff0c\u4ece\u800c\u5728\u89e3\u9501\u65f6\u76f4\u63a5\u6fc0\u6d3b\u4e2d\u65ad\u3002\u4f46\u662f\u5185\u6838\u7684\u5e9e\u5927\u6027\u5f80\u5f80\u8ba9\u4eba\u5f88\u96be\u641e\u6e05\u695a\u4e2d\u65ad\u5728\u5f53\u524d\u6267\u884c\u4ee3\u7801\u524d\u662f\u5426\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\uff0c\u6240\u4ee5\u8be5\u65b9\u6cd5\u5e76\u4e0d\u63d0\u5021\u3002

\u7531\u4e8e\u4e2d\u65ad\u4e0b\u534a\u90e8\u53ef\u4ee5\u62a2\u5360\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u7684\u4ee3\u7801\uff0c\u56e0\u6b64\u52a0\u9501\u7684\u540c\u65f6\u8fd8\u9700\u8981\u7981\u6b62\u4e0b\u534a\u90e8\u6267\u884c\u3002\u51fd\u6570spin_lock_bh()\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u540c\u6837\uff0c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u53ef\u4ee5\u62a2\u5360\u4e0b\u534a\u90e8\uff0c\u56e0\u6b64\u5982\u679c\u4e0b\u534a\u90e8\u6709\u9700\u8981\u548c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u5171\u4eab\u7684\u6570\u636e\u65f6\uff0c\u4e5f\u5fc5\u987b\u7981\u6b62\u4e2d\u65ad\u3002

\u4e2d\u65ad\u5904\u7406\u662f\u53e6\u4e00\u4e2a\u8bdd\u9898\uff0c\u8bf7\u53c2\u8003\u4e2d\u65ad\u548c\u4e2d\u65ad\u5904\u7406\u3002

"},{"location":"linux/kernel/synch/#_9","title":"\u8bfb/\u5199\u81ea\u65cb\u9501","text":"

\u6709\u65f6\uff0c\u9501\u7684\u7528\u9014\u53ef\u4ee5\u660e\u786e\u5730\u5206\u4e3a\u8bfb\u548c\u5199\u4e24\u4e2a\u573a\u666f\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u9700\u8981\u5927\u91cf\u8bfb\u64cd\u4f5c\uff0c\u800c\u5199\u64cd\u4f5c\u5f88\u5c11\u7684\u60c5\u51b5\u65f6\uff0c\u5f15\u5165\u8bfb\u5199\u9501\u53ef\u4ee5\u5f88\u5927\u5730\u6539\u5584\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u8bfb\u5199\u9501\u7684\u57fa\u672c\u903b\u8f91\u662f\uff1a\u8bfb\u6a21\u5f0f\u662f\u5171\u4eab\u7684\uff0c\u5199\u6a21\u5f0f\u662f\u72ec\u5360\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u5f53\u8fdb\u884c\u5199\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u53ea\u80fd\u7531\u5355\u4e2a\u4efb\u52a1\u8fdb\u884c\u3002\u800c\u8fdb\u884c\u8bfb\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5e76\u53d1\u7684\u6267\u884c\u800c\u4e0d\u7528\u62c5\u5fc3\u5b89\u5168\u95ee\u9898\u3002\u8bfb/\u5199\u9501\u7684\u57fa\u672c\u65b9\u6cd5\u5982\u4e0b\uff1a

DEFINE_RWLOCK(rw_lock);\n//\u8bfb\u64cd\u4f5c\nread_lock(&rw_lock);\nread_unlock(&rw_lock);\n//\u5199\u64cd\u4f5c\nwrite_lock(&rw_lock);\nwrite_unlock(&rw_lock);\n

\u901a\u5e38\uff0c\u8bfb\u5199\u64cd\u4f5c\u5e94\u5904\u4e8e\u5b8c\u5168\u5206\u5272\u5f00\u7684\u4ee3\u7801\u5206\u652f\u4e2d\uff0c\u5982\u679c\u8bfb\u5199\u64cd\u4f5c\u4e0d\u80fd\u6e05\u6670\u5730\u8fa8\u522b\uff0c\u4e0d\u8981\u4f7f\u7528\u8bfb\u5199\u9501\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002\u4f8b\u5982\u5728\u6267\u884c\u8bfb\u64cd\u4f5c\u4ee3\u7801\u65f6\uff0c\u4e5f\u52a0\u5165\u4e86\u5199\u64cd\u4f5c\u3002

\u5728\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\uff0c\u5982\u679c\u53ea\u6709\u8bfb\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528read_lock()\u6765\u4fdd\u62a4\u8bfb\u64cd\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u7981\u6b62\u4e2d\u65ad\u4e86\u3002\u4f46\u662f\uff0c\u5199\u64cd\u4f5c\u7684\u4e2d\u65ad\u5fc5\u987b\u7981\u6b62\uff0c\u5426\u5219\u5c31\u4f1a\u9020\u6210\u6b7b\u9501\u3002\u56e0\u4e3a\u5199\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\u8bfb\u64cd\u4f5c\u91ca\u653e\u9501\uff0c\u800c\u8bfb\u64cd\u4f5c\u9700\u8981\u7b49\u5f85\u5199\u64cd\u4f5c\u7684\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u8fd4\u56de\u3002

\u5728\u4f7f\u7528\u8bfb\u5199\u9501\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\uff0c\u5f53\u8bfb\u9501\u88ab\u6301\u6709\u65f6\uff0c\u5199\u9501\u53ea\u80fd\u7b49\u5f85\u3002\u7136\u800c\u8bfb\u9501\u5374\u53ef\u4ee5\u7ee7\u7eed\u6210\u529f\u5730\u5360\u7528\u9501\u3002\u8fd9\u4f1a\u9020\u6210\u5199\u9501\u9965\u997f\u73b0\u8c61\u3002

\u5982\u679c\u52a0\u9501\u65f6\u95f4\u5f88\u957f\u6216\u8005\u4ee3\u7801\u5728\u6301\u6709\u9501\u65f6\u6709\u53ef\u80fd\u7761\u7720\uff0c\u90a3\u4e48\u6700\u597d\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u5904\u7406\u3002

"},{"location":"linux/kernel/synch/#_10","title":"\u987a\u5e8f\u9501","text":"

\u987a\u5e8f\u9501\uff08seqlock\uff09\u5728struct fs_struct\u4e2d\u88ab\u4f7f\u7528\u3002

"},{"location":"linux/kernel/synch/#_11","title":"\u4fe1\u53f7\u91cf","text":"

Linux\u4e2d\u7684\u4fe1\u53f7\u91cf\u662f\u4e00\u79cd\u7761\u7720\u9501\u3002\u5f53\u6709\u4e00\u4e2a\u4efb\u52a1\u8bd5\u56fe\u83b7\u5f97\u4e00\u4e2a\u5df2\u7ecf\u88ab\u5360\u7528\u7684\u4fe1\u53f7\u91cf\u65f6\uff0c\u8be5\u4efb\u52a1\u4f1a\u52a0\u5165\u7b49\u5f85\u961f\u5217\uff0c\u7136\u540e\u7761\u7720\u3002\u7531\u4e8e\u91ca\u653e\u4e86\u8d44\u6e90\u5904\u7406\u5668\u53ef\u4ee5\u53bb\u6267\u884c\u5176\u4ed6\u7684\u4ee3\u7801\u3002\u5f53\u6301\u6709\u7684\u4fe1\u53f7\u91cf\u88ab\u91ca\u653e\u65f6\uff0c\u5904\u4e8e\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u90a3\u4e2a\u4efb\u52a1\u4f1a\u88ab\u5524\u9192\uff0c\u5e76\u83b7\u5f97\u8be5\u4fe1\u53f7\u91cf\u3002

\u5b9e\u9645\u4e0a\uff0cLinux\u63d0\u4f9b\u4e24\u79cd\u4fe1\u53f7\u91cf\uff1a

  • \u5185\u6838\u4fe1\u53f7\u91cf\uff0c\u7531\u5185\u6838\u6001\u4f7f\u7528
  • System V IPC\u4fe1\u53f7\u91cf\uff0c\u7531\u7528\u6237\u6001\u4f7f\u7528

\u7531\u4e8e\u4fe1\u53f7\u91cf\u4f1a\u7761\u7720\uff0c\u56e0\u6b64\u6709\u4ee5\u4e0b\u7ed3\u8bba\uff1a

  • \u4fe1\u53f7\u91cf\u9002\u7528\u4e8e\u9501\u4f1a\u88ab\u957f\u65f6\u95f4\u5360\u6709\u7684\u573a\u666f\u3002
  • \u4fe1\u53f7\u91cf\u53ea\u80fd\u5728\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u4e2d\u65ad\u4e0a\u4e0b\u6587\u7981\u6b62\u7761\u7720\u3002
  • \u591a\u4e2a\u8fdb\u7a0b\u8bd5\u56fe\u83b7\u5f97\u4fe1\u53f7\u91cf\u4e0d\u4f1a\u6b7b\u9501\u3002
  • \u5982\u679c\u5df2\u7ecf\u5360\u7528\u4e86\u4fe1\u53f7\u91cf\uff0c\u4e0d\u80fd\u518d\u4f7f\u7528\u81ea\u65cb\u9501\uff0c\u56e0\u4e3a\u81ea\u65cb\u9501\u7981\u6b62\u7761\u7720\u3002

\u4fe1\u53f7\u91cf\u76f8\u6bd4\u81ea\u65cb\u9501\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5730\u65b9\uff0c\u5c31\u662f\u5b83\u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2acount\u503c\uff0c\u8be5count\u503c\u7b49\u540c\u4e8e\u540c\u4e00\u65f6\u95f4\u80fd\u591f\u6301\u6709\u4fe1\u53f7\u91cf\u7684\u6570\u91cf\u3002\u5982\u679c\u8fd9\u4e2a\u503c\u662f1\uff0c\u90a3\u4e48\u4fe1\u53f7\u91cf\u53c8\u88ab\u79f0\u4e3a\u4e92\u65a5\u4fe1\u53f7\u91cf\u3002\u4fe1\u53f7\u91cf\u652f\u6301\u4e24\u4e2a\u64cd\u4f5c\uff1adown()\u64cd\u4f5c\u901a\u8fc7\u5bf9\u4fe1\u53f7\u91cf\u8ba1\u6570\u51cf1\u6765\u83b7\u5f97\u5b83\uff0c\u800cup()\u64cd\u4f5c\u52a01\u6765\u91ca\u653e\u5b83\u3002

\u4fe1\u53f7\u91cf\u662f\u4e0e\u4f53\u7cfb\u7ed3\u6784\u76f8\u5173\u7684\uff0c\u5b9a\u4e49\u5728<asm/semaphore.h>\u4e2d\u3002\u521b\u5efa\u65b9\u6cd5\u5982\u4e0b\uff1a

struct semaphore name;\nsema_init(&name, count);\n

\u51fd\u6570down_interruptible()\u8bd5\u56fe\u83b7\u53d6\u6307\u5b9a\u7684\u4fe1\u53f7\u91cf\uff0c\u5982\u679c\u4e0d\u53ef\u7528\uff0c\u5219\u5c06\u8fdb\u7a0b\u8bbe\u7f6e\u4e3aTASK_INTERRUPTIBLE\u3002\u8fd9\u79cd\u8fdb\u7a0b\u72b6\u6001\u610f\u5473\u7740\u4efb\u52a1\u53ef\u4ee5\u88ab\u4fe1\u53f7\u5524\u9192\u3002\u4f7f\u7528down_trylock()\u51fd\u6570\uff0c\u5728\u4fe1\u53f7\u91cf\u5df2\u7ecf\u88ab\u5360\u7528\u65f6\uff0c\u7acb\u5373\u8fd4\u56de\u800c\u4e0d\u662f\u8ba9\u8fdb\u7a0b\u7761\u7720\u3002

"},{"location":"linux/kernel/synch/#_12","title":"\u4e92\u65a5\u4f53","text":"

\u5728\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4fe1\u53f7\u91cf\u53ea\u662f\u4f5c\u4e3a\u4e00\u4e2a\u8ba1\u6570\u4e3a1\u7684\u5141\u8bb8\u7761\u7720\u7684\u81ea\u65cb\u9501\u5b58\u5728\u3002\u4e3a\u4e86\u627e\u5230\u4e00\u4e2a\u66f4\u7b80\u5355\u4e14\u53ef\u4ee5\u7761\u7720\u7684\u9501\uff0c\u5f00\u53d1\u8005\u4eec\u5f15\u5165\u4e86\u4e92\u65a5\u4f53\uff08mutex\uff09\u3002\u5176\u884c\u4e3a\u548c\u8ba1\u6570\u4e3a1\u7684\u4fe1\u53f7\u91cf\u7c7b\u4f3c\uff0c\u4f46\u662f\u64cd\u4f5c\u7684\u63a5\u53e3\u66f4\u7b80\u5355\uff0c\u5b9e\u73b0\u4e5f\u66f4\u9ad8\u6548\u3002\u57fa\u672c\u9002\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a

DEFINE_MUTEX(mutex);\nmutex_init(&mutex);\nmutex_lock(&mutex);\nmutex_unlock(&mutex);\n

mutex\u7684\u7b80\u4ecb\u4e0e\u9ad8\u6548\u6e90\u4e8e\u76f8\u6bd4\u4f7f\u7528\u4fe1\u53f7\u91cf\u66f4\u591a\u7684\u53d7\u9650\u6027\uff1a

  • \u4efb\u4f55\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u4efb\u52a1\u6301\u6709mutex\u3002
  • \u5fc5\u987b\u7531\u4e0a\u9501\u8005\u89e3\u9501\u2014\u2014\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e0a\u9501\uff0c\u800c\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u89e3\u9501\u3002
  • \u9012\u5f52\u5730\u4e0a\u9501\u548c\u89e3\u9501\u65f6\u4e0d\u88ab\u5141\u8bb8\u7684\u3002
  • \u5f53\u6301\u6709\u4e00\u4e2amutex\u65f6\uff0c\u8fdb\u7a0b\u4e0d\u80fd\u9000\u51fa\u3002
  • mutex\u4e0d\u80fd\u5728\u4e2d\u65ad\u6216\u8005\u4e0b\u534a\u90e8\u4e2d\u4f7f\u7528\u3002
\u9700\u6c42 \u5efa\u8bae\u7684\u52a0\u9501\u65b9\u5f0f \u4f4e\u5f00\u9500\u52a0\u9501 \u4f18\u5148\u4f7f\u7528\u81ea\u65cb\u9501 \u77ed\u671f\u52a0\u9501 \u4f18\u5148\u4f7f\u7528\u81ea\u65cb\u9501 \u957f\u671f\u52a0\u9501 \u4f18\u5148\u4f7f\u7528\u4e92\u65a5\u4f53 \u4e2d\u65ad\u4e0a\u4e0b\u6587\u52a0\u9501 \u4f7f\u7528\u81ea\u65cb\u9501 \u6301\u6709\u9501\u9700\u8981\u7761\u7720 \u4f7f\u7528\u4e92\u65a5\u4f53"},{"location":"linux/kernel/synch/#_13","title":"\u5b8c\u6210\u53d8\u91cf","text":"

\u5982\u679c\u5728\u5185\u6838\u4e2d\u4e00\u4e2a\u4efb\u52a1\u9700\u8981\u53d1\u9001\u4fe1\u53f7\u901a\u77e5\u53e6\u4e00\u4e2a\u4efb\u52a1\u53d1\u751f\u4e86\u67d0\u79cd\u7279\u5b9a\u4e8b\u4ef6\uff0c\u6b64\u65f6\u53ef\u4ee5\u7528\u5b8c\u6210\u53d8\u91cf\uff08completion variable\uff09\u3002\u5f53\u67d0\u4e2a\u4efb\u52a1\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u4f1a\u4f7f\u7528\u5b8c\u6210\u53d8\u91cf\u53bb\u5524\u9192\u6b63\u5728\u7b49\u5f85\u7684\u4efb\u52a1\u3002

\u5b8c\u6210\u53d8\u91cf\u7531\u7ed3\u6784\u4f53completion\u8868\u793a\uff0c\u5b9a\u4e49\u5728<linux/completion.h>\u4e2d\u3002\u5176\u521b\u5efa\u65b9\u6cd5\u5982\u4e0b\uff1a

DECLARE_COMPLETION(comp);\n

\u6216\u8005\u4f7f\u7528init_completion()\u52a8\u6001\u521b\u5efa\u3002\u9700\u8981\u7b49\u5f85\u7684\u4efb\u52a1\u8c03\u7528wait_for_completion()\u6765\u7b49\u5f85\u7279\u5b9a\u4e8b\u4ef6\u3002\u5f53\u4e8b\u4ef6\u53d1\u751f\u540e\uff0c\u4ea7\u751f\u4e8b\u4ef6\u7684\u4efb\u52a1\u8c03\u7528complete()\u6765\u53d1\u9001\u4fe1\u53f7\u5524\u9192\u6b63\u5728\u7b49\u5f85\u7684\u4efb\u52a1\u3002

"},{"location":"linux/kernel/synch/#rcu","title":"RCU\u673a\u5236","text":"

\u8bfb\u53d6-\u590d\u5236-\u66f4\u65b0\uff08read-copy-update\uff09\u662f\u4e00\u79cd\u9ad8\u7ea7\u4e92\u65a5\u673a\u5236\uff0c\u4e00\u822c\u7528\u4e0d\u5230\u4f46\u662f\u6211\u4eec\u5bf9\u8fd9\u4e2a\u6982\u5ff5\u5f97\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u4e86\u89e3\u3002RCU\u673a\u5236\u4e3b\u8981\u9488\u5bf9\u8bfb\u53d6\u7ecf\u5e38\u53d1\u751f\u3001\u4f46\u662f\u5199\u5165\u5f88\u5c11\u7684\u60c5\u51b5\u3002\u5728\u9700\u8981\u4fee\u6539\u6570\u636e\u65f6\uff0c\u5199\u5165\u7ebf\u7a0b\u9996\u5148\u590d\u5236\u4e00\u4efd\uff0c\u7136\u540e\u4fee\u6539\u526f\u672c\u3002\u5176\u4ed6\u7ebf\u7a0b\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u4ecd\u7136\u6307\u5411\u539f\u59cb\u7684\u5171\u4eab\u6570\u636e\u5730\u5740\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u5728\u5199\u64cd\u4f5c\u53d1\u751f\u524d\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4ecd\u7136\u53ef\u4ee5\u8bfb\u53d6\u5230\u4e00\u81f4\u7684\u6570\u636e\u3002\u76f4\u5230\u66f4\u65b0\u7ebf\u7a0b\u5b8c\u6210\u4e86\u6570\u636e\u7684\u4fee\u6539\uff0c\u5e76\u901a\u8fc7\u7279\u5b9a\u7684API\u5c06\u66f4\u65b0\u540e\u7684\u6570\u636e\u6307\u9488\u8d4b\u503c\u56de\u539f\u6765\u7684\u5171\u4eab\u6570\u636e\u4f4d\u7f6e\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u201c\u66f4\u65b0\u5b8c\u6210\u201d\u3002\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\u7528\u4e8e\u4fdd\u8bc1\u53ea\u6709\u5728\u6570\u636e\u7ed3\u6784\u88ab\u4fee\u6539\u4e4b\u540e\uff0c\u5df2\u66f4\u65b0\u7684\u6307\u9488\u624d\u5bf9\u5176\u4ed6CPU\u53ef\u89c1\u3002\u8bfb\u53d6\u7aef\u7684\u4ee3\u7801\u5fc5\u987b\u653e\u7f6e\u4e8ercu_read_lock()\u548crcu_read_unlock()\u4e4b\u95f4\u3002

\u4f7f\u7528RCU\u6280\u672f\u7684\u96be\u70b9\u5728\u4e8e\uff1a\u5199\u5165\u7aef\u4fee\u6539\u6307\u9488\u65f6\u4e0d\u80fd\u7acb\u523b\u91ca\u653e\u6570\u636e\u7ed3\u6784\u7684\u65e7\u6307\u9488\uff0c\u56e0\u4e3a\u8fd8\u6709\u5176\u4ed6\u7684\u8bfb\u53d6\u7aef\u5728\u4f7f\u7528\u3002\u53ea\u6709\u5f53\u6240\u6709\u7684\u8bfb\u53d6\u7aef\u6267\u884c\u5b8c\u5b8frcu_read_unlcok()\u4e4b\u540e\uff0c\u624d\u53ef\u4ee5\u91ca\u653e\u65e7\u6307\u9488\u3002\u5199\u5165\u7aef\u8c03\u7528\u51fd\u6570call_rcu()\u6765\u91ca\u653e\u65e7\u6307\u9488\u3002

"},{"location":"linux/kernel/synch/#_14","title":"\u7981\u6b62\u62a2\u5360","text":"

Linux\u662f\u62a2\u5360\u5f0f\u5185\u6838\uff0c\u5176\u4e3b\u8981\u7279\u70b9\u662f\uff1a\u4e00\u4e2a\u5728\u5185\u6838\u6001\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u53ef\u80fd\u5728\u6267\u884c\u5185\u6838\u6001\u51fd\u6570\u671f\u95f4\u88ab\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u62a2\u5360\u3002\u5728\u8fdb\u7a0bA\u6267\u884c\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u65f6\uff08\u6b21\u5403\u4f4d\u4e8e\u5185\u6838\u6001\uff09\uff0c\u4e00\u4e2a\u66f4\u6709\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0bB\u53d8\u4e3a\u53ef\u6267\u884c\u72b6\u6001\u3002\u5982\u679c\u5185\u6838\u662f\u53ef\u62a2\u5360\u7684\uff0c\u5c31\u4f1a\u53d1\u751f\u5f3a\u5236\u6027\u4efb\u52a1\u5207\u6362\uff0c\u8ba9B\u53d6\u4ee3A\u3002\u518d\u6bd4\u5982\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u5df2\u7ecf\u7528\u5b8c\u4e86\u5b83\u7684\u65f6\u95f4\u7247\u914d\u989d\uff0c\u62a2\u5360\u5f0f\u5185\u6838\u4f1a\u7acb\u523b\u8ba9\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u53d6\u4ee3\u5b83\u3002

\u5185\u6838\u4f7f\u7528thread_info\u4e2d\u7684preempt_count\u5b57\u6bb5\u8868\u793a\u62a2\u5360\u8ba1\u6570\u3002\u5f53\u8fd9\u4e2a\u503c\u5927\u4e8e0\u65f6\uff0c\u5c31\u7981\u6b62\u5185\u6838\u62a2\u5360\u3002\u5b83\u5728\u4ee5\u4e0b\u4efb\u4f55\u4e00\u79cd\u60c5\u51b5\u53d1\u751f\u65f6\uff0c\u53d6\u503c\u90fd\u5927\u4e8e0\uff1a

  1. \u5185\u6838\u6b63\u5728\u6267\u884c\u4e2d\u65ad\u670d\u52a1\u7a0b\u5e8f\u3002

  2. \u53ef\u5ef6\u8fdf\u51fd\u6570\u88ab\u7981\u6b62\uff08\u5f53\u5185\u6838\u6b63\u5728\u6267\u884c\u8f6f\u4e2d\u65ad\u6216tasklet\uff09\u3002

  3. \u663e\u793a\u8bbe\u7f6e\u62a2\u5360\u8ba1\u6570\u5668\u4e3a\u6b63\u6570\u3002

\u5b8f \u8bf4\u660e preempt_count() \u8fd4\u56de\u62a2\u5360\u8ba1\u6570\u503c preempt_disable() \u4f7f\u62a2\u5360\u8ba1\u6570+1 preempt_enable() \u4f7f\u62a2\u5360\u8ba1\u6570-1\uff0c\u5e76\u68c0\u67e5TIF_NEED_RESCHED\u6807\u5fd7 preempt_enable_no_resched() \u4f7f\u62a2\u5360\u8ba1\u6570-1

\u4e0a\u9762\u7b2c\u4e09\u4e2a\u5b8f\u9700\u8981\u8bf4\u660e\u4e00\u4e0b\u3002preempt_enable()\u5b8f\u9996\u5148\u9012\u51cf\u62a2\u5360\u8ba1\u6570\u5668\uff0c\u5e76\u4e14\u68c0\u67e5TIF_NEED_RESCHED\u6807\u5fd7\u662f\u5426\u88ab\u8bbe\u7f6e\u3002\u5f53\u8fd9\u4e2a\u6807\u5fd7\u4e3a1\u65f6\u8868\u793a\u9700\u8981\u6267\u884c\u8c03\u5ea6\u7a0b\u5e8f\u3002\u4e8e\u662f\u6211\u4eec\u8fd8\u4f1a\u8c03\u7528preempt_schedule()\u6765\u8c03\u7528schedule()\u9009\u62e9\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u8fd0\u884c\u3002

"},{"location":"linux/kernel/synch/#_15","title":"\u987a\u5e8f\u4e0e\u5c4f\u969c","text":"

\u4e3a\u4ec0\u4e48\u9700\u8981\u987a\u5e8f\u548c\u5c4f\u969c\u6307\u4ee4\uff1f

  • \u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\u5bfc\u81f4\u7684\u91cd\u6392\uff1a\u73b0\u4ee3\u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c\u5404\u79cd\u4f18\u5316\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u5305\u62ec\u6307\u4ee4\u91cd\u6392\u3002\u5728\u4e0d\u8003\u8651\u5185\u5b58\u64cd\u4f5c\u987a\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u6539\u53d8\u6307\u4ee4\u7684\u6267\u884c\u987a\u5e8f\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u7684\u884c\u4e3a\u4e0e\u9884\u671f\u4e0d\u7b26\u3002\u901a\u8fc7\u4f7f\u7528\u987a\u5e8f\u548c\u5c4f\u969c\u6307\u4ee4\uff0c\u53ef\u4ee5\u9650\u5236\u7f16\u8bd1\u5668\u5bf9\u67d0\u4e9b\u5173\u952e\u4ee3\u7801\u6bb5\u7684\u4f18\u5316\u3002

  • \u4fdd\u8bc1\u5e76\u53d1\u64cd\u4f5c\u7684\u4e00\u81f4\u6027\uff1a\u5728\u591a\u6838\u5904\u7406\u5668\u548c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8c03\u5ea6\u5668\u53ef\u80fd\u4f1a\u5728\u4e0d\u540c\u7684\u5904\u7406\u5668\u6838\u5fc3\u4e0a\u5e76\u884c\u6267\u884c\u591a\u4e2a\u7ebf\u7a0b\u3002\u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u9700\u8981\u786e\u4fdd\u6240\u6709\u6838\u5fc3\u4e0a\u7684\u5185\u5b58\u64cd\u4f5c\u90fd\u6309\u7167\u7a0b\u5e8f\u6307\u5b9a\u7684\u987a\u5e8f\u6267\u884c\u3002

  • \u89e3\u51b3CPU\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\uff1aCPU\u7f13\u5b58\u662f\u5904\u7406\u5668\u901f\u5ea6\u7684\u5173\u952e\u90e8\u5206\uff0c\u4f46\u5b83\u7684\u5b58\u5728\u4e5f\u5e26\u6765\u4e86\u7f13\u5b58\u4e00\u81f4\u6027\u7684\u95ee\u9898\u3002\u5f53\u4e00\u4e2aCPU\u6838\u5fc3\u5199\u5165\u6570\u636e\uff0c\u800c\u8fd9\u4e2a\u6570\u636e\u53c8\u88ab\u53e6\u4e00\u4e2a\u6838\u5fc3\u7684\u7f13\u5b58\u6240\u7f13\u5b58\u65f6\uff0c\u6ca1\u6709\u5c4f\u969c\u7684\u8bdd\uff0c\u53e6\u4e00\u4e2a\u6838\u5fc3\u53ef\u80fd\u4f1a\u8bfb\u53d6\u5230\u65e7\u7684\u6570\u636e\u7248\u672c\u3002\u5185\u5b58\u5c4f\u969c\u80fd\u591f\u786e\u4fdd\u6240\u6709\u6838\u5fc3\u770b\u5230\u5185\u5b58\u64cd\u4f5c\u7684\u6700\u7ec8\u4e00\u81f4\u6027\u3002

Linux\u4e2d\u7684\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\uff1a

\u5b8f \u8bf4\u660e mb() \u9002\u7528\u4e8eMP\u548cUP\u7684\u5185\u5b58\u5c4f\u969c rmb() \u9002\u7528\u4e8eMP\u548cUP\u7684\u8bfb\u5185\u5b58\u5c4f\u969c wmb() \u9002\u7528\u4e8eMP\u548cUP\u7684\u5199\u5185\u5b58\u5c4f\u969c smp_mb() \u4ec5\u9002\u7528\u4e8eMP\u7684\u5185\u5b58\u5c4f\u969c smp_rmb() \u4ec5\u9002\u7528\u4e8eMP\u7684\u8bfb\u5185\u5b58\u5c4f\u969c smp_wmb() \u4ec5\u9002\u7528\u4e8eMP\u7684\u5199\u5185\u5b58\u5c4f\u969c

\u6b64\u7c7b\u6307\u4ee4\u4e0e\u4f53\u7cfb\u7ed3\u6784\u5bc6\u5207\u76f8\u5173\uff0c\u8bf7\u53c2\u8003ARM\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\u3002

"},{"location":"linux/kernel/vfs/","title":"\u865a\u62df\u6587\u4ef6\u7cfb\u7edf","text":"

\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff08VFS\uff09\u662fLinux\u5185\u6838\u7684\u4e00\u4e2a\u91cd\u8981\u5b50\u7cfb\u7edf\uff0c\u5b83\u4e3a\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u4e00\u5957\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u4ee5\u5b9e\u73b0\u5bf9\u4e0d\u540c\u6587\u4ef6\u7cfb\u7edf\u7684\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd9\u4e9b\u63a5\u53e3\uff0c\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u53ef\u4ee5\u91c7\u7528\u6807\u51c6\u7684\u7cfb\u7edf\u8c03\u7528\u6765\u6267\u884c\u6587\u4ef6\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u5173\u5fc3\u8fd9\u4e9b\u64cd\u4f5c\u5b9e\u9645\u662f\u5728\u54ea\u79cd\u6587\u4ef6\u7cfb\u7edf\u4e0a\u6267\u884c\u7684\u3002VFS\u4f7f\u5f97\u5185\u6838\u80fd\u591f\u652f\u6301\u591a\u79cd\u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b\uff0c\u5982ext4\u3001ReiserFS\u3001XFS\u7b49\u3002\u5bf9\u4e8e\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7cfb\u7edf\u770b\u8d77\u6765\u90fd\u662f\u4e00\u6837\u7684\uff0c\u56e0\u4e3aVFS\u9690\u85cf\u4e86\u5e95\u5c42\u6587\u4ef6\u7cfb\u7edf\u7684\u5dee\u5f02\u3002VFS\u7684\u8bbe\u8ba1\u4f7f\u5f97Linux\u5185\u6838\u80fd\u591f\u7075\u6d3b\u5730\u652f\u6301\u591a\u79cd\u6587\u4ef6\u7cfb\u7edf\uff0c\u540c\u65f6\u4fdd\u6301\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002\u8fd9\u5bf9\u4e8eLinux\u7cfb\u7edf\u7684\u53ef\u6269\u5c55\u6027\u548c\u7075\u6d3b\u6027\u81f3\u5173\u91cd\u8981\u3002

\u4e3a\u4e86\u652f\u6301\u591a\u6587\u4ef6\u7cfb\u7edf\uff0cVFS\u63d0\u4f9b\u4e86\u4e00\u4e2a\u901a\u7528\u6587\u4ef6\u7cfb\u7edf\u6a21\u578b\u7684\u62bd\u8c61\u5c42\uff0c\u8be5\u6a21\u578b\u56ca\u62ec\u4e86\u4efb\u4f55\u6587\u4ef6\u7cfb\u7edf\u5e38\u7528\u7684\u529f\u80fd\u548c\u884c\u4e3a\u3002\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u53ea\u9700\u8981\u4f7f\u7528VFS\u66b4\u9732\u7684\u63a5\u53e3\uff0c\u800c\u65e0\u987b\u5173\u5fc3\u5e95\u5c42\u6587\u4ef6\u7cfb\u7edf\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u6bd4\u5982\u8c03\u7528\u4e00\u4e2a\u7b80\u5355\u7684write()\u51fd\u6570\uff0c\u9996\u5148\u7531VFS\u8f6c\u6362\u4e3asys_write()\uff0c\u518d\u6839\u636efd\u6587\u4ef6\u63cf\u8ff0\u7b26\u6240\u5728\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u627e\u5230\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u6700\u540e\u518d\u6267\u884c\u8be5\u64cd\u4f5c\u3002

\u901a\u8fc7\u8fd9\u5f20\u56fe\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cVFS\u652f\u6301\u591a\u79cd\u4e0d\u540c\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u6309\u7167\u5b58\u50a8\u4f4d\u7f6e\u7684\u4e0d\u540c\uff0c\u8fd9\u4e9b\u6587\u4ef6\u7cfb\u7edf\u53ef\u4ee5\u5206\u4e3a\u4e09\u7c7b\uff1a

  • \u57fa\u4e8e\u78c1\u76d8\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u4e5f\u5c31\u662f\u628a\u6570\u636e\u76f4\u63a5\u5b58\u50a8\u5728\u672c\u5730\u78c1\u76d8\u4e2d\u3002\u5e38\u89c1\u7684\u7531Ext4\u3001XFS\u3001OverlayFS\u7b49\u3002

  • \u57fa\u4e8e\u5185\u5b58\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u4e0d\u9700\u8981\u78c1\u76d8\u5206\u914d\u5b58\u50a8\u7a7a\u95f4\uff0c\u4f46\u4f1a\u5360\u7528\u989d\u5916\u7684\u5185\u5b58\u3002/proc\u6587\u4ef6\u7cfb\u7edf\u5c31\u5c5e\u4e8e\u8fd9\u4e00\u7c7b\u3002

  • \u57fa\u4e8e\u7f51\u7edc\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u7528\u6765\u8bbf\u95ee\u5176\u4ed6\u8ba1\u7b97\u673a\u6570\u636e\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u6bd4\u5982NFS\u3002

\u4e0d\u7ba1\u662f\u54ea\u7c7b\u6587\u4ef6\u7cfb\u7edf\uff0c\u90fd\u9700\u8981\u6302\u8f7d\u5230VFS\u76ee\u5f55\u6811\u4e2d\u7684\u67d0\u4e2a\u5b50\u76ee\u5f55\uff0c\u624d\u80fd\u88ab\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u8bbf\u95ee\u5230\u3002\u62ff\u57fa\u4e8e\u78c1\u76d8\u7684\u6587\u4ef6\u7cfb\u7edf\u4e3a\u4f8b\uff0c\u5728\u5b89\u88c5\u7cfb\u7edf\u65f6\uff0c\u5c31\u9700\u8981\u6307\u5b9a\u6302\u8f7d\u70b9\u2014\u2014\u6839\u76ee\u5f55\uff08/\uff09\uff0c\u5728\u6839\u76ee\u5f55\u4e0b\u518d\u628a\u5176\u4ed6\u6587\u4ef6\u7cfb\u7edf\u6302\u8f7d\u8fdb\u6765\u3002

Linux\u7cfb\u7edf\u603b\u5171\u91c7\u7528\u4e86\u56db\u79cd\u6838\u5fc3\u6982\u5ff5\u6765\u62bd\u8c61\u6587\u4ef6\u7cfb\u7edf\uff1a\u6587\u4ef6\u3001\u76ee\u5f55\u9879\u3001\u7d22\u5f15\u70b9\u548c\u6302\u8f7d\u70b9\u3002\u8fd9\u4e9b\u6982\u5ff5\u5171\u540c\u6784\u5efa\u4e86\u4e00\u4e2a\u5206\u5c42\u7684\u6570\u636e\u5b58\u50a8\u7ed3\u6784\uff0c\u7528\u4e8e\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u53ca\u76f8\u5173\u7684\u63a7\u5236\u4fe1\u606f\u3002Linux\u6587\u4ef6\u7cfb\u7edf\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u7edf\u4e00\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u6240\u6709\u7684\u6587\u4ef6\u88ab\u6302\u8f7d\u5230\u4e00\u4e2a\u5168\u5c40\u7684\u5c42\u6b21\u7ed3\u6784\u4e2d\uff0c\u5f62\u6210\u4e00\u4e2a\u7c7b\u4f3c\u6839\u6587\u4ef6\u7cfb\u7edf\u7684\u7ed3\u6784\u3002\u8fd9\u4e0eWindows\u7cfb\u7edf\u5c06\u6587\u4ef6\u7cfb\u7edf\u5212\u5206\u4e0d\u540c\u7684\u5206\u533a\u4e0d\u540c\u3002

Linux\u7cfb\u7edf\u533a\u5206\u6587\u4ef6\u672c\u8eab\u548c\u6587\u4ef6\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u8bbf\u95ee\u63a7\u5236\u6743\u9650\u3001\u5927\u5c0f\u3001\u6240\u6709\u8005\u3001\u521b\u5efa\u65f6\u95f4\u7b49\u3002\u8fd9\u4e9b\u76f8\u5173\u4fe1\u606f\uff0c\u6709\u65f6\u88ab\u79f0\u4f5c\u6587\u4ef6\u7684\u5143\u6570\u636e\uff0c\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u2014\u2014\u7d22\u5f15\u8282\u70b9\uff08inode\uff09\u3002

"},{"location":"linux/kernel/vfs/#vfs","title":"VFS\u5bf9\u8c61","text":"

VFS\u4f7f\u7528\u4e00\u7ec4\u6570\u636e\u7ed3\u6784\u6765\u4ee3\u8868\u901a\u7528\u6587\u4ef6\u5bf9\u8c61\uff0c\u5b83\u4eec\u5206\u522b\u662f\uff1a

  • \u8d85\u7ea7\u5757\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u5177\u4f53\u7684\u5df2\u5b89\u88c5\u7684\u6587\u4ef6\u7cfb\u7edf\u3002
  • \u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u5177\u4f53\u6587\u4ef6\u3002
  • \u76ee\u5f55\u9879\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u76ee\u5f55\u9879\uff0c\u662f\u4e00\u4e2a\u8def\u5f84\u7684\u7ec4\u6210\u90e8\u5206\u3002
  • \u6587\u4ef6\u5bf9\u8c61\uff0c\u4ee3\u8868\u7531\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u3002

\u6bcf\u4e2a\u901a\u7528\u5bf9\u8c61\u90fd\u5305\u542b\u4e00\u4e2a\u64cd\u4f5c\u5bf9\u8c61\uff0c\u63cf\u8ff0\u4e86\u5185\u6838\u5bf9\u901a\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u2014\u2014super_operations\u3001inode_operations\u3001dentry_operations\u3001file_operations\u3002

"},{"location":"linux/kernel/vfs/#_2","title":"\u8d85\u7ea7\u5757\u5bf9\u8c61","text":"

\u8d85\u7ea7\u5757\u5bf9\u8c61\u662f\u7528\u6765\u63cf\u8ff0\u4e00\u4e2a\u5df2\u5b89\u88c5\u7684\u6587\u4ef6\u7cfb\u7edf\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5305\u542b\u4e86\u6587\u4ef6\u7cfb\u7edf\u7684\u5173\u952e\u5143\u6570\u636e\uff0c\u8fd9\u4e9b\u5143\u6570\u636e\u7528\u4e8e\u5185\u6838\u7ba1\u7406\u548c\u7ef4\u62a4\u6587\u4ef6\u7cfb\u7edf\u3002\u8d85\u7ea7\u5757\u5bf9\u8c61\u662f\u6587\u4ef6\u7cfb\u7edf\u5728\u5185\u6838\u4e2d\u8868\u793a\u81ea\u5df1\u7684\u65b9\u5f0f\uff0c\u5b83\u63d0\u4f9b\u4e86\u6587\u4ef6\u7cfb\u7edf\u7684\u603b\u4f53\u89c6\u56fe\u3002\u8d85\u7ea7\u5757\u5bf9\u8c61\u7531super_block\u7ed3\u6784\u4f53\u8868\u793a\uff0c\u901a\u8fc7alloc_super()\u51fd\u6570\u521b\u5efa\u5e76\u521d\u59cb\u5316\u3002\u5728\u6587\u4ef6\u7cfb\u7edf\u5b89\u88c5\u65f6\uff0c\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u4ee5\u4fbf\u4ece\u78c1\u76d8\u4e2d\u8bfb\u53d6\u6587\u4ef6\u7cfb\u7edf\u8d85\u7ea7\u5757\u3002\u8d85\u7ea7\u5757\u64cd\u4f5c\u7531super_operations\u7ed3\u6784\u4f53\u8868\u793a\uff0c\u8be5\u7ed3\u6784\u4f53\u4e2d\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u4e2a\u6307\u5411\u8d85\u7ea7\u5757\u64cd\u4f5c\u51fd\u6570\u7684\u6307\u9488\u3002

"},{"location":"linux/kernel/vfs/#_3","title":"\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61","text":"

\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u4ee3\u8868\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u903b\u8f91\u8868\u793a\uff0c\u5305\u542b\u4e86\u5173\u4e8e\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5143\u6570\u636e\uff0c\u8fd9\u4e9b\u5143\u6570\u636e\u63cf\u8ff0\u4e86\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u5c5e\u6027\uff0c\u5982\u6743\u9650\u3001\u6240\u6709\u8005\u3001\u6587\u4ef6\u5927\u5c0f\u3001\u6700\u540e\u8bbf\u95ee\u548c\u4fee\u6539\u65f6\u95f4\u7b49\u3002\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u7531struct inode\u7ed3\u6784\u4f53\u6765\u8868\u793a\u3002\u6bcf\u4e2a\u6587\u4ef6\u7cfb\u7edf\u90fd\u6709\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u8868\uff0c\u8be5\u8868\u5305\u542b\u4e86\u6240\u6709\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u5f53\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u901a\u8fc7\u6807\u51c6\u7684\u6587\u4ef6\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\uff08\u5982 open()\u3001read()\u3001write()\u7b49\uff09\u4e0e\u6587\u4ef6\u7cfb\u7edf\u4ea4\u4e92\u65f6\uff0c\u5b83\u4eec\u5b9e\u9645\u4e0a\u662f\u5728\u4e0e\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u8fdb\u884c\u4ea4\u4e92\u3002

\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u4ee3\u8868\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6\uff0c\u5b83\u53ef\u4ee5\u662f\u8bbe\u5907\u6216\u7ba1\u9053\u8fd9\u6837\u7684\u7279\u6b8a\u6587\u4ef6\u3002\u5728struct inode\u7ed3\u6784\u4f53\u4e2d\u6709\u4e00\u4e9b\u548c\u7279\u6b8a\u6587\u4ef6\u76f8\u5173\u7684\u9879\uff0c\u6bd4\u5982i_pipe\u6307\u5411\u6709\u540d\u7ba1\u9053\uff0ci_bdev\u6307\u5411\u5757\u8bbe\u5907\uff0ci_cdev\u6307\u5411\u5b57\u7b26\u8bbe\u5907\u3002\u8fd9\u4e09\u4e2a\u6307\u9488\u88ab\u653e\u5728\u4e00\u4e2a\u5171\u7528\u4f53\u4e2d\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u6bcf\u6b21\u53ea\u80fd\u8868\u793a\u5176\u4e2d\u4e00\u79cd\u3002

\u4e0e\u8d85\u7ea7\u5757\u7c7b\u4f3c\uff0c\u7d22\u5f15\u8282\u70b9\u64cd\u4f5c\u7531inode_operations\u7ed3\u6784\u4f53\u8868\u793a\u3002

"},{"location":"linux/kernel/vfs/#_4","title":"\u76ee\u5f55\u9879\u5bf9\u8c61","text":"

\u5728VFS\u4e2d\uff0c\u76ee\u5f55\u88ab\u5f53\u4f5c\u6587\u4ef6\u6765\u5904\u7406\uff0c\u8fd9\u5141\u8bb8\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u5bf9\u6587\u4ef6\u548c\u76ee\u5f55\u8fdb\u884c\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5728\u8def\u5f84/bin/vi\u4e2d\uff0cbin\u548cvi\u90fd\u88ab\u89c6\u4e3a\u6587\u4ef6\u3002\u8fd9\u91cc\u7684bin\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u76ee\u5f55\u6587\u4ef6\uff0c\u5b83\u5305\u542b\u4e86\u6307\u5411\u7cfb\u7edf\u4e2d\u53ef\u6267\u884c\u6587\u4ef6\u7684\u94fe\u63a5\uff0c\u800cvi\u662f\u4e00\u4e2a\u666e\u901a\u6587\u4ef6\uff0c\u5305\u542b\u4e86\u6587\u672c\u7f16\u8f91\u5668\u7684\u6e90\u4ee3\u7801\u3002

\u5728VFS\u4e2d\uff0c\u8def\u5f84\u4e2d\u7684\u6bcf\u4e2a\u7ec4\u6210\u90e8\u5206\uff08\u65e0\u8bba\u662f\u666e\u901a\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\uff09\u90fd\u662f\u7531\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u8868\u793a\u7684\u3002

\u4e3a\u4e86\u7b80\u5316\u8def\u5f84\u540d\u67e5\u627e\u7b49\u76ee\u5f55\u76f8\u5173\u7684\u64cd\u4f5c\uff0cVFS\u5f15\u5165\u4e86\u76ee\u5f55\u9879\u7684\u6982\u5ff5\u3002\u76ee\u5f55\u9879\u5bf9\u8c61\u662f\u8def\u5f84\u4e2d\u7684\u4e00\u4e2a\u7279\u5b9a\u90e8\u5206\uff0c\u5b83\u5305\u542b\u4e86\u5bf9\u7d22\u5f15\u8282\u70b9\u5bf9\u8c61\u7684\u5f15\u7528\u4ee5\u53ca\u6587\u4ef6\u540d\u548c\u5176\u4ed6\u76f8\u5173\u4fe1\u606f\u3002\u8fd9\u6837\uff0cVFS\u5728\u8fdb\u884c\u8def\u5f84\u540d\u67e5\u627e\u65f6\uff0c\u5c31\u4e0d\u5fc5\u5bf9\u6574\u4e2a\u8def\u5f84\u8fdb\u884c\u9010\u5b57\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u800c\u662f\u53ef\u4ee5\u57fa\u4e8e\u76ee\u5f55\u9879\u5bf9\u8c61\u5feb\u901f\u5b9a\u4f4d\u5230\u76ee\u6807\u6587\u4ef6\u6216\u76ee\u5f55\u3002

\u76ee\u5f55\u9879\u5bf9\u8c61\u8fd8\u5305\u62ec\u4e86\u6302\u8f7d\u70b9\uff0c\u8fd9\u662f\u6587\u4ef6\u7cfb\u7edf\u6302\u8f7d\u65f6\u7684\u6839\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5728\u8def\u5f84/mnt/cdrom/foo \u4e2d\uff0c/\u3001mnt\u3001cdrom \u548cfoo\u90fd\u662f\u76ee\u5f55\u9879\u5bf9\u8c61\u3002VFS\u5728\u6267\u884c\u76ee\u5f55\u64cd\u4f5c\u65f6\u4f1a\u73b0\u573a\u521b\u5efa\u76ee\u5f55\u9879\u5bf9\u8c61\u3002

\u76ee\u5f55\u9879\u5bf9\u8c61\u7531struct dentry\u7ed3\u6784\u4f53\u8868\u793a\u3002

\u7531\u4e8e\u6587\u4ef6\u7684\u8bbf\u95ee\u5448\u73b0\u7a7a\u95f4\u548c\u65f6\u95f4\u7684\u5c40\u90e8\u6027\uff0c\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u8bbf\u95ee\u6587\u4ef6\u7684\u6027\u80fd\uff0cVFS\u5bf9\u4e8e\u76ee\u5f55\u9879\u548c\u7d22\u5f15\u8282\u70b9\u8fdb\u884c\u4e86\u7f13\u5b58\uff0c\u4ee5\u51cf\u5c11\u5bf9\u4e8e\u78c1\u76d8\u7684\u8bbf\u95ee\u64cd\u4f5c\u3002\u76ee\u5f55\u9879\u7f13\u5b58\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a

  1. \u7f13\u5b58\u6570\u636e\uff1a\u5f53\u5185\u6838\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u65f6\uff0c\u5b83\u4f1a\u9996\u5148\u68c0\u67e5\u76ee\u5f55\u9879\u7f13\u5b58\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u8be5\u76ee\u5f55\u9879\u7684\u6570\u636e\u3002\u5982\u679c\u6570\u636e\u5728\u7f13\u5b58\u4e2d\uff0c\u5185\u6838\u5c06\u76f4\u63a5\u4f7f\u7528\u7f13\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4ece\u78c1\u76d8\u8bfb\u53d6\u3002

  2. \u7f13\u5b58\u66f4\u65b0\uff1a\u5f53\u5185\u6838\u4fee\u6539\u4e86\u76ee\u5f55\u9879\u7684\u6570\u636e\uff08\u4f8b\u5982\uff0c\u521b\u5efa\u3001\u5220\u9664\u6216\u91cd\u547d\u540d\u6587\u4ef6\u6216\u76ee\u5f55\uff09\u65f6\uff0c\u5b83\u4f1a\u5c06\u4fee\u6539\u540e\u7684\u6570\u636e\u5199\u5165\u76ee\u5f55\u9879\u7f13\u5b58\u3002\u8fd9\u6837\uff0c\u4e0b\u4e00\u6b21\u8bbf\u95ee\u76f8\u540c\u7684\u6570\u636e\u65f6\uff0c\u5185\u6838\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u800c\u4e0d\u662f\u518d\u6b21\u8bbf\u95ee\u78c1\u76d8\u3002

  3. \u7f13\u5b58\u6e05\u7406\uff1a\u4e3a\u4e86\u4fdd\u6301\u7f13\u5b58\u7684\u6570\u636e\u662f\u6700\u65b0\u7684\uff0c\u5185\u6838\u4f1a\u5b9a\u671f\u6216\u6839\u636e\u9700\u8981\u6e05\u7406\u7f13\u5b58\uff0c\u79fb\u9664\u4e0d\u518d\u5f15\u7528\u7684\u76ee\u5f55\u9879\u6570\u636e\u3002

  4. \u7f13\u5b58\u4e00\u81f4\u6027\uff1a\u5728\u591a\u6838\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u4fdd\u6301\u7f13\u5b58\u7684\u4e00\u81f4\u6027\uff0c\u5185\u6838\u4f1a\u4f7f\u7528\u5404\u79cd\u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae\uff08\u5982MESI\u534f\u8bae\uff09\u6765\u540c\u6b65\u7f13\u5b58\u6570\u636e\u3002

struct dentry_operations\u7ed3\u6784\u4f53\u6307\u660e\u4e86VFS\u64cd\u4f5c\u76ee\u5f55\u9879\u7684\u6240\u6709\u65b9\u6cd5\u3002

"},{"location":"linux/kernel/vfs/#_5","title":"\u6587\u4ef6\u5bf9\u8c61","text":"

\u6587\u4ef6\u5bf9\u8c61\u662f\u8fdb\u7a0b\u5df2\u6253\u5f00\u7684\u6587\u4ef6\u5728\u5185\u5b58\u4e2d\u7684\u8868\u793a\uff0c\u8be5\u5bf9\u8c61\u7531open()\u521b\u5efa\uff0cclose()\u64a4\u9500\u3002\u4e0e\u6587\u4ef6\u6709\u5173\u7684\u8c03\u7528\u5b9e\u9645\u4e0a\u90fd\u662f\u6587\u4ef6\u64cd\u4f5c\u8868struct files_operations\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u6307\u9488\uff0c\u8fd9\u91cc\u5217\u51fa\u6765\u4f5c\u4e3a\u53c2\u8003\uff1a

struct file_operations {\n    struct module *owner;\n    loff_t (*llseek) (struct file *, loff_t, int);\n    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);\n    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);\n    ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);\n    ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);\n    int (*iopoll)(struct kiocb *kiocb, bool spin);\n    int (*iterate) (struct file *, struct dir_context *);\n    int (*iterate_shared) (struct file *, struct dir_context *);\n    __poll_t (*poll) (struct file *, struct poll_table_struct *);\n    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);\n    long (*compat_ioctl) (struct file *, unsigned int, unsigned long);\n    int (*mmap) (struct file *, struct vm_area_struct *);\n    unsigned long mmap_supported_flags;\n    int (*open) (struct inode *, struct file *);\n    int (*flush) (struct file *, fl_owner_t id);\n    int (*release) (struct inode *, struct file *);\n    int (*fsync) (struct file *, loff_t, loff_t, int datasync);\n    int (*fasync) (int, struct file *, int);\n    int (*lock) (struct file *, int, struct file_lock *);\n    ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);\n    unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);\n    int (*check_flags)(int);\n    int (*flock) (struct file *, int, struct file_lock *);\n    ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);\n    ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);\n    int (*setlease)(struct file *, long, struct file_lock **, void **);\n    long (*fallocate)(struct file *file, int mode, loff_t offset,\n              loff_t len);\n    void (*show_fdinfo)(struct seq_file *m, struct file *f);\n#ifndef CONFIG_MMU\n    unsigned (*mmap_capabilities)(struct file *);\n#endif\n    ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,\n            loff_t, size_t, unsigned int);\n    loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,\n                   struct file *file_out, loff_t pos_out,\n                   loff_t len, unsigned int remap_flags);\n    int (*fadvise)(struct file *, loff_t, loff_t, int);\n};\n

\u603b\u7684\u6765\u8bf4\uff0c\u5185\u6838\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u90fd\u5206\u914d\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u2014\u2014\u7d22\u5f15\u8282\u70b9\u548c\u76ee\u5f55\u9879\uff0c\u5206\u522b\u7528\u6765\u8bb0\u5f55\u6587\u4ef6\u7684\u5143\u6570\u636e\u548c\u76ee\u5f55\u7ed3\u6784\u3002\u7d22\u5f15\u8282\u70b9\u662f\u6587\u4ef6\u7684\u552f\u4e00\u6807\u5fd7\uff0c\u591a\u4e2a\u76ee\u5f55\u9879\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2a\u7d22\u5f15\u8282\u70b9\u3002\u6bd4\u5982\u901a\u8fc7\u786c\u94fe\u63a5\u4e3a\u6587\u4ef6\u521b\u5efa\u522b\u540d\uff0c\u867d\u7136\u76ee\u5f55\u4f4d\u7f6e\u4e0d\u540c\uff0c\u4f46\u662f\u94fe\u63a5\u7684\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u56e0\u6b64\u7d22\u5f15\u8282\u70b9\u4e00\u81f4\u3002

"},{"location":"linux/kernel/vfs/#_6","title":"\u8fdb\u7a0b\u4e2d\u7684\u6587\u4ef6\u6570\u636e\u7ed3\u6784","text":"

\u5728\u8fdb\u7a0b\u7ba1\u7406\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u4e86\u5728\u8fdb\u7a0b\u63cf\u8ff0\u7b26tast_struct\u4e2d\u6709\u4e24\u4e2a\u4e0e\u6587\u4ef6\u7cfb\u7edf\u76f8\u5173\u7684\u6210\u5458\u53d8\u91cf\uff1a

struct fs_struct *fs;\nstruct files_struct *files;\n

fs_struct\u5305\u542b\u6587\u4ef6\u7cfb\u7edf\u548c\u8fdb\u7a0b\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/fs_struct.h>\u4e2d\uff1a

struct fs_struct {\n    int users;\n    int umask;\n    int in_exec;\n    struct path root, pwd;\n};\n

user\uff1a\u5f53\u524d\u6253\u5f00\u8be5\u6587\u4ef6\u7684\u7528\u6237\u6570\u91cf\u3002

umask\uff1a\u7528\u6237\u6a21\u5f0f\u63a9\u7801\uff0c\u51b3\u5b9a\u4e86\u7cfb\u7edf\u5728\u521b\u5efa\u6587\u4ef6\u548c\u76ee\u5f55\u65f6\u7684\u9ed8\u8ba4\u6743\u9650\u63a9\u7801\u3002

in_exec\uff1a\u8868\u793a\u6587\u4ef6\u7cfb\u7edf\u662f\u5426\u5904\u4e8e\u6267\u884c\u6a21\u5f0f\u3002

root\uff0cpwd\uff1a\u6839\u76ee\u5f55\u8def\u5f84\u548c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u8def\u5f84\u3002

files_struct\u6807\u8bc6\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u4fe1\u606f\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/fdtable.h>\u4e2d\uff1a

struct files_struct {\n    atomic_t count;\n\n    struct fdtable __rcu *fdt;\n    struct fdtable fdtab;\n\n    unsigned int next_fd;\n    unsigned long close_on_exec_init[1];\n    unsigned long open_fds_init[1];\n    unsigned long full_fds_bits_init[1];\n    struct file __rcu * fd_array[NR_OPEN_DEFAULT];\n};\n

count\uff1a\u8ddf\u8e2a\u5f53\u524d\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6570\u91cf\u3002

fd\uff1a\u7f13\u5b58\u4e0b\u4e00\u4e2a\u53ef\u7528\u7684fd\u3002

fd_array\uff1a\u6307\u5411\u4e00\u4e2astruct file\u7ed3\u6784\u4f53\u7684\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u572864\u4f4d\u4f53\u7cfb\u4e2d\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u9ed8\u8ba4\u53ef\u4ee5\u6253\u5f0064\u4e2a\u6587\u4ef6\u5bf9\u8c61\u3002

\u8fd8\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7ed3\u6784\u4f53\u662fnamespace\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/mount.h>\u4e2d\u3002\u5b83\u53ef\u4ee5\u4f7f\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u5728\u7cfb\u7edf\u4e2d\u90fd\u770b\u5230\u552f\u4e00\u7684\u6587\u4ef6\u7cfb\u7edf\u5c42\u6b21\u7ed3\u6784\uff0c\u662fLinux\u4e09\u5927\u9694\u79bb\u6280\u672f\u4e4b\u4e00\u3002\u5177\u4f53\u7684\u7ec6\u8282\u5c31\u4e0d\u5728\u8fd9\u91cc\u63a2\u8ba8\u4e86\uff0c\u5728\u5bb9\u5668\u6280\u672f\u4e00\u7ae0\u6211\u4f1a\u518d\u505a\u8be6\u7ec6\u4ecb\u7ecd\u3002

\u4e0a\u8ff0\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u90fd\u662f\u901a\u8fc7\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u94fe\u8868\u8fde\u63a5\u8d77\u6765\u7684\u3002\u5bf9\u4e8e\u591a\u6570\u8fdb\u7a0b\uff0c\u5b83\u4eec\u7684\u63cf\u8ff0\u7b26\u90fd\u6307\u5411\u552f\u4e00\u7684files_struct\u548cfs_struct\u7ed3\u6784\u4f53\u3002\u4f46\u662f\u5bf9\u4e8e\u90a3\u4e9b\u5728\u8c03\u7528clone()\u51fd\u6570\u65f6\u6307\u5b9a\u4e86\u514b\u9686\u6807\u5fd7CLONE_FILES\u6216\u8005CLONE_FS\u7684\u8fdb\u7a0b\uff0c\u4f1a\u5171\u4eab\u8fd9\u4e24\u4e2a\u7ed3\u6784\u4f53\u3002

namespace\u7ed3\u6784\u4f53\u7684\u4f7f\u7528\u65b9\u6cd5\u6bd4\u8f83\u7279\u6b8a\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u8fdb\u7a0b\u90fd\u662f\u5171\u4eab\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u7684\uff0c\u53ea\u6709\u5f53clone()\u51fd\u6570\u65f6\u6307\u5b9a\u4e86CLONE_NEWS\u6807\u5fd7\uff0c\u624d\u4f1a\u7ed9\u8fdb\u7a0b\u4e00\u4e2a\u552f\u4e00\u7684\u547d\u540d\u7a7a\u95f4\u7ed3\u6784\u4f53\u7684\u62f7\u8d1d\u3002

"},{"location":"linux/kernel/vfs/#proc","title":"proc\u6587\u4ef6\u7cfb\u7edf","text":"

proc\u6587\u4ef6\u7cfb\u7edf\u662f\u7279\u6b8a\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u5b83\u4eec\u7684\u4fe1\u606f\u5e76\u4e0d\u4fdd\u5b58\u5728\u78c1\u76d8\u4e0a\u800c\u662f\u5728\u6536\u5230\u8bf7\u6c42\u540e\u7531\u5185\u6838\u52a8\u6001\u751f\u6210\u3002proc\u6587\u4ef6\u7cfb\u7edf\u63d0\u4f9b\u4e86\u5185\u6838\u5404\u4e2a\u5b50\u7cfb\u7edf\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u8fdb\u7a0b\u4fe1\u606f\u3001\u5185\u5b58\u4fe1\u606f\u3001\u7f51\u7edc\u4fe1\u606f\u7b49\u3002\u5b83\u4f7f\u5f97\u7528\u6237\u901a\u8fc7\u7b80\u5355\u5730\u4f7f\u7528cat\u547d\u4ee4\u5c31\u53ef\u4ee5\u67e5\u770b\u7cfb\u7edf\u4fe1\u606f\uff0c\u751a\u8fd8\u53ef\u4ee5\u5728\u7cfb\u7edf\u8fd0\u884c\u65f6\u52a8\u6001\u4fee\u6539\u5185\u6838\u53c2\u6570\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u5185\u6838\u7684\u4ee3\u7801\u3002

\u5728proc\u6587\u4ef6\u7cfb\u7edf\u5f15\u5165\u4e4b\u524d\uff0c\u8981\u60f3\u83b7\u53d6\u6216\u4fee\u6539\u5185\u6838\u7684\u53c2\u6570\u65f6\uff0c\u9700\u8981\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u4e13\u95e8\u7684\u7279\u6743\u7ea7\u7a0b\u5e8f\uff0c\u8fd9\u9700\u8981\u5f00\u53d1\u4eba\u5458\u5177\u6709\u975e\u5e38\u4e13\u4e1a\u7684\u5185\u6838\u77e5\u8bc6\uff0c\u8fd9\u79cd\u505a\u6cd5\u5bf9\u4e8e\u5185\u6838\u6765\u8bf4\u5f88\u4e0d\u5b89\u5168\u3002\u6709\u4e86proc\u6587\u4ef6\u7cfb\u7edf\u4e4b\u540e\uff0c\u7528\u6237\u901a\u8fc7\u7b80\u5355\u7684read/write\u547d\u4ee4\u5c31\u53ef\u4ee5\u8bfb\u53d6\u548c\u4fee\u6539\u5185\u6838\u53c2\u6570\uff0c\u4e0d\u4ec5\u589e\u52a0\u4e86\u5e94\u7528\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u4e5f\u964d\u4f4e\u4e86\u5185\u6838\u7a33\u5b9a\u6027\u88ab\u5e94\u7528\u7a0b\u5e8f\u7834\u574f\u7684\u98ce\u9669\u3002

\u4e0b\u9762\u5bf9proc\u76ee\u5f55\u4e0b\u51e0\u4e2a\u5e38\u7528\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u548c\u4f7f\u7528\u573a\u666f\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u8bf4\u660e\u3002

"},{"location":"linux/kernel/vfs/#proccpuinfo","title":"/proc/cpuinfo","text":"

\u8be5\u6587\u4ef6\u5305\u542b\u4e86\u5f53\u524d\u7cfb\u7edfCPU\u7684\u53c2\u6570\u4fe1\u606f\uff0c\u5305\u62ecCPU\u7684\u6838\u5fc3\u6570\u91cf\u3001\u6bcf\u4e2a\u6838\u5fc3\u7684\u5de5\u4f5c\u9891\u7387\u3001\u7f13\u5b58\u5927\u5c0f\u7b49\u3002

"},{"location":"linux/kernel/vfs/#procmeminfo","title":"/proc/meminfo","text":"

\u8be5\u6587\u4ef6\u663e\u793a\u7684\u662f\u7cfb\u7edf\u4e2d\u5f53\u524d\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u6bd4\u5982\u7269\u7406\u5185\u5b58\u5bb9\u91cf\u3001\u5df2\u4f7f\u7528\u5185\u5b58\u3001\u7a7a\u95f2\u5185\u5b58\u3001\u5171\u4eab\u5185\u5b58\u3001\u4ea4\u6362\u5185\u5b58\u5927\u5c0f\u7b49\u3002\u65e5\u5e38\u5f00\u53d1\u8fc7\u7a0b\u4e2dtop\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u65f6\uff0c\u5c31\u662f\u901a\u8fc7\u8bfb\u53d6\u8be5\u6587\u4ef6\u83b7\u53d6\u7684\u3002\u76f8\u6bd4\u4e8etop\u547d\u4ee4\uff0c\u8be5\u6587\u4ef6\u63d0\u4f9b\u4e86\u66f4\u4e3a\u8be6\u7ec6\u7684\u5185\u5b58\u72b6\u6001\u4fe1\u606f\uff0c\u6bd4\u5982slab\u5185\u5b58\u5360\u7528\u91cf\u3001\u53ef\u56de\u6536\u5185\u5b58\u91cf\u3001\u5185\u5b58\u6808\u7a7a\u95f4\u5927\u5c0f\u3001\u4f7f\u7528malloc()\u5206\u914d\u7684\u5806\u5185\u5b58\u91cf\u3001\u4f7f\u7528mmap()\u6620\u5c04\u7684\u5185\u5b58\u91cf\u7b49\u3002

"},{"location":"linux/kernel/vfs/#prockallsyms","title":"/proc/kallsyms","text":"

\u8be5\u6587\u4ef6\u662f\u4e00\u4e2a\u7b26\u53f7\u8868\uff0c\u5305\u542b\u4e86\u5185\u6838\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002

\u8fd9\u662f\u4e00\u4e2a\u5e2e\u52a9\u5185\u6838\u5f00\u53d1\u8005\u8c03\u8bd5\u5185\u6838\u800c\u52a0\u5165\u7684\u6587\u4ef6\uff0c\u5728Linux\u7cfb\u7edf\u5d29\u6e83\u4ea7\u751f\u7684\u4fe1\u606f\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u5806\u6808\u4e2d\u663e\u793a\u51fa\u6765\u7684\u51fd\u6570\u540d\uff0c\u5c31\u662f\u5728\u8fd9\u4e2a\u6587\u4ef6\u7684\u5e2e\u52a9\u4e0b\u4ea7\u751f\u7684\u3002

"},{"location":"linux/kernel/vfs/#procinterrupts","title":"/proc/interrupts","text":"

\u8be5\u6587\u4ef6\u8bb0\u5f55\u4e86\u7cfb\u7edf\u5728\u6bcf\u4e2aCPU\u4e0a\u5904\u7406\u7684\u5404\u7c7b\u4e2d\u65ad\u7684\u7edf\u8ba1\u4fe1\u606f\u3002

"},{"location":"linux/kernel/vfs/#procloadavg","title":"/proc/loadavg","text":"

\u8be5\u6587\u4ef6\u663e\u793a\u7cfb\u7edf\u5728\u8fc7\u53bb\u4e00\u6bb5\u65f6\u95f4\u7684\u5e73\u5747\u8d1f\u8f7d\uff1a

\u4ece\u5de6\u5230\u53f3\u5206\u522b\u662f\uff1a

  • \u8fc7\u53bb\u4e00\u5206\u949f\u7684\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d
  • \u8fc7\u53bb\u4e94\u5206\u949f\u7684\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d
  • \u8fc7\u53bb\u5341\u4e94\u5206\u949f\u7684\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d
  • \u91c7\u6837\u65f6\u523b\u8fd0\u884c\u961f\u5217\u7684\u4efb\u52a1\u6570/\u7cfb\u7edf\u4e2d\u6d3b\u8dc3\u7684\u603b\u4efb\u52a1\u6570
  • \u91c7\u6837\u65f6\u523b\u5360\u7528\u6700\u5927\u7684\u7ebf\u7a0bID

\u8f93\u51fa\u7684\u524d\u4e09\u4e2a\u503c\u7ecf\u5e38\u88ab\u7528\u6765\u89c2\u5bdf\u7cfb\u7edf\u8d1f\u8f7d\u7684\u53d1\u5c55\u8d8b\u52bf\uff0c\u5982\u679c\u524d\u9762\u7684\u503c\u6bd4\u540e\u9762\u7684\u503c\u5927\uff0c\u8bf4\u660e\u7cfb\u7edf\u7684\u8d1f\u8f7d\u5448\u73b0\u4e0a\u5347\u8d8b\u52bf\u3002

"},{"location":"linux/kernel/vfs/#procpid","title":"/proc/PID/","text":"

\u8fd9\u662f\u5305\u542b\u8fdb\u7a0b\u8fd0\u884c\u4fe1\u606f\u7684\u76ee\u5f55\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u7ef4\u62a4\u4e00\u4e2a\u4ee5PID\u547d\u540d\u7684\u76ee\u5f55\uff0c\u5728\u8be5\u76ee\u5f55\u4e0b\u4fdd\u5b58\u8be5\u8fdb\u7a0b\u8fd0\u884c\u65f6\u7684\u5404\u79cd\u72b6\u6001\u6570\u636e\uff0c\u6bd4\u5982\u8fd0\u884c\u72b6\u6001\uff08status\uff09\u3001\u865a\u62df\u5185\u5b58\u6620\u5c04\u4fe1\u606f\uff08maps\uff09\u3001\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08fd\uff09\u3001\u521b\u5efa\u7684\u7f51\u7edc\u5957\u63a5\u5b57\u7b49\u3002

\u5176\u4e2d\u7528\u7684\u6bd4\u8f83\u591a\u7684\uff0c\u5e94\u8be5\u662f\u865a\u62df\u5185\u5b58\u6620\u5c04\u4fe1\u606f\uff08maps\uff09\uff1a

"},{"location":"linux/kernel/vfs/#procnet","title":"/proc/net/","text":"

\u8be5\u76ee\u5f55\u4e0b\u4fdd\u5b58\u4e86\u7cfb\u7edf\u7f51\u7edc\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u7f51\u7edc\u63a5\u53e3\u3001\u7f51\u7edc\u534f\u8bae\u6808\u3001\u7f51\u7edc\u8fde\u63a5\u3001\u7f51\u7edc\u8def\u7531\u8868\u7b49\u3002\u6700\u5e38\u7528\u7684/proc/net/dev\u4fdd\u5b58\u4e86\u901a\u8fc7\u5404\u7f51\u7edc\u63a5\u53e3\u4f20\u8f93\u7684\u6570\u636e\u7684\u7edf\u8ba1\u91cf\uff0c\u7ecf\u5e38\u7528\u5b83\u6765\u67e5\u770b\u7f51\u7edc\u6d41\u91cf\u548c\u68c0\u67e5\u7f51\u7edc\u4f20\u8f93\u8d28\u91cf\u3002

"},{"location":"linux/kernel/vma/","title":"\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4","text":"

\u5185\u6838\u9664\u4e86\u7ba1\u7406\u672c\u8eab\u7684\u5185\u5b58\u5916\uff0c\u8fd8\u5fc5\u987b\u7ba1\u7406\u7528\u6237\u7a7a\u95f4\u4e2d\u8fdb\u7a0b\u7684\u5185\u5b58\u3002\u6211\u4eec\u79f0\u8fd9\u4e2a\u5185\u5b58\u4e3a\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u3002Linux\u64cd\u4f5c\u7cfb\u7edf\u91c7\u7528\u865a\u62df\u5185\u5b58\u6280\u672f\uff0c\u5bf9\u4e00\u4e2a\u8fdb\u7a0b\u800c\u8a00\uff0c\u5b83\u4ee5\u865a\u62df\u7684\u65b9\u5f0f\u8bbf\u95ee\u6574\u4e2a\u7cfb\u7edf\u7684\u6240\u6709\u7269\u7406\u5185\u5b58\uff0c\u751a\u81f3\u5176\u865a\u62df\u7a7a\u95f4\u53ef\u4ee5\u8fdc\u8fdc\u5927\u4e8e\u7269\u7406\u5185\u5b58\u3002

\u4e3a\u4ec0\u4e48\u8981\u6709\u865a\u62df\u5185\u5b58\uff1f

\u5728\u8003\u8651\u4e24\u4e2a\u7a0b\u5e8f\u540c\u65f6\u64cd\u4f5c\u7269\u7406\u5185\u5b58\u7684\u573a\u666f\u65f6\uff0c\u6211\u4eec\u5fc5\u987b\u8c28\u614e\u5730\u4e3a\u5b83\u4eec\u5206\u914d\u5185\u5b58\u5730\u5740\uff0c\u5e76\u786e\u4fdd\u6bcf\u4e2a\u7a0b\u5e8f\u4ec5\u8bbf\u95ee\u5176\u88ab\u5206\u914d\u7684\u5185\u5b58\u533a\u57df\uff0c\u9632\u6b62\u5730\u5740\u51b2\u7a81\u3002\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u5730\u5740\u4f1a\u6781\u5927\u5730\u589e\u52a0\u7f16\u7a0b\u7684\u590d\u6742\u6027\u3002\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u5b9e\u73b0\u4e00\u79cd\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u63d0\u4f9b\u4e00\u5957\u9694\u79bb\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c06\u4e3a\u8fd9\u4e9b\u865a\u62df\u5730\u5740\u6620\u5c04\u5230\u7269\u7406\u5185\u5b58\uff0c\u4e14\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u72ec\u7acb\u4e8e\u5176\u4ed6\u8fdb\u7a0b\u64cd\u4f5c\uff0c\u90a3\u4e48\u8fd9\u5c06\u6781\u5927\u5730\u7b80\u5316\u7f16\u7a0b\u5de5\u4f5c\u3002\u8fd9\u79cd\u673a\u5236\u5c31\u662f\u865a\u62df\u5185\u5b58\u7ba1\u7406\uff0c\u800c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u5355\u5143\uff08MMU\uff09\u5219\u8d1f\u8d23\u5c06\u865a\u62df\u5730\u5740\u8f6c\u6362\u4e3a\u7269\u7406\u5730\u5740\u3002

Linux\u91c7\u7528\u4e86\u57fa\u4e8e\u5206\u9875\u7684\u865a\u62df\u5185\u5b58\u7ba1\u7406\u673a\u5236\uff0c\u5176\u4e2d\u5185\u5b58\u88ab\u5212\u5206\u4e3a\u5927\u5c0f\u56fa\u5b9a\u7684\u9875\uff0c\u6bcf\u4e00\u9875\u901a\u5e38\u662f4KB\u3002\u8fd9\u79cd\u5206\u9875\u673a\u5236\u4f7f\u5f97\u5185\u5b58\u7ba1\u7406\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002\u5728Linux\u4e2d\uff0c\u865a\u62df\u5730\u5740\u5230\u7269\u7406\u5730\u5740\u7684\u8f6c\u6362\u662f\u901a\u8fc7\u591a\u7ea7\u9875\u8868\u6765\u5b9e\u73b0\u7684\uff0c\u8fd9\u79cd\u5206\u7ea7\u7ed3\u6784\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u5927\u578b\u5730\u5740\u7a7a\u95f4\uff0c\u540c\u65f6\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u4e3a\u4e86\u63d0\u9ad8\u5730\u5740\u8f6c\u6362\u7684\u901f\u5ea6\uff0c\u73b0\u4ee3CPU\u8bbe\u8ba1\u901a\u5e38\u5305\u62ec\u4e00\u4e2a\u79f0\u4e3a\u8f6c\u6362\u540e\u63f4\u7f13\u51b2\u5668\uff08TLB\uff09\u7684\u786c\u4ef6\u7f13\u5b58\u3002TLB\u7f13\u5b58\u4e86\u6700\u8fd1\u4f7f\u7528\u8fc7\u7684\u9875\u8868\u9879\uff0c\u8fd9\u6837\u5f53CPU\u9700\u8981\u8bbf\u95ee\u4e00\u4e2a\u5185\u5b58\u5730\u5740\u65f6\uff0c\u5b83\u9996\u5148\u68c0\u67e5TLB\uff0c\u5982\u679c\u627e\u5230\u4e86\u5339\u914d\u7684\u6761\u76ee\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u88ab\u79f0\u4e3aTLB\u547d\u4e2d\uff0c\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u5730\u5740\u8f6c\u6362\uff0c\u907f\u514d\u8bbf\u95ee\u4e3b\u5185\u5b58\u4e2d\u7684\u9875\u8868\uff0c\u4ece\u800c\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002\u5f53\u53d1\u751fTLB\u672a\u547d\u4e2d\u7684\u60c5\u51b5\u65f6\uff0cCPU\u5fc5\u987b\u8bbf\u95ee\u4e3b\u5185\u5b58\u4e2d\u7684\u9875\u8868\u6765\u83b7\u53d6\u6b63\u786e\u7684\u7269\u7406\u5730\u5740\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8f83\u5927\u7684\u6027\u80fd\u5f00\u9500\u3002\u56e0\u6b64\uff0c\u64cd\u4f5c\u7cfb\u7edf\u548c\u786c\u4ef6\u8bbe\u8ba1\u8005\u90fd\u4f1a\u5c3d\u91cf\u4f18\u5316TLB\u7684\u547d\u4e2d\u7387\uff0c\u4ee5\u51cf\u5c11\u8fd9\u79cd\u5f00\u9500\u3002

"},{"location":"linux/kernel/vma/#_2","title":"\u5730\u5740\u7a7a\u95f4\u7684\u6982\u5ff5","text":"

\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u7531\u53ef\u5bfb\u5740\u7684\u865a\u62df\u5185\u5b58\u7ec4\u6210\uff0c\u572832\u4f4d\u4f53\u7cfb\u4e2d\uff08\u53ea\u8ba8\u8bba32\u4f4d\u67b6\u6784\uff0c64\u4f4d\u7c7b\u4f3c\uff09\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u53ef\u5bfb\u57404GB\u7684\u865a\u62df\u5185\u5b58\uff0c\u5176\u4e2d1GB\u5206\u914d\u7ed9\u5185\u6838\u7a7a\u95f4\uff0c\u5269\u4e0b3GB\u5206\u914d\u7ed9\u7528\u6237\u7a7a\u95f4\u3002\u4e00\u4e2a\u8fdb\u7a0b\u53ea\u80fd\u8bbf\u95ee\u6709\u6548\u5185\u5b58\u533a\u57df\u5185\u7684\u5185\u5b58\u5730\u5740\uff0c\u90e8\u5206\u5730\u5740\u662f\u975e\u6cd5\u7684\uff0c\u4e00\u65e6\u8bbf\u95ee\uff0c\u5c31\u4f1a\u7acb\u523b\u89e6\u53d1\u6bb5\u9519\u8bef\uff0c\u7531\u5185\u6838\u7ec8\u6b62\u8be5\u8fdb\u7a0b\u3002\u6bcf\u4e2a\u5185\u5b58\u533a\u57df\u4e5f\u6709\u76f8\u5173\u7684\u6743\u9650\uff0c\u6bd4\u5982\u53ef\u8bfb\u53ef\u5199\u53ef\u6267\u884c\u7b49\u3002

\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u5206\u5e03\u5982\u4e0b\u56fe\u6240\u793a\uff1a

\u4ece\u4e0a\u56fe\u4e2d\u6211\u4eec\u770b\u5230\uff0c0x0000 0000\u52300x0804 8000\u8fd9\u6bb5\u7a7a\u95f4\u662f\u4e0d\u53ef\u8bbf\u95ee\u7684\u4fdd\u7559\u533a\uff0c\u5728\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u6570\u503c\u6bd4\u8f83\u5c0f\u7684\u5730\u5740\u662f\u975e\u6cd5\u5730\u5740\uff0c\u4e0d\u5141\u8bb8\u8bbf\u95ee\u7684\u3002\u6bd4\u5982NULL\u6307\u9488\u5c31\u4f1a\u6307\u5411\u8fd9\u7247\u533a\u57df\u3002

0x0804 8000\u81f30xC000 0000\u662f\u7528\u6237\u6001\u7a7a\u95f4\u5730\u5740\uff0c\u518d\u7f51\u4e0a\u5c31\u662f\u6240\u8c13\u7684\u9ad8\u7aef\u5730\u5740\u2014\u2014\u4f9b\u5185\u6838\u4f7f\u7528\u3002\u5185\u6838\u6001\u4e0e\u7528\u6237\u6001\u7684\u5206\u754c\u7ebf\u7531\u6210\u5458\u53d8\u91cftask_size\u5b9a\u4e49\u3002

\u4fdd\u7559\u533a\u4e0a\u65b9\u662f\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\uff0c\u5b83\u4eec\u662f\u4ece\u53ef\u6267\u884c\u6587\u4ef6\u76f4\u63a5\u52a0\u8f7d\u8fdb\u6765\u7684\u3002\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u653e\u5728\u4ee3\u7801\u6bb5\uff0c\u6570\u636e\u6bb5\u7528\u6765\u5b58\u653e\u5df2\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u3002

BSS\u6bb5\u7528\u6765\u5b58\u653e\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4ee50\u586b\u5145\u3002

BSS\u6bb5\u4e0a\u65b9\u662f\u5806\u7a7a\u95f4\uff0c\u5730\u5740\u7684\u589e\u957f\u65b9\u5411\u662f\u4ece\u4f4e\u5230\u9ad8\u3002\u5185\u6838\u4f7f\u7528start_brk\u6807\u8bc6\u5806\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0cbrk\u6807\u8bc6\u5806\u5f53\u524d\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002\u5f53\u5806\u7533\u8bf7\u4e00\u5757\u5f88\u5c0f\u7684\u5185\u5b58\uff08128K\u4ee5\u5185\uff09\uff0c\u53ea\u9700\u8981\u5c06brk\u6307\u9488\u589e\u52a0\u5bf9\u5e94\u7684\u5927\u5c0f\u5373\u53ef\u3002\u5806\u5185\u5b58\u7684\u7ba1\u7406\u6bd4\u8f83\u590d\u6742\uff0c\u6700\u5927\u7684\u56f0\u96be\u5c31\u662f\u9891\u7e41\u5730\u5206\u914d\u548c\u91ca\u653e\u4f1a\u9020\u6210\u5185\u5b58\u788e\u7247\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u5185\u6838\u91c7\u7528\u4e86\u57fa\u4e8e\u4f19\u4f34\u7cfb\u7edf\u7684\u5185\u5b58\u5206\u914d\u7b97\u6cd5\u3002

\u5806\u7a7a\u95f4\u4e0a\u65b9\u662f\u5f85\u5206\u914d\u533a\u57df\uff0c\u7528\u6765\u6269\u5c55\u5806\u7a7a\u95f4\u7684\u4f7f\u7528\u3002\u63a5\u4e0b\u6765\u662f\u5185\u5b58\u6620\u5c04\u533a\u57df\u3002\u4efb\u4f55\u5e94\u7528\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u901a\u8fc7mmap()\u7cfb\u7edf\u8c03\u7528\u6620\u5c04\u81f3\u6b64\u533a\u57df\u3002\u5185\u5b58\u6620\u5c04\u53ef\u4ee5\u7528\u6765\u52a0\u8f7d\u52a8\u6001\u5e93\uff0c\u6bd4\u5982 ld-linux.so \u5c31\u88ab\u52a0\u8f7d\u4e8e\u6b64\uff0c\u53e6\u5916\uff0c\u5982\u679c\u4f60\u901a\u8fc7malloc()\u7533\u8bf7\u4e86\u8d85\u8fc7128K\u5185\u5b58\uff0c\u5185\u6838\u5c06\u76f4\u63a5\u4e3a\u4f60\u5206\u914d\u4e00\u5757\u6620\u5c04\u533a\u57df\u4f5c\u4e3a\u5185\u5b58\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u5806\u5185\u5b58\u3002

\u6700\u540e\u4e00\u5757\u533a\u57df\u662f\u6808\u7a7a\u95f4\uff0c\u5728\u8fd9\u91cc\u4fdd\u5b58\u51fd\u6570\u8fd0\u884c\u8fc7\u7a0b\u9700\u8981\u7684\u5c40\u90e8\u53d8\u91cf\u4ee5\u53ca\u51fd\u6570\u53c2\u6570\u7b49\u4fe1\u606f\u3002\u6808\u7a7a\u95f4\u7684\u5730\u5740\u662f\u4ece\u9ad8\u5230\u4f4e\u589e\u957f\u7684\u3002\u5185\u6838\u4f7f\u7528start_stack\u6807\u8bc6\u6808\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002SP\u5bc4\u5b58\u5668\u4fdd\u5b58\u6808\u9876\u6307\u9488\uff0cBP\u5bc4\u5b58\u5668\u4fdd\u5b58\u6808\u57fa\u5730\u5740\u3002

\u5728\u547d\u4ee4\u884c\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528cat /proc/[pid]/maps\u67e5\u770b\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5185\u5b58\u5e03\u5c40\u3002

"},{"location":"linux/kernel/vma/#mm_struct","title":"\u5185\u5b58\u63cf\u8ff0\u7b26mm_struct","text":"

mm_struct\u7528\u6765\u8868\u793a\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u7684\u4fe1\u606f\uff0c\u5b9a\u4e49\u5728\u6587\u4ef6<linux/mm_types.h>\u4e2d\uff1a

struct mm_struct {\n    atomic_t mm_users;\n    atomic_t mm_count;\n    struct vm_area_struct *mmap;\n    struct rb_root mm_rb;\n    struct list_head mmlist;\n\n    unsigned long start_code, end_code;    //\u4ee3\u7801\u6bb5\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long start_data, end_data;    //\u6570\u636e\u6bb5\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long start_brk, brk,          //\u5806\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long start_stack;             //\u6808\u7684\u8d77\u59cb\u5730\u5740\n    unsigned long arg_start, arg_end,      //\u547d\u4ee4\u884c\u53c2\u6570\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long env_start, env_end;      //\u73af\u5883\u53d8\u91cf\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5730\u5740\n    unsigned long mmap_base;               //\u5185\u5b58\u6620\u5c04\u533a\u7684\u8d77\u59cb\u5730\u5740\n    unsigned long total_vm;                //\u603b\u5171\u6620\u5c04\u7684\u9875\u6570\u76ee\n    unsigned long locked_vm;               //\u88ab\u9501\u5b9a\u4e0d\u80fd\u6362\u51fa\u7684\u9875\u6570\u76ee\n    unsigned long pinned_vm;               //\u65e2\u4e0d\u80fd\u6362\u51fa\uff0c\u4e5f\u4e0d\u80fd\u79fb\u52a8\u7684\u9875\u6570\u76ee\n    unsigned long data_vm;                 //\u6570\u636e\u6bb5\u4e2d\u6620\u5c04\u7684\u9875\u6570\u76ee\n    unsigned long exec_vm;                 //\u4ee3\u7801\u6bb5\u4e2d\u53ef\u6267\u884c\u6587\u4ef6\u7684\u9875\u6570\u76ee\n    unsigned long stack_vm;                //\u6808\u4e2d\u6620\u5c04\u7684\u9875\u6570\u76ee\n};\n

mm_users\uff1a\u6b63\u5728\u4f7f\u7528\u8be5\u5730\u5740\u7684\u8fdb\u7a0b\u6570\u3002

mm_count\uff1amm_struct\u7ed3\u6784\u4f53\u7684\u4e3b\u5f15\u7528\u8ba1\u6570\uff0c\u8be5\u503c\u4e3a0\u65f6\uff0c\u91ca\u653e\u7ed3\u6784\u4f53\u3002

mmap\uff1a\u6307\u5411\u4e00\u4e2avm_area_struct\u7ed3\u6784\u4f53\u7684\u94fe\u8868\u5934\u3002

mm_rb\uff1a\u6307\u5411\u4e00\u4e2avm_area_struct\u7ed3\u6784\u4f53\u7684\u7ea2\u9ed1\u6811\u6839\u8282\u70b9\u3002

mmlist\uff1a\u6307\u5411\u4e00\u4e2amm_struct\u7ed3\u6784\u4f53\u7684\u94fe\u8868\u5934\u3002

\u5728\u8fdb\u7a0b\u7ba1\u7406\u4e0e\u8c03\u5ea6\u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86mm\u53d8\u91cf\uff0c\u7528\u6765\u5b58\u653e\u8be5\u8fdb\u7a0b\u4f7f\u7528\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\uff0ccurrent->mm\u5c31\u6307\u5411\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u3002fork()\u51fd\u6570\u5229\u7528copy_mm()\u51fd\u6570\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u3002\u5982\u679c\u7236\u5b50\u8fdb\u7a0b\u5171\u4eab\u5730\u5740\u7a7a\u95f4\uff0c\u5219\u5728\u8c03\u7528clone()\u65f6\uff0c\u8bbe\u7f6eCLONE_VM\u6807\u5fd7\uff0c\u8fd9\u6837\u7684\u8fdb\u7a0b\u5c31\u662f\u7ebf\u7a0b\u3002\u5728Linux\u73af\u5883\u4e0b\uff0c\u662f\u5426\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u51e0\u4e4e\u662f\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u672c\u8d28\u4e0a\u7684\u552f\u4e00\u533a\u522b\u3002\u5982\u679c\u6307\u5b9a\u4e86CLONE_VM\uff0c\u7ebf\u7a0b\u5c31\u4e0d\u9700\u8981\u53e6\u5916\u5206\u914d\u5730\u5740\u7a7a\u95f4\u4e86\uff0c\u800c\u662f\u76f4\u63a5\u5c06mm\u57df\u76f4\u63a5\u6307\u5411\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u5373\u53ef\u3002

\u5185\u6838\u7ebf\u7a0b\u6ca1\u6709\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\uff0c\u4e5f\u6ca1\u6709\u76f8\u5173\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\uff0c\u5176mm\u57df\u4e3aNULL\u3002\u5f53\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u88ab\u8c03\u5ea6\u65f6\uff0c\u5b83\u4f1a\u76f4\u63a5\u4f7f\u7528\u524d\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5185\u5b58\u63cf\u8ff0\u7b26\u3002

"},{"location":"linux/kernel/vma/#_3","title":"\u865a\u62df\u5185\u5b58\u533a\u57df","text":"

\u865a\u62df\u5185\u5b58\u533a\u57df\uff08Virtual Memory Area, VMA\uff09\u5728\u5185\u6838\u4e2d\u7528vm_area_struct\u7ed3\u6784\u4f53\u63cf\u8ff0\u3002\u6bcf\u4e2avm_area_struct\u7ed3\u6784\u90fd\u5bf9\u5e94\u4e8e\u6307\u5b9a\u5730\u5740\u7a7a\u95f4\u4e0a\u67d0\u5757\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\u3002vm_start\u6307\u5411\u4e86\u8fd9\u5757\u865a\u62df\u5185\u5b58\u533a\u57df\u7684\u8d77\u59cb\u5730\u5740\uff0cvm_end\u6307\u5411\u4e86\u8fd9\u5757\u865a\u62df\u5185\u5b58\u533a\u57df\u7684\u7ed3\u675f\u5730\u5740\u3002vm_area_struct\u7ed3\u6784\u4f53\u63cf\u8ff0\u7684\u662f[vm_start\uff0cvm_end)\u8fd9\u6837\u4e00\u6bb5\u5de6\u95ed\u53f3\u5f00\u7684\u865a\u62df\u5185\u5b58\u533a\u57df\u3002

\u5185\u6838\u5c06\u6bcf\u4e2a\u5185\u5b58\u533a\u57df\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5185\u5b58\u5bf9\u8c61\u7ba1\u7406\uff0c\u6bcf\u4e2a\u533a\u57df\u53c8\u4e00\u81f4\u7684\u5c5e\u6027\u548c\u64cd\u4f5c\uff0c\u4e0b\u9762\u7ed9\u51favm_area_struct\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\uff1a

struct vm_area_struct {\n    struct mm_struct *vm_mm;                      //\u76f8\u5173\u7684mm_struct\u7ed3\u6784\u4f53\n    unsigned long vm_start;                       //\u533a\u95f4\u7684\u9996\u5730\u5740\n    unsigned long vm_end;                         //\u533a\u95f4\u7684\u5c3e\u5730\u5740\n    struct vm_area_struct *vm_next;               //VMA\u94fe\u8868\n    pgprot_t vm_page_prot;                        //\u8bbf\u95ee\u63a7\u5236\u6743\u9650\n    unsigned long vm_flags;                       //\u63cf\u8ff0\u8be5\u533a\u57df\u7684\u6807\u5fd7\uff0c\u91cd\u8981\u7684\u4e24\u4e2a\u662fVM_IO\u548cVM_RESERVED\n    struct rb_node vm_rb;                         //\u7ea2\u9ed1\u6811\u8282\u70b9\n    struct anon_vma *anon_vma;                    //\u533f\u540dVMA\u5bf9\u8c61\n    const struct vm_operations_struct *vm_ops;    //VMA\u7684\u64cd\u4f5c\u51fd\u6570\n    unsigned long vm_pgoff;                       //\u6587\u4ef6\u4e2d\u8be5\u533a\u57df\u7684\u504f\u79fb\u91cf\n    struct file *vm_file;                         //\u6307\u5411\u4e0e\u8be5\u533a\u57df\u76f8\u5173\u8054\u7684file\u7ed3\u6784\u6307\u9488\n    void *vm_private_data;                        //\u79c1\u6709\u6570\u636e\n};\n

\u6bcf\u4e2a\u5185\u5b58\u63cf\u8ff0\u7b26\u5bf9\u5e94\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u4e2d\u7684\u552f\u4e00\u7a7a\u95f4\uff0c\u8303\u56f4\u662f[vm_start, vm_end)\u3002vm_mm\u6307\u5411\u4e0eVMA\u76f8\u5173\u7684mm_struct\u7ed3\u6784\u4f53\uff0cvm_next\u8d1f\u8d23\u5c06VMA\u4e32\u8054\u6210\u94fe\u8868\u3002

"},{"location":"linux/kernel/vma/#vma","title":"VMA\u6807\u5fd7","text":"

vm_flags\u5b9a\u4e49\u4e86VMA\u7684\u6807\u5fd7\uff0c\u5b83\u8868\u793a\u5185\u5b58\u533a\u57df\u7684\u884c\u4e3a\u548c\u4fe1\u606f\u3002\u4e00\u4e9b\u6bd4\u8f83\u91cd\u8981\u7684\u6807\u5fd7\u6bd4\u5982\uff1aVM_READ\u3001VM_WRITE\u548cVM_EXEC\u6807\u5fd7\u4e86\u5185\u5b58\u533a\u57df\u4e2d\u9875\u9762\u7684\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u53ef\u6267\u884c\u6743\u9650\u3002\u5f53\u8bbf\u95eeVMA\u65f6\uff0c\u9700\u8981\u67e5\u770b\u5176\u8bbf\u95ee\u6743\u9650\u3002

VM_SHARED\u8868\u793a\u5185\u5b58\u533a\u57df\u5305\u542b\u7684\u6620\u5c04\u662f\u5426\u53ef\u4ee5\u5728\u591a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u8be5\u6807\u5fd7\u4f4d\uff0c\u6211\u4eec\u79f0\u5176\u4f4d\u5171\u4eab\u6620\u5c04\uff0c\u5426\u5219\u5c31\u662f\u79c1\u6709\u6620\u5c04\u3002

VM_IO\u6807\u5fd7\u5185\u5b58\u533a\u57df\u4e2d\u5bf9\u8bbe\u5907I/O\u7a7a\u95f4\u7684\u6620\u5c04\u3002\u8be5\u6807\u5fd7\u901a\u5e38\u5728\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u6267\u884cmmap()\u51fd\u6570\u65f6\u624d\u88ab\u8bbe\u7f6e\u3002

VM_RESERVED\u6807\u5fd7\u89c4\u5b9a\u4e86\u5185\u5b58\u533a\u57df\u4e0d\u80fd\u88ab\u6362\u51fa\u3002

"},{"location":"linux/kernel/vma/#vma_1","title":"VMA\u64cd\u4f5c","text":"

vm_ops\u57df\u6307\u5411\u4e0e\u5185\u5b58\u533a\u57df\u76f8\u5173\u7684\u64cd\u4f5c\u51fd\u6570\uff0c\u7531vm_operations_struct\u7ed3\u6784\u4f53\u8868\u793a\uff1a

struct vm_operations_struct {\n    void (*open)(struct vm_area_struct *);\n    void (*close)(sturct vm_area_struct *);\n    int (*fault)(struct vm_area_struct *, struct vm_fault *);\n    int (*page_mkwrite)(struct vm_area_struct *vma, struct vm_fault *vmf);\n    int (*access)(struct vm_area_struct *, unsigned long, void *, int, int);\n};\n
"},{"location":"linux/kernel/vma/#malloc","title":"malloc()\u539f\u7406","text":"

malloc()\u662fC\u5e93\u91cc\u7684\u4e00\u4e2a\u52a8\u6001\u5206\u914d\u5185\u5b58\u7684\u51fd\u6570\uff0c\u7533\u8bf7\u5185\u5b58\u65f6\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a

  • \u65b9\u5f0f\u4e00\uff1a\u901a\u8fc7brk()\u7cfb\u7edf\u8c03\u7528\u4ece\u5806\u5206\u914d\u5185\u5b58\u3002

  • \u65b9\u5f0f\u4e8c\uff1a\u901a\u8fc7mmap()\u7cfb\u7edf\u8c03\u7528\u4ece\u6587\u4ef6\u6620\u5c04\u533a\u5206\u914d\u5185\u5b58\u3002

\u7b2c\u4e00\u79cd\u65b9\u5f0f\u5f88\u7b80\u5355\uff0c\u901a\u8fc7\u7b80\u5355\u5730\u8c03\u6574brk\u6307\u9488\u5411\u9ad8\u5730\u5740\u79fb\u52a8\uff0c\u5373\u5b8c\u6210\u4e86\u5185\u5b58\u5206\u914d\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u76f4\u63a5\u5728\u6587\u4ef6\u6620\u5c04\u533a\u6620\u5c04\u4e00\u5757\u5185\u5b58\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

malloc()\u6e90\u7801\u89c4\u5b9a\u4e86\u4e00\u4e2a\u9608\u503c\uff1a

  • \u5982\u679c\u5206\u914d\u7684\u5185\u5b58\u5c0f\u4e8e\u8fd9\u4e2a\u9608\u503c\uff0c\u5219\u901a\u8fc7brk()\u7533\u8bf7\u5185\u5b58\u3002

  • \u5982\u679c\u5206\u914d\u7684\u5185\u5b58\u5927\u4e8e\u8fd9\u4e2a\u9608\u503c\uff0c\u5219\u901a\u8fc7mmap()\u7533\u8bf7\u5185\u5b58\u3002

\u8bbe\u8ba1\u5185\u5b58\u5206\u914d\u7b56\u7565\u65f6\uff0c\u6838\u5fc3\u76ee\u6807\u662f\u5728\u6027\u80fd\u548c\u8d44\u6e90\u7ba1\u7406\u4e4b\u95f4\u627e\u5230\u6700\u4f73\u5e73\u8861\u70b9\u3002\u5bf9\u4e8e\u5c0f\u5185\u5b58\u5757\u7684\u9891\u7e41\u5206\u914d\u4e0e\u91ca\u653e\u64cd\u4f5c\uff0cmalloc() \u91c7\u7528 brk() \u7cfb\u7edf\u8c03\u7528\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3002\u91ca\u653e\u7684\u5185\u5b58\u5e76\u4e0d\u4f1a\u7acb\u5373\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\uff0c\u800c\u662f\u88ab\u7f13\u5b58\u81f3\u5185\u5b58\u6c60\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5206\u914d\u8bf7\u6c42\u80fd\u591f\u5feb\u901f\u91cd\u7528\u8fd9\u4e9b\u5df2\u91ca\u653e\u7684\u5185\u5b58\u5757\uff0c\u4ece\u800c\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570\u548c\u76f8\u5173\u7684\u6027\u80fd\u5f00\u9500\u3002

\u5bf9\u4e8e\u5927\u5185\u5b58\u5757\u7684\u5206\u914d\uff0cmalloc \u5219\u53ef\u80fd\u4f7f\u7528 mmap() \u7cfb\u7edf\u8c03\u7528\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u9996\u6b21\u8bbf\u95ee\u65f6\u53ef\u80fd\u4f1a\u89e6\u53d1\u7f3a\u9875\u4e2d\u65ad\uff0c\u56e0\u4e3a\u5206\u914d\u7684\u5185\u5b58\u521d\u59cb\u5904\u4e8e\u672a\u6620\u5c04\u72b6\u6001\uff0c\u9700\u8981\u64cd\u4f5c\u7cfb\u7edf\u4ecb\u5165\u4ee5\u5efa\u7acb\u865a\u62df\u5730\u5740\u5230\u7269\u7406\u5185\u5b58\u7684\u6620\u5c04\u3002\u8fd9\u4e00\u673a\u5236\u76f8\u8f83\u4e8e brk() \u7cfb\u7edf\u8c03\u7528\uff0c\u4f1a\u5e26\u6765\u66f4\u9ad8\u7684\u6027\u80fd\u6210\u672c\u3002

"},{"location":"linux/optimization/","title":"\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/#table-of-contents","title":"Table of Contents","text":"
  • CPU\u6027\u80fd
  • \u5185\u5b58\u6027\u80fd
  • \u78c1\u76d8I/O\u6027\u80fd
  • \u7f51\u7edc\u6027\u80fd
"},{"location":"linux/optimization/#_2","title":"\u53c2\u8003\u8d44\u6599","text":"
  • Linux\u6027\u80fd\u4f18\u5316\u5b9e\u6218
"},{"location":"linux/optimization/cpu/","title":"CPU\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/cpu/#cpu_1","title":"CPU\u6027\u80fd\u6307\u6807","text":""},{"location":"linux/optimization/cpu/#cpu_2","title":"CPU\u4f7f\u7528\u7387","text":"

CPU\u4f7f\u7528\u7387\uff0c\u662f\u6307\u5355\u4f4d\u65f6\u95f4\u5185CPU\u4f7f\u7528\u60c5\u51b5\u7684\u7edf\u8ba1\uff0c\u4ee5\u767e\u5206\u6bd4\u7684\u65b9\u5f0f\u5c55\u793a\u3002

\u4e0eCPU\u4f7f\u7528\u7387\u76f8\u5173\u7684\u6307\u6807\u6709\u8bb8\u591a\uff1a

  • user\uff1a\u7528\u6237\u6001\u65f6\u95f4
  • nice\uff1a\u4f4e\u4f18\u5148\u7ea7\u7528\u6237\u6001\u65f6\u95f4
  • system\uff1a\u5185\u6838\u6001\u65f6\u95f4
  • idle\uff1a\u7a7a\u95f2\u65f6\u95f4
  • iowait\uff1a\u7b49\u5f85I/O\u5b8c\u6210\u7684\u65f6\u95f4
  • irq\uff1a\u786c\u4e2d\u65ad\u65f6\u95f4
  • softirq\uff1a\u8f6f\u4e2d\u65ad\u65f6\u95f4
  • steal\uff1a\u7ba1\u7406\u7a0b\u5e8f\u65f6\u95f4
  • guest\uff1a\u8fd0\u884c\u865a\u62df\u5904\u7406\u5668\u7684\u65f6\u95f4
  • guest_nice\uff1a\u4f4e\u4f18\u5148\u7ea7\u8fd0\u884c\u865a\u62df\u5904\u7406\u5668\u7684\u65f6\u95f4

\u4f7f\u7528perf top\u5b9e\u65f6\u663e\u793a\u5360\u7528CPU\u65f6\u949f\u6700\u591a\u7684\u51fd\u6570\u6216\u8005\u6307\u4ee4\uff1a

$ perf top\nSamples: 833  of event 'cpu-clock', Event count (approx.): 97742399\nOverhead  Shared Object       Symbol\n   7.28%  perf                [.] 0x00000000001f78a4\n   4.72%  [kernel]            [k] vsnprintf\n   4.32%  [kernel]            [k] module_get_kallsym\n   3.65%  [kernel]            [k] _raw_spin_unlock_irqrestore\n...\n

\u8f93\u51fa\u7ed3\u679c\u7684\u7b2c\u4e00\u884c\u5206\u522b\u4ee3\u8868\u91c7\u6837\u6570\uff08Samples\uff09\u3001\u4e8b\u4ef6\u7c7b\u578b\uff08event\uff09\u3001\u4e8b\u4ef6\u8ba1\u6570\uff08Event count\uff09\u3002

\u4e0b\u65b9\u7684\u8868\u683c\u5404\u5217\u7684\u542b\u4e49\u662f\uff1a

  • Overhead\uff1a\u5f53\u524d\u51fd\u6570\u6216\u8005\u6307\u4ee4\u5360\u7528\u7684CPU\u65f6\u949f\u767e\u5206\u6bd4
  • Shared Object\uff1a\u51fd\u6570\u6216\u8005\u6307\u4ee4\u6240\u5728\u7684\u52a8\u6001\u94fe\u63a5\u5e93\u540d
  • Symbol\uff1a\u51fd\u6570\u6216\u8005\u6307\u4ee4\u540d\uff0c16\u8fdb\u5236\u5730\u5740\u4ee3\u8868\u672a\u77e5\u7b26\u53f7

perf top\u4e0d\u4fdd\u5b58\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528perf record\u548cperf report\u547d\u4ee4\u6765\u8bb0\u5f55\u548c\u5206\u6790\u6570\u636e\uff0c\u540c\u65f6\u52a0\u4e0a-g\u53c2\u6570\u4ee5\u663e\u793a\u8c03\u7528\u5173\u7cfb\u94fe\u3002

"},{"location":"linux/optimization/cpu/#_1","title":"\u5e73\u5747\u8d1f\u8f7d","text":"

\u6bcf\u5f53\u7cfb\u7edf\u53d8\u6162\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f\u6267\u884ctop\u6216\u8005uptime\u547d\u4ee4\u6765\u4e86\u89e3\u4e0b\u7cfb\u7edf\u7684\u8d1f\u8f7d\u60c5\u51b5\uff0c\u6bd4\u5982\uff1a

$ uptime\n02:34:03 up 2 days, 20:14,  1 user,  load average: 0.63, 0.83, 0.88\n

\u5bf9\u5e94\u7684\u542b\u4e49\u662f\uff1a

02:34:03              //\u5f53\u524d\u65f6\u95f4\nup 2 days, 20:14      //\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4\n1 user                //\u6b63\u5728\u767b\u5f55\u7528\u6237\u6570\nload average: 0.63, 0.83, 0.88  //1\u5206\u949f\u30015\u5206\u949f\u300115\u5206\u949f\u7684\u5e73\u5747\u8d1f\u8f7d\n

\u4ec0\u4e48\u662f\u5e73\u5747\u8d1f\u8f7d\uff1f\u5e73\u5747\u8d1f\u8f7d\u662f\u6307\u5355\u4f4d\u65f6\u95f4\u5185\uff0c\u7cfb\u7edf\u5904\u4e8e\u53ef\u8fd0\u884c\u72b6\u6001\u548c\u4e0d\u53ef\u4e2d\u65ad\u72b6\u6001\u7684\u5e73\u5747\u8fdb\u7a0b\u6570\uff0c\u4e5f\u5c31\u662f\u5e73\u5747\u6d3b\u8dc3\u8fdb\u7a0b\u6570\uff0c\u5b83\u548cCPU\u4f7f\u7528\u7387\u5e76\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\u3002

\u6240\u8c13\u53ef\u8fd0\u884c\u72b6\u6001\u7684\u8fdb\u7a0b\uff0c\u662f\u6307\u6b63\u5728\u4f7f\u7528CPU\u6216\u8005\u6b63\u5728\u7b49\u5f85CPU\u7684\u8fdb\u7a0b\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u4f7f\u7528ps\u547d\u4ee4\u770b\u5230\u7684\uff0c\u5904\u4e8eR\u72b6\u6001\uff08Running\u6216Runnable\uff09\u7684\u8fdb\u7a0b\u3002

\u4e0d\u53ef\u4e2d\u65ad\u72b6\u6001\u7684\u8fdb\u7a0b\u5219\u662f\u6b63\u5904\u4e8e\u5185\u6838\u6001\u5173\u952e\u6d41\u7a0b\u4e2d\u7684\u8fdb\u7a0b\uff0c\u5e76\u4e14\u8fd9\u4e9b\u6d41\u7a0b\u4e0d\u80fd\u88ab\u6253\u65ad\u3002\u6bd4\u5982\u6700\u5e38\u89c1\u7684\u662f\u7b49\u5f85\u78c1\u76d8I/O\u7684\u8fdb\u7a0b\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u4f7f\u7528ps\u547d\u4ee4\u770b\u5230\u7684\uff0c\u5904\u4e8eD\u72b6\u6001\uff08Uninterruptible Sleep\uff0c\u4e5f\u88ab\u79f0\u4e3aDisk Sleep\uff09\u7684\u8fdb\u7a0b\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u5411\u78c1\u76d8\u8bfb\u5199\u6570\u636e\u65f6\uff0c\u5b83\u9700\u8981\u7b49\u5f85\u78c1\u76d8\u7684\u56de\u5e94\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u662f\u4e0d\u80fd\u88ab\u6253\u65ad\u7684\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u78c1\u76d8\u548c\u5185\u5b58\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002

\u6240\u4ee5\u5e73\u5747\u8d1f\u8f7d\u53ef\u4ee5\u7b80\u5355\u5730\u7406\u89e3\u4e3a\u662f\u5e73\u5747\u6d3b\u8dc3\u8fdb\u7a0b\u6570\uff0c\u6700\u7406\u60f3\u7684\u60c5\u51b5\u662f\u6bcf\u4e2aCPU\u521a\u597d\u8fd0\u884c\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u8fd9\u65f6\u7684\u5e73\u5747\u8d1f\u8f7d\u5c31\u7b49\u4e8eCPU\u7684\u4e2a\u6570\u3002\u6bd4\u5982\u5728\u53ea\u67092\u4e2aCPU\u7684\u7cfb\u7edf\u4e0a\uff0c\u5982\u679c\u5e73\u5747\u8d1f\u8f7d\u662f2\uff0c\u90a3\u4e48\u610f\u5473\u7740\u6240\u6709\u7684CPU\u90fd\u521a\u597d\u88ab\u5b8c\u5168\u5360\u7528\uff1b\u5982\u679c\u5e73\u5747\u8d1f\u8f7d\u662f4\uff0c\u610f\u5473\u7740\u6709\u4e00\u534a\u7684\u8fdb\u7a0b\u7ade\u4e89\u4e0d\u5230CPU\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2/proc/cpuoinfo\u6765\u83b7\u53d6\u5f53\u524d\u7cfb\u7edf\u6709\u51e0\u4e2aCPU\u3002

\u5e73\u5747\u8d1f\u8f7d\u6709\u4e09\u4e2a\u503c\uff0c\u6211\u4eec\u9700\u8981\u7efc\u5408\u67e5\u770b\uff0c\u5206\u6790\u7cfb\u7edf\u8d1f\u8f7d\u7684\u8d8b\u52bf\uff1a

  • \u5982\u679c\u8fd9\u4e09\u4e2a\u503c\u76f8\u5dee\u4e0d\u5927\uff0c\u8bf4\u660e\u7cfb\u7edf\u8fd0\u884c\u5e73\u7a33\u3002
  • \u5982\u679c1\u5206\u949f\u7684\u503c\u8fdc\u5c0f\u4e8e15\u5206\u949f\u7684\u503c\uff0c\u8bf4\u660e\u7cfb\u7edf\u6700\u8fd1\u7684\u8d1f\u8f7d\u5728\u51cf\u5c11\u3002
  • \u800c\u53cd\u8fc7\u6765\uff0c\u5982\u679c1\u5206\u949f\u7684\u503c\u8fdc\u5927\u4e8e15\u5206\u949f\u7684\u503c\uff0c\u8bf4\u660e\u7cfb\u7edf\u6700\u8fd1\u7684\u8d1f\u8f7d\u5728\u589e\u52a0\u3002

\u5e73\u5747\u8d1f\u8f7d\u548cCPU\u4f7f\u7528\u7387\u4e0d\u4e00\u5b9a\u5b8c\u5168\u5bf9\u5e94\uff0c\u6bd4\u5982\uff1a

  • CPU\u5bc6\u96c6\u578b\u8fdb\u7a0b\uff0c\u4f7f\u7528\u5927\u91cfCPU\u4e5f\u4f1a\u5bfc\u81f4\u5e73\u5747\u8d1f\u8f7d\u5347\u9ad8\uff0c\u6b64\u65f6\u4e24\u8005\u5bf9\u5e94\u3002
  • I/O\u5bc6\u96c6\u578b\u8fdb\u7a0b\uff0c\u7b49\u5f85I/O\u4f1a\u5bfc\u81f4\u5e73\u5747\u8d1f\u8f7d\u5347\u9ad8\uff0c\u4f46\u662fCPU\u4f7f\u7528\u7387\u4e0d\u4e00\u5b9a\u5f88\u9ad8\u3002
  • \u5927\u91cf\u7b49\u5f85CPU\u7684\u8fdb\u7a0b\u8c03\u5ea6\u4f1a\u5bfc\u81f4\u5e73\u5747\u8d1f\u8f7d\u5347\u9ad8\uff0c\u6b64\u65f6\u7684CPU\u4f7f\u7528\u7387\u4e5f\u4f1a\u6bd4\u8f83\u9ad8\u3002
"},{"location":"linux/optimization/cpu/#_2","title":"\u4e0a\u4e0b\u6587\u5207\u6362","text":"

\u4f17\u6240\u5468\u77e5\uff0cLinux\u662f\u4e00\u4e2a\u591a\u4efb\u52a1\u64cd\u4f5c\u7cfb\u7edf\uff0c\u652f\u6301\u8fdc\u5927\u4e8e\u5176CPU\u6838\u5fc3\u6570\u7684\u8fdb\u7a0b\u8fd0\u884c\u3002\u8fd9\u4e9b\u8fdb\u7a0b\u5e76\u975e\u540c\u65f6\u8fd0\u884c\uff0c\u800c\u662f\u4f7f\u7528CPU\u7684\u8c03\u5ea6\u7b97\u6cd5\u8f6e\u6d41\u6267\u884c\u3002\u5728\u6bcf\u4e2a\u4efb\u52a1\u8fd0\u884c\u524d\uff0cCPU\u9700\u8981\u77e5\u9053\u4efb\u52a1\u4ece\u54ea\u91cc\u52a0\u8f7d\u3001\u8fd0\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\u7cfb\u7edf\u9700\u8981\u63d0\u524d\u8bbe\u7f6e\u5bc4\u5b58\u5668\u4ee5\u53ca\u7a0b\u5e8f\u8ba1\u6570\u5668\u7684\u5185\u5bb9\uff0c\u8fd9\u79cd\u73af\u5883\u88ab\u79f0\u4e3aCPU\u4e0a\u4e0b\u6587\u3002

\u800cCPU\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u5c31\u662f\u628a\u4e0a\u4e00\u4e2a\u4efb\u52a1\u7684\u73af\u5883\u4fdd\u5b58\u8d77\u6765\uff0c\u7136\u540e\u52a0\u8f7d\u4e0b\u4e00\u4e2a\u4efb\u52a1\u7684\u73af\u5883\u3002\u6682\u65f6\u6302\u8d77\u7684\u4efb\u52a1\u4f1a\u5728\u672a\u6765\u7684\u67d0\u4e2a\u65f6\u95f4\u70b9\u4e0a\u7ee7\u7eed\u6267\u884c\u3002

\u7531\u4e8e\u8fd0\u884c\u4efb\u52a1\u7684\u4e0d\u540c\uff0cCPU\u4e0a\u4e0b\u6587\u5207\u6362\u4e5f\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684\u73af\u5883\u5206\u4e3a\u4e0d\u540c\u7684\u573a\u666f\uff1a

  • \u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\uff1a\u8fdb\u7a0b\u662fLinux\u7cfb\u7edf\u4e2d\u6700\u57fa\u672c\u7684\u6267\u884c\u5355\u5143\uff0c\u8fdb\u7a0b\u7684\u5207\u6362\u7531\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u6765\u5b8c\u6210\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u4fdd\u5b58\u548c\u6062\u590d\u8fdb\u7a0b\u7684\u5404\u79cd\u5bc4\u5b58\u5668\u72b6\u6001\u3001\u5185\u5b58\u9875\u7b49\u6570\u636e\uff0c\u56e0\u6b64\uff0c\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u6bd4\u8f83\u5927\u3002
  • \u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\uff1a\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u8fdb\u7a0b\uff0c\u540c\u4e00\u8fdb\u7a0b\u7684\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u662f\u5171\u4eab\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u7684\uff0c\u56e0\u6b64\uff0c\u7ebf\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u6bd4\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u8981\u5feb\u5f97\u591a\u3002
  • \u4e2d\u65ad\u4e0a\u4e0b\u6587\u5207\u6362\uff1a\u4e3a\u4e86\u5feb\u901f\u54cd\u5e94\u786c\u4ef6\u7684\u4e8b\u60c5\uff0c\u4e00\u4e2a\u786c\u4ef6\u4e2d\u65ad\u4f1a\u968f\u65f6\u6253\u65ad\u8fdb\u7a0b\u7684\u6b63\u5e38\u6267\u884c\uff0c\u8f6c\u800c\u6267\u884c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002\u5728\u6253\u65ad\u5176\u4ed6\u8fdb\u7a0b\u65f6\uff0c\u5c31\u9700\u8981\u5c06\u8fdb\u7a0b\u5f53\u524d\u7684\u72b6\u6001\u4fdd\u5b58\u4e0b\u6765\uff0c\u4ee5\u4fbf\u540e\u7eed\u6062\u590d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u53eb\u505a\u4e2d\u65ad\u4e0a\u4e0b\u6587\u5207\u6362\u3002

\u5bf9\u4e8e\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u8fd9\u91cc\u9700\u8981\u8bf4\u660e\u4e00\u4e0b\u3002\u4e00\u6b21\u7cfb\u7edf\u8c03\u7528\u6d89\u53ca\u5230\u4ece\u7528\u6237\u6001\u5230\u5185\u6838\u6001\uff0c\u518d\u4ece\u5185\u6838\u6001\u5230\u7528\u6237\u6001\uff0c\u4e00\u5171\u4e24\u6b21CPU\u4e0a\u4e0b\u6587\u5207\u6362\u3002

\u5bf9\u4e8e\u4e00\u822c\u7684\u8fdb\u7a0b\u4e4b\u95f4\u7684\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u53ea\u6709\u5728\u8fdb\u7a0b\u8c03\u5ea6\u7684\u65f6\u5019\u624d\u4f1a\u7528\u5230\u3002Linux\u4e3a\u6bcf\u4e2aCPU\u90fd\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5c31\u7eea\u961f\u5217\uff0c\u5c06\u6d3b\u8dc3\u8fdb\u7a0b\uff08\u6b63\u5728\u8fd0\u884c\u548c\u7b49\u5f85\u8fd0\u884c\u7684\u8fdb\u7a0b\uff09\u6309\u7167\u4f18\u5148\u7ea7\u548c\u7b49\u5f85\u65f6\u95f4\u6392\u5e8f\uff0c\u7136\u540e\u9009\u62e9\u6700\u9700\u8981CPU\u7684\u8fdb\u7a0b\uff0c\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u8bf7\u770b\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5\u3002\u8fdb\u7a0b\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\uff0c\u4f1a\u89e6\u53d1\u8fdb\u7a0b\u8c03\u5ea6\uff1a

  • \u5206\u914d\u4e2a\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u8017\u5c3d\u3002
  • \u8fdb\u7a0b\u5728\u7b49\u5f85\u67d0\u4e2a\u8d44\u6e90\u3002
  • \u8fdb\u7a0b\u8c03\u7528sleep()\u7b49\u51fd\u6570\u4e3b\u52a8\u6302\u8d77\u3002
  • \u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0b\u9700\u8981\u8fd0\u884c\u3002
  • \u53d1\u751f\u786c\u4ef6\u4e2d\u65ad\u3002

\u4e0a\u4e0b\u6587\u5207\u6362\u662f\u8861\u91cf\u6027\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u6307\u6807\uff0c\u5b83\u53ef\u4ee5\u662f\u81ea\u613f\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u975e\u81ea\u613f\u7684\uff1a

  • \u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u53d8\u591a\u4e86\uff0c\u8bf4\u660e\u8fdb\u7a0b\u90fd\u5728\u7b49\u5f85\u8d44\u6e90\uff0c\u53ef\u80fd\u53d1\u751f\u4e86I/O\u95ee\u9898\u3002
  • \u975e\u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u53d8\u591a\u4e86\uff0c\u8bf4\u660e\u8fdb\u7a0b\u90fd\u5728\u88ab\u5f3a\u5236\u8c03\u5ea6\uff0c\u53ef\u80fd\u53d1\u751f\u4e86CPU\u7ade\u4e89\u95ee\u9898\u3002
"},{"location":"linux/optimization/cpu/#cpu_3","title":"CPU\u7f13\u5b58\u547d\u4e2d\u7387","text":"

CPU\u5177\u6709L1\u3001L2\u3001L3\u4e09\u7ea7\u7f13\u5b58\uff0c\u5176\u4e2dL3\u662f\u5171\u4eab\u7684\u3002\u4eceL1\u5230L3\uff0c\u7f13\u5b58\u7684\u5927\u5c0f\u4f9d\u6b21\u589e\u52a0\uff0c\u4f46\u662f\u6027\u80fd\u4f9d\u6b21\u964d\u4f4e\u3002\u5b83\u4eec\u7684\u547d\u4ee4\u7387\uff0c\u8861\u91cf\u7684\u662fCPU\u7f13\u5b58\u7684\u590d\u7528\u60c5\u51b5\u3002

"},{"location":"linux/optimization/cpu/#cpu_4","title":"CPU\u6027\u80fd\u5206\u6790\u5de5\u5177","text":"

\u5176\u4e2dtop\u3001vmstat\u3001pidstat\u51e0\u4e4e\u6db5\u76d6\u4e86\u6240\u6709\u91cd\u8981\u7684CPU\u6027\u80fd\u6307\u6807\u3002

\u4f7f\u7528top\u547d\u4ee4\u52a8\u6001\u76d1\u6d4bCPU\uff1a

$ top\n  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND\n28961 root      20   0   43816   3148   4040 R   3.2  0.0   0:00.01 top\n  620 root      20   0   37280  33676    908 D   0.3  0.4   0:00.01 app\n    1 root      20   0  160072   9416   6752 S   0.0  0.1   0:37.64 systemd\n 1896 root      20   0       0      0      0 Z   0.0  0.0   0:00.00 devapp\n    2 root      20   0       0      0      0 S   0.0  0.0   0:00.10 kthreadd\n    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H\n    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq\n    7 root      20   0       0      0      0 S   0.0  0.0   0:06.37 ksoftirqd/0\n...\n
  • \u4f7f\u7528uptime\u67e5\u770b\u7cfb\u7edf\u5e73\u5747\u8d1f\u8f7d\uff1a
$ uptime\n02:34:03 up 2 days, 20:14,  1 user,  load average: 0.63, 0.83, 0.88\n
  • \u4f7f\u7528mpstat\u67e5\u770bCPU\u4f7f\u7528\u7387\uff1a
# -P ALL \u8868\u793a\u76d1\u63a7\u6240\u6709CPU\uff0c\u540e\u9762\u6570\u5b575\u8868\u793a\u95f4\u96945\u79d2\u540e\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n$ mpstat -P ALL 5\nLinux 4.15.0 (ubuntu) 09/22/18 _x86_64_ (2 CPU)\n13:30:06     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle\n13:30:11     all   50.05    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   49.95\n13:30:11       0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00\n13:30:11       1  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00\n
  • \u4f7f\u7528pidstat\u67e5\u770b\u8fdb\u7a0b\u7684CPU\u4f7f\u7528\u7387\uff1a
# \u95f4\u96945\u79d2\u540e\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n$ pidstat -u 5 1\n13:37:07      UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command\n13:37:12        0      2962  100.00    0.00    0.00    0.00  100.00     1  stress\n
  • \u4f7f\u7528vmstat\u67e5\u770b\u4e0a\u4e0b\u6587\u5207\u6362\u60c5\u51b5\uff1a
# \u6bcf\u96945\u79d2\u8f93\u51fa1\u7ec4\u6570\u636e\n$ vmstat 5\nprocs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----\n r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st\n 0  0      0 7005360  91564 818900    0    0     0     0   25   33  0  0 100  0  0\n

cs\uff1a\u6bcf\u79d2\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570\u3002

in\uff1a\u6bcf\u79d2\u4e2d\u65ad\u6b21\u6570\u3002

r\uff1a\u5c31\u7eea\u961f\u5217\u957f\u5ea6\u3002

b\uff1a\u7b49\u5f85\u8d44\u6e90\u7684\u8fdb\u7a0b\u6570\u3002

  • \u4f7f\u7528pidstat\u67e5\u770b\u6bcf\u4e2a\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u60c5\u51b5\uff1a
# \u6bcf\u96945\u79d2\u8f93\u51fa1\u7ec4\u6570\u636e\n$ pidstat -w 5\nLinux 4.15.0 (ubuntu)  09/23/18  _x86_64_  (2 CPU)\n\n08:18:26      UID       PID   cswch/s nvcswch/s  Command\n08:18:31        0         1      0.20      0.00  systemd\n08:18:31        0         8      5.40      0.00  rcu_sched\n...\n

cswch/s\uff1a\u6bcf\u79d2\u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570\u3002

nvcswch/s\uff1a\u6bcf\u79d2\u975e\u81ea\u613f\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570\u3002

  • \u4f7f\u7528dstat\u67e5\u770bCPU\u548cI/O\u4f7f\u7528\u60c5\u51b5\uff1a
# \u95f4\u96941\u79d2\u8f93\u51fa10\u7ec4\u6570\u636e\n$ dstat 1 10\nYou did not select any stats, using -cdngy by default.\n--total-cpu-usage-- -dsk/total- -net/total- ---paging-- ---system--\nusr sys idl wai stl| read  writ| recv  send|  in   out | int   csw\n  0   0  96   4   0|1219k  408k|   0     0 |   0     0 |  42   885\n  0   0   2  98   0|  34M    0 | 198B  790B|   0     0 |  42   138\n  0   0   0 100   0|  34M    0 |  66B  342B|   0     0 |  42   135\n  0   0  84  16   0|5633k    0 |  66B  342B|   0     0 |  52   177\n  0   3  39  58   0|  22M    0 |  66B  342B|   0     0 |  43   144\n  0   0   0 100   0|  34M    0 | 200B  450B|   0     0 |  46   147\n  0   0   2  98   0|  34M    0 |  66B  342B|   0     0 |  45   134\n  0   0   0 100   0|  34M    0 |  66B  342B|   0     0 |  39   131\n  0   0  83  17   0|5633k    0 |  66B  342B|   0     0 |  46   168\n  0   3  39  59   0|  22M    0 |  66B  342B|   0     0 |  37   134\n...\n
  • \u4f7f\u7528sar\u67e5\u770b\u7cfb\u7edf\u7684\u7f51\u7edc\u6536\u53d1\u60c5\u51b5\uff1a
# -n DEV \u8868\u793a\u663e\u793a\u7f51\u7edc\u6536\u53d1\u7684\u62a5\u544a\uff0c\u95f4\u96941\u79d2\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n$ sar -n DEV 1\n15:03:46        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil\n15:03:47         eth0  12607.00   6304.00    664.86    358.11      0.00      0.00      0.00      0.01\n15:03:47      docker0   6302.00  12604.00    270.79    664.66      0.00      0.00      0.00      0.00\n15:03:47           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00\n15:03:47    veth9f6bbcd   6302.00  12604.00    356.95    664.66      0.00      0.00      0.00      0.05\n

IFACE\uff1a\u7f51\u5361

rxpck/txpck\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u6570\u636e\u5305

rxkB/txkB\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u5343\u5b57\u8282

rxcmp/txcmp\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u538b\u7f29\u6570\u636e\u5305

rxmcst/txmcst\uff1a\u6bcf\u79d2\u63a5\u6536\u4e0e\u53d1\u9001\u7684\u591a\u64ad\u6570\u636e\u5305

%ifutil\uff1a\u7f51\u7edc\u63a5\u53e3\u7684\u4f7f\u7528\u7387

  • \u4f7f\u7528tcpdump\u6293\u53d6\u7f51\u7edc\u5305\uff1a
# -i eth0 \u53ea\u6293\u53d6eth0\u7f51\u5361\uff0c-n\u4e0d\u89e3\u6790\u534f\u8bae\u540d\u548c\u4e3b\u673a\u540d\n# tcp port 80\u8868\u793a\u53ea\u6293\u53d6tcp\u534f\u8bae\u5e76\u4e14\u7aef\u53e3\u53f7\u4e3a80\u7684\u7f51\u7edc\u5e27\n$ tcpdump -i eth0 -n tcp port 80\n15:11:32.678966 IP 192.168.0.2.18238 > 192.168.0.30.80: Flags [S], seq 458303614, win 512, length 0\n...\n
"},{"location":"linux/optimization/disk/","title":"\u78c1\u76d8I/O\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/disk/#_1","title":"\u78c1\u76d8\u6027\u80fd\u6307\u6807","text":"
  • \u4f7f\u7528\u7387\uff1a\u78c1\u76d8\u5904\u7406I/O\u7684\u65f6\u95f4\u767e\u5206\u6bd4\u3002
  • \u9971\u548c\u5ea6\uff1a\u78c1\u76d8\u5904\u7406I/O\u7684\u7e41\u5fd9\u7a0b\u5ea6\u3002
  • IOPS\uff1a\u6bcf\u79d2\u7684I/O\u8bf7\u6c42\u6570\u3002
  • \u541e\u5410\u91cf\uff1a\u6bcf\u79d2\u7684I/O\u8bf7\u6c42\u6570\u636e\u91cf\u3002
  • \u54cd\u5e94\u65f6\u95f4\uff1aI/O\u8bf7\u6c42\u4ece\u53d1\u51fa\u5230\u6536\u5230\u54cd\u5e94\u7684\u95f4\u9694\u65f6\u95f4\u3002
"},{"location":"linux/optimization/disk/#_2","title":"\u78c1\u76d8\u6027\u80fd\u5206\u6790\u5de5\u5177","text":"
  • \u4f7f\u7528iostat\u89c2\u5bdf\u78c1\u76d8I/O\u4f7f\u7528\u60c5\u51b5\uff1a
# -d -x\u8868\u793a\u663e\u793a\u6240\u6709\u78c1\u76d8I/O\u7684\u6307\u6807\n$ iostat -d -x 1 \nDevice            r/s     w/s     rkB/s     wkB/s   rrqm/s   wrqm/s  %rrqm  %wrqm r_await w_await aqu-sz rareq-sz wareq-sz  svctm  %util \nloop0            0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \nloop1            0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \nsda              0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \nsdb              0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00 \n
  • \u4f7f\u7528pidstat\u89c2\u5bdf\u8fdb\u7a0bI/O\u4f7f\u7528\u60c5\u51b5\uff1a
$ pidstat -d 1 \n13:39:51      UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command \n13:39:52      102       916      0.00      4.00      0.00       0  rsyslogd\n
  • \u4f7f\u7528iotop\u5bf9\u8fdb\u7a0bI/O\u6392\u5e8f\uff1a
$ iotop\nTotal DISK READ :       0.00 B/s | Total DISK WRITE :       7.85 K/s \nActual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s \n  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND \n15055 be/3 root        0.00 B/s    7.85 K/s  0.00 %  0.00 % systemd-journald \n
  • \u4f7f\u7528filetop\u8ddf\u8e2a\u6587\u4ef6\u7684\u8bfb\u5199\u60c5\u51b5\uff1a
# -C \u9009\u9879\u8868\u793a\u8f93\u51fa\u65b0\u5185\u5bb9\u65f6\u4e0d\u6e05\u7a7a\u5c4f\u5e55 \n$ ./filetop -C \n\nTID    COMM             READS  WRITES R_Kb    W_Kb    T FILE \n514    python           0      1      0       2832    R 669.txt \n514    python           0      1      0       2490    R 667.txt \n514    python           0      1      0       2685    R 671.txt \n514    python           0      1      0       2392    R 670.txt \n514    python           0      1      0       2050    R 672.txt \n\n...\n\nTID    COMM             READS  WRITES R_Kb    W_Kb    T FILE \n514    python           2      0      5957    0       R 651.txt \n514    python           2      0      5371    0       R 112.txt \n514    python           2      0      4785    0       R 861.txt \n514    python           2      0      4736    0       R 213.txt \n514    python           2      0      4443    0       R 45.txt \n
"},{"location":"linux/optimization/memory/","title":"\u5185\u5b58\u6027\u80fd\u8c03\u4f18","text":""},{"location":"linux/optimization/memory/#_2","title":"\u5185\u5b58\u6027\u80fd\u6307\u6807","text":""},{"location":"linux/optimization/memory/#buffercache","title":"Buffer/Cache","text":"

Buffer\u548cCache\u88ab\u8bbe\u8ba1\u7528\u6765\u63d0\u5347\u7cfb\u7edfI/O\u7684\u6027\u80fd\uff0c\u5b83\u4eec\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5145\u5f53\u8d77\u6162\u901f\u78c1\u76d8\u4e0e\u5feb\u901fCPU\u4e4b\u95f4\u7684\u6865\u6881\u3002

  • Buffers\u662f\u5bf9\u78c1\u76d8\u6570\u636e\u7684\u7f13\u5b58\uff0c\u901a\u5e38\u4e0d\u4f1a\u7279\u522b\u5927\u3002

  • Cached\u662f\u5bf9\u6587\u4ef6\u6570\u636e\u7684\u7f13\u5b58\u3002

\u4e0d\u8bba\u662f\u8bfb\u8fd8\u662f\u5199\uff0c\u5e94\u7528\u7a0b\u5e8f\u90fd\u4f18\u5148\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u8bbf\u95ee\u4f4e\u901f\u7684\u78c1\u76d8\uff0c\u4ece\u800c\u63d0\u9ad8\u7cfb\u7edf\u7684I/O\u6027\u80fd\u3002

"},{"location":"linux/optimization/memory/#_3","title":"\u5185\u5b58\u6027\u80fd\u5206\u6790\u5de5\u5177","text":"
  • \u4f7f\u7528free\u67e5\u770b\u7cfb\u7edf\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a
$ free\n              total        used        free      shared  buff/cache   available\nMem:        8169348      263524     6875352         668     1030472     7611064\nSwap:             0           0           0\n

total\uff1a\u603b\u5185\u5b58\u5927\u5c0f

used\uff1a\u5df2\u7ecf\u4f7f\u7528\u7684\u5185\u5b58\u5927\u5c0f

free\uff1a\u7a7a\u95f2\u7684\u5185\u5b58\u5927\u5c0f

shared\uff1a\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u7684\u5185\u5b58\u5927\u5c0f

buff/cache\uff1a\u7f13\u5b58\u548c\u7f13\u51b2\u533a\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f

available\uff1a\u53ef\u7528\u7684\u5185\u5b58\u5927\u5c0f

  • \u4f7f\u7528top\u67e5\u770b\u8fdb\u7a0b\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a
$ top\n...\nKiB Mem :  8169348 total,  6871440 free,   267096 used,  1030812 buff/cache\nKiB Swap:        0 total,        0 free,        0 used.  7607492 avail Mem\n\n\n  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND\n  430 root      19  -1  122360  35588  23748 S   0.0  0.4   0:32.17 systemd-journal\n 1075 root      20   0  771860  22744  11368 S   0.0  0.3   0:38.89 snapd\n 1048 root      20   0  170904  17292   9488 S   0.0  0.2   0:00.24 networkd-dispat\n    1 root      20   0   78020   9156   6644 S   0.0  0.1   0:22.92 systemd\n12376 azure     20   0   76632   7456   6420 S   0.0  0.1   0:00.01 systemd\n12374 root      20   0  107984   7312   6304 S   0.0  0.1   0:00.00 sshd\n...\n

VIRT\uff1a\u8fdb\u7a0b\u4f7f\u7528\u7684\u865a\u62df\u5185\u5b58\u5927\u5c0f\uff0c\u7533\u8bf7\u8fc7\u4f46\u5c1a\u672a\u5206\u914d\u7269\u7406\u5185\u5b58\u4e5f\u4f1a\u8ba1\u7b97\u5728\u5185\u3002

RES\uff1a\u8fdb\u7a0b\u5b9e\u9645\u4f7f\u7528\u7684\u7269\u7406\u5185\u5b58\u5927\u5c0f\u3002

SHR\uff1a\u8fdb\u7a0b\u4f7f\u7528\u7684\u5171\u4eab\u5185\u5b58\u5927\u5c0f\uff0c\u6bd4\u5982\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002

%MEM\uff1a\u8fdb\u7a0b\u4f7f\u7528\u7684\u7269\u7406\u5185\u5b58\u5360\u7cfb\u7edf\u603b\u5185\u5b58\u7684\u767e\u5206\u6bd4\u3002

  • \u4f7f\u7528cachestat\u548ccachetop\u67e5\u770b\u7f13\u5b58\u547d\u4e2d\u60c5\u51b5\uff1a

\u8fd9\u4e24\u4e2a\u5de5\u5177\u662fbcc\u8f6f\u4ef6\u5305\u7684\u4e00\u90e8\u5206\uff0c\u57fa\u4e8eebpf\u6280\u672f\u3002

$ cachestat 1 3\n   TOTAL   MISSES     HITS  DIRTIES   BUFFERS_MB  CACHED_MB\n       2        0        2        1           17        279\n       2        0        2        1           17        279\n       2        0        2        1           17        279 \n

TOTAL\uff1a\u603b\u7f13\u5b58\u6b21\u6570\u3002

MISSES\uff1a\u7f13\u5b58\u672a\u547d\u4e2d\u7684\u6b21\u6570\u3002

HITS\uff1a\u7f13\u5b58\u547d\u4e2d\u7684\u6b21\u6570\u3002

DIRTIES\uff1a\u7f13\u5b58\u4e2ddirty pages\u810f\u9875\uff09\u7684\u6b21\u6570\u3002

BUFFERS_MB\uff1a\u7f13\u5b58Buffers\u7684\u5927\u5c0f\uff0c\u5355\u4f4d\u662fMB\u3002

CACHED_MB\uff1a\u7f13\u5b58\u4e2dCache\u7684\u6570\u91cf\uff0c\u5355\u4f4d\u662fMB\u3002

$ cachetop\n11:58:50 Buffers MB: 258 / Cached MB: 347 / Sort: HITS / Order: ascending\nPID      UID      CMD              HITS     MISSES   DIRTIES  READ_HIT%  WRITE_HIT%\n   13029 root     python                  1        0        0     100.0%       0.0%\n

READ_HIT\uff1a\u8bfb\u7f13\u5b58\u547d\u4e2d\u7387\u3002

WRITE_HIT\uff1a\u5199\u7f13\u5b58\u547d\u4e2d\u7387\u3002

  • \u4f7f\u7528smem\u5c06\u8fdb\u7a0b\u6309\u7167swap\u4f7f\u7528\u91cf\u6392\u5e8f\uff1a
$ smem --sort swap\n  PID User     Command                         Swap      USS      PSS      RSS \n1168907 ubuntu   bash                               0      468     1091     3576 \n1169141 ubuntu   /bin/bash --init-file /home        0     1060     1693     4416 \n1207585 ubuntu   sleep 180                          0      120      268     1916 \n1208733 ubuntu   /usr/bin/python3 /usr/bin/s        0    10908    11197    13956 \n1784762 ubuntu   /bin/bash --init-file /home        0      976     1038     3104 \n2002992 ubuntu   /lib/systemd/systemd --user        0     2052     2620     6248 \n2003004 ubuntu   /usr/libexec/tracker-miner-        0    10288    10768    16148 \n2003010 ubuntu   /usr/bin/dbus-daemon --sess        0      776      903     3868\n...\n
  • \u4f7f\u7528sar\u67e5\u770b\u5185\u5b58\u5404\u6307\u6807\u53d8\u5316\u60c5\u51b5\uff1a
# \u95f4\u96941\u79d2\u8f93\u51fa\u4e00\u7ec4\u6570\u636e\n# -r\u8868\u793a\u663e\u793a\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c-S\u8868\u793a\u663e\u793aSwap\u4f7f\u7528\u60c5\u51b5\n$ sar -r -S 1\n04:39:56    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty\n04:39:57      6249676   6839824   1919632     23.50    740512     67316   1691736     10.22    815156    841868         4\n\n04:39:56    kbswpfree kbswpused  %swpused  kbswpcad   %swpcad\n04:39:57      8388604         0      0.00         0      0.00\n\n04:39:57    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty\n04:39:58      6184472   6807064   1984836     24.30    772768     67380   1691736     10.22    847932    874224        20\n\n04:39:57    kbswpfree kbswpused  %swpused  kbswpcad   %swpcad\n04:39:58      8388604         0      0.00         0      0.00\n\n\u2026\n\n\n04:44:06    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty\n04:44:07       152780   6525716   8016528     98.13   6530440     51316   1691736     10.22    867124   6869332         0\n\n04:44:06    kbswpfree kbswpused  %swpused  kbswpcad   %swpcad\n04:44:07      8384508      4096      0.05        52      1.27\n
"},{"location":"linux/optimization/network/","title":"\u7f51\u7edc\u6027\u80fd\u8c03\u4f18","text":""},{"location":"others/","title":"\u4e0d\u592a\u597d\u5206\u7c7b\u7684\u653e\u8fd9","text":""},{"location":"others/#_2","title":"\u53c2\u8003\u8d44\u6599","text":"
  • \u7a0b\u5e8f\u5458\u7684\u81ea\u6211\u4fee\u517b
"},{"location":"others/ld/","title":"\u4ece\u4ee3\u7801\u5230\u53ef\u6267\u884c\u6587\u4ef6","text":""},{"location":"others/ld/#gcc","title":"gcc\u7f16\u8bd1\u6b65\u9aa4","text":""},{"location":"others/ld/#_2","title":"\u7a0b\u5e8f\u7684\u94fe\u63a5\u4e0e\u88c5\u8f7d","text":""},{"location":"others/ld/#_3","title":"\u7a0b\u5e8f\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f","text":"

\u7ecf\u8fc7\u9884\u5904\u7406\u3001\u7f16\u8bd1\u3001\u6c47\u7f16\u4ee5\u540e\uff0c.c\u6587\u4ef6\u88ab\u7f16\u8bd1\u6210\u4e86.o\u6587\u4ef6\uff0c\u53c8\u53eb\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6\uff0c\u8fd9\u662fELF\u683c\u5f0f\u7684\u4e00\u79cd\u3002\u8fd9\u4e2a\u6587\u4ef6\u683c\u5f0f\u662f\u8fd9\u6837\u7684\u3002

\u5176\u4e2dELF Header\u8bb0\u5f55\u4e00\u4e2a\u4e2asection\u7684\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528readelf -h\u67e5\u770b\u4e00\u4e2a.o\u6587\u4ef6\u7684Header\u4fe1\u606f\u3002\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u91cc\u5b58\u653e\u7740\u7a0b\u5e8f\u7684\u4ee3\u7801\u4ee5\u53ca\u53d8\u91cf\u7b49\u7b49\u4fe1\u606f\u3002

.text\uff1a\u5b58\u653e\u7f16\u8bd1\u597d\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\u3002

.rodata\uff1a\u5b58\u653e\u53ea\u8bfb\u6570\u636e\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u5e38\u91cf\u3002

.data\uff1a\u5b58\u653e\u5df2\u7ecf\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\u3002

.bss\uff1a\u5b58\u653e\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\u548c\u9759\u6001\u53d8\u91cf\uff0c\u5168\u90e8\u7f6e\u4e3a0.

.symtab\uff1a\u7b26\u53f7\u8868\uff0c\u5b58\u653e\u7684\u662f\u51fd\u6570\u548c\u53d8\u91cf\u7684\u7b26\u53f7\u4fe1\u606f\u3002

.rel.text\uff0c.rel.data\uff1a\u5b58\u653e\u9700\u8981\u91cd\u5b9a\u4f4d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002

.strtab\uff1a\u5b57\u7b26\u4e32\u8868\uff0c\u6bd4\u5982\u51fd\u6570\u540d\u3001\u53d8\u91cf\u540d\u3002

\u8fd9\u4e9bsection\u7684\u6570\u636e\u4fe1\u606f\u4fdd\u5b58\u5728Section Header Table\u91cc\u3002\u4e3a\u4ec0\u4e48\u53eb\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6\uff0c\u56e0\u4e3a\u7f16\u8bd1\u6210.o\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u4e86\u4e00\u4e9b\u5916\u90e8\u51fd\u6570\u6216\u8005\u53d8\u91cf\uff0c\u6b64\u65f6\u5e76\u4e0d\u77e5\u9053\u8fd9\u4e9b\u51fd\u6570\u6216\u53d8\u91cf\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\uff0c\u6240\u4ee5\u9700\u8981\u5728.o\u6587\u4ef6\u4e2d\u8bf4\u660e\u8fd9\u4e9b\u7b26\u53f7\u5c06\u6765\u662f\u9700\u8981\u91cd\u65b0\u5b9a\u4f4d\u7684\u3002

"},{"location":"others/ld/#_4","title":"\u9759\u6001\u5e93\u4e0e\u52a8\u6001\u5e93","text":"

\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u4e2a.o\u6587\u4ef6\u6253\u5305\u6210\u5e93\u6587\u4ef6\u7684\u5f62\u5f0f\uff0c\u4f9b\u5176\u4ed6\u7a0b\u5e8f\u8c03\u7528\u3002\u5f62\u6210\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u53eb\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u662fELF\u6587\u4ef6\u7684\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u3002\u5b83\u7684\u7ed3\u6784\u5982\u4e0b\u3002

\u8fd9\u4e2a\u683c\u5f0f\u4e0e\u53ef\u91cd\u5b9a\u4f4d\u6587\u4ef6\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u5728\u94fe\u63a5\u7684\u8fc7\u7a0b\u4e2d\uff0c\u90e8\u5206section\u5408\u5e76\u4e86\u3002\u6bd4\u5982\u90fd\u662f\u4e8c\u8fdb\u5236\u4ee3\u7801\uff0c\u90a3\u5c31\u4e00\u8d77\u5408\u5e76\u5230.text\u6bb5\u4e2d\u53bb\u3002\u5c0f\u7684section\u5408\u5e76\u6210\u4e3a\u4e86\u5927\u7684segment\u3002\u5728ELF\u5934\u91cc\u9762\uff0c\u6709\u4e2a\u53ebe_entry\u7684\u865a\u62df\u5730\u5740\uff0c\u662f\u7a0b\u5e8f\u7684\u5165\u53e3\u5730\u5740\u3002\u5f53\u9759\u6001\u5e93\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u94fe\u63a5\u5728\u4e00\u8d77\u65f6\uff0c\u4f1a\u628a\u9759\u6001\u5e93\u7684\u4fe1\u606f\u5408\u5e76\u8fdb\u53bb\uff0c\u5f62\u6210\u4e00\u4e2a\u66f4\u5927\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd9\u6837\u7684\u597d\u5904\u5c31\u662f\u8be5\u7a0b\u5e8f\u6267\u884c\u7684\u65f6\u5019\u4e0d\u4f9d\u8d56\u4e8e\u9759\u6001\u5e93\u6587\u4ef6\u3002\u574f\u5904\u5c31\u662f\uff0c\u6709\u5176\u4ed6\u9700\u8981\u94fe\u63a5\u7684\u6587\u4ef6\uff0c\u540c\u6837\u4f1a\u590d\u5236\u4e00\u4efd\u4fe1\u606f\u5230\u5185\u5b58\uff0c\u9020\u6210\u4e86\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002\u5f53\u9759\u6001\u5e93\u66f4\u65b0\u540e\uff0c\u5982\u679c\u4e0d\u91cd\u65b0\u8fdb\u884c\u7f16\u8bd1\uff0c\u7a0b\u5e8f\u5c31\u65e0\u6cd5\u8fd0\u884c\u4e86\u3002\u4e3a\u4e86\u89e3\u51b3\u7a7a\u95f4\u6d6a\u8d39\u4ee5\u53ca\u7a0b\u5e8f\u6269\u5c55\u6027\u5dee\u7684\u95ee\u9898\uff0c\u52a8\u6001\u5e93\u5c31\u51fa\u73b0\u4e86\u3002\u52a8\u6001\u5e93\u4e5f\u662fELF\u6587\u4ef6\u7684\u7b2c\u4e09\u79cd\u5f62\u5f0f\u2014\u2014\u5171\u4eab\u5bf9\u8c61\u6587\u4ef6\u3002

\u5f53\u52a8\u6001\u5e93\u88ab\u94fe\u63a5\u5230\u7a0b\u5e8f\u4e2d\u53bb\u65f6\uff0c\u53ea\u4fdd\u7559\u4e86\u5bf9\u52a8\u6001\u5e93\u7684\u5f15\u7528\uff0c\u5e76\u4e14\u4e00\u4e2a\u52a8\u6001\u5e93\u53ef\u4ee5\u88ab\u591a\u4e2a\u7a0b\u5e8f\u5f15\u7528\u3002\u800c\u4e3a\u4e86\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u51fd\u6570\u4f4d\u7f6e\uff0c\u57fa\u4e8e\u52a8\u6001\u5e93\u94fe\u63a5\u7684.so\u6587\u4ef6\u591a\u4e86\u4e00\u4e9b\u4fe1\u606f\u3002\u9996\u5148\u662f\u4e00\u4e2a\u53eb.interp\u7684\u6bb5\uff0c\u8fd9\u91cc\u9762\u662fld-linux.so\uff0c\u8fd9\u662f\u52a8\u6001\u94fe\u63a5\u5668\uff0c\u8fd0\u884c\u65f6\u7684\u94fe\u63a5\u52a8\u4f5c\u90fd\u662f\u5b83\u505a\u7684\u3002\u53e6\u5916\u8fd8\u591a\u4e86\u4e24\u4e2asection\uff0c\u4e00\u4e2a\u662f.plt\uff0c\u8fc7\u7a0b\u94fe\u63a5\u8868\uff0c\u4e00\u4e2a\u662f.got\uff0c\u5168\u5c40\u504f\u79fb\u91cf\u8868\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570func()\u9700\u8981\u91cd\u5b9a\u4f4d\uff0c\u7531\u4e8e\u4ee3\u7801\u6bb5\u662f\u4e0d\u53ef\u5199\u7684\uff0c\u6240\u4ee5\u5728\u94fe\u63a5\u8fc7\u7a0b\u4e2d\uff0c\u65e0\u6cd5\u5c06\u5176\u5b9e\u9645\u5730\u5740\u5199\u5165\u3002\u89e3\u51b3\u529e\u6cd5\u662f\u7528\u4e86\u4e00\u4e2a\u5c0f\u6280\u5de7\uff0c\u5728\u6c47\u7f16\u6307\u4ee4\u4e2d\uff0c\u5f53\u6267\u884c\u5230func()\u51fd\u6570\u65f6\uff0c\u544a\u8bc9\u7a0b\u5e8f\u4f60\u8981\u53bbPLT[x]\u4f4d\u7f6e\u5904\u53bb\u5bfb\u627efunc\u51fd\u6570\u7684\u4f4d\u7f6e\u3002\u800cPLT[X]\u53c8\u544a\u8bc9\u7a0b\u5e8f\u4f60\u8981\u53bbGOT[Y]\u7684\u5730\u65b9\u53bb\u627e\u51fd\u6570\u4f4d\u7f6e\u3002\u5f53\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\uff0cGOT[Y]\u91cc\u7684\u4f4d\u7f6e\u4fe1\u606f\u662f\u7a7a\u7684\uff0c\u4e8e\u662fGOT\u544a\u8bc9PLT\uff0c\u6211\u8fd9\u91cc\u6ca1\u6709\u5b9a\u4f4d\u4fe1\u606f\u3002PLT\u6536\u5230\u8fd9\u4e2a\u4fe1\u606f\u4e4b\u540e\uff0c\u8c03\u7528PLT[0]\uff0cPLT[0]\u8f6c\u800c\u8c03\u7528GOT[2]\uff0c\u8fd9\u91cc\u9762\u4fdd\u5b58\u7684\u662f\u94fe\u63a5\u5668\u7684\u5165\u53e3\u5730\u5740\u3002\u94fe\u63a5\u5668\u4f1a\u627e\u5230func()\u51fd\u6570\u7684\u5165\u53e3\u5730\u5740\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u5730\u5740\u5199\u5165\u5230GOT[Y]\u4e2d\uff0c\u90a3\u4e48\u4e0b\u6b21\u518d\u8c03\u7528func()\u51fd\u6570\u65f6\uff0c\u5c31\u4e0d\u9700\u8981\u4ee5\u4e0a\u8fd9\u4e48\u9ebb\u70e6\u7684\u6b65\u9aa4\u4e86\uff0c\u56e0\u4e3a\u51fd\u6570\u7684\u5730\u5740\u5df2\u7ecf\u5199\u5728\u4e86GOT\u8868\u4e2d\u3002

"},{"location":"others/ld/#_5","title":"\u7a0b\u5e8f\u7684\u88c5\u8f7d","text":"

\u901a\u8fc7execv()\u7cfb\u7edf\u8c03\u7528\uff0c\u6267\u884cload_elf_binary()\u51fd\u6570\uff0c\u5c06\u53ef\u6267\u884c\u6587\u4ef6\u88c5\u8f7d\u5230\u5185\u5b58\u4e2d\u3002

\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u5f85\u5b9a\u3002

"},{"location":"pl/","title":"\u7f16\u7a0b\u7684\u5fc3\u5f97\u4f53\u4f1a","text":""},{"location":"rtos/","title":"RTOS","text":""},{"location":"rtos/#table-of-contents","title":"Table of Contents","text":"
  • freertos
  • CMSIS\u6807\u51c6
  • rt-thread
"},{"location":"rtos/#_1","title":"\u53c2\u8003\u8d44\u6599","text":"
  • freertos doc
  • rt-thread\u6587\u6863\u4e2d\u5fc3
  • \u5d4c\u5165\u5f0f\u5b9e\u65f6\u64cd\u4f5c\u7cfb\u7edfRT-Thread\u8bbe\u8ba1\u4e0e\u5b9e\u73b0
"},{"location":"rtos/freertos/","title":"\u7b80\u660e\u5165\u95e8","text":"

\u5927\u591a\u6570\u7684\u64cd\u4f5c\u7cfb\u7edf\u90fd\u80fd\u540c\u65f6\u6267\u884c\u591a\u4e2a\u7a0b\u5e8f\uff0c\u8fd9\u88ab\u79f0\u4e3a\u591a\u4efb\u52a1\u5904\u7406\u3002\u5b9e\u9645\u4e0a\uff0c\u6bcf\u4e2a\u5904\u7406\u5668\u6838\u5fc3\u5728\u7ed9\u5b9a\u65f6\u95f4\u7247\u5185\u53ea\u80fd\u8fd0\u884c\u5355\u4e2a\u4efb\u52a1\u3002CPU\u901a\u8fc7\u5feb\u901f\u5207\u6362\u65f6\u95f4\u7247\u9020\u6210\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u6267\u884c\u7684\u5047\u8c61\u3002 \u5b9e\u65f6\u64cd\u4f5c\u7cfb\u7edf(RTOS)\u4e2d\u7684\u4efb\u52a1\u8c03\u5ea6\u5668\u65e8\u5728\u63d0\u4f9b\u53ef\u9884\u6d4b\u7684\u6267\u884c\u6a21\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u5bf9\u5d4c\u5165\u5f0f\u7cfb\u7edf\u610f\u4e49\u91cd\u5927\uff0c\u56e0\u4e3a\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f80\u5f80\u6709\u5b9e\u65f6\u6027\u7684\u8981\u6c42\uff0c\u6bd4\u5982\u6211\u8981\u6c42\u57283\u79d2\u5185\u53d1\u51fa\u8b66\u62a5\u58f0\u3002

\u4f7f\u7528STM32CubeMX\u521b\u5efa\u7684FreeRTOS\u5de5\u7a0b\u4e2d\uff0c\u6e90\u7801\u4e3b\u8981\u6d89\u53ca\u4e24\u4e2a\u76ee\u5f55\uff1a

  • Core
    • Inc\u76ee\u5f55\u4e0b\u7684FreeRTOSConfig.h\u662f\u914d\u7f6e\u6587\u4ef6
    • Src\u76ee\u5f55\u4e0b\u7684freertos.c\u662fSTM32CubeMX\u521b\u5efa\u7684\u9ed8\u8ba4\u4efb\u52a1
  • Middlewares/Thirt_Party/FreeRTOS/Source
    • \u6839\u76ee\u5f55\u4e0b\u662f\u6838\u5fc3\u6587\u4ef6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u662f\u901a\u7528\u7684
    • portable\u76ee\u5f55\u4e0b\u662f\u79fb\u690d\u65f6\u9700\u8981\u7684\u6587\u4ef6\uff1aport.c\u548cportmacro.h
      • \u76ee\u5f55\u540d\u4e3a\uff1a[compiler]/[architecture]
      • \u6bd4\u5982\uff1aRVDS/ARM-CM3\uff0c\u8fd9\u8868\u793acortexM3\u67b6\u6784\u5728RVDS\u5de5\u5177\u4e0a\u7684\u79fb\u690d\u6587\u4ef6
FreeRTOS/Srouce/ \u4f5c\u7528 tasks.c \u5fc5\u987b\uff0c\u4efb\u52a1\u64cd\u4f5c list.c \u5fc5\u987b\uff0c \u5217\u8868 queue.c \u57fa\u672c\u5fc5\u987b\uff0c\u63d0\u4f9b\u961f\u5217\u64cd\u4f5c\u3001\u4fe1\u53f7\u91cf timer.c \u53ef\u9009\uff0c\u63d0\u4f9b\u5b9a\u65f6\u5668 event_groups.c \u53ef\u9009\uff0c\u63d0\u4f9bevent group\u529f\u80fd croutine.c \u53ef\u9009\uff0c\u5df2\u8fc7\u65f6

\u5934\u6587\u4ef6\u76ee\u5f55\u9700\u8981\u5305\u542b\uff1a

  • FreeRTOS\u81ea\u8eab\u5934\u6587\u4ef6\uff1aMiddlewares/Third_Party/FreeRTOS/Source/include

  • \u914d\u7f6e\u6587\u4ef6\uff1aCore/Inc/FreeRTOSConfig.h

\u6570\u636e\u7c7b\u578b\uff1a

\u5728portmacro.h\u5934\u6587\u4ef6\u91cc\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a\u6570\u636e\u7c7b\u578b\uff1a

  • TickType_t\uff1a
    • FreeRTOS\u914d\u7f6e\u4e86\u4e00\u4e2a\u5468\u671f\u6027\u7684\u65f6\u949f\u4e2d\u65ad\uff1aTick Interrupt
    • \u6bcf\u53d1\u751f\u4e00\u6b21\u4e2d\u65ad\uff0c\u4e2d\u65ad\u6b21\u6570\u7d2f\u52a0\uff0c\u8fd9\u88ab\u79f0\u4e3atick count
    • tick count\u8fd9\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u5c31\u662fTickType_t
    • TickType_t\u53ef\u4ee5\u662f16\u4f4d\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f32\u4f4d\u7684
    • FreeRTOSConfig.h\u4e2d\u5b9a\u4e49configUSE_16_BIT_TICKS\u65f6\uff0cTickType_t\u5c31\u662fuint16_t
    • \u5426\u5219TickType_t\u5c31\u662fuint32_t
    • \u5bf9\u4e8e32\u4f4d\u67b6\u6784\uff0c\u5efa\u8bae\u628aTickType_t\u914d\u7f6e\u4e3auint32_t
  • BaseType_t\uff1a
    • \u8fd9\u662f\u8be5\u67b6\u6784\u6700\u9ad8\u6548\u7684\u6570\u636e\u7c7b\u578b
    • 32\u4f4d\u67b6\u6784\u4e2d\uff0c\u5b83\u5c31\u662fuint32_t
    • 16\u4f4d\u67b6\u6784\u4e2d\uff0c\u5b83\u5c31\u662fuint16_t
    • 8\u4f4d\u67b6\u6784\u4e2d\uff0c\u5b83\u5c31\u662fuint8_t
    • BaseType_t\u901a\u5e38\u7528\u4f5c\u7b80\u5355\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u8fd8\u6709\u903b\u8f91\u503c\uff0c\u6bd4\u5982pdTRUE/pdFALSE

\u547d\u540d\u89c4\u8303\uff1a

\u53d8\u91cf\u540d\u524d\u7f00 \u542b\u4e49 ul uint32_t us uint16_t uc uint8_t x \u975estdint\u7c7b\u578b\uff0c\u6bd4\u5982BaseType_t\u548cTickType_t\uff0c\u6216\u8005size_t ux UBaseType_t e enum p \u6307\u9488 c char \u51fd\u6570\u540d\u524d\u7f00 \u542b\u4e49 prv \u79c1\u6709\u51fd\u6570(file scope) v \u8fd4\u56de\u503cvoid task \u5b9a\u4e49\u5728task.c \u5b8f\u524d\u7f00 \u542b\u4e49 config \u5b9a\u4e49\u5728FreeRTOSConfig.h"},{"location":"rtos/freertos/#_2","title":"\u5185\u5b58\u7ba1\u7406","text":"

\u6bcf\u6b21\u521b\u5efa\u4efb\u52a1\u3001\u961f\u5217\u3001\u4e92\u65a5\u9501\u3001\u8f6f\u4ef6\u5b9a\u65f6\u5668\u3001\u4fe1\u53f7\u91cf\u6216\u4e8b\u4ef6\u7ec4\u65f6\uff0c\u5185\u6838\u90fd\u9700\u8981RAM\uff0cRAM\u53ef\u4ee5\u7531RTOS\u5806\u52a8\u6001\u5206\u914d\uff0c\u4e5f\u53ef\u4ee5\u7531\u7f16\u5199\u8005\u624b\u52a8\u63d0\u4f9b\u3002

\u5c3d\u7ba1C\u5e93\u63d0\u4f9b\u4e86malloc()\u548cfree()\u51fd\u6570\uff0c\u4f46\u662f\uff1a 1. \u5b83\u4eec\u5e76\u4e0d\u603b\u662f\u9002\u7528\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf 2. \u5b83\u4eec\u8017\u8d39\u65f6\u95f4\u6bd4\u8f83\u957f 3. \u5b83\u4eec\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684 4. \u8fd0\u884c\u7ed3\u679c\u4e0d\u786e\u5b9a

\u56e0\u6b64freertos\u5b9e\u73b0\u4e86\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u63a5\u53e3\u51fd\u6570\u3002

freertos\u4e2d\u6709\u4e94\u79cd\u5806\u5185\u5b58\uff0c\u6587\u4ef6\u5b9a\u4e49\u5728Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang\u4e0b\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fheap_4.c\uff0c\u5b83\u53ef\u4ee5\u5c06\u76f8\u90bb\u7684\u7a7a\u95f4\u5185\u5b58\u5757\u5408\u5e76\uff0c\u89e3\u51b3\u4e86\u5185\u5b58\u788e\u7247\u95ee\u9898\u3002\u800cheap_5.c\u5141\u8bb8\u591a\u4e2a\u4e0d\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\u3002\u9700\u8981\u7528\u5230\u4ee5\u4e0b\u7ed3\u6784\u4f53\u8fdb\u884c\u4e0d\u540c\u5185\u5b58\u5757\u7684\u521d\u59cb\u5316\uff1a

typedef struct HeapRegion\n{\n    uint8_t *pucStartAddress;   //\u8d77\u59cb\u5730\u5740\n    size_t xSizeInBytes;       //\u5927\u5c0f\n}HeapRegion_t;\n

\u5f53\u6307\u5b9a\u591a\u5757\u5185\u5b58\u65f6\uff0c\u9700\u8981\u7528\u5230HeapRegion_t\u7c7b\u578b\u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u90fd\u662f\u4e00\u4e2aHeapRegion_t\u5143\u7d20\u3002\u8fd9\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4f4e\u5730\u5740\u5728\u524d\uff0c\u9ad8\u5730\u5740\u5728\u540e\u3002

HeapRegion_t xHeapRegions[] = \n{\n    {(uint8_t *)0x80000000UL, 0x10000}, //\u8d77\u59cb\u5730\u5740\u548c\u5927\u5c0f\n    {(uint8_t *)0x90000000UL, 0x10000}, \n    {NULL, 0}  //\u8868\u793a\u6570\u7ec4\u7ed3\u675f\n};\n

\u5b9a\u4e49\u4e86\u5185\u5b58\u5757\u6570\u7ec4\u8fd8\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff1a

void vPortDefineHeapRegions(const HeapRegion_t *const pxHeapRegions);\n

Heap\u7684\u5206\u914d\u548c\u91ca\u653e\uff1a

void *pvPortMalloc(size_t xWantedSize);\nvoid vPortFree(void *pv);\n

\u83b7\u53d6\u5f53\u524d\u7a7a\u95f2\u5185\u5b58\u7a7a\u95f4\uff1a

size_t xPortGetFreeHeapSize(void);\n

\u83b7\u53d6\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7a7a\u95f2\u5185\u5b58\u7684\u6700\u5c0f\u503c\uff1a

size_t xPortGetMinimumEverFreeHeapSize(void);\n
"},{"location":"rtos/freertos/#_3","title":"\u4efb\u52a1\u7ba1\u7406","text":"

\u4e00\u4e2a\u4efb\u52a1\u6700\u57fa\u672c\u7684\u5143\u7d20\u6709\uff1a

  • \u4efb\u52a1\u72b6\u6001\uff1a\u6bd4\u5982\u963b\u585e\uff0c\u5c31\u7eea\uff0c\u6302\u8d77\u7b49
  • \u4f18\u5148\u7ea7\uff1a\u6bcf\u4e2a\u4efb\u52a1\u90fd\u5206\u914d\u4e86\u4ece0\u5230configMAX_PRIORITIES-1\u7684\u4f18\u5148\u7ea7
  • \u6808\uff1a\u7528\u6765\u4fdd\u5b58\u4efb\u52a1\u7684\u5c40\u90e8\u6570\u636e
  • \u4e8b\u4ef6\uff1a\u8868\u793a\u4efb\u52a1\u505a\u4e86\u4ec0\u4e48\u4e8b\u60c5

\u5bf9\u4e8e\u5355\u6838\u5904\u7406\u5668\u6765\u8bf4\uff0c\u4efb\u4f55\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u4efb\u52a1\u5f97\u5230\u8fd0\u884c\uff0c\u5f53\u8c03\u5ea6\u5668\u51b3\u5b9a\u5207\u6362\u4efb\u52a1\u65f6\uff0c\u9700\u8981\u5c06\u4e0a\u4e00\u4e2a\u4efb\u52a1\u7684\u4e0a\u4e0b\u6587\uff08\u5bc4\u5b58\u5668\uff0c\u6808\uff09\u4fdd\u5b58\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u4efb\u52a1\u51fa\u73b0\u4e86\u6b7b\u5faa\u73af\u64cd\u4f5c\uff0c\u90a3\u4e48\u6bd4\u8fd9\u4e2a\u4efb\u52a1\u4f18\u5148\u7ea7\u4f4e\u7684\u4efb\u52a1\u90fd\u5c06\u65e0\u6cd5\u6267\u884c\u3002\u6240\u4ee5\u5728\u8bbe\u8ba1\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u4fdd\u8bc1\u4efb\u52a1\u5728\u4e0d\u6d3b\u8dc3\u72b6\u6001\u65f6\u8fdb\u5165\u963b\u585e\u72b6\u6001\u4ee5\u8ba9\u51faCPU\u4f7f\u7528\u6743\uff0c\u4fdd\u8bc1\u5176\u4ed6\u4f4e\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u53ef\u4ee5\u6267\u884c\u3002

"},{"location":"rtos/freertos/#_4","title":"\u521b\u5efa\u4efb\u52a1","text":"

\u5728FreeRTOS\u4e2d\uff0c\u4efb\u52a1\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

void vATaskFunction(void *pvParameters)\n{\n    for(;;)\n    {\n        //RTOS\u63a8\u8350\u91c7\u7528\u4e8b\u4ef6\u9a71\u52a8\u578b\u4efb\u52a1\n        if(WaitForEvent(EventObject, Timeout) == pdPASS)\n        {\n            //\u5904\u7406\u4e8b\u4ef6\n        }\n        else\n        {\n\n        }\n    }\n\n    vTaskDelete(NULL);  //\u6267\u884c\u5b8c\u6bd5\u5220\u9664\u81ea\u5df1\n}\n
\u52a8\u6001\u521b\u5efa\u4efb\u52a1\u4f7f\u7528\u7684\u51fd\u6570\u5982\u4e0b\uff1a

BaseType_t xTaskCreate(TaskFunction_t pxTaskCode,\n                        const char *const pcName, \n                        const configSTACK_DEPTH_TYPE usStackDepth,\n                        void *const pvParameters,\n                        UBaseType_t uxPriority,\n                        TaskHandle_t *const pxCreatedTask);\n

\u53c2\u6570\u8bf4\u660e\uff1a

\u53c2\u6570 \u63cf\u8ff0 pvTaskCode \u4efb\u52a1\u51fd\u6570 pcName \u4efb\u52a1\u540d\u79f0 usStackDepth \u4efb\u52a1\u6808\u5927\u5c0f\uff0c\u5355\u4f4dword pvParameters \u4f20\u9012\u7ed9\u4efb\u52a1\u51fd\u6570\u7684\u53c2\u6570 uxPriority \u4efb\u52a1\u4f18\u5148\u7ea7 pxCreatedTask \u4efb\u52a1\u53e5\u67c4\uff0c\u7528\u6765\u64cd\u4f5c\u4efb\u52a1\u51fd\u6570 \u8fd4\u56de\u503c \u6210\u529f\u8fd4\u56depdPass\uff0c\u5931\u8d25\u901a\u5e38\u662f\u56e0\u4e3a\u5185\u5b58\u4e0d\u8db3

\u9759\u6001\u521b\u5efa\u4efb\u52a1\u4f7f\u7528\u7684\u51fd\u6570\u5982\u4e0b\uff1a

TaskHandle_t xTaskCreateStatic(TaskFunction_t pxTaskCode,\n                                const char *const pcName,\n                                const uint32_t ulStackDepth,\n                                void *const pvParameters,\n                                UBaseType_t uxPriority,\n                                StackType_t *const puxStackBuffer,\n                                StaticTask_t *const pxTaskBuffer);\n

\u76f8\u5bf9\u4e8e\u52a8\u6001\u5206\u914d\uff0c\u6709\u4e24\u4e2a\u53c2\u6570\u4e0d\u540c\uff1a

\u53c2\u6570 \u63cf\u8ff0 puxStackBuffer \u6808\u6570\u7ec4\uff0c\u7d22\u5f15\u5fc5\u987b\u4e0d\u5c0f\u4e8eulStackDepth pxTaskBuffer \u6307\u5411StaticTask_t\u7684\u6307\u9488\uff0c\u7528\u6765\u4fdd\u5b58\u4efb\u52a1\u7ed3\u6784\u4f53

\u6bcf\u4e2a\u4efb\u52a1\u90fd\u9700\u8981RAM\u6765\u4fdd\u5b58\u4efb\u52a1\u72b6\u6001\uff0c\u5e76\u7531\u4efb\u52a1\u7528\u4f5c\u5176\u5806\u6808\u3002\u5982\u679c\u4f7f\u7528\u52a8\u6001\u521b\u5efa\uff0c\u5219\u4f1a\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4eceFreeRTOS\u5806\u4e2d\u81ea\u52a8\u5206\u914dRAM\u3002\u5982\u679c\u662f\u9759\u6001\u521b\u5efa\uff0cRAM\u7531\u7f16\u5199\u8005\u624b\u52a8\u63d0\u4f9b\uff0c\u4f46\u5728\u7f16\u8bd1\u65f6\u5c31\u5df2\u786e\u5b9a\u3002

"},{"location":"rtos/freertos/#tick","title":"Tick","text":"

\u4efb\u52a1\u8fdb\u5165\u7761\u7720\u540e\u9700\u8981\u6307\u5b9a\u5524\u9192\u7684\u65f6\u95f4\uff0cFreeRTOS\u901a\u8fc7Tick\u53d8\u91cf\u6d4b\u91cf\u65f6\u95f4\u3002\u5b9a\u65f6\u5668\u4e2d\u65ad\u4ee5\u4e25\u683c\u7684\u65f6\u95f4\u7cbe\u5ea6\u589e\u52a0Tick count\u3002\u6bcf\u6b21Tick\u589e\u52a0\u65f6\uff0c\u5185\u6838\u5fc5\u987b\u68c0\u67e5\u73b0\u5728\u662f\u5426\u9700\u8981\u89e3\u9664\u963b\u585e\u6216\u8005\u5524\u9192\u4efb\u52a1\uff0c\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u5148\u88ab\u5524\u9192\u3002\u4efb\u52a1\u901a\u8fc7\u8c03\u7528vTaskDelay()\u51fd\u6570\u6765\u4e3b\u52a8\u7b49\u5f85\u4e00\u5b9a\u7684\u65f6\u95f4\u3002\u5b8fpdMS_TO_TICKS()\u5c06\u6beb\u79d2\u8f6c\u6362\u4e3aTick count\u3002

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5185\u6838\u672c\u8eab\u7684\u4efb\u52a1\u5207\u6362\u9700\u8981\u65f6\u95f4\uff0c\u5f53\u6307\u5b9aN\u4e2aTick\u7684\u5ef6\u8fdf\u540e\uff0c\u5b9e\u9645\u5ef6\u8fdf\u65f6\u95f4\u5c06\u5728(N-1)~(N)\u4e2aTick\u4e4b\u95f4\u3002

"},{"location":"rtos/freertos/#_5","title":"\u4efb\u52a1\u72b6\u6001","text":"

\u4efb\u52a1\u53ef\u4ee5\u6709\u4ee5\u4e0b\u72b6\u6001\uff1a

  • \u8fd0\u884c\uff1a\u6700\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u88ab\u6267\u884c

  • \u5c31\u7eea\uff1a\u80fd\u591f\u6267\u884c\uff0c\u7b49\u5f85\u8c03\u5ea6\u5668\u8c03\u5ea6

  • \u963b\u585e\uff1a\u6b63\u5728\u7b49\u5f85\u65f6\u95f4\u6216\u8005\u5916\u90e8\u4e8b\u4ef6

  • \u6302\u8d77\uff1a\u653e\u5f03CPU\u4f7f\u7528\u6743\uff0c\u4e0d\u53c2\u4e0e\u8c03\u5ea6\u3002

"},{"location":"rtos/freertos/#_6","title":"\u8c03\u5ea6\u7b97\u6cd5","text":"

\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a\u62a2\u5360\u548c\u8f6e\u8f6c\u3002\u901a\u8fc7\u914d\u7f6econfigUSE_PREEMPTION\u4f7f\u80fd\u62a2\u5360\uff0c\u5373\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u53ef\u4ee5\u62a2\u5360\u4f4e\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u914d\u7f6econfigUSE_TIME_SLICING\u4f7f\u80fd\u8f6e\u8f6c\uff0c\u5373\u76f8\u540c\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u8f6e\u6d41\u6267\u884c\u3002

"},{"location":"rtos/freertos/#_7","title":"\u7a7a\u95f2\u4efb\u52a1\u548c\u94a9\u5b50\u51fd\u6570","text":"

\u5728\u6ca1\u6709\u5176\u4ed6\u4efb\u52a1\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u6267\u884c\u7a7a\u95f2\u4efb\u52a1\u3002vTaskStartScheduler()\u51fd\u6570\u5185\u90e8\u4f1a\u521b\u5efa\u7a7a\u95f2\u4efb\u52a1\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u4e3a0\uff0c\u6c38\u8fdc\u4f4e\u4e8e\u7528\u6237\u4efb\u52a1\uff0c\u4e14\u8981\u4e48\u5904\u4e8e\u5c31\u7eea\u6001\uff0c\u8981\u4e48\u5904\u4e8e\u8fd0\u884c\u6001\u3002

\u6211\u4eec\u53ef\u4ee5\u5728\u7a7a\u95f2\u4efb\u52a1\u5185\u6dfb\u52a0\u4e00\u4e2a\u94a9\u5b50\u51fd\u6570\uff0c\u8fd9\u6837\u7a7a\u95f2\u4efb\u52a1\u6bcf\u6267\u884c\u4e00\u6b21\uff0c\u90fd\u4f1a\u8c03\u7528\u4e00\u6b21\u94a9\u5b50\u51fd\u6570\u3002\u94a9\u5b50\u51fd\u6570\u7684\u4f5c\u7528\u662f\uff1a

  • \u6267\u884c\u4e00\u4e9b\u540e\u53f0\u4efb\u52a1
  • \u6d4b\u91cf\u7cfb\u7edf\u7a7a\u95f2\u65f6\u95f4
  • \u4f7f\u7cfb\u7edf\u8fdb\u5165\u7701\u7535\u6a21\u5f0f

\u8981\u6ce8\u610f\u7684\u662f\uff0c\u94a9\u5b50\u51fd\u6570\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55\u4f7f\u5176\u5bfc\u81f4\u963b\u585e\u7684\u51fd\u6570\u3002\u7a7a\u95f2\u94a9\u5b50\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a

void vApplicationIdleHook(void);\n
"},{"location":"rtos/freertos/#_8","title":"\u4efb\u52a1\u540c\u6b65\u4e0e\u901a\u4fe1","text":"

\u8fd9\u91cc\u9762\u7684\u5185\u5bb9\u4ee5\u7406\u89e3\u539f\u7406\u4e3a\u4e3b\uff0c\u6ca1\u6709\u5fc5\u8981\u53bb\u8bb0\u51fd\u6570\u63a5\u53e3\uff0c\u7528\u5230\u4ec0\u4e48\u5c31\u67e5\u4ec0\u4e48\u3002

"},{"location":"rtos/freertos/#_9","title":"\u6d88\u606f\u961f\u5217","text":"

\u6d88\u606f\u961f\u5217\u7b80\u79f0\u4e3a\u961f\u5217\uff0c\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u652f\u6301\u4efb\u52a1\u6216\u8005\u4e2d\u65ad\u7684\u8bfb\u5199\u3002\u5f53\u961f\u5217\u4e2d\u7684\u6d88\u606f\u4e3a\u7a7a\u65f6\uff0c\u8bfb\u53d6\u6d88\u606f\u7684\u4efb\u52a1\u5c06\u88ab\u963b\u585e\uff1b\u5f53\u961f\u5217\u5df2\u6ee1\uff0c\u53d1\u9001\u6d88\u606f\u7684\u4efb\u52a1\u5c06\u88ab\u963b\u585e\u3002\u53ef\u4ee5\u5c06\u4e00\u6761\u6216\u8005\u591a\u6761\u6d88\u606f\u653e\u5165\u961f\u5217\u4e2d\uff0c\u591a\u4e2a\u4efb\u52a1\u4e5f\u53ef\u4ee5\u4ece\u540c\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u8bfb\u5199\u6d88\u606f\u3002\u5982\u679c\u662f\u7d27\u6025\u6d88\u606f\uff0c\u53ef\u4ee5\u53d1\u9001\u81f3\u961f\u5217\u5934\u4ece\u800c\u63d0\u524d\u88ab\u63a5\u6536\u3002

\u4e3a\u4e86\u5b9e\u73b0\u5148\u8fdb\u5148\u51fa\u7684\u673a\u5236\uff0c\u961f\u5217\u4f7f\u7528\u4e86\u963b\u585e\u7684\u673a\u5236\u3002\u5f53\u4efb\u52a1A\u5bf9\u67d0\u4e2a\u961f\u5217\u8fdb\u884c\u8bfb\u64cd\u4f5c\u65f6\uff0c\u5982\u679c\u961f\u5217\u6b64\u65f6\u4e3a\u7a7a\uff0c\u90a3\u4e48\u5b83\u53ef\u4ee5\u9009\u62e9\uff1a

  1. \u76f4\u63a5\u653e\u5f03\u8bfb\u64cd\u4f5c\uff0c\u53bb\u5e72\u522b\u7684\u4e8b\u60c5\u3002
  2. \u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u3002\u5982\u679c\u5728\u7b49\u5f85\u671f\u95f4\u6d88\u606f\u6765\u4e86\uff0c\u5c31\u53d8\u4e3a\u5c31\u7eea\u72b6\u6001\uff1b\u5982\u679c\u7b49\u5f85\u65f6\u95f4\u8fc7\u4e86\u6d88\u606f\u90fd\u6ca1\u6765\uff0c\u90a3\u4e48\u5c31\u8fd4\u56de\u4e00\u4e2a\u672a\u6536\u5230\u6d88\u606f\u7684\u9519\u8bef\u7801\uff0c\u7136\u540e\u6267\u884c\u5176\u4ed6\u7a0b\u5e8f\u3002
  3. \u6b7b\u7b49\u76f4\u5230\u6d88\u606f\u5230\u6765\u4e3a\u6b62\u3002

\u5199\u64cd\u4f5c\u7684\u963b\u585e\u673a\u5236\u7c7b\u4f3c\u3002

\u6ce8\u610f\uff1a\u7531\u4e8e\u4e2d\u65ad\u51fd\u6570\u4e5f\u53ef\u4ee5\u5bf9\u961f\u5217\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u4f46\u662f\u4e2d\u65ad\u51fd\u6570\u4e0d\u5141\u8bb8\u963b\u585e\uff0c\u56e0\u6b64\u5728\u4e2d\u65ad\u5904\u7406\u6d41\u7a0b\u4e2d\u5fc5\u987b\u4f7f\u7528\u7279\u6b8a\u7684\u8bfb\u5199\u51fd\u6570\u63a5\u53e3\u3002

"},{"location":"rtos/freertos/#_10","title":"\u4fe1\u53f7\u91cf","text":"

\u4fe1\u53f7\u91cf\u5206\u4e3a\u4e8c\u503c\u4fe1\u53f7\u91cf\u548c\u8ba1\u6570\u4fe1\u53f7\u91cf\uff0c\u5176\u539f\u7406\u548cLinux\u5185\u6838\u76f8\u4f3c\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002

"},{"location":"rtos/freertos/#_11","title":"\u4e92\u65a5\u91cf","text":"

\u4e92\u65a5\u91cf\u662f\u7279\u6b8a\u7684\u4e8c\u503c\u4fe1\u53f7\u91cf\uff0c\u6709\u7740\u9012\u5f52\u8bbf\u95ee\u548c\u9632\u6b62\u4f18\u5148\u7ea7\u53cd\u8f6c\u7684\u7279\u6027\uff0c\u4e3b\u8981\u7528\u4e8e\u4e34\u754c\u8d44\u6e90\u7684\u4fdd\u62a4\u3002\u4e92\u65a5\u91cf\u5177\u6709\u7279\u6b8a\u7684\u4f18\u5148\u7ea7\u7ee7\u627f\u7684\u673a\u5236\uff0c\u8981\u7406\u89e3\u8fd9\u4e2a\u673a\u5236\uff0c\u6211\u4eec\u9996\u5148\u8981\u89e3\u91ca\u4e0b\u4ec0\u4e48\u662f\u4f18\u5148\u7ea7\u53cd\u8f6c\u3002

\u5728\u4e00\u4e2a\u7b80\u5316\u8fc7\u7684\u573a\u666f\u4e2d\uff0c\u6709\u4e09\u4e2a\u4efb\u52a1H\u3001M\u3001L\uff0c\u5b83\u4eec\u7684\u4f18\u5148\u7ea7\u4f9d\u6b21\u4e3aH>M>L\u3002H\u4e0eL\u5171\u4eab\u67d0\u4e2a\u8d44\u6e90\uff0cM\u4e0d\u5171\u4eab\u3002\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u6700\u9ad8\u4f18\u5148\u7ea7\u7684H\u5e94\u8be5\u80fd\u591f\u4f18\u5148\u6267\u884c\uff0c\u5e76\u4e14\u6709\u6743\u9650\u6253\u65adM\u6216\u8005L\u7684\u6267\u884c\u72b6\u6001\u4ece\u800c\u62a2\u5360CPU\u7684\u63a7\u5236\u6743\u3002\u7136\u800c\u5b58\u5728\u4e00\u79cd\u60c5\u51b5,\u4f1a\u5bfc\u81f4H\u4e00\u76f4\u88ab\u963b\u585e\uff0c\u7b49\u5f85M\u548cL\u7684\u5b8c\u6210\u8fd0\u884c\uff1a

\u5728\u67d0\u4e00\u65f6\u523b\uff0cL\u5df2\u7ecf\u83b7\u53d6\u4e86\u5171\u4eab\u8d44\u6e90\u7684\u4f7f\u7528\u6743\uff0c\u6b63\u5728\u6267\u884c\u81ea\u5df1\u7684\u4efb\u52a1\u3002\u5982\u679c\u6b64\u65f6H\u88ab\u5524\u9192\uff0c\u7531\u4e8eH\u7684\u4f18\u5148\u7ea7\u5927\u4e8eL\uff0c\u5b83\u5c1d\u8bd5\u83b7\u53d6CPU\u7684\u63a7\u5236\u6743\u3002\u4f46\u662f\u7531\u4e8e\u5171\u4eab\u8d44\u6e90\u88abL\u5360\u7528\uff0cH\u4e0d\u5f97\u4e0d\u7b49\u5f85L\u91ca\u653e\u8d44\u6e90\u800c\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u6709\u8da3\u7684\u662f\uff0c\u5982\u679cM\u88ab\u5524\u9192\uff0c\u7531\u4e8eM\u4e0eL\u5e76\u4e0d\u5171\u4eab\u8d44\u6e90\u4e14\u4f18\u5148\u7ea7\u5927\u4e8eL\uff0c\u5b83\u5c06\u7acb\u523b\u4e2d\u65adL\u7684\u6267\u884c\uff0c\u83b7\u53d6CPU\u7684\u63a7\u5236\u6743\u3002\u53ea\u6709\u5f53M\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u624d\u4f1a\u5c06CPU\u63a7\u5236\u6743\u5f52\u8fd8\u7ed9L\uff0c\u4f7f\u5176\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5269\u4f59\u7684\u4ee3\u7801\u3002\u4e00\u65e6L\u6267\u884c\u5b8c\u6bd5\u540e\u91ca\u653e\u4e86\u5171\u4eab\u8d44\u6e90\uff0cH\u4efb\u52a1\u624d\u80fd\u4ece\u963b\u585e\u72b6\u6001\u4e2d\u88ab\u5524\u9192\u3002\u6211\u4eec\u53d1\u73b0\uff0c\u6700\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1H\u53cd\u800c\u9700\u8981\u7b49\u5f85\u4e24\u4e2a\u4f4e\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\u5148\u6267\u884c\u5b8c\u6bd5\uff0c\u5176\u963b\u585e\u65f6\u95f4\u7b49\u4e8eM\u548cL\u7684\u6267\u884c\u65f6\u95f4\u4e4b\u548c\u3002\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u201c\u4f18\u5148\u7ea7\u53cd\u8f6c\u201d\uff0c\u5728\u7cfb\u7edf\u4e2d\u53ef\u80fd\u5bfc\u81f4\u4e25\u91cd\u540e\u679c\uff0c\u751a\u81f3\u5d29\u6e83\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u8f86\u6c7d\u8f66\u7684\u5239\u8f66\u7cfb\u7edf\u662f\u6700\u9ad8\u4f18\u5148\u7ea7\u7684\u4efb\u52a1\uff0c\u4f46\u9700\u8981\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u8fd9\u5c06\u5bfc\u81f4\u707e\u96be\u6027\u7684\u540e\u679c\u3002\u56e0\u6b64\u4e3a\u4e86\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u8fd0\u884c\uff0c\u5fc5\u987b\u89e3\u51b3\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898\u3002

\u4f46\u662f\u5047\u5982\u6211\u4eec\u5f15\u8fdb\u4e86\u4f18\u5148\u7ea7\u7ee7\u627f\u673a\u5236\u3002\u540c\u6837\u662fL\u83b7\u53d6\u5171\u4eab\u8d44\u6e90\uff0cH\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u7cfb\u7edf\u4e34\u65f6\u5c06L\u7684\u4f18\u5148\u7ea7\u63d0\u5347\u5230H\uff0c\u90a3\u4e48M\u5c31\u65e0\u6cd5\u6253\u65adL\u7684\u6267\u884c\u3002\u6240\u4ee5L\u6267\u884c\u5b8c\u5b83\u7684\u4efb\u52a1\u5c31\u4f1a\u7acb\u523b\u91ca\u653e\u8d44\u6e90\uff0c\u7136\u540e\u7531H\u6765\u62a2\u5360CPU\u7684\u4f7f\u7528\u6743\u3002\u5176\u963b\u585e\u65f6\u95f4\u4ec5\u4ec5\u662fL\u7684\u6267\u884c\u65f6\u95f4\uff0c\u8fd9\u5c06\u5927\u5927\u964d\u4f4e\u4f18\u5148\u7ea7\u53cd\u8f6c\u7684\u5371\u5bb3\u3002

"},{"location":"rtos/freertos/#_12","title":"\u4e8b\u4ef6\u7ec4","text":"

\u4e8b\u4ef6\u7ec4\u4e3b\u8981\u7528\u6765\u5b9e\u73b0\u591a\u4efb\u52a1\u95f4\u7684\u540c\u6b65\uff0c\u53ea\u80fd\u7528\u6765\u4e8b\u4ef6\u7684\u901a\u4fe1\uff0c\u4e0d\u80fd\u7528\u6765\u53d1\u9001\u6570\u636e\u3002\u53ef\u4ee5\u662f\u4e00\u5bf9\u591a\uff0c\u4e5f\u53ef\u4ee5\u662f\u591a\u5bf9\u4e00\u3002

\u4e8b\u4ef6\u7ec4\u7528\u4e00\u4e2aEventBits_t\u7c7b\u578b\u7684\u53d8\u91cf\u6765\u8868\u793a\uff0c\u5176\u4e2d\u9ad88\u4f4d\u7559\u7ed9\u5185\u6838\uff0c\u7528\u5176\u4ed6\u7684\u4f4d\u6765\u8868\u793a\u4e8b\u4ef6\u3002\u6bcf\u4e00\u4f4d\u90fd\u4ee3\u8868\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u542b\u4e49\u7531\u7f16\u5199\u8005\u51b3\u5b9a\uff0c\u6bd4\u5982\u7528bit0\u8868\u793a\u706f\u6ce1\u4eae\u706d\uff0cbit1\u8868\u793a\u6309\u952e\u662f\u5426\u6309\u4e0b\u3002\u8fd9\u4e9b\u4f4d\uff0c\u503c\u4e3a1\u8868\u793a\u4e8b\u4ef6\u53d1\u751f\uff0c\u503c\u4e3a0\u8868\u793a\u4e8b\u4ef6\u672a\u53d1\u751f\u3002\u4efb\u52a1\u901a\u8fc7\u201c\u903b\u8f91\u4e0e\u201d\u3001\u201c\u903b\u8f91\u6216\u201d\u4e0e\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u4e8b\u4ef6\u5efa\u7acb\u5173\u8054\u3002\u903b\u8f91\u4e0e\u8868\u793a\u4efb\u52a1\u611f\u5174\u8da3\u7684\u591a\u4e2a\u4e8b\u4ef6\u540c\u65f6\u53d1\u751f\u624d\u88ab\u5524\u9192\uff0c\u903b\u8f91\u6216\u8868\u793a\u4efb\u4e00\u4e8b\u4ef6\u53d1\u751f\u5373\u53ef\u88ab\u5524\u9192\u3002

\u4e8b\u4ef6\u7ec4\u7684\u64cd\u4f5c\u6d41\u7a0b\u5982\u4e0b\uff1a

  • \u521b\u5efa\u4e00\u4e2a\u4e8b\u4ef6\u7ec4
  • \u4efb\u52a1A\u3001B\u63a7\u5236\u4e8b\u4ef6\u4e2d\u7684\u67d0\u4e9b\u4f4d
  • \u4efb\u52a1C\u3001D\u7b49\u5f85\u4e8b\u4ef6\u4e2d\u7684\u4f4d\uff0c\u5e76\u4e14\u51b3\u5b9a\u53d1\u751f\u4e8b\u4ef6\u540e\u7684\u64cd\u4f5c

\u8fd9\u91cc\u5bf9\u4e24\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u51fd\u6570\u8bf4\u660e\u4e00\u4e0b\uff1a

\u7b49\u5f85\u4e8b\u4ef6\u51fd\u6570\uff1a

EventBits_t xEventGroupWaitBits(EventGroupsHandle_t xEventGroup,\n                                const EvetnBits_t uxBitsToWaitFor,\n                                const BaseType_t xClearOnExit,\n                                const BaseType_t xWaitForAllBits,\n                                TickType_t xTickToWait);\n
\u53c2\u6570 \u8bf4\u660e xEventGroup \u7b49\u5f85\u7684\u4e8b\u4ef6\u7ec4 uxBitsToWaitFor \u7b49\u5f85\u7684\u4f4d xClearOnExit pdTRUE:\u6e05\u9664\u4f4d xWaitForAllBits pdTRUE\uff1a\u7b49\u5f85\u7684\u4f4d\u5168\u90e8\u4e3a1\uff1bpdFALSE\uff1a\u67d0\u4e00\u4f4d\u4e3a1 xTicksToWait \u963b\u585e\u65f6\u95f4

\u4e3e\u4f8b\u8bf4\u660e\uff1a

\u4e8b\u4ef6\u7ec4\u7684\u503c uxBitsToWaitFor xWaitForAllBits \u8bf4\u660e 0100 0101 pdTrue \u671f\u671bbit0,bit2\u90fd\u4e3a1\uff0c\u4e0d\u6ee1\u8db3\u8fdb\u5165\u963b\u585e 0100 0110 pdFALSE \u671f\u671bbit0, bit2\u67d0\u4f4d\u4e3a1\uff0c\u6ee1\u8db3\u9000\u51fa

\u540c\u6b65\u4e8b\u4ef6\u51fd\u6570\uff1a

EventBits_t xEventGroupSync(EventGroupHandle_t xEventGroup,\n                            const EventBits_t uxBitsToSet,\n                            const EventBits_t uxBitsToWaitFor,\n                            TickType_t xTicksToWait);\n

\u8be5\u51fd\u6570\u7528\u6765\u534f\u540c\u591a\u4e2a\u4efb\u52a1\uff0c\u5f53\u671f\u671b\u7684\u4e8b\u4ef6\u4e00\u8d77\u53d1\u751f\u540e\uff0c\u624d\u4f1a\u6210\u529f\u8fd4\u56de\u3002

"},{"location":"rtos/freertos/#_13","title":"\u4efb\u52a1\u901a\u77e5","text":"

FreeRTOS\u4eceV8.2.0\u7248\u672c\u5f00\u59cb\u63d0\u4f9b\u4efb\u52a1\u901a\u77e5\u8fd9\u4e2a\u529f\u80fd\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4efb\u52a1\u901a\u77e5\u53ef\u4ee5\u66ff\u4ee3\u4fe1\u53f7\u91cf\u3001\u4e8b\u4ef6\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u66ff\u4ee3\u957f\u5ea6\u4e3a1\u7684\u961f\u5217\u3002\u4efb\u52a1\u901a\u77e5\u66f4\u52a0\u7075\u6d3b\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\uff0c\u5e76\u4e14\u66f4\u52a0\u8282\u7701RAM\u7a7a\u95f4\uff0c\u7f3a\u70b9\u662f\u4efb\u52a1\u901a\u77e5\u53ea\u80fd\u7531\u4e00\u4e2a\u4efb\u52a1\u6765\u63a5\u6536\u901a\u77e5\u6d88\u606f\u3002

FreeRTOS\u63d0\u4f9b\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u53d1\u9001\u901a\u77e5\u7ed9\u4efb\u52a1\uff1a

  • \u5982\u679c\u6709\u901a\u77e5\u672a\u8bfb\uff0c\u4e0d\u8986\u76d6\u901a\u77e5\u503c
  • \u76f4\u63a5\u8986\u76d6\u901a\u77e5\u503c
  • \u8bbe\u7f6e\u901a\u77e5\u503c\u7684\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u4f4d\uff0c\u7c7b\u4f3c\u4e8b\u4ef6\u7ec4
  • \u9012\u589e\u901a\u77e5\u503c\uff0c\u7c7b\u4f3c\u8ba1\u6570\u4fe1\u53f7\u91cf

\u4efb\u52a1\u901a\u77e5\u662f\u4efb\u52a1\u63a7\u5236\u5757\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u6210\u5458\u53d8\u91cf\uff0c\u56e0\u6b64\u4efb\u52a1\u5728\u88ab\u521b\u5efa\u7684\u65f6\u5019\uff0c\u4efb\u52a1\u901a\u77e5\u5c31\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u4e86\u3002\u4efb\u52a1\u901a\u77e5\u53ef\u4ee5\u7531\u4efb\u52a1\u53d1\u9001\uff0c\u4e5f\u53ef\u4ee5\u7531\u4e2d\u65ad\u53d1\u9001\u3002\u53ea\u6709\u5728\u4efb\u52a1\u4e2d\u624d\u53ef\u4ee5\u7b49\u5f85\u901a\u77e5\uff0c\u800c\u4e0d\u5141\u8bb8\u5728\u4e2d\u65ad\u4e2d\u7b49\u5f85\u901a\u77e5\u3002\u5982\u679c\u4efb\u52a1\u5728\u7b49\u5f85\u901a\u77e5\u7684\u65f6\u5019\u6682\u65f6\u65e0\u6548\uff0c\u5219\u4efb\u52a1\u4f1a\u6839\u636e\u7528\u6237\u6307\u5b9a\u7684\u963b\u585e\u65f6\u95f4\u5185\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u53ea\u8981\u6709\u4efb\u52a1\u6216\u8005\u4e2d\u65ad\u53d1\u9001\u4efb\u52a1\u901a\u77e5\uff0c\u4efb\u52a1\u5c31\u4f1a\u4ece\u963b\u585e\u72b6\u6001\u89e3\u9664\u3002

\u6bcf\u4e2a\u4efb\u52a1\u90fd\u6709\u4e00\u4e2a\u7ed3\u6784\u4f53:TCB\uff08Task Control Block\uff09\uff0c\u91cc\u9762\u6709\u4e24\u4e2a\u6210\u5458\uff0c\u4e00\u4e2a\u7528\u6765\u8868\u793a\u901a\u77e5\u72b6\u6001\uff0c\u4e00\u4e2a\u7528\u6765\u8868\u793a\u901a\u77e5\u503c\u3002

typedef struct tskTaskControlBlock\n{\n    ......\n    /* configTASK_NOTIFICATION_ARRAY_ENTRIES = 1 */\n    volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];\n    volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];\n    ......\n} tskTCB;\n

\u901a\u77e5\u72b6\u6001\u67093\u79cd\u53d6\u503c\uff1a

  • taskNOT_WAITING_NOTIFICATION\uff1a\u4efb\u52a1\u6ca1\u6709\u5728\u7b49\u5f85\u901a\u77e5
  • taskWAITING_NOTIFICATION\uff1a\u4efb\u52a1\u5728\u7b49\u5f85\u901a\u77e5
  • taskNOTIFICATION_RECEIVED\uff1a\u4efb\u52a1\u63a5\u6536\u5230\u4e86\u901a\u77e5
"},{"location":"rtos/freertos/#_14","title":"\u8f6f\u4ef6\u5b9a\u65f6\u5668","text":"

\u8f6f\u4ef6\u5b9a\u65f6\u5668\u5c31\u662f\u201c\u95f9\u949f\u201d\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u95f9\u949f\uff1a\u65e9\u4e0a7\u70b9\u53eb\u9192\u4f60\uff1b\u6216\u8005\uff0c\u6bcf\u5de5\u4f5c\u4e00\u5c0f\u65f6\u5c31\u4f11\u606f\u4e00\u4f1a\u3002\u8f6f\u4ef6\u5b9a\u65f6\u5668\u7684\u529f\u80fd\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u662f\u5728\u672a\u6765\u67d0\u4e2a\u65f6\u95f4\u70b9\u89e6\u53d1\u67d0\u4e2a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u5468\u671f\u6027\u5730\u8fd0\u884c\u51fd\u6570\u3002

\u8f6f\u4ef6\u5b9a\u65f6\u5668\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4e00\u7c7b\u63a5\u53e3\uff0c\u5b83\u6784\u5efa\u5728\u786c\u4ef6\u5b9a\u65f6\u5668\u57fa\u7840\u4e4b\u4e0a\uff0c\u4ee5\u7cfb\u7edf\u8282\u62cd\u5468\u671f\u4e3a\u5355\u4f4d\u3002\u7cfb\u7edf\u8282\u62cd\u5c31\u662f\u7cfb\u7edf\u65f6\u949f\u7684\u9891\u7387\uff0c\u914d\u7f6e\u4e3aconfigTICK_RATE_HZ\uff0c\u9ed8\u8ba4\u4e3a1000\u3002\u4e5f\u5c31\u662f\u8bf41s\u8df3\u52a81000\u6b21\uff0c\u90a3\u4e48\u7cfb\u7edf\u8857\u62cd\u5468\u671f\u5c31\u662f1ms\u3002\u8f6f\u4ef6\u5b9a\u65f6\u5668\u7684\u5b9a\u65f6\u6570\u503c\u5fc5\u987b\u662f\u8fd9\u4e2a\u8282\u62cd\u5468\u671f\u7684\u6574\u6570\u500d\u3002\u5f53\u7136\uff0c\u7cfb\u7edf\u8282\u62cd\u7cbe\u5ea6\u8d8a\u9ad8\uff0c\u8f6f\u4ef6\u5b9a\u65f6\u5668\u6240\u80fd\u8bbe\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5c31\u8d8a\u5c0f\uff0c\u4f46\u662f\u7cfb\u7edf\u7684\u5f00\u9500\u4e5f\u4f1a\u53d8\u5927\u3002

FreeRTOS\u901a\u8fc7\u4e00\u4e2aprvTimerTask\u7684\u5b88\u62a4\u4efb\u52a1\u6765\u7ba1\u7406\u8f6f\u4ef6\u5b9a\u65f6\u5668\uff0c\u5b83\u662f\u5728\u542f\u52a8\u8c03\u5ea6\u5668\u65f6\u81ea\u52a8\u521b\u5efa\u7684\u3002\u5b88\u62a4\u4efb\u52a1\u4f1a\u5b9a\u671f\u68c0\u67e5\u7528\u6237\u7f16\u5199\u7684\u5b9a\u65f6\u5668\uff0c\u5e76\u8c03\u7528\u5176\u56de\u8c03\u51fd\u6570\u3002\u7528\u6237\u81ea\u5df1\u7f16\u5199\u7684\u4efb\u52a1\u51fd\u6570\u8981\u8c03\u7528\u5b9a\u65f6\u5668\u51fd\u6570\u65f6\uff0c\u662f\u901a\u8fc7\u201c\u5b9a\u65f6\u5668\u547d\u4ee4\u961f\u5217\u201d\u548c\u5b88\u62a4\u4efb\u52a1\u4ea4\u4e92\u7684\u3002\u5b88\u62a4\u4efb\u52a1\u7684\u4e3b\u8981\u5de5\u4f5c\u5c31\u662f\u4ece\u547d\u4ee4\u961f\u5217\u4e2d\u53d6\u51fa\u547d\u4ee4\u3001\u5904\u7406\uff0c\u6267\u884c\u5b9a\u65f6\u5668\u7684\u56de\u8c03\u51fd\u6570\u3002\u80fd\u5426\u53ca\u65f6\u5904\u7406\u5b9a\u65f6\u5668\u547d\u4ee4\uff0c\u53d6\u51b3\u4e8e\u5b88\u62a4\u4efb\u52a1\u7684\u4f18\u5148\u7ea7\u3002\u5176\u4f18\u5148\u7ea7\u9ed8\u8ba4\u4e3aconfiTIMER_TASK_PRIORITY\uff0c\u4e3a\u4e86\u66f4\u597d\u5730\u54cd\u5e94\uff0c\u8be5\u4f18\u5148\u7ea7\u5e94\u8bbe\u7f6e\u4e3a\u6240\u6709\u4efb\u52a1\u4e2d\u6700\u9ad8\u7684\u4f18\u5148\u7ea7\u3002

\u5b9a\u65f6\u5668\u56de\u8c03\u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b\uff1a

void ATimerCallback(TimerHandler_t xTimer);\n

\u5b9a\u65f6\u5668\u7684\u56de\u8c03\u51fd\u6570\u662f\u5728\u5b88\u62a4\u4efb\u52a1\u4e2d\u88ab\u8c03\u7528\u7684\uff0c\u56e0\u6b64\u8be5\u51fd\u6570\u4e0d\u80fd\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u4e5f\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55\u4f1a\u5f15\u8d77\u963b\u585e\u7684\u51fd\u6570\u3002

\u5b9a\u65f6\u5668\u7684\u590d\u4f4d\uff1a

BaseType_t xTimerReset(TimerHandle_t xTimer, TickType_t xTicksToWait);\n

\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u8ba9\u5b9a\u65f6\u5668\u7684\u72b6\u6001\u4ece\u51ac\u7720\u6001\u53d8\u4e3a\u8fd0\u884c\u6001\uff0c\u5982\u679c\u5b9a\u65f6\u5668\u5df2\u7ecf\u8fd0\u884c\uff0c\u5219\u76f8\u5f53\u4e8e\u91cd\u65b0\u786e\u5b9a\u8d85\u65f6\u65f6\u95f4\u3002

\u4fee\u6539\u5468\u671f\uff1a

BaseType_t xTimerChangePeriod(TimerHandle_t xTimer, TickType_t xNewPeriod, TickType_t xTicksToWait);\n
"},{"location":"rtos/freertos/#_15","title":"\u4e2d\u65ad\u7ba1\u7406","text":"

\u4e2d\u65ad\u5c5e\u4e8e\u5f02\u6b65\u5f02\u5e38\uff0c\u6765\u6e90\u4e8e\u5916\u90e8\u786c\u4ef6\u3002\u5f53\u6ca1\u6709\u4e2d\u65ad\u53d1\u751f\u65f6\uff0cCPU\u6267\u884c\u81ea\u5df1\u7684\u4efb\u52a1\u3002\u5f53\u4e2d\u65ad\u4ea7\u751f\u65f6\uff0cCPU\u5f53\u524d\u4efb\u52a1\u88ab\u6253\u65ad\uff0c\u8f6c\u800c\u53bb\u6267\u884c\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u3002\u5916\u90e8\u8bbe\u5907\u4ea7\u751f\u4e00\u4e2a\u4e2d\u65ad\u4fe1\u53f7\u5e76\u53d1\u9001\u81f3\u4e2d\u65ad\u63a7\u5236\u5668\uff0c\u7531\u4e2d\u65ad\u63a7\u5236\u5668\u5c06\u4fe1\u53f7\u518d\u8f6c\u53d1\u7ed9CPU\uff0cCPU\u54cd\u5e94\u4e2d\u65ad\u6e90\u7684\u8bf7\u6c42\u3002\u5f53\u4e2d\u65ad\u4ea7\u751f\u65f6\uff0c\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u4e0a\u4e0b\u6587\u72b6\u6001\u3002

\u4e2d\u65ad\u7684\u73af\u5883\u6709\u4e24\u79cd\uff1a

  • \u4efb\u52a1\u4e0a\u4e0b\u6587\uff1a\u65e0\u8bba\u4e2d\u65ad\u7684\u4f18\u5148\u7ea7\u662f\u591a\u5927\uff0c\u90fd\u4f1a\u6253\u65ad\u5f53\u524d\u4efb\u52a1\u7684\u6267\u884c\u3002
  • \u4e2d\u65ad\u4e0a\u4e0b\u6587\uff1a\u4ec5\u66f4\u9ad8\u4f18\u5148\u7ea7\u7684\u4e2d\u65ad\u53ef\u4ee5\u6253\u65ad\u5f53\u524d\u4e2d\u65ad\u7684\u6267\u884c\uff0c\u8fd9\u5fc5\u987b\u8981\u7cfb\u7edf\u652f\u6301\u4e2d\u65ad\u5d4c\u5957\u3002
"},{"location":"rtos/freertos/CMSIS/","title":"CMSIS\u6807\u51c6","text":"

Cortex\u7cfb\u5217\u7684\u82af\u7247\u5185\u6838\u662f\u76f8\u540c\u7684\uff0c\u4f46\u662f\u4e0d\u540c\u7684\u5382\u5546\u5728\u751f\u4ea7\u7684\u65f6\u5019\u5916\u8bbe\u4f1a\u6709\u533a\u522b\uff0c\u8fd9\u4e9b\u5dee\u5f02\u5bfc\u81f4\u8f6f\u4ef6\u5728\u540c\u5185\u6838\uff0c\u4e0d\u540c\u5916\u8bbe\u7684\u82af\u7247\u4e0a\u79fb\u690d\u56f0\u96be\u3002\u4e3a\u4e86\u89e3\u51b3\u4e0d\u540c\u82af\u7247\u5382\u5546\u751f\u4ea7\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff0cARM\u4e0e\u5404\u82af\u7247\u5382\u5546\u5efa\u8bae\u4e86CMSIS\u6807\u51c6\u3002

\u6240\u8c13\u7684CMSIS\u6807\u51c6\uff0c\u5176\u5b9e\u5c31\u662f\u65b0\u5efa\u4e86\u4e00\u4e2a\u8f6f\u4ef6\u62bd\u8c61\u5c42\uff0c\u5c06\u4e0d\u540c\u7684RTOS\u7684\u63a5\u53e3\u7edf\u4e00\u8d77\u6765\uff0c\u63d0\u4f9bCMSIS\u7684\u63a5\u53e3\u4f9b\u4f7f\u7528\u8005\u4f7f\u7528\u3002

\u672c\u6587\u53ea\u7b80\u5355\u5217\u4e3e\u4e86\u90e8\u5206CMSIS\u6807\u51c6\u4e0b\u7684\u51fd\u6570\u63a5\u53e3\uff0c\u5176\u4ed6\u51fd\u6570\u53ca\u5176\u5b9e\u73b0\u7ec6\u8282\u8bf7\u53c2\u8003\uff1aCMSIS-RTOS2

"},{"location":"rtos/freertos/CMSIS/#_1","title":"\u4efb\u52a1\u7ba1\u7406","text":"
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);    //\u521b\u5efa\u4efb\u52a1\nosThreadId_t osThreadGetId(void);    //\u8fd4\u56de\u5f53\u524d\u4efb\u52a1ID\nosStatus_t osThreadSuspend(osThreadId_t thread_id);    //\u6302\u8d77\u4efb\u52a1\nosStatus_t osThreadResume(osThreadId_t thread_id);    //\u6062\u590d\u4efb\u52a1\nosStatus_t osThreadYield(void);    //\u4e3b\u52a8\u8ba9\u51fa\u8d44\u6e90\nvoid osThreadExit(void)    //\u7ec8\u6b62\u5f53\u524d\u4efb\u52a1\nosStatus_t osThreadTerminate(osThreadId_t thread_id);    //\u7ec8\u6b62\u5176\u4ed6\u4efb\u52a1\nosStatus_t osDelay(uint32_t ticks);    //\u5ef6\u65f6ticks\u65f6\u949f\u5468\u671f\n
"},{"location":"rtos/freertos/CMSIS/#_2","title":"\u6d88\u606f\u961f\u5217","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5730\u5740\n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n    void *mq_mem;        //\u6570\u636e\u5b58\u50a8\u5730\u5740\n    uint32_t mq_size;    //\u6570\u636e\u5b58\u50a8\u5927\u5c0f\n}osMessageQueueAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);    //\u521b\u5efa\nosStatus_t osMessageQueueDelete(osMessageQueueId_t mq_id);    //\u5220\u9664\nosStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);    //\u53d1\u9001\u6d88\u606f\nosStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);    //\u83b7\u53d6\u6d88\u606f\nosStatus_t osMessageQueueReset(osMessageQueueId_t mq_id);    //\u91cd\u7f6e\u6d88\u606f\u961f\u5217\n
"},{"location":"rtos/freertos/CMSIS/#_3","title":"\u4fe1\u53f7\u91cf","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;   //\u540d\u5b57\n    uint32_t attr_bits; //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;       //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5730\u5740\n    uint32_t cb_size;   //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osSemaphoreAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);    //\u521b\u5efa\nosStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout);    //\u83b7\u53d6\nosStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id);    //\u91ca\u653e\nuint32_t osSemaphoreGetCount(osSemaphoreID_t semaphore_id);    //\u83b7\u53d6\u8ba1\u6570\u91cf\nosStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_4","title":"\u4e92\u65a5\u91cf","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //\u4e92\u65a5\u91cf\u7c7b\u578b\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5185\u5b58 \n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osMutexAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osMutexId_t osMutexNew(const osMutexAttr_t *attr);    //\u521b\u5efa\nosStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout);    //\u83b7\u53d6\nosStatus_t osMutexRelease(osMutexId_t mutex_id);    //\u91ca\u653e\nosStatus_t osMutexDelete(osMutexId_t mutex_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_5","title":"\u4e8b\u4ef6","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5185\u5b58\n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osEventFlagsAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr);    //\u521b\u5efa\nuint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags);    //\u8bbe\u7f6e\u4f4d\nuint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags);    //\u6e05\u7a7a\u4f4d\nuint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout);    //\u7b49\u5f85\u4f4d\nosStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_6","title":"\u4efb\u52a1\u901a\u77e5","text":"

CMSIS\u6807\u51c6\u6ca1\u6709\u63d0\u4f9b\u4efb\u52a1\u901a\u77e5\u7684API\uff0c\u5c5e\u4e8eFreeRTOS\u7684\u7279\u6027\u3002

"},{"location":"rtos/freertos/CMSIS/#_7","title":"\u5b9a\u65f6\u5668","text":"

\u5b9a\u4e49\uff1a

typedef struct {\n    const char *name;    //\u540d\u5b57\n    uint32_t attr_bits;  //FreeRTOS\u672a\u4f7f\u7528\n    void *cb_mem;        //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5185\u5b58\n    uint32_t cb_size;    //\u63a7\u5236\u5757\uff08\u9759\u6001\u521b\u5efa\uff09\u5927\u5c0f\n}osTimerAttr_t;\n

\u5e38\u7528\u51fd\u6570\uff1a

osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);    //\u521b\u5efa\nosStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks);    //\u542f\u52a8\nosStatus_t osTimerStop(osTimerId_t timer_id);   //\u505c\u6b62\nosStatus_t osTimerDelete(osTimerId_t timer_id);    //\u5220\u9664\n
"},{"location":"rtos/freertos/CMSIS/#_8","title":"\u5185\u5b58\u7ba1\u7406","text":"

\u5e38\u7528\u51fd\u6570\uff1a

osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, unit32_t block_size, const osMemoryPoolAttr_t *attr);    //\u521b\u5efa\nvoid *osMemoryPoolAlloc(osMemoryPoolId_t mp_id, unit32_t timeout);    //\u5206\u914d\nosStatut_t osMemoryPoolFree(osMemoryPoolId_t mp_id, void *block);    //\u91ca\u653e\nosStatus_t osMemoryPoolDelete(osMemoryPoolId_t mp_id);    //\u5220\u9664\n
"},{"location":"rtos/rt-thread/","title":"rtt\u5185\u6838\u7b80\u4ecb","text":"

\u7531\u4e8e\u4e0d\u540cRTOS\u7684\u5185\u6838\u5927\u5dee\u4e0d\u5dee\uff0c\u8fd9\u91cc\u53ea\u8bb0\u5f55\u4e0brtt\u5185\u6838\u6bd4\u8f83\u7279\u6b8a\u7684\u5730\u65b9\u3002

rtt\u5185\u6838\u67b6\u6784\u5982\u4e0b\u56fe\u6240\u793a\uff1a

rtt\u5185\u6838\u6a21\u5757\u4e3b\u8981\u5206\u4e3a\uff1a

  1. \u7ebf\u7a0b\u8c03\u5ea6
  2. \u65f6\u949f\u7ba1\u7406
  3. \u7ebf\u7a0b\u95f4\u540c\u6b65\u4e0e\u901a\u4fe1
  4. \u5185\u5b58\u7ba1\u7406
  5. \u4e2d\u65ad\u7ba1\u7406
  6. I/O\u8bbe\u5907\u7ba1\u7406
  7. \u5916\u8bbe\u63a5\u53e3

\u4e0b\u56fe\u663e\u793a\u4e86rtt\u4e2d\u5404\u7c7b\u5185\u6838\u5bf9\u8c61\u7684\u6d3e\u751f\u548c\u7ee7\u627f\u5173\u7cfb\u3002

\u5185\u6838\u5bf9\u8c61\u63a7\u5236\u5757\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct rt_object {\n    char name[RT_NAME_MAX];    //\u5185\u6838\u5bf9\u8c61\u540d\u79f0\n    rt_uint8_t type;           //\u5185\u6838\u5bf9\u8c61\u7c7b\u578b\n    rt_uint8_t flag;           //\u5185\u6838\u5bf9\u8c61\u53c2\u6570\n    rt_list_t list;            //\u5185\u6838\u5bf9\u8c61\u7ba1\u7406\u94fe\u8868\n};\n

\u5185\u6838\u5bf9\u8c61\u5bb9\u5668\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a

struct rt_object_information {\n    enum rt_object_class_type type;    //\u5bf9\u8c61\u7c7b\u578b\n    rt_list_t object_list;             //\u5bf9\u8c61\u94fe\u8868\n    rt_size_t object_size;             //\u5bf9\u8c61\u5927\u5c0f\n};\n

\u540c\u4e00\u7c7b\u5bf9\u8c61\u7531\u4e00\u4e2art_object_information\u7ed3\u6784\u4f53\u6765\u7ba1\u7406\uff0c\u8fd9\u7c7b\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u4ee5\u94fe\u8868\u7684\u5f62\u5f0f\u6302\u5728object_list\u94fe\u8868\u4e0a\u3002\u5f53\u67d0\u4e2a\u5185\u6838\u5bf9\u8c61\u88ab\u521d\u59cb\u5316\u540e\uff0c\u8be5\u5bf9\u8c61\u5c31\u88ab\u6dfb\u52a0\u5230\u5bf9\u5e94\u5bb9\u5668\u4e2d\u7ba1\u7406\u3002

"},{"location":"rtos/rt-thread/#_1","title":"\u7ebf\u7a0b\u7ba1\u7406","text":"

rtt\u4e2d\u7ebf\u7a0b\u72b6\u6001\u591a\u4e86\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\uff0c\u5f53\u7ebf\u7a0b\u521b\u5efa\u5b8c\u6bd5\u8fd8\u6ca1\u5f00\u59cb\u8fd0\u884c\u65f6\u5c31\u5904\u4e8e\u521d\u59cb\u72b6\u6001\uff0c\u8be5\u72b6\u6001\u4e0b\u7ebf\u7a0b\u4e0d\u53c2\u4e0e\u8c03\u5ea6\uff0c\u5b8f\u5b9a\u4e49\u4e3aRT_THREAD_INIT\u3002\u521d\u59cb\u72b6\u6001\u7684\u7ebf\u7a0b\u9700\u8981\u8c03\u7528rt_thread_startup()\u8fdb\u5165\u5c31\u7eea\u72b6\u6001\u3002

rtt\u6700\u5927\u652f\u6301256\u4e2a\u4f18\u5148\u7ea7\uff0c\u5bf9\u4e8eARM Cortex-M\u7cfb\u5217\u4e00\u822c\u91c7\u752832\u4e2a\u4f18\u5148\u7ea7\u3002

\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u65f6\u95f4\u7247\u53c2\u6570\uff0c\u65f6\u95f4\u7247\u4ec5\u5bf9\u4f18\u5148\u7ea7\u76f8\u540c\u7684\u5c31\u7eea\u72b6\u6001\u7ebf\u7a0b\u6709\u6548\uff08\u5373\u540c\u4f18\u5148\u7ea7\u53c2\u4e0e\u8c03\u5ea6\uff09\u3002\u65f6\u95f4\u7247\u7684\u5355\u4f4d\u662f\u4e00\u4e2a\u7cfb\u7edf\u8282\u62cd\uff0c\u5176\u610f\u4e49\u662f\u7ebf\u7a0b\u5355\u6b21\u8fd0\u884c\u7684\u65f6\u957f\u3002

\u4f7f\u7528rt_thread_create()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u7ebf\u7a0b\uff0c\u4f7f\u7528rt_thread_delete()\u51fd\u6570\u5220\u9664\u4e00\u4e2a\u52a8\u6001\u7ebf\u7a0b\u3002\u9759\u6001\u7ebf\u7a0b\u7684API\u5206\u522b\u662frt_thread_init()\u51fd\u6570\u548crt_thread_detach()\u51fd\u6570\u3002\u4f7f\u7528rt_thread_startup()\u51fd\u6570\u542f\u52a8\u7ebf\u7a0b\uff0crt_thread_yield()\u51fd\u6570\u8ba9\u51fa\u7ebf\u7a0b\u8d44\u6e90\uff0crt_thread_delay()\u51fd\u6570\u4f7f\u7ebf\u7a0b\u7761\u7720\uff0crt_thread_resume()\u51fd\u6570\u6062\u590d\u7ebf\u7a0b\u3002

"},{"location":"rtos/rt-thread/#_2","title":"\u65f6\u949f\u7ba1\u7406","text":"

\u5168\u5c40\u53d8\u91cfrt_tick\u6bcf\u6b21\u7ecf\u8fc7\u4e00\u4e2a\u65f6\u949f\u8282\u62cd\uff0c\u503c\u5c31\u4f1a\u52a01\uff0c\u5b83\u8868\u793a\u7cfb\u7edf\u4ece\u542f\u52a8\u5f00\u59cb\u603b\u5171\u7ecf\u8fc7\u7684\u59cb\u7ec8\u8282\u62cd\u6570\u3002\u6b64\u5916\uff0crt_tick\u6bcf\u6b21\u52a01\u90fd\u4f1a\u68c0\u67e5\u5f53\u524d\u7ebf\u7a0b\u7684\u65f6\u95f4\u7247\u662f\u5426\u4f7f\u7528\u5b8c\uff0c\u4ee5\u53ca\u662f\u5426\u6709\u5b9a\u65f6\u5668\u8d85\u65f6\u3002

rtt\u7684\u5b9a\u65f6\u5668\u53ef\u4ee5\u5206\u4e3aHARD_TIMER\u6a21\u5f0f\u2014\u2014\u5728\u4e2d\u65ad\u73af\u5883\u4e2d\u6267\u884c\uff0c\u548cSOFT_TIMER\u6a21\u5f0f\u2014\u2014\u5728\u7ebf\u7a0b\u73af\u5883\u4e2d\u6267\u884c\u3002

\u65b0\u521b\u5efa\u5e76\u6fc0\u6d3b\u7684\u5b9a\u65f6\u5668\u90fd\u4f1a\u4ee5\u8d85\u65f6\u65f6\u95f4\u6392\u5e8f\u7684\u65b9\u5f0f\u63d2\u5165\u5230rt_timer_list\u94fe\u8868\u4e2d\u3002\u6240\u6709\u5b9a\u65f6\u5668\u5728\u5b9a\u65f6\u8d85\u65f6\u540e\u90fd\u4f1a\u4ece\u5b9a\u65f6\u5668\u94fe\u8868\u4e2d\u88ab\u79fb\u9664\uff0c\u800c\u5468\u671f\u6027\u5b9a\u65f6\u5668\u4f1a\u5728\u5b83\u518d\u6b21\u542f\u52a8\u65f6\u88ab\u52a0\u5165\u5b9a\u65f6\u5668\u94fe\u8868\u3002\u4e0e\u5b9a\u65f6\u5668\u6709\u5173\u7684\u64cd\u4f5c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

"},{"location":"rtos/rt-thread/#_3","title":"\u7ebf\u7a0b\u95f4\u540c\u6b65","text":""},{"location":"rtos/rt-thread/#_4","title":"\u4fe1\u53f7\u91cf","text":"

\u4fe1\u53f7\u91cf\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

rtt\u5185\u6838\u5728\u521b\u5efa\u4fe1\u53f7\u91cf\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u901a\u8fc7flag\u53c2\u6570\u6307\u5b9a\u4fe1\u53f7\u91cf\u4e0d\u53ef\u7528\u65f6\u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u7684\u6392\u961f\u65b9\u5f0f\u3002\u6bd4\u5982RT_IPC_FLAG_FIFO\u8868\u793a\u6309\u7167\u5148\u8fdb\u5148\u51fa\u7684\u65b9\u5f0f\u6392\u961f\uff0cRT_IPC_FLAG_PRIO\u8868\u793a\u6309\u7167\u4f18\u5148\u7ea7\u7684\u65b9\u5f0f\u8fdb\u884c\u6392\u961f\u3002\u4e00\u822c\u91c7\u7528\u4f18\u5148\u7ea7\u65b9\u5f0f\uff0c\u9664\u975e\u5e94\u7528\u7a0b\u5e8f\u975e\u5e38\u5728\u610f\u5148\u6765\u540e\u5230\u3002

"},{"location":"rtos/rt-thread/#_5","title":"\u4e92\u65a5\u91cf","text":"

\u4e92\u65a5\u91cf\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

\u4e92\u65a5\u91cf\u4e3b\u8981\u9002\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a

  1. \u7ebf\u7a0b\u591a\u6b21\u6301\u6709\u4e92\u65a5\u91cf\uff0c\u4e92\u65a5\u91cf\u53ef\u4ee5\u9012\u5f52\u6301\u6709\u800c\u4fe1\u53f7\u91cf\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002
  2. \u7531\u4e8e\u591a\u7ebf\u7a0b\u540c\u6b65\u800c\u9020\u6210\u4f18\u5148\u7ea7\u53cd\u8f6c\u95ee\u9898\u3002
"},{"location":"rtos/rt-thread/#_6","title":"\u4e8b\u4ef6\u7ec4","text":"

rtt\u5b9a\u4e49\u7684\u4e8b\u4ef6\u96c6\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a

  1. \u4e8b\u4ef6\u53ea\u4e0e\u7ebf\u7a0b\u76f8\u5173\uff0c\u4e8b\u4ef6\u95f4\u76f8\u4e92\u72ec\u7acb\uff1a\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u62e5\u670932\u4e2a\u4e8b\u4ef6\u6807\u5fd7\uff0c\u91c7\u7528\u4e00\u4e2a32bit\u65e0\u7b26\u53f7\u6574\u578b\u6570\u8fdb\u884c\u8bb0\u5f55\uff0c\u6bcf\u4e00\u4e2abit\u4ee3\u8868\u4e00\u4e2a\u4e8b\u4ef6\uff1b

  2. \u4e8b\u4ef6\u4ec5\u7528\u4e8e\u540c\u6b65\uff0c\u4e0d\u63d0\u4f9b\u6570\u636e\u4f20\u8f93\u529f\u80fd\uff1b

  3. \u4e8b\u4ef6\u65e0\u6392\u961f\u6027\uff0c\u5373\u591a\u6b21\u5411\u7ebf\u7a0b\u53d1\u9001\u540c\u4e00\u4e8b\u4ef6 (\u5982\u679c\u7ebf\u7a0b\u8fd8\u672a\u6765\u5f97\u53ca\u8bfb\u8d70)\uff0c\u5176\u6548\u679c\u7b49\u540c\u4e8e\u53ea\u53d1\u9001\u4e00\u6b21\u3002

\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u62e5\u6709\u4e00\u4e2a\u4e8b\u4ef6\u4fe1\u606f\u6807\u8bb0\uff0c\u5b83\u6709\u4e09\u4e2a\u5c5e\u6027\uff0c\u5206\u522b\u662fRT_EVENT_FLAG_AND(\u903b\u8f91\u4e0e)\uff0cRT_EVENT_FLAG_OR(\u903b\u8f91\u6216\uff09\u4ee5\u53caRT_EVENT_FLAG_CLEAR(\u6e05\u9664\u6807\u8bb0\uff09\u3002\u5f53\u7ebf\u7a0b\u7b49\u5f85\u4e8b\u4ef6\u540c\u6b65\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc732\u4e2a\u4e8b\u4ef6\u6807\u5fd7\u548c\u8fd9\u4e2a\u4e8b\u4ef6\u4fe1\u606f\u6807\u8bb0\u6765\u5224\u65ad\u5f53\u524d\u63a5\u6536\u7684\u4e8b\u4ef6\u662f\u5426\u6ee1\u8db3\u540c\u6b65\u6761\u4ef6\u3002

\u4e8b\u4ef6\u7ec4\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_7","title":"\u7ebf\u7a0b\u95f4\u901a\u4fe1","text":""},{"location":"rtos/rt-thread/#_8","title":"\u90ae\u7bb1","text":"

\u90ae\u7bb1\u7684\u7279\u70b9\u662f\u5f00\u9500\u6bd4\u8f83\u4f4e\uff0c\u6548\u7387\u8f83\u9ad8\u3002\u90ae\u7bb1\u4e2d\u7684\u6bcf\u4e00\u5c01\u90ae\u4ef6\u53ea\u80fd\u5bb9\u7eb3\u56fa\u5b9a\u76844\u5b57\u8282\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\u8bf4\u90ae\u7bb1\u53ea\u80fd\u7528\u6765\u4f20\u8f93\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5bb9\u3002\u90ae\u7bb1\u4f7f\u7528\u4e00\u4e2a\u56fa\u5b9a\u7f13\u51b2\u533a\u5927\u5c0f\u6765\u5b58\u50a8\u90ae\u4ef6\uff0c\u5176\u5927\u5c0f\u5728\u521b\u5efa\u65f6\u88ab\u5b9a\u4e49\u3002

\u975e\u963b\u585e\u65b9\u5f0f\u7684\u90ae\u4ef6\u53d1\u9001\u8fc7\u7a0b\u80fd\u591f\u5b89\u5168\u7684\u5e94\u7528\u4e8e\u4e2d\u65ad\u670d\u52a1\u4e2d\uff0c\u662f\u7ebf\u7a0b\u3001\u4e2d\u65ad\u670d\u52a1\u3001\u5b9a\u65f6\u5668\u5411\u7ebf\u7a0b\u53d1\u9001\u6d88\u606f\u7684\u6709\u6548\u624b\u6bb5\u3002\u6839\u636e\u5bf9\u5e94\u7684\u60c5\u51b5\uff0c\u90ae\u7bb1\u7684\u6536\u53d6\u6216\u8005\u53d1\u9001\u90fd\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u8d85\u65f6\u963b\u585e\uff0c\u8fdb\u5165\u963b\u585e\u72b6\u6001\u7684\u7ebf\u7a0b\u5c06\u7761\u7720\uff0c\u7b49\u5f85\u5bf9\u5e94\u6761\u4ef6\u6ee1\u8db3\u540e\u5524\u9192\u3002

\u90ae\u7bb1\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_9","title":"\u6d88\u606f\u961f\u5217","text":"

\u6d88\u606f\u961f\u5217\u662f\u90ae\u7bb1\u7684\u6269\u5c55\uff0c\u53ef\u4ee5\u7528\u6765\u4f20\u8f93\u4e0d\u56fa\u5b9a\u957f\u5ea6\u7684\u5185\u5bb9\u3002

\u6d88\u606f\u961f\u5217\u9700\u8981\u5c06\u6d88\u606f\u7f13\u5b58\u5728\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u56e0\u6b64\u5185\u5b58\u5360\u7528\u6bd4\u90ae\u7bb1\u8981\u5927\u3002\u591a\u4e2a\u7ebf\u7a0b\u80fd\u591f\u8bfb\u53d6\u540c\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u3002\u800c\u5f53\u6d88\u606f\u961f\u5217\u662f\u7a7a\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u6302\u8d77\u8bfb\u53d6\u7ebf\u7a0b\u3002\u5f53\u6709\u65b0\u7684\u6d88\u606f\u5230\u8fbe\u65f6\uff0c\u6302\u8d77\u7684\u7ebf\u7a0b\u5c06\u88ab\u5524\u9192\u4ee5\u63a5\u6536\u5e76\u5904\u7406\u6d88\u606f\u3002\u6d88\u606f\u961f\u5217\u662f\u4e00\u79cd\u5f02\u6b65\u7684\u901a\u4fe1\u65b9\u5f0f\u3002

\u6d88\u606f\u961f\u5217\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_10","title":"\u4fe1\u53f7","text":"

\u4fe1\u53f7\u5176\u5b9e\u662f\u7528\u6237\u5c42\u9762\u5bf9\u786c\u4ef6\u4e2d\u65ad\u7684\u4e00\u79cd\u6a21\u62df\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u6536\u5230\u4fe1\u53f7\u4e4b\u540e\u4f1a\u8f6c\u53bb\u5bf9\u5e94\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002rtt\u4e2d\u4f7f\u7528rt_sigset_t\u7c7b\u578b\u6765\u5b9a\u4e49\u4fe1\u53f7\u96c6\u3002

\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6536\u5230\u4fe1\u53f7\u65f6\uff0c\u5982\u679c\u5b83\u5904\u4e8e\u6302\u8d77\u72b6\u6001\uff0c\u90a3\u4e48\u5b83\u4f1a\u6539\u4e3a\u5c31\u7eea\u72b6\u6001\u53bb\u5904\u7406\u5bf9\u5e94\u7684\u4fe1\u53f7\u3002

\u4fe1\u53f7\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

\u5728\u4fe1\u53f7\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5148\u5b9a\u4e49\u7ebf\u7a0b\u9700\u8981\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528rt_signal_install()\u51fd\u6570\u5c06signo\u4e0e\u5bf9\u5e94\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u76f8\u5173\u8054\u3002

"},{"location":"rtos/rt-thread/#_11","title":"\u5185\u5b58\u7ba1\u7406","text":"

\u5b9e\u65f6\u7cfb\u7edf\u8981\u6c42\u5185\u5b58\u5757\u7684\u5206\u914d\u8fc7\u7a0b\u5728\u53ef\u9884\u6d4b\u7684\u65f6\u95f4\u5185\u5b8c\u6210\uff0c\u5426\u5219\u5b9e\u65f6\u4efb\u52a1\u5bf9\u5916\u90e8\u4e8b\u4ef6\u7684\u54cd\u5e94\u5c06\u53d8\u5f97\u4e0d\u786e\u5b9a\u3002rtt\u5185\u6838\u5728\u5185\u5b58\u5206\u914d\u7ba1\u7406\u7b97\u6cd5\u4e0a\uff0c\u63d0\u4f9b\u4e86\u4e24\u5927\u7c7b\uff1a\u5185\u5b58\u5806\u7ba1\u7406\u4e0e\u5185\u5b58\u6c60\u7ba1\u7406\u3002\u800c\u5185\u5b58\u5bf9\u7ba1\u7406\u53c8\u5212\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a\u5c0f\u5185\u5b58\u7ba1\u7406\u7b97\u6cd5\uff0cslab\u7ba1\u7406\u7b97\u6cd5\u548cmemheap\u7ba1\u7406\u7b97\u6cd5\u3002

"},{"location":"rtos/rt-thread/#_12","title":"\u5185\u5b58\u5806\u7ba1\u7406","text":"

\u5c0f\u5185\u5b58\u7ba1\u7406\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5185\u5b58\u5206\u914d\u7b97\u6cd5\u3002\u521d\u59cb\u65f6\uff0c\u5b83\u662f\u4e00\u5757\u5927\u5185\u5b58\uff0c\u5f53\u9700\u8981\u5206\u914d\u5185\u5b58\u5757\u65f6\uff0c\u4ece\u8fd9\u5757\u5927\u5185\u5b58\u4e0a\u5206\u5272\u51fa\u54cd\u5e94\u7684\u5185\u5b58\u5757\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u5757\u5f52\u8fd8\u7ed9\u5806\u7ba1\u7406\u7cfb\u7edf\u3002\u6bcf\u4e2a\u5185\u5b58\u5757\u90fd\u7528\u53cc\u5411\u94fe\u8868\u7684\u65b9\u5f0f\u94fe\u63a5\u8d77\u6765\u3002\u901a\u8fc7*used\u6210\u5458\u53d8\u91cf\u6807\u8bb0\u5f53\u524d\u5185\u5b58\u5757\u662f\u5426\u5df2\u88ab\u4f7f\u7528\u3002

slab\u5206\u914d\u5668\u4f1a\u6839\u636e\u5bf9\u8c61\u7684\u5927\u5c0f\u5206\u6210\u591a\u4e2a\u533a\uff08zone\uff09\uff0c\u4e00\u4e2azone\u7684\u5927\u5c0f\u572832K\u5230128K\u5b57\u8282\u4e4b\u95f4\uff0c\u7cfb\u7edf\u4e2d\u7684zone\u6700\u591a\u5305\u62ec72\u79cd\u5bf9\u8c61\uff0c\u4e00\u6b21\u6027\u6700\u5927\u53ef\u4ee5\u5206\u914d16K\u7684\u7a7a\u95f4\u3002\u5982\u679c\u8d85\u51fa\u4e8616K\u5219\u76f4\u63a5\u4ece\u9875\u5206\u914d\u5668\u4e2d\u5206\u914d\u3002\u6bcf\u4e2azone\u4e0a\u90fd\u662f\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u5757\u94fe\u63a5\u5728\u4e00\u8d77\u3002\u800c72\u79cd\u5bf9\u8c61\u7684zone\u94fe\u8868\u5219\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7edf\u4e00\u7ba1\u7406\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a

memheap\u7ba1\u7406\u7b97\u6cd5\u9002\u7528\u4e8e\u7cfb\u7edf\u6709\u591a\u4e2a\u4e0d\u53ef\u8fde\u7eed\u5730\u5740\u7684\u5185\u5b58\u5806\u3002\u7528\u6237\u53ea\u9700\u8981\u5728\u7cfb\u7edf\u521d\u59cb\u5316\u65f6\u5c06\u591a\u4e2a\u6240\u9700\u7684memheap\u521d\u59cb\u5316\uff0c\u5e76\u5f00\u542fmemheap\u529f\u80fd\u5c31\u53ef\u4ee5\u5c06\u591a\u4e2amemheap\u7c98\u5408\u8d77\u6765\u7edf\u4e00\u7528\u4f5c\u5185\u5b58\u5206\u914d\u3002

\u5728\u4f7f\u7528\u5185\u5b58\u5806\u65f6\uff0c\u5fc5\u987b\u5728\u7cfb\u7edf\u521d\u59cb\u5316\u7684\u65f6\u5019\u8fdb\u884c\u5806\u7684\u521d\u59cb\u5316\uff1a

void rt_system_heap_init(void *begin_addr, void *end_addr);\n

\u5185\u5b58\u5806\u7ba1\u7406\u7684\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_13","title":"\u5185\u5b58\u6c60\u7ba1\u7406","text":"

\u9891\u7e41\u5730\u8c03\u7528\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u51fd\u6570\u4f1a\u5bfc\u81f4\u5185\u5b58\u788e\u7247\uff0c\u56e0\u6b64rtt\u5f15\u8fdb\u4e86\u53e6\u5916\u4e00\u79cd\u5185\u5b58\u7ba1\u7406\u65b9\u6cd5\uff1a\u5185\u5b58\u6c60\u3002

\u5185\u5b58\u6c60\u7528\u4e8e\u5206\u914d\u5927\u91cf\u5927\u5c0f\u76f8\u540c\u7684\u5c0f\u5185\u5b58\u5757\uff0c\u5185\u5b58\u6c60\u5728\u521b\u5efa\u65f6\u5148\u5411\u7cfb\u7edf\u7533\u8bf7\u4e00\u5927\u5757\u5185\u5b58\uff0c\u7136\u540e\u5206\u6210\u540c\u6837\u5927\u5c0f\u7684\u591a\u4e2a\u5c0f\u5185\u5b58\u5757\uff0c\u5c0f\u5185\u5b58\u5757\u76f4\u63a5\u901a\u8fc7\u94fe\u8868\u8fde\u63a5\u8d77\u6765\uff08\u6b64\u94fe\u8868\u4e5f\u79f0\u4e3a\u7a7a\u95f2\u94fe\u8868\uff09\u3002\u6bcf\u6b21\u5206\u914d\u7684\u65f6\u5019\uff0c\u4ece\u7a7a\u95f2\u94fe\u8868\u4e2d\u53d6\u51fa\u94fe\u5934\u4e0a\u7b2c\u4e00\u4e2a\u5185\u5b58\u5757\uff0c\u63d0\u4f9b\u7ed9\u7533\u8bf7\u8005\u3002\u5185\u5b58\u6c60\u4e00\u65e6\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u5185\u90e8\u7684\u5185\u5b58\u5757\u5927\u5c0f\u5c06\u4e0d\u80fd\u518d\u505a\u8c03\u6574\u3002

\u5185\u5b58\u6c60\u76f8\u5173\u63a5\u53e3\u51fd\u6570\u5982\u4e0b\uff1a

"},{"location":"rtos/rt-thread/#_14","title":"\u4e2d\u65ad\u7ba1\u7406","text":""},{"location":"tools/","title":"\u5de5\u5177","text":"

\u5217\u4e3e\u4e00\u4e0b\u81ea\u5df1\u4f7f\u7528\u8fc7\u7684\u5de5\u5177\u3002

"},{"location":"tools/github/","title":"github","text":"

\u7531\u4e8e\u4f17\u6240\u5468\u77e5\u7684\u539f\u56e0\uff0cgithub\u5728\u56fd\u5185\u7684\u8bbf\u95ee\u4e0d\u5c3d\u5982\u4eba\u610f\uff0c\u4f46\u662f\u56fd\u5916\u5f00\u6e90\u9879\u76ee\u51e0\u4e4e\u90fd\u90e8\u7f72\u5728github\u4e0a\uff0c\u4e0d\u5f97\u4e0d\u7528\u3002\u5982\u679c\u662f\u81ea\u5df1\u7684\u865a\u62df\u673a\u8fde\u63a5\u7f51\u901f\u6bd4\u8f83\u6162\uff0c\u5efa\u8bae\u5f00\u4e2a\u68af\u5b50\u7136\u540e\u914d\u7f6e\u4e0b\u6d41\u91cf\u4ee3\u7406\uff1a

git config --global http.proxy http://127.0.0.1:[\u7aef\u53e3\u53f7]\n\ngit config --global https.proxy http://127.0.0.1:[\u7aef\u53e3\u53f7]\n

\u8fd9\u4e24\u884c\u547d\u4ee4\u7684\u610f\u601d\u662f\u8ba9http\u548chttps\u534f\u8bae\u7684\u6d41\u91cf\u5168\u90e8\u8d70\u4f60\u68af\u5b50\u7684\u4ee3\u7406\uff0c\u7aef\u53e3\u53f7\u6839\u636e\u68af\u5b50\u7684\u914d\u7f6e\u81ea\u884c\u8f93\u5165\u3002\u914d\u7f6e\u5b8c\u4ee5\u540egit clone\u5e94\u8be5\u4e0d\u4f1a\u5361\u4e86\u3002\u5982\u679c\u4f60\u8981\u53d6\u6d88\u5168\u5c40\u4ee3\u7406\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a

git config --global --unset http.proxy\n\ngit config --global --unset https.proxy\n

\u4e00\u822c\u6765\u8bf4\uff0cgithub\u7684SSH\u9ed8\u8ba4\u901a\u8fc7\u7aef\u53e322\u5efa\u7acb\u8fde\u63a5\uff0c\u9664\u975e\u4f60\u66f4\u6539\u4e86\u914d\u7f6e\u6587\u4ef6\u5f3a\u5236\u4f7f\u7528HTTPS\u7684443\u7aef\u53e3\u8fde\u63a5\u3002

\u8981\u6d4b\u8bd5\u901a\u8fc7HTTPS\u7aef\u53e3\u7684SSH\u8fde\u63a5\u662f\u5426\u53ef\u884c\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a

ssh -T -p 443 git@ssh.github.com\n\n> Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.\n
\u5982\u679c\u8f93\u51fa\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5219\u8bf4\u660e\u8fde\u63a5\u53ef\u884c\u3002\u4f60\u53ef\u4ee5\u66f4\u6539~/.ssh/config\u6587\u4ef6\uff0c\u5f3a\u5236\u4e0egithub\u7684\u8fde\u63a5\u90fd\u901a\u8fc7\u8be5\u670d\u52a1\u5668\u548c\u7aef\u53e3\uff1a

Host github.com\nHostname ssh.github.com\nPort 443\nUser git\n

\u901a\u8fc7\u518d\u6b21\u8fde\u63a5\u5230github\u6765\u6d4b\u8bd5\u662f\u5426\u6709\u6548\uff0c\u6ce8\u610f\u8fd9\u65f6\u5c31\u4e0d\u9700\u8981\u6307\u5b9a\u7aef\u53e3\u53f7\u4e86\uff1a

ssh -T git@github.com\n\n> Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.\n

\u6ce8\u610f\uff1aSSH\u901a\u8fc7\u516c\u94a5\u548c\u79c1\u94a5\u914d\u5bf9\u7684\u65b9\u5f0f\u6765\u9a8c\u8bc1\u8fde\u63a5\u662f\u5426\u5b89\u5168\uff0c\u5f53\u4f60\u7b2c\u4e00\u6b21\u901a\u8fc7SSH\u8fde\u63a5\u65f6\uff0c\u4f1a\u8be2\u95ee\u4f60\u662f\u5426\u4fe1\u4efb\u8be5\u670d\u52a1\u5668\uff0c\u8f93\u5165yes\u5373\u53ef\u3002

"},{"location":"tools/github/#ssh","title":"\u6dfb\u52a0SSH\u5bc6\u94a5","text":"

\u6dfb\u52a0SSH\u5bc6\u94a5\u7684\u8fc7\u7a0b\u5f88\u7b80\u5355\uff0c\u6309\u7167\u6b65\u9aa4\u4e00\u6b65\u6b65\u6765\u5373\u53ef\u3002

1.\u9996\u5148\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5c06\u90ae\u4ef6\u5730\u5740\u6539\u4e3a\u4f60\u6ce8\u518cgithub\u7684\u90ae\u7bb1\uff1a

ssh-keygen -t ed25519 -C \"your_email@example.com\"\n

\u7cfb\u7edf\u4f1a\u8981\u6c42\u4f60\u8f93\u5165\u4e00\u4e9b\u4fe1\u606f\uff0c\u8fde\u7eed\u6309\u56de\u8f66\u8df3\u8fc7\u5373\u53ef\u3002

2.\u5c06SSH\u5bc6\u94a5\u6dfb\u52a0\u5230ssh-agent\uff1a

\u5148\u542f\u52a8ssh\u4ee3\u7406\uff1a

eval \"$(ssh-agent -s)\"\n

\u7136\u540e\u6dfb\u52a0\u79c1\u94a5\uff1a

ssh-add ~/.ssh/id_ed25519\n

3.\u5411\u4f60\u7684\u8d26\u6237\u6dfb\u52a0\u65b0\u7684SSH\u5bc6\u94a5

\u73b0\u5728\uff0c.ssh/\u76ee\u5f55\u4e0b\u6709\u4e24\u4e2a\u6587\u4ef6\uff0c\u4ee5pub\u7ed3\u5c3e\u7684\u5c31\u662f\u516c\u94a5\uff0c\u9700\u8981\u4f60\u4e0a\u4f20\u81f3github\u670d\u52a1\u5668\u3002

cat ~/.ssh/id_ed25519.pub\n

\u590d\u5236\u6253\u5370\u7684\u5185\u5bb9\uff0c\u7136\u540e\u5728github\u4e2a\u4eba\u8d44\u6599\u9762\u70b9\u51fb\u8bbe\u7f6e\uff0c\u627e\u5230SSH and GPG keys\uff0c\u70b9\u51fbNew SSH key\uff0c\u6700\u540e\u7c98\u8d34\u590d\u5236\u7684\u516c\u94a5\u5373\u53ef\u3002

"},{"location":"tools/github/#_1","title":"\u7ba1\u7406\u8fdc\u7a0b\u4ed3\u5e93","text":"

\u8981\u6dfb\u52a0\u4e00\u4e2a\u8fdc\u7a0b\u4ed3\u5e93\u94fe\u63a5\uff0c\u4f7f\u7528git remote add \u547d\u4ee4\uff0c\u8be5\u547d\u4ee4\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a

  • \u8fdc\u7a0b\u4ed3\u5e93\u540d\uff0c\u6bd4\u5982origin
  • \u8fdc\u7a0b\u4ed3\u5e93\u5730\u5740\uff0c\u6bd4\u5982https://github.com/OWNER/REPOSITORY.git
git remote add origin https://github.com/OWNER/REPOSITORY.git\n

\u5728\u6dfb\u52a0\u5b8c\u8fdc\u7a0b\u4ed3\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528git remote -v\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u4f60\u5f53\u524d\u6dfb\u52a0\u7684\u8fdc\u7a0b\u4ed3\u5e93\uff1a

git remote -v\n# Verify new remote\n> origin  https://github.com/OWNER/REPOSITORY.git (fetch)\n> origin  https://github.com/OWNER/REPOSITORY.git (push)\n

\u5982\u679c\u8981\u66f4\u6539\u8fde\u63a5\u8fdc\u7a0b\u4ed3\u5e93\u7684\u65b9\u5f0f\uff0c\u6bd4\u5982\u4eceHTTPS\u6539\u4e3aSSH\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528git remote set-url\u547d\u4ee4\uff1a

git remote set-url origin git@github.com:OWNER/REPOSITORY.git\n

\u8fd9\u65f6\u8fdc\u7a0b\u4ed3\u5e93\u7684\u683c\u5f0f\u5e94\u8be5\u662f\uff1a

origin  git@github.com:OWNER/REPOSITORY.git (fetch)\norigin  git@github.com:OWNER/REPOSITORY.git (push)\n
"},{"location":"tools/github/#git","title":"git\u5e38\u7528\u547d\u4ee4","text":"

\u9879\u76ee\u521d\u59cb\u5316\uff1a

git init\n

\u521d\u59cb\u5316\u4e2a\u4eba\u4fe1\u606f\uff1a

git config --global user.name \"\u4f60\u7684\u540d\u5b57\"\ngit config --global user.email \"\u4f60\u7684\u90ae\u7bb1\"\n

\u5c06\u53d8\u52a8\u7684\u6587\u4ef6\u6dfb\u52a0\u5230\u6682\u5b58\u533a\uff1a

git add --all\n

\u63d0\u4ea4\u4ee3\u7801\uff1a

git commit -m \"\u63d0\u4ea4\u4fe1\u606f\"\n

\u63a8\u9001\u4ee3\u7801\uff1a

git push origin main\n

origin\u66ff\u6362\u4e3a\u8fdc\u7a0b\u4ed3\u5e93\u7684\u540d\u79f0\uff0cmain\u66ff\u6362\u4e3a\u63d0\u4ea4\u7684\u5206\u652f\u540d

\u62c9\u53d6\u4ee3\u7801\u5230\u672c\u5730\uff1a

git pull origin main\n

\u67e5\u770b\u5f53\u524dgit\u72b6\u6001\uff1a

git status\n

\u67e5\u770b\u63d0\u4ea4\u65e5\u5fd7\uff1a

git log\n

\u5207\u6362\u5206\u652f\uff1a

git check -b [\u65b0\u5206\u652f\u540d]\n

\u5408\u5e76\u4ee3\u7801\uff1a

git merge [\u53e6\u4e00\u4e2a\u5206\u652f\u540d]\n
"},{"location":"tools/markdown/","title":"Markdown","text":"

Markdown\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6807\u8bb0\u8bed\u8a00\uff0c\u6392\u7248\u8bed\u6cd5\u7b80\u6d01\uff0c\u8ba9\u4eba\u4eec\u66f4\u591a\u5730\u5173\u6ce8\u5185\u5bb9\u672c\u8eab\u800c\u975e\u6392\u7248\u3002\u5b83\u4f7f\u7528\u6613\u8bfb\u6613\u5199\u7684\u7eaf\u6587\u672c\u683c\u5f0f\u7f16\u5199\u6587\u6863\uff0c\u53ef\u4e0eHTML\u6df7\u7f16\uff0c\u53ef\u5bfc\u51faHTML\u3001PDF\u4ee5\u53ca\u672c\u8eab\u7684.md\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u56e0\u7b80\u6d01\u3001\u9ad8\u6548\u3001\u6613\u8bfb\u3001\u6613\u5199\uff0cMarkdown\u88ab\u5927\u91cf\u4f7f\u7528\uff0c\u5982Github\u3001Wikipedia\u3001\u7b80\u4e66\u7b49\u3002

\u53ef\u4ee5\u5148\u5728\u7ebf\u4f53\u9a8c\u4e00\u4e0bMarkdown\u5728\u7ebf\u7f16\u8f91\u5668\u3002

Markdown\u7684\u8bed\u6cd5\u5341\u5206\u7b80\u5355\uff0c\u534a\u5c0f\u65f6\u8db3\u4ee5\u638c\u63e1\u3002\u5b66\u5b8c\u4e4b\u540e\u53ef\u4ee5\u4e13\u5fc3\u4e8e\u5185\u5bb9\u8f93\u51fa\uff0c\u5927\u91cf\u63d0\u9ad8\u751f\u4ea7\u529b\uff0c\u975e\u5e38\u63a8\u8350\u3002

\u5b98\u65b9\u7f51\u7ad9\u7684\u5185\u5bb9\u5df2\u7ecf\u8db3\u591f\u8be6\u7ec6\uff0c\u8bf7\u76f4\u63a5\u67e5\u770b\u5b98\u65b9\u6587\u6863\u81ea\u5b66\uff1aMarkdown\u5b98\u65b9\u6559\u7a0b\u3002

"},{"location":"tools/vim/","title":"vim","text":"

\u8fd9\u4e0d\u662f\u65b0\u624b\u6559\u7a0b\uff0c\u53ea\u8bb0\u5f55\u4e00\u4e9b\u5e38\u7528\u64cd\u4f5c\u3002

vim\u547d\u4ee4\u53ef\u4ee5\u7531\u64cd\u4f5c\u7b26 + \u6b21\u6570 + \u52a8\u4f5c\u6765\u7ec4\u6210\uff0c\u6bd4\u5982d3w\u8868\u793a\u5220\u96643\u4e2a\u5355\u8bcd\u3002

u\uff1a\u64a4\u9500\u4fee\u6539

CTRL + R\uff1a\u6062\u590d\u4fee\u6539

"},{"location":"tools/vim/#_1","title":"\u79fb\u52a8","text":"

h, j, k, l\uff1a\u5de6\u4e0b\u4e0a\u53f3

e\uff1a\u8df3\u8f6c\u81f3\u4e0b\u4e00\u4e2a\u5355\u8bcd\u672b\u5c3e

b:\u8df3\u8f6c\u81f3\u4e0a\u4e00\u4e2a\u5355\u8bcd\u5f00\u5934

w\uff1a\u8df3\u8f6c\u81f3\u4e0b\u4e00\u4e2a\u5355\u8bcd\u5f00\u5934

$\uff1a\u8df3\u8f6c\u81f3\u884c\u672b

CTRL + O\uff1a\u8fd4\u56de\u4e0a\u6b21\u4f4d\u7f6e

CTRL + I\uff1a\u524d\u5f80\u4e0b\u6b21\u4f4d\u7f6e

0\uff1a\u8df3\u8f6c\u81f3\u884c\u9996

nG\uff1a\u8df3\u8f6c\u81f3\u7b2cn\u884c

GG\uff1a\u8df3\u8f6c\u81f3\u6587\u4ef6\u672b\u5c3e

gg\uff1a\u8df3\u8f6c\u81f3\u6587\u4ef6\u5f00\u5934

"},{"location":"tools/vim/#_2","title":"\u63d2\u5165","text":"

a\uff1a\u5149\u6807\u540e\u63d2\u5165

A\uff1a\u884c\u5c3e\u63d2\u5165

i\uff1a\u5149\u6807\u524d\u63d2\u5165

I\uff1a\u884c\u9996\u63d2\u5165

o\uff1a\u4e0b\u4e00\u884c\u63d2\u5165

O\uff1a\u4e0a\u4e00\u884c\u63d2\u5165

"},{"location":"tools/vim/#_3","title":"\u5220\u9664","text":"

x\uff1a\u5220\u9664\u5149\u6807\u5904\u5b57\u7b26

dd\uff1a\u5220\u9664\u6574\u884c

D\uff1a\u5220\u9664\u81f3\u884c\u672b

dw\uff1a\u4ece\u5f53\u524d\u5149\u6807\u4f4d\u7f6e\uff0c\u4e00\u76f4\u5220\u9664\u81f3\u4e0b\u4e00\u4e2a\u5355\u8bcd\u5f00\u5934

d$\uff1a\u4ece\u5f53\u524d\u5149\u6807\u4f4d\u7f6e\uff0c\u4e00\u76f4\u5220\u9664\u81f3\u884c\u672b

"},{"location":"tools/vim/#_4","title":"\u590d\u5236\u4e0e\u7c98\u8d34","text":"

p\uff1a\u5728\u5149\u6807\u4e0b\u4e00\u884c\u4f4d\u7f6e\u590d\u5236

y\uff1a\u590d\u5236\u9009\u4e2d\u5185\u5bb9\uff0c\u9700\u8981\u914d\u5408visual mode

yy\uff1a\u590d\u5236\u5f53\u524d\u884c

yw\uff1a\u590d\u5236\u4e00\u4e2a\u5355\u8bcd

"},{"location":"tools/vim/#_5","title":"\u4fee\u6539\u4e0e\u66ff\u6362","text":"

rx\uff1a\u5c06\u5f53\u524d\u5149\u6807\u5904\u5b57\u7b26\u4fee\u6539\u4e3ax

ce\uff1a\u4fee\u6539\u5355\u8bcd

:s/old/new/g\uff1a\u5c06\u5f53\u524d\u884c\u7684'old'\u5168\u90e8\u6539\u4e3a'new'

:%s/old/new/g\uff1a\u5c06\u5f53\u524d\u6587\u4ef6\u7684'old'\u5168\u90e8\u6539\u4e3a'new'

"},{"location":"tools/vim/#_6","title":"\u67e5\u627e","text":"

\u6309/\u8fdb\u5165\u67e5\u627e\u6a21\u5f0f\uff0c\u8f93\u5165\u4f60\u8981\u641c\u5bfb\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\uff1a

n\uff1a\u67e5\u627e\u4e0b\u4e00\u4e2a

N\uff1a\u67e5\u627e\u4e0a\u4e00\u4e2a

%\uff1a\u641c\u5bfb\u4e0b\u4e00\u4e2a\u5339\u914d\u7684), ], }

"}]} \ No newline at end of file diff --git a/sitemap.xml b/sitemap.xml index 7f2b9329..61ee5ebb 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -2,277 +2,277 @@ https://tomsjtu.github.io/notes/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/arm/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/arm/aarch64/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/arm/barrier/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/arm/instructions/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/cloud/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/cloud/docker/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/blkdev/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/bluetooth/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/chardev/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/device_model/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/dma/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/dts/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/ethernet/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/gpio/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/i2c/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/input/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/mmc/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/netdev/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/pci/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/pinctrl/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/platform/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/spi/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/uart/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/usb/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/drivers/wifi/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/ebpf/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/block/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/interrupt/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/mm/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/network/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/others/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/sched/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/synch/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/vfs/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/kernel/vma/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/optimization/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/optimization/cpu/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/optimization/disk/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/optimization/memory/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/linux/optimization/network/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/others/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/others/ld/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/pl/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/rtos/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/rtos/freertos/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/rtos/freertos/CMSIS/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/rtos/rt-thread/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/tools/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/tools/github/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/tools/markdown/ - 2024-02-08 + 2024-02-12 daily https://tomsjtu.github.io/notes/tools/vim/ - 2024-02-08 + 2024-02-12 daily \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz index 0c6f0d20..2aa03ce2 100644 Binary files a/sitemap.xml.gz and b/sitemap.xml.gz differ