qemu-kvm 代码分析

Posted allcloud

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qemu-kvm 代码分析相关的知识,希望对你有一定的参考价值。

qemu-kvm 代码分析


虚拟机组成
实际上面所说计算机组成比较笼统,实际处理器,存储器,输入与设备种类繁多,
    -   i440FX host PCI bridge and PIIX3 PCI to ISA bridge
  总线桥:  i440FX pci桥,PIIX3 ISA桥
    -   Cirrus CLGD 5446 PCI VGA card or dummy VGA card with Bochs VESA
           extensions (hardware level, including all non standard modes).
  显卡:
    -   PS/2 mouse and keyboard
  鼠标和键盘
       -   2 PCI IDE interfaces with hard disk and CD-ROM support
  IDE的硬盘和光驱
       -   Floppy disk
  软盘
       -   PCI and ISA network adapters
  PCI ISA 总线
       -   Serial ports
  串口
       -   Creative SoundBlaster 16 sound card
  声卡 SoundBlaster 1370 AC97等
       -   ENSONIQ AudioPCI ES1370 sound card
 
       -   Intel 82801AA AC97 Audio compatible sound card

       -   Intel HD Audio Controller and HDA codec

       -   Adlib(OPL2) - Yamaha YM3812 compatible chip

       -   Gravis Ultrasound GF1 sound card

       -   CS4231A compatible sound card
 UHCI USB总线控制器
       -   PCI UHCI USB controller and a virtual USB hub.
 最大支持 255个cpu
       SMP is supported with up to 255 CPUs.

qemu-kvm参数说明:
 -S -M rhel6.3.0 -enable-kvm  //启用硬件辅助模拟,模拟的机器类型为rhel6.3.0,虚拟机默认不启动。(后面解释类型为rhel6.3.0大概配置)
//机器配置
-m 1111  //内存
-smp 1,sockets=1,cores=1,threads=1 //处理器
-rtc base=localtime,clock=vm,driftfix=slew //时钟
-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 //usb控制器usb-uhci
-device usb-ehci,id=usb1,bus=pci.0,addr=0x5 //usb控制器usb-ehci
-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 //pci
-drive file=/mnt/local/4d4fdbb5-216a-44a4-afae-ea6e2a80b481,if=none,id=drive-ide0-0-0,format=qcow2,cache=writeback //ide硬盘
-device ide-drive,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=2 //主ide
-drive if=none,media=cdrom,id=drive-ide0-1-0,readonly=on,format=raw,cache=writeback //ide光驱
-device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0,bootindex=1  //从ide
-netdev tap,fd=23,id=hostnet0 -device e1000,netdev=hostnet0,id=net0,mac=02:00:77:a2:00:0d,bus=pci.0,addr=0x3  //网卡
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 //伪终端
-chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 //串口
-device usb-tablet,id=input0,bus=usb.0,port=1  //触摸盘
-spice port=5902,addr=0.0.0.0,disable-ticketing //远程显示
-vga qxl -global qxl-vga.vram_size=67108864 -device AC97,id=sound0,bus=pci.0,addr=0x4  //显卡
-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -readconfig /etc/qemu/ich9-ehci-uhci.cfg -chardev spicevmc,name=usbredir,id=usbredirchardev1
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1,bus=ehci.0,debug=3 -chardev spicevmc,name=usbredir,id=usbredirchardev2 //usb设备重定向
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2,bus=ehci.0,debug=3 -chardev spicevmc,name=usbredir,id=usbredirchardev3
-device usb-redir,chardev=usbredirchardev3,id=usbredirdev3,bus=ehci.0,debug=3

虚拟机流程

1.硬件虚拟设备初始化
main-->kvm_init-->kvm_create_context-->kvm_create
r = kvm_create_vm(kvm);
r = kvm_arch_create(kvm, phys_mem_bytes, vm_mem);
r = kvm_create_default_phys_mem(kvm, phys_mem_bytes, vm_mem);
kvm_create_irqchip(kvm);
由上面代码可见,硬件虚拟技术为处理器,内存,中断

2.启动虚拟机
1>初始化所有设备,
module_call_init(MODULE_INIT_DEVICE);
块驱动,外围设备,机器初始化...
module.h:30:#define block_init(function) module_init(function, MODULE_INIT_BLOCK)
module.h:31:#define device_init(function) module_init(function, MODULE_INIT_DEVICE)
module.h:32:#define machine_init(function) module_init(function, MODULE_INIT_MACHINE)

hw/sb16.c:1418:device_init (sb16_register)
module.h:31:#define device_init(function) module_init(function, MODULE_INIT_DEVICE)

block/qcow2.c:1525:block_init(bdrv_qcow2_init);
module.h:30:#define block_init(function) module_init(function, MODULE_INIT_BLOCK)

hw/pc.c:1787:machine_init(rhel_machine_init);
module.h:32:#define machine_init(function) module_init(function, MODULE_INIT_MACHINE)

2>根据-M rhel6.3.0类型启动虚拟机。
main--> machine->init(ram_size, boot_devices,kernel_filename, kernel_cmdline, initrd_filename, cpu_model);



1)创建vcpu
kvm_vm_ioctl(kvm_state, KVM_CREATE_VCPU, id);
初始化vcpu
kvm_arch_init_vcpu
2) 创建内存
    mmap_size = kvm_ioctl(kvm_state, KVM_GET_VCPU_MMAP_SIZE, 0);
   env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, env->kvm_fd,
3) 启动虚拟机,运行系统
kvm_run(env);

qemu-kvm虚拟机线程
1. qemu-kvm线程工作过程:
1)启动一个子线程,创建初始化vcpu,主线程等待
2)子线程创建初始化vcpu完毕,子线程等待,并等候通知主线程运行
3)主线程继续初始化虚拟机工作,初始化完成,通知子线程继续运行
4)子线程继续启动虚拟机kvm_run,主线程执行select交互处理



[[email protected] ~]$ ps aux |grep qemu
qemu     27033  1.3 12.9 3153132 502284 ?      Sl   14:58   4:19 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2 -device lsi,id=scsi0,bus=pci.0,addr=0x9 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough -device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8

http://blog.chinaunix.net/uid-26000137-id-3747844.html

2.一个虚拟机进程包含着几个线程
1)启动虚拟机
2)ps -eLf | grep qemu-kvm


[[email protected] ~]$ sudo ps -eLf | grep qemu
qemu     27033     1 27033  1    5 14:58 ?        00:03:14 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2 -device lsi,id=scsi0,bus=pci.0,addr=0x9 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough -device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 27090  0    5 14:58 ?        00:00:34 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2 -device lsi,id=scsi0,bus=pci.0,addr=0x9 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough -device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 27092  0    5 14:58 ?        00:00:27 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2 -device lsi,id=scsi0,bus=pci.0,addr=0x9 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough -device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 27093  0    5 14:58 ?        00:00:00 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2 -device lsi,id=scsi0,bus=pci.0,addr=0x9 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough -device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
qemu     27033     1 29367  0    5 20:14 ?        00:00:00 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name RHEL6.5-20131111.0-Server -S -machine pc-i440fx-1.6,accel=kvm,usb=off -m 1024 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 3e537a51-2313-47a5-901a-51ae497732f7 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2 -device lsi,id=scsi0,bus=pci.0,addr=0x9 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive file=/var/lib/libvirt/images/RHEL6.5-20131111.0-Server.1393425363,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on,format=raw -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -drive file=/home/simon/scsi1c.qcow2,if=none,id=drive-scsi0-0-1,format=qcow2,cache=writethrough -device scsi-hd,bus=scsi0.0,scsi-id=1,drive=drive-scsi0-0-1,id=scsi0-0-1 -netdev tap,fd=26,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:ee:9b:67,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -device usb-tablet,id=input0 -vnc 127.0.0.1:1 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x8
simon    29431 29188 29431  0    1 20:15 pts/2    00:00:00 grep --color=auto qemu




[[email protected] ~]$ sudo gdb -p 27033

(gdb) info thread
  Id   Target Id         Frame
  5    Thread 0x7fb6929e4700 (LWP 27090) "qemu-system-x86" 0x00007fb6a0246067 in ioctl () from /lib64/libc.so.6
  4    Thread 0x7fb68b422700 (LWP 27092) "qemu-system-x86" 0x00007fb6a02449dd in poll () from /lib64/libc.so.6
  3    Thread 0x7fb68abff700 (LWP 27093) "qemu-system-x86" 0x00007fb6a81bad20 in [email protected]@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  2    Thread 0x7fb607fff700 (LWP 29444) "qemu-system-x86" 0x00007fb6a81bcec0 in sem_timedwait () from /lib64/libpthread.so.0
* 1    Thread 0x7fb6a9f0fa80 (LWP 27033) "qemu-system-x86" 0x00007fb6a02449dd in poll () from /lib64/libc.so.6

整好5个

(gdb) bt
#0  0x00007fb6a02449dd in poll () from /lib64/libc.so.6
#1  0x00007fb6aa155046 in main_loop_wait ()
#2  0x00007fb6aa0079a0 in main ()
这个线程是主线程

(gdb) thread 2
[Switching to thread 2 (Thread 0x7fb607fff700 (LWP 29444))]
#0  0x00007fb6a81bcec0 in sem_timedwait () from /lib64/libpthread.so.0
(gdb) bt
#0  0x00007fb6a81bcec0 in sem_timedwait () from /lib64/libpthread.so.0
#1  0x00007fb6aa2f6487 in qemu_sem_timedwait ()
#2  0x00007fb6aa1a848c in worker_thread ()
#3  0x00007fb6a81b6f33 in start_thread () from /lib64/libpthread.so.0
#4  0x00007fb6a024eded in clone () from /lib64/libc.so.6
(gdb) thread 3
[Switching to thread 3 (Thread 0x7fb68abff700 (LWP 27093))]
#0  0x00007fb6a81bad20 in [email protected]@GLIBC_2.3.2 () from /lib64/libpthread.so.0
(gdb) bt
#0  0x00007fb6a81bad20 in [email protected]@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x00007fb6aa2f62b9 in qemu_cond_wait ()
#2  0x00007fb6aa1c86a3 in vnc_worker_thread_loop ()
#3  0x00007fb6aa1c8b30 in vnc_worker_thread ()
#4  0x00007fb6a81b6f33 in start_thread () from /lib64/libpthread.so.0
#5  0x00007fb6a024eded in clone () from /lib64/libc.so.6
异步进行i/o操作,主要针对磁盘映像操作(block drive)
(gdb) thread 4
[Switching to thread 4 (Thread 0x7fb68b422700 (LWP 27092))]
#0  0x00007fb6a02449dd in poll () from /lib64/libc.so.6
(gdb) bt
#0  0x00007fb6a02449dd in poll () from /lib64/libc.so.6
#1  0x00007fb6a0f2e8f7 in red_worker_main () from /lib64/libspice-server.so.1
#2  0x00007fb6a81b6f33 in start_thread () from /lib64/libpthread.so.0
#3  0x00007fb6a024eded in clone () from /lib64/libc.so.6
(gdb) thread 5
[Switching to thread 5 (Thread 0x7fb6929e4700 (LWP 27090))]
#0  0x00007fb6a0246067 in ioctl () from /lib64/libc.so.6
(gdb) bt
#0  0x00007fb6a0246067 in ioctl () from /lib64/libc.so.6
#1  0x00007fb6aa230475 in kvm_vcpu_ioctl ()
#2  0x00007fb6aa230554 in kvm_cpu_exec ()
#3  0x00007fb6aa1d94b2 in qemu_kvm_cpu_thread_fn ()
#4  0x00007fb6a81b6f33 in start_thread () from /lib64/libpthread.so.0
#5  0x00007fb6a024eded in clone () from /lib64/libc.so.6
这个子线程,kvm_run启动和运行虚拟机


qemu-kvm虚拟机进程的字符设备处理(char drive)
1.查看采用哪些字符设备
[[email protected] ~]$ ps aux |grep qemu |grep "\-chardev"
-chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/RHEL6.5-20131111.0-Server.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control
-chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0
-chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0


以上是关于qemu-kvm 代码分析的主要内容,如果未能解决你的问题,请参考以下文章

恶意代码分析技巧-汇总

常见静态代码分析工具

恶意代码分析技术

代码分析平台 SonarQube 实战

动态生成代码的代码覆盖率、分析和分析

[恶意代码分析]恶意代码种类以及分析环境介绍