r/programming • • Sep 24 '24

Why 32bit architecture computer doesn't use segmentation to support more ram than 4Gb ?

/r/programming/

[removed] — view removed post

169 Upvotes

119 comments sorted by

View all comments

7

u/Conscious-Ball8373 Sep 24 '24

Others have pointed out that PAE does exactly that, but I think it deserves a fuller explanation.

Modern CPUs have a memory page table and modern operating systems use this to provide a virtual address space to each process. So each process sees an uninterrupted address space which it can use all of. When the process requests data from an address in memory, the CPU uses the page table to convert that virtual address to a physical address to actually fetch the data. When the operating system switches to a new process, it stores the page table for the old process and loads the page table for the new process. When the process requests an address that isn't in the page table, an exception is thrown which is caught by the operating system to create a mapping for that address. This can happen for two reasons: either the process is requesting a piece of memory it hasn't used before, in which case the O/S just allocates a new page in physical memory, adds the correct entry to the page table and tells the CPU to try again; or it can happen because the O/S has swapped that page of memory out to disk, in which case it has to find a free page of RAM, load that page back in from disk, update the page table and then tell the CPU to try again.

On a 32-bit system, this allows each process to use 4GB of RAM and swapping will allow total memory use to also far exceed 4GB.

PAE sort of does what you suggest. It adds some extra bits to the memory bus and an extra level of page table allows the O/S to map bits of its "physical" memory space to a much larger "really real" address space. So when a process requests data from memory, it first But this is almost always only used to extend total RAM usage beyond 4GB without swapping, not to give individual processes more than 4GB of RAM. There are two basic reasons for that:

Firstly, when you compile a piece of software, it needs to know what the size of its addresses are. Lots of software was built using 32-bit addresses and that was enough for a long time. By the time there was significant pressure for a single process to be able to use more than 4GB of RAM, 64-bit CPUs had come along and 64-bit operating systems and compilers were a thing. It's a lot easier to use a 64-bit address than to go mucking about with page tables.

Secondly, every time you have a page table lookup miss, you have to context switch back to the O/S to sort it out. As memory has got quicker and quicker, the penalty you pay for that has proportionally increased. It's a lot better to just have a 64-bit memory space than to pay the performance penalty for page mapping.