CS552 Overview © 2009, D. J. Foreman 1 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 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 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 Kernel Basics • • • • • • States State vectors Interrupts Contexts Context switching Reserved RAM © 2009, D. J. Foreman 5 Remainder of Course • • • • • • • • Paging Scheduling & Dispatching Virtualization Multi-tasking File & storage systems Deadlock Security & reliability Distributed systems © 2009, D. J. Foreman 6 Basic Computer Architecture © 2009, D. J. Foreman 7 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 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 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 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 Interrupts © 2009, D. J. Foreman 12 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 – SVC © 2009, D. J. Foreman 0x80 n PC style (‘n’ is on stack) z/390 style 13 Instruction Processing with Interrupts No No previous inst fetch execute Interrupts allowed? yes Interrupt pending? yes process interrupt © 2009, D. J. Foreman 14 Trap or System Call Instruction • 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 © 2009, D. J. Foreman 15 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 © 2009, D. J. Foreman 16 System call example User space Kernel space fork (My_fork_loc); { ● ● ● trap (K_FORK, *My_fork_loc); } My_fork_loc:…; Trap table Do_fork(loc) *Do_fork { ● ● start_process (loc); mode=0; return; } Kernel space K_fork is entry # for "FORK" © 2009, D. J. Foreman 17 Interrupts x86 specific © 2009, D. J. Foreman 18 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 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 Example: the setuid system call • Coded as: _syscall1(int,setuid, uid_t, uid); expands to: _setuid: subl pushl movzwl movl movl movl int movl testl jge negl movl movl popl addl ret L2: movl popl addl © 2009, D. J. Foreman ret $4,%exp %ebx 12(%esp),%eax %eax,4(%esp) $23,%eax 4(%esp),%ebx $0x80 %eax,%edx %edx,%edx L2 %edx %edx,_errno $-1,%eax %ebx $4,%esp --->trap into kernel --return from kernel %edx,%eax %ebx $4,%esp 21 Interrupts z/390 specific © 2009, D. J. Foreman 22 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 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 Contexts © 2009, D. J. Foreman 25 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 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 Context Switching From 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 Context Switching From 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 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 Hex address 0 8 10 18 20 28 30 38 58 60 68 70 78 80 88 (4 bytes) © 2009, D. J. Foreman IBM z/390 memory content IPL PSW or Restart PSW IPL CCW1 or Restart old PSW IPL CCW2 External Old PSW Supervisor Call Old PSW paired Program Check Old PSW Machine Check Old PSW I/O Old PSW External New PSW Supervisor Call New PSW Program Check New PSW Machine Check New PSW paired I/O New PSW External interrupt data SVC interruption data: 13-14= ILC, 16-31= interruption code (SVC #) 31 PC-bootable disk layout 0x00-0x02 0x03-0x0a 0x0b-0x0c 0x0d-0x0f 0x10-0x10 0x11-0x12 0x13-0x14 0x15-0x15 0x16-0x17 0x18-0x19 0x1a-0x10b 0x1c-0x1d 0x1e-… © 2009, D. J. Foreman jump inst to 0x1e PC manufacturer name sectors/cluster reserved for boot record # of FAT's # root directory entries # logical sectors media descriptor sectors/FAT sectors/track # surfaces (heads) # hidden sectors boot program 32 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