fix(libkernel): answer sysarch's LDT, I/O permission and i386 GS base operations, as FreeBSD 11.4 does #22039

Merged
brajankw merged 1 commit from upstream-pr/22039 into main 2026-11-17 06:30:49 +00:00
brajankw commented 2026-11-17 03:05:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-17T03:05:45Z.


What

sysarch threw for operations 0, 1, 3, 4 and 10. FreeBSD 11.4's amd64 sys_machdep.c answers them, and this process never has an LDT or an I/O permission map installed (the sysarch XFPUSTATE entry in docs/dev/debt/libkernel.md listed them as unimplemented):

  • I386_GET_LDT reads its 16-byte arguments and returns 0 descriptors (amd64_get_ldt with no LDT);
  • I386_SET_LDT without descriptors returns 0, or EINVAL for a nonzero start with a count of 0 (amd64_set_ldt); with descriptors it would install an LDT and still throws (after EINVAL for more than 512);
  • I386_GET_IOPERM fails a start port from 65536 with EINVAL, otherwise writes back a length of 0 (amd64_get_ioperm with no TSS);
  • I386_SET_IOPERM fails with EPERM, as priv_check(PRIV_IO) does for a jailed process (the jail model agreed on #11216; not measured);
  • I386_SET_GSBASE sets the GS base to the zero-extended 32-bit value through the existing GS base path.

Unreadable arguments fail with EFAULT first, as copyin does. GuestSysarch.cpp covers each case and the null arguments.

Tested

Linux (Debian 13, GCC 14.2.0, Release), on main ec7369ecb8.

  • ctest -R '^guest_sysarch$': passed.
  • Negative control: with main's Process.cpp and this branch's test, guest_sysarch aborts at operation 0, which throws there.
  • Full ctest (5896 tests, -j 12) with this branch and my other twelve branches of this round merged onto main 521fb7fec4: 5880 passed. The 16 failures are the set that fails on main in the same environment: six agc_driver cases (agc_driver_image_table_packed_guards passes when run on its own) and ten that need a .git directory, network or a passwd entry for the user.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch: no overlap with open pull requests.
  • Not tested on Windows or macOS. No title.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5349745523 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22039). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-17T03:05:45Z. --- ### What `sysarch` threw for operations 0, 1, 3, 4 and 10. FreeBSD 11.4's amd64 [`sys_machdep.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/amd64/amd64/sys_machdep.c) answers them, and this process never has an LDT or an I/O permission map installed (the `sysarch` XFPUSTATE entry in `docs/dev/debt/libkernel.md` listed them as unimplemented): - `I386_GET_LDT` reads its 16-byte arguments and returns 0 descriptors (`amd64_get_ldt` with no LDT); - `I386_SET_LDT` without descriptors returns 0, or `EINVAL` for a nonzero start with a count of 0 (`amd64_set_ldt`); with descriptors it would install an LDT and still throws (after `EINVAL` for more than 512); - `I386_GET_IOPERM` fails a start port from 65536 with `EINVAL`, otherwise writes back a length of 0 (`amd64_get_ioperm` with no TSS); - `I386_SET_IOPERM` fails with `EPERM`, as `priv_check(PRIV_IO)` does for a jailed process (the jail model agreed on #11216; not measured); - `I386_SET_GSBASE` sets the GS base to the zero-extended 32-bit value through the existing GS base path. Unreadable arguments fail with `EFAULT` first, as `copyin` does. `GuestSysarch.cpp` covers each case and the null arguments. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux (Debian 13, GCC 14.2.0, Release), on `main` `ec7369ecb8`. - `ctest -R '^guest_sysarch$'`: passed. - Negative control: with `main`'s `Process.cpp` and this branch's test, `guest_sysarch` aborts at operation 0, which throws there. - Full `ctest` (5896 tests, `-j 12`) with this branch and my other twelve branches of this round merged onto `main` `521fb7fec4`: 5880 passed. The 16 failures are the set that fails on `main` in the same environment: six `agc_driver` cases (`agc_driver_image_table_packed_guards` passes when run on its own) and ten that need a `.git` directory, network or a passwd entry for the user. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch`: no overlap with open pull requests. - Not tested on Windows or macOS. No title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-17 03:14:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T03:14:24Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 32d98f93fb:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_sysarch_tests built cleanly.
  • CTest (guest_sysarch):
    1/1 Test #5359: guest_sysarch ....................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/System/src/Process.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5359: guest_sysarch ....................Subprocess aborted***Exception:   0.39 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified LocalDescriptorTable in Process.cpp handles I386_GET_LDT and I386_SET_LDT, validating parameters with GuestCopyIn_nid_no_patch, maxLdtSegment boundary (512), and EINVAL when start != 0 && count == 0.
    • Verified IoPermission in Process.cpp checks arguments[0] >= 65536 returning EINVAL, returns EPERM (1) for I386_SET_IOPERM, and safely copies in/out guest memory.
    • Verified I386_SET_GSBASE handling in sysarch properly copies a 32-bit guest integer, zero-extends it to uint64_t base, checks base < userAddressEnd, and sets GS base correctly.
    • Checked docs/dev/debt/changes/ fragment formatting and debt documentation compliance with AGENTS.md rules.
    • Audited GuestSysarch.cpp test coverage across valid calls, boundary ports, invalid descriptor arguments, and null pointer inputs.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5932177695 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22039#pullrequestreview-5932177695). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T03:14:24Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `32d98f93fb`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_sysarch_tests` built cleanly. - **CTest** (`guest_sysarch`): ``` 1/1 Test #5359: guest_sysarch .................... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/System/src/Process.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5359: guest_sysarch ....................Subprocess aborted***Exception: 0.39 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified LocalDescriptorTable in Process.cpp handles I386_GET_LDT and I386_SET_LDT, validating parameters with GuestCopyIn_nid_no_patch, maxLdtSegment boundary (512), and EINVAL when start != 0 && count == 0. - Verified IoPermission in Process.cpp checks arguments[0] >= 65536 returning EINVAL, returns EPERM (1) for I386_SET_IOPERM, and safely copies in/out guest memory. - Verified I386_SET_GSBASE handling in sysarch properly copies a 32-bit guest integer, zero-extends it to uint64_t base, checks base < userAddressEnd, and sets GS base correctly. - Checked docs/dev/debt/changes/ fragment formatting and debt documentation compliance with AGENTS.md rules. - Audited GuestSysarch.cpp test coverage across valid calls, boundary ports, invalid descriptor arguments, and null pointer inputs. Looks Good To Me
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!22039
No description provided.