• linux中断响应和处理过程


    linux中断响应和处理过程:

    首先中断属于异常的一种。异常,就是可以打断CPU正常运行流程的一些事情,比如说外部中断,未定义的指定,试图修改只读数据,执行SWI指定(software interrupt instructin,软件中断指令,比如说上层调用sys_read,sys_write就会产生swi)等。

    内核启动时在start_kernel函数(init/main.c)中调用trap_init ,  init_IRQ两个函数来设置异常的处理函数。

    trap_init函数(arch/arm/kernel/traps.c

    void_init trap_init(void)

    {

    ......

    memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);

    memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);

    .......

    }

    上面两条定义的是异常向量的存放地方,:__stubs_start~~~~~ __stubs_end之间就是异常向量.

    接下来 我们看异常向量之间的定义:(arch/arm/kernel/entry-armv.s)

    .equ stubs_offset, __vectors_start + 0x200 - __stubs_start

        .globl __vectors_start

    __vectors_start:

     ARM( swi SYS_ERROR0 )      //复位时.CPU交执行这条指令

     THUMB( svc #0 )

     THUMB( nop )

    W(b) vector_und + stubs_offset   //未定义异常时,CPU将执行这条跳转指令

    W(ldr) pc, .LCvswi + stubs_offset   //swi异常

    W(b) vector_pabt + stubs_offset   //指令预取中止

    W(b) vector_dabt + stubs_offset   //数据访问中止

    W(b) vector_addrexcptn + stubs_offset  //没有用到

    W(b) vector_irq + stubs_offset         //irq中断

    W(b) vector_fiq + stubs_offset         //fig中断 (快速中断)

    .globl __vectors_end

    __vectors_end:

    各种异常的处理函数可以分为五类,分别分布在下面不同的文件中:

    1arch/arm/kernel/traps.c

       处理未定义指令异常,总入口函数为do_undefinstr

    2arch/arm/mm/fault.c

       与内存访问相关的异常,总入口函数为do_DataAbort, do_PretftchAbort

    3. arch/arm/mm/irq.c

       中断处理函数在这个文件中定义,总入口函数为asm_do_IRQ

    4. arch/arm/kernel/call.s

      swi异常处理  比如说:sys_read,  sys_open.

    5. 没有使用的异常

    除了IRQ中断外(FIG中断linux一般不使用),所有的异常内核都定义了细致而完备的处理函数.  所以我们这里关心的也只是上面红色部分,:IRQ中断.

    Init_IRQ函数(arch/arm/kernel/irq.c),被用来初使化中断的处理框架,设置各种中断的默认处理函数.

    Linux内核将所有中断统一编号,使用irq_desc结构来描述中断:每个数组项对应一个中断(也可能是一组中断,它们使用共同的中断号),里面记录了中断的名称,中断状态,中断标记,并提供硬件访问函数(清除,屏蔽,使能中断),提供了这个中断的处理函数的入口,通过它可以调用用户注册的中断处理函数

    include/linux/irq.h

    {.........

    irq_flow_handler_t handle_irq;   //当前的中断处理函数入口

    struct irq_chip *chip;       //底层的硬件访问

    ..........

    struct irqaction *action; //用户提供的中断处理函数链表

    unsigned int status; //IRQ状态

    ...........

    const char *name;     //中断名称

    } ____cacheline_internodealigned_in_smp;

    Handle_irq是这个或者这组中断的处理函数入口.

    当中断发生时总中断入口函数asm_do_IRQ将根据中断号调用相应irq_desc数组中的handle_irq函数,handle_irq使用chip结构中的函数来清除,屏蔽,使用中断,还会一一调用用户在action链表中注册的中断处理函数.

    Struct irq_chip{

    const char *name;           //启动中断,如果不设置,缺省为"enable"

    unsigned int (*startup)(unsigned int irq); //启动中断,如果不设置,缺省为"enable"

    void (*shutdown)(unsigned int irq);  //关闭中断,如果不设置,缺省为"disable"

    void (*enable)(unsigned int irq);    //使能中断,如果不设置,缺省为unmask

    void (*disable)(unsigned int irq);   //禁止中断如果不设置,缺省为"mask"

    void  (*ack)(unsigned int irq); //响应中断,通常是清除当前中断使得可以接收下一个中断

    void (*mask)(unsigned int irq); //屏蔽中断源

    void (*mask_ack)(unsigned int irq); //屏蔽和响应中断

    void (*unmask)(unsigned int irq); //开启中断源

    ..........

    }

    struct irqaction *action; 结构类型在include/linux/interrupt..h中定义.

    用户注册的每一个中断处理函数都用一个irqaction结构表示,一个中断(比如共享中断)可以有多个处理函数,它们的irqacion结构链接成一个链表,action为表头.

    struct irqaction {

    irq_handler_t handler;   //用户注册的中断处理函数

    unsigned long flags;//中断标志,比如是否为共享中断,电平触发还是边沿触发

    const char *name;   //用户注册的中断名字

    void *dev_id;       //用户供给的handle参数,还可以区分共享中断

    struct irqaction *next;

    int irq;            //中断号 

    struct proc_dir_entry *dir;

    irq_handler_t thread_fn;

    struct task_struct *thread;

    unsigned long thread_flags;

    };

    Irq_desc结构数组中:

    "irq_flow_handler_thandle_irq"   ,   "struct irq_chip *chip "   ,   "struct ,irqaction *action"这三种数据结构构成了中断处理体系结构.

     

    图片

     

     

    很明显,中断需要用户处理的只有最后一步,就是用户的action中断处理函数.

    所以我们需要告诉内核我们相应的中断处理函数在哪里,中断注册:reguest_irq, 相对应的中断卸载:  free_irq.

    int request_irq(unsigned int irq, //中申请中断的中断号,可以根据不用的引脚到irqs.h里面查找
            irqreturn_t (*handler)(int, void *, struct pt_regs *),
    //用户写的中断处理函数
            unsigned long irqflags,
    //中断触发方式(高电平触发,低电平触发,上升沿,下降沿等)
            const char *devname,
    //中断名字(自己取名)
            void *dev_id);
         //dev_id(共享中断时,用于识别倒里是哪个硬件产生的中断)  

                       //同一中断的不同处理函数必须用dev_id来区分

    //共享中断之间既可使用同一中断处理函数,也可使用不同中断处理函数,都需要dev_id区分.

    中断注册做了三件事:

    1.提供用户action中断处理函数链接

    2.中断触发方式是什么

    3.中断使能

    Void free_irq(unsigned int irq, //中断号与注册时对应

    void *dev_id)             //共享中断时用于区分不同硬件与注册时对应

    中断卸载和注册相反:

    1.根据中断号irq,dev_idaction链表中找到表项,将它删除

    2.如果它是唯一表项,还要调用IRQ_DESC[IRQ].CHIP->SHUTDOWNDESC[IRQ].CHIP->DISABLE来关闭中断.

    所以很显然,用户要自己写一个中断程序,只需要实现三步,

    1.向内核申请注册中断

    2.实现用户中断处理函数

    3.在不需要该中断的时候卸载中断

    附上一个例子:按键的中断程序

    驱动程序:

    #include <linux/module.h>

    #include <linux/kernel.h>

    #include <linux/fs.h>

    #include <linux/init.h>

    #include <linux/delay.h>

    #include <asm/irq.h>

    #include <linux/interrupt.h>

    #include <asm/uaccess.h>

    #include <asm/arch/regs-gpio.h>

    #include <asm/hardware.h>

    #define DEVICE_NAME     "buttons"   /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */

    #define BUTTON_MAJOR    232         /* 主设备号 */

    struct button_irq_desc {

        int irq;

        unsigned long flags;

        char *name;

    };

    /* 用来指定按键所用的外部中断引脚及中断触发方式名字 */

    static struct button_irq_desc button_irqs [] = {

        {IRQ_EINT19, IRQF_TRIGGER_FALLING, "KEY1"}, /* K1 */

        {IRQ_EINT11, IRQF_TRIGGER_FALLING, "KEY2"}, /* K2 */

        {IRQ_EINT2,  IRQF_TRIGGER_FALLING, "KEY3"}, /* K3 */

        {IRQ_EINT0,  IRQF_TRIGGER_FALLING, "KEY4"}, /* K4 */

    };

    /* 按键被按下的次数(准确地说,是发生中断的次数) */

    static volatile int press_cnt [] = {0, 0, 0, 0};

    /* 等待队列

     * 当没有按键被按下时,如果有进程调用s3c24xx_buttons_read函数,

     * 它将休眠

     */

    static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

    /* 中断事件标志中断服务程序将它置1s3c24xx_buttons_read将它清0 */

    static volatile int ev_press = 0;

    static irqreturn_t buttons_interrupt(int irq, void *dev_id)

    {

        volatile int *press_cnt = (volatile int *)dev_id;

        

        *press_cnt = *press_cnt + 1; /* 按键计数加1 */

        ev_press = 1;                /* 表示中断发生了 */

        wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */

        

        return IRQ_RETVAL(IRQ_HANDLED);

    }

    /* 应用程序对设备文件/dev/buttons执行open(...)时,

     * 就会调用s3c24xx_buttons_open函数

     */

    static int s3c24xx_buttons_open(struct inode *inode, struct file *file)

    {

        int i;

        int err;

        

        for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {

            // 注册中断处理函数

            err = request_irq(button_irqs[i].irq, buttons_interrupt, button_irqs[i].flags, 

                              button_irqs[i].name, (void *)&press_cnt[i]);

            if (err)

                break;

        }

        if (err) {

            // 释放已经注册的中断

            i--;

            for (; i >= 0; i--)

                free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);

            return -EBUSY;

        }

        

        return 0;

    }

    /* 应用程序对设备文件/dev/buttons执行close(...)时,

     * 就会调用s3c24xx_buttons_close函数

     */

    static int s3c24xx_buttons_close(struct inode *inode, struct file *file)

    {

        int i;

        

        for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {

            // 释放已经注册的中断

            free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);

        }

        return 0;

    }

    /* 应用程序对设备文件/dev/buttons执行read(...)时,

     * 就会调用s3c24xx_buttons_read函数

     */

    static int s3c24xx_buttons_read(struct file *filp, char __user *buff, 

                                             size_t count, loff_t *offp)

    {

        unsigned long err;

        

        /* 如果ev_press等于0,休眠 */

        wait_event_interruptible(button_waitq, ev_press);

        /* 执行到这里时,ev_press等于1,将它清0 */

        ev_press = 0;

        /* 将按键状态复制给用户,并清0 */

        err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));

        memset((void *)press_cnt, 0, sizeof(press_cnt));

        return err ? -EFAULT : 0;

    }

    /* 这个结构是字符设备驱动程序的核心

     * 当应用程序操作设备文件时所调用的openreadwrite等函数,

     * 最终会调用这个结构中的对应函数

     */

    static struct file_operations s3c24xx_buttons_fops = {

        .owner   =   THIS_MODULE,    /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */

        .open    =   s3c24xx_buttons_open,

        .release =   s3c24xx_buttons_close, 

        .read    =   s3c24xx_buttons_read,

    };

    /*

     * 执行“insmod s3c24xx_buttons.ko”命令时就会调用这个函数

     */

    static int __init s3c24xx_buttons_init(void)

    {

        int ret;

        /* 注册字符设备驱动程序

         * 参数为主设备号、设备名字、file_operations结构;

         * 这样,主设备号就和具体的file_operations结构联系起来了,

         * 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数

         * BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号

         */

        ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);

        if (ret < 0) {

          printk(DEVICE_NAME " can't register major number ");

          return ret;

        }

        

        printk(DEVICE_NAME " initialized ");

        return 0;

    }

    /*

     * 执行”rmmod s3c24xx_buttons.ko”命令时就会调用这个函数 

     */

    static void __exit s3c24xx_buttons_exit(void)

    {

        /* 卸载驱动程序 */

        unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);

    }

    /* 这两行指定驱动程序的初始化函数和卸载函数 */

    module_init(s3c24xx_buttons_init);

    module_exit(s3c24xx_buttons_exit);

    /* 描述驱动程序的一些信息,不是必须的 */

    MODULE_AUTHOR("http://www.100ask.net");             // 驱动程序的作者

    MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");   // 一些描述信息

    MODULE_LICENSE("GPL");                              // 遵循的协议///

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    应用调用程序:

    #include <stdio.h>

    #include <stdlib.h>

    #include <unistd.h>

    #include <sys/ioctl.h>

    int main(int argc, char **argv)

    {

        int i;

        int ret;

        int fd;

        int press_cnt[4];

        

        fd = open("/dev/buttons", 0);  // 打开设备

        if (fd < 0) {

            printf("Can't open /dev/buttons ");

            return -1;

        }

        // 这是个无限循环,进程有可能在read函数中休眠,当有按键被按下时,它才返回

        while (1) {

            // 读出按键被按下的次数

            ret = read(fd, press_cnt, sizeof(press_cnt));

            if (ret < 0) {

                printf("read err! ");

                continue;

            } 

            for (i = 0; i < sizeof(press_cnt)/sizeof(press_cnt[0]); i++) {

                // 如果被按下的次数不为0,打印出来

                if (press_cnt[i])

                    printf("K%d has been pressed %d times! ", i+1, press_cnt[i]);

            }

        }

        

        close(fd);

        return 0;    

    }

     





  • 相关阅读:
    Hadoop跳过回收站删除HDFS文件
    hadoop安全模式解除方法和为什么会安全模式
    从业18年,我总结了9个最有价值的经验
    mysql复制整个数据库及数据
    Oozie调度原理(queueName与launcher_queName详解)
    Hive升级参考
    Hive表设计压缩问题
    sql中join的on和where操作引发的谓词下推优化
    将毫秒转换成天小时分钟
    数据库优化之一:通过修改postgresql的Planner Method Configuration更改 查询计划优化慢sql
  • 原文地址:https://www.cnblogs.com/superit/p/3838687.html
Copyright © 2020-2023  润新知