+ All Categories
Home > Documents > November 12, 2003Serguei Mokhov, [email protected] 1 CPU Internals SOEN228, Fall 2003.

November 12, 2003Serguei Mokhov, [email protected] 1 CPU Internals SOEN228, Fall 2003.

Date post: 29-Dec-2015
Category:
Upload: alisha-blake
View: 217 times
Download: 2 times
Share this document with a friend
Popular Tags:
39
November 12, 200 3 Serguei Mokhov, mokhov@cs .concordia.ca 1 CPU Internals SOEN228, Fall 2003
Transcript

November 12, 2003 Serguei Mokhov, [email protected]

1

CPU Internals

SOEN228, Fall 2003

November 12, 2003 Serguei Mokhov, [email protected]

2

Four Steps of Synchronization Handshake between Writer and Reader

data old data

data data

IOSCR Writer

Reader

IODR

Reader

Step 1: Writer Writes Data Step 2: Writer Signals with Token

Step 4: Reader Deletes TokenStep 3: Reader Reads Data

November 12, 2003 Serguei Mokhov, [email protected]

3

Case: Programmed Input from Keyboard

data old data

data data

Keyboard Controller

CPU

IODR

CPU

Step 1 Step 2

Step 4Step 3

ASCII Key

copy:char IODR -> reg0 copy #0 -> IOSCR

November 12, 2003 Serguei Mokhov, [email protected]

4

Case: Programmed Input from Keyboard (2)

• The ready flag (full/empty bit) is typically a particular bit in the status register of the I/O port.

• The flag is equal to 1 iff the data register (IODR) is holding valid data yet to be read (consumed) by the reader. It will be reset to 0 when the reader has read the data register.

• In this way, the I/O port can be used to carry out a transaction by using busy-wait loop: each party will loop indefinitely to test the status flag before it produces/consumes.

• Drawback: Programmed I/O via busy-wait loops consume processing power (time); for example, when the CPU executes in the busy-wait loop, no useful work is done by the processor.– Compare CPU speed and human input speed!

November 12, 2003 Serguei Mokhov, [email protected]

5

Interrupts and Interrupt Handler

November 12, 2003 Serguei Mokhov, [email protected]

6

Interrupts and Interrupt Handler

• Programmed I/O suffers from the fact that the participants in an I/O transfer may have greatly diversified speeds.

• Using the keyboard example, we know that the system could read thousands of times faster than the typing speed of the fastest typist.

• Hence, the fast partner has to wait for the slow partner, leading to a huge waste of resources (for example, computational resources).

• The asynchrony between the synchronous computer and the external world often requires better solutions.

• Interrupts is one such a solution.

November 12, 2003 Serguei Mokhov, [email protected]

7

Basic Principle (1)

• The computer will assume that I/O activities are temporally suspended (such as when the human has not yet struck the next key) and schedule itself to handle other computational task (program).

November 12, 2003 Serguei Mokhov, [email protected]

8

Basic Principle (2)

• When I/O attention is needed, the external action is translated into an interrupt signal that gets into the CPU.

• This serves as a trap, forcing the CPU to suspend its current (possibly computational) program and transfer its flow of control to an interrupt handling subroutine.

• This is equivalent to a hardwired subroutine jump.

November 12, 2003 Serguei Mokhov, [email protected]

9

Basic Principle (3)

• The interrupt service routine (handler) may be chosen from a few candidates, based on the type of interrupt received.

• When finished, control could be returned to the program that was interrupted, just like subroutine return.

November 12, 2003 Serguei Mokhov, [email protected]

10

Case: Interrupt Input from Keyboard

data old data

data data

Keyboard Controller

CPU

IODR

CPU

Step 1 Step 2

Step 4: Completion of ISRStep 3: CPU executes ISR

ASCII Key

copy:char IODR -> 0 copy #0 -> IOSCR

Interrupt CPU

CPU Acknowledges the Interrupt

November 12, 2003 Serguei Mokhov, [email protected]

11

Interrupt Processing

• Interrupt I/O involves the use of hardware signals:– when an I/O port needs CPU attention, an interrupt

signal is sent and later received by the CPU.

– The CPU can acknowledge the interrupt by returning an INTA (interrupt acknowledge signal) and subsequently jumps to the interrupt service routine (ISR) to process/serve the I/O port.

– The key feature is the asynchrony: the CPU is free to do something else until attention is needed. Hence the busy-wait is avoided.

November 12, 2003 Serguei Mokhov, [email protected]

12

Enable keyboardinput and interrupts

Read/processkeyboard data buffer

Done?

No

Yes

Enable keyboardinput and interrupts

Go to do other usefulthingies

Interrupt Service Routine

Return from

Interrupt

Other CPU-bound process

November 12, 2003 Serguei Mokhov, [email protected]

13

Key Observations

• Since the CPU is freed from busy-wait, it could be switched to perform other useful program executions.

• Thus, it improves the utilization of the system, leading to a much better response time for all users.

• I/O can proceed asynchronously. There is no assumption on the exact timing of events.

November 12, 2003 Serguei Mokhov, [email protected]

14

Instruction Cycle and Interrupts

• Instruction Cycle:– Interrupt Request (INTR) is tested at end of Cycle

– If INTR is ‘true’, then a hardwired subroutine jump to the interrupt service routine (ISR) will be used as the next instruction to be executed.

– Notice that the current program is interrupted and the return address is pushed on the stack as part of the subroutine call.

IF ID OF EX OS

call ISR Interrupt?

No

Yes

November 12, 2003 Serguei Mokhov, [email protected]

15

Interrupt Acknowledge

• 1: Interrupt Service Routine:– CPU reads data from device port.

• 2: Interrupt Service Routine:– CPU transfer the data read to memory.

MemoryUnit

CPU Device

12

Interrupt

Interrupt Acknowledge

November 12, 2003 Serguei Mokhov, [email protected]

16

Interrupt Acknowledge (2)• Interrupt Acknowledge (INTA) is the signal from the

CPU indicating that the CPU is ready to handle the interrupt.

• Typically, this signal is used to handshake with the device that has caused the interrupt so that the device can identify itself in the form of an interrupt vector.

• The interrupt vector is received by the CPU and is used as a pointer or address to the needed interrupt service routine.

• Problem:– How can the CPU scale up to the number of external interrupts?

November 12, 2003 Serguei Mokhov, [email protected]

17

Multiplicity of Interrupt Sources

• Interrupts can come from multiple devices or sources.• In a typical computer system, interrupts could be caused

by:– Power Failure– Timer– I/O Devices such as disks, terminals, printers etc.– Hardware error conditions (parity error, etc.)– Software error conditions (divide-by-zero, etc.)

• It raises a couple of issues:– Priority– Interrupt Identification

November 12, 2003 Serguei Mokhov, [email protected]

18

Priority• Can we allow an interrupt to interrupt an interrupt handler? Why?

Which interrupt should be served next?– Usually, priorities can be assigned to various sources of interrupt according

to some criteria, such as the penalty of not reacting in time, or the expected return if served.

– So, the following priority assignment is quite typical:• Power failure; Timer; Error; I/O.

• Each sub-class of priority may correspond to a single bit of signal sent to the CPU interface.– This is the interrupt signal of that sub-class.– In implementation, the instruction cycle of the CPU is changed such that at

the end of a cycle, the existence of an unmasked (enabled) interrupt forces the CPU to execute the equivalence ofjump:subroutine interrupt_handler.

November 12, 2003 Serguei Mokhov, [email protected]

19

Interrupt Identification• The choice of interrupt_handler can be

resolved in a number of ways.• In general, the CPU must identify the source of the

interrupt (with the highest priority).• This identification can be achieved using

– Software polling: the interrupt handler can be programmed to poll through the various I/O interfaces (status registers), highest priority interface first.

– Once it comes across an interface that indicates its interrupt status, then the handler proceeds to execute the corresponding handler for that interface.

November 12, 2003 Serguei Mokhov, [email protected]

20

Software Polling Flowchart

ISR for 1Interface 1

ready?

ISR for 2Interface 2

ready?

ISR for NInterface N

ready?

Return to the Interrupted Program

Yes

Yes

Yes

No

No

No

November 12, 2003 Serguei Mokhov, [email protected]

21

Hardware Polling (Daisy Chain)• The polling can be done by hardware in which the interrupt

requests from all interfaces (devices) are “OR’ed” together to interrupt the CPU.

• When the CPU acknowledges to the interrupt via INTA (interrupt acknowledge), the latter signal is allowed to propagate through the interfaces arranged in a daisy chain, with the highest priority interface receiving the INTA first.

• An interface propagates INTA to its next lower priority neighbor only if it has not generated an interrupt request; otherwise, it will stop the propagation and identify itself to the CPU (for example, by depositing its ID onto the system bus).

• Notice that INTR and INTA represent an instance of the 4-phase handshaking solution explained earlier.

November 12, 2003 Serguei Mokhov, [email protected]

22

Daisy Chain Priority Scheme• INTR: Interrupt Request (to CPU)

• INTA: Interrupt Acknowledge (from CPU)

• INTA is propagated from device 1 to device N, and it stops at the first device that has generated an interrupt.

DeviceInterface N

DeviceInterface 2

DeviceInterface 1

To Data BusInterrupt Vector

INTR

INTR1 INTR1 INTRNControl Bus

INTA

November 12, 2003 Serguei Mokhov, [email protected]

23

Interrupt Vector

• A typical way is to associate an interrupt vector with entries for each interrupt source.

• Entries in this interrupt vector actually point to (directly or indirectly) the starting address of the required interrupt service routine.

• For example, the interrupt handler for the keyboard is immediately executed once the interrupt source is known to the CPU.

November 12, 2003 Serguei Mokhov, [email protected]

24

Enabling/Disabling of Interrupts

• Even though the use of priority in some sense already disables lower priority interrupts from interrupting a higher priority interrupt service routine, it is often necessary to disable certain interrupts dynamically.

• An example would involve an input process and an output process from before.– The input process reads a character from the keyboard and the output

process writes the character to the display.

– The input and output must be done in a sequence alternately.

– To synchronize the two processes, we could use the enabling/disabling feature in interrupts.

November 12, 2003 Serguei Mokhov, [email protected]

25

Enabling/Disabling of Interrupts (2)

• The Enabling/Disabling can be accomplished using MASKING.

• A mask flag is associated with every interrupt source.

• An interrupt will be received by the CPU only if it has not been masked off, i.e., the associated mask has not been reset.

• The follow up diagram illustrates such a masked interface.

November 12, 2003 Serguei Mokhov, [email protected]

26

Priority Encoder Resolution of Interrupts

PriorityEncoder

1 2 3 ... N

INTR1

INTR2

INTRN

:::

: INTR

INTA

Interrupt Mask Register

Eg: Intel 82C59A

November 12, 2003 Serguei Mokhov, [email protected]

27

Context Switching (2)• Since the handler will use the registers and even destroy their

content, it will be important to preserve these register contexts so that when the interrupted program is returned, the original context is still available and intact.

• So, an assume-nothing interrupt handler will adopt the following structure:

interrupt_handler: push reg0 push reg1 :::: take care of service pop reg7 pop reg6 :::: return

November 12, 2003 Serguei Mokhov, [email protected]

28

Drawbacks?

• Is there any drawback using interrupt processing?– Interrupt is asynchronous and frees the CPU to perform

other tasks via proper context switching.

– However, there is a price being paid every time context switching takes place.

• Alternative:– Why not free the CPU entirely from doing the work of

the interrupt handler, except once-in-a-while?

November 12, 2003 Serguei Mokhov, [email protected]

29

Direct Memory Access(DMA)

November 12, 2003 Serguei Mokhov, [email protected]

30

Motivation

• I/O transfer often involves transferring a large “chunk” of data to or from the computer, for example, disk transfer involves multiple sectors.

• Much of the interrupt handler for such a disk transfer involves moving memory pointers and transferring the data until the needed number of bytes are transferred.

• The software managed transfer can be replaced by a hardware component called DMA controller.

November 12, 2003 Serguei Mokhov, [email protected]

31

DMA Controller• A DMA controller is a hardware interface containing:

– data register:• data to be transmitted

– status and control register:• status and control

– word/byte count register:• remaining word count

– memory address register:• next memory location

– device address register:• next device address

November 12, 2003 Serguei Mokhov, [email protected]

32

A Typical DMA Controller

DataCount

DataRegister

AddressRegister

ControlLogic

Data Lines

Address Lines

DMARDMAA

INTRRead

Write

November 12, 2003 Serguei Mokhov, [email protected]

33

DMA Controller (2)• A DMA controller (DMAC) can be considered as an intelligent I/O

port.

• We will illustrate its operation via a disk transfer example.

• Reading 1K byte from Disk Address (1234) to RAM address (1000).

• The CPU executes only the following:

copy #1234h -> device_address_register;copy #1000h -> memory_address_register;copy #1024 -> word_count_register;copy #1 -> device_status_register;:::::::::::::::

November 12, 2003 Serguei Mokhov, [email protected]

34

DMA Controller (3)• The CPU is involved only as a master; it initializes the

disk address to be 1234h, followed by the target storage address in RAM to be 1000h and the word count of 1024 in the word count register.

• Then it writes #1 into the device_status_register. This tells the disk controller (the DMAC) to start reading.

• Afterwards, the CPU moves on to do whatever else is there to do.

• As usual, the CPU can be interrupted later if attention is needed.

November 12, 2003 Serguei Mokhov, [email protected]

35

Behavior of the DMAC• The DMAC proceeds to control the disk drive

to access the desired sector (address 1234h).• Afterwards, it proceeds to conduct the transfer

of 1024 bytes from disk to RAM starting from RAM location 1000h.

• The transfer is conducted by stealing memory cycles from the CPU (the memory is connected to both the CPU and the DMAC via System Bus).

November 12, 2003 Serguei Mokhov, [email protected]

36

Comparing DMA with Interrupt I/O• You pay a price in using DMA. It incurs additional

hardware and hence cost.• What have you gained?

– Suppose the total transfer time of 1K bytes from the disk takes 1 ms.

– During this interval, the CPU is not paying any attention to the DMAC.

– If interrupts are used, the CPU will be interrupted 1024 times.

– Suppose the interrupt handler for the disk takes 0.0005 ms to execute.

– The CPU will have to spend 0.512 ms to serve the interrupts.

– Equivalently, its availability is reduced by 50%.

November 12, 2003 Serguei Mokhov, [email protected]

37

Comparing DMA with Interrupt I/O• Notice also that if the external device is fast, interrupt processing

to update the external device may be too slow:– interrupt handler involves a software solution that is usually slower than its

hardware counterpart.

– As an immediate example, you can redo the comparison assuming that the disk completes the transfer in 0.5 ms instead of 1 ms. What will happen?

MemoryUnit

CPU Device

DMA

DMA

November 12, 2003 Serguei Mokhov, [email protected]

38

System Bus

Structure

MemoryCPU

InterruptI/O

Data Bus

Address Bus

Control Bus

DMA I/OInterrupt

I/OProgrammed

I/O

7 1 2 6 5 4 3

6 5 4 37 2 1

3 4 1

4

2 7

7 2 1

2 7 1

:::

Daisy Chain

1: Data2: Address3: INTR4: INTA5: DMAR6: DMAA7: Read / Write

November 12, 2003 Serguei Mokhov, [email protected]

39

Library for PA4

• /pkg/nasm/lib/libcomp228.a


Recommended