x86/PV: avoid speculation abuse through guest accessors plus ...
cover x86/PV: avoid speculation abuse through guest accessors plus ...
... shadow adjustments towards not building 2- and 3-level code when !HVM. While the latter isn't functionally related to the former, it depends on some of the rearrangements done there.
01: shadow: use __put_user() instead of __copy_to_user() 02: split __{get,put}_user() into "guest" and "unsafe" variants 03: split __copy_{from,to}_user() into "guest" and "unsafe" variants 04: PV: harden guest memory accesses against speculative abuse 05: rename {get,put}_user() to {get,put}_guest()
06: gdbsx: convert "user" to "guest" accesses
07: rename copy_{from,to}_user() to copy_{from,to}_guest_pv() 08: move stac()/clac() from {get,put}_unsafe_asm() ... 09: PV: use get_unsafe() instead of copy_from_unsafe() 10: shadow: use get_unsafe() instead of copy_from_unsafe() 11: shadow: polish shadow_write_entries()
12: shadow: move shadow_set_l<N>e() to their own source file 13: shadow: don't open-code SHF_* shorthands
14: shadow: SH_type_l2h_shadow is PV-only
15: shadow: drop SH_type_l2h_pae_shadow
16: shadow: only 4-level guest code needs building when !HVM 17: shadow: adjust is_pv_*() checks
Jan
PATCH 1/17 x86/shadow: use __put_user() instead of __copy_to_user()
In a subsequent patch I would almost have broken the logic here, if I hadn't happened to read through the comment at the top of safe_write_entry(): __copy_from_user() does not provide a guarantee shadow_write_entries() requires - it's only an optimization that it makes use of __put_user_size() for certain sizes. Use __put_user() directly, which does expand to a single (memory accessing) insn.
---
In a future patch I guess I'll make this write store the intended data instead of doing this "no-op" write, making the subsequent loop start from 1 in the success case. In fact I also think safe_write_entry() would better go away, in favor of direct use of write_atomic().
xen/arch/x86/mm/shadow/multi.c +2 -2
@@ -776,9 +776,9 @@ shadow_write_entries(void *d, void *s, i/* Because we mirror access rights at all levels in the shadow, an * l2 (or higher) entry with the RW bit cleared will leave us with * no write access through the linear map. -* We detect that by writing to the shadow with copy_to_user() and +* We detect that by writing to the shadow with __put_user() and * using map_domain_page() to get a writeable mapping if we need to. */ -if (__copy_to_user (d, d,sizeof (unsigned long )) !=0 ) +if (__put_user (*dst, dst) ) {perfc_incr (shadow_linear_map_failed); map =map_domain_page (mfn);
PATCH 2/17 x86: split __{get,put}_user() into "guest" and "unsafe" variants
In reply to Jan BeulichThe "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not. (For descriptor table accesses the low bits of the addresses may still be guest controlled, but this still won't allow speculation to "escape" into unwanted areas.) Subsequently we will want them to have different behavior, so as first step identify which one is which. For now, both groups of constructs alias one another.
Double underscore prefixes are retained only on __{get,put}_guest(), to allow still distinguishing them from their "checking" counterparts once they also get renamed (to {get,put}_guest()).
Since for them it's almost a full re-write, move what becomes {get,put}_unsafe_size() into the "common" uaccess.h (x86_64/*.h should disappear at some point anyway).
In __copy_to_user() one of the two casts in each put_guest_size() invocation gets dropped. They're not needed and did break symmetry with __copy_from_user().
xen/arch/x86/mm/shadow/multi.c +2 -2
@@ -776,9 +776,9 @@ shadow_write_entries(void *d, void *s, i/* Because we mirror access rights at all levels in the shadow, an * l2 (or higher) entry with the RW bit cleared will leave us with * no write access through the linear map. -* We detect that by writing to the shadow with __put_user() and +* We detect that by writing to the shadow with put_unsafe() and * using map_domain_page() to get a writeable mapping if we need to. */ -if (__put_user (*dst, dst) ) +if (put_unsafe (*dst, dst) ) {perfc_incr (shadow_linear_map_failed); map =map_domain_page (mfn);
xen/arch/x86/pv/emul-gate-op.c +4 -4
@@ -40,7 +40,7 @@ static int read_gate_descriptor(unsigned ((gate_sel >>3 ) + !is_pv_32bit_vcpu (v) >= (gate_sel &4 ? v->arch.pv.ldt_ents : v->arch.pv.gdt_ents)) || - __get_user(desc, pdesc) ) + get_unsafe(desc, pdesc) ) return 0; *sel = (desc.a >>16 ) &0x0000fffc ;
@@ -59,7 +59,7 @@ static int read_gate_descriptor(unsigned {if ( (*ar &0x1f00 ) !=0x0c00 ||/* Limit check done above already. */ -__get_user (desc, pdesc +1 ) || +get_unsafe (desc, pdesc +1 ) || (desc.b &0x1f00 ) )return 0 ;
@@ -294,7 +294,7 @@ void pv_emulate_gate_op(struct cpu_user_ { \ --stkp; \ esp -=4 ; \ - rc =__put_user (item, stkp); \ + rc =__put_guest (item, stkp); \if ( rc ) \ { \ pv_inject_page_fault(PFEC_write_access, \
@@ -362,7 +362,7 @@ void pv_emulate_gate_op(struct cpu_user_unsigned int parm; --ustkp; - rc =__get_user (parm, ustkp); + rc =__get_guest (parm, ustkp);if ( rc ) {pv_inject_page_fault (0 , (unsigned long )(ustkp +1 ) - rc);
xen/arch/x86/pv/emulate.c +2 -2
@@ -34,13 +34,13 @@ int pv_emul_read_descriptor(unsigned intif ( sel <4 ||/* * Don't apply the GDT limit here, as the selector may be a Xen -* provided one. __get_user() will fail (without taking further +* provided one. get_unsafe() will fail (without taking further * action) for ones falling in the gap between guest populated * and Xen ones. */ ((sel &4 ) && (sel >>3 ) >= v->arch.pv.ldt_ents) ) desc.b = desc.a =0 ; -else if (__get_user (desc,gdt_ldt_desc_ptr (sel)) ) +else if (get_unsafe (desc,gdt_ldt_desc_ptr (sel)) )return 0 ;if ( !insn_fetch ) desc.b &= ~_SEGMENT_L;
xen/arch/x86/pv/iret.c +11 -11
@@ -114,15 +114,15 @@ unsigned int compat_iret(void) regs->rsp = (u32 )regs->rsp;/* Restore EAX (clobbered by hypercall). */ -if (unlikely (__get_user (regs->eax, (u32 *)regs->rsp)) ) +if (unlikely (__get_guest (regs->eax, (u32 *)regs->rsp)) ) {domain_crash (v->domain);return 0 ; }/* Restore CS and EIP. */ -if (unlikely (__get_user (regs->eip, (u32 *)regs->rsp +1 )) || -unlikely (__get_user (regs->cs, (u32 *)regs->rsp +2 )) ) +if (unlikely (__get_guest (regs->eip, (u32 *)regs->rsp +1 )) || +unlikely (__get_guest (regs->cs, (u32 *)regs->rsp +2 )) ) {domain_crash (v->domain);return 0 ;
@@ -132,7 +132,7 @@ unsigned int compat_iret(void)* Fix up and restore EFLAGS. We fix up in a local staging area * to avoid firing the BUG_ON(IOPL) check in arch_get_info_guest. */ -if (unlikely (__get_user (eflags, (u32 *)regs->rsp +3 )) ) +if (unlikely (__get_guest (eflags, (u32 *)regs->rsp +3 )) ) {domain_crash (v->domain);return 0 ;
@@ -164,16 +164,16 @@ unsigned int compat_iret(void) {for (i =1 ; i <10 ; ++i) { - rc |=__get_user (x, (u32 *)regs->rsp + i); - rc |=__put_user (x, (u32 *)(unsigned long )ksp + i); + rc |=__get_guest (x, (u32 *)regs->rsp + i); + rc |=__put_guest (x, (u32 *)(unsigned long )ksp + i); } } else if ( ksp > regs->esp ) {for ( i =9 ; i >0 ; --i ) { - rc |=__get_user (x, (u32 *)regs->rsp + i); - rc |=__put_user (x, (u32 *)(unsigned long )ksp + i); + rc |=__get_guest (x, (u32 *)regs->rsp + i); + rc |=__put_guest (x, (u32 *)(unsigned long )ksp + i); } }if ( rc )
@@ -189,7 +189,7 @@ unsigned int compat_iret(void) eflags &= ~X86_EFLAGS_IF ; regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF |X86_EFLAGS_NT |X86_EFLAGS_TF ); -if (unlikely (__put_user (0 , (u32 *)regs->rsp)) ) +if (unlikely (__put_guest (0 , (u32 *)regs->rsp)) ) {domain_crash (v->domain);return 0 ;
@@ -205,8 +205,8 @@ unsigned int compat_iret(void)else if (ring_1 (regs) ) regs->esp +=16 ;/* Return to ring 2/3: restore ESP and SS. */ -else if (__get_user (regs->ss, (u32 *)regs->rsp +5 ) || -__get_user (regs->esp, (u32 *)regs->rsp +4 ) ) +else if (__get_guest (regs->ss, (u32 *)regs->rsp +5 ) || +__get_guest (regs->esp, (u32 *)regs->rsp +4 ) ) {domain_crash (v->domain);return 0 ;
xen/arch/x86/traps.c +2 -2
@@ -274,7 +274,7 @@ static void compat_show_guest_stack(stru {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) ) {if ( i !=0 )printk ("\n " );
@@ -343,7 +343,7 @@ static void show_guest_stack(struct vcpu {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) ) {if ( i !=0 )printk ("\n " );
xen/include/asm-x86/uaccess.h +56 -31
172 lines not on this page · raw message
xen/include/asm-x86/x86_64/uaccess.h +0 -24
@@ -57,28 +57,4 @@ extern void *xlat_malloc(unsigned long * (likely ((count) < (~0U / (size))) && \ compat_access_ok(addr, 0 + (count) * (size))) -#define __put_user_size (x,ptr,size,retval,errret) \ -do { \ - retval = 0; \ - switch (size) { \ - case 1: __put_user_asm(x,ptr,retval,"b","b","iq",errret);break; \ - case 2: __put_user_asm(x,ptr,retval,"w","w","ir",errret);break; \ - case 4: __put_user_asm(x,ptr,retval,"l","k","ir",errret);break; \ - case 8: __put_user_asm(x,ptr,retval,"q","","ir",errret);break; \ - default: __put_user_bad(); \ - } \ -} while (0) - -#define __get_user_size (x,ptr,size,retval,errret) \ -do { \ - retval = 0; \ - switch (size) { \ - case 1: __get_user_asm(x,ptr,retval,"b","b","=q",errret);break; \ - case 2: __get_user_asm(x,ptr,retval,"w","w","=r",errret);break; \ - case 4: __get_user_asm(x,ptr,retval,"l","k","=r",errret);break; \ - case 8: __get_user_asm(x,ptr,retval,"q","","=r",errret); break; \ - default: __get_user_bad(); \ - } \ -} while (0) -#endif /* __X86_64_UACCESS_H */
xen/test/livepatch/xen_hello_world_func.c +1 -1
@@ -26,7 +26,7 @@ const char *xen_hello_world(void)* Any BUG, or WARN_ON will contain symbol and payload name. Furthermore * exceptions will be caught and processed properly. */ - rc =__get_user (tmp, non_canonical_addr); + rc =get_unsafe (tmp, non_canonical_addr);BUG_ON (rc != -EFAULT );#endif #if defined(CONFIG_ARM )
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Maybe an example would help clarify.
Thanks, Roger.
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Not exactly, not. "unsafe" means access to anything which may fault, guest controlled or not. do_invalid_op()'s reading of the insn stream is a good example - the faulting insn there isn't guest controlled at all, but we still want to be careful when trying to read these bytes, as we don't want to fully trust %rip there.
Jan
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Quoted 6 lines
Not exactly, not. "unsafe" means access to anything which may fault, guest controlled or not. do_invalid_op()'s reading of the insn stream is a good example - the faulting insn there isn't guest controlled at all, but we still want to be careful when trying to read these bytes, as we don't want to fully trust %rip there.
Would it make sense to threat everything as 'guest' accesses for the sake of not having this difference?
I think having two accessors it's likely to cause confusion and could possibly lead to the wrong one being used in unexpected contexts. Does it add a too big performance penalty to always use the most restrictive one?
Thanks, Roger.
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 20 lines · Roger Pau Monné
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Quoted 6 lines
Not exactly, not. "unsafe" means access to anything which may fault, guest controlled or not. do_invalid_op()'s reading of the insn stream is a good example - the faulting insn there isn't guest controlled at all, but we still want to be careful when trying to read these bytes, as we don't want to fully trust %rip there.
Quoted 2 lines
Would it make sense to threat everything as 'guest' accesses for the sake of not having this difference?
That's what we've been doing until now. It is the purpose of this change to allow the two to behave differently.
Quoted 4 lines
I think having two accessors it's likely to cause confusion and could possibly lead to the wrong one being used in unexpected contexts. Does it add a too big performance penalty to always use the most restrictive one?
The problem is the most restrictive one is going to be too restrictive - we wouldn't be able to access Xen space anymore e.g. from the place pointed at above as example. This is because for guest accesses (but not for "unsafe" ones) we're going to divert them into non-canonical space (and hence make speculation impossible, as such an access would fault) if it would touch Xen space.
Jan
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Not exactly, not. "unsafe" means access to anything which may fault, guest controlled or not. do_invalid_op()'s reading of the insn stream is a good example - the faulting insn there isn't guest controlled at all, but we still want to be careful when trying to read these bytes, as we don't want to fully trust %rip there.
Oh, I see. It's possible that %rip points to an unmapped address there, and we need to be careful when reading, even if the value of %rip cannot be controlled by the guest and can legitimacy point to Xen's address space.
Quoted 11 lines
Quoted 2 lines
Would it make sense to threat everything as 'guest' accesses for the sake of not having this difference?
That's what we've been doing until now. It is the purpose of this change to allow the two to behave differently.
Quoted 4 lines
I think having two accessors it's likely to cause confusion and could possibly lead to the wrong one being used in unexpected contexts. Does it add a too big performance penalty to always use the most restrictive one?
Quoted 7 lines
The problem is the most restrictive one is going to be too restrictive - we wouldn't be able to access Xen space anymore e.g. from the place pointed at above as example. This is because for guest accesses (but not for "unsafe" ones) we're going to divert them into non-canonical space (and hence make speculation impossible, as such an access would fault) if it would touch Xen space.
Yes, I understand now. I think it would have been helpful (for me) to have the first sentence as:
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are expected to be used in order to access addresses not under the guest control, but that could trigger faults anyway (like accessing the instruction stream in do_invalid_op).
Thanks, Roger.
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 46 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Not exactly, not. "unsafe" means access to anything which may fault, guest controlled or not. do_invalid_op()'s reading of the insn stream is a good example - the faulting insn there isn't guest controlled at all, but we still want to be careful when trying to read these bytes, as we don't want to fully trust %rip there.
Oh, I see. It's possible that %rip points to an unmapped address there, and we need to be careful when reading, even if the value of %rip cannot be controlled by the guest and can legitimacy point to Xen's address space.
Quoted 11 lines
Quoted 2 lines
Would it make sense to threat everything as 'guest' accesses for the sake of not having this difference?
That's what we've been doing until now. It is the purpose of this change to allow the two to behave differently.
Quoted 4 lines
I think having two accessors it's likely to cause confusion and could possibly lead to the wrong one being used in unexpected contexts. Does it add a too big performance penalty to always use the most restrictive one?
Quoted 7 lines
The problem is the most restrictive one is going to be too restrictive - we wouldn't be able to access Xen space anymore e.g. from the place pointed at above as example. This is because for guest accesses (but not for "unsafe" ones) we're going to divert them into non-canonical space (and hence make speculation impossible, as such an access would fault) if it would touch Xen space.
Quoted 8 lines
Yes, I understand now. I think it would have been helpful (for me) to have the first sentence as:
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are expected to be used in order to access addresses not under the guest control, but that could trigger faults anyway (like accessing the instruction stream in
do_invalid_op).
I can use some of this, but in particular "access addresses not under the guest control" isn't entirely correct. The question isn't whether there's a guest control aspect, but which part of the address space the addresses are in. See specifically x86/PV: use get_unsafe() instead of copy_from_unsafe()" for two pretty good examples. The address within the linear page tables are - in a way at least - still somewhat guest controlled, but we're deliberately accessing Xen virtual addresses there.
Jan
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 56 lines · Jan Beulich
Quoted 46 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Not exactly, not. "unsafe" means access to anything which may fault, guest controlled or not. do_invalid_op()'s reading of the insn stream is a good example - the faulting insn there isn't guest controlled at all, but we still want to be careful when trying to read these bytes, as we don't want to fully trust %rip there.
Oh, I see. It's possible that %rip points to an unmapped address there, and we need to be careful when reading, even if the value of %rip cannot be controlled by the guest and can legitimacy point to Xen's address space.
Quoted 11 lines
Quoted 2 lines
Would it make sense to threat everything as 'guest' accesses for the sake of not having this difference?
That's what we've been doing until now. It is the purpose of this change to allow the two to behave differently.
Quoted 4 lines
I think having two accessors it's likely to cause confusion and could possibly lead to the wrong one being used in unexpected contexts. Does it add a too big performance penalty to always use the most restrictive one?
Quoted 7 lines
The problem is the most restrictive one is going to be too restrictive - we wouldn't be able to access Xen space anymore e.g. from the place pointed at above as example. This is because for guest accesses (but not for "unsafe" ones) we're going to divert them into non-canonical space (and hence make speculation impossible, as such an access would fault) if it would touch Xen space.
Quoted 8 lines
Yes, I understand now. I think it would have been helpful (for me) to have the first sentence as:
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are expected to be used in order to access addresses not under the guest control, but that could trigger faults anyway (like accessing the instruction stream in
do_invalid_op).
Quoted 8 lines
I can use some of this, but in particular "access addresses not under the guest control" isn't entirely correct. The question isn't whether there's a guest control aspect, but which part of the address space the addresses are in. See specifically x86/PV: use
get_unsafe()instead ofcopy_from_unsafe()" for two pretty good examples. The address within the linear page tables are - in a way at least - still somewhat guest controlled, but we're deliberately accessing Xen virtual addresses there.
OK, could this be somehow added to the commit message then?
Maybe it would be better to have something like:
The "guest" variants are intended to work with addresses belonging to the guest address space, while the "unsafe" variants should be used for addresses that fall into the Xen address space.
I think it's important to list exactly how the distinction between the guest/unsafe accessors is made, or else it's impossible to review that the changes done here are correct.
Thanks, Roger.
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 70 lines · Roger Pau Monné
Quoted 56 lines · Jan Beulich
Quoted 46 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Roger Pau Monné
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 2 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not.
Just to clarify, both work against user addresses, but guest variants need to be more careful because the guest provided address can also be modified?
I'm trying to understand the difference between "fully guest controlled" and "guest controlled".
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Not exactly, not. "unsafe" means access to anything which may fault, guest controlled or not. do_invalid_op()'s reading of the insn stream is a good example - the faulting insn there isn't guest controlled at all, but we still want to be careful when trying to read these bytes, as we don't want to fully trust %rip there.
Oh, I see. It's possible that %rip points to an unmapped address there, and we need to be careful when reading, even if the value of %rip cannot be controlled by the guest and can legitimacy point to Xen's address space.
Quoted 11 lines
Quoted 2 lines
Would it make sense to threat everything as 'guest' accesses for the sake of not having this difference?
That's what we've been doing until now. It is the purpose of this change to allow the two to behave differently.
Quoted 4 lines
I think having two accessors it's likely to cause confusion and could possibly lead to the wrong one being used in unexpected contexts. Does it add a too big performance penalty to always use the most restrictive one?
Quoted 7 lines
The problem is the most restrictive one is going to be too restrictive - we wouldn't be able to access Xen space anymore e.g. from the place pointed at above as example. This is because for guest accesses (but not for "unsafe" ones) we're going to divert them into non-canonical space (and hence make speculation impossible, as such an access would fault) if it would touch Xen space.
Quoted 8 lines
Yes, I understand now. I think it would have been helpful (for me) to have the first sentence as:
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are expected to be used in order to access addresses not under the guest control, but that could trigger faults anyway (like accessing the instruction stream in
do_invalid_op).Quoted 8 lines
I can use some of this, but in particular "access addresses not under the guest control" isn't entirely correct. The question isn't whether there's a guest control aspect, but which part of the address space the addresses are in. See specifically x86/PV: use
get_unsafe()instead ofcopy_from_unsafe()" for two pretty good examples. The address within the linear page tables are - in a way at least - still somewhat guest controlled, but we're deliberately accessing Xen virtual addresses there.OK, could this be somehow added to the commit message then?
Maybe it would be better to have something like:
Quoted 7 lines
The "guest" variants are intended to work with addresses belonging to the guest address space, while the "unsafe" variants should be used for addresses that fall into the Xen address space.
I think it's important to list exactly how the distinction between the guest/unsafe accessors is made, or else it's impossible to review that the changes done here are correct.
I did change the paragraph to read
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are intended to be used in order to access addresses not (directly) under guest control, within Xen's part of virtual address space. (For linear page table and descriptor table accesses the low bits of the addresses may still be guest controlled, but this still won't allow speculation to "escape" into unwanted areas.) Subsequently we will want them to have distinct behavior, so as first step identify which one is which. For now, both groups of constructs alias one another.
earlier today. Is this clear enough? Relevant parts I've also mirrored into patch 3's description?
Jan
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 12 lines · Jan Beulich
I did change the paragraph to read
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are intended to be used in order to access addresses not (directly) under guest control, within Xen's part of virtual address space. (For linear page table and descriptor table accesses the low bits of the addresses may still be guest controlled, but this still won't allow speculation to "escape" into unwanted areas.) Subsequently we will want them to have distinct behavior, so as first step identify which one is which. For now, both groups of constructs alias one another.
Quoted 2 lines
earlier today. Is this clear enough? Relevant parts I've also mirrored into patch 3's description?
Yes, that does indeed seem clearer, thanks.
Roger.
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
In reply to Jan BeulichQuoted 5 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not. (For descriptor table accesses the low bits of the addresses may still be guest controlled, but this still won't allow speculation to "escape" into unwanted areas.)
Descriptor table is also in guest address space, and hence would be fine using the _guest accessors? (even if not in guest control and thus unsuitable as an speculation vector)
Quoted 171 lines
Subsequently we will want them to have different
behavior, so as first step identify which one is which. For now, both groups of constructs alias one another.Double underscore prefixes are retained only on __{get,put}_guest(), to allow still distinguishing them from their "checking" counterparts once they also get renamed (to {get,put}_guest()).
Since for them it's almost a full re-write, move what becomes {get,put}_unsafe_size() into the "common"
uaccess.h(x86_64/*.h should disappear at some point anyway).In
__copy_to_user()one of the two casts in eachput_guest_size()invocation gets dropped. They're not needed and did break symmetry with__copy_from_user().Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/mm/shadow/multi.c +2
-2@@ -776,9 +776,9 @@ shadow_write_entries(void *d, void *s, i/* Because we mirror access rights at all levels in the shadow, an * l2 (or higher) entry with the RW bit cleared will leave us with * no write access through the linear map. -* We detect that by writing to the shadow with __put_user() and +* We detect that by writing to the shadow with put_unsafe() and * using map_domain_page() to get a writeable mapping if we need to. */ -if (__put_user (*dst, dst) ) +if (put_unsafe (*dst, dst) ) {perfc_incr (shadow_linear_map_failed); map =map_domain_page (mfn);xen/arch/x86/pv/emul-gate-op.c +4
-4@@ -40,7 +40,7 @@ static int read_gate_descriptor(unsigned ((gate_sel >>3 ) + !is_pv_32bit_vcpu (v) >= (gate_sel &4 ? v->arch.pv.ldt_ents : v->arch.pv.gdt_ents)) || - __get_user(desc, pdesc) ) + get_unsafe(desc, pdesc) ) return 0; *sel = (desc.a >>16 ) &0x0000fffc ;@@ -59,7 +59,7 @@ static int read_gate_descriptor(unsigned {if ( (*ar &0x1f00 ) !=0x0c00 ||/* Limit check done above already. */ -__get_user (desc, pdesc +1 ) || +get_unsafe (desc, pdesc +1 ) || (desc.b &0x1f00 ) )return 0 ;@@ -294,7 +294,7 @@ void pv_emulate_gate_op(struct cpu_user_ { \ --stkp; \ esp -=4 ; \ - rc =__put_user (item, stkp); \ + rc =__put_guest (item, stkp); \if ( rc ) \ { \ pv_inject_page_fault(PFEC_write_access, \@@ -362,7 +362,7 @@ void pv_emulate_gate_op(struct cpu_user_unsigned int parm; --ustkp; - rc =__get_user (parm, ustkp); + rc =__get_guest (parm, ustkp);if ( rc ) {pv_inject_page_fault (0 , (unsigned long )(ustkp +1 ) - rc);xen/arch/x86/pv/emulate.c +2
-2@@ -34,13 +34,13 @@ int pv_emul_read_descriptor(unsigned intif ( sel <4 ||/* * Don't apply the GDT limit here, as the selector may be a Xen -* provided one. __get_user() will fail (without taking further +* provided one. get_unsafe() will fail (without taking further * action) for ones falling in the gap between guest populated * and Xen ones. */ ((sel &4 ) && (sel >>3 ) >= v->arch.pv.ldt_ents) ) desc.b = desc.a =0 ; -else if (__get_user (desc,gdt_ldt_desc_ptr (sel)) ) +else if (get_unsafe (desc,gdt_ldt_desc_ptr (sel)) )return 0 ;if ( !insn_fetch ) desc.b &= ~_SEGMENT_L;xen/arch/x86/pv/iret.c +11
-11@@ -114,15 +114,15 @@ unsigned int compat_iret(void) regs->rsp = (u32 )regs->rsp;/* Restore EAX (clobbered by hypercall). */ -if (unlikely (__get_user (regs->eax, (u32 *)regs->rsp)) ) +if (unlikely (__get_guest (regs->eax, (u32 *)regs->rsp)) ) {domain_crash (v->domain);return 0 ; }/* Restore CS and EIP. */ -if (unlikely (__get_user (regs->eip, (u32 *)regs->rsp +1 )) || -unlikely (__get_user (regs->cs, (u32 *)regs->rsp +2 )) ) +if (unlikely (__get_guest (regs->eip, (u32 *)regs->rsp +1 )) || +unlikely (__get_guest (regs->cs, (u32 *)regs->rsp +2 )) ) {domain_crash (v->domain);return 0 ;@@ -132,7 +132,7 @@ unsigned int compat_iret(void)* Fix up and restore EFLAGS. We fix up in a local staging area * to avoid firing the BUG_ON(IOPL) check in arch_get_info_guest. */ -if (unlikely (__get_user (eflags, (u32 *)regs->rsp +3 )) ) +if (unlikely (__get_guest (eflags, (u32 *)regs->rsp +3 )) ) {domain_crash (v->domain);return 0 ;@@ -164,16 +164,16 @@ unsigned int compat_iret(void) {for (i =1 ; i <10 ; ++i) { - rc |=__get_user (x, (u32 *)regs->rsp + i); - rc |=__put_user (x, (u32 *)(unsigned long )ksp + i); + rc |=__get_guest (x, (u32 *)regs->rsp + i); + rc |=__put_guest (x, (u32 *)(unsigned long )ksp + i); } } else if ( ksp > regs->esp ) {for ( i =9 ; i >0 ; --i ) { - rc |=__get_user (x, (u32 *)regs->rsp + i); - rc |=__put_user (x, (u32 *)(unsigned long )ksp + i); + rc |=__get_guest (x, (u32 *)regs->rsp + i); + rc |=__put_guest (x, (u32 *)(unsigned long )ksp + i); } }if ( rc )@@ -189,7 +189,7 @@ unsigned int compat_iret(void) eflags &= ~X86_EFLAGS_IF ; regs->eflags &= ~(X86_EFLAGS_VM |X86_EFLAGS_RF |X86_EFLAGS_NT |X86_EFLAGS_TF ); -if (unlikely (__put_user (0 , (u32 *)regs->rsp)) ) +if (unlikely (__put_guest (0 , (u32 *)regs->rsp)) ) {domain_crash (v->domain);return 0 ;@@ -205,8 +205,8 @@ unsigned int compat_iret(void)else if (ring_1 (regs) ) regs->esp +=16 ;/* Return to ring 2/3: restore ESP and SS. */ -else if (__get_user (regs->ss, (u32 *)regs->rsp +5 ) || -__get_user (regs->esp, (u32 *)regs->rsp +4 ) ) +else if (__get_guest (regs->ss, (u32 *)regs->rsp +5 ) || +__get_guest (regs->esp, (u32 *)regs->rsp +4 ) ) {domain_crash (v->domain);return 0 ;xen/arch/x86/traps.c +1
-1@@ -274,7 +274,7 @@ static void compat_show_guest_stack(stru {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) ) {if ( i !=0 )printk ("\n " );
xen/arch/x86/traps.c static void show_guest_stack(struct vcpu · line 346
Quoted 5 lines
xen/arch/x86/traps.c +1
-1@@ -343,7 +343,7 @@ static void show_guest_stack(struct vcpu {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) )
Shouldn't accessing the guest stack use the _guest accessors?
Or has this address been verified by Xen and not in idrect control of the guest, and thus can't be used for speculation purposes?
I feel like this should be using the _guest accessors anyway, as the guest stack is an address in guest space?
Thanks, Roger.
Re: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 10 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not. (For descriptor table accesses the low bits of the addresses may still be guest controlled, but this still won't allow speculation to "escape" into unwanted areas.)
Descriptor table is also in guest address space, and hence would be fine using the
_guestaccessors? (even if not in guest control and thus unsuitable as an speculation vector)
No, we don't access descriptor tables in guest space, I don't think. See read_gate_descriptor() for an example. After all PV guests don't even have the full concept of self-managed (in their VA space) descriptor tables (GDT gets specified in terms of frames, while LDT gets specified in terms of (VA,size) tuples, but just for Xen to read the underlying page table entries upon 1st access).
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/traps.c +1
-1@@ -274,7 +274,7 @@ static void compat_show_guest_stack(stru {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) ) {if ( i !=0 )printk ("\n " );
Quoted 8 lines
xen/arch/x86/traps.c static void show_guest_stack(struct vcpu · line 346
Quoted 6 lines
xen/arch/x86/traps.c +1
-1@@ -343,7 +343,7 @@ static void show_guest_stack(struct vcpu {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) )Shouldn't accessing the guest stack use the
_guestaccessors?
Hmm, yes indeed.
Quoted 5 lines
Or has this address been verified by Xen and not in idrect control of the guest, and thus can't be used for speculation purposes?
I feel like this should be using the
_guestaccessors anyway, as the guest stack is an address in guest space?
I think this being a debugging function only, not directly accessible by guests, is what made me think speculation is not an issue here and hence the "unsafe" variants are fine to use (they're slightly cheaper after all, once the subsequent changes are in place). But I guess I will better switch these two around.
Jan
Roger Pau Monné
Reviewed-byRe: [PATCH 02/17] x86: split __{get,put}_user() into "guest" and "unsafe" variants
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not. (For descriptor table accesses the low bits of the addresses may still be guest controlled, but this still won't allow speculation to "escape" into unwanted areas.)
Descriptor table is also in guest address space, and hence would be fine using the
_guestaccessors? (even if not in guest control and thus unsuitable as an speculation vector)
Quoted 7 lines
No, we don't access descriptor tables in guest space, I don't think. See
read_gate_descriptor()for an example. After all PV guests don't even have the full concept of self-managed (in their VA space) descriptor tables (GDT gets specified in terms of frames, while LDT gets specified in terms of (VA,size) tuples, but just for Xen to read the underlying page table entries upon 1st access).
I see, read_gate_descriptor uses gdt_ldt_desc_ptr which points into the per-domain Xen virt space, so it's indeed an address in Xen address space. I realize it doesn't make sense for the descriptor table to be in guest address space, as it's not fully under guest control.
Quoted 28 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/traps.c +1
-1@@ -274,7 +274,7 @@ static void compat_show_guest_stack(stru {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) ) {if ( i !=0 )printk ("\n " );Quoted 8 lines
xen/arch/x86/traps.c static void show_guest_stack(struct vcpu · line 346
Quoted 6 lines
xen/arch/x86/traps.c +1
-1@@ -343,7 +343,7 @@ static void show_guest_stack(struct vcpu {if ( (((long )stack -1 ) ^ ((long )(stack +1 ) -1 )) & mask )break ; -if (__get_user (addr, stack) ) +if (get_unsafe (addr, stack) )Shouldn't accessing the guest stack use the
_guestaccessors?Hmm, yes indeed.
Quoted 5 lines
Or has this address been verified by Xen and not in idrect control of the guest, and thus can't be used for speculation purposes?
I feel like this should be using the
_guestaccessors anyway, as the guest stack is an address in guest space?
Quoted 6 lines
I think this being a debugging function only, not directly accessible by guests, is what made me think speculation is not an issue here and hence the "unsafe" variants are fine to use (they're slightly cheaper after all, once the subsequent changes are in place). But I guess I will better switch these two around.
With that:
Thanks, Roger.
PATCH 3/17 x86: split __copy_{from,to}_user() into "guest" and "unsafe" variants
In reply to Jan BeulichThe "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not. Subsequently we will want them to have different behavior, so as first step identify which one is which. For now, both groups of constructs alias one another.
Double underscore prefixes are retained only on __copy_{from,to}_guest_pv(), to allow still distinguishing them from their "checking" counterparts once they also get renamed (to copy_{from,to}_guest_pv()).
Add previously missing __user at some call sites.
---
Instead of __copy_{from,to}_guest_pv(), perhaps name them just __copy_{from,to}_pv()?
xen/arch/x86/gdbstub.c +2 -2
@@ -33,13 +33,13 @@ gdb_arch_signal_num(struct cpu_user_regsunsigned int gdb_arch_copy_from_user (void *dest,const void *src,unsigned len) { -return __copy_from_user (dest, src, len); +return copy_from_unsafe (dest, src, len); }unsigned int gdb_arch_copy_to_user (void *dest,const void *src,unsigned len) { -return __copy_to_user (dest, src, len); +return copy_to_unsafe (dest, src, len); }void
xen/arch/x86/mm/shadow/multi.c +8 -8
@@ -2614,7 +2614,7 @@ static int sh_page_fault(struct vcpu *v, {shadow_l2e_t sl2e;mfn_t gl1mfn; -if ( (__copy_from_user (&sl2e, +if ( (copy_from_unsafe (&sl2e, (sh_linear_l2_table (v) +shadow_l2_linear_offset (va)), sizeof(sl2e)) != 0)
@@ -2633,7 +2633,7 @@ static int sh_page_fault(struct vcpu *v,#endif /* SHOPT_OUT_OF_SYNC */ /* The only reasons for reserved bits to be set in shadow entries * are the two "magic" shadow_l1e entries. */ - if ( likely((__copy_from_user(&sl1e, + if ( likely((copy_from_unsafe(&sl1e, (sh_linear_l1_table(v) + shadow_l1_linear_offset(va)), sizeof(sl1e)) == 0)
@@ -3308,10 +3308,10 @@ static bool sh_invlpg(struct vcpu *v, un sh_linear_l4_table(v)[shadow_l4_linear_offset(linear)]) & _PAGE_PRESENT) )return false; -/* This must still be a copy-from-user because we don't have the +/* This must still be a copy-from-unsafe because we don't have the * paging lock, and the higher-level shadows might disappear * under our feet. */ -if (__copy_from_user (&sl3e, (sh_linear_l3_table (v) +if (copy_from_unsafe (&sl3e, (sh_linear_l3_table (v) +shadow_l3_linear_offset (linear)),sizeof (sl3e)) !=0 ) {
@@ -3330,9 +3330,9 @@ static bool sh_invlpg(struct vcpu *v, unreturn false;#endif -/* This must still be a copy-from-user because we don't have the shadow +/* This must still be a copy-from-unsafe because we don't have the shadow * lock, and the higher-level shadows might disappear under our feet. */ -if (__copy_from_user (&sl2e, +if (copy_from_unsafe (&sl2e,sh_linear_l2_table (v) +shadow_l2_linear_offset (linear),sizeof (sl2e)) !=0 ) {
@@ -3371,11 +3371,11 @@ static bool sh_invlpg(struct vcpu *v, un* hold the paging lock yet. Check again with the lock held. */ paging_lock (d); -/* This must still be a copy-from-user because we didn't +/* This must still be a copy-from-unsafe because we didn't * have the paging lock last time we checked, and the * higher-level shadows might have disappeared under our * feet. */ -if (__copy_from_user (&sl2e, +if (copy_from_unsafe (&sl2e,sh_linear_l2_table (v) +shadow_l2_linear_offset (linear),sizeof (sl2e)) !=0 )
xen/arch/x86/pv/emul-gate-op.c +2 -2
@@ -149,12 +149,12 @@ static int read_mem(enum x86_segment seg addr = (uint32_t )addr; -if ( (rc =__copy_from_user (p_data, (void *)addr, bytes)) ) +if ( (rc =__copy_from_guest_pv (p_data, (void __user *)addr, bytes)) ) {/* * TODO: This should report PFEC_insn_fetch when goc->insn_fetch && * cpu_has_nx, but we'd then need a "fetch" variant of -* __copy_from_user() respecting NX, SMEP, and protection keys. +* __copy_from_guest_pv() respecting NX, SMEP, and protection keys. */ x86_emul_pagefault (0 , addr + bytes - rc, ctxt);return X86EMUL_EXCEPTION ;
xen/arch/x86/pv/emul-priv-op.c +6 -4
@@ -649,7 +649,8 @@ static int rep_ins(uint16_t port,if ( rc !=X86EMUL_OKAY )return rc; -if ( (rc =__copy_to_user ((void *)addr, &data, bytes_per_rep)) !=0 ) +if ( (rc =__copy_to_guest_pv ((void __user *)addr, &data, + bytes_per_rep)) !=0 ) {x86_emul_pagefault (PFEC_write_access, addr + bytes_per_rep - rc, ctxt);
@@ -716,7 +717,8 @@ static int rep_outs(enum x86_segment segif ( rc !=X86EMUL_OKAY )return rc; -if ( (rc =__copy_from_user (&data, (void *)addr, bytes_per_rep)) !=0 ) +if ( (rc =__copy_from_guest_pv (&data, (void __user *)addr, + bytes_per_rep)) !=0 ) {x86_emul_pagefault (0 , addr + bytes_per_rep - rc, ctxt);return X86EMUL_EXCEPTION ;
@@ -1253,12 +1255,12 @@ static int insn_fetch(enum x86_segment sif ( rc !=X86EMUL_OKAY )return rc; -if ( (rc =__copy_from_user (p_data, (void *)addr, bytes)) !=0 ) +if ( (rc =__copy_from_guest_pv (p_data, (void __user *)addr, bytes)) !=0 ) {/* * TODO: This should report PFEC_insn_fetch when goc->insn_fetch && * cpu_has_nx, but we'd then need a "fetch" variant of -* __copy_from_user() respecting NX, SMEP, and protection keys. +* __copy_from_guest_pv() respecting NX, SMEP, and protection keys. */ x86_emul_pagefault (0 , addr + bytes - rc, ctxt);return X86EMUL_EXCEPTION ;
xen/arch/x86/pv/mm.c +1 -1
@@ -41,7 +41,7 @@ l1_pgentry_t *map_guest_l1e(unsigned lonreturn NULL ;/* Find this l1e and its enclosing l1mfn in the linear map. */ -if (__copy_from_user (&l2e, +if (copy_from_unsafe (&l2e, &__linear_l2_table[l2_linear_offset (linear)],sizeof (l2_pgentry_t)) )return NULL ;
xen/arch/x86/pv/mm.h +1 -1
@@ -22,7 +22,7 @@ static inline l1_pgentry_t guest_get_efftoggle_guest_pt (curr);if (unlikely (!__addr_ok (linear)) || -__copy_from_user (&l1e, +copy_from_unsafe (&l1e, &__linear_l1_table[l1_linear_offset (linear)],sizeof (l1_pgentry_t)) ) l1e =l1e_empty ();
xen/arch/x86/pv/ro-page-fault.c +1 -1
@@ -43,7 +43,7 @@ static int ptwr_emulated_read(enum x86_sunsigned long addr = offset;if ( !__addr_ok (addr) || - (rc =__copy_from_user (p_data, (void *)addr, bytes)) ) + (rc =__copy_from_guest_pv (p_data, (void *)addr, bytes)) ) {x86_emul_pagefault (0 , addr + bytes - rc, ctxt);/* Read fault. */ return X86EMUL_EXCEPTION ;
xen/arch/x86/traps.c +1 -1
@@ -1097,7 +1097,7 @@ void do_invalid_op(struct cpu_user_regs }if ( !is_active_kernel_text (regs->rip) || -__copy_from_user (bug_insn, eip,sizeof (bug_insn)) || +copy_from_unsafe (bug_insn, eip,sizeof (bug_insn)) ||memcmp (bug_insn,"\xf\xb" ,sizeof (bug_insn)) ) goto die;
xen/arch/x86/usercopy.c +2 -2
@@ -110,7 +110,7 @@ unsigned __copy_from_user_ll(void *to, cunsigned copy_to_user (void __user *to,const void *from,unsigned n) {if (access_ok (to, n) ) - n =__copy_to_user (to, from, n); + n =__copy_to_guest_pv (to, from, n);return n; }
@@ -168,7 +168,7 @@ unsigned clear_user(void __user *to, unsunsigned copy_from_user (void *to,const void __user *from,unsigned n) {if (access_ok (from, n) ) - n =__copy_from_user (to, from, n); + n =__copy_from_guest_pv (to, from, n);else memset (to,0 , n);return n;
xen/include/asm-x86/guest_access.h +2 -2
@@ -28,11 +28,11 @@#define __raw_copy_to_guest (dst, src, len) \ (is_hvm_vcpu(current) ? \ copy_to_user_hvm((dst), (src), (len)) : \ - __copy_to_user((dst), (src), (len))) + __copy_to_guest_pv(dst, src, len))#define __raw_copy_from_guest (dst, src, len) \ (is_hvm_vcpu(current) ? \ copy_from_user_hvm((dst), (src), (len)) : \ - __copy_from_user((dst), (src), (len))) + __copy_from_guest_pv(dst, src, len))#define __raw_clear_guest (dst, len) \ (is_hvm_vcpu(current) ? \ clear_user_hvm((dst), (len)) : \
xen/include/asm-x86/uaccess.h +14 -14
@@ -197,21 +197,20 @@ do {#define get_guest_size get_unsafe_size/** -* __copy_to_user: - Copy a block of data into user space, with less checking -* @to: Destination address, in user space. -* @from: Source address, in kernel space. +* __copy_to_guest_pv: - Copy a block of data into guest space, with less +* checking +* @to: Destination address, in guest space. +* @from: Source address, in hypervisor space. * @n: Number of bytes to copy. * -* Context: User context only. This function may sleep. -* -* Copy data from kernel space to user space. Caller must check +* Copy data from hypervisor space to guest space. Caller must check * the specified block with access_ok() before calling this function. * * Returns number of bytes that could not be copied. * On success, this will be zero. */ static always_inline unsigned long -__copy_to_user (void __user *to,const void *from,unsigned long n) +__copy_to_guest_pv (void __user *to,const void *from,unsigned long n) {if (__builtin_constant_p (n)) {unsigned long ret;
@@ -233,16 +232,16 @@ __copy_to_user(void __user *to, const vo }return __copy_to_user_ll (to, from, n); } +#define copy_to_unsafe __copy_to_guest_pv/** -* __copy_from_user: - Copy a block of data from user space, with less checking -* @to: Destination address, in kernel space. -* @from: Source address, in user space. +* __copy_from_guest_pv: - Copy a block of data from guest space, with less +* checking +* @to: Destination address, in hypervisor space. +* @from: Source address, in guest space. * @n: Number of bytes to copy. * -* Context: User context only. This function may sleep. -* -* Copy data from user space to kernel space. Caller must check +* Copy data from guest space to hypervisor space. Caller must check * the specified block with access_ok() before calling this function. * * Returns number of bytes that could not be copied.
@@ -252,7 +251,7 @@ __copy_to_user(void __user *to, const vo* data to the requested size using zero bytes. */ static always_inline unsigned long -__copy_from_user (void *to,const void __user *from,unsigned long n) +__copy_from_guest_pv (void *to,const void __user *from,unsigned long n) {if (__builtin_constant_p (n)) {unsigned long ret;
@@ -274,6 +273,7 @@ __copy_from_user(void *to, const void __ }return __copy_from_user_ll (to, from, n); } +#define copy_from_unsafe __copy_from_guest_pv/* * The exception table consists of pairs of addresses: the first is the
Re: [PATCH 03/17] x86: split __copy_{from,to}_user() into "guest" and "unsafe" variants
Quoted 22 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not. Subsequently we will want them to have different behavior, so as first step identify which one is which. For now, both groups of constructs alias one another.
Double underscore prefixes are retained only on __copy_{from,to}_guest_pv(), to allow still distinguishing them from their "checking" counterparts once they also get renamed (to copy_{from,to}_guest_pv()).
Add previously missing
__userat some call sites.Signed-off-by Jan Beulich <[email protected]>---
Instead of __copy_{from,to}_guest_pv(), perhaps name them just __copy_{from,to}_pv()?xen/arch/x86/gdbstub.c +0
-0@@ -33,13 +33,13 @@ gdb_arch_signal_num(struct cpu_user_regsunsigned int gdb_arch_copy_from_user (void *dest,const void *src,unsigned len)
xen/arch/x86/gdbstub.c gdb_arch_signal_num(struct cpu_user_regs · line 42
Quoted 10 lines
xen/arch/x86/gdbstub.c +2
-2{ -return __copy_from_user (dest, src, len); +return copy_from_unsafe (dest, src, len); }unsigned int gdb_arch_copy_to_user (void *dest,const void *src,unsigned len) { -return __copy_to_user (dest, src, len); +return copy_to_unsafe (dest, src, len);
I assume we need to use the unsafe variants here, because the input addresses are fully controlled by gdb, and hence not suitable as speculation vectors?
Also could point to addresses belonging to both Xen or the guest address space AFAICT.
xen/include/asm-x86/uaccess.h
Quoted 2 lines
xen/include/asm-x86/uaccess.h +0
-0
At some point we should also rename this to pvaccess.h maybe?
xen/include/asm-x86/uaccess.h do { · line 201
Quoted 9 lines
xen/include/asm-x86/uaccess.h +2
-3@@ -197,21 +197,20 @@ do {#define get_guest_size get_unsafe_size/** -* __copy_to_user: - Copy a block of data into user space, with less checking -* @to: Destination address, in user space. -* @from: Source address, in kernel space. +* __copy_to_guest_pv: - Copy a block of data into guest space, with less +* checking
I would have preferred pv to be a prefix rather than a suffix, but we already have the hvm accessors using that nomenclature.
Thanks, Roger.
Re: [PATCH 03/17] x86: split __copy_{from,to}_user() into "guest" and "unsafe" variants
Quoted 28 lines · Roger Pau Monné
Quoted 27 lines · Jan Beulich
The "guest" variants are intended to work with (potentially) fully guest controlled addresses, while the "unsafe" variants are not. Subsequently we will want them to have different behavior, so as first step identify which one is which. For now, both groups of constructs alias one another.
Double underscore prefixes are retained only on __copy_{from,to}_guest_pv(), to allow still distinguishing them from their "checking" counterparts once they also get renamed (to copy_{from,to}_guest_pv()).
Add previously missing
__userat some call sites.Signed-off-by Jan Beulich <[email protected]>---
Instead of __copy_{from,to}_guest_pv(), perhaps name them just __copy_{from,to}_pv()?xen/arch/x86/gdbstub.c +1
-1@@ -33,13 +33,13 @@ gdb_arch_signal_num(struct cpu_user_regsunsigned int gdb_arch_copy_from_user (void *dest,const void *src,unsigned len) { -return __copy_from_user (dest, src, len); +return copy_from_unsafe (dest, src, len); }
Quoted 9 lines
xen/arch/x86/gdbstub.c gdb_arch_signal_num(struct cpu_user_regs · line 42
Quoted 5 lines
xen/arch/x86/gdbstub.c +1
-1unsigned int gdb_arch_copy_to_user (void *dest,const void *src,unsigned len) { -return __copy_to_user (dest, src, len); +return copy_to_unsafe (dest, src, len);I assume we need to use the unsafe variants here, because the input addresses are fully controlled by gdb, and hence not suitable as speculation vectors?
Speculation doesn't matter when it comes to debugging, I think. We were using the variants without access_ok() checks already anyway to allow access to Xen addresses. In fact it is my understanding ...
Quoted 2 lines
Also could point to addresses belonging to both Xen or the guest address space AFAICT.
... that the primary goal here is to access Xen addresses, and guest space only falls into the "may also happen to be accessed" category.
Quoted 4 lines
xen/include/asm-x86/uaccess.h
Quoted 2 lines
xen/include/asm-x86/uaccess.h +0
-0At some point we should also rename this to
pvaccess.hmaybe?
We could, but I'd rather not - this isn't about PV only. Instead I would simply re-interpret 'u' from standing for "user" (which didn't make much sense in Xen anyway, and was only attributed to the Linux origin) to standing for "unsafe" (both meanings - guest and in-Xen-but-unsafe).
Quoted 12 lines
xen/include/asm-x86/uaccess.h do { · line 201
Quoted 9 lines
xen/include/asm-x86/uaccess.h +2
-3@@ -197,21 +197,20 @@ do {#define get_guest_size get_unsafe_size/** -* __copy_to_user: - Copy a block of data into user space, with less checking -* @to: Destination address, in user space. -* @from: Source address, in kernel space. +* __copy_to_guest_pv: - Copy a block of data into guest space, with less +* checking I would have preferred pv to be a prefix rather than a suffix, but we already have the hvm accessors using that nomenclature.
Right, I wanted to match that naming model. Later we can think about renaming to copy_{to,from}_{hvm,pv}() or whatever else naming scheme we like. I have to admit though I'm not convinced the longer {hvm,pv}_copy_{from,to}_guest() would really be better.
Quoted 1 line
Reviewed-by Roger Pau Monné <[email protected]>
Thanks!
Jan
PATCH 4/17 x86/PV: harden guest memory accesses against speculative abuse
In reply to Jan BeulichInspired by
https://lore.kernel.org/lkml/f12e7d3cecf41b2c29734ea45a393be21d4a8058.1597848273.git.jpoimboe@redhat.com/
and prior work in that area of x86 Linux, suppress speculation with guest specified pointer values by suitably masking the addresses to non-canonical space in case they fall into Xen's virtual address range.
Introduce a new Kconfig control.
Note that it is necessary in such code to avoid using "m" kind operands:
If we didn't, there would be no guarantee that the register passed to guest_access_mask_ptr is also the (base) one used for the memory access.
As a minor unrelated change in get_unsafe_asm() the unnecessary "itype" parameter gets dropped and the XOR on the fixup path gets changed to be a 32-bit one in all cases: This way we avoid pointless REX.W or operand size overrides, or writes to partial registers.
---
The insn sequence chosen is certainly up for discussion; I've picked this one despite the RCR because alternatives I could come up with, like
mov $(HYPERVISOR_VIRT_END), %rax mov $~0, %rdx mov $0x7fffffffffffffff, %rcx cmp %rax, %rdi cmovb %rcx, %rdx and %rdx, %rdi
weren't necessarily better: Either, as above, they are longer and require a 3rd scratch register, or they also utilize the carry flag in some similar way.
---
Judging from the comment ahead of put_unsafe_asm() we might as well not tell gcc at all anymore about the memory access there, now that there's no use of the operand anymore in the assembly code.
xen/arch/x86/usercopy.c +31 -3
@@ -10,12 +10,19 @@#include <xen/sched.h> #include <asm/uaccess.h> -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n) +#ifndef GUARD +# define GUARD UA_KEEP +#endif + +unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n) {unsigned dummy;stac (); asmvolatile ( + GUARD( +" guest_access_mask_ptr %[to], %q[scratch1], %q[scratch2]\n" + ) " cmp $"STR(2*BYTES_PER_LONG-1)", %[cnt]\n"" jbe 1f\n" " mov %k[to], %[cnt]\n"
@@ -42,6 +49,7 @@ unsigned __copy_to_user_ll(void __user *_ASM_EXTABLE (1b ,2b ) : [cnt] "+c" (n), [to] "+D" (to), [from] "+S" (from), [aux] "=&r" (dummy) + GUARD(, [scratch1] "=&r" (dummy), [scratch2] "=&r" (dummy)) : "[aux]" (n) : "memory" );clac ();
@@ -49,12 +57,15 @@ unsigned __copy_to_user_ll(void __user *return n; } -unsigned __copy_from_user_ll (void *to,const void __user *from,unsigned n) +unsigned int copy_from_guest_ll (void *to,const void __user *from,unsigned int n) {unsigned dummy;stac (); asmvolatile ( + GUARD( +" guest_access_mask_ptr %[from], %q[scratch1], %q[scratch2]\n" + ) " cmp $"STR(2*BYTES_PER_LONG-1)", %[cnt]\n"" jbe 1f\n" " mov %k[to], %[cnt]\n"
@@ -87,6 +98,7 @@ unsigned __copy_from_user_ll(void *to, c_ASM_EXTABLE (1b ,6b ) : [cnt] "+c" (n), [to] "+D" (to), [from] "+S" (from), [aux] "=&r" (dummy) + GUARD(, [scratch1] "=&r" (dummy), [scratch2] "=&r" (dummy)) : "[aux]" (n) : "memory" );clac ();
@@ -94,6 +106,8 @@ unsigned __copy_from_user_ll(void *to, creturn n; } +#if GUARD (1 ) +0 +/** * copy_to_user: - Copy a block of data into user space. * @to: Destination address, in user space.
@@ -128,8 +142,11 @@ unsigned clear_user(void __user *to, uns {if (access_ok (to, n) ) { +long dummy; +stac (); asmvolatile ( +" guest_access_mask_ptr %[to], %[scratch1], %[scratch2]\n" "0: rep stos" __OS"\n" " mov %[bytes], %[cnt]\n" "1: rep stosb\n"
@@ -140,7 +157,8 @@ unsigned clear_user(void __user *to, uns".previous\n" _ASM_EXTABLE(0b,3b)_ASM_EXTABLE (1b ,2b ) - : [cnt] "=&c" (n), [to] "+D" (to) + : [cnt] "=&c" (n), [to] "+D" (to), [scratch1] "=&r" (dummy), + [scratch2] "=&r" (dummy) : [bytes] "r" (n & (BYTES_PER_LONG - 1)), [longs] "0" (n / BYTES_PER_LONG), "a" (0) );clac ();
@@ -174,6 +192,16 @@ unsigned copy_from_user(void *to, const return n; } +# undef GUARD +# define GUARD UA_DROP +# define copy_to_guest_ll copy_to_unsafe_ll +# define copy_from_guest_ll copy_from_unsafe_ll +# undef __user +# define __user +# include __FILE__ + +#endif /* GUARD(1) */ +/* * Local variables: * mode: C
xen/arch/x86/x86_64/entry.S +2 -0
@@ -446,6 +446,8 @@ UNLIKELY_START(g, create_bounce_frame_ba jmp asm_domain_crash_synchronous /* Does not return */ __UNLIKELY_END(create_bounce_frame_bad_sp) + guest_access_mask_ptr %rsi, %rax, %rcx + #define STORE_GUEST_STACK(reg, n) \ 0: movq %reg,(n)*8(%rsi); \ _ASM_EXTABLE(0b, domain_crash_page_fault_ ## n ## x8)
xen/common/Kconfig +18 -0
@@ -114,6 +114,24 @@ config SPECULATIVE_HARDEN_BRANCH If unsure, say Y. +config SPECULATIVE_HARDEN_GUEST_ACCESS + bool "Speculative PV Guest Memory Access Hardening" + default y + depends on PV + help + Contemporary processors may use speculative execution as a + performance optimisation, but this can potentially be abused by an + attacker to leak data via speculative sidechannels. + + One source of data leakage is via speculative accesses to hypervisor + memory through guest controlled values used to access guest memory. + + When enabled, code paths accessing PV guest memory will have guest + controlled addresses massaged such that memory accesses through them + won't touch hypervisor address space. + + If unsure, say Y. + endmenu config HYPFS
xen/include/asm-x86/asm-defns.h +13 -0
@@ -44,3 +44,16 @@ .macro INDIRECT_JMP arg:req INDIRECT_BRANCH jmp \arg .endm + +.macro guest_access_mask_ptr ptr:req, scratch1:req, scratch2:req +#if defined(CONFIG_SPECULATIVE_HARDEN_GUEST_ACCESS ) + mov $(HYPERVISOR_VIRT_END - 1), \scratch1 + mov $~0, \scratch2 + cmp \ptr, \scratch1 + rcr $1, \scratch2 + and \scratch2, \ptr +#elif defined(CONFIG_DEBUG) && defined(CONFIG_PV) + xor $~\@, \scratch1 + xor $~\@, \scratch2 +#endif +.endm
xen/include/asm-x86/uaccess.h +151 -31
264 lines not on this page · raw message
Re: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
Quoted 48 lines · Jan Beulich
Inspired by
https://lore.kernel.org/lkml/f12e7d3cecf41b2c29734ea45a393be21d4a8058.1597848273.git.jpoimboe@redhat.com/
and prior work in that area of x86 Linux, suppress speculation with guest specified pointer values by suitably masking the addresses to non-canonical space in case they fall into Xen's virtual address range.Introduce a new
Kconfigcontrol.Note that it is necessary in such code to avoid using "m" kind operands:
If we didn't, there would be no guarantee that the register passed toguest_access_mask_ptris also the (base) one used for the memory access.As a minor unrelated change in
get_unsafe_asm()the unnecessary "itype" parameter gets dropped and the XOR on the fixup path gets changed to be a 32-bit one in all cases: This way we avoid pointless REX.W or operand size overrides, or writes to partial registers.Requested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>---
The insn sequence chosen is certainly up for discussion; I've picked this one despite the RCR because alternatives I could come up with, likemov $(HYPERVISOR_VIRT_END), %rax mov $~0, %rdx mov $0x7fffffffffffffff, %rcx cmp %rax, %rdi cmovb %rcx, %rdx and %rdx, %rdiweren't necessarily better: Either, as above, they are longer and require a 3rd scratch register, or they also utilize the carry flag in some similar way.
---
Judging from the comment ahead ofput_unsafe_asm()we might as well not tell gcc at all anymore about the memory access there, now that there's no use of the operand anymore in the assembly code.xen/arch/x86/usercopy.c +2
-1@@ -10,12 +10,19 @@#include <xen/sched.h> #include <asm/uaccess.h> -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n) +#ifndef GUARD +# define GUARD UA_KEEP
xen/arch/x86/usercopy.c line 24
Quoted 10 lines
xen/arch/x86/usercopy.c +5
-0+#endif + +unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n) {unsigned dummy;stac (); asmvolatile ( + GUARD( + " guest_access_mask_ptr %[to], %q[scratch1], %q[scratch2]\n"
Don't you need to also take 'n' into account here to assert that the address doesn't end in hypervisor address space? Or that's fine as speculation wouldn't go that far?
I also wonder why this needs to be done in assembly, could you check the address(es) using C?
Quoted 152 lines
xen/arch/x86/usercopy.c +24
-2+ ) " cmp $"STR(2*BYTES_PER_LONG-1)", %[cnt]\n"" jbe 1f\n" " mov %k[to], %[cnt]\n" @@ -42,6 +49,7 @@ unsigned __copy_to_user_ll(void __user *_ASM_EXTABLE (1b ,2b ) : [cnt] "+c" (n), [to] "+D" (to), [from] "+S" (from), [aux] "=&r" (dummy) + GUARD(, [scratch1] "=&r" (dummy), [scratch2] "=&r" (dummy)) : "[aux]" (n) : "memory" );clac ();@@ -49,12 +57,15 @@ unsigned __copy_to_user_ll(void __user *return n; } -unsigned __copy_from_user_ll (void *to,const void __user *from,unsigned n) +unsigned int copy_from_guest_ll (void *to,const void __user *from,unsigned int n) {unsigned dummy;stac (); asmvolatile ( + GUARD( +" guest_access_mask_ptr %[from], %q[scratch1], %q[scratch2]\n" + ) " cmp $"STR(2*BYTES_PER_LONG-1)", %[cnt]\n"" jbe 1f\n" " mov %k[to], %[cnt]\n" @@ -87,6 +98,7 @@ unsigned __copy_from_user_ll(void *to, c_ASM_EXTABLE (1b ,6b ) : [cnt] "+c" (n), [to] "+D" (to), [from] "+S" (from), [aux] "=&r" (dummy) + GUARD(, [scratch1] "=&r" (dummy), [scratch2] "=&r" (dummy)) : "[aux]" (n) : "memory" );clac ();@@ -94,6 +106,8 @@ unsigned __copy_from_user_ll(void *to, creturn n; } +#if GUARD (1 ) +0 +/** * copy_to_user: - Copy a block of data into user space. * @to: Destination address, in user space. @@ -128,8 +142,11 @@ unsigned clear_user(void __user *to, uns {if (access_ok (to, n) ) { +long dummy; +stac (); asmvolatile ( +" guest_access_mask_ptr %[to], %[scratch1], %[scratch2]\n" "0: rep stos" __OS"\n" " mov %[bytes], %[cnt]\n" "1: rep stosb\n"@@ -140,7 +157,8 @@ unsigned clear_user(void __user *to, uns".previous\n" _ASM_EXTABLE(0b,3b)_ASM_EXTABLE (1b ,2b ) - : [cnt] "=&c" (n), [to] "+D" (to) + : [cnt] "=&c" (n), [to] "+D" (to), [scratch1] "=&r" (dummy), + [scratch2] "=&r" (dummy) : [bytes] "r" (n & (BYTES_PER_LONG - 1)), [longs] "0" (n / BYTES_PER_LONG), "a" (0) );clac ();@@ -174,6 +192,16 @@ unsigned copy_from_user(void *to, const return n; } +# undef GUARD +# define GUARD UA_DROP +# define copy_to_guest_ll copy_to_unsafe_ll +# define copy_from_guest_ll copy_from_unsafe_ll +# undef __user +# define __user +# include __FILE__ + +#endif /* GUARD(1) */ +/* * Local variables: * mode: C xen/arch/x86/x86_64/entry.S +2
-0@@ -446,6 +446,8 @@ UNLIKELY_START(g, create_bounce_frame_ba jmp asm_domain_crash_synchronous /* Does not return */ __UNLIKELY_END(create_bounce_frame_bad_sp) + guest_access_mask_ptr %rsi, %rax, %rcx + #define STORE_GUEST_STACK(reg, n) \ 0: movq %reg,(n)*8(%rsi); \ _ASM_EXTABLE(0b, domain_crash_page_fault_ ## n ## x8)xen/common/Kconfig +18
-0@@ -114,6 +114,24 @@ config SPECULATIVE_HARDEN_BRANCH If unsure, say Y. +config SPECULATIVE_HARDEN_GUEST_ACCESS + bool "Speculative PV Guest Memory Access Hardening" + default y + depends on PV + help + Contemporary processors may use speculative execution as a + performance optimisation, but this can potentially be abused by an + attacker to leak data via speculative sidechannels. + + One source of data leakage is via speculative accesses to hypervisor + memory through guest controlled values used to access guest memory. + + When enabled, code paths accessing PV guest memory will have guest + controlled addresses massaged such that memory accesses through them + won't touch hypervisor address space. + + If unsure, say Y. + endmenu config HYPFSxen/include/asm-x86/asm-defns.h +13
-0@@ -44,3 +44,16 @@ .macro INDIRECT_JMP arg:req INDIRECT_BRANCH jmp \arg .endm + +.macro guest_access_mask_ptr ptr:req, scratch1:req, scratch2:req +#if defined(CONFIG_SPECULATIVE_HARDEN_GUEST_ACCESS ) + mov $(HYPERVISOR_VIRT_END - 1), \scratch1 + mov $~0, \scratch2 + cmp \ptr, \scratch1 + rcr $1, \scratch2 + and \scratch2, \ptr +#elif defined(CONFIG_DEBUG) && defined(CONFIG_PV) + xor $~\@, \scratch1 + xor $~\@, \scratch2 +#endif +.endmxen/include/asm-x86/uaccess.h +1
-2@@ -13,13 +13,19 @@unsigned copy_to_user (void *to,const void *from,unsigned len);unsigned clear_user (void *to,unsigned len);unsigned copy_from_user (void *to,const void *from,unsigned len); +/* Handles exceptions in both to and from, but doesn't do access_ok */ -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n); -unsigned __copy_from_user_ll (void *to,const void __user *from,unsigned n);
xen/include/asm-x86/uaccess.h line 27
Quoted 10 lines
xen/include/asm-x86/uaccess.h +6
-0+unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n); +unsigned int copy_from_guest_ll (void *to,const void __user *from,unsigned int n); +unsigned int copy_to_unsafe_ll (void *to,const void *from,unsigned int n); +unsigned int copy_from_unsafe_ll (void *to,const void *from,unsigned int n);extern long __get_user_bad (void );extern void __put_user_bad (void ); +#define UA_KEEP(args...) args +#define UA_DROP(args...)
I assume UA means user access, and since you have dropped other uses of user and changed to guest instead I wonder if we should name this just A_{KEEP/DROP}.
Thanks, Roger.
Re: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
xen/arch/x86/usercopy.c +5
-1@@ -10,12 +10,19 @@#include <xen/sched.h> #include <asm/uaccess.h> -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n) +#ifndef GUARD +# define GUARD UA_KEEP +#endif + +unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n) {unsigned dummy;
Quoted 8 lines
xen/arch/x86/usercopy.c line 24
Quoted 4 lines
xen/arch/x86/usercopy.c +2
-0stac (); asmvolatile ( + GUARD( + " guest_access_mask_ptr %[to], %q[scratch1], %q[scratch2]\n"Don't you need to also take 'n' into account here to assert that the address doesn't end in hypervisor address space? Or that's fine as speculation wouldn't go that far?
Like elsewhere this leverages that the hypervisor VA range starts immediately after the non-canonical hole. I'm unaware of speculation being able to cross over that hole.
Quoted 2 lines
I also wonder why this needs to be done in assembly, could you check the address(es) using C?
For this to be efficient (in avoiding speculation) the insn sequence would better not have any conditional jumps. I don't think the compiler can be told so.
Quoted 18 lines
Quoted 18 lines
xen/include/asm-x86/uaccess.h +5
-2@@ -13,13 +13,19 @@unsigned copy_to_user (void *to,const void *from,unsigned len);unsigned clear_user (void *to,unsigned len);unsigned copy_from_user (void *to,const void *from,unsigned len); +/* Handles exceptions in both to and from, but doesn't do access_ok */ -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n); -unsigned __copy_from_user_ll (void *to,const void __user *from,unsigned n); +unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n); +unsigned int copy_from_guest_ll (void *to,const void __user *from,unsigned int n); +unsigned int copy_to_unsafe_ll (void *to,const void *from,unsigned int n); +unsigned int copy_from_unsafe_ll (void *to,const void *from,unsigned int n);extern long __get_user_bad (void );extern void __put_user_bad (void );
Quoted 6 lines
xen/include/asm-x86/uaccess.h line 27
Quoted 2 lines
xen/include/asm-x86/uaccess.h +2
-0+#define UA_KEEP(args...) args +#define UA_DROP(args...)I assume UA means user access, and since you have dropped other uses of user and changed to guest instead I wonder if we should name this just A_{KEEP/DROP}.
Like in the name of the file I mean to see 'u' stand for "unsafe" going forward. (A single letter name prefix would also seem more prone to future collisions to me.)
Jan
Re: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
Quoted 30 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
xen/arch/x86/usercopy.c +5
-1@@ -10,12 +10,19 @@#include <xen/sched.h> #include <asm/uaccess.h> -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n) +#ifndef GUARD +# define GUARD UA_KEEP +#endif + +unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n) {unsigned dummy;Quoted 8 lines
xen/arch/x86/usercopy.c line 24
Quoted 4 lines
xen/arch/x86/usercopy.c +2
-0stac (); asmvolatile ( + GUARD( + " guest_access_mask_ptr %[to], %q[scratch1], %q[scratch2]\n"Don't you need to also take 'n' into account here to assert that the address doesn't end in hypervisor address space? Or that's fine as speculation wouldn't go that far?
Like elsewhere this leverages that the hypervisor VA range starts immediately after the non-canonical hole. I'm unaware of speculation being able to cross over that hole.
Quoted 6 lines
Quoted 2 lines
I also wonder why this needs to be done in assembly, could you check the address(es) using C?
For this to be efficient (in avoiding speculation) the insn sequence would better not have any conditional jumps. I don't think the compiler can be told so.
Why not use evaluate_nospec to check the address like we do in other places?
Thanks, Roger.
Re: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
Quoted 34 lines · Roger Pau Monné
Quoted 33 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
xen/arch/x86/usercopy.c +5
-1@@ -10,12 +10,19 @@#include <xen/sched.h> #include <asm/uaccess.h> -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n) +#ifndef GUARD +# define GUARD UA_KEEP +#endif + +unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n) {unsigned dummy;Quoted 8 lines
xen/arch/x86/usercopy.c line 24
Quoted 4 lines
xen/arch/x86/usercopy.c +2
-0stac (); asmvolatile ( + GUARD( + " guest_access_mask_ptr %[to], %q[scratch1], %q[scratch2]\n"Don't you need to also take 'n' into account here to assert that the address doesn't end in hypervisor address space? Or that's fine as speculation wouldn't go that far?
Like elsewhere this leverages that the hypervisor VA range starts immediately after the non-canonical hole. I'm unaware of speculation being able to cross over that hole.
Quoted 2 lines
I also wonder why this needs to be done in assembly, could you check the address(es) using C?
Quoted 6 lines
Quoted 3 lines
For this to be efficient (in avoiding speculation) the insn sequence would better not have any conditional jumps. I don't think the compiler can be told so.
Why not use
evaluate_nospecto check the address like we do in other places?
Because LFENCE is far heavier than what we do here, which is effectively a (distant) relative of array_index_nospec().
Jan
Re: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
In reply to Jan BeulichQuoted 96 lines · Jan Beulich
Inspired by
https://lore.kernel.org/lkml/f12e7d3cecf41b2c29734ea45a393be21d4a8058.1597848273.git.jpoimboe@redhat.com/
and prior work in that area of x86 Linux, suppress speculation with guest specified pointer values by suitably masking the addresses to non-canonical space in case they fall into Xen's virtual address range.Introduce a new
Kconfigcontrol.Note that it is necessary in such code to avoid using "m" kind operands:
If we didn't, there would be no guarantee that the register passed toguest_access_mask_ptris also the (base) one used for the memory access.As a minor unrelated change in
get_unsafe_asm()the unnecessary "itype" parameter gets dropped and the XOR on the fixup path gets changed to be a 32-bit one in all cases: This way we avoid pointless REX.W or operand size overrides, or writes to partial registers.Requested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>---
The insn sequence chosen is certainly up for discussion; I've picked this one despite the RCR because alternatives I could come up with, likemov $(HYPERVISOR_VIRT_END), %rax mov $~0, %rdx mov $0x7fffffffffffffff, %rcx cmp %rax, %rdi cmovb %rcx, %rdx and %rdx, %rdiweren't necessarily better: Either, as above, they are longer and require a 3rd scratch register, or they also utilize the carry flag in some similar way.
---
Judging from the comment ahead ofput_unsafe_asm()we might as well not tell gcc at all anymore about the memory access there, now that there's no use of the operand anymore in the assembly code.xen/arch/x86/usercopy.c +14
-2@@ -10,12 +10,19 @@#include <xen/sched.h> #include <asm/uaccess.h> -unsigned __copy_to_user_ll (void __user *to,const void *from,unsigned n) +#ifndef GUARD +# define GUARD UA_KEEP +#endif + +unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n) {unsigned dummy;stac (); asmvolatile ( + GUARD( +" guest_access_mask_ptr %[to], %q[scratch1], %q[scratch2]\n" + ) " cmp $"STR(2*BYTES_PER_LONG-1)", %[cnt]\n"" jbe 1f\n" " mov %k[to], %[cnt]\n" @@ -42,6 +49,7 @@ unsigned __copy_to_user_ll(void __user *_ASM_EXTABLE (1b ,2b ) : [cnt] "+c" (n), [to] "+D" (to), [from] "+S" (from), [aux] "=&r" (dummy) + GUARD(, [scratch1] "=&r" (dummy), [scratch2] "=&r" (dummy)) : "[aux]" (n) : "memory" );clac ();@@ -49,12 +57,15 @@ unsigned __copy_to_user_ll(void __user *return n; } -unsigned __copy_from_user_ll (void *to,const void __user *from,unsigned n) +unsigned int copy_from_guest_ll (void *to,const void __user *from,unsigned int n) {unsigned dummy;stac (); asmvolatile ( + GUARD( +" guest_access_mask_ptr %[from], %q[scratch1], %q[scratch2]\n" + ) " cmp $"STR(2*BYTES_PER_LONG-1)", %[cnt]\n"" jbe 1f\n" " mov %k[to], %[cnt]\n" @@ -87,6 +98,7 @@ unsigned __copy_from_user_ll(void *to, c_ASM_EXTABLE (1b ,6b ) : [cnt] "+c" (n), [to] "+D" (to), [from] "+S" (from), [aux] "=&r" (dummy) + GUARD(, [scratch1] "=&r" (dummy), [scratch2] "=&r" (dummy)) : "[aux]" (n) : "memory" );clac ();
xen/arch/x86/usercopy.c unsigned __copy_from_user_ll(void *to, c · line 109
Quoted 4 lines
xen/arch/x86/usercopy.c +1
-0@@ -94,6 +106,8 @@ unsigned __copy_from_user_ll(void *to, creturn n; } +#if GUARD (1 ) +0
Why do you need the '+ 0' here? I guess it's to prevent the preprocessor from complaining when GUARD(1) gets replaced by nothing?
Quoted 85 lines
xen/arch/x86/usercopy.c +16
-1+/** * copy_to_user: - Copy a block of data into user space. * @to: Destination address, in user space. @@ -128,8 +142,11 @@ unsigned clear_user(void __user *to, uns {if (access_ok (to, n) ) { +long dummy; +stac (); asmvolatile ( +" guest_access_mask_ptr %[to], %[scratch1], %[scratch2]\n" "0: rep stos" __OS"\n" " mov %[bytes], %[cnt]\n" "1: rep stosb\n"@@ -140,7 +157,8 @@ unsigned clear_user(void __user *to, uns".previous\n" _ASM_EXTABLE(0b,3b)_ASM_EXTABLE (1b ,2b ) - : [cnt] "=&c" (n), [to] "+D" (to) + : [cnt] "=&c" (n), [to] "+D" (to), [scratch1] "=&r" (dummy), + [scratch2] "=&r" (dummy) : [bytes] "r" (n & (BYTES_PER_LONG - 1)), [longs] "0" (n / BYTES_PER_LONG), "a" (0) );clac ();@@ -174,6 +192,16 @@ unsigned copy_from_user(void *to, const return n; } +# undef GUARD +# define GUARD UA_DROP +# define copy_to_guest_ll copy_to_unsafe_ll +# define copy_from_guest_ll copy_from_unsafe_ll +# undef __user +# define __user +# include __FILE__ + +#endif /* GUARD(1) */ +/* * Local variables: * mode: C xen/arch/x86/x86_64/entry.S +2
-0@@ -446,6 +446,8 @@ UNLIKELY_START(g, create_bounce_frame_ba jmp asm_domain_crash_synchronous /* Does not return */ __UNLIKELY_END(create_bounce_frame_bad_sp) + guest_access_mask_ptr %rsi, %rax, %rcx + #define STORE_GUEST_STACK(reg, n) \ 0: movq %reg,(n)*8(%rsi); \ _ASM_EXTABLE(0b, domain_crash_page_fault_ ## n ## x8)xen/common/Kconfig +18
-0@@ -114,6 +114,24 @@ config SPECULATIVE_HARDEN_BRANCH If unsure, say Y. +config SPECULATIVE_HARDEN_GUEST_ACCESS + bool "Speculative PV Guest Memory Access Hardening" + default y + depends on PV + help + Contemporary processors may use speculative execution as a + performance optimisation, but this can potentially be abused by an + attacker to leak data via speculative sidechannels. + + One source of data leakage is via speculative accesses to hypervisor + memory through guest controlled values used to access guest memory. + + When enabled, code paths accessing PV guest memory will have guest + controlled addresses massaged such that memory accesses through them + won't touch hypervisor address space. + + If unsure, say Y. + endmenu config HYPFSxen/include/asm-x86/asm-defns.h +0
-0@@ -44,3 +44,16 @@ .macro INDIRECT_JMP arg:req
xen/include/asm-x86/asm-defns.h line 54
Quoted 10 lines
xen/include/asm-x86/asm-defns.h +8
-0INDIRECT_BRANCH jmp \arg .endm + +.macro guest_access_mask_ptr ptr:req, scratch1:req, scratch2:req +#if defined(CONFIG_SPECULATIVE_HARDEN_GUEST_ACCESS) + mov $(HYPERVISOR_VIRT_END - 1), \scratch1 + mov $~0, \scratch2 + cmp \ptr, \scratch1 + rcr $1, \scratch2 + and \scratch2, \ptr
If my understanding is correct, that's equivalent to:
ptr &= ~0ull >> (ptr < HYPERVISOR_VIRT_END);
It might be helpful to add this as a comment, to clarify the indented functionality of the assembly bit.
I wonder if the C code above can generate any jumps? As you pointed out, we already use something similar in array_index_mask_nospec and that's fine to do in C.
Thanks, Roger.
Re: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
Quoted 9 lines · Roger Pau Monné
xen/arch/x86/usercopy.c unsigned __copy_from_user_ll(void *to, c · line 109
Quoted 5 lines · Jan Beulich
xen/arch/x86/usercopy.c +1
-0@@ -94,6 +106,8 @@ unsigned __copy_from_user_ll(void *to, creturn n; } +#if GUARD (1 ) +0 Why do you need the '+ 0' here? I guess it's to prevent the preprocessor from complaining when GUARD(1) gets replaced by nothing?
Yes. "#if" with nothing after it is an error from all I know.
Quoted 17 lines
Quoted 4 lines
xen/include/asm-x86/asm-defns.h +0
-0@@ -44,3 +44,16 @@ .macro INDIRECT_JMP arg:reqxen/include/asm-x86/asm-defns.h line 54
Quoted 10 lines
xen/include/asm-x86/asm-defns.h +8
-0INDIRECT_BRANCH jmp \arg .endm + +.macro guest_access_mask_ptr ptr:req, scratch1:req, scratch2:req +#if defined(CONFIG_SPECULATIVE_HARDEN_GUEST_ACCESS) + mov $(HYPERVISOR_VIRT_END - 1), \scratch1 + mov $~0, \scratch2 + cmp \ptr, \scratch1 + rcr $1, \scratch2 + and \scratch2, \ptrIf my understanding is correct, that's equivalent to:
Quoted 8 lines
ptr &= ~0ull >> (ptr <
HYPERVISOR_VIRT_END);It might be helpful to add this as a comment, to clarify the indented functionality of the assembly bit.
I wonder if the C code above can generate any jumps? As you pointed out, we already use something similar in
array_index_mask_nospecand that's fine to do in C.
Note how array_index_mask_nospec() gets away without any use of relational operators. They're what poses the risk of getting translated to branches. (Quite likely the compiler wouldn't use any in the case here, as the code can easily get away without, but we don't want to chance it. Afaict it would instead use a 3rd scratch register, so register pressure might still lead to using a branch instead in some exceptional case.)
Jan
Roger Pau Monné
Reviewed-byRe: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
Quoted 39 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monné
xen/arch/x86/usercopy.c unsigned __copy_from_user_ll(void *to, c · line 109
Quoted 5 lines · Jan Beulich
xen/arch/x86/usercopy.c +1
-0@@ -94,6 +106,8 @@ unsigned __copy_from_user_ll(void *to, creturn n; } +#if GUARD (1 ) +0 Why do you need the '+ 0' here? I guess it's to prevent the preprocessor from complaining when GUARD(1) gets replaced by nothing?
Yes. "#if" with nothing after it is an error from all I know.
Quoted 17 lines
Quoted 4 lines
xen/include/asm-x86/asm-defns.h +0
-0@@ -44,3 +44,16 @@ .macro INDIRECT_JMP arg:reqxen/include/asm-x86/asm-defns.h line 54
Quoted 10 lines
xen/include/asm-x86/asm-defns.h +8
-0INDIRECT_BRANCH jmp \arg .endm + +.macro guest_access_mask_ptr ptr:req, scratch1:req, scratch2:req +#if defined(CONFIG_SPECULATIVE_HARDEN_GUEST_ACCESS) + mov $(HYPERVISOR_VIRT_END - 1), \scratch1 + mov $~0, \scratch2 + cmp \ptr, \scratch1 + rcr $1, \scratch2 + and \scratch2, \ptrIf my understanding is correct, that's equivalent to:
Quoted 8 lines
ptr &= ~0ull >> (ptr <
HYPERVISOR_VIRT_END);It might be helpful to add this as a comment, to clarify the indented functionality of the assembly bit.
I wonder if the C code above can generate any jumps? As you pointed out, we already use something similar in
array_index_mask_nospecand that's fine to do in C.
Quoted 7 lines
Note how
array_index_mask_nospec()gets away without any use of relational operators. They're what poses the risk of getting translated to branches. (Quite likely the compiler wouldn't use any in the case here, as the code can easily get away without, but we don't want to chance it. Afaict it would instead use a 3rd scratch register, so register pressure might still lead to using a branch instead in some exceptional case.)
I see, it's not easy to build such construct without using any relational operator. Would you mind adding the C equivalent next to assembly chunk?
I don't think I have further comments:
Thanks, Roger.
Re: [PATCH 04/17] x86/PV: harden guest memory accesses against speculative abuse
Quoted 37 lines · Roger Pau Monné
Quoted 28 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich
xen/include/asm-x86/asm-defns.h +0
-0@@ -44,3 +44,16 @@ .macro INDIRECT_JMP arg:reqxen/include/asm-x86/asm-defns.h line 54
Quoted 10 lines
xen/include/asm-x86/asm-defns.h +8
-0INDIRECT_BRANCH jmp \arg .endm + +.macro guest_access_mask_ptr ptr:req, scratch1:req, scratch2:req +#if defined(CONFIG_SPECULATIVE_HARDEN_GUEST_ACCESS) + mov $(HYPERVISOR_VIRT_END - 1), \scratch1 + mov $~0, \scratch2 + cmp \ptr, \scratch1 + rcr $1, \scratch2 + and \scratch2, \ptrIf my understanding is correct, that's equivalent to:
Quoted 8 lines
ptr &= ~0ull >> (ptr <
HYPERVISOR_VIRT_END);It might be helpful to add this as a comment, to clarify the indented functionality of the assembly bit.
I wonder if the C code above can generate any jumps? As you pointed out, we already use something similar in
array_index_mask_nospecand that's fine to do in C.Quoted 7 lines
Note how
array_index_mask_nospec()gets away without any use of relational operators. They're what poses the risk of getting translated to branches. (Quite likely the compiler wouldn't use any in the case here, as the code can easily get away without, but we don't want to chance it. Afaict it would instead use a 3rd scratch register, so register pressure might still lead to using a branch instead in some exceptional case.)
Quoted 3 lines
I see, it's not easy to build such construct without using any relational operator. Would you mind adding the C equivalent next to assembly chunk?
Sure:
/*
* Here we want
*
* ptr &= ~0ull >> (ptr < HYPERVISOR_VIRT_END);
*
* but guaranteed without any conditional branches (hence in assembly).
*/Quoted 3 lines
I don't think I have further comments:
Reviewed-by Roger Pau Monné <[email protected]>
Thanks much!
Jan
PATCH 5/17 x86: rename {get,put}_user() to {get,put}_guest()
In reply to Jan BeulichBring them (back) in line with __{get,put}_guest().
xen/arch/x86/domain.c +20 -20
@@ -1626,19 +1626,19 @@ static void load_segments(struct vcpu *nif ( !ring_1 (regs) ) { - ret =put_user (regs->ss, esp-1 ); - ret |=put_user (regs->esp, esp-2 ); + ret =put_guest (regs->ss, esp -1 ); + ret |=put_guest (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 ) ) +put_guest (rflags, esp -1 ) | +put_guest (cs_and_mask, esp -2 ) | +put_guest (regs->eip, esp -3 ) | +put_guest (uregs->gs, esp -4 ) | +put_guest (uregs->fs, esp -5 ) | +put_guest (uregs->es, esp -6 ) | +put_guest (uregs->ds, esp -7 ) ) {gprintk (XENLOG_ERR ,"error while creating compat failsafe callback frame\n" );
@@ -1667,17 +1667,17 @@ static void load_segments(struct vcpu *n 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 ) ) +if (put_guest (regs->ss, rsp -1 ) | +put_guest (regs->rsp, rsp -2 ) | +put_guest (rflags, rsp -3 ) | +put_guest (cs_and_mask, rsp -4 ) | +put_guest (regs->rip, rsp -5 ) | +put_guest (uregs->gs, rsp -6 ) | +put_guest (uregs->fs, rsp -7 ) | +put_guest (uregs->es, rsp -8 ) | +put_guest (uregs->ds, rsp -9 ) | +put_guest (regs->r11, rsp -10 ) | +put_guest (regs->rcx, rsp -11 ) ) {gprintk (XENLOG_ERR ,"error while creating failsafe callback frame\n" );
xen/include/asm-x86/uaccess.h +14 -19
@@ -27,14 +27,12 @@ extern void __put_user_bad(void); #define UA_DROP(args...)/** -* get_user: - Get a simple variable from user space. +* get_guest: - Get a simple variable from guest space. * @x: Variable to store result. -* @ptr: Source address, in user space. -* -* Context: User context only. This function may sleep. +* @ptr: Source address, in guest space. * -* This macro copies a single simple variable from user space to kernel -* space. It supports simple types like char and int, but not larger +* This macro load a single simple variable from guest space. +* It supports simple types like char and int, but not larger * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and the result of
@@ -43,18 +41,15 @@ extern void __put_user_bad(void);* Returns zero on success, or -EFAULT on error. * On error, the variable @x is set to zero. */ -#define get_user (x,ptr) \ - __get_user_check((x),(ptr),sizeof(*(ptr))) +#define get_guest (x, ptr) get_guest_check(x, ptr, sizeof(*(ptr)))/** -* put_user: - Write a simple value into user space. -* @x: Value to copy to user space. -* @ptr: Destination address, in user space. -* -* Context: User context only. This function may sleep. +* put_guest: - Write a simple value into guest space. +* @x: Value to store in guest space. +* @ptr: Destination address, in guest space. * -* This macro copies a single simple value from kernel space to user -* space. It supports simple types like char and int, but not larger +* This macro stores a single simple value from to guest space. +* It supports simple types like char and int, but not larger * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable
@@ -62,8 +57,8 @@ extern void __put_user_bad(void);* * Returns zero on success, or -EFAULT on error. */ -#define put_user (x,ptr) \ - __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr))) +#define put_guest (x, ptr) \ + put_guest_check((__typeof__(*(ptr)))(x), ptr, sizeof(*(ptr)))/** * __get_guest: - Get a simple variable from guest space, with less checking.
@@ -119,7 +114,7 @@ extern void __put_user_bad(void); err_; \ }) -#define __put_user_check (x, ptr, size) \ +#define put_guest_check (x, ptr, size) \ ({ \ __typeof__(*(ptr)) __user *ptr_ = (ptr); \ __typeof__(size) size_ = (size); \
@@ -141,7 +136,7 @@ extern void __put_user_bad(void); err_; \ }) -#define __get_user_check (x, ptr, size) \ +#define get_guest_check (x, ptr, size) \ ({ \ __typeof__(*(ptr)) __user *ptr_ = (ptr); \ __typeof__(size) size_ = (size); \
PATCH 6/17 x86/gdbsx: convert "user" to "guest" accesses
In reply to Jan BeulichUsing copy_{from,to}_user(), this code was assuming to be called only by PV guests. Use copy_{from,to}_guest() instead, transforming the incoming structure field into a guest handle (the field should really have been one in the first place). Also do not transform the debuggee address into a pointer.
As a not originally intended side effect this also fixes a bug in dbg_rw_guest_mem(): At the end of the loop "addr" was incremented, but then in the next loop iteration (with the variable also having gone out of scope inbetween) re-initialized from the function parameter.
xen/arch/x86/debug.c +9 -11
@@ -108,14 +108,14 @@ dbg_pv_va2mfn(dbgva_t vaddr, struct doma }/* Returns: number of bytes remaining to be copied */ -static unsigned int dbg_rw_guest_mem (struct domain *dp,void *__user gaddr, -void *__user buf,unsigned int len, -bool toaddr,uint64_t pgd3) +static unsigned int dbg_rw_guest_mem (struct domain *dp,unsigned long addr, +XEN_GUEST_HANDLE_PARAM (void ) buf, +unsigned int len,bool toaddr, +uint64_t pgd3) {while ( len >0 ) {char *va; -unsigned long addr = (unsigned long )gaddr;mfn_t mfn;gfn_t gfn =INVALID_GFN ;unsigned long pagecnt;
@@ -133,20 +133,18 @@ static unsigned int dbg_rw_guest_mem(strif ( toaddr ) { -copy_from_user (va, buf, pagecnt);/* va = buf */ +copy_from_guest (va, buf, pagecnt);paging_mark_dirty (dp, mfn); }else - { -copy_to_user (buf, va, pagecnt);/* buf = va */ - } +copy_to_guest (buf, va, pagecnt);unmap_domain_page (va);if ( !gfn_eq (gfn,INVALID_GFN ) )put_gfn (dp,gfn_x (gfn)); addr += pagecnt; - buf += pagecnt; +guest_handle_add_offset (buf, pagecnt); len -= pagecnt; }
@@ -160,7 +158,7 @@ static unsigned int dbg_rw_guest_mem(str* pgd3: value of init_mm.pgd[3] in guest. see above. * Returns: number of bytes remaining to be copied. */ -unsigned int dbg_rw_mem (void *__user addr,void *__user buf, +unsigned int dbg_rw_mem (unsigned long gva,XEN_GUEST_HANDLE_PARAM (void ) buf,unsigned int len,domid_t domid,bool toaddr,uint64_t pgd3) {
@@ -169,7 +167,7 @@ unsigned int dbg_rw_mem(void * __user adif ( d ) {if ( !d->is_dying ) - len =dbg_rw_guest_mem (d, addr, buf, len, toaddr, pgd3); + len =dbg_rw_guest_mem (d, gva, buf, len, toaddr, pgd3);rcu_unlock_domain (d); }
xen/arch/x86/domctl.c +2 -4
@@ -40,10 +40,8 @@#ifdef CONFIG_GDBSX static int gdbsx_guest_mem_io (domid_t domid,struct xen_domctl_gdbsx_memio *iop) { -void *__user gva = (void *)iop->gva, *__user uva = (void *)iop->uva; - - iop->remain =dbg_rw_mem (gva, uva, iop->len, domid, - !!iop->gwr, iop->pgd3val); + iop->remain =dbg_rw_mem (iop->gva,guest_handle_from_ptr (iop->uva, void), + iop->len, domid, iop->gwr, iop->pgd3val);return iop->remain ? -EFAULT :0 ; }
xen/include/asm-x86/debugger.h +2 -2
@@ -93,9 +93,9 @@ static inline bool debugger_trap_entry(#endif #ifdef CONFIG_GDBSX -unsigned int dbg_rw_mem (void *__user addr,void *__user buf, +unsigned int dbg_rw_mem (unsigned long gva,XEN_GUEST_HANDLE_PARAM (void ) buf,unsigned int len,domid_t domid,bool toaddr, -uint64_t pgd3); +unsigned long pgd3);#endif #endif /* __X86_DEBUGGER_H__ */
PATCH 7/17 x86: rename copy_{from,to}_user() to copy_{from,to}_guest_pv()
In reply to Jan BeulichBring them (back) in line with __copy_{from,to}_guest_pv(). Since it falls in the same group, also convert clear_user(). Instead of adjusting __raw_clear_guest(), drop it - it's unused and would require a non- checking __clear_guest_pv() which we don't have.
Add previously missing __user at some call sites and in the function declarations.
xen/arch/x86/pv/emul-inv-op.c +3 -2
@@ -33,7 +33,7 @@ static int emulate_forced_invalid_op(str eip = regs->rip;/* Check for forced emulation signature: ud2 ; .ascii "xen". */ -if ( (rc =copy_from_user (sig, (char *)eip,sizeof (sig))) !=0 ) +if ( (rc =copy_from_guest_pv (sig, (char __user *)eip,sizeof (sig))) !=0 ) {pv_inject_page_fault (0 , eip +sizeof (sig) - rc);return EXCRET_fault_fixed;
@@ -43,7 +43,8 @@ static int emulate_forced_invalid_op(str eip +=sizeof (sig);/* We only emulate CPUID. */ -if ( ( rc =copy_from_user (instr, (char *)eip,sizeof (instr))) !=0 ) +if ( (rc =copy_from_guest_pv (instr, (char __user *)eip, +sizeof (instr))) !=0 ) {pv_inject_page_fault (0 , eip +sizeof (instr) - rc);return EXCRET_fault_fixed;
xen/arch/x86/pv/iret.c +2 -2
@@ -54,8 +54,8 @@ unsigned long do_iret(void)struct iret_context iret_saved;struct vcpu *v = current; -if (unlikely (copy_from_user (&iret_saved, (void *)regs->rsp, -sizeof (iret_saved))) ) +if (unlikely (copy_from_guest_pv (&iret_saved, (void __user *)regs->rsp, +sizeof (iret_saved))) ) {gprintk (XENLOG_ERR ,"Fault while reading IRET context from guest stack\n" );
xen/arch/x86/pv/ro-page-fault.c +2 -1
@@ -90,7 +90,8 @@ static int ptwr_emulated_update(unsigned/* Align address; read full word. */ addr &= ~(sizeof (full) -1 ); -if ( (rc =copy_from_user (&full, (void *)addr,sizeof (full))) !=0 ) +if ( (rc =copy_from_guest_pv (&full, (void __user *)addr, +sizeof (full))) !=0 ) { x86_emul_pagefault(0, /* Read fault. */ addr + sizeof(full) - rc,
xen/arch/x86/usercopy.c +15 -18
@@ -109,19 +109,17 @@ unsigned int copy_from_guest_ll(void *to#if GUARD (1 ) +0 /** -* copy_to_user: - Copy a block of data into user space. -* @to: Destination address, in user space. -* @from: Source address, in kernel space. +* copy_to_guest_pv: - Copy a block of data into guest space. +* @to: Destination address, in guest space. +* @from: Source address, in hypervisor space. * @n: Number of bytes to copy. * -* Context: User context only. This function may sleep. -* -* Copy data from kernel space to user space. +* Copy data from hypervisor space to guest space. * * Returns number of bytes that could not be copied. * On success, this will be zero. */ -unsigned copy_to_user (void __user *to,const void *from,unsigned n) +unsigned int copy_to_guest_pv (void __user *to,const void *from,unsigned int n) {if (access_ok (to, n) ) n =__copy_to_guest_pv (to, from, n);
@@ -129,16 +127,16 @@ unsigned copy_to_user(void __user *to, c }/** -* clear_user: - Zero a block of memory in user space. -* @to: Destination address, in user space. +* clear_guest_pv: - Zero a block of memory in guest space. +* @to: Destination address, in guest space. * @n: Number of bytes to zero. * -* Zero a block of memory in user space. +* Zero a block of memory in guest space. * * Returns number of bytes that could not be cleared. * On success, this will be zero. */ -unsigned clear_user (void __user *to,unsigned n) +unsigned int clear_guest_pv (void __user *to,unsigned int n) {if (access_ok (to, n) ) {
@@ -168,14 +166,12 @@ unsigned clear_user(void __user *to, uns }/** -* copy_from_user: - Copy a block of data from user space. -* @to: Destination address, in kernel space. -* @from: Source address, in user space. +* copy_from_guest_pv: - Copy a block of data from guest space. +* @to: Destination address, in hypervisor space. +* @from: Source address, in guest space. * @n: Number of bytes to copy. * -* Context: User context only. This function may sleep. -* -* Copy data from user space to kernel space. +* Copy data from guest space to hypervisor space. * * Returns number of bytes that could not be copied. * On success, this will be zero.
@@ -183,7 +179,8 @@ unsigned clear_user(void __user *to, uns* If some data could not be copied, this function will pad the copied * data to the requested size using zero bytes. */ -unsigned copy_from_user (void *to,const void __user *from,unsigned n) +unsigned int copy_from_guest_pv (void *to,const void __user *from, +unsigned int n) {if (access_ok (from, n) ) n =__copy_from_guest_pv (to, from, n);
xen/include/asm-x86/guest_access.h +3 -7
@@ -16,15 +16,15 @@#define raw_copy_to_guest (dst, src, len) \ (is_hvm_vcpu(current) ? \ copy_to_user_hvm((dst), (src), (len)) : \ - copy_to_user((dst), (src), (len))) + copy_to_guest_pv(dst, src, len))#define raw_copy_from_guest (dst, src, len) \ (is_hvm_vcpu(current) ? \ copy_from_user_hvm((dst), (src), (len)) : \ - copy_from_user((dst), (src), (len))) + copy_from_guest_pv(dst, src, len))#define raw_clear_guest (dst, len) \ (is_hvm_vcpu(current) ? \ clear_user_hvm((dst), (len)) : \ - clear_user((dst), (len))) + clear_guest_pv(dst, len))#define __raw_copy_to_guest (dst, src, len) \ (is_hvm_vcpu(current) ? \ copy_to_user_hvm((dst), (src), (len)) : \
@@ -33,10 +33,6 @@ (is_hvm_vcpu (current) ? \copy_from_user_hvm ((dst), (src), (len)) : \__copy_from_guest_pv (dst, src, len)) -#define __raw_clear_guest (dst, len) \ - (is_hvm_vcpu(current) ? \ - clear_user_hvm((dst), (len)) : \ - clear_user((dst), (len)))/* * Pre-validate a guest handle.
xen/include/asm-x86/uaccess.h +5 -3
@@ -10,9 +10,11 @@#include <asm/x86_64/uaccess.h> -unsigned copy_to_user (void *to,const void *from,unsigned len); -unsigned clear_user (void *to,unsigned len); -unsigned copy_from_user (void *to,const void *from,unsigned len); +unsigned int copy_to_guest_pv (void __user *to,const void *from, +unsigned int len); +unsigned int clear_guest_pv (void __user *to,unsigned int len); +unsigned int copy_from_guest_pv (void *to,const void __user *from, +unsigned int len);/* Handles exceptions in both to and from, but doesn't do access_ok */ unsigned int copy_to_guest_ll (void __user *to,const void *from,unsigned int n);
PATCH 8/17 x86: move stac()/clac() from {get,put}_unsafe_asm() ...
In reply to Jan Beulich... to {get,put}_unsafe_size(). There's no need to have the macros expanded once per case label in the latter. This also makes the former well-formed single statements again. No change in generated code.
xen/include/asm-x86/uaccess.h +6 -6
@@ -155,7 +155,6 @@ struct __large_struct { unsigned long bu* aliasing issues. */ #define put_unsafe_asm (x, addr,GUARD , err, itype, rtype, ltype, errret) \ - stac(); \ __asm__ __volatile__( \ GUARD( \ " guest_access_mask_ptr %[ptr], %[scr1], %[scr2]\n" \
@@ -170,11 +169,9 @@ struct __large_struct { unsigned long bu : [ret] "+r" (err), [ptr] "=&r" (dummy_) \ GUARD(, [scr1] "=&r" (dummy_), [scr2] "=&r" (dummy_)) \ : [val] ltype (x), "m" (__m(addr)), \ - "[ptr]" (addr), [errno] "i" (errret)); \ - clac() + "[ptr]" (addr), [errno] "i" (errret))#define get_unsafe_asm (x, addr,GUARD , err, rtype, ltype, errret) \ - stac(); \ __asm__ __volatile__( \ GUARD( \ " guest_access_mask_ptr %[ptr], %[scr1], %[scr2]\n" \
@@ -191,12 +188,12 @@ struct __large_struct { unsigned long bu [ptr] "=&r" (dummy_) \ GUARD(, [scr1] "=&r" (dummy_), [scr2] "=&r" (dummy_)) \ : "m" (__m(addr)), "[ptr]" (addr), \ - [errno] "i" (errret)); \ - clac() + [errno] "i" (errret)) #define put_unsafe_size(x, ptr, size, grd, retval, errret) \ do { \ retval = 0; \ +stac (); \switch ( size ) \ { \long dummy_; \
@@ -214,6 +211,7 @@ do {break ; \default :__put_user_bad (); \ } \ +clac (); \ }while ( false ) #define put_guest_size(x, ptr, size, retval, errret) \
@@ -222,6 +220,7 @@ do {#define get_unsafe_size (x, ptr, size, grd, retval, errret) \ do { \ retval = 0; \ + stac(); \ switch ( size ) \ { \ long dummy_; \
@@ -231,6 +230,7 @@ do {case 8 :get_unsafe_asm (x, ptr, grd, retval,"" ,"=r" , errret);break ; \default :__get_user_bad (); \ } \ +clac (); \ }while ( false ) #define get_guest_size(x, ptr, size, retval, errret) \
PATCH 9/17 x86/PV: use get_unsafe() instead of copy_from_unsafe()
In reply to Jan BeulichThe former expands to a single (memory accessing) insn, which the latter does not guarantee. Yet we'd prefer to read consistent PTEs rather than risking a split read racing with an update done elsewhere.
xen/arch/x86/pv/mm.c +1 -3
@@ -41,9 +41,7 @@ l1_pgentry_t *map_guest_l1e(unsigned lonreturn NULL ;/* Find this l1e and its enclosing l1mfn in the linear map. */ -if (copy_from_unsafe (&l2e, - &__linear_l2_table[l2_linear_offset (linear)], -sizeof (l2_pgentry_t)) ) +if (get_unsafe (l2e, &__linear_l2_table[l2_linear_offset (linear)]) )return NULL ;/* Check flags that it will be safe to read the l1e. */
xen/arch/x86/pv/mm.h +1 -3
@@ -22,9 +22,7 @@ static inline l1_pgentry_t guest_get_efftoggle_guest_pt (curr);if (unlikely (!__addr_ok (linear)) || -copy_from_unsafe (&l1e, - &__linear_l1_table[l1_linear_offset (linear)], -sizeof (l1_pgentry_t)) ) +get_unsafe (l1e, &__linear_l1_table[l1_linear_offset (linear)]) ) l1e =l1e_empty ();if ( user_mode )
PATCH 10/17 x86/shadow: use get_unsafe() instead of copy_from_unsafe()
In reply to Jan BeulichThis is the slightly more direct way of getting at what we want, and better in line with shadow_write_entries()'s use of put_unsafe().
xen/arch/x86/mm/shadow/multi.c +15 -18
@@ -2614,10 +2614,9 @@ static int sh_page_fault(struct vcpu *v, {shadow_l2e_t sl2e;mfn_t gl1mfn; -if ( (copy_from_unsafe (&sl2e, - (sh_linear_l2_table (v) - +shadow_l2_linear_offset (va)), -sizeof (sl2e)) !=0 ) +if ( (get_unsafe (sl2e, + (sh_linear_l2_table (v) + +shadow_l2_linear_offset (va))) !=0 ) || !(shadow_l2e_get_flags (sl2e) & _PAGE_PRESENT) || !mfn_valid (gl1mfn =backpointer (mfn_to_page ( shadow_l2e_get_mfn(sl2e))))
@@ -2633,10 +2632,9 @@ static int sh_page_fault(struct vcpu *v,#endif /* SHOPT_OUT_OF_SYNC */ /* The only reasons for reserved bits to be set in shadow entries * are the two "magic" shadow_l1e entries. */ -if (likely ((copy_from_unsafe (&sl1e, - (sh_linear_l1_table (v) - +shadow_l1_linear_offset (va)), -sizeof (sl1e)) ==0 ) +if (likely ((get_unsafe (sl1e, + (sh_linear_l1_table (v) + +shadow_l1_linear_offset (va))) ==0 ) &&sh_l1e_is_magic (sl1e)) ) {
@@ -3311,9 +3309,9 @@ static bool sh_invlpg(struct vcpu *v, un/* This must still be a copy-from-unsafe because we don't have the * paging lock, and the higher-level shadows might disappear * under our feet. */ -if (copy_from_unsafe (&sl3e, (sh_linear_l3_table (v) - +shadow_l3_linear_offset (linear)), -sizeof (sl3e)) !=0 ) +if (get_unsafe (sl3e, + (sh_linear_l3_table (v) + +shadow_l3_linear_offset (linear))) !=0 ) {perfc_incr (shadow_invlpg_fault);return false;
@@ -3332,9 +3330,9 @@ static bool sh_invlpg(struct vcpu *v, un/* This must still be a copy-from-unsafe because we don't have the shadow * lock, and the higher-level shadows might disappear under our feet. */ -if (copy_from_unsafe (&sl2e, -sh_linear_l2_table (v) +shadow_l2_linear_offset (linear), -sizeof (sl2e)) !=0 ) +if (get_unsafe (sl2e, + (sh_linear_l2_table (v) + +shadow_l2_linear_offset (linear))) !=0 ) {perfc_incr (shadow_invlpg_fault);return false;
@@ -3375,10 +3373,9 @@ static bool sh_invlpg(struct vcpu *v, un* have the paging lock last time we checked, and the * higher-level shadows might have disappeared under our * feet. */ -if (copy_from_unsafe (&sl2e, -sh_linear_l2_table (v) - +shadow_l2_linear_offset (linear), -sizeof (sl2e)) !=0 ) +if (get_unsafe (sl2e, + (sh_linear_l2_table (v) + +shadow_l2_linear_offset (linear))) !=0 ) {perfc_incr (shadow_invlpg_fault);paging_unlock (d);
PATCH 11/17 x86/shadow: polish shadow_write_entries()
In reply to Jan BeulichFirst of all, avoid the initial dummy write: Try to write the actual new value instead, and start the loop from 1 if this was successful. Further, drop safe_write_entry() and use write_atomic() instead. This eliminates the need for the BUILD_BUG_ON() there at the same time.
Then
- use const and unsigned,
- drop a redundant NULL check,
- don't open-code PAGE_OFFSET() and IS_ALIGNED(),
- adjust comment style.
xen/arch/x86/mm/shadow/multi.c +28 -28
@@ -746,50 +746,50 @@ l1e_propagate_from_guest(struct vcpu *v,* functions which ever write (non-zero) data onto a shadow page. */ -static inline void safe_write_entry (void *dst,void *src) -/* Copy one PTE safely when processors might be running on the -* destination pagetable. This does *not* give safety against -* concurrent writes (that's what the paging lock is for), just -* stops the hardware picking up partially written entries. */ -{ -volatile unsigned long *d = dst; -unsigned long *s = src; -ASSERT (!((unsigned long ) d & (sizeof (shadow_l1e_t) -1 ))); -/* In 64-bit, sizeof(pte) == sizeof(ulong) == 1 word, -* which will be an atomic write, since the entry is aligned. */ -BUILD_BUG_ON (sizeof (shadow_l1e_t) !=sizeof (unsigned long )); - *d = *s; -} - -static inline void -shadow_write_entries (void *d,void *s,int entries,mfn_t mfn) -/* This function does the actual writes to shadow pages. +shadow_write_entries (void *d,const void *s,unsigned int entries,mfn_t mfn) +/* +* This function does the actual writes to shadow pages. * It must not be called directly, since it doesn't do the bookkeeping -* that shadow_set_l*e() functions do. */ +* that shadow_set_l*e() functions do. +* +* Copy PTEs safely when processors might be running on the +* destination pagetable. This does *not* give safety against +* concurrent writes (that's what the paging lock is for), just +* stops the hardware picking up partially written entries. +*/ {shadow_l1e_t *dst = d; -shadow_l1e_t *src = s; +const shadow_l1e_t *src = s;void *map =NULL ; -int i; +unsigned int i =0 ; -/* Because we mirror access rights at all levels in the shadow, an +/* +* Because we mirror access rights at all levels in the shadow, an * l2 (or higher) entry with the RW bit cleared will leave us with * no write access through the linear map. * We detect that by writing to the shadow with put_unsafe() and -* using map_domain_page() to get a writeable mapping if we need to. */ -if (put_unsafe (*dst, dst) ) +* using map_domain_page() to get a writeable mapping if we need to. +*/ +if (put_unsafe (*src, dst) ) {perfc_incr (shadow_linear_map_failed); map =map_domain_page (mfn); - dst = map + ((unsigned long )dst & (PAGE_SIZE -1 )); + dst = map +PAGE_OFFSET (dst); + } +else + { + ++src; + ++dst; + i =1 ; } +ASSERT (IS_ALIGNED ((unsigned long )dst,sizeof (*dst))); -for ( i =0 ; i < entries; i++ ) -safe_write_entry (dst++, src++); +for ( ; i < entries; i++ ) +write_atomic (&dst++->l1, src++->l1); -if ( map !=NULL )unmap_domain_page (map); +unmap_domain_page (map); }/* type is only used to distinguish grant map pages from ordinary RAM
PATCH 12/17 x86/shadow: move shadow_set_l<N>e() to their own source file
In reply to Jan BeulichThe few GUEST_PAGING_LEVELS dependencies (of shadow_set_l2e() only) can be easily expressed by function parameters; I suppose the extra indirect call is acceptable for the increasingly little used 32-bit non-PAE case. This way shadow_set_l[12]e(), each of which compiles to almost 1k of code, need building just once.
The implication is the need for some "relaxation" in types.h: The underlying PTE types don't vary anymore (and aren't expected to down the road), so they as well as some basic helpers can be exposed even in the new, artificial GUEST_PAGING_LEVELS == 0 case.
Almost pure code movement - exceptions are the conversion of "#if GUEST_PAGING_LEVELS == 2" to runtime conditionals and style corrections (including to avoid open-coding mfn_to_maddr() and PAGE_OFFSET()).
xen/arch/x86/mm/shadow/Makefile +1 -1
@@ -1,5 +1,5 @@ ifeq ($(CONFIG_SHADOW_PAGING),y) -obj-y += common.o guest_2.o guest_3.o guest_4.o +obj-y += common.o guest_2.o guest_3.o guest_4.o set.o obj-$(CONFIG_HVM) += hvm.o obj-$(CONFIG_PV) += pv.o else
xen/arch/x86/mm/shadow/multi.c +5 -385
404 lines not on this page · raw message
xen/arch/x86/mm/shadow/set.c new file +418 -0
418 lines not on this page · raw message
xen/arch/x86/mm/shadow/types.h +28 -16
127 lines not on this page · raw message
PATCH 13/17 x86/shadow: don't open-code SHF_* shorthands
In reply to Jan BeulichUse SHF_L1_ANY, SHF_32, SHF_PAE, as well as SHF_64, and introduce SHF_FL1_ANY.
Note that in shadow_audit_tables() this has the effect of no longer (I assume mistakenly, or else I don't see why the respective callback table entry isn't NULL) excluding SHF_L2H_64.
xen/arch/x86/mm/shadow/common.c +5 -21
@@ -1827,14 +1827,7 @@ int sh_remove_write_access(struct domain NULL /* unused */ }; -static const unsigned int callback_mask = -SHF_L1_32 - |SHF_FL1_32 - |SHF_L1_PAE - |SHF_FL1_PAE - |SHF_L1_64 - |SHF_FL1_64 - ; +static const unsigned int callback_mask =SHF_L1_ANY |SHF_FL1_ANY ;struct page_info *pg =mfn_to_page (gmfn);#if SHADOW_OPTIMIZATIONS &SHOPT_WRITABLE_HEURISTIC struct vcpu *curr = current;
@@ -2057,14 +2050,7 @@ int sh_remove_all_mappings(struct domain NULL /* unused */ }; -static const unsigned int callback_mask = -SHF_L1_32 - |SHF_FL1_32 - |SHF_L1_PAE - |SHF_FL1_PAE - |SHF_L1_64 - |SHF_FL1_64 - ; +static const unsigned int callback_mask =SHF_L1_ANY |SHF_FL1_ANY ;perfc_incr (shadow_mappings);if (sh_check_page_has_no_refs (page) )
@@ -3411,11 +3397,9 @@ void shadow_audit_tables(struct vcpu *v)/* Audit only the current mode's tables */ switch ( v->arch.paging.mode->guest_levels ) { -case 2 : mask = (SHF_L1_32 |SHF_FL1_32 |SHF_L2_32 );break ; -case 3 : mask = (SHF_L1_PAE |SHF_FL1_PAE |SHF_L2_PAE - |SHF_L2H_PAE );break ; -case 4 : mask = (SHF_L1_64 |SHF_FL1_64 |SHF_L2_64 - |SHF_L3_64 |SHF_L4_64 );break ; +case 2 : mask =SHF_32 ;break ; +case 3 : mask =SHF_PAE ;break ; +case 4 : mask =SHF_64 ;break ;default :BUG (); } }
xen/arch/x86/mm/shadow/private.h +1 -0
@@ -269,6 +269,7 @@ static inline void sh_terminate_list(str#define SHF_64 (SHF_L1_64|SHF_FL1_64|SHF_L2_64|SHF_L2H_64|SHF_L3_64|SHF_L4_64)#define SHF_L1_ANY (SHF_L1_32|SHF_L1_PAE|SHF_L1_64) +#define SHF_FL1_ANY (SHF_FL1_32|SHF_FL1_PAE|SHF_FL1_64)#if (SHADOW_OPTIMIZATIONS &SHOPT_OUT_OF_SYNC )/* Marks a guest L1 page table which is shadowed but not write-protected.
PATCH 14/17 x86/shadow: SH_type_l2h_shadow is PV-only
In reply to Jan Beulich..., i.e. being used only with 4 guest paging levels. Drop its L2/PAE alias and adjust / drop conditionals. Use >= 4 where touching them anyway, in preparation for 5-level paging.
xen/arch/x86/mm/shadow/multi.c +12 -17
@@ -334,7 +334,7 @@ static void sh_audit_gw(struct vcpu *v,if (mfn_valid ((smfn =get_shadow_status (d, gw->l2mfn, SH_type_l2_shadow))) ) (void )sh_audit_l2_table (v, smfn,INVALID_MFN ); -#if GUEST_PAGING_LEVELS ==3 +#if GUEST_PAGING_LEVELS >=4 /* 32-bit PV only */ if (mfn_valid ((smfn =get_shadow_status (d, gw->l2mfn, SH_type_l2h_shadow))) ) (void )sh_audit_l2_table (v, smfn,INVALID_MFN );
@@ -937,7 +937,8 @@ sh_make_shadow(struct vcpu *v, mfn_t gmf/* Lower-level shadow, not yet linked form a higher level */ mfn_to_page (smfn)->up =0 ; -#if GUEST_PAGING_LEVELS ==4 +#if GUEST_PAGING_LEVELS >=4 +#if (SHADOW_OPTIMIZATIONS &SHOPT_LINUX_L3_TOPLEVEL )if ( shadow_type == SH_type_l4_64_shadow &&unlikely (d->arch.paging.shadow.opt_flags &SHOPT_LINUX_L3_TOPLEVEL ) )
@@ -969,14 +970,12 @@ sh_make_shadow(struct vcpu *v, mfn_t gmf } }#endif -#endif // Create the Xen mappings... if ( !shadow_mode_external (d) ) {switch (shadow_type) { -#if GUEST_PAGING_LEVELS ==4 case SH_type_l4_shadow: {shadow_l4e_t *l4t =map_domain_page (smfn);
@@ -988,8 +987,7 @@ sh_make_shadow(struct vcpu *v, mfn_t gmfunmap_domain_page (l4t); }break ; -#endif -#if GUEST_PAGING_LEVELS >=3 +case SH_type_l2h_shadow:BUILD_BUG_ON (sizeof (l2_pgentry_t) !=sizeof (shadow_l2e_t));if (is_pv_32bit_domain (d) )
@@ -1000,11 +998,12 @@ sh_make_shadow(struct vcpu *v, mfn_t gmf unmap_domain_page(l2t); }break ; -#endif default :/* Do nothing */ break ; } } +#endif /* GUEST_PAGING_LEVELS >= 4 */ +shadow_promote (d, gmfn, shadow_type);set_shadow_status (d, gmfn, shadow_type, smfn);
@@ -1334,7 +1333,7 @@ void sh_destroy_l2_shadow(struct domainSHADOW_DEBUG (DESTROY_SHADOW ,"%" PRI_mfn"\n" ,mfn_x (smfn)); -#if GUEST_PAGING_LEVELS >=3 +#if GUEST_PAGING_LEVELS >=4 ASSERT (t == SH_type_l2_shadow || t == SH_type_l2h_shadow);#else ASSERT (t == SH_type_l2_shadow);
@@ -1858,7 +1857,7 @@ int sh_map_and_validate_gl2he(struct vcpu *v, mfn_t gl2mfn, void *new_gl2p, u32 size) { -#if GUEST_PAGING_LEVELS >= 3 +#if GUEST_PAGING_LEVELS >= 4 return sh_map_and_validate(v, gl2mfn, new_gl2p, size, SH_type_l2h_shadow, shadow_l2_index,
@@ -3359,9 +3358,7 @@ sh_update_cr3(struct vcpu *v, int do_loc gl2gfn =guest_l3e_get_gfn (gl3e[i]); gl2mfn =get_gfn_query_unlocked (d,gfn_x (gl2gfn), &p2mt);if (p2m_is_ram (p2mt) ) -sh_set_toplevel_shadow (v, i, gl2mfn, (i ==3 ) - ? SH_type_l2h_shadow - : SH_type_l2_shadow, +sh_set_toplevel_shadow (v, i, gl2mfn, SH_type_l2_shadow, sh_make_shadow);else sh_set_toplevel_shadow(v, i, INVALID_MFN, 0,
@@ -3663,7 +3660,7 @@ void sh_clear_shadow_entry(struct domain (void )shadow_set_l1e (d, ep,shadow_l1e_empty (), p2m_invalid, smfn);break ;case SH_type_l2_shadow: -#if GUEST_PAGING_LEVELS >=3 +#if GUEST_PAGING_LEVELS >=4 case SH_type_l2h_shadow:#endif (void )shadow_set_l2e (d, ep,shadow_l2e_empty (), smfn);
@@ -4115,10 +4112,8 @@ int sh_audit_l3_table(struct vcpu *v, mf mfn =shadow_l3e_get_mfn (*sl3e); gmfn =get_shadow_status (d,get_gfn_query_unlocked ( d,gfn_x (gfn), &p2mt), - ((GUEST_PAGING_LEVELS ==3 || -is_pv_32bit_domain (d)) - && !shadow_mode_external (d) - && (guest_index (gl3e) %4 ) ==3 ) + (is_pv_32bit_domain (d) && +guest_index (gl3e) ==3 ) ? SH_type_l2h_shadow : SH_type_l2_shadow);if ( !mfn_eq (gmfn, mfn) )
xen/arch/x86/mm/shadow/types.h +0 -1
@@ -195,7 +195,6 @@ static inline shadow_l4e_t shadow_l4e_fr#define SH_type_l1_shadow SH_type_l1_pae_shadow#define SH_type_fl1_shadow SH_type_fl1_pae_shadow#define SH_type_l2_shadow SH_type_l2_pae_shadow -#define SH_type_l2h_shadow SH_type_l2h_pae_shadow#elif GUEST_PAGING_LEVELS ==4 #define SH_type_l1_shadow SH_type_l1_64_shadow#define SH_type_fl1_shadow SH_type_fl1_64_shadow
PATCH 15/17 x86/shadow: drop SH_type_l2h_pae_shadow
In reply to Jan BeulichThis is a remnant from 32-bit days, having no place anymore where a shadow of this type would be created.
---
hash_{domain,vcpu}_foreach() have a use each of literal 15. It's not clear to me what the proper replacement constant would be, as it doesn't look as if SH_type_monitor_table was meant.
xen/arch/x86/mm/shadow/common.c +1 -15
@@ -792,9 +792,6 @@ sh_validate_guest_entry(struct vcpu *v,if ( page->shadow_flags &SHF_L2_PAE ) result |=SHADOW_INTERNAL_NAME (sh_map_and_validate_gl2e,3 ) (v, gmfn, entry, size); -if ( page->shadow_flags &SHF_L2H_PAE ) - result |=SHADOW_INTERNAL_NAME (sh_map_and_validate_gl2he,3 ) - (v, gmfn, entry, size);if ( page->shadow_flags &SHF_L1_64 ) result |=SHADOW_INTERNAL_NAME (sh_map_and_validate_gl1e,4 )
@@ -859,7 +856,6 @@ const u8 sh_type_to_size[] = {1 ,/* SH_type_l1_pae_shadow */ 1 ,/* SH_type_fl1_pae_shadow */ 1 ,/* SH_type_l2_pae_shadow */ -1 ,/* SH_type_l2h_pae_shadow */ 1 ,/* SH_type_l1_64_shadow */ 1 ,/* SH_type_fl1_64_shadow */ 1, /* SH_type_l2_64_shadow */
@@ -900,7 +896,6 @@ void shadow_unhook_mappings(struct domaiSHADOW_INTERNAL_NAME (sh_unhook_32b_mappings,2 )(d, smfn, user_only);break ;case SH_type_l2_pae_shadow: -case SH_type_l2h_pae_shadow:SHADOW_INTERNAL_NAME (sh_unhook_pae_mappings,3 )(d, smfn, user_only);break ;case SH_type_l4_64_shadow:
@@ -1757,7 +1752,6 @@ void sh_destroy_shadow(struct domain *d,SHADOW_INTERNAL_NAME (sh_destroy_l1_shadow,3 )(d, smfn);break ;case SH_type_l2_pae_shadow: -case SH_type_l2h_pae_shadow:SHADOW_INTERNAL_NAME (sh_destroy_l2_shadow,3 )(d, smfn);break ;
@@ -1816,7 +1810,6 @@ int sh_remove_write_access(struct domainSHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,3 ),/* l1_pae */ SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,3 ),/* fl1_pae */ NULL ,/* l2_pae */ -NULL ,/* l2h_pae */ SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,4 ),/* l1_64 */ SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,4 ),/* fl1_64 */ NULL, /* l2_64 */
@@ -2039,7 +2032,6 @@ int sh_remove_all_mappings(struct domainSHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,3 ),/* l1_pae */ SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,3 ),/* fl1_pae */ NULL ,/* l2_pae */ -NULL ,/* l2h_pae */ SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,4 ),/* l1_64 */ SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,4 ),/* fl1_64 */ NULL, /* l2_64 */
@@ -2136,7 +2128,6 @@ static int sh_remove_shadow_via_pointer(break ;case SH_type_l1_pae_shadow:case SH_type_l2_pae_shadow: -case SH_type_l2h_pae_shadow:SHADOW_INTERNAL_NAME (sh_clear_shadow_entry,3 )(d, vaddr, pmfn);break ;case SH_type_l1_64_shadow:
@@ -2181,7 +2172,6 @@ void sh_remove_shadows(struct domain *d,NULL ,/* l1_pae */ NULL ,/* fl1_pae */ SHADOW_INTERNAL_NAME (sh_remove_l1_shadow,3 ),/* l2_pae */ -SHADOW_INTERNAL_NAME (sh_remove_l1_shadow,3 ),/* l2h_pae */ NULL ,/* l1_64 */ NULL ,/* fl1_64 */ SHADOW_INTERNAL_NAME(sh_remove_l1_shadow, 4), /* l2_64 */
@@ -2198,10 +2188,9 @@ void sh_remove_shadows(struct domain *d,SHF_L2_32 ,/* l1_32 */ 0 ,/* fl1_32 */ 0 ,/* l2_32 */ -SHF_L2H_PAE |SHF_L2_PAE ,/* l1_pae */ +SHF_L2_PAE ,/* l1_pae */ 0 ,/* fl1_pae */ 0 ,/* l2_pae */ -0 ,/* l2h_pae */ SHF_L2H_64 |SHF_L2_64 ,/* l1_64 */ 0 ,/* fl1_64 */ SHF_L3_64, /* l2_64 */
@@ -2261,7 +2250,6 @@ void sh_remove_shadows(struct domain *d,DO_UNSHADOW (SH_type_l2_32_shadow);DO_UNSHADOW (SH_type_l1_32_shadow); -DO_UNSHADOW (SH_type_l2h_pae_shadow);DO_UNSHADOW (SH_type_l2_pae_shadow);DO_UNSHADOW (SH_type_l1_pae_shadow);DO_UNSHADOW (SH_type_l4_64_shadow);
@@ -2344,7 +2332,6 @@ void sh_reset_l3_up_pointers(struct vcpuNULL ,/* l1_pae */ NULL ,/* fl1_pae */ NULL ,/* l2_pae */ -NULL ,/* l2h_pae */ NULL ,/* l1_64 */ NULL ,/* fl1_64 */ NULL, /* l2_64 */
@@ -3368,7 +3355,6 @@ void shadow_audit_tables(struct vcpu *v)SHADOW_INTERNAL_NAME (sh_audit_l1_table,3 ),/* l1_pae */ SHADOW_INTERNAL_NAME (sh_audit_fl1_table,3 ),/* fl1_pae */ SHADOW_INTERNAL_NAME (sh_audit_l2_table,3 ),/* l2_pae */ -SHADOW_INTERNAL_NAME (sh_audit_l2_table,3 ),/* l2h_pae */ SHADOW_INTERNAL_NAME (sh_audit_l1_table,4 ),/* l1_64 */ SHADOW_INTERNAL_NAME (sh_audit_fl1_table,4 ),/* fl1_64 */ SHADOW_INTERNAL_NAME(sh_audit_l2_table, 4), /* l2_64 */
xen/arch/x86/mm/shadow/hvm.c +1 -1
@@ -568,7 +568,7 @@ static inline void check_for_early_unsha && sh_mfn_is_a_page_table(gmfn) && (!d->arch.paging.shadow.pagetable_dying_op || !(mfn_to_page(gmfn)->shadow_flags - & (SHF_L2_32|SHF_L2_PAE|SHF_L2H_PAE|SHF_L4_64))) ) + & (SHF_L2_32|SHF_L2_PAE|SHF_L4_64))) ) {perfc_incr (shadow_early_unshadow);sh_remove_shadows (d, gmfn,1 ,0 /* Fast, can fail to unshadow */ );
xen/arch/x86/mm/shadow/multi.c +1 -2
@@ -837,8 +837,7 @@ do {int _i; \shadow_l2e_t *_sp =map_domain_page ((_sl2mfn)); \ASSERT (shadow_mode_external (_dom)); \ -ASSERT (mfn_to_page (_sl2mfn)->u.sh.type == SH_type_l2_pae_shadow \ - ||mfn_to_page (_sl2mfn)->u.sh.type == SH_type_l2h_pae_shadow); \ +ASSERT (mfn_to_page (_sl2mfn)->u.sh.type == SH_type_l2_pae_shadow); \for ( _i =0 ; _i <SHADOW_L2_PAGETABLE_ENTRIES ; _i++ ) \ { \ (_sl2e) = _sp + _i; \
xen/arch/x86/mm/shadow/private.h +12 -15
@@ -177,18 +177,17 @@ extern void shadow_audit_tables(struct v#define SH_type_l1_pae_shadow (4U)/* shadowing a pae L1 page */ #define SH_type_fl1_pae_shadow (5U)/* L1 shadow for pae 2M superpg */ #define SH_type_l2_pae_shadow (6U)/* shadowing a pae L2-low page */ -#define SH_type_l2h_pae_shadow (7U)/* shadowing a pae L2-high page */ -#define SH_type_l1_64_shadow (8U)/* shadowing a 64-bit L1 page */ -#define SH_type_fl1_64_shadow (9U)/* L1 shadow for 64-bit 2M superpg */ -#define SH_type_l2_64_shadow (10U)/* shadowing a 64-bit L2 page */ -#define SH_type_l2h_64_shadow (11U)/* shadowing a compat PAE L2 high page */ -#define SH_type_l3_64_shadow (12U)/* shadowing a 64-bit L3 page */ -#define SH_type_l4_64_shadow (13U)/* shadowing a 64-bit L4 page */ -#define SH_type_max_shadow (13U) -#define SH_type_p2m_table (14U)/* in use as the p2m table */ -#define SH_type_monitor_table (15U)/* in use as a monitor table */ -#define SH_type_oos_snapshot (16U)/* in use as OOS snapshot */ -#define SH_type_unused (17U) +#define SH_type_l1_64_shadow (7U)/* shadowing a 64-bit L1 page */ +#define SH_type_fl1_64_shadow (8U)/* L1 shadow for 64-bit 2M superpg */ +#define SH_type_l2_64_shadow (9U)/* shadowing a 64-bit L2 page */ +#define SH_type_l2h_64_shadow (10U)/* shadowing a compat PAE L2 high page */ +#define SH_type_l3_64_shadow (11U)/* shadowing a 64-bit L3 page */ +#define SH_type_l4_64_shadow (12U)/* shadowing a 64-bit L4 page */ +#define SH_type_max_shadow (12U) +#define SH_type_p2m_table (13U)/* in use as the p2m table */ +#define SH_type_monitor_table (14U)/* in use as a monitor table */ +#define SH_type_oos_snapshot (15U)/* in use as OOS snapshot */ +#define SH_type_unused (16U)/* * What counts as a pinnable shadow?
@@ -200,7 +199,6 @@ static inline int sh_type_is_pinnable(st* persist even when not currently in use in a guest CR3 */ if ( t == SH_type_l2_32_shadow || t == SH_type_l2_pae_shadow - || t == SH_type_l2h_pae_shadow || t == SH_type_l4_64_shadow )return 1 ;
@@ -256,7 +254,6 @@ static inline void sh_terminate_list(str#define SHF_L1_PAE (1u << SH_type_l1_pae_shadow)#define SHF_FL1_PAE (1u << SH_type_fl1_pae_shadow)#define SHF_L2_PAE (1u << SH_type_l2_pae_shadow) -#define SHF_L2H_PAE (1u << SH_type_l2h_pae_shadow)#define SHF_L1_64 (1u << SH_type_l1_64_shadow)#define SHF_FL1_64 (1u << SH_type_fl1_64_shadow)#define SHF_L2_64 (1u << SH_type_l2_64_shadow)
@@ -265,7 +262,7 @@ static inline void sh_terminate_list(str#define SHF_L4_64 (1u << SH_type_l4_64_shadow)#define SHF_32 (SHF_L1_32|SHF_FL1_32|SHF_L2_32) -#define SHF_PAE (SHF_L1_PAE|SHF_FL1_PAE|SHF_L2_PAE|SHF_L2H_PAE) +#define SHF_PAE (SHF_L1_PAE|SHF_FL1_PAE|SHF_L2_PAE)#define SHF_64 (SHF_L1_64|SHF_FL1_64|SHF_L2_64|SHF_L2H_64|SHF_L3_64|SHF_L4_64)#define SHF_L1_ANY (SHF_L1_32|SHF_L1_PAE|SHF_L1_64)
Re: [PATCH 15/17] x86/shadow: drop SH_type_l2h_pae_shadow
Hi,
Quoted 8 lines · Jan Beulich
This is a remnant from 32-bit days, having no place anymore where a shadow of this type would be created.
Signed-off-by Jan Beulich <[email protected]>---
hash_{domain,vcpu}_foreach() have a use each of literal 15. It's not clear to me what the proper replacement constant would be, as it doesn't look as ifSH_type_monitor_tablewas meant.
Good spot. I think the '<= 15' should be replaced with '< SH_type_unused'. It originally matched the callback arrays all being coded as "static hash_callback_t callbacks[16]".
Cheers,
Tim
Re: [PATCH 15/17] x86/shadow: drop SH_type_l2h_pae_shadow
Quoted 10 lines · Tim Deegan
Quoted 8 lines · Jan Beulich
This is a remnant from 32-bit days, having no place anymore where a shadow of this type would be created.
Signed-off-by Jan Beulich <[email protected]>---
hash_{domain,vcpu}_foreach() have a use each of literal 15. It's not clear to me what the proper replacement constant would be, as it doesn't look as ifSH_type_monitor_tablewas meant.
Quoted 3 lines
Good spot. I think the '<= 15' should be replaced with '<
SH_type_unused'. It originally matched the callback arrays all being coded as "statichash_callback_tcallbacks[16]".
So are you saying this was off by one then prior to this patch (when SH_type_unused was still 17), albeit in apparently a benign way? And the comments in sh_remove_write_access(), sh_remove_all_mappings(), sh_remove_shadows(), and sh_reset_l3_up_pointers() are then wrong as well, and would instead better be like in shadow_audit_tables()?
Because of this having been benign (due to none of the callback tables specifying non-NULL entries there), wouldn't it make sense to dimension the tables by SH_type_max_shadow + 1 only? Or would you consider this too risky?
In any event I guess I'll make the patch addressing this (one way or the other) a prereq to everything here.
Jan
Re: [PATCH 15/17] x86/shadow: drop SH_type_l2h_pae_shadow
Hi,
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Tim Deegan
Quoted 3 lines · Jan Beulich
hash_{domain,vcpu}_foreach() have a use each of literal 15. It's not clear to me what the proper replacement constant would be, as it doesn't look as if
SH_type_monitor_tablewas meant.
Quoted 7 lines
Quoted 3 lines
Good spot. I think the '<= 15' should be replaced with '<
SH_type_unused'. It originally matched the callback arrays all being coded as "statichash_callback_tcallbacks[16]".So are you saying this was off by one then prior to this patch (when
SH_type_unusedwas still 17), albeit in apparently a benign way?
Yes - this assertion is just to catch overruns of the callback table, and so it was OK for its limit to be too low. The new types that were added since then are never put in the hash table, so don't trigger this assertion.
Quoted 4 lines
And the comments in
sh_remove_write_access(),sh_remove_all_mappings(),sh_remove_shadows(), andsh_reset_l3_up_pointers()are then wrong as well, and would instead better be like inshadow_audit_tables()?
Yes, it looks like those comments are also out of date where they mention 'unused'.
Quoted 4 lines
Because of this having been benign (due to none of the callback tables specifying non-NULL entries there), wouldn't it make sense to dimension the tables by
SH_type_max_shadow+ 1 only? Or would you consider this too risky?
Yes, I think that would be fine, also changing '<= 15' to '<= SH_type_max_shadow'. Maybe add a matching ASSERT(t <= SH_type_max_shadow) in shadow_hash_insert as well?
Cheers,
Tim.
Re: [PATCH 15/17] x86/shadow: drop SH_type_l2h_pae_shadow
Quoted 20 lines · Tim Deegan
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Tim Deegan
Quoted 3 lines · Jan Beulich
hash_{domain,vcpu}_foreach() have a use each of literal 15. It's not clear to me what the proper replacement constant would be, as it doesn't look as if
SH_type_monitor_tablewas meant.Quoted 7 lines
Quoted 3 lines
Good spot. I think the '<= 15' should be replaced with '<
SH_type_unused'. It originally matched the callback arrays all being coded as "statichash_callback_tcallbacks[16]".So are you saying this was off by one then prior to this patch (when
SH_type_unusedwas still 17), albeit in apparently a benign way?Yes - this assertion is just to catch overruns of the callback table, and so it was OK for its limit to be too low. The new types that were added since then are never put in the hash table, so don't trigger this assertion.
Quoted 7 lines
Quoted 4 lines
And the comments in
sh_remove_write_access(),sh_remove_all_mappings(),sh_remove_shadows(), andsh_reset_l3_up_pointers()are then wrong as well, and would instead better be like inshadow_audit_tables()?Yes, it looks like those comments are also out of date where they mention 'unused'.
For this, which likely will end up being part of ...
Quoted 8 lines
Quoted 4 lines
Because of this having been benign (due to none of the callback tables specifying non-NULL entries there), wouldn't it make sense to dimension the tables by
SH_type_max_shadow+ 1 only? Or would you consider this too risky?Yes, I think that would be fine, also changing '<= 15' to '<=
SH_type_max_shadow'. Maybe add a matchingASSERT(t <= SH_type_max_shadow)inshadow_hash_insertas well?
... this, I'll send a patch for 4.16 going beyond the more immediate one sent, which I'll ask Ian to consider for 4.15 (assuming of course you consider it okay in the first place).
Jan
Re: [PATCH 15/17] x86/shadow: drop SH_type_l2h_pae_shadow
In reply to Tim DeeganQuoted 9 lines · Tim Deegan
Quoted 4 lines · Jan Beulich
Because of this having been benign (due to none of the callback tables specifying non-NULL entries there), wouldn't it make sense to dimension the tables by
SH_type_max_shadow+ 1 only? Or would you consider this too risky?Yes, I think that would be fine, also changing '<= 15' to '<=
SH_type_max_shadow'. Maybe add a matchingASSERT(t <= SH_type_max_shadow)inshadow_hash_insertas well?
The latter (also for shadow_hash_delete()) would seem kind of orthogonal to me, but for now I've put it in there.
Jan
PATCH 16/17 x86/shadow: only 4-level guest code needs building when !HVM
In reply to Jan BeulichIn order to limit #ifdef-ary, provide "stub" #define-s for SH_type_{l1,fl1,l2}_{32,pae}_shadow and SHF_{L1,FL1,L2}_{32,PAE}.
The change in shadow_vcpu_init() is necessary to cover for "x86: correct is_pv_domain() when !CONFIG_PV" (or any other change along those lines)
- we should only rely on is_hvm_*() to become a build time constant.
xen/arch/x86/mm/Makefile +1 -1
@@ -3,7 +3,7 @@ obj-$(CONFIG_HVM) += hap/ obj-$(CONFIG_HVM) += altp2m.o obj-$(CONFIG_HVM) += guest_walk_2.o guest_walk_3.o guest_walk_4.o -obj-$(CONFIG_SHADOW_PAGING) += guest_walk_2.o guest_walk_3.o guest_walk_4.o +obj-$(CONFIG_SHADOW_PAGING) += guest_walk_4.o obj-$(CONFIG_MEM_ACCESS) += mem_access.o obj-y += mem_paging.o obj-$(CONFIG_MEM_SHARING) += mem_sharing.o
xen/arch/x86/mm/shadow/Makefile +3 -3
@@ -1,7 +1,7 @@ ifeq ($(CONFIG_SHADOW_PAGING),y) -obj-y += common.o guest_2.o guest_3.o guest_4.o set.o -obj-$(CONFIG_HVM) += hvm.o -obj-$(CONFIG_PV) += pv.o +obj-y += common.o set.o +obj-$(CONFIG_HVM) += hvm.o guest_2.o guest_3.o guest_4.o +obj-$(CONFIG_PV) += pv.o guest_4.o else obj-y += none.o endif
xen/arch/x86/mm/shadow/common.c +27 -3
@@ -90,9 +90,9 @@ void shadow_vcpu_init(struct vcpu *v) }#endif - v->arch.paging.mode =is_pv_vcpu (v) ? - &SHADOW_INTERNAL_NAME (sh_paging_mode,4 ) : - &SHADOW_INTERNAL_NAME (sh_paging_mode,3 ); + v->arch.paging.mode =is_hvm_vcpu (v) ? + &SHADOW_INTERNAL_NAME (sh_paging_mode,3 ) : + &SHADOW_INTERNAL_NAME (sh_paging_mode,4 ); }#if SHADOW_AUDIT
@@ -257,6 +257,7 @@ static int sh_remove_write_access_from_sswitch (mfn_to_page (smfn)->u.sh.type ) { +#ifdef CONFIG_HVM case SH_type_l1_32_shadow:case SH_type_fl1_32_shadow:return SHADOW_INTERNAL_NAME (sh_rm_write_access_from_sl1p,2 )
@@ -266,6 +267,7 @@ static int sh_remove_write_access_from_scase SH_type_fl1_pae_shadow:return SHADOW_INTERNAL_NAME (sh_rm_write_access_from_sl1p,3 ) (d, gmfn, smfn, off); +#endif case SH_type_l1_64_shadow:case SH_type_fl1_64_shadow:
@@ -848,6 +850,7 @@ sh_validate_guest_entry(struct vcpu *v,* the free pool. */ +#ifdef CONFIG_HVM const u8 sh_type_to_size[] = {1 ,/* SH_type_none */ 2, /* SH_type_l1_32_shadow */
@@ -866,6 +869,7 @@ const u8 sh_type_to_size[] = {1 ,/* SH_type_monitor_table */ 1 /* SH_type_oos_snapshot */ }; +#endif /* * Figure out the least acceptable quantity of shadow memory.
@@ -892,12 +896,14 @@ void shadow_unhook_mappings(struct domaistruct page_info *sp =mfn_to_page (smfn);switch ( sp->u.sh.type ) { +#ifdef CONFIG_HVM case SH_type_l2_32_shadow:SHADOW_INTERNAL_NAME (sh_unhook_32b_mappings,2 )(d, smfn, user_only);break ;case SH_type_l2_pae_shadow:SHADOW_INTERNAL_NAME (sh_unhook_pae_mappings,3 )(d, smfn, user_only);break ; +#endif case SH_type_l4_64_shadow:SHADOW_INTERNAL_NAME (sh_unhook_64b_mappings,4 )(d, smfn, user_only);break ;
@@ -1104,8 +1110,10 @@ mfn_t shadow_alloc(struct domain *d,/* Backpointers that are MFNs need to be packed into PDXs (PFNs don't) */ switch (shadow_type) { +#ifdef CONFIG_HVM case SH_type_fl1_32_shadow:case SH_type_fl1_pae_shadow: +#endif case SH_type_fl1_64_shadow:break ;default :
@@ -1739,6 +1747,7 @@ void sh_destroy_shadow(struct domain *d,* small numbers that the compiler will enjoy */ switch ( t ) { +#ifdef CONFIG_HVM case SH_type_l1_32_shadow:case SH_type_fl1_32_shadow:SHADOW_INTERNAL_NAME (sh_destroy_l1_shadow,2 )(d, smfn);
@@ -1754,6 +1763,7 @@ void sh_destroy_shadow(struct domain *d,case SH_type_l2_pae_shadow:SHADOW_INTERNAL_NAME (sh_destroy_l2_shadow,3 )(d, smfn);break ; +#endif case SH_type_l1_64_shadow:case SH_type_fl1_64_shadow:
@@ -1804,12 +1814,14 @@ int sh_remove_write_access(struct domain/* Dispatch table for getting per-type functions */ static const hash_domain_callback_t callbacks[SH_type_unused] = {NULL ,/* none */ +#ifdef CONFIG_HVMSHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,2 ),/* l1_32 */ SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,2 ),/* fl1_32 */ NULL ,/* l2_32 */ SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,3 ),/* l1_pae */ SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,3 ),/* fl1_pae */ NULL ,/* l2_pae */ +#endifSHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,4 ),/* l1_64 */ SHADOW_INTERNAL_NAME (sh_rm_write_access_from_l1,4 ),/* fl1_64 */ NULL ,/* l2_64 */
@@ -2026,12 +2038,14 @@ int sh_remove_all_mappings(struct domain/* Dispatch table for getting per-type functions */ static const hash_domain_callback_t callbacks[SH_type_unused] = {NULL ,/* none */ +#ifdef CONFIG_HVMSHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,2 ),/* l1_32 */ SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,2 ),/* fl1_32 */ NULL ,/* l2_32 */ SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,3 ),/* l1_pae */ SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,3 ),/* fl1_pae */ NULL ,/* l2_pae */ +#endifSHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,4 ),/* l1_64 */ SHADOW_INTERNAL_NAME (sh_rm_mappings_from_l1,4 ),/* fl1_64 */ NULL ,/* l2_64 */
@@ -2122,6 +2136,7 @@ static int sh_remove_shadow_via_pointer(/* Blank the offending entry */ switch (sp->u.sh.type) { +#ifdef CONFIG_HVM case SH_type_l1_32_shadow:case SH_type_l2_32_shadow:SHADOW_INTERNAL_NAME (sh_clear_shadow_entry,2 )(d, vaddr, pmfn);
@@ -2130,6 +2145,7 @@ static int sh_remove_shadow_via_pointer(case SH_type_l2_pae_shadow:SHADOW_INTERNAL_NAME (sh_clear_shadow_entry,3 )(d, vaddr, pmfn);break ; +#endif case SH_type_l1_64_shadow:case SH_type_l2_64_shadow:case SH_type_l2h_64_shadow:
@@ -2166,12 +2182,14 @@ void sh_remove_shadows(struct domain *d,* be called with the function to remove a lower-level shadow. */ static const hash_domain_callback_t callbacks[SH_type_unused] = {NULL ,/* none */ +#ifdef CONFIG_HVMNULL ,/* l1_32 */ NULL ,/* fl1_32 */ SHADOW_INTERNAL_NAME (sh_remove_l1_shadow,2 ),/* l2_32 */ NULL ,/* l1_pae */ NULL ,/* fl1_pae */ SHADOW_INTERNAL_NAME (sh_remove_l1_shadow,3 ),/* l2_pae */ +#endifNULL ,/* l1_64 */ NULL ,/* fl1_64 */ SHADOW_INTERNAL_NAME (sh_remove_l1_shadow,4 ),/* l2_64 */
@@ -2185,12 +2203,14 @@ void sh_remove_shadows(struct domain *d,/* Another lookup table, for choosing which mask to use */ static const unsigned int masks[SH_type_unused] = {0 ,/* none */ +#ifdef CONFIG_HVMSHF_L2_32 ,/* l1_32 */ 0 ,/* fl1_32 */ 0 ,/* l2_32 */ SHF_L2_PAE ,/* l1_pae */ 0 ,/* fl1_pae */ 0 ,/* l2_pae */ +#endifSHF_L2H_64 |SHF_L2_64 ,/* l1_64 */ 0 ,/* fl1_64 */ SHF_L3_64 ,/* l2_64 */
@@ -2326,12 +2346,14 @@ void sh_reset_l3_up_pointers(struct vcpu {static const hash_vcpu_callback_t callbacks[SH_type_unused] = {NULL ,/* none */ +#ifdef CONFIG_HVMNULL ,/* l1_32 */ NULL ,/* fl1_32 */ NULL ,/* l2_32 */ NULL ,/* l1_pae */ NULL ,/* fl1_pae */ NULL ,/* l2_pae */ +#endifNULL ,/* l1_64 */ NULL ,/* fl1_64 */ NULL ,/* l2_64 */
@@ -3349,12 +3371,14 @@ void shadow_audit_tables(struct vcpu *v)static const hash_vcpu_callback_t callbacks[SH_type_unused] = {NULL ,/* none */ #if SHADOW_AUDIT & (SHADOW_AUDIT_ENTRIES | SHADOW_AUDIT_ENTRIES_FULL) +# ifdef CONFIG_HVMSHADOW_INTERNAL_NAME (sh_audit_l1_table,2 ),/* l1_32 */ SHADOW_INTERNAL_NAME (sh_audit_fl1_table,2 ),/* fl1_32 */ SHADOW_INTERNAL_NAME (sh_audit_l2_table,2 ),/* l2_32 */ SHADOW_INTERNAL_NAME (sh_audit_l1_table,3 ),/* l1_pae */ SHADOW_INTERNAL_NAME (sh_audit_fl1_table,3 ),/* fl1_pae */ SHADOW_INTERNAL_NAME (sh_audit_l2_table,3 ),/* l2_pae */ +# endifSHADOW_INTERNAL_NAME (sh_audit_l1_table,4 ),/* l1_64 */ SHADOW_INTERNAL_NAME (sh_audit_fl1_table,4 ),/* fl1_64 */ SHADOW_INTERNAL_NAME(sh_audit_l2_table, 4), /* l2_64 */
xen/arch/x86/mm/shadow/private.h +36 -0
@@ -171,6 +171,7 @@ extern void shadow_audit_tables(struct v/* Shadow type codes */ #define SH_type_none (0U)/* on the shadow free list */ #define SH_type_min_shadow (1U) +#ifdef CONFIG_HVM #define SH_type_l1_32_shadow (1U)/* shadowing a 32-bit L1 guest page */ #define SH_type_fl1_32_shadow (2U)/* L1 shadow for a 32b 4M superpage */ #define SH_type_l2_32_shadow (3U)/* shadowing a 32-bit L2 guest page */
@@ -188,6 +189,25 @@ extern void shadow_audit_tables(struct v#define SH_type_monitor_table (14U)/* in use as a monitor table */ #define SH_type_oos_snapshot (15U)/* in use as OOS snapshot */ #define SH_type_unused (16U) +#else +#define SH_type_l1_32_shadow SH_type_unused +#define SH_type_fl1_32_shadow SH_type_unused +#define SH_type_l2_32_shadow SH_type_unused +#define SH_type_l1_pae_shadow SH_type_unused +#define SH_type_fl1_pae_shadow SH_type_unused +#define SH_type_l2_pae_shadow SH_type_unused +#define SH_type_l1_64_shadow 1U/* shadowing a 64-bit L1 page */ +#define SH_type_fl1_64_shadow 2U/* L1 shadow for 64-bit 2M superpg */ +#define SH_type_l2_64_shadow 3U/* shadowing a 64-bit L2 page */ +#define SH_type_l2h_64_shadow 4U/* shadowing a compat PAE L2 high page */ +#define SH_type_l3_64_shadow 5U/* shadowing a 64-bit L3 page */ +#define SH_type_l4_64_shadow 6U/* shadowing a 64-bit L4 page */ +#define SH_type_max_shadow 6U +#define SH_type_p2m_table 7U/* in use as the p2m table */ +#define SH_type_monitor_table 8U/* in use as a monitor table */ +#define SH_type_oos_snapshot 9U/* in use as OOS snapshot */ +#define SH_type_unused 10U +#endif /* * What counts as a pinnable shadow?
@@ -248,12 +268,21 @@ static inline void sh_terminate_list(str (((1u << (SH_type_max_shadow +1u )) -1u ) - \ ((1u << SH_type_min_shadow) - 1u)) +#ifdef CONFIG_HVM #define SHF_L1_32 (1u << SH_type_l1_32_shadow)#define SHF_FL1_32 (1u << SH_type_fl1_32_shadow)#define SHF_L2_32 (1u << SH_type_l2_32_shadow)#define SHF_L1_PAE (1u << SH_type_l1_pae_shadow)#define SHF_FL1_PAE (1u << SH_type_fl1_pae_shadow)#define SHF_L2_PAE (1u << SH_type_l2_pae_shadow) +#else +#define SHF_L1_32 0 +#define SHF_FL1_32 0 +#define SHF_L2_32 0 +#define SHF_L1_PAE 0 +#define SHF_FL1_PAE 0 +#define SHF_L2_PAE 0 +#endif #define SHF_L1_64 (1u << SH_type_l1_64_shadow)#define SHF_FL1_64 (1u << SH_type_fl1_64_shadow)#define SHF_L2_64 (1u << SH_type_l2_64_shadow)
@@ -329,8 +358,13 @@ extern const u8 sh_type_to_size[SH_type_static inline unsigned int shadow_size (unsigned int shadow_type) { +#ifdef CONFIG_HVM ASSERT (shadow_type <ARRAY_SIZE (sh_type_to_size));return sh_type_to_size[shadow_type]; +#else +ASSERT (shadow_type < SH_type_unused); +return 1 ; +#endif }/******************************************************************************
@@ -488,8 +522,10 @@ static inline unsigned long __backpointe {switch (sp->u.sh.type) { +#ifdef CONFIG_HVM case SH_type_fl1_32_shadow:case SH_type_fl1_pae_shadow: +#endif case SH_type_fl1_64_shadow:return sp->v.sh.back; }
PATCH 17/17 x86/shadow: adjust is_pv_*() checks
In reply to Jan BeulichTo cover for "x86: correct is_pv_domain() when !CONFIG_PV" (or any other change along those lines) we should prefer is_hvm_*(), as it may become a build time constant while is_pv_*() generally won't.
Also when a domain pointer is in scope, prefer is_*_domain() over is_*_vcpu().
xen/arch/x86/mm/shadow/common.c +2 -2
@@ -674,7 +674,7 @@ int sh_unsync(struct vcpu *v, mfn_t gmfnif ( pg->shadow_flags & ((SHF_page_type_mask & ~SHF_L1_ANY ) | SHF_out_of_sync) ||sh_page_has_multiple_shadows (pg) - ||is_pv_vcpu (v) + || !is_hvm_vcpu (v) || !v->domain->arch.paging.shadow.oos_active )return 0 ;
@@ -2419,7 +2419,7 @@ static void sh_update_paging_modes(struc v->arch.paging.mode->shadow.detach_old_tables (v);#ifdef CONFIG_HVM -if ( !is_pv_domain (d) ) +if (is_hvm_domain (d) ) {const struct paging_mode *old_mode = v->arch.paging.mode;
xen/arch/x86/mm/shadow/multi.c +5 -5
@@ -666,8 +666,8 @@ _sh_propagate(struct vcpu *v,// PV guests in 64-bit mode use two different page tables for user vs // supervisor permissions, making the guest's _PAGE_USER bit irrelevant. // It is always shadowed as present... -if ( (GUEST_PAGING_LEVELS ==4 ) && !is_pv_32bit_domain (d) - &&is_pv_domain (d) ) +if ( (GUEST_PAGING_LEVELS ==4 ) && !is_hvm_domain (d) && + !is_pv_32bit_domain (d) ) { sflags |= _PAGE_USER; }
@@ -1119,7 +1119,7 @@ static shadow_l2e_t * shadow_get_and_creunsigned int t = SH_type_l2_shadow;/* Tag compat L2 containing hypervisor (m2p) mappings */ -if (is_pv_32bit_vcpu (v) && +if (is_pv_32bit_domain (d) &&guest_l4_table_offset (gw->va) ==0 &&guest_l3_table_offset (gw->va) ==3 ) t = SH_type_l2h_shadow;
@@ -2313,7 +2313,7 @@ static int sh_page_fault(struct vcpu *v,return 0 ; } - cpl =is_pv_vcpu (v) ? (regs->ss &3 ) :hvm_get_cpl (v); + cpl =is_hvm_domain (d) ?hvm_get_cpl (v) : (regs->ss &3 ); rewalk:
@@ -3236,7 +3236,7 @@ sh_update_cr3(struct vcpu *v, int do_loc#endif /* Don't do anything on an uninitialised vcpu */ -if (is_pv_domain (d) && !v->is_initialised ) +if ( !is_hvm_domain (d) && !v->is_initialised ) {ASSERT (v->arch.cr3 ==0 );return ;
xen/arch/x86/mm/shadow/none.c +1 -1
@@ -27,7 +27,7 @@ int shadow_domain_init(struct domain *d) };paging_log_dirty_init (d, &sh_none_ops); -return is_pv_domain (d) ?0 : -EOPNOTSUPP ; +return is_hvm_domain (d) ? -EOPNOTSUPP :0 ; } static int _page_fault(struct vcpu *v, unsigned long va,
Quoted 3 lines · Jan Beulich
... shadow adjustments towards not building 2- and 3-level code when !HVM. While the latter isn't functionally related to the former, it depends on some of the rearrangements done there.
The shadow changes look good, thank you!
I have read the uaccess stuff in passing and it looks OK to me too, but I didn't review it in detail.
Cheers,
Tim.