10/29/2015\course\cpeg323-08f\topic2d-323.ppt1 topic 2d high-level languages and systems software...

49
06/27/22 \course\cpeg323-08F\Topic2d-323.ppt 1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering (CPEG 323)

Upload: may-summers

Post on 04-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 1

Topic 2d High-Level languages and

Systems Software

(Memory Layout)

Introduction to Computer Systems Engineering

(CPEG 323)

Page 2: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 2

Reading List

• Slides: Topic2d

• Operating System and Compiler Books

• Other papers as assigned in class or home works

Page 3: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 3

Several Topics

Object File FormatGP register and GP areaProcess Memory ImageRun-time Stack Virtual / Physical memory

Page 4: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 4

Object File Format Compiler or assembler translates the

program into an object file, which is consequently linked into a executable file. These "object" files and "executable" files have a specific format.

Several common formats are:a.out: assembler and linker output formatCOFF: Common Object File FormatECOFF: Extended Common Object File Format ELF: Executable and Linking Format

Page 5: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 5

Object File Format(Cont.)a.out: assembler and linker output format A fairly primitive format, lacking some key

features to enable easy shared libraries, etc. On UNIX boxes, a.out is the default output

format of the system assembler and the linker. The linker makes a.out executable files. A file in a.out format consists of: a header, the program text, program data, text and data relocation information, a symbol table, and a string table (in that order).

Page 6: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 6

Object File Format(Cont.)

Common Object File Format (COFF) binary files COFF is a portable format for binary

applications on UNIX System V

Extended Common Object File Format (ECOFF) binary files Under Windows, Visual C, C++ and every

Windows compiler generates ECOFF files. MIPS

Page 7: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 7

Object File Format(Cont.)ELF: Executable and Linking Format ELF and COFF formats are very similar

but ELF has greater power and flexibility

Become the standard in file format ELF representation is platform

independent

Page 8: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 8

Object File Format(Cont.) Three main types of ELF files executable file

supplies information necessary for the operating system to create a process image.

relocatable file

describes how it should be linked with other object files to create an executable file or shared library.

shared object file contains information needed in both static

and dynamic linking.

Page 9: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 9

ELF Object File Format

ELF Format Linking and Execution Views

Page 10: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 10

ELF Object File Format(Cont.)

The ELF Header ELF Header is always the first section of the

file(The other sections can be in any order) What does the ELF Header describe? ● the type of the object file ● target architecture ● The location of the Program Header table,

Section Header table, and String table ● number and size of entries for each table in

the ELF ● the location of the first executable

instruction

Page 11: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 11

ELF Object File Format(Cont.)

The Program Header Table only important in executable and

shared object files It is an array of entries each entry is a structure describing a

segment in the object file The OS copies the segment into

memory according to the location and size information

Page 12: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 12

ELF Object File Format(Cont.)

The Section Header Table Has pointers to all sections in object

files It is similar to the program header Each entry correlates to a section in

the file. Each entry provides the name, type,

memory image starting address, file offset, the section’s size, alignment, and how the information in the section should be interpreted.

Page 13: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 13

ELF Object File Format(Cont.)

The ELF Sections Hold code, data, dynamic linking

information, debugging data, symbol tables, relocation information, comments, string tables, and notes.

Sections are treated in different ways ● loaded into the process image ● or provide information needed in

the building of a process image ● or are used only in linking object

files

Page 14: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 14

ELF Object File Format(Cont.)

The ELF Segments Group related sections ● text segment groups executable code, ● data segment groups the program data, ● dynamic segment groups information relevant

to dynamic loading. Each segment consists of one or more sections. A process image is created by loading and

interpreting segments. The OS logically copies a file’s segment to a

virtual memory segment according to the information provided in the program header table.

Page 15: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 15

Process Memory Image (32-bit Linux)

Data

BSS

Text0x00000000

OSReserved

0x7FFFFFFF

0x80000000

0xFFFFFFFF

codevariables with

initial values

variables without initial values

Why separate BSS and DATA?

Heap

Stack

1. initialized/uninitialized2. Image space

Page 16: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 16

GP Register and GP Area

Text

BSS

Stack

Heap

GPGlobal data area64k

Data

0x10008000

0x00000000

0xEFFFFFFF

positiveoffset

negativeoffset

Page 17: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 17

Why Global Data Area ?

Without GP: 3 instructions

Li r9, x -- low 16-bit of x

Addiu r9, x -- high 16-bit of x

Lw r10, 0(r9) -- load

With GP: 1 – instruction

LW r10, 24(GP) -- load

GP Register and GP Area(Cont.)

Load variable x to r10

Page 18: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 18

What should be put into Global Data Area ?

Most Frequently Access Data

• Global Data Area requires linker support• $gp register must be correctly initialized (by the startup routine)• assembly code must not modify the $gp register

How to Use Global Data Area ?

GP Register and GP Area(Cont.)

Page 19: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 19

Runtime StackStack organization

Local & temporariesSaved registers(including returnreg)

……

framesize

Frame offset

Virtual framePointer($fp)

High memory

low memory

stackPointer($sp)

Procedure callArgument area

argumentn

argument1

……

Page 20: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 20

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

i1=foo(4, s1);

printf(“i1= %d %s\n”, i1, s1);

}

Q: which variable in which section?

Page 21: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 21

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text foo(), main()

.rodata s1=“hello world”

.data i3

.bss i1, i2, p1

Stack

Heap

Page 22: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 22

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

i1=foo(4, s1);

printf(“i1= %d %s\n”, i1, s1);

}

Q: where is i4?

Page 23: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 23

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text foo(), main()

.rodata s1=“hello world”

.data i3

.bss i1, i2, i4, p1

Stack

Heap

Page 24: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 24

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world” i3=2 i1 i2I4p1

FPSPi1.0

Page 25: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 25

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world” i3=2 i1 i2I4p1

FPSPi1.0

Page 26: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 26

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FPSP

Hello world

i1.0

Page 27: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 27

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Hello world

i1.04copy of p1

Page 28: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 28

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Hello world

i1.04copy of p1Return address of main()callee saved registersp2

Page 29: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 29

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Hello world

i1.04copy of p1Return address of main()callee saved registersp2

Page 30: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 30

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Hello world

i1.04copy of p1Return address of main()callee saved registersp2

Hello world

Page 31: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 31

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Hello world

i1.04copy of p1

Page 32: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 32

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Hello world

i1.0 =15 4copy of p1

Page 33: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 33

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2)

{

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP SP

Hello world

Page 34: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 34

Runtime Execution Examplesint i1, i2[10], i3=2;

int *p1;

const char *s1=“hello world”;

int foo(int a1, char * a2){

static int i4;

char * p2;

p2=alloc(strlen(a2));

strcpy(p2, a2);

return a1+strlen(p2);

}

main() {

int i1;

p1=malloc(strlen(s1)+1);

strcpy(p1, s1);

i1=foo(4, p1);

printf(“i1= %d %s\n”, i1, s1);

free(p1);

}

.text

s1=“hello world”

i3=2

i1i2i4p1

FP SP

Hello worldMemory Leak

Page 35: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 35

Runtime Execution Examples.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Why do we need both SP and FP?

Can we only use one of them? (Under what kind of situation?)

Page 36: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 36

Runtime Execution Examples.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Why do we need both SP and FP?

Answer: because of “alloc()”

Can we only use one of them? (Under what kind of situation?)

Answer: if no dynamically allocated object on stack

Page 37: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 37

Runtime Execution Examples.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

In case both FP and SP are present, which one should be used to access variables on stack?

Page 38: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 38

Runtime Execution Examples.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

Offset is always limited (16-bit). One may not be able to access all the variables from one end, when?

LDD R9, OFFSET(FP)

Page 39: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 39

Runtime Execution Examples.text

s1=“hello world”

i3=2

i1i2i4p1

FP

SP

For 16-bit offset, when frame size >64KB, more instructions are needed

ADD R5, SP, OFFSET

LDD R9, 0(R5)

Page 40: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 40

Virtual / Physical memory

User memory spaceOS memory space

Page 41: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 41

Layout of Memory(virtual memory - user)

Text

Data

BSS

Stack

Heap

0x7FFF EFFF

0x0040 0000

Text: instructions

Data: variables with initial value

BSS: variables without initial value

HEAP: for malloc/free

STACK: for function call

Global Data

Page 42: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 42

Reserved

Static dataDynamic data

800000016

40 000016

Stack segment

Data segment

Text segment

Layout of Memory ( OS memory space)

For Interrupt vector

Firmware

Reserved for kernel

Page 43: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 43

Virtual Memory / Physical Memory

Why Virtual Memory Limited physical memory size

64MB to 1GB Unlimited virtual memory size

Each process may have 2GB Many processes in the system

Page 44: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 44

Virtual Memory/Physical Memory

Physical memory as cache of virtual memory (disk) Physical memory and virtual memory broke into fixed size pages; Each physical page holds a virtual page (may come from different

processes) Only the active pages of each process reside in physical memory,

physical memory works as cache of virtual memory (disk) Other pages stay on disk

Physical address Virtual address v rwx

Physical page Start address

Virtual page Disk address

Present bit Protection bits

Page tablePhysical pagei

P1: pagekP2: pagen Pn: pagem

Page 45: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 45

Virtual and Physical Memory

Physical Memory

OSOS

OSOSU1/P0U1/P0

U1/P1U1/P1

U1/P3U1/P3

U1/P4U1/P4

U1/P5U1/P5

U1/P6U1/P6

U1/P7U1/P7

U2/P0U2/P0

U2/P1U2/P1

U2/P2U2/P2

U2/P3U2/P3

U2/P4U2/P4

U2/P5U2/P5

U2/P6U2/P6

U2/P7U2/P7

Process 1 Process 2

Page 0

Page 1

Page 2

Page 3

Page 4

Page 5

Page 6

Page 7

U1/P2U1/P0

U1/P3

U1/P6

U1/P7

U2/P1

U2/P3

On Disk

Page 46: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 46

Process Memory Image

• what will happen with the following code? int x;

int * p = NULL;

*p = 12;

• why?

Invalid pointer – p points to arbitrary address (address 0?)

Page protection will assign “readable/executable” to the pages in this section

Page 47: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 47

Process Memory Image

.text

.rodata

.data.bss

Stack

Heap

Different page permissions:

.text -> read and execute

.rodata -> read

.data and .bss -> read and write

Stack: read, write and execute

Heap: read and write

0x0

2GB

Segmentation Fault

Page 48: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 48

#include <malloc.h>

main()

{

int *p;

p=(int *)malloc(sizeof(int));

*p=12;

}

Process Memory Image

Page 49: 10/29/2015\course\cpeg323-08F\Topic2d-323.ppt1 Topic 2d High-Level languages and Systems Software (Memory Layout) Introduction to Computer Systems Engineering

04/20/23 \course\cpeg323-08F\Topic2d-323.ppt 49

Summary

Object file formats (a.out, COFF, ECOFF, ELF)

Process memory image

Runtime stack

Mapping between Virtual memory and physical memory