Interrupts in Protected-Mode - PowerPoint PPT Presentation

About This Presentation
Title:

Interrupts in Protected-Mode

Description:

... interrupt-service routine for the timer-tick interrupt. Rationale ... Continuously show tick-count (for 10secs) Reset IF=0 to disable interrupts (for exit) ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 19
Provided by: ProfessorA
Learn more at: https://www.cs.usfca.edu
Category:

less

Transcript and Presenter's Notes

Title: Interrupts in Protected-Mode


1
Interrupts in Protected-Mode
  • Writing a protected-mode interrupt-service
    routine for the timer-tick interrupt

2
Rationale
  • Usefulness of a general-purpose computer is
    dependent on its ability to interact with various
    peripheral devices attached to it (e.g.,
    keyboard, display, disk-drives, etc.)
  • Devices require a prompt response from the cpu
    when various events occur, even when the cpu is
    busy running a program
  • The x86 interrupt-mechanism provides this

3
New requirements
  • Unlike real-mode, where all code executes with
    full privileges (i.e., ring 0), protected-mode
    code usually is executed with some privilege
    restrictions
  • Normally these restrictions prevent direct
    control of the peripheral devices
  • Thus, when responding to an interrupt in
    protected-mode, a ring-transition (and its
    accompanying stack-switch) are needed

4
Interrupt-Gate Descriptors
start-offset 31..16
gate type
P
0
D P L
code-segment selector
start-offset 15..0
Legend
Ppresent (1yes, 0no) DPLDescriptor
Privilege-Level (0,1,2,3) code-selector
(specifies memory-segment containing procedure
code) start-offset (specifies the procedures
entry-point within its code-segment) gate-types
0x6 16bit Interrupt-Gate, 0x7 16-bit
Trap-Gate 0xE 32bit Interrupt-Gate, 0xF
32-bit Trap-Gate
5
Trap-Gate vs. Interrupt-Gate
  • The only distinction between a Trap-Gate and an
    Interrupt-Gate is in whether or not the CPU will
    automatically clear the IF-bit (Interrupt-Flag in
    EFLAGS register) as part of its response to an
    interrupt-request
  • This is needed in cases where an Interrupt
    Service Routine executes outside ring0, so could
    not execute cli or sti instructions

6
16bit-Gate vs. 32bit-Gate
  • The CPU constructs different stackframes for the
    16-bit versus the 32-bit gate-types

16-bits
32-bits
ring0 stack
ring0 stack
SS
SS
SP
ESP
FLAGS
EFLAGS
CS
CS
IP
EIP
SSSP
SSESP
always pushed
pushed if privilege-level changed
7
Return-from-Interrupt
  • The programmer who writes an Interrupt Service
    Routine must know whether the Gate was 16-bit or
    32-bit, in order to use the correct
    interrupt-return instruction
  • In a code-segment whose default-bit is 0 (i.e.,
    USE16), the iret instruction performs the
    correct return-actions for a 16-bit Gate
  • Use iretl for returning with a 32-bit Gate

8
Interrupt Descriptor Table
  • The Gate-Descriptors for device interrupts form
    an array (called the IDT) and reside in a special
    system memory-segment
  • The CPU will locate the IDT by referring to the
    value in its IDTR register (48-bits)
  • A pair of special instructions exists which allow
    reading and writing this register
  • sidt mem store IDTR into a memory-operand
  • lidt mem load IDTR from a memory-operand

9
Format of register IDTR
15
16
47
0
base_address 31..0
segment-limit 15..0
16-bits
32-bits
The instruction lidt is privileged (can only be
executed in ring 0), but the instruction sidt
is unprivileged (it can execute in any
ring) These features are analogous to the
instructions sgdt and lgdt used to store or
to load GDTR (Global Descriptor Table Register)
10
Register relationships
code-segment
Interrupt Descriptor Table (256 entries)
ISR
Interrupt-gate
Global Descriptor Table
code-descriptor
INT ID
GDTR
IDTR
11
Two Interrupt-Controllers
x86 CPU
Master PIC
Slave PIC
INTR
Programmable Interval-Timer
Keyboard controller
12
Each PIC has a Mask Register
IRQ 7
IRQ 6
IRQ 5
IRQ 4
IRQ 3
IRQ 2
IRQ 1
IRQ 0
Master PIC Interrupt-mask (I/O-port 0x21)
IRQ 15
IRQ 14
IRQ 13
IRQ 12
IRQ 11
IRQ 10
IRQ 9
IRQ 8
Slave PIC Interrupt-mask (I/O-port 0xA1)
If a mask-bit is 1, the corresponding
device-interrupts are masked If a mask-bit is 0,
the corresponding device-interrupts are unmasked
13
Demo-program pmtimer.s
  • Lets create a protected-mode program that will
    handle the timer-tick interrupts
  • Its ISR (Interrupt Service Routine) is very
    similar to the real-mode interrupt-handler
  • It increments a 32-bit counter (at 406C), and it
    sets a flag (at 4070) at midnight
  • It decrements an 8-bit counter (at 4040), and
    turns off diskette motors when zero
  • It sends EOI-notification to Master PIC

14
Two threads in our demo
ROM-BIOS DATA (threads share access)
main program-thread
Build descriptor-tables Enter protected-mode
(most interrupts masked) Do for ten seconds
Read tick_count Show tick_count Leave
protected-mode (most interrupts unmasked) Exit
to our loader
motor_status
tick_count
motor_count
midnight_flag
Interrupt Service Routine
Increment tick_count (maybe set
midnight_flag) Decrement motor_count (maybe set
motor_status) Issue EOI command
read
write
15
Defining INT-8 Gate-Descriptor
P1 DPL0 S0 type6
0x0000
0x8600
sel_CS
isrPIT
16-bit Interrupt-Gate
selector for ring0 code-segment
Our label for the interrupt-handlers entry-point
16
Key Steps in the Demo
  • Initialize the Descriptor-Table(s)
  • Enter Protected-Mode with IF0
  • Load GDTR, IDTR and segment-selectors
  • Mask all device-interrupts except timer
  • Set IF1 to enable unmasked interrupts
  • Continuously show tick-count (for 10secs)
  • Reset IF0 to disable interrupts (for exit)

17
In-class exercise 1
  • Try modifying the pmtimer.s program so that it
    uses a 32-bit Interrupt-Gate for the timer
    interrupt (gate-type 0xE) instead of the 16-bit
    Interrupt-Gate (gate-type 0x6)
  • NOTE You will need to adjust the iret opcode
    in your Interrupt Service Routine to accommodate
    the resulting changes in the layout of your ISRs
    stackframe.

18
In-class Exercise 2
  • Apply your knowledge of privilege-rings to modify
    this demo so that its main routine (i.e.,
    exec_timer_tick_demo) executes at
    privilege-level 3 (instead of at ring 0)
  • You will need to add more descriptors to the
    Global Descriptor Table (for code and stack at
    ring3), and also a Call-Gate when returning to
    ring0 from ring3 (for quitting)
Write a Comment
User Comments (0)
About PowerShow.com