Skip to content

feat(x86_64): Multiboot2 zone loading and virtio device fixes for Asterinas#98

Open
yydawx wants to merge 3 commits into
syswonder:dev-asterinasfrom
yydawx:ccf-asterinas
Open

feat(x86_64): Multiboot2 zone loading and virtio device fixes for Asterinas#98
yydawx wants to merge 3 commits into
syswonder:dev-asterinasfrom
yydawx:ccf-asterinas

Conversation

@yydawx

@yydawx yydawx commented Jun 3, 2026

Copy link
Copy Markdown

Summary

PR for Multiboot2-based zone loading (required by Asterinas) and virtio
device robustness fixes. Complements the hvisor-side PR at
yydawx/hvisor#.

Changes

Multiboot2 Zone Loading (tools/hvisor.c, include/zone_config.h)

  • Parse ELF kernel images and load individual segments to guest memory
  • Build Multiboot2 info structure: basic memory info, memory map, boot
    command line, initramfs module, and optional framebuffer tags
  • Extend zone_config struct with multiboot_info_paddr and
    multiboot_enabled fields; bump CONFIG_MAGIC_VERSION to 0x06

Virtio Device Fixes (tools/virtio/virtio.c)

  • Always set queue_sel on MMIO write so the guest can correctly
    probe the number of queues by iterating queue_sel values
  • Return 0 in QUEUE_NUM_MAX read when queue_sel is out of bounds
  • Add NULL pointer checks after get_virt_addr() in virtqueue
    descriptor, avail, and used table setup
  • Initialize config.blk_size to SECTOR_BSIZE (512) to match the
    guest's expected block size

Console Terminal Fix (tools/virtio/devices/console/virtio_console.c)

  • Convert LF to CRLF in the TX handler to prevent garbled text
    indentation when the PTY master does not handle ONLCR
  • Return error codes and close file descriptors after PTY init
    failures instead of continuing with an invalid state

Verification

  • Asterinas zone1 boots via Multiboot2 with console, blk, and net
  • Regression tests and benchmarks pass under the hvisor + hvisor-tool
    combination
  • Linux zone1 still works, but I'm not sure the regression problem

yydawx added 2 commits June 3, 2026 23:31
…setup

- Always set queue_sel in virtio-mmio write handler so the guest can
  correctly probe the number of queues by iterating queue_sel values.
- Return 0 in QUEUE_NUM_MAX read handler when queue_sel is out of bounds
  to signal the end of the queue list.
- Add NULL pointer checks after get_virt_addr() calls in virtqueue
  descriptor/avail/used table setup to fail gracefully.
- Initialize config.blk_size to SECTOR_BSIZE (512) in virtio-blk device
  init to match the guest's assertion.
- Return -ENOMEM on kmalloc failure in the hvisor kernel module.
- Guard against NULL vdev->dev pointer in virtio_mmio_read config access.

Assisted by: DeepSeek V4
- Return error codes and close file descriptors after PTY operations
  (posix_openpt, grantpt, unlockpt) fail during initialization.
- Enable ONLCR terminal flag so newline characters are automatically
  translated to carriage-return plus newline.
- Convert LF to CRLF in the TX handler as a fallback for environments
  where the PTY master does not handle ONLCR.

Assisted by: DeepSeek V4
@yydawx

yydawx commented Jun 3, 2026

Copy link
Copy Markdown
Author

@Solicey 同学提出的错位问题,我目前在tool这一侧做了处理,所以暂时正常了,如果后期asterinas上游修复了,我们可以去掉这部分修改。
blk写入测试没问题,压力测试还有些问题,但是还在定位出错的地方
net只是让asterinas显示了网卡设备,但是还没有进行功能测试

@yydawx

yydawx commented Jun 3, 2026

Copy link
Copy Markdown
Author

网卡这里因为asterinas的设备初始化顺序有些问题,所以我修改了部分asterinas的源码才能注册识别。我会尝试不修改asterinas就能识别到网卡,尽量减少复现成本。如果实在有必要,再向asterinas上游提交issue

@yydawx yydawx changed the base branch from main to dev-asterinas June 3, 2026 16:27
@caodg caodg requested review from Solicey, agicy and li041 June 3, 2026 23:02
Comment thread include/zone_config.h Outdated
Comment on lines +184 to +187
// Multiboot support (NEW)
__u64 multiboot_info_paddr;
__u32 multiboot_enabled;

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.

As mentioned in hvisor's pr, multiboot_info_paddr and multiboot_enabled should be put inside arch_config

Comment on lines +191 to +207
// Convert \n -> \r\n for proper terminal output.
// PTY master doesn't do ONLCR; we handle it here.
for (int i = 0; i < n; i++) {
char *p = iov[i].iov_base;
size_t remain = iov[i].iov_len;
char *start = p;
char *nl;
while (remain > 0 && (nl = memchr(start, '\n', remain)) != NULL) {
size_t pre = nl - start;
if (pre > 0)
write(dev->master_fd, start, pre);
write(dev->master_fd, "\r\n", 2);
start = nl + 1;
remain -= pre + 1;
}
if (remain > 0)
write(dev->master_fd, start, remain);

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.

I recently raised a pr to Asterinas. This is basically Asterinas's problem, thus I suggest changing your local Asterinas's code instead of hvisor-tool's code.

@yydawx yydawx force-pushed the ccf-asterinas branch 2 times, most recently from edb13a6 to 7d67eb8 Compare June 8, 2026 06:13
- Add multiboot_info_paddr/multiboot_enabled to x86_64 arch_zone_config
- Move multiboot fields from zone_config to arch_config (x86-specific)
- Load boot_filepath binary with GPA-to-HPA offset translation
- Store ELF entry point in kernel_entry_gpa for bootloader
- Skip kernel_entry_gpa JSON parsing when multiboot is enabled
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