Skip to content

Commit ff53604

Browse files
Xiao Guangrongbonzini
authored andcommitted
KVM: x86: move MTRR related code to a separate file
MTRR code locates in x86.c and mmu.c so that move them to a separate file to make the organization more clearer and it will be the place where we fully implement vMTRR Signed-off-by: Xiao Guangrong <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent b18d543 commit ff53604

File tree

7 files changed

+342
-318
lines changed

7 files changed

+342
-318
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,6 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3);
894894

895895
int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
896896
const void *val, int bytes);
897-
u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
898897

899898
struct kvm_irq_mask_notifier {
900899
void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);

arch/x86/kvm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \
1212
kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
1313

1414
kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
15-
i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
15+
i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o
1616
kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += assigned-dev.o iommu.o
1717
kvm-intel-y += vmx.o
1818
kvm-amd-y += svm.o

arch/x86/kvm/mmu.c

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,109 +2437,6 @@ int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
24372437
}
24382438
EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
24392439

2440-
/*
2441-
* The function is based on mtrr_type_lookup() in
2442-
* arch/x86/kernel/cpu/mtrr/generic.c
2443-
*/
2444-
static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
2445-
u64 start, u64 end)
2446-
{
2447-
u64 base, mask;
2448-
u8 prev_match, curr_match;
2449-
int i, num_var_ranges = KVM_NR_VAR_MTRR;
2450-
2451-
/* MTRR is completely disabled, use UC for all of physical memory. */
2452-
if (!(mtrr_state->enabled & 0x2))
2453-
return MTRR_TYPE_UNCACHABLE;
2454-
2455-
/* Make end inclusive end, instead of exclusive */
2456-
end--;
2457-
2458-
/* Look in fixed ranges. Just return the type as per start */
2459-
if (mtrr_state->have_fixed && (mtrr_state->enabled & 0x1) &&
2460-
(start < 0x100000)) {
2461-
int idx;
2462-
2463-
if (start < 0x80000) {
2464-
idx = 0;
2465-
idx += (start >> 16);
2466-
return mtrr_state->fixed_ranges[idx];
2467-
} else if (start < 0xC0000) {
2468-
idx = 1 * 8;
2469-
idx += ((start - 0x80000) >> 14);
2470-
return mtrr_state->fixed_ranges[idx];
2471-
} else if (start < 0x1000000) {
2472-
idx = 3 * 8;
2473-
idx += ((start - 0xC0000) >> 12);
2474-
return mtrr_state->fixed_ranges[idx];
2475-
}
2476-
}
2477-
2478-
/*
2479-
* Look in variable ranges
2480-
* Look of multiple ranges matching this address and pick type
2481-
* as per MTRR precedence
2482-
*/
2483-
prev_match = 0xFF;
2484-
for (i = 0; i < num_var_ranges; ++i) {
2485-
unsigned short start_state, end_state;
2486-
2487-
if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
2488-
continue;
2489-
2490-
base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
2491-
(mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
2492-
mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
2493-
(mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
2494-
2495-
start_state = ((start & mask) == (base & mask));
2496-
end_state = ((end & mask) == (base & mask));
2497-
if (start_state != end_state)
2498-
return 0xFE;
2499-
2500-
if ((start & mask) != (base & mask))
2501-
continue;
2502-
2503-
curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
2504-
if (prev_match == 0xFF) {
2505-
prev_match = curr_match;
2506-
continue;
2507-
}
2508-
2509-
if (prev_match == MTRR_TYPE_UNCACHABLE ||
2510-
curr_match == MTRR_TYPE_UNCACHABLE)
2511-
return MTRR_TYPE_UNCACHABLE;
2512-
2513-
if ((prev_match == MTRR_TYPE_WRBACK &&
2514-
curr_match == MTRR_TYPE_WRTHROUGH) ||
2515-
(prev_match == MTRR_TYPE_WRTHROUGH &&
2516-
curr_match == MTRR_TYPE_WRBACK)) {
2517-
prev_match = MTRR_TYPE_WRTHROUGH;
2518-
curr_match = MTRR_TYPE_WRTHROUGH;
2519-
}
2520-
2521-
if (prev_match != curr_match)
2522-
return MTRR_TYPE_UNCACHABLE;
2523-
}
2524-
2525-
if (prev_match != 0xFF)
2526-
return prev_match;
2527-
2528-
return mtrr_state->def_type;
2529-
}
2530-
2531-
u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
2532-
{
2533-
u8 mtrr;
2534-
2535-
mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
2536-
(gfn << PAGE_SHIFT) + PAGE_SIZE);
2537-
if (mtrr == 0xfe || mtrr == 0xff)
2538-
mtrr = MTRR_TYPE_WRBACK;
2539-
return mtrr;
2540-
}
2541-
EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
2542-
25432440
static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
25442441
{
25452442
trace_kvm_mmu_unsync_page(sp);

0 commit comments

Comments
 (0)