2

The second paragraph in Section 3.2.1 Basic Flat Model in "Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3" has the following sentence:

To implement a basic flat memory model with the IA-32 architecture, at least two segment descriptors must be created, one for referencing a code segment and one for referencing a data segment (see Figure 3-2). Both of these segments, however, are mapped to the entire linear address space: that is, both segment descriptors have the same base address value of 0 and the same segment limit of 4 GBytes.

Thus, I'm inclined to think that these two segments can overlap in memory, i.e., they may contain pages that belong to the code and the data segments. But this goes against the well behaved picture shown on Figure 3.2 (see below), where the two segments (CS and DS) don't intercept each other.

enter image description here

From this link I can see that my conjecture is correct, i.e., the code and the data segment can overlap.

Assume now that an instruction belonging to a page X in the code segment accesses data in the same page X. How does the system handle this case? It seems to me that the page X belonging to the code segment will have to be swapped to disk before the instruction is actually executed. Then, how is it going to be executed?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
João Afonso
  • 1,934
  • 13
  • 19
  • 1
    There is no reason a page in memory can't be accessable via two or more different segments simultaneously. – Chris Dodd Dec 03 '21 at 21:34
  • 1
    Segments can overlap. That has been the case since the dawn of x86, even without paging. You will just make sure that you don't actually overlap the addresses actually used. – Jester Dec 03 '21 at 22:46
  • Related: [Observing stale instruction fetching on x86 with self-modifying code](https://stackoverflow.com/q/17395557) / and also [Why do Compilers put data inside .text(code) section of the PE and ELF files and how does the CPU distinguish between data and code?](https://stackoverflow.com/q/55607052) (they don't, only obfuscators do that). – Peter Cordes Dec 04 '21 at 02:29
  • Are you picturing that there'd be two different pages at the same linear address, hence having to swap between them? x86 is a von Neumann architecture, not Harvard; code and data share the same address-space, it's only a performance problem to be writing near code that's executing. (x86 even has coherent I-cache so Self Modifying Code "just works".) – Peter Cordes Dec 04 '21 at 02:37

0 Answers0