Segmentation in Linux : Segmentation & Paging are redundant?
Asked Answered
M

5

6

I'm reading "Understanding Linux Kernel". This is the snippet that explains how Linux uses Segmentation which I didn't understand.

Segmentation has been included in 80 x 86 microprocessors to encourage programmers to split their applications into logically related entities, such as subroutines or global and local data areas. However, Linux uses segmentation in a very limited way. In fact, segmentation and paging are somewhat redundant, because both can be used to separate the physical address spaces of processes: segmentation can assign a different linear address space to each process, while paging can map the same linear address space into different physical address spaces. Linux prefers paging to segmentation for the following reasons:

Memory management is simpler when all processes use the same segment register values that is, when they share the same set of linear addresses.

One of the design objectives of Linux is portability to a wide range of architectures; RISC architectures in particular have limited support for segmentation.

All Linux processes running in User Mode use the same pair of segments to address instructions and data. These segments are called user code segment and user data segment , respectively. Similarly, all Linux processes running in Kernel Mode use the same pair of segments to address instructions and data: they are called kernel code segment and kernel data segment , respectively. Table 2-3 shows the values of the Segment Descriptor fields for these four crucial segments.

I'm unable to understand 1st and last paragraph.

Malefactor answered 12/6, 2010 at 14:58 Comment(2)
That's nice, but is there a question?Haith
Possible duplicate of Differences or similarities between Segmented paging and Paged segmentation?Grosvenor
M
9

The 80x86 family of CPUs generate a real address by adding the contents of a CPU register called a segment register to that of the program counter. Thus by changing the segment register contents you can change the physical addresses that the program accesses. Paging does something similar by mapping the same virtual address to different real addresses. Linux using uses the latter - the segment registers for Linux processes will always have the same unchanging contents.

Mockingbird answered 12/6, 2010 at 15:6 Comment(3)
In protected mode it's not actually the contents of the segment register itself that is added to addresses; the segment register contains a reference to a segment descriptor (stored in memory, in a descriptor table), and one of the fields of the segment descriptor is the base address of the segment, which is added to the offset to generate a linear address.Diligence
Segmentation was dropped in x86-64 architecture (or amd64 is Linux calls it). This newer architecture uses the flat memory model.Kanazawa
@Diligence Thanks for the elaboration about protected mode. Here's more info about the different CPU modes for those who are curious.Hawkeyed
W
8

Segmentation and Paging are not at all redundant. The Linux OS fully incorporates demand paging, but it does not use memory segmentation. This gives all tasks a flat, linear, virtual address space of 32/64 bits.

Paging adds on another layer of abstraction to the memory address translation. With paging, linear memory addresses are mapped to pages of memory, instead of being translated directly to physical memory. Since pages can be swapped in and out of physical RAM, paging allows more memory to be allocated than what is physically available. Only pages that are being actively used need to be mapped into physical memory.

An alternative to page swapping is segment swapping, but it is generally much less efficient given that segments are usually larger than pages.

Segmentation of memory is a method of allocating multiple chunks of memory (per task) for different purposes and allowing those chunks to be protected from each other. In Linux a task's code, data, and stack sections are all mapped to a single segment of memory.

The 32-bit processors do not have a mode bit for disabling segmentation, but the same effect can be achieved by mapping the stack, code, and data spaces to the same range of linear addresses. The 32-bit offsets used by 32-bit processor instructions can cover a four-gigabyte linear address space.

Aditionally, the Intel documentation states:

A flat model without paging minimally requires a GDT with one code and one data segment descriptor. A null descriptor in the first GDT entry is also required. A flat model with paging may provide code and data descriptors for supervisor mode and another set of code and data descriptors for user mode

This is the reason for having a one pair of CS/DS for kernel privilege execution (ring 0), and one pair of CS/DS for user privilege execution (ring 3).

Summary: Segmentation provides a means to isolate and protect sections of memory. Paging provides a means to allocate more memory that what is physically available.

Wanderjahr answered 15/8, 2014 at 16:52 Comment(2)
But on Linux with x86, it’s supposedly paging which provides memory protection (r/w/x flags, supervisor mode bit), am I right ?Polyurethane
The User/Supervisor flag of a Page Table Entry would denote the page as being in Kernel Segment or the User Segment (UVA/KVA). This is checking the privilege level of the process. The RWX permissions on segments of memory allocated via mmap() protect the memory from 'same privilege' level exploitation, abuse or misuse. eg. execution of malicious code on the stack.Wanderjahr
K
4

Windows uses the fs segment for local thread storage. Therefore, wine has to use it, and the linux kernel needs to support it.

Ketubim answered 23/6, 2010 at 17:43 Comment(0)
H
2

Modern operating systems (i.e. Linux, other Unixen, Windows NT, etc.) do not use the segmentation facility provided by the x86 processor. Instead, they use a flat 32 bit memory model. Each user mode process has it's own 32 bit virtual address space.

(Naturally the widths are expanded to 64 bits on x86_64 systems)

Haith answered 12/6, 2010 at 15:2 Comment(0)
C
2

Intel first added segmentation on the 80286, and then paging on the 80386. Unix-like OSes typically use paging for virtual memory.

Anyway, since paging on x86 didn't support execute permissions until recently, OpenWall Linux used segmentation to provide non-executable stack regions, i.e. it set the code segment limit to a lower value than the other segment's limits, and did some emulation to support trampolines on the stack.

Caller answered 23/6, 2010 at 18:51 Comment(2)
80286 is not the first cpu with segmentation memory addressing model. at least 8086 and 8088 have segmentation. They have segment registers CS, SS, DS, ES etc. However 8086 and 8088 dont have protected mode but 80286 has. 80286 appeared later. the architecture of 8086\8088 => bitsavers.org/components/intel/_dataBooks/…Gaffney
That's a totally different kind of segments (which allows to extend a 16 bit address space into a 20 bit address space), and not the meaning at all when talking about virtual memory and process isolation.Caller

© 2022 - 2024 — McMap. All rights reserved.