in the name of zero

May 28, 2006

on the idt

Filed under: hermetic studies

friday 5/27/06

[ what are interrupts? ]
interrupts are events/alerts raised by certain things (keyboard interrupt for example) that alerts the processor to give attention to it. it’s a way to conserve processor time because it eliminates the need for constant polling just to determine if processor attention must be given. simply put, whenever a special port, i/o, device needs the processor to do something, it only needs to raise it’s hand and it will be attended by the processor. similar to a classroom. a special routine handles each kinds of interrupt and collectively, they are known as interrupt service routines but how is the processor able to determine which interrupt service routine to call for each interrupt? this problem is solved by what is known as the interrupt descriptor table.

5.10 IDTR interrupt descriptor table

The interrupt descriptor table (IDT) associates each exception or interrupt vector with a gate descriptor for the procedure or task used to service the associated exception or interrupt.
like the global descriptor table which is a collection of segment descriptors the interrupt descriptor table is also a collection of segments, specifically, gate descriptors which fall under three kinds:
a) task gate descriptor
b) interrupt gate descriptor
c) trap gate descriptor
the size of these descriptors is also 8 bytes (64 bits or quadword) long. see: intel manual figure 5-12 IDT gate descriptors

the i386 gate descriptors look like this:

31						            0
[OFFSET 	  | P | DPL | 0 | D | TYPE | 000 | RESERVED ]
[SEGMENT SELECTOR | 				     OFFSET ]

P - present field. you need to set this for interrupts that you’ll be using.
DPL - 0 for exceptions and hardware interrupts and 3 for software interrupts.
TPYE - is this a task gate? an interrupt gate? or a trap gate?
SEGMENT SELECTOR - what segment to run interrupt handler in? this must point to our kernel’s code segment defined in the GDT
OFFSET - address of interrupt handler in the SEGMENT defined in the segment selector.

remember the gdt entry struct i defined in last the last post? well, since a segment descriptor and a gate descriptor are both 64 bits, we could just typedef that to something more descriptive right? or better yet, define a template structure which we can typedefine to either a gdt selector or an idt selector. (but i won’t go through that in detail now since this is not a classroom)

#define _PACK __attribute__((packed));
	
struct idt_entry
{
	unsigned long dword1;
	unsigned long dword2;
};
	
/* and the idt pointer we feed to idtr via lidt
 * see 2.4.3 interrupt descriptor table
 * base address is 32 bits in protected mode (int)
 * limit is 16 bits (short or char*2) */
struct idt_ptr
{
	unsigned short limit;
	unsigned int base;
} _PACK;

suggested reading. chapter 5: interrupt and exception handling in the intel software develope’s manual.

[ interrupt and exceptions overview ]
interrupts and exceptions are events that indicate that a condition exists somewhere in the system, the processor, or within the currently executing program or task that requires the attention of the processor. interrupts occur at random times during the execution of a program in response to signals from hardware. exceptions occur when the processor detects an error condition while executing an instruction.

the greatest example of an exception is a division by zero.

whenever an interrupt or exception is encountered the currently running procedure or task is suspended and the processor immediately gives control to the appropriate interrupt or exception handler. after the execution is done, the suspended procedure or task is resumed without loss of program continuity unless recovery isn’t possible.

[ exception and interrupt vectors ]
each exception or interrupt requires a special handler by the processor, and thus, is assigned a unique number for identification. this identification is called a vector and it is also used as an index in the interrupt descriptor table which in turn provides entry points to an exception or interrupt handler.

i hope this diagram would clear things up:

[ interrupt/exception ] -> [ idt index ] -> [ interrupt/exception handler ]

a total of 255 vectors are allowed to be specified. vectors from zero (0) to thirty one (31) are reserved for the processor (im doing intel ia-32), architecture-defined exception/interrupt constants. the remaining vectors (32-255) are designated as user-defined and are not reserved. see table 5-1 in the intel dev manual to see the list of defined vector constants. also see 5.5 exception classifications.

[ note ]
at this point, no interrupt descriptors and interrupt service routines have been implemented for stephy yet. so i tried doing a division by zero after setting the gdt and doing a “sti”, to see what happens. and guess what!! the whole thing reboots. my next hurdle would be, to make a simple idt-isr-exception handler that would address this issue and also print a message that a “division by zero occured”.
[ end note ]

next stop, interrupt service routines.

1 Comment »

The URI to TrackBack this entry is: http://gnurbs.blogsome.com/2006/05/28/on-the-idt/trackback/

  1. Nice post,

    Great advice,

    Keep up the good work

    Comment by software development company — November 24, 2009 @ 5:42 pm

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Get free blog up and running in minutes with Blogsome | Theme designs available here