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;
30 int v3_deinit_cpuid_map(struct v3_vm_info * vm) {
31 struct rb_node * node = v3_rb_first(&(vm->cpuid_map.map));
32 struct v3_cpuid_hook * hook = NULL;
33 struct rb_node * tmp_node = NULL;
37 hook = rb_entry(node, struct v3_cpuid_hook, tree_node);
39 node = v3_rb_next(node);
41 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
50 static inline struct v3_cpuid_hook * __insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
51 struct rb_node ** p = &(vm->cpuid_map.map.rb_node);
52 struct rb_node * parent = NULL;
53 struct v3_cpuid_hook * tmp_hook = NULL;
57 tmp_hook = rb_entry(parent, struct v3_cpuid_hook, tree_node);
59 if (hook->cpuid < tmp_hook->cpuid) {
61 } else if (hook->cpuid > tmp_hook->cpuid) {
67 rb_link_node(&(hook->tree_node), parent, p);
73 static inline struct v3_cpuid_hook * insert_cpuid_hook(struct v3_vm_info * vm, struct v3_cpuid_hook * hook) {
74 struct v3_cpuid_hook * ret;
76 if ((ret = __insert_cpuid_hook(vm, hook))) {
80 v3_rb_insert_color(&(hook->tree_node), &(vm->cpuid_map.map));
87 static struct v3_cpuid_hook * get_cpuid_hook(struct v3_vm_info * vm, uint32_t cpuid) {
88 struct rb_node * n = vm->cpuid_map.map.rb_node;
89 struct v3_cpuid_hook * hook = NULL;
92 hook = rb_entry(n, struct v3_cpuid_hook, tree_node);
94 if (cpuid < hook->cpuid) {
96 } else if (cpuid > hook->cpuid) {
107 int v3_unhook_cpuid(struct v3_vm_info * vm, uint32_t cpuid) {
108 struct v3_cpuid_hook * hook = get_cpuid_hook(vm, cpuid);
111 PrintError("Could not find cpuid to unhook (0x%x)\n", cpuid);
115 v3_rb_erase(&(hook->tree_node), &(vm->cpuid_map.map));
122 int v3_hook_cpuid(struct v3_vm_info * vm, uint32_t cpuid,
123 int (*hook_fn)(struct guest_info * info, uint32_t cpuid, \
124 uint32_t * eax, uint32_t * ebx, \
125 uint32_t * ecx, uint32_t * edx, \
126 void * private_data),
127 void * private_data) {
128 struct v3_cpuid_hook * hook = NULL;
130 if (hook_fn == NULL) {
131 PrintError("CPUID hook requested with null handler\n");
135 hook = (struct v3_cpuid_hook *)V3_Malloc(sizeof(struct v3_cpuid_hook));
137 hook->private_data = private_data;
138 hook->hook_fn = hook_fn;
140 if (insert_cpuid_hook(vm, hook)) {
141 PrintError("Could not hook cpuid 0x%x (already hooked)\n", cpuid);
149 int v3_handle_cpuid(struct guest_info * info) {
150 uint32_t cpuid = info->vm_regs.rax;
151 struct v3_cpuid_hook * hook = get_cpuid_hook(info->vm_info, cpuid);
153 //PrintDebug("CPUID called for 0x%x\n", cpuid);
156 //PrintDebug("Calling passthrough handler\n");
157 // call the passthrough handler
159 (uint32_t *)&(info->vm_regs.rax),
160 (uint32_t *)&(info->vm_regs.rbx),
161 (uint32_t *)&(info->vm_regs.rcx),
162 (uint32_t *)&(info->vm_regs.rdx));
164 // PrintDebug("Calling hook function\n");
166 if (hook->hook_fn(info, cpuid,
167 (uint32_t *)&(info->vm_regs.rax),
168 (uint32_t *)&(info->vm_regs.rbx),
169 (uint32_t *)&(info->vm_regs.rcx),
170 (uint32_t *)&(info->vm_regs.rdx),
171 hook->private_data) == -1) {
172 PrintError("Error in cpuid handler for 0x%x\n", cpuid);
177 // PrintDebug("Cleaning up register contents\n");
179 info->vm_regs.rax &= 0x00000000ffffffffLL;
180 info->vm_regs.rbx &= 0x00000000ffffffffLL;
181 info->vm_regs.rcx &= 0x00000000ffffffffLL;
182 info->vm_regs.rdx &= 0x00000000ffffffffLL;