Math behind 4GB limit on 32 bit systems

后端 未结 2 1906
情书的邮戳
情书的邮戳 2021-02-08 03:01

I have a very fundamental question relating to 32 bit memory addresses. My understanding is that 2^32 is the maximum number of possible memory addresses on a 32 bit system. Wher

2条回答
  •  野性不改
    2021-02-08 03:24

    This is a case of basic arithmetics: Number of bytes per addressed unit times number of addressable units equals number of addressable bytes.

    The hard part is, where to get those numbers from. Here is my take on it:

    1 - What is a Kilobyte, Megabyte, Gigabyte?

    • For RAM, there is consent, that a Gigabyte is 1024 Megabytes, each consisting of 1024 Kilobytes, each being 1024 Bytes. This stems from the fact, that 1024 is 2^10, but close enough to 1000 to historically allow the Kilo prefix
    • For Storage, vendors have years ago started to use strictly decimal units, a Megabyte being 1000000 bytes (As it makes the capacities look bigger in glossy brochures)

    This has led to 1024*1024 Bytes being called a MiB and 1000*1000 Bytes being called a MB

    2 - The addressable unit

    • For RAM, the addressable unit is the byte, even if it is fetched from physical RAM in chunks of at least 4.
    • For mass Storage, the addressable unit is the sector or block, which most often is 512 bytes, but 4096 Bytes catches up fast.

    3 - The number of addressable units is much more complicated, let's start with RAM:

    • A 32 Bit CPU (sans the MMU!) can address 2^32 Bytes or 4 GiB
    • All modern 32 Bit CPUs include a MMU, that maps these 4 GiB of virtual address space into a physical address space
    • This physical address space can have a different size than 4 GiB, as a function of the MMU using more (or in prehistoric times less) than 32 physical address lines. Today's most common implementation are 36 or more physical Bits, resulting in 16*4 GiB or more (PAE or physical adress extension)
    • This MMU magic does not work around the CPU running in 32 Bit mode, i.e. for every process, the address space can't be larger than 4 GiB
    • To make things a little more interesting, a part of this address space is used for kernel functionality in every modern OS I know of. This results in 2 GiB or 3 GiB maximum usable address space per process for all mainstream OSes.
    • And as this still is much too simple: Running the MMU in a mode, where it can actually use more than 4 GiB of physical RAM must be supported by the OS. A remarkable example is Windows XP 32 Bit, which does NOT allow that.
    • And last but not least: A part of the physical address space is most often used for memory-mapping hardware. If this is combined with OS limits as above, it results in Windows XP 32 Bit sometimes being able to use only 2.5 to 3.5 GiB of physical RAM

    It's much less of a hassle for storage:

    • in all modern PC-based Cases I know of, the addressable units are simply counted with 32 or 48 Bits (LBA or logical block addressing). Even in it's most basic version this is enough for 2 TiB of storage per disk (2^32 blocks of 512 Bytes each). Maxed-out versions with 48 Bit LBA and 4 KiB per block result in ca. a Gazillion TiB per disk.

提交回复
热议问题