Upload
jeremy-dorsey
View
222
Download
0
Embed Size (px)
Citation preview
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 1
Topic2c High-Level languages and
System Software(Toolchain)
Introduction to Computer Systems Engineering
(CPEG 323)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 2
Reading List
• Slides: Topic2c
• Operating System and Compiler Books
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 3
Tool Chain
toolchain A collection of system softwares
used to develop for a particular hardware target
If you designed a new processor, what is the basic system software tool set you need?
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 4
C program
Compiler
Assembly language program
Assembler
Object: Machine language module Object: Library routine (machine language)
Linker
Executable: Machine language program
Loader
Memory
A Typical Toolchain and its Translation Hierarchy
ToolchainToolchain
DebuggerUtility tools
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 5
Tool Chain
Two good examples SimpleScalar
www.simplescalar.com GNUPro
www.intel.com (Search GNUPro)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 6
Tool Chain
Basic Set:• Compilers: C, C++, Fortran, and etc.• Binary utilities: assembler, linker, objdump, ar, nm• Debugger• Simulator (functional / cycle-accurate)• Others: performance monitor (VTune of Intel)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 7
Tool Chaingcc –v –O0 –o foo foo.c
(old Step 0: cpp)
foo.i
Step 1: cc1 - compiler
foo.s
Step 2: as - assembler
foo.o
Step 3: ld - linker
foo
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 8
Tool Chain - Preprocessor
Functionality Header files Definitions Conditional compilation Pragma(Preprocessor Directives ) Delete the comments
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 9
Tool Chain - Compiler
Transform a program from high level language to assembly language (or machine language)Optimizations
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 10
Parameter Passing
• Caller save. The calling procedure (caller) is responsible for saving and restoring any registers that must be preserved across the call. The called procedure (callee) can then modify any register without constraint.
• Callee save. The callee is responsible for saving and restoring any registers that it might use. The caller uses registers without worrying about restoring them after a call.
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 11
Saving Registers
• If you call a function, whatever you have in $s0 to $s7 is guaranteed to be there when the function gets back to you
• But registers $t0 - $t9 are fair game to be reused by the function
• What are the alternatives?
- Save nothing?
- Save everything?
• Why caller/callee save ?
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 12
Why caller save / callee save?
If all caller save ? Even callee doesn’t kill any of the
saved registers – waste of cycles and memory resource
If all callee save ? Callee has to save all the register
(which will be used by callee), even caller doesn’t use them
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 13
Caller save
Add $10, $11,$12
Save $10, $12, $13
Jal B
Restore $10, $12, $13
Sub $11, $2, $12
Mul $12, $10, $13
Add $2, $4, $5
Br $31
Function A Function B
How to save ? - save to stack sw $10, 20(sp)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 14
Callee Save
Add $10, $11,$12
Jal B
Sub $11, $2, $12
Mul $12, $10, $13
Save $10, $11, $12
(if they are used in B)
Lw $10, 4(sp)
Add $11, $8, $9
Sub $12, $11, $10
Mul $2, $12, $11
Restore $10, $11, $12
Br $31
Function A Function B
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 15
Caller Save / Callee Save
When to use caller save register ? TEMPORARY VARIABLE Also called Scratch Register
When to use callee save register ? GLOBAL VARIABLE
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 16
Tool Chain - Assembler
Transform assembly code into binary (machine code)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 17
Tool Chain - Linker
Linking – resolve symbolsRelocation – assign memory address
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 18
Tool Chain - loader
Cannot see by userDone by Shell and OS kernel
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 19
Tool Chain – Library
Libc/Libm
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 20
Tool Chain
Objdump See the memory layout and sections Symbol table Disassembly code Relocation information
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 21
C program
Compiler
Assembly language program
Assembler
Object: Machine language module Object: Library routine (machine language)
Linker
Executable: Machine language program
Loader
Memory
Creating an Executable File(User view)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 22
Compiling/Assembling a Program
• Code converted from high-level to machine language (binary)
• Each source file converted to a separate “object file”- in Unix, object files have extension .o- This is not executable (yet)!- Intended to be combined with other modules, not stand-alone- May not have everything we need (e.g., a main () function)
• Functions not assigned specific locations in memory
• Each function given a “relocation table”
- This table tells exactly which addresses need to be resolved
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 23
Relocation Table
Suppose function f() in module a.c calls g() in b.c
• a.c should declare g with extern (directly or in .h file)
• Relocation table in a.o says something like: ”the jal at the 52nd instruction in f calls g, but I don’t know where g is.”
• Relocation table in b.o says something like: “I have a function g, which starts at location 628 in my file.”
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 24
Linking a program• Linker combines one or more object files into a proper executable
• Linker determines which functions needed, discards the rest
• All needed functions put one after the other in text segment
• Linker resolves all labels; for instance- Function f() in a.o calls g() in b.o- Linker knows where it put g(), so it fixes the jal in f()
• Linker includes extra code:- Initialization code before call to main()- “Cleanup” code after main() returns
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 25
Loading
A program that links without an error can be run. Before being run, the program resides in a file on secondary storage, such as a disk. On Unix system, the operating system kernel brings a program into memory and starts running.
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 26
Libraries• Libraries contain functions intended to be shared & reused, e.g.,
- C library: printf(), malloc(), strcmp(), sin(), cos()
- STL (Standard Template Library) in C++
- Big software projects may make their own libraries
• Static libraries (* .a in Unix) made part of the executable by linker
• Dynamic libraries (* .so in Unix, * .dll in Windows) combined at runtime
- Executable still has relocation table of unresolved function calls
- Loader does the final resolution when you execute the program
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 27
Dynamic vs. Static Library
Dynamic library: processes share one copy of the code Static library: each process has its own copy of the codeWhy?
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 28
Dynamic Shared Library ?
Also called dynamic linked library
Call printfCall printf Call printfCall printf
Program A Program B
Printf: Printf: DSO(dynamic shared
object )Table
DSO(dynamic shared
object )Table
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 29
Static Library
Call printfCall printf
Call printfCall printf
Program A Program B
Printf: Printf:
Printf: Printf:
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 30
Comparison
Dynamic Less memory space Less disk space Most of the case: Slower
Static More memory size More disk size Most of the case: faster
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 31
Debugger
Instruction level debuggerSource level debuggerMajor techniques Ptrace (POSIX API. on Linux/Unix
system) Embedded or raw machine
Software trap Single step mode
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 32
Debugger
• “Source-level” debugger lets you step through your source code
• Requires extra information attached to executable- Location and type of every function and variable- First instruction address corresponding to each line of
source
• Usually requires extra switches to compiler and linker, e.g., -g
• Two popular graphical debuggers are ddd and xxgdb (on ECE/CIS machines)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 33
The operating system performs the following steps:
1.Reads the executable file’s header to determine the size of the text and data segments.
2.“Establish” a new address space (e.g. via the creation of a new page table) for the program. This address space is large enough to hold the text and data segments, along with a stack segment
3.Copies instructions and data from the executable file into the new address space
Run a Executable File (OS View)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 34
4. Copies arguments passed to the program onto the stack.
5. Initializes the machine registers. In general, most registers are cleared but the stack pointer must be assigned the address of the first free stack location
6. Jumps to a start-up routine that copies the program’s arguments from the stack to registers and calls the program’s main routine. If the main routine returns, the start-up routine terminates the program with the exit system call.
(cont’d)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 35
Typical Layout of an Executable File
stack
Dynamic data
Reserved
Text
Static data
$sp 7fff ffffhex
$gp 1000 8000hex
1000 8000 hex
pc 0040 0000 hex
(From Patterson and Hennessy, p. 152; COPYRIGHT 1988 MORGAN KAUFMANN PUBLISHERS, INC. ALL RRIGHTS RESERVED)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 36
The Role of the OS KernelThe OS “kernel” performs the following essential functions:
• Manages resources (memory, disks, I/O) – mostly via “drivers”
• Switches between users (in a multi-user system such as copland)
• Provides convenient functions for applications to access resources
• Protects users from one another
• Provides essential “glue”, e.g., support for loaders
• For this to work efficiently, the CPU must have some support for the kernel.
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 37
Processor Support for the OS kernel
• Most processors have at least 2 distinct levels or “modes”:
- “Supervisor” (or “privileged” or “kernel”) mode
- “User” level (including “root” or “administrator”)
• Lower levels can’t do some things, e.g., access the disk drive
• CPU boots in kernel mode; drops to user mode to run user code
• Early micros (such as 8086) lacked such modes
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 38
Traps
So once we’re in user mode, how do we get back to the
privileged mode? Through “traps” – exceptional or unusual
conditions requiring intervention by the kernel:
• Hardware error (divide by 0 or access to illegal memory
address)
• Hardware “interrupt” (Ethernet card got data; mouse clicked)
• Clock signal telling multi-user OS to switch to another user
• “Software trap” when user code requests something from
kernel
• PC reaches value stored in special “breakpoint” register
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 39
Trap Handlers
When a trap occurs, the CPU:
• Sets bits in special “status” reg., indicating the cause of the
trap
• Switches to privileged mode
• Jumps to a “trap handler” (installed at boot time) at fixed
location
- Handler reads status bits and takes appropriate action
- Return address saved, like jal instruction
* When kernel is done, a special instruction return to the user
code, dropping into user mode automatically
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 40
Software Traps
To do just about anything on the system involving shared
resources (such as write to a file), the user code must ask the
kernel to do it!
• User code gets access to the kernel through “trap” instructions
• “System calls” provided for operations such as writing files
• A function call to a system call converted to a software trap
• Args passed in the usual way (e.g., $a0-$a3 in MIPS)
• In MIPS, use the “syscall” instruction
- No operands in assemble-language instruction
- Specify which system call you want by putting a value in $v0
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 41
System Calls
• POSIX standard defines system calls and their numbers
• For instance, call no. 4 is the write() function:
#include <unistd.h>
ssize_t write(int fildes,
cost void *buf, size_t nbyte);
Every open file is identified by a unique “file descriptor” (int)
• This function writes nbyte bytes, starting at address buf, to the
file
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 42
Example: Call to printf()
• User code a.c calls printf (“Answer is %d\n”, i);
• printf() declared as an extern function in stdio.h
• Compiler generates a.o with printf unresolved in relocation
table
• Data segment of a.o has string “Answer is %dl_” (NUL at
end)
- 14 bytes, with local label (e.g., L314) in relocation table
• Reference resolved when linked with libc (C library):
- By linker if statically (e.g., -Bstatic in Sun CC)
- B y loader if dynamically
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 43
Calling printf()
• Program sets args to printf (L314 and i)’ does jal printf
• printf (still in user mode) does the following:
- Creates new stack frame (as any non-leaf function should)
- Processes args; makes new string “Answer is 42l” in heap
- Creates args to write() function:
• Constant 1 in $a0 (file descriptor 1 is stdout)
• Address of heap string in $a1
• Constant 13 in $a2
- Puts constant 4 in $v0 and does a syscall instruction
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 44
Processing the Trap
• The CPU executes the syscall (trap) instruction:
- Switches to privileged mode
- Sets bits in status regs indicating trap caused by syscall
- Jumps to trap handler
• Trap handler checks status bits; sees trap came from syscall
• Checks call # in $v0; fetches 4th entry in function table and
jumps
• System call transfers 13 bytes to low-level driver
- Driver writes them to graphics display (if normal stdout)
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 45
Toolchain Review
Caller save register. The registers that the calling procedure (caller) is responsible for saving and restoring across the call. The called procedure (callee) can then modify the registers without constraint.Callee save register. The registers that the callee is responsible for saving and restoring if it might use. The caller uses the registers without worrying about restoring them after a call.
Caller save /Callee save register
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 46
Program Translates (Summary)
a.cInt I;…Printf(“Answer is %d”, i)…
compilea.s
.text…
Parameter pass
Jal printf
…
.data
assembly
a.o
323: Parameter pass
444: Jal reloc add.<printf>
Ref: <printf> 444
a.o- relocation table
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 47
Program Translates(cont.)
a.o323: Parameter pass
444: Jal reloc add.<printf>
ret
555:
Create stackProcess args$a0 <- file ID$a1 <- adds. Of heap$a2 <- length of string$v0 <- 4 (write)Syscallret
printf.o
Ref: <printf> 444
Def: <printf> 555
a.o- relocation table
printf.o- relocation table
L323: Parameter pass
L444: Jal L555
L555:
Create stackProcess args$a0 <- 1$a1 <- adds. Of heap$a2 <- 13$v0 <- 4Syscallret
linker
Executable file
Start-up routine
__main: jal main jal exit
04/21/23 \course\cpeg323-07F\Topic2c-323.ppt 48
Program Translates (Cont.)
323: Parameter pass
444: J reloc 555555:
Create stackProcess args$a0 <- 1$a1 <- adds. Of heap$a2 <- 13$v0 <- 4Syscallret.
Executable file
(software trap)
user mode privileged mode
Set status register
Jal trap(4) handler
trap(4) handlerwhat trap -- syscall
$v0 ? ------- 4
Jal 4th function driver
Transfer 13 bytes to graphic display
4th function driver
__main: jal main jal exit
main: