Reimpliement {compat_, }create_bounce_frame() in C
cover Reimpliement {compat_, }create_bounce_frame() in C
Bloat-o-meter reports:
add/remove: 1/1 grow/shrink: 0/1 up/down: 1211/-2587 (-1376)
function old new delta
pv_create_exception_frame - 1211 +1211
asm_domain_crash_synchronous 65 - -65
context_switch 3565 1043 -2522
but as the assembly symbols aren't tagged with type/size information, it can't spot the removal of {compat_,}create_bounce_frame() at all.
This series is based on top of the "[PATCH for-4.9 0/2] x86/pv: Misc fixes" series, which were discovered while developing this series.
Andrew Cooper (4):
x86/pv: Drop int80_bounce from struct pv_vcpu
x86/pv: Introduce pv_create_exception_frame()
x86/pv: Drop {compat_,}create_bounce_frame() and use the C version instead x86/pv: Implement the failsafe callback using the general path
xen/arch/x86/domain.c | 102 ++-------------------- xen/arch/x86/pv/traps.c | 152 ++++++++++++++++++++++++++++++++ xen/arch/x86/traps.c | 27 ------ xen/arch/x86/x86_64/asm-offsets.c | 1 - xen/arch/x86/x86_64/compat/entry.S | 116 ++----------------------- xen/arch/x86/x86_64/compat/traps.c | 4 - xen/arch/x86/x86_64/entry.S | 173 +++++++------------------------------ xen/arch/x86/x86_64/traps.c | 14 --- xen/include/asm-x86/domain.h | 1 - xen/include/asm-x86/processor.h | 3 +- xen/include/xen/sched.h | 7 -- 11 files changed, 198 insertions(+), 402 deletions(-)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/4 x86/pv: Drop int80_bounce from struct pv_vcpu
The int80_bounce field of struct pv_vcpu is a bit of an odd special case, because it is a simple derivation of trap_ctxt[0x80], which is also stored.
It is also the only use of {compat_,}create_bounce_frame() which isn't referencing the plain trap_bounce field of struct pv_vcpu. (And altering this property the purpose of this patch.)
Remove the int80_bounce field entirely, along with init_int80_direct_trap(), which in turn requires that the int80_direct_trap() path gain logic previously contained in init_int80_direct_trap().
This does admittedly make the int80 fastpath slightly longer, but these few instructions are in the noise compared to the architectural context switch overhead, and it now matches the syscall/sysenter paths (which have far less architectural overhead already).
No behavioural change from the guests point of view.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/domain.c | 2 -- xen/arch/x86/traps.c | 4 ---- xen/arch/x86/x86_64/asm-offsets.c | 1 - xen/arch/x86/x86_64/compat/traps.c | 4 ---- xen/arch/x86/x86_64/entry.S | 32 ++++++++++++++++++++++++++++---- xen/arch/x86/x86_64/traps.c | 14 -------------- xen/include/asm-x86/domain.h | 1 - xen/include/asm-x86/processor.h | 2 -- 8 files changed, 28 insertions(+), 32 deletions(-)
xen/arch/x86/domain.c +0 -2
@@ -833,8 +833,6 @@ int arch_set_info_guest( goto out; } -init_int80_direct_trap (v); -/* IOPL privileges are virtualised. */ v->arch.pv_vcpu.iopl = v->arch.user_regs.eflags &X86_EFLAGS_IOPL ; v->arch.user_regs.eflags &= ~X86_EFLAGS_IOPL ;
xen/arch/x86/traps.c +0 -4
@@ -4013,7 +4013,6 @@ long do_set_trap_table(XEN_GUEST_HANDLE_PARAM(const_trap_info_t) traps)if (guest_handle_is_null (traps) ) {memset (dst,0 ,NR_VECTORS *sizeof (*dst)); -init_int80_direct_trap (curr);return 0 ; }
@@ -4035,9 +4034,6 @@ long do_set_trap_table(XEN_GUEST_HANDLE_PARAM(const_trap_info_t) traps)memcpy (&dst[cur.vector], &cur,sizeof (cur)); -if ( cur.vector ==0x80 ) -init_int80_direct_trap (curr); -guest_handle_add_offset (traps,1 );if (hypercall_preempt_check () )
xen/arch/x86/x86_64/asm-offsets.c +0 -1
@@ -61,7 +61,6 @@ void __dummy__(void) OFFSET(VCPU_domain, struct vcpu, domain); OFFSET(VCPU_vcpu_info, struct vcpu, vcpu_info); OFFSET(VCPU_trap_bounce, struct vcpu, arch.pv_vcpu.trap_bounce); - OFFSET(VCPU_int80_bounce, struct vcpu, arch.pv_vcpu.int80_bounce); OFFSET(VCPU_thread_flags, struct vcpu, arch.flags); OFFSET(VCPU_event_addr, struct vcpu, arch.pv_vcpu.event_callback_eip); OFFSET(VCPU_event_sel, struct vcpu, arch.pv_vcpu.event_callback_cs);
xen/arch/x86/x86_64/compat/traps.c +0 -4
@@ -339,7 +339,6 @@ int compat_set_trap_table(XEN_GUEST_HANDLE(trap_info_compat_t) traps)if (guest_handle_is_null (traps) ) {memset (dst,0 ,NR_VECTORS *sizeof (*dst)); -init_int80_direct_trap (current);return 0 ; }
@@ -358,9 +357,6 @@ int compat_set_trap_table(XEN_GUEST_HANDLE(trap_info_compat_t) traps)XLAT_trap_info (dst + cur.vector, &cur); -if ( cur.vector ==0x80 ) -init_int80_direct_trap (current); -guest_handle_add_offset (traps,1 );if (hypercall_preempt_check () )
xen/arch/x86/x86_64/entry.S +28 -4
@@ -233,12 +233,36 @@ UNLIKELY_END(msi_check) GET_CURRENT(bx) - /* Check that the callback is non-null. */ - leaq VCPU_int80_bounce(%rbx),%rdx - cmpb $0,TRAPBOUNCE_flags(%rdx) + mov VCPU_trap_ctxt(%rbx), %rsi + mov VCPU_domain(%rbx), %rax + + /* + * if ( null_trap_bounce(v, &v->arch.pv_vcpu.trap_ctxt[0x80]) ) + * goto int80_slow_path; + */ + mov 0x80 * TRAPINFO_sizeof + TRAPINFO_eip(%rsi), %rdi + movzwl 0x80 * TRAPINFO_sizeof + TRAPINFO_cs (%rsi), %ecx + + mov %ecx, %edx + and $~3, %edx + + testb $1, DOMAIN_is_32bit_pv(%rax) + cmove %rdi, %rdx + + test %rdx, %rdx jz int80_slow_path - movq VCPU_domain(%rbx),%rax + /* Construct trap_bounce from trap_ctxt[0x80]. */ + lea VCPU_trap_bounce(%rbx), %rdx + movw %cx, TRAPBOUNCE_cs(%rdx) + movq %rdi, TRAPBOUNCE_eip(%rdx) + + /* TB_flags = TBF_EXCEPTION | (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */ + testb $4, 0x80 * TRAPINFO_sizeof + TRAPINFO_flags(%rsi) + setnz %cl + lea TBF_EXCEPTION(, %rcx, TBF_INTERRUPT), %ecx + movb %cl, TRAPBOUNCE_flags(%rdx) + testb $1,DOMAIN_is_32bit_pv(%rax) jnz compat_int80_direct_trap
xen/arch/x86/x86_64/traps.c +0 -14
@@ -422,20 +422,6 @@ void subarch_percpu_traps_init(void)wrmsrl (MSR_SYSCALL_MASK ,XEN_SYSCALL_MASK ); } -void init_int80_direct_trap (struct vcpu *v) -{ -struct trap_info *ti = &v->arch.pv_vcpu.trap_ctxt[0x80 ]; -struct trap_bounce *tb = &v->arch.pv_vcpu.int80_bounce; - - tb->cs = ti->cs; - tb->eip = ti->address; - -if (null_trap_bounce (v, tb) ) - tb->flags =0 ; -else - tb->flags =TBF_EXCEPTION | (TI_GET_IF (ti) ?TBF_INTERRUPT :0 ); -} -static long register_guest_callback (struct callback_register *reg) {long ret =0 ;
xen/include/asm-x86/domain.h +0 -1
@@ -473,7 +473,6 @@ struct pv_vcpu/* Bounce information for propagating an exception to guest OS. */ struct trap_bounce trap_bounce; -struct trap_bounce int80_bounce;/* I/O-port access bitmap. */ XEN_GUEST_HANDLE (uint8) iobmp;/* Guest kernel vaddr of the bitmap. */
xen/include/asm-x86/processor.h +0 -2
@@ -466,8 +466,6 @@ extern idt_entry_t *idt_tables[]; DECLARE_PER_CPU(struct tss_struct, init_tss); -extern void init_int80_direct_trap (struct vcpu *v); -extern void write_ptbase (struct vcpu *v);void destroy_gdt (struct vcpu *d);
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 1/4] x86/pv: Drop int80_bounce from struct pv_vcpu
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/x86_64/entry.S +4
-3@@ -233,12 +233,36 @@ UNLIKELY_END(msi_check) GET_CURRENT(bx) - /* Check that the callback is non-null. */ - leaq VCPU_int80_bounce(%rbx),%rdx - cmpb $0,TRAPBOUNCE_flags(%rdx) + mov VCPU_trap_ctxt(%rbx), %rsi + mov VCPU_domain(%rbx), %rax + + /*
xen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 249
Quoted 10 lines
xen/arch/x86/x86_64/entry.S +10
-0+ * if ( null_trap_bounce(v, &v->arch.pv_vcpu.trap_ctxt[0x80]) ) + * goto int80_slow_path; + */ + mov 0x80 * TRAPINFO_sizeof + TRAPINFO_eip(%rsi), %rdi + movzwl 0x80 * TRAPINFO_sizeof + TRAPINFO_cs (%rsi), %ecx + + mov %ecx, %edx + and $~3, %edx + + testb $1, DOMAIN_is_32bit_pv(%rax)
While we may have other such instances, this is dangerous (and long ago I think we had actual issues with constructs like this one). Either "cmpb" against zero, or "testb" with 0xff.
xen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 250
Quoted 1 line
xen/arch/x86/x86_64/entry.S +1
-0+ cmove %rdi, %rdx
As there's nothing "equal" here, but only the question of ZF being set of clear, the more natural form would be "cmovz" (just like you use "jz" and "setnz" below).
Quoted 4 lines
xen/arch/x86/x86_64/entry.S +1
-1+ test %rdx, %rdx jz int80_slow_path - movq VCPU_domain(%rbx),%rax
xen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 264
Quoted 10 lines
xen/arch/x86/x86_64/entry.S +10
-0+ /* Construct trap_bounce from trap_ctxt[0x80]. */ + lea VCPU_trap_bounce(%rbx), %rdx + movw %cx, TRAPBOUNCE_cs(%rdx) + movq %rdi, TRAPBOUNCE_eip(%rdx) + + /* TB_flags = TBF_EXCEPTION | (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */ + testb $4, 0x80 * TRAPINFO_sizeof + TRAPINFO_flags(%rsi) + setnz %cl + lea TBF_EXCEPTION(, %rcx, TBF_INTERRUPT), %ecx + movb %cl, TRAPBOUNCE_flags(%rdx)
In code further up you avoided mnemonic suffixes where possible, yet in this code section you're (partly) using them even when a register operand makes them redundant.
Since I understand this code will go away with subsequent patches anyway, I don't insist on changing these though. Hence with or without the adjustments
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/4] x86/pv: Drop int80_bounce from struct pv_vcpu
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/x86_64/entry.S +9
-3@@ -233,12 +233,36 @@ UNLIKELY_END(msi_check) GET_CURRENT(bx) - /* Check that the callback is non-null. */ - leaq VCPU_int80_bounce(%rbx),%rdx - cmpb $0,TRAPBOUNCE_flags(%rdx) + mov VCPU_trap_ctxt(%rbx), %rsi + mov VCPU_domain(%rbx), %rax + + /* + * if ( null_trap_bounce(v, &v->arch.pv_vcpu.trap_ctxt[0x80]) ) + * goto int80_slow_path; + */ + mov 0x80 * TRAPINFO_sizeof + TRAPINFO_eip(%rsi), %rdi + movzwl 0x80 * TRAPINFO_sizeof + TRAPINFO_cs (%rsi), %ecx
Quoted 8 lines
xen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 249
Quoted 5 lines
xen/arch/x86/x86_64/entry.S +5
-0+ + mov %ecx, %edx + and $~3, %edx + + testb $1, DOMAIN_is_32bit_pv(%rax)While we may have other such instances, this is dangerous (and long ago I think we had actual issues with constructs like this one). Either "cmpb" against zero, or "testb" with
0xff.
Hmm. This was a straight copy of the logic to switch between the exit paths.
I think I will prepare a fix for this general issue in a separate patch, to avoid being mixed in with this logical change.
Quoted 20 lines
xen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 250
Quoted 2 lines
xen/arch/x86/x86_64/entry.S +1
-0+ cmove %rdi, %rdxAs there's nothing "equal" here, but only the question of ZF being set of clear, the more natural form would be "cmovz" (just like you use "jz" and "setnz" below).
Quoted 4 lines
xen/arch/x86/x86_64/entry.S +1
-1+ test %rdx, %rdx jz int80_slow_path - movq VCPU_domain(%rbx),%raxxen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 264
Quoted 10 lines
xen/arch/x86/x86_64/entry.S +10
-0+ /* Construct trap_bounce from trap_ctxt[0x80]. */ + lea VCPU_trap_bounce(%rbx), %rdx + movw %cx, TRAPBOUNCE_cs(%rdx) + movq %rdi, TRAPBOUNCE_eip(%rdx) + + /* TB_flags = TBF_EXCEPTION | (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */ + testb $4, 0x80 * TRAPINFO_sizeof + TRAPINFO_flags(%rsi) + setnz %cl + lea TBF_EXCEPTION(, %rcx, TBF_INTERRUPT), %ecx + movb %cl, TRAPBOUNCE_flags(%rdx)
Quoted 3 lines
In code further up you avoided mnemonic suffixes where possible, yet in this code section you're (partly) using them even when a register operand makes them redundant.
Ah yes. I had a bug originally where I was moving %ecx rather than %cl and clobbering flags, so reintroduced the suffixes to match the struct trap_bounce definition.
Quoted 5 lines
Since I understand this code will go away with subsequent patches anyway, I don't insist on changing these though. Hence with or without the adjustments
Reviewed-by Jan Beulich <[email protected]>
This code (hopefully) isn't going to stay around long, but it is certainly not removed in this series. I will try to leave it in a good state.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 2/4 x86/pv: Introduce pv_create_exception_frame()
In reply to Andrew CooperThis is a C implementation of {compat_,}create_bounce_frame(), based loosely on the existing failsafe implementation in load_segments(). It picks up all injection information from the trap_bounce structure.
One minor improvement is that at not point is regs->cs left with an rpl of 0 on the root stack frame.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/pv/traps.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+)
xen/arch/x86/pv/traps.c +133 -0
@@ -32,6 +32,139 @@ void do_entry_int82(struct cpu_user_regs *regs) }/* +* This function emulates the behaviour of hardware when Xen needs to inject +* an event into into a guest. +* +* It may switch from user mode to kernel mode, will write an appropriate +* hardware exception frame (including Xen-specific extras), and alter the +* root stack frame to invoke the guest kernels correct entry point on exit +* from the hypervisor. +*/ +void pv_create_exception_frame (void ) +{ +struct vcpu *curr = current; +struct trap_bounce *tb = &curr->arch.pv_vcpu.trap_bounce; +struct cpu_user_regs *regs =guest_cpu_user_regs (); +const bool user_mode_frame = !guest_kernel_mode (curr, regs); +uint8_t *evt_mask = &vcpu_info (curr, evtchn_upcall_mask); +unsigned long rflags; +unsigned int bytes, missing; + +ASSERT_NOT_IN_ATOMIC (); + +if (unlikely (null_trap_bounce (curr, tb)) ) + { +gprintk (XENLOG_ERR ,"Fatal: Attempting to inject null trap bounce\n" ); +__domain_crash_synchronous (); + } + +/* Fold the upcall mask and architectural IOPL into the guests rflags. */ + rflags = regs->rflags & ~(X86_EFLAGS_IF |X86_EFLAGS_IOPL ); + rflags |= ((*evt_mask ?0 :X86_EFLAGS_IF ) | + (VM_ASSIST (curr->domain, architectural_iopl) + ? curr->arch.pv_vcpu.iopl :0 )); + +if (is_pv_32bit_vcpu (curr) ) + { +/* { [ERRCODE,] EIP, CS/MASK , EFLAGS, [ESP, SS] } */ +unsigned int frame[6 ], *ptr = frame, ksp = + (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->esp); + +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) + *ptr++ = tb->error_code; + + *ptr++ = regs->eip; + *ptr++ = regs->cs | (((unsigned int )*evt_mask) <<16 ); + *ptr++ = rflags; + +if ( user_mode_frame ) + { + *ptr++ = regs->esp; + *ptr++ = regs->ss; + } + +/* Copy the constructed frame to the guest kernel stack. */ + bytes =_p (ptr) -_p (frame); + ksp -= bytes; + +if (unlikely ((missing =__copy_to_user (_p (ksp), frame, bytes)) !=0 ) ) + { +gprintk (XENLOG_ERR ,"Fatal: Fault while writing exception frame\n" ); +show_page_walk (ksp + missing); +__domain_crash_synchronous (); + } + +/* Rewrite our stack frame. */ + regs->rip = (uint32_t )tb->eip; + regs->cs = tb->cs; + regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF | +X86_EFLAGS_NT |X86_EFLAGS_TF ); + regs->rsp = ksp; +if ( user_mode_frame ) + regs->ss = curr->arch.pv_vcpu.kernel_ss; + } +else + { +/* { RCX, R11, [ERRCODE,] RIP, CS/MASK, RFLAGS, RSP, SS } */ +unsigned long frame[7 ], *ptr = frame, ksp = + (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->rsp) & ~0xf ; + +if ( user_mode_frame ) +toggle_guest_mode (curr); + + *ptr++ = regs->rcx; + *ptr++ = regs->r11; + +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) + *ptr++ = tb->error_code; + + *ptr++ = regs->rip; + *ptr++ = (user_mode_frame ? regs->cs : regs->cs & ~3 ) | + ((unsigned long )(*evt_mask) <<32 ); + *ptr++ = rflags; + *ptr++ = regs->rsp; + *ptr++ = regs->ss; + +/* Copy the constructed frame to the guest kernel stack. */ + bytes =_p (ptr) -_p (frame); + ksp -= bytes; + +if (unlikely (!__addr_ok (ksp)) ) + { +gprintk (XENLOG_ERR ,"Fatal: Bad guest kernel stack %p\n" ,_p (ksp)); +__domain_crash_synchronous (); + } +else if (unlikely ((missing = +__copy_to_user (_p (ksp), frame, bytes)) !=0 ) ) + { +gprintk (XENLOG_ERR ,"Fatal: Fault while writing exception frame\n" ); +show_page_walk (ksp + missing); +__domain_crash_synchronous (); + } + +/* Rewrite our stack frame. */ + regs->entry_vector |= TRAP_syscall; + regs->rip = tb->eip; + regs->cs =FLAT_KERNEL_CS ; + regs->rflags &= ~(X86_EFLAGS_AC |X86_EFLAGS_VM |X86_EFLAGS_RF | +X86_EFLAGS_NT |X86_EFLAGS_TF ); + regs->rsp = ksp; + regs->ss =FLAT_KERNEL_SS ; + } + +/* Mask events if requested. */ +if ( tb->flags &TBF_INTERRUPT ) + *evt_mask =1 ; + +/* +* Clobber the injection information now it has been completed. Buggy +* attempts to inject the same event twice will hit the null_trap_bounce() +* check above. +*/ + *tb = (struct trap_bounce ){}; +} + +/* * Local variables: * mode: C * c-file-style: "BSD"
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 2/4] x86/pv: Introduce pv_create_exception_frame()
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 46
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/pv/traps.c +4
-0+void pv_create_exception_frame (void ) +{ +struct vcpu *curr = current; +struct trap_bounce *tb = &curr->arch.pv_vcpu.trap_bounce;
const (twice)?
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 50
Quoted 4 lines
xen/arch/x86/pv/traps.c +4
-0+struct cpu_user_regs *regs =guest_cpu_user_regs (); +const bool user_mode_frame = !guest_kernel_mode (curr, regs); +uint8_t *evt_mask = &vcpu_info (curr, evtchn_upcall_mask); +unsigned long rflags;
Does this really need to be "long"?
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 58
Quoted 8 lines
xen/arch/x86/pv/traps.c +8
-0+unsigned int bytes, missing; + +ASSERT_NOT_IN_ATOMIC (); + +if (unlikely (null_trap_bounce (curr, tb)) ) + { +gprintk (XENLOG_ERR ,"Fatal: Attempting to inject null trap bounce\n" ); +__domain_crash_synchronous ();
Why not domain_crash() followed by "return"?
Quoted 9 lines
xen/arch/x86/pv/traps.c +9
-0+ } + +/* Fold the upcall mask and architectural IOPL into the guests rflags. */ + rflags = regs->rflags & ~(X86_EFLAGS_IF |X86_EFLAGS_IOPL ); + rflags |= ((*evt_mask ?0 :X86_EFLAGS_IF ) | + (VM_ASSIST (curr->domain, architectural_iopl) + ? curr->arch.pv_vcpu.iopl :0 )); + +if (is_pv_32bit_vcpu (curr) )
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 77
Quoted 10 lines
xen/arch/x86/pv/traps.c +10
-0+ { +/* { [ERRCODE,] EIP, CS/MASK , EFLAGS, [ESP, SS] } */ +unsigned int frame[6 ], *ptr = frame, ksp = + (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->esp); + +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) + *ptr++ = tb->error_code; + + *ptr++ = regs->eip; + *ptr++ = regs->cs | (((unsigned int )*evt_mask) <<16 );
Do you really need the cast here? In no case is there a need for the parentheses around the cast expression.
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 90
Quoted 13 lines
xen/arch/x86/pv/traps.c +13
-0+ *ptr++ = rflags; + +if ( user_mode_frame ) + { + *ptr++ = regs->esp; + *ptr++ = regs->ss; + } + +/* Copy the constructed frame to the guest kernel stack. */ + bytes =_p (ptr) -_p (frame); + ksp -= bytes; + +if (unlikely ((missing =__copy_to_user (_p (ksp), frame, bytes)) !=0 ) )
While I don't think we need to be really bothered, it's perhaps still worth noting in a comment that the wrapping behavior here is wrong (and slightly worse than the assembly original), due to (implicit) address arithmetic all being done with 64-bit operands.
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 101
Quoted 11 lines
xen/arch/x86/pv/traps.c +11
-0+ { +gprintk (XENLOG_ERR ,"Fatal: Fault while writing exception frame\n" ); +show_page_walk (ksp + missing); +__domain_crash_synchronous (); + } + +/* Rewrite our stack frame. */ + regs->rip = (uint32_t )tb->eip; + regs->cs = tb->cs; + regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF | +X86_EFLAGS_NT |X86_EFLAGS_TF );
You write ->rip above and ->rsp below - preferably those would become ->eip and ->esp, but alternatively (for consistency) this may want switching to ->rflags.
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 109
Quoted 8 lines
xen/arch/x86/pv/traps.c +8
-0+ regs->rsp = ksp; +if ( user_mode_frame ) + regs->ss = curr->arch.pv_vcpu.kernel_ss; + } +else + { +/* { RCX, R11, [ERRCODE,] RIP, CS/MASK, RFLAGS, RSP, SS } */ + unsigned long frame[7], *ptr = frame, ksp =
I clearly count 8 elements in the comment.
Quoted 4 lines
xen/arch/x86/pv/traps.c +4
-0+ (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->rsp) & ~0xf ; + +if ( user_mode_frame ) +toggle_guest_mode (curr);
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 123
Quoted 10 lines
xen/arch/x86/pv/traps.c +10
-0+ + *ptr++ = regs->rcx; + *ptr++ = regs->r11; + +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) + *ptr++ = tb->error_code; + + *ptr++ = regs->rip; + *ptr++ = (user_mode_frame ? regs->cs : regs->cs & ~3 ) | + ((unsigned long )(*evt_mask) <<32 );
Stray parentheses again.
Quoted 31 lines
xen/arch/x86/pv/traps.c +31
-0+ *ptr++ = rflags; + *ptr++ = regs->rsp; + *ptr++ = regs->ss; + +/* Copy the constructed frame to the guest kernel stack. */ + bytes =_p (ptr) -_p (frame); + ksp -= bytes; + +if (unlikely (!__addr_ok (ksp)) ) + { +gprintk (XENLOG_ERR ,"Fatal: Bad guest kernel stack %p\n" ,_p (ksp)); +__domain_crash_synchronous (); + } +else if (unlikely ((missing = +__copy_to_user (_p (ksp), frame, bytes)) !=0 ) ) + { +gprintk (XENLOG_ERR ,"Fatal: Fault while writing exception frame\n" ); +show_page_walk (ksp + missing); +__domain_crash_synchronous (); + } + +/* Rewrite our stack frame. */ + regs->entry_vector |= TRAP_syscall; + regs->rip = tb->eip; + regs->cs =FLAT_KERNEL_CS ; + regs->rflags &= ~(X86_EFLAGS_AC |X86_EFLAGS_VM |X86_EFLAGS_RF | +X86_EFLAGS_NT |X86_EFLAGS_TF ); + regs->rsp = ksp; + regs->ss =FLAT_KERNEL_SS ; + } +
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 164
Quoted 10 lines
xen/arch/x86/pv/traps.c +10
-0+/* Mask events if requested. */ +if ( tb->flags &TBF_INTERRUPT ) + *evt_mask =1 ; + +/* +* Clobber the injection information now it has been completed. Buggy +* attempts to inject the same event twice will hit the null_trap_bounce() +* check above. +*/ + *tb = (struct trap_bounce ){};
Ah, so that prevents tb becoming a pointer to const. I wonder though whether, on a rather hot path, we really want to zap the entire structure here. As I can see the value in satisfying null_trap_bounce(), how about zapping just ->eip / ->cs on the split paths above?
Overall, did you compare generated code with the current assembly implementation? That one surely would have had some room for improvement, so the result here at least shouldn't be worse than that.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/4] x86/pv: Introduce pv_create_exception_frame()
Quoted 7 lines · Jan Beulich
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 46
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/pv/traps.c +4
-0+void pv_create_exception_frame (void ) +{ +struct vcpu *curr = current; +struct trap_bounce *tb = &curr->arch.pv_vcpu.trap_bounce;const (twice)?
Quoted 5 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 50
Quoted 4 lines
xen/arch/x86/pv/traps.c +4
-0+struct cpu_user_regs *regs =guest_cpu_user_regs (); +const bool user_mode_frame = !guest_kernel_mode (curr, regs); +uint8_t *evt_mask = &vcpu_info (curr, evtchn_upcall_mask); +unsigned long rflags;Does this really need to be "long"?
The answer to several of these questions are "probably not, but that's how load_segments() did it".
Quoted 10 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 58
Quoted 9 lines
xen/arch/x86/pv/traps.c +8
-0+unsigned int bytes, missing; + +ASSERT_NOT_IN_ATOMIC (); + +if (unlikely (null_trap_bounce (curr, tb)) ) + { +gprintk (XENLOG_ERR ,"Fatal: Attempting to inject null trap bounce\n" ); +__domain_crash_synchronous ();Why not
domain_crash()followed by "return"?
Because the existing code uses synchronous crashes.
Looking again at the callsites of pv_create_exception_frame(), we immediately jump back to {compat_,}test_all_events, which proceeds to run softirqs again.
Therefore, domain_crash() and a return should work. (I think?)
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/pv/traps.c +12
-0+ } + +/* Fold the upcall mask and architectural IOPL into the guests rflags. */ + rflags = regs->rflags & ~(X86_EFLAGS_IF |X86_EFLAGS_IOPL ); + rflags |= ((*evt_mask ?0 :X86_EFLAGS_IF ) | + (VM_ASSIST (curr->domain, architectural_iopl) + ? curr->arch.pv_vcpu.iopl :0 )); + +if (is_pv_32bit_vcpu (curr) ) + { +/* { [ERRCODE,] EIP, CS/MASK , EFLAGS, [ESP, SS] } */ + unsigned int frame[6], *ptr = frame, ksp =
Quoted 8 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 77
Quoted 7 lines
xen/arch/x86/pv/traps.c +7
-0+ (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->esp); + +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) + *ptr++ = tb->error_code; + + *ptr++ = regs->eip; + *ptr++ = regs->cs | (((unsigned int )*evt_mask) <<16 );Do you really need the cast here?
Does it promote correctly if the top bit of the mask is set?
Quoted 12 lines
In no case is there a need for the
parentheses around the cast expression.Quoted 9 lines
xen/arch/x86/pv/traps.c +9
-0+ *ptr++ = rflags; + +if ( user_mode_frame ) + { + *ptr++ = regs->esp; + *ptr++ = regs->ss; + } + +/* Copy the constructed frame to the guest kernel stack. */
Quoted 8 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 90
Quoted 4 lines
xen/arch/x86/pv/traps.c +4
-0+ bytes =_p (ptr) -_p (frame); + ksp -= bytes; + +if (unlikely ((missing =__copy_to_user (_p (ksp), frame, bytes)) !=0 ) )While I don't think we need to be really bothered, it's perhaps still worth noting in a comment that the wrapping behavior here is wrong (and slightly worse than the assembly original), due to (implicit) address arithmetic all being done with 64-bit operands.
Ah - At some point, I had a comment here explaining the lack of an __access_ok() check, but it appears to have got lost in a rebase. I will try to reinstate it.
The wrapping behaviour around the 4GB => 0 boundary is undefined, and different between Intel and AMD (as we discovered with XSA-186). If we passing the exception back to the guest we would need to swap #PF for #SS (for Intel), or properly wrap around (for AMD).
Would it be ok just to comment this point and leave it as is?
Quoted 12 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 101
Quoted 12 lines
xen/arch/x86/pv/traps.c +11
-0+ { +gprintk (XENLOG_ERR ,"Fatal: Fault while writing exception frame\n" ); +show_page_walk (ksp + missing); +__domain_crash_synchronous (); + } + +/* Rewrite our stack frame. */ + regs->rip = (uint32_t )tb->eip; + regs->cs = tb->cs; + regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF | +X86_EFLAGS_NT |X86_EFLAGS_TF );
Quoted 3 lines
You write ->rip above and ->rsp below - preferably those would become ->eip and ->esp, but alternatively (for consistency) this may want switching to ->rflags.
Ah - these are deliberately 64bit values even in the 32bit path, so a 32bit guest with an unexpected 64bit code segment will be truncated back into its own range.
I will comment this point, and switch to using rflags.
Quoted 10 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 109
Quoted 9 lines
xen/arch/x86/pv/traps.c +8
-0+ regs->rsp = ksp; +if ( user_mode_frame ) + regs->ss = curr->arch.pv_vcpu.kernel_ss; + } +else + { +/* { RCX, R11, [ERRCODE,] RIP, CS/MASK, RFLAGS, RSP, SS } */ + unsigned long frame[7], *ptr = frame, ksp =I clearly count 8 elements in the comment.
:)
Quoted 55 lines
Quoted 5 lines
xen/arch/x86/pv/traps.c +4
-0+ (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->rsp) & ~0xf ; + +if ( user_mode_frame ) +toggle_guest_mode (curr);xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 123
Quoted 10 lines
xen/arch/x86/pv/traps.c +10
-0+ + *ptr++ = regs->rcx; + *ptr++ = regs->r11; + +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) + *ptr++ = tb->error_code; + + *ptr++ = regs->rip; + *ptr++ = (user_mode_frame ? regs->cs : regs->cs & ~3 ) | + ((unsigned long )(*evt_mask) <<32 );Stray parentheses again.
Quoted 38 lines
xen/arch/x86/pv/traps.c +38
-0+ *ptr++ = rflags; + *ptr++ = regs->rsp; + *ptr++ = regs->ss; + +/* Copy the constructed frame to the guest kernel stack. */ + bytes =_p (ptr) -_p (frame); + ksp -= bytes; + +if (unlikely (!__addr_ok (ksp)) ) + { +gprintk (XENLOG_ERR ,"Fatal: Bad guest kernel stack %p\n" ,_p (ksp)); +__domain_crash_synchronous (); + } +else if (unlikely ((missing = +__copy_to_user (_p (ksp), frame, bytes)) !=0 ) ) + { +gprintk (XENLOG_ERR ,"Fatal: Fault while writing exception frame\n" ); +show_page_walk (ksp + missing); +__domain_crash_synchronous (); + } + +/* Rewrite our stack frame. */ + regs->entry_vector |= TRAP_syscall; + regs->rip = tb->eip; + regs->cs =FLAT_KERNEL_CS ; + regs->rflags &= ~(X86_EFLAGS_AC |X86_EFLAGS_VM |X86_EFLAGS_RF | +X86_EFLAGS_NT |X86_EFLAGS_TF ); + regs->rsp = ksp; + regs->ss =FLAT_KERNEL_SS ; + } + +/* Mask events if requested. */ +if ( tb->flags &TBF_INTERRUPT ) + *evt_mask =1 ; + +/* +* Clobber the injection information now it has been completed. Buggy +* attempts to inject the same event twice will hit the null_trap_bounce()
Quoted 8 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 164
Quoted 3 lines
xen/arch/x86/pv/traps.c +3
-0+* check above. +*/ + *tb = (struct trap_bounce ){};Ah, so that prevents tb becoming a pointer to const. I wonder though whether, on a rather hot path, we really want to zap the entire structure here. As I can see the value in satisfying
null_trap_bounce(), how about zapping just ->eip / ->cs on the split paths above?
This ends up being two 8-byte writes of zeroes into a cache-hot line; it isn't by any means a slow part of this path, whereas the 16bit write to clobber just %cs would be.
Irrespective of that, the following patch depends on this clobbering of ->flags.
Quoted 4 lines
Overall, did you compare generated code with the current assembly implementation? That one surely would have had some room for improvement, so the result here at least shouldn't be worse than that.
The final C version (including failsafe, and some error handling the asm functions didn't have) is a bit less than twice the size of the asm functions in terms of absolute size.
I haven't done any performance analysis, but I trust the compiler to make better code overall (there are definitely pipeline stalls in the asm versions), and wouldn't be surprised if it is faster to execute.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/4] x86/pv: Introduce pv_create_exception_frame()
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 09.05.17 at 19:09, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 46
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/pv/traps.c +4
-0+void pv_create_exception_frame (void ) +{ +struct vcpu *curr = current; +struct trap_bounce *tb = &curr->arch.pv_vcpu.trap_bounce;const (twice)?
Quoted 8 lines
Quoted 5 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 50
Quoted 4 lines
xen/arch/x86/pv/traps.c +4
-0+struct cpu_user_regs *regs =guest_cpu_user_regs (); +const bool user_mode_frame = !guest_kernel_mode (curr, regs); +uint8_t *evt_mask = &vcpu_info (curr, evtchn_upcall_mask); +unsigned long rflags;Does this really need to be "long"?
The answer to several of these questions are "probably not, but that's how
load_segments()did it".
Since you overhaul it, no reason to not do sensible adjustments, I would think.
Quoted 10 lines
Quoted 9 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 58
Quoted 8 lines
xen/arch/x86/pv/traps.c +8
-0+unsigned int bytes, missing; + +ASSERT_NOT_IN_ATOMIC (); + +if (unlikely (null_trap_bounce (curr, tb)) ) + { +gprintk (XENLOG_ERR ,"Fatal: Attempting to inject null trap bounce\n" ); +__domain_crash_synchronous ();Why not
domain_crash()followed by "return"?
Quoted 7 lines
Because the existing code uses synchronous crashes.
Looking again at the callsites of
pv_create_exception_frame(), we immediately jump back to {compat_,}test_all_events, which proceeds to run softirqs again.Therefore,
domain_crash()and a return should work. (I think?)
I think so too.
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/x86/pv/traps.c +12
-0+ } + +/* Fold the upcall mask and architectural IOPL into the guests rflags. */ + rflags = regs->rflags & ~(X86_EFLAGS_IF |X86_EFLAGS_IOPL ); + rflags |= ((*evt_mask ?0 :X86_EFLAGS_IF ) | + (VM_ASSIST (curr->domain, architectural_iopl) + ? curr->arch.pv_vcpu.iopl :0 )); + +if (is_pv_32bit_vcpu (curr) ) + { +/* { [ERRCODE,] EIP, CS/MASK , EFLAGS, [ESP, SS] } */ + unsigned int frame[6], *ptr = frame, ksp =Quoted 8 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 77
Quoted 7 lines
xen/arch/x86/pv/traps.c +7
-0+ (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->esp); + +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) + *ptr++ = tb->error_code; + + *ptr++ = regs->eip; + *ptr++ = regs->cs | (((unsigned int )*evt_mask) <<16 );Do you really need the cast here?
Quoted 1 line
Does it promote correctly if the top bit of the mask is set?
It's a uint8_t -> plain int conversion, which is a zero-extension.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/pv/traps.c +9
-0+ *ptr++ = rflags; + +if ( user_mode_frame ) + { + *ptr++ = regs->esp; + *ptr++ = regs->ss; + } + +/* Copy the constructed frame to the guest kernel stack. */ Quoted 8 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 90
Quoted 4 lines
xen/arch/x86/pv/traps.c +4
-0+ bytes =_p (ptr) -_p (frame); + ksp -= bytes; + +if (unlikely ((missing =__copy_to_user (_p (ksp), frame, bytes)) !=0 ) )While I don't think we need to be really bothered, it's perhaps still worth noting in a comment that the wrapping behavior here is wrong (and slightly worse than the assembly original), due to (implicit) address arithmetic all being done with 64-bit operands.
Quoted 3 lines
Ah - At some point, I had a comment here explaining the lack of an
__access_ok()check, but it appears to have got lost in a rebase. I will try to reinstate it.
Just to avoid misunderstandings - if anything other than a comment, it ought to be __compat_access_ok().
Quoted 6 lines
The wrapping behaviour around the 4GB => 0 boundary is undefined, and different between Intel and AMD (as we discovered with XSA-186). If we passing the exception back to the guest we would need to swap #PF for #SS (for Intel), or properly wrap around (for AMD).
Would it be ok just to comment this point and leave it as is?
Sure - that's what I did ask for.
Quoted 12 lines
Quoted 11 lines
xen/arch/x86/pv/traps.c void do_entry_int82(struct cpu_user_regs *regs) · line 164
Quoted 6 lines
xen/arch/x86/pv/traps.c +6
-0+/* +* Clobber the injection information now it has been completed. Buggy +* attempts to inject the same event twice will hit the null_trap_bounce() +* check above. +*/ + *tb = (struct trap_bounce ){};Ah, so that prevents tb becoming a pointer to const. I wonder though whether, on a rather hot path, we really want to zap the entire structure here. As I can see the value in satisfying
null_trap_bounce(), how about zapping just ->eip / ->cs on the split paths above?
Quoted 6 lines
This ends up being two 8-byte writes of zeroes into a cache-hot line; it isn't by any means a slow part of this path, whereas the 16bit write to clobber just %cs would be.
Irrespective of that, the following patch depends on this clobbering of ->flags.
Right, I did see this. Keep it as it is then.
Quoted 8 lines
Quoted 4 lines
Overall, did you compare generated code with the current assembly implementation? That one surely would have had some room for improvement, so the result here at least shouldn't be worse than that.
The final C version (including failsafe, and some error handling the asm functions didn't have) is a bit less than twice the size of the asm functions in terms of absolute size.
Size alone is no meaningful criteria anyway.
Quoted 3 lines
I haven't done any performance analysis, but I trust the compiler to make better code overall (there are definitely pipeline stalls in the asm versions), and wouldn't be surprised if it is faster to execute.
That's the hope, but if it was double the number on instructions on the common path (we can surely leave error paths out of consideration), I wouldn't be convinced these are faster to execute than the asm code with a couple of stalls (some of which we surely could take care of). If this wasn't on some highly critical paths, I wouldn't be worried, but this code really needs to be fast (besides the very reasonable desire of making it better maintainable). For example, at this point I'm not (yet) convinced making this a single function serving both 64- and 32-bit guests is really the right choice, even if it's only one or two extra conditionals.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 3/4 x86/pv: Drop {compat_, }create_bounce_frame() and use the C version instead
In reply to Andrew CooperThe clobbering of TRAPBOUNCE_flags in .L{compat_}bounce_exception is subsumed by the logic at the end of pv_create_bounce_frame().
This cleanup removes all callers of asm_domain_crash_synchronous(), which is therefore dropped as well.
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/traps.c | 23 ------ xen/arch/x86/x86_64/compat/entry.S | 116 ++---------------------------- xen/arch/x86/x86_64/entry.S | 141 ++----------------------------------- xen/include/xen/sched.h | 7 -- 4 files changed, 10 insertions(+), 277 deletions(-)
xen/arch/x86/traps.c +0 -23
@@ -4195,29 +4195,6 @@ unsigned long do_get_debugreg(int reg)return -EINVAL ; } -void asm_domain_crash_synchronous (unsigned long addr) -{ -/* -* We need clear AC bit here because in entry.S AC is set -* by ASM_STAC to temporarily allow accesses to user pages -* which is prevented by SMAP by default. -* -* For some code paths, where this function is called, clac() -* is not needed, but adding clac() here instead of each place -* asm_domain_crash_synchronous() is called can reduce the code -* redundancy, and it is harmless as well. -*/ -clac (); - -if ( addr ==0 ) - addr =this_cpu (last_extable_addr); - -printk ("domain_crash_sync called from entry.S: fault at %p %pS\n" , -_p (addr),_p (addr)); - -__domain_crash_synchronous (); -} -/* * Local variables: * mode: C
xen/arch/x86/x86_64/compat/entry.S +5 -111
142 lines not on this page · raw message
xen/arch/x86/x86_64/entry.S +5 -136
177 lines not on this page · raw message
xen/include/xen/sched.h +0 -7
@@ -632,13 +632,6 @@ void noreturn __domain_crash_synchronous(void);__domain_crash_synchronous (); \ } while (0) -/* -* Called from assembly code, with an optional address to help indicate why -* the crash occured. If addr is 0, look up address from last extable -* redirection. -*/ -void noreturn asm_domain_crash_synchronous(unsigned long addr); -#define set_current_state (_s) do { current->state = (_s); } while (0)void scheduler_init (void );int sched_init_vcpu (struct vcpu *v,unsigned int processor);
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 3/4] x86/pv: Drop {compat_, }create_bounce_frame() and use the C version instead
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/x86_64/compat/entry.S +1
-1@@ -51,7 +51,7 @@ compat_test_guest_events: movl VCPU_event_sel(%rbx),%eax movw %ax,TRAPBOUNCE_cs(%rdx) movb $TBF_INTERRUPT,TRAPBOUNCE_flags(%rdx) - call compat_create_bounce_frame + call pv_create_exception_frame jmp compat_test_all_events ALIGN
xen/arch/x86/x86_64/compat/entry.S compat_process_nmi: · line 99
Quoted 6 lines
xen/arch/x86/x86_64/compat/entry.S +1
-1@@ -95,7 +95,7 @@ compat_process_nmi: /* FALLTHROUGH */ compat_process_trap: leaq VCPU_trap_bounce(%rbx),%rdx - call compat_create_bounce_frame + call pv_create_exception_frame jmp compat_test_all_events
The leaq should go away then too.
xen/arch/x86/x86_64/compat/entry.S ENTRY(compat_post_handle_exception) · line 185
Quoted 8 lines
xen/arch/x86/x86_64/compat/entry.S +1
-2@@ -181,8 +181,7 @@ ENTRY(compat_post_handle_exception) testb $TBF_EXCEPTION,TRAPBOUNCE_flags(%rdx) jz compat_test_all_events .Lcompat_bounce_exception: - call compat_create_bounce_frame - movb $0,TRAPBOUNCE_flags(%rdx) + call pv_create_exception_frame jmp compat_test_all_events
Considering this recurring pattern of call/jmp I wonder whether we could reduce the branch tracking structure utilization in the CPU a little by folding these paths.
Quoted 21 lines
xen/arch/x86/x86_64/entry.S +2
-2@@ -28,7 +28,7 @@ ENTRY(switch_to_kernel) setc %cl leal (,%rcx,TBF_INTERRUPT),%ecx movb %cl,TRAPBOUNCE_flags(%rdx) - call create_bounce_frame + call pv_create_exception_frame andl $~X86_EFLAGS_DF,UREGS_eflags(%rsp) jmp test_all_events@@ -131,7 +131,7 @@ test_guest_events: movq VCPU_event_addr(%rbx),%rax movq %rax,TRAPBOUNCE_eip(%rdx) movb $TBF_INTERRUPT,TRAPBOUNCE_flags(%rdx) - call create_bounce_frame + call pv_create_exception_frame jmp test_all_events ALIGN
xen/arch/x86/x86_64/entry.S process_nmi: · line 179
Quoted 6 lines
xen/arch/x86/x86_64/entry.S +1
-1@@ -175,7 +175,7 @@ process_nmi: /* FALLTHROUGH */ process_trap: leaq VCPU_trap_bounce(%rbx),%rdx - call create_bounce_frame + call pv_create_exception_frame jmp test_all_events
Leftover leaq again.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 3/4] x86/pv: Drop {compat_, }create_bounce_frame() and use the C version instead
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/x86_64/compat/entry.S +1
-1@@ -51,7 +51,7 @@ compat_test_guest_events: movl VCPU_event_sel(%rbx),%eax movw %ax,TRAPBOUNCE_cs(%rdx) movb $TBF_INTERRUPT,TRAPBOUNCE_flags(%rdx) - call compat_create_bounce_frame + call pv_create_exception_frame jmp compat_test_all_events
Quoted 9 lines
xen/arch/x86/x86_64/compat/entry.S compat_process_nmi: · line 99
Quoted 8 lines
xen/arch/x86/x86_64/compat/entry.S +1
-1ALIGN@@ -95,7 +95,7 @@ compat_process_nmi: /* FALLTHROUGH */ compat_process_trap: leaq VCPU_trap_bounce(%rbx),%rdx - call compat_create_bounce_frame + call pv_create_exception_frame jmp compat_test_all_eventsThe leaq should go away then too.
So it can.
Quoted 12 lines
xen/arch/x86/x86_64/compat/entry.S ENTRY(compat_post_handle_exception) · line 185
Quoted 9 lines
xen/arch/x86/x86_64/compat/entry.S +1
-2@@ -181,8 +181,7 @@ ENTRY(compat_post_handle_exception) testb $TBF_EXCEPTION,TRAPBOUNCE_flags(%rdx) jz compat_test_all_events .Lcompat_bounce_exception: - call compat_create_bounce_frame - movb $0,TRAPBOUNCE_flags(%rdx) + call pv_create_exception_frame jmp compat_test_all_eventsConsidering this recurring pattern of call/jmp I wonder whether we could reduce the branch tracking structure utilization in the CPU a little by folding these paths.
I think we can lift all of this softirq/event/nmi/mce handling logic up into C, which will remove almost all of the guest-handling asm code in entry.S
However, it would still retain this "goto again" structure, and will eventually have to drop back into asm to actually return to the guest.
The problem (which I haven't got a good solution for yet) is that, if I make the C functions noreturn, I will need to duplicate them so they can jmp to the proper return point. The alternative would be a moderately hard to predict conditional jump.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 3/4] x86/pv: Drop {compat_, }create_bounce_frame() and use the C version instead
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 09.05.17 at 19:24, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
xen/arch/x86/x86_64/compat/entry.S ENTRY(compat_post_handle_exception) · line 185
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/x86_64/compat/entry.S +1
-2@@ -181,8 +181,7 @@ ENTRY(compat_post_handle_exception) testb $TBF_EXCEPTION,TRAPBOUNCE_flags(%rdx) jz compat_test_all_events .Lcompat_bounce_exception: - call compat_create_bounce_frame - movb $0,TRAPBOUNCE_flags(%rdx) + call pv_create_exception_frame jmp compat_test_all_eventsConsidering this recurring pattern of call/jmp I wonder whether we could reduce the branch tracking structure utilization in the CPU a little by folding these paths.
I think we can lift all of this softirq/event/nmi/mce handling logic up into C, which will remove almost all of the guest-handling asm code in
entry.S
Quoted 7 lines
However, it would still retain this "goto again" structure, and will eventually have to drop back into asm to actually return to the guest.
The problem (which I haven't got a good solution for yet) is that, if I make the C functions noreturn, I will need to duplicate them so they can jmp to the proper return point. The alternative would be a moderately hard to predict conditional jump.
Quite likely it would be possible to avoid source level duplication, having the compiler produce two (or more) variants distinguished mainly by that final jump. Question is whether the extra cache (and to a lesser degree TLB) pressure wouldn't be worse than a frequently mis-predicted branch. Another option might be to use an indirect branch instead, making sure this uses a register operand, with the involved register loaded well ahead of its use.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 4/4 x86/pv: Implement the failsafe callback using the general path
In reply to Andrew CooperReintroduce TBF_FAILSAFE and update pv_create_exception_frame() to cope with the additional data segment registers.
load_segments() now fills in trap_bounce, and lets the general return-to-guest path inject the exception.
Bloat-o-meter reports:
add/remove: 0/0 grow/shrink: 1/1 up/down: 123/-2522 (-2399)
function old new delta
pv_create_exception_frame 1088 1211 +123 context_switch 3565 1043 -2522
which I suspect is largely due to the quantity of code hidden behind put_user().
---
CC: Jan Beulich <[email protected]>
--- xen/arch/x86/domain.c | 100 +++------------------------------------- xen/arch/x86/pv/traps.c | 31 ++++++++++--- xen/include/asm-x86/processor.h | 1 + 3 files changed, 33 insertions(+), 99 deletions(-)
xen/arch/x86/domain.c +7 -93
@@ -1275,100 +1275,14 @@ static void load_segments(struct vcpu *n)if (unlikely (!all_segs_okay) ) { -struct pv_vcpu *pv = &n->arch.pv_vcpu; -struct cpu_user_regs *regs =guest_cpu_user_regs (); -unsigned long *rsp = - (unsigned long *)(((n->arch.flags & TF_kernel_mode) - ? regs->rsp : pv->kernel_sp) & ~0xf ); -unsigned long cs_and_mask, rflags; - -/* Fold upcall mask and architectural IOPL into RFLAGS.IF. */ - rflags = regs->rflags & ~(X86_EFLAGS_IF |X86_EFLAGS_IOPL ); - rflags |= !vcpu_info (n, evtchn_upcall_mask) <<9 ; -if (VM_ASSIST (n->domain, architectural_iopl) ) - rflags |= n->arch.pv_vcpu.iopl; - -if (is_pv_32bit_vcpu (n) ) - { -unsigned int *esp =ring_1 (regs) ? - (unsigned int *)regs->rsp : - (unsigned int *)pv->kernel_sp; -int ret =0 ; - -/* CS longword also contains full evtchn_upcall_mask. */ - cs_and_mask = (unsigned short )regs->cs | - ((unsigned int )vcpu_info (n, evtchn_upcall_mask) <<16 ); - -if ( !ring_1 (regs) ) - { - ret =put_user (regs->ss, esp-1 ); - ret |=put_user (regs->esp, esp-2 ); - esp -=2 ; - } - -if ( ret | -put_user (rflags, esp-1 ) | -put_user (cs_and_mask, esp-2 ) | -put_user (regs->eip, esp-3 ) | -put_user (uregs->gs, esp-4 ) | -put_user (uregs->fs, esp-5 ) | -put_user (uregs->es, esp-6 ) | -put_user (uregs->ds, esp-7 ) ) - { -gprintk (XENLOG_ERR , -"error while creating compat failsafe callback frame\n" ); -domain_crash (n->domain); - } +bool disable = n->arch.vgc_flags & VGCF_failsafe_disables_events; -if ( n->arch.vgc_flags & VGCF_failsafe_disables_events ) -vcpu_info (n, evtchn_upcall_mask) =1 ; - - regs->entry_vector |= TRAP_syscall; - regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF |X86_EFLAGS_NT | -X86_EFLAGS_IOPL |X86_EFLAGS_TF ); - regs->ss =FLAT_COMPAT_KERNEL_SS ; - regs->esp = (unsigned long )(esp-7 ); - regs->cs =FLAT_COMPAT_KERNEL_CS ; - regs->eip = pv->failsafe_callback_eip; -return ; - } - -if ( !(n->arch.flags & TF_kernel_mode) ) -toggle_guest_mode (n); -else - regs->cs &= ~3 ; - -/* CS longword also contains full evtchn_upcall_mask. */ - cs_and_mask = (unsigned long )regs->cs | - ((unsigned long )vcpu_info (n, evtchn_upcall_mask) <<32 ); - -if (put_user (regs->ss, rsp-1 ) | -put_user (regs->rsp, rsp-2 ) | -put_user (rflags, rsp-3 ) | -put_user (cs_and_mask, rsp-4 ) | -put_user (regs->rip, rsp-5 ) | -put_user (uregs->gs, rsp-6 ) | -put_user (uregs->fs, rsp-7 ) | -put_user (uregs->es, rsp-8 ) | -put_user (uregs->ds, rsp-9 ) | -put_user (regs->r11, rsp-10 ) | -put_user (regs->rcx, rsp-11 ) ) - { -gprintk (XENLOG_ERR , -"error while creating failsafe callback frame\n" ); -domain_crash (n->domain); - } - -if ( n->arch.vgc_flags & VGCF_failsafe_disables_events ) -vcpu_info (n, evtchn_upcall_mask) =1 ; - - regs->entry_vector |= TRAP_syscall; - regs->rflags &= ~(X86_EFLAGS_AC |X86_EFLAGS_VM |X86_EFLAGS_RF | -X86_EFLAGS_NT |X86_EFLAGS_IOPL |X86_EFLAGS_TF ); - regs->ss =FLAT_KERNEL_SS ; - regs->rsp = (unsigned long )(rsp-11 ); - regs->cs =FLAT_KERNEL_CS ; - regs->rip = pv->failsafe_callback_eip; + n->arch.pv_vcpu.trap_bounce = (struct trap_bounce ){ + .flags = (TBF_FAILSAFE |TBF_EXCEPTION | + (disable ?TBF_INTERRUPT :0 )), + .cs =FLAT_COMPAT_KERNEL_CS ,/* Ignored for 64bit guests. */ + .eip = n->arch.pv_vcpu.failsafe_callback_eip + }; } }
xen/arch/x86/pv/traps.c +25 -6
@@ -44,7 +44,8 @@ void pv_create_exception_frame(void) {struct vcpu *curr = current;struct trap_bounce *tb = &curr->arch.pv_vcpu.trap_bounce; -struct cpu_user_regs *regs =guest_cpu_user_regs (); +struct cpu_user_regs *regs =guest_cpu_user_regs (), + *uregs = &curr->arch.user_regs;const bool user_mode_frame = !guest_kernel_mode (curr, regs);uint8_t *evt_mask = &vcpu_info (curr, evtchn_upcall_mask);unsigned long rflags;
@@ -66,10 +67,18 @@ void pv_create_exception_frame(void)if (is_pv_32bit_vcpu (curr) ) { -/* { [ERRCODE,] EIP, CS/MASK , EFLAGS, [ESP, SS] } */ -unsigned int frame[6 ], *ptr = frame, ksp = +/* { [DS-GS,] [ERRCODE,] EIP, CS/MASK , EFLAGS, [ESP, SS] } */ +unsigned int frame[10 ], *ptr = frame, ksp = (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->esp); +if ( tb->flags &TBF_FAILSAFE ) + { + *ptr++ = uregs->ds; + *ptr++ = uregs->es; + *ptr++ = uregs->fs; + *ptr++ = uregs->gs; + } +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) *ptr++ = tb->error_code;
@@ -100,13 +109,15 @@ void pv_create_exception_frame(void) regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF |X86_EFLAGS_NT |X86_EFLAGS_TF ); regs->rsp = ksp; -if ( user_mode_frame ) +if ( tb->flags &TBF_FAILSAFE ) + regs->ss =FLAT_COMPAT_KERNEL_SS ; +else if ( user_mode_frame ) regs->ss = curr->arch.pv_vcpu.kernel_ss; }else { -/* { RCX, R11, [ERRCODE,] RIP, CS/MASK, RFLAGS, RSP, SS } */ -unsigned long frame[7 ], *ptr = frame, ksp = +/* { RCX, R11, [DS-GS,] [ERRCODE,] RIP, CS/MASK, RFLAGS, RSP, SS } */ +unsigned long frame[11 ], *ptr = frame, ksp = (user_mode_frame ? curr->arch.pv_vcpu.kernel_sp : regs->rsp) & ~0xf ;if ( user_mode_frame )
@@ -115,6 +126,14 @@ void pv_create_exception_frame(void) *ptr++ = regs->rcx; *ptr++ = regs->r11; +if ( tb->flags &TBF_FAILSAFE ) + { + *ptr++ = uregs->ds; + *ptr++ = uregs->es; + *ptr++ = uregs->fs; + *ptr++ = uregs->gs; + } +if ( tb->flags &TBF_EXCEPTION_ERRCODE ) *ptr++ = tb->error_code;
xen/include/asm-x86/processor.h +1 -0
@@ -60,6 +60,7 @@/* 'trap_bounce' flags values */ #define TBF_EXCEPTION 1#define TBF_EXCEPTION_ERRCODE 2 +#define TBF_FAILSAFE 4#define TBF_INTERRUPT 8/* 'arch_vcpu' flags values */
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 4/4] x86/pv: Implement the failsafe callback using the general path
Quoted 95 lines
Quoted 1 line
Quoted 1 line · On 08.05.17 at 17:48, <[email protected]> wrote:
xen/arch/x86/domain.c +1
-86@@ -1275,100 +1275,14 @@ static void load_segments(struct vcpu *n)if (unlikely (!all_segs_okay) ) { -struct pv_vcpu *pv = &n->arch.pv_vcpu; -struct cpu_user_regs *regs =guest_cpu_user_regs (); -unsigned long *rsp = - (unsigned long *)(((n->arch.flags & TF_kernel_mode) - ? regs->rsp : pv->kernel_sp) & ~0xf ); -unsigned long cs_and_mask, rflags; - -/* Fold upcall mask and architectural IOPL into RFLAGS.IF. */ - rflags = regs->rflags & ~(X86_EFLAGS_IF |X86_EFLAGS_IOPL ); - rflags |= !vcpu_info (n, evtchn_upcall_mask) <<9 ; -if (VM_ASSIST (n->domain, architectural_iopl) ) - rflags |= n->arch.pv_vcpu.iopl; - -if (is_pv_32bit_vcpu (n) ) - { -unsigned int *esp =ring_1 (regs) ? - (unsigned int *)regs->rsp : - (unsigned int *)pv->kernel_sp; -int ret =0 ; - -/* CS longword also contains full evtchn_upcall_mask. */ - cs_and_mask = (unsigned short )regs->cs | - ((unsigned int )vcpu_info (n, evtchn_upcall_mask) <<16 ); - -if ( !ring_1 (regs) ) - { - ret =put_user (regs->ss, esp-1 ); - ret |=put_user (regs->esp, esp-2 ); - esp -=2 ; - } - -if ( ret | -put_user (rflags, esp-1 ) | -put_user (cs_and_mask, esp-2 ) | -put_user (regs->eip, esp-3 ) | -put_user (uregs->gs, esp-4 ) | -put_user (uregs->fs, esp-5 ) | -put_user (uregs->es, esp-6 ) | -put_user (uregs->ds, esp-7 ) ) - { -gprintk (XENLOG_ERR , -"error while creating compat failsafe callback frame\n" ); -domain_crash (n->domain); - } +bool disable = n->arch.vgc_flags & VGCF_failsafe_disables_events; -if ( n->arch.vgc_flags & VGCF_failsafe_disables_events ) -vcpu_info (n, evtchn_upcall_mask) =1 ; - - regs->entry_vector |= TRAP_syscall; - regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF |X86_EFLAGS_NT | -X86_EFLAGS_IOPL |X86_EFLAGS_TF ); - regs->ss =FLAT_COMPAT_KERNEL_SS ; - regs->esp = (unsigned long )(esp-7 ); - regs->cs =FLAT_COMPAT_KERNEL_CS ; - regs->eip = pv->failsafe_callback_eip; -return ; - } - -if ( !(n->arch.flags & TF_kernel_mode) ) -toggle_guest_mode (n); -else - regs->cs &= ~3 ; - -/* CS longword also contains full evtchn_upcall_mask. */ - cs_and_mask = (unsigned long )regs->cs | - ((unsigned long )vcpu_info (n, evtchn_upcall_mask) <<32 ); - -if (put_user (regs->ss, rsp-1 ) | -put_user (regs->rsp, rsp-2 ) | -put_user (rflags, rsp-3 ) | -put_user (cs_and_mask, rsp-4 ) | -put_user (regs->rip, rsp-5 ) | -put_user (uregs->gs, rsp-6 ) | -put_user (uregs->fs, rsp-7 ) | -put_user (uregs->es, rsp-8 ) | -put_user (uregs->ds, rsp-9 ) | -put_user (regs->r11, rsp-10 ) | -put_user (regs->rcx, rsp-11 ) ) - { -gprintk (XENLOG_ERR , -"error while creating failsafe callback frame\n" ); -domain_crash (n->domain); - } - -if ( n->arch.vgc_flags & VGCF_failsafe_disables_events ) -vcpu_info (n, evtchn_upcall_mask) =1 ; -
xen/arch/x86/domain.c static void load_segments(struct vcpu *n) · line 1282
Quoted 10 lines
xen/arch/x86/domain.c +3
-7- regs->entry_vector |= TRAP_syscall; - regs->rflags &= ~(X86_EFLAGS_AC |X86_EFLAGS_VM |X86_EFLAGS_RF | -X86_EFLAGS_NT |X86_EFLAGS_IOPL |X86_EFLAGS_TF ); - regs->ss =FLAT_KERNEL_SS ; - regs->rsp = (unsigned long )(rsp-11 ); - regs->cs =FLAT_KERNEL_CS ; - regs->rip = pv->failsafe_callback_eip; + n->arch.pv_vcpu.trap_bounce = (struct trap_bounce ){ + .flags = (TBF_FAILSAFE |TBF_EXCEPTION | + (disable ?TBF_INTERRUPT :0 )),
Pointless outer parentheses.
xen/arch/x86/pv/traps.c void pv_create_exception_frame(void) · line 48
Quoted 9 lines
xen/arch/x86/pv/traps.c +2
-1@@ -44,7 +44,8 @@ void pv_create_exception_frame(void) {struct vcpu *curr = current;struct trap_bounce *tb = &curr->arch.pv_vcpu.trap_bounce; -struct cpu_user_regs *regs =guest_cpu_user_regs (); +struct cpu_user_regs *regs =guest_cpu_user_regs (), + *uregs = &curr->arch.user_regs;
These should be const, even if this requires a fully separate declaration.
With these taken care of (and suitable re-basing over changes to earlier patches),
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel