ARM Linux 中斷分析

admin @ 2014-03-25 , reply:0

    ARM體系結構中,把複位、中斷、快速中斷等都看作‘異常’,當這些‘異常’發生時,CPU會到固定地址處去找指令,他們對應的地址如下:

地址 異常類型 進入時的工作模式
0x00000000 Reset Supervisor
0x00000004 Und Undefined
0x00000008 Soft interupt Supervisor
0x0000000c  Abort(prefetch)  Abort
0x00000010 Abort(data) Abort
0x00000014 Reserved  Reserved
0x00000018 IRQ IRQ
0x0000001c FIQ FIQ

    首先要明確的一點就是,無論內存地址空間是如何映射的,以上這些地址都不會變,比如當有快速中斷髮生時,ARM將鐵定到0X0000001C這個地址處取指令。這也是BOOTLOADER把操作系統引導以後,內存必須重映射的原因!否則操作系統不能真正接管整套系統!
    LINUX啟動以後要初始化這些區域,初始化代碼在main.c中的start_kernel()中,具體是調用函數trap_ini()來實現的。如下面所示(具體可參照entry-armv.S):

.LCvectors:    swi  SYS_ERROR0
             b      __real_stubs_start + (vector_undefinstr - __stubs_start)
           ldr     pc, __real_stubs_start + (.LCvswi - __stubs_start)
             b      __real_stubs_start + (vector_prefetch - __stubs_start)
             b      __real_stubs_start + (vector_data - __stubs_start)
             b      __real_stubs_start + (vector_addrexcptn - __stubs_start)
             b      __real_stubs_start + (vector_IRQ - __stubs_start)
             b      __real_stubs_start + (vector_FIQ - __stubs_start)

ENTRY(__trap_init)
           stmfd         sp!, {r4 - r6, lr}
           adr    r1, .LCvectors                        @ set up the vectors
           ldmia r1, {r1, r2, r3, r4, r5, r6, ip, lr}
           stmia r0, {r1, r2, r3, r4, r5, r6, ip, lr}
           add    r2, r0, #0x200
           adr    r0, __stubs_start          @ copy stubs to 0x200
           adr    r1, __stubs_end
1:                 ldr     r3, [r0], #4
           str     r3, [r2], #4
           cmp  r0, r1
           blt     1b
           LOADREGS(fd, sp!, {r4 - r6, pc}) 

以上可以看出這個函數初始化了中斷向量,實際上把相應的跳轉指令拷貝到了對應的地址。
當發生中斷時,不管是從用戶模式還是管理模式調用的,最終都要調用do_IRQ():

__irq_usr:    sub    sp, sp, #S_FRAME_SIZE
           stmia sp, {r0 - r12}                         @ save r0 - r12
           ldr     r4, .LCirq
           add    r8, sp, #S_PC
           ldmia r4, {r5 - r7}                           @ get saved PC, SPSR
           stmia r8, {r5 - r7}                           @ save pc, psr, old_r0
           stmdb         r8, {sp, lr}^
           alignment_trap r4, r7, __temp_irq
           zero_fp
1:                 get_irqnr_and_base r0, r6, r5, lr
           movne        r1, sp
           adrsvc        ne, lr, 1b
           @
           @ routine called with r0 = irq number, r1 = struct pt_regs *
           @
           bne    do_IRQ    @ 調用do_IRQ來實現具體的中斷處理
           mov  why, #0
           get_current_task tsk
           b       ret_to_user 

對於以上代碼,在很多文章中都有過分析,這裡不再贅述。 

    Linux每個中斷通過一個結構irqdesc來描述,各中斷的信息都在這個結構中得以體現:

struct irqdesc {
unsigned int         nomask   : 1;            /* IRQ does not mask in IRQ   */
unsigned int         enabled  : 1;              /* IRQ is currently enabled   */
unsigned int         triggered: 1;                 /* IRQ has occurred           */
unsigned int         probing  : 1;              /* IRQ in use for a probe     */
unsigned int         probe_ok : 1;              /* IRQ can be used for probe  */
unsigned int         valid    : 1;               /* IRQ claimable       */
unsigned int         noautoenable : 1;        /* don't automatically enable IRQ */
unsigned int         unused   :25;
void (*mask_ack)(unsigned int irq);         /* Mask and acknowledge IRQ   */
void (*mask)(unsigned int irq);                /* Mask IRQ                      */
void (*unmask)(unsigned int irq);   /* Unmask IRQ                  */
struct irqaction *action;
/*
 * IRQ lock detection
 */
unsigned int         lck_cnt;
unsigned int         lck_pc;
unsigned int         lck_jif;
};

在具體的ARM晶元中會有很多的中斷類型,每一種類型的中斷用以上結構來表示:
struct irqdesc irq_desc[NR_IRQS];   /* NR_IRQS根據不同的MCU會有所區別*/

在通過request_irq()函數註冊中斷服務程序的時候,將會把中斷向量和中斷服務程序對應起來。
我們來看一下request_irq的源碼:

int request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *),
            unsigned long irq_flags, const char * devname, void *dev_id)
{
unsigned long retval;
struct irqaction *action;
if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
    (irq_flags & SA_SHIRQ && !dev_id))
           return -EINVAL;
action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
if (!action)              /*  生成action結構*/
           return -ENOMEM;
action->handler = handler;
action->flags = irq_flags;
action->mask = 0;
action->name = devname;
action->next = NULL;
action->dev_id = dev_id;
retval = setup_arm_irq(irq, action);   /*把中斷號irq和action 對應起來*/
if (retval)
           kfree(action);
return retval;
}
    其中第一個參數irq就是中斷向量,第二個參數即是要註冊的中斷服務程序。很多同仁可能疑惑的是,我們要註冊的中斷向量號是怎麼確定的呢?這要根據具體晶元的中斷控制器,比如三星的S3C2410,需要通過讀取其中的中斷狀態寄存器,來獲得是哪個設備發生了中斷: 

if defined(CONFIG_ARCH_S3C2410)
#include <asm/hardware.h>
           .macro  disable_fiq
           .endm
           .macro  get_irqnr_and_base, irqnr, irqstat, base, tmp
           mov  r4, #INTBASE             @ virtual address of IRQ registers
           ldr     irqnr, [r4, #0x8]  @ read INTMSK   中斷掩碼寄存器
           ldr     irqstat, [r4, #0x10]   @ read INTPND  中斷寄存器
           bics    irqstat, irqstat, irqnr
           bics    irqstat, irqstat, irqnr
           beq    1002f                  
           mov  irqnr, #0
1001:           tst     irqstat, #1
           bne    1002f                            @ found IRQ
           add    irqnr, irqnr, #1
           mov  irqstat, irqstat, lsr #1
           cmp  irqnr, #32
           bcc    1001b
1002:
           .endm
           .macro  irq_prio_table
           .endm                

以上代碼也告訴了我們,中斷號的確定,其實是和S3C2410手冊中SRCPND寄存器是一致的,即: 

/* Interrupt Controller */
#define IRQ_EINT0               0       /* External interrupt 0 */
#define IRQ_EINT1               1       /* External interrupt 1 */
#define IRQ_EINT2               2       /* External interrupt 2 */
#define IRQ_EINT3               3       /* External interrupt 3 */
#define IRQ_EINT4_7           4       /* External interrupt 4 ~ 7 */
#define IRQ_EINT8_23                  5       /* External interrupt 8 ~ 23 */
#define IRQ_RESERVED6              6       /* Reserved for future use */
#define IRQ_BAT_FLT                   7
#define IRQ_TICK                 8       /* RTC time tick interrupt  */
#define IRQ_WDT                          9       /* Watch-Dog timer interrupt */
#define IRQ_TIMER0           10     /* Timer 0 interrupt */
#define IRQ_TIMER1           11     /* Timer 1 interrupt */
#define IRQ_TIMER2           12     /* Timer 2 interrupt */
#define IRQ_TIMER3           13     /* Timer 3 interrupt */
#define IRQ_TIMER4           14     /* Timer 4 interrupt */
#define IRQ_UART2             15     /* UART 2 interrupt  */
#define IRQ_LCD                            16     /* reserved for future use */
#define IRQ_DMA0              17     /* DMA channel 0 interrupt */
#define IRQ_DMA1              18     /* DMA channel 1 interrupt */
#define IRQ_DMA2              19     /* DMA channel 2 interrupt */
#define IRQ_DMA3              20     /* DMA channel 3 interrupt */
#define IRQ_SDI                    21     /* SD Interface interrupt */
#define IRQ_SPI0                   22     /* SPI interrupt */
#define IRQ_UART1             23     /* UART1 receive interrupt */
#define IRQ_RESERVED24            24
#define IRQ_USBD                25     /* USB device interrupt */
#define IRQ_USBH                26     /* USB host interrupt */
#define IRQ_IIC                     27     /* IIC interrupt */
#define IRQ_UART0             28     /* UART0 transmit interrupt */
#define IRQ_SPI1                   29     /* UART1 transmit interrupt */
#define IRQ_RTC                            30     /* RTC alarm interrupt */
#define IRQ_ADCTC            31     /* ADC EOC interrupt */
#define NORMAL_IRQ_OFFSET 32

這些宏定義在文件irqs.h中,大家可以看到它的定義取自S3C2410的文檔。 

總結: linux在初始化的時候已經把每個中斷向量的地址準備好了!就是說添加中斷服務程序的框架已經給出,當某個中斷髮生時,將會到確定的地址處去找指令,所以我們做驅動程序時,只需要經過request_irq()來掛接自己編寫的中斷服務程序即可。

另:對於快速中斷,linux在初始化時是空的,所以要對它掛接中斷處理程序,就需要單獨的函數set_fiq_handler()來實現,此函數在源文件fiq.c中,有興趣的讀者可進一步研究。




[admin via 研發互助社區 ] ARM Linux 中斷分析已經有1734次圍觀

http://cocdig.com/docs/show-post-42208.html