Skip to content

feat(libkernel): implement madvise - #1633

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
babyhuey:feat/libkernel-madvise
Oct 10, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
babyhuey:feat/libkernel-madvise

Conversation

@babyhuey

@babyhuey babyhuey commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

What

Implements madvise (Jahsnh4KKkg) in libkernel. Part of #1578: PPSA99039 and PPSA99001 import it.

Follows FreeBSD 11 kern_madvise (sys/vm/vm_mmap.c) and vm_map_madvise (sys/vm/vm_map.c):

  • behav outside MADV_NORMAL..MADV_CORE (0..9) fails with EINVAL.
  • A range that wraps or ends past VM_MAXUSER_ADDRESS (0x800000000000 on amd64) fails with EINVAL. Unmapped holes are not an error.
  • Every accepted value is advisory. MADV_DONTNEED only lowers page priority and MADV_FREE lets the kernel discard pages, so keeping the contents is valid for both; the call returns 0 without touching memory.
  • MADV_PROTECT (10) goes through procctl(PROC_SPROTECT) and its privilege check on FreeBSD, so it calls NotImplemented_nid_no_patch.

Tested

Linux (Ubuntu 24.04, GCC 13, Release): guest_memory passes with --repeat until-fail:3. It now checks all ten values on an mmap range with its contents intact afterwards, a zero length, an unmapped range, invalid values (also with zero length), a wrapping range, a start at 0x800000000000 and a range ending exactly there. The test fails when the range check is removed. check_conventions.py passes. Windows: PosixMemory.cpp compiles with MinGW-w64 GCC 13 (-Wall -Wextra -fsyntax-only); not run on Windows. No title run.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

@klosixgaming-arch klosixgaming-arch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Clean implementation of madvise following FreeBSD semantics with proper validation (advice range 0-9, address bounds) and correct handling of MADV_PROTECT as unimplemented. The test coverage is excellent - testing all valid advice values, edge cases (zero length, invalid values, wrapping ranges), boundary conditions at the user address limit, and verifying contents remain intact after advisory calls. Good addition to support PPSA99039 and PPSA99001's import requirements.

@babyhuey

babyhuey commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Fork CI while the upstream run waits for approval: Build run, all jobs green on d5a4ac1 (Linux 466/466, Windows 457/457, relinker on Linux, macOS and Windows). It builds the branch head, not the merge with main.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Progress report (c9da0f9 → d7e52cb)

📉 System libraries: 85.67% (-0.03%, +1 declared functions)

🆕 1 declared as stubs
Library Name
libkernel madvise_nid_postfix

@babyhuey
babyhuey force-pushed the feat/libkernel-madvise branch from d5a4ac1 to 97c5985 Compare October 8, 2026 21:35
@github-actions github-actions Bot mentioned this pull request Oct 8, 2026
9 tasks done
@babyhuey

babyhuey commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Titles that stop on madvise on current main. Linux, Ubuntu 24.04 container, GCC 13, Release, main d64134f (relinker and libs); titles from the homebrew catalog, relinked for Linux and run from the title folder.

Title main with this branch
PPSA99110 Prospero Multi Tools 1 app.elf: symbol lookup error: app.elf: undefined symbol: Jahsnh4KKkg (madvise) next stop JZKw5+Wrnaw (pthread_sigmask)
PPSA99920 Prospero Vibrate 1.0 app.elf: symbol lookup error: app.elf: undefined symbol: Jahsnh4KKkg (madvise) next stop JZKw5+Wrnaw (pthread_sigmask)

The "with this branch" build also had my other open PRs merged; only this branch exports Jahsnh4KKkg.

@babyhuey

babyhuey commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Windows follow-up. The two madvise titles above don't relink with --windows on main (LSDA check, fixed in #1944). With #1944 and this branch merged (Windows 11, WinLibs GCC 15.2.0 r7, main f145139), both get past madvise:

Title with #1944 + this branch
PPSA99110 Prospero Multi Tools FAIL: unresolved ELF import JZKw5+Wrnaw from libkernel.prx (pthread_sigmask)
PPSA99920 Prospero Vibrate FAIL: unresolved ELF import JZKw5+Wrnaw from libkernel.prx (pthread_sigmask)

@babyhuey
babyhuey force-pushed the feat/libkernel-madvise branch from 97c5985 to 73dac43 Compare October 9, 2026 08:31
@popcoder565 popcoder565 mentioned this pull request Oct 9, 2026
9 tasks done
@popcoder565

Copy link
Copy Markdown
Contributor

First Windows run, since the description says it was only compiled with MinGW. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. #1624, #1628, #1633 and #1634 merged together onto main b7218b3.

guest_memory, which carries this PR's madvise cases, passes.

🤖 Addressed by Claude Code

@babyhuey
babyhuey force-pushed the feat/libkernel-madvise branch from 73dac43 to 888c263 Compare October 9, 2026 17:30
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 9, 2026
@babyhuey
babyhuey force-pushed the feat/libkernel-madvise branch from 888c263 to 3d54ddc Compare October 9, 2026 21:32
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 9, 2026
@babyhuey
babyhuey force-pushed the feat/libkernel-madvise branch from 3d54ddc to 7777d98 Compare October 9, 2026 22:30
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 9, 2026
@babyhuey
babyhuey force-pushed the feat/libkernel-madvise branch from 7777d98 to b0e0a74 Compare October 9, 2026 23:30
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 10, 2026
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 10, 2026
@github-actions

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2545 madvise_nid_postfix

@oneandonlydean
oneandonlydean merged commit 6dbc921 into boykopovar:main Oct 10, 2026
10 checks passed
SleglJan pushed a commit to SleglJan/AnyPS5 that referenced this pull request Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants