win32 programming lesson 14: introducing windows memory (c rox…)

18
Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Upload: geoffrey-mitchell

Post on 05-Jan-2016

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Win32 ProgrammingLesson 14: Introducing Windows Memory(C Rox…)

Page 2: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Where are we? We’ve covered threads in glorious detail But to really take advantage of threads, we

need to know a lot more about Windows Memory Architecture Why?

Page 3: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Why Study Memory “How do I share data between applications?” “Where is this information stored?” “Why is my program so sloooooow?” Understanding how the system uses memory

helps us answer these questions and more…

Page 4: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Process Memory Every 32-bit process has its own virtual

address space 4GB (why?) How much for a 64-bit process? As the space is virtual, memory by default

isn’t shared between processes (but is between threads… why?)

Page 5: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Virtualization Two different processes can have data stored

at 0x12345678… but the values are different Each Virtual space is partitioned into different

areas

Page 6: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Null-Pointer Assignment If you point to a NULL pointer, you want

your code to throw an exception This is accomplished by the NULL-pointer

assignment partition space

Page 7: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

User-Mode Partition Contains the process’ private (unshared)

address space Not accessible from other Processes However, not all of this is directly useable by

the application

Page 8: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Kernel-mode partition Used by the Kernel for various process-

related things Everything in this partition is shared among

processes

Page 9: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Regions in Address Space When a process is created, most of its virtual

memory is free To use this space, must call VirtualAlloc Reserving memory happens in blocks called

“chunks” (allocation granularity, to use a fancy term)

Memory is reserved in multiples of the systems “page” size

Page 10: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

But… You must free memory if you use it Call VirtualFree

Page 11: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Physical Storage If every 32-bit process has 4GB of storage, don’t I

run out of memory? No… because of the paging file Transparent in operation Pages blocks of memory to disk from RAM So, Memory works a lot like this…

Page 12: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Paging

Page 13: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Thrashing When you’re really short of memory, an

application can thrash (demo…) Happens when the OS spends most of its time

fetching memory from disk Hence, to speed up your machine add RAM

(thanks Nate!)

Page 14: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Memory Protection It is possible to share physical

memory… Thus, you need to know about memory

protection attributes

Page 15: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Flags PAGE_NOACCESS – Any attempt to read, write or execute

raises a violation PAGE_READONLY PAGE_READWRITE PAGE_EXECUTE PAGE_EXECUTE_READ PAGE_EXECUTE_READWRITE PAGE_WRITECOPY – Attempts to write to this page causes

the application to get its own private copy of this page PAGE_EXECUTE_WRITECOPY

Page 16: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Copy On Write Interesting idea Memory is shared until you write Exists to conserve RAM and prevent thrashing When a write is detected:

OS finds a free page in RAM Copies the page to the new page and marks it

PAGE_READWRITE or PAGE_EXECUTE_READWRITE

Updates the process’ page tables

Page 17: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Special Flags PAGE_NOCACHE – Disable Cacheing on

this page (why… Hardware) PAGE_WRITECOMBINE – Multiple writes

are linked together PAGE_GUARD – alert when a write occurs

Page 18: Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Assignment Create a command line program in Visual Studio Write an application which allocates memory and

tries different protection schemes Show how the system throws exceptions when you

access this memory (for example, try and execute code from the memory location when PAGE_READONLY is set)

Use an exception handler to catch this exception Reset the PAGE to EXECUTE and show how the

exception is no longer thrown…