diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index 5c6261a2f8..043139b9b9 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -421,6 +421,8 @@ static int hvmemul_virtual_to_linear( ? 1 : 4096); reg = hvmemul_get_seg_reg(seg, hvmemul_ctxt); + if ( IS_ERR(reg) ) + return -PTR_ERR(reg); if ( (hvmemul_ctxt->ctxt.regs->eflags & X86_EFLAGS_DF) && (*reps > 1) ) { @@ -1000,6 +1002,10 @@ static int hvmemul_read_segment( struct hvm_emulate_ctxt *hvmemul_ctxt = container_of(ctxt, struct hvm_emulate_ctxt, ctxt); struct segment_register *sreg = hvmemul_get_seg_reg(seg, hvmemul_ctxt); + + if ( IS_ERR(sreg) ) + return -PTR_ERR(sreg); + memcpy(reg, sreg, sizeof(struct segment_register)); return X86EMUL_OKAY; } @@ -1013,6 +1019,9 @@ static int hvmemul_write_segment( container_of(ctxt, struct hvm_emulate_ctxt, ctxt); struct segment_register *sreg = hvmemul_get_seg_reg(seg, hvmemul_ctxt); + if ( IS_ERR(sreg) ) + return -PTR_ERR(sreg); + memcpy(sreg, reg, sizeof(struct segment_register)); __set_bit(seg, &hvmemul_ctxt->seg_reg_dirty); @@ -1477,13 +1486,22 @@ void hvm_emulate_writeback( } } +/* + * Callers which pass a known in-range x86_segment can rely on the return + * pointer being valid. Other callers must explicitly check for errors. + */ struct segment_register *hvmemul_get_seg_reg( enum x86_segment seg, struct hvm_emulate_ctxt *hvmemul_ctxt) { - if ( !__test_and_set_bit(seg, &hvmemul_ctxt->seg_reg_accessed) ) - hvm_get_segment_register(current, seg, &hvmemul_ctxt->seg_reg[seg]); - return &hvmemul_ctxt->seg_reg[seg]; + unsigned int idx = seg; + + if ( idx >= ARRAY_SIZE(hvmemul_ctxt->seg_reg) ) + return ERR_PTR(-X86EMUL_UNHANDLEABLE); + + if ( !__test_and_set_bit(idx, &hvmemul_ctxt->seg_reg_accessed) ) + hvm_get_segment_register(current, idx, &hvmemul_ctxt->seg_reg[idx]); + return &hvmemul_ctxt->seg_reg[idx]; } static const char *guest_x86_mode_to_str(int mode) diff --git a/xen/arch/x86/mm/shadow/common.c b/xen/arch/x86/mm/shadow/common.c index 3820297f2d..455106683e 100644 --- a/xen/arch/x86/mm/shadow/common.c +++ b/xen/arch/x86/mm/shadow/common.c @@ -120,12 +120,22 @@ __initcall(shadow_audit_key_init); /* x86 emulator support for the shadow code */ +/* + * Callers which pass a known in-range x86_segment can rely on the return + * pointer being valid. Other callers must explicitly check for errors. + */ struct segment_register *hvm_get_seg_reg( enum x86_segment seg, struct sh_emulate_ctxt *sh_ctxt) { - struct segment_register *seg_reg = &sh_ctxt->seg_reg[seg]; - if ( !__test_and_set_bit(seg, &sh_ctxt->valid_seg_regs) ) - hvm_get_segment_register(current, seg, seg_reg); + unsigned int idx = seg; + struct segment_register *seg_reg; + + if ( idx >= ARRAY_SIZE(sh_ctxt->seg_reg) ) + return ERR_PTR(-X86EMUL_UNHANDLEABLE); + + seg_reg = &sh_ctxt->seg_reg[idx]; + if ( !__test_and_set_bit(idx, &sh_ctxt->valid_seg_regs) ) + hvm_get_segment_register(current, idx, seg_reg); return seg_reg; } @@ -140,14 +150,9 @@ static int hvm_translate_linear_addr( struct segment_register *reg; int okay; - /* - * Can arrive here with non-user segments. However, no such cirucmstance - * is part of a legitimate pagetable update, so fail the emulation. - */ - if ( !is_x86_user_segment(seg) ) - return X86EMUL_UNHANDLEABLE; - reg = hvm_get_seg_reg(seg, sh_ctxt); + if ( IS_ERR(reg) ) + return -PTR_ERR(reg); okay = hvm_virtual_to_linear_addr( seg, reg, offset, bytes, access_type, sh_ctxt->ctxt.addr_size, paddr); @@ -249,9 +254,6 @@ hvm_emulate_write(enum x86_segment seg, unsigned long addr; int rc; - if ( !is_x86_user_segment(seg) ) - return X86EMUL_UNHANDLEABLE; - /* How many emulations could we save if we unshadowed on stack writes? */ if ( seg == x86_seg_ss ) perfc_incr(shadow_fault_emulate_stack); @@ -279,9 +281,6 @@ hvm_emulate_cmpxchg(enum x86_segment seg, unsigned long addr, old[2], new[2]; int rc; - if ( !is_x86_user_segment(seg) ) - return X86EMUL_UNHANDLEABLE; - rc = hvm_translate_linear_addr( seg, offset, bytes, hvm_access_write, sh_ctxt, &addr); if ( rc ) diff --git a/xen/include/asm-x86/hvm/emulate.h b/xen/include/asm-x86/hvm/emulate.h index 5411302eef..9a68ea894d 100644 --- a/xen/include/asm-x86/hvm/emulate.h +++ b/xen/include/asm-x86/hvm/emulate.h @@ -13,6 +13,7 @@ #define __ASM_X86_HVM_EMULATE_H__ #include +#include #include #include