Vedon Book
1. Understanding of the concept of anomalies
Exceptions are events that can interrupt the CPU's normal operation, such as external interrupts, undefined directives, soft interrupts, and so on. When these exceptions occur, it interrupts the normal operation of the CPU and jumps to the appropriate exception handler to handle some of the actions required by these exceptions.
2. Linux Kernel exception handling framework
Based on Linux-2.6.32, the kernel starts early_trap_init (void) copies the exception vector table to the 0xffff0000 virtual address:
memcpy (void *) vectors,__vectors_start, __vectors_end-__vectors_start);
memcpy (void *) vectors + 0x200, __stubs_start, __stubs_end-__stubs_start);
memcpy (void *) vectors + 0X1000-KUSER_SZ, __kuser_helper_start, KUSER_SZ);
Where the anomaly vectors are ARCH/ARM/KERNEL/ENTRY-ARMV. Defined in S:
. Globl __vectors_start
__vectors_start:
ARM (Swi sys_error0)
THUMB (Svc #0)
THUMB (NOP)
W (b) vector_und + stubs_offset ==> macro + offset:
W (LDR) PC,. LCVSWI + Stubs_offset
W (b) Vector_pabt + Stubs_offset
W (b) Vector_dabt + Stubs_offset
W (b) VECTOR_ADDREXCPTN + Stubs_offset
W (b) Vector_irq + Stubs_offset
W (b) Vector_fiq + Stubs_offset
Each section jumps to execute the corresponding code:
(1) Taking Vector_und as an example
vector_stub und, Und_mode
. Long __und_usr @ 0 (usr_26/usr_32)= = "__und_usr==" __und_usr_unknown== asmlinkage void __exception do_undefinstr (struct pt_regs *regs)
. Long __und_invalid @ 1 (fiq_26/fiq_32)
. Long __und_invalid @ 2 (irq_26/irq_32)
. Long __und_svc @ 3 (svc_26/svc_32)
. Long __und_invalid @ 4
. Long __und_invalid @ 5
. Long __und_invalid @ 6
. Long __und_invalid @ 7
. long __und_invalid @ 8
. Long __und_invalid @ 9
. Long __und_invalid @ A
. long __und_invalid @ b
. long __und_invalid @ C
. long __und_invalid @ d
. long __und_invalid @ E
. long __und_invalid @ F
. Align 5
vector_stub und, Und_mode Macro expansion:
. macro vector_stub, name, mode, correction=0
. Align 5
Vector_\name: = = "Vector_und
Stmia sp, {r0, LR} @ Save R0, LR
Mrs LR, SPSR
str LR, [sp, #8] @ save SPSR
Mrs R0, CPSR
Eor r0, R0, # (\mode ^ Svc_mode | Psr_isetstate)
MSR spsr_cxsf, R0
and LR, LR, #0x0f
THUMB (ADR R0, 1f)
THUMB (Ldr LR, [R0, LR, LSL #2])
mov r0, SP
ARM (Ldr LR, [PC, LR, LSL #2])
Movs PC, LR endproc (vector_und)
. Align 2
1:
. endm
here, the calculation of the return address after the exception, save some registers, and then into the management mode, and finally according to the interrupted work mode to call a jump branch above, different jump branches just slightly different at the entrance, followed by the same processing, are called the corresponding C function,
(2) Look at W (b) Vector_irq + Stubs_offset
vector_stub IRQ, Irq_mode, 4
. Long __irq_usr @ 0 (usr_26/usr_32) = =Usr_entry= = "Irq_handler= = "ASM_DO_IRQ
. Long __irq_invalid @ 1 (fiq_26/fiq_32)
. Long __irq_invalid @ 2 (irq_26/irq_32)
. Long __irq_svc @ 3 (svc_26/svc_32)
. Long __irq_invalid @ 4
. Long __irq_invalid @ 5
. Long __irq_invalid @ 6
. Long __irq_invalid @ 7
. long __irq_invalid @ 8
. Long __irq_invalid @ 9
. Long __irq_invalid @ A
. long __irq_invalid @ b
. long __irq_invalid @ C
. long __irq_invalid @ d
. long __irq_invalid @ E
. long __irq_invalid @ F
vector_stub IRQ, Irq_mode, 4 macro expansion:
. macro vector_stub, name, mode, correction=0
. Align 5
VECTOR_IRQ:
Sub LR, LR, #4//COMPUTE return address
Stmia sp, {r0, LR} @ save R0, LR
Mrs LR, SPSR
str LR, [sp, #8] @ Save SPSR
Mrs R0, CPSR
Eor r0, R0, # (\mode ^ Svc_mode | Psr_isetstate)
MSR spsr_cxsf, R0
and LR, LR, #0x0f
THUMB (ADR R0, 1f)
THUMB (Ldr LR, [R0, LR, LSL #2])
mov r0, SP
ARM (Ldr LR, [PC, LR, LSL #2])
Movs pc, LR @ branch to handler in SVC mode
Endproc (VECTOR_IRQ)
. Align 2
1:
. endm
3. Linux kernel interrupt handling
In the previous step, we knew that ASM_DO_IRQ would eventually be invoked in the IRQ , and then we'd take a look at the ASM_DO_IRQ process:
asmlinkage void __exception ASM_DO_IRQ(unsigned int IRQ, struct pt_regs)
{
struct Pt_regs *old_regs = Set_irq_regs (regs);
Irq_enter (); The RCU module records an internal count that indicates that the NOHZ state is currently exited and the hard interrupt count in the preemption count of the current task is added to 1. This count represents the current interrupt nesting level.
if (Unlikely (IRQ >= Nr_irqs)) {//To determine if the error interrupt number is to be counted without processing
if (Printk_ratelimit ())
PRINTK (kern_warning "Bad irq%u\n", IRQ);
ACK_BAD_IRQ (IRQ);
} else {
GENERIC_HANDLE_IRQ (IRQ); Real Interrupt Handling
}
Irq_finish (IRQ);
Irq_exit ();
Set_irq_regs (Old_regs);
}
static inline void GENERIC_HANDLE_IRQ(unsigned int IRQ) calls Generic_handle_irq_desc (IRQ, Irq_to_desc (IRQ));
struct Irq_desc *irq_to_desc(unsigned int IRQ)//Remove interrupt descriptor based on interrupt number
{
Return (IRQ < NR_IRQS)? Irq_desc + IRQ: NULL;
}
static inline void Generic_handle_irq_desc(unsigned int IRQ, struct IRQ_DESC *desc)
{
#ifdef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ//defined in the configuration file
DESC->HANDLE_IRQ (IRQ, DESC);
#else
if (likely (DESC->HANDLE_IRQ))
DESC->HANDLE_IRQ (IRQ, DESC);
Else
__DO_IRQ (IRQ);
#endif
}
S3c24xx_init_irq (void) fills the structure body struct IRQ_DESC member Chip,handle_irq
Set_irq_handler (unsigned int IRQ, irq_flow_handler_t handle)
{
__set_irq_handler (IRQ, handle, 0, NULL);
}
for (irqno = irq_eint4; irqno <= irq_eint23; irqno++) {
IRQDBF ("Registering IRQ%d (extended s3c IRQ) \ n", irqno);
Set_irq_chip (Irqno, &s3c_irqext_chip);
Set_irq_handler (Irqno, HANDLE_EDGE_IRQ);
Set_irq_flags (Irqno, irqf_valid);
}
in the HANDLE_EDGE_IRQ function:
Desc->chip->ack (IRQ)// Clear Interrupt
handle_irq_event (IRQ, action),// remove members from the action list, execute action->handle.
4. REQUEST_IRQ Interrupt Registration
REQUEST_IRQ (unsigned int IRQ, irq_handler_t handler, unsigned long flags,const char *name, void *dev)
= = "REQUEST_THREADED_IRQ (IRQ, Handler, NULL, flags, name, dev);"
int REQUEST_THREADED_IRQ (unsigned int IRQ, irq_handler_t handler, irq_handler_tthread_fn, unsigned longirqflags, const char *devname, void *dev_id)
{
struct Irqaction *action;
struct IRQ_DESC *desc;
int retval;
if (Irqflags & (irqf_shared| irqf_disabled)) = =
(irqf_shared| irqf_disabled)) {
Pr_warning (
"IRQ%d/%s:irqf_disabled is isn't guaranteed on shared irqs\n",
IRQ, Devname);
}
#ifdef CONFIG_LOCKDEP
/*
* LOCKDEP wants atomic interrupt handlers:
*/
Irqflags |= irqf_disabled;
#endif
if ((Irqflags & irqf_shared) &&!dev_id)
Return-einval;
desc = Irq_to_desc (IRQ);
if (!DESC)
Return-einval;
if (Desc->status & Irq_norequest)
Return-einval;
if (!handler) {
if (!THREAD_FN)
Return-einval;
handler = Irq_default_primary_handler;
}
action = kzalloc (sizeof (struct irqaction), gfp_kernel); Request and populate the structure body
if (!action)
Return-enomem;
Action->handler = handler;
ACTION->THREAD_FN = THREAD_FN;
Action->flags = Irqflags;
Action->name = Devname;
action->dev_id = dev_id;
Chip_bus_lock (IRQ, DESC);
retval = __SETUP_IRQ (IRQ, desc, action) = = = "Add the action;set_type;starup/enable of the formal parameter to the action of an existing DESC[IRQ in this function"
Chip_bus_sync_unlock (IRQ, DESC);
if (retval)
Kfree (action);
#ifdef CONFIG_DEBUG_SHIRQ
if (Irqflags & irqf_shared) {
unsigned long flags;
DISABLE_IRQ (IRQ);
Local_irq_save (flags);
Handler (IRQ, dev_id);
Local_irq_restore (flags);
ENABLE_IRQ (IRQ);
}
#endif
return retval;
}
5. FREE_IRQ Interrupt Uninstall
(1) According to the interrupt number IRQ, dev_id from the action list to find the table entries, remove him
(2) If it is the only table entry, also call Irq_desc[irq].chip->shutdown or irq_desc[irq].chip->disable to close the table entry