feat(libkernel): implement madvise #1633

Merged
babyhuey merged 1 commit from upstream-pr/1633 into main 2026-10-10 04:40:10 +00:00
babyhuey commented 2026-10-08 15:20:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-08T15:20:31Z.


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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4789008425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-08T15:20:31Z. --- ### 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
klosixgaming-arch commented 2026-10-08 15:33:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T15:33:07Z.

Review: APPROVED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5459120791 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633#pullrequestreview-5459120791). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-08T15:33:07Z. **Review:** APPROVED --- 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 commented 2026-10-08 17:37:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-08T17:37:07Z.


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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065573317 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633#issuecomment-6065573317). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-08T17:37:07Z. --- Fork CI while the upstream run waits for approval: [Build run](https://github.com/babyhuey/AnyPS5/actions/runs/37812355121), all jobs green on d5a4ac17 (Linux 466/466, Windows 457/457, relinker on Linux, macOS and Windows). It builds the branch head, not the merge with `main`.
github-actions[bot] commented 2026-10-08 21:25:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T21:25:39Z.


Progress report (c9da0f9 → d7e52cb)

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

🆕 1 declared as stubs
Library Name
libkernel madvise_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069353843 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633#issuecomment-6069353843). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T21:25:39Z. --- <!-- progress-report --> ### Progress report (c9da0f9 → d7e52cb) 📉 **System libraries**: 85.67% (-0.03%, +1 declared functions) <details> <summary>🆕 1 declared as stubs</summary> | Library | Name | | - | - | | libkernel | `madvise_nid_postfix` | </details>
babyhuey commented 2026-10-08 22:57:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-08T22:57:42Z.


Titles that stop on madvise on current main. Linux, Ubuntu 24.04 container, GCC 13, Release, main d64134f0 (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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6070702841 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633#issuecomment-6070702841). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-08T22:57:42Z. --- Titles that stop on `madvise` on current `main`. Linux, Ubuntu 24.04 container, GCC 13, Release, `main` d64134f0 (`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 commented 2026-10-09 01:34:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-09T01:34:47Z.


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 f1451396), 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)
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6072451462 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633#issuecomment-6072451462). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-09T01:34:47Z. --- 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` f1451396), 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`) |
popcoder565 commented 2026-10-09 10:00:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T10:00:51Z.


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 b7218b3c.

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

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078684555 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633#issuecomment-6078684555). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T10:00:51Z. --- 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` b7218b3c. `guest_memory`, which carries this PR's `madvise` cases, passes. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
github-actions[bot] commented 2026-10-10 04:17:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T04:17:58Z.


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
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6093683928 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1633#issuecomment-6093683928). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T04:17:58Z. --- <!-- pr-overlap --> 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` | |
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!1633
No description provided.