⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtrr.c

📁 xen虚拟机源代码安装包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * mtrr.c: MTRR/PAT virtualization * * Copyright (c) 2007, Intel Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, * version 2, as published by the Free Software Foundation. * * This program is distributed in the hope it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 Temple * Place - Suite 330, Boston, MA 02111-1307 USA. */#include <public/hvm/e820.h>#include <xen/types.h>#include <asm/e820.h>#include <asm/paging.h>#include <asm/p2m.h>#include <xen/domain_page.h>#include <asm/mtrr.h>#include <asm/hvm/support.h>#include <asm/hvm/cacheattr.h>extern struct mtrr_state mtrr_state;static uint64_t phys_base_msr_mask;static uint64_t phys_mask_msr_mask;static uint32_t size_or_mask;static uint32_t size_and_mask;/* Get page attribute fields (PAn) from PAT MSR. */#define pat_cr_2_paf(pat_cr,n)  ((((uint64_t)pat_cr) >> ((n)<<3)) & 0xff)/* PAT entry to PTE flags (PAT, PCD, PWT bits). */static uint8_t pat_entry_2_pte_flags[8] = {    0,           _PAGE_PWT,    _PAGE_PCD,   _PAGE_PCD | _PAGE_PWT,    _PAGE_PAT,   _PAGE_PAT | _PAGE_PWT,    _PAGE_PAT | _PAGE_PCD, _PAGE_PAT | _PAGE_PCD | _PAGE_PWT };/* Effective mm type lookup table, according to MTRR and PAT. */static uint8_t mm_type_tbl[MTRR_NUM_TYPES][PAT_TYPE_NUMS] = {/********PAT(UC,WC,RS,RS,WT,WP,WB,UC-)*//* RS means reserved type(2,3), and type is hardcoded here */ /*MTRR(UC):(UC,WC,RS,RS,UC,UC,UC,UC)*/            {0, 1, 2, 2, 0, 0, 0, 0}, /*MTRR(WC):(UC,WC,RS,RS,UC,UC,WC,WC)*/            {0, 1, 2, 2, 0, 0, 1, 1}, /*MTRR(RS):(RS,RS,RS,RS,RS,RS,RS,RS)*/            {2, 2, 2, 2, 2, 2, 2, 2}, /*MTRR(RS):(RS,RS,RS,RS,RS,RS,RS,RS)*/            {2, 2, 2, 2, 2, 2, 2, 2}, /*MTRR(WT):(UC,WC,RS,RS,WT,WP,WT,UC)*/            {0, 1, 2, 2, 4, 5, 4, 0}, /*MTRR(WP):(UC,WC,RS,RS,WT,WP,WP,WC)*/            {0, 1, 2, 2, 4, 5, 5, 1}, /*MTRR(WB):(UC,WC,RS,RS,WT,WP,WB,UC)*/            {0, 1, 2, 2, 4, 5, 6, 0}};/* * Reverse lookup table, to find a pat type according to MTRR and effective * memory type. This table is dynamically generated. */static uint8_t mtrr_epat_tbl[MTRR_NUM_TYPES][MEMORY_NUM_TYPES];/* Lookup table for PAT entry of a given PAT value in host PAT. */static uint8_t pat_entry_tbl[PAT_TYPE_NUMS];static void get_mtrr_range(uint64_t base_msr, uint64_t mask_msr,                           uint64_t *base, uint64_t *end){    uint32_t mask_lo = (uint32_t)mask_msr;    uint32_t mask_hi = (uint32_t)(mask_msr >> 32);    uint32_t base_lo = (uint32_t)base_msr;    uint32_t base_hi = (uint32_t)(base_msr >> 32);    uint32_t size;    if ( (mask_lo & 0x800) == 0 )    {        /* Invalid (i.e. free) range */        *base = 0;        *end = 0;        return;    }    /* Work out the shifted address mask. */    mask_lo = (size_or_mask | (mask_hi << (32 - PAGE_SHIFT)) |               (mask_lo >> PAGE_SHIFT));    /* This works correctly if size is a power of two (a contiguous range). */    size = -mask_lo;    *base = base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT;    *end = *base + size - 1;}bool_t is_var_mtrr_overlapped(struct mtrr_state *m){    int32_t seg, i;    uint64_t phys_base, phys_mask, phys_base_pre, phys_mask_pre;    uint64_t base_pre, end_pre, base, end;    uint8_t num_var_ranges = (uint8_t)m->mtrr_cap;    for ( i = 0; i < num_var_ranges; i++ )    {        phys_base_pre = ((uint64_t*)m->var_ranges)[i*2];        phys_mask_pre = ((uint64_t*)m->var_ranges)[i*2 + 1];        get_mtrr_range(phys_base_pre, phys_mask_pre,                        &base_pre, &end_pre);        for ( seg = i + 1; seg < num_var_ranges; seg ++ )        {            phys_base = ((uint64_t*)m->var_ranges)[seg*2];            phys_mask = ((uint64_t*)m->var_ranges)[seg*2 + 1];            get_mtrr_range(phys_base, phys_mask,                            &base, &end);            if ( ((base_pre != end_pre) && (base != end))                 || ((base >= base_pre) && (base <= end_pre))                 || ((end >= base_pre) && (end <= end_pre))                 || ((base_pre >= base) && (base_pre <= end))                 || ((end_pre >= base) && (end_pre <= end)) )            {                /* MTRR is overlapped. */                return 1;            }        }    }    return 0;}#define MTRR_PHYSMASK_VALID_BIT  11#define MTRR_PHYSMASK_SHIFT      12#define MTRR_PHYSBASE_TYPE_MASK  0xff   /* lowest 8 bits */#define MTRR_PHYSBASE_SHIFT      12#define MTRR_VCNT                8#define MTRRphysBase_MSR(reg) (0x200 + 2 * (reg))#define MTRRphysMask_MSR(reg) (0x200 + 2 * (reg) + 1)bool_t mtrr_var_range_msr_set(struct mtrr_state *m, uint32_t msr,                              uint64_t msr_content);bool_t mtrr_fix_range_msr_set(struct mtrr_state *m, uint32_t row,                              uint64_t msr_content);static int hvm_mtrr_pat_init(void){    extern uint64_t host_pat;    unsigned int i, j, phys_addr;    memset(&mtrr_epat_tbl, INVALID_MEM_TYPE, sizeof(mtrr_epat_tbl));    for ( i = 0; i < MTRR_NUM_TYPES; i++ )    {        for ( j = 0; j < PAT_TYPE_NUMS; j++ )        {            int32_t tmp = mm_type_tbl[i][j];            if ( (tmp >= 0) && (tmp < MEMORY_NUM_TYPES) )                mtrr_epat_tbl[i][tmp] = j;        }    }    memset(&pat_entry_tbl, INVALID_MEM_TYPE,           PAT_TYPE_NUMS * sizeof(pat_entry_tbl[0]));    for ( i = 0; i < PAT_TYPE_NUMS; i++ )    {        for ( j = 0; j < PAT_TYPE_NUMS; j++ )        {            if ( pat_cr_2_paf(host_pat, j) == i )            {                pat_entry_tbl[i] = j;                break;            }        }    }    phys_addr = 36;    if ( cpuid_eax(0x80000000) >= 0x80000008 )        phys_addr = (uint8_t)cpuid_eax(0x80000008);    phys_base_msr_mask = ~((((uint64_t)1) << phys_addr) - 1) | 0xf00UL;    phys_mask_msr_mask = ~((((uint64_t)1) << phys_addr) - 1) | 0x7ffUL;    size_or_mask = ~((1 << (phys_addr - PAGE_SHIFT)) - 1);    size_and_mask = ~size_or_mask & 0xfff00000;    return 0;}__initcall(hvm_mtrr_pat_init);uint8_t pat_type_2_pte_flags(uint8_t pat_type){    int32_t pat_entry = pat_entry_tbl[pat_type];    /* INVALID_MEM_TYPE, means doesn't find the pat_entry in host pat for     * a given pat_type. If host pat covers all the pat types,     * it can't happen.     */    if ( likely(pat_entry != INVALID_MEM_TYPE) )        return pat_entry_2_pte_flags[pat_entry];    return pat_entry_2_pte_flags[pat_entry_tbl[PAT_TYPE_UNCACHABLE]];}int hvm_vcpu_cacheattr_init(struct vcpu *v){    struct mtrr_state *m = &v->arch.hvm_vcpu.mtrr;    memset(m, 0, sizeof(*m));    m->var_ranges = xmalloc_array(struct mtrr_var_range, MTRR_VCNT);    if ( m->var_ranges == NULL )        return -ENOMEM;    memset(m->var_ranges, 0, MTRR_VCNT * sizeof(struct mtrr_var_range));    m->mtrr_cap = (1u << 10) | (1u << 8) | MTRR_VCNT;    v->arch.hvm_vcpu.pat_cr =        ((uint64_t)PAT_TYPE_WRBACK) |               /* PAT0: WB */        ((uint64_t)PAT_TYPE_WRTHROUGH << 8) |       /* PAT1: WT */        ((uint64_t)PAT_TYPE_UC_MINUS << 16) |       /* PAT2: UC- */        ((uint64_t)PAT_TYPE_UNCACHABLE << 24) |     /* PAT3: UC */        ((uint64_t)PAT_TYPE_WRBACK << 32) |         /* PAT4: WB */        ((uint64_t)PAT_TYPE_WRTHROUGH << 40) |      /* PAT5: WT */        ((uint64_t)PAT_TYPE_UC_MINUS << 48) |       /* PAT6: UC- */        ((uint64_t)PAT_TYPE_UNCACHABLE << 56);      /* PAT7: UC */    return 0;}void hvm_vcpu_cacheattr_destroy(struct vcpu *v){    xfree(v->arch.hvm_vcpu.mtrr.var_ranges);}/* * Get MTRR memory type for physical address pa. */static uint8_t get_mtrr_type(struct mtrr_state *m, paddr_t pa){   int32_t     addr, seg, index;   uint8_t     overlap_mtrr = 0;   uint8_t     overlap_mtrr_pos = 0;   uint64_t    phys_base;   uint64_t    phys_mask;   uint8_t     num_var_ranges = m->mtrr_cap & 0xff;   if ( unlikely(!(m->enabled & 0x2)) )       return MTRR_TYPE_UNCACHABLE;   if ( (pa < 0x100000) && (m->enabled & 1) )   {       /* Fixed range MTRR takes effective */       addr = (uint32_t) pa;       if ( addr < 0x80000 )       {           seg = (addr >> 16);           return m->fixed_ranges[seg];       }       else if ( addr < 0xc0000 )       {           seg = (addr - 0x80000) >> 14;           index = (seg >> 3) + 1;           seg &= 7;            /* select 0-7 segments */           return m->fixed_ranges[index*8 + seg];       }       else       {           /* 0xC0000 --- 0x100000 */           seg = (addr - 0xc0000) >> 12;           index = (seg >> 3) + 3;           seg &= 7;            /* select 0-7 segments */           return m->fixed_ranges[index*8 + seg];       }   }   /* Match with variable MTRRs. */   for ( seg = 0; seg < num_var_ranges; seg++ )   {       phys_base = ((uint64_t*)m->var_ranges)[seg*2];       phys_mask = ((uint64_t*)m->var_ranges)[seg*2 + 1];       if ( phys_mask & (1 << MTRR_PHYSMASK_VALID_BIT) )       {           if ( ((uint64_t) pa & phys_mask) >> MTRR_PHYSMASK_SHIFT ==                (phys_base & phys_mask) >> MTRR_PHYSMASK_SHIFT )           {               if ( unlikely(m->overlapped) )               {                    overlap_mtrr |= 1 << (phys_base & MTRR_PHYSBASE_TYPE_MASK);                    overlap_mtrr_pos = phys_base & MTRR_PHYSBASE_TYPE_MASK;               }               else               {                   /* If no overlap, return the found one */                   return (phys_base & MTRR_PHYSBASE_TYPE_MASK);               }           }       }   }   /* Overlapped or not found. */   if ( unlikely(overlap_mtrr == 0) )       return m->def_type;   if ( likely(!(overlap_mtrr & ~( ((uint8_t)1) << overlap_mtrr_pos ))) )       /* Covers both one variable memory range matches and        * two or more identical match.        */       return overlap_mtrr_pos;   if ( overlap_mtrr & 0x1 )       /* Two or more match, one is UC. */       return MTRR_TYPE_UNCACHABLE;   if ( !(overlap_mtrr & 0xaf) )       /* Two or more match, WT and WB. */       return MTRR_TYPE_WRTHROUGH;   /* Behaviour is undefined, but return the last overlapped type. */   return overlap_mtrr_pos;}/* * return the memory type from PAT. * NOTE: valid only when paging is enabled. *       Only 4K page PTE is supported now. */static uint8_t page_pat_type(uint64_t pat_cr, uint32_t pte_flags){    int32_t pat_entry;    /* PCD/PWT -> bit 1/0 of PAT entry */    pat_entry = ( pte_flags >> 3 ) & 0x3;    /* PAT bits as bit 2 of PAT entry */    if ( pte_flags & _PAGE_PAT )        pat_entry |= 4;    return (uint8_t)pat_cr_2_paf(pat_cr, pat_entry);}/* * Effective memory type for leaf page. */static uint8_t effective_mm_type(struct mtrr_state *m,                                 uint64_t pat,                                 paddr_t gpa,                                 uint32_t pte_flags){    uint8_t mtrr_mtype, pat_value, effective;    mtrr_mtype = get_mtrr_type(m, gpa);    pat_value = page_pat_type(pat, pte_flags);    effective = mm_type_tbl[mtrr_mtype][pat_value];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -