Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Stack Discipline - Operating System Design and Implementation - Lecture Slides, Slides of Operating Systems

This lecture is from Operating System Design and Implementation course. Its key words are: Stack Discipline, Registration, Process Memory Model, Ia32 Stack Organization, Register Saving Conventions, Project 0, Linux Memory Layout, Stack Operation, Procedure Return, Linux Stack Frame, Register Saving Conventions, Mysterious Parts, Data Flow

Typology: Slides

2013/2014

Uploaded on 02/01/2014

sashie
sashie 🇮🇳

4.2

(40)

185 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
115-410, F’13
Stack Discipline
Aug. 28, 2013
Dave Eckhardt
Dave Eckhardt
Todd Mowry
Todd Mowry
Slides originally stolen from 15-213
Slides originally stolen from 15-213
15-410
“An Experience Like No Other”
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download Stack Discipline - Operating System Design and Implementation - Lecture Slides and more Slides Operating Systems in PDF only on Docsity!

Stack Discipline

Aug. 28, 2013

Dave Eckhardt Dave Eckhardt

Todd Mowry Todd Mowry

Slides originally stolen from 15-213 Slides originally stolen from 15-

“An Experience Like No Other”

Movie Night

“ “Primer”Primer”

 Thursday, August 29th

 19:30, GHC 4401 (“Rashid Auditorium”)

 $1 Pizza

 Presented by #!/cmu/cc

 Funded in part by your Student Activities Fee

Outline

Topics Topics

 Process memory model

 IA32 stack organization

 Register saving conventions

 Before & after main()

 Project 0

Why Only 32?

You may have learned x86-64 aka EMT64 aka AMD64 You may have learned x86-64 aka EMT64 aka AMD

 x86-64 is simpler than x86(-32) for user program code

 (^) Lots of registers, registers more orthogonal

Why will 410 be x86 / IA32? Why will 410 be x86 / IA32?

 x86-64 is not simpler for kernel code

 (^) Machine begins in 16-bit mode, then 32, finally 64 » You don't have time to write 32⇒64 transition code » If we gave it to you, it would be a^ big^ black box

 x86-64 is not simpler during debugging

 (^) More registers means more registers to have wrong values

 x86-64 virtual memory is a bit of a drag

 (^) More steps than x86-32, but not more intellectually stimulating

 There are still a lot of 32-bit machines in the world

 (^) ...which can boot and run your personal OS

Linux Memory Layout

Stack Stack

 (^) Runtime stack (8MB limit by default)

Heap Heap

 (^) Dynamically allocated storage  (^) Managed by malloc() , calloc() , new

Shared/Dynamic Libraries aka Shared Objects Shared/Dynamic Libraries aka Shared Objects

 (^) Library routines (e.g., printf() , malloc() )  (^) Linked into object code when first executed  (^) Windows has “DLLs” (semantic differences)

Data, BSS Data, BSS

 (^) Statically allocated data (BSS starts all-zero)  (^) e.g., arrays & variables declared in code

Text, RODATA Text, RODATA

 (^) Text - Executable machine instructions  (^) RODATA – Read-only (e.g., “ const ”)  (^) String literals Upper 2 hex digits of address Red Hat v. 6. ~1920MB memory limit

FF

BF

7F

3F

C

Stack

Shared

Libraries

Text

Data

Heap

Heap

Linux Memory Allocation

Linked

BF

7F

3F

Stack Libraries Text Data 08

Some

Heap

BF

7F

3F

Stack Libraries Text Data Heap 08

More

Heap

BF

7F

3F

Stack Libraries Text Data Heap Heap 08

Initially

BF

7F

3F

Stack Text Data 08

IA32 Stack Pushing

Pushing Pushing

 pushl Src

 Fetch operand from Src

 (^) Maybe a register: %ebp  (^) Maybe memory: 8(%ebp)

 Decrement %esp by 4

 Store operand in memory at

address given by %esp

Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Stack Pointer %esp -

IA32 Stack Popping

Popping Popping

 popl Dest

 Read memory at address

given by %esp

 Increment %esp by 4

 Store into Dest operand

Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom”

13 15-410, F'

Procedure Control Flow

 Use stack to support procedure call and return

Procedure call: Procedure call:

call label Push return address; Jump to label

“ “Return address”?Return address”?

 Address of instruction after call

 Example from disassembly

8048 54e: e8 3d 06 00 00 call 8048b90

8048553: 50 pushl %eax

 (^) Return address = 0x

Procedure return: Procedure return:

ret Pop address from stack; Jump to address

%esp %eip %esp %eip 0x804854e 0x 0x 0x10c 0x 0x 0x804854e 0x

Procedure Call Example

0x 0x10c 0x 123 0x call 8048b 804854e: e8 3d 06 00 00 call 8048b90

8048553: 50 pushl %eax 0x8048b 0x %eip is program counter

Stack-Based Languages

Languages that support recursion Languages that support recursion

 e.g., C, Pascal, Java

 Code must be “ reentrant ”

 (^) Multiple instantiations of a single procedure “live” at same time

 Need some place to store state of each instantiation

 (^) Arguments  (^) Local variables  (^) Return pointer (maybe)  (^) Weird things (static links, exception handling, …)

Stack discipline – key observation Stack discipline – key observation

 State for given procedure needed for limited time

 (^) From time of call to time of return

 Note: callee returns before caller does

Therefore stack allocated in nested Therefore stack allocated in nested framesframes

 State for single procedure instantiation

Call Chain Example

Code Structure Code Structure

yoo(…) {

**-

who();

} who(…) {

  • • • amI();
  • • • amI();
  • • • } amI(…) {

amI();

} yoo who amI amI amI**

Call Chain

 Procedure amI()

recursive

amI

IA32/Linux Stack Frame

Current Stack Frame (“Top” Current Stack Frame (“Top”

to “Bottom”) to “Bottom”)

 Parameters for function

we're about to call

 (^) “Argument build”

 Local variables

 (^) If don't all fit in registers

 Caller's saved registers

 Caller's saved frame pointer

Caller's Stack Frame Caller's Stack Frame

 Return address

 (^) Pushed by call instruction

 Arguments for this call

Stack Pointer ( %esp ) Frame Pointer ( %ebp ) Return Addr Saved Registers

Local Variables Argument Build Old %ebp Arguments Caller Frame

swap()

**void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; xp = t1; yp = t0; } int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } call_swap:

**- • • pushl $zip2 # Global var pushl $zip1 # Global var call swap

  • • •** &zip &zip Rtn adr %esp

Resulting

Stack

Calling swap from call_swap