您的位置:首页 > 编程语言 > Qt开发

qemu中KVM硬件虚拟化的初始化分析 (tcg、xen、kvm、qtest)(转)

2014-11-19 11:10 375 查看
qemu中KVM硬件虚拟化的初始化分析 2014-04-25
14:38:38

分类: 虚拟化

1.传入-enable-kvm参数,启用KVM硬件虚拟化支持:

在文件qemu-options.hx中,有对参数-enable-kvm的定义,如下:

DEF("enable-kvm", 0, QEMU_OPTION_enable_kvm, \

"-enable-kvm enable KVM full virtualization support\n", QEMU_ARCH_ALL)

STEXI

@item -enable-kvm

@findex -enable-kvm

Enable KVM full virtualization support. This option is only available

if KVM support is enabled when compiling.

ETEXI

2.main函数中对应的参数解析

int main(int argc, char **argv, char **envp)

{

/* second pass of option parsing */

optind = 1;

for(;;) {

......

case QEMU_OPTION_enable_kvm:

olist = qemu_find_opts("machine");

qemu_opts_parse(olist, "accel=kvm", 0);

break;

......



......

os_daemonize();

......

configure_accelerator(machine);

......

}

(gdb) p *machine

$46 = {name = 0x555555a17666 "pc-i440fx-2.0", alias = 0x555555a17674 "pc",

desc = 0x555555a175d8 "Standard PC (i440FX + PIIX, 1996)", init = 0x5555558c2e2c <pc_init_pci>, reset = 0,

hot_add_cpu = 0x5555558c1061 <pc_hot_add_cpu>, kvm_type = 0, block_default_type = IF_IDE, max_cpus = 255, no_serial = 0,

no_parallel = 0, use_virtcon = 0, use_sclp = 0, no_floppy = 0, no_cdrom = 0, no_sdcard = 0, is_default = 1,

default_machine_opts = 0x555555a17677 "firmware=bios-256k.bin", default_boot_order = 0x555555a1768e "cad", compat_props = 0x0,

next = 0x0, hw_version = 0x0}

//qemu支持4种方式执行:tcg、xen、kvm、qtest

static struct {

const char *opt_name;

const char *name;

int (*available)(void);

int (*init)(QEMUMachine *);

bool *allowed;

} accel_list[] = {

{ "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },

{ "xen", "Xen", xen_available, xen_init, &xen_allowed },

{ "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },

{ "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },

};

//解析参数,并初始化相关模块

static int configure_accelerator(QEMUMachine *machine)

{

const char *p;

char buf[10];

int i, ret;

bool accel_initialised = false;

bool init_failed = false;

//在没有带-enable-kvm参数时,默认采用软件仿真的tcg

p = qemu_opt_get(qemu_get_machine_opts(), "accel");

if (p == NULL) {

/* Use the default "accelerator", tcg */

p = "tcg";

}

while (!accel_initialised && *p != '\0') {

if (*p == ':') {

p++;

}

p = get_opt_name(buf, sizeof (buf), p, ':');

for (i = 0; i < ARRAY_SIZE(accel_list); i++) {

if (strcmp(accel_list[i].opt_name, buf) == 0) {

if (!accel_list[i].available()) {

printf("%s not supported for this target\n",

accel_list[i].name);

continue;

}

*(accel_list[i].allowed) = true;

ret = accel_list[i].init(machine);

if (ret < 0) {

init_failed = true;

fprintf(stderr, "failed to initialize %s: %s\n",

accel_list[i].name,

strerror(-ret));

*(accel_list[i].allowed) = false;

} else {

accel_initialised = true;

}

break;

}

}

if (i == ARRAY_SIZE(accel_list)) {

fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);

}

}

if (!accel_initialised) {

if (!init_failed) {

fprintf(stderr, "No accelerator found!\n");

}

exit(1);

}

if (init_failed) {

fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);

}

return !accel_initialised;

}

3.kvm_init

int kvm_init(QEMUMachine *machine)

{

......

s->fd = qemu_open("/dev/kvm", O_RDWR);

if (s->fd == -1) {

fprintf(stderr, "Could not access KVM kernel module: %m\n");

ret = -errno;

goto err;

}

//匹配kvm内核模块的版本,版本太旧或者太新都会不支持

ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);

if (ret < KVM_API_VERSION) {

if (ret > 0) {

ret = -EINVAL;

}

fprintf(stderr, "kvm version too old\n");

goto err;

}

if (ret > KVM_API_VERSION) {

ret = -EINVAL;

fprintf(stderr, "kvm version not supported\n");

goto err;

}

......

/* check the vcpu limits */

soft_vcpus_limit = kvm_recommended_vcpus(s); //获取kvm内核推荐使用的vcpu个数

hard_vcpus_limit = kvm_max_vcpus(s); //获取kvm内核最多可用的vcpu个数

......

do {

ret = kvm_ioctl(s, KVM_CREATE_VM, type); //创建一个VM

} while (ret == -EINTR);

......

ret = kvm_arch_init(s);

if (ret < 0) {

goto err;

}

ret = kvm_irqchip_create(s);

if (ret < 0) {

goto err;

}

kvm_state = s;

memory_listener_register(&kvm_memory_listener, &address_space_memory);

memory_listener_register(&kvm_io_listener, &address_space_io);

s->many_ioeventfds = kvm_check_many_ioeventfds();

cpu_interrupt_handler = kvm_handle_interrupt;

return 0;

......

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: