device management

66
1 Device Management Device Management • The von Neumann Architecture • System Architecture • Device Management •Polling •Interrupts •DMA operating systems

Upload: eliana-moody

Post on 01-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

operating systems. Device Management. The von Neumann Architecture System Architecture Device Management Polling Interrupts DMA. operating systems. Von Neumann Architecture. Born 28 December 1903, Budapest, Hungary; Died 8 February 1957, Washington DC; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Device Management

1

Device ManagementDevice ManagementDevice ManagementDevice Management

• The von Neumann Architecture• System Architecture• Device Management

•Polling•Interrupts•DMA

operatingsystems

Page 2: Device Management

2

Von Neumann Von Neumann ArchitectureArchitecture

Von Neumann Von Neumann ArchitectureArchitecture

Born 28 December 1903, Budapest, Hungary; Died 8 February 1957, Washington DC; Brilliant mathematician, synthesizer, and promoter of the stored program concept which became the prototype of most of its successors - the von Neumann Architecture

operatingsystems

Page 3: Device Management

3

Von Neumann Von Neumann ArchitectureArchitecture

Von Neumann Von Neumann ArchitectureArchitecture

bus

cpumemorydevicedevice

Machine has a fixed set of electronic parts

Actions are determined by a program stored in the computer memory.

Instructions and Data

operatingsystems

Prior to this time programming was done with switches & plugboards

Page 4: Device Management

4

bus

cpu

address linesdata lines

ALUControlUnit

General Purpose&

Status Registers

operatingsystems The CPU and the BusThe CPU and the Bus

Page 5: Device Management

5

cpu

ALUControl

Unit

General Purpose&

Status Registers

fetch unit

decode unit

execution unit

instruction reg

program ctraddress

instruction

operatingsystems

Page 6: Device Management

6

bus

Memory unit

memory MAR

MDR

Command

Memory Address register

Memory Data register

operatingsystems The Memory UnitThe Memory Unit

Page 7: Device Management

7

bus

Memory unit

memoryMAR

MDR

CMDREG

Writing to MemoryWriting to Memory

Address to write toData to write

Write command

operatingsystems

Memory Address register

Memory Data register

Page 8: Device Management

8

DevicesDevicesDevicesDevices

In a Unix system, every device is treated just like a file.In fact, all devices have a file name in the file system

operatingsystems

Files that represent devices are in the /dev directory

$ ls –C /dev

You can list the terminal device you are attached to

$ tty

You can copy a file to the terminal

$ cp myFile.txt /dev/tty2

Page 9: Device Management

9

Block Devices

Stores data in fixed size blocks.Blocks can be read/written independently

Character DevicesDelivers or accepts a stream of characters.

DevicesDevicesDevicesDevicesoperatingsystems

Page 10: Device Management

10

Do an ls command on a terminal device

$ ls –al /dev/ttyp1

operatingsystems

crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1

A character device

this is usually wherethe file size goes.What’s up?

Page 11: Device Management

11

Do an ls command on a terminal device

$ ls –al /dev/tty1

operatingsystems

crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1

A character device

The i-node in this case does notcontain the address of a disk block,but the address of a device driver.

In this case, 4 is the “address” of thedevice driver, and 1 is an argumentpassed to the device driver.

Page 12: Device Management

12

operatingsystems

crw - - w - - - - 1 debry tty 4, 1 Aug 4 4:18 /dev/tty1

A character device

only the terminal owner can read from the terminal.

any other terminal user can write to the terminal

Page 13: Device Management

13

At the API, all devices support a common set of commands:

openclosereadwrite ...

operatingsystems

Page 14: Device Management

14

For example, to read from a tape drive, you mightwrite code that looks like the following:

int fd;fd = open(“dev/tape”,O_RDONLY);lseek(fd, long (4096, SEEK_SET);n = read(fd, buffer_length);close(fd);

Page 15: Device Management

15

Note that all devices do not support all of thefile system commands. For example, you cannotdo a seek on a terminal.

Page 16: Device Management

16

I/O LayersI/O LayersI/O LayersI/O Layers

User ProgramUserSpace

Operating System

DeviceDriver

KernelSpace

DeviceController

DeviceDriver

DeviceController

DeviceDriver

DeviceController

API

operatingsystems

Standard I/F

openclosereadwriteioctl

Page 17: Device Management

17

Layers of the I/O system and the main

functions of each layer

Layers of the I/O system and the main

functions of each layer

User Process

Device Independent Layer

Device Dependent Layer

Interrupt Handler

LOGICAL I/O: open, read, write, …

Naming, protection, blocking, allocation

Write to device registers, read statusDri

ver

Wake up driver when I/O completes

Device Controller

Perfom the physical I/O operationdata status command

operatingsystems

Page 18: Device Management

18

Device ControllersDevice ControllersDevice ControllersDevice ControllersI/O devices have two major components:

A mechanical component An electronic component

The electronic component is the device controller. It may be able to handle multiple devices

Controller's tasksconvert serial bit stream to block of bytesperform error correction as necessarymake data available to main memorycontinuously monitor the state of the deviceControl the device

I/O devices have two major components:A mechanical component An electronic component

The electronic component is the device controller. It may be able to handle multiple devices

Controller's tasksconvert serial bit stream to block of bytesperform error correction as necessarymake data available to main memorycontinuously monitor the state of the deviceControl the device

operatingsystems

Page 19: Device Management

19

Control and statusregisters

Data buffer

Device Controller

I/O Bus

Put an address on the address line of the I/O bus the address selects the device

put the command to be executed on the control line

operatingsystems

Page 20: Device Management

20

A Device Controller A Device Controller Example:Example:

Reading Data From DiskReading Data From Disk

A Device Controller A Device Controller Example:Example:

Reading Data From DiskReading Data From Disk

CPU Controller

buffer

Memory

operatingsystems

1. The cpu issues a read command to the disk controller

read

Page 21: Device Management

21

Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk

CPU Controller Memory

1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s internal buffer

operatingsystems

||||||||||||

buffer

Page 22: Device Management

22

Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk

CPUController

buffer

Memory

1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s buffer3. The controller sends an interrupt to the cpu indicating that data is ready to be read

INT

operatingsystems

Page 23: Device Management

23

Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk

CPU Controller

buffer

Memory

1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s buffer3. The controller sends an interrupt to the cpu indicating that data is ready to be read4. The cpu reads one byte of data from the controller buffer

reg

operatingsystems

a

Page 24: Device Management

24

Reading Data From DiskReading Data From DiskReading Data From DiskReading Data From Disk

CPU Controller

buffer

Memory

1. The cpu issues a read command to the disk controller2. The disk controller reads the required data from the device and stores it in it’s buffer3. The controller sends an interrupt to the cpu indicating that data is ready to be read4. The cpu reads one byte of data from the controller buffer5. The cpu writes the byte to memory. This process continues until all data is transferred

reg

operatingsystems

a

Page 25: Device Management

25

Using Direct Memory Using Direct Memory AccessAccess

Using Direct Memory Using Direct Memory AccessAccess

CPU Controller

buffer

MemoryDMA Controller

A DMA controller is like a mini-cpu. It can readAnd write data to memory without involving

The CPU.

operatingsystems

Page 26: Device Management

26

Using Direct Memory Using Direct Memory AccessAccess

Using Direct Memory Using Direct Memory AccessAccess

CPU Controller

buffer

Memory

1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count,and a command to execute. It also sends a read command to the disk controller. The cpu can now go on and do other work.

DMA Controller

read

operatingsystems

pgm

Page 27: Device Management

27

Using Direct Memory Using Direct Memory AccessAccess

Using Direct Memory Using Direct Memory AccessAccess

CPU Controller

buffer

Memory

1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.

2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.

DMA Controller

operatingsystems

||||||||||

Page 28: Device Management

28

Using Direct Memory Using Direct Memory AccessAccess

Using Direct Memory Using Direct Memory AccessAccess

CPU Controller

buffer

Memory

1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.

2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.

3. The DMA controller sends a command to the disk controller requesting abyte of data.

DMA Controller

operatingsystems

Page 29: Device Management

29

Using Direct Memory Using Direct Memory AccessAccess

Using Direct Memory Using Direct Memory AccessAccess

CPU Controller

buffer

Memory

1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.

2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.

3. The DMA controller sends a command to the disk controller requesting abyte of data.

4. The disk controller sends a byte of data to the dma controller

DMA Controller

operatingsystems

a

Page 30: Device Management

30

Using Direct Memory Using Direct Memory AccessAccess

Using Direct Memory Using Direct Memory AccessAccess

CPU Controller

buffer

Memory

1. The cpu programs the dma controller, giving it a beginning address in memory, a byte count, and a command to execute. It also sends a read command to the disk controller.

2. The disk controller reads the data from the disk and stores it in its data buffer, just as it did before.

3. The DMA controller sends a command to the disk controller requesting abyte of data.

4. The disk controller sends a byte of data to the dma controller5. The dma controller writes the byte to memory.

DMA Controller

operatingsystems

a

Page 31: Device Management

31

Using Direct Memory Using Direct Memory AccessAccess

Using Direct Memory Using Direct Memory AccessAccess

CPU Controller

buffer

Memory

6. The disk controller sends an acknowledgement signal to the dma controller. This continues until all of the data has been transferred. All of this happens without

the cpu being involved. When all of the data has been transferred, then the cpu is

interrupted.

DMA Controller

ack

operatingsystems

int

Page 32: Device Management

32

Memory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/O

Primarymemory

device 0

device 1

device 2

memory addresses

device addresses

Traditional approachuses special I/Oinstructions to getat device registers

copy_in R3, 0x012, 4

Copy the contents of register 4 in the device at address 012into cpu register 3

operatingsystems

Page 33: Device Management

33

Memory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/OMemory-Mapped I/O

Primarymemory

device 0

device 1

device 2

memory addresses

With memory mapped I/O no special instructions are needed. Device registers are mapped by the hardware into memory addresses.

Load R3, 0xFFF0124

Load register 3 from memoryAddress 0xFFF0124

operatingsystems

Page 34: Device Management

34

Single Bus ArchitectureSingle Bus ArchitectureSingle Bus ArchitectureSingle Bus Architecture

bus

cpumemorydevicedevice

All addresses and data go on the same bus

operatingsystems

Page 35: Device Management

35

Multiple Bus ArchitectureMultiple Bus ArchitectureMultiple Bus ArchitectureMultiple Bus Architecture

bus

cpumemorydevicedevice

All device addresses and data go on another bus

The cpu uses one bus to access memory

operatingsystems

Page 36: Device Management

36

Polling vs. InterruptsPolling vs. InterruptsPolling vs. InterruptsPolling vs. Interruptsoperatingsystems

Page 37: Device Management

37

PollingPollingPollingPolling

Because there is a tremendous difference in speed between an I/O device and the computer, the computer must wait until the device is ready to process the next byte of data.

InstructionRegister

AccumulatorStatus

Register

Control lines and address lines

Data linesStatus lines

operatingsystems

Page 38: Device Management

38

ExampleExampleExampleExample

InstructionRegister

AccumulatorStatus

Register

Control lines and address lines

Data linesStatus lines

character to be printed

print instructiondevice address

characterprint

printed!

operatingsystems

Page 39: Device Management

39

PollingPollingPollingPolling

Issue WriteCommand

Load characterinto accumulator

Is theprinterdone?

noLoad characterinto accumulator

yes

What is wrong with this approach ?

If the printer can print 100 charactersa second, then it takes 10ms to printeach character. Thus this polling loopruns for 10 ms between each character.Clear the

status register

operatingsystems

Page 40: Device Management

40

Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

1. Application issues a read( ) call. The OS blocks the application.

operatingsystems

An example of a device driver using polling

Page 41: Device Management

41

Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

2. The device driver queries the status register in the controller to determine whether or not the device is busy. It polls until it is not.

BLOCKED

operatingsystems

Page 42: Device Management

42

Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling

2.

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

3. The driver stores a command in the command register of the device controller.

BLOCKED

operatingsystems

Page 43: Device Management

43

Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

4. The controller reads from the device. Since this involves mechanical parts, the read can take some time.

BLOCKED

operatingsystems

Page 44: Device Management

44

Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

BLOCKED

operatingsystems

5. Meanwhile, the device driver queries the status register in the controller to see if the operation is complete. It polls until it is.The CPU cannot be used for anything else.

Page 45: Device Management

45

Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

7. The device driver reads the data register(s) in the device controller to get the data that has been read from the device.

BLOCKED

operatingsystems

Page 46: Device Management

46

Direct I/O with PollingDirect I/O with PollingDirect I/O with PollingDirect I/O with Polling2.

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

8. The device driver copies the data into user space. The OS unblocks the application program.

BLOCKED

operatingsystems

Page 47: Device Management

47

Interrupts Interrupts Interrupts Interrupts

Handling interrupts on machines that do look-ahead or on super-scalar machines is quite complicated. This discussion is

simplified.

bus

cpu memoryInterruptcontroller

device

Device finishes it operation.It writes its status to theInterrupt controller

cpu

The Interrupt controllerissues the interrupt to theCPU. The interrupt includesa number which is used toindex into an interrupt table.

The CPU stops whatever it is doing,and executes the interrupt handlerpointed to by the interrupt.

operatingsystems

Page 48: Device Management

48

Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

1. Application issues a read( ) call. The OS blocks the application.

operatingsystems

A Device Driver Using Interrupts

Page 49: Device Management

49

Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

2. The device driver queries status registers in the device controller to see if the device is busy. If it is, it waits.

BLOCKED

operatingsystems

Page 50: Device Management

50

Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

3. When the device is ready, the device driver stores a command in the command register of the device controller. This starts the device.

BLOCKED

operatingsystems

Page 51: Device Management

51

Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

3. The driver then issues a wait().

BLOCKED

operatingsystems

wait means “I will voluntarily give up the cpuand wait for an interrupt to occur.

Page 52: Device Management

52

Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

4. The OS schedules another process.

5. Meanwhile, the device controller reads from the device. Some other process is using the CPU

BLOCKED AnotherProcess

BLOCKED

operatingsystems

Page 53: Device Management

53

operatingsystems

Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

6. The read is complete. The device controller interrupts the CPU.

7. The interrupt handler determines which device caused the interrupt. It does its clean up and then sends a signal to the device driver to wake it up.

InterruptHandler

BLOCKED

BLOCKED

signal

Page 54: Device Management

54

Direct I/O with InterruptsDirect I/O with InterruptsDirect I/O with InterruptsDirect I/O with Interrupts

User Program

Operating System

DeviceDriver

KernelSpace

DeviceController

Device Interface (API)

UserSpace

8. The device driver copies the data from the data registers in the device controller into the user’s address space. It then does a return to the application.

operatingsystems

Page 55: Device Management

55

ExampleExampleExampleExample

processText to print

userspace

kernelspace

prin

t user process makesa print request

operatingsystems

Page 56: Device Management

56

Code executed when the system call is made

copy (userBuffer, kernelBuffer, count);enable interrupts; while (printerStatusReg != READY);printerDataReg = kernelBuffer[0];run scheduler;

Prints the first character

// Block the user and run some other process

Page 57: Device Management

57

ExampleExampleExampleExample

process

Text to print

userspace

kernelspace

blocked

The user process is blocked.

the print data is transferred

to the kernel.

process Another user process is scheduled to run.

operatingsystems

Page 58: Device Management

58

ExampleExampleExampleExample

process

Text to print

userspace

kernelspace

blocked

the first character is sent to the printer

process while the printer works, this process can run.

operatingsystems

Page 59: Device Management

59

ExampleExampleExampleExample

process

Text to print

userspace

kernelspace

blocked

The printer sends an interrupt.It is ready for another character.

process This process is interrupted.

operatingsystems

Page 60: Device Management

60

ExampleExampleExampleExample

process

Text to print

userspace

kernelspace

blocked

the next character is sent to the printer

process while the printer works, this process can run again.

operatingsystems

Page 61: Device Management

61

ExampleExampleExampleExample

process

userspace

kernelspace

When all of the data has beenprinted, the process is unblocked.

It will now be scheduled to run.

process

operatingsystems

Page 62: Device Management

62

Interrupt HandlersInterrupt HandlersInterrupt HandlersInterrupt Handlersoperatingsystems

Interrupt handlers are best hidden deep in the O/S. In the best case, drivers are kernel processes – they use privileged instructions.

have the driver starting an I/O operation block until an interrupt notifies it of completion

Interrupt procedure does its work - when finished it unblocks driver that started it

Page 63: Device Management

63

Handling an InterruptHandling an InterruptHandling an InterruptHandling an Interruptoperatingsystems

Save any registers not saved by the interrupt hardware

Set up the context for the interrupt to run in Copy saved registers to the process table Set up stack for interrupt service procedure

Ack interrupt controller, re-enable interrupts

Run the interrupt handler

Determine which process to run next

Set up context for the next process to run

Load new process' registers

Start running the new process

Page 64: Device Management

64

Print Driver Interrupt Service Routine

if (count == 0) done so unblock user;else{ i = i + 1; count = count -1; printerDataReg = kernelBuffer[i];}Acknowledge interrupt;Return from interrupt;

Page 65: Device Management

65

operatingsystems

Device Manager DesignDevice Manager DesignDevice Manager DesignDevice Manager Design

User Program

Operating System

KernelSpace

Device Interface (API)

UserSpace

read( device_x, … )

Trap Tableread( device_x )

read( deviceID, …){ switch(deviceID) { case dev0: dev0_read( … ); break;

case dev1: dev1_read( …); break; …

This designrequires that thekernel be recompiledevery time a newdevice is added.

Page 66: Device Management

66

Reconfigurable DriversReconfigurable DriversReconfigurable DriversReconfigurable DriversDevice 01

Device 02

Device 03

Device 04

open( ) read( ) write( ) ioctl( )

When a new device is added, fill in entries in the table using a device “Registration” procedure.

operatingsystems