Lecture 02
Lecture 02
Arithmetic
Control
Registers Logic
Unit
Unit
Communication bus
The von Neuman
bottleneck
Memory (for
instructions I/O
and data)
An alternative is the Harvard architecture that uses separate buses and memory for
program instructions and data...
1
Structure of a Simple Computer
Monitor
Bus
2
What is an Interrupt?
• An event that transfers control to an interrupt
service routine generally, through the interrupt
vector, which contains the addresses of all the
service routines
• A trap is a software-generated interrupt caused
either by an error or a user request
• Modern operating systems are interrupt-driven
3
Interrupt Handling
• The interrupt handler preserves the state of the CPU by
storing registers
– The address of the interrupted instruction must be saved so that
interrupt handler can return to it
• The handler determines the type of interrupt that occurred
– There are a fixed number of interrupts for a CPU associated with
specific devices
• Separate segments of code determine what action should
be taken for each type of interrupt
– The OS stores interrupt handling routine addresses in an interrupt
vector generally indexed by device number
• Generally, interrupts are disabled while an interrupt is
being processed to prevent lost interrupts
4
Performing I/O
5
Asynchronous I/O Handling
6
Direct Memory Access (DMA) I/O
• Consider a slow I/O device, like terminal input
– A character may arrive every 1000 microseconds, perhaps
– An interrupt service/handler requires 2 microseconds
– This leaves 998 microseconds out of 1000 to do work
– A high speed device could seriously eat into CPU time
• DMA is required for high-speed devices
– The CPU sets up buffers, pointers, and counters for the I/O device
– The device controller transfers blocks of data from buffer storage
directly into main memory without CPU intervention
– Only one interrupt is generated per block, rather than the one
interrupt per byte or word
– Device contends with CPU for access to memory on the bus
Storage Structure
• Main memory is the only large storage media that
the CPU can access directly
• Secondary storage is an extension of main memory
that provides large nonvolatile storage capacity
– Magnetic disks are rigid metal or glass platters covered with
magnetic recording material
• Disk surface is logically divided into tracks, which are
subdivided into sectors
• The disk controller determines the logical interaction between
the device and the computer
7
Hard Disk Mechanism
Storage Hierarchy
• The top of the storage hierarchy is very fast, but very expensive
• The bottom of the storage hierarchy is very inexpensive, but very slow
• The types of storage also differ because the upper layers are volatile
(not persistent) where the lower layers are nonvolatile (persistent)
• In general, each lower layer of the hierarchical is used as a cache for
the layer above it
8
Structure of a Typical Computer
The following diagram depicts a more complex computer system
9
Intel Pentium Registers
Pentium integer registers
31 15 87 0
%eax %ah %al Accumulator
10
Intel x86 Assembler Example
main:
pushl %ebp # Save base pointer on stack.
movl %esp,%ebp # Use stack pointer as our new
# base pointer.
subl $24,%esp # Allocate some space on the
# stack for our variables.
movl $12,-4(%ebp) # Initialize variable i.
movl $2,-8(%ebp) # Initialize variable j.
movl -4(%ebp),%ecx # Retrieve value of i.
movl %ecx,%eax # Put value of i into %eax;
# the division command 64 bit
# %edx is high-order bytes and
# %eax is low-order bytes.
cltd # This sign extends %eax
# into %edx.
idivl -8(%ebp) # Divide the value of i in %edx
# and %eax by j.
# continued on next slide
11
Hardware Protection
• Early OSs dealt with one program at a time and
were not largely concerned with protection
• As OS sophistication increased so did the need to
protect program from one another
– Dual-Mode Operation
– I/O Protection
– Memory Protection
– CPU Protection
Dual-Mode Operation
• The OS must ensure that an incorrect program
cannot cause other programs to execute incorrectly
• The main approach to enable protection is to
provide hardware support to differentiate between
at least two modes of operations
1. User mode – execution done on behalf of a user
2. Monitor mode (also supervisor mode or system mode) –
execution done on behalf of operating system
12
Dual-Mode Operation
• A mode bit was added to the CPU to indicate the
current mode of operation: monitor (0) or user (1)
• When an interrupt or fault occurs hardware switches to
monitor mode
Interrupt/fault
monitor user
I/O Protection
• I/O instructions may disrupt normal computer
operations and must be protected
• As a result, all I/O instructions are privileged
• Must ensure that a user program could never gain
control of the computer in monitor mode (i.e., a
user program that, as part of its execution, stores a
new address in the interrupt vector)
13
Memory Protection
• Must provide memory protection at least for the
interrupt vector and the interrupt service routines
• In order to have memory protection, two registers
are added to the CPU that determine the range of
legal addresses a program may access
– The base register holds the smallest legal physical memory
address
– The limit register contains the size of the memory range
• Memory outside the defined range is protected
Memory Protection
The left side shows a single base/limit register set, it is also possible to
have two sets of registers for the program text (instructions) and the
program data; this allows sharing of program text
14
Memory Protection
CPU Protection
• Timer – interrupts computer after specified period
to ensure operating system maintains control
– Timer is decremented every clock tick
– When timer reaches the value 0, an interrupt occurs
• Timer commonly used to implement time sharing
• Time also used to compute the current time
• Load-timer is a privileged instruction
15
System Calls
• Given the I/O instructions are privileged, how does
the user program perform I/O?
• System call is the method used by a process to
request action by the operating system
– Usually takes the form of a trap to a specific location in the
interrupt vector
– Control passes through the interrupt vector to a service
routine in the OS, and the mode bit is set to monitor mode
– The monitor verifies that the parameters are correct and
legal, executes the request, and returns control to the
instruction following the system call
• More on these in next lecture...
16