Download Computer Organization CS224

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Organization
CS224
Fall 2012
Lesson 12

Two processors or threads sharing an area of memory
l
P1 writes, then P2 reads
l
Data race if P1 and P2 don’t synchronize
- Result depends of order of accesses !!


Software synchronization routines needed
l
Lock and unlock, etc.
l
To implement mutual exclusion to shared memory
Hardware support required
l
l
l
l
l
Atomic read-and-modify memory operation
No other access to the location allowed between the read and write
Used for e.g. atomic swap of register ↔ memory
Could be a single instruction (as in some processors)
Or an atomic pair of instructions (as in MIPS and others)
§2.11 Parallelism and Instructions: Synchronization
Synchronization
Atomic Exchange Support

Atomic exchange (atomic swap) – interchanges a value
in a register for a value in memory atomically, i.e., as one
operation (instruction)
l
Implementing an atomic exchange would require both a memory
read and a memory write in a single, uninterruptable instruction.
An alternative is to have a pair of specially configured
instructions
ll
$t1, 0($s1)
#load linked
sc
$t0, 0($s1)
#store conditional
Atomic Exchange with ll and sc

If the contents of the memory location specified by the
ll are changed before the sc to the same address
occurs, the sc fails (returns a zero)
try:
add $t0, $zero, $s4
ll $t1, 0($s1)
sc $t0, 0($s1)
beq $t0, $zero, try
add $s4, $zero, $t1

#$t0=$s4 (exchange value)
#load memory value to $t1
#try to store exchange
#value to memory, if fail
#$t0 will be 0
#try again on failure
#load value in $s4
If the value in memory between the ll and the sc
instructions changes, then sc returns a 0 in $t0 causing
the code sequence to try again.
C program
compiler
assembly code
assembler
object code
library routines
linker
machine code
executable
loader
memory
§2.12 Translating and Starting a Program
The C Code Translation Hierarchy
Assembler Pseudoinstructions

Most assembler instructions represent machine
instructions one-to-one

Pseudoinstructions: figments of the assembler’s
imagination 
move $t0, $t1
→
add $t0, $zero, $t1
blt $t0, $t1, L
→
slt $at, $t0, $t1
bne $at, $zero, L
l
$at (register 1): assembler temporary
Producing an Object Code Module

Assembler (or compiler) translates program into machine
instructions, makes an object module

Provides information for building a complete program
from the pieces
l
Header: described contents of object module
l
Text segment: translated instructions
l
Static data segment: data allocated for the life of the
program
l
Relocation info: for contents that depend on absolute
location of loaded program
l
Symbol table: global definitions and external refs
l
Debug info: for associating with source code
Linking Object Modules

Produces an executable image (.exe file)
1. Merges segments
2. Resolve labels (determine their addresses)
3. Patch location-dependent and external refs

Could leave location dependencies for fixing later with a
relocating loader
l
l
But with virtual memory, no need to do this
Program can be loaded into absolute location in virtual memory
space
Loading a Program

Load from image file on disk into memory
1. Read header to determine segment sizes for text and data
2. Create virtual address space (large enough for text and data)
3. Copy text and initialized data into memory
4. Copy parameters for main program (if any) onto stack
5. Initialize registers (including $sp, $fp, $gp)
6. Jump to startup routine
- Copies arguments to $a0, … and calls main
- When main returns, do exit syscall
Dynamic Linking

Only link/load library procedure when it is called
l
Requires procedure code to be relocatable
l
Avoids image bloat caused by static linking of all
(transitively) referenced libraries
l
Automatically picks up new library versions
Lazy Linkage
Indirection table
Stub: loads routine ID,
jumps to linker/loader
Linker/loader code
Dynamically
mapped code
Starting Java Applications
Simple portable
instruction set for
the JVM
Compiles
bytecodes of
“hot” methods
into native
code for host
machine
Interprets
bytecodes