内核代码阅读(15) - 中断请求队列的初始化

简介: 中断请求队列的初始化

中断请求队列的初始化

数据结构

irq通道的控制

struct hw_interrupt_type {
        const char * typename;
        unsigned int (*startup)(unsigned int irq);
        void (*shutdown)(unsigned int irq);
        void (*enable)(unsigned int irq);
        void (*disable)(unsigned int irq);
        void (*ack)(unsigned int irq);
        void (*end)(unsigned int irq);
        void (*set_affinity)(unsigned int irq, unsigned long mask);
    };
    typedef struct hw_interrupt_type  hw_irq_controller;
1) startup
   启动通道
2) ack
   响应

irq请求队列头部数据结构

typedef struct {
        unsigned int status;                /* IRQ status */
        hw_irq_controller *handler;
        struct irqaction *action;        /* IRQ action list */
        unsigned int depth;                /* nested irq disables */
        spinlock_t lock;
    } ____cacheline_aligned irq_desc_t;
    extern irq_desc_t irq_desc [NR_IRQS];
1) handler 就是通道级别的操作函数。
2) action 是这个队列的头部,类型是 irqaction。

irqaction结构

struct irqaction {
        void (*handler)(int, void *, struct pt_regs *);
        unsigned long flags;
        unsigned long mask;
        const char *name;
        void *dev_id;
        struct irqaction *next;
    };
1) handler
   就是中断服务程序。
2) dev_id
   是设备号,因为这个通道是共享的,所以要依赖这个dev_id区分到底是谁产生的中断。
3) next 链表。

队列头irq_desc的初始化

在 init_IRQ -> init_ISA_irqs中
void __init init_ISA_irqs (void)
    {
        int i;
        init_8259A(0);
        for (i = 0; i < NR_IRQS; i++) {
                irq_desc[i].status = IRQ_DISABLED;
                irq_desc[i].action = 0;
                irq_desc[i].depth = 1;
                if (i < 16) {
                        irq_desc[i].handler = &i8259A_irq_type;
                } else {
                        irq_desc[i].handler = &no_irq_type;
                }
        }
    }
1) irq_desc[i].handler = &i8259A_irq_type;
   设置队列头 irq_desc中的handler为8259的处理函数。

添加一个 interrupt line 到系统中(外设主动调用把自己的中断服务程序注册到相对的请求队列中)

int request_irq(unsigned int irq, 
                void (*handler)(int, void *, struct pt_regs *),
                unsigned long irqflags, 
                const char * devname,
                void *dev_id)
    {
        int retval;
        struct irqaction * action;
    #if 1
        if (irqflags & SA_SHIRQ) {
                if (!dev_id)
                        printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
        }
    #endif
        if (irq >= NR_IRQS)
                return -EINVAL;
        if (!handler)
                return -EINVAL;
        action = (struct irqaction *)
                        kmalloc(sizeof(struct irqaction), GFP_KERNEL);
        if (!action)
                return -ENOMEM;
        action->handler = handler;
        action->flags = irqflags;
        action->mask = 0;
        action->name = devname;
        action->next = NULL;
        action->dev_id = dev_id;
        retval = setup_irq(irq, action);
        if (retval)
                kfree(action);
        return retval;
    }
1) if (!dev_id)
   如果没有dev_id则报错。
2) kmalloc(sizeof(struct irqaction), GFP_KERNEL);
   从slab分配一个 irqaction
3) action->handler = handler;
   设置handler
4) action->dev_id = dev_id;
   设置dev_id
5) printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
   注意这个printk的技巧:打印出调用者的指令地址。
   &irq取出第一个参数在栈上的地址,
   (&irq)[-1] 在栈上往上找一个,就是函数的返回地址。也就是调用者的地址。

setup_irq 链入请求队列

int setup_irq(unsigned int irq, struct irqaction * new)
    {
        int shared = 0;
        unsigned long flags;
        struct irqaction *old, **p;
        irq_desc_t *desc = irq_desc + irq;
        if (new->flags & SA_SAMPLE_RANDOM) {
                rand_initialize_irq(irq);
        }
        spin_lock_irqsave(&desc->lock,flags);
        p = &desc->action;
        if ((old = *p) != NULL) {
                if (!(old->flags & new->flags & SA_SHIRQ)) {
                        spin_unlock_irqrestore(&desc->lock,flags);
                        return -EBUSY;
                }
                do {
                        p = &old->next;
                        old = *p;
                } while (old);
                shared = 1;
        }
        *p = new;
        if (!shared) {
                desc->depth = 0;
                desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
                desc->handler->startup(irq);
        }
        spin_unlock_irqrestore(&desc->lock,flags);
        register_irq_proc(irq);
        return 0;
    }
1) rand_initialize_irq(irq);
   借助外设中断的时序来增进随机数。
2) do {
            p = &old->next;
            old = *p;
  } while (old);
  找到队列尾端,并插入。
相关文章
|
1月前
|
算法 数据安全/隐私保护 C++
【C/C++ 编程题 03】用两个栈实现一个队列的功能
【C/C++ 编程题 03】用两个栈实现一个队列的功能
16 0
|
1月前
|
算法 编译器 C语言
【C/C++ 编程题 02】用两个栈实现一个队列的功能
【C/C++ 编程题 02】用两个栈实现一个队列的功能
19 0
|
8月前
|
算法 C语言
如何在C语言中实现队列和堆栈的动态扩容
队列和堆栈是在C语言中常用的数据结构,它们可以帮助我们高效地处理数据。然而,在实际编程中,我们经常会遇到数据量超过容量限制的情况。这时,我们需要实现队列和堆栈的动态扩容,以满足实际需求。
66 1
|
4月前
|
存储 调度
FreeRTOS深入教程(队列内部机制和源码分析)
FreeRTOS深入教程(队列内部机制和源码分析)
63 0
|
4月前
|
监控 安全 API
6.9 Windows驱动开发:内核枚举进线程ObCall回调
在笔者上一篇文章`《内核枚举Registry注册表回调》`中我们通过特征码定位实现了对注册表回调的枚举,本篇文章`LyShark`将教大家如何枚举系统中的`ProcessObCall`进程回调以及`ThreadObCall`线程回调,之所以放在一起来讲解是因为这两中回调在枚举是都需要使用通用结构体`_OB_CALLBACK`以及`_OBJECT_TYPE`所以放在一起来讲解最好不过。
45 1
6.9 Windows驱动开发:内核枚举进线程ObCall回调
|
10月前
|
存储 编译器
从汇编代码探究函数栈帧的创建和销毁的底层原理(二)
从汇编代码探究函数栈帧的创建和销毁的底层原理
|
10月前
|
C语言 C++
从汇编代码探究函数栈帧的创建和销毁的底层原理(一)
从汇编代码探究函数栈帧的创建和销毁的底层原理
【函数栈帧的创建和销毁】 -- 神仙级别底层原理,你学会了吗?(2)
1.函数的调用方式 相信你对调用函数一点都不陌生,但是在调用函数的过程中,却存在着很多你无法见到的东西,这是底层信息,想要理解透彻,就得深入底层去观察。 本文以一个最简单的加法函数为例,深入讲解内存空间中的每一条指令。
|
11月前
第十二章队列模拟注意事项
第十二章队列模拟注意事项
40 0
|
11月前
|
API
驱动开发:内核远程堆分配与销毁
在开始学习内核内存读写篇之前,我们先来实现一个简单的内存分配销毁堆的功能,在内核空间内用户依然可以动态的申请与销毁一段可控的堆空间,一般而言内核中提供了`ZwAllocateVirtualMemory`这个函数用于专门分配虚拟空间,而与之相对应的则是`ZwFreeVirtualMemory`此函数则用于销毁堆内存,当我们需要分配内核空间时往往需要切换到对端进程栈上再进行操作,接下来`LyShark`将从API开始介绍如何运用这两个函数实现内存分配与使用,并以此来作为驱动读写篇的入门知识。
209 0