Operating systems

Segmentation

Prashanth L.A.

2023-09-05

Lecture 15

So far

 

An address space

Why isn’t base and bounds optimal

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

  • 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

Which segment am I referring to?

Address translation

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

Implementation

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

What about backwards-growing segments?

Segment Base Size (max \(4K\)) Grows positive?
Code 32K 2K 1
Heap 34K 2K 1
Stack 28K 2K 0
  • Stack translation
    • Virtual address \(= 15KB = 1536010 = 111100000000002_2\)
    • Top 2 bits say stack
    • Remaining: \(1100000000002_2 = 3KB\)
    • Negative offset = \(3KB\) minus max segment size \(= 3KB-4KB=-1KB\)
    • Stack base is \(28KB\)
    • Phy addr \(= 28KB - 1KB = 27KB\)