memory management ii cs 470 - spring 2002. overview logical addressing and virtual memory –logical...

23
Memory Management II CS 470 - Spring 2002

Upload: cecil-nash

Post on 05-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Memory Management II

CS 470 - Spring 2002

Page 2: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Overview

• Logical Addressing and Virtual Memory– Logical to Linear Address Mapping– Linear to Physical Address Mapping

• NT Virtual Address Descriptors– What is a VAD?– Virtual Memory Functions– Example: Displaying the VAD splay– Example: How does the stack work?

Page 3: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Logical to Physical Mapping

Selector Segment OffsetLogical Address

Segment Translation

PG?

Dir Page Page Offset

Page Translation

Linear Address

Physical Address

Yes

NoControl Register 0, bit 31

031

31 0

31 0015

Page 4: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Linear to Physical Mapping

Dir Page Offset

0122231Linear Address

Dir Entry.

Page Directory

Pg Tbl Entry

Page Table

CR3

Physical Address

031Physical Address

Trans. Lookaside Buffer

misshit

Valid?

yes

Page FaultHandler

no

Page 5: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Page/Directory Table Entry

Page Frame Addr D ACD

RW

US

V

31 12 9 8 7 6 5 4 3 2 1 0

V ValidR/W Read / WriteU/S User / SupervisorW/T Write throughC/D Cache DisabledA AccessedD DirtyL Large pageGL Global

WT

GL

L

Page 6: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

VM Access Steps• Instruction references logical address

• Hardware looks up page table entry

• Valid PTE gives physical address

• Invalid PTE causes address exception (page fault)

• Handler copies page to memory from disk or net, updates PTE and restarts instruction. Now have valid PTE and so get physical address

• Physical address used to access cache

Page 7: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Virtual Memory Advantages

• Allows programs to be larger than physical memory, but more importantly it allows many more processes to be simultaneously active

• Page table entries allow for security with page level granularity

• But, much added complexity, especially danger of thrashing as memory is so much faster than disk access

Page 8: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

NT Process Structure

Process

AccessToken

Thread a

File c

Section f

Object Table

Virtual Address Space Description

Handle 1

Handle 2

Handle 3

Page 9: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Virtual Address Descriptors• Per process splay of VAD’s describes its

virtual address space

• VAD records location, security, and inheritance of a range of pages

• Each region can be free, reserved, or reserved and committed.– Reserved - No storage, Inaccessible, can’t

reserve a second time– Committed - Storage can be associated with

the region, can be accessible, PTE constructed on first access.

Page 10: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

VAD Information• Starting and Ending address for VAD

range; amount of committed memory

• Pointers to other VAD structures in splay

• Attributes– Is allocated memory committed?– Shared/private flag– Protection (cf next slide)– Copy-on-write enabled flag - For Posix fork()– Inherited by forked child? (for mapped views)– Mapped view of section object?

Page 11: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

VAD Protection Bits

• Combinations of the following: PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD, and PAGE_NOCACHE

• Allocation types:

MEM_RESERVE, MEM_COMMIT, MEM_TOP_DOWN

Page 12: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Virtual Memory Functions

• VirtualAllocateEx - To reserve or commit

• VirtualFreeEx - To de-commit or release

• VirtualProtectEx - To modify protection

• VirtualLock, VirtualUnlock - To lock pages into memory

• VirtualQueryEx - To get information on a region of memory

• GlobalMemoryStatus - To get summary information

Page 13: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Virtual Memory Allocation

LPVOID VirtualAllocEx(

HANDLE hProcess,

LPVOID lpAddress, // can be NULL

DWORD dwSize,

DWORD flAllocationType, // See last slide

DWORD flProtect // See last slide

);

Page 14: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Freeing Virtual Memory

• BOOL VirtualFreeEx(

HANDLE hProcess,

LPVOID lpAddress,

DWORD dwSize,

DWORD dwFreeType );

• Types: MEM_DECOMMIT, MEM_RELEASE

Page 15: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Changing Protection

• BOOL VirtualProtectEx(

HANDLE hProcess,

LPVOID lpAddress,

DWORD dwSize,

DWORD flNewProtect,

PDWORD lpflOldProtect );

Page 16: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Locking Pages into Memory

• BOOL VirtualLock(

LPVOID lpAddress,

DWORD dwSize );

• BOOL VirtualUnlock(

LPVOID lpAddress,

DWORD dwSize );

• At most 30 pages can be locked -- without changing minimum working set size.

Page 17: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

VAD Status Functions

• DWORD VirtualQueryEx(

HANDLE hProcess,

LPCVOID lpAddress,

PMEMORY_BASIC_INFORMATION lpBuffer, // See next

slide

DWORD dwLength );

• VOID GlobalMemoryStatus(

LPMEMORYSTATUS lpBuffer );

Page 18: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Memory Info Structure• typedef struct

_MEMORY_BASIC_INFORMATION {

PVOID BaseAddress;

PVOID AllocationBase;

DWORD AllocationProtect;

DWORD RegionSize;

DWORD State;

DWORD Protect;

DWORD Type; // e.g. MEM_PRIVATE

} MEMORY_BASIC_INFORMATION;

Page 19: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Summary Info Struct

typedef struct _MEMORYSTATUS {

DWORD dwLength; // of this struct

DWORD dwMemoryLoad;

DWORD dwTotalPhys, dwAvailPhys;

DWORD dwTotalPageFile;

dwAvailPageFile;

DWORD dwTotalVirtual, dwAvailVirtual;

} MEMORYSTATUS;

Page 20: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Example: mem.c• Use VirtualQueryEx to print out vad info• DWORD ShowRegion(

HANDLE hProcess, LPCVOID addr) {

MEMORY_BASIC_INFORMATION mbi;

if (!VirtualQueryEx(hProcess, addr, &mbi, sizeof(mbi))) {

Gripe(); return -1;

} else {

print_out_mbi (&mbi);

} }

Page 21: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

PAGE_GUARD Protection

• Visual C++ VirtualAlloc doc says --

Pages in the region become guard pages. Any attempt to read from or write to a guard page causes the operating system to raise a STATUS_GUARD_PAGE exception and turn off the guard page status. Guard pages thus act as a one-shot access alarm.

Page 22: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

How does the stack work?#include <stdio.h>

#include <windows.h>

void main() {

unsigned sptr;

__asm {

mov eax, esp

mov sptr, eax

}

printf("esp: 0x%x\n", sptr);

while (getchar()) { __asm { mov eax, esp sub eax, 4096 mov esp, eax mov sptr, eax mov eax, [esp] } printf("esp: 0x%x\n",

sptr); }}

Page 23: Memory Management II CS 470 - Spring 2002. Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address

Jumping over the Guard Page• void main() { char a[4096]; }• The assembly language is:

push ebp mov ebp, esp mov eax, 4096 call __chkstk mov esp, ebp pop ebp• See vc98\crt\src\intel\chkstk.asm in c:\

program files\Microsoft Visual Studio