linux & memory maps - uet lahore · don’t confuse physical & virtual address spaces •...

29
Linux & Memory Maps 1 Presented by : Abdul Qadeer Presented in Open Source Series Workshop 2010 22-24 December, 2010 © ICOSST 2010

Upload: others

Post on 23-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Linux & Memory Maps

1

Presented by : Abdul Qadeer

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 2: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

• Virtual address space

– No one understands that outside the processor!

• Physical address space

Don’t confuse physical & virtual address spaces

• Physical address space

– You need that to do memory operations and many

other I/O operations

2

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 3: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Don’t confuse physical & virtual address spaces

int x = 25;

int address_of_x = &x;

3

The C / C++ address of operator (&). Does it

return a virtual address or a physical address?

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 4: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Don’t confuse physical & virtual address spaces

• VAS > PAS– Probably a normal scenario

• VAS == PAS– E.g. 32 bit VAS and 4GiB installed DRAM

– Some DRAM might go un-used and hence wasted!

4

– Some DRAM might go un-used and hence wasted!

• VAS < PAS– Not a common scenario

– Might be used to physically isolate DRAM used by programs

– Physical address extension; 32 bit VAS, 36bit PAS

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 5: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Don’t confuse physical & virtual address spaces

Can look VAS and PAS in /proc/cpuinfo

5

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 6: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Memory mapped I/O

• Part of PAS assigned to a device

– Writing to this mem => writing to device

– Reading from this mem => reading from device

– Devices negotiate for their required MMI/O region

• Old, dumb devices (e.g. ISA devices)

• Newer, smarter devices (e.g. PCIe devices)

6

• DMA concerns

• MMI/O usage needs special care

– No compiler optimization (e.g. by using –O0)

– No pipeline reordering (use Linux read / write barriers)

– Disable caching (e.g. using bits in TLB)

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 7: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Memory mapped I/O

• MMI/O PAS not usable as normal DRAM

– MMI/O does not take away actual DRAM!

– But does take away a region of PAS

– The corresponding DRAM might go wasted

7

• Relocate actual DRAM to elsewhere

– VAS should be big enough for relocation

– Chipset should allow relocation

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 8: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Memory mapped I/O

Can look PAS allocation in /proc/iomem

8

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 9: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

9Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Picture taken from Gustavo Duarte blog accessible at: http://duartes.org/gustavo/blog/post/motherboard-chipsets-memory-map

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 10: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Processor

10Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Picture taken from Gustavo Duarte blog accessible at: http://duartes.org/gustavo/blog/post/motherboard-chipsets-memory-map

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 11: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Processor

Memory & Graphics

Controller Hub

11Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Picture taken from Gustavo Duarte blog accessible at: http://duartes.org/gustavo/blog/post/motherboard-chipsets-memory-map

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 12: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Processor

Memory & Graphics

Controller Hub

Memory

12Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Picture taken from Gustavo Duarte blog accessible at: http://duartes.org/gustavo/blog/post/motherboard-chipsets-memory-map

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 13: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

Processor

Memory & Graphics

Controller Hub

Memory

13Pro

cess

or,

N

ort

h

&

So

uth

Bri

dg

es

I/O Controller Hub

Picture taken from Gustavo Duarte blog accessible at: http://duartes.org/gustavo/blog/post/motherboard-chipsets-memory-map

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 14: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

MTRR registers

• Memory region registration process

– MTTR registers used

– BIOS tells some regions initially

– OS tries to find out the optimal registration

14

• Looking Linux log in dmesg

– Shows interesting system information

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 15: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

MTRR registers

15

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 16: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

MTRR registers

16

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 17: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

MTRR registers

17

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 18: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

How an Intel processor starts from 0XFF FF FF F0

Reset vector

18Picture taken from Gustavo Duarte blog accessible at: http://duartes.org/gustavo/blog/post/how-computers-boot-up/

All grey stuff is

memory mapped

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 19: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

How an Intel processor starts from 0XFF FF FF F0

Reset vector

Jump to main BIOS

19Picture taken from Gustavo Duarte blog accessible at: http://duartes.org/gustavo/blog/post/how-computers-boot-up/

All grey stuff is

memory mapped

This is due to X86

legacy

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 20: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

VAS distribution among Kernel and a user space process

• 3G/1G split

– Theoretically 2^32 VAS available to a process

– Kernel is mapped for every process for

performance reasons

– Can change this split at kernel compile time to say

20

– Can change this split at kernel compile time to say

2G/2G or by a patch to 4G/4G

• Half / Half split in 64 bit systems

– 128 TB for processes and Kernel

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 21: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

VAS distribution among Kernel and a user space process

21

32bit System 64bit SystemPicture taken from http://blogs.technet.com/b/markrussinovich/archive/2008/11/17/3155406.aspx?PageIndex=6 and http://en.wikipedia.org/wiki/X86-64

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 22: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Process address space

• Process can use almost any part of user-VAS

22

Figure taken from Virtual Machines, versatile platforms for systems and processes, ISBN-10: 1558609105

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 23: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Process address space

• Process can use almost any part of user-VAS

• Application binary interface (ABI)

– Conventions

23

Figure taken from Virtual Machines, versatile platforms for systems and processes, ISBN-10: 1558609105

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 24: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Process address space

• Process can use almost any part of user-VAS

• Application binary interface (ABI)

– Conventions

• Binary file format

24

– ELF, COFF

• Language run-time

– C, C++ etc.

• OS conventions

– Loader, linker

Figure taken from Virtual Machines, versatile platforms for systems and processes, ISBN-10: 1558609105

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 25: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Process address space

• Process can use almost any part of user-VAS

• Application binary interface (ABI)

– Conventions

• Binary file format

25

– ELF, COFF

• Language run-time

– C, C++ etc.

• OS conventions

– Loader, linker

Figure taken from Virtual Machines, versatile platforms for systems and processes, ISBN-10: 1558609105

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 26: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

• Not everything in VAS is mapped

– Address 0 is invalid

– Guard page between stack and

heap

– Similar guard pages for thread

Guard pages

– Similar guard pages for thread

stack

26

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 27: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

• See process virtual address map

– cat /proc/1/maps or cat /proc/self/maps

Guard pages

Address Range Rights Offset Device Inode File Associated

27

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 28: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

• Use Linux

– May be via live CDs

• Compare address spaces

Learn how your computer is using memory

• Compare address spaces

– 32 bit

– 64 bit

– Specially interesting if you have 4 GiB or more

installed memory

28

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010

Page 29: Linux & Memory Maps - UET Lahore · Don’t confuse physical & virtual address spaces • VAS > PAS – Probably a normal scenario • VAS == PAS – E.g. 32 bit VAS and 4GiB installed

Linux & Memory Maps

29

Presented in Open Source Series Workshop 2010

22-24 December, 2010 © ICOSST 2010