Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1847749
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2388
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-21 22:26
个人简介

90后空巢老码农

文章分类

全部博文(184)

文章存档

2021年(26)

2020年(56)

2019年(54)

2018年(47)

2017年(1)

我的朋友

分类: LINUX

2020-11-10 15:34:04

中断,在linux内核里面是异步的,所以,linux在内核里面有单独的两个上下文来管理,分别是中断上下文和软中断上下文。
首先,中断产生于外部硬件,通过中断控制器的判定,发往cpu(这时会被中断控制器转换成中断向量)。这里需要注意的是,系统在初始化的时候,会设置前32个中断向量的中断函数和系统调用中断的处理函数,这个对应关系存储在一个中断向量表当中(IDT tables)

点击(此处)折叠或打开

  1. /*
  2.  * Linux IRQ vector layout.
  3.  *
  4.  * There are 256 IDT entries (per CPU - each entry is 8 bytes) which can
  5.  * be defined by Linux. They are used as a jump table by the CPU when a
  6.  * given vector is triggered - by a CPU-external, CPU-internal or
  7.  * software-triggered event.
  8.  *
  9.  * Linux sets the kernel code address each entry jumps to early during
  10.  * bootup, and never changes them. This is the general layout of the
  11.  * IDT entries:
  12.  *
  13.  * Vectors 0 ... 31 : system traps and exceptions - hardcoded events
  14.  * Vectors 32 ... 127 : device interrupts
  15.  * Vector 128 : legacy int80 syscall interface
  16.  * Vectors 129 ... INVALIDATE_TLB_VECTOR_START-1 except 204 : device interrupts
  17.  * Vectors INVALIDATE_TLB_VECTOR_START ... 255 : special interrupts
  18.  *
  19.  * 64-bit x86 has per CPU IDT tables, 32-bit has one shared IDT table.
  20.  *
  21.  * This file enumerates the exact layout of them:
  22.  */
剩余没有用到的中断,都会根据其偏移量,来进行对应的设置:

点击(此处)折叠或打开

  1. /**
  2.  * idt_setup_apic_and_irq_gates - Setup APIC/SMP and normal interrupt gates
  3.  */
  4. void __init idt_setup_apic_and_irq_gates(void)
  5. {
  6.     int i = FIRST_EXTERNAL_VECTOR;
  7.     void *entry;

  8.     idt_setup_from_table(idt_table, apic_idts, ARRAY_SIZE(apic_idts), true);

  9.     for_each_clear_bit_from(i, used_vectors, FIRST_SYSTEM_VECTOR) {
  10.         entry = irq_entries_start + 8 * (i - FIRST_EXTERNAL_VECTOR);
  11.         set_intr_gate(i, entry);
  12.     }

  13.     for_each_clear_bit_from(i, used_vectors, NR_VECTORS) {
  14. #ifdef CONFIG_X86_LOCAL_APIC
  15.         set_bit(i, used_vectors);
  16.         set_intr_gate(i, spurious_interrupt);
  17. #else
  18.         entry = irq_entries_start + 8 * (i - FIRST_EXTERNAL_VECTOR);
  19.         set_intr_gate(i, entry);
  20. #endif
  21.     }
  22. }
也就是说,每一个初始化没有被设置的中断向量,会根据其偏移初始外部中断号的多少,去irq_entries_start这个起始地址,找到相应的偏移函数,对该中断向量进行设置,irq_entries_start定义如下:

点击(此处)折叠或打开

  1. /*
  2.  * Build the entry stubs with some assembler magic.
  3.  * We pack 1 stub into every 8-byte block.
  4.  */
  5.     .align 8
  6. ENTRY(irq_entries_start)
  7.     vector=FIRST_EXTERNAL_VECTOR
  8.     .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  9.     pushl $(~vector+0x80) /* Note: always in signed byte range */
  10.     vector=vector+1
  11.     jmp common_interrupt
  12.     .align 8
  13.     .endr
  14. END(irq_entries_start)

  15. /*
  16.  * the CPU automatically disables interrupts when executing an IRQ vector,
  17.  * so IRQ-flags tracing has to follow that:
  18.  */
  19.     .p2align CONFIG_X86_L1_CACHE_SHIFT
  20. common_interrupt:
  21.     ASM_CLAC
  22.     addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
  23.     SAVE_ALL
  24.     ENCODE_FRAME_POINTER
  25.     TRACE_IRQS_OFF
  26.     movl %esp, %eax
  27.     call do_IRQ
  28.     jmp ret_from_intr
  29. ENDPROC(common_interrupt)
所以,最终从FIRST_EXTERNAL_VECTOR 到FIRST_SYSTEM_VECTOR的中断向量,都会调用do_IRQ这个函数

由于不同CPU中相同的中断向量对应的虚拟中断号irq需要不同(这是irq这一虚拟中断层出现的原因),所以,需要每个CPU在系统初始化的时候,将虚拟中断号irq与其本地的中断向量进行一个映射:

点击(此处)折叠或打开

  1. DEFINE_PER_CPU(vector_irq_t, vector_irq) = {
  2.     [0 ... NR_VECTORS - 1] = VECTOR_UNUSED,
  3. };
然后,cpu会根据中断向量携带的相关信息,中断号,设备号等,先找到对应的整个内核都可以看得到的irq号(这部分映射主要是通过硬件传过来的硬件中断以及事先建立好的中断向量与irq号映射关系获得)。获得irq号之后,就会有一个与之对应的irq_desc结构,如下:


点击(此处)折叠或打开

  1. struct irq_desc {
  2.     struct irq_common_data irq_common_data;
  3.     struct irq_data irq_data;
  4.     unsigned int __percpu *kstat_irqs;
  5.     irq_flow_handler_t handle_irq;
  6. ...
  7.     struct irqaction *action; /* IRQ action list */
  8.     unsigned int status_use_accessors;
  9.     unsigned int core_internal_state__do_not_mess_with_it;
  10.     unsigned int depth; /* nested irq disables */
  11.     unsigned int wake_depth; /* nested wake enables */
  12.     unsigned int tot_count;
  13.     unsigned int irq_count; /* For detecting broken IRQs */
  14.     unsigned long last_unhandled; /* Aging timer for unhandled count */
  15.     unsigned int irqs_unhandled;
  16.     atomic_t threads_handled;
  17.     int threads_handled_last;
  18.     raw_spinlock_t lock;
  19.     struct cpumask *percpu_enabled;
  20.     const struct cpumask *percpu_affinity;

  21. ...
  22.     struct mutex request_mutex;
  23.     int parent_irq;
  24.     struct module *owner;
  25.     const char *name;
  26. } ____cacheline_internodealigned_in_smp;
这里的action是一串链表,表示当前irq号对应的一些操作,这时候就会遍历这个链表找到对应的硬件设备信息来执行相关的函数了。这些相关的函数,其实就是在驱动程序向系统注册相关irq的时候挂在irqaction链表后面的。
中断处理一般会分成上半部分和下半部分。其中上半部分处理时间紧急的工作,在这期间,要关闭外部硬件中断的响应;下半部分是处理时间不是那么紧急的工作,基本上实现机制都是基于软中断(在硬件中断退出会调用,ksoftirqd守护进程也会执行),软中断的话实现是在内核里面写死的,基于它的有网络收发,计时器,tasklet等。另外,内核当中有关于软中断运行时间(2ms)以及运行次数的限制(最多10次循环处理pending软中断),避免软中断一直执行,占用CPU





阅读(11585) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~