diff --git a/SUMMARY.md b/SUMMARY.md index 08ecd780d1d0c72455a6633c0dc9c1c20c578247..3d46422c5200d34c32de1bced67ff130030aac6b 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -5,6 +5,7 @@ - 快速上手 - [虚拟平台运行](tutorial/quick-start.md) - [构建rootfs](tutorial/build-rootfs.md) + - [虚拟化Xhyp](tutorial/xhyp.md) - 内核 - [整体框架](programing-manual/base/framework.md) diff --git a/programing-manual/xhyp/figures/cmd.png b/programing-manual/xhyp/figures/cmd.png new file mode 100644 index 0000000000000000000000000000000000000000..d07062062fc87548963285f294a6e54127fef0a9 Binary files /dev/null and b/programing-manual/xhyp/figures/cmd.png differ diff --git a/programing-manual/xhyp/figures/create.png b/programing-manual/xhyp/figures/create.png new file mode 100644 index 0000000000000000000000000000000000000000..adf19437b60fe1442aea6678467a7aa7d612b41d Binary files /dev/null and b/programing-manual/xhyp/figures/create.png differ diff --git a/programing-manual/xhyp/figures/hook.png b/programing-manual/xhyp/figures/hook.png new file mode 100644 index 0000000000000000000000000000000000000000..a12d9348493c62f1f8357433465cfe15c32763d4 Binary files /dev/null and b/programing-manual/xhyp/figures/hook.png differ diff --git a/programing-manual/xhyp/figures/menuconfig.png b/programing-manual/xhyp/figures/menuconfig.png new file mode 100644 index 0000000000000000000000000000000000000000..47d90040f0c2a26f9c792997e4a723c7203b3337 Binary files /dev/null and b/programing-manual/xhyp/figures/menuconfig.png differ diff --git a/programing-manual/xhyp/figures/run.png b/programing-manual/xhyp/figures/run.png new file mode 100644 index 0000000000000000000000000000000000000000..c34f811947146e3cab011fdf2f897fdfd2253f3e Binary files /dev/null and b/programing-manual/xhyp/figures/run.png differ diff --git a/programing-manual/xhyp/misc/gos.bin b/programing-manual/xhyp/misc/gos.bin new file mode 100644 index 0000000000000000000000000000000000000000..57cb9326e720310c3a3ddf9b573892fd6180aa70 Binary files /dev/null and b/programing-manual/xhyp/misc/gos.bin differ diff --git a/programing-manual/xhyp/misc/romdisk.S b/programing-manual/xhyp/misc/romdisk.S new file mode 100644 index 0000000000000000000000000000000000000000..37f285f94289279c502ba1a12400c87d9a7d84ac --- /dev/null +++ b/programing-manual/xhyp/misc/romdisk.S @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2018-2022, NXOS Development Team + * SPDX-License-Identifier: Apache-2.0 + * + * Contains: romdisk from cpio binary + * + * Change Logs: + * Date Author Notes + * 2022-3-21 JasonHu Init + */ + +#define __ASSEMBLY__ +#include + +#ifdef CONFIG_NX_DRIVER_ROMDISK + +.section .romdisk, "a" +.incbin "../../rootfs.cpio" /* path from current dir */ + +#endif diff --git a/programing-manual/xhyp/misc/rootfs.cpio b/programing-manual/xhyp/misc/rootfs.cpio new file mode 100644 index 0000000000000000000000000000000000000000..8d5de932481c9f71cda3cb8abc8bce658c4d1a83 Binary files /dev/null and b/programing-manual/xhyp/misc/rootfs.cpio differ diff --git a/tutorial/xhyp.md b/tutorial/xhyp.md new file mode 100644 index 0000000000000000000000000000000000000000..c22c19289bb610e07fef53a446263f29e49dcb89 --- /dev/null +++ b/tutorial/xhyp.md @@ -0,0 +1,88 @@ +# `Xhyp` 虚拟化框架设计与实现 +## 更新时间:2023-11-09 + +### 项目简介 +`Xhyp` 项目是经过对 `nxos` 内核进行修改而实现的 `Type-1` 型虚拟化框架,其最终的目标是在单板系统上启动并运行多种操作系统,包括 `Linux`、`nxos` 以及一些流行的实时操作系统等。 + +项目目前仅面向 `RISC-V` 指令集架构进行初步的开发。 + +### 虚拟化框架接口设计 +#### 虚拟机状态控制 +在 `nxos` 内核中创建相关的数据结构,记录系统虚拟化状态以及虚拟机相关状态,设计相关接口如下表。 + +功能 | 接口 | 备注 +--- | --- | --- +查看虚拟机 | `xhyp -l` +创建虚拟机 | `xhyp -c 0` | 以第0号配置创建虚拟机 +运行虚拟机 | `xhyp -r 0` | 运行编号为0的虚拟机 +暂停虚拟机 | `xhyp -p 0` | 暂停编号为0的虚拟机 +停止虚拟机 | `xhyp -h 0` | 将编号为0的虚拟机关机 +销毁虚拟机 | `xhyp -d 0` | 销毁编号为0的虚拟机,并回收相关资源 +运行帮助 | `xhyp -h` | 打印命令内容 + +#### CPU虚拟化 +在 `nxos` 内核中利用线程管理相关接口,将 `vCPU` 映射为内核中的线程,设计相关接口如下表。 + +功能 | 接口 +--- | --- +创建 `vCPU` | VcpuCreate +运行 `vCPU` | VcpuGo +暂停 `vCPU` | VcpuPause +停止 `vCPU` | VcpuHalt +销毁 `vCPU` | VcpuDestroy + +#### 内存虚拟化 +内存虚拟化很大程度上复用了 `nxos` 内核中的内存映射代码,缩小了重复代码量,实现了精简化。 + +#### `I/O` 虚拟化 +针对 `I/O` 设备有多种管理方式:设备直通、中介设备直通、`VritIO`、设备模拟等。 + +目前仅针对虚拟机实现了最基本的串口设备直通功能,满足基本的虚拟机输入输出。 + +后续会针对需求,进一步完善 `I/O` 虚拟化方式。 + +### 虚拟化框架实现计划和进度 +#### 实现计划 +虚拟化有多种实现,按照 `CPU` 是否按照调度,可以简单分为静态分区(`Static Partition Hypervisor, SPH`)和动态调度两种实现。 + +前者较为典型的实现是西门子的 `Jailhouse` 和 `Bao-hypervisor`,后者则有开源项目 `minos` 可供借鉴。 + +### 使用指南 +#### 临时修改 +本项目在主线多合并后出现了一些小问题,暂时需要回滚到提交版本: +`git reset --hard 9f16e9f` + +#### 项目编译 +`nxos` 的虚拟化功能 `xhyp` 并不是默认开启的功能,需要使用者在编译前简单修改配置。 + +参考 [编译与运行](https://gitee.com/BookOS/nxos-documentation/blob/master/tutorial/quick-start.md) 文档依次输入命令。在输入 `make menuconfig` 的时候,在 `OS Kernel -> xhyp - nxos virtualization -> Using NXOS as hypervisor` 下开启虚拟化功能 `xhyp`,如下图所示。 + +![在配置菜单中开启虚拟化功能1](../programing-manual/xhyp/figures/menuconfig.png) + +此外,还需要在 `Hook system` 中打开 `thread context switch` 选项,如下: + +![在配置菜单中开启虚拟化功能2](../programing-manual/xhyp/figures/hook.png) + +#### 文件补充 +在运行前,还需要补充 `3` 个相关的[文件](../programing-manual/xhyp/misc),分别是: + +文件 | 路径 | 备注 +--- | --- | --- +`rootfs.cpio` | `nxos/` | +`romdisk.S` | `nxos/src/drivers/block/` | +`gos.bin` | `nxos/src/platform/qemu_riscv64/tools/` | `Guest OS` 镜像文件 + +#### 运行指令 +完成上述操作后,在顶级目录下执行 `make run`,就会开始编译并运行,如下图所示。 + +![xhyp 命令菜单](../programing-manual/xhyp/figures/cmd.png) + +然后可以使用 `xhyp` 命令创建、运行虚拟机,具体如下: + +`xhyp -n 0`,创建虚拟机: + +![xhyp 创建虚拟机](../programing-manual/xhyp/figures/create.png) + +`xhyp -r 0`,运行虚拟机: + +![xhyp 运行虚拟机](../programing-manual/xhyp/figures/run.png)