feat(kernel): implement vectored file IO on Windows #1131

Merged
orbz22 merged 3 commits from upstream-pr/1131 into main 2026-10-08 08:18:30 +00:00
orbz22 commented 2026-10-07 13:22:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orbz22 on 2026-10-07T13:22:28Z.


What

sceKernelReadv, sceKernelWritev, sceKernelPreadv and sceKernelPwritev threw not implemented on Windows (listed in TechnicalDebt). On Windows they now gather the iovecs into one host buffer (writes) or read into one and scatter it (reads), so each call is a single _read/_write or a single positioned ReadFile/WriteFile. Argument checks and error codes follow the Linux path: EINVAL for iovcnt outside 0..1024 or a negative offset, EFAULT for a null iovec array, guest write destinations are opened through GuestArena::HostWrite as on Linux. In addition, an iovec with a null base and non-zero length returns EFAULT and a total length above INT64_MAX returns EINVAL, which the Linux kernel reports itself.

CheckIovecs and OpenIovecs move out of the Linux-only block so both paths use them.

Positioned transfers and the file offset. NativePositioned (Windows) used ReadFile/WriteFile with an OVERLAPPED offset on a synchronous handle, which also moves the handle's file pointer. preadv/pwritev must leave it unchanged (GuestFileVector.cpp checks this), so the helper now saves the pointer and restores it after the transfer, and returns ESPIPE for handles that are not disk files. The same helper backs pread/pwrite on Windows, so those now keep the offset too, as they do on POSIX and on the Linux path. Concurrent read/write on the same descriptor from another thread can still observe the pointer between the transfer and the restore; there is no way to avoid that with a shared CRT descriptor short of reopening the file.

guest_file_vector was registered only on Linux; it now runs on Windows too, with libkernel's directory on PATH.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), CMake 4.4.4, Ninja 1.13.2, rebased on cea1ef13.

  • ctest -R '^guest_file_vector$' --repeat until-fail:3: passed 3/3. With Stdio.cpp from main the same executable terminates on the not implemented on Windows exception.
  • Full build, --target libs, full ctest: 396/396 passed.
  • Linux: not run locally. The Linux code path only gained the moved helpers; guest_file_vector is still registered there.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01UTjpbMX7BUFMZZaxJE98zV

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4773276812 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1131). Originally posted by [orbz22](https://github.com/orbz22) on 2026-10-07T13:22:28Z. --- ### What `sceKernelReadv`, `sceKernelWritev`, `sceKernelPreadv` and `sceKernelPwritev` threw `not implemented on Windows` (listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md)). On Windows they now gather the iovecs into one host buffer (writes) or read into one and scatter it (reads), so each call is a single `_read`/`_write` or a single positioned `ReadFile`/`WriteFile`. Argument checks and error codes follow the Linux path: `EINVAL` for `iovcnt` outside 0..1024 or a negative offset, `EFAULT` for a null iovec array, guest write destinations are opened through `GuestArena::HostWrite` as on Linux. In addition, an iovec with a null base and non-zero length returns `EFAULT` and a total length above `INT64_MAX` returns `EINVAL`, which the Linux kernel reports itself. `CheckIovecs` and `OpenIovecs` move out of the Linux-only block so both paths use them. **Positioned transfers and the file offset.** `NativePositioned` (Windows) used `ReadFile`/`WriteFile` with an `OVERLAPPED` offset on a synchronous handle, which also moves the handle's file pointer. `preadv`/`pwritev` must leave it unchanged (`GuestFileVector.cpp` checks this), so the helper now saves the pointer and restores it after the transfer, and returns `ESPIPE` for handles that are not disk files. The same helper backs `pread`/`pwrite` on Windows, so those now keep the offset too, as they do on POSIX and on the Linux path. Concurrent `read`/`write` on the same descriptor from another thread can still observe the pointer between the transfer and the restore; there is no way to avoid that with a shared CRT descriptor short of reopening the file. `guest_file_vector` was registered only on Linux; it now runs on Windows too, with `libkernel`'s directory on `PATH`. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), CMake 4.4.4, Ninja 1.13.2, rebased on cea1ef13. - `ctest -R '^guest_file_vector$' --repeat until-fail:3`: passed 3/3. With `Stdio.cpp` from `main` the same executable terminates on the `not implemented on Windows` exception. - Full build, `--target libs`, full `ctest`: 396/396 passed. - Linux: not run locally. The Linux code path only gained the moved helpers; `guest_file_vector` is still registered there. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01UTjpbMX7BUFMZZaxJE98zV
KSWORK2001 commented 2026-10-07 21:05:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by KSWORK2001 on 2026-10-07T21:05:31Z.


I was implementing the same four exports today (before noticing this pull request - not opening a duplicate) and independently hit and confirmed the file-pointer finding: with NativePositioned from main, my run of guest_file_vector failed at GuestFileVector.cpp line 69 (sceKernelLseek == 10 after a preadv), and the save/restore fixes it. Same toolchain here (WinLibs r7 GCC 15.2.0, Windows 11); full ctest 399/399 with the fix in place.

Two deltas I noticed between this gather-into-one-buffer design and direct per-iovec transfers, for the reviewer's judgement:

  1. NativeTransfer throws NativeTransfer: nbytes exceeds platform limit for a total above INT_MAX, and the throw terminates the process per project policy. On the console a legal readv/writev is bounded only by SSIZE_MAX (which this pull request already returns EINVAL for), so an iovec set summing to between 2 GiB and 8 EiB transfers on the console and throws here. Per-iovec transfers chunked at the DWORD limit avoid the bound.
  2. Each call allocates and copies a host buffer of the total size (std::max<std::size_t>(total, 1)); with KERNEL_IOV_MAX 1024 entries that is a large transient allocation and a double copy for big saves, where per-iovec _read/_write needs neither.

Happy to hand over my per-iovec variant (it also early-outs a pipe readv with PeekNamedPipe once no bytes remain, which the single-call design gets for free) if any of this is wanted here; otherwise this works as merged.

Written with AI assistance (ZCode).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6046841490 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1131#issuecomment-6046841490). Originally posted by [KSWORK2001](https://github.com/KSWORK2001) on 2026-10-07T21:05:31Z. --- I was implementing the same four exports today (before noticing this pull request - not opening a duplicate) and independently hit and confirmed the file-pointer finding: with `NativePositioned` from `main`, my run of `guest_file_vector` failed at `GuestFileVector.cpp` line 69 (`sceKernelLseek == 10` after a `preadv`), and the save/restore fixes it. Same toolchain here (WinLibs r7 GCC 15.2.0, Windows 11); full `ctest` 399/399 with the fix in place. Two deltas I noticed between this gather-into-one-buffer design and direct per-iovec transfers, for the reviewer's judgement: 1. `NativeTransfer` throws `NativeTransfer: nbytes exceeds platform limit` for a total above `INT_MAX`, and the throw terminates the process per project policy. On the console a legal `readv`/`writev` is bounded only by `SSIZE_MAX` (which this pull request already returns `EINVAL` for), so an iovec set summing to between 2 GiB and 8 EiB transfers on the console and throws here. Per-iovec transfers chunked at the `DWORD` limit avoid the bound. 2. Each call allocates and copies a host buffer of the total size (`std::max<std::size_t>(total, 1)`); with `KERNEL_IOV_MAX` 1024 entries that is a large transient allocation and a double copy for big saves, where per-iovec `_read`/`_write` needs neither. Happy to hand over my per-iovec variant (it also early-outs a pipe `readv` with `PeekNamedPipe` once no bytes remain, which the single-call design gets for free) if any of this is wanted here; otherwise this works as merged. Written with AI assistance (ZCode).
orbz22 commented 2026-10-08 00:49:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orbz22 on 2026-10-08T00:49:08Z.


Thanks for checking this independently and for both points. Both are fixed in 3b264868:

  1. Each iovec is now read or written in place, in chunks of at most INT_MAX bytes, so a total above 2 GiB no longer reaches the throw. preadv/pwritev advance the offset by the bytes done, and the sum offset + total is checked for overflow (EINVAL).
  2. The gather buffer and the double copy are gone.

Since the transfer is no longer a single call, a readv on a pipe stops after one read; otherwise a second _read would block once the available bytes were consumed. A short read on a pipe is allowed, so I used that instead of PeekNamedPipe. A partial transfer followed by an error returns the byte count.

New checks in GuestFileVector.cpp: a zero-length iovec between two buffers for readv and preadv; readv on a pipe holding fewer bytes than the iovecs (this hangs if the loop does not stop); preadv/pwritev on a pipe return ESPIPE. guest_file_vector passes on Windows (WinLibs GCC 15.2.0 r7).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6049852425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1131#issuecomment-6049852425). Originally posted by [orbz22](https://github.com/orbz22) on 2026-10-08T00:49:08Z. --- Thanks for checking this independently and for both points. Both are fixed in 3b264868: 1. Each iovec is now read or written in place, in chunks of at most `INT_MAX` bytes, so a total above 2 GiB no longer reaches the throw. `preadv`/`pwritev` advance the offset by the bytes done, and the sum `offset + total` is checked for overflow (`EINVAL`). 2. The gather buffer and the double copy are gone. Since the transfer is no longer a single call, a `readv` on a pipe stops after one read; otherwise a second `_read` would block once the available bytes were consumed. A short read on a pipe is allowed, so I used that instead of `PeekNamedPipe`. A partial transfer followed by an error returns the byte count. New checks in `GuestFileVector.cpp`: a zero-length iovec between two buffers for `readv` and `preadv`; `readv` on a pipe holding fewer bytes than the iovecs (this hangs if the loop does not stop); `preadv`/`pwritev` on a pipe return `ESPIPE`. `guest_file_vector` passes on Windows (WinLibs GCC 15.2.0 r7).
adriadam10 commented 2026-10-08 07:59:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T07:59:54Z.


Checked: Linux build + full ctest (424/424) on main + this PR; Windows CI job runs guest_file_vector and passes (including the new gapped-iovec and pipe cases); argument checks and error codes match the Linux path; offset + total overflow and the pipe short-read case are handled.

Added by us (commit e8697e693, nothing in the code changed):

  • A ### Functional TechnicalDebt line for the deviations this PR introduces: the file pointer is restored after a positioned transfer, so a concurrent read/write on the same descriptor can see it moved in between; non-disk handles return ESPIPE; readv on a pipe or console stops after the first iovec read; a failure after the first transfer returns the bytes already done.

Remaining, not blocking: the PR description still says the iovecs are gathered into one host buffer / a single _read/_write. That was true before 3b264868; please update it. The >2 GiB chunking isn't covered by any test.

Merging once CI is green on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6055468875 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1131#issuecomment-6055468875). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T07:59:54Z. --- Checked: Linux build + full ctest (424/424) on main + this PR; Windows CI job runs `guest_file_vector` and passes (including the new gapped-iovec and pipe cases); argument checks and error codes match the Linux path; `offset + total` overflow and the pipe short-read case are handled. Added by us (commit e8697e693, nothing in the code changed): - A `### Functional` TechnicalDebt line for the deviations this PR introduces: the file pointer is restored after a positioned transfer, so a concurrent `read`/`write` on the same descriptor can see it moved in between; non-disk handles return `ESPIPE`; `readv` on a pipe or console stops after the first iovec read; a failure after the first transfer returns the bytes already done. Remaining, not blocking: the PR description still says the iovecs are gathered into one host buffer / a single `_read`/`_write`. That was true before 3b264868; please update it. The >2 GiB chunking isn't covered by any test. Merging once CI is green on the rebased head.
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!1131
No description provided.