Top Banner
CS552 Overview © 2009, D. J. Foreman 1
33

CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Jan 20, 2016

Download

Documents

Nancy McBride
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

CS552

Overview

© 2009, D. J. Foreman 1

Page 2: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Generalized O/S structure• Basic interrupt handler

– Determines cause– Uses branch table to specific handler

• I/O manager– Performs I/O for the user’s requests

• Directly• Via drivers

– Paging– Basic error handling

• Scheduler• Dispatcher

© 2009, D. J. Foreman 2

Page 3: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Generalized O/S Operation“Booting” & login

• Load self into RAM• Display logon screen• Enter h/w wait state - NO CPU cycles occur• Display pressed keys on screen• Call logon manager

– Create a Process Control Block for user– Create an address space for user– Load GUI code into user’s address space– Load “user mode” state with address of GUI

© 2009, D. J. Foreman 3

Page 4: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Generalized GUI Operation

• Create windows and buttons• Wait for user interaction

– User presses keys or uses mouse• I/O interrupt occurs (hardware gate flips)

• O/S – handles interrupt (in kernel mode)

• posts a flag for GUI to see– Value of key pressed – position of mouse

– Returns to GUI in user mode (always)• GUI performs required action on the screen

© 2009, D. J. Foreman 4

Page 5: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Kernel Basics

• States• State vectors• Interrupts• Contexts• Context switching• Reserved RAM

© 2009, D. J. Foreman 5

Page 6: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Remainder of Course• Paging• Scheduling & Dispatching• Virtualization• Multi-tasking• File & storage systems• Deadlock• Security & reliability• Distributed systems

© 2009, D. J. Foreman 6

Page 7: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Basic Computer Architecture

© 2009, D. J. Foreman 7

Page 8: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Components• Elementary instructions

– Load– Add, subtract, etc– Store– Compare– Branch

• Data access– Register (like a pointer)– Displacement

• offset or “distance in bytes, from register contents

© 2009, D. J. Foreman 8

Page 9: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Instruction types• Risc

– Operation code and one operand– e.g. L x

loads content of x into an accumulator

• CISC– Operation code and TWO operands– e.g. L x,y

loads contents of y into register x

© 2009, D. J. Foreman 9

Page 10: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

States

• Define current machine capabilities– Interrupts allowed or not– Privileged instructions allowed or not

• (ie; kernel mode vs. user mode)

