Skip to content

[WIP] Migrate x86_64-qemu-virt platform to kbootloader#47

Draft
Copilot wants to merge 1 commit intomainfrom
copilot/migrate-x86-64-to-kbootloader
Draft

[WIP] Migrate x86_64-qemu-virt platform to kbootloader#47
Copilot wants to merge 1 commit intomainfrom
copilot/migrate-x86-64-to-kbootloader

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 10, 2026

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.

Original prompt

Background

The aarch64-qemu-virt platform has been migrated to use the unified kbootloader crate for boot entry. x86_64-qemu-virt is still using platform-private boot.rs/multiboot.S and manual entry functions; the goal is to migrate this platform to the new system, with all entry code unified in kbootloader.

Tasks for this PR (x86_64-qemu-virt only)

  1. Implement x86_64 arch support in kbootloader:
    • Add arch/x86_64/entry.rs: provides __primary_switched(magic, mbi) and __secondary_switched(magic); validates the Multiboot magic, gets current cpu_id using raw_cpuid, calls INIT_PERCPU, INIT_TRAP, then call_kernel_entry.
    • Add arch/x86_64/boot.rs: includes unified multiboot.S as global_asm!; symbols/param config through kbuild_config.
    • Move/merge multiboot.S (and differentiate cbit_mask, entry names etc through kconfig).
    • Cargo.toml: Add [target.'cfg(target_arch = "x86_64")'.dependencies] raw_cpuid = "11"
    • Update arch/mod.rs to include/use x86_64.
  2. Clean up x86_64-qemu-virt platform:
    • Remove boot.rs, all platform-specific global_asm for boot.
    • Remove all platform-level entryers (rust_entry, rust_entry_secondary, current_cpu_id, multiboot magic check etc) from lib.rs/init.rs.
    • Use extern crate kbootloader;, not mod boot; in lib.rs.
    • Remove call to kcpu::boot::init_trap() from early_init/init.rs; boot init now handled via distributed_slice.
    • Keep SMP/secondary startup (mp.rs, ap_start.S) unchanged; ap_entry32 global symbol is provided by kbootloader multiboot.S.
  3. Ensure platform builds and doc all config/entry changes (README/notes as needed).

Design/compatibility notes

  • All changes apply ONLY to x86_64-qemu-virt in this PR (multi-PR plan, one per platform).
  • Use only parameterized multiboot.S in kbootloader, no platform subdir .S. Use kconfig for CBIT_MASK, PHYS_VIRT_OFFSET, BOOT_STACK_SIZE etc.
  • INIT_PERCPU and INIT_TRAP must be registered (by kcpu crate, to ensure migration safety for other platforms).

Next PRs

  • Subsequent PRs will migrate x86-csv and aarch64-crosvm-virt using the same procedure, and expand/merge boot hooks as needed for those platforms.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: j 从kbootloader设计来看 我希望他尽可能少依赖外部库 不过没有办法的花,用 target dependency 引入也行 k: 这个校验逻辑是保留在 kbootloader 中 l) 统一 multiboot.S 的方案 可以用一份 只要你确认功能不会互相影响 m: INIT_PERCPU 的签名应该是 fn(usize) n) 我们可以在 kbootloader 中提供一个可选的 PLATFORM_PRE_MMU 回调 让platform决定是否有额外操作 去决定是否实现 o) 可以用 boot_device_mm[(start, end), (start ,end)] 和 BOOT_NORMAL_MM((start, end), (start ,end)) 配置 然后动态计算映射页表 p 可以

Assistant:

好的,所有技术细节已经确认清楚。让我整理一下完整的实现方案,然后开始创建 PR。

最终实现方案总结

1. kbootloader 新增 x86_64 架构支持

  • 新增 src/arch/x86_64/ 模块:包含 mod.rsentry.rsmultiboot.S
  • 统一 multiboot.S:合并两个平台的汇编,使用 {cbit_mask} 参数(非 SEV 时为 0),保留 _start32 + _start(64-bit 跳板)兼容两种入口
  • __primary_switched:校验 multiboot magic → call_init_percpu!(cpu_id)call_init_trap!()call_kernel_entry!(PRIMARY_KERNEL_ENTRY, cpu_id, mbi)
  • __secondary_switchedcall_init_percpu!(cpu_id)call_init_trap!()call_kernel_entry!(SECOND_KERNEL_ENTRY, cpu_id)
  • current_cpu_id():通过 target dependency 引入 raw_cpuid

2. 新增 distributed_slice

  • INIT_PERCPU: [fn(usize)] — 签名 fn(cpu_id: usize)
  • 新增 call_init_percpu!
  • 调用顺序:init_percpuinit_trapkernel_entry

3. aarch64 新增 PLATFORM_PRE_MMU 回调

  • PLATFORM_PRE_MMU: [fn()] — 可选,platform 决定是否注册
  • create_boot_page_tables() 之前调用
  • crosvm 注册此回调实现 kvm_guard_granule_init + do_xmap_granules

4. aarch64 页表映射可配置化

  • 通过 kbuild_config 新增 BOOT_DEVICE_MMBOOT_NORMAL_MM 配置
  • create_boot_page_tables() 根据配置动态创建映射

5. 三个平台迁移

  • x86_64-qemu-virt:删除 boot.rslib.rsextern crate kbootloaderinit.rs 移除 init_trap 调用
  • x86-csv:同上,额外处理 cbit_mask
  • aarch64-crosvm-virt:删除 boot.rs,统一使用 kbootloader 的 _start(带 MZ header,注释说明 crosvm 待测试),init.rs 移除 init_trappower.rs 引用改为 kbootloader::arch::_start_secondary

让我现在创建 PR。

Assistant: I've submitted the pull request to the coding agent. It will create a PR on the dyn_bootloader branch of guoweikang/x-kernel that migrates all three remaining platforms (x86_64-qemu-virt, x86-csv, aarch64-crosvm-virt) to use the unified kbootloader crate.

Here's a summary of the migration plan the agent will implement:

kbootloader Enhancements

  1. INIT_PERCPU distributed_slice — fn(usize) for x86_64's percpu init before init_trap
  2. PLATFORM_PRE_MMU distributed_slice — fn() for crosvm's KVM guard granule ops before MMU enable
  3. Configurable aarch64 boot page tables via BOOT_DEVICE_MM / BOOT_NORMAL_MM kconfig arrays
  4. x86_64 arch support — unified multiboot.S (with cbit_mask param), Rust entry points, multiboot magic validation
  5. INIT_PERCPU calls added to existing aarch64 entr...

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

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.

2 participants