Analysis of Linux implementation features based on i386 -- about interruption

Source: Internet
Author: User
Article title: Analysis of Linux implementation features based on i386-about interruptions. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Summary
?? After the page-based virtual management is initialized, the Linux kernel calls the trap_init () and init_IRQ () functions to initialize the interrupt mechanism. Among them, trap_init () is mainly used to initialize the interrupt vectors reserved by some systems, while init_IRQ () is mainly used to interrupt peripherals.
III. implementation of interrupt management in Linux
?? 1. initialization of the interrupt vector table IDT
?? After the page-based virtual management is initialized, the Linux kernel calls the trap_init () and init_IRQ () functions to initialize the interrupt mechanism. Among them, trap_init () is mainly used to initialize the interrupt vectors reserved by some systems, while init_IRQ () is mainly used to interrupt peripherals.
Void _ init trap_init (void)
{
# Ifdef CONFIG_EISA
If (isa_readl (0x0FFFD9) =
'E + ('I' <8) + ('s '<16) + ('A' <24 ))
EISA_bus = 1;
# Endif
  
Set_trap_gate (0, empty _ error );
Set_trap_gate (1, & debug );
Set_intr_gate (2, & nmi );
Set_system_gate (3, & int3);/* int3-5 can be called from all */
Set_system_gate (4, & overflow );
Set_system_gate (5, & bounds );
Set_trap_gate (6, & invalid_op)
Set_trap_gate (7, device_not_available );
Set_trap_gate (8, & double_fault );
Set_trap_gate (9, & coprocessor_segment_overrun );
Set_trap_gate (10, & invalid_TSS );
Set_trap_gate (11, & segment_not_present );
Set_trap_gate (12, & stack_segment );
Set_trap_gate (13, & general_protection );
Set_trap_gate (14, & page_fault );
Set_trap_gate (15, & spurious_interrupt_bug );
Set_trap_gate (16, & coprocessor_error );
Set_trap_gate (17, & alignment_check );
Set_trap_gate (18, & machine_check );
Set_trap_gate (19, & simd_coprocessor_error );
  
Set_system_gate (SYSCALL_VECTOR, & system_call );
  
/*
* Default LDT is a single_entry callgate to lcall7 for iBCS
* And a callgate to lcall27 for Solaris/x86 binaries
*/
Set_call_gate (& default_ldt [0], lcall7 );
Set_call_gate (& default_ldt [4], lcall27 );
/*
_ Set_gate (a, 12, 3, addr );
// 12 is 1100b, and the type code is 100, that is, the call gate
}
?? These functions call the same subroutine _ set_gate (). The first parameter is used to set the nth entry in the idt_table of the interrupt descriptor table, the second parameter corresponds to the D-bit and type-bit segments in the gate format, and the third parameter is the corresponding DPL bit segment.
# Define _ set_gate (gate_addr, type, dpl, addr)
Do {
Int _ d0, _ d1;
_ Asm _ volatile _ ("movw % dx, % ax
")
"Movw % 4, % dx
"
"Mov1% eax, % 0
"
"Mov1% edx, % 1"
: "= M" (* (long *) (gate_addr ))),
"= M" (* (1 + (long *) (gate_addr), "= & a" (_ d0), "= & d" (_ d1)
: "I" (short) (0x8000 + (dpl <13) + (type <8 ))),
"3" (char *) (addr), "2" (_ KERNEL_CS <16 ));
} While (0)
?? The output part is between the first ":" and the second ":". there are four constraint outputs, and four variables will be changed, % 0, % 1, % 2, and % 3 respectively. Both % 0 and % 1 are memory units, which are combined with gate_addr and gate_addr + 1, % 2 with local variable _ d0, and stored in register % eax; % 3 is combined with local variable _ d1 and stored in register % edx.
?? The part after the second ":" is the input part. the output part has defined % 0-% 3. the first variable in the input part is % 4, the second and third variables are equivalent to % 3 and % 2 in the output part. The location values of input variables, including % 3 and % 2, are all set before these variables are referenced. Before executing the code of the Instruction Department, % eax is set to (_ KERNEL_CS <16) and % edx is set to addr, % 4 variable is set to (0x8000 + (dpl <13) + type <8), then % 4 variable corresponds to the 32-47 digit of the door structure, the P bit is 1.
Tables available
?? In the first instruction of the Instruction Department, "movw % dx, % ax", insert the low 16-bit value of % dx into the low 16-bit value of % ax. in this way, in % eax, its 16-bit height is _ KERNEL_CS, while its 16-bit height is addr's 16-bit low, forming the 0-31 bits in the gate structure.
?? The second command "movx % 4, % dx" puts % 4 into % dx, that is, placing the 32-47 bits in the door structure in % dx, for % edx, it corresponds to the 32-bit high in the door structure.
?? The third command "mov1% eax, % 0" writes % edx to the variable % 0, that is, * gate_addr.
?? Command 4 "mov1% eax, % 1" writes % edx to the variable % 1, that is, * (gate_addr + 1 ).
?? To sum up the commands 3 and 4, write the content of the entire gate structure in * gate_addr.
Void _ inti init_IRQ (void)
{
Int I;
# Ifndef CONFIG_X86_VISWS-APIC
Init_ISA_irqs ();
# Else
Init_VISWS_APIC_irqs ();
# Endif
/*
* Cover the whole vector space, no vector can escape
* Us. (some of these will be overridden and become
* 'Special 'SMP interrupts)
*/
For (I = 0; I <NR_IRQS; I ++ ){
Int vector = FIRST_EXTERNAL_VECTOR + I;
If (vector! = SYSCALL_VECTOR)
Set_intr_gate (vector, interrupt [I]);
}
  
# Ifdef CONFIG_SMP
/*
* IRQ0 must ve given a fixed assignment and initialized,
* Because it's used before the I0-APIC is set up.
*/
Set_intr-gate (FIRST_DEVICE_VECTOR, interrupt [0]);
  
/*
* The reschedule interrupt is a CPU-to-CPU reschedule-helper
* IPI, driven by wakeup.
*/
Set_intr_gate (RESCHEDULE_VECTOR, reschedule_interrupt );
  
/* IPI for generic function call */
Set_intr_gate (CALL_FUCTION_VECTOR, call_funtion_interrupt );
# Endif
  
# Ifdef CONFIG_X86_LOCAL_APIC
/* IPI vectors for APIC spurous and error interrupts */
Set_intr_gate (SPURIOUS_APIC_VECTOR, spurious_interrupt );
Set_intr_gate (ERROR_APIC_VECTOR, error_interrupt );
# Endif
  
/*
* Set the clock to HZ Hz, we already have a valid
* Vector now;
*/
Outb_p (0x34,0x43);/* binary, mode 2, LSB/MSB, ch 0 */
Outb_p (LATCH & 0xff, 0x40);/* LSB */
Outb (LATCH> 8, 0x40);/* MSB */
  
# Ifndef CONFIG_VISWS
Setup_irq (2, & irq2)
# Endif
  
/*
* External FPU? Set up irq13 if so,
* Original braindamaged ibm ferr coupling.
*/
If (boot_cpu_data.hard_math &&! Cpu_has_fpu)
Setup_irq (13, & irq13 );
?? The i386 system supports 256 interrupt vectors. after deducting the reserved cpu vectors, Linux, as a general operating system, is hard to say whether the remaining interrupt vectors are sufficient. Therefore, in Linux, a queue is set for each interrupt vector, and the interrupt service program is mounted to the corresponding queue according to the interrupt vector used by each interrupt source, each element in the array irq_desc [] is such a queue header and control structure. When an interruption occurs, first execute a section of the total service program corresponding to the interrupt vector, and find a specific service program in the queue to which it belongs based on the device number of the specific interrupt source.
?? Initialize the interrupt controller 8259A of the PC and initialize the array iirq_desc []. Then, from FIRST_EXTERNAL_VECTOR, the IDT table items of the NR_IRQS interrupt vectors are set up. The constant FIRST_EXTERNAL_VECTOR is defined as 0x20, while NR_IRQS is 224. The system call vector 0x80 is skipped.
?? Ignore the SMP structure that we don't care about and the special processing of the SG1 Workstation. The rest is the initialization of the system clock. In PC, the timer/counter chip 8254 has three channels. Channel 0 is a system timer that generates real-time clock signals, and channel 0 must be set in the program. There are four ports used to control 8254. the first three correspond to the ports of a single channel, and the last channel corresponds to the control character register port of 8254.
Outb_p (0x34,0x43); // you can specify how the channel works.
// Select channel 0. read and write the high byte first, and then read and write the low byte. this parameter is applicable to Method 2 and binary data.
Outb_p (LATCH & 0xff, 0x40 );
// Write low bytes
Outb (LATCH> 8, 0x40 );
// Write high bytes
// Set the record value of channel 0
?? At this point, IDT has been set and an interrupt vector has been set. the interrupt clock is interrupted at No. 0. However, although the endpoint address of the interrupt service has already been set in the interrupt vector table, we have not mounted the specific interrupt service program of the No. 0 interrupt to the queue of the No. 0 interrupt. At this time, the queues in the interrupt locations are empty. Therefore, even if an interrupt occurs and the clock is interrupted, it is just to let the interrupt process's total service program run for a while.
?? After the interrupt vector table is set, the interrupt queue is still empty. To enable the interrupt program to take effect, the next step is to initialize the interrupt request queue and mount the specific interrupt service program into the interrupt queue.
?? 2. initialize the interrupt request queue
?? The universal interrupt gate allows multiple interrupt sources to be shared, and the shared structure can be dynamically changed during system operation, therefore, in the IDT initialization phase, only one interrupt request queue is prepared for each interrupt vector.

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.