x86: Drop the vm86 segments selectors from struct cpu_user_regs
v2 cover x86: Drop the vm86 segments selectors from struct cpu_user_regs
This is the next chunk of work in order to support FRED. Half of v1 is already committed.
https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/1972002392
Andrew Cooper (4):
x86/domctl: Stop using XLAT_cpu_user_regs()
x86/pv: Store the data segment selectors outside of cpu_user_regs x86/public: Split the struct cpu_user_regs type
x86: Drop the vm86 segments selectors from struct cpu_user_regs
xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/domain.c | 92 ++++++++++++++++-------- xen/arch/x86/domctl.c | 38 +++++++++- xen/arch/x86/include/asm/cpu-user-regs.h | 58 +++++++++++++++ xen/arch/x86/include/asm/current.h | 11 +-- xen/arch/x86/include/asm/domain.h | 2 + xen/arch/x86/include/asm/hvm/hvm.h | 4 -- xen/arch/x86/include/asm/regs.h | 11 +-- xen/arch/x86/pv/dom0_build.c | 6 +- xen/arch/x86/traps.c | 2 +- xen/arch/x86/x86_64/asm-offsets.c | 2 +- xen/arch/x86/x86_64/traps.c | 16 ++--- xen/arch/x86/x86_emulate/private.h | 1 + xen/include/public/arch-x86/xen-x86_32.h | 9 +++ xen/include/public/arch-x86/xen-x86_64.h | 9 +++ xen/include/public/arch-x86/xen.h | 11 +++ xen/include/xlat.lst | 2 - 17 files changed, 208 insertions(+), 68 deletions(-) create mode 100644 xen/arch/x86/include/asm/cpu-user-regs.h
Signature · 1 line
2.39.5
PATCH v2 1/4 x86/domctl: Stop using XLAT_cpu_user_regs()
In order to support FRED, we're going to have to remove the {ds..gs} fields from struct cpu_user_regs, meaning that it is going to have to become a different type to the structure embedded in vcpu_guest_context_u.
In both arch_{get,set}_info_guest(), expand the memcpy()/XLAT_cpu_user_regs() to copy the fields individually. This will allow us to eventually make them different types.
This does cause some minor changes in behaviour for the hypercalls.
It is specifically not the case that a toolstack could set_info(); get_info(); and get an identical bit pattern back. Amongst other things, the architectural sticky bits in registers are applied during setting.
Previously, XLAT_cpu_user_regs() omitted the _pad fields in the compat case whereas the non-compat case included them owing to the single memcpy().
Omit the _pad fields in the non-compat case too; for all but the oldest of CPUs, the segment selectors are zero-extended by hardware when pushed onto the stack, so non-zero values here get lost naturally. Furthermore, FRED reuses the space above cs and ss for extra state, and a PV guest for now at least must not be able to write the control state.
Omit the error_code and entry_vector fields too. They're already identified as private fields in the public API, and are stale outside of Xen's interrupt/exception/syscall handler. They're also a very minor information leak of which event caused the last deschedule of a vCPU.
Finally, omit saved_upcall_mask. Xen doesn't consume this, and only produces it in {compat_,}create_bounce_frame(), based on the vcpu_info page and settings about the event being injected. Similar to error_code/entry_vector, it is stale outside of the guest's event handler.
No change that toolstacks or guests are expected to notice or care about.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
v2:
* Zero memory before a partial copy, or note that it already is.
* Omit error_code/entry_vector/saved_upcall_mask too.
I can't see why saved_upcall_mask exists in the first place, given that it is also reflected in (v)rflags.IF. None of MiniOS, Linux or NetBSD use it at all. I suspect the reflecting in IF was a slightly later addition dicovered when running non-toy PV guests, and saved_upcall_mask got left behind as a wart in the ABI.
--- xen/arch/x86/domain.c | 38 ++++++++++++++++++++++++++++++++++++-- xen/arch/x86/domctl.c | 38 ++++++++++++++++++++++++++++++++++++-- xen/include/xlat.lst | 2 -- 3 files changed, 72 insertions(+), 6 deletions(-)
xen/arch/x86/domain.c +36 -2
@@ -1233,7 +1233,24 @@ int arch_set_info_guest(if ( !compat ) { -memcpy (&v->arch.user_regs, &c.nat->user_regs,sizeof (c.nat->user_regs)); +memset (&v->arch.user_regs,0 ,sizeof (v->arch.user_regs)); + v->arch.user_regs.rbx = c.nat->user_regs.rbx; + v->arch.user_regs.rcx = c.nat->user_regs.rcx; + v->arch.user_regs.rdx = c.nat->user_regs.rdx; + v->arch.user_regs.rsi = c.nat->user_regs.rsi; + v->arch.user_regs.rdi = c.nat->user_regs.rdi; + v->arch.user_regs.rbp = c.nat->user_regs.rbp; + v->arch.user_regs.rax = c.nat->user_regs.rax; + v->arch.user_regs.rip = c.nat->user_regs.rip; + v->arch.user_regs.cs = c.nat->user_regs.cs; + v->arch.user_regs.rflags = c.nat->user_regs.rflags; + v->arch.user_regs.rsp = c.nat->user_regs.rsp; + v->arch.user_regs.ss = c.nat->user_regs.ss; + v->arch.user_regs.es = c.nat->user_regs.es; + v->arch.user_regs.ds = c.nat->user_regs.ds; + v->arch.user_regs.fs = c.nat->user_regs.fs; + v->arch.user_regs.gs = c.nat->user_regs.gs; +if (is_pv_domain (d) )memcpy (v->arch.pv.trap_ctxt, c.nat->trap_ctxt,sizeof (c.nat->trap_ctxt));
@@ -1241,7 +1258,24 @@ int arch_set_info_guest(#ifdef CONFIG_COMPAT else { -XLAT_cpu_user_regs (&v->arch.user_regs, &c.cmp->user_regs); +memset (&v->arch.user_regs,0 ,sizeof (v->arch.user_regs)); + v->arch.user_regs.ebx = c.cmp->user_regs.ebx; + v->arch.user_regs.ecx = c.cmp->user_regs.ecx; + v->arch.user_regs.edx = c.cmp->user_regs.edx; + v->arch.user_regs.esi = c.cmp->user_regs.esi; + v->arch.user_regs.edi = c.cmp->user_regs.edi; + v->arch.user_regs.ebp = c.cmp->user_regs.ebp; + v->arch.user_regs.eax = c.cmp->user_regs.eax; + v->arch.user_regs.eip = c.cmp->user_regs.eip; + v->arch.user_regs.cs = c.cmp->user_regs.cs; + v->arch.user_regs.eflags = c.cmp->user_regs.eflags; + v->arch.user_regs.esp = c.cmp->user_regs.esp; + v->arch.user_regs.ss = c.cmp->user_regs.ss; + v->arch.user_regs.es = c.cmp->user_regs.es; + v->arch.user_regs.ds = c.cmp->user_regs.ds; + v->arch.user_regs.fs = c.cmp->user_regs.fs; + v->arch.user_regs.gs = c.cmp->user_regs.gs; +if (is_pv_domain (d) ) {for ( i =0 ; i <ARRAY_SIZE (c.cmp->trap_ctxt); ++i )
xen/arch/x86/domctl.c +36 -2
@@ -1399,7 +1399,24 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)c (flags |= VGCF_online);if ( !compat ) { -memcpy (&c.nat->user_regs, &v->arch.user_regs,sizeof (c.nat->user_regs)); +/* Backing memory is pre-zeroed. */ + c.nat->user_regs.rbx = v->arch.user_regs.rbx; + c.nat->user_regs.rcx = v->arch.user_regs.rcx; + c.nat->user_regs.rdx = v->arch.user_regs.rdx; + c.nat->user_regs.rsi = v->arch.user_regs.rsi; + c.nat->user_regs.rdi = v->arch.user_regs.rdi; + c.nat->user_regs.rbp = v->arch.user_regs.rbp; + c.nat->user_regs.rax = v->arch.user_regs.rax; + c.nat->user_regs.rip = v->arch.user_regs.rip; + c.nat->user_regs.cs = v->arch.user_regs.cs; + c.nat->user_regs.rflags = v->arch.user_regs.rflags; + c.nat->user_regs.rsp = v->arch.user_regs.rsp; + c.nat->user_regs.ss = v->arch.user_regs.ss; + c.nat->user_regs.es = v->arch.user_regs.es; + c.nat->user_regs.ds = v->arch.user_regs.ds; + c.nat->user_regs.fs = v->arch.user_regs.fs; + c.nat->user_regs.gs = v->arch.user_regs.gs; +if (is_pv_domain (d) )memcpy (c.nat->trap_ctxt, v->arch.pv.trap_ctxt,sizeof (c.nat->trap_ctxt));
@@ -1407,7 +1424,24 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)#ifdef CONFIG_COMPAT else { -XLAT_cpu_user_regs (&c.cmp->user_regs, &v->arch.user_regs); +/* Backing memory is pre-zeroed. */ + c.cmp->user_regs.ebx = v->arch.user_regs.ebx; + c.cmp->user_regs.ecx = v->arch.user_regs.ecx; + c.cmp->user_regs.edx = v->arch.user_regs.edx; + c.cmp->user_regs.esi = v->arch.user_regs.esi; + c.cmp->user_regs.edi = v->arch.user_regs.edi; + c.cmp->user_regs.ebp = v->arch.user_regs.ebp; + c.cmp->user_regs.eax = v->arch.user_regs.eax; + c.cmp->user_regs.eip = v->arch.user_regs.eip; + c.cmp->user_regs.cs = v->arch.user_regs.cs; + c.cmp->user_regs.eflags = v->arch.user_regs.eflags; + c.cmp->user_regs.esp = v->arch.user_regs.esp; + c.cmp->user_regs.ss = v->arch.user_regs.ss; + c.cmp->user_regs.es = v->arch.user_regs.es; + c.cmp->user_regs.ds = v->arch.user_regs.ds; + c.cmp->user_regs.fs = v->arch.user_regs.fs; + c.cmp->user_regs.gs = v->arch.user_regs.gs; +if (is_pv_domain (d) ) {for ( i =0 ; i <ARRAY_SIZE (c.cmp->trap_ctxt); ++i )
xen/include/xlat.lst +0 -2
@@ -34,8 +34,6 @@ ? pmu_intel_ctxt arch-x86/pmu.h ? pmu_regs arch-x86/pmu.h -! cpu_user_regs arch-x86/xen-@[email protected] - ? cpu_offline_action arch-x86/xen-mca.h ? mc arch-x86/xen-mca.h ! mc_fetch arch-x86/xen-mca.h
Signature · 1 line
2.39.5
Re: [PATCH v2 1/4] x86/domctl: Stop using XLAT_cpu_user_regs()
Quoted 23 lines · Andrew Cooper
In order to support FRED, we're going to have to remove the {ds..gs} fields from struct
cpu_user_regs, meaning that it is going to have to become a different type to the structure embedded invcpu_guest_context_u.In both arch_{get,set}_info_guest(), expand the memcpy()/XLAT_cpu_user_regs() to copy the fields individually. This will allow us to eventually make them different types.
This does cause some minor changes in behaviour for the hypercalls.
It is specifically not the case that a toolstack could
set_info();get_info(); and get an identical bit pattern back. Amongst other things, the architectural sticky bits in registers are applied during setting.Previously,
XLAT_cpu_user_regs()omitted the_padfields in the compat case whereas the non-compat case included them owing to the singlememcpy().Omit the
_padfields in the non-compat case too; for all but the oldest of CPUs, the segment selectors are zero-extended by hardware when pushed onto the stack, so non-zero values here get lost naturally. Furthermore, FRED reuses the space above cs and ss for extra state, and a PV guest for now at least must not be able to write the control state.
Quoted 4 lines
Omit the
error_codeandentry_vectorfields too. They're already identified as private fields in the public API, and are stale outside of Xen's interrupt/exception/syscall handler. They're also a very minor information leak of which event caused the last deschedule of a vCPU.
I think my prior remark towards tools like xenctx wasn't really addressed. Then again that particular tool doesn't use the fields now, so apparently no-one ever saw a need.
xen/arch/x86/domain.c int arch_set_info_guest( · line 1236
Quoted 8 lines
xen/arch/x86/domain.c +1
-1@@ -1233,7 +1233,24 @@ int arch_set_info_guest(if ( !compat ) { -memcpy (&v->arch.user_regs, &c.nat->user_regs,sizeof (c.nat->user_regs)); +memset (&v->arch.user_regs,0 ,sizeof (v->arch.user_regs));
Any reason to have this and ...
Quoted 21 lines
xen/arch/x86/domain.c +17
-0+ v->arch.user_regs.rbx = c.nat->user_regs.rbx; + v->arch.user_regs.rcx = c.nat->user_regs.rcx; + v->arch.user_regs.rdx = c.nat->user_regs.rdx; + v->arch.user_regs.rsi = c.nat->user_regs.rsi; + v->arch.user_regs.rdi = c.nat->user_regs.rdi; + v->arch.user_regs.rbp = c.nat->user_regs.rbp; + v->arch.user_regs.rax = c.nat->user_regs.rax; + v->arch.user_regs.rip = c.nat->user_regs.rip; + v->arch.user_regs.cs = c.nat->user_regs.cs; + v->arch.user_regs.rflags = c.nat->user_regs.rflags; + v->arch.user_regs.rsp = c.nat->user_regs.rsp; + v->arch.user_regs.ss = c.nat->user_regs.ss; + v->arch.user_regs.es = c.nat->user_regs.es; + v->arch.user_regs.ds = c.nat->user_regs.ds; + v->arch.user_regs.fs = c.nat->user_regs.fs; + v->arch.user_regs.gs = c.nat->user_regs.gs; +if (is_pv_domain (d) )memcpy (v->arch.pv.trap_ctxt, c.nat->trap_ctxt,sizeof (c.nat->trap_ctxt));
xen/arch/x86/domain.c int arch_set_info_guest( · line 1261
Quoted 5 lines
xen/arch/x86/domain.c +1
-1@@ -1241,7 +1258,24 @@ int arch_set_info_guest(#ifdef CONFIG_COMPAT else { -XLAT_cpu_user_regs (&v->arch.user_regs, &c.cmp->user_regs); +memset (&v->arch.user_regs,0 ,sizeof (v->arch.user_regs));
... this separate, rather than putting just one ahead of the if()?
Preferably with that adjustment:
Jan
Re: [PATCH v2 1/4] x86/domctl: Stop using XLAT_cpu_user_regs()
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper
In order to support FRED, we're going to have to remove the {ds..gs} fields from struct
cpu_user_regs, meaning that it is going to have to become a different type to the structure embedded invcpu_guest_context_u.In both arch_{get,set}_info_guest(), expand the memcpy()/XLAT_cpu_user_regs() to copy the fields individually. This will allow us to eventually make them different types.
This does cause some minor changes in behaviour for the hypercalls.
It is specifically not the case that a toolstack could
set_info();get_info(); and get an identical bit pattern back. Amongst other things, the architectural sticky bits in registers are applied during setting.Previously,
XLAT_cpu_user_regs()omitted the_padfields in the compat case whereas the non-compat case included them owing to the singlememcpy().Omit the
_padfields in the non-compat case too; for all but the oldest of CPUs, the segment selectors are zero-extended by hardware when pushed onto the stack, so non-zero values here get lost naturally. Furthermore, FRED reuses the space above cs and ss for extra state, and a PV guest for now at least must not be able to write the control state.
Quoted 7 lines
Quoted 4 lines
Omit the
error_codeandentry_vectorfields too. They're already identified as private fields in the public API, and are stale outside of Xen's interrupt/exception/syscall handler. They're also a very minor information leak of which event caused the last deschedule of a vCPU.I think my prior remark towards tools like xenctx wasn't really addressed. Then again that particular tool doesn't use the fields now, so apparently no-one ever saw a need.
Oh, sorry. I did specifically look (everywhere in tools, not just xenctx), and they're not used at all.
Xenalyze uses an error_code, but that's a field name from the EPT/NPT fault trace record, not from cpu_user_regs.
Finally, the observation about the information leak. The information present is often the timer interrupt (end of time-slice), or the event check IPI (from vcpu_pause()).
gdbsx is the only utility that stands a chance of reliably using ->entry_vector, and even it doesn't because that's not how GDB works.
Overall, I'd say people have been pretty good at following the /* Private */ note in the public ABI.
Quoted 30 lines
xen/arch/x86/domain.c int arch_set_info_guest( · line 1236
Quoted 9 lines
xen/arch/x86/domain.c +1
-1@@ -1233,7 +1233,24 @@ int arch_set_info_guest(if ( !compat ) { -memcpy (&v->arch.user_regs, &c.nat->user_regs,sizeof (c.nat->user_regs)); +memset (&v->arch.user_regs,0 ,sizeof (v->arch.user_regs));Any reason to have this and ...
Quoted 19 lines
xen/arch/x86/domain.c +17
-0+ v->arch.user_regs.rbx = c.nat->user_regs.rbx; + v->arch.user_regs.rcx = c.nat->user_regs.rcx; + v->arch.user_regs.rdx = c.nat->user_regs.rdx; + v->arch.user_regs.rsi = c.nat->user_regs.rsi; + v->arch.user_regs.rdi = c.nat->user_regs.rdi; + v->arch.user_regs.rbp = c.nat->user_regs.rbp; + v->arch.user_regs.rax = c.nat->user_regs.rax; + v->arch.user_regs.rip = c.nat->user_regs.rip; + v->arch.user_regs.cs = c.nat->user_regs.cs; + v->arch.user_regs.rflags = c.nat->user_regs.rflags; + v->arch.user_regs.rsp = c.nat->user_regs.rsp; + v->arch.user_regs.ss = c.nat->user_regs.ss; + v->arch.user_regs.es = c.nat->user_regs.es; + v->arch.user_regs.ds = c.nat->user_regs.ds; + v->arch.user_regs.fs = c.nat->user_regs.fs; + v->arch.user_regs.gs = c.nat->user_regs.gs; +if (is_pv_domain (d) ) memcpy(v->arch.pv.trap_ctxt, c.nat->trap_ctxt,
Quoted 8 lines
xen/arch/x86/domain.c int arch_set_info_guest( · line 1261
Quoted 7 lines
xen/arch/x86/domain.c +1
-1sizeof(c.nat->trap_ctxt));@@ -1241,7 +1258,24 @@ int arch_set_info_guest(#ifdef CONFIG_COMPAT else { -XLAT_cpu_user_regs (&v->arch.user_regs, &c.cmp->user_regs); +memset (&v->arch.user_regs,0 ,sizeof (v->arch.user_regs));... this separate, rather than putting just one ahead of the
if()?
Code generation. If you hoist the memset(), it can't be merged with the assignments.
Although I see now it's not even attempting the mere (it was in the past), and I don't care enough to argue, so I'll change it.
Quoted 2 lines
Preferably with that adjustment:
Reviewed-by Jan Beulich <[email protected]>
Thanks.
~Andrew
PATCH v2 2/4 x86/pv: Store the data segment selectors outside of cpu_user_regs
In reply to Andrew CooperIn order to support FRED, we're going to have to remove the {ds..gs} fields from struct cpu_user_regs. This will impact v->arch.user_regs.
These fields are unused for HVM guests, but for PV hold the selector values when the vCPU is scheduled out.
Introduce new fields for the selectors in struct pv_vcpu, and update:
* {save,load}_segments(), context switching
* arch_{set,set}_info_guest(), hypercalls
* vcpu_show_registers(), diagnostics
* dom0_construct(), PV dom0
to use the new storage. This removes the final user of read_sregs() so drop it too.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/domain.c | 70 +++++++++++++++---------------- xen/arch/x86/domctl.c | 16 +++---- xen/arch/x86/include/asm/domain.h | 2 + xen/arch/x86/include/asm/regs.h | 8 ---- xen/arch/x86/pv/dom0_build.c | 6 ++- xen/arch/x86/x86_64/traps.c | 8 ++-- 6 files changed, 53 insertions(+), 57 deletions(-)
xen/arch/x86/domain.c +35 -35
@@ -1246,10 +1246,10 @@ int arch_set_info_guest( v->arch.user_regs.rflags = c.nat->user_regs.rflags; v->arch.user_regs.rsp = c.nat->user_regs.rsp; v->arch.user_regs.ss = c.nat->user_regs.ss; - v->arch.user_regs.es = c.nat->user_regs.es; - v->arch.user_regs.ds = c.nat->user_regs.ds; - v->arch.user_regs.fs = c.nat->user_regs.fs; - v->arch.user_regs.gs = c.nat->user_regs.gs; + v->arch.pv.es = c.nat->user_regs.es; + v->arch.pv.ds = c.nat->user_regs.ds; + v->arch.pv.fs = c.nat->user_regs.fs; + v->arch.pv.gs = c.nat->user_regs.gs;if (is_pv_domain (d) ) memcpy(v->arch.pv.trap_ctxt, c.nat->trap_ctxt,
@@ -1271,10 +1271,10 @@ int arch_set_info_guest( v->arch.user_regs.eflags = c.cmp->user_regs.eflags; v->arch.user_regs.esp = c.cmp->user_regs.esp; v->arch.user_regs.ss = c.cmp->user_regs.ss; - v->arch.user_regs.es = c.cmp->user_regs.es; - v->arch.user_regs.ds = c.cmp->user_regs.ds; - v->arch.user_regs.fs = c.cmp->user_regs.fs; - v->arch.user_regs.gs = c.cmp->user_regs.gs; + v->arch.pv.es = c.cmp->user_regs.es; + v->arch.pv.ds = c.cmp->user_regs.ds; + v->arch.pv.fs = c.cmp->user_regs.fs; + v->arch.pv.gs = c.cmp->user_regs.gs;if (is_pv_domain (d) ) {
@@ -1762,7 +1762,6 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)*/ static void load_segments (struct vcpu *n) { -struct cpu_user_regs *uregs = &n->arch.user_regs;unsigned long gsb =0 , gss =0 ;bool compat =is_pv_32bit_vcpu (n);bool all_segs_okay = true, fs_gs_done = false;
@@ -1796,7 +1795,7 @@ static void load_segments(struct vcpu *n)if ( !(n->arch.flags & TF_kernel_mode) )SWAP (gsb, gss); -if (using_svm () && (uregs->fs | uregs->gs) <=3 ) +if (using_svm () && (n->arch.pv.fs | n->arch.pv.gs) <=3 ) fs_gs_done =svm_load_segs (n->arch.pv.ldt_ents,LDT_VIRT_START (n), n->arch.pv.fs_base, gsb, gss); }
@@ -1805,12 +1804,12 @@ static void load_segments(struct vcpu *n) {load_LDT (n); -TRY_LOAD_SEG (fs, uregs->fs); -TRY_LOAD_SEG (gs, uregs->gs); +TRY_LOAD_SEG (fs, n->arch.pv.fs); +TRY_LOAD_SEG (gs, n->arch.pv.gs); } -TRY_LOAD_SEG (ds, uregs->ds); -TRY_LOAD_SEG (es, uregs->es); +TRY_LOAD_SEG (ds, n->arch.pv.ds); +TRY_LOAD_SEG (es, n->arch.pv.es);if ( !fs_gs_done && !compat ) {
@@ -1863,13 +1862,13 @@ static void load_segments(struct vcpu *n) }if ( ret | -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 ) ) +put_guest (rflags, esp -1 ) | +put_guest (cs_and_mask, esp -2 ) | +put_guest (regs->eip, esp -3 ) | +put_guest (n->arch.pv.gs, esp -4 ) | +put_guest (n->arch.pv.fs, esp -5 ) | +put_guest (n->arch.pv.es, esp -6 ) | +put_guest (n->arch.pv.ds, esp -7 ) )domain_crash (n->domain,"Error creating compat failsafe callback frame\n" );
@@ -1895,17 +1894,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_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 ) ) +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 (n->arch.pv.gs, rsp -6 ) | +put_guest (n->arch.pv.fs, rsp -7 ) | +put_guest (n->arch.pv.es, rsp -8 ) | +put_guest (n->arch.pv.ds, rsp -9 ) | +put_guest (regs->r11, rsp -10 ) | +put_guest (regs->rcx, rsp -11 ) )domain_crash (n->domain,"Error creating failsafe callback frame\n" );
@@ -1934,9 +1933,10 @@ static void load_segments(struct vcpu *n)*/ static void save_segments (struct vcpu *v) { -struct cpu_user_regs *regs = &v->arch.user_regs; - -read_sregs (regs); + asm ("mov %%ds, %0" :"=m" (v->arch.pv.ds) ); + asm ("mov %%es, %0" :"=m" (v->arch.pv.es) ); + asm ("mov %%fs, %0" :"=m" (v->arch.pv.fs) ); + asm ("mov %%gs, %0" :"=m" (v->arch.pv.gs) );if ( !is_pv_32bit_vcpu (v) ) {
xen/arch/x86/domctl.c +8 -8
@@ -1412,10 +1412,10 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c) c.nat->user_regs.rflags = v->arch.user_regs.rflags; c.nat->user_regs.rsp = v->arch.user_regs.rsp; c.nat->user_regs.ss = v->arch.user_regs.ss; - c.nat->user_regs.es = v->arch.user_regs.es; - c.nat->user_regs.ds = v->arch.user_regs.ds; - c.nat->user_regs.fs = v->arch.user_regs.fs; - c.nat->user_regs.gs = v->arch.user_regs.gs; + c.nat->user_regs.es = v->arch.pv.es; + c.nat->user_regs.ds = v->arch.pv.ds; + c.nat->user_regs.fs = v->arch.pv.fs; + c.nat->user_regs.gs = v->arch.pv.gs;if (is_pv_domain (d) ) memcpy(c.nat->trap_ctxt, v->arch.pv.trap_ctxt,
@@ -1437,10 +1437,10 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c) c.cmp->user_regs.eflags = v->arch.user_regs.eflags; c.cmp->user_regs.esp = v->arch.user_regs.esp; c.cmp->user_regs.ss = v->arch.user_regs.ss; - c.cmp->user_regs.es = v->arch.user_regs.es; - c.cmp->user_regs.ds = v->arch.user_regs.ds; - c.cmp->user_regs.fs = v->arch.user_regs.fs; - c.cmp->user_regs.gs = v->arch.user_regs.gs; + c.cmp->user_regs.es = v->arch.pv.es; + c.cmp->user_regs.ds = v->arch.pv.ds; + c.cmp->user_regs.fs = v->arch.pv.fs; + c.cmp->user_regs.gs = v->arch.pv.gs;if (is_pv_domain (d) ) {
xen/arch/x86/include/asm/domain.h +2 -0
@@ -546,6 +546,8 @@ struct pv_vcpubool syscall32_disables_events;bool sysenter_disables_events; +uint16_t ds, es, fs, gs; +/* * 64bit segment bases. *
xen/arch/x86/include/asm/regs.h +0 -8
@@ -44,12 +44,4 @@ __sel; \ }) -static inline void read_sregs (struct cpu_user_regs *regs) -{ - asm ("mov %%ds, %0" :"=m" (regs->ds) ); - asm ("mov %%es, %0" :"=m" (regs->es) ); - asm ("mov %%fs, %0" :"=m" (regs->fs) ); - asm ("mov %%gs, %0" :"=m" (regs->gs) ); -} -#endif /* __X86_REGS_H__ */
xen/arch/x86/pv/dom0_build.c +4 -2
@@ -1012,8 +1012,10 @@ static int __init dom0_construct(const struct boot_domain *bd)* [rAX,rBX,rCX,rDX,rDI,rBP,R8-R15 are zero] */ regs = &v->arch.user_regs; - regs->ds = regs->es = regs->fs = regs->gs = - (compat ?FLAT_COMPAT_KERNEL_DS :FLAT_KERNEL_DS ); + v->arch.pv.ds = (compat ?FLAT_COMPAT_KERNEL_DS :FLAT_KERNEL_DS ); + v->arch.pv.es = (compat ?FLAT_COMPAT_KERNEL_DS :FLAT_KERNEL_DS ); + v->arch.pv.fs = (compat ?FLAT_COMPAT_KERNEL_DS :FLAT_KERNEL_DS ); + v->arch.pv.gs = (compat ?FLAT_COMPAT_KERNEL_DS :FLAT_KERNEL_DS ); regs->ss = (compat ?FLAT_COMPAT_KERNEL_SS :FLAT_KERNEL_SS ); regs->cs = (compat ?FLAT_COMPAT_KERNEL_CS :FLAT_KERNEL_CS ); regs->rip = parms.virt_entry;
xen/arch/x86/x86_64/traps.c +4 -4
@@ -220,10 +220,10 @@ void vcpu_show_registers(struct vcpu *v) state.gsb = gsb; state.gss = gss; - state.ds = v->arch.user_regs.ds; - state.es = v->arch.user_regs.es; - state.fs = v->arch.user_regs.fs; - state.gs = v->arch.user_regs.gs; + state.ds = v->arch.pv.ds; + state.es = v->arch.pv.es; + state.fs = v->arch.pv.fs; + state.gs = v->arch.pv.gs; context = CTXT_pv_guest; }
Signature · 1 line
2.39.5
PATCH v2 3/4 x86/public: Split the struct cpu_user_regs type
In reply to Andrew CooperIn order to support FRED, we're going to have to remove the {ds..gs} fields from struct cpu_user_regs, meaning that it is going to have to become a different type to the structure embedded in vcpu_guest_context_u.
struct cpu_user_regs is a name used in common Xen code (i.e. needs to stay using this name), so renaming the public struct to be guest_user_regs in Xen's view only.
Introduce a brand hew cpu-user-regs.h, currently containing a duplicate structure.
Notably, this removes the need to include pubic/xen.h in ~every translation unit in Xen (via current.h), and highlights one case where the emulator was picking up cpu_user_regs transitively.
Include comments describing how hardware interacts with this structure under IDT delivery, as it's quite magic to start with. FRED is going make things more complicated.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
v2:
* Remove the macros from cpu-user-regs.h and write the struct longhand
* Exclude the guest handles in Xen context
--- xen/arch/x86/include/asm/cpu-user-regs.h | 63 ++++++++++++++++++++++++ xen/arch/x86/include/asm/current.h | 3 +- xen/arch/x86/x86_emulate/private.h | 1 + xen/include/public/arch-x86/xen-x86_32.h | 9 ++++ xen/include/public/arch-x86/xen-x86_64.h | 9 ++++ xen/include/public/arch-x86/xen.h | 11 +++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 xen/arch/x86/include/asm/cpu-user-regs.h
xen/arch/x86/include/asm/cpu-user-regs.h new file +63 -0
@@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef X86_CPU_USER_REGS_H +#define X86_CPU_USER_REGS_H + +#include <xen/stdint.h> + +/* +* cpu_user_regs represents the interrupted GPR state at the point of an +* interrupt, exception or syscall. The layout is dictated by the hardware +* format for the event frame, with software filling in the rest. +*/ +struct cpu_user_regs +{ +union {uint64_t r15;uint32_t r15d;uint16_t r15w;uint8_t r15b; }; +union {uint64_t r14;uint32_t r14d;uint16_t r14w;uint8_t r14b; }; +union {uint64_t r13;uint32_t r13d;uint16_t r13w;uint8_t r13b; }; +union {uint64_t r12;uint32_t r12d;uint16_t r12w;uint8_t r12b; }; +union {uint64_t rbp;uint32_t ebp;uint16_t bp;uint8_t bpl; }; +union {uint64_t rbx;uint32_t ebx;uint16_t bx;struct {uint8_t bl, bh; }; }; +union {uint64_t r11;uint32_t r11d;uint16_t r11w;uint8_t r11b; }; +union {uint64_t r10;uint32_t r10d;uint16_t r10w;uint8_t r10b; }; +union {uint64_t r9;uint32_t r9d;uint16_t r9w;uint8_t r9b; }; +union {uint64_t r8;uint32_t r8d;uint16_t r8w;uint8_t r8b; }; +union {uint64_t rax;uint32_t eax;uint16_t ax;struct {uint8_t al, ah; }; }; +union {uint64_t rcx;uint32_t ecx;uint16_t cx;struct {uint8_t cl, ch; }; }; +union {uint64_t rdx;uint32_t edx;uint16_t dx;struct {uint8_t dl, dh; }; }; +union {uint64_t rsi;uint32_t esi;uint16_t si;uint8_t sil; }; +union {uint64_t rdi;uint32_t edi;uint16_t di;uint8_t dil; }; + +/* +* During IDT delivery for exceptions with an error code, hardware pushes +* to this point. Entry_vector is filled in by software. +*/ + +uint32_t error_code; +uint32_t entry_vector; + +/* +* During IDT delivery for interrupts or exceptions without an error code, +* hardware pushes to this point. Both error_code and entry_vector are +* filled in by software. +*/ + +union {uint64_t rip;uint32_t eip;uint16_t ip; }; +uint16_t cs, _pad0[1 ]; +uint8_t saved_upcall_mask;/* PV (v)rflags.IF == !saved_upcall_mask */ +uint8_t _pad1[3 ]; +union {uint64_t rflags;uint32_t eflags;uint16_t flags; }; +union {uint64_t rsp;uint32_t esp;uint16_t sp;uint8_t spl; }; +uint16_t ss, _pad2[3 ]; + +/* +* For IDT delivery, tss->rsp0 points to this boundary as embedded within +* struct cpu_info. It must be 16-byte aligned. +*/ + +uint16_t es, _pad3[3 ]; +uint16_t ds, _pad4[3 ]; +uint16_t fs, _pad5[3 ]; +uint16_t gs, _pad6[3 ]; +}; + +#endif /* X86_CPU_USER_REGS_H */
xen/arch/x86/include/asm/current.h +2 -1
@@ -9,7 +9,8 @@#include <xen/percpu.h> #include <xen/page-size.h> -#include <public/xen.h> + +#include <asm/cpu-user-regs.h> /* * Xen's cpu stacks are 8 pages (8-page aligned), arranged as:
xen/arch/x86/x86_emulate/private.h +1 -0
@@ -14,6 +14,7 @@# include <xen/bug.h> # include <xen/kernel.h> +# include <asm/cpu-user-regs.h> # include <asm/endbr.h> # include <asm/msr-index.h> # include <asm/stubs.h>
xen/include/public/arch-x86/xen-x86_32.h +9 -0
@@ -114,6 +114,10 @@#define __DECL_REG_LO16 (name) uint32_t e ## name#endif +#ifdef __XEN__ +#define cpu_user_regs guest_user_regs +#endif +struct cpu_user_regs {__DECL_REG_LO8 (b);__DECL_REG_LO8 (c);
@@ -136,8 +140,13 @@ struct cpu_user_regs {uint16_t fs, _pad4;uint16_t gs, _pad5; }; + +#ifdef __XEN__ +#undef cpu_user_regs +#else typedef struct cpu_user_regs cpu_user_regs_t ;DEFINE_XEN_GUEST_HANDLE (cpu_user_regs_t); +#endif #undef __DECL_REG_LO8#undef __DECL_REG_LO16
xen/include/public/arch-x86/xen-x86_64.h +9 -0
@@ -159,6 +159,10 @@ struct iret_context {#define __DECL_REG_HI (num) uint64_t r ## num#endif +#ifdef __XEN__ +#define cpu_user_regs guest_user_regs +#endif +struct cpu_user_regs { __DECL_REG_HI(15); __DECL_REG_HI(14);
@@ -189,8 +193,13 @@ struct cpu_user_regs {uint16_t fs, _pad5[3 ];uint16_t gs, _pad6[3 ]; }; + +#ifdef __XEN__ +#undef cpu_user_regs +#else typedef struct cpu_user_regs cpu_user_regs_t ;DEFINE_XEN_GUEST_HANDLE (cpu_user_regs_t); +#endif #undef __DECL_REG#undef __DECL_REG_LOHI
xen/include/public/arch-x86/xen.h +11 -0
@@ -173,7 +173,18 @@ struct vcpu_guest_context {#define _VGCF_online 5#define VGCF_online (1<<_VGCF_online)unsigned long flags;/* VGCF_* flags */ + +/* +* Outside of Xen, regs type stays named cpu_user_regs for backwards +* compatibility. Inside Xen, the type called cpu_user_regs is different, +* and the public API type is renamed to guest_user_regs. +*/ +#ifdef __XEN__ +struct guest_user_regs user_regs;/* User-level CPU registers */ +#else struct cpu_user_regs user_regs;/* User-level CPU registers */ +#endif +struct trap_info trap_ctxt[256 ];/* Virtual IDT */ unsigned long ldt_base, ldt_ents;/* LDT (linear address, # ents) */ unsigned long gdt_frames[16 ], gdt_ents;/* GDT (machine frames, # ents) */
Signature · 1 line
2.39.5
PATCH v2 4/4 x86: Drop the vm86 segments selectors from struct cpu_user_regs
In reply to Andrew CooperThe data segment registers are part of the on-stack IRET frame when interrupting Virtual 8086 mode, but this ceased being relevant for Xen in commit 5d1181a5ea5e ("xen: Remove x86_32 build target.") in 2012.
With all other cleanup in place, delete the fields so we can introduce FRED support which uses this space for different data.
Everywhere which used the es field as an offset in cpu_user_regs needs adjusting. However, they'll change again for FRED, so no cleanup is performed at this juncture.
This also undoes the OoB Read workaround in show_registers(), which can now switch back to being simple structure copy.
No functional change, but a lot of rearranging of stack and struct layout under the hood.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/include/asm/cpu-user-regs.h | 5 ----- xen/arch/x86/include/asm/current.h | 8 ++++---- xen/arch/x86/include/asm/hvm/hvm.h | 4 ---- xen/arch/x86/include/asm/regs.h | 3 +-- xen/arch/x86/traps.c | 2 +- xen/arch/x86/x86_64/asm-offsets.c | 2 +- xen/arch/x86/x86_64/traps.c | 8 +------- 8 files changed, 9 insertions(+), 25 deletions(-)
xen/arch/x86/cpu/common.c +1 -1
@@ -959,7 +959,7 @@ void load_system_tables(void)* Defer checks until exception support is sufficiently set up. */ BUILD_BUG_ON ((sizeof (struct cpu_info ) - - offsetof(struct cpu_info, guest_cpu_user_regs.es)) & 0xf); +sizeof (struct cpu_user_regs )) &0xf );BUG_ON (system_state != SYS_STATE_early_boot && (stack_bottom &0xf )); }
xen/arch/x86/include/asm/cpu-user-regs.h +0 -5
@@ -53,11 +53,6 @@ struct cpu_user_regs* For IDT delivery, tss->rsp0 points to this boundary as embedded within * struct cpu_info. It must be 16-byte aligned. */ - -uint16_t es, _pad3[3 ]; -uint16_t ds, _pad4[3 ]; -uint16_t fs, _pad5[3 ]; - uint16_t gs, _pad6[3]; };#endif /* X86_CPU_USER_REGS_H */
xen/arch/x86/include/asm/current.h +4 -4
@@ -106,12 +106,12 @@ static inline struct cpu_info *get_cpu_info(void)#define get_per_cpu_offset () (get_cpu_info()->per_cpu_offset)/* -* Get the bottom-of-stack, as stored in the per-CPU TSS. This actually points -* into the middle of cpu_info.guest_cpu_user_regs, at the section that -* precisely corresponds to a CPU trap frame. +* Get the bottom-of-stack, as stored in the per-CPU TSS. This points at the +* end of cpu_info.guest_cpu_user_regs, at the section that precisely +* corresponds to a CPU trap frame. */ #define get_stack_bottom () \ - ((unsigned long)&get_cpu_info()->guest_cpu_user_regs.es) + ((unsigned long)(&get_cpu_info()->guest_cpu_user_regs + 1))/* * Get the reasonable stack bounds for stack traces and stack dumps. Stack
xen/arch/x86/include/asm/hvm/hvm.h +0 -4
@@ -624,10 +624,6 @@ static inline void hvm_sanitize_regs_fields(struct cpu_user_regs *regs, regs->saved_upcall_mask =0xbf ; regs->cs =0xbeef ; regs->ss =0xbeef ; - regs->ds =0xbeef ; - regs->es =0xbeef ; - regs->fs =0xbeef ; - regs->gs =0xbeef ;#endif }
xen/arch/x86/include/asm/regs.h +1 -2
@@ -23,8 +23,7 @@ (!is_pv_32bit_vcpu (v) ? ((tb)->eip ==0 ) : (((tb)->cs & ~3 ) ==0 )) /* Number of bytes of on-stack execution state to be context-switched. */ -/* NB. Segment registers and bases are not saved/restored on x86/64 stack. */ -#define CTXT_SWITCH_STACK_BYTES (offsetof(struct cpu_user_regs, es)) +#define CTXT_SWITCH_STACK_BYTES sizeof(struct cpu_user_regs)#define guest_mode (r) \ ({ \
xen/arch/x86/traps.c +1 -1
@@ -387,7 +387,7 @@ unsigned long get_stack_trace_bottom(unsigned long sp) { case 1 ... 4:return ROUNDUP (sp,PAGE_SIZE ) - - offsetof(struct cpu_user_regs , es) -sizeof (unsigned long ); +sizeof (struct cpu_user_regs ) -sizeof (unsigned long ); case 6 ... 7: return ROUNDUP(sp, STACK_SIZE) -
xen/arch/x86/x86_64/asm-offsets.c +1 -1
@@ -52,7 +52,7 @@ void __dummy__(void) OFFSET(UREGS_eflags, struct cpu_user_regs, rflags); OFFSET(UREGS_rsp, struct cpu_user_regs, rsp); OFFSET(UREGS_ss, struct cpu_user_regs, ss); - OFFSET(UREGS_kernel_sizeof, struct cpu_user_regs, es); +DEFINE (UREGS_kernel_sizeof,sizeof (struct cpu_user_regs ));BLANK ();/*
xen/arch/x86/x86_64/traps.c +1 -7
@@ -135,17 +135,11 @@ static void _show_registers(void show_registers (const struct cpu_user_regs *regs) { -struct cpu_user_regs fault_regs; +struct cpu_user_regs fault_regs = *regs;struct extra_state fault_state;enum context context;struct vcpu *v = system_state >= SYS_STATE_smp_boot ? current :NULL ; -/* -* Don't read beyond the end of the hardware frame. It is out of bounds -* for WARN()/etc. -*/ -memcpy (&fault_regs, regs, offsetof(struct cpu_user_regs , es)); -if (guest_mode (regs) &&is_hvm_vcpu (v) ) {get_hvm_registers (v, &fault_regs, &fault_state);
Signature · 1 line
2.39.5