From 8b6073b6b1bc52cd84aa8b93bf9b7b5890c2fddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Sat, 28 Nov 2026 09:40:41 -0700 Subject: [PATCH] fix(patches): use 64 KiB initial commit and null checks for QEMU Win32 coroutine fibers --- .github/workflows/build-runtime.yml | 6 +- docs/portable-runtime-spec.md | 4 ++ docs/upstream-pr-prep.md | 15 +++++ patches/README.md | 1 + .../qemu-win32-coroutine-fiber-commit.patch | 60 +++++++++++++++++++ scripts/pack-runtime.ps1 | 6 +- 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 patches/qemu-win32-coroutine-fiber-commit.patch diff --git a/.github/workflows/build-runtime.yml b/.github/workflows/build-runtime.yml index e90f7db..9e095bf 100644 --- a/.github/workflows/build-runtime.yml +++ b/.github/workflows/build-runtime.yml @@ -75,7 +75,8 @@ jobs: for p in \ "$WS_UNIX/patches/qemu-whpx-win10-compat.patch" \ "$WS_UNIX/patches/qemu-whpx-win10-startup-ipis.patch" \ - "$WS_UNIX/patches/qemu-whpx-nested-virt-nonfatal.patch"; do + "$WS_UNIX/patches/qemu-whpx-nested-virt-nonfatal.patch" \ + "$WS_UNIX/patches/qemu-win32-coroutine-fiber-commit.patch"; do [ -f "$p" ] || continue git -C vendor/qemu apply --check "$p" git -C vendor/qemu apply "$p" @@ -99,7 +100,8 @@ jobs: -OutDir $outDir ` -TempRoot $env:RUNNER_TEMP ` -Channel candidate ` - -IncludeNestedVirtPatch + -IncludeNestedVirtPatch ` + -IncludeWin32FiberPatch - name: Upload candidate artifacts uses: actions/upload-artifact@v4 diff --git a/docs/portable-runtime-spec.md b/docs/portable-runtime-spec.md index 3d3f1f8..4ecc86b 100644 --- a/docs/portable-runtime-spec.md +++ b/docs/portable-runtime-spec.md @@ -107,6 +107,7 @@ reims-runtime-/ | `qemu-whpx-win10-compat.patch` | `vendor/qemu` 的 `target/i386/whpx/whpx-all.c` | ① `whpx_tsc_deadline_refused()`(`3210-3230`)原实现在真实分区上建、删 vCPU 0,Win10 19045 上随后 `whpx_init_vcpu()` 再次创建 vCPU 0 时必现 `WHvCreateVirtualProcessor` 返回 `E_INVALIDARG`(`hr=80070057`),改为独立临时探测分区;② `whpx_set_registers()`(`534`)直接调用 `WHvResetPartition` 未判空,Win10 19045 的 `WinHvPlatform.dll` 不导出该接口导致重置阶段必现 `0xc0000005 at 0x0` 崩溃(已核对导出表,10 个 supplemental API 中仅此一处漏判空) | 2026-11-28 实测复现并修复,待提交上游 PR(由人操作) | | `qemu-whpx-win10-startup-ipis.patch` | 同上(在前一项之后应用) | Win10 开启内核 irqchip 时转发多核 INIT/SIPI,避免 XNU 从核启动停住 | 已合入补丁队列,客体冷重启待验证,待提交上游 PR | | `qemu-whpx-nested-virt-nonfatal.patch` | 同上(在前两项之后应用) | 部分 Win11 宿主 `WHvSetPartitionProperty(NestedVirtualization)` 返回 `hr=0x80370302`(`WHV_E_UNKNOWN_PROPERTY`)导致 QEMU 在 `whpx-all.c:3479-3483` 直接致命退出,改为非致命警告并清零 `Bank1.NestedVirtSupport`,保留 `kernel-irqchip=on` 内核态 x2APIC 加速 | 2026-11-28 新增补丁,待编入下一版 candidate 运行时并在 Win11 真机冒烟验证后提 PR | +| `qemu-win32-coroutine-fiber-commit.patch` | `vendor/qemu` 的 `util/coroutine-windows.c` | 登录 Apple ID 等高并发 VA-API + 磁盘 I/O 场景下,`CreateFiber(1 MiB)` 预先提交整块 1 MiB 协程栈易耗尽 Windows Commit Charge 返回 `NULL`,未判空即调用 `SwitchToFiber(NULL)` 触发 `0xc0000005`(读 `0x0000000000000010`)崩溃;改用 `CreateFiberEx(64 KiB, 1 MiB, 0, ...)` 并将 `NULL` 与 `IsThreadAFiber()` 纳入显式处理 | 2026-11-28 符号化定位并新增补丁,待编入下一版 candidate 运行时实测验证后提 PR | ### 5.1 本机实测排障与待验证优化沉淀(2026-11-28,Win10 19045) 1. **Win10 下默认关闭 `kernel-irqchip` 导致高 CPU 开销(待验证优化)**: @@ -116,6 +117,9 @@ reims-runtime-/ - 32 GB 物理内存宿主在开启常见软件后,实测可用提交内存(`FreeVirtualMemory`)仅 `14.7 GiB`(即使空闲物理内存仍有 `17.3 GiB`);QEMU 启动时一次性 `CreateFileMapping` 提交全部 Guest RAM,分配 `16G` 直接退出报 `Cannot set up guest memory 'ram': 系统资源不足`。`reims-go` 必须在体检与启动前检测提交内存并动态限位(详见 [`architecture.md`](architecture.md) 3.1 节)。 3. **在线 Recovery 拉取 17 GB 极易因 Apple CDN 断流失败**: - 实测拉取 961 MB 恢复镜像期间 Apple CDN 即出现 6 次 `Peer disconnected`;在 Recovery 内经 QEMU 用户态 NAT(`slirp`)拉取 ~17 GB 系统包时,`OSISDownloadOperation` 抗断流能力差,易在接近完成(实测 `disk.qcow2` 已达 `16.95 GiB`)时失败。因此 `reims-go` GUI 默认改为宿主机调用 `reims-util offline-installer --json` 断点续传下载离线安装包(详见 [`architecture.md`](architecture.md) 3.3 节)。 +4. **登录 Apple ID 后 `SwitchToFiber(NULL)` 崩溃(`0xc0000005`,读 `0x10`)**: + - 2026-11-28 对真实崩溃栈在 `runtime-3731afd` 上符号化定位:`whpx_vcpu_run` → `execute_ncq_command`(`hw/ide/ahci.c:1096`)→ `blk_aio_prwv`(`block/block-backend.c:1624`)→ `qemu_aio_coroutine_enter`(`util/qemu-coroutine.c:298`)→ `qemu_coroutine_switch`(`util/coroutine-windows.c:59`)→ `KERNELBASE!SwitchToFiber`(`movq 0x10(%rcx), %rax`)。 + - 根因是 `util/coroutine-windows.c:78` 调用 `CreateFiber(COROUTINE_STACK_SIZE, ...)` 预提交整块 1 MiB 栈,在 VA-API HEVC 批量解码 + iCloud/AHCI 并发读盘叠加导致 Commit Charge 见底时返回 `NULL`,且未判空直接传给 `SwitchToFiber`。补丁 [`qemu-win32-coroutine-fiber-commit.patch`](../patches/qemu-win32-coroutine-fiber-commit.patch) 改用 `CreateFiberEx(64 KiB, 1 MiB, 0, ...)` 并补齐空指针诊断与 `IsThreadAFiber()` 保护。 规则: 1. 补丁原文的权威副本放在 `reims-go/patches/`。macos-vm-lab 的那份是研究时的原始记录。 diff --git a/docs/upstream-pr-prep.md b/docs/upstream-pr-prep.md index 5b48604..907b2fe 100644 --- a/docs/upstream-pr-prep.md +++ b/docs/upstream-pr-prep.md @@ -16,6 +16,7 @@ | `#123` | 窗口体验 | `Ctrl+Alt+Enter` 热切全屏 | 合入后在前端文档/帮助提示同步快捷键 | | `#172` | 窗口体验 | Windows 拖动窗口贴合,消除缩放黑边 | 合入后验证高 DPI 与多显示器缩放 | | Win → Command | 键位映射 | 自由指针下 Windows 键映射为 macOS Command | 优先向上游提议/提交 PR,避免长期维护私有补丁 | +| `reims-util` `--lang` / `prev-lang:kbd` | 客体语言预设 | `reims-util@7867384` `src/opencore/config.rs:216-228` 硬编码 `prev-lang:kbd=en-US:0` 并置于 `NVRAM > Delete`,导致恢复模式/设置助理强制回退英文 | 短期本仓停机改写 `opencore.qcow2` 并在 `update-opencore` 后重放;长期向上游提议 `opencore --lang` 与重建保留 | --- @@ -56,6 +57,20 @@ - 将 `error_report` + `goto error` 改为 `warn_report` 并将 `processor_features.Bank1.NestedVirtSupport` 清零,继续保留完整 `kernel-irqchip=on` 启动。 - **实测状态**:已在 `Reims-Go` 宿主探测逻辑(`internal/host/whpx_probe_windows.go`)复现 `hr=0x80370302`;带该补丁的 `candidate` 运行时需经真机编译冒烟验证后再提上游 PR(**待验证**)。 +### PR 4:`util/coroutine-windows: reserve 1 MiB stack with 64 KiB initial commit and check fiber creation` + +- **补丁文件**:[`patches/qemu-win32-coroutine-fiber-commit.patch`](../patches/qemu-win32-coroutine-fiber-commit.patch)(独立于 PR 1–3,修改 `util/coroutine-windows.c`) +- **影响环境**:Windows 10 / 11 宿主在高并发磁盘 I/O + GPU/VA-API 显存提交叠加场景(如登录 Apple ID 触发 iCloud/Spotlight 并发读盘与 HEVC 硬解)。 +- **复现与根因**: + 1. `runtime-3731afd` 真机崩溃日志在 `CPU 0/WHPX` 线程触发 `0xc0000005`(read of `0x0000000000000010`),符号化调用栈为 `execute_ncq_command`(`hw/ide/ahci.c:1096`)→ `blk_aio_prwv`(`block/block-backend.c:1624`)→ `qemu_aio_coroutine_enter`(`util/qemu-coroutine.c:298`)→ `qemu_coroutine_switch`(`util/coroutine-windows.c:58`)→ `KERNELBASE!SwitchToFiber`(`movq 0x10(%rcx), %rax`)。 + 2. `qemu_coroutine_new()`(`util/coroutine-windows.c:78`)调用 `CreateFiber(COROUTINE_STACK_SIZE, ...)`,Windows 会将 `1 MiB` 全部作为初始 Commit Size 立即扣除系统 Commit Charge;当提交内存见底时 `CreateFiber` 返回 `NULL`,原代码未判空即返回并在 `SwitchToFiber(NULL)` 崩溃。 + 3. 同时 `qemu_coroutine_self()`(`util/coroutine-windows.c:99`)直接调用 `ConvertThreadToFiber(NULL)`,若线程已被外部库转为 Fiber 会返回 `NULL`(`ERROR_ALREADY_FIBER`)且同样未检查。 +- **最小修复**: + 1. 改用 `CreateFiberEx(64 * 1024, COROUTINE_STACK_SIZE, 0, ...)`,保留完整 1 MiB 栈空间但将每个协程初始提交降为 64 KiB(提交开销降低 16 倍); + 2. 在 `qemu_coroutine_self()` 中通过 `IsThreadAFiber()` 复用现有 Fiber; + 3. 对 `co->fiber == NULL` 与 `leader->fiber == NULL` 打印 `GetLastError()` 并显式 `abort()`,杜绝静默空指针解引用。 +- **实测状态**:补丁已通过 `git apply --check`(基于 `4571bc5`);需编入下一版 `candidate` 运行时并在真机验证登录后高负载场景(**待验证**)。 + --- ## 三、补丁退役检查表 diff --git a/patches/README.md b/patches/README.md index 5dfe006..2f2ce0b 100644 --- a/patches/README.md +++ b/patches/README.md @@ -14,3 +14,4 @@ | `qemu-whpx-win10-compat.patch` | `steelbrain/qemu-reims-vgpu` (`4571bc5`) | 修复 Windows 10 19045 上 WHPX 两处必现崩溃:① `whpx_tsc_deadline_refused()` 在正式 partition 上删建 vCPU 0 导致 `WHvCreateVirtualProcessor` 返回 `0x80070057`;② `whpx_set_registers()` 调用 Win11 新增的 `WHvResetPartition` 未判空导致 `0xc0000005` 崩溃 | 待提交上游 PR | | `qemu-whpx-win10-startup-ipis.patch` | 同上,须在前一项之后应用 | Win10 开启内核 irqchip 时,将多核客体的 INIT/SIPI 经 `WHvRequestInterrupt` 转发,避免 XNU 启动从核时停住。8 核两次冷启动与 12 核冷启动均验证 x2APIC、FileVault、SSH 和图形登录,默认 8 核启动与正常关机也通过。多核整体性能收益和客体冷重启仍待验证 | 待提交上游 PR | | `qemu-whpx-nested-virt-nonfatal.patch` | 同上,须在前两项之后应用 | 部分 Windows 11 宿主(如 24H2/Home 或 Core Ultra 200)在 `WHvGetCapability` 报告 `Bank1.NestedVirtSupport=1`,但 `WHvSetPartitionProperty(NestedVirtualization)` 返回 `hr=0x80370302`(`WHV_E_UNKNOWN_PROPERTY`)。原代码将其视为致命错误直接中止启动,迫使退回 `kernel-irqchip=off`;改为 `warn_report` 并清零 `Bank1.NestedVirtSupport`,保留完整的 Hyper-V 内核态 x2APIC / SynIC 加速 | 待真机编译冒烟后提交上游 PR | +| `qemu-win32-coroutine-fiber-commit.patch` | `steelbrain/qemu-reims-vgpu` (`4571bc5`) 的 `util/coroutine-windows.c` | 修复 Windows 下并发磁盘 I/O(如登录 Apple ID 触发 VA-API 解码 + iCloud/AHCI 并发读盘)时 `CreateFiber(1 MiB)` 提前提交整块 1 MiB 栈导致提交内存见底返回 `NULL`,且未判空直接进入 `SwitchToFiber(NULL)` 触发 `0xc0000005`(读 `0x10`)闪退;改为 `CreateFiberEx(64 KiB, 1 MiB, 0, ...)` 将初始提交降为 1/16,同时在 `qemu_coroutine_new()` 与 `qemu_coroutine_self()` 中处理 `IsThreadAFiber()` 并对 `NULL` 报错中止 | 待编译冒烟验证后提交上游 PR | diff --git a/patches/qemu-win32-coroutine-fiber-commit.patch b/patches/qemu-win32-coroutine-fiber-commit.patch new file mode 100644 index 0000000..b01ca0a --- /dev/null +++ b/patches/qemu-win32-coroutine-fiber-commit.patch @@ -0,0 +1,60 @@ +diff --git a/util/coroutine-windows.c b/util/coroutine-windows.c +--- a/util/coroutine-windows.c ++++ b/util/coroutine-windows.c +@@ -26,6 +26,8 @@ + #include "qemu/coroutine_int.h" + #include "qemu/coroutine-tls.h" + ++#define COROUTINE_INITIAL_COMMIT_SIZE (64 * 1024) ++ + typedef struct + { + Coroutine base; +@@ -71,11 +73,26 @@ + + Coroutine *qemu_coroutine_new(void) + { + const size_t stack_size = COROUTINE_STACK_SIZE; ++ const size_t commit_size = MIN(COROUTINE_INITIAL_COMMIT_SIZE, stack_size); + CoroutineWin32 *co; + + co = g_malloc0(sizeof(*co)); +- co->fiber = CreateFiber(stack_size, coroutine_trampoline, &co->base); ++ /* ++ * CreateFiber(stack_size, ...) commits the full 1 MiB stack up front, ++ * which can exhaust Windows commit charge during burst disk I/O + GPU ++ * workloads and return NULL (later crashing inside SwitchToFiber(NULL) ++ * reading 0x10). Reserve stack_size while committing only 64 KiB upfront. ++ */ ++ co->fiber = CreateFiberEx(commit_size, stack_size, 0, ++ coroutine_trampoline, &co->base); ++ if (!co->fiber) { ++ fprintf(stderr, ++ "qemu_coroutine_new: CreateFiberEx(commit=%zu, reserve=%zu) failed: " ++ "GetLastError()=%lu\n", ++ commit_size, stack_size, (unsigned long)GetLastError()); ++ abort(); ++ } + return &co->base; + } + +@@ -96,7 +113,18 @@ + + current = &leader->base; + set_current(current); +- leader->fiber = ConvertThreadToFiber(NULL); ++ if (IsThreadAFiber()) { ++ leader->fiber = GetCurrentFiber(); ++ } else { ++ leader->fiber = ConvertThreadToFiber(NULL); ++ } ++ if (!leader->fiber) { ++ fprintf(stderr, ++ "qemu_coroutine_self: ConvertThreadToFiber failed: " ++ "GetLastError()=%lu\n", ++ (unsigned long)GetLastError()); ++ abort(); ++ } + } + return current; + } diff --git a/scripts/pack-runtime.ps1 b/scripts/pack-runtime.ps1 index 327430f..3cca9f7 100644 --- a/scripts/pack-runtime.ps1 +++ b/scripts/pack-runtime.ps1 @@ -37,7 +37,8 @@ param( [ValidateSet('candidate', 'stable')] [string]$Channel = 'candidate', [int]$ZstdLevel = 15, - [switch]$IncludeNestedVirtPatch + [switch]$IncludeNestedVirtPatch, + [switch]$IncludeWin32FiberPatch ) $ErrorActionPreference = 'Stop' @@ -110,6 +111,9 @@ $patchSpecs = @( if ($IncludeNestedVirtPatch) { $patchSpecs += @{ Name = 'qemu-whpx-nested-virt-nonfatal.patch'; Target = 'vendor/qemu'; Repo = $qemuRepo; Paths = @() } } +if ($IncludeWin32FiberPatch) { + $patchSpecs += @{ Name = 'qemu-win32-coroutine-fiber-commit.patch'; Target = 'vendor/qemu'; Repo = $qemuRepo; Paths = @() } +} $patches = @() # Group patches by repo so multi-patch stacks (e.g. 2-3 QEMU WHPX patches) are # verified together against a clean HEAD export with zero undeclared diff.