chapter 3 memory management. 3.1 from programs to address space 3 steps to run the programs of an...

15
Chapter 3 Memory Management

Upload: sterling-burkett

Post on 15-Dec-2015

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

Chapter 3 Memory Management

Page 2: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.1 From Programs To Address Space

• 3 steps to run the programs of an application– A Compiler translates the source code of each

module to its object module, – A linker links all the object modules to form a

single binary executable also known as load module,

– A loader loads the load module in the memory of the computer

Page 3: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

The Linker

• Each object module has its own sections of text, initialized data, symbol table and relocation information

• The task of the linker is to merge all the object modules into one load module

• This requires relocating the object modules

Page 4: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

Loader

• Logical address spaces– The starting address of this space is 0

• This logical address to a physical address– CPU and OS

Page 5: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.2 Memory Management Schemes

• Contiguous Allocation • Paging • Segmentation • Segmentation with Paging

Page 6: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.3 Swapping

• 略

Page 7: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.4 MIPS Simulator

• 3.4.1 Components of MIPS Machine

Page 8: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.4.1 Components of MIPS Machine

• The major components of the machine – user registers: int registers[NumTotalRegs]; – main memory: char *mainMemory; – page table for the current user process:

TranslationEntry *pageTable; – tlb (table look-ahead buffer): TranslationEntry *tlb;

Page 9: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

The machine operations

• 129 bool ReadMem(int addr, int size, int* value); • 130 bool WriteMem(int addr, int size, int value); • 116 int ReadRegister(int num); // read a register• 118 void WriteRegister(int num, int value); • 114 void Run(); // Run a user program ... • 124 void OneInstruction(Instruction *instr); • 142 void RaiseException(ExceptionType which, int

badVAddr); • 135 ExceptionType Translate(int virtAddr, int* physAddr, int

size,bool writing);

Page 10: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.4.2 Instruction Interpretation

• OneInstruction(Instruction*) see P46

Page 11: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.5 Nachos User Programs

• Use gcc MIPS cross compiler to produce the executable in the normal UNIXCOFF format

• Use program coff2noff made in directory ../bin/ to convert it to the Nachos NOFF format– NOFF format is the format of binary executables

used by Nachos. – It is similar to COFF format – See p48

Page 12: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

NOFF format• #define NOFFMAGIC 0xbadfad• typedef struct segment {

– int virtualAddr; /* location of segment in virt addr space */ – int inFileAddr; /* location of segment in this file */ – int size; /* size of segment */ 16 }

• } Segment;• typedef struct noffHeader {

– int noffMagic; /* should be NOFFMAGIC */ – Segment code; /* executable code segment */ – Segment initData; /* initialized data segment */ – Segment uninitData; /* uninitialized data segment */

• } NoffHeader;

Page 13: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.6 Address Space of User Process in Nachos

• Nachos uses paging• An address space is formed by calling the

constructor of AddrSpace• Nachos uses the a stub file system built on top

of the UNIX system, see p51.• Create address space– Page the table– Code – InitData

Page 14: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.7 Memory Management in Nachos

• Address translation see p54• both page table and TLB• the alignment of the logical address is checked• The page number and the offset of the logical

address are calculated• The TLB can translation generates a pagefault• Physical address is also checked

Page 15: Chapter 3 Memory Management. 3.1 From Programs To Address Space 3 steps to run the programs of an application – A Compiler translates the source code

3.8 From Thread to User Process

• After the thread constructs the address space and initializes the registers, it becomes a user process running on the simulated MIPS machine by calling machine->Run()

• If both the Nachos kernel and the user program were running on a real MIPS raw machine, a kernel thread becomes the user process by jumping to the first instruction of the code section of the address space