Skip to content

Improve process memory#843

Draft
vinc wants to merge 14 commits into
trunkfrom
feature/process-memory
Draft

Improve process memory#843
vinc wants to merge 14 commits into
trunkfrom
feature/process-memory

Conversation

@vinc
Copy link
Copy Markdown
Owner

@vinc vinc commented May 1, 2026

This PR create the process page table from scratch, sharing only what is needed instead of cloning every tables. The kernel heap area and the processes area (in the kernel page table) were moved to different L4 indexes to simplify this.

The processes area in the kernel page table located high in memory is also remapped low in the process page table where programs are linked, to avoid a hack in the page fault handler where this was done as demand paging backed by memory, that is by copying the program code from high memory during instruction fetching.

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 1, 2026

This PR move the kernel heap from 0x4444_4444_0000 to 0x4000_0000_0000 (L4 index 128) and the process area used by the kernel from after the end of the heap to an independent location at 0x5000_0000_0000 (L4 index 160).

> print hello world
DEBUG: sys code start 0x00000000200000: L4=000, L3=000, L2=001, L1=000
DEBUG: sys code end   0x00000000600000: L4=000, L3=000, L2=003, L1=000
DEBUG: sys heap start 0x00400000000000: L4=128, L3=000, L2=000, L1=000
DEBUG: sys heap end   0x00400001020000: L4=128, L3=000, L2=008, L1=032
DEBUG: usr code       0x00500000A00000: L4=160, L3=000, L2=005, L1=000
DEBUG: usr stack      0x005000013FF000: L4=160, L3=000, L2=009, L1=511
DEBUG: Kernel Page Table (without L4=256)
DEBUG: --------> L4=000 PageTableEntry { addr: PhysAddr(0x2000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED) }
DEBUG:    -----> L3=000 PageTableEntry { addr: PhysAddr(0x3000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG:       --> L2=000 PageTableEntry { addr: PhysAddr(0x4000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED) }
DEBUG:       --> L2=001 PageTableEntry { addr: PhysAddr(0x61d000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG:       --> L2=002 PageTableEntry { addr: PhysAddr(0x621000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG: --------> L4=031 PageTableEntry { addr: PhysAddr(0x61e000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG: --------> L4=128 PageTableEntry { addr: PhysAddr(0x315000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED) }
DEBUG:    -----> L3=000 PageTableEntry { addr: PhysAddr(0x316000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED) }
DEBUG:       --> L2=000 PageTableEntry { addr: PhysAddr(0x317000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED) }
DEBUG:       --> L2=001 PageTableEntry { addr: PhysAddr(0x743000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG:       --> L2=002 PageTableEntry { addr: PhysAddr(0x944000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG:       --> L2=003 PageTableEntry { addr: PhysAddr(0xb45000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG:       --> L2=004 PageTableEntry { addr: PhysAddr(0xd46000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG:       --> L2=005 PageTableEntry { addr: PhysAddr(0xf47000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG:       --> L2=006 PageTableEntry { addr: PhysAddr(0x1148000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG:       --> L2=007 PageTableEntry { addr: PhysAddr(0x1349000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG:       --> L2=008 PageTableEntry { addr: PhysAddr(0x154a000), flags: PageTableFlags(PRESENT | WRITABLE) }
DEBUG: --------> L4=160 PageTableEntry { addr: PhysAddr(0x156b000), flags: PageTableFlags(PRESENT | WRITABLE | USER_ACCESSIBLE | ACCESSED) }
DEBUG:    -----> L3=000 PageTableEntry { addr: PhysAddr(0x156c000), flags: PageTableFlags(PRESENT | WRITABLE | USER_ACCESSIBLE | ACCESSED) }
DEBUG:       --> L2=000 PageTableEntry { addr: PhysAddr(0x156d000), flags: PageTableFlags(PRESENT | WRITABLE | USER_ACCESSIBLE) }
DEBUG: --------> L4=511 PageTableEntry { addr: PhysAddr(0x61a000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG:    -----> L3=000 PageTableEntry { addr: PhysAddr(0x622000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG:       --> L2=000 PageTableEntry { addr: PhysAddr(0x623000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG:       --> L2=001 PageTableEntry { addr: PhysAddr(0x624000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG:    -----> L3=510 PageTableEntry { addr: PhysAddr(0x61b000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
DEBUG:       --> L2=000 PageTableEntry { addr: PhysAddr(0x61c000), flags: PageTableFlags(PRESENT | WRITABLE | ACCESSED | DIRTY) }
hello world

Next step is to refactor process page table creation.

@vinc vinc changed the title Move kernel heap and process memory Improve process memory May 2, 2026
@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 2, 2026

The new process page table with just what is needed for the process is starting to work. We can now run more than one process at a time again.

TODO: Remove the page fault instruction fetching hack

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 2, 2026

The mapper clean_up function has this safety comment:

The caller has to guarantee that it’s safe to free page table frames: All page table frames must only be used once and only in this page table (e.g. no reference counted page tables or reusing the same page tables for different virtual addresses ranges in the same page table).

So we can't use it anymore. Indeed it was cleaning up the frame used for L4=160 that was then re-used for the next process page table, creating a copy of the previous page table inside L4=160.

@vinc
Copy link
Copy Markdown
Owner Author

vinc commented May 2, 2026

TODO: Stop using shared high memory for processes. Copy flat binaries to a fixed location (0x800000) and ELF binaries to where they've been linked to (hopefully 0x800000 also), using the physical frames allocated to that location in the user page table. Something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant