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

Slides:



Advertisements
Similar presentations
Processor Privilege-Levels
Advertisements

1 Homework Reading –Intel 8254 Programmable Interval Timer (PIT) Data Sheet Machine Projects –Continue on MP3 Labs –Continue in labs with your assigned.
Unit 4 Chapter-1 Multitasking. The Task State Segment.
Intel MP.
Ring-transitions for EM64T How the CPU can accomplish transitions among its differing privilege-levels in 64-bit mode.
Microprocessors system architectures – IA32 real and virtual-8086 mode Jakub Yaghob.
IA32 Paging Scheme Introduction to the Pentium’s support for “virtual” memory.
Interrupts in Protected-Mode Writing a protected-mode interrupt-service routine for the timer-tick interrupt.
Interrupts in Protected-Mode Writing a protected-mode interrupt-service routine for the timer-tick interrupt.
IA32 Paging Scheme Introduction to the Intel x86’s support for “virtual” memory.
Processor Exceptions A survey of the x86 exceptions and mechanism for handling faults, traps, and aborts.
IA32 Paging Scheme Introduction to the Pentium’s support for “virtual” memory.
Getting Ready to Enter x86 Protected Mode Survival tactics for enabling Protected-Mode with a minimum of supporting infrastructure.
8086 emulation Using Virtual-8086 mode to execute real-mode procedures in a protected-mode environment.
Interrupts What is an interrupt? What does an interrupt do to the “flow of control” Interrupts used to overlap computation & I/O – Examples would be console.
EM64T ‘fast’ system-calls A look at the requirements for using Intel’s ‘syscall’ and ‘sysret’ instructions in 64-bit mode.
IA-32 Processor Architecture
Page-Faults in Linux How can we study the handling of page-fault exceptions?
Exceptions and Interrupts How does Linux handle service- requests from the cpu and from the peripheral devices?
Interrupts in Protected-Mode Writing a protected-mode interrupt-service routine for the timer-tick interrupt.
The various x86 ‘modes’ On understanding key differences among the processor’s several execution-architectures.
Processor Privilege-Levels How the x86 processor accomplishes transitions among its four distinct privilege-levels.
Venturing into protected-mode A first look at the CPU registers and instructions which provide the essential supporting infrastructure.
Timeout for some demos An instructive look at how the Linux Operating System sets up system data-structures.
1 Hardware and Software Architecture Chapter 2 n The Intel Processor Architecture n History of PC Memory Usage (Real Mode)
X86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT
Memory Management (II)
1 CE6105 Linux 作業系統 Linux Operating System 許 富 皓.
Resolving interrupt conflicts An introduction to reprogramming of the 8259A Interrupt Controllers.
Processor Exceptions A survey of the x86 exceptions and mechanism for handling faults, traps, and aborts.
Task-Switching How the x86 processor assists with context-switching among multiple program-threads.
CE6105 Linux 作業系統 Linux Operating System 許 富 皓. Chapter 2 Memory Addressing.
The i/o-sensitive instructions An introduction to the software emulation of i/o-sensitive instructions in Virtual-8086 mode.
Linux Operating System
Linux Operating System
Venturing into 64-bit mode Examining the steps needed to take the processor into IA-32e mode -- and then back out again.
Segment-registers’ hidden bits A look at how segmentation attributes are cached within the CPU’s segment-registers.
Venturing into protected-mode
Introduction to the Intel x86’s support for “virtual” memory
Introduction to Interrupts
A ‘protected-mode’ exploration A look at the steps needed to build segment-descriptors for displaying a message while in protected-mode.
Homework / Exam Return and Review Exam #1 Reading Machine Projects
Microprocessor Systems Design I Instructor: Dr. Michael Geiger Fall 2012 Lecture 15: Protected mode intro.
8086 emulation Using Virtual-8086 mode to execute real-mode procedures in a protected-mode environment.
Venturing into protected-mode A first look at the CPU registers and instructions which provide the essential supporting infrastructure.
UNIT 2 Memory Management Unit and Segment Description and Paging
Intel IA32 OS Support -Refresh
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
1 Linux Operating System 許 富 皓. 2 Memory Addressing.
Interrupts in the guest VM A look at the steps needed to “reflect” hardware interrupts back into the ROM-BIOS for servicing.
13-Nov-15 (1) CSC Computer Organization Lecture 7: Input/Output Organization.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
1 i386 Memory Management Professor Ching-Chi Hsu 1998 年 4 月.
EFLAG Register of The The only new flag bit is the AC alignment check, used to indicate that the microprocessor has accessed a word at an odd.
Microprocessor system architectures – IA32 tasks Jakub Yaghob.
10. Epilogue ENGI 3655 Lab Sessions.  We took control of the computer as early as possible, right after the end of the BIOS  Our multi-stage bootloader.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
Information Security - 2. Task Switching Every process has an associated Task State Segment, whose starting point is stored in the Task register. A task.
Homework / Exam Return and Review Exam #1 Reading Machine Projects
Interrupts and exceptions
Interrupts and signals
Microprocessor and Assembly Language
Microprocessor Systems Design I
Anton Burtsev February, 2017
Homework Reading Labs S&S Extracts ,
Interrupts – (Chapter 12)
x86 segmentation, page tables, and interrupts
Interrupt Driven I/O References Text: Tanenbaum ch.1.4.3, ch Receiver Interrupt Program Example:
Introduction to the Intel x86’s support for “virtual” memory
Interrupts and System Calls
Presentation transcript:

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

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

Simplified Block Diagram Central Processing Unit Main Memory I/O device I/O device I/O device I/O device system bus

PM 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 (e.g., usually ring3) Normally these restrictions prevent direct control of any of the peripheral devices Thus, when responding to an interrupt in protected-mode, a ring-transition, and an accompanying stack-switch, are involved

IA-32 Interrupt-Gate Descriptors start-offset[ ] code-segment selectorstart-offset[ ] Reserved (must be 0) gate type P0 DPLDPL Legend: P=present (1=yes, 0=no) DPL=Descriptor Privilege-Level (0,1,2,3) code-selector (specifies memory-segment containing procedure code) start-offset (specifies the procedure’s 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

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

16bit-Gate vs. 32bit-Gate The CPU constructs different stackframes for the 16-bit versus the 32-bit gate-types ring0 stack ring0 stack IP CS FLAGS SP SS EIP EFLAGS ESP SS:SPSS:ESP 16-bits32-bits CS SS = always pushed= pushed if privilege-level changed

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.,.code16), an ‘iret’ instruction performs the correct return-actions for a 16-bit Gate Use ‘iretl’ for returning with a 32-bit Gate

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

Format of register IDTR base_address[ ]segment-limit[ ] bits32-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)

Register relationships GDTR IDTR Interrupt-gate code-descriptor Interrupt Descriptor Table (256 entries) ISR code-segment Global Descriptor Table INT ID

Two Interrupt-Controllers x86 CPU Master PIC (8259) Slave PIC (8259) INTR Programmable Interval-TimerKeyboard controller Serial-UART controller

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

Demo-program: ‘tryisr32.s’ Let’s 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 (i.e., ‘jiffies’) It sends EOI-notification to Master PIC It must take care to preserve the values that are contained in the CPU’s registers

Two ‘threads’ in our demo Build descriptor-tables Enter protected-mode (most interrupts masked) Do for fifteen seconds: { Convert ‘jiffies’ value to hours, mins, secs Write time to screen } Leave protected-mode (most interrupts unmasked) Exit to our ‘loader’ Increment ‘jiffies’ value Issue an EOI command ‘main’ program-thread Interrupt Service Routine program-variables (threads share access) jiffies read write

Defining INT-8 Gate-Descriptor 0x0000 sel_CS isrTMR[15..0] 0x8E00 32-bit Interrupt-Gate selector for the ring0 code-segment Offset into code-segment of interrupt-handler’s entry-point P=1 DPL=0 S=0 type=0xE isrTMR[31..16]

Some Key Steps in the Demo Initialize the Descriptor-Table(s) Enter Protected-Mode with IF=0 Load GDTR, IDTR and segment-selectors Mask all device-interrupts except timer Set IF=1 to enable unmasked interrupts Continuously show tick-count (for 15secs) Reset IF=0 to disable interrupts (for exit)

‘identity-mapping’ To make our demo as similar as possible to the code we would need in 64-bit mode (where ‘paging’ is always mandatory), we have chosen to enable paging in our demo and to use Intel’s 3-level mapping scheme (which supports 36-bit physical addresses) because it’s most like the 64-bit mode However, we use an ‘identity-mapping’ of the bottom 1-megabyte (to keep it simple)

3-Level Translation Scheme CR3 PAGE FRAMES PAGE TABLES PAGE DIRECTORIES PAGE DIRECTORY POINTER TABLE

3-level address-translation The CPU examines any virtual address it encounters, subdividing it into four fields offset into page-frame index into page-table bits9-bits12-bits index into page- directory index into page- directory pointer table 9-bits Each ‘virtual address’ is 32-bits but gets mapped to a physical address which can contain 36-bits

Reserved (must be 0) The ‘pgdir’ and ‘pgtbl’ entries Page-frame physical base-address[31..12] Page-frame physical base-address [35..32] PWU PWTPWT PCDPCD A avl Meaning of these bits varies with the table Legend: P = Present (1=yes, 0=no)PWT = Page Cache Disable (1=yes, 0=no) W = Writable (1=yes, 0=no)PWT = Page Write-Through (1=yes, 0=no) U = User-page (1=yes, 0=no) avl = available for user-defined purposes A = Accessed (1=yes, 0=no) EXB = Execution-disabled Bit (if EFER.NXE=1) (Assumes PAE=1, but LME=0)

Page-directory-pointer entries Reserved (must be 0) Page-frame physical base-address[31..12] Page-frame physical base-address [35..32] P Rsv (0) PWTPWT PCDPCD Reserved (must be 0) avl Legend: P = Present (1=yes, 0=no) PWT = Page Cache Disable (1=yes, 0=no) PWT = Page Write-Through (1=yes, 0=no) avl = available for user-defined purposes (Assumes PAE=1, but LME=0)

What about IA-32e modes? Some differences in what amount of CPU context information is saved on the stack All stack-elements are quadwords (64-bits) All the Interrupt Service Routines execute in 64-bit code-segments (i.e., ‘long mode’) All memory-addresses involving registers CS, DS, ES, and SS are ‘flat’ addresses All the IDT gate-descriptors are 128-bits

IA-32e IDT Gate descriptors Base[31..24] (if S=0) GD L AVLAVL offset[31..16]P DPLDPL 0 X C/DC/D R/WR/W Gate Type Reserved (must be 0) code-segment selectoroffset[15..0] offset[63..32] Reserved (must be 0) offset[63..32] P=Present Gate Type: 1110 = Interrupt-Gate, 1111 = Trap-Gate DPL = Descriptor Privilege Level IST = Interrupt Stack Table IST

64-bit interrupt-stackframe The CPU constructs a different stackframe for the 64-bit IDT gate-types – its format is independent of any privilege-changes ring0 stack RIP RFLAGS RSP SS:RSP 64-bits CS SS = always pushed

In-class exercise #1 Try modifying our ‘tryisr32.s’ program so that it does not use page-mapping at all (i.e., leave the PG-bit in CR0 turned off) Do you discover any problems when you try to execute that modified program?

In-class Exercise #2 Make a copy the ‘tryisr32.s’ file (named ‘tryisr64.s’) and see if you can discover what code-modifications are required if you wanted the timer’s ISR to execute from a 64-bit code-segment (rather than from the 32-bit code-segment it formerly used); try it out on an ‘anchor’ machine (This exercise will help with Project #1)