Skip to content

Feat/x86 64 ptrace#1060

Closed
54dK3n wants to merge 4 commits into
rcore-os:devfrom
54dK3n:feat/x86_64-ptrace
Closed

Feat/x86 64 ptrace#1060
54dK3n wants to merge 4 commits into
rcore-os:devfrom
54dK3n:feat/x86_64-ptrace

Conversation

@54dK3n

@54dK3n 54dK3n commented May 31, 2026

Copy link
Copy Markdown
Contributor

No description provided.

… support

补齐 x86_64 ptrace 主干功能:

## Kernel changes (os/StarryOS/kernel)

### ptrace.rs: x86_64 register read/write + singlestep
- 新增 `X8664UserRegs` 结构体 (repr(C)),对应 Linux
  `user_regs_struct`,含 r15..rsp/ss/fs_base/gs_base 全部 27 个字段
- 实现 `From<UserContext> for X8664UserRegs` 和 `write_to()`
- 补齐 x86_64 上之前缺失的 ptrace opcode:
  GETREGS / SETREGS / GETREGSET(NT_PRSTATUS) / SETREGSET(NT_PRSTATUS) /
  GETSIGINFO / SETSIGINFO
- 将 `ptrace_getregset_prstatus` 中 `regs` 局部变量从内部 block 提升到
  函数级作用域,修复 reg_bytes slice 的 use-after-free bug(regs 出 block
  后被栈复用覆盖,导致用户态读到随机值,表现为 RSP=0x1)
- 新增 `ptrace_setup_singlestep` for x86_64:设置 RFLAGS 的 TF (bit 8)
  触发 CPU 单步执行

### user.rs: SINGLESTEP + #DB 异常路由
- 为 x86_64 添加 singlestep setup 调用(之前仅 riscv64)
- 新增 `ExceptionKind::Debug` 处理分支:命中 traced task 的 #DB
  异常时,清除 TF 并调用 ptrace_stop_current(SIGTRAP)
- Intel SDM 规定 CPU 在交付 TF 诱导的 #DB 时会清除 pushed RFLAGS
  中的 TF,但 QEMU 不完全遵守,故显式清除防止 resume 后再次单步

## Test cases (test-suit/starryos/normal/qemu-smp1)

| 测试 | 验证内容 |
|------|----------|
| test-ptrace-x86-regs | TRACEME -> GETREGS/GETREGSET/SETREGS/SETREGSET 闭环 |
| test-ptrace-exec-stop | TRACEME + execve -> SIGTRAP + DETACH |
| test-ptrace-x86-breakpoint | 双测试:software int3 最小闭环 + SINGLESTEP 基础验证 |
| test-ptrace-x86-singlestep | PTRACE_SINGLESTEP 执行一条指令后以 SIGTRAP stop |
| test-ptrace-x86-breakpoint-reinsert | gdb 风格断点恢复:rewind RIP -> 恢复原字节 -> SINGLESTEP -> 重新插入断点 -> 再次命中 |
| test-gdb-native-batch | gdb -q -batch 端到端:break native_marker / run / bt / info registers / continue |

所有 6 个 x86_64 ptrace case 全部通过。
@54dK3n
54dK3n force-pushed the feat/x86_64-ptrace branch from 97348e1 to 396463a Compare May 31, 2026 11:14
54dK3n and others added 3 commits May 31, 2026 21:24
…docs

- apps/starry/gdb: gdb --version smoke test for x86_64 QEMU
  (qemu-x86_64.toml, build-x86_64-unknown-none.toml, prebuild.sh)
- docs: update mount-umount2 Linux compat documentation with
  shared/private/slave/unbindable propagation, bind subdirectory,
  MS_MOVE ELOOP, MS_REMOUNT|MS_BIND|MS_RDONLY semantics
…64")]

ExceptionKind::Debug and uctx.rflags only exist on x86_64, but the
Debug exception handler in user.rs had no cfg guard, leaking x86_64
single-step logic into all architecture builds.  Wrap the entire
block in #[cfg(target_arch = "x86_64")].

Fixes CI failures on loongarch64/aarch64/riscv64.

@mai-team-app mai-team-app Bot 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.

PR 概述

本 PR 为 StarryOS 内核添加 x86_64 架构的 ptrace 支持,包括:

  1. 内核 ptrace 实现(ptrace.rs + user.rs):

    • X8664UserRegs 结构体,匹配 Linux user_regs_struct 布局(27 个 u64 字段)
    • PTRACE_GETREGS / SETREGS / GETREGSET / SETREGSET 支持
    • PTRACE_SINGLESTEP 通过 RFLAGS Trap Flag(bit 8)实现,符合 Intel SDM Vol 3A §17.3.2
    • #DB 异常处理:检测 ExceptionKind::Debug + ptrace 状态,清除 TF 后触发 SIGTRAP
    • PTRACE_GETSIGINFO / SETSIGINFO 从 riscv64 扩展到 x86_64
    • 所有新增代码正确使用 cfg target_arch 门控
  2. 新增 5 个 x86_64 测试用例(test-suit/starryos/normal/qemu-smp1/):

    • test-ptrace-x86-regs:GETREGS/GETREGSET 一致性 + SETREGSET/SETREGS 寄存器读写
    • test-ptrace-x86-breakpoint:软件断点(int3)+ 寄存器读回 + RIP 重定向
    • test-ptrace-x86-singlestep:PTRACE_SINGLESTEP 单步执行 + RIP 前进验证
    • test-ptrace-x86-breakpoint-reinsert:断点恢复 + 单步 + 断点重插入
    • test-gdb-native-batch:GDB batch 模式端到端测试
    • 测试放置正确,在 test-suit/starryos/normal/qemu-smp1/ 下
  3. GDB smoke app(apps/starry/gdb/):放置正确

  4. 现有测试 x86_64 扩展:test-ptrace-exec-stop 新增 qemu-x86_64.toml

  5. 文档更新:mount-umount2-linux-compat.md 更新了 shared subtree 语义描述

实现逻辑分析

ptrace 实现遵循现有 riscv64 模式:

  • X8664UserRegs::from(UserContext) 正确映射所有通用寄存器,orig_rax 使用 u64::MAX 作为非系统调用停止的哨兵值
  • write_to() 正确回写通用寄存器和 RIP/RSP/RFLAGS,不回写段寄存器 ds/es/fs/gs(64 位模式下无意义,可接受)
  • ptrace_setup_singlestep() 直接设置 TF 位,无需额外 tracee 状态(与 riscv64 不同,x86_64 硬件自动清除 TF)
  • #DB 异常处理在 user.rs 中正确门控,清除 TF 防止 QEMU 不可靠行为

测试代码质量良好:断言不静默跳过,错误路径打印 FAIL 标记,success_regex 精确匹配

本地验证

cargo fmt --check 通过(无格式问题)
clippy 无法在 x86_64 host 上验证 no-std 目标构建(需要 x86_64-unknown-none target)

CI 状态

所有 GitHub Actions 检查均显示 conclusion=skipped。可能因路径过滤器未覆盖本 PR 变更的文件。无 CI 通过证据。

重复与重叠分析

  • PR #1059(同一作者 54dK3n)Mount umount2 linux compat:本 PR 包含对 mount-umount2-linux-compat.md 的大量更新(84 行新增、20 行删除),描述 shared/private/slave/unbindable 传播语义。经确认这些内核实现已在 origin/dev 基线中。但文档更新属于 mount 子系统维护,应放入 #1059 而非 ptrace PR。
  • 基线已有 riscv64 ptrace 测试(test-ptrace-exec-stop、test-ptrace-gdb、test-ptrace-traceme-stop),本 PR 正确扩展到 x86_64 并新增 x86_64 特有测试。
  • 无其他 PR 与 ptrace x86_64 功能冲突。

Powered by mimo-v2.5-pro

staging_root="${STARRY_STAGING_ROOT:-}"
overlay_dir="${STARRY_OVERLAY_DIR:-}"

READELF="${READELF:-/opt/homebrew/opt/binutils/bin/readelf}"

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.

READELF 默认路径是 macOS(Apple Silicon Homebrew)专用路径。在 Linux 开发环境或 CI 中,如果不设置 READELF 环境变量,ensure_host_tools() 会将 readelf 报为缺失并退出。

建议改为使用系统 PATH 查找:
READELF="${READELF:-readelf}"

@@ -84,19 +88,20 @@ Linux 中:

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.

本 PR 核心是 x86_64 ptrace 支持,但对此文档做了大量更新(+84/-20),描述 shared/private/slave/unbindable 传播语义实现状态。经验证这些内核实现已在 dev 基线中,文档更新准确。但此变更与 ptrace 功能无关,建议拆分到 PR #1059(同一作者的 mount 兼容 PR)中,保持每个 PR 职责单一。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant