2023-09-05

# So far

• OS illusion: memory virtualization

• Dynamic relocation via base and bounds

Â

# Why isnâ€™t base and bounds optimal

• Wastes free space
• May not be able to fit many processes in main memory
• Solution: Segmentation

# Segmentation

• Segment: a contiguous portion of the address space of a particular length
• Examples: code, stack, heap

Â

• Place each segment in a different part of physical memory .
• Need base and bounds for each segment

# Handling segments

• Need three base+bounds register pairs;
Segment Base Size
Code 32K 2K
Heap 34K 2K
Stack 28K 2K
• Segment Base Size: (Code, 32K, 2K), (Heap, 34K, 2K), (Stack, 28K, 2K)
• Code translation
• Virtual address $$= 100$$
• This refers to code segment
• Physical address $$= 32768 + 100 = 32868$$
• Heap translation
• Virtual address $$= 4200$$

• Physical address is not $$34KB+4200$$

• Heap starts at virtual address $$4KB$$

• Virtual offset is $$4200-4KB=104$$

• Phy addr $$= 34KB + 104 = 34920$$

# Segmentation Fault

• arises from a memory access on a segmented machine to an illegal address
• E.g. virtual address beyond heap
• hardware generates out of bounds
• OS usually terminates the process

# Which segment am I referring to?

• Use top few bits of virtual address to identify the segment

• $$00 \rightarrow$$ code, $$01 \rightarrow$$ heap and so on

$$01 \rightarrow$$ heap, $$0000 0110 1000$$, or hex $$0x068$$, or decimal $$104$$ is the offset

## Implementation

// get top 2 bits of 14-bit VA
// now get offset
if (Offset >= Bounds[Segment])
RaiseException(PROTECTION_FAULT)
else
Register = AccessMemory(PhysAddr)

Segment Base Size (max $$4K$$) Grows positive?
• Virtual address $$= 15KB = 1536010 = 111100000000002_2$$
• Remaining: $$1100000000002_2 = 3KB$$
• Negative offset = $$3KB$$ minus max segment size $$= 3KB-4KB=-1KB$$
• Stack base is $$28KB$$
• Phy addr $$= 28KB - 1KB = 27KB$$