Linux Kernel Development -- interrupt

Source: Internet
Author: User
Upper and lower half of the interrupt

Interruption is a mechanism for communication between the system hardware and the processor. When the hardware device is interrupted, the kernel is interrupted and the corresponding processing function is executed. When the interrupt service program is executed, the kernel is in the interrupt context. At this time, if the interrupt is not prohibited, the interrupt handler may still be interrupted by other interruption events. Therefore, we hope that the faster the service interruption program is executed, the better. A service interruption program usually needs to do a lot of work. For example, when a network card is interrupted, it not only needs to respond to the network card, but also copy the network data packets to the system memory, handle the issue and hand it over to the appropriate protocol stack. In this way, the two must make a trade-off if they want to speed up and complete a lot of work.

The kernel handles this problem skillfully: the kernel divides the entire interrupt processing process into the upper half and lower half. The upper half function is "registration interruption". When an interruption occurs, it reads and writes the relevant hardware and then mounts the lower half of the Interrupt Routine to the execution queue of the device. Therefore, the execution speed in the upper half is very fast, and more interrupt requests can be served. However, only "registration interruption" is far from enough, because the interruption event may be complicated. Therefore, Linux introduces a lower half to accomplish the vast majority of the mission of Interrupt events. The biggest difference between the lower half and the upper half is that the lower half can be interrupted, while the upper half cannot be interrupted. The lower half has almost done all the things of the interrupt handler and can be interrupted by new interruptions! In the lower half, it is not very urgent and usually time-consuming. Therefore, the system schedules the running time on its own and does not execute it in the context of the interrupted service. The kernel provides many lower-half execution mechanisms, such as soft interrupt, tasklet, and waiting queue.

For example, in the Linux kernel, when the NIC receives data, the NIC will tell the kernel to process data through interruption, and the kernel will interrupt the processing function (that is, the upper half) of the NIC) execute the necessary settings for some Nic hardware, because this is an urgent task after the response is interrupted. Next, the kernel calls the corresponding lower half function to process the data received by the network card, because the data processing does not need to be executed immediately in the interrupt processing function, and the interruption can be made more urgent.

Interrupt Processing Function

The interrupt handler uses the components of the hardware device driver. The driver registers and activates an interrupt handler through request_irq. When the driver is uninstalled, use free_irq () to cancel the interrupt processing function and release the disconnection. It should be noted that request_irq () may be sleep, so it cannot be used in code that interrupts the context or does not allow blocking; free_irq () must be called in the process context ().

The prototype of the interrupt processing function is:

   1:  /**
   2:   * enum irqreturn
   3:   * @IRQ_NONE            interrupt was not from this device
   4:   * @IRQ_HANDLED         interrupt was handled by this device
   5:   * @IRQ_WAKE_THREAD     handler requests to wake the handler thread
   6:   */
   7:  enum irqreturn {
   8:          IRQ_NONE                = (0 << 0),
   9:          IRQ_HANDLED             = (1 << 0),
  10:          IRQ_WAKE_THREAD         = (1 << 1),
  11:  };
  12:   
  13:  typedef enum irqreturn irqreturn_t;
  14:   
  15:  typedef irqreturn_t (*irq_handler_t)(int, void *);

Interrupt Processing functions do not need to be re-imported, because when an interrupt processing function is executed, other interruptions on the interrupt line will be blocked, to prevent receiving another interruption on the same interrupt line.

An example of a typical interrupt processing function: RTC Interrupt Processing Function

   1:  /*
   2:   *    A very tiny interrupt handler. It runs with IRQF_DISABLED set,
   3:   *    but there is possibility of conflicting with the set_rtc_mmss()
   4:   *    call (the rtc irq and the timer irq can easily run at the same
   5:   *    time in two different CPUs). So we need to serialize
   6:   *    accesses to the chip with the rtc_lock spinlock that each
   7:   *    architecture should implement in the timer code.
   8:   *    (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
   9:   */
  10:   
  11:  static irqreturn_t rtc_interrupt(int irq, void *dev_id)
  12:  {
  13:      /*
  14:       *    Can be an alarm interrupt, update complete interrupt,
  15:       *    or a periodic interrupt. We store the status in the
  16:       *    low byte and the number of interrupts received since
  17:       *    the last read in the remainder of rtc_irq_data.
  18:       */
  19:   
  20:      spin_lock(&rtc_lock);
  21:      rtc_irq_data += 0x100;
  22:      rtc_irq_data &= ~0xff;
  23:      if (is_hpet_enabled()) {
  24:          /*
  25:           * In this case it is HPET RTC interrupt handler
  26:           * calling us, with the interrupt information
  27:           * passed as arg1, instead of irq.
  28:           */
  29:          rtc_irq_data |= (unsigned long)irq & 0xF0;
  30:      } else {
  31:          rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
  32:      }
  33:   
  34:      if (rtc_status & RTC_TIMER_ON)
  35:          mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
  36:   
  37:      spin_unlock(&rtc_lock);
  38:   
  39:      /* Now do the rest of the actions */
  40:      spin_lock(&rtc_task_lock);
  41:      if (rtc_callback)
  42:          rtc_callback->func(rtc_callback->private_data);
  43:      spin_unlock(&rtc_task_lock);
  44:      wake_up_interruptible(&rtc_wait);
  45:   
  46:      kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
  47:   
  48:      return IRQ_HANDLED;
  49:  }
  50:  #endif

Interrupt Context

Different from the process context (the kernel indicates that the process performs an operation and associates it with the current process through the current macro to sleep or call the Scheduler). When the process is interrupted or the lower half, the kernel is in the interrupt context. The interrupt context is irrelevant to the process. Therefore, the interrupt context cannot be sleep. The interrupt handler has a strict time limit because it interrupts other code. Work should be separated from the interrupt handler whenever possible and put in the lower half for execution, because the lower half can run at a more appropriate time.

Interrupt handling mechanism

Figure interrupting the route from hardware to Kernel

  • Locate the interrupt number based on the interrupt controller when the interrupt occurs.
  • Interrupt execution in the upper half: Call do_irq (), save the current state old_regs = set_irq_regs (regs), irq_enter (), call the interrupt processing function, and then irq_exit () to restore the original state set_irq_regs)
  • Interrupt lower half execution: Soft Interrupt and tasklet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.