1 / 134

Linux Kernel Programming 許 富 皓

Linux Kernel Programming 許 富 皓. C Preprocessor: Stringification.

lowri
Download Presentation

Linux Kernel Programming 許 富 皓

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux Kernel Programming 許 富 皓

  2. C Preprocessor: Stringification • When a macro parameter is used with a leading #, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification. #define WARN_IF(EXP) \ do { if (EXP) \ fprintf (stderr, "Warning: "#EXP"\n"); } \ while (0) =============================================== WARN_IF (x == 0); ==> do { if (x == 0) fprintf (stderr, "Warning: ""x == 0""\n"); } while (0);

  3. Multiple Kernel Mode Stacks • If the size of the thread_union structure is 8KB, the kernel mode stack of the current process is used for every type of kernel control path: exceptions, interrupts, and deferrable functions. • If the size of the thread_union structure is 4KB, the kernel makes use of three types of kernel mode stacks.

  4. Exception Stack • The exception stack is used when handling exceptions (including system calls). • This is the stack contained in the per-process thread_union data structure, thus the kernel makes use of a different exception stack for each process in the system.

  5. Hard IRQ Stack • The hard IRQ stack is used when handling interrupts. • There is one hard IRQ stack for each CPU in the system, and each stack is contained in a single page frame. • In a multiprocessor system, all hard IRQ stacks are contained in the hardirq_stack array.

  6. Structure of Hard IRQ Stack static char hardirq_stack[NR_CPUS * THREAD_SIZE] __attribute__((__aligned__(THREAD_SIZE))); element 5 (THREAD_SIZE bytes) element 4 (THREAD_SIZE bytes) element 3 (THREAD_SIZE bytes) element 2 (THREAD_SIZE bytes) element 1 (THREAD_SIZE bytes) hardirq_stack Each hardirq_stackarray element is a union of type irq_ctx that span a single page. union irq_ctx { struct thread_info tinfo; u32 stack[THREAD_SIZE/sizeof(u32)]; };

  7. Soft IRQ Stack • The soft IRQ stack is used when handling deferrable functions (softirqs or tasklets). • There is one soft IRQ stack for each CPU in the system, and each stack is contained in a single page frame. • All soft IRQ stacks are contained in the softirq_stack array.

  8. Structure of Soft IRQ Stack static char softirq_stack[NR_CPUS * THREAD_SIZE] __attribute__((__aligned__(THREAD_SIZE))); element 5 (THREAD_SIZE bytes) element 4 (THREAD_SIZE bytes) element 3 (THREAD_SIZE bytes) element 2 (THREAD_SIZE bytes) element 1 (THREAD_SIZE bytes) softirq_stack Each softirq_stackarray element is a union of type irq_ctx that span a single page. union irq_ctx { struct thread_info tinfo; u32 stack[THREAD_SIZE/sizeof(u32)]; };

  9. Layout of a irq_ctx Structure • At the bottom of a irq_ctx page is stored a thread_info structure, while the spare memory locations are used for the stack • remember that each stack grows towards lower addresses.

  10. Differences between Hard IRQ Stacks, Soft IRQ Stacks and Exception Stacks • Hard IRQ stacks and soft IRQ stacks are very similar to the exception stacks, the only difference is that in the former the thread_info structure coupled with each stack is associated with a CPU rather than a process.

  11. hardirq_ctx and softirq_ctx • The hardirq_ctx and softirq_ctx arrays allow the kernel to quickly determine the hard IRQ stack and soft IRQ stack of a given CPU, respectively: they contain pointers to the corresponding irq_ctxelements. static union irq_ctx *hardirq_ctx[NR_CPUS]; static union irq_ctx *softirq_ctx[NR_CPUS];

  12. From hardirq_ctx to hardirq_stack Initialized byirq_ctx_init Stack thread_info . element 5 (THREAD_SIZE bytes) element 4 (THREAD_SIZE bytes) element 3 (THREAD_SIZE bytes) element 2 (THREAD_SIZE bytes) element 1 (THREAD_SIZE bytes) CPU 1 hardirq_stack[] hardirq_ctx[]

  13. do_IRQ()

  14. Invoke do_IRQ • The do_IRQ( ) function is invoked to execute all interrupt service routines associated with an interrupt. It is declared as follows: __attribute__((regparm(3))) unsigned int do_IRQ(struct pt_regs *regs) • The regparm keyword instructs the function to go to the eax register to find the value of the regs argument; as seen above, eax points to the stack location containing the last register value pushed on by SAVE_ALL.

  15. do_IRQ() – Increase a Counter • Executes the irq_enter( ) macro, which increases a counter representing the number of nested interrupt handlers. • The counter is stored in the preempt_count field of the thread_info structure of the current process (see Table 4-10 later in this chapter). #define irq_enter() \ do { \ account_system_vtime(current); \ add_preempt_count(HARDIRQ_OFFSET); \ } while (0) P.S.: the depth field of an element of irq_descarray only records information for the corresponding interrupt.

  16. do_IRQ() – Prepare Hard IRQ Stack If the Size of thread_union Is 4KB • If the size of the thread_union structure is 4 KB, it switches to the hard IRQ stack. • In particular, the function performs some extra substeps before proceeding to the next step.

  17. 4KBthread_union – (1) • Execute the current_thread_info()function to get the address of the thread_info descriptor associated with the Kernel Mode stack addressed by the esp register.

  18. 4KBthread_union – (2) • Compares the address of the thread_info descriptor obtained in the previous step with the address stored in hardirq_ctx[smp_processor_id( )], that is, the address of the thread_info descriptor associated with the local CPU. • If the two addresses are equal, the kernel is already using the hard IRQ stack, thus jumps to step 3. • This happens when an IRQ is raised while the kernel is still handling another interrupt.

  19. 4KBthread_union – (3) • Here the Kernel Mode stack has to be switched. • Stores the pointer to the current process descriptor in the task field of the thread_info descriptor in irq_ctx union of the local CPU. • This is done so that the current macro works as expected while the kernel is using the hard IRQ stack.

  20. 4KBthread_union – (4) • Stores the current value of the esp stack pointer register in the previous_esp field of thethread_info descriptor in the irq_ctx union of the local CPU (this field is used only when preparing the function call trace for a kernel oops).

  21. 4KB thread_union – (5) • Loads in the esp stack register the top location of the hard IRQ stack of the local CPU (the value in hardirq_ctx[smp_processor_id( )] plus 4096); the previous value of the esp register is saved in the ebx register.

  22. per CPU hard IRQ stack per process kernel mode exception stack hardirq_stack[i] ss esp eflags cs eip $n-256 es ds eax ebp edi esi edx ecx ebx process descriptor thread esp esp0 eip ebx %esp %esp task previous_esp thread_info thread_info %esp hardirq_ctx[i]

  23. do_IRQ() – Invoke __do_IRQ( ) • Invokes the __do_IRQ( ) function passing to it the pointer regs and the IRQ number obtained from the regs->orig_eax field (see the following section).

  24. do_IRQ() – Switching back to the Exception Stack or Soft IRQ Stack That Were in Use before • If the hard IRQ stack has been effectively switched in step 2e above, the function copies the original stack pointer from the ebx register into the esp register, thus switching back to the exception stack or soft IRQ stack that were in use before.

  25. do_IRQ() – Executes the irq_exit( ) Macro • Executes the irq_exit( ) macro, which • decreases the interrupt counter • checks whether deferrable kernel functions are waiting to be executed (see the section "Softirqs and Tasklets" later in this chapter). void irq_exit(void) { account_system_vtime(current); sub_preempt_count(IRQ_EXIT_OFFSET); if (!in_interrupt() && local_softirq_pending()) invoke_softirq(); preempt_enable_no_resched(); }

  26. in_interrupt() • #define in_interrupt() (irq_count()) • #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK)) • #define preempt_count() (current_thread_info()->preempt_count) • We put the hardirq and softirq counter into the preemption counter. The bitmask has the following meaning: • - bits 0-7 are the preemption count (max preemption depth: 256) • - bits 8-15 are the softirq count (max # of softirqs: 256) • The hardirq count can be overridden per architecture, the default is: • - bits 16-27 are the hardirq count (max # of hardirqs: 4096) • - ( bit 28 is the PREEMPT_ACTIVE flag. ) • PREEMPT_MASK: 0x000000ff • SOFTIRQ_MASK: 0x0000ff00 • HARDIRQ_MASK: 0x0fff0000

  27. do_IRQ() – Transfer Control to ret_from_intr( ) • Terminates: the control is transferred to the ret_from_intr( ) function. • see the later section "Returning from Interrupts and Exceptions".

  28. __do_IRQ()

  29. Parameters of __do_IRQ() • The __do_IRQ() function receives • an IRQ number as its parameters (through the eax register) • a pointer to the pt_regs structure where the User Mode register values have been saved (through the edx register).

  30. Equivalent Code of __do_IRQ() spin_lock(&(irq_desc[irq].lock)); irq_desc[irq].handler->ack(irq); /* e.g. set IMR of 8259A */ irq_desc[irq].status &= ~(IRQ_REPLAY | IRQ_WAITING); irq_desc[irq].status |= IRQ_PENDING; if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS;    do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock)); handle_IRQ_event(irq, regs, irq_desc[irq].action);        spin_lock(&(irq_desc[irq].lock)); } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } irq_desc[irq].handler->end(irq); /* e.g. clean IMR of 8259A */ spin_unlock(&(irq_desc[irq].lock)); 

  31. Exclusive Access to the Main IRQ Descriptor • Before accessing the main IRQ descriptor, the kernel acquires the corresponding spin lock. • We'll see in Chapter 5 that the spin lock protects against concurrent accesses by different CPUs. • This spin lock is necessary in a multiprocessor system, because • other interrupts of the same kind may be raised and • other CPUs might take care of the new interrupt occurrences. • Without the spin lock, the main IRQ descriptor would be accessed concurrently by several CPUs. As we'll see, this situation must be absolutely avoided.

  32. 8259A Block Diagram [Intel]

  33. PIC Registers [wiki] • There are three registers, • an Interrupt Mask Register (IMR), • an Interrupt Request Register (IRR), and • an In-Service Register (ISR). • The IRR maintains a mask of the current interrupts that are pending acknowledgement. • The ISR maintains a mask of the interrupts that are pending an EOI. • The IMR maintains a mask of interrupts that should not be sent an acknowledgement. different events

  34. EOI[wiki] • An End Of Interrupt (EOI) is a signal sent to a Programmable Interrupt Controller (PIC) to indicate the completion of interrupt processing for a given interrupt. • An EOI is used to cause a PIC to clear the corresponding bit in the In-Service Register (ISR), and thus allow more interrupt requests of equal or lower priority to be generated by the PIC.

  35. 8259A Interrupt Sequence [Intel] • One or more of the INTERRUPT REQUEST lines (IR7±0) are raised high, setting the corresponding IRR bit(s). • The 8259A evaluates these requests, and sends an INT to the CPU, if appropriate. • The CPU acknowledges the INT and responds with an INTA pulse. • Upon receiving an INTA from the CPU group, the highest priority ISR bit is set and the corresponding IRR bit is reset. The 8259A does not drive the Data Bus during this cycle. • …

  36. Fully Nested Mode [Intel] • This mode is entered after initialization unless another mode is programmed. • The interrupt requests are ordered in priority from 0 through 7 (0 highest). • When an interrupt is acknowledged the highest priority request is determined and its vector placed on the bus. • Additionally, a bit of the Interrupt Service Register (ISO-7) is set. This bit remains set until • the microprocessor issues an End of Interrupt (EOI) command immediately before returning from the service routine, or • if AEOI (Automatic End of Interrupt) bit is set, until the trailing edge of the last INTA. • While the IS bit is set, all further interrupts of the same or lower priority are inhibited, while higher levels will generate an interrupt (which will be acknowledged only if the microprocessor internal Interrupt enable flip-flop has been re-enabled through software).

  37. EOI 8259A Block Diagram [Intel] ACK While the IS bit is set, all further interrupts of the same or lower priority are inhibited, while higher levels will generate an interrupt.

  38. Acknowledge the PIC Object • After acquiring the spin lock, the function invokes the ack method of the main IRQ descriptor. • When using the old 8259A PIC, the corresponding mask_and_ack_8259A( )function • acknowledges the interrupt on the PIC(i.e. sends an EOI command) and • also DISABLES the IRQ line. (sets the IMR) • Masking the IRQ line ensures that the CPU does not accept further occurrences of this type of interrupt until the handler terminates. • Remember that the __do_IRQ( ) function runs with local interrupts disabled; in fact, the CPU control unit automatically clears the IF flag of the eflags register because the interrupt handler is invoked through an IDT's interrupt gate. • However, we'll see shortly that the kernel might re-enable local interrupts before executing the interrupt service routines of this interrupt.

  39. Multi-APIC System

  40. Acknowledgement in a I/O APIC System • When using the I/O APIC, however, things are much more complicated. • Depending on the type of interrupt, acknowledging the interrupt could • either be done by the ack method or • delayed until the interrupt handler terminates • that is, acknowledgement could be done by the end method. • In either case, we can take for granted that the local APIC doesn't accept further interrupts of this type until the handler terminates, although further occurrences ofthis type of interrupt may be accepted by other CPUs.

  41. Equivalent Code of __do_IRQ() spin_lock(&(irq_desc[irq].lock)); irq_desc[irq].handler->ack(irq); /* e.g. set IMR of 8259A */ irq_desc[irq].status &= ~(IRQ_REPLAY | IRQ_WAITING); irq_desc[irq].status |= IRQ_PENDING; if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS;    do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock)); handle_IRQ_event(irq, regs, irq_desc[irq].action);        spin_lock(&(irq_desc[irq].lock)); } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } irq_desc[irq].handler->end(irq); /* e.g. clean IMR of 8259A */ spin_unlock(&(irq_desc[irq].lock)); 

  42. Set Flags • The __do_IRQ( ) function then initializes a few flags of the main IRQ descriptor. • It sets the IRQ_PENDING flag because the interrupt has been acknowledged (well, sort of), but not yet really serviced. • It also clears the IRQ_WAITING and IRQ_REPLAY flags (but we don't have to care about them now).

  43. Check Whether It Can Handle the Interrupt • Now __do_IRQ( ) checks whether it must really handle the interrupt. • There are three cases in which nothing has to be done. • IRQ_DISABLED is set • IRQ_INPROGRESS is set • irq_desc[irq].action is NULL

  44. IRQ_DISABLED • A CPU might execute the __do_IRQ( ) function even if the IRQ_DISABLED flag of the corresponding IRQ line is set. • An explanation for this nonintuitive case is given in the later section "Reviving a lost interrupt." • Moreover, buggy motherboards may generate spurious interrupts even when the IRQ line is disabled in the PIC. P.S. block means its inner statement has been modified by us.

  45. Flag IRQ_DISABLED void disable_irq(unsigned int irq) {irq_desc_t *desc = irq_desc + irq; disable_irq_nosync(irq); if (desc->action) synchronize_irq(irq); } void disable_irq_nosync(unsigned int irq) {irq_desc_t *desc = irq_desc + irq; unsigned long flags; spin_lock_irqsave(&desc->lock,flags); if (!desc->depth++) { desc->status |= IRQ_DISABLED; desc->handler->disable(irq); } spin_unlock_irqrestore(&desc->lock,flags); } • Flag IRQ_DISABLED means that the IRQ line has been deliberately disabled by a device driver. spin_lock(&(irq_desc[irq].lock)); : if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS;    do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock));        handle_IRQ_event(irq, regs, irq_desc[irq].action); spin_lock(&(irq_desc[irq].lock));    } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } irq_desc[irq].handler->end(irq); spin_unlock(&(irq_desc[irq].lock));  : disable_irq() : enable_irq() : e.g. set IMR of 8259A e.g. clean IMR of 8259A

  46. IRQ_INPROGRESS • In a multiprocessor system, another CPU might be handling a previous occurrence of the same interrupt. Why not defer the handling of this occurrence to that CPU? This is exactly what is done by Linux. • This leads to a simpler kernel architecture because device drivers' interrupt service routines need not to be reentrant (their execution is serialized). • Moreover, the freed CPU can quickly return to what it was doing, without dirtying its hardware cache; this is beneficial to system performance. • The IRQ_INPROGRESS flag is set whenever a CPU is committed to execute the interrupt service routines of the interrupt; therefore, the __do_IRQ( ) function checks it before starting the real work.

  47. Use Flags to Guarantee Serial Execution of a type of Interrupt (1) CPU 1 prevents CPU 2from executing the same ISR(s)/the code inside the if()statement irq_desc[irq].status |= IRQ_PENDING; if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS; do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock));        handle_IRQ_event(irq, &regs, irq_desc[irq].action);        spin_lock(&(irq_desc[irq].lock));    } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } irq_desc[irq].status |= IRQ_PENDING; if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS; do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock)); handle_IRQ_event(irq, &regs, irq_desc[irq].action);        spin_lock(&(irq_desc[irq].lock));    } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } irq_desc[irq].status |= IRQ_PENDING; if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS; do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock)); handle_IRQ_event(irq, &regs, irq_desc[irq].action);        spin_lock(&(irq_desc[irq].lock));    } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } irq_desc[irq].status |= IRQ_PENDING; if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS;    do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock));        handle_IRQ_event(irq, &regs, irq_desc[irq].action);        spin_lock(&(irq_desc[irq].lock));    } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } irq_desc[irq].status |= IRQ_PENDING; if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)) && irq_desc[irq].action) { irq_desc[irq].status |= IRQ_INPROGRESS;    do { irq_desc[irq].status &= ~IRQ_PENDING;        spin_unlock(&(irq_desc[irq].lock));        handle_IRQ_event(irq, &regs, irq_desc[irq].action);        spin_lock(&(irq_desc[irq].lock));    } while (irq_desc[irq].status & IRQ_PENDING);    irq_desc[irq].status &= ~IRQ_INPROGRESS; } CPU 1 executes this code first CPU 2 (2) However, CPU 1 will finish the work that CPU 2 doesn’t finish.

  48. irq_desc[irq].action • This case occurs when there is no interrupt service routine associated with the interrupt. • Normally, this happens only when the kernel is probing a hardware device.

  49. Execute Interrupt Service Routines • Let's suppose that none of the above three cases holds, so the interrupt has to be serviced. • The __do_IRQ( ) function sets the IRQ_INPROGRESS flag and starts a loop. • In each iteration, the function • clears the IRQ_PENDING flag • releases the interrupt spin lock • executes the interrupt service routines by invoking handle_IRQ_event( ) (described later in the chapter).

  50. Should Another Iteration Be Executed? • When the latter function terminates, __do_IRQ( ) • acquires the spin lock again • checks the value of the IRQ_PENDING flag. • If it is clear, no further occurrence of the interrupt has been delivered to another CPU, so the loop ends. • Conversely, if IRQ_PENDING is set, another CPU has executed the do_IRQ( ) function for this type of interrupt while this CPU was executing handle_IRQ_event( ). Therefore, do_IRQ( ) performs another iteration of the loop, servicing the new occurrence of the interrupt. • Because IRQ_PENDING is a flag and not a counter, only the second occurrence of the interrupt can be recognized. Further occurrences in each iteration of the __do_IRQ( )'s loop are simply lost. Bouns question (3 points): Impact?

More Related