From: "Jan Beulich" Subject: [PATCH v2 4/5] x86/HVM: use manifest constants / enumerators for memory types Date: Wed, 05 Mar 2014 16:34:02 +0000 Message-ID: <5317600A020000780012142D@nat28.tlf.novell.com> References: <530C7CAB020000780011F121@nat28.tlf.novell.com> <530C7EE9020000780011F13C@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part97A5DEEA.1__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WLElj-0000cv-Cn for xen-devel@lists.xenproject.org; Wed, 05 Mar 2014 16:34:07 +0000 In-Reply-To: <530C7EE9020000780011F13C@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Yang Z Zhang , Keir Fraser , Dongxiao Xu , Eddie Dong , Jun Nakajima List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part97A5DEEA.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... instead of literal numbers, thus making it possible for the reader to understand the code without having to look up the meaning of these numbers. Signed-off-by: Jan Beulich --- v2: Spotted two more cases (in get_mtrr_type()). --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -235,9 +235,16 @@ int hvm_set_guest_pat(struct vcpu *v, u6 uint8_t *value =3D (uint8_t *)&guest_pat; =20 for ( i =3D 0; i < 8; i++ ) - if ( unlikely(!(value[i] =3D=3D 0 || value[i] =3D=3D 1 || - value[i] =3D=3D 4 || value[i] =3D=3D 5 || - value[i] =3D=3D 6 || value[i] =3D=3D 7)) ) { + switch ( value[i] ) + { + case PAT_TYPE_UC_MINUS: + case PAT_TYPE_UNCACHABLE: + case PAT_TYPE_WRBACK: + case PAT_TYPE_WRCOMB: + case PAT_TYPE_WRPROT: + case PAT_TYPE_WRTHROUGH: + break; + default: HVM_DBG_LOG(DBG_LEVEL_MSR, "invalid guest PAT: %"PRIx64"\n", guest_pat);=20 return 0; --- a/xen/arch/x86/hvm/mtrr.c +++ b/xen/arch/x86/hvm/mtrr.c @@ -42,22 +42,28 @@ static const uint8_t pat_entry_2_pte_fla =20 /* Effective mm type lookup table, according to MTRR and PAT. */ static const uint8_t mm_type_tbl[MTRR_NUM_TYPES][PAT_TYPE_NUMS] =3D { -/********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} +#define RS MEMORY_NUM_TYPES +#define UC MTRR_TYPE_UNCACHABLE +#define WB MTRR_TYPE_WRBACK +#define WC MTRR_TYPE_WRCOMB +#define WP MTRR_TYPE_WRPROT +#define WT MTRR_TYPE_WRTHROUGH + +/* PAT(UC, WC, RS, RS, WT, WP, WB, UC-) */ +/* MTRR(UC) */ {UC, WC, RS, RS, UC, UC, UC, UC}, +/* MTRR(WC) */ {UC, WC, RS, RS, UC, UC, WC, WC}, +/* MTRR(RS) */ {RS, RS, RS, RS, RS, RS, RS, RS}, +/* MTRR(RS) */ {RS, RS, RS, RS, RS, RS, RS, RS}, +/* MTRR(WT) */ {UC, WC, RS, RS, WT, WP, WT, UC}, +/* MTRR(WP) */ {UC, WC, RS, RS, WT, WP, WP, WC}, +/* MTRR(WB) */ {UC, WC, RS, RS, WT, WP, WB, UC} + +#undef UC +#undef WC +#undef WT +#undef WP +#undef WB +#undef RS }; =20 /* @@ -301,11 +307,12 @@ static uint8_t get_mtrr_type(struct mtrr */ return overlap_mtrr_pos; =20 - if ( overlap_mtrr & 0x1 ) + if ( overlap_mtrr & (1 << MTRR_TYPE_UNCACHABLE) ) /* Two or more match, one is UC. */ return MTRR_TYPE_UNCACHABLE; =20 - if ( !(overlap_mtrr & 0xaf) ) + if ( !(overlap_mtrr & + ~((1 << MTRR_TYPE_WRTHROUGH) | (1 << MTRR_TYPE_WRBACK))) ) /* Two or more match, WT and WB. */ return MTRR_TYPE_WRTHROUGH; =20 @@ -403,13 +410,26 @@ uint32_t get_pat_flags(struct vcpu *v, return pat_type_2_pte_flags(pat_entry_value); } =20 +static inline bool_t valid_mtrr_type(uint8_t type) +{ + switch ( type ) + { + case MTRR_TYPE_UNCACHABLE: + case MTRR_TYPE_WRBACK: + case MTRR_TYPE_WRCOMB: + case MTRR_TYPE_WRPROT: + case MTRR_TYPE_WRTHROUGH: + return 1; + } + return 0; +} + bool_t mtrr_def_type_msr_set(struct mtrr_state *m, uint64_t msr_content) { uint8_t def_type =3D msr_content & 0xff; uint8_t enabled =3D (msr_content >> 10) & 0x3; =20 - if ( unlikely(!(def_type =3D=3D 0 || def_type =3D=3D 1 || def_type = =3D=3D 4 || - def_type =3D=3D 5 || def_type =3D=3D 6)) ) + if ( unlikely(!valid_mtrr_type(def_type)) ) { HVM_DBG_LOG(DBG_LEVEL_MSR, "invalid MTRR def type:%x\n", = def_type); return 0; @@ -436,15 +456,11 @@ bool_t mtrr_fix_range_msr_set(struct mtr if ( fixed_range_base[row] !=3D msr_content ) { uint8_t *range =3D (uint8_t*)&msr_content; - int32_t i, type; + unsigned int i; =20 for ( i =3D 0; i < 8; i++ ) - { - type =3D range[i]; - if ( unlikely(!(type =3D=3D 0 || type =3D=3D 1 || - type =3D=3D 4 || type =3D=3D 5 || type =3D=3D = 6)) ) + if ( unlikely(!valid_mtrr_type(range[i])) ) return 0; - } =20 fixed_range_base[row] =3D msr_content; } @@ -455,7 +471,7 @@ bool_t mtrr_fix_range_msr_set(struct mtr bool_t mtrr_var_range_msr_set( struct domain *d, struct mtrr_state *m, uint32_t msr, uint64_t = msr_content) { - uint32_t index, type, phys_addr, eax, ebx, ecx, edx; + uint32_t index, phys_addr, eax, ebx, ecx, edx; uint64_t msr_mask; uint64_t *var_range_base =3D (uint64_t*)m->var_ranges; =20 @@ -463,9 +479,7 @@ bool_t mtrr_var_range_msr_set( if ( var_range_base[index] =3D=3D msr_content ) return 1; =20 - type =3D (uint8_t)msr_content; - if ( unlikely(!(type =3D=3D 0 || type =3D=3D 1 || - type =3D=3D 4 || type =3D=3D 5 || type =3D=3D 6)) ) + if ( unlikely(!valid_mtrr_type((uint8_t)msr_content)) ) return 0; =20 phys_addr =3D 36; --- a/xen/include/asm-x86/mtrr.h +++ b/xen/include/asm-x86/mtrr.h @@ -20,7 +20,6 @@ enum { PAT_TYPE_UNCACHABLE=3D0, PAT_TYPE_WRCOMB=3D1, - PAT_TYPE_RESERVED=3D2, PAT_TYPE_WRTHROUGH=3D4, PAT_TYPE_WRPROT=3D5, PAT_TYPE_WRBACK=3D6, --=__Part97A5DEEA.1__= Content-Type: text/plain; name="x86-PAT-types.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-PAT-types.patch" x86/HVM: use manifest constants / enumerators for memory types=0A=0A... = instead of literal numbers, thus making it possible for the reader=0Ato = understand the code without having to look up the meaning of these=0Anumber= s.=0A=0ASigned-off-by: Jan Beulich =0A---=0Av2: Spotted = two more cases (in get_mtrr_type()).=0A=0A--- a/xen/arch/x86/hvm/hvm.c=0A++= + b/xen/arch/x86/hvm/hvm.c=0A@@ -235,9 +235,16 @@ int hvm_set_guest_pat(str= uct vcpu *v, u6=0A uint8_t *value =3D (uint8_t *)&guest_pat;=0A =0A = for ( i =3D 0; i < 8; i++ )=0A- if ( unlikely(!(value[i] =3D=3D 0 = || value[i] =3D=3D 1 ||=0A- value[i] =3D=3D 4 || = value[i] =3D=3D 5 ||=0A- value[i] =3D=3D 6 || = value[i] =3D=3D 7)) ) {=0A+ switch ( value[i] )=0A+ {=0A+ = case PAT_TYPE_UC_MINUS:=0A+ case PAT_TYPE_UNCACHABLE:=0A+ = case PAT_TYPE_WRBACK:=0A+ case PAT_TYPE_WRCOMB:=0A+ case = PAT_TYPE_WRPROT:=0A+ case PAT_TYPE_WRTHROUGH:=0A+ = break;=0A+ default:=0A HVM_DBG_LOG(DBG_LEVEL_MSR, = "invalid guest PAT: %"PRIx64"\n",=0A guest_pat); = =0A return 0;=0A--- a/xen/arch/x86/hvm/mtrr.c=0A+++ b/xen/arch/= x86/hvm/mtrr.c=0A@@ -42,22 +42,28 @@ static const uint8_t pat_entry_2_pte_f= la=0A =0A /* Effective mm type lookup table, according to MTRR and PAT. = */=0A static const uint8_t mm_type_tbl[MTRR_NUM_TYPES][PAT_TYPE_NUMS] =3D = {=0A-/********PAT(UC,WC,RS,RS,WT,WP,WB,UC-)*/=0A-/* RS means reserved = type(2,3), and type is hardcoded here */=0A- /*MTRR(UC):(UC,WC,RS,RS,UC,UC,= UC,UC)*/=0A- {0, 1, 2, 2, 0, 0, 0, 0},=0A- /*MTRR(WC):(UC,WC,RS,= RS,UC,UC,WC,WC)*/=0A- {0, 1, 2, 2, 0, 0, 1, 1},=0A- /*MTRR(RS):(= RS,RS,RS,RS,RS,RS,RS,RS)*/=0A- {2, 2, 2, 2, 2, 2, 2, 2},=0A- = /*MTRR(RS):(RS,RS,RS,RS,RS,RS,RS,RS)*/=0A- {2, 2, 2, 2, 2, 2, = 2, 2},=0A- /*MTRR(WT):(UC,WC,RS,RS,WT,WP,WT,UC)*/=0A- {0, 1, 2, = 2, 4, 5, 4, 0},=0A- /*MTRR(WP):(UC,WC,RS,RS,WT,WP,WP,WC)*/=0A- = {0, 1, 2, 2, 4, 5, 5, 1},=0A- /*MTRR(WB):(UC,WC,RS,RS,WT,WP,WB,UC)*/=0A- = {0, 1, 2, 2, 4, 5, 6, 0}=0A+#define RS MEMORY_NUM_TYPES=0A+#define= UC MTRR_TYPE_UNCACHABLE=0A+#define WB MTRR_TYPE_WRBACK=0A+#define WC = MTRR_TYPE_WRCOMB=0A+#define WP MTRR_TYPE_WRPROT=0A+#define WT MTRR_TYPE_WRT= HROUGH=0A+=0A+/* PAT(UC, WC, RS, RS, WT, WP, WB, UC-) */=0A+/* = MTRR(UC) */ {UC, WC, RS, RS, UC, UC, UC, UC},=0A+/* MTRR(WC) */ {UC, WC, = RS, RS, UC, UC, WC, WC},=0A+/* MTRR(RS) */ {RS, RS, RS, RS, RS, RS, RS, = RS},=0A+/* MTRR(RS) */ {RS, RS, RS, RS, RS, RS, RS, RS},=0A+/* MTRR(WT) */ = {UC, WC, RS, RS, WT, WP, WT, UC},=0A+/* MTRR(WP) */ {UC, WC, RS, RS, WT, = WP, WP, WC},=0A+/* MTRR(WB) */ {UC, WC, RS, RS, WT, WP, WB, UC}=0A+=0A+#und= ef UC=0A+#undef WC=0A+#undef WT=0A+#undef WP=0A+#undef WB=0A+#undef RS=0A = };=0A =0A /*=0A@@ -301,11 +307,12 @@ static uint8_t get_mtrr_type(struct = mtrr=0A */=0A return overlap_mtrr_pos;=0A =0A- if ( = overlap_mtrr & 0x1 )=0A+ if ( overlap_mtrr & (1 << MTRR_TYPE_UNCACHABLE) = )=0A /* Two or more match, one is UC. */=0A return MTRR_TYPE_= UNCACHABLE;=0A =0A- if ( !(overlap_mtrr & 0xaf) )=0A+ if ( !(overlap_mt= rr &=0A+ ~((1 << MTRR_TYPE_WRTHROUGH) | (1 << MTRR_TYPE_WRBACK))) = )=0A /* Two or more match, WT and WB. */=0A return MTRR_TYPE_= WRTHROUGH;=0A =0A@@ -403,13 +410,26 @@ uint32_t get_pat_flags(struct vcpu = *v,=0A return pat_type_2_pte_flags(pat_entry_value);=0A }=0A =0A+static= inline bool_t valid_mtrr_type(uint8_t type)=0A+{=0A+ switch ( type = )=0A+ {=0A+ case MTRR_TYPE_UNCACHABLE:=0A+ case MTRR_TYPE_WRBACK:= =0A+ case MTRR_TYPE_WRCOMB:=0A+ case MTRR_TYPE_WRPROT:=0A+ case = MTRR_TYPE_WRTHROUGH:=0A+ return 1;=0A+ }=0A+ return 0;=0A+}=0A= +=0A bool_t mtrr_def_type_msr_set(struct mtrr_state *m, uint64_t msr_conten= t)=0A {=0A uint8_t def_type =3D msr_content & 0xff;=0A uint8_t = enabled =3D (msr_content >> 10) & 0x3;=0A =0A- if ( unlikely(!(def_type = =3D=3D 0 || def_type =3D=3D 1 || def_type =3D=3D 4 ||=0A- = def_type =3D=3D 5 || def_type =3D=3D 6)) )=0A+ if ( unlikely(!valid_mt= rr_type(def_type)) )=0A {=0A HVM_DBG_LOG(DBG_LEVEL_MSR, = "invalid MTRR def type:%x\n", def_type);=0A return 0;=0A@@ = -436,15 +456,11 @@ bool_t mtrr_fix_range_msr_set(struct mtr=0A if ( = fixed_range_base[row] !=3D msr_content )=0A {=0A uint8_t = *range =3D (uint8_t*)&msr_content;=0A- int32_t i, type;=0A+ = unsigned int i;=0A =0A for ( i =3D 0; i < 8; i++ )=0A- = {=0A- type =3D range[i];=0A- if ( unlikely(!(type = =3D=3D 0 || type =3D=3D 1 ||=0A- type =3D=3D 4 = || type =3D=3D 5 || type =3D=3D 6)) )=0A+ if ( unlikely(!valid_m= trr_type(range[i])) )=0A return 0;=0A- }=0A =0A = fixed_range_base[row] =3D msr_content;=0A }=0A@@ -455,7 +471,7 @@ = bool_t mtrr_fix_range_msr_set(struct mtr=0A bool_t mtrr_var_range_msr_set(= =0A struct domain *d, struct mtrr_state *m, uint32_t msr, uint64_t = msr_content)=0A {=0A- uint32_t index, type, phys_addr, eax, ebx, ecx, = edx;=0A+ uint32_t index, phys_addr, eax, ebx, ecx, edx;=0A uint64_t = msr_mask;=0A uint64_t *var_range_base =3D (uint64_t*)m->var_ranges;=0A = =0A@@ -463,9 +479,7 @@ bool_t mtrr_var_range_msr_set(=0A if ( = var_range_base[index] =3D=3D msr_content )=0A return 1;=0A =0A- = type =3D (uint8_t)msr_content;=0A- if ( unlikely(!(type =3D=3D 0 || = type =3D=3D 1 ||=0A- type =3D=3D 4 || type =3D=3D 5 || = type =3D=3D 6)) )=0A+ if ( unlikely(!valid_mtrr_type((uint8_t)msr_conten= t)) )=0A return 0;=0A =0A phys_addr =3D 36;=0A--- a/xen/include= /asm-x86/mtrr.h=0A+++ b/xen/include/asm-x86/mtrr.h=0A@@ -20,7 +20,6 @@=0A = enum {=0A PAT_TYPE_UNCACHABLE=3D0,=0A PAT_TYPE_WRCOMB=3D1,=0A- = PAT_TYPE_RESERVED=3D2,=0A PAT_TYPE_WRTHROUGH=3D4,=0A PAT_TYPE_WRPRO= T=3D5,=0A PAT_TYPE_WRBACK=3D6,=0A --=__Part97A5DEEA.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part97A5DEEA.1__=--