– Memory protection key or state (# and/or on/off)– Next sequential instruction to perform– Addressing mode– User-mode flag (div by 0, etc)

© 2009, D. J. Foreman 10

Page 11: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Current State Vector

• Contains the state information– Is NOT a memory location or data structure

• Changeable by events (interrupts)– I/O completion or external signals– Machine failure, program failure (÷0), service call

• Changeable by privileged instructions– Vector is loaded from data in RAM

• Saved to RAM by hardware events

© 2009, D. J. Foreman 11

Page 12: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Interrupts

© 2009, D. J. Foreman 12

Page 13: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Interrupts - Generalized• Two types

– Hardware (MAY be “turned off” by kernel)• I/O • Clock/interval timer• Program exception (e.g.; div by 0)• Paging• Addressing (32/64 bit)

– Software (Service call - ALWAYS allowed)• Program requests for kernel service• Machine language instruction causes hardware “trap”

– Int 0x80 PC style (‘n’ is on stack)– SVC n z/390 style

© 2009, D. J. Foreman 13

Page 14: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

© 2009, D. J. Foreman 14

Instruction Processing with Interrupts

fetch executeInterruptsallowed?

No

yesprevious

instInterruptpending?

No

processinterrupt

yes

Page 15: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

© 2009, D. J. Foreman 15

Trap or System CallInstruction

• Atomic operation– Causes an interrupt (type=service request)– Kernel processes normally

• Common service request handler– Uses code to select address in trap table– Trap table contains addresses of specific programs

for specific request

Page 16: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

© 2009, D. J. Foreman 16

Traps or Kernel Calls• Examples

– Cout << x;– Seek (device, position);– X=ftime();

• User functions expand into assembly code for a "trap" or "svc" instruction

• "trap" causes a H/W switch to the kernel• Kernel performs op and returns to user

Page 17: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

© 2009, D. J. Foreman 17

System call example

fork (My_fork_loc);{ ●

●●

trap (K_FORK, *My_fork_loc);}

My_fork_loc:…;

*Do_fork

Do_fork(loc){ ●

start_process (loc);

mode=0;

return;

}

Traptable

*Do_fork

User space Kernel space

K_fork is entry # for "FORK"Kernel space

Page 18: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

x86 specific

Interrupts

© 2009, D. J. Foreman 18

Page 19: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Allowing x86 interrupts

• The x86 has an interrupt flag (IF) in the FLAGS register. Only for hardware interrupts.

• cli sets this flag to 0 - disabled• sti sets it to 1 - enabled• Instructions that load values into the FLAGS

register (such as popf and iret) may also modify this flag.

© 2009, D. J. Foreman 19

Page 20: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

X86 specific - 2

• int arg• int 0x04

– Calls interrupt handler #4, IF overflow flag is set

• int 0x80– Calls the service-call handler

© 2009, D. J. Foreman 20

Page 21: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Example: the setuid system call • Coded as: _syscall1(int,setuid, uid_t, uid); expands to:

_setuid: subl $4,%exppushl %ebx movzwl 12(%esp),%eax movl %eax,4(%esp) movl $23,%eax movl 4(%esp),%ebx int $0x80 --->trap into kernelmovl %eax,%edx --return from kerneltestl %edx,%edx jge L2 negl %edx movl %edx,_errno movl $-1,%eax popl %ebx addl $4,%esp ret

L2: movl %edx,%eax popl %ebx addl $4,%esp ret © 2009, D. J. Foreman 21

Page 22: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

z/390 specific

Interrupts

© 2009, D. J. Foreman 22

Page 23: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Allowing z/390 interrupts

• Program Status Word controls hardware interrupts– Bits 0-7– Bits 20-23

• (fixedpoint overflow, decimal overflow, exp underflow, significance)

• LPSW instruction loads all 64 bits of status• SSM instruction sets individual bits 0-7 only• See slides on setting Machine State

© 2009, D. J. Foreman 23

Page 24: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Reserved space in RAM

• Allows software/hardware interaction• Different for every machine architecture• Key to understanding of machine control• Key to understanding of Operating Systems

© 2009, D. J. Foreman 24

Page 25: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Contexts

© 2009, D. J. Foreman 25

Page 26: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

User Mode• Normal programs:

– (payroll, taxes, compilers, etc.)

• Cannot perform ANY privileged instructions• Cannot branch or jump into kernel• Kernel does not branch or jump to user code

– MUST use a “state switch” instruction

• Must use “exposed” functions via Service Calls

© 2009, D. J. Foreman 26

Page 27: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Kernel Mode• Can access ANY memory• Can use ANY instructions• NOT for doing “problem solving”• Manages users

– Pages– Access to CPU– Access to devices (disk, monitor, etc)

© 2009, D. J. Foreman 27

Page 28: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Context SwitchingFrom Kernel To User

• Set up values for new state vector• Save any kernel registers and stack data• Atomic state change

– Interrupts on– Privilege off– Memory protect on– Set IC

© 2009, D. J. Foreman 28

Page 29: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Context SwitchingFrom User to Kernel

• Set up values for service call• Issue service call (assembler instruction)• Atomic state change occurs

– Interrupts off– Privilege on– Memory protect off– Set IC to predefined interrupt handler in kernel

• Save any user’s registers and stack data

© 2009, D. J. Foreman 29

Page 30: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Reserved RAM• Defined in hardware• Used by kernel only• Same for ANY O/S on that type of machine:

– IBM-compatible PC– z/390– MAC– powerPC– Sun

© 2009, D. J. Foreman 30

Page 31: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Hex address IBM z/390 memory content0 IPL PSW or Restart PSW8 IPL CCW1 or Restart old PSW10 IPL CCW218 External Old PSW20 Supervisor Call Old PSW28 Program Check Old PSW30 Machine Check Old PSW38 I/O Old PSW58 External New PSW60 Supervisor Call New PSW68 Program Check New PSW70 Machine Check New PSW78 I/O New PSW80 External interrupt data88 (4 bytes) SVC interruption data: 13-14= ILC,

16-31= interruption code (SVC #)

paired

paired

© 2009, D. J. Foreman 31

Page 32: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

© 2009, D. J. Foreman 32

PC-bootable disk layout0x00-0x02 jump inst to 0x1e0x03-0x0a PC manufacturer name0x0b-0x0c sectors/cluster0x0d-0x0f reserved for boot record0x10-0x10 # of FAT's0x11-0x12 # root directory entries0x13-0x14 # logical sectors0x15-0x15 media descriptor0x16-0x17 sectors/FAT0x18-0x19 sectors/track0x1a-0x10b # surfaces (heads)0x1c-0x1d # hidden sectors0x1e-… boot program

Page 33: CS552 Overview © 2009, D. J. Foreman1. Generalized O/S structure Basic interrupt handler – Determines cause – Uses branch table to specific handler I/O.

Preparing for Interrupts (PC)• BIOS loads the initial address of the IDT table

into the idtr register• Linux init moves & re-inits the table

– setup_idt( ) – an assembly language function – fills all of idt_table with ignore(int)– 2nd pass – fills in true handlers

• Enable interrupts

© 2009, D. J. Foreman 33