Memory Overview

Paging is practically not used.  64-bit mode requires paging, however, so it is identity-mapped -- virtual identical to 
physical.  All tasks on all cores use the same page table map, just as though all addresses are physical addresses.  2Meg or 
1Gig page table entries are used.   Nothing swaps to disk.

In ZealOS, the lowest 2Gig of memory is called the code heap.  ZealOS's compiler always uses 32-bit signed relative JMP & CALL 
insts because 64-bit CALLs take two insts.  With signed +/- 32-bit values, code can only call a function within 2Gig distance. 
 Therefore, ZealOS keeps all code in the lowest 2Gig memory addresses including what would normally be called "the kernel".  
Two Gig is plenty for code, don't worry.

You can create new, independent heaps using HeapCtrlInit(). Then, use the CHeapCtrl as the 2nd arg to MAlloc(). See HeapLog() 
for an example.

Memory alloced by a task will be freed when the task is killed.  The System Task is a task that never dies. Its memory is like 
kernel memory in other operating systems.   See SysCAlloc(), SysMAlloc(), SysMAllocIdent() and SysStrNew().

All of the regular page tables are marked, "cached".    When accessing hardware, however, you need uncached page table.  The 
lowest 4Gig addresses have an alias to access hardware located toward the top of mapped space, 0x10000000000.   See 

During an extended powered-on session of ZealOS, in theory, memory will become fragmented, requiring a reboot.

See MemRep() and ::/Demo/MemDemo.ZC.

                                                Single System-wide Memory Map

 0x0000007C00- 0x000003A3FF
  Kernel module, placed here by the boot-loader, BOOT_RAM_BASE.

 0x0000096600- 0x0000096FFF
  Boot block relocated here before loading the Kernel module, BootDVD & BootHD.

 0x0000097000- 0x000009703B Multicore start-up vect code, MPN_VECT.
~0x000009F000- 0x000009FFFF Extended BIOS data area.
 0x0000100000- 0x0000102FFF CSysFixedArea for misc.
 0x000050B000- 0x007FFCFFFF Code Heap mem.

 0x00E0000000- 0x00FFFFFFFF
  32-bit devices could alloc memory at 0xF0000000 going up, but this is wrong, since some PCs already have devices at 
  0xF0000000.  PCI devices are supported, so Mem32DevAlloc() flaws could become an issue.

  Data Heap mem.    (The physical memory that exists in this range is data heap.)

 0x10000000000- 0x100FFFFFFFF
  Uncached alias of first 4Gig.  (For 32-bit device access.)

                         - 0x100FFFFFFFF
  64-bit devices are alloced with Mem64DevAlloc() counting backward.

* Note: There is a break in the data-heap block pool.  This has no effect except the obvious effect that fragmentation has on 
contiguous requests.    You can MAlloc() an 8Gig chunk on a 12Gig machine.  You can MAlloc() a 32Gig chunk on a 64Gig machine.

* Note: For systems with less than 2Gig RAM, the code and data heap block pools are the same.  For systems with 2-4Gig of RAM, 
the code heap is 1/4 of the total.  See BlkPoolsInit().


In 2003, Terry Davis wanted to make a no-paging ring-0-only 64-bit operating system for super speed with simplicity and full 
access. With paging, every memory request requires 5 accesses -- it must access the address itself, 4K, 2Meg, 1Gig, and 512Gig 
page tables, but the CPU's translation look-aside buffer mostly removes the penalty for using paging.   So, he did not want to 
use paging, but long mode requires it.  He did the next best thing -- he identity-mapped everything and achieved the 
simplicity he was after with subtle performance boosts, not wasting time changing address maps.  We look forward to the day 
Intel makes an optimized no-paging long mode.

Terry needed VGA A0000-BFFFF memory to be write-through and 0xE0000000-0xFFFFFFFF to be uncached for various devices.  All 
64-bit computers allow stopping address translation at 2Meg page size, not using 4K.  He wanted to use 2Meg for everything 
because it's faster, with one less level of page tables.  He had to make A0000-BFFFF write-through, though, so he could not 
use 2Meg size on the lowest page.   He did the lowest 2Meg area as 4K pages.  He also unmapped the first 4K to cause a fault 
when dereferencing NULL.

In 2016, Terry came-up with an alternate idea.  He double mapped the lowest memory with an alias that was uncached. Accessing 
the lowest 2Meg area directly was cached but the alias he created up at the top of address space was uncached.  See 
UncachedAliasAlloc().  Unfortunately, he could no longer boast of the simplicity of identity mapping everything.  We also no 
longer cause a fault when dereferencing NULL.

Then, Terry switched to 1Gig page sizes.    For the lowest 4Gig, he set-up an alias up at the top of address space. See 
UncachedAliasAlloc().   Not all computers support 1Gig page tables, however, so he also supported 2Meg.

Terry's original plan was to allow changing the page tables as needed, so he had code for taking control of 2Meg pages and 
marking them uncached or whatever.  When he did an HDAudio driver, he requested some 32-bit address space as uncached.  Today, 
all of the first 4Gig can be accessed without caching at the dev.uncached_alias.