Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


updated IO and MSRs to allow hooking/unhooking dynamic at runtime
[palacios.git] / palacios / src / palacios / svm_msr.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/svm_msr.h>
21 #include <palacios/vmm_msr.h>
22
23 #include <palacios/vmm_list.h>
24
25
26 #define PENTIUM_MSRS_START            0x00000000
27 #define PENTIUM_MSRS_END              0x00001fff
28 #define AMD_6_GEN_MSRS_START          0xc0000000
29 #define AMD_6_GEN_MSRS_END            0xc0001fff
30 #define AMD_7_8_GEN_MSRS_START        0xc0010000
31 #define AMD_7_8_GEN_MSRS_END          0xc0011fff
32
33 #define PENTIUM_MSRS_INDEX            (0x0 * 4)
34 #define AMD_6_GEN_MSRS_INDEX          (0x800 * 4)
35 #define AMD_7_8_GEN_MSRS_INDEX        (0x1000 * 4)
36
37
38
39 static int get_bitmap_index(uint_t msr) {
40     if ((msr >= PENTIUM_MSRS_START) && 
41         (msr <= PENTIUM_MSRS_END)) {
42         return (PENTIUM_MSRS_INDEX + (msr - PENTIUM_MSRS_START));
43     } else if ((msr >= AMD_6_GEN_MSRS_START) && 
44                (msr <= AMD_6_GEN_MSRS_END)) {
45         return (AMD_6_GEN_MSRS_INDEX + (msr - AMD_6_GEN_MSRS_START));
46     } else if ((msr >= AMD_7_8_GEN_MSRS_START) && 
47                (msr <= AMD_7_8_GEN_MSRS_END)) {
48         return (AMD_7_8_GEN_MSRS_INDEX + (msr - AMD_7_8_GEN_MSRS_START));
49     } else {
50         PrintError("MSR out of range (MSR=0x%x)\n", msr);
51         return -1;
52     }
53 }
54
55
56 static int update_map(struct guest_info * info, uint_t msr, int hook_reads, int hook_writes) {
57     int index = get_bitmap_index(msr);
58     uint_t major = index / 4;
59     uint_t minor = (index % 4) * 2;
60     uchar_t val = 0;
61     uchar_t mask = 0x3;
62     uint8_t * bitmap = (uint8_t *)(info->msr_map.arch_data);
63
64     if (hook_reads) {
65         val |= 0x1;
66     } 
67     
68     if (hook_writes) {
69         val |= 0x2;
70     }
71
72     *(bitmap + major) &= ~(mask << minor);
73     *(bitmap + major) |= (val << minor);
74     
75     return 0;
76 }
77
78
79 int v3_init_svm_msr_map(struct guest_info * info) {
80     struct v3_msr_map * msr_map = &(info->msr_map);
81   
82     msr_map->update_map = update_map;
83
84     msr_map->arch_data = V3_VAddr(V3_AllocPages(2));  
85     memset(msr_map->arch_data, 0, PAGE_SIZE_4KB * 2);
86
87     return 0;
88 }
89
90
91
92 int v3_handle_msr_write(struct guest_info * info) {
93     uint_t msr_num = info->vm_regs.rcx;
94     struct v3_msr msr_val;
95     struct v3_msr_hook * hook = NULL;
96
97     hook = v3_get_msr_hook(info, msr_num);
98
99     if (!hook) {
100         PrintError("Hook for MSR write %d not found\n", msr_num);
101         return -1;
102     }
103
104     msr_val.value = 0;
105     msr_val.lo = info->vm_regs.rax;
106     msr_val.hi = info->vm_regs.rdx;
107
108     if (hook->write(msr_num, msr_val, hook->priv_data) == -1) {
109         PrintError("Error in MSR hook Write\n");
110         return -1;
111     }
112
113     return 0;
114 }
115
116
117
118 int v3_handle_msr_read(struct guest_info * info) {
119     uint_t msr_num = info->vm_regs.rcx;
120     struct v3_msr msr_val;
121     struct v3_msr_hook * hook = NULL;
122
123     hook = v3_get_msr_hook(info, msr_num);
124
125     if (!hook) {
126         PrintError("Hook for MSR read %d not found\n", msr_num);
127         return -1;
128     }
129
130     msr_val.value = 0;
131   
132     if (hook->read(msr_num, &msr_val, hook->priv_data) == -1) {
133         PrintError("Error in MSR hook Read\n");
134         return -1;
135     }
136
137     info->vm_regs.rax = msr_val.lo;
138     info->vm_regs.rdx = msr_val.hi;
139   
140     return 0;
141 }