x86: Cleanup to segment handling
cover x86: Cleanup to segment handling
Andrew Cooper (2):
x86: Switch "struct desc_struct" to being seg_desc_t x86/pv: Code improvements to do_update_descriptor()
xen/arch/x86/cpu/common.c | 4 ++-- xen/arch/x86/domain.c | 2 +- xen/arch/x86/hvm/hvm.c | 4 ++-- xen/arch/x86/hvm/svm/svm.c | 4 ++-- xen/arch/x86/mm.c | 2 +- xen/arch/x86/pv/descriptor-tables.c | 41 +++++++++++++++++-------------------- xen/arch/x86/pv/emul-gate-op.c | 4 ++-- xen/arch/x86/pv/emulate.c | 2 +- xen/arch/x86/pv/emulate.h | 4 ++-- xen/arch/x86/smpboot.c | 2 +- xen/arch/x86/traps.c | 4 ++-- xen/arch/x86/x86_64/mm.c | 2 +- xen/include/asm-x86/desc.h | 17 ++++++++------- xen/include/asm-x86/ldt.h | 2 +- xen/include/asm-x86/mm.h | 2 +- 15 files changed, 48 insertions(+), 48 deletions(-)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 1/2 x86: Switch "struct desc_struct" to being seg_desc_t
The struct suffix is redundant in the name, and a future change will want to turn it into a union, rather than a structure. As this represents a segment descriptor, give it an appropriate typedef.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/cpu/common.c | 4 ++-- xen/arch/x86/domain.c | 2 +- xen/arch/x86/hvm/hvm.c | 4 ++-- xen/arch/x86/hvm/svm/svm.c | 4 ++-- xen/arch/x86/mm.c | 2 +- xen/arch/x86/pv/descriptor-tables.c | 6 +++--- xen/arch/x86/pv/emul-gate-op.c | 4 ++-- xen/arch/x86/pv/emulate.c | 2 +- xen/arch/x86/pv/emulate.h | 4 ++-- xen/arch/x86/smpboot.c | 2 +- xen/arch/x86/traps.c | 4 ++-- xen/arch/x86/x86_64/mm.c | 2 +- xen/include/asm-x86/desc.h | 14 +++++++------- xen/include/asm-x86/ldt.h | 2 +- xen/include/asm-x86/mm.h | 2 +- 15 files changed, 29 insertions(+), 29 deletions(-)
xen/arch/x86/cpu/common.c +2 -2
@@ -724,9 +724,9 @@ void load_system_tables(void) stack_top = stack_bottom & ~(STACK_SIZE -1 );struct tss_struct *tss = &this_cpu (init_tss); -struct desc_struct *gdt = +seg_desc_t *gdt =this_cpu (gdt_table) -FIRST_RESERVED_GDT_ENTRY ; -struct desc_struct *compat_gdt = +seg_desc_t *compat_gdt =this_cpu (compat_gdt_table) -FIRST_RESERVED_GDT_ENTRY ;const struct desc_ptr gdtr = {
xen/arch/x86/domain.c +1 -1
@@ -1665,7 +1665,7 @@ static void __context_switch(void)struct vcpu *p =per_cpu (curr_vcpu, cpu);struct vcpu *n = current;struct domain *pd = p->domain, *nd = n->domain; -struct desc_struct *gdt; +seg_desc_t *gdt;struct desc_ptr gdt_desc;ASSERT (p != n);
xen/arch/x86/hvm/hvm.c +2 -2
@@ -2695,7 +2695,7 @@ static int task_switch_load_seg( enum x86_segment seg, uint16_t sel, unsigned int cpl, unsigned int eflags) { struct segment_register desctab, segr; -struct desc_struct *pdesc =NULL , desc; +seg_desc_t *pdesc =NULL , desc;u8 dpl, rpl;bool_t writable;int fault_type = TRAP_invalid_tss;
@@ -2876,7 +2876,7 @@ void hvm_task_switch(struct vcpu *v = current;struct cpu_user_regs *regs =guest_cpu_user_regs ();struct segment_register gdt, tr, prev_tr, segr; -struct desc_struct *optss_desc =NULL , *nptss_desc =NULL , tss_desc; +seg_desc_t *optss_desc =NULL , *nptss_desc =NULL , tss_desc;bool_t otd_writable, ntd_writable;unsigned int eflags, new_cpl;pagefault_info_t pfinfo;
xen/arch/x86/hvm/svm/svm.c +2 -2
@@ -1636,8 +1636,8 @@ bool svm_load_segs(unsigned int ldt_ents, unsigned long ldt_base,else {/* Keep GDT in sync. */ -struct desc_struct *desc =this_cpu (gdt_table) +LDT_ENTRY - -FIRST_RESERVED_GDT_ENTRY ; +seg_desc_t *desc = +this_cpu (gdt_table) +LDT_ENTRY -FIRST_RESERVED_GDT_ENTRY ;_set_tssldt_desc (desc, ldt_base, ldt_ents *8 -1 , SYS_DESC_ldt);
xen/arch/x86/mm.c +1 -1
@@ -626,7 +626,7 @@ const char __section(".bss.page_aligned.const") __aligned(PAGE_SIZE)static int alloc_segdesc_page (struct page_info *page) {const struct domain *owner =page_get_owner (page); -struct desc_struct *descs =__map_domain_page (page); +seg_desc_t *descs =__map_domain_page (page);unsigned i;for ( i =0 ; i <512 ; i++ )
xen/arch/x86/pv/descriptor-tables.c +3 -3
@@ -212,16 +212,16 @@ long do_update_descriptor(uint64_t pa, uint64_t desc)unsigned long gmfn = pa >>PAGE_SHIFT ;unsigned long mfn;unsigned int offset; -struct desc_struct *gdt_pent, d; +seg_desc_t *gdt_pent, d;struct page_info *page;long ret = -EINVAL ; - offset = ((unsigned int )pa & ~PAGE_MASK ) /sizeof (struct desc_struct ); + offset = ((unsigned int )pa & ~PAGE_MASK ) /sizeof (seg_desc_t); *(uint64_t *)&d = desc; page =get_page_from_gfn (currd, gmfn,NULL ,P2M_ALLOC ); -if ( (((unsigned int )pa %sizeof (struct desc_struct )) !=0 ) || +if ( (((unsigned int )pa %sizeof (seg_desc_t)) !=0 ) || !page || !check_descriptor (currd, &d) ) {
xen/arch/x86/pv/emul-gate-op.c +2 -2
@@ -47,8 +47,8 @@ static int read_gate_descriptor(unsigned int gate_sel,unsigned long *off, unsigned int *ar) { - struct desc_struct desc; -const struct desc_struct *pdesc =gdt_ldt_desc_ptr (gate_sel); + seg_desc_t desc; +const seg_desc_t *pdesc =gdt_ldt_desc_ptr (gate_sel); if ( (gate_sel < 4) || ((gate_sel >= FIRST_RESERVED_GDT_BYTE) && !(gate_sel & 4)) ||
xen/arch/x86/pv/emulate.c +1 -1
@@ -29,7 +29,7 @@ int pv_emul_read_descriptor(unsigned int sel, const struct vcpu *v, unsigned long *base, unsigned long *limit, unsigned int *ar, bool insn_fetch) { - struct desc_struct desc; + seg_desc_t desc;if ( sel <4 ) desc.b = desc.a =0 ;
xen/arch/x86/pv/emulate.h +2 -2
@@ -18,10 +18,10 @@ static inline int pv_emul_is_mem_write(const struct x86_emulate_state *state, }/* Return a pointer to the GDT/LDT descriptor referenced by sel. */ -static inline const struct desc_struct *gdt_ldt_desc_ptr (unsigned int sel) +static inline const seg_desc_t *gdt_ldt_desc_ptr (unsigned int sel) {const struct vcpu *curr = current; -const struct desc_struct *tbl = (void *) +const seg_desc_t *tbl = (void *) ((sel &X86_XEC_TI ) ?LDT_VIRT_START (curr) :GDT_VIRT_START (curr));return &tbl[sel >>3 ];
xen/arch/x86/smpboot.c +1 -1
@@ -972,7 +972,7 @@ static int cpu_smpboot_alloc(unsigned int cpu) {unsigned int i, order, memflags =0 ;nodeid_t node =cpu_to_node (cpu); -struct desc_struct *gdt; +seg_desc_t *gdt;unsigned long stub_page;int rc = -ENOMEM ;
xen/arch/x86/traps.c +2 -2
@@ -96,8 +96,8 @@ string_param("nmi", opt_nmi);DEFINE_PER_CPU (uint64_t, efer); static DEFINE_PER_CPU(unsigned long, last_extable_addr); -DEFINE_PER_CPU_READ_MOSTLY (struct desc_struct *, gdt_table); -DEFINE_PER_CPU_READ_MOSTLY(struct desc_struct *, compat_gdt_table); +DEFINE_PER_CPU_READ_MOSTLY (seg_desc_t *, gdt_table); +DEFINE_PER_CPU_READ_MOSTLY(seg_desc_t *, compat_gdt_table);/* Master table, used by CPU0. */ idt_entry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
xen/arch/x86/x86_64/mm.c +1 -1
@@ -1075,7 +1075,7 @@ long do_set_segment_base(unsigned int which, unsigned long base)/* Returns TRUE if given descriptor is valid for GDT or LDT. */ -int check_descriptor (const struct domain *dom,struct desc_struct *d) +int check_descriptor (const struct domain *dom,seg_desc_t *d) {u32 a = d->a, b = d->b;u16 cs;
xen/include/asm-x86/desc.h +7 -7
@@ -102,9 +102,9 @@#define SYS_DESC_irq_gate 14#define SYS_DESC_trap_gate 15 -struct desc_struct { -u32 a, b; -}; +typedef struct { +uint32_t a, b; +}seg_desc_t ; typedef union { struct {
@@ -201,10 +201,10 @@ struct __packed desc_ptr {unsigned long base; }; -extern struct desc_struct boot_cpu_gdt_table[]; -DECLARE_PER_CPU(struct desc_struct *, gdt_table); -extern struct desc_struct boot_cpu_compat_gdt_table[]; -DECLARE_PER_CPU(struct desc_struct *, compat_gdt_table); +extern seg_desc_t boot_cpu_gdt_table[]; +DECLARE_PER_CPU(seg_desc_t *, gdt_table); +extern seg_desc_t boot_cpu_compat_gdt_table[]; +DECLARE_PER_CPU(seg_desc_t *, compat_gdt_table);extern void load_TR (void );
xen/include/asm-x86/ldt.h +1 -1
@@ -6,7 +6,7 @@static inline void load_LDT (struct vcpu *v) { -struct desc_struct *desc; +seg_desc_t *desc;unsigned long ents;if ( (ents = v->arch.pv.ldt_ents) ==0 )
xen/include/asm-x86/mm.h +1 -1
@@ -438,7 +438,7 @@ static inline int get_page_and_type(struct page_info *page,ASSERT (((_p)->count_info & PGC_count_mask) !=0 ); \ ASSERT(page_get_owner(_p) == (_d)) -int check_descriptor (const struct domain *,struct desc_struct *d); +int check_descriptor (const struct domain *d,seg_desc_t *desc);extern paddr_t mem_hotplug;
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/2] x86: Switch "struct desc_struct" to being seg_desc_t
Quoted 7 lines · Andrew Cooper
The struct suffix is redundant in the name, and a future change will want to turn it into a union, rather than a structure. As this represents a segment descriptor, give it an appropriate typedef.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/2] x86: Switch "struct desc_struct" to being seg_desc_t
In reply to Andrew CooperQuoted 8 lines
Quoted 1 line
Quoted 1 line · On 06.12.18 at 19:03, <[email protected]> wrote:
The struct suffix is redundant in the name, and a future change will want to turn it into a union, rather than a structure. As this represents a segment descriptor, give it an appropriate typedef.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
but with a (more general) question: Are we then ourselves on a position different than some Linux maintainers have taken in the past, declaring that typedef-s are evil? I've never understood what reason(s) they had, so please don't take this as an objection in any way.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 2/2 x86/pv: Code improvements to do_update_descriptor()
In reply to Andrew Cooper * Add "uint64_t raw" to seg_desc_t to remove the opencoded uint64_t casting in this function.
* Rename the 'pa' parameter to 'gaddr', because it lives in GFN space rather than physical address space.
* Use gfn_t and mfn_t rather than unsigned longs.
* Check the alignment and proposed new descriptor before taking a page reference.
* Reuse the out label for all exit paths.
* Use the more flexible ACCESS_ONCE() accessor in preference to write_atomic()
No expected change in behaviour.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/pv/descriptor-tables.c | 41 +++++++++++++++++-------------------- xen/include/asm-x86/desc.h | 7 +++++-- 2 files changed, 24 insertions(+), 24 deletions(-)
xen/arch/x86/pv/descriptor-tables.c +19 -22
@@ -206,30 +206,26 @@ int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list,return ret; } -long do_update_descriptor (uint64_t pa,uint64_t desc) +long do_update_descriptor (uint64_t gaddr,uint64_t desc) {struct domain *currd = current->domain; -unsigned long gmfn = pa >>PAGE_SHIFT ; -unsigned long mfn; -unsigned int offset; -seg_desc_t *gdt_pent, d; +gfn_t gfn =gaddr_to_gfn (gaddr); +mfn_t mfn; +seg_desc_t *entry, d;struct page_info *page;long ret = -EINVAL ; - offset = ((unsigned int )pa & ~PAGE_MASK ) /sizeof (seg_desc_t); + d.raw = desc; - *(uint64_t *)&d = desc; +/* gaddr must be aligned, or it will corrupt adjacent descriptors. */ +if ( !IS_ALIGNED (gaddr,sizeof (d)) || !check_descriptor (currd, &d) ) + goto out; - page =get_page_from_gfn (currd, gmfn,NULL ,P2M_ALLOC ); -if ( (((unsigned int )pa %sizeof (seg_desc_t)) !=0 ) || - !page || - !check_descriptor (currd, &d) ) - { -if ( page ) -put_page (page); -return -EINVAL ; - } - mfn =mfn_x (page_to_mfn (page)); + page =get_page_from_gfn (currd,gfn_x (gfn),NULL ,P2M_ALLOC ); +if ( !page ) + goto out; + + mfn =page_to_mfn (page);/* Check if the given frame is in use in an unsafe context. */ switch ( page->u.inuse.type_info & PGT_type_mask )
@@ -244,19 +240,20 @@ long do_update_descriptor(uint64_t pa, uint64_t desc)break ; } -paging_mark_dirty (currd,_mfn (mfn)); +paging_mark_dirty (currd, mfn);/* All is good so make the update. */ - gdt_pent =map_domain_page (_mfn (mfn)); -write_atomic ((uint64_t *)&gdt_pent[offset], *(uint64_t *)&d); -unmap_domain_page (gdt_pent); + entry =map_domain_page (mfn) + (gaddr & ~PAGE_MASK ); +ACCESS_ONCE (entry->raw) = d.raw; +unmap_domain_page (entry);put_page_type (page); ret =0 ;/* success */ out: -put_page (page); +if ( page ) +put_page (page);return ret; }
xen/include/asm-x86/desc.h +5 -2
@@ -102,8 +102,11 @@#define SYS_DESC_irq_gate 14#define SYS_DESC_trap_gate 15 -typedef struct { -uint32_t a, b; +typedef union { +struct { +uint32_t a, b; + }; +uint64_t raw; }seg_desc_t ; typedef union {
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 2/2] x86/pv: Code improvements to do_update_descriptor()
Quoted 11 lines · Andrew Cooper
* Add "
uint64_traw" toseg_desc_tto remove the opencodeduint64_tcasting in this function.
* Rename the 'pa' parameter to 'gaddr', because it lives in GFN space rather than physical address space.
* Usegfn_tandmfn_trather than unsigned longs.
* Check the alignment and proposed new descriptor before taking a page reference.
* Reuse the out label for all exit paths.
* Use the more flexibleACCESS_ONCE()accessor in preference towrite_atomic()
Quoted 3 lines
No expected change in behaviour.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 2/2] x86/pv: Code improvements to do_update_descriptor()
In reply to Andrew CooperQuoted 15 lines
Quoted 1 line
Quoted 1 line · On 06.12.18 at 19:03, <[email protected]> wrote:
xen/arch/x86/pv/descriptor-tables.c +1
-5@@ -206,30 +206,26 @@ int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list,return ret; } -long do_update_descriptor (uint64_t pa,uint64_t desc) +long do_update_descriptor (uint64_t gaddr,uint64_t desc) {struct domain *currd = current->domain; -unsigned long gmfn = pa >>PAGE_SHIFT ; -unsigned long mfn; -unsigned int offset; -seg_desc_t *gdt_pent, d;
xen/arch/x86/pv/descriptor-tables.c int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list, · line 221
Quoted 10 lines
xen/arch/x86/pv/descriptor-tables.c +4
-2+gfn_t gfn =gaddr_to_gfn (gaddr); +mfn_t mfn; +seg_desc_t *entry, d;struct page_info *page;long ret = -EINVAL ; - offset = ((unsigned int )pa & ~PAGE_MASK ) /sizeof (seg_desc_t); + d.raw = desc; - *(uint64_t *)&d = desc;
Instead of old or new variant, how about simply making the second function parameter seg_desc_t?
xen/arch/x86/pv/descriptor-tables.c int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list, · line 222
Quoted 3 lines
xen/arch/x86/pv/descriptor-tables.c +3
-0+/* gaddr must be aligned, or it will corrupt adjacent descriptors. */ +if ( !IS_ALIGNED (gaddr,sizeof (d)) || !check_descriptor (currd, &d) ) + goto out;
I'd really prefer if we didn't see goto-s added where they're really unneeded. You easily "return -EINVAL" here and ...
xen/arch/x86/pv/descriptor-tables.c int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list, · line 226
Quoted 13 lines
xen/arch/x86/pv/descriptor-tables.c +3
-10- page =get_page_from_gfn (currd, gmfn,NULL ,P2M_ALLOC ); -if ( (((unsigned int )pa %sizeof (seg_desc_t)) !=0 ) || - !page || - !check_descriptor (currd, &d) ) - { -if ( page ) -put_page (page); -return -EINVAL ; - } - mfn =mfn_x (page_to_mfn (page)); + page =get_page_from_gfn (currd,gfn_x (gfn),NULL ,P2M_ALLOC ); +if ( !page ) + goto out;
... here, avoiding the if() at the "out" label at the same time.
xen/arch/x86/pv/descriptor-tables.c long do_update_descriptor(uint64_t pa, uint64_t desc) · line 247
Quoted 13 lines
xen/arch/x86/pv/descriptor-tables.c +3
-4@@ -244,19 +240,20 @@ long do_update_descriptor(uint64_t pa, uint64_t desc)break ; } -paging_mark_dirty (currd,_mfn (mfn)); +paging_mark_dirty (currd, mfn);/* All is good so make the update. */ - gdt_pent =map_domain_page (_mfn (mfn)); -write_atomic ((uint64_t *)&gdt_pent[offset], *(uint64_t *)&d); -unmap_domain_page (gdt_pent); + entry =map_domain_page (mfn) + (gaddr & ~PAGE_MASK ); +ACCESS_ONCE (entry->raw) = d.raw;
Why not "ACCESS_ONCE(*entry) = d;"? I'm having trouble to understand why the macro insists on using scalar types (there's no comment there explaining the need).
The earlier and this change together would then also eliminate the need for a union as it seems.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 2/2] x86/pv: Code improvements to do_update_descriptor()
Quoted 7 lines · Jan Beulich
Quoted 7 lines
xen/arch/x86/pv/descriptor-tables.c +1
-1@@ -244,19 +240,20 @@ long do_update_descriptor(uint64_t pa, uint64_t desc)break ; } -paging_mark_dirty (currd,_mfn (mfn)); +paging_mark_dirty (currd, mfn);
Quoted 9 lines
xen/arch/x86/pv/descriptor-tables.c long do_update_descriptor(uint64_t pa, uint64_t desc) · line 247
Quoted 6 lines
xen/arch/x86/pv/descriptor-tables.c +2
-3/* All is good so make the update. */ - gdt_pent =map_domain_page (_mfn (mfn)); -write_atomic ((uint64_t *)&gdt_pent[offset], *(uint64_t *)&d); -unmap_domain_page (gdt_pent); + entry =map_domain_page (mfn) + (gaddr & ~PAGE_MASK ); +ACCESS_ONCE (entry->raw) = d.raw;Why not "
ACCESS_ONCE(*entry)= d;"? I'm having trouble to understand why the macro insists on using scalar types (there's no comment there explaining the need).
GCC 4.x (6 and 7 iirc) have a bug where the volatile cast doesn't get applied to structure members. This case I think it would be safe as the write can't be hoisted before the map_domain_page(), but this bug did result in incorrect compiled code in Linux.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v2 2/2 x86/pv: Code improvements to do_update_descriptor()
In reply to Andrew Cooper * Add "uint64_t raw" to seg_desc_t to remove the opencoded uint64_t casting in this function. Change the parameter to be of type seg_desc_t.
* Rename the 'pa' parameter to 'gaddr', because it lives in GFN space rather than physical address space.
* Use gfn_t and mfn_t rather than unsigned longs.
* Check the alignment and proposed new descriptor before taking a page reference.
* Reuse the out label for all exit paths.
* Use the more flexible ACCESS_ONCE() accessor in preference to write_atomic()
No expected change in behaviour.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
v2:
* Change parameter to be seg_desc_t.
--- xen/arch/x86/pv/descriptor-tables.c | 43 +++++++++++++++++-------------------- xen/include/asm-x86/desc.h | 7 ++++-- xen/include/asm-x86/hypercall.h | 3 +-- 3 files changed, 26 insertions(+), 27 deletions(-)
xen/arch/x86/pv/descriptor-tables.c +20 -23
@@ -206,30 +206,24 @@ int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list,return ret; } -long do_update_descriptor (uint64_t pa,uint64_t desc) +long do_update_descriptor (uint64_t gaddr,seg_desc_t d) {struct domain *currd = current->domain; -unsigned long gmfn = pa >>PAGE_SHIFT ; -unsigned long mfn; -unsigned int offset; -seg_desc_t *gdt_pent, d; +gfn_t gfn =gaddr_to_gfn (gaddr); +mfn_t mfn; +seg_desc_t *entry;struct page_info *page;long ret = -EINVAL ; - offset = ((unsigned int )pa & ~PAGE_MASK ) /sizeof (seg_desc_t); - - *(uint64_t *)&d = desc; +/* gaddr must be aligned, or it will corrupt adjacent descriptors. */ +if ( !IS_ALIGNED (gaddr,sizeof (d)) || !check_descriptor (currd, &d) ) +return -EINVAL ; - page =get_page_from_gfn (currd, gmfn,NULL ,P2M_ALLOC ); -if ( (((unsigned int )pa %sizeof (seg_desc_t)) !=0 ) || - !page || - !check_descriptor (currd, &d) ) - { -if ( page ) -put_page (page); + page =get_page_from_gfn (currd,gfn_x (gfn),NULL ,P2M_ALLOC ); +if ( !page )return -EINVAL ; - } - mfn =mfn_x (page_to_mfn (page)); + + mfn =page_to_mfn (page);/* Check if the given frame is in use in an unsafe context. */ switch ( page->u.inuse.type_info & PGT_type_mask )
@@ -244,12 +238,12 @@ long do_update_descriptor(uint64_t pa, uint64_t desc)break ; } -paging_mark_dirty (currd,_mfn (mfn)); +paging_mark_dirty (currd, mfn);/* All is good so make the update. */ - gdt_pent =map_domain_page (_mfn (mfn)); -write_atomic ((uint64_t *)&gdt_pent[offset], *(uint64_t *)&d); -unmap_domain_page (gdt_pent); + entry =map_domain_page (mfn) + (gaddr & ~PAGE_MASK ); +ACCESS_ONCE (entry->raw) = d.raw; +unmap_domain_page (entry);put_page_type (page);
@@ -264,8 +258,11 @@ long do_update_descriptor(uint64_t pa, uint64_t desc)int compat_update_descriptor (uint32_t pa_lo,uint32_t pa_hi,uint32_t desc_lo,uint32_t desc_hi) { -return do_update_descriptor (pa_lo | ((uint64_t )pa_hi <<32 ), - desc_lo | ((uint64_t )desc_hi <<32 )); +seg_desc_t d; + + d.raw = ((uint64_t )desc_hi <<32 ) | desc_lo; + +return do_update_descriptor (pa_lo | ((uint64_t )pa_hi <<32 ), d); }/*
xen/include/asm-x86/desc.h +5 -2
@@ -102,8 +102,11 @@#define SYS_DESC_irq_gate 14#define SYS_DESC_trap_gate 15 -typedef struct { -uint32_t a, b; +typedef union { +struct { +uint32_t a, b; + }; +uint64_t raw; }seg_desc_t ; typedef union {
xen/include/asm-x86/hypercall.h +1 -2
@@ -88,8 +88,7 @@ do_get_debugreg(extern long do_update_descriptor ( -u64 pa, -u64 desc); +uint64_t gaddr,seg_desc_t desc);extern long do_mca (XEN_GUEST_HANDLE_PARAM (xen_mc_t) u_xen_mc);
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v2 2/2] x86/pv: Code improvements to do_update_descriptor()
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 10.12.18 at 15:06, <[email protected]> wrote:
* Add "
uint64_traw" toseg_desc_tto remove the opencodeduint64_tcasting in this function. Change the parameter to be of typeseg_desc_t.
* Rename the 'pa' parameter to 'gaddr', because it lives in GFN space rather than physical address space.
* Usegfn_tandmfn_trather than unsigned longs.
* Check the alignment and proposed new descriptor before taking a page reference.
* Reuse the out label for all exit paths.
* Use the more flexibleACCESS_ONCE()accessor in preference towrite_atomic()
Quoted 4 lines
No expected change in behaviour.
Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Wei Liu <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel