Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory segmentation in modern operating systems

In studying operating systems (primarily with Linux as reference), there are a few points that I don't find well explained in the material that I have studied.

Programs loaded into memory are often described as being divided into segments of text, data, stack etc., even in the context of operating systems like Linux where virtual memory is based purely on paging. Is it the case that it is just the program, and not the memory itself that is referred to as segmented? If so, I find the terminology confusing.

I saw that malloc can be implemented in Linux using the call 'sbrk' that increases the size of the data segment. Again, is this 'data segment' just a region of memory that is used for data by convention and not a 'real' segment? (Extra question: 'sbrk' does not seem to be able to decrease the size of the 'segment'. Does this mean that a process can never release memory to the OS other than quitting?)

Also I am interested in knowing why modern operating systems seems not to be using (paged) segmentation. Wouldn't it prevent certain kinds of attacks to have the code residing in it's own protected segment, thereby increasing security? On the other hand, would this make e.g. JIT compilation impossible/difficult?

Besides "yes"/"no" answers to the questions above, I am interested in any insightful elaboration on the subject.

like image 761
Halle Knast Avatar asked Feb 26 '12 23:02

Halle Knast


1 Answers

The segment in "data segment" has nothing to do with hardware segmentation, which is a feature of little relevance to modern operating systems (i.e. redundant with respect to paging) which rely on paging to implement virtual memory. Segments also have severe drawbacks compared to paging (e.g. memory contiguous in a segment must be physically contiguous) without any benefit. By "segment" for user-space programs, one literally means a contiguous section of the virtual space of the process.

Many architectures do not have segmentation anymore. On x86, segmentation is just an historical payload and is set up to have a code and data segment that covers the entire address space because segmentation cannot be bypassed.

Your question about freeing memory obtained through sbrk is answered here: How do I free memory obtained by sbrk()?

like image 70
Gnurou Avatar answered Nov 07 '22 20:11

Gnurou