2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_cpuid.h>
22 #include <palacios/vmm_lowlevel.h>
23 #include <palacios/vm_guest.h>
26 void v3_init_cpuid_map(struct v3_vm_info * vm) {
27 vm->cpuid_map.map.rb_node = NULL;
31 static inline struct v3_cpuid_hook * __insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
32 struct rb_node ** p = &(vm->cpuid_map.map.rb_node);
33 struct rb_node * parent = NULL;
34 struct v3_cpuid_hook * tmp_hook = NULL;
38 tmp_hook = rb_entry(parent, struct v3_cpuid_hook, tree_node);
40 if (hook->cpuid < tmp_hook->cpuid) {
42 } else if (hook->cpuid > tmp_hook->cpuid) {
48 rb_link_node(&(hook->tree_node), parent, p);
54 static inline struct v3_cpuid_hook * insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
55 struct v3_cpuid_hook * ret;
57 if ((ret = __insert_cpuid_hook(vm, hook))) {
61 v3_rb_insert_color(&(hook->tree_node), &(vm->cpuid_map.map));
68 static struct v3_cpuid_hook * get_cpuid_hook(struct v3_vm_info * vm, uint32_t cpuid) {
69 struct rb_node * n = vm->cpuid_map.map.rb_node;
70 struct v3_cpuid_hook * hook = NULL;
73 hook = rb_entry(n, struct v3_cpuid_hook, tree_node);
75 if (cpuid < hook->cpuid) {
77 } else if (cpuid > hook->cpuid) {
88 int v3_unhook_cpuid(struct v3_vm_info * vm, uint32_t cpuid) {
89 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
92 PrintError("Could not find cpuid to unhook (0x%x)\n", cpuid);
96 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
103 int v3_hook_cpuid(struct v3_vm_info * vm, uint32_t cpuid,
104 int (*hook_fn)(struct guest_info * info, uint32_t cpuid, \
105 uint32_t * eax, uint32_t * ebx, \
106 uint32_t * ecx, uint32_t * edx, \
107 void * private_data),
108 void * private_data) {
109 struct v3_cpuid_hook * hook = NULL;
111 if (hook_fn == NULL) {
112 PrintError("CPUID hook requested with null handler\n");
116 hook = (struct v3_cpuid_hook *)V3_Malloc(sizeof(struct v3_cpuid_hook));
118 hook->private_data = private_data;
119 hook->hook_fn = hook_fn;
121 if (insert_cpuid_hook(vm, hook)) {
122 PrintError("Could not hook cpuid 0x%x (already hooked)\n", cpuid);
130 int v3_handle_cpuid(struct guest_info * info) {
131 uint32_t cpuid = info->vm_regs.rax;
132 struct v3_cpuid_hook * hook = get_cpuid_hook(info->vm_info, cpuid);
134 //PrintDebug("CPUID called for 0x%x\n", cpuid);
137 //PrintDebug("Calling passthrough handler\n");
138 // call the passthrough handler
140 (uint32_t *)&(info->vm_regs.rax),
141 (uint32_t *)&(info->vm_regs.rbx),
142 (uint32_t *)&(info->vm_regs.rcx),
143 (uint32_t *)&(info->vm_regs.rdx));
145 // PrintDebug("Calling hook function\n");
147 if (hook->hook_fn(info, cpuid,
148 (uint32_t *)&(info->vm_regs.rax),
149 (uint32_t *)&(info->vm_regs.rbx),
150 (uint32_t *)&(info->vm_regs.rcx),
151 (uint32_t *)&(info->vm_regs.rdx),
152 hook->private_data) == -1) {
153 PrintError("Error in cpuid handler for 0x%x\n", cpuid);
158 // PrintDebug("Cleaning up register contents\n");
160 info->vm_regs.rax &= 0x00000000ffffffffLL;
161 info->vm_regs.rbx &= 0x00000000ffffffffLL;
162 info->vm_regs.rcx &= 0x00000000ffffffffLL;
163 info->vm_regs.rdx &= 0x00000000ffffffffLL;