Professional Documents
Culture Documents
2
x86 address translation
Logical
Address
(segment Linear
+
Physical
CPU offset) Segmentation Address Paging Address
Unit Unit
Physical
Memory
3
x86 address translation
Logical
Address
(segment Linear
+
Physical
CPU offset) Segmentation Address Paging Address
Unit Unit
Physical
Memory
4
Segmentation Unit
• Virtual address space of process divided into
separate logical segments
• Each segment associated with a segment
selector and offset Heap
Stack
Heap
Stack Data
Data
Text Text
6
Example
0x3000
0x3000 Segment Base Limit
4500
Data 4000
3000
1 2000
+
segment register (eg %CS) Text
1000
100
0
offset register (eg %eip) Address Map of Process
7
Pointer to Descriptor Table
• Global Descriptor Table (GDT)
• Stored in memory
• Pointed to by GDTR (GDT Register)
– lgdt (instruction used to load the GDT register)
• Similar table called LDT present in x86 (not used by xv6!)
Segment Descriptor
Segment Descriptor
base size Segment Descriptor
47 16 0 Segment Descriptor
Segment Descriptor
GDTR
Segment Descriptor
Size : size of GDT GDT
Base : pointer to GDT
8
Segment Descriptor
• Base Address
Access Limit
– 0 to 4GB
Base Address
• Limit
– 0 to 4GB
• Access Rights
– Execute, Read, Write
– Privilege Level (0-3)
9
Segment Registers
• Holds 16 bit segment selectors
– Points to offsets in GDT
• Segments associated with one of three types of storage
– Code
• %CS register holds segment selector
• %EIP register holds offset
– Data
• %DS, %ES, %FS, %GS registers hold segment selector
– Stack
• %SS register holds segment selector
• %SP register holds stack pointer
(Note: Only one code segment and stack segment can be accessible
at a time. But 4 data segments can be accessed simultaneously)
10
x86 address translation
Logical
Address
(segment Linear
+
Physical
CPU offset) Segmentation Address Paging Address
Unit Unit
Physical
Memory
11
Linear to Physical Address
• 2 level page translation • How many page
tables are
present?
• What is the
maximum size of
the process’
address space?
– 4G
Each proces
Initialized once
has one
common for all
processes
13
Virtual Address Advantages
(Isolation between Processes)
Process A Process B
Stack Stack
Heap Heap
Process A Process B
Data Page Page Data
Table Table
Text Text
(instructions) (instructions)
14
Virtual Addressing Advantages
Paging on Demand
• RAM only loads pages into memory whenever needed
• When new program is executed page table is empty
Present/absent
0 0 0 0
1 0 0
1 2 0 0
2 3 0 0
4 0 0
3 5 0 0 0
6 0 0
4 1
Page table
5 for process 2
(is stored in RAM)
6 3
16
Paging on Demand(3)
• As execution progresses, more entries in page table get filled.
• Similarly, more frames in RAM get used
• Eventually, entire RAM is filled
What next?
Present/absent
0 0 1 1
1 0 0
1 2 1 3
2 3 1 2
4 1 0
3 5 0 0 0
6 0 0
4 1
Page table
5 for process 2
(is stored in RAM)
6 3
17
Paging on Demand (4)
• A particular frame is selected and swapped out into disk
• A new page swapped in
Disk
• Page table is updated (swap space)
Present/absent
0 0 0 1 Swap in
1 0 0
1 2 1 3 Swap out
2 3 1 2
4 1 0
3 5 0 0 0
6 1 1
4 1
Page table
5 for process 2
(is stored in RAM)
6 3
18
Virtual Memory achieves Security
• Security
– Page tables augmented by protection bits
protection bits
Page table
for process
19
Protection Bits in x86
20
Virtual Addressing Advantages
(easy to make copies of a process)
• Making a copy of a process
is called forking.
Parent
– Parent (is the original) Page
Table
– child (is the new process)
• When fork is invoked,
– child is an exact copy of
parent
Child
• When fork is called all pages Page
are shared between parent Table
and child Physical Memory
• Easily done by copying the
parent’s page tables
21
Virtual Addressing Advantages
(Shared libraries)
• Many common functions such as printf implemented in shared libraries
• Pages from shared libraries, shared between processes
Process A Process B
Process A Process B
Page Page
Table Table
printf(){ …}
printf(){ …} printf(){ …}
kernel
23
back to booting…
24
so far…
Power on Reset
• executes on reset.
• does POST, initializes devices
BIOS
• loads boot loader to 0x07c00 and jump to it
(all in real mode)
bootloader
• disable interrupts
• Setup GDT (8941)
• switch real mode to protected mode
• setup an initial stack (8967)
• load kernel from second sector of disk to
0x100000
• executes kernel (_start)
25
Memory when kernel is invoked
(just after the bootloader)
• Segmentation enabled but no paging
Segmentation physical
CPU
logical Unit physical memory
stack kernel
code logical
data memory
physical memory
0x100000
bootloader
• Advantages
– Got the kernel into protected mode (32 bit code) with minimum
trouble
• Disadvantages
– Protection of kernel memory from user writes
– Protection between user processes
– User space restricted by physical memory
• The plan ahead
– Need to get paging up and running
27
OS code are not Relocatable
• kernel.asm (xv6)
• The linker sets the
executable so that
the kernel starts
from 0x80100000
• 0x80100000 is a
virtual address and
not a physical
address
28
Virtual Address Space
0xffffffff Virtual
• Kernel memory mapped into every process
Device memory - easy to switch between kernel and user modes
• VA(KERNBASE:+PHYSTOP) PA(0:PHYSTOP)
- convert from VA to PA just by +/- KERNBASE
- easily write to physical page
Kernel Memory - limits size of physical memory to 2GB
+0x100000
KERNBASE Physical
0x80000000
PHYSTOP
0x100000
0
0
ref : memlayout.h (0200) 29
Converting virtual to physical
in kernel space
What would be the address generated before and immediately after paging is
enabled?
before : 0x001000xx
Immediately after : 0x8001000xx
So the OS needs to be present at two memory ranges
30
Early Kernel Paging Initialization
• Kernel entry point : _start (1036)
Turn on Page size
extension
31
4MB Pages
32
Kernel memory setup
• First setup two 4MB pages
– Entry 0:
Virtual addresses 0 to 0x04000000 Physical addresses 0 to
4MB
– Entry 512:
Virtual addresses 0x80000000 to 0x84000000
Physical addresses 0 to 4MB
33
First Page Table
logical
memory
virtual memory
physical memory
Enable Paging
35
Stack setup
• Entry point : _start (1036)
Turn on Page size
extension
Enable Paging
Stack setup
36
Stack
Enable Paging
Stack setup
Jump to main
38
New Address Scheme Analysis
Scheme : enable paging with 2 pages of 4MB each
• Advantages,
– Useful for initializing the rest of memory
• (issues with kmalloc …. later!!!)
• Disadvantages
– Kernel mapped twice, reducing user space area
– Only 4MB of physical memory is mapped. Remaining
is unutilized
39
(Re)Initializing Paging
• Configure another page table
– Map kernel only once making space for other
user level processes
– Map more physical memory, not just the first
4MB
– Use 4KB pages instead of 4MB pages
• 4MB pages very wasteful if processes are small
• Xv6 programs are a few dozen kilobytes
40
Virtual Address Space
KERNBASE = 0x80000000
KERNLINK = KERNBASE + 0x100000
PHYSTOP = 0xE000000
EXTMEM = 0x100000
1823
41
mappages (1779)
• Fill page table entries
mapping virtual addresses
to physical addresses
• Which page table entry?
– obtained from walkpgdir
• What are the contents?
– Physical address
– Permissions
– present
42
walkpgdir (1754)
• Create a page table entry
corresponding to a virtual
address.
• If page table is not present,
then allocate it.
43
Using Page Tables (in OS)
• Functions available
– mappages (1779) : create page table entries mapping
virtual addresses to physical addresses
– copyuvm (2053): copy a process’s page table into
another
– walkpgdir (1754) : return page table entry
corresponding to a virtual address
44
User Pages mapped twice
0xffffffff Virtual
• Kernel has easy access to user pages (useful for
Device memory system calls)
Kernel Memory
+0x10000
KERNBASE Physical
Device memory
PHYSTOP
0x10000
0
0
45
(Re)Initializing Segmentation
• Segments
– Kernel code
– Kernel data
– User code
– User data
– Per CPU data
ref : x86.h 48
Per CPU Data
49
Recall
Memory is Symmetric Across Processors
Processor Processor Processor Processor
1 2 3 4
Memory bus
DRAM North Bridge
• Memory Symmetry
• All processors in the system share the same memory space
• Advantage : Common operating system code
• However there are certain data which have to be unique to each
processor
• This is the per-cpu data
• example, cpu id, scheduler context, taskstate, gdt, etc.
Naïve implementation of
per-cpu data
55
Allocating Pages (kalloc)
freelist
Physical
Memory
Used page
Free page
PHYSTOP
end of
kernel • Physical memory allocation done in page
0 granularity (i.e. 4KB)
• Free physical pages in a list
• Page Allocation removes from list head (see
function kalloc)
• Page free adds to list head (see kfree)
57
Initializing the list
(chicken & egg problem)
this needs
at boot
59