qemu cxl

1
2
3
4
5
6
7
8
9
./build/aarch64-softmmu/qemu-system-aarch64 -M virt,gic-version=3,cxl=on -m 4g,maxmem=8G,slots=8 -cpu max \
-hda ../vm_disk/ubuntu_cxl.qcow2 \
-cdrom ../ubuntu-20.04.5-desktop-amd64.iso \
-object memory-backend-file,id=cxl-mem1,share=on,mem-path=/tmp/cxltest.raw,size=256M \
-object memory-backend-file,id=cxl-lsa1,share=on,mem-path=/tmp/lsa.raw,size=256M \
-device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
-device cxl-rp,port=0,bus=cxl.1,id=root_port13,chassis=0,slot=2 \
-device cxl-type3,bus=root_port13,memdev=cxl-mem1,lsa=cxl-lsa1,id=cxl-pmem0 \
-M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G

-M

Libnvdimm

image-20221015200747925

image-20221015200623751

image-20221015200854839

1
2
3
4
5
6
7
8
9
10
11
12
13
~/qemu/build/aarch64-softmmu/qemu-system-aarch64 -M virt,gic-version=3,cxl=on -m 4g,maxmem=8G,slots=8 -cpu max \
-bios ~/repo/edk2/Build/ArmVirtQemu-AARCH64/DEBUG_GCC48/FV/QEMU_EFI.fd \
-kernel ~/linux/arch/arm64/boot/Image \
-initrd ./rootfs.cpio.gz \
-monitor telnet:127.0.0.1:4444,server,nowait \
-object memory-backend-file,id=cxl-mem1,share=on,mem-path=/tmp/cxltest.raw,size=256M \
-object memory-backend-file,id=cxl-lsa1,share=on,mem-path=/tmp/lsa.raw,size=256M \
-device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
-device cxl-rp,port=0,bus=cxl.1,id=root_port13,chassis=0,slot=2 \
-device cxl-type3,bus=root_port13,memdev=cxl-mem1,lsa=cxl-lsa1,id=cxl-pmem0 \
-cxl-fixed-memory-window targets.0=cxl.1,size=4G \
-append "console=ttyAMA0 root=/dev/ram rdinit=/init acpi=on" \
-nographic \

image-20221015202421449

State-of-the-art Shitcode

1
2
3
4
5
6
7
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j16
sudo make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules_install
sudo make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- install
sudo update-grub
reboot

Giant VM 操作过程

主机环境:Windows 11 64位 core i9 12900p

此次使用VMWare Workstation pro 16进行操作

1.环境配置

step1 系统准备

win+r 打开cmd

sysinfo

查看是否开启了Hyper-V,如果开启应将其关闭

关闭方式

  • 【启用或关闭Windows功能】-> 将虚拟机平台和windows虚拟机监控程序平台关闭
    image-20220930105100032
  • 如果看到了Hyper-V的选项框,将其取消勾选
  • 然后重启生效修改

step2 VMware 设置

VMware 中启动Ubuntu16.04
配置为
内核: Linux4.15.0-112,磁盘分配>40G,在CPU设置中启用嵌套虚拟化

step3 下载必要的包

1
2
3
sudo apt-get install build-essential openssl libncurses5-dev libssl-dev

sudo apt-get install zlibc minizip libidn11-dev libidn11 bison flex

step4 获得Linux-DSM

1
git clone https://github.com/GiantVM/Linux-DSM.git

step5

1
cd Linux-DSM

step6 Enable DSM support

1
make menuconfig

Virtualization –> KVM distributed software memory support –> press 'Y' to include the option
Save –> Exit

step7 Compile the Kernel (make)

make -jN
[N 是]
wait for about three hour(or more)
之前的失败经历: Environment: win11 wsl2 Ubuntu16.04 LinuxKernel version 5.10 output : makefile:976: recipe for target 'vmlinux' failed

step 8 install the Kernel

1
2
sudo make modules_install
sudo make install

step 9 update the grub

[在我的尝试中,这时候应当先打开grub这个文件]
[gedit 比较方便看,用vi也可以]

1
sudo gedit /etc/default/grub

[然后将GRUB_HIDDEN_TIMEOUT 这个属性置为0,不然之后重启的时候没时间换系统]
这自己操作
[然后是核心操作 ]

1
sudo update-grub

[之后重启]

1
reboot

[重启后看到下面界面,按照图片选择]
[之后等待,启动后,在shell里输入]

1
uname -a 

[可以看到版本为ubuntu 4.9.76+]

2.QEMU

step1 Prepartion

sudo apt-get install python pkg-config libglib2.0-dev zlib1g-dev libpixman-1-dev libfdt-dev
git clone https://github.com/GiantVM/QEMU.git
cd QEMU

step2 Configuration

1
./configure --target-list=x86_64-softmmu --enable-kvm

step3 Compilation

1
make -jN

step4 Create hard disk image

1
2
cd ..
wget http://ftp.sjtu.edu.cn/ubuntu-cd/16.04.7/ubuntu-16.04.7-server-amd64.iso

[如果找不到,可以直接输入http://ftp.sjtu.edu.cn/ubuntu-cd/16.04.7,在里面找到Ubuntu-16.x-server.iso ,然后wget]

[这里就是得用apt下一个qemu,选择不下,用底下x86-64_softmmu/ 底下的qemu-system-x86_64会卡死]

1
2
3
/qemu-img create -f qcow2 ubuntu-server.img 10G
sudo apt-get install qemu
qemu-system-x86_64 -m 1024 ubuntu-server.img -cdrom ../ubuntu-16.04.7-server-amd64.iso -enable-kvm

[上面会跳出系统设置,基本设置一下用户名和密码,然后会问是否要装载GRUB,选择yes,其他无所谓]

3.Run Giant VM on a single machine

First we install vncviewer to monitor the guest.

1
2
wget https://www.realvnc.com/download/file/viewer.files/VNC-Viewer-6.19.325-Linux-x64.deb
sudo dpkg -i VNC-Viewer-6.19.325-Linux-x64.deb

如果下面报错说内存不够,把虚拟机关掉,多分配给它一点内存(>8G)

terminal 1 :

1
2
3
cd QEMU/

sudo x86_64-softmmu/qemu-system-x86_64 --nographic -hda ubuntu-server.img -cpu host -machine kernel-irqchip=off -smp 4 -m 4096 --enable-kvm -serial mon:stdio -local-cpu 2,start=0,iplist="127.0.0.1 127.0.0.1" -vnc :0

terminal 2:

1
2
cd QEMU/
sudo x86_64-softmmu/qemu-system-x86_64 --nographic -hda ubuntu-server.img -cpu host -machine kernel-irqchip=off -smp 4 -m 2048 --enable-kvm -serial mon:stdio -local-cpu 2,start=2,iplist="127.0.0.1 127.0.0.1"

terminal 3:[启动 vncviewer]

冒号后面的0和前面 terminal1 后面-vnc 后面的数字对应

1
vncviewer :0

如果启动之后看到 nobootable device ,可能需要检查2 QEMU 最后一步的 qemu-system-x86_64 那段是不是正常

CPU虚拟化

MAIN TASK: 执行虚拟机程序指令,响应虚拟机内外部事件

面临的挑战

1. 敏感非特权指令

挑战来源: 基于 陷入-模拟 机制的虚拟化架构只能在所有敏感指令都是特权指令的架构中被建立
敏感指令: 操作敏感物理资源的指令,如I/O指令、页表基地址切换指令等
特权指令; 必须运行在最高特权级的指令,在非特权级中执行这些指令将会触发特权级切换。
解决方案:

软件方案: 解释执行【无脑解释执行所有指令,效率较低】,二进制翻译【将敏感指令替换为其他指令,会增加指令数量】,扫描与修补【在执行前,将敏感指令替换为特权指令,代码局部性较差】,半虚拟化【在执行敏感指令的时候通过超调用主动陷入Hypervisor中,避免扫描二进制代码引入的开销,但是打破了虚拟机和Hypervisor之间的界限】
硬件辅助方案: Intel VT-x,AMD SVM, ARM EL2, RISC-V H-Extension
将所有敏感指令转化为特权指令【可能存在兼容性问题】
引入虚拟化模式

2. 上下文切换

类似进程上下文,虚拟机发生退出时需要保持各寄存器的状态
发生虚拟CPU调度时,需要保存当前虚拟CPU的上下文兵加载待调度虚拟CPU上下文

3. 中断处理

模拟方式:为每一个虚拟机维护一个虚拟中断控制器

QEMU/KVM CPU虚拟化实现