| 1 |
/*
|
| 2 |
* Copyright (c) 2007, 2008 University of Tsukuba
|
| 3 |
* All rights reserved.
|
| 4 |
*
|
| 5 |
* Redistribution and use in source and binary forms, with or without
|
| 6 |
* modification, are permitted provided that the following conditions are met:
|
| 7 |
*
|
| 8 |
* 1. Redistributions of source code must retain the above copyright notice,
|
| 9 |
* this list of conditions and the following disclaimer.
|
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 11 |
* this list of conditions and the following disclaimer in the documentation
|
| 12 |
* and/or other materials provided with the distribution.
|
| 13 |
* 3. Neither the name of the University of Tsukuba nor the names of its
|
| 14 |
* contributors may be used to endorse or promote products derived from
|
| 15 |
* this software without specific prior written permission.
|
| 16 |
*
|
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 18 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
| 21 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 22 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 23 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 24 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 25 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 26 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 27 |
* POSSIBILITY OF SUCH DAMAGE.
|
| 28 |
*/
|
| 29 |
/*
|
| 30 |
* Copyright (c) 2010-2012 Yuichi Watanabe
|
| 31 |
*/
|
| 32 |
|
| 33 |
#include <core/initfunc.h>
|
| 34 |
#include <core/printf.h>
|
| 35 |
#include "apic_pass.h"
|
| 36 |
#include "asm.h"
|
| 37 |
#include "current.h"
|
| 38 |
#include "int.h"
|
| 39 |
#include "mm.h"
|
| 40 |
#include "panic.h"
|
| 41 |
|
| 42 |
|
| 43 |
#define PAT_ENCODING_UC 0x00
|
| 44 |
#define PAT_ENCODING_WC 0x01
|
| 45 |
#define PAT_ENCODING_WT 0x04
|
| 46 |
#define PAT_ENCODING_WP 0x05
|
| 47 |
#define PAT_ENCODING_WB 0x06
|
| 48 |
#define PAT_ENCODING_WUC 0x07
|
| 49 |
#define MSR_IA32_PAT_GUEST_INIT_VAL 0x0007040600070406LL
|
| 50 |
/* UC WUC WT WB UC WUC WT WB */
|
| 51 |
|
| 52 |
static u64
|
| 53 |
read_pat()
|
| 54 |
{
|
| 55 |
return current->msr_data.pat_reg;
|
| 56 |
}
|
| 57 |
|
| 58 |
static void
|
| 59 |
write_pat(u64 msrdata)
|
| 60 |
{
|
| 61 |
int i;
|
| 62 |
u8 pat_encoding;
|
| 63 |
u32 flag;
|
| 64 |
|
| 65 |
current->msr_data.pat_reg = msrdata;
|
| 66 |
for (i = 0; i < MSR_DATA_PAT_COUNT; i++) {
|
| 67 |
pat_encoding = (msrdata >> (8 * i)) & 0x7;
|
| 68 |
switch (pat_encoding) {
|
| 69 |
case 0:
|
| 70 |
flag = MAPMEM_UC;
|
| 71 |
break;
|
| 72 |
case 1:
|
| 73 |
flag = MAPMEM_WC;
|
| 74 |
break;
|
| 75 |
case 4:
|
| 76 |
flag = MAPMEM_WT;
|
| 77 |
break;
|
| 78 |
case 5:
|
| 79 |
flag = MAPMEM_WP;
|
| 80 |
break;
|
| 81 |
case 6:
|
| 82 |
flag = MAPMEM_WB;
|
| 83 |
break;
|
| 84 |
case 7:
|
| 85 |
flag = MAPMEM_WUC;
|
| 86 |
break;
|
| 87 |
default:
|
| 88 |
panic ("Unsupported pat encoding 0x%x", pat_encoding);
|
| 89 |
}
|
| 90 |
current->msr_data.pat_to_cache_flag[i] = flag;
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
bool
|
| 95 |
msr_read(u32 msrindex, u64 *msrdata)
|
| 96 |
{
|
| 97 |
switch (msrindex) {
|
| 98 |
case MSR_IA32_TIME_STAMP_COUNTER:
|
| 99 |
asm_rdmsr64(MSR_IA32_TIME_STAMP_COUNTER, msrdata);
|
| 100 |
*msrdata += current->tsc_offset;
|
| 101 |
break;
|
| 102 |
case MSR_IA32_PAT:
|
| 103 |
read_pat(msrdata);
|
| 104 |
break;
|
| 105 |
default:
|
| 106 |
asm_rdmsr64(msrindex, msrdata);
|
| 107 |
}
|
| 108 |
return false;
|
| 109 |
}
|
| 110 |
|
| 111 |
u32
|
| 112 |
msr_pte_to_cache_flag(u64 pte)
|
| 113 |
{
|
| 114 |
int pat_index;
|
| 115 |
|
| 116 |
pat_index =
|
| 117 |
((pte & PTE_PWT_BIT) ? 1 : 0) |
|
| 118 |
((pte & PTE_PCD_BIT) ? 2 : 0) |
|
| 119 |
((pte & PTE_PAT_BIT) ? 4 : 0);
|
| 120 |
return current->msr_data.pat_to_cache_flag[pat_index];
|
| 121 |
}
|
| 122 |
|
| 123 |
bool
|
| 124 |
msr_write(u32 msrindex, u64 msrdata)
|
| 125 |
{
|
| 126 |
u64 tmp;
|
| 127 |
|
| 128 |
/* FIXME: Exception handling */
|
| 129 |
switch (msrindex) {
|
| 130 |
case MSR_IA32_BIOS_UPDT_TRIG:
|
| 131 |
printf ("msr_pass: microcode updates cannot be loaded.\n");
|
| 132 |
break;
|
| 133 |
case MSR_IA32_TIME_STAMP_COUNTER:
|
| 134 |
asm_rdmsr64(MSR_IA32_TIME_STAMP_COUNTER, &tmp);
|
| 135 |
current->tsc_offset = msrdata - tmp;
|
| 136 |
current->vmctl.tsc_offset_changed ();
|
| 137 |
break;
|
| 138 |
case MSR_IA32_APIC_BASE:
|
| 139 |
if (msrdata & MSR_IA32_APIC_BASE_APIC_GLOBAL_ENABLE_BIT) {
|
| 140 |
tmp = msrdata & MSR_IA32_APIC_BASE_APIC_BASE_MASK;
|
| 141 |
apic_base_changed(tmp);
|
| 142 |
if (phys_in_vmm(tmp))
|
| 143 |
panic ("relocating APIC Base to VMM address!");
|
| 144 |
}
|
| 145 |
goto pass;
|
| 146 |
case MSR_IA32_PAT:
|
| 147 |
write_pat(msrdata);
|
| 148 |
break;
|
| 149 |
default:
|
| 150 |
pass:
|
| 151 |
asm_wrmsr64(msrindex, msrdata);
|
| 152 |
}
|
| 153 |
return false;
|
| 154 |
}
|
| 155 |
|
| 156 |
static void
|
| 157 |
msr_init(void)
|
| 158 |
{
|
| 159 |
write_pat(MSR_IA32_PAT_GUEST_INIT_VAL);
|
| 160 |
}
|
| 161 |
|
| 162 |
INITFUNC("passcpu0", msr_init);
|