RE: [PATCH 10/37] xen/x86: use helpers to access/update mem_hotplug

2022-01-25 Thread Wei Chen
Hi Jan,

> -Original Message-
> From: Jan Beulich 
> Sent: 2022年1月25日 0:30
> To: Wei Chen 
> Cc: Bertrand Marquis ; xen-
> de...@lists.xenproject.org; sstabell...@kernel.org; jul...@xen.org
> Subject: Re: [PATCH 10/37] xen/x86: use helpers to access/update
> mem_hotplug
> 
> On 23.09.2021 14:02, Wei Chen wrote:
> > --- a/xen/arch/x86/srat.c
> > +++ b/xen/arch/x86/srat.c
> > @@ -391,8 +391,8 @@ acpi_numa_memory_affinity_init(const struct
> acpi_srat_mem_affinity *ma)
> > memblk_nodeid[num_node_memblks] = node;
> > if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) {
> > __set_bit(num_node_memblks, memblk_hotplug);
> > -   if (end > mem_hotplug)
> > -   mem_hotplug = end;
> > +   if (end > mem_hotplug_boundary())
> > +   mem_hotplug_update_boundary(end);
> 
> Can the if() please be folded into mem_hotplug_update_boundary(),
> eliminating (at least for the purpose here) the need for the
> separate mem_hotplug_boundary()? As said on the previous patch,
> I think the two want folding.
> 

Yes, these changes would be more make sense. I will do it in next
version.

> Jan



RE: [PATCH 09/37] xen/x86: introduce two helpers to access memory hotplug end

2022-01-25 Thread Wei Chen
Hi Jan,

> -Original Message-
> From: Jan Beulich 
> Sent: 2022年1月25日 0:25
> To: Wei Chen 
> Cc: Bertrand Marquis ; xen-
> de...@lists.xenproject.org; sstabell...@kernel.org; jul...@xen.org
> Subject: Re: [PATCH 09/37] xen/x86: introduce two helpers to access memory
> hotplug end
> 
> On 23.09.2021 14:02, Wei Chen wrote:
> > x86 provides a mem_hotplug to maintain the end of memory hotplug
> > end address. This variable can be accessed out of mm.c. We want
> > some code out of mm.c can be reused by other architectures without
> > memory hotplug ability. So in this patch, we introduce these two
> > helpers to replace mem_hotplug direct access. This will give the
> > ability to stub these two API.
> >
> > Signed-off-by: Wei Chen 
> > ---
> >  xen/include/asm-x86/mm.h | 10 ++
> >  1 file changed, 10 insertions(+)
> >
> > diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h
> > index cb90527499..af2fc4b0cd 100644
> > --- a/xen/include/asm-x86/mm.h
> > +++ b/xen/include/asm-x86/mm.h
> > @@ -475,6 +475,16 @@ static inline int get_page_and_type(struct
> page_info *page,
> >
> >  extern paddr_t mem_hotplug;
> >
> > +static inline void mem_hotplug_update_boundary(paddr_t end)
> > +{
> > +mem_hotplug = end;
> > +}
> > +
> > +static inline paddr_t mem_hotplug_boundary(void)
> > +{
> > +return mem_hotplug;
> > +}
> > +
> >
> /*
> *
> >   * With shadow pagetables, the different kinds of address start
> >   * to get get confusing.
> 
> Imo for this to make sense you want to also use the new functions
> right away in the place(s) where the direct access(es) get(s) in
> your way.
> 

OK, I will merge this patch with the patch that will use these
functions firstly. 

> Jan



Re: [XEN PATCH v2 2/5] xen: export get_free_port

2022-01-25 Thread Jan Beulich
On 26.01.2022 02:03, Stefano Stabellini wrote:
> Are you guys OK with something like this?

With proper proof that this isn't going to regress anything else, maybe.
But ...

> --- a/xen/include/xsm/dummy.h
> +++ b/xen/include/xsm/dummy.h
> @@ -92,7 +92,9 @@ static always_inline int xsm_default_action(
>  return 0;
>  /* fall through */
>  case XSM_PRIV:
> -if ( is_control_domain(src) )
> +if ( is_control_domain(src) ||
> + src->domain_id == DOMID_IDLE ||
> + src->domain_id == DOMID_XEN )
>  return 0;

... my first question would be under what circumstances you might observe
DOMID_XEN here and hence why this check is there.

Jan




Re: [XEN PATCH v2 2/5] xen: export get_free_port

2022-01-25 Thread Jan Beulich
On 25.01.2022 23:49, Stefano Stabellini wrote:
> On Tue, 25 Jan 2022, Jan Beulich wrote:
>> On 25.01.2022 02:10, Stefano Stabellini wrote:
>>> @@ -284,7 +285,27 @@ void evtchn_free(struct domain *d, struct evtchn *chn)
>>>  xsm_evtchn_close_post(chn);
>>>  }
>>>  
>>> -static int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
>>> +struct evtchn *evtchn_alloc_unbound(struct domain *d, domid_t remote_dom)
>>> +{
>>> +struct evtchn *chn;
>>> +int port;
>>> +
>>> +if ( (port = get_free_port(d)) < 0 )
>>> +return ERR_PTR(port);
>>> +chn = evtchn_from_port(d, port);
>>> +
>>> +evtchn_write_lock(chn);
>>> +
>>> +chn->state = ECS_UNBOUND;
>>> +chn->u.unbound.remote_domid = remote_dom;
>>> +evtchn_port_init(d, chn);
>>> +
>>> +evtchn_write_unlock(chn);
>>> +
>>> +return chn;
>>> +}
>>> +
>>> +static int _evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
>>>  {
>>>  struct evtchn *chn;
>>>  struct domain *d;
>>
>> Instead of introducing a clone of this function (with, btw, still
>> insufficient locking), did you consider simply using the existing
>> evtchn_alloc_unbound() as-is, i.e. with the caller passing
>> evtchn_alloc_unbound_t *?
> 
> Yes, we tried that first. Unfortunately the (dummy) XSM check cannot
> work. This is how we would want to call the function:
> 
> 
> alloc.dom = d->domain_id;
> alloc.remote_dom = hardware_domain->domain_id;
> rc = evtchn_alloc_unbound(&alloc);
> 
> 
> This is the implementation of the XSM check:
> 
> static XSM_INLINE int xsm_evtchn_unbound(
> XSM_DEFAULT_ARG struct domain *d, struct evtchn *chn, domid_t id2)
> {
> XSM_ASSERT_ACTION(XSM_TARGET);
> return xsm_default_action(action, current->domain, d);
> }
> 
> 
> Note the usage of current->domain. If you have any suggestions on how to
> fix it please let me know.

As an alternative to Julien's suggestion the function could also simply
be given a new boolean parameter indicating whether to bypass the XSM
check. That would be more explicit than deriving from system state.

Jan




[xen-4.13-testing test] 167817: tolerable FAIL - PUSHED

2022-01-25 Thread osstest service owner
flight 167817 xen-4.13-testing real [real]
http://logs.test-lab.xenproject.org/osstest/logs/167817/

Failures :-/ but no regressions.

Tests which did not succeed, but are not blocking:
 test-amd64-amd64-xl-qemuu-win7-amd64 19 guest-stopfail like 167215
 test-amd64-i386-xl-qemuu-win7-amd64 19 guest-stop fail like 167215
 test-armhf-armhf-libvirt-qcow2 15 saverestore-support-check   fail like 167215
 test-amd64-i386-xl-qemut-win7-amd64 19 guest-stop fail like 167215
 test-armhf-armhf-libvirt-raw 15 saverestore-support-checkfail  like 167215
 test-armhf-armhf-libvirt 16 saverestore-support-checkfail  like 167215
 test-amd64-amd64-qemuu-nested-amd 20 debian-hvm-install/l1/l2 fail like 167215
 test-amd64-i386-xl-qemut-ws16-amd64 19 guest-stop fail like 167215
 test-amd64-amd64-xl-qemut-win7-amd64 19 guest-stopfail like 167215
 test-amd64-i386-xl-qemuu-ws16-amd64 19 guest-stop fail like 167215
 test-amd64-amd64-xl-qemuu-ws16-amd64 19 guest-stopfail like 167215
 test-amd64-amd64-xl-qemut-ws16-amd64 19 guest-stopfail like 167215
 test-amd64-i386-xl-pvshim14 guest-start  fail   never pass
 test-arm64-arm64-xl  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl  16 saverestore-support-checkfail   never pass
 test-amd64-i386-libvirt-xsm  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-multivcpu 15 migrate-support-checkfail  never pass
 test-armhf-armhf-xl-multivcpu 16 saverestore-support-checkfail  never pass
 test-amd64-amd64-libvirt 15 migrate-support-checkfail   never pass
 test-amd64-amd64-libvirt-xsm 15 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-xsm 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit2  15 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-xsm 16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-credit1  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit2  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit1  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-seattle  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-seattle  16 saverestore-support-checkfail   never pass
 test-amd64-i386-libvirt  15 migrate-support-checkfail   never pass
 test-amd64-i386-libvirt-raw  14 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-arndale  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-arndale  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-credit2  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-credit2  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-rtds 15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-rtds 16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-thunderx 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-thunderx 16 saverestore-support-checkfail   never pass
 test-arm64-arm64-libvirt-raw 14 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-raw 15 saverestore-support-checkfail   never pass
 test-amd64-amd64-libvirt-vhd 14 migrate-support-checkfail   never pass
 test-amd64-amd64-libvirt-qemuu-debianhvm-amd64-xsm 13 migrate-support-check 
fail never pass
 test-amd64-i386-libvirt-qemuu-debianhvm-amd64-xsm 13 migrate-support-check 
fail never pass
 test-armhf-armhf-libvirt-qcow2 14 migrate-support-checkfail never pass
 test-armhf-armhf-xl  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-cubietruck 15 migrate-support-checkfail never pass
 test-armhf-armhf-xl-cubietruck 16 saverestore-support-checkfail never pass
 test-arm64-arm64-xl-vhd  14 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-vhd  15 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-credit1  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-credit1  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-libvirt-raw 14 migrate-support-checkfail   never pass
 test-armhf-armhf-libvirt 15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-vhd  14 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-vhd  15 saverestore-support-checkfail   never pass

version targeted for testing:
 xen  ce49a1d6d819f4587436b4ff73334d3676c1aab6
baseline version:
 xen  d0e2c2762b98

Re: [XEN v4] xen/arm64: io: Decode ldr/str post-indexing instructions

2022-01-25 Thread Stefano Stabellini
On Tue, 25 Jan 2022, Julien Grall wrote:
> > +
> >   /* TODO: Handle ARM instruction */
> >   gprintk(XENLOG_ERR, "unhandled ARM instruction\n");
> > return 1;
> >   }
> >   +#if CONFIG_ARM_64
> > +void post_increment_register(union ldr_str_instr_class *instr)
> 
> instr should not be modified, so please use const. Also, it would be
> preferrable to pass the regs in parameter. So the none of the decoding code
> relies on the current regs.
> 
> Furthermore, decode.c should only contain code to update the syndrome and in
> theory Arm could decide to provide an valid syndrome in future revision. So I
> would move this code in io.c (or maybe traps.c).

I was the one to suggest moving it to decode.c to keep it closer to the
decoding function it is related to, and also because it felt a bit out
of place in io.c.

I don't feel strongly about this at all; I am fine either way.


> > +{
> > +struct cpu_user_regs *regs = guest_cpu_user_regs();
> > +register_t val;
> > +
> > +/* handle when rn = SP */
> > +if ( instr->code.rn == 31 )
> > +val = regs->sp_el1;
> > +else
> > +val = get_user_reg(regs, instr->code.rn);
> > +
> > +val += instr->code.imm9;
> > +
> > +if ( instr->code.rn == 31 )
> > +regs->sp_el1 = val;
> > +else
> > +set_user_reg(regs, instr->code.rn, val);
> > +}
> > +#endif
> > +
> >   /*
> >* Local variables:
> >* mode: C
> > diff --git a/xen/arch/arm/decode.h b/xen/arch/arm/decode.h
> > index 4613763bdb..511cd4a05f 100644
> > --- a/xen/arch/arm/decode.h
> > +++ b/xen/arch/arm/decode.h
> > @@ -23,6 +23,35 @@
> >   #include 
> >   #include 
> >   +/*
> > + * Refer to the ARMv8 ARM (DDI 0487G.b), Section C4.1.4 Loads and Stores
> > + * Page 318 specifies the following bit pattern for
> > + * "load/store register (immediate post-indexed)".
> > + *
> > + * 31 30 29  27 26 25  23   21 20  11   9 4   0
> > + * ___
> > + * |size|1 1 1 |V |0 0 |opc |0 |  imm9 |0 1 |  Rn |  Rt   |
> > + * ||__|__|||__|___||_|___|
> > + */
> > +union ldr_str_instr_class {
> > +uint32_t value;
> > +struct ldr_str {
> > +unsigned int rt:5; /* Rt register */
> > +unsigned int rn:5; /* Rn register */
> > +unsigned int fixed1:2; /* value == 01b */
> > +signed int imm9:9;/* imm9 */
> > +unsigned int fixed2:1; /* value == 0b */
> > +unsigned int opc:2;/* opc */
> > +unsigned int fixed3:2; /* value == 00b */
> > +unsigned int v:1;  /* vector */
> > +unsigned int fixed4:3; /* value == 111b */
> > +unsigned int size:2;   /* size */
> > +} code;
> > +};
> 
> Looking at the code, post_increment_register() only care about 'rn' and
> 'imm9'. So rather than exposing the full instruction, could we instead provide
> the strict minimum? I.e something like:
> 
> struct
> {
>  enum instr_type; /* Unknown, ldr/str post increment */
>  union
>  {
>  struct
>  {
>register; /* Register to increment */
>imm;  /* Immediate to add */
>  } ldr_str;
>  }
>  uint64_t register;
> }
 
The full description helped a lot during review. I would prefer to keep
it if you don't feel strongly about it.



[PATCH v2] automation: remove python-dev from debian unstable build containers

2022-01-25 Thread Stefano Stabellini
From: Stefano Stabellini 

Debian unstable doesn't have the legacy python-dev package anymore.
Note: only the arm64v8 container has been rebuilt.

Signed-off-by: Stefano Stabellini 
Acked-by: Andrew Cooper 
---
Changes in v2:
- remove python-dev from all debian unstable containers
---
 automation/build/debian/unstable-arm64v8.dockerfile | 1 -
 automation/build/debian/unstable-i386.dockerfile| 1 -
 automation/build/debian/unstable.dockerfile | 1 -
 3 files changed, 3 deletions(-)

diff --git a/automation/build/debian/unstable-arm64v8.dockerfile 
b/automation/build/debian/unstable-arm64v8.dockerfile
index 4409f596e1..5c73458d9d 100644
--- a/automation/build/debian/unstable-arm64v8.dockerfile
+++ b/automation/build/debian/unstable-arm64v8.dockerfile
@@ -15,7 +15,6 @@ RUN apt-get update && \
 zlib1g-dev \
 libncurses5-dev \
 libssl-dev \
-python-dev \
 python3-dev \
 xorg-dev \
 uuid-dev \
diff --git a/automation/build/debian/unstable-i386.dockerfile 
b/automation/build/debian/unstable-i386.dockerfile
index 06ffb97384..6ce54a481b 100644
--- a/automation/build/debian/unstable-i386.dockerfile
+++ b/automation/build/debian/unstable-i386.dockerfile
@@ -17,7 +17,6 @@ RUN apt-get update && \
 zlib1g-dev \
 libncurses5-dev \
 libssl-dev \
-python-dev \
 python3-dev \
 xorg-dev \
 uuid-dev \
diff --git a/automation/build/debian/unstable.dockerfile 
b/automation/build/debian/unstable.dockerfile
index bd61cd12c2..9de766d596 100644
--- a/automation/build/debian/unstable.dockerfile
+++ b/automation/build/debian/unstable.dockerfile
@@ -15,7 +15,6 @@ RUN apt-get update && \
 zlib1g-dev \
 libncurses5-dev \
 libssl-dev \
-python-dev \
 python3-dev \
 xorg-dev \
 uuid-dev \
-- 
2.25.1




[xen-4.12-testing test] 167816: tolerable FAIL - PUSHED

2022-01-25 Thread osstest service owner
flight 167816 xen-4.12-testing real [real]
http://logs.test-lab.xenproject.org/osstest/logs/167816/

Failures :-/ but no regressions.

Tests which did not succeed, but are not blocking:
 test-amd64-amd64-xl-qcow219 guest-localmigrate/x10   fail  like 167214
 test-amd64-i386-xl-qemuu-win7-amd64 19 guest-stop fail like 167214
 test-armhf-armhf-libvirt-raw 15 saverestore-support-checkfail  like 167214
 test-armhf-armhf-libvirt 16 saverestore-support-checkfail  like 167214
 test-armhf-armhf-libvirt-qcow2 15 saverestore-support-check   fail like 167214
 test-amd64-amd64-xl-qemuu-ws16-amd64 19 guest-stopfail like 167214
 test-amd64-amd64-xl-qemut-ws16-amd64 19 guest-stopfail like 167214
 test-amd64-i386-xl-qemuu-ws16-amd64 19 guest-stop fail like 167214
 test-amd64-amd64-qemuu-nested-amd 20 debian-hvm-install/l1/l2 fail like 167214
 test-amd64-amd64-xl-qemut-win7-amd64 19 guest-stopfail like 167214
 test-amd64-i386-xl-qemut-win7-amd64 19 guest-stop fail like 167214
 test-amd64-amd64-xl-qemuu-win7-amd64 19 guest-stopfail like 167214
 test-amd64-i386-xl-qemut-ws16-amd64 19 guest-stop fail like 167214
 test-arm64-arm64-xl-thunderx 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-thunderx 16 saverestore-support-checkfail   never pass
 test-amd64-i386-libvirt-xsm  15 migrate-support-checkfail   never pass
 test-amd64-i386-xl-pvshim14 guest-start  fail   never pass
 test-amd64-i386-libvirt  15 migrate-support-checkfail   never pass
 test-amd64-amd64-libvirt-xsm 15 migrate-support-checkfail   never pass
 test-amd64-amd64-libvirt 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit1  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-seattle  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit1  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-seattle  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-credit2  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit2  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-libvirt-xsm 15 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-xsm 16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-vhd  14 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-vhd  15 saverestore-support-checkfail   never pass
 test-amd64-amd64-libvirt-vhd 14 migrate-support-checkfail   never pass
 test-armhf-armhf-xl  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-credit2  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-credit2  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-credit1  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-credit1  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl  16 saverestore-support-checkfail   never pass
 test-amd64-amd64-libvirt-qemuu-debianhvm-amd64-xsm 13 migrate-support-check 
fail never pass
 test-armhf-armhf-xl-multivcpu 15 migrate-support-checkfail  never pass
 test-armhf-armhf-xl-multivcpu 16 saverestore-support-checkfail  never pass
 test-armhf-armhf-xl-arndale  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-arndale  16 saverestore-support-checkfail   never pass
 test-amd64-i386-libvirt-qemuu-debianhvm-amd64-xsm 13 migrate-support-check 
fail never pass
 test-amd64-i386-libvirt-raw  14 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-raw 14 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-raw 15 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-rtds 15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-rtds 16 saverestore-support-checkfail   never pass
 test-armhf-armhf-libvirt-raw 14 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-cubietruck 15 migrate-support-checkfail never pass
 test-armhf-armhf-xl-cubietruck 16 saverestore-support-checkfail never pass
 test-armhf-armhf-libvirt 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-vhd  14 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-vhd  15 saverestore-support-checkfail   never pass
 test-armhf-armhf-libvirt-qcow2 14 migrate-support-checkfail never pass

version targeted for testing:
 xen  71e9d0c94dd71

Re: [XEN PATCH v2 2/5] xen: export get_free_port

2022-01-25 Thread Stefano Stabellini
On Tue, 25 Jan 2022, Julien Grall wrote:
> On 25/01/2022 22:49, Stefano Stabellini wrote:
> > On Tue, 25 Jan 2022, Jan Beulich wrote:
> > > On 25.01.2022 02:10, Stefano Stabellini wrote:
> > > > On Sun, 23 Jan 2022, Julien Grall wrote:
> > > > > > diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
> > > > > > index da88ad141a..5b0bcaaad4 100644
> > > > > > --- a/xen/common/event_channel.c
> > > > > > +++ b/xen/common/event_channel.c
> > > > > > @@ -232,7 +232,7 @@ int evtchn_allocate_port(struct domain *d,
> > > > > > evtchn_port_t
> > > > > > port)
> > > > > >return 0;
> > > > > >}
> > > > > >-static int get_free_port(struct domain *d)
> > > > > > +int get_free_port(struct domain *d)
> > > > > 
> > > > > I dislike the idea to expose get_free_port() (or whichever name we
> > > > > decide)
> > > > > because this can be easily misused.
> > > > > 
> > > > > In fact looking at your next patch (#3), you are misusing it as it is
> > > > > meant to
> > > > > be called with d->event_lock. I know this doesn't much matter
> > > > > in your situation because this is done at boot with no other domains
> > > > > running
> > > > > (or potentially any event channel allocation). However, I still think
> > > > > we
> > > > > should get the API right.
> > > > > 
> > > > > I am also not entirely happy of open-coding the allocation in
> > > > > domain_build.c.
> > > > > Instead, I would prefer if we provide a new helper to allocate an
> > > > > unbound
> > > > > event channel. This would be similar to your v1 (I still need to
> > > > > review the
> > > > > patch though).
> > > > 
> > > > I am happy to go back to v1 and address feedback on that patch. However,
> > > > I am having difficulties with the implementation. Jan pointed out:
> > > > 
> > > > 
> > > > > > -
> > > > > > -chn->state = ECS_UNBOUND;
> > > > > 
> > > > > This cannot be pulled ahead of the XSM check (or in general anything
> > > > > potentially resulting in an error), as check_free_port() relies on
> > > > > ->state remaining ECS_FREE until it is known that the calling function
> > > > > can't fail anymore.
> > > > 
> > > > This makes it difficult to reuse _evtchn_alloc_unbound for the
> > > > implementation of evtchn_alloc_unbound. In fact, I couldn't find a way
> > > > to do it.
> > > > 
> > > > Instead, I just create a new public function called
> > > > "evtchn_alloc_unbound" and renamed the existing funtion to
> > > > "_evtchn_alloc_unbound" (this to addresses Jan's feedback that the
> > > > static function should be the one starting with "_"). So the function
> > > > names are inverted compared to v1.
> > > > 
> > > > Please let me know if you have any better suggestions.
> > > > 
> > > > 
> > > > diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
> > > > index da88ad141a..c6b7dd7fbd 100644
> > > > --- a/xen/common/event_channel.c
> > > > +++ b/xen/common/event_channel.c
> > > > @@ -18,6 +18,7 @@
> > > > #include 
> > > >   #include 
> > > > +#include 
> > > >   #include 
> > > >   #include 
> > > >   #include 
> > > > @@ -284,7 +285,27 @@ void evtchn_free(struct domain *d, struct evtchn
> > > > *chn)
> > > >   xsm_evtchn_close_post(chn);
> > > >   }
> > > >   -static int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
> > > > +struct evtchn *evtchn_alloc_unbound(struct domain *d, domid_t
> > > > remote_dom)
> > > > +{
> > > > +struct evtchn *chn;
> > > > +int port;
> > > > +
> > > > +if ( (port = get_free_port(d)) < 0 )
> > > > +return ERR_PTR(port);
> > > > +chn = evtchn_from_port(d, port);
> > > > +
> > > > +evtchn_write_lock(chn);
> > > > +
> > > > +chn->state = ECS_UNBOUND;
> > > > +chn->u.unbound.remote_domid = remote_dom;
> > > > +evtchn_port_init(d, chn);
> > > > +
> > > > +evtchn_write_unlock(chn);
> > > > +
> > > > +return chn;
> > > > +}
> > > > +
> > > > +static int _evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
> > > >   {
> > > >   struct evtchn *chn;
> > > >   struct domain *d;
> > > 
> > > Instead of introducing a clone of this function (with, btw, still
> > > insufficient locking), did you consider simply using the existing
> > > evtchn_alloc_unbound() as-is, i.e. with the caller passing
> > > evtchn_alloc_unbound_t *?
> > 
> > Yes, we tried that first. Unfortunately the (dummy) XSM check cannot
> > work. This is how we would want to call the function:
> > 
> > 
> >  alloc.dom = d->domain_id;
> >  alloc.remote_dom = hardware_domain->domain_id;
> >  rc = evtchn_alloc_unbound(&alloc);
> > 
> > 
> > This is the implementation of the XSM check:
> > 
> > static XSM_INLINE int xsm_evtchn_unbound(
> >  XSM_DEFAULT_ARG struct domain *d, struct evtchn *chn, domid_t id2)
> > {
> >  XSM_ASSERT_ACTION(XSM_TARGET);
> >  return xsm_default_action(action, current->domain, d);
> > }
> > 
> > 
> > Note the usage of current->domain. If you have any suggestions on how to
> > fix it please let me know.
> 

Re: [XEN PATCH v2 3/5] xen/arm: configure dom0less domain for enabling xenstore after boot

2022-01-25 Thread Stefano Stabellini
On Sun, 23 Jan 2022, Julien Grall wrote:
> On 13/01/2022 14:15, Bertrand Marquis wrote:
> > Hi Stefano,
> > 
> > + Penny in CC for the question.
> > 
> > > On 13 Jan 2022, at 00:58, Stefano Stabellini 
> > > wrote:
> > > 
> > > From: Luca Miccio 
> > > 
> > > If "xen,enhanced" is enabled, then add to dom0less domains:
> > > 
> > > - the hypervisor node in device tree
> > > - the xenstore event channel
> > > 
> > > The xenstore event channel is also used for the first notification to
> > > let the guest know that xenstore has become available.
> > > 
> > > Signed-off-by: Luca Miccio 
> > > Signed-off-by: Stefano Stabellini 
> > > CC: Julien Grall 
> > > CC: Volodymyr Babchuk 
> > > CC: Bertrand Marquis 
> > 
> > Reviewed-by: Bertrand Marquis 
> > 
> > Just one question: GUEST_GNTTAB_BASE is fixed but could it be a problem for
> > a direct map guest in the future ?
> It will be an issue. I think we can re-use the same method as we do in dom0
> (see find_gnttab_region()).

Good idea. I prototyped it and it works fine.  I am not going to add the
patch to this series because it needs Penny's but I can easily provide a
patch to her for it.



Re: automation: remove python-dev from unstable-arm64v8.dockerfile

2022-01-25 Thread Andrew Cooper
On 26/01/2022 00:31, Stefano Stabellini wrote:
> Debian unstable doesn't have the legacy python-dev package anymore.
>
> Signed-off-by: Stefano Stabellini 

That's fine, but:

$ git grep python-dev -- automation/build/debian/unstable*
automation/build/debian/unstable-arm64v8.dockerfile:18:    python-dev \
automation/build/debian/unstable-i386.dockerfile:20:    python-dev \
automation/build/debian/unstable.dockerfile:18:    python-dev \

All 3 want editing together, even if we don't have a reason to rebuild
the x86 containers yet.

With that done, Acked-by: Andrew Cooper  to
save a trivial repost.



automation: remove python-dev from unstable-arm64v8.dockerfile

2022-01-25 Thread Stefano Stabellini
Debian unstable doesn't have the legacy python-dev package anymore.

Signed-off-by: Stefano Stabellini 

diff --git a/automation/build/debian/unstable-arm64v8.dockerfile 
b/automation/build/debian/unstable-arm64v8.dockerfile
index 4409f596e1..5c73458d9d 100644
--- a/automation/build/debian/unstable-arm64v8.dockerfile
+++ b/automation/build/debian/unstable-arm64v8.dockerfile
@@ -15,7 +15,6 @@ RUN apt-get update && \
 zlib1g-dev \
 libncurses5-dev \
 libssl-dev \
-python-dev \
 python3-dev \
 xorg-dev \
 uuid-dev \



[xen-4.16-testing test] 167814: regressions - FAIL

2022-01-25 Thread osstest service owner
flight 167814 xen-4.16-testing real [real]
http://logs.test-lab.xenproject.org/osstest/logs/167814/

Regressions :-(

Tests which did not succeed and are blocking,
including tests which could not be run:
 build-amd64   6 xen-buildfail REGR. vs. 167620
 build-amd64-xsm   6 xen-buildfail REGR. vs. 167620

Tests which did not succeed, but are not blocking:
 test-amd64-i386-qemuu-rhel6hvm-amd  1 build-check(1)   blocked n/a
 test-amd64-i386-qemuu-rhel6hvm-intel  1 build-check(1) blocked n/a
 test-amd64-i386-xl1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-pvshim 1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-qemut-debianhvm-amd64  1 build-check(1) blocked n/a
 test-amd64-i386-xl-qemut-debianhvm-i386-xsm  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemut-stubdom-debianhvm-amd64-xsm 1 build-check(1) blocked 
n/a
 test-amd64-i386-xl-qemut-win7-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemut-ws16-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-amd64  1 build-check(1) blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-amd64-shadow  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-i386-xsm  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-dmrestrict-amd64-dmrestrict 1 build-check(1) blocked 
n/a
 test-amd64-i386-xl-qemuu-ws16-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-shadow 1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-vhd1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-xsm1 build-check(1)   blocked  n/a
 test-amd64-coresched-amd64-xl  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-xsm   1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-shadow1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-rtds  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-qemuu-ws16-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-win7-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-ovmf-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-dmrestrict-amd64-dmrestrict 1 build-check(1) blocked 
n/a
 test-amd64-amd64-xl-qemuu-debianhvm-i386-xsm  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-debianhvm-amd64-shadow  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-debianhvm-amd64  1 build-check(1)blocked n/a
 test-amd64-amd64-xl-qemut-ws16-amd64  1 build-check(1) blocked n/a
 build-amd64-libvirt   1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-qemut-win7-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemut-stubdom-debianhvm-amd64-xsm 1 build-check(1) blocked 
n/a
 test-amd64-amd64-xl-qemut-debianhvm-i386-xsm  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemut-debianhvm-amd64  1 build-check(1)blocked n/a
 test-amd64-amd64-xl-qcow2 1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvshim1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvhv2-intel  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvhv2-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-multivcpu  1 build-check(1)   blocked  n/a
 test-amd64-amd64-dom0pvh-xl-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-dom0pvh-xl-intel  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-credit2   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt  1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-pair  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-credit1   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-qemuu-debianhvm-amd64-xsm 1 build-check(1) blocked n/a
 test-amd64-amd64-libvirt-vhd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-xsm  1 build-check(1)   blocked  n/a
 test-amd64-amd64-livepatch1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-nested-intel  1 build-check(1)  blocked n/a
 test-amd64-amd64-migrupgrade  1 build-check(1)   blocked  n/a
 test-amd64-amd64-pair 1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-nested-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-pygrub   1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-freebsd11-amd64  1 build-check(1)   blocked n/a
 test-amd64-amd64-qemuu-freebsd12-amd64  1 build-check(1)   blocked n/a
 test-amd64-i386-xl-qemuu-ovmf-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-win7-amd64  1 build-che

[xen-4.14-testing bisection] complete build-amd64

2022-01-25 Thread osstest service owner
branch xen-4.14-testing
xenbranch xen-4.14-testing
job build-amd64
testid xen-build

Tree: ovmf git://xenbits.xen.org/osstest/ovmf.git
Tree: qemu git://xenbits.xen.org/qemu-xen-traditional.git
Tree: qemuu git://xenbits.xen.org/qemu-xen.git
Tree: seabios git://xenbits.xen.org/osstest/seabios.git
Tree: xen git://xenbits.xen.org/xen.git

*** Found and reproduced problem changeset ***

  Bug is in tree:  xen git://xenbits.xen.org/xen.git
  Bug introduced:  4e25a788d000e57c4d04fdd33c209d7173420580
  Bug not present: c45c2c2e09295ef3008a79d78673af0819ff4e4f
  Last fail repro: http://logs.test-lab.xenproject.org/osstest/logs/167837/


  commit 4e25a788d000e57c4d04fdd33c209d7173420580
  Author: Andrew Cooper 
  Date:   Tue Jan 25 13:52:30 2022 +0100
  
  x86/msr: Split MSR_SPEC_CTRL handling
  
  In order to fix a VT-x bug, and support MSR_SPEC_CTRL on AMD, move
  MSR_SPEC_CTRL handling into the new {pv,hvm}_{get,set}_reg() 
infrastructure.
  
  Duplicate the msrs->spec_ctrl.raw accesses in the PV and VT-x paths for 
now.
  The SVM path is currently unreachable because of the CPUID policy.
  
  No functional change.
  
  Signed-off-by: Andrew Cooper 
  Reviewed-by: Jan Beulich 
  master commit: 6536688439dbca1d08fd6db5be29c39e3917fb2f
  master date: 2022-01-20 16:32:11 +


For bisection revision-tuple graph see:
   
http://logs.test-lab.xenproject.org/osstest/results/bisect/xen-4.14-testing/build-amd64.xen-build.html
Revision IDs in each graph node refer, respectively, to the Trees above.


Running cs-bisection-step 
--graph-out=/home/logs/results/bisect/xen-4.14-testing/build-amd64.xen-build 
--summary-out=tmp/167837.bisection-summary --basis-template=167629 
--blessings=real,real-bisect,real-retry xen-4.14-testing build-amd64 xen-build
Searching for failure / basis pass:
 167812 fail [host=himrod1] / 167629 [host=fiano1] 167415 [host=godello0] 
167216 ok.
Failure / basis pass flights: 167812 / 167216
(tree with no url: minios)
Tree: ovmf git://xenbits.xen.org/osstest/ovmf.git
Tree: qemu git://xenbits.xen.org/qemu-xen-traditional.git
Tree: qemuu git://xenbits.xen.org/qemu-xen.git
Tree: seabios git://xenbits.xen.org/osstest/seabios.git
Tree: xen git://xenbits.xen.org/xen.git
Latest 7e5c603cba0823fd97456984f4cfc21c4c831b52 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
e4f02c12518c0fe8154950b2e34c56a92721626e 
50935b88b4cd7f9cefe9eb2ffc5150d06c501d05
Basis pass dfafa8e45382939fb5dc78e9d37b97b500a43613 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
2dd4b9b3f84019668719344b40dba79d681be41c 
c4cf5388652e8434652e30c73aa79635b4253675
Generating revisions with ./adhoc-revtuple-generator  
git://xenbits.xen.org/osstest/ovmf.git#dfafa8e45382939fb5dc78e9d37b97b500a43613-7e5c603cba0823fd97456984f4cfc21c4c831b52
 
git://xenbits.xen.org/qemu-xen-traditional.git#3c659044118e34603161457db9934a34f816d78b-3c659044118e34603161457db9934a34f816d78b
 
git://xenbits.xen.org/qemu-xen.git#d7d6a60e73ee21e82f0bac2036153f996e6c-d7d6a60e73ee21e82f0bac2036153f996e6c
 
git://xenbits.xen.org/osstest/seabios.git#2dd4b9b3f84019668719344b40dba79d681be\
 41c-e4f02c12518c0fe8154950b2e34c56a92721626e 
git://xenbits.xen.org/xen.git#c4cf5388652e8434652e30c73aa79635b4253675-50935b88b4cd7f9cefe9eb2ffc5150d06c501d05
Loaded 12639 nodes in revision graph
Searching for test results:
 167216 pass dfafa8e45382939fb5dc78e9d37b97b500a43613 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
2dd4b9b3f84019668719344b40dba79d681be41c 
c4cf5388652e8434652e30c73aa79635b4253675
 167415 [host=godello0]
 167629 [host=fiano1]
 167812 fail 7e5c603cba0823fd97456984f4cfc21c4c831b52 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
e4f02c12518c0fe8154950b2e34c56a92721626e 
50935b88b4cd7f9cefe9eb2ffc5150d06c501d05
 167821 pass dfafa8e45382939fb5dc78e9d37b97b500a43613 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
2dd4b9b3f84019668719344b40dba79d681be41c 
c4cf5388652e8434652e30c73aa79635b4253675
 167823 fail 7e5c603cba0823fd97456984f4cfc21c4c831b52 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
e4f02c12518c0fe8154950b2e34c56a92721626e 
50935b88b4cd7f9cefe9eb2ffc5150d06c501d05
 167824 pass 04eacd39439d55bb1a5cbd366c19b1c03d5c7846 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
2dd4b9b3f84019668719344b40dba79d681be41c 
c4cf5388652e8434652e30c73aa79635b4253675
 167825 pass ee1f8262b83dd88b30091e6e81221ff299796099 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
34c3fce6e7361c2e38f22a3d973d72b994285313 
cbadf67bcab4e29c883410db393f4f5ef34df04a
 167826 pass 9dd14fc91c174eae87fd122c7ac70073a363527f 
3c659044118e34603161457db9934a34f816d78b 
d7d6a60e73ee21e82f0bac2036153f996e6c 
6a62e0c

Re: [XEN PATCH v2 2/5] xen: export get_free_port

2022-01-25 Thread Julien Grall

Hi Stefano,

On 25/01/2022 22:49, Stefano Stabellini wrote:

On Tue, 25 Jan 2022, Jan Beulich wrote:

On 25.01.2022 02:10, Stefano Stabellini wrote:

On Sun, 23 Jan 2022, Julien Grall wrote:

diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
index da88ad141a..5b0bcaaad4 100644
--- a/xen/common/event_channel.c
+++ b/xen/common/event_channel.c
@@ -232,7 +232,7 @@ int evtchn_allocate_port(struct domain *d, evtchn_port_t
port)
   return 0;
   }
   -static int get_free_port(struct domain *d)
+int get_free_port(struct domain *d)


I dislike the idea to expose get_free_port() (or whichever name we decide)
because this can be easily misused.

In fact looking at your next patch (#3), you are misusing it as it is meant to
be called with d->event_lock. I know this doesn't much matter
in your situation because this is done at boot with no other domains running
(or potentially any event channel allocation). However, I still think we
should get the API right.

I am also not entirely happy of open-coding the allocation in domain_build.c.
Instead, I would prefer if we provide a new helper to allocate an unbound
event channel. This would be similar to your v1 (I still need to review the
patch though).


I am happy to go back to v1 and address feedback on that patch. However,
I am having difficulties with the implementation. Jan pointed out:



-
-chn->state = ECS_UNBOUND;


This cannot be pulled ahead of the XSM check (or in general anything
potentially resulting in an error), as check_free_port() relies on
->state remaining ECS_FREE until it is known that the calling function
can't fail anymore.


This makes it difficult to reuse _evtchn_alloc_unbound for the
implementation of evtchn_alloc_unbound. In fact, I couldn't find a way
to do it.

Instead, I just create a new public function called
"evtchn_alloc_unbound" and renamed the existing funtion to
"_evtchn_alloc_unbound" (this to addresses Jan's feedback that the
static function should be the one starting with "_"). So the function
names are inverted compared to v1.

Please let me know if you have any better suggestions.


diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
index da88ad141a..c6b7dd7fbd 100644
--- a/xen/common/event_channel.c
+++ b/xen/common/event_channel.c
@@ -18,6 +18,7 @@
  
  #include 

  #include 
+#include 
  #include 
  #include 
  #include 
@@ -284,7 +285,27 @@ void evtchn_free(struct domain *d, struct evtchn *chn)
  xsm_evtchn_close_post(chn);
  }
  
-static int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)

+struct evtchn *evtchn_alloc_unbound(struct domain *d, domid_t remote_dom)
+{
+struct evtchn *chn;
+int port;
+
+if ( (port = get_free_port(d)) < 0 )
+return ERR_PTR(port);
+chn = evtchn_from_port(d, port);
+
+evtchn_write_lock(chn);
+
+chn->state = ECS_UNBOUND;
+chn->u.unbound.remote_domid = remote_dom;
+evtchn_port_init(d, chn);
+
+evtchn_write_unlock(chn);
+
+return chn;
+}
+
+static int _evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
  {
  struct evtchn *chn;
  struct domain *d;


Instead of introducing a clone of this function (with, btw, still
insufficient locking), did you consider simply using the existing
evtchn_alloc_unbound() as-is, i.e. with the caller passing
evtchn_alloc_unbound_t *?


Yes, we tried that first. Unfortunately the (dummy) XSM check cannot
work. This is how we would want to call the function:


 alloc.dom = d->domain_id;
 alloc.remote_dom = hardware_domain->domain_id;
 rc = evtchn_alloc_unbound(&alloc);


This is the implementation of the XSM check:

static XSM_INLINE int xsm_evtchn_unbound(
 XSM_DEFAULT_ARG struct domain *d, struct evtchn *chn, domid_t id2)
{
 XSM_ASSERT_ACTION(XSM_TARGET);
 return xsm_default_action(action, current->domain, d);
}


Note the usage of current->domain. If you have any suggestions on how to
fix it please let me know.


If I am not mistaken, current should still point to a domain (in this 
case idle).


So one alternative would be to ignore XSM if current->domain == idle and 
the system is booting (this could be part of xsm_default_action())


Another alternative would be to switch current to another domain. 'dom0' 
wouldn't be a solution because it doesn't exist for "true" dom0less. So 
a possibility would be to use dom_xen or create a fake build domain to 
be used for XSM check during boot.


Cheers,

--
Julien Grall



Re: [XEN v4] xen/arm64: io: Decode ldr/str post-indexing instructions

2022-01-25 Thread Julien Grall

Hi,

On 25/01/2022 21:18, Ayan Kumar Halder wrote:

At the moment, Xen is only handling data abort with valid syndrome (i.e.
ISV=0). Unfortunately, this doesn't cover all the instructions a domain
could use to access MMIO regions.

For instance, a baremetal OS can use any of the following instructions, where
x1 contains the address of the MMIO region:

1.  ldr x2,[x1],#8
2.  ldr w2,[x1],#-4
3.  ldr x2,[x1],#-8
4.  ldr w2,[x1],#4
5.  ldrhw2,[x1],#2
6.  ldrbw2,[x1],#1
7.  str x2,[x1],#8
8.  str w2,[x1],#-4
9.  strhw2,[x1],#2
10. strbw2,[x1],#1

In the following two instructions, Rn could theoretically be stack pointer which
might contain the address of the MMIO region:-
11. ldrbw2,[Rn],#1
12. ldrbwzr,   [Rn],#1

In order to handle post-indexing store/load instructions (like those mentioned
above), Xen will need to fetch and decode the instruction.

This patch only cover post-index store/load instructions from AArch64 mode.
For now, this is left unimplemented for trap from AArch32 mode.

Signed-off-by: Ayan Kumar Halder 
---

Changelog :-
v2 - 1. Updated the rn register after reading from it. (Pointed by Julien,
 Stefano)
  2. Used a union to represent the instruction opcode (Suggestd by Bertrand)
  3. Fixed coding style issues (Pointed by Julien)
  4. In the previous patch, I was updating dabt->sign based on the 
signedness
 of imm9. This was incorrect. As mentioned in ARMv8 ARM  DDI 0487G.b,
 Page 3221, SSE indicates the signedness of the data item loaded. In our
 case, the data item loaded is always unsigned.

v3- 1. Handled all the variants of ldr/str (ie 64, 32, 16, 8 bit variants).
Thus, I have removed the check for "instr->code.opc == 0" (Suggested by
Andre)
 2. Handled the scenario when rn = SP, rt = XZR (Suggested by Jan, Andre)
 3. Added restriction for "rt != rn" (Suggested by Andre)
 4. Moved union ldr_str_instr_class {} to decode.h. This is the header 
included
by io.c and decode.c (where the union is referred). (Suggested by Jan)
 5. Indentation and typo fixes (Suggested by Jan)

v4- 1. Fixed the patch as per Stefano's comments on v3. They are as follows :-
 1.1 Use macros to determine the fixed values in the instruction opcode
 1.2 Checked if instr != NULL
 1.3 Changed some data types and added #define ARM_64 for AArch64 
specific
 code
 1.4 Moved post_increment_register() to decode.c so that the decoding
 logic is confined to a single file.
 1.5 Moved some checks from post_increment_register() to
 decode_loadstore_postindexing()
 1.6 Removed a duplicate check
 2. Updated the commit message as per Andre's comments.
 3. Changed the names of a label and some comments. *32bit* was erroneously
mentioned in a label and comments in decode_loadstore_postindexing()
although the function handled all variants of ldr/str post indexing.

  xen/arch/arm/decode.c | 124 +-
  xen/arch/arm/decode.h |  41 +-
  xen/arch/arm/io.c |  41 +++---
  3 files changed, 195 insertions(+), 11 deletions(-)

diff --git a/xen/arch/arm/decode.c b/xen/arch/arm/decode.c
index 792c2e92a7..0c12af7afa 100644
--- a/xen/arch/arm/decode.c
+++ b/xen/arch/arm/decode.c
@@ -84,6 +84,101 @@ bad_thumb2:
  return 1;
  }
  
+static int decode_loadstore_postindexing(register_t pc,


This is only handling AArch64 instruction. So please add aarch64 (or 
arm64) in the name.



+ struct hsr_dabt *dabt,
+ union ldr_str_instr_class *instr)
+{
+struct cpu_user_regs *regs = guest_cpu_user_regs();
+
+if ( instr == NULL )


Wouldn't it be a programming error? If so, should it be ASSERT(...)?


+{
+gprintk(XENLOG_ERR, "instr should not be NULL\n");
+return -EINVAL;
+}
+
+if ( raw_copy_from_guest(&instr->value, (void * __user)pc, sizeof (instr)) 
)
+{
+gprintk(XENLOG_ERR, "Could not copy the instruction from PC\n");
+return -EFAULT;
+}
+
+/*
+ * Rn -ne Rt for ldr/str instruction.
+ * Check https://developer.arm.com/documentation/dui0802/a/CIHGJHED
+ * (Register restrictions)


I find a bit odd that you are pointing to 3 different spec (Compiler, 
Cortex-A, Arm Arm). Can we please use only the Arm Arm? It should 
contain everything we need...



+ *
+ * The only exception for this is when rn = 31. It denotes SP ("Use of SP")
+ *
+ * And when rt = 31, it denotes wzr/xzr. (Refer
+ * 
https://developer.arm.com/documentation/den0024/a/ARMv8-Registers/AArch64-special-registers


Same here.

Also, please avoid URL and use the document reference (e.g.

Re: [XEN PATCH v2 2/5] xen: export get_free_port

2022-01-25 Thread Stefano Stabellini
On Tue, 25 Jan 2022, Jan Beulich wrote:
> On 25.01.2022 02:10, Stefano Stabellini wrote:
> > On Sun, 23 Jan 2022, Julien Grall wrote:
> >>> diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
> >>> index da88ad141a..5b0bcaaad4 100644
> >>> --- a/xen/common/event_channel.c
> >>> +++ b/xen/common/event_channel.c
> >>> @@ -232,7 +232,7 @@ int evtchn_allocate_port(struct domain *d, 
> >>> evtchn_port_t
> >>> port)
> >>>   return 0;
> >>>   }
> >>>   -static int get_free_port(struct domain *d)
> >>> +int get_free_port(struct domain *d)
> >>
> >> I dislike the idea to expose get_free_port() (or whichever name we decide)
> >> because this can be easily misused.
> >>
> >> In fact looking at your next patch (#3), you are misusing it as it is 
> >> meant to
> >> be called with d->event_lock. I know this doesn't much matter
> >> in your situation because this is done at boot with no other domains 
> >> running
> >> (or potentially any event channel allocation). However, I still think we
> >> should get the API right.
> >>
> >> I am also not entirely happy of open-coding the allocation in 
> >> domain_build.c.
> >> Instead, I would prefer if we provide a new helper to allocate an unbound
> >> event channel. This would be similar to your v1 (I still need to review the
> >> patch though).
> > 
> > I am happy to go back to v1 and address feedback on that patch. However,
> > I am having difficulties with the implementation. Jan pointed out:
> > 
> > 
> >>> -
> >>> -chn->state = ECS_UNBOUND;
> >>
> >> This cannot be pulled ahead of the XSM check (or in general anything
> >> potentially resulting in an error), as check_free_port() relies on
> >> ->state remaining ECS_FREE until it is known that the calling function
> >> can't fail anymore.
> > 
> > This makes it difficult to reuse _evtchn_alloc_unbound for the
> > implementation of evtchn_alloc_unbound. In fact, I couldn't find a way
> > to do it.
> > 
> > Instead, I just create a new public function called
> > "evtchn_alloc_unbound" and renamed the existing funtion to
> > "_evtchn_alloc_unbound" (this to addresses Jan's feedback that the
> > static function should be the one starting with "_"). So the function
> > names are inverted compared to v1.
> > 
> > Please let me know if you have any better suggestions.
> > 
> > 
> > diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
> > index da88ad141a..c6b7dd7fbd 100644
> > --- a/xen/common/event_channel.c
> > +++ b/xen/common/event_channel.c
> > @@ -18,6 +18,7 @@
> >  
> >  #include 
> >  #include 
> > +#include 
> >  #include 
> >  #include 
> >  #include 
> > @@ -284,7 +285,27 @@ void evtchn_free(struct domain *d, struct evtchn *chn)
> >  xsm_evtchn_close_post(chn);
> >  }
> >  
> > -static int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
> > +struct evtchn *evtchn_alloc_unbound(struct domain *d, domid_t remote_dom)
> > +{
> > +struct evtchn *chn;
> > +int port;
> > +
> > +if ( (port = get_free_port(d)) < 0 )
> > +return ERR_PTR(port);
> > +chn = evtchn_from_port(d, port);
> > +
> > +evtchn_write_lock(chn);
> > +
> > +chn->state = ECS_UNBOUND;
> > +chn->u.unbound.remote_domid = remote_dom;
> > +evtchn_port_init(d, chn);
> > +
> > +evtchn_write_unlock(chn);
> > +
> > +return chn;
> > +}
> > +
> > +static int _evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
> >  {
> >  struct evtchn *chn;
> >  struct domain *d;
> 
> Instead of introducing a clone of this function (with, btw, still
> insufficient locking), did you consider simply using the existing
> evtchn_alloc_unbound() as-is, i.e. with the caller passing
> evtchn_alloc_unbound_t *?

Yes, we tried that first. Unfortunately the (dummy) XSM check cannot
work. This is how we would want to call the function:


alloc.dom = d->domain_id;
alloc.remote_dom = hardware_domain->domain_id;
rc = evtchn_alloc_unbound(&alloc);


This is the implementation of the XSM check:

static XSM_INLINE int xsm_evtchn_unbound(
XSM_DEFAULT_ARG struct domain *d, struct evtchn *chn, domid_t id2)
{
XSM_ASSERT_ACTION(XSM_TARGET);
return xsm_default_action(action, current->domain, d);
}


Note the usage of current->domain. If you have any suggestions on how to
fix it please let me know.



Re: [XEN v4] xen/arm64: io: Decode ldr/str post-indexing instructions

2022-01-25 Thread Stefano Stabellini
On Tue, 25 Jan 2022, Ayan Kumar Halder wrote:
> At the moment, Xen is only handling data abort with valid syndrome (i.e.
> ISV=0). Unfortunately, this doesn't cover all the instructions a domain
> could use to access MMIO regions.
> 
> For instance, a baremetal OS can use any of the following instructions, where
> x1 contains the address of the MMIO region:
> 
> 1.  ldr x2,[x1],#8
> 2.  ldr w2,[x1],#-4
> 3.  ldr x2,[x1],#-8
> 4.  ldr w2,[x1],#4
> 5.  ldrhw2,[x1],#2
> 6.  ldrbw2,[x1],#1
> 7.  str x2,[x1],#8
> 8.  str w2,[x1],#-4
> 9.  strhw2,[x1],#2
> 10. strbw2,[x1],#1
> 
> In the following two instructions, Rn could theoretically be stack pointer 
> which
> might contain the address of the MMIO region:-
> 11. ldrbw2,[Rn],#1
> 12. ldrbwzr,   [Rn],#1
> 
> In order to handle post-indexing store/load instructions (like those mentioned
> above), Xen will need to fetch and decode the instruction.
> 
> This patch only cover post-index store/load instructions from AArch64 mode.
> For now, this is left unimplemented for trap from AArch32 mode.

NIT: "For now, AArch32 mode is left unimplemented."


> Signed-off-by: Ayan Kumar Halder 
> ---
> 
> Changelog :-
> v2 - 1. Updated the rn register after reading from it. (Pointed by Julien,
> Stefano)
>  2. Used a union to represent the instruction opcode (Suggestd by 
> Bertrand)
>  3. Fixed coding style issues (Pointed by Julien)
>  4. In the previous patch, I was updating dabt->sign based on the 
> signedness
> of imm9. This was incorrect. As mentioned in ARMv8 ARM  DDI 0487G.b,
> Page 3221, SSE indicates the signedness of the data item loaded. In 
> our
> case, the data item loaded is always unsigned.
> 
> v3- 1. Handled all the variants of ldr/str (ie 64, 32, 16, 8 bit variants).
>Thus, I have removed the check for "instr->code.opc == 0" (Suggested by
>Andre)
> 2. Handled the scenario when rn = SP, rt = XZR (Suggested by Jan, Andre)
> 3. Added restriction for "rt != rn" (Suggested by Andre)
> 4. Moved union ldr_str_instr_class {} to decode.h. This is the header 
> included
>by io.c and decode.c (where the union is referred). (Suggested by Jan)
> 5. Indentation and typo fixes (Suggested by Jan)
> 
> v4- 1. Fixed the patch as per Stefano's comments on v3. They are as follows :-
> 1.1 Use macros to determine the fixed values in the instruction opcode
> 1.2 Checked if instr != NULL
> 1.3 Changed some data types and added #define ARM_64 for AArch64 
> specific
> code 
> 1.4 Moved post_increment_register() to decode.c so that the decoding
> logic is confined to a single file.
> 1.5 Moved some checks from post_increment_register() to
> decode_loadstore_postindexing()
> 1.6 Removed a duplicate check
> 2. Updated the commit message as per Andre's comments.
> 3. Changed the names of a label and some comments. *32bit* was erroneously
>mentioned in a label and comments in decode_loadstore_postindexing()
>although the function handled all variants of ldr/str post indexing.
> 
>  xen/arch/arm/decode.c | 124 +-
>  xen/arch/arm/decode.h |  41 +-
>  xen/arch/arm/io.c |  41 +++---
>  3 files changed, 195 insertions(+), 11 deletions(-)
> 
> diff --git a/xen/arch/arm/decode.c b/xen/arch/arm/decode.c
> index 792c2e92a7..0c12af7afa 100644
> --- a/xen/arch/arm/decode.c
> +++ b/xen/arch/arm/decode.c
> @@ -84,6 +84,101 @@ bad_thumb2:
>  return 1;
>  }
>  
> +static int decode_loadstore_postindexing(register_t pc,
> + struct hsr_dabt *dabt,
> + union ldr_str_instr_class *instr)
> +{
> +struct cpu_user_regs *regs = guest_cpu_user_regs();
> +
> +if ( instr == NULL )
> +{
> +gprintk(XENLOG_ERR, "instr should not be NULL\n");
> +return -EINVAL;
> +}
> +
> +if ( raw_copy_from_guest(&instr->value, (void * __user)pc, sizeof 
> (instr)) )
> +{
> +gprintk(XENLOG_ERR, "Could not copy the instruction from PC\n");
> +return -EFAULT;
> +}
> +
> +/*
> + * Rn -ne Rt for ldr/str instruction.
> + * Check https://developer.arm.com/documentation/dui0802/a/CIHGJHED
> + * (Register restrictions)
> + *
> + * The only exception for this is when rn = 31. It denotes SP ("Use of 
> SP")
> + *
> + * And when rt = 31, it denotes wzr/xzr. (Refer
> + * 
> https://developer.arm.com/documentation/den0024/a/ARMv8-Registers/AArch64-special-registers
> + * "There is no register called X31 or W31. Many instructions are encoded
> + * such that the number 31 represents the zero register, ZR (WZR/XZR)."
> + 

[xen-4.15-testing test] 167813: regressions - FAIL

2022-01-25 Thread osstest service owner
flight 167813 xen-4.15-testing real [real]
http://logs.test-lab.xenproject.org/osstest/logs/167813/

Regressions :-(

Tests which did not succeed and are blocking,
including tests which could not be run:
 build-amd64-xsm   6 xen-buildfail REGR. vs. 167628
 build-amd64   6 xen-buildfail REGR. vs. 167628

Regressions which are regarded as allowable (not blocking):
 test-armhf-armhf-xl-rtds18 guest-start/debian.repeat fail REGR. vs. 167628

Tests which did not succeed, but are not blocking:
 test-amd64-i386-qemuu-rhel6hvm-amd  1 build-check(1)   blocked n/a
 test-amd64-i386-qemuu-rhel6hvm-intel  1 build-check(1) blocked n/a
 test-amd64-i386-xl1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-pvshim 1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-qemut-debianhvm-amd64  1 build-check(1) blocked n/a
 test-amd64-i386-xl-qemut-debianhvm-i386-xsm  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemut-stubdom-debianhvm-amd64-xsm 1 build-check(1) blocked 
n/a
 test-amd64-i386-xl-qemut-win7-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemut-ws16-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-amd64  1 build-check(1) blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-amd64-shadow  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-i386-xsm  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-dmrestrict-amd64-dmrestrict 1 build-check(1) blocked 
n/a
 test-amd64-i386-xl-qemuu-ws16-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-shadow 1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-vhd1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-xsm1 build-check(1)   blocked  n/a
 test-amd64-coresched-amd64-xl  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-xsm   1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-shadow1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-rtds  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-qemuu-ws16-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-win7-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-ovmf-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-dmrestrict-amd64-dmrestrict 1 build-check(1) blocked 
n/a
 test-amd64-amd64-xl-qemuu-debianhvm-i386-xsm  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-debianhvm-amd64-shadow  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-debianhvm-amd64  1 build-check(1)blocked n/a
 test-amd64-amd64-xl-qemut-ws16-amd64  1 build-check(1) blocked n/a
 build-amd64-libvirt   1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-qemut-win7-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemut-stubdom-debianhvm-amd64-xsm 1 build-check(1) blocked 
n/a
 test-amd64-amd64-xl-qemut-debianhvm-i386-xsm  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemut-debianhvm-amd64  1 build-check(1)blocked n/a
 test-amd64-amd64-xl-qcow2 1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvshim1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvhv2-intel  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvhv2-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-multivcpu  1 build-check(1)   blocked  n/a
 test-amd64-amd64-dom0pvh-xl-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-dom0pvh-xl-intel  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-credit2   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt  1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-pair  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-credit1   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-qemuu-debianhvm-amd64-xsm 1 build-check(1) blocked n/a
 test-amd64-amd64-libvirt-vhd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-xsm  1 build-check(1)   blocked  n/a
 test-amd64-amd64-livepatch1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-nested-intel  1 build-check(1)  blocked n/a
 test-amd64-amd64-migrupgrade  1 build-check(1)   blocked  n/a
 test-amd64-amd64-pair 1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-nested-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-pygrub   1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-freebsd11-amd64  1 build-check(1)   blocked n/a
 test-amd64-amd64-qemuu-freebsd12-amd64  1 build-check(1)   

Re: [XEN v3] xen/arm64: io: Decode ldr/str post-indexing instructions

2022-01-25 Thread Ayan Kumar Halder

Hi Stefano/Andre/All,

Thanks for the feedback.

On 22/01/2022 01:04, Stefano Stabellini wrote:

On Thu, 20 Jan 2022, Ayan Kumar Halder wrote:

At the moment, Xen is only handling data abort with valid syndrome (i.e.
ISV=0). Unfortunately, this doesn't cover all the instructions a domain
could use to access MMIO regions.

For instance, a baremetal OS can use any of the following instructions, where
x1 contains the address of the MMIO region:

1.  ldr x2,[x1],#4
2.  ldr w2,[x1],#-4
3.  ldr x2,[x1],#-8
4.  ldr w2,[x1],#4
5.  ldrhw2,[x1],#8
6.  ldrbw2,[x1],#16
7.  str x2,[x1],#4
8.  str w2,[x1],#-4
9.  strhw2,[x1],#8
10. strbw2,[x1],#16

In the following two instructions, sp contains the address of the MMIO region:-
11. ldrbw2,[sp],#16
12. ldrbwzr,   [sp],#16

In order to handle post-indexing store/load instructions (like those mentioned
above), Xen will need to fetch and decode the instruction.

This patch only cover post-index store/load instructions from AArch64 mode.
For now, this is left unimplemented for trap from AArch32 mode.

Signed-off-by: Ayan Kumar Halder 

This is a lot better, thanks!



---

Changelog :-
v2 - 1. Updated the rn register after reading from it. (Pointed by Julien,
 Stefano)
  2. Used a union to represent the instruction opcode (Suggestd by Bertrand)
  3. Fixed coding style issues (Pointed by Julien)
  4. In the previous patch, I was updating dabt->sign based on the 
signedness
 of imm9. This was incorrect. As mentioned in ARMv8 ARM  DDI 0487G.b,
 Page 3221, SSE indicates the signedness of the data item loaded. In our
 case, the data item loaded is always unsigned.

v3- 1. Handled all the variants of ldr/str (ie 64, 32, 16, 8 bit variants).
Thus, I have removed the check for "instr->code.opc == 0" (Suggested by
Andre)
 2. Handled the scenario when rn = SP, rt = XZR (Suggested by Jan, Andre)
 3. Added restriction for "rt != rn" (Suggested by Andre)
 4. Moved union ldr_str_instr_class {} to decode.h. This is the header 
included
by io.c and decode.c (where the union is referred). (Suggested by Jan)
 5. Indentation and typo fixes (Suggested by Jan)

Changes suggested but could not be considered due to reasons :-
 1. Using accessor macros instead of bitfields for "ldr_str_instr_class". 
(Andre)
Reason - I could not find a simple way to represent 9 bit signed integer
(ie imm9) without using bitfields. If I use accessor macros, then I need
to manually calculate two's complement to obtain the value when signed
bit is present.

 2. I/D cache cohenerncy (Andre)
Reason :- I could not see any instruction to flush the I cache.
Refer 
https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/IC--Instruction-Cache-operation--an-alias-of-SYS-?lang=en#sa_ic_op
So, this patch assumes that the I/D caches are coherent.

  xen/arch/arm/decode.c | 78 ++-
  xen/arch/arm/decode.h | 29 +++-
  xen/arch/arm/io.c | 66 
  3 files changed, 165 insertions(+), 8 deletions(-)

diff --git a/xen/arch/arm/decode.c b/xen/arch/arm/decode.c
index 792c2e92a7..f1c59ddd1a 100644
--- a/xen/arch/arm/decode.c
+++ b/xen/arch/arm/decode.c
@@ -84,6 +84,76 @@ bad_thumb2:
  return 1;
  }
  
+static int decode_loadstore_postindexing(register_t pc,

+ struct hsr_dabt *dabt,
+ union ldr_str_instr_class *instr)
+{
+if ( raw_copy_from_guest(&instr->value, (void * __user)pc, sizeof (instr)) 
)
+return -EFAULT;
+
+/*
+ * Rn -ne Rt for ldr/str instruction.
+ * Check https://developer.arm.com/documentation/dui0802/a/CIHGJHED
+ * (Register restrictions)
+ *
+ * The only exception for this is when rn = 31. It denotes SP ("Use of SP")
+ *
+ * And when rt = 31, it denotes wzr/xzr. (Refer
+ * 
https://developer.arm.com/documentation/den0024/a/ARMv8-Registers/AArch64-special-registers
+ * "There is no register called X31 or W31. Many instructions are encoded
+ * such that the number 31 represents the zero register, ZR (WZR/XZR)."
+ */
+if ( (instr->code.rn == instr->code.rt) && (instr->code.rn != 31) )
+return -EINVAL;

+/* First, let's check for the fixed values */
+if ( !((instr->code.fixed1 == 1) && (instr->code.fixed2 == 0) &&
+ (instr->code.fixed3 == 0) && (instr->code.fixed4 == 7)) )
+{
+gprintk(XENLOG_ERR, "Cannot decode instruction 0x%x",instr->value);
+gprintk(XENLOG_ERR, "Decoding not supported for instructions other 
than"
+" ldr/str post indexing\n");
+goto bad_32bit_loadstore;
+}

Ma

Re: [RFC v1 3/5] xen/arm: introduce SCMI-SMC mediator driver

2022-01-25 Thread Stefano Stabellini
On Tue, 25 Jan 2022, Oleksii Moisieiev wrote:
> On Mon, Jan 24, 2022 at 02:14:43PM -0800, Stefano Stabellini wrote:
> > On Mon, 24 Jan 2022, Julien Grall wrote:
> > > On 24/01/2022 19:06, Stefano Stabellini wrote:
> > > > It looks like XEN_DOMCTL_host_node_by_path and
> > > > XEN_DOMCTL_find_host_compatible_node would also solve the problem but I
> > > > think that a single hypercall that retrieves the entire host DTB would
> > > > be easier to implement
> > > 
> > > DOMCTL should only be used to handle per-domain information. If we want to
> > > create a new sub-hypercall of either __HYPERVISOR_platform_op or
> > > __HYPERVISOR_sysctl_op (not sure which one).
> > > 
> > > AFAICT, both are versioned.
> > > 
> > > > and more robust in the long term. >
> > > > hypfs has the advantage that it would create an interface more similar
> > > > to the one people are already used to on Linux systems
> > > > (/proc/device-tree). xl/libxl would have to scan the whole hypfs tree,
> > > > which intuitively I think it would be slower.
> > > 
> > > Even if you have the binary blob, you would still have to scan the
> > > device-tree. That said, it is probably going to be potentially a bit 
> > > faster
> > > because you have less hypercall.
> > > 
> > > However, here this is a trade-off between memory use and speed. If you 
> > > want
> > > speed, then you may have to transfer up to 2MB every time. So the 
> > > question is
> > > do we care more about speed or memory usage?
> > > 
> > > > Also the feature might be
> > > > harder to implement but I am not sure.
> > > > 
> > > > I don't have a strong preference and this is not a stable interface (we
> > > > don't have to be extra paranoid about forward and backward
> > > > compatibility). So I am fine either way. Let's see what the others think
> > > > as well.
> > > 
> > > My preference would be to use hypfs as this is cleaner than exposing a 
> > > blob.
> > 
> > That's also fine by me. Probably the hypfs implementation shouldn't be
> > much more difficult than something like
> > XEN_DOMCTL_host_node_by_path/XEN_DOMCTL_find_host_compatible_node.
> > 
> > 
> > > However, are we sure we can simply copy the content of the host 
> > > Device-Tree to
> > > the guest Device-Tree for SCMI? For instance, I know that for device
> > > passthrough there are some property that needs to be altered for some 
> > > devices.
> > > Hence, why it is not present. Although, I vaguely recalled to have 
> > > written a
> > > PoC, not sure if it was posted on the ML.
> > 
> > The SCMI node cannot be copied "as is" from host to guest. It needs a
> > couple of changes but they seem feasible as they are limited to the
> > channels exposed to the guest. (The generic device passthrough case is a
> > lot more difficult.)
> 
> 
> Hi Stefano,
> 
> What I'm thinking about is do we actually need to create SCMI node in DomU 
> device-tree?
> I have this question is because we don't need SCMI node to be present in DomU 
> device-tree if it has no passed-through devices, which are using scmi. 
> So if we don't have passed-through devices or do not provide DomU partial 
> device-tree 
> in config, then there is no need to create SCMI node.
> 
> For now I see the following possible domu configurations:
> 1) If DomU has a lot of passed-through devices and it's easier to inherit 
> host device-tree and disable not passed-through devices.
> Partial device tree will looks like this:
> 
> #include "r8a77961-salvator-xs.dts" //include host device tree
> 
> /
> {
>   soc {
>   ...
>   }
> 
> };
> 
> // Disable non passed-through devices
> &hscif {
>   status = "disabled";
> };
> 
> In this case DomU partial device-tree will inherit arm,scmi-smc and 
> arm,scmi-shmem nodes and all clock/reset/power-domains which are using scmi. 
> All this nodes can be copied to DomU device-tree from partial device-tree.

This is an almost dom0 configuration. For this kind of use-cases, I
think it is enough to handle dom0 automatically correctly. I wouldn't
ask for anything more than that.


> 2) DomU has few passed-through devices, so it's easier to add the device 
> nodes 
> to the passthrough node of DomU partial device-tree.
> DomU partial device-tree will look like this:
> {
>   scmi_shmem: scp-shmem@0x53FF {
>   compatible = "arm,scmi-shmem";
>   reg = <0x0 0x53FF 0x0 0x1>;  
>   };
>   scmi {
>   arm,smc-id = <>;
>   compatible = "arm,scmi-smc"; 
>   shmem = <&scmi_shmem>;
>   scmi_clock: protocol@14 {
>   ...
>   };
>   scmi_reset: protocol@16 {
>   ...
>   };
>   }; 
>   passthrough {
>   hscif0: serial@e654 { 
>   compatible = "renesas,hscif-r8a77961";
>   scmi_devid = <5>;
>   clocks = <&scmi_clock 5>;
>   resets = <&scmi_res

[XEN v4] xen/arm64: io: Decode ldr/str post-indexing instructions

2022-01-25 Thread Ayan Kumar Halder
At the moment, Xen is only handling data abort with valid syndrome (i.e.
ISV=0). Unfortunately, this doesn't cover all the instructions a domain
could use to access MMIO regions.

For instance, a baremetal OS can use any of the following instructions, where
x1 contains the address of the MMIO region:

1.  ldr x2,[x1],#8
2.  ldr w2,[x1],#-4
3.  ldr x2,[x1],#-8
4.  ldr w2,[x1],#4
5.  ldrhw2,[x1],#2
6.  ldrbw2,[x1],#1
7.  str x2,[x1],#8
8.  str w2,[x1],#-4
9.  strhw2,[x1],#2
10. strbw2,[x1],#1

In the following two instructions, Rn could theoretically be stack pointer which
might contain the address of the MMIO region:-
11. ldrbw2,[Rn],#1
12. ldrbwzr,   [Rn],#1

In order to handle post-indexing store/load instructions (like those mentioned
above), Xen will need to fetch and decode the instruction.

This patch only cover post-index store/load instructions from AArch64 mode.
For now, this is left unimplemented for trap from AArch32 mode.

Signed-off-by: Ayan Kumar Halder 
---

Changelog :-
v2 - 1. Updated the rn register after reading from it. (Pointed by Julien,
Stefano)
 2. Used a union to represent the instruction opcode (Suggestd by Bertrand)
 3. Fixed coding style issues (Pointed by Julien)
 4. In the previous patch, I was updating dabt->sign based on the signedness
of imm9. This was incorrect. As mentioned in ARMv8 ARM  DDI 0487G.b,
Page 3221, SSE indicates the signedness of the data item loaded. In our
case, the data item loaded is always unsigned.

v3- 1. Handled all the variants of ldr/str (ie 64, 32, 16, 8 bit variants).
   Thus, I have removed the check for "instr->code.opc == 0" (Suggested by
   Andre)
2. Handled the scenario when rn = SP, rt = XZR (Suggested by Jan, Andre)
3. Added restriction for "rt != rn" (Suggested by Andre)
4. Moved union ldr_str_instr_class {} to decode.h. This is the header 
included
   by io.c and decode.c (where the union is referred). (Suggested by Jan)
5. Indentation and typo fixes (Suggested by Jan)

v4- 1. Fixed the patch as per Stefano's comments on v3. They are as follows :-
1.1 Use macros to determine the fixed values in the instruction opcode
1.2 Checked if instr != NULL
1.3 Changed some data types and added #define ARM_64 for AArch64 
specific
code 
1.4 Moved post_increment_register() to decode.c so that the decoding
logic is confined to a single file.
1.5 Moved some checks from post_increment_register() to
decode_loadstore_postindexing()
1.6 Removed a duplicate check
2. Updated the commit message as per Andre's comments.
3. Changed the names of a label and some comments. *32bit* was erroneously
   mentioned in a label and comments in decode_loadstore_postindexing()
   although the function handled all variants of ldr/str post indexing.

 xen/arch/arm/decode.c | 124 +-
 xen/arch/arm/decode.h |  41 +-
 xen/arch/arm/io.c |  41 +++---
 3 files changed, 195 insertions(+), 11 deletions(-)

diff --git a/xen/arch/arm/decode.c b/xen/arch/arm/decode.c
index 792c2e92a7..0c12af7afa 100644
--- a/xen/arch/arm/decode.c
+++ b/xen/arch/arm/decode.c
@@ -84,6 +84,101 @@ bad_thumb2:
 return 1;
 }
 
+static int decode_loadstore_postindexing(register_t pc,
+ struct hsr_dabt *dabt,
+ union ldr_str_instr_class *instr)
+{
+struct cpu_user_regs *regs = guest_cpu_user_regs();
+
+if ( instr == NULL )
+{
+gprintk(XENLOG_ERR, "instr should not be NULL\n");
+return -EINVAL;
+}
+
+if ( raw_copy_from_guest(&instr->value, (void * __user)pc, sizeof (instr)) 
)
+{
+gprintk(XENLOG_ERR, "Could not copy the instruction from PC\n");
+return -EFAULT;
+}
+
+/*
+ * Rn -ne Rt for ldr/str instruction.
+ * Check https://developer.arm.com/documentation/dui0802/a/CIHGJHED
+ * (Register restrictions)
+ *
+ * The only exception for this is when rn = 31. It denotes SP ("Use of SP")
+ *
+ * And when rt = 31, it denotes wzr/xzr. (Refer
+ * 
https://developer.arm.com/documentation/den0024/a/ARMv8-Registers/AArch64-special-registers
+ * "There is no register called X31 or W31. Many instructions are encoded
+ * such that the number 31 represents the zero register, ZR (WZR/XZR)."
+ */
+if ( (instr->code.rn == instr->code.rt) && (instr->code.rn != 31) )
+{
+gprintk(XENLOG_ERR, "Rn should not be equal to Rt except for r31\n");
+return -EINVAL;
+}
+
+/* First, let's check for the fixed values */
+if ( (instr->value & POST_INDEX_FIXED_MASK) != POST_INDEX_FIXED_VALUE )
+{
+   

[xen-unstable-smoke test] 167819: tolerable all pass - PUSHED

2022-01-25 Thread osstest service owner
flight 167819 xen-unstable-smoke real [real]
http://logs.test-lab.xenproject.org/osstest/logs/167819/

Failures :-/ but no regressions.

Tests which did not succeed, but are not blocking:
 test-amd64-amd64-libvirt 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl  16 saverestore-support-checkfail   never pass

version targeted for testing:
 xen  9480a1a519cf016623f657dc544cb372a82b5708
baseline version:
 xen  329b7bed80032fd52904af6a0cac7dd3716d27cf

Last test of basis   167811  2022-01-25 11:02:54 Z0 days
Testing same since   167819  2022-01-25 16:00:27 Z0 days1 attempts


People who touched revisions under test:
  Jan Beulich 
  Julien Grall 

jobs:
 build-arm64-xsm  pass
 build-amd64  pass
 build-armhf  pass
 build-amd64-libvirt  pass
 test-armhf-armhf-xl  pass
 test-arm64-arm64-xl-xsm  pass
 test-amd64-amd64-xl-qemuu-debianhvm-amd64pass
 test-amd64-amd64-libvirt pass



sg-report-flight on osstest.test-lab.xenproject.org
logs: /home/logs/logs
images: /home/logs/images

Logs, config files, etc. are available at
http://logs.test-lab.xenproject.org/osstest/logs

Explanation of these reports, and of osstest in general, is at
http://xenbits.xen.org/gitweb/?p=osstest.git;a=blob;f=README.email;hb=master
http://xenbits.xen.org/gitweb/?p=osstest.git;a=blob;f=README;hb=master

Test harness code can be found at
http://xenbits.xen.org/gitweb?p=osstest.git;a=summary


Pushing revision :

To xenbits.xen.org:/home/xen/git/xen.git
   329b7bed80..9480a1a519  9480a1a519cf016623f657dc544cb372a82b5708 -> smoke



Re: [PATCH] libxl: force netback to wait for hotplug execution before connecting

2022-01-25 Thread Wei Liu
On Mon, Jan 24, 2022 at 05:02:48PM +0100, Roger Pau Monne wrote:
> By writing an empty "hotplug-status" xenstore node in the backend path
> libxl can force Linux netback to wait for hotplug script execution
> before proceeding to the 'connected' state.
> 
> This is required so that netback doesn't skip state 2 (InitWait) and
> thus blocks libxl waiting for such state in order to launch the
> hotplug script (see libxl__wait_device_connection).
> 
> Reported-by: James Dingwall 
> Signed-off-by: Roger Pau Monné 
> Tested-by: James Dingwall 

Reviewed-by: Wei Liu 



[xen-4.14-testing test] 167812: regressions - FAIL

2022-01-25 Thread osstest service owner
flight 167812 xen-4.14-testing real [real]
http://logs.test-lab.xenproject.org/osstest/logs/167812/

Regressions :-(

Tests which did not succeed and are blocking,
including tests which could not be run:
 build-amd64   6 xen-buildfail REGR. vs. 167629
 build-amd64-xsm   6 xen-buildfail REGR. vs. 167629

Tests which did not succeed, but are not blocking:
 test-amd64-i386-qemuu-rhel6hvm-amd  1 build-check(1)   blocked n/a
 test-amd64-i386-qemuu-rhel6hvm-intel  1 build-check(1) blocked n/a
 test-amd64-i386-xl1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-pvshim 1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-qemut-debianhvm-amd64  1 build-check(1) blocked n/a
 test-amd64-i386-xl-qemut-debianhvm-i386-xsm  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemut-stubdom-debianhvm-amd64-xsm 1 build-check(1) blocked 
n/a
 test-amd64-i386-xl-qemut-win7-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemut-ws16-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-amd64  1 build-check(1) blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-amd64-shadow  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-debianhvm-i386-xsm  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-dmrestrict-amd64-dmrestrict 1 build-check(1) blocked 
n/a
 test-amd64-i386-xl-qemuu-ws16-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-shadow 1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-vhd1 build-check(1)   blocked  n/a
 test-amd64-i386-xl-xsm1 build-check(1)   blocked  n/a
 test-amd64-coresched-amd64-xl  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-xsm   1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-shadow1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-rtds  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-qemuu-ws16-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-win7-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-ovmf-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-dmrestrict-amd64-dmrestrict 1 build-check(1) blocked 
n/a
 test-amd64-amd64-xl-qemuu-debianhvm-i386-xsm  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-debianhvm-amd64-shadow  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemuu-debianhvm-amd64  1 build-check(1)blocked n/a
 test-amd64-amd64-xl-qemut-ws16-amd64  1 build-check(1) blocked n/a
 build-amd64-libvirt   1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-qemut-win7-amd64  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemut-stubdom-debianhvm-amd64-xsm 1 build-check(1) blocked 
n/a
 test-amd64-amd64-xl-qemut-debianhvm-i386-xsm  1 build-check(1) blocked n/a
 test-amd64-amd64-xl-qemut-debianhvm-amd64  1 build-check(1)blocked n/a
 test-amd64-amd64-xl-qcow2 1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvshim1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvhv2-intel  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-pvhv2-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-multivcpu  1 build-check(1)   blocked  n/a
 test-amd64-amd64-dom0pvh-xl-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-dom0pvh-xl-intel  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-credit2   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt  1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-pair  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl-credit1   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-qemuu-debianhvm-amd64-xsm 1 build-check(1) blocked n/a
 test-amd64-amd64-libvirt-vhd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-xl   1 build-check(1)   blocked  n/a
 test-amd64-amd64-libvirt-xsm  1 build-check(1)   blocked  n/a
 test-amd64-amd64-livepatch1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-nested-intel  1 build-check(1)  blocked n/a
 test-amd64-amd64-migrupgrade  1 build-check(1)   blocked  n/a
 test-amd64-amd64-pair 1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-nested-amd  1 build-check(1)   blocked  n/a
 test-amd64-amd64-pygrub   1 build-check(1)   blocked  n/a
 test-amd64-amd64-qemuu-freebsd11-amd64  1 build-check(1)   blocked n/a
 test-amd64-amd64-qemuu-freebsd12-amd64  1 build-check(1)   blocked n/a
 test-amd64-i386-xl-qemuu-win7-amd64  1 build-check(1)  blocked n/a
 test-amd64-i386-xl-qemuu-ovmf-amd64  1 build-che

Re: [PATCH] libxl: force netback to wait for hotplug execution before connecting

2022-01-25 Thread Julien Grall

Hi Roger,

On 25/01/2022 16:09, Roger Pau Monné wrote:

On Tue, Jan 25, 2022 at 03:32:16PM +, Julien Grall wrote:

Hi,

On 24/01/2022 16:02, Roger Pau Monne wrote:

By writing an empty "hotplug-status" xenstore node in the backend path
libxl can force Linux netback to wait for hotplug script execution
before proceeding to the 'connected' state.


I was actually chasing the same issue today :).



This is required so that netback doesn't skip state 2 (InitWait) and


Technically netback never skip state 2 (otherwise it would always be
reproducible). Instead, libxl may not be able to observe state 2 because
receive a watch is asynchronous and doesn't contain the value of the node.
So the backend may have moved to Connected before the state is read.


Right, might be more accurate to say it skips waiting for hotplug
script execution, and thus jumps from state 2 into 4.


I would add the jump happens when the frontend decides to connect.


Note I think
it's also possible that by the time we setup the watch in libxl the
state has already been set to 4.


Correct.

Cheers,

--
Julien Grall



Re: [XEN PATCH v2 2/5] xen: export get_free_port

2022-01-25 Thread Julien Grall

Hi Jan,

On 25/01/2022 08:22, Jan Beulich wrote:

On 25.01.2022 02:10, Stefano Stabellini wrote:

On Sun, 23 Jan 2022, Julien Grall wrote:

diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
index da88ad141a..5b0bcaaad4 100644
--- a/xen/common/event_channel.c
+++ b/xen/common/event_channel.c
@@ -232,7 +232,7 @@ int evtchn_allocate_port(struct domain *d, evtchn_port_t
port)
   return 0;
   }
   -static int get_free_port(struct domain *d)
+int get_free_port(struct domain *d)


I dislike the idea to expose get_free_port() (or whichever name we decide)
because this can be easily misused.

In fact looking at your next patch (#3), you are misusing it as it is meant to
be called with d->event_lock. I know this doesn't much matter
in your situation because this is done at boot with no other domains running
(or potentially any event channel allocation). However, I still think we
should get the API right.

I am also not entirely happy of open-coding the allocation in domain_build.c.
Instead, I would prefer if we provide a new helper to allocate an unbound
event channel. This would be similar to your v1 (I still need to review the
patch though).


I am happy to go back to v1 and address feedback on that patch. However,
I am having difficulties with the implementation. Jan pointed out:



-
-chn->state = ECS_UNBOUND;


This cannot be pulled ahead of the XSM check (or in general anything
potentially resulting in an error), as check_free_port() relies on
->state remaining ECS_FREE until it is known that the calling function
can't fail anymore.


This makes it difficult to reuse _evtchn_alloc_unbound for the
implementation of evtchn_alloc_unbound. In fact, I couldn't find a way
to do it.

Instead, I just create a new public function called
"evtchn_alloc_unbound" and renamed the existing funtion to
"_evtchn_alloc_unbound" (this to addresses Jan's feedback that the
static function should be the one starting with "_"). So the function
names are inverted compared to v1.

Please let me know if you have any better suggestions.


diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
index da88ad141a..c6b7dd7fbd 100644
--- a/xen/common/event_channel.c
+++ b/xen/common/event_channel.c
@@ -18,6 +18,7 @@
  
  #include 

  #include 
+#include 
  #include 
  #include 
  #include 
@@ -284,7 +285,27 @@ void evtchn_free(struct domain *d, struct evtchn *chn)
  xsm_evtchn_close_post(chn);
  }
  
-static int evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)

+struct evtchn *evtchn_alloc_unbound(struct domain *d, domid_t remote_dom)
+{
+struct evtchn *chn;
+int port;
+
+if ( (port = get_free_port(d)) < 0 )
+return ERR_PTR(port);
+chn = evtchn_from_port(d, port);
+
+evtchn_write_lock(chn);
+
+chn->state = ECS_UNBOUND;
+chn->u.unbound.remote_domid = remote_dom;
+evtchn_port_init(d, chn);
+
+evtchn_write_unlock(chn);
+
+return chn;
+}
+
+static int _evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
  {
  struct evtchn *chn;
  struct domain *d;


Instead of introducing a clone of this function (with, btw, still
insufficient locking), did you consider simply using the existing
evtchn_alloc_unbound() as-is, i.e. with the caller passing
evtchn_alloc_unbound_t *?


This is feasible with some tweaking. Which reminds me that I have a 
similar patch to what you describe:


https://xenbits.xen.org/gitweb/?p=people/julieng/xen-unstable.git;a=commit;h=560d656a9a792450530eeefd0d06cfd54dcd7685

This is doing more than what we need here as it takes care about 
restoring a port (for Live-Update).


Note that They are forward port from 4.11 to unstable and untested on 
the latter.


Cheers,

--
Julien Grall



Re: [XEN v3] xen/arm64: io: Decode ldr/str post-indexing instructions

2022-01-25 Thread Andre Przywara
On Mon, 24 Jan 2022 17:58:55 +
Julien Grall  wrote:

Hi Julien,

> Hi Andre,
> 
> On 24/01/2022 14:36, Andre Przywara wrote:
> > On Mon, 24 Jan 2022 12:07:42 +  
> >> Also, if an instruction is being modified by the guest (after it has
> >> been loaded in the I cache), and if the guest does not invalidate the I
> >> cache + ISB, then this is a malicious behavior by the guest. Is my
> >> understanding correct ?  
> > 
> > I wouldn't say malicious per se, there might be legitimate reasons to do
> > so, but in the Xen context this is mostly irrelevant, since we don't trust
> > the guest anyway. So whether it's malicious or accidental, the hypervisor
> > might be mislead.  
> 
> I agree the hypervisor will be mislead to execute the wrong instruction. 
> But, in reality, I don't see how this is a massive problem as this 
> thread seems to imply. At best the guest will shoot itself in the foot.

I didn't really imply anything, I genuinely meant that I don't want to
spend brain cells thinking about possible exploits - I always figured you
(and Xen people in general) are so much better in this. (genuine
compliment!)
I was just pointing out that this emulation might be wrong then.
That ties back to the original question of how many bitter pills you want
to swallow for having this emulation code - which is your decision to make.

Cheers,
Andre

> IOW, for now, I think it is fine to assume that the guest will have 
> invalidated the cache instruction before executing any instruction that 
> may fault with ISV=0. This could be revisted if we have use-cases where 
> we really need to know what the guest executed.
> 
> Cheers,
> 




Re: [XEN PATCH v9 30/30] build: adding out-of-tree support to the xen build

2022-01-25 Thread Daniel P. Smith
On 1/25/22 06:01, Anthony PERARD wrote:
> This implement out-of-tree support, there's two ways to create an
> out-of-tree build tree (after that, `make` in that new directory
> works):
> make O=build
> mkdir build; cd build; make -f ../Makefile
> also works with an absolute path for both.
> 
> This implementation only works if the source tree is clean, as we use
> VPATH.
> 
> This patch copies most new code with handling out-of-tree build from
> Linux v5.12.
> 
> Signed-off-by: Anthony PERARD 
> Acked-by: Jan Beulich 
> ---
> 
> Notes:
> v9:
> - acked
> - add "source -> ." symlink in the in-tree build as well. This allow to
>   make relative symlink to the "common/efi/*.c" sources.
> - also now, the "source" symlink is removed on `distclean`, add added to
>   .gitignore.
> 
> v8:
> - replace script/mkmakefile by cmd_makefile like it's been done in Linux
>   5.13.
> - fix GNU Make version, replace few 0.81 by 3.81.
> - reorder include header path in CFLAGS ( -I ), to have the $(objtree)
>   paths listed before the $(srctree) paths. This will be usefull later
>   if we allow to build out-of-tree from a dirty source tree.
> - make use of -iquote
> - sometime, add -iquote path even when not necessary when building 
> in-tree.
> - extract code movement into a separate patch
> - make use of the new "$(srcdir)" shortcut
> - split command line in headers*.chk target into more lines
> 
>  .gitignore   |   1 +
>  xen/Makefile | 143 +++
>  xen/Rules.mk |  11 ++-
>  xen/arch/x86/arch.mk |   5 +-
>  xen/arch/x86/boot/Makefile   |   6 ++
>  xen/common/efi/efi-common.mk |   3 +-
>  xen/include/Makefile |  11 ++-
>  xen/test/livepatch/Makefile  |   2 +
>  xen/xsm/flask/Makefile   |   3 +-
>  xen/xsm/flask/ss/Makefile|   1 +
>  10 files changed, 164 insertions(+), 22 deletions(-)
> 



> diff --git a/xen/xsm/flask/Makefile b/xen/xsm/flask/Makefile
> index a99038cb5722..d25312f4fa1c 100644
> --- a/xen/xsm/flask/Makefile
> +++ b/xen/xsm/flask/Makefile
> @@ -4,7 +4,8 @@ obj-y += flask_op.o
>  
>  obj-y += ss/
>  
> -CFLAGS-y += -I$(obj)/include
> +CFLAGS-y += -iquote $(obj)/include
> +CFLAGS-y += -I$(srcdir)/include
>  
>  AWK = awk
>  
> diff --git a/xen/xsm/flask/ss/Makefile b/xen/xsm/flask/ss/Makefile
> index aba1339f3808..ffe92ec19ed6 100644
> --- a/xen/xsm/flask/ss/Makefile
> +++ b/xen/xsm/flask/ss/Makefile
> @@ -8,4 +8,5 @@ obj-y += services.o
>  obj-y += conditional.o
>  obj-y += mls.o
>  
> +CFLAGS-y += -iquote $(objtree)/xsm/flask/include
>  CFLAGS-y += -I$(srctree)/xsm/flask/include

Reviewed-by: Daniel P. Smith 

V/r,
Daniel P. Smith
Apertus Solutions, LLC



Re: [XEN PATCH v9 26/30] build: replace $(BASEDIR) and use $(srctree)

2022-01-25 Thread Daniel P. Smith
On 1/25/22 06:00, Anthony PERARD wrote:
> $(srctree) is a better description for the source directory than
> $(BASEDIR) that has been used for both source and build directory
> (which where the same).
> 
> This adds $(srctree) to a few path where make's VPATH=$(srctree) won't
> apply. And replace $(BASEDIR) by $(srctree).
> 
> Introduce "$(srcdir)" as a shortcut for "$(srctree)/$(src)" as the
> later is used often enough.
> 
> Signed-off-by: Anthony PERARD 
> Acked-by: Jan Beulich 
> ---
> 
> Notes:
> v9:
> - acked
> - reword "# shortcut for $(srctree)/$(src)" into "# shortcuts" in both
>   places.
> 
> v8:
> - merge of two patchs from v7:
> build: add $(srctree) in few key places
> build: replace $(BASEDIR) by $(srctree)
>   both patch were acked
> - introduce $(srcdir) as a shortcut for $(srctree)/$(src)
> 
>  xen/Kconfig |  4 ++--
>  xen/Makefile|  7 +++
>  xen/Rules.mk|  9 ++---
>  xen/arch/x86/arch.mk|  2 +-
>  xen/build.mk|  4 ++--
>  xen/common/Makefile |  2 +-
>  xen/common/libfdt/Makefile  |  2 +-
>  xen/include/Makefile| 14 +++---
>  xen/scripts/Kconfig.include |  2 +-
>  xen/scripts/Makefile.clean  |  5 -
>  xen/xsm/flask/Makefile  | 10 +-
>  11 files changed, 33 insertions(+), 28 deletions(-)



> diff --git a/xen/xsm/flask/Makefile b/xen/xsm/flask/Makefile
> index 4ac6fb8778ae..a99038cb5722 100644
> --- a/xen/xsm/flask/Makefile
> +++ b/xen/xsm/flask/Makefile
> @@ -8,8 +8,8 @@ CFLAGS-y += -I$(obj)/include
>  
>  AWK = awk
>  
> -FLASK_H_DEPEND := $(addprefix $(src)/policy/,security_classes initial_sids)
> -AV_H_DEPEND = $(src)/policy/access_vectors
> +FLASK_H_DEPEND := $(addprefix $(srcdir)/policy/,security_classes 
> initial_sids)
> +AV_H_DEPEND := $(srcdir)/policy/access_vectors
>  
>  FLASK_H_FILES := flask.h class_to_string.h initial_sid_to_string.h
>  AV_H_FILES := av_perm_to_string.h av_permissions.h
> @@ -18,14 +18,14 @@ ALL_H_FILES := $(addprefix include/,$(FLASK_H_FILES) 
> $(AV_H_FILES))
>  $(addprefix $(obj)/,$(obj-y)) $(obj)/ss/built_in.o: $(addprefix 
> $(obj)/,$(ALL_H_FILES))
>  extra-y += $(ALL_H_FILES)
>  
> -mkflask := $(src)/policy/mkflask.sh
> +mkflask := $(srcdir)/policy/mkflask.sh
>  quiet_cmd_mkflask = MKFLASK $@
>  cmd_mkflask = $(SHELL) $(mkflask) $(AWK) $(obj)/include $(FLASK_H_DEPEND)
>  
>  $(addprefix $(obj)/%/,$(FLASK_H_FILES)): $(FLASK_H_DEPEND) $(mkflask) FORCE
>   $(call if_changed,mkflask)
>  
> -mkaccess := $(src)/policy/mkaccess_vector.sh
> +mkaccess := $(srcdir)/policy/mkaccess_vector.sh
>  quiet_cmd_mkaccess = MKACCESS VECTOR $@
>  cmd_mkaccess = $(SHELL) $(mkaccess) $(AWK) $(obj)/include $(AV_H_DEPEND)
>  
> @@ -36,7 +36,7 @@ obj-bin-$(CONFIG_XSM_FLASK_POLICY) += flask-policy.o
>  $(obj)/flask-policy.o: $(obj)/policy.bin
>  
>  $(obj)/flask-policy.S: BINFILE_FLAGS := -i
> -$(obj)/flask-policy.S: $(BASEDIR)/tools/binfile FORCE
> +$(obj)/flask-policy.S: $(srctree)/tools/binfile FORCE
>   $(call if_changed,binfile,$(obj)/policy.bin xsm_flask_init_policy)
>  targets += flask-policy.S
>  

Reviewed-by: Daniel P. Smith 

V/r,
Daniel P. Smith
Apertus Solutions, LLC



Re: [XEN PATCH v9 20/30] build: rework "clean" to clean from the root dir

2022-01-25 Thread Daniel P. Smith
On 1/25/22 06:00, Anthony PERARD wrote:
> This will allow "clean" to work from an out-of-tree build when
> it will be available.
> 
> Some of the file been removed in current "clean" target aren't added
> to $(clean-files) because they are already listed in $(extra-) or
> $(extra-y).
> 
> Also start to clean files listed in $(targets). This allows to clean
> "common/config_data.S" and "xsm/flask/flask-policy.S" without
> having to list them a second time.
> 
> Also clean files in "arch/x86/boot" from that directory by allowing
> "clean" to descend into the subdir by adding "boot" into $(subdir-).
> 
> Signed-off-by: Anthony PERARD 
> Reviewed-by: Jan Beulich 
> ---
> 
> Notes:
> v9:
> - reviewed
> 
> v8:
> - fix comment for the shorthand
> - move this patch earlier in the series
> - replace some BASEDIR by objtree
> - start to clean $(targets) files.
> 
>  xen/Makefile| 24 
>  xen/arch/arm/Makefile   |  5 +
>  xen/arch/x86/Makefile   | 20 
>  xen/arch/x86/boot/Makefile  |  2 ++
>  xen/common/Makefile |  3 +--
>  xen/include/Makefile|  4 +---
>  xen/scripts/Kbuild.include  |  4 ++--
>  xen/scripts/Makefile.clean  | 14 +++---
>  xen/test/livepatch/Makefile |  4 +---
>  xen/xsm/flask/Makefile  |  4 +---
>  10 files changed, 44 insertions(+), 40 deletions(-)
> 



> diff --git a/xen/xsm/flask/Makefile b/xen/xsm/flask/Makefile
> index 832f65274cc0..4ac6fb8778ae 100644
> --- a/xen/xsm/flask/Makefile
> +++ b/xen/xsm/flask/Makefile
> @@ -49,6 +49,4 @@ $(obj)/policy.bin: FORCE
>   FLASK_BUILD_DIR=$(FLASK_BUILD_DIR) POLICY_FILENAME=$(POLICY_SRC)
>   cmp -s $(POLICY_SRC) $@ || cp $(POLICY_SRC) $@
>  
> -.PHONY: clean
> -clean::
> - rm -f $(ALL_H_FILES) policy.* $(POLICY_SRC) flask-policy.S
> +clean-files := policy.* $(POLICY_SRC)

Reviewed-by: Daniel P. Smith 

V/r,
Daniel P. Smith
Apertus Solutions, LLC



Re: [XEN PATCH v9 19/30] build: clean-up "clean" rules of duplication

2022-01-25 Thread Daniel P. Smith
On 1/25/22 06:00, Anthony PERARD wrote:
> All those files to be removed are already done in the main Makefile,
> either by the "find" command or directly (for $(TARGET).efi).
> 
> Signed-off-by: Anthony PERARD 
> Acked-by: Jan Beulich 
> Acked-by: Julien Grall 
> ---
>  xen/Makefile| 2 +-
>  xen/arch/arm/Makefile   | 1 -
>  xen/arch/x86/Makefile   | 5 ++---
>  xen/test/livepatch/Makefile | 2 +-
>  xen/xsm/flask/Makefile  | 2 +-
>  5 files changed, 5 insertions(+), 7 deletions(-)
> 



> diff --git a/xen/xsm/flask/Makefile b/xen/xsm/flask/Makefile
> index 49cf730cf098..832f65274cc0 100644
> --- a/xen/xsm/flask/Makefile
> +++ b/xen/xsm/flask/Makefile
> @@ -51,4 +51,4 @@ $(obj)/policy.bin: FORCE
>  
>  .PHONY: clean
>  clean::
> - rm -f $(ALL_H_FILES) *.o $(DEPS_RM) policy.* $(POLICY_SRC) 
> flask-policy.S
> + rm -f $(ALL_H_FILES) policy.* $(POLICY_SRC) flask-policy.S


Reviewed-by: Daniel P. Smith 

V/r,
Daniel P. Smith
Apertus Solutions, LLC



Re: [PATCH] libxl: force netback to wait for hotplug execution before connecting

2022-01-25 Thread Roger Pau Monné
On Tue, Jan 25, 2022 at 03:32:16PM +, Julien Grall wrote:
> Hi,
> 
> On 24/01/2022 16:02, Roger Pau Monne wrote:
> > By writing an empty "hotplug-status" xenstore node in the backend path
> > libxl can force Linux netback to wait for hotplug script execution
> > before proceeding to the 'connected' state.
> 
> I was actually chasing the same issue today :).
> 
> > 
> > This is required so that netback doesn't skip state 2 (InitWait) and
> 
> Technically netback never skip state 2 (otherwise it would always be
> reproducible). Instead, libxl may not be able to observe state 2 because
> receive a watch is asynchronous and doesn't contain the value of the node.
> So the backend may have moved to Connected before the state is read.

Right, might be more accurate to say it skips waiting for hotplug
script execution, and thus jumps from state 2 into 4. Note I think
it's also possible that by the time we setup the watch in libxl the
state has already been set to 4.

Thanks, Roger.



Re: [XEN PATCH v9 09/30] build: build everything from the root dir, use obj=$subdir

2022-01-25 Thread Daniel P. Smith
On 1/25/22 06:00, Anthony PERARD wrote:
> A subdirectory is now built by setting "$(obj)" instead of changing
> directory. "$(obj)" should always be set when using "Rules.mk" and
> thus a shortcut "$(build)" is introduced and should be used.
> 
> A new variable "$(need-builtin)" is introduce. It is to be used
> whenever a "built_in.o" is wanted from a subdirectory. "built_in.o"
> isn't the main target anymore, and thus only needs to depends on the
> objects that should be part of "built_in.o".
> 
> Introduce $(srctree) and $(objtree) to replace $(BASEDIR) in cases a
> relative path is better, and $(abs_srctree) and $(abs_objtree) which
> have an absolute path.
> 
> DEPS is updated as the existing macro to deal with it doesn't know
> about $(obj).
> 
> There's some changes in "Rules.mk" which in addition to deal with
> "$(obj)" also make it's looks more like "Makefile.build" from Linux
> v5.12.
> 
> test/Makefile doesn't need special handling in order to build
> everything under test/, Rules.mk will visit test/livepatch via
> $(subdir-y), thus "tests" "all" and "build" target are removed.
> "subtree-force-update" target isn't useful so it is removed as well.
> 
> test/livepatch/Makefile doesn't need default target anymore, Rules.mk
> will build everything in $(extra-y) and thus all *.livepatch.
> 
> Adjust cloc recipe: dependency files generated by CC will now have the
> full path to the source file, so we don't need to prepend the
> subdirectory. This fix some issue with source not been parsed by cloc
> before. Also source from tools/kconfig would be listed with changes in
> this patch so adjust the find command to stop listing the "tools"
> directory and thus kconfig. With a default build of Xen on X86, they
> are a few new files parsed by cloc:
> arch/x86/x86_64/compat/mm.c
> arch/x86/x86_64/mm.c
> common/compat/domain.c
> common/compat/memory.c
> common/compat/xlat.c
> 
> Signed-off-by: Anthony PERARD 
> Acked-by: Bob Eshleman 
> Acked-by: Julien Grall 
> Reviewed-by: Jan Beulich 
> ---
> 
> Notes:
> v9:
> - reviewed
> 
> v8:
> - rebased
> - move check for $(obj) in Rules.mk earlier
> - use $(sort ) when adding directories in $(subdir-y)
> - fix missing space between "DEPS" and ":="
> - use -iquote when adding '.' for xen/arch/x86/usercopy.o
> - introduce also abs_srctree and abs_objtree
>   those were in patch "build: replace $(BASEDIR) by $(objtree)"
> - reworked cmd_objcopy_fix_sym, we don't need to do anything for old
>   version of clang, and for recent version of clang the change is the
>   same a for gcc.
> - adjust "cloc" recipe
> - add some more $(Q) to silent $(MAKE) $(build)=...
> 
> v7:
> - Spell "Makefile.build" as "Rules.mk" in the warning in Rules.mk about
>   undefined $(obj).
> - use $(srctree) for include/xen/config.h
> - handle $(nocov-y) and $(noubsan-y), needed to add $(obj)/
> 
>  xen/Makefile|  36 ++
>  xen/Rules.mk| 138 
>  xen/arch/arm/Makefile   |   4 +-
>  xen/arch/arm/Rules.mk   |   4 --
>  xen/arch/arm/arch.mk|   4 +-
>  xen/arch/riscv/arch.mk  |   4 +-
>  xen/arch/x86/Makefile   |  11 +--
>  xen/arch/x86/Rules.mk   |   4 +-
>  xen/arch/x86/arch.mk|  12 ++--
>  xen/arch/x86/boot/Makefile  |   8 +--
>  xen/build.mk|  12 +++-
>  xen/include/Makefile|   6 +-
>  xen/scripts/Kbuild.include  |   6 ++
>  xen/test/Makefile   |   7 +-
>  xen/test/livepatch/Makefile |   6 --
>  xen/xsm/flask/Makefile  |   2 +-
>  xen/xsm/flask/ss/Makefile   |   2 +-
>  17 files changed, 162 insertions(+), 104 deletions(-)
> 



> diff --git a/xen/xsm/flask/Makefile b/xen/xsm/flask/Makefile
> index 51fd37f6c4d5..49cf730cf098 100644
> --- a/xen/xsm/flask/Makefile
> +++ b/xen/xsm/flask/Makefile
> @@ -40,7 +40,7 @@ $(obj)/flask-policy.S: $(BASEDIR)/tools/binfile FORCE
>   $(call if_changed,binfile,$(obj)/policy.bin xsm_flask_init_policy)
>  targets += flask-policy.S
>  
> -FLASK_BUILD_DIR := $(CURDIR)
> +FLASK_BUILD_DIR := $(abs_objtree)/$(obj)
>  POLICY_SRC := $(FLASK_BUILD_DIR)/xenpolicy-$(XEN_FULLVERSION)
>  
>  $(obj)/policy.bin: FORCE
> diff --git a/xen/xsm/flask/ss/Makefile b/xen/xsm/flask/ss/Makefile
> index d32b9e07138e..aba1339f3808 100644
> --- a/xen/xsm/flask/ss/Makefile
> +++ b/xen/xsm/flask/ss/Makefile
> @@ -8,4 +8,4 @@ obj-y += services.o
>  obj-y += conditional.o
>  obj-y += mls.o
>  
> -CFLAGS-y += -I../include
> +CFLAGS-y += -I$(srctree)/xsm/flask/include


Reviewed-by: Daniel P. Smith 

V/r,
Daniel P. Smith
Apertus Solutions, LLC



Re: [XEN PATCH v9 05/30] build: prepare to always invoke $(MAKE) from xen/, use $(obj)

2022-01-25 Thread Daniel P. Smith
On 1/25/22 06:00, Anthony PERARD wrote:
> In a future patch, when building a subdirectory, we will set
> "obj=$subdir" rather than change directory.
> 
> Before that, we add "$(obj)" and "$(src)" in as many places as
> possible where we will need to know which subdirectory is been built.
> "$(obj)" is for files been generated during the build, and "$(src)" is
> for files present in the source tree.
> 
> For now, we set both to "." in Rules.mk and Makefile.clean.
> 
> A few places don't tolerate the addition of "./", this is because make
> remove the leading "./" in targets and dependencies in rules, so these
> will be change later.
> 
> Signed-off-by: Anthony PERARD 
> Acked-by: Jan Beulich 
> Acked-by: Julien Grall 
> ---
> 
> Notes:
> v8:
> - acked
> - rebased
> 
>  xen/Rules.mk|  5 ++-
>  xen/arch/arm/Makefile   | 14 
>  xen/arch/x86/Makefile   | 48 -
>  xen/arch/x86/boot/Makefile  | 14 
>  xen/arch/x86/efi/Makefile   |  6 ++--
>  xen/common/Makefile |  8 ++---
>  xen/common/libelf/Makefile  |  4 +--
>  xen/common/libfdt/Makefile  |  6 ++--
>  xen/include/Makefile| 44 +++
>  xen/scripts/Makefile.clean  |  5 ++-
>  xen/xsm/flask/Makefile  | 36 +--
>  xen/xsm/flask/policy/mkaccess_vector.sh |  7 ++--
>  12 files changed, 103 insertions(+), 94 deletions(-)
> 



> diff --git a/xen/xsm/flask/Makefile b/xen/xsm/flask/Makefile
> index 11c530dcf458..51fd37f6c4d5 100644
> --- a/xen/xsm/flask/Makefile
> +++ b/xen/xsm/flask/Makefile
> @@ -4,46 +4,46 @@ obj-y += flask_op.o
>  
>  obj-y += ss/
>  
> -CFLAGS-y += -I./include
> +CFLAGS-y += -I$(obj)/include
>  
>  AWK = awk
>  
> -FLASK_H_DEPEND = policy/security_classes policy/initial_sids
> -AV_H_DEPEND = policy/access_vectors
> +FLASK_H_DEPEND := $(addprefix $(src)/policy/,security_classes initial_sids)
> +AV_H_DEPEND = $(src)/policy/access_vectors
>  
> -FLASK_H_FILES = include/flask.h include/class_to_string.h 
> include/initial_sid_to_string.h
> -AV_H_FILES = include/av_perm_to_string.h include/av_permissions.h
> -ALL_H_FILES = $(FLASK_H_FILES) $(AV_H_FILES)
> +FLASK_H_FILES := flask.h class_to_string.h initial_sid_to_string.h
> +AV_H_FILES := av_perm_to_string.h av_permissions.h
> +ALL_H_FILES := $(addprefix include/,$(FLASK_H_FILES) $(AV_H_FILES))
>  
> -$(obj-y) ss/built_in.o: $(ALL_H_FILES)
> +$(addprefix $(obj)/,$(obj-y)) $(obj)/ss/built_in.o: $(addprefix 
> $(obj)/,$(ALL_H_FILES))
>  extra-y += $(ALL_H_FILES)
>  
> -mkflask := policy/mkflask.sh
> +mkflask := $(src)/policy/mkflask.sh
>  quiet_cmd_mkflask = MKFLASK $@
> -cmd_mkflask = $(SHELL) $(mkflask) $(AWK) include $(FLASK_H_DEPEND)
> +cmd_mkflask = $(SHELL) $(mkflask) $(AWK) $(obj)/include $(FLASK_H_DEPEND)
>  
> -$(subst include/,%/,$(FLASK_H_FILES)): $(FLASK_H_DEPEND) $(mkflask) FORCE
> +$(addprefix $(obj)/%/,$(FLASK_H_FILES)): $(FLASK_H_DEPEND) $(mkflask) FORCE
>   $(call if_changed,mkflask)
>  
> -mkaccess := policy/mkaccess_vector.sh
> +mkaccess := $(src)/policy/mkaccess_vector.sh
>  quiet_cmd_mkaccess = MKACCESS VECTOR $@
> -cmd_mkaccess = $(SHELL) $(mkaccess) $(AWK) $(AV_H_DEPEND)
> +cmd_mkaccess = $(SHELL) $(mkaccess) $(AWK) $(obj)/include $(AV_H_DEPEND)
>  
> -$(subst include/,%/,$(AV_H_FILES)): $(AV_H_DEPEND) $(mkaccess) FORCE
> +$(addprefix $(obj)/%/,$(AV_H_FILES)): $(AV_H_DEPEND) $(mkaccess) FORCE
>   $(call if_changed,mkaccess)
>  
>  obj-bin-$(CONFIG_XSM_FLASK_POLICY) += flask-policy.o
> -flask-policy.o: policy.bin
> +$(obj)/flask-policy.o: $(obj)/policy.bin
>  
> -flask-policy.S: BINFILE_FLAGS := -i
> -flask-policy.S: $(BASEDIR)/tools/binfile FORCE
> - $(call if_changed,binfile,policy.bin xsm_flask_init_policy)
> +$(obj)/flask-policy.S: BINFILE_FLAGS := -i
> +$(obj)/flask-policy.S: $(BASEDIR)/tools/binfile FORCE
> + $(call if_changed,binfile,$(obj)/policy.bin xsm_flask_init_policy)
>  targets += flask-policy.S
>  
>  FLASK_BUILD_DIR := $(CURDIR)
>  POLICY_SRC := $(FLASK_BUILD_DIR)/xenpolicy-$(XEN_FULLVERSION)
>  
> -policy.bin: FORCE
> +$(obj)/policy.bin: FORCE
>   $(MAKE) -f $(XEN_ROOT)/tools/flask/policy/Makefile.common \
>   -C $(XEN_ROOT)/tools/flask/policy \
>   FLASK_BUILD_DIR=$(FLASK_BUILD_DIR) POLICY_FILENAME=$(POLICY_SRC)
> diff --git a/xen/xsm/flask/policy/mkaccess_vector.sh 
> b/xen/xsm/flask/policy/mkaccess_vector.sh
> index 942ede4713f1..ad9772193bff 100755
> --- a/xen/xsm/flask/policy/mkaccess_vector.sh
> +++ b/xen/xsm/flask/policy/mkaccess_vector.sh
> @@ -8,9 +8,12 @@ set -e
>  awk=$1
>  shift
>  
> +output_dir=$1
> +shift
> +
>  # output files
> -av_permissions="include/av_permissions.h"
> -av_perm_to_string="include/av_perm_to_string.h"
> +av_permissions="$output_dir/av_permissions.h"
> +av_perm_to_string="$output_dir/av_perm_to_string.h"
>  
>  cat $* | $awk "
>  BEGI

Re: [PATCH] x86emul: recognize CLDEMOTE

2022-01-25 Thread Durrant, Paul

On 25/01/2022 15:08, Jan Beulich wrote:

On 25.01.2022 15:22, Jan Beulich wrote:

We claim to support the insn, but so far the emulator has been handling
it as a NOP.

Signed-off-by: Jan Beulich 


I'm sorry, I should have Cc-ed Paul here as well.



Acked-by: Paul Durrant 


Jan


---
While handling x86emul_cldemote separately in hvmemul_cache_op() means
to carry some redundant code, folding it with CLFLUSH{,OPT} / CLWB
didn't seem very attractive either.

--- a/xen/arch/x86/arch.mk
+++ b/xen/arch/x86/arch.mk
@@ -23,6 +23,7 @@ $(call as-option-add,CFLAGS,CC,"xsaveopt
  $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
  $(call as-option-add,CFLAGS,CC,"clac",-DHAVE_AS_CLAC_STAC)
  $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
+$(call as-option-add,CFLAGS,CC,"cldemote (%rax)",-DHAVE_AS_CLDEMOTE)
  $(call as-option-add,CFLAGS,CC,".equ \"x\"$$(comma)1",-DHAVE_AS_QUOTED_SYM)
  $(call as-option-add,CFLAGS,CC,"invpcid 
(%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
  $(call as-option-add,CFLAGS,CC,"movdiri %rax$$(comma)(%rax)",-DHAVE_AS_MOVDIR)
--- a/xen/arch/x86/hvm/emulate.c
+++ b/xen/arch/x86/hvm/emulate.c
@@ -2351,6 +2351,28 @@ static int hvmemul_cache_op(
   * to be sensibly used is in (virtualization unaware) firmware.
   */
  break;
+
+case x86emul_cldemote:
+ASSERT(!is_x86_system_segment(seg));
+
+if ( !boot_cpu_has(X86_FEATURE_CLDEMOTE) ||
+ hvmemul_virtual_to_linear(seg, offset, 0, NULL, hvm_access_none,
+   hvmemul_ctxt, &addr) != X86EMUL_OKAY )
+break;
+
+if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
+pfec |= PFEC_user_mode;
+
+mapping = hvmemul_map_linear_addr(addr, 0, pfec, hvmemul_ctxt);
+if ( mapping == ERR_PTR(~X86EMUL_EXCEPTION) )
+x86_emul_reset_event(&hvmemul_ctxt->ctxt);
+if ( IS_ERR_OR_NULL(mapping) )
+break;
+
+cldemote(mapping);
+
+hvmemul_unmap_linear_addr(mapping, addr, 0, hvmemul_ctxt);
+break;
  }
  
  return X86EMUL_OKAY;

--- a/xen/arch/x86/include/asm/system.h
+++ b/xen/arch/x86/include/asm/system.h
@@ -37,6 +37,16 @@ static inline void clwb(const void *p)
  #endif
  }
  
+static inline void cldemote(const void *p)

+{
+#if defined(HAVE_AS_CLDEMOTE)
+asm volatile ( "cldemote %0" :: "m" (*(const char *)p) );
+#else
+asm volatile ( ".byte 0x0f, 0x1c, 0x02"
+   :: "d" (p), "m" (*(const char *)p) );
+#endif
+}
+
  #define xchg(ptr,v) \
  ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr
  
--- a/xen/arch/x86/x86_emulate/x86_emulate.c

+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -6290,7 +6290,8 @@ x86_emulate(
  
  case X86EMUL_OPC(0x0f, 0x0d): /* GrpP (prefetch) */

  case X86EMUL_OPC(0x0f, 0x18): /* Grp16 (prefetch/nop) */
-case X86EMUL_OPC(0x0f, 0x19) ... X86EMUL_OPC(0x0f, 0x1f): /* nop */
+case X86EMUL_OPC(0x0f, 0x19) ... X86EMUL_OPC(0x0f, 0x1b): /* nop */
+case X86EMUL_OPC(0x0f, 0x1d) ... X86EMUL_OPC(0x0f, 0x1f): /* nop */
  break;
  
  #ifndef X86EMUL_NO_MMX

@@ -6627,6 +6628,12 @@ x86_emulate(
  
  #endif /* !X86EMUL_NO_SIMD */
  
+case X86EMUL_OPC(0x0f, 0x1c): /* cldemote / nop */

+if ( ctxt->cpuid->feat.cldemote && !vex.pfx && !modrm_reg &&
+ ops->cache_op )
+ops->cache_op(x86emul_cldemote, ea.mem.seg, ea.mem.off, ctxt);
+break;
+
  case X86EMUL_OPC(0x0f, 0x20): /* mov cr,reg */
  case X86EMUL_OPC(0x0f, 0x21): /* mov dr,reg */
  case X86EMUL_OPC(0x0f, 0x22): /* mov reg,cr */
--- a/xen/arch/x86/x86_emulate/x86_emulate.h
+++ b/xen/arch/x86/x86_emulate/x86_emulate.h
@@ -177,6 +177,7 @@ enum x86_emulate_fpu_type {
  };
  
  enum x86emul_cache_op {

+x86emul_cldemote,
  x86emul_clflush,
  x86emul_clflushopt,
  x86emul_clwb,









Re: [PATCH] libxl: force netback to wait for hotplug execution before connecting

2022-01-25 Thread Julien Grall

Hi,

On 24/01/2022 16:02, Roger Pau Monne wrote:

By writing an empty "hotplug-status" xenstore node in the backend path
libxl can force Linux netback to wait for hotplug script execution
before proceeding to the 'connected' state.


I was actually chasing the same issue today :).



This is required so that netback doesn't skip state 2 (InitWait) and


Technically netback never skip state 2 (otherwise it would always be 
reproducible). Instead, libxl may not be able to observe state 2 because 
receive a watch is asynchronous and doesn't contain the value of the 
node. So the backend may have moved to Connected before the state is read.



thus blocks libxl waiting for such state in order to launch the
hotplug script (see libxl__wait_device_connection).

Reported-by: James Dingwall 
Signed-off-by: Roger Pau Monné 
Tested-by: James Dingwall 


I could easily reproduced by adding a sleep(1) before reading the key 
and use 'xl network-attach ...'.


Tested-by: Julien Grall 


---
Cc: Wei Liu 
Cc: Paul Durrant 
---
  tools/libs/light/libxl_nic.c | 7 +++
  1 file changed, 7 insertions(+)

diff --git a/tools/libs/light/libxl_nic.c b/tools/libs/light/libxl_nic.c
index 0b45469dca..0b9e70c9d1 100644
--- a/tools/libs/light/libxl_nic.c
+++ b/tools/libs/light/libxl_nic.c
@@ -248,6 +248,13 @@ static int libxl__set_xenstore_nic(libxl__gc *gc, uint32_t 
domid,
  flexarray_append(ro_front, "mtu");
  flexarray_append(ro_front, GCSPRINTF("%u", nic->mtu));
  
+/*

+ * Force backend to wait for hotplug script execution before switching to
+ * connected state.
+ */
+flexarray_append(back, "hotplug-status");
+flexarray_append(back, "");
+
  return 0;
  }
  


Cheers,

--
Julien Grall



Re: [PATCH] x86emul: recognize CLDEMOTE

2022-01-25 Thread Jan Beulich
On 25.01.2022 16:09, Andrew Cooper wrote:
> On 25/01/2022 14:22, Jan Beulich wrote:
>> We claim to support the insn, but so far the emulator has been handling
>> it as a NOP.
>>
>> Signed-off-by: Jan Beulich 
> 
> Treating CLDEMOTE as a NOP is going to be more efficient than actually
> setting up the mapping to execute a real CLDEMOTE instruction on the
> line in question.  CLDEMOTE is very specifically an optimisation for
> software producer/consumer pairs.

Some similar argument could likely be made for treating CLFLUSH etc as
just a NOP then?

> If we want to take this patch, it should Fix[es]: ad3abc47dd23c which
> made the claim that CLDEMOTE needed no further additions.

Added.

> The only issue on whether we can treat it as a NOP completely is whether
> we believe the exception list.  I'm not sure I believe the absence of
> AGU faults,

I also was puzzled by this, but I have no way to verify one way or the
other. Hence the implementation follows what the SDM says.

> but the instruction is taken from hint-nop space so
> guaranteed to behave similarly to clflush/clwb.

I'm confused: CLFLUSH / CLWB specifically do not live in NOP space:
The former are under 0FAE, while NOP space is 0F18 ... 0F1F (with
CLDEMOTE being 0F1C).

Jan




Re: [PATCH 1/3] xen/vioapic: add support for the extended destination ID field

2022-01-25 Thread Roger Pau Monné
On Mon, Jan 24, 2022 at 02:20:47PM +0100, Jan Beulich wrote:
> On 20.01.2022 16:23, Roger Pau Monne wrote:
> > Such field uses bits 55:48, but for the purposes the register will be
> > used use bits 55:49 instead. Bit 48 is used to signal an RTE entry is
> > in remappable format which is not supported by the vIO-APIC.
> 
> Neither here nor in the cover letter you point at a formal specification
> of this mode of operation.

I'm not aware of any formal specification of this mode, apart from the
work done to introduce support in Linux and QEMU:

https://lore.kernel.org/all/20201009104616.1314746-1-dw...@infradead.org/
https://git.qemu.org/?p=qemu.git;a=commitdiff;h=c1bb5418e

Adding David in case there's some kind of specification somewhere I'm
not aware of.

> What I'm aware of are vague indications of
> this mode's existence in some of Intel's chipset data sheets. Yet that
> leaves open, for example, whether indeed bit 48 cannot be used here.

Bit 48 cannot be used because it's already used to signal an RTE is in
remappable format. We still want to differentiate an RTE entry in
remappable format, as it should be possible to expose both the
extended ID support and an emulated IOMMU.

> > --- a/xen/arch/x86/hvm/vioapic.c
> > +++ b/xen/arch/x86/hvm/vioapic.c
> > @@ -412,7 +412,8 @@ static void ioapic_inj_irq(
> >  
> >  static void vioapic_deliver(struct hvm_vioapic *vioapic, unsigned int pin)
> >  {
> > -uint16_t dest = vioapic->redirtbl[pin].fields.dest_id;
> > +uint16_t dest = vioapic->redirtbl[pin].fields.dest_id |
> > +(vioapic->redirtbl[pin].fields.ext_dest_id << 8);
> 
> What if an existing guest has been writing non-zero in these bits? Can
> you really use them here without any further indication by the guest?

Those bits where reserved previously, so no OS should have used them.
There are hypervisors already in the field (QEMU/KVM and HyperV) using
this mode.

We could add a per-domain option to disable extended ID mode if we are
really worried about OSes having used those bits for some reason.

Thanks, Roger.



[xen-unstable-smoke test] 167811: tolerable all pass - PUSHED

2022-01-25 Thread osstest service owner
flight 167811 xen-unstable-smoke real [real]
http://logs.test-lab.xenproject.org/osstest/logs/167811/

Failures :-/ but no regressions.

Tests which did not succeed, but are not blocking:
 test-amd64-amd64-libvirt 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl  16 saverestore-support-checkfail   never pass

version targeted for testing:
 xen  329b7bed80032fd52904af6a0cac7dd3716d27cf
baseline version:
 xen  ec32910f4f871dce0f0e32dfb36f218fa1a2e869

Last test of basis   167805  2022-01-24 08:01:41 Z1 days
Testing same since   167811  2022-01-25 11:02:54 Z0 days1 attempts


People who touched revisions under test:
  Roger Pau Monne 
  Roger Pau Monné 

jobs:
 build-arm64-xsm  pass
 build-amd64  pass
 build-armhf  pass
 build-amd64-libvirt  pass
 test-armhf-armhf-xl  pass
 test-arm64-arm64-xl-xsm  pass
 test-amd64-amd64-xl-qemuu-debianhvm-amd64pass
 test-amd64-amd64-libvirt pass



sg-report-flight on osstest.test-lab.xenproject.org
logs: /home/logs/logs
images: /home/logs/images

Logs, config files, etc. are available at
http://logs.test-lab.xenproject.org/osstest/logs

Explanation of these reports, and of osstest in general, is at
http://xenbits.xen.org/gitweb/?p=osstest.git;a=blob;f=README.email;hb=master
http://xenbits.xen.org/gitweb/?p=osstest.git;a=blob;f=README;hb=master

Test harness code can be found at
http://xenbits.xen.org/gitweb?p=osstest.git;a=summary


Pushing revision :

To xenbits.xen.org:/home/xen/git/xen.git
   ec32910f4f..329b7bed80  329b7bed80032fd52904af6a0cac7dd3716d27cf -> smoke



Re: [PATCH] x86emul: recognize CLDEMOTE

2022-01-25 Thread Andrew Cooper
On 25/01/2022 14:22, Jan Beulich wrote:
> We claim to support the insn, but so far the emulator has been handling
> it as a NOP.
>
> Signed-off-by: Jan Beulich 

Treating CLDEMOTE as a NOP is going to be more efficient than actually
setting up the mapping to execute a real CLDEMOTE instruction on the
line in question.  CLDEMOTE is very specifically an optimisation for
software producer/consumer pairs.

If we want to take this patch, it should Fix[es]: ad3abc47dd23c which
made the claim that CLDEMOTE needed no further additions.

The only issue on whether we can treat it as a NOP completely is whether
we believe the exception list.  I'm not sure I believe the absence of
AGU faults, but the instruction is taken from hint-nop space so
guaranteed to behave similarly to clflush/clwb.

~Andrew


Re: [PATCH] x86emul: recognize CLDEMOTE

2022-01-25 Thread Jan Beulich
On 25.01.2022 15:22, Jan Beulich wrote:
> We claim to support the insn, but so far the emulator has been handling
> it as a NOP.
> 
> Signed-off-by: Jan Beulich 

I'm sorry, I should have Cc-ed Paul here as well.

Jan

> ---
> While handling x86emul_cldemote separately in hvmemul_cache_op() means
> to carry some redundant code, folding it with CLFLUSH{,OPT} / CLWB
> didn't seem very attractive either.
> 
> --- a/xen/arch/x86/arch.mk
> +++ b/xen/arch/x86/arch.mk
> @@ -23,6 +23,7 @@ $(call as-option-add,CFLAGS,CC,"xsaveopt
>  $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
>  $(call as-option-add,CFLAGS,CC,"clac",-DHAVE_AS_CLAC_STAC)
>  $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
> +$(call as-option-add,CFLAGS,CC,"cldemote (%rax)",-DHAVE_AS_CLDEMOTE)
>  $(call as-option-add,CFLAGS,CC,".equ \"x\"$$(comma)1",-DHAVE_AS_QUOTED_SYM)
>  $(call as-option-add,CFLAGS,CC,"invpcid 
> (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
>  $(call as-option-add,CFLAGS,CC,"movdiri 
> %rax$$(comma)(%rax)",-DHAVE_AS_MOVDIR)
> --- a/xen/arch/x86/hvm/emulate.c
> +++ b/xen/arch/x86/hvm/emulate.c
> @@ -2351,6 +2351,28 @@ static int hvmemul_cache_op(
>   * to be sensibly used is in (virtualization unaware) firmware.
>   */
>  break;
> +
> +case x86emul_cldemote:
> +ASSERT(!is_x86_system_segment(seg));
> +
> +if ( !boot_cpu_has(X86_FEATURE_CLDEMOTE) ||
> + hvmemul_virtual_to_linear(seg, offset, 0, NULL, hvm_access_none,
> +   hvmemul_ctxt, &addr) != X86EMUL_OKAY )
> +break;
> +
> +if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
> +pfec |= PFEC_user_mode;
> +
> +mapping = hvmemul_map_linear_addr(addr, 0, pfec, hvmemul_ctxt);
> +if ( mapping == ERR_PTR(~X86EMUL_EXCEPTION) )
> +x86_emul_reset_event(&hvmemul_ctxt->ctxt);
> +if ( IS_ERR_OR_NULL(mapping) )
> +break;
> +
> +cldemote(mapping);
> +
> +hvmemul_unmap_linear_addr(mapping, addr, 0, hvmemul_ctxt);
> +break;
>  }
>  
>  return X86EMUL_OKAY;
> --- a/xen/arch/x86/include/asm/system.h
> +++ b/xen/arch/x86/include/asm/system.h
> @@ -37,6 +37,16 @@ static inline void clwb(const void *p)
>  #endif
>  }
>  
> +static inline void cldemote(const void *p)
> +{
> +#if defined(HAVE_AS_CLDEMOTE)
> +asm volatile ( "cldemote %0" :: "m" (*(const char *)p) );
> +#else
> +asm volatile ( ".byte 0x0f, 0x1c, 0x02"
> +   :: "d" (p), "m" (*(const char *)p) );
> +#endif
> +}
> +
>  #define xchg(ptr,v) \
>  ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr
>  
> --- a/xen/arch/x86/x86_emulate/x86_emulate.c
> +++ b/xen/arch/x86/x86_emulate/x86_emulate.c
> @@ -6290,7 +6290,8 @@ x86_emulate(
>  
>  case X86EMUL_OPC(0x0f, 0x0d): /* GrpP (prefetch) */
>  case X86EMUL_OPC(0x0f, 0x18): /* Grp16 (prefetch/nop) */
> -case X86EMUL_OPC(0x0f, 0x19) ... X86EMUL_OPC(0x0f, 0x1f): /* nop */
> +case X86EMUL_OPC(0x0f, 0x19) ... X86EMUL_OPC(0x0f, 0x1b): /* nop */
> +case X86EMUL_OPC(0x0f, 0x1d) ... X86EMUL_OPC(0x0f, 0x1f): /* nop */
>  break;
>  
>  #ifndef X86EMUL_NO_MMX
> @@ -6627,6 +6628,12 @@ x86_emulate(
>  
>  #endif /* !X86EMUL_NO_SIMD */
>  
> +case X86EMUL_OPC(0x0f, 0x1c): /* cldemote / nop */
> +if ( ctxt->cpuid->feat.cldemote && !vex.pfx && !modrm_reg &&
> + ops->cache_op )
> +ops->cache_op(x86emul_cldemote, ea.mem.seg, ea.mem.off, ctxt);
> +break;
> +
>  case X86EMUL_OPC(0x0f, 0x20): /* mov cr,reg */
>  case X86EMUL_OPC(0x0f, 0x21): /* mov dr,reg */
>  case X86EMUL_OPC(0x0f, 0x22): /* mov reg,cr */
> --- a/xen/arch/x86/x86_emulate/x86_emulate.h
> +++ b/xen/arch/x86/x86_emulate/x86_emulate.h
> @@ -177,6 +177,7 @@ enum x86_emulate_fpu_type {
>  };
>  
>  enum x86emul_cache_op {
> +x86emul_cldemote,
>  x86emul_clflush,
>  x86emul_clflushopt,
>  x86emul_clwb,
> 
> 




Re: [PATCH] x86emul: recognize CLDEMOTE

2022-01-25 Thread Jan Beulich
On 25.01.2022 15:35, Roger Pau Monné wrote:
> On Tue, Jan 25, 2022 at 03:22:25PM +0100, Jan Beulich wrote:
>> We claim to support the insn, but so far the emulator has been handling
>> it as a NOP.
> 
> While not ideal, the SDM mentions that "The CLDEMOTE instruction may
> be ignored by hardware in certain cases and is not a guarantee.".

Right; the same is effectively the case for CLFLUSH etc. Still, unlike
prefetches, we implement them in the emulator.

>> --- a/xen/arch/x86/hvm/emulate.c
>> +++ b/xen/arch/x86/hvm/emulate.c
>> @@ -2351,6 +2351,28 @@ static int hvmemul_cache_op(
>>   * to be sensibly used is in (virtualization unaware) firmware.
>>   */
>>  break;
>> +
>> +case x86emul_cldemote:
>> +ASSERT(!is_x86_system_segment(seg));
>> +
>> +if ( !boot_cpu_has(X86_FEATURE_CLDEMOTE) ||
>> + hvmemul_virtual_to_linear(seg, offset, 0, NULL, 
>> hvm_access_none,
>> +   hvmemul_ctxt, &addr) != X86EMUL_OKAY 
>> )
>> +break;
>> +
>> +if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
>> +pfec |= PFEC_user_mode;
>> +
>> +mapping = hvmemul_map_linear_addr(addr, 0, pfec, hvmemul_ctxt);
> 
> I think the emulator should map the address using the same cache
> attributes as the guest, or else the result might be different than
> intended?

That's a pre-existing problem everywhere, not something specific to
this one insn.

Jan




Re: [PATCH v7 03/11] libx86: introduce helper to fetch msr entry

2022-01-25 Thread Jan Beulich
On 25.01.2022 15:49, Roger Pau Monne wrote:
> Use such helper in order to replace the code in
> x86_msr_copy_from_buffer. Note the introduced helper should not be
> directly called and instead x86_msr_get_entry should be used that will
> properly deal with const and non-const inputs.
> 
> Note this requires making the raw fields uint64_t so that it can
> accommodate the maximum size of MSRs values, and in turn removing the
> truncation tests.
> 
> Suggested-by: Andrew Cooper 
> Signed-off-by: Roger Pau Monné 

Reviewed-by: Jan Beulich 




Re: [PATCH v7 01/11] libx86: introduce helper to fetch cpuid leaf

2022-01-25 Thread Jan Beulich
On 25.01.2022 15:49, Roger Pau Monne wrote:
> Introduce a helper based on the current Xen guest_cpuid code in order
> to fetch a cpuid leaf from a policy. The newly introduced function in
> cpuid.c should not be directly called and instead the provided
> x86_cpuid_get_leaf macro should be used that will properly deal with
> const and non-const inputs.
> 
> Also add a test to check that the introduced helper doesn't go over
> the bounds of the policy.
> 
> Note the code in x86_cpuid_copy_from_buffer is not switched to use the
> new function because of the boundary checks against the max fields of
> the policy, which might not be properly set at the point where
> x86_cpuid_copy_from_buffer get called, for example when filling an
> empty policy from scratch.
> 
> Suggested-by: Andrew Cooper 
> Signed-off-by: Roger Pau Monné 
> ---
> Changes since v6:
>  - Add more tests.
>  - Drop Jan R-b.

Please feel free to reinstate.

Jan




[PATCH v7 11/11] x86/CPUID: shrink max_{,sub}leaf fields according to actual leaf contents

2022-01-25 Thread Roger Pau Monne
From: Jan Beulich 

Zapping leaf data for out of range leaves is just one half of it: To
avoid guests (bogusly or worse) inferring information from mere leaf
presence, also shrink maximum indicators such that the respective
trailing entry is not all blank (unless of course it's the initial
subleaf of a leaf that's not the final one).

This is also in preparation of bumping the maximum basic leaf we
support, to ensure guests not getting exposed related features won't
observe a change in behavior.

Note that such shrinking is only done when creating a policy for a
domain from scratch. Migrated in domains keep their previous policy if
present untouched, and for migrated in domains not having CPUID data
the crafted Xen pre-4.13 policy is not trimmed to keep a behavior
compatible with those older Xen versions.

Signed-off-by: Jan Beulich 
Signed-off-by: Roger Pau Monné 
---
Changes since v4:
 - New in this version, picked up from 540d911c2813.
 - Only shrink policies for newly created domains.
---
 tools/include/xenguest.h |   3 +
 tools/libs/guest/xg_cpuid_x86.c  |   5 ++
 tools/libs/light/libxl_cpuid.c   |   7 ++
 tools/tests/cpu-policy/test-cpu-policy.c | 101 +++
 xen/include/xen/lib/x86/cpuid.h  |   7 ++
 xen/lib/x86/cpuid.c  |  39 +
 6 files changed, 162 insertions(+)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index ffe74c7a51..4b1ced4c49 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -835,6 +835,9 @@ int xc_cpu_policy_apply_featureset(xc_interface *xch, 
xc_cpu_policy_t *policy,
const uint32_t *featureset,
unsigned int nr_features);
 
+/* Sanitize a policy: can change the contents of the passed policy. */
+void xc_cpu_policy_sanitize(xc_interface *xch, xc_cpu_policy_t *policy);
+
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
   uint32_t *nr_features, uint32_t *featureset);
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index 032459f3de..1b83533098 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -651,3 +651,8 @@ int xc_cpu_policy_apply_featureset(xc_interface *xch, 
xc_cpu_policy_t *policy,
 
 return 0;
 }
+
+void xc_cpu_policy_sanitize(xc_interface *xch, xc_cpu_policy_t *policy)
+{
+x86_cpuid_policy_shrink_max_leaves(&policy->cpuid);
+}
diff --git a/tools/libs/light/libxl_cpuid.c b/tools/libs/light/libxl_cpuid.c
index 5d28476eb9..61b85c6e0a 100644
--- a/tools/libs/light/libxl_cpuid.c
+++ b/tools/libs/light/libxl_cpuid.c
@@ -660,6 +660,13 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, 
bool restore,
 goto out;
 }
 
+/*
+ * Do not attempt any modifications if creating a policy that aims to be
+ * compatible with pre-4.13 Xen versions.
+ */
+if (!restore)
+xc_cpu_policy_sanitize(ctx->xch, policy);
+
 r = xc_cpu_policy_set_domain(ctx->xch, domid, policy);
 if (r) {
 LOGED(ERROR, domid, "Failed to set domain CPUID policy");
diff --git a/tools/tests/cpu-policy/test-cpu-policy.c 
b/tools/tests/cpu-policy/test-cpu-policy.c
index 8f51447117..d883f750f5 100644
--- a/tools/tests/cpu-policy/test-cpu-policy.c
+++ b/tools/tests/cpu-policy/test-cpu-policy.c
@@ -9,10 +9,13 @@
 #include 
 
 #include 
+#include 
 #include 
 #include 
 #include 
 
+#define XSTATE_FP_SSE  (X86_XCR0_FP | X86_XCR0_SSE)
+
 static unsigned int nr_failures;
 #define fail(fmt, ...)  \
 ({  \
@@ -772,6 +775,103 @@ static void test_msr_get_entry(void)
 }
 }
 
+static void test_cpuid_maximum_leaf_shrinking(void)
+{
+static const struct test {
+const char *name;
+struct cpuid_policy p;
+} tests[] = {
+{
+.name = "basic",
+.p = {
+/* Very basic information only. */
+.basic.max_leaf = 1,
+.basic.raw_fms = 0xc2,
+},
+},
+{
+.name = "cache",
+.p = {
+/* Cache subleaves present. */
+.basic.max_leaf = 4,
+.cache.subleaf[0].type = 1,
+},
+},
+{
+.name = "feat#0",
+.p = {
+/* Subleaf 0 only with some valid bit. */
+.basic.max_leaf = 7,
+.feat.max_subleaf = 0,
+.feat.fsgsbase = 1,
+},
+},
+{
+.name = "feat#1",
+.p = {
+/* Subleaf 1 only with some valid bit. */
+.basic.max_leaf = 7,
+.feat.max_subleaf = 1,
+.feat.avx_vnni = 1,
+},
+},
+{
+.name = "topo",
+.p =

[PATCH v7 10/11] libs/guest: (re)move xc_cpu_policy_apply_cpuid

2022-01-25 Thread Roger Pau Monne
Move the logic from xc_cpu_policy_apply_cpuid into libxl, now that the
xc_cpu_policy_* helpers allow modifying a cpu policy. By moving such
parsing into libxl directly we can get rid of xc_xend_cpuid, as libxl
will now implement it's own private type for storing CPUID
information, which currently matches xc_xend_cpuid.

Note the function logic is moved as-is, but requires adapting to the
libxl coding style.

No functional change intended.

Signed-off-by: Roger Pau Monné 
Reviewed-by: Anthony PERARD 
---
Changes since v2:
 - Use LOG*D.
 - Pass a gc to apply_policy.
 - Use 'r' for libxc return values.
---
 tools/include/libxl.h |   6 +-
 tools/include/xenctrl.h   |  26 --
 tools/include/xenguest.h  |   5 --
 tools/libs/guest/xg_cpuid_x86.c   | 116 --
 tools/libs/light/libxl_cpuid.c| 133 --
 tools/libs/light/libxl_internal.h |  26 ++
 6 files changed, 156 insertions(+), 156 deletions(-)

diff --git a/tools/include/libxl.h b/tools/include/libxl.h
index 2bbbd21f0b..8a8032ba25 100644
--- a/tools/include/libxl.h
+++ b/tools/include/libxl.h
@@ -1420,10 +1420,10 @@ void libxl_bitmap_init(libxl_bitmap *map);
 void libxl_bitmap_dispose(libxl_bitmap *map);
 
 /*
- * libxl_cpuid_policy is opaque in the libxl ABI.  Users of both libxl and
- * libxc may not make assumptions about xc_xend_cpuid.
+ * libxl_cpuid_policy is opaque in the libxl ABI. Users of libxl may not make
+ * assumptions about libxl__cpuid_policy.
  */
-typedef struct xc_xend_cpuid libxl_cpuid_policy;
+typedef struct libxl__cpuid_policy libxl_cpuid_policy;
 typedef libxl_cpuid_policy * libxl_cpuid_policy_list;
 void libxl_cpuid_dispose(libxl_cpuid_policy_list *cpuid_list);
 int libxl_cpuid_policy_list_length(const libxl_cpuid_policy_list *l);
diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h
index 745d67c970..79169f8ace 100644
--- a/tools/include/xenctrl.h
+++ b/tools/include/xenctrl.h
@@ -1803,32 +1803,6 @@ int xc_domain_debug_control(xc_interface *xch,
 
 #if defined(__i386__) || defined(__x86_64__)
 
-/*
- * CPUID policy data, expressed in the legacy XEND format.
- *
- * Policy is an array of strings, 32 chars long:
- *   policy[0] = eax
- *   policy[1] = ebx
- *   policy[2] = ecx
- *   policy[3] = edx
- *
- * The format of the string is the following:
- *   '1' -> force to 1
- *   '0' -> force to 0
- *   'x' -> we don't care (use default)
- *   'k' -> pass through host value
- *   's' -> legacy alias for 'k'
- */
-struct xc_xend_cpuid {
-union {
-struct {
-uint32_t leaf, subleaf;
-};
-uint32_t input[2];
-};
-char *policy[4];
-};
-
 int xc_mca_op(xc_interface *xch, struct xen_mc *mc);
 int xc_mca_op_inject_v2(xc_interface *xch, unsigned int flags,
 xc_cpumap_t cpumap, unsigned int nr_cpus);
diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index d3dc8719c6..ffe74c7a51 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -830,11 +830,6 @@ void xc_cpu_policy_make_compat_4_12(xc_interface *xch, 
xc_cpu_policy_t *policy,
 void xc_cpu_policy_legacy_topology(xc_interface *xch, xc_cpu_policy_t *policy,
const xc_cpu_policy_t *host);
 
-/* Apply an xc_xend_cpuid object to the policy. */
-int xc_cpu_policy_apply_cpuid(xc_interface *xch, xc_cpu_policy_t *policy,
-  const xc_cpu_policy_t *host,
-  const struct xc_xend_cpuid *cpuid, bool hvm);
-
 /* Apply a featureset to the policy. */
 int xc_cpu_policy_apply_featureset(xc_interface *xch, xc_cpu_policy_t *policy,
const uint32_t *featureset,
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index 28993f0356..032459f3de 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -254,122 +254,6 @@ int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t 
domid,
 return ret;
 }
 
-int xc_cpu_policy_apply_cpuid(xc_interface *xch, xc_cpu_policy_t *policy,
-  const xc_cpu_policy_t *host,
-  const struct xc_xend_cpuid *cpuid, bool hvm)
-{
-int rc;
-xc_cpu_policy_t *def = NULL;
-
-def = xc_cpu_policy_init();
-if ( !def )
-{
-PERROR("Failed to init policy");
-rc = -ENOMEM;
-goto out;
-}
-
-/* Get the domain type's default policy. */
-rc = xc_cpu_policy_get_system(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default
-   : XEN_SYSCTL_cpu_policy_pv_default,
-  def);
-if ( rc )
-{
-PERROR("Failed to obtain %s def policy", hvm ? "hvm" : "pv");
-goto out;
-}
-
-rc = -EINVAL;
-for ( ; cpuid->leaf != XEN_CPUID_INPUT_UNUSED; ++cpuid )
-{
-xen_cpuid_leaf_t cur_leaf;
-xen_cpuid_leaf_t def_leaf;
-xen_cpuid_l

[PATCH v7 08/11] libs/guest: apply a featureset into a cpu policy

2022-01-25 Thread Roger Pau Monne
Pull out the code from xc_cpuid_apply_policy that applies a featureset
to a cpu policy and place it on it's own standalone function that's
part of the public interface.

No functional change intended.

Signed-off-by: Roger Pau Monné 
Reviewed-by: Jan Beulich 
---
 tools/include/xenguest.h|  5 ++
 tools/libs/guest/xg_cpuid_x86.c | 95 -
 2 files changed, 62 insertions(+), 38 deletions(-)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index 98a998f11d..d3dc8719c6 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -835,6 +835,11 @@ int xc_cpu_policy_apply_cpuid(xc_interface *xch, 
xc_cpu_policy_t *policy,
   const xc_cpu_policy_t *host,
   const struct xc_xend_cpuid *cpuid, bool hvm);
 
+/* Apply a featureset to the policy. */
+int xc_cpu_policy_apply_featureset(xc_interface *xch, xc_cpu_policy_t *policy,
+   const uint32_t *featureset,
+   unsigned int nr_features);
+
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
   uint32_t *nr_features, uint32_t *featureset);
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index 3503f21793..a2e2f7c850 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -445,46 +445,15 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 
 if ( featureset )
 {
-uint32_t disabled_features[FEATURESET_NR_ENTRIES],
-feat[FEATURESET_NR_ENTRIES] = {};
-static const uint32_t deep_features[] = INIT_DEEP_FEATURES;
-unsigned int i, b;
-
-/*
- * The user supplied featureset may be shorter or longer than
- * FEATURESET_NR_ENTRIES.  Shorter is fine, and we will zero-extend.
- * Longer is fine, so long as it only padded with zeros.
- */
-unsigned int user_len = min(FEATURESET_NR_ENTRIES + 0u, nr_features);
-
-/* Check for truncated set bits. */
-rc = -EOPNOTSUPP;
-for ( i = user_len; i < nr_features; ++i )
-if ( featureset[i] != 0 )
-goto out;
-
-memcpy(feat, featureset, sizeof(*featureset) * user_len);
-
-/* Disable deep dependencies of disabled features. */
-for ( i = 0; i < ARRAY_SIZE(disabled_features); ++i )
-disabled_features[i] = ~feat[i] & deep_features[i];
-
-for ( b = 0; b < sizeof(disabled_features) * CHAR_BIT; ++b )
+policy.cpuid = *p;
+rc = xc_cpu_policy_apply_featureset(xch, &policy, featureset,
+nr_features);
+if ( rc )
 {
-const uint32_t *dfs;
-
-if ( !test_bit(b, disabled_features) ||
- !(dfs = x86_cpuid_lookup_deep_deps(b)) )
-continue;
-
-for ( i = 0; i < ARRAY_SIZE(disabled_features); ++i )
-{
-feat[i] &= ~dfs[i];
-disabled_features[i] &= ~dfs[i];
-}
+ERROR("Failed to apply featureset to policy");
+goto out;
 }
-
-cpuid_featureset_to_policy(feat, p);
+*p = policy.cpuid;
 }
 else
 {
@@ -881,3 +850,53 @@ void xc_cpu_policy_legacy_topology(xc_interface *xch, 
xc_cpu_policy_t *policy,
 }
 }
 }
+
+int xc_cpu_policy_apply_featureset(xc_interface *xch, xc_cpu_policy_t *policy,
+   const uint32_t *featureset,
+   unsigned int nr_features)
+{
+uint32_t disabled_features[FEATURESET_NR_ENTRIES],
+feat[FEATURESET_NR_ENTRIES] = {};
+static const uint32_t deep_features[] = INIT_DEEP_FEATURES;
+unsigned int i, b;
+
+/*
+ * The user supplied featureset may be shorter or longer than
+ * FEATURESET_NR_ENTRIES.  Shorter is fine, and we will zero-extend.
+ * Longer is fine, so long as it only padded with zeros.
+ */
+unsigned int user_len = min(FEATURESET_NR_ENTRIES + 0u, nr_features);
+
+/* Check for truncated set bits. */
+for ( i = user_len; i < nr_features; ++i )
+if ( featureset[i] != 0 )
+{
+errno = EOPNOTSUPP;
+return -1;
+}
+
+memcpy(feat, featureset, sizeof(*featureset) * user_len);
+
+/* Disable deep dependencies of disabled features. */
+for ( i = 0; i < ARRAY_SIZE(disabled_features); ++i )
+disabled_features[i] = ~feat[i] & deep_features[i];
+
+for ( b = 0; b < sizeof(disabled_features) * CHAR_BIT; ++b )
+{
+const uint32_t *dfs;
+
+if ( !test_bit(b, disabled_features) ||
+ !(dfs = x86_cpuid_lookup_deep_deps(b)) )
+continue;
+
+for ( i = 0; i < ARRAY_SIZE(disabled_features); ++i )
+{
+feat[i] &= ~

[PATCH v7 09/11] libs/{light,guest}: implement xc_cpuid_apply_policy in libxl

2022-01-25 Thread Roger Pau Monne
With the addition of the xc_cpu_policy_* now libxl can have better
control over the cpu policy, this allows removing the
xc_cpuid_apply_policy function and instead coding the required bits by
libxl in libxl__cpuid_legacy directly.

Remove xc_cpuid_apply_policy.

Signed-off-by: Roger Pau Monné 
Reviewed-by: Anthony PERARD 
---
Changes since v6:
 - Use a single call to libxl_cpuid_parse_config in order to add the
   extra options.

Changes since v4:
 - Correctly account for PVH guests being HVM in libxl__cpuid_legacy.
 - PAE option is only available to HVM guests (_not_ including PVH).

Changes since v2:
 - Use 'r' for libxc return values.
 - Fix comment about making a cpu policy compatible.
 - Use LOG*D macros.
---
 tools/include/xenctrl.h |  18 -
 tools/libs/guest/xg_cpuid_x86.c | 133 
 tools/libs/light/libxl_cpuid.c  |  73 --
 3 files changed, 67 insertions(+), 157 deletions(-)

diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h
index 95bd5eca67..745d67c970 100644
--- a/tools/include/xenctrl.h
+++ b/tools/include/xenctrl.h
@@ -1829,24 +1829,6 @@ struct xc_xend_cpuid {
 char *policy[4];
 };
 
-/*
- * Make adjustments to the CPUID settings for a domain.
- *
- * This path is used in two cases.  First, for fresh boots of the domain, and
- * secondly for migrate-in/restore of pre-4.14 guests (where CPUID data was
- * missing from the stream).  The @restore parameter distinguishes these
- * cases, and the generated policy must be compatible with a 4.13.
- *
- * Either pass a full new @featureset (and @nr_features), or adjust individual
- * features (@pae, @itsc, @nested_virt).
- *
- * Then (optionally) apply legacy XEND overrides (@xend) to the result.
- */
-int xc_cpuid_apply_policy(xc_interface *xch,
-  uint32_t domid, bool restore,
-  const uint32_t *featureset,
-  unsigned int nr_features, bool pae, bool itsc,
-  bool nested_virt, const struct xc_xend_cpuid *xend);
 int xc_mca_op(xc_interface *xch, struct xen_mc *mc);
 int xc_mca_op_inject_v2(xc_interface *xch, unsigned int flags,
 xc_cpumap_t cpumap, unsigned int nr_cpus);
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index a2e2f7c850..28993f0356 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -370,139 +370,6 @@ int xc_cpu_policy_apply_cpuid(xc_interface *xch, 
xc_cpu_policy_t *policy,
 return rc;
 }
 
-int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore,
-  const uint32_t *featureset, unsigned int nr_features,
-  bool pae, bool itsc, bool nested_virt,
-  const struct xc_xend_cpuid *cpuid)
-{
-int rc;
-xc_dominfo_t di;
-unsigned int nr_leaves, nr_msrs;
-xen_cpuid_leaf_t *leaves = NULL;
-struct cpuid_policy *p = NULL;
-xc_cpu_policy_t *policy = NULL;
-xc_cpu_policy_t *host = NULL;
-uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
-
-if ( xc_domain_getinfo(xch, domid, 1, &di) != 1 ||
- di.domid != domid )
-{
-ERROR("Failed to obtain d%d info", domid);
-rc = -ESRCH;
-goto out;
-}
-
-rc = xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs);
-if ( rc )
-{
-PERROR("Failed to obtain policy info size");
-rc = -errno;
-goto out;
-}
-
-rc = -ENOMEM;
-if ( (leaves = calloc(nr_leaves, sizeof(*leaves))) == NULL ||
- (p = calloc(1, sizeof(*p))) == NULL ||
- (policy = xc_cpu_policy_init()) == NULL ||
- (host = xc_cpu_policy_init()) == NULL )
-goto out;
-
-rc = xc_cpu_policy_get_system(xch, XEN_SYSCTL_cpu_policy_host, host);
-if ( rc )
-{
-PERROR("Failed to get host policy");
-rc = -errno;
-goto out;
-}
-
-/* Get the domain's default policy. */
-nr_msrs = 0;
-rc = get_system_cpu_policy(xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default
-   : XEN_SYSCTL_cpu_policy_pv_default,
-   &nr_leaves, leaves, &nr_msrs, NULL);
-if ( rc )
-{
-PERROR("Failed to obtain %s default policy", di.hvm ? "hvm" : "pv");
-rc = -errno;
-goto out;
-}
-
-rc = x86_cpuid_copy_from_buffer(p, leaves, nr_leaves,
-&err_leaf, &err_subleaf);
-if ( rc )
-{
-ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = 
%s)",
-  err_leaf, err_subleaf, -rc, strerror(-rc));
-goto out;
-}
-
-if ( restore )
-{
-policy->cpuid = *p;
-xc_cpu_policy_make_compat_4_12(xch, policy, host, di.hvm);
-*p = policy->cpuid;
-}
-
-if ( featureset )
-{
-policy.cpuid = *p;
-rc = xc_cpu_policy_apply_featureset(xch, &pol

[PATCH v7 07/11] libs/guest: rework xc_cpuid_xend_policy

2022-01-25 Thread Roger Pau Monne
Rename xc_cpuid_xend_policy to xc_cpu_policy_apply_cpuid and make it
public. Modify the function internally to use the new xc_cpu_policy_*
set of functions. Also don't apply the passed policy to a domain
directly, and instead modify the provided xc_cpu_policy_t. The caller
will be responsible of applying the modified cpu policy to the domain.

Note that further patches will end up removing this function, as the
callers should have the necessary helpers to modify an xc_cpu_policy_t
themselves.

The find_leaf helper and related comparison function is also removed,
as it's no longer needed to search for cpuid leafs as finding the
matching leaves is now done using xc_cpu_policy_get_cpuid.

No functional change intended.

Signed-off-by: Roger Pau Monné 
---
Changes since v6:
 - Pass a host policy to xc_cpuid_apply_policy.

Changes since v3:
 - Drop find_leaf and comparison helper.
---
 tools/include/xenguest.h|   5 +
 tools/libs/guest/xg_cpuid_x86.c | 203 
 2 files changed, 81 insertions(+), 127 deletions(-)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index 5e60f81192..98a998f11d 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -830,6 +830,11 @@ void xc_cpu_policy_make_compat_4_12(xc_interface *xch, 
xc_cpu_policy_t *policy,
 void xc_cpu_policy_legacy_topology(xc_interface *xch, xc_cpu_policy_t *policy,
const xc_cpu_policy_t *host);
 
+/* Apply an xc_xend_cpuid object to the policy. */
+int xc_cpu_policy_apply_cpuid(xc_interface *xch, xc_cpu_policy_t *policy,
+  const xc_cpu_policy_t *host,
+  const struct xc_xend_cpuid *cpuid, bool hvm);
+
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
   uint32_t *nr_features, uint32_t *featureset);
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index 6d8d16eed5..3503f21793 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -254,144 +254,99 @@ int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t 
domid,
 return ret;
 }
 
-static int compare_leaves(const void *l, const void *r)
-{
-const xen_cpuid_leaf_t *lhs = l;
-const xen_cpuid_leaf_t *rhs = r;
-
-if ( lhs->leaf != rhs->leaf )
-return lhs->leaf < rhs->leaf ? -1 : 1;
-
-if ( lhs->subleaf != rhs->subleaf )
-return lhs->subleaf < rhs->subleaf ? -1 : 1;
-
-return 0;
-}
-
-static xen_cpuid_leaf_t *find_leaf(
-xen_cpuid_leaf_t *leaves, unsigned int nr_leaves,
-const struct xc_xend_cpuid *xend)
-{
-const xen_cpuid_leaf_t key = { xend->leaf, xend->subleaf };
-
-return bsearch(&key, leaves, nr_leaves, sizeof(*leaves), compare_leaves);
-}
-
-static int xc_cpuid_xend_policy(
-xc_interface *xch, uint32_t domid, const struct xc_xend_cpuid *xend)
+int xc_cpu_policy_apply_cpuid(xc_interface *xch, xc_cpu_policy_t *policy,
+  const xc_cpu_policy_t *host,
+  const struct xc_xend_cpuid *cpuid, bool hvm)
 {
 int rc;
-xc_dominfo_t di;
-unsigned int nr_leaves, nr_msrs;
-uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
-/*
- * Three full policies.  The host, default for the domain type,
- * and domain current.
- */
-xen_cpuid_leaf_t *host = NULL, *def = NULL, *cur = NULL;
-unsigned int nr_host, nr_def, nr_cur;
+xc_cpu_policy_t *def = NULL;
 
-if ( xc_domain_getinfo(xch, domid, 1, &di) != 1 ||
- di.domid != domid )
+def = xc_cpu_policy_init();
+if ( !def )
 {
-ERROR("Failed to obtain d%d info", domid);
-rc = -ESRCH;
-goto fail;
-}
-
-rc = xc_cpu_policy_get_size(xch, &nr_leaves, &nr_msrs);
-if ( rc )
-{
-PERROR("Failed to obtain policy info size");
-rc = -errno;
-goto fail;
-}
-
-rc = -ENOMEM;
-if ( (host = calloc(nr_leaves, sizeof(*host))) == NULL ||
- (def  = calloc(nr_leaves, sizeof(*def)))  == NULL ||
- (cur  = calloc(nr_leaves, sizeof(*cur)))  == NULL )
-{
-ERROR("Unable to allocate memory for %u CPUID leaves", nr_leaves);
-goto fail;
-}
-
-/* Get the domain's current policy. */
-nr_msrs = 0;
-nr_cur = nr_leaves;
-rc = get_domain_cpu_policy(xch, domid, &nr_cur, cur, &nr_msrs, NULL);
-if ( rc )
-{
-PERROR("Failed to obtain d%d current policy", domid);
-rc = -errno;
-goto fail;
+PERROR("Failed to init policy");
+rc = -ENOMEM;
+goto out;
 }
 
 /* Get the domain type's default policy. */
-nr_msrs = 0;
-nr_def = nr_leaves;
-rc = get_system_cpu_policy(xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default
+rc = xc_cpu_policy_get_system(xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default
  

[PATCH v7 06/11] libs/guest: introduce helper set cpu topology in cpu policy

2022-01-25 Thread Roger Pau Monne
This logic is pulled out from xc_cpuid_apply_policy and placed into a
separate helper. Note the legacy part of the introduced function, as
long term Xen will require a proper topology setter function capable
of expressing a more diverse set of topologies.

No functional change intended.

Signed-off-by: Roger Pau Monné 
---
Changes since v6:
 - Pass a host policy to xc_cpu_policy_legacy_topology.

Changes since v5:
 - Keep using the host featureset.
 - Fix copied comment typo.

Changes since v4:
 - s/xc_cpu_policy_topology/xc_cpu_policy_legacy_topology/
---
 tools/include/xenguest.h|   9 ++
 tools/libs/guest/xg_cpuid_x86.c | 165 
 2 files changed, 91 insertions(+), 83 deletions(-)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index df18c73984..5e60f81192 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -821,6 +821,15 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, 
xc_cpu_policy_t *host,
 void xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy,
 const xc_cpu_policy_t *host, bool hvm);
 
+/*
+ * Setup the legacy policy topology.
+ *
+ * The `host` parameter should only be provided when creating a policy for a PV
+ * guest.
+ */
+void xc_cpu_policy_legacy_topology(xc_interface *xch, xc_cpu_policy_t *policy,
+   const xc_cpu_policy_t *host);
+
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
   uint32_t *nr_features, uint32_t *featureset);
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index 20fd786da3..6d8d16eed5 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -429,14 +429,12 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 {
 int rc;
 xc_dominfo_t di;
-unsigned int i, nr_leaves, nr_msrs;
+unsigned int nr_leaves, nr_msrs;
 xen_cpuid_leaf_t *leaves = NULL;
 struct cpuid_policy *p = NULL;
 xc_cpu_policy_t *policy = NULL;
 xc_cpu_policy_t *host = NULL;
 uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
-uint32_t host_featureset[FEATURESET_NR_ENTRIES] = {};
-uint32_t len = ARRAY_SIZE(host_featureset);
 
 if ( xc_domain_getinfo(xch, domid, 1, &di) != 1 ||
  di.domid != domid )
@@ -461,24 +459,14 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
  (host = xc_cpu_policy_init()) == NULL )
 goto out;
 
-/* Get the host policy. */
-rc = xc_get_cpu_featureset(xch, XEN_SYSCTL_cpu_featureset_host,
-   &len, host_featureset);
+rc = xc_cpu_policy_get_system(xch, XEN_SYSCTL_cpu_policy_host, host);
 if ( rc )
 {
-/* Tolerate "buffer too small", as we've got the bits we need. */
-if ( errno == ENOBUFS )
-rc = 0;
-else
-{
-PERROR("Failed to obtain host featureset");
-rc = -errno;
-goto out;
-}
+PERROR("Failed to get host policy");
+rc = -errno;
+goto out;
 }
 
-cpuid_featureset_to_policy(host_featureset, &host->cpuid);
-
 /* Get the domain's default policy. */
 nr_msrs = 0;
 rc = get_system_cpu_policy(xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default
@@ -562,72 +550,9 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 }
 }
 
-if ( !di.hvm )
-{
-/*
- * On hardware without CPUID Faulting, PV guests see real topology.
- * As a consequence, they also need to see the host htt/cmp fields.
- */
-p->basic.htt   = test_bit(X86_FEATURE_HTT, host_featureset);
-p->extd.cmp_legacy = test_bit(X86_FEATURE_CMP_LEGACY, host_featureset);
-}
-else
-{
-/*
- * Topology for HVM guests is entirely controlled by Xen.  For now, we
- * hardcode APIC_ID = vcpu_id * 2 to give the illusion of no SMT.
- */
-p->basic.htt = true;
-p->extd.cmp_legacy = false;
-
-/*
- * Leaf 1 EBX[23:16] is Maximum Logical Processors Per Package.
- * Update to reflect vLAPIC_ID = vCPU_ID * 2, but make sure to avoid
- * overflow.
- */
-if ( !p->basic.lppp )
-p->basic.lppp = 2;
-else if ( !(p->basic.lppp & 0x80) )
-p->basic.lppp *= 2;
-
-switch ( p->x86_vendor )
-{
-case X86_VENDOR_INTEL:
-for ( i = 0; (p->cache.subleaf[i].type &&
-  i < ARRAY_SIZE(p->cache.raw)); ++i )
-{
-p->cache.subleaf[i].cores_per_package =
-(p->cache.subleaf[i].cores_per_package << 1) | 1;
-p->cache.subleaf[i].threads_per_cache = 0;
-}
-break;
-
-case X86_VEN

[PATCH v7 05/11] libs/guest: make a cpu policy compatible with older Xen versions

2022-01-25 Thread Roger Pau Monne
Older Xen versions used to expose some CPUID bits which are no longer
exposed by default. In order to keep a compatible behavior with
guests migrated from versions of Xen that don't encode the CPUID data
on the migration stream introduce a function that sets the same bits
as older Xen versions.

This is pulled out from xc_cpuid_apply_policy which already has this
logic present.

No functional change intended.

Signed-off-by: Roger Pau Monné 
---
Changes since v6:
 - Pass the host policy to the newly created helper.

Changes since v3:
 - Rename function to xc_cpu_policy_make_compat_4_12.

Changes since v1:
 - Move comments and explicitly mention pre-4.13 Xen.
---
 tools/include/xenguest.h|  4 +++
 tools/libs/guest/xg_cpuid_x86.c | 50 +
 2 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index 2672fd043c..df18c73984 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -817,6 +817,10 @@ int xc_cpu_policy_get_msr(xc_interface *xch, const 
xc_cpu_policy_t *policy,
 bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
  xc_cpu_policy_t *guest);
 
+/* Make a policy compatible with pre-4.13 Xen versions. */
+void xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy,
+const xc_cpu_policy_t *host, bool hvm);
+
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
   uint32_t *nr_features, uint32_t *featureset);
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index af5b8e47f4..20fd786da3 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -432,6 +432,8 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 unsigned int i, nr_leaves, nr_msrs;
 xen_cpuid_leaf_t *leaves = NULL;
 struct cpuid_policy *p = NULL;
+xc_cpu_policy_t *policy = NULL;
+xc_cpu_policy_t *host = NULL;
 uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
 uint32_t host_featureset[FEATURESET_NR_ENTRIES] = {};
 uint32_t len = ARRAY_SIZE(host_featureset);
@@ -454,7 +456,9 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 
 rc = -ENOMEM;
 if ( (leaves = calloc(nr_leaves, sizeof(*leaves))) == NULL ||
- (p = calloc(1, sizeof(*p))) == NULL )
+ (p = calloc(1, sizeof(*p))) == NULL ||
+ (policy = xc_cpu_policy_init()) == NULL ||
+ (host = xc_cpu_policy_init()) == NULL )
 goto out;
 
 /* Get the host policy. */
@@ -473,6 +477,8 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 }
 }
 
+cpuid_featureset_to_policy(host_featureset, &host->cpuid);
+
 /* Get the domain's default policy. */
 nr_msrs = 0;
 rc = get_system_cpu_policy(xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default
@@ -496,23 +502,9 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 
 if ( restore )
 {
-/*
- * Account for feature which have been disabled by default since Xen 
4.13,
- * so migrated-in VM's don't risk seeing features disappearing.
- */
-p->basic.rdrand = test_bit(X86_FEATURE_RDRAND, host_featureset);
-p->feat.hle = test_bit(X86_FEATURE_HLE, host_featureset);
-p->feat.rtm = test_bit(X86_FEATURE_RTM, host_featureset);
-
-if ( di.hvm )
-{
-p->feat.mpx = test_bit(X86_FEATURE_MPX, host_featureset);
-}
-
-/* Clamp maximum leaves to the ones supported on 4.12. */
-p->basic.max_leaf = min(p->basic.max_leaf, 0xdu);
-p->feat.max_subleaf = 0;
-p->extd.max_leaf = min(p->extd.max_leaf, 0x801c);
+policy->cpuid = *p;
+xc_cpu_policy_make_compat_4_12(xch, policy, host, di.hvm);
+*p = policy->cpuid;
 }
 
 if ( featureset )
@@ -662,6 +654,8 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t 
domid, bool restore,
 out:
 free(p);
 free(leaves);
+xc_cpu_policy_destroy(policy);
+xc_cpu_policy_destroy(host);
 
 return rc;
 }
@@ -919,3 +913,23 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, 
xc_cpu_policy_t *host,
 
 return false;
 }
+
+void xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy,
+const xc_cpu_policy_t *host, bool hvm)
+{
+/*
+ * Account for features which have been disabled by default since Xen 4.13,
+ * so migrated-in VM's don't risk seeing features disappearing.
+ */
+policy->cpuid.basic.rdrand = host->cpuid.basic.rdrand;
+policy->cpuid.feat.hle = host->cpuid.feat.hle;
+policy->cpuid.feat.rtm = host->cpuid.feat.rtm;
+
+if ( hvm )
+policy->cpuid.feat.mpx = host->cpuid.feat.mpx;
+
+  

[PATCH v7 04/11] libs/guest: allow fetching a specific MSR entry from a cpu policy

2022-01-25 Thread Roger Pau Monne
Introduce an interface that returns a specific MSR entry from a cpu
policy in xen_msr_entry_t format.

This is useful to callers can peek data from the opaque
xc_cpu_policy_t type.

No caller of the interface introduced on this patch.

Signed-off-by: Roger Pau Monné 
Reviewed-by: Jan Beulich 
---
Changes since v3:
 - Use x86_msr_get_entry.

Changes since v1:
 - Introduce a helper to perform a binary search of the MSR entries
   array.
---
 tools/include/xenguest.h|  2 ++
 tools/libs/guest/xg_cpuid_x86.c | 20 
 2 files changed, 22 insertions(+)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index 0a6fd99306..2672fd043c 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -810,6 +810,8 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, 
xc_cpu_policy_t *policy,
 int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy,
 uint32_t leaf, uint32_t subleaf,
 xen_cpuid_leaf_t *out);
+int xc_cpu_policy_get_msr(xc_interface *xch, const xc_cpu_policy_t *policy,
+  uint32_t msr, xen_msr_entry_t *out);
 
 /* Compatibility calculations. */
 bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index ad6b349680..af5b8e47f4 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -881,6 +881,26 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const 
xc_cpu_policy_t *policy,
 return 0;
 }
 
+int xc_cpu_policy_get_msr(xc_interface *xch, const xc_cpu_policy_t *policy,
+  uint32_t msr, xen_msr_entry_t *out)
+{
+const uint64_t *val;
+
+*out = (xen_msr_entry_t){};
+
+val = x86_msr_get_entry(&policy->msr, msr);
+if ( !val )
+{
+errno = ENOENT;
+return -1;
+}
+
+out->idx = msr;
+out->val = *val;
+
+return 0;
+}
+
 bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
  xc_cpu_policy_t *guest)
 {
-- 
2.34.1




[PATCH v7 03/11] libx86: introduce helper to fetch msr entry

2022-01-25 Thread Roger Pau Monne
Use such helper in order to replace the code in
x86_msr_copy_from_buffer. Note the introduced helper should not be
directly called and instead x86_msr_get_entry should be used that will
properly deal with const and non-const inputs.

Note this requires making the raw fields uint64_t so that it can
accommodate the maximum size of MSRs values, and in turn removing the
truncation tests.

Suggested-by: Andrew Cooper 
Signed-off-by: Roger Pau Monné 
---
Changes since v4:
 - Rename _x86_msr_get_entry to x86_msr_get_entry_const.
 - Add newline before endif.

Changes since v3:
 - New in this version.
---
 tools/tests/cpu-policy/test-cpu-policy.c | 48 +++-
 xen/include/xen/lib/x86/msr.h| 20 +-
 xen/lib/x86/msr.c| 41 ++--
 3 files changed, 76 insertions(+), 33 deletions(-)

diff --git a/tools/tests/cpu-policy/test-cpu-policy.c 
b/tools/tests/cpu-policy/test-cpu-policy.c
index 09e4b5f528..8f51447117 100644
--- a/tools/tests/cpu-policy/test-cpu-policy.c
+++ b/tools/tests/cpu-policy/test-cpu-policy.c
@@ -387,16 +387,6 @@ static void test_msr_deserialise_failure(void)
 .msr = { .idx = 0xce, .flags = 1 },
 .rc = -EINVAL,
 },
-{
-.name = "truncated val",
-.msr = { .idx = 0xce, .val = ~0ull },
-.rc = -EOVERFLOW,
-},
-{
-.name = "truncated val",
-.msr = { .idx = 0x10a, .val = ~0ull },
-.rc = -EOVERFLOW,
-},
 };
 
 printf("Testing MSR deserialise failure:\n");
@@ -745,6 +735,43 @@ static void test_cpuid_get_leaf(void)
 }
 }
 
+static void test_msr_get_entry(void)
+{
+static const struct test {
+const char *name;
+unsigned int idx;
+bool success;
+} tests[] = {
+{
+.name = "bad msr index",
+.idx = -1,
+},
+{
+.name = "good msr index",
+.idx = 0xce,
+.success = true,
+},
+};
+const struct msr_policy pc;
+const uint64_t *ec;
+struct msr_policy p;
+uint64_t *e;
+
+/* Constness build test. */
+ec = x86_msr_get_entry(&pc, 0);
+e = x86_msr_get_entry(&p, 0);
+
+printf("Testing MSR get leaf:\n");
+
+for ( size_t i = 0; i < ARRAY_SIZE(tests); ++i )
+{
+const struct test *t = &tests[i];
+
+if ( !!x86_msr_get_entry(&pc, t->idx) != t->success )
+fail("  Test %s failed\n", t->name);
+}
+}
+
 static void test_is_compatible_success(void)
 {
 static struct test {
@@ -865,6 +892,7 @@ int main(int argc, char **argv)
 
 test_msr_serialise_success();
 test_msr_deserialise_failure();
+test_msr_get_entry();
 
 test_is_compatible_success();
 test_is_compatible_failure();
diff --git a/xen/include/xen/lib/x86/msr.h b/xen/include/xen/lib/x86/msr.h
index 48ba4a59c0..4d84b7cf27 100644
--- a/xen/include/xen/lib/x86/msr.h
+++ b/xen/include/xen/lib/x86/msr.h
@@ -17,7 +17,7 @@ struct msr_policy
  * is dependent on real hardware support.
  */
 union {
-uint32_t raw;
+uint64_t raw;
 struct {
 uint32_t :31;
 bool cpuid_faulting:1;
@@ -32,7 +32,7 @@ struct msr_policy
  * fixed in hardware.
  */
 union {
-uint32_t raw;
+uint64_t raw;
 struct {
 bool rdcl_no:1;
 bool ibrs_all:1;
@@ -91,6 +91,22 @@ int x86_msr_copy_from_buffer(struct msr_policy *policy,
  const msr_entry_buffer_t msrs, uint32_t 
nr_entries,
  uint32_t *err_msr);
 
+/**
+ * Get a MSR entry from a policy object.
+ *
+ * @param policy  The msr_policy object.
+ * @param idx The index.
+ * @returns a pointer to the requested leaf or NULL in case of error.
+ *
+ * Do not call this function directly and instead use x86_msr_get_entry that
+ * will deal with both const and non-const policies returning a pointer with
+ * constness matching that of the input.
+ */
+const uint64_t *x86_msr_get_entry_const(const struct msr_policy *policy,
+uint32_t idx);
+#define x86_msr_get_entry(p, i) \
+((__typeof__(&(p)->platform_info.raw))x86_msr_get_entry_const(p, i))
+
 #endif /* !XEN_LIB_X86_MSR_H */
 
 /*
diff --git a/xen/lib/x86/msr.c b/xen/lib/x86/msr.c
index 7d71e92a38..e9b337dd70 100644
--- a/xen/lib/x86/msr.c
+++ b/xen/lib/x86/msr.c
@@ -74,6 +74,8 @@ int x86_msr_copy_from_buffer(struct msr_policy *p,
 
 for ( i = 0; i < nr_entries; i++ )
 {
+uint64_t *val;
+
 if ( copy_from_buffer_offset(&data, msrs, i, 1) )
 return -EFAULT;
 
@@ -83,31 +85,13 @@ int x86_msr_copy_from_buffer(struct msr_policy *p,
 goto err;
 }
 
-switch ( data.idx )
+val = x86_msr_get_entry(p, data.idx);
+if ( !val )
 {
-/*
- * Assign data.val to p->field, checking 

[PATCH v7 02/11] libs/guest: allow fetching a specific CPUID leaf from a cpu policy

2022-01-25 Thread Roger Pau Monne
Introduce an interface that returns a specific leaf/subleaf from a cpu
policy in xen_cpuid_leaf_t format.

This is useful to callers can peek data from the opaque
xc_cpu_policy_t type.

No caller of the interface introduced on this patch.

Signed-off-by: Roger Pau Monné 
Reviewed-by: Jan Beulich 
---
Changes since v6:
 - Add newline before return.

Changes since v5:
 - Zero out parameter.

Changes since v3:
 - Use x86_cpuid_get_leaf.

Changes since v1:
 - Use find leaf.
---
 tools/include/xenguest.h|  3 +++
 tools/libs/guest/xg_cpuid_x86.c | 26 ++
 2 files changed, 29 insertions(+)

diff --git a/tools/include/xenguest.h b/tools/include/xenguest.h
index e01f494b77..0a6fd99306 100644
--- a/tools/include/xenguest.h
+++ b/tools/include/xenguest.h
@@ -807,6 +807,9 @@ int xc_cpu_policy_update_cpuid(xc_interface *xch, 
xc_cpu_policy_t *policy,
uint32_t nr);
 int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy,
   const xen_msr_entry_t *msrs, uint32_t nr);
+int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy,
+uint32_t leaf, uint32_t subleaf,
+xen_cpuid_leaf_t *out);
 
 /* Compatibility calculations. */
 bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index b9e827ce7e..ad6b349680 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -855,6 +855,32 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, 
xc_cpu_policy_t *policy,
 return rc;
 }
 
+int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy,
+uint32_t leaf, uint32_t subleaf,
+xen_cpuid_leaf_t *out)
+{
+const struct cpuid_leaf *tmp;
+
+*out = (xen_cpuid_leaf_t){};
+
+tmp = x86_cpuid_get_leaf(&policy->cpuid, leaf, subleaf);
+if ( !tmp )
+{
+/* Unable to find a matching leaf. */
+errno = ENOENT;
+return -1;
+}
+
+out->leaf = leaf;
+out->subleaf = subleaf;
+out->a = tmp->a;
+out->b = tmp->b;
+out->c = tmp->c;
+out->d = tmp->d;
+
+return 0;
+}
+
 bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
  xc_cpu_policy_t *guest)
 {
-- 
2.34.1




[PATCH v7 00/11] libs/guest: new CPUID/MSR interface

2022-01-25 Thread Roger Pau Monne
Hello,

The following series introduces a new CPUID/MSR interface for the
xenguest library. Such interface handles both CPUID and MSRs using the
same opaque object, and provides some helpers for the user to peek or
modify such data without exposing the backing type. This is useful for
future development as CPUID and MSRs are closely related, so it makes
handling those much easier if they are inside the same object (ie: a
change to a CPUID bit might expose or hide an MSR).

In this patch series libxl and other in tree users have been switched to
use the new interface, so it shouldn't result in any functional change
from a user point of view.

Note there are still some missing pieces likely. The way to modify CPUID
data is not ideal, as it requires fetching a leaf and modifying it
directly. We might want some kind of interface in order to set specific
CPUID features more easily, but that's to be discussed, and would be
done as a follow up series.

The addition of a helper to generate compatible policies given two
inputs has been removed from this iteration, sine Andrew Cooper has
posted a patch to set the foundation for that, and further work should
be done against that baseline.

Thanks, Roger.

Jan Beulich (1):
  x86/CPUID: shrink max_{,sub}leaf fields according to actual leaf
contents

Roger Pau Monne (10):
  libx86: introduce helper to fetch cpuid leaf
  libs/guest: allow fetching a specific CPUID leaf from a cpu policy
  libx86: introduce helper to fetch msr entry
  libs/guest: allow fetching a specific MSR entry from a cpu policy
  libs/guest: make a cpu policy compatible with older Xen versions
  libs/guest: introduce helper set cpu topology in cpu policy
  libs/guest: rework xc_cpuid_xend_policy
  libs/guest: apply a featureset into a cpu policy
  libs/{light,guest}: implement xc_cpuid_apply_policy in libxl
  libs/guest: (re)move xc_cpu_policy_apply_cpuid

 tools/include/libxl.h|   6 +-
 tools/include/xenctrl.h  |  44 --
 tools/include/xenguest.h |  26 +
 tools/libs/guest/xg_cpuid_x86.c  | 607 ---
 tools/libs/light/libxl_cpuid.c   | 205 +++-
 tools/libs/light/libxl_internal.h|  26 +
 tools/tests/cpu-policy/test-cpu-policy.c | 326 +++-
 xen/arch/x86/cpuid.c |  55 +-
 xen/include/xen/lib/x86/cpuid.h  |  26 +
 xen/include/xen/lib/x86/msr.h|  20 +-
 xen/lib/x86/cpuid.c  |  91 
 xen/lib/x86/msr.c|  41 +-
 12 files changed, 925 insertions(+), 548 deletions(-)

-- 
2.34.1




[PATCH v7 01/11] libx86: introduce helper to fetch cpuid leaf

2022-01-25 Thread Roger Pau Monne
Introduce a helper based on the current Xen guest_cpuid code in order
to fetch a cpuid leaf from a policy. The newly introduced function in
cpuid.c should not be directly called and instead the provided
x86_cpuid_get_leaf macro should be used that will properly deal with
const and non-const inputs.

Also add a test to check that the introduced helper doesn't go over
the bounds of the policy.

Note the code in x86_cpuid_copy_from_buffer is not switched to use the
new function because of the boundary checks against the max fields of
the policy, which might not be properly set at the point where
x86_cpuid_copy_from_buffer get called, for example when filling an
empty policy from scratch.

Suggested-by: Andrew Cooper 
Signed-off-by: Roger Pau Monné 
---
Changes since v6:
 - Add more tests.
 - Drop Jan R-b.

Changes since v4:
 - Rename _x86_cpuid_get_leaf to x86_cpuid_get_leaf_const.

Changes since v3:
 - New in this version.
---
 tools/tests/cpu-policy/test-cpu-policy.c | 177 +++
 xen/arch/x86/cpuid.c |  55 +--
 xen/include/xen/lib/x86/cpuid.h  |  19 +++
 xen/lib/x86/cpuid.c  |  52 +++
 4 files changed, 255 insertions(+), 48 deletions(-)

diff --git a/tools/tests/cpu-policy/test-cpu-policy.c 
b/tools/tests/cpu-policy/test-cpu-policy.c
index ed450a0997..09e4b5f528 100644
--- a/tools/tests/cpu-policy/test-cpu-policy.c
+++ b/tools/tests/cpu-policy/test-cpu-policy.c
@@ -1,6 +1,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -570,6 +571,180 @@ static void test_cpuid_out_of_range_clearing(void)
 }
 }
 
+static void test_cpuid_get_leaf_failure(void)
+{
+static const struct test {
+struct cpuid_policy p;
+const char *name;
+uint32_t leaf, subleaf;
+} tests[] = {
+/* Test for invalid configurations in the object itself. */
+{
+.name = "Basic max leaf >= array size",
+.p = {
+.basic.max_leaf = CPUID_GUEST_NR_BASIC,
+},
+},
+{
+.name = "Feature max leaf >= array size",
+.p = {
+.basic.max_leaf = CPUID_GUEST_NR_BASIC - 1,
+.feat.max_subleaf = CPUID_GUEST_NR_FEAT,
+},
+.leaf = 0x7,
+},
+{
+.name = "Extended max leaf >= array size",
+.p = {
+.extd.max_leaf = 0x8000 + CPUID_GUEST_NR_EXTD,
+},
+.leaf = 0x8000,
+},
+
+/* Test out-of-bounds checks in the accessor. */
+{
+.name = "Basic leaf >= max leaf",
+.p = {
+.basic.max_leaf = CPUID_GUEST_NR_BASIC - 1,
+},
+.leaf = CPUID_GUEST_NR_BASIC,
+},
+{
+.name = "Cache leaf >= cache array size",
+.p = {
+.basic.max_leaf = CPUID_GUEST_NR_BASIC - 1,
+},
+.leaf = 0x4,
+.subleaf = CPUID_GUEST_NR_CACHE,
+},
+{
+.name = "Feature leaf >= max leaf",
+.p = {
+.basic.max_leaf = CPUID_GUEST_NR_BASIC - 1,
+.feat.max_subleaf = CPUID_GUEST_NR_FEAT - 1,
+},
+.leaf = 0x7,
+.subleaf = CPUID_GUEST_NR_FEAT,
+},
+{
+.name = "Extended Topo leaf >= cache array size",
+.p = {
+.basic.max_leaf = CPUID_GUEST_NR_BASIC - 1,
+},
+.leaf = 0xb,
+.subleaf = CPUID_GUEST_NR_TOPO,
+},
+{
+.name = "Xstate leaf >= cache array size",
+.p = {
+.basic.max_leaf = CPUID_GUEST_NR_BASIC - 1,
+},
+.leaf = 0xd,
+.subleaf = CPUID_GUEST_NR_XSTATE,
+},
+{
+.name = "Extended leaf >= max leaf",
+.p = {
+.extd.max_leaf = 0x8000 + CPUID_GUEST_NR_EXTD - 1,
+},
+.leaf = 0x8000 + CPUID_GUEST_NR_EXTD,
+},
+
+/* Test fetching Xsave without present. */
+{
+.name = "Fetch Xsave without present",
+.p = {
+.basic = {
+.max_leaf = CPUID_GUEST_NR_BASIC - 1,
+.xsave = false,
+},
+},
+.leaf = 0xd,
+},
+
+};
+const struct cpuid_policy pc = {};
+const struct cpuid_leaf *lc;
+struct cpuid_policy p = {};
+struct cpuid_leaf *l;
+
+/* Constness build test. */
+lc = x86_cpuid_get_leaf(&pc, 0, 0);
+l = x86_cpuid_get_leaf(&p, 0, 0);
+
+printf("Testing CPUID get leaf bound checking:\n");
+
+for ( size_t i = 0; i < ARRAY_SIZE(tests); ++i )
+{
+const struct test *t = &tests[i];
+const struct cpuid_policy *p = memdup(&t->p);
+
+if ( x86_cpuid_get_leaf_const(p, t->leaf, t->subleaf) )
+fail("

Re: [PATCH] x86emul: recognize CLDEMOTE

2022-01-25 Thread Roger Pau Monné
On Tue, Jan 25, 2022 at 03:22:25PM +0100, Jan Beulich wrote:
> We claim to support the insn, but so far the emulator has been handling
> it as a NOP.

While not ideal, the SDM mentions that "The CLDEMOTE instruction may
be ignored by hardware in certain cases and is not a guarantee.".

> Signed-off-by: Jan Beulich 
> ---
> While handling x86emul_cldemote separately in hvmemul_cache_op() means
> to carry some redundant code, folding it with CLFLUSH{,OPT} / CLWB
> didn't seem very attractive either.
> 
> --- a/xen/arch/x86/arch.mk
> +++ b/xen/arch/x86/arch.mk
> @@ -23,6 +23,7 @@ $(call as-option-add,CFLAGS,CC,"xsaveopt
>  $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
>  $(call as-option-add,CFLAGS,CC,"clac",-DHAVE_AS_CLAC_STAC)
>  $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
> +$(call as-option-add,CFLAGS,CC,"cldemote (%rax)",-DHAVE_AS_CLDEMOTE)
>  $(call as-option-add,CFLAGS,CC,".equ \"x\"$$(comma)1",-DHAVE_AS_QUOTED_SYM)
>  $(call as-option-add,CFLAGS,CC,"invpcid 
> (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
>  $(call as-option-add,CFLAGS,CC,"movdiri 
> %rax$$(comma)(%rax)",-DHAVE_AS_MOVDIR)
> --- a/xen/arch/x86/hvm/emulate.c
> +++ b/xen/arch/x86/hvm/emulate.c
> @@ -2351,6 +2351,28 @@ static int hvmemul_cache_op(
>   * to be sensibly used is in (virtualization unaware) firmware.
>   */
>  break;
> +
> +case x86emul_cldemote:
> +ASSERT(!is_x86_system_segment(seg));
> +
> +if ( !boot_cpu_has(X86_FEATURE_CLDEMOTE) ||
> + hvmemul_virtual_to_linear(seg, offset, 0, NULL, hvm_access_none,
> +   hvmemul_ctxt, &addr) != X86EMUL_OKAY )
> +break;
> +
> +if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
> +pfec |= PFEC_user_mode;
> +
> +mapping = hvmemul_map_linear_addr(addr, 0, pfec, hvmemul_ctxt);

I think the emulator should map the address using the same cache
attributes as the guest, or else the result might be different than
intended?

Thanks, Roger.



Re: [RFC v1 3/5] xen/arm: introduce SCMI-SMC mediator driver

2022-01-25 Thread Oleksii Moisieiev
On Mon, Jan 24, 2022 at 02:14:43PM -0800, Stefano Stabellini wrote:
> On Mon, 24 Jan 2022, Julien Grall wrote:
> > On 24/01/2022 19:06, Stefano Stabellini wrote:
> > > It looks like XEN_DOMCTL_host_node_by_path and
> > > XEN_DOMCTL_find_host_compatible_node would also solve the problem but I
> > > think that a single hypercall that retrieves the entire host DTB would
> > > be easier to implement
> > 
> > DOMCTL should only be used to handle per-domain information. If we want to
> > create a new sub-hypercall of either __HYPERVISOR_platform_op or
> > __HYPERVISOR_sysctl_op (not sure which one).
> > 
> > AFAICT, both are versioned.
> > 
> > > and more robust in the long term. >
> > > hypfs has the advantage that it would create an interface more similar
> > > to the one people are already used to on Linux systems
> > > (/proc/device-tree). xl/libxl would have to scan the whole hypfs tree,
> > > which intuitively I think it would be slower.
> > 
> > Even if you have the binary blob, you would still have to scan the
> > device-tree. That said, it is probably going to be potentially a bit faster
> > because you have less hypercall.
> > 
> > However, here this is a trade-off between memory use and speed. If you want
> > speed, then you may have to transfer up to 2MB every time. So the question 
> > is
> > do we care more about speed or memory usage?
> > 
> > > Also the feature might be
> > > harder to implement but I am not sure.
> > > 
> > > I don't have a strong preference and this is not a stable interface (we
> > > don't have to be extra paranoid about forward and backward
> > > compatibility). So I am fine either way. Let's see what the others think
> > > as well.
> > 
> > My preference would be to use hypfs as this is cleaner than exposing a blob.
> 
> That's also fine by me. Probably the hypfs implementation shouldn't be
> much more difficult than something like
> XEN_DOMCTL_host_node_by_path/XEN_DOMCTL_find_host_compatible_node.
> 
> 
> > However, are we sure we can simply copy the content of the host Device-Tree 
> > to
> > the guest Device-Tree for SCMI? For instance, I know that for device
> > passthrough there are some property that needs to be altered for some 
> > devices.
> > Hence, why it is not present. Although, I vaguely recalled to have written a
> > PoC, not sure if it was posted on the ML.
> 
> The SCMI node cannot be copied "as is" from host to guest. It needs a
> couple of changes but they seem feasible as they are limited to the
> channels exposed to the guest. (The generic device passthrough case is a
> lot more difficult.)


Hi Stefano,

What I'm thinking about is do we actually need to create SCMI node in DomU 
device-tree?
I have this question is because we don't need SCMI node to be present in DomU 
device-tree if it has no passed-through devices, which are using scmi. 
So if we don't have passed-through devices or do not provide DomU partial 
device-tree 
in config, then there is no need to create SCMI node.

For now I see the following possible domu configurations:
1) If DomU has a lot of passed-through devices and it's easier to inherit 
host device-tree and disable not passed-through devices.
Partial device tree will looks like this:

#include "r8a77961-salvator-xs.dts" //include host device tree

/
{
soc {
...
}

};

// Disable non passed-through devices
&hscif {
status = "disabled";
};

In this case DomU partial device-tree will inherit arm,scmi-smc and 
arm,scmi-shmem nodes and all clock/reset/power-domains which are using scmi. 
All this nodes can be copied to DomU device-tree from partial device-tree.

2) DomU has few passed-through devices, so it's easier to add the device nodes 
to the passthrough node of DomU partial device-tree.
DomU partial device-tree will look like this:
{
scmi_shmem: scp-shmem@0x53FF {
compatible = "arm,scmi-shmem";
reg = <0x0 0x53FF 0x0 0x1>;  
};
scmi {
arm,smc-id = <>;
compatible = "arm,scmi-smc"; 
shmem = <&scmi_shmem>;
scmi_clock: protocol@14 {
...
};
scmi_reset: protocol@16 {
...
};
}; 
passthrough {
hscif0: serial@e654 { 
compatible = "renesas,hscif-r8a77961";
scmi_devid = <5>;
clocks = <&scmi_clock 5>;
resets = <&scmi_reset 5>;
...
};
};
};

As you can see in this case we have to manually copy arm,scmi-shmem and 
arm,scmi-smc nodes with hscif0 node or the device-tree compilation will fail.
We can use 0x53FF, provided in arm,scmi-shmem node and map domain channel 
to this address and copy scmi related nodes to the DomU device-tree.
This is useful when we need to expose only certain protocols to 

[PATCH] x86emul: recognize CLDEMOTE

2022-01-25 Thread Jan Beulich
We claim to support the insn, but so far the emulator has been handling
it as a NOP.

Signed-off-by: Jan Beulich 
---
While handling x86emul_cldemote separately in hvmemul_cache_op() means
to carry some redundant code, folding it with CLFLUSH{,OPT} / CLWB
didn't seem very attractive either.

--- a/xen/arch/x86/arch.mk
+++ b/xen/arch/x86/arch.mk
@@ -23,6 +23,7 @@ $(call as-option-add,CFLAGS,CC,"xsaveopt
 $(call as-option-add,CFLAGS,CC,"rdseed %eax",-DHAVE_AS_RDSEED)
 $(call as-option-add,CFLAGS,CC,"clac",-DHAVE_AS_CLAC_STAC)
 $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB)
+$(call as-option-add,CFLAGS,CC,"cldemote (%rax)",-DHAVE_AS_CLDEMOTE)
 $(call as-option-add,CFLAGS,CC,".equ \"x\"$$(comma)1",-DHAVE_AS_QUOTED_SYM)
 $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID)
 $(call as-option-add,CFLAGS,CC,"movdiri %rax$$(comma)(%rax)",-DHAVE_AS_MOVDIR)
--- a/xen/arch/x86/hvm/emulate.c
+++ b/xen/arch/x86/hvm/emulate.c
@@ -2351,6 +2351,28 @@ static int hvmemul_cache_op(
  * to be sensibly used is in (virtualization unaware) firmware.
  */
 break;
+
+case x86emul_cldemote:
+ASSERT(!is_x86_system_segment(seg));
+
+if ( !boot_cpu_has(X86_FEATURE_CLDEMOTE) ||
+ hvmemul_virtual_to_linear(seg, offset, 0, NULL, hvm_access_none,
+   hvmemul_ctxt, &addr) != X86EMUL_OKAY )
+break;
+
+if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
+pfec |= PFEC_user_mode;
+
+mapping = hvmemul_map_linear_addr(addr, 0, pfec, hvmemul_ctxt);
+if ( mapping == ERR_PTR(~X86EMUL_EXCEPTION) )
+x86_emul_reset_event(&hvmemul_ctxt->ctxt);
+if ( IS_ERR_OR_NULL(mapping) )
+break;
+
+cldemote(mapping);
+
+hvmemul_unmap_linear_addr(mapping, addr, 0, hvmemul_ctxt);
+break;
 }
 
 return X86EMUL_OKAY;
--- a/xen/arch/x86/include/asm/system.h
+++ b/xen/arch/x86/include/asm/system.h
@@ -37,6 +37,16 @@ static inline void clwb(const void *p)
 #endif
 }
 
+static inline void cldemote(const void *p)
+{
+#if defined(HAVE_AS_CLDEMOTE)
+asm volatile ( "cldemote %0" :: "m" (*(const char *)p) );
+#else
+asm volatile ( ".byte 0x0f, 0x1c, 0x02"
+   :: "d" (p), "m" (*(const char *)p) );
+#endif
+}
+
 #define xchg(ptr,v) \
 ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr
 
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -6290,7 +6290,8 @@ x86_emulate(
 
 case X86EMUL_OPC(0x0f, 0x0d): /* GrpP (prefetch) */
 case X86EMUL_OPC(0x0f, 0x18): /* Grp16 (prefetch/nop) */
-case X86EMUL_OPC(0x0f, 0x19) ... X86EMUL_OPC(0x0f, 0x1f): /* nop */
+case X86EMUL_OPC(0x0f, 0x19) ... X86EMUL_OPC(0x0f, 0x1b): /* nop */
+case X86EMUL_OPC(0x0f, 0x1d) ... X86EMUL_OPC(0x0f, 0x1f): /* nop */
 break;
 
 #ifndef X86EMUL_NO_MMX
@@ -6627,6 +6628,12 @@ x86_emulate(
 
 #endif /* !X86EMUL_NO_SIMD */
 
+case X86EMUL_OPC(0x0f, 0x1c): /* cldemote / nop */
+if ( ctxt->cpuid->feat.cldemote && !vex.pfx && !modrm_reg &&
+ ops->cache_op )
+ops->cache_op(x86emul_cldemote, ea.mem.seg, ea.mem.off, ctxt);
+break;
+
 case X86EMUL_OPC(0x0f, 0x20): /* mov cr,reg */
 case X86EMUL_OPC(0x0f, 0x21): /* mov dr,reg */
 case X86EMUL_OPC(0x0f, 0x22): /* mov reg,cr */
--- a/xen/arch/x86/x86_emulate/x86_emulate.h
+++ b/xen/arch/x86/x86_emulate/x86_emulate.h
@@ -177,6 +177,7 @@ enum x86_emulate_fpu_type {
 };
 
 enum x86emul_cache_op {
+x86emul_cldemote,
 x86emul_clflush,
 x86emul_clflushopt,
 x86emul_clwb,




[linux-linus test] 167810: tolerable FAIL - PUSHED

2022-01-25 Thread osstest service owner
flight 167810 linux-linus real [real]
flight 167815 linux-linus real-retest [real]
http://logs.test-lab.xenproject.org/osstest/logs/167810/
http://logs.test-lab.xenproject.org/osstest/logs/167815/

Failures :-/ but no regressions.

Tests which are failing intermittently (not blocking):
 test-amd64-amd64-freebsd12-amd64 21 guest-start/freebsd.repeat fail pass in 
167815-retest

Tests which did not succeed, but are not blocking:
 test-amd64-amd64-xl-qemut-win7-amd64 19 guest-stopfail like 167801
 test-armhf-armhf-libvirt 16 saverestore-support-checkfail  like 167801
 test-amd64-amd64-qemuu-nested-amd 20 debian-hvm-install/l1/l2 fail like 167801
 test-amd64-amd64-xl-qemuu-ws16-amd64 19 guest-stopfail like 167801
 test-amd64-amd64-xl-qemut-ws16-amd64 19 guest-stopfail like 167801
 test-amd64-amd64-xl-qemuu-win7-amd64 19 guest-stopfail like 167801
 test-armhf-armhf-libvirt-qcow2 15 saverestore-support-check   fail like 167801
 test-armhf-armhf-libvirt-raw 15 saverestore-support-checkfail  like 167801
 test-amd64-amd64-libvirt 15 migrate-support-checkfail   never pass
 test-amd64-amd64-libvirt-xsm 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-seattle  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-seattle  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-credit2  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit2  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-xsm  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-libvirt-xsm 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit1  15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-credit1  16 saverestore-support-checkfail   never pass
 test-arm64-arm64-libvirt-xsm 16 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-thunderx 15 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-thunderx 16 saverestore-support-checkfail   never pass
 test-amd64-amd64-libvirt-qemuu-debianhvm-amd64-xsm 13 migrate-support-check 
fail never pass
 test-amd64-amd64-libvirt-qcow2 14 migrate-support-checkfail never pass
 test-amd64-amd64-libvirt-raw 14 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-raw 14 migrate-support-checkfail   never pass
 test-arm64-arm64-libvirt-raw 15 saverestore-support-checkfail   never pass
 test-arm64-arm64-xl-vhd  14 migrate-support-checkfail   never pass
 test-arm64-arm64-xl-vhd  15 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-credit1  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-credit1  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-rtds 15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-rtds 16 saverestore-support-checkfail   never pass
 test-armhf-armhf-libvirt 15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-credit2  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-credit2  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl-cubietruck 15 migrate-support-checkfail never pass
 test-armhf-armhf-xl-cubietruck 16 saverestore-support-checkfail never pass
 test-armhf-armhf-libvirt-qcow2 14 migrate-support-checkfail never pass
 test-armhf-armhf-xl-multivcpu 15 migrate-support-checkfail  never pass
 test-armhf-armhf-xl-multivcpu 16 saverestore-support-checkfail  never pass
 test-armhf-armhf-xl-arndale  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-arndale  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-xl  15 migrate-support-checkfail   never pass
 test-armhf-armhf-xl  16 saverestore-support-checkfail   never pass
 test-armhf-armhf-libvirt-raw 14 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-vhd  14 migrate-support-checkfail   never pass
 test-armhf-armhf-xl-vhd  15 saverestore-support-checkfail   never pass

version targeted for testing:
 linuxa08b41ab9e2e468647f78eb17c28e29b93006394
baseline version:
 linuxdd81e1c7d5fb126e5fbc5c9e334d7b3ec29a16a0

Last test of basis   167801  2022-01-23 22:40:37 Z1 days
Testing same since   167810  2022-01-25 06:40:10 Z0 days1 attempts


People who touched revisions under test:
  Ard Biesheuvel 
  huangshaobo 
  Linus Torvalds 
  Russell King (Oracle) 
  sparkhuang 

jobs:
 build-amd64-xsm  

Re: [PATCH] libxl: force netback to wait for hotplug execution before connecting

2022-01-25 Thread Durrant, Paul

On 24/01/2022 16:02, Roger Pau Monne wrote:

By writing an empty "hotplug-status" xenstore node in the backend path
libxl can force Linux netback to wait for hotplug script execution
before proceeding to the 'connected' state.

This is required so that netback doesn't skip state 2 (InitWait) and
thus blocks libxl waiting for such state in order to launch the
hotplug script (see libxl__wait_device_connection).

Reported-by: James Dingwall 
Signed-off-by: Roger Pau Monné 
Tested-by: James Dingwall 
---
Cc: Wei Liu 
Cc: Paul Durrant 


Reviewed-by: Paul Durrant 


---
  tools/libs/light/libxl_nic.c | 7 +++
  1 file changed, 7 insertions(+)

diff --git a/tools/libs/light/libxl_nic.c b/tools/libs/light/libxl_nic.c
index 0b45469dca..0b9e70c9d1 100644
--- a/tools/libs/light/libxl_nic.c
+++ b/tools/libs/light/libxl_nic.c
@@ -248,6 +248,13 @@ static int libxl__set_xenstore_nic(libxl__gc *gc, uint32_t 
domid,
  flexarray_append(ro_front, "mtu");
  flexarray_append(ro_front, GCSPRINTF("%u", nic->mtu));
  
+/*

+ * Force backend to wait for hotplug script execution before switching to
+ * connected state.
+ */
+flexarray_append(back, "hotplug-status");
+flexarray_append(back, "");
+
  return 0;
  }
  





Re: [PATCH v2] xen-mapcache: Avoid entry->lock overflow

2022-01-25 Thread Durrant, Paul

On 24/01/2022 10:44, Ross Lagerwall wrote:

In some cases, a particular mapcache entry may be mapped 256 times
causing the lock field to wrap to 0. For example, this may happen when
using emulated NVME and the guest submits a large scatter-gather write.
At this point, the entry map be remapped causing QEMU to write the wrong
data or crash (since remap is not atomic).

Avoid this overflow by increasing the lock field to a uint32_t and also
detect it and abort rather than continuing regardless.

Signed-off-by: Ross Lagerwall 


Reviewed-by: Paul Durrant 


---
Changes in v2: Change type to uint32_t since there is a hole there
anyway. The struct size remains at 48 bytes on x86_64.

  hw/i386/xen/xen-mapcache.c | 8 +++-
  1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
index bd47c3d672..f2ef977963 100644
--- a/hw/i386/xen/xen-mapcache.c
+++ b/hw/i386/xen/xen-mapcache.c
@@ -52,7 +52,7 @@ typedef struct MapCacheEntry {
  hwaddr paddr_index;
  uint8_t *vaddr_base;
  unsigned long *valid_mapping;
-uint8_t lock;
+uint32_t lock;
  #define XEN_MAPCACHE_ENTRY_DUMMY (1 << 0)
  uint8_t flags;
  hwaddr size;
@@ -355,6 +355,12 @@ tryagain:
  if (lock) {
  MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
  entry->lock++;
+if (entry->lock == 0) {
+fprintf(stderr,
+"mapcache entry lock overflow: "TARGET_FMT_plx" -> %p\n",
+entry->paddr_index, entry->vaddr_base);
+abort();
+}
  reventry->dma = dma;
  reventry->vaddr_req = mapcache->last_entry->vaddr_base + 
address_offset;
  reventry->paddr_index = mapcache->last_entry->paddr_index;





Re: [PATCH RFC v2 4/5] x86/mwait-idle: enable interrupts before C1 on Xeons

2022-01-25 Thread Roger Pau Monné
On Mon, Jan 24, 2022 at 03:44:53PM +0100, Jan Beulich wrote:
> On 20.01.2022 16:52, Roger Pau Monné wrote:
> > On Thu, Jan 20, 2022 at 03:04:39PM +0100, Jan Beulich wrote:
> >> From: Artem Bityutskiy 
> >> Unlike Linux we want to disable IRQs again after MWAITing, as
> >> subsequently invoked functions assume so.
> > 
> > I'm also wondering whether there could be interrupts that rely on some
> > of the housekeeping that's done when returning from mwait. I guess
> > it's unlikely for an interrupt handler to have anything to do with the
> > TSC not having been restored.
> 
> Actually this is a good point you make: We don't want to enable
> IRQs when cstate_restore_tsc() is not a no-op, or else we might
> confuse the time rendezvous. (I thought that I would remember
> TSC to stop only in deeper C-states, but maybe I'm mixing this up
> with the APIC timer.)

There's a comment in time.c that mentions the TSC only stopping in
'deep C states'. Also note that in that case the rendezvous function
already updates the TSC, so I'm not sure whether calling it with an
out of date TSC would be harmful - it will be updated anyway to match
the master TSC.

Might be safer to disable interrupts unconditionally on CPUs that
don't have a non-stop TSC just to be on the safe side.

Thanks, Roger.



Re: [PATCH] x86/time: minor adjustments to init_pit()

2022-01-25 Thread Roger Pau Monné
On Thu, Jan 20, 2022 at 04:17:37PM +, Andrew Cooper wrote:
> On 17/01/2022 10:36, Jan Beulich wrote:
> > For one, "using_pit" shouldn't be set ahead of the function's last
> > (for now: only) error path. Otherwise "clocksource=pit" on the command
> > line can lead to misbehavior when actually taking that error path.
> >
> > And then make an implicit assumption explicit: CALIBRATE_FRAC cannot,
> > for example, simply be changed to 10. The way init_pit() works, the
> > upper bound on the calibration period is about 54ms.
> >
> > Signed-off-by: Jan Beulich 
> 
> Acked-by: Andrew Cooper 
> 
> > ---
> > Really I've noticed this while considering what would happen if someone
> > specified  "clocksource=pit" on the shim's command line. Unlike "hpet"
> > and "acpi", "pit" presently wouldn't be (explicitly) ignored. While,
> > aiui, right now the only error path would be taken (due to port 0x61
> > reads being supposed to get back 0xff), I don't think we can build on
> > that longer term: Seeing what we use port 0x61 for in traps.c, I think
> > sooner or later we will need to have some form of emulation for it. Such
> > emulation is then not unlikely to continuously report 0 in the bit in
> > question. That would leed to an infinite loop here.
> 
> If we're not already doing it, pv shim really ought to set the FADT
> hardware reduced bits.  There should be no need to depend on heuristics
> around ~0.

We have talked about setting the hardware reduced flag for PVH,
it's however tricky. For once hardware-reduced ACPI mandates the usage
of UEFI firmware, which we don't yet fully support for PVH.

And then we cannot set that flag for a PVH dom0, because we use a mix
of crafted and native ACPI tables, so we risk that setting the bit
creates incompatibilities with the native tables we expose.

Roger.



Xen Security Advisory 395 v2 (CVE-2022-23035) - Insufficient cleanup of passed-through device IRQs

2022-01-25 Thread Xen . org security team
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Xen Security Advisory CVE-2022-23035 / XSA-395
   version 2

  Insufficient cleanup of passed-through device IRQs

UPDATES IN VERSION 2


Adjust patch subject.

Public release.

ISSUE DESCRIPTION
=

The management of IRQs associated with physical devices exposed to x86
HVM guests involves an iterative operation in particular when cleaning
up after the guest's use of the device.  In the case where an interrupt
is not quiescent yet at the time this cleanup gets invoked, the cleanup
attempt may be scheduled to be retried.  When multiple interrupts are
involved, this scheduling of a retry may get erroneously skipped.  At
the same time pointers may get cleared (resulting in a de-reference of
NULL) and freed (resulting in a use-after-free), while other code would
continue to assume them to be valid.

IMPACT
==

The precise impact is system specific, but would typically be a Denial
of Service (DoS) affecting the entire host.  Privilege escalation and
information leaks cannot be ruled out.

VULNERABLE SYSTEMS
==

Xen versions 4.6 and later are vulnerable.  Xen versions 4.5 and earlier
are not vulnerable.

Only x86 HVM guests with one or more passed-through physical devices
using (together) multiple physical interupts can leverage the
vulnerability.  x86 PV guests cannot leverage the vulnerability.  x86
HVM guests without passed-through devices or with a passed-through
device using just a single physical interrupt also cannot leverage the
vulnerability.  Device pass-through is unsupported for x86 PVH guests
and all Arm guests.

MITIGATION
==

There is no mitigation (other than not passing through to x86 HVM guests
PCI devices with, overall, more than a single physical interrupt).

CREDITS
===

This issue was discovered by Julien Grall of Amazon.

RESOLUTION
==

Applying the appropriate attached patch resolves this issue.

Note that patches for released versions are generally prepared to
apply to the stable branches, and may not apply cleanly to the most
recent release tarball.  Downstreams are encouraged to update to the
tip of the stable branch before applying these patches.

xsa395.patch   xen-unstable - Xen 4.15.x
xsa395-4.14.patch  Xen 4.14.x - Xen 4.12.x

$ sha256sum xsa395*
f460be598b936bb5cfb9276787f2f21d90b029d1fe10dabd572ae50f84a1124d  xsa395.meta
295b876c52cf5efe19150757275da3d154beb72ac2d7be267e16c9262e410de3  xsa395.patch
5697f3137e0a202744f31b1c6cbcfa459d8fa9b4b68be59561b78c40fe1233c5  
xsa395-4.14.patch
$

DEPLOYMENT DURING EMBARGO
=

Deployment of the patches and/or mitigations described above (or
others which are substantially similar) is permitted during the
embargo, even on public-facing systems with untrusted guest users and
administrators.

But: Distribution of updated software is prohibited (except to other
members of the predisclosure list).

Predisclosure list members who wish to deploy significantly different
patches and/or mitigations, please contact the Xen Project Security
Team.

(Note: this during-embargo deployment notice is retained in
post-embargo publicly released Xen Project advisories, even though it
is then no longer applicable.  This is to enable the community to have
oversight of the Xen Project Security Team's decisionmaking.)

For more information about permissible uses of embargoed information,
consult the Xen Project community's agreed Security Policy:
  http://www.xenproject.org/security-policy.html
-BEGIN PGP SIGNATURE-

iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAmHv39QMHHBncEB4ZW4u
b3JnAAoJEIP+FMlX6CvZhowIAIZYZq4efyEAP5rB3zX4yRel2GNz+2Dpjok4PExB
uSOrPaH5dDILhNdVJNG48MckDe0dMDsn3OGr1I6lbxcV1TWR1JFrBQoxeUnwdiEf
GjeTni0hhefan3IEEd5HUDInQgf9oI7fUcgEdVAoIV87BQdlK0ofjJ3TggSrr8jl
pL5dmIh4OICD6YttR11Of1vhPY2WhZQb2xgSxzEQbDeY8k3JaRWy8mYwwxPD0HXn
+hmLK59ZhkJd5Sk8AxttRUTEsl6nKESrUz3vv/vFInV5Go+35AElL//gQNgOOTAS
nljLLtJdfHSuRy459Sw/lm4mwQ9zkfOFH6B+M6efSkHMyoE=
=Iv+w
-END PGP SIGNATURE-


xsa395.meta
Description: Binary data


xsa395.patch
Description: Binary data


xsa395-4.14.patch
Description: Binary data


Xen Security Advisory 394 v3 (CVE-2022-23034) - A PV guest could DoS Xen while unmapping a grant

2022-01-25 Thread Xen . org security team
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Xen Security Advisory CVE-2022-23034 / XSA-394
   version 3

   A PV guest could DoS Xen while unmapping a grant

UPDATES IN VERSION 3


Public release.

ISSUE DESCRIPTION
=

To address XSA-380, reference counting was introduced for grant
mappings for the case where a PV guest would have the IOMMU enabled. PV
guests can request two forms of mappings.  When both are in use for any
individual mapping, unmapping of such a mapping can be requested in two
steps.  The reference count for such a mapping would then mistakenly be
decremented twice.  Underflow of the counters gets detected, resulting
in the triggering of a hypervisor bug check.

IMPACT
==

Malicious guest kernels may be able to mount a Denial of Service (DoS)
attack affecting the entire system.

VULNERABLE SYSTEMS
==

All Xen versions from at least 3.2 onwards are vulnerable in principle,
if they have the XSA-380 fixes applied.

Only x86 systems are vulnerable.  Arm systems are not vulnerable.

Only x86 PV guests with access to PCI devices can leverage the
vulnerability.  x86 HVM and PVH guests, as well as PV guests without
access to PCI devices, cannot leverage the vulnerability.

Additionally from Xen 4.13 onwards x86 PV guests can leverage this
vulnerability only when being granted access to pages owned by another
domain.

MITIGATION
==

Not running PV guests will avoid the vulnerability.

For Xen 4.12 and older not passing through PCI devices to PV guests will
avoid the vulnerability.

For Xen 4.13 and newer not enabling PCI device pass-through for PV
guests will avoid the vulnerability.  This can be achieved via omitting
any "passthrough=..." and "pci=..." settings from xl guest configuration
files, or by setting "passthrough=disabled" there.

- From Xen 4.13 onwards, XSM SILO can be available as a security policy
designed to permit guests to only be able to communicate with Dom0.
Dom0 does not normally offer its pages for guests to map, which means
the use of SILO mode normally mitigates the vulnerability.

CREDITS
===

This issue was discovered by Julien Grall of Amazon.

RESOLUTION
==

Applying the appropriate attached patch resolves this issue.

Note that patches for released versions are generally prepared to
apply to the stable branches, and may not apply cleanly to the most
recent release tarball.  Downstreams are encouraged to update to the
tip of the stable branch before applying these patches.

xsa394.patch   xen-unstable - Xen 4.13.x
xsa394-4.12.patch  Xen 4.12.x

$ sha256sum xsa394*
93f4d3b58d49ba239115753c9905b7c3720b438c48ef8fb701f15081aa317159  xsa394.meta
f2a3420e8d3eb1cf728f90d3c352ace0d3c67f7933201ce9b784d63afaeaa179  xsa394.patch
ee93797546ac9e82f98211366f9acc72b0d5ab7ef73840c2acd2bb1439ca  
xsa394-4.12.patch
$

DEPLOYMENT DURING EMBARGO
=

Deployment of the patches described above (or others which are
substantially similar) is permitted during the embargo, even on public-
facing systems with untrusted guest users and administrators.

HOWEVER, deployment of the mitigations described above is NOT permitted
during the embargo on public-facing systems with untrusted guest users
and administrators.  This is because such a configuration change is
recognizable by the affected guests.

AND: Distribution of updated software is prohibited (except to other
members of the predisclosure list).

Predisclosure list members who wish to deploy significantly different
patches and/or mitigations, please contact the Xen Project Security
Team.

(Note: this during-embargo deployment notice is retained in
post-embargo publicly released Xen Project advisories, even though it
is then no longer applicable.  This is to enable the community to have
oversight of the Xen Project Security Team's decisionmaking.)

For more information about permissible uses of embargoed information,
consult the Xen Project community's agreed Security Policy:
  http://www.xenproject.org/security-policy.html
-BEGIN PGP SIGNATURE-

iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAmHv39IMHHBncEB4ZW4u
b3JnAAoJEIP+FMlX6CvZfCYH/iZn73/JRTKI7B+9v2fW6v/k1IcVhpu+N4+TuRhh
Al5igmiTJLU3LcHM/H2KScgtnSwEKfCyddY1Gt3MZ+5lBDwR8elRkPdqn+P7xfol
4D5NgnEJDAYUWwJZOFn0qWfqNDnDkAvuKpm1zmv8RE0Xmw6a74Fvbfvi8PCuN9CO
zdippi5r5FlzFU7Q5MoWmOhmvVe3Fg7tGs4GXIyVUYkpDYyBGEWBo6rcoQ5aDvir
g8T0P1Y8XKCVvYM9SOdKWENppam0uIh00Mm+QDjQNaXD4I3DCDXLXkT7OGImZglr
MW8z5iNFjd0iXxFqTVBe1omxUhLC1xcB1fNySjd3zpt3RfA=
=mIA+
-END PGP SIGNATURE-


xsa394.meta
Description: Binary data


xsa394.patch
Description: Binary data


xsa394-4.12.patch
Description: Binary data


Xen Security Advisory 393 v2 (CVE-2022-23033) - arm: guest_physmap_remove_page not removing the p2m mappings

2022-01-25 Thread Xen . org security team
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Xen Security Advisory CVE-2022-23033 / XSA-393
   version 2

 arm: guest_physmap_remove_page not removing the p2m mappings

UPDATES IN VERSION 2


Public release.

ISSUE DESCRIPTION
=

The functions to remove one or more entries from a guest p2m pagetable
on Arm (p2m_remove_mapping, guest_physmap_remove_page, and p2m_set_entry
with mfn set to INVALID_MFN) do not actually clear the pagetable entry
if the entry doesn't have the valid bit set.  It is possible to have a
valid pagetable entry without the valid bit set when a guest operating
system uses set/way cache maintenance instructions.  For instance, a
guest issuing a set/way cache maintenance instruction, then calling the
XENMEM_decrease_reservation hypercall to give back memory pages to Xen,
might be able to retain access to those pages even after Xen started
reusing them for other purposes.

IMPACT
==

A malicious guest may be able to access Xen and other domains' memory.
This could cause information leaks, host or domain Denial of Service
(DoS), and privilege escalations.

VULNERABLE SYSTEMS
==

Xen version 4.12 and newer are vulnerable.  Only Arm systems are
vulnerable.

x86 systems are not vulnerable.

MITIGATION
==

There is no known mitigation.

CREDITS
===

This issue was discovered by Dmytro Firsov of EPAM.

RESOLUTION
==

Applying the appropriate attached patch resolves this issue.

Note that patches for released versions are generally prepared to
apply to the stable branches, and may not apply cleanly to the most
recent release tarball.  Downstreams are encouraged to update to the
tip of the stable branch before applying these patches.

xsa393.patch   xen-unstable - Xen 4.12.x

$ sha256sum xsa393*
ccd746687c6080ec00ba363477d8815bc648d957c21c47d3a5330be9251806a4  xsa393.meta
89e5d66c437bacbe344e72d15720c1dde98dd97fab7184c7a6ff32bb63d442dd  xsa393.patch
$

DEPLOYMENT DURING EMBARGO
=

Deployment of the patches and/or mitigations described above (or
others which are substantially similar) is permitted during the
embargo, even on public-facing systems with untrusted guest users and
administrators.

But: Distribution of updated software is prohibited (except to other
members of the predisclosure list).

Predisclosure list members who wish to deploy significantly different
patches and/or mitigations, please contact the Xen Project Security
Team.

(Note: this during-embargo deployment notice is retained in
post-embargo publicly released Xen Project advisories, even though it
is then no longer applicable.  This is to enable the community to have
oversight of the Xen Project Security Team's decisionmaking.)

For more information about permissible uses of embargoed information,
consult the Xen Project community's agreed Security Policy:
  http://www.xenproject.org/security-policy.html
-BEGIN PGP SIGNATURE-

iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAmHv38oMHHBncEB4ZW4u
b3JnAAoJEIP+FMlX6CvZfAcH/iXwGyTpGU7AIOGNGH1VYnn3FBAVBvT4etuPXO8o
heX252xCZNh7M7qel/Db1aaAMpo2T2ypH02ZguKsojnoRAo4QrEjrnBGsCasfzqv
HFd3nMlmksNlKI9xGPxt+Q6eNuoEHgu7i/7r3J2DgiC/Pa5Hw4SMF2eat7Er5zDL
waDHFkiONa6LM/dtgZkkgps5d3B8cR4tXo3VDLzBC0pK3IysSLnacLy7FfvLg7c0
pc/qFvUXbsFjKVmG+EKu8VlCpkWONFP1FXC4pfM+rSjDdVhmc8FhFzOLzD6Tkptt
MJhgOCMrO1Z//F07l0B9C9sxVi7K5mUDSWhonUQVPCWgl2s=
=06Nb
-END PGP SIGNATURE-


xsa393.meta
Description: Binary data


xsa393.patch
Description: Binary data


Re: [XEN PATCH v9 30/30] build: adding out-of-tree support to the xen build

2022-01-25 Thread Anthony PERARD
On Tue, Jan 25, 2022 at 11:01:03AM +, Anthony PERARD wrote:
> This implement out-of-tree support, there's two ways to create an
> out-of-tree build tree (after that, `make` in that new directory
> works):
> make O=build
> mkdir build; cd build; make -f ../Makefile
> also works with an absolute path for both.

Maybe I should have made it more explicit in the description that those
only works within the xen dir, i.e. not the whole project.

-- 
Anthony PERARD



[XEN PATCH v9 19/30] build: clean-up "clean" rules of duplication

2022-01-25 Thread Anthony PERARD
All those files to be removed are already done in the main Makefile,
either by the "find" command or directly (for $(TARGET).efi).

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
Acked-by: Julien Grall 
---
 xen/Makefile| 2 +-
 xen/arch/arm/Makefile   | 1 -
 xen/arch/x86/Makefile   | 5 ++---
 xen/test/livepatch/Makefile | 2 +-
 xen/xsm/flask/Makefile  | 2 +-
 5 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index 65b09c20a599..b2df072d2a62 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -425,7 +425,7 @@ _clean:
-o -name ".*.o.tmp" -o -name "*~" -o -name "core" \
-o -name '*.lex.c' -o -name '*.tab.[ch]' \
-o -name "*.gcno" -o -name ".*.cmd" -o -name "lib.a" \) -exec 
rm -f {} \;
-   rm -f include/asm $(TARGET) $(TARGET).gz $(TARGET).efi 
$(TARGET).efi.map $(TARGET)-syms $(TARGET)-syms.map *~ core
+   rm -f include/asm $(TARGET) $(TARGET).gz $(TARGET).efi 
$(TARGET).efi.map $(TARGET)-syms $(TARGET)-syms.map
rm -f asm-offsets.s arch/*/include/asm/asm-offsets.h
rm -f .banner .allconfig.tmp include/xen/compile.h
 
diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile
index fd24f0212ffa..3ce5f1674f6f 100644
--- a/xen/arch/arm/Makefile
+++ b/xen/arch/arm/Makefile
@@ -120,4 +120,3 @@ $(obj)/dtb.o: $(patsubst "%",%,$(CONFIG_DTB_FILE))
 clean::
rm -f $(obj)/xen.lds
rm -f $(BASEDIR)/.xen-syms.[0-9]*
-   rm -f $(TARGET).efi
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index de7f7ce07b98..60e802ba9503 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -258,9 +258,8 @@ $(obj)/efi/mkreloc: $(src)/efi/mkreloc.c
 
 .PHONY: clean
 clean::
-   rm -f *.lds boot/*.o boot/*~ boot/core boot/mkelf32
+   rm -f *.lds boot/mkelf32
rm -f asm-macros.i $(BASEDIR)/arch/x86/include/asm/asm-macros.*
-   rm -f $(BASEDIR)/.xen-syms.[0-9]* boot/.*.d $(BASEDIR)/.xen.elf32
+   rm -f $(BASEDIR)/.xen-syms.[0-9]* $(BASEDIR)/.xen.elf32
rm -f $(BASEDIR)/.xen.efi.[0-9]* efi/*.efi efi/mkreloc
rm -f boot/cmdline.S boot/reloc.S boot/*.lnk boot/*.bin
-   rm -f note.o
diff --git a/xen/test/livepatch/Makefile b/xen/test/livepatch/Makefile
index afb8d589ecae..adb484dc5d2c 100644
--- a/xen/test/livepatch/Makefile
+++ b/xen/test/livepatch/Makefile
@@ -162,4 +162,4 @@ uninstall:
 
 .PHONY: clean
 clean::
-   rm -f *.o .*.o.d *.livepatch config.h expect_config.h
+   rm -f *.livepatch config.h expect_config.h
diff --git a/xen/xsm/flask/Makefile b/xen/xsm/flask/Makefile
index 49cf730cf098..832f65274cc0 100644
--- a/xen/xsm/flask/Makefile
+++ b/xen/xsm/flask/Makefile
@@ -51,4 +51,4 @@ $(obj)/policy.bin: FORCE
 
 .PHONY: clean
 clean::
-   rm -f $(ALL_H_FILES) *.o $(DEPS_RM) policy.* $(POLICY_SRC) 
flask-policy.S
+   rm -f $(ALL_H_FILES) policy.* $(POLICY_SRC) flask-policy.S
-- 
Anthony PERARD




[XEN PATCH v9 21/30] build: use main rune to build host binary x86's mkelf32 and mkreloc

2022-01-25 Thread Anthony PERARD
Also, remove the HOSTCFLAGS "-g" from "mkreloc" command line.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---

Notes:
v8:
- acked
- remove the cflags "-g" from "mkreloc"

 xen/arch/x86/Makefile | 13 -
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index bb57ed5096d5..c94b4092d4c1 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -83,6 +83,9 @@ subdir- += boot
 extra-y += asm-macros.i
 extra-y += xen.lds
 
+hostprogs-y += boot/mkelf32
+hostprogs-y += efi/mkreloc
+
 # Allows usercopy.c to include itself
 $(obj)/usercopy.o: CFLAGS-y += -iquote .
 
@@ -253,17 +256,9 @@ $(obj)/efi.lds: AFLAGS-y += -DEFI
 $(obj)/xen.lds $(obj)/efi.lds: $(src)/xen.lds.S FORCE
$(call if_changed_dep,cpp_lds_S)
 
-$(obj)/boot/mkelf32: $(src)/boot/mkelf32.c
-   $(HOSTCC) $(HOSTCFLAGS) -o $@ $<
-
-$(obj)/efi/mkreloc: $(src)/efi/mkreloc.c
-   $(HOSTCC) $(HOSTCFLAGS) -g -o $@ $<
-
 clean-files := \
-boot/mkelf32 \
 include/asm/asm-macros.* \
 $(objtree)/.xen-syms.[0-9]* \
 $(objtree)/.xen.elf32 \
 $(objtree)/.xen.efi.[0-9]* \
-efi/*.efi \
-efi/mkreloc
+efi/*.efi
-- 
Anthony PERARD




[XEN PATCH v9 25/30] build: replace $(BASEDIR) by $(objtree)

2022-01-25 Thread Anthony PERARD
We need to differentiate between source files and generated/built
files. We will be replacing $(BASEDIR) by $(objtree) for files that
are generated.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---

Notes:
v9:
- acked

v8:
- rebased

 xen/Rules.mk|  2 +-
 xen/arch/arm/Makefile   | 10 +-
 xen/arch/x86/Makefile   | 28 ++--
 xen/common/Makefile |  2 +-
 xen/test/livepatch/Makefile | 12 ++--
 5 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/xen/Rules.mk b/xen/Rules.mk
index abeba1ab7494..7712bfa063e0 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -12,7 +12,7 @@ src := $(obj)
 PHONY := __build
 __build:
 
--include $(BASEDIR)/include/config/auto.conf
+-include $(objtree)/include/config/auto.conf
 
 include $(XEN_ROOT)/Config.mk
 include $(BASEDIR)/scripts/Kbuild.include
diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile
index cecfaf4f3c0f..ae7a2f907540 100644
--- a/xen/arch/arm/Makefile
+++ b/xen/arch/arm/Makefile
@@ -90,21 +90,21 @@ ifeq ($(CONFIG_ARM_64),y)
ln -sf $(@F) $@.efi
 endif
 
-$(TARGET)-syms: $(BASEDIR)/prelink.o $(obj)/xen.lds
+$(TARGET)-syms: $(objtree)/prelink.o $(obj)/xen.lds
$(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< \
-   $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
+   $(objtree)/common/symbols-dummy.o -o $(@D)/.$(@F).0
$(NM) -pa --format=sysv $(@D)/.$(@F).0 \
-   | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort 
>$(@D)/.$(@F).0.S
+   | $(objtree)/tools/symbols $(all_symbols) --sysv --sort 
>$(@D)/.$(@F).0.S
$(MAKE) $(build)=$(@D) $(@D)/.$(@F).0.o
$(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< \
$(@D)/.$(@F).0.o -o $(@D)/.$(@F).1
$(NM) -pa --format=sysv $(@D)/.$(@F).1 \
-   | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort 
>$(@D)/.$(@F).1.S
+   | $(objtree)/tools/symbols $(all_symbols) --sysv --sort 
>$(@D)/.$(@F).1.S
$(MAKE) $(build)=$(@D) $(@D)/.$(@F).1.o
$(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< $(build_id_linker) \
$(@D)/.$(@F).1.o -o $@
$(NM) -pa --format=sysv $(@D)/$(@F) \
-   | $(BASEDIR)/tools/symbols --all-symbols --xensyms --sysv 
--sort \
+   | $(objtree)/tools/symbols --all-symbols --xensyms --sysv 
--sort \
>$(@D)/$(@F).map
rm -f $(@D)/.$(@F).[0-9]*
 
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index a847c989ff92..3fb59987776b 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -94,8 +94,8 @@ ifneq ($(CONFIG_HVM),y)
 $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
 endif
 
-efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \
-  -O $(BASEDIR)/include/xen/compile.h ]; then \
+efi-y := $(shell if [ ! -r $(objtree)/include/xen/compile.h -o \
+  -O $(objtree)/include/xen/compile.h ]; then \
  echo '$(TARGET).efi'; fi) \
  $(space)
 efi-$(CONFIG_PV_SHIM_EXCLUSIVE) :=
@@ -133,23 +133,23 @@ $(TARGET): $(TARGET)-syms $(efi-y) $(obj)/boot/mkelf32
 
 CFLAGS-$(XEN_BUILD_EFI) += -DXEN_BUILD_EFI
 
-$(TARGET)-syms: $(BASEDIR)/prelink.o $(obj)/xen.lds
+$(TARGET)-syms: $(objtree)/prelink.o $(obj)/xen.lds
$(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< $(build_id_linker) \
-   $(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
+   $(objtree)/common/symbols-dummy.o -o $(@D)/.$(@F).0
$(NM) -pa --format=sysv $(@D)/.$(@F).0 \
-   | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort \
+   | $(objtree)/tools/symbols $(all_symbols) --sysv --sort \
>$(@D)/.$(@F).0.S
$(MAKE) $(build)=$(@D) $(@D)/.$(@F).0.o
$(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< $(build_id_linker) \
$(@D)/.$(@F).0.o -o $(@D)/.$(@F).1
$(NM) -pa --format=sysv $(@D)/.$(@F).1 \
-   | $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort 
$(syms-warn-dup-y) \
+   | $(objtree)/tools/symbols $(all_symbols) --sysv --sort 
$(syms-warn-dup-y) \
>$(@D)/.$(@F).1.S
$(MAKE) $(build)=$(@D) $(@D)/.$(@F).1.o
$(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< $(build_id_linker) \
$(@D)/.$(@F).1.o -o $@
$(NM) -pa --format=sysv $(@D)/$(@F) \
-   | $(BASEDIR)/tools/symbols --all-symbols --xensyms --sysv 
--sort \
+   | $(objtree)/tools/symbols --all-symbols --xensyms --sysv 
--sort \
>$(@D)/$(@F).map
rm -f $(@D)/.$(@F).[0-9]* $(@D)/..$(@F).[0-9]*
 
@@ -199,28 +199,28 @@ note_file_option ?= $(note_file)
 
 extra-$(XEN_BUILD_PE) += efi.lds
 ifeq ($(XEN_BUILD_PE),y)
-$(TARGET).efi: $(BASEDIR)/prelink.o $(note_file) $(obj)/efi.lds 
$(obj)/efi/relocs-dummy.o $(obj)/efi/mkreloc
+$(TARGET).efi: $(objtree)/prelink.o $(note_file) $(obj)/efi.lds 
$(obj)/efi/relocs-dummy.o $(

[XEN PATCH v9 20/30] build: rework "clean" to clean from the root dir

2022-01-25 Thread Anthony PERARD
This will allow "clean" to work from an out-of-tree build when
it will be available.

Some of the file been removed in current "clean" target aren't added
to $(clean-files) because they are already listed in $(extra-) or
$(extra-y).

Also start to clean files listed in $(targets). This allows to clean
"common/config_data.S" and "xsm/flask/flask-policy.S" without
having to list them a second time.

Also clean files in "arch/x86/boot" from that directory by allowing
"clean" to descend into the subdir by adding "boot" into $(subdir-).

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
---

Notes:
v9:
- reviewed

v8:
- fix comment for the shorthand
- move this patch earlier in the series
- replace some BASEDIR by objtree
- start to clean $(targets) files.

 xen/Makefile| 24 
 xen/arch/arm/Makefile   |  5 +
 xen/arch/x86/Makefile   | 20 
 xen/arch/x86/boot/Makefile  |  2 ++
 xen/common/Makefile |  3 +--
 xen/include/Makefile|  4 +---
 xen/scripts/Kbuild.include  |  4 ++--
 xen/scripts/Makefile.clean  | 14 +++---
 xen/test/livepatch/Makefile |  4 +---
 xen/xsm/flask/Makefile  |  4 +---
 10 files changed, 44 insertions(+), 40 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index b2df072d2a62..dc25fa443b82 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -409,18 +409,18 @@ _debug:
 
 .PHONY: _clean
 _clean:
-   $(MAKE) $(clean) tools
-   $(MAKE) $(clean) include
-   $(MAKE) $(clean) common
-   $(MAKE) $(clean) drivers
-   $(MAKE) $(clean) lib
-   $(MAKE) $(clean) xsm
-   $(MAKE) $(clean) crypto
-   $(MAKE) $(clean) arch/arm
-   $(MAKE) $(clean) arch/riscv
-   $(MAKE) $(clean) arch/x86
-   $(MAKE) $(clean) test
-   $(MAKE) $(clean) tools/kconfig
+   $(Q)$(MAKE) $(clean)=tools
+   $(Q)$(MAKE) $(clean)=include
+   $(Q)$(MAKE) $(clean)=common
+   $(Q)$(MAKE) $(clean)=drivers
+   $(Q)$(MAKE) $(clean)=lib
+   $(Q)$(MAKE) $(clean)=xsm
+   $(Q)$(MAKE) $(clean)=crypto
+   $(Q)$(MAKE) $(clean)=arch/arm
+   $(Q)$(MAKE) $(clean)=arch/riscv
+   $(Q)$(MAKE) $(clean)=arch/x86
+   $(Q)$(MAKE) $(clean)=test
+   $(Q)$(MAKE) $(clean)=tools/kconfig
find . \( -name "*.o" -o -name ".*.d" -o -name ".*.d2" \
-o -name ".*.o.tmp" -o -name "*~" -o -name "core" \
-o -name '*.lex.c' -o -name '*.tab.[ch]' \
diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile
index 3ce5f1674f6f..cecfaf4f3c0f 100644
--- a/xen/arch/arm/Makefile
+++ b/xen/arch/arm/Makefile
@@ -116,7 +116,4 @@ $(obj)/xen.lds: $(src)/xen.lds.S FORCE
 
 $(obj)/dtb.o: $(patsubst "%",%,$(CONFIG_DTB_FILE))
 
-.PHONY: clean
-clean::
-   rm -f $(obj)/xen.lds
-   rm -f $(BASEDIR)/.xen-syms.[0-9]*
+clean-files := $(objtree)/.xen-syms.[0-9]*
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 60e802ba9503..bb57ed5096d5 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -77,6 +77,9 @@ obj-$(CONFIG_COMPAT) += x86_64/platform_hypercall.o
 obj-y += sysctl.o
 endif
 
+# Allows "clean" to descend into boot/
+subdir- += boot
+
 extra-y += asm-macros.i
 extra-y += xen.lds
 
@@ -190,8 +193,8 @@ note_file :=
 endif
 note_file_option ?= $(note_file)
 
+extra-$(XEN_BUILD_PE) += efi.lds
 ifeq ($(XEN_BUILD_PE),y)
-extra-y += efi.lds
 $(TARGET).efi: $(BASEDIR)/prelink.o $(note_file) $(obj)/efi.lds 
$(obj)/efi/relocs-dummy.o $(obj)/efi/mkreloc
 ifeq ($(CONFIG_DEBUG_INFO),y)
$(if $(filter --strip-debug,$(EFI_LDFLAGS)),echo,:) "Will strip debug 
info from $(@F)"
@@ -256,10 +259,11 @@ $(obj)/boot/mkelf32: $(src)/boot/mkelf32.c
 $(obj)/efi/mkreloc: $(src)/efi/mkreloc.c
$(HOSTCC) $(HOSTCFLAGS) -g -o $@ $<
 
-.PHONY: clean
-clean::
-   rm -f *.lds boot/mkelf32
-   rm -f asm-macros.i $(BASEDIR)/arch/x86/include/asm/asm-macros.*
-   rm -f $(BASEDIR)/.xen-syms.[0-9]* $(BASEDIR)/.xen.elf32
-   rm -f $(BASEDIR)/.xen.efi.[0-9]* efi/*.efi efi/mkreloc
-   rm -f boot/cmdline.S boot/reloc.S boot/*.lnk boot/*.bin
+clean-files := \
+boot/mkelf32 \
+include/asm/asm-macros.* \
+$(objtree)/.xen-syms.[0-9]* \
+$(objtree)/.xen.elf32 \
+$(objtree)/.xen.efi.[0-9]* \
+efi/*.efi \
+efi/mkreloc
diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index ba732e4a88c3..1ac8cb435e0e 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -21,3 +21,5 @@ $(obj)/cmdline.S: $(src)/cmdline.c $(CMDLINE_DEPS) 
$(src)/build32.lds
 
 $(obj)/reloc.S: $(src)/reloc.c $(RELOC_DEPS) $(src)/build32.lds
$(MAKE) -f $(abs_srctree)/$(src)/build32.mk -C $(obj) $(@F) 
RELOC_DEPS="$(RELOC_DEPS)"
+
+clean-files := cmdline.S reloc.S *.lnk *.bin
diff --git a/xen/common/Makefile b/xen/common/Makefile
index ca839118e4d1..dc8d3a13f5b8 100644
--- a/xen/common/Makefile
+++ b/xen/common/Makefile
@@ -84,5 +84,4 @@ $(obj)/config_data.S: $(BASEDIR)

[XEN PATCH v9 27/30] build: rework "headers*.chk" prerequisite in include/

2022-01-25 Thread Anthony PERARD
Listing public headers when out-of-tree build are involved becomes
more annoying where every path to every headers needs to start with
"$(srctree)/$(src)", or $(wildcard ) will not work. This means more
repetition. ( "$(srcdir)" is a shortcut for "$(srctree)/$(src)" )

This patch attempt to reduce the amount of duplication and make better
use of make's meta programming capability. The filters are now listed
in a variable and don't have to repeat the path to the headers files
as this is added later as needed.

Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- reorder *-headers variables
- call public-filter-headers on $(public-c99-headers) as well, for this I
  needed to rework $(public-c99-headers) and $(public-ansi-headers-filter)

v8:
- add prefix "public-" to newly introduced macros.
- make use of the new "$(srcdir)" shortcut.

 xen/include/Makefile | 23 ++-
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/xen/include/Makefile b/xen/include/Makefile
index 5a2b4c9f65fa..fddf5a575bf6 100644
--- a/xen/include/Makefile
+++ b/xen/include/Makefile
@@ -39,8 +39,8 @@ cppflags-$(CONFIG_X86)+= -m32
 
 endif
 
-public-$(CONFIG_X86) := $(wildcard $(src)/public/arch-x86/*.h 
$(src)/public/arch-x86/*/*.h)
-public-$(CONFIG_ARM) := $(wildcard $(src)/public/arch-arm/*.h 
$(src)/public/arch-arm/*/*.h)
+public-$(CONFIG_X86) := $(wildcard $(srcdir)/public/arch-x86/*.h 
$(srcdir)/public/arch-x86/*/*.h)
+public-$(CONFIG_ARM) := $(wildcard $(srcdir)/public/arch-arm/*.h 
$(srcdir)/public/arch-arm/*/*.h)
 
 .PHONY: all
 all: $(addprefix $(obj)/,$(headers-y))
@@ -81,10 +81,23 @@ ifeq ($(XEN_TARGET_ARCH),$(XEN_COMPILE_ARCH))
 
 all: $(obj)/headers.chk $(obj)/headers99.chk $(obj)/headers++.chk
 
-PUBLIC_HEADERS := $(filter-out $(src)/public/arch-% $(src)/public/dom0_ops.h, 
$(wildcard $(src)/public/*.h $(src)/public/*/*.h) $(public-y))
+public-hdrs-path := $(srcdir)/public
 
-PUBLIC_C99_HEADERS := $(src)/public/io/9pfs.h $(src)/public/io/pvcalls.h
-PUBLIC_ANSI_HEADERS := $(filter-out $(src)/public/%ctl.h $(src)/public/xsm/% 
$(src)/public/%hvm/save.h $(PUBLIC_C99_HEADERS), $(PUBLIC_HEADERS))
+public-list-headers = $(wildcard $1/*.h $1/*/*.h)
+public-filter-headers = $(filter-out $(addprefix $(public-hdrs-path)/, 
$($1-filter)), $($1))
+
+public-headers := $(call public-list-headers, $(public-hdrs-path)) $(public-y)
+public-ansi-headers := $(public-headers)
+public-c99-headers := $(addprefix $(public-hdrs-path)/, io/9pfs.h io/pvcalls.h)
+
+public-headers-filter := dom0_ops.h arch-%
+public-ansi-headers-filter := %ctl.h xsm/% %hvm/save.h 
$(public-headers-filter) \
+$(patsubst $(public-hdrs-path)/%,%,$(public-c99-headers))
+public-c99-headers-filter :=
+
+PUBLIC_HEADERS := $(call public-filter-headers,public-headers)
+PUBLIC_ANSI_HEADERS := $(call public-filter-headers,public-ansi-headers)
+PUBLIC_C99_HEADERS := $(call public-filter-headers,public-c99-headers)
 
 $(src)/public/io/9pfs.h-prereq := string
 $(src)/public/io/pvcalls.h-prereq := string
-- 
Anthony PERARD




[XEN PATCH v9 29/30] build: shuffle main Makefile

2022-01-25 Thread Anthony PERARD
Reorganize a bit the Makefile ahead of patch
"build: adding out-of-tree support to the xen build"

We are going to want to calculate all the $(*srctree) and $(*objtree)
once, when we can calculate them. This can happen within the
"$(root-make-done)" guard, in an out-of-tree build scenario, so move
those variable there.

$(XEN_ROOT) is going to depends on the value of $(abs_srctree) so
needs to move as well. "Kbuild.include" also depends on $(srctree).

Next, "Config.mk" depends on $(XEN_ROOT) and $(TARGET_*ARCH) depends
on "Config.mk" so those needs to move as well.

This should only be code movement without functional changes.

Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- add some explanation in the commit message about why the code movement
  is needed.

v8:
- new patch

 xen/Makefile | 46 +++---
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index c39c1699966f..08114b3fef1e 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -19,17 +19,6 @@ export PYTHON?= $(PYTHON_INTERPRETER)
 
 export CHECKPOLICY ?= checkpolicy
 
-export XEN_ROOT := $(CURDIR)/..
-
-abs_objtree := $(CURDIR)
-abs_srctree := $(CURDIR)
-
-export abs_srctree abs_objtree
-
-srctree := .
-objtree := .
-export srctree objtree
-
 # Do not use make's built-in rules and variables
 MAKEFLAGS += -rR
 
@@ -41,16 +30,6 @@ SRCARCH=$(shell echo $(ARCH) | \
   -e s'/riscv.*/riscv/g')
 export ARCH SRCARCH
 
-# Don't break if the build process wasn't called from the top level
-# we need XEN_TARGET_ARCH to generate the proper config
-include $(XEN_ROOT)/Config.mk
-
-# Set ARCH/SUBARCH appropriately.
-export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
-export TARGET_ARCH := $(shell echo $(XEN_TARGET_ARCH) | \
-sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
--e s'/riscv.*/riscv/g')
-
 # Allow someone to change their config file
 export KCONFIG_CONFIG ?= .config
 
@@ -64,8 +43,6 @@ default: build
 .PHONY: dist
 dist: install
 
-include scripts/Kbuild.include
-
 ifneq ($(root-make-done),y)
 # section to run before calling Rules.mk, but only once.
 
@@ -141,6 +118,17 @@ endif
 
 export quiet Q KBUILD_VERBOSE
 
+abs_objtree := $(CURDIR)
+abs_srctree := $(CURDIR)
+
+export abs_srctree abs_objtree
+
+srctree := .
+objtree := .
+export srctree objtree
+
+export XEN_ROOT := $(CURDIR)/..
+
 # To make sure we do not include .config for any of the *config targets
 # catch them early, and hand them over to tools/kconfig/Makefile
 
@@ -163,6 +151,18 @@ ifneq ($(filter %config,$(MAKECMDGOALS)),)
 config-build := y
 endif
 
+include scripts/Kbuild.include
+
+# Don't break if the build process wasn't called from the top level
+# we need XEN_TARGET_ARCH to generate the proper config
+include $(XEN_ROOT)/Config.mk
+
+# Set ARCH/SUBARCH appropriately.
+export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
+export TARGET_ARCH := $(shell echo $(XEN_TARGET_ARCH) | \
+sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
+-e s'/riscv.*/riscv/g')
+
 export CONFIG_SHELL := $(SHELL)
 export YACC = $(if $(BISON),$(BISON),bison)
 export LEX = $(if $(FLEX),$(FLEX),flex)
-- 
Anthony PERARD




[XEN PATCH v9 14/30] build: remove KBUILD_ specific from Makefile.host

2022-01-25 Thread Anthony PERARD
This will allow $(HOSTCFLAGS) to actually be used when building
programmes for the build-host.

The other variable don't exist in our build system.

Also remove $(KBUILD_EXTMOD) since it should always be empty.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---
 xen/scripts/Makefile.host | 26 --
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/xen/scripts/Makefile.host b/xen/scripts/Makefile.host
index 8a85f94316bc..d6c358095ee8 100644
--- a/xen/scripts/Makefile.host
+++ b/xen/scripts/Makefile.host
@@ -82,18 +82,16 @@ host-cxxshobjs  := $(addprefix 
$(obj)/,$(host-cxxshobjs))
 #
 # Handle options to gcc. Support building with separate output directory
 
-_hostc_flags   = $(KBUILD_HOSTCFLAGS)   $(HOST_EXTRACFLAGS)   \
+_hostc_flags   = $(HOSTCFLAGS)   $(HOST_EXTRACFLAGS)   \
  $(HOSTCFLAGS_$(target-stem).o)
-_hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
+_hostcxx_flags = $(HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
  $(HOSTCXXFLAGS_$(target-stem).o)
 
 # $(objtree)/$(obj) for including generated headers from checkin source files
-ifeq ($(KBUILD_EXTMOD),)
 ifdef building_out_of_srctree
 _hostc_flags   += -I $(objtree)/$(obj)
 _hostcxx_flags += -I $(objtree)/$(obj)
 endif
-endif
 
 hostc_flags= -Wp,-MD,$(depfile) $(_hostc_flags)
 hostcxx_flags  = -Wp,-MD,$(depfile) $(_hostcxx_flags)
@@ -104,17 +102,17 @@ hostcxx_flags  = -Wp,-MD,$(depfile) $(_hostcxx_flags)
 # Create executable from a single .c file
 # host-csingle -> Executable
 quiet_cmd_host-csingle = HOSTCC  $@
-  cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(KBUILD_HOSTLDFLAGS) -o $@ 
$< \
-   $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
+  cmd_host-csingle = $(HOSTCC) $(hostc_flags) $(HOSTLDFLAGS) -o $@ $< \
+   $(HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
 $(host-csingle): $(obj)/%: $(src)/%.c FORCE
$(call if_changed_dep,host-csingle)
 
 # Link an executable based on list of .o files, all plain c
 # host-cmulti -> executable
 quiet_cmd_host-cmulti  = HOSTLD  $@
-  cmd_host-cmulti  = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -o $@ \
+  cmd_host-cmulti  = $(HOSTCC) $(HOSTLDFLAGS) -o $@ \
  $(addprefix $(obj)/, $($(target-stem)-objs)) \
- $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
+ $(HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
 $(host-cmulti): FORCE
$(call if_changed,host-cmulti)
 $(call multi-depend, $(host-cmulti), , -objs)
@@ -129,10 +127,10 @@ $(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
 # Link an executable based on list of .o files, a mixture of .c and .cc
 # host-cxxmulti -> executable
 quiet_cmd_host-cxxmulti= HOSTLD  $@
-  cmd_host-cxxmulti= $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -o $@ \
+  cmd_host-cxxmulti= $(HOSTCXX) $(HOSTLDFLAGS) -o $@ \
  $(foreach o,objs cxxobjs,\
  $(addprefix $(obj)/, $($(target-stem)-$(o \
- $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
+ $(HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem))
 $(host-cxxmulti): FORCE
$(call if_changed,host-cxxmulti)
 $(call multi-depend, $(host-cxxmulti), , -objs -cxxobjs)
@@ -163,9 +161,9 @@ $(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCE
 # Link a shared library, based on position independent .o files
 # *.o -> .so shared library (host-cshlib)
 quiet_cmd_host-cshlib  = HOSTLLD -shared $@
-  cmd_host-cshlib  = $(HOSTCC) $(KBUILD_HOSTLDFLAGS) -shared -o $@ \
+  cmd_host-cshlib  = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
  $(addprefix $(obj)/, $($(target-stem)-objs)) \
- $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem).so)
+ $(HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem).so)
 $(host-cshlib): FORCE
$(call if_changed,host-cshlib)
 $(call multi-depend, $(host-cshlib), .so, -objs)
@@ -173,9 +171,9 @@ $(call multi-depend, $(host-cshlib), .so, -objs)
 # Link a shared library, based on position independent .o files
 # *.o -> .so shared library (host-cxxshlib)
 quiet_cmd_host-cxxshlib= HOSTLLD -shared $@
-  cmd_host-cxxshlib= $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -shared -o 
$@ \
+  cmd_host-cxxshlib= $(HOSTCXX) $(HOSTLDFLAGS) -shared -o $@ \
  $(addprefix $(obj)/, $($(target-stem)-objs)) \
- $(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem).so)
+ $(HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem).so)
 $(host-cxxshlib): FORCE
$(call if_changed,host-cxxshlib)
 $(call multi-depend, $(host-cxxshlib), .so, -objs)
-- 
Anthony PERARD




[XEN PATCH v9 13/30] xen/tools/kconfig: fix build with -Wdeclaration-after-statement

2022-01-25 Thread Anthony PERARD
We are going to start building kconfig with HOSTCFLAGS from Config.mk,
it has the flag "-Wdeclaration-after-statement".

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
---

Notes:
v9:
- reviewed

 xen/tools/kconfig/confdata.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/xen/tools/kconfig/confdata.c b/xen/tools/kconfig/confdata.c
index 3569d2dec37c..a69250c91355 100644
--- a/xen/tools/kconfig/confdata.c
+++ b/xen/tools/kconfig/confdata.c
@@ -1237,6 +1237,7 @@ void set_all_choice_values(struct symbol *csym)
 
 bool conf_set_all_new_symbols(enum conf_def_mode mode)
 {
+   bool has_changed = false;
struct symbol *sym, *csym;
int i, cnt, pby, pty, ptm;  /* pby: probability of bool = y
 * pty: probability of tristate = y
@@ -1283,7 +1284,6 @@ bool conf_set_all_new_symbols(enum conf_def_mode mode)
exit( 1 );
}
}
-   bool has_changed = false;
 
for_all_symbols(i, sym) {
if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
-- 
Anthony PERARD




[XEN PATCH v9 12/30] build: hook kconfig into xen build system

2022-01-25 Thread Anthony PERARD
Now that xen's build system is very close to Linux's ones, we can hook
"Makefile.host" into Xen's build system, and we can build Kconfig with
that.

"tools/kconfig/Makefile" now needs a workaround to not rebuild
"$(XEN_ROOT)/.config", as `make` tries the rules "%.config" which
fails with:
tools/kconfig/Makefile:95: *** No configuration exists for this target on 
this architecture.  Stop.

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
---

Notes:
v8:
- reviewed
- adjust Makefile.host to use the renamed macro "multi-depend".
- move .config empty rules workaround into kconfig/Makefile (from Rules.mk)
  and merge the change of patch
build: add an other explicite rules to not build $(XEN_ROOT)/.config
  into
build: hook kconfig into xen build system

 xen/Makefile |  34 --
 xen/Rules.mk |  13 ++-
 xen/scripts/Kbuild.include   |  31 ++
 xen/scripts/Makefile.clean   |  11 +-
 xen/{tools/kconfig => scripts}/Makefile.host |  11 +-
 xen/tools/kconfig/Makefile   |   3 +
 xen/tools/kconfig/Makefile.kconfig   | 106 ---
 7 files changed, 86 insertions(+), 123 deletions(-)
 rename xen/{tools/kconfig => scripts}/Makefile.host (95%)
 delete mode 100644 xen/tools/kconfig/Makefile.kconfig

diff --git a/xen/Makefile b/xen/Makefile
index 3dc7b4da10fb..0705e67a9e7d 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -40,6 +40,7 @@ ARCH=$(XEN_TARGET_ARCH)
 SRCARCH=$(shell echo $(ARCH) | \
   sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
   -e s'/riscv.*/riscv/g')
+export ARCH SRCARCH
 
 # Don't break if the build process wasn't called from the top level
 # we need XEN_TARGET_ARCH to generate the proper config
@@ -163,6 +164,13 @@ ifneq ($(filter %config,$(MAKECMDGOALS)),)
 config-build := y
 endif
 
+export CONFIG_SHELL := $(SHELL)
+export YACC = $(if $(BISON),$(BISON),bison)
+export LEX = $(if $(FLEX),$(FLEX),flex)
+
+# Default file for 'make defconfig'.
+export KBUILD_DEFCONFIG := $(ARCH)_defconfig
+
 # CLANG_FLAGS needs to be calculated before calling Kconfig
 ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
 CLANG_FLAGS :=
@@ -203,9 +211,6 @@ PHONY += tools_fixdep
 tools_fixdep:
$(MAKE) -C tools fixdep
 
-# Shorthand for kconfig
-kconfig = -f $(BASEDIR)/tools/kconfig/Makefile.kconfig ARCH=$(ARCH) 
SRCARCH=$(SRCARCH) HOSTCC="$(HOSTCC)" HOSTCXX="$(HOSTCXX)"
-
 ifeq ($(config-build),y)
 # ===
 # *config targets only - make sure prerequisites are updated, and descend
@@ -221,14 +226,14 @@ filechk_kconfig_allconfig = \
 .allconfig.tmp: FORCE
set -e; { $(call filechk_kconfig_allconfig); } > $@
 
-config: FORCE
-   $(MAKE) $(kconfig) $@
+config: tools_fixdep FORCE
+   $(Q)$(MAKE) $(build)=tools/kconfig $@
 
 # Config.mk tries to include .config file, don't try to remake it
 %/.config: ;
 
-%config: .allconfig.tmp FORCE
-   $(MAKE) $(kconfig) KCONFIG_ALLCONFIG=$< $@
+%config: .allconfig.tmp tools_fixdep FORCE
+   $(Q)$(MAKE) $(build)=tools/kconfig KCONFIG_ALLCONFIG=$< $@
 
 else # !config-build
 
@@ -238,9 +243,15 @@ ifeq ($(need-config),y)
 # changes are detected.
 -include include/config/auto.conf.cmd
 
+# This allows make to build fixdep before invoking defconfig. We can't use
+# "tools_fixdep" which is a .PHONY target and would force make to call
+# "defconfig" again to update $(KCONFIG_CONFIG).
+tools/fixdep:
+   $(MAKE) -C tools fixdep
+
 # Allow people to just run `make` as before and not force them to configure
-$(KCONFIG_CONFIG):
-   $(MAKE) $(kconfig) defconfig
+$(KCONFIG_CONFIG): tools/fixdep
+   $(Q)$(MAKE) $(build)=tools/kconfig defconfig
 
 # The actual configuration files used during the build are stored in
 # include/generated/ and include/config/. Update them if .config is newer than
@@ -249,7 +260,7 @@ $(KCONFIG_CONFIG):
 # This exploits the 'multi-target pattern rule' trick.
 # The syncconfig should be executed only once to make all the targets.
 include/config/%.conf include/config/%.conf.cmd: $(KCONFIG_CONFIG)
-   $(MAKE) $(kconfig) syncconfig
+   $(Q)$(MAKE) $(build)=tools/kconfig syncconfig
 
 ifeq ($(CONFIG_DEBUG),y)
 CFLAGS += -O1
@@ -406,9 +417,10 @@ _clean:
$(MAKE) $(clean) arch/riscv
$(MAKE) $(clean) arch/x86
$(MAKE) $(clean) test
-   $(MAKE) $(kconfig) clean
+   $(MAKE) $(clean) tools/kconfig
find . \( -name "*.o" -o -name ".*.d" -o -name ".*.d2" \
-o -name ".*.o.tmp" -o -name "*~" -o -name "core" \
+   -o -name '*.lex.c' -o -name '*.tab.[ch]' \
-o -name "*.gcno" -o -name ".*.cmd" -o -name "lib.a" \) -exec 
rm -f {} \;
rm -f include/asm $(TARGET) $(TARGET).gz $(TARGET).efi 
$(TARGET).efi.map $(TARGET)-syms $(TARGET)-syms.map *~ core
rm -f asm

[XEN PATCH v9 09/30] build: build everything from the root dir, use obj=$subdir

2022-01-25 Thread Anthony PERARD
A subdirectory is now built by setting "$(obj)" instead of changing
directory. "$(obj)" should always be set when using "Rules.mk" and
thus a shortcut "$(build)" is introduced and should be used.

A new variable "$(need-builtin)" is introduce. It is to be used
whenever a "built_in.o" is wanted from a subdirectory. "built_in.o"
isn't the main target anymore, and thus only needs to depends on the
objects that should be part of "built_in.o".

Introduce $(srctree) and $(objtree) to replace $(BASEDIR) in cases a
relative path is better, and $(abs_srctree) and $(abs_objtree) which
have an absolute path.

DEPS is updated as the existing macro to deal with it doesn't know
about $(obj).

There's some changes in "Rules.mk" which in addition to deal with
"$(obj)" also make it's looks more like "Makefile.build" from Linux
v5.12.

test/Makefile doesn't need special handling in order to build
everything under test/, Rules.mk will visit test/livepatch via
$(subdir-y), thus "tests" "all" and "build" target are removed.
"subtree-force-update" target isn't useful so it is removed as well.

test/livepatch/Makefile doesn't need default target anymore, Rules.mk
will build everything in $(extra-y) and thus all *.livepatch.

Adjust cloc recipe: dependency files generated by CC will now have the
full path to the source file, so we don't need to prepend the
subdirectory. This fix some issue with source not been parsed by cloc
before. Also source from tools/kconfig would be listed with changes in
this patch so adjust the find command to stop listing the "tools"
directory and thus kconfig. With a default build of Xen on X86, they
are a few new files parsed by cloc:
arch/x86/x86_64/compat/mm.c
arch/x86/x86_64/mm.c
common/compat/domain.c
common/compat/memory.c
common/compat/xlat.c

Signed-off-by: Anthony PERARD 
Acked-by: Bob Eshleman 
Acked-by: Julien Grall 
Reviewed-by: Jan Beulich 
---

Notes:
v9:
- reviewed

v8:
- rebased
- move check for $(obj) in Rules.mk earlier
- use $(sort ) when adding directories in $(subdir-y)
- fix missing space between "DEPS" and ":="
- use -iquote when adding '.' for xen/arch/x86/usercopy.o
- introduce also abs_srctree and abs_objtree
  those were in patch "build: replace $(BASEDIR) by $(objtree)"
- reworked cmd_objcopy_fix_sym, we don't need to do anything for old
  version of clang, and for recent version of clang the change is the
  same a for gcc.
- adjust "cloc" recipe
- add some more $(Q) to silent $(MAKE) $(build)=...

v7:
- Spell "Makefile.build" as "Rules.mk" in the warning in Rules.mk about
  undefined $(obj).
- use $(srctree) for include/xen/config.h
- handle $(nocov-y) and $(noubsan-y), needed to add $(obj)/

 xen/Makefile|  36 ++
 xen/Rules.mk| 138 
 xen/arch/arm/Makefile   |   4 +-
 xen/arch/arm/Rules.mk   |   4 --
 xen/arch/arm/arch.mk|   4 +-
 xen/arch/riscv/arch.mk  |   4 +-
 xen/arch/x86/Makefile   |  11 +--
 xen/arch/x86/Rules.mk   |   4 +-
 xen/arch/x86/arch.mk|  12 ++--
 xen/arch/x86/boot/Makefile  |   8 +--
 xen/build.mk|  12 +++-
 xen/include/Makefile|   6 +-
 xen/scripts/Kbuild.include  |   6 ++
 xen/test/Makefile   |   7 +-
 xen/test/livepatch/Makefile |   6 --
 xen/xsm/flask/Makefile  |   2 +-
 xen/xsm/flask/ss/Makefile   |   2 +-
 17 files changed, 162 insertions(+), 104 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index ec24856a5d46..780d555e8620 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -22,6 +22,15 @@ export CHECKPOLICY   ?= checkpolicy
 export BASEDIR := $(CURDIR)
 export XEN_ROOT := $(BASEDIR)/..
 
+abs_objtree := $(CURDIR)
+abs_srctree := $(CURDIR)
+
+export abs_srctree abs_objtree
+
+srctree := .
+objtree := .
+export srctree objtree
+
 # Do not use make's built-in rules and variables
 MAKEFLAGS += -rR
 
@@ -47,7 +56,7 @@ export KCONFIG_CONFIG ?= .config
 
 export CC CXX LD
 
-export TARGET := $(BASEDIR)/xen
+export TARGET := xen
 
 .PHONY: default
 default: build
@@ -250,7 +259,7 @@ endif
 CFLAGS += -nostdinc -fno-builtin -fno-common
 CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith
 $(call cc-option-add,CFLAGS,CC,-Wvla)
-CFLAGS += -pipe -D__XEN__ -include $(BASEDIR)/include/xen/config.h
+CFLAGS += -pipe -D__XEN__ -include $(srctree)/include/xen/config.h
 CFLAGS-$(CONFIG_DEBUG_INFO) += -g
 
 ifneq ($(CONFIG_CC_IS_CLANG),y)
@@ -349,10 +358,10 @@ _install: $(TARGET)$(CONFIG_XEN_INSTALL_SUFFIX)
 
 .PHONY: tests
 tests:
-   $(MAKE) -f $(BASEDIR)/Rules.mk -C test tests
+   $(Q)$(MAKE) $(build)=test
 .PHONY: install-tests
 install-tests:
-   $(MAKE) -f $(BASEDIR)/Rules.mk -C test install
+   $(Q)$(MAKE) $(build)=test install
 
 .PHONY: _uninstall
 _uninstall: D=$(DESTDIR)
@@ -408,16 +417,16 @@ $(TARGET).gz: $(TARGET)
 
 $(TARGET): FORCE
$(MAKE) -C tools
-   $(MAKE) -f $(BASE

[XEN PATCH v9 23/30] build,x86: remove the need for build32.mk

2022-01-25 Thread Anthony PERARD
Rework "arch/x86/boot/Makefile" to allow it to build both file
"cmdline.S" and "reloc.S" without "build32.mk".

These will now use the main rules for "%.o: %.c", and thus generate a
dependency file. (We will not need to track the dependency manually
anymore.)

But for that, we need to override the main CFLAGS to do a 32bit build.
We introduce XEN_TREEWIDE_CFLAGS which can be reused in boot/Makefile,
and avoid the need to reparse Config.mk with a different value for
XEN_TARGET_ARCH. From this new $(XEN_TREEWIDE_CFLAGS), we only need to
change -m64 to have the 32bit flags. Then those are applied only to
"cmdline.o" and "reloc.o".

Specifically apply the rule "%.S: %.bin" to both cmdline.S and reloc.S
to avoid make trying to regenerate other %.S files with it.

There is no change expected to the resulting "cmdline.S" and
"reloc.S", only the *.o file changes as their symbol for FILE goes
from "cmdline.c" to "arch/x86//cmdline.c". (No idea why "boot" is
missing from the string.) (I've only check with GCC, not clang.)

Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- rename XEN_COMMON_CFLAGS to XEN_TREEWIDE_CFLAGS
- rename $(head-objs) to $(head-srcs)
- substitute LDFLAGS_DIRECT in %.lnk rule, rather than changing the value
  of it.

v8:
- avoid the need to list CFLAGS from Config.mk a second time by
  introducing XEN_COMMON_CFLAGS, and using it in boot/
- improve LDFLAGS_DIRECT, by just substitute x64 to i368 from x86
  LDFLAGS_DIRECT. And thus avoid copying the logic from Config.mk.

 xen/Makefile |  4 +++
 xen/arch/x86/boot/Makefile   | 49 
 xen/arch/x86/boot/build32.mk | 40 -
 3 files changed, 37 insertions(+), 56 deletions(-)
 delete mode 100644 xen/arch/x86/boot/build32.mk

diff --git a/xen/Makefile b/xen/Makefile
index dc25fa443b82..8baa260b93a7 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -171,6 +171,10 @@ export LEX = $(if $(FLEX),$(FLEX),flex)
 # Default file for 'make defconfig'.
 export KBUILD_DEFCONFIG := $(ARCH)_defconfig
 
+# Copy CFLAGS generated by "Config.mk" so they can be reused later without
+# reparsing Config.mk by e.g. arch/x86/boot/.
+export XEN_TREEWIDE_CFLAGS := $(CFLAGS)
+
 # CLANG_FLAGS needs to be calculated before calling Kconfig
 ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
 CLANG_FLAGS :=
diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index 1ac8cb435e0e..ca8001c72b23 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -1,25 +1,42 @@
 obj-bin-y += head.o
+head-srcs := cmdline.S reloc.S
 
-DEFS_H_DEPS = $(abs_srctree)/$(src)/defs.h $(abs_srctree)/include/xen/stdbool.h
+nocov-y += $(head-srcs:.S=.o)
+noubsan-y += $(head-srcs:.S=.o)
+targets += $(head-srcs:.S=.o)
 
-CMDLINE_DEPS = $(DEFS_H_DEPS) $(abs_srctree)/$(src)/video.h \
-  $(BASEDIR)/include/xen/kconfig.h \
-  $(BASEDIR)/include/generated/autoconf.h
+head-srcs := $(addprefix $(obj)/, $(head-srcs))
 
-RELOC_DEPS = $(DEFS_H_DEPS) \
-$(BASEDIR)/include/generated/autoconf.h \
-$(BASEDIR)/include/xen/kconfig.h \
-$(BASEDIR)/include/xen/multiboot.h \
-$(BASEDIR)/include/xen/multiboot2.h \
-$(BASEDIR)/include/xen/const.h \
-$(BASEDIR)/include/public/arch-x86/hvm/start_info.h
+$(obj)/head.o: $(head-srcs)
 
-$(obj)/head.o: $(obj)/cmdline.S $(obj)/reloc.S
+CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS))
+$(call cc-options-add,CFLAGS_x86_32,CC,$(EMBEDDED_EXTRA_CFLAGS))
+CFLAGS_x86_32 += -Werror -fno-builtin -g0 -msoft-float
+CFLAGS_x86_32 += -I$(srctree)/include
 
-$(obj)/cmdline.S: $(src)/cmdline.c $(CMDLINE_DEPS) $(src)/build32.lds
-   $(MAKE) -f $(abs_srctree)/$(src)/build32.mk -C $(obj) $(@F) 
CMDLINE_DEPS="$(CMDLINE_DEPS)"
+# override for 32bit binaries
+$(head-srcs:.S=.o): CFLAGS_stack_boundary :=
+$(head-srcs:.S=.o): XEN_CFLAGS := $(CFLAGS_x86_32) -fpic
 
-$(obj)/reloc.S: $(src)/reloc.c $(RELOC_DEPS) $(src)/build32.lds
-   $(MAKE) -f $(abs_srctree)/$(src)/build32.mk -C $(obj) $(@F) 
RELOC_DEPS="$(RELOC_DEPS)"
+$(head-srcs): %.S: %.bin
+   (od -v -t x $< | tr -s ' ' | awk 'NR > 1 {print s} {s=$$0}' | \
+   sed 's/ /,0x/g' | sed 's/,0x$$//' | sed 's/^[0-9]*,/ .long /') >$@
+
+# Drop .got.plt during conversion to plain binary format.
+# Please check build32.lds for more details.
+%.bin: %.lnk
+   $(OBJDUMP) -h $< | sed -n '/[0-9]/{s,00*,0,g;p;}' | \
+   while read idx name sz rest; do \
+   case "$$name" in \
+   .got.plt) \
+   test $$sz != 0c || continue; \
+   echo "Error: non-empty $$name: 0x$$sz" >&2; \
+   exit $$(expr $$idx + 1);; \
+   esac; \
+   done
+   $(OBJCOPY) -O binary -R .got.plt $< $@
+
+%.lnk: %.o $(src)/build32.lds
+

[XEN PATCH v9 24/30] build: grab common EFI source files in arch specific dir

2022-01-25 Thread Anthony PERARD
Rather than preparing the efi source file, we will make the symbolic
link as needed from the build location.

The `ln` command is run every time to allow to update the link in case
the source tree change location.

This patch also introduce "efi-common.mk" which allow to reuse the
common make instructions without having to duplicate them into each
arch.

And now that we have a list of common source file, we can start to
remove the links to the source files on clean.

Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- rename efi_common.mk to efi-common.mk
- generalize comment about cleaning "efi" and "boot" subdir in x86.
- add a space after the other comma of $(patsubst )
- create a relative symlink instead of an absolute one
- with the above, we don't need to use $(abs_srctree) anymore in the
  prerequisite of the link to the efi source file, use $(srctree).

v8:
- use symbolic link instead of making a copy of the source
- introduce efi_common.mk
- remove links to source file on clean
- use -iquote for "efi.h" headers in common/efi

 xen/Makefile |  5 -
 xen/arch/arm/efi/Makefile|  4 ++--
 xen/arch/x86/Makefile|  3 ++-
 xen/arch/x86/efi/Makefile|  5 +
 xen/common/efi/efi-common.mk | 15 +++
 5 files changed, 20 insertions(+), 12 deletions(-)
 create mode 100644 xen/common/efi/efi-common.mk

diff --git a/xen/Makefile b/xen/Makefile
index 8baa260b93a7..443784dfce80 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -444,11 +444,6 @@ $(TARGET).gz: $(TARGET)
 $(TARGET): FORCE
$(Q)$(MAKE) $(build)=tools
$(Q)$(MAKE) $(build)=. include/xen/compile.h
-   [ -e arch/$(TARGET_ARCH)/efi ] && for f in $$(cd common/efi; echo 
*.[ch]); \
-   do test -r arch/$(TARGET_ARCH)/efi/$$f || \
-  ln -nsf ../../../common/efi/$$f arch/$(TARGET_ARCH)/efi/; \
-   done; \
-   true
$(Q)$(MAKE) $(build)=include all
$(Q)$(MAKE) $(build)=arch/$(TARGET_ARCH) include
$(Q)$(MAKE) $(build)=. arch/$(TARGET_ARCH)/include/asm/asm-offsets.h
diff --git a/xen/arch/arm/efi/Makefile b/xen/arch/arm/efi/Makefile
index 1b1ed06feddc..4313c390665f 100644
--- a/xen/arch/arm/efi/Makefile
+++ b/xen/arch/arm/efi/Makefile
@@ -1,4 +1,4 @@
-CFLAGS-y += -fshort-wchar
+include $(srctree)/common/efi/efi-common.mk
 
-obj-y += boot.init.o pe.init.o ebmalloc.o runtime.o
+obj-y += $(EFIOBJ-y)
 obj-$(CONFIG_ACPI) +=  efi-dom0.init.o
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index c94b4092d4c1..a847c989ff92 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -77,8 +77,9 @@ obj-$(CONFIG_COMPAT) += x86_64/platform_hypercall.o
 obj-y += sysctl.o
 endif
 
-# Allows "clean" to descend into boot/
+# Allows "clean" to descend
 subdir- += boot
+subdir- += efi
 
 extra-y += asm-macros.i
 extra-y += xen.lds
diff --git a/xen/arch/x86/efi/Makefile b/xen/arch/x86/efi/Makefile
index e08b4d8e4808..034ec87895df 100644
--- a/xen/arch/x86/efi/Makefile
+++ b/xen/arch/x86/efi/Makefile
@@ -1,4 +1,4 @@
-CFLAGS-y += -fshort-wchar
+include $(srctree)/common/efi/efi-common.mk
 
 quiet_cmd_objcopy_o_ihex = OBJCOPY $@
 cmd_objcopy_o_ihex = $(OBJCOPY) -I ihex -O binary $< $@
@@ -8,9 +8,6 @@ $(obj)/%.o: $(src)/%.ihex FORCE
 
 $(obj)/boot.init.o: $(obj)/buildid.o
 
-EFIOBJ-y := boot.init.o pe.init.o ebmalloc.o runtime.o
-EFIOBJ-$(CONFIG_COMPAT) += compat.o
-
 $(call cc-option-add,cflags-stack-boundary,CC,-mpreferred-stack-boundary=4)
 $(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := 
$(cflags-stack-boundary)
 
diff --git a/xen/common/efi/efi-common.mk b/xen/common/efi/efi-common.mk
new file mode 100644
index ..ad3c6f2569c3
--- /dev/null
+++ b/xen/common/efi/efi-common.mk
@@ -0,0 +1,15 @@
+EFIOBJ-y := boot.init.o pe.init.o ebmalloc.o runtime.o
+EFIOBJ-$(CONFIG_COMPAT) += compat.o
+
+CFLAGS-y += -fshort-wchar
+CFLAGS-y += -iquote $(srctree)/common/efi
+
+# Part of the command line transforms $(obj) in to a relative reverted path.
+# e.g.: It transforms "dir/foo/bar" into successively
+#   "dir foo bar", ".. .. ..", "../../.."
+$(obj)/%.c: $(srctree)/common/efi/%.c FORCE
+   $(Q)ln -nfs $(subst $(space),/,$(patsubst %,..,$(subst /, 
,$(obj/common/efi/$(

[XEN PATCH v9 18/30] build: generate x86's asm-macros.h with filechk

2022-01-25 Thread Anthony PERARD
When we will build out-of-tree, make is going to try to generate
"asm-macros.h" before the directories "arch/x86/include/asm" exist,
thus we would need to call `mkdir` explicitly. We will use "filechk"
for that as it does everything that the current recipe does and does
call `mkdir`.

Also, they are no more "*.new" files generated in this directory.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---
 xen/arch/x86/Makefile | 29 -
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 9494e4a3df79..de7f7ce07b98 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -230,18 +230,21 @@ include: $(BASEDIR)/arch/x86/include/asm/asm-macros.h
 $(obj)/asm-macros.i: CFLAGS-y += -D__ASSEMBLY__ -P
 
 $(BASEDIR)/arch/x86/include/asm/asm-macros.h: $(obj)/asm-macros.i 
$(src)/Makefile
-   echo '#if 0' >$@.new
-   echo '.if 0' >>$@.new
-   echo '#endif' >>$@.new
-   echo '#ifndef __ASM_MACROS_H__' >>$@.new
-   echo '#define __ASM_MACROS_H__' >>$@.new
-   echo 'asm ( ".include \"$@\"" );' >>$@.new
-   echo '#endif /* __ASM_MACROS_H__ */' >>$@.new
-   echo '#if 0' >>$@.new
-   echo '.endif' >>$@.new
-   cat $< >>$@.new
-   echo '#endif' >>$@.new
-   $(call move-if-changed,$@.new,$@)
+   $(call filechk,asm-macros.h)
+
+define filechk_asm-macros.h
+echo '#if 0'; \
+echo '.if 0'; \
+echo '#endif'; \
+echo '#ifndef __ASM_MACROS_H__'; \
+echo '#define __ASM_MACROS_H__'; \
+echo 'asm ( ".include \"$@\"" );'; \
+echo '#endif /* __ASM_MACROS_H__ */'; \
+echo '#if 0'; \
+echo '.endif'; \
+cat $<; \
+echo '#endif'
+endef
 
 $(obj)/efi.lds: AFLAGS-y += -DEFI
 $(obj)/xen.lds $(obj)/efi.lds: $(src)/xen.lds.S FORCE
@@ -255,7 +258,7 @@ $(obj)/efi/mkreloc: $(src)/efi/mkreloc.c
 
 .PHONY: clean
 clean::
-   rm -f *.lds *.new boot/*.o boot/*~ boot/core boot/mkelf32
+   rm -f *.lds boot/*.o boot/*~ boot/core boot/mkelf32
rm -f asm-macros.i $(BASEDIR)/arch/x86/include/asm/asm-macros.*
rm -f $(BASEDIR)/.xen-syms.[0-9]* boot/.*.d $(BASEDIR)/.xen.elf32
rm -f $(BASEDIR)/.xen.efi.[0-9]* efi/*.efi efi/mkreloc
-- 
Anthony PERARD




[XEN PATCH v9 15/30] build: handle always-y and hostprogs-always-y

2022-01-25 Thread Anthony PERARD
This will be used for xen/tools/.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---
 xen/Rules.mk   | 10 +-
 xen/scripts/Makefile.clean |  3 ++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/xen/Rules.mk b/xen/Rules.mk
index 13c1943da97e..5f2368805b36 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -78,9 +78,17 @@ else
 obj-y:= $(filter-out %/, $(obj-y))
 endif
 
+# hostprogs-always-y += foo
+# ... is a shorthand for
+# hostprogs-y += foo
+# always-y  += foo
+hostprogs-y += $(hostprogs-always-y)
+always-y += $(hostprogs-always-y)
+
 # Add subdir path
 
 extra-y := $(addprefix $(obj)/,$(extra-y))
+always-y:= $(addprefix $(obj)/,$(always-y))
 targets := $(addprefix $(obj)/,$(targets))
 lib-y   := $(addprefix $(obj)/,$(lib-y))
 obj-y   := $(addprefix $(obj)/,$(obj-y))
@@ -283,7 +291,7 @@ targets += $(call intermediate_targets, .init.o, .o) \
 # Build
 # ---
 
-__build: $(targets-for-builtin) $(subdir-y)
+__build: $(targets-for-builtin) $(subdir-y) $(always-y)
@:
 
 # Descending
diff --git a/xen/scripts/Makefile.clean b/xen/scripts/Makefile.clean
index 156d6307cf83..c2689d4af5fa 100644
--- a/xen/scripts/Makefile.clean
+++ b/xen/scripts/Makefile.clean
@@ -18,7 +18,8 @@ subdir-all := $(subdir-y) $(subdir-n) $(subdir-) \
   $(patsubst %/,%, $(filter %/, $(obj-y) $(obj-n) $(obj-)))
 
 __clean-files := \
-$(clean-files) $(hostprogs-y) $(hostprogs-)
+$(clean-files) $(hostprogs-y) $(hostprogs-) \
+$(hostprogs-always-y) $(hostprogs-always-)
 
 __clean-files := $(wildcard $(__clean-files))
 
-- 
Anthony PERARD




[XEN PATCH v9 11/30] build: rename __LINKER__ to LINKER_SCRIPT

2022-01-25 Thread Anthony PERARD
For two reasons: this macro is used to generate a "linker script" and
is not by the linker, and name starting with an underscore '_' are
supposed to be reserved, so better avoid them when not needed.

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
Acked-by: Julien Grall 
---
 xen/Rules.mk  | 2 +-
 xen/arch/arm/include/asm/config.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/xen/Rules.mk b/xen/Rules.mk
index 567a23a54cd9..fea3f70cdb72 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -256,7 +256,7 @@ $(obj)/%.s: $(src)/%.S FORCE
 
 # Linker scripts, .lds.S -> .lds
 quiet_cmd_cpp_lds_S = LDS $@
-cmd_cpp_lds_S = $(CPP) -P $(call cpp_flags,$(a_flags)) -D__LINKER__ -MQ $@ -o 
$@ $<
+cmd_cpp_lds_S = $(CPP) -P $(call cpp_flags,$(a_flags)) -DLINKER_SCRIPT -MQ $@ 
-o $@ $<
 
 targets := $(filter-out $(PHONY), $(targets))
 
diff --git a/xen/arch/arm/include/asm/config.h 
b/xen/arch/arm/include/asm/config.h
index c7b77912013e..2aced0bc3b8b 100644
--- a/xen/arch/arm/include/asm/config.h
+++ b/xen/arch/arm/include/asm/config.h
@@ -191,7 +191,7 @@ extern unsigned long frametable_virt_end;
 #define watchdog_disable() ((void)0)
 #define watchdog_enable()  ((void)0)
 
-#if defined(__ASSEMBLY__) && !defined(__LINKER__)
+#if defined(__ASSEMBLY__) && !defined(LINKER_SCRIPT)
 #include 
 #include 
 #endif
-- 
Anthony PERARD




[XEN PATCH v9 10/30] build: introduce if_changed_deps

2022-01-25 Thread Anthony PERARD
This macro does compare command line like if_changed, but it also
rewrite the dependencies generated by $(CC) in order to depend on a
CONFIG_* as generated by kconfig instead of depending on autoconf.h.
This allow to make a change in kconfig options and only rebuild the
object that uses that CONFIG_* option.

cmd_and_record isn't needed anymore as it is replace by
cmd_and_fixdep.

There's only one .*.d dependency file left which is explicitly
included as a workound, all the other are been absorb into the .*.cmd
dependency files via `fixdep`. So including .*.d can be removed from
the makefile.

Also adjust "cloc" recipe due to .*.d been replace by .*.cmd files.

This imports fixdep.c and if_changed_deps macro from Linux v5.12.

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
---

Notes:
v8:
- reviewd
- remove "tools_fixdep" prerequisite in xen/Makefile from "$(TARGET)"
  target as the first command of the recipe will build everything in
  tools/ and thus tools/fixdep.
- adjust "cloc" recipe.

 .gitignore |   1 +
 xen/Makefile   |  15 +-
 xen/Rules.mk   |  17 +-
 xen/arch/x86/Makefile  |   8 +-
 xen/build.mk   |   1 +
 xen/scripts/Kbuild.include |  17 +-
 xen/tools/Makefile |   7 +-
 xen/tools/fixdep.c | 404 +
 8 files changed, 441 insertions(+), 29 deletions(-)
 create mode 100644 xen/tools/fixdep.c

diff --git a/.gitignore b/.gitignore
index 3f9d55ba87f0..cc87a7dfae7b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -332,6 +332,7 @@ xen/include/xen/lib/x86/cpuid-autogen.h
 xen/test/livepatch/config.h
 xen/test/livepatch/expect_config.h
 xen/test/livepatch/*.livepatch
+xen/tools/fixdep
 xen/tools/kconfig/.tmp_gtkcheck
 xen/tools/kconfig/.tmp_qtcheck
 xen/tools/symbols
diff --git a/xen/Makefile b/xen/Makefile
index 780d555e8620..3dc7b4da10fb 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -196,6 +196,13 @@ export XEN_HAS_CHECKPOLICY := $(call 
success,$(CHECKPOLICY) -h 2>&1 | grep -q xe
 export root-make-done := y
 endif # root-make-done
 
+# ===
+# Rules shared between *config targets and build targets
+
+PHONY += tools_fixdep
+tools_fixdep:
+   $(MAKE) -C tools fixdep
+
 # Shorthand for kconfig
 kconfig = -f $(BASEDIR)/tools/kconfig/Makefile.kconfig ARCH=$(ARCH) 
SRCARCH=$(SRCARCH) HOSTCC="$(HOSTCC)" HOSTCXX="$(HOSTCXX)"
 
@@ -471,18 +478,18 @@ cscope:
 _MAP:
$(NM) -n $(TARGET)-syms | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] 
\)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' > System.map
 
-%.o %.i %.s: %.c FORCE
+%.o %.i %.s: %.c tools_fixdep FORCE
$(Q)$(MAKE) $(build)=$(*D) $(*D)/$(@F)
 
-%.o %.s: %.S FORCE
+%.o %.s: %.S tools_fixdep FORCE
$(Q)$(MAKE) $(build)=$(*D) $(*D)/$(@F)
 
-%/: FORCE
+%/: tools_fixdep FORCE
$(Q)$(MAKE) $(build)=$* need-builtin=1
 
 .PHONY: cloc
 cloc:
-   find . -name tools -prune -o -name '*.o.d' -print | while read f; do \
+   find . -name tools -prune -o -name '*.o.cmd' -print | while read f; do \
for sf in $$(grep -o "[a-zA-Z0-9_/-]*\.[cS]" $$f); do \
test -f "$$sf" && echo "$$sf"; \
done; \
diff --git a/xen/Rules.mk b/xen/Rules.mk
index 67112e00773f..567a23a54cd9 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -147,8 +147,8 @@ cpp_flags = $(filter-out -Wa$(comma)% -flto,$(1))
 # Calculation of flags, first the generic flags, then the arch specific flags,
 # and last the flags modified for a target or a directory.
 
-c_flags = -MMD -MP -MF $(@D)/.$(@F).d $(XEN_CFLAGS)
-a_flags = -MMD -MP -MF $(@D)/.$(@F).d $(XEN_AFLAGS)
+c_flags = -MMD -MP -MF $(depfile) $(XEN_CFLAGS)
+a_flags = -MMD -MP -MF $(depfile) $(XEN_AFLAGS)
 
 include $(BASEDIR)/arch/$(TARGET_ARCH)/Rules.mk
 
@@ -205,7 +205,7 @@ else
 endif
 
 define rule_cc_o_c
-$(call cmd_and_record,cc_o_c)
+$(call cmd_and_fixdep,cc_o_c)
 $(call cmd,objcopy_fix_sym)
 endef
 
@@ -216,7 +216,7 @@ quiet_cmd_cc_o_S = CC  $@
 cmd_cc_o_S = $(CC) $(a_flags) -c $< -o $@
 
 $(obj)/%.o: $(src)/%.S FORCE
-   $(call if_changed,cc_o_S)
+   $(call if_changed_dep,cc_o_S)
 
 
 quiet_cmd_obj_init_o = INIT_O  $@
@@ -246,13 +246,13 @@ quiet_cmd_cpp_s_S = CPP $@
 cmd_cpp_s_S = $(CPP) $(call cpp_flags,$(a_flags)) -MQ $@ -o $@ $<
 
 $(obj)/%.i: $(src)/%.c FORCE
-   $(call if_changed,cpp_i_c)
+   $(call if_changed_dep,cpp_i_c)
 
 $(obj)/%.s: $(src)/%.c FORCE
-   $(call if_changed,cc_s_c)
+   $(call if_changed_dep,cc_s_c)
 
 $(obj)/%.s: $(src)/%.S FORCE
-   $(call if_changed,cpp_s_S)
+   $(call if_changed_dep,cpp_s_S)
 
 # Linker scripts, .lds.S -> .lds
 quiet_cmd_cpp_lds_S = LDS $@
@@ -291,9 +291,6 @@ existing-targets := $(wildcard $(sort $(targets)))
 
 -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
 
-DEPS := $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).d)
--include $(DEPS_INCLUDE)
-
 #

[XEN PATCH v9 26/30] build: replace $(BASEDIR) and use $(srctree)

2022-01-25 Thread Anthony PERARD
$(srctree) is a better description for the source directory than
$(BASEDIR) that has been used for both source and build directory
(which where the same).

This adds $(srctree) to a few path where make's VPATH=$(srctree) won't
apply. And replace $(BASEDIR) by $(srctree).

Introduce "$(srcdir)" as a shortcut for "$(srctree)/$(src)" as the
later is used often enough.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---

Notes:
v9:
- acked
- reword "# shortcut for $(srctree)/$(src)" into "# shortcuts" in both
  places.

v8:
- merge of two patchs from v7:
build: add $(srctree) in few key places
build: replace $(BASEDIR) by $(srctree)
  both patch were acked
- introduce $(srcdir) as a shortcut for $(srctree)/$(src)

 xen/Kconfig |  4 ++--
 xen/Makefile|  7 +++
 xen/Rules.mk|  9 ++---
 xen/arch/x86/arch.mk|  2 +-
 xen/build.mk|  4 ++--
 xen/common/Makefile |  2 +-
 xen/common/libfdt/Makefile  |  2 +-
 xen/include/Makefile| 14 +++---
 xen/scripts/Kconfig.include |  2 +-
 xen/scripts/Makefile.clean  |  5 -
 xen/xsm/flask/Makefile  | 10 +-
 11 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/xen/Kconfig b/xen/Kconfig
index bcbd2758e5d3..ac9a638d372e 100644
--- a/xen/Kconfig
+++ b/xen/Kconfig
@@ -14,14 +14,14 @@ config CC_IS_GCC
 
 config GCC_VERSION
int
-   default $(shell,$(BASEDIR)/scripts/gcc-version.sh $(CC))
+   default $(shell,$(srctree)/scripts/gcc-version.sh $(CC))
 
 config CC_IS_CLANG
def_bool $(success,$(CC) --version | head -n 1 | grep -q clang)
 
 config CLANG_VERSION
int
-   default $(shell,$(BASEDIR)/scripts/clang-version.sh $(CC))
+   default $(shell,$(srctree)/scripts/clang-version.sh $(CC))
 
 # -fvisibility=hidden reduces -fpic cost, if it's available
 config CC_HAS_VISIBILITY_ATTRIBUTE
diff --git a/xen/Makefile b/xen/Makefile
index 443784dfce80..c39c1699966f 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -19,8 +19,7 @@ export PYTHON ?= $(PYTHON_INTERPRETER)
 
 export CHECKPOLICY ?= checkpolicy
 
-export BASEDIR := $(CURDIR)
-export XEN_ROOT := $(BASEDIR)/..
+export XEN_ROOT := $(CURDIR)/..
 
 abs_objtree := $(CURDIR)
 abs_srctree := $(CURDIR)
@@ -189,7 +188,7 @@ ifeq ($(TARGET_ARCH),x86)
 t1 = $(call as-insn,$(CC),".L0: .L1: .skip (.L1 - .L0)",,-no-integrated-as)
 
 # Check whether clang asm()-s support .include.
-t2 = $(call as-insn,$(CC) -I$(BASEDIR)/arch/x86/include,".include 
\"asm/asm-defns.h\"",,-no-integrated-as)
+t2 = $(call as-insn,$(CC) -I$(srctree)/arch/x86/include,".include 
\"asm/asm-defns.h\"",,-no-integrated-as)
 
 # Check whether clang keeps .macro-s between asm()-s:
 # https://bugs.llvm.org/show_bug.cgi?id=36110
@@ -329,7 +328,7 @@ ALL_OBJS-$(CONFIG_CRYPTO) += crypto/built_in.o
 
 ALL_LIBS-y:= lib/lib.a
 
-include $(BASEDIR)/arch/$(TARGET_ARCH)/arch.mk
+include $(srctree)/arch/$(TARGET_ARCH)/arch.mk
 
 # define new variables to avoid the ones defined in Config.mk
 export XEN_CFLAGS := $(CFLAGS)
diff --git a/xen/Rules.mk b/xen/Rules.mk
index 7712bfa063e0..57a029455586 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -9,13 +9,16 @@ endif
 
 src := $(obj)
 
+# shortcuts
+srcdir := $(srctree)/$(src)
+
 PHONY := __build
 __build:
 
 -include $(objtree)/include/config/auto.conf
 
 include $(XEN_ROOT)/Config.mk
-include $(BASEDIR)/scripts/Kbuild.include
+include $(srctree)/scripts/Kbuild.include
 
 # Initialise some variables
 obj-y :=
@@ -58,7 +61,7 @@ cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $< $@
 # binfile
 # use e.g. $(call if_changed,binfile,binary-file varname)
 quiet_cmd_binfile = BINFILE $@
-cmd_binfile = $(SHELL) $(BASEDIR)/tools/binfile $(BINFILE_FLAGS) $@ $(2)
+cmd_binfile = $(SHELL) $(srctree)/tools/binfile $(BINFILE_FLAGS) $@ $(2)
 
 # Figure out what we need to build from the various variables
 # ===
@@ -177,7 +180,7 @@ cpp_flags = $(filter-out -Wa$(comma)% -flto,$(1))
 c_flags = -MMD -MP -MF $(depfile) $(XEN_CFLAGS)
 a_flags = -MMD -MP -MF $(depfile) $(XEN_AFLAGS)
 
-include $(BASEDIR)/arch/$(TARGET_ARCH)/Rules.mk
+include $(srctree)/arch/$(TARGET_ARCH)/Rules.mk
 
 c_flags += $(_c_flags)
 a_flags += $(_c_flags)
diff --git a/xen/arch/x86/arch.mk b/xen/arch/x86/arch.mk
index 8bc0e01ceb2b..2fcb6271faf7 100644
--- a/xen/arch/x86/arch.mk
+++ b/xen/arch/x86/arch.mk
@@ -63,7 +63,7 @@ ifneq ($(CONFIG_PV_SHIM_EXCLUSIVE),y)
 efi-check := arch/x86/efi/check
 
 # Check if the compiler supports the MS ABI.
-XEN_BUILD_EFI := $(call if-success,$(CC) $(CFLAGS) -c $(efi-check).c -o 
$(efi-check).o,y)
+XEN_BUILD_EFI := $(call if-success,$(CC) $(CFLAGS) -c 
$(srctree)/$(efi-check).c -o $(efi-check).o,y)
 
 # Check if the linker supports PE.
 EFI_LDFLAGS := $(patsubst -m%,-mi386pep,$(LDFLAGS)) --subsystem=10
diff --git a/xen/build.mk b/xen/build.mk
index e718743ef

[XEN PATCH v9 30/30] build: adding out-of-tree support to the xen build

2022-01-25 Thread Anthony PERARD
This implement out-of-tree support, there's two ways to create an
out-of-tree build tree (after that, `make` in that new directory
works):
make O=build
mkdir build; cd build; make -f ../Makefile
also works with an absolute path for both.

This implementation only works if the source tree is clean, as we use
VPATH.

This patch copies most new code with handling out-of-tree build from
Linux v5.12.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---

Notes:
v9:
- acked
- add "source -> ." symlink in the in-tree build as well. This allow to
  make relative symlink to the "common/efi/*.c" sources.
- also now, the "source" symlink is removed on `distclean`, add added to
  .gitignore.

v8:
- replace script/mkmakefile by cmd_makefile like it's been done in Linux
  5.13.
- fix GNU Make version, replace few 0.81 by 3.81.
- reorder include header path in CFLAGS ( -I ), to have the $(objtree)
  paths listed before the $(srctree) paths. This will be usefull later
  if we allow to build out-of-tree from a dirty source tree.
- make use of -iquote
- sometime, add -iquote path even when not necessary when building in-tree.
- extract code movement into a separate patch
- make use of the new "$(srcdir)" shortcut
- split command line in headers*.chk target into more lines

 .gitignore   |   1 +
 xen/Makefile | 143 +++
 xen/Rules.mk |  11 ++-
 xen/arch/x86/arch.mk |   5 +-
 xen/arch/x86/boot/Makefile   |   6 ++
 xen/common/efi/efi-common.mk |   3 +-
 xen/include/Makefile |  11 ++-
 xen/test/livepatch/Makefile  |   2 +
 xen/xsm/flask/Makefile   |   3 +-
 xen/xsm/flask/ss/Makefile|   1 +
 10 files changed, 164 insertions(+), 22 deletions(-)

diff --git a/.gitignore b/.gitignore
index cc87a7dfae7b..0464098ed2b5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -329,6 +329,7 @@ xen/include/xen/*.new
 xen/include/xen/acm_policy.h
 xen/include/xen/compile.h
 xen/include/xen/lib/x86/cpuid-autogen.h
+xen/source
 xen/test/livepatch/config.h
 xen/test/livepatch/expect_config.h
 xen/test/livepatch/*.livepatch
diff --git a/xen/Makefile b/xen/Makefile
index 08114b3fef1e..499f9580dab8 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -1,3 +1,7 @@
+# $(lastword,) for GNU Make older than 3.81
+lastword = $(word $(words $(1)),$(1))
+this-makefile := $(call lastword,$(MAKEFILE_LIST))
+
 # This is the correct place to edit the build version.
 # All other places this is stored (eg. compile.h) should be autogenerated.
 export XEN_VERSION   = 4
@@ -19,6 +23,13 @@ export PYTHON?= $(PYTHON_INTERPRETER)
 
 export CHECKPOLICY ?= checkpolicy
 
+$(if $(filter __%, $(MAKECMDGOALS)), \
+$(error targets prefixed with '__' are only for internal use))
+
+# That's our default target when none is given on the command line
+PHONY := __all
+__all:
+
 # Do not use make's built-in rules and variables
 MAKEFLAGS += -rR
 
@@ -37,9 +48,6 @@ export CC CXX LD
 
 export TARGET := xen
 
-.PHONY: default
-default: build
-
 .PHONY: dist
 dist: install
 
@@ -118,16 +126,88 @@ endif
 
 export quiet Q KBUILD_VERBOSE
 
+# $(realpath,) for GNU Make older than 3.81
+realpath = $(wildcard $(foreach file,$(1),$(shell cd -P $(dir $(file)) && echo 
"$$PWD/$(notdir $(file))")))
+
+ifeq ("$(origin O)", "command line")
+KBUILD_OUTPUT := $(O)
+endif
+
+ifneq ($(KBUILD_OUTPUT),)
+# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
+# expand a shell special character '~'. We use a somewhat tedious way here.
+abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
+$(if $(abs_objtree),, \
+ $(error failed to create output directory "$(KBUILD_OUTPUT)"))
+
+# $(realpath ...) resolves symlinks
+abs_objtree := $(call realpath,$(abs_objtree))
+else
 abs_objtree := $(CURDIR)
-abs_srctree := $(CURDIR)
+endif
+
+ifeq ($(abs_objtree),$(CURDIR))
+# Suppress "Entering directory ..." unless we are changing the work directory.
+MAKEFLAGS += --no-print-directory
+else
+need-sub-make := 1
+endif
+
+abs_srctree := $(call realpath,$(dir $(this-makefile)))
+
+ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
+$(error source directory cannot contain spaces or colons)
+endif
+
+ifneq ($(abs_srctree),$(abs_objtree))
+# Look for make include files relative to root of kernel src
+#
+# This does not become effective immediately because MAKEFLAGS is re-parsed
+# once after the Makefile is read. We need to invoke sub-make.
+MAKEFLAGS += --include-dir=$(abs_srctree)
+need-sub-make := 1
+endif
 
 export abs_srctree abs_objtree
+export root-make-done := y
+
+ifeq ($(need-sub-make),1)
+
+PHONY += $(MAKECMDGOALS) __sub-make
+
+$(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make
+   @:
+
+# Invoke a second make in the output directory, passing relevant variables
+__sub-make:
+   $(Q)$(MAKE) -C $(abs_objtree) -f $(abs_srctree)/Makefile $(

[XEN PATCH v9 28/30] build: specify source tree in include/ for prerequisite

2022-01-25 Thread Anthony PERARD
When doing an out-of-tree build, and thus setting VPATH,
GNU Make 3.81 on Ubuntu Trusty complains about Circular dependency of
include/Makefile and include/xlat.lst and drop them. The build fails
later due to headers malformed.

This might be due to bug #13529
"Incorrect circular dependancy"
https://savannah.gnu.org/bugs/?13529
which was fixed in 3.82.

Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- add potential bug report link in commit message

v8:
- make use of the new "$(srcdir)" shortcut
- move the patch ahead of the problematic patch:
build: adding out-of-tree support to the xen build

 xen/include/Makefile | 18 +-
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/xen/include/Makefile b/xen/include/Makefile
index fddf5a575bf6..c8c4bcd93bd3 100644
--- a/xen/include/Makefile
+++ b/xen/include/Makefile
@@ -45,19 +45,19 @@ public-$(CONFIG_ARM) := $(wildcard 
$(srcdir)/public/arch-arm/*.h $(srcdir)/publi
 .PHONY: all
 all: $(addprefix $(obj)/,$(headers-y))
 
-$(obj)/compat/%.h: $(obj)/compat/%.i $(src)/Makefile 
$(srctree)/tools/compat-build-header.py
+$(obj)/compat/%.h: $(obj)/compat/%.i $(srcdir)/Makefile 
$(srctree)/tools/compat-build-header.py
$(PYTHON) $(srctree)/tools/compat-build-header.py <$< $(patsubst 
$(obj)/%,%,$@) >>$@.new; \
mv -f $@.new $@
 
-$(obj)/compat/%.i: $(obj)/compat/%.c $(src)/Makefile
+$(obj)/compat/%.i: $(obj)/compat/%.c $(srcdir)/Makefile
$(CPP) $(filter-out -Wa$(comma)% -include 
%/include/xen/config.h,$(XEN_CFLAGS)) $(cppflags-y) -o $@ $<
 
-$(obj)/compat/%.c: $(src)/public/%.h $(src)/xlat.lst $(src)/Makefile 
$(srctree)/tools/compat-build-source.py
+$(obj)/compat/%.c: $(src)/public/%.h $(srcdir)/xlat.lst $(srcdir)/Makefile 
$(srctree)/tools/compat-build-source.py
mkdir -p $(@D)
$(PYTHON) $(srctree)/tools/compat-build-source.py $(srcdir)/xlat.lst 
<$< >$@.new
mv -f $@.new $@
 
-$(obj)/compat/.xlat/%.h: $(obj)/compat/%.h $(obj)/compat/.xlat/%.lst 
$(srctree)/tools/get-fields.sh $(src)/Makefile
+$(obj)/compat/.xlat/%.h: $(obj)/compat/%.h $(obj)/compat/.xlat/%.lst 
$(srctree)/tools/get-fields.sh $(srcdir)/Makefile
export PYTHON=$(PYTHON); \
while read what name; do \
$(SHELL) $(srctree)/tools/get-fields.sh "$$what" compat_$$name 
$< || exit $$?; \
@@ -65,7 +65,7 @@ $(obj)/compat/.xlat/%.h: $(obj)/compat/%.h 
$(obj)/compat/.xlat/%.lst $(srctree)/
mv -f $@.new $@
 
 .PRECIOUS: $(obj)/compat/.xlat/%.lst
-$(obj)/compat/.xlat/%.lst: $(src)/xlat.lst $(src)/Makefile
+$(obj)/compat/.xlat/%.lst: $(srcdir)/xlat.lst $(srcdir)/Makefile
mkdir -p $(@D)
grep -v '^[[:blank:]]*#' $< | sed -ne 's,@arch@,$(compat-arch-y),g' -re 
's,[[:blank:]]+$*\.h[[:blank:]]*$$,,p' >$@.new
$(call move-if-changed,$@.new,$@)
@@ -73,7 +73,7 @@ $(obj)/compat/.xlat/%.lst: $(src)/xlat.lst $(src)/Makefile
 xlat-y := $(shell sed -ne 's,@arch@,$(compat-arch-y),g' -re 
's,^[?!][[:blank:]]+[^[:blank:]]+[[:blank:]]+,,p' $(srcdir)/xlat.lst | uniq)
 xlat-y := $(filter $(patsubst compat/%,%,$(headers-y)),$(xlat-y))
 
-$(obj)/compat/xlat.h: $(addprefix $(obj)/compat/.xlat/,$(xlat-y)) 
$(obj)/config/auto.conf $(src)/Makefile
+$(obj)/compat/xlat.h: $(addprefix $(obj)/compat/.xlat/,$(xlat-y)) 
$(obj)/config/auto.conf $(srcdir)/Makefile
cat $(filter %.h,$^) >$@.new
mv -f $@.new $@
 
@@ -102,7 +102,7 @@ PUBLIC_C99_HEADERS := $(call 
public-filter-headers,public-c99-headers)
 $(src)/public/io/9pfs.h-prereq := string
 $(src)/public/io/pvcalls.h-prereq := string
 
-$(obj)/headers.chk: $(PUBLIC_ANSI_HEADERS) $(src)/Makefile
+$(obj)/headers.chk: $(PUBLIC_ANSI_HEADERS) $(srcdir)/Makefile
for i in $(filter %.h,$^); do \
$(CC) -x c -ansi -Wall -Werror -include stdint.h \
  -S -o /dev/null $$i || exit 1; \
@@ -110,7 +110,7 @@ $(obj)/headers.chk: $(PUBLIC_ANSI_HEADERS) $(src)/Makefile
done >$@.new
mv $@.new $@
 
-$(obj)/headers99.chk: $(PUBLIC_C99_HEADERS) $(src)/Makefile
+$(obj)/headers99.chk: $(PUBLIC_C99_HEADERS) $(srcdir)/Makefile
rm -f $@.new
$(foreach i, $(filter %.h,$^),\
echo "#include "\"$(i)\"  \
@@ -120,7 +120,7 @@ $(obj)/headers99.chk: $(PUBLIC_C99_HEADERS) $(src)/Makefile
|| exit $$?; echo $(i) >> $@.new;)
mv $@.new $@
 
-$(obj)/headers++.chk: $(PUBLIC_HEADERS) $(src)/Makefile
+$(obj)/headers++.chk: $(PUBLIC_HEADERS) $(srcdir)/Makefile
rm -f $@.new
if ! $(CXX) -v >/dev/null 2>&1; then  \
touch $@.new; \
-- 
Anthony PERARD




[XEN PATCH v9 22/30] build: rework coverage and ubsan CFLAGS handling

2022-01-25 Thread Anthony PERARD
When assigning a value a target-specific variable, that also affect
prerequisite of the target. This is mostly fine, but there is one case
where we will not want the COV_FLAGS added to the CFLAGS.

In arch/x86/boot, we have "head.o" with "cmdline.S" as prerequisite
and ultimately "cmdline.o", we don't want COV_FLAGS to that last one.

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
---

Notes:
v8:
- reorder target-specific assignments
- fix typo in comment
- reviewed

 xen/Rules.mk | 21 ++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/xen/Rules.mk b/xen/Rules.mk
index 5f2368805b36..abeba1ab7494 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -126,19 +126,31 @@ targets += $(targets-for-builtin)
 
 $(filter %.init.o,$(obj-y) $(obj-bin-y) $(extra-y)): CFLAGS-y += 
-DINIT_SECTIONS_ONLY
 
+non-init-objects = $(filter-out %.init.o, $(obj-y) $(obj-bin-y) $(extra-y))
+
 ifeq ($(CONFIG_COVERAGE),y)
 ifeq ($(CONFIG_CC_IS_CLANG),y)
 COV_FLAGS := -fprofile-instr-generate -fcoverage-mapping
 else
 COV_FLAGS := -fprofile-arcs -ftest-coverage
 endif
-$(filter-out %.init.o $(nocov-y),$(obj-y) $(obj-bin-y) $(extra-y)): CFLAGS-y 
+= $(COV_FLAGS)
+
+# Reset COV_FLAGS in cases where an objects has another one as prerequisite
+$(nocov-y) $(filter %.init.o, $(obj-y) $(obj-bin-y) $(extra-y)): \
+COV_FLAGS :=
+
+$(non-init-objects): _c_flags += $(COV_FLAGS)
 endif
 
 ifeq ($(CONFIG_UBSAN),y)
 # Any -fno-sanitize= options need to come after any -fsanitize= options
-$(filter-out %.init.o $(noubsan-y),$(obj-y) $(obj-bin-y) $(extra-y)): \
-CFLAGS-y += $(filter-out -fno-%,$(CFLAGS_UBSAN)) $(filter 
-fno-%,$(CFLAGS_UBSAN))
+UBSAN_FLAGS := $(filter-out -fno-%,$(CFLAGS_UBSAN)) $(filter 
-fno-%,$(CFLAGS_UBSAN))
+
+# Reset UBSAN_FLAGS in cases where an objects has another one as prerequisite
+$(noubsan-y) $(filter %.init.o, $(obj-y) $(obj-bin-y) $(extra-y)): \
+UBSAN_FLAGS :=
+
+$(non-init-objects): _c_flags += $(UBSAN_FLAGS)
 endif
 
 ifeq ($(CONFIG_LTO),y)
@@ -167,6 +179,9 @@ a_flags = -MMD -MP -MF $(depfile) $(XEN_AFLAGS)
 
 include $(BASEDIR)/arch/$(TARGET_ARCH)/Rules.mk
 
+c_flags += $(_c_flags)
+a_flags += $(_c_flags)
+
 c_flags += $(CFLAGS-y)
 a_flags += $(CFLAGS-y) $(AFLAGS-y)
 
-- 
Anthony PERARD




[XEN PATCH v9 17/30] build: add headers path to CFLAGS once for all archs

2022-01-25 Thread Anthony PERARD
This just remove duplication.

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
Acked-by: Julien Grall 
---
 xen/Makefile   | 3 +++
 xen/arch/arm/arch.mk   | 3 ---
 xen/arch/riscv/arch.mk | 2 --
 xen/arch/x86/arch.mk   | 2 --
 4 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index 6c5c69d2e19b..65b09c20a599 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -312,6 +312,9 @@ CFLAGS += -flto
 LDFLAGS-$(CONFIG_CC_IS_CLANG) += -plugin LLVMgold.so
 endif
 
+CFLAGS += -I$(srctree)/include
+CFLAGS += -I$(srctree)/arch/$(TARGET_ARCH)/include
+
 # Note that link order matters!
 ALL_OBJS-y:= common/built_in.o
 ALL_OBJS-y+= drivers/built_in.o
diff --git a/xen/arch/arm/arch.mk b/xen/arch/arm/arch.mk
index 4e3f7014305e..094b67072304 100644
--- a/xen/arch/arm/arch.mk
+++ b/xen/arch/arm/arch.mk
@@ -1,9 +1,6 @@
 
 # arm-specific definitions
 
-CFLAGS += -I$(srctree)/include
-CFLAGS += -I$(srctree)/arch/$(TARGET_ARCH)/include
-
 $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS))
 $(call cc-option-add,CFLAGS,CC,-Wnested-externs)
 
diff --git a/xen/arch/riscv/arch.mk b/xen/arch/riscv/arch.mk
index 694ba053ceab..ae8fe9dec730 100644
--- a/xen/arch/riscv/arch.mk
+++ b/xen/arch/riscv/arch.mk
@@ -11,5 +11,3 @@ riscv-march-$(CONFIG_RISCV_ISA_C)   := $(riscv-march-y)c
 # -mcmodel=medlow would force Xen into the lower half.
 
 CFLAGS += -march=$(riscv-march-y) -mstrict-align -mcmodel=medany
-CFLAGS += -I$(srctree)/include
-CFLAGS += -I$(srctree)/arch/$(TARGET_ARCH)/include
diff --git a/xen/arch/x86/arch.mk b/xen/arch/x86/arch.mk
index e39fa15be7e3..8bc0e01ceb2b 100644
--- a/xen/arch/x86/arch.mk
+++ b/xen/arch/x86/arch.mk
@@ -3,8 +3,6 @@
 
 export XEN_IMG_OFFSET := 0x20
 
-CFLAGS += -I$(srctree)/include
-CFLAGS += -I$(srctree)/arch/$(TARGET_ARCH)/include
 CFLAGS += -I$(srctree)/arch/x86/include/asm/mach-generic
 CFLAGS += -I$(srctree)/arch/x86/include/asm/mach-default
 CFLAGS += -DXEN_IMG_OFFSET=$(XEN_IMG_OFFSET)
-- 
Anthony PERARD




[XEN PATCH v9 16/30] build: start building the tools with the main makefiles

2022-01-25 Thread Anthony PERARD
This will make out-of-tree build easier.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---

Notes:
v8:
- add some $(Q)

 xen/Makefile   |  8 
 xen/tools/Makefile | 17 ++---
 2 files changed, 6 insertions(+), 19 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index 0705e67a9e7d..6c5c69d2e19b 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -209,7 +209,7 @@ endif # root-make-done
 
 PHONY += tools_fixdep
 tools_fixdep:
-   $(MAKE) -C tools fixdep
+   $(Q)$(MAKE) $(build)=tools tools/fixdep
 
 ifeq ($(config-build),y)
 # ===
@@ -247,7 +247,7 @@ ifeq ($(need-config),y)
 # "tools_fixdep" which is a .PHONY target and would force make to call
 # "defconfig" again to update $(KCONFIG_CONFIG).
 tools/fixdep:
-   $(MAKE) -C tools fixdep
+   $(Q)$(MAKE) $(build)=tools tools/fixdep
 
 # Allow people to just run `make` as before and not force them to configure
 $(KCONFIG_CONFIG): tools/fixdep
@@ -406,7 +406,7 @@ _debug:
 
 .PHONY: _clean
 _clean:
-   $(MAKE) -C tools clean
+   $(MAKE) $(clean) tools
$(MAKE) $(clean) include
$(MAKE) $(clean) common
$(MAKE) $(clean) drivers
@@ -435,7 +435,7 @@ $(TARGET).gz: $(TARGET)
mv $@.new $@
 
 $(TARGET): FORCE
-   $(MAKE) -C tools
+   $(Q)$(MAKE) $(build)=tools
$(Q)$(MAKE) $(build)=. include/xen/compile.h
[ -e arch/$(TARGET_ARCH)/efi ] && for f in $$(cd common/efi; echo 
*.[ch]); \
do test -r arch/$(TARGET_ARCH)/efi/$$f || \
diff --git a/xen/tools/Makefile b/xen/tools/Makefile
index 722f3664549d..a5078b7cb8de 100644
--- a/xen/tools/Makefile
+++ b/xen/tools/Makefile
@@ -1,15 +1,2 @@
-
-include $(XEN_ROOT)/Config.mk
-
-.PHONY: default
-default: symbols fixdep
-
-.PHONY: clean
-clean:
-   rm -f *.o symbols fixdep
-
-symbols: symbols.c
-   $(HOSTCC) $(HOSTCFLAGS) -o $@ $<
-
-fixdep: fixdep.c
-   $(HOSTCC) $(HOSTCFLAGS) -o $@ $<
+hostprogs-always-y += symbols
+hostprogs-always-y += fixdep
-- 
Anthony PERARD




[XEN PATCH v9 08/30] build: fix enforce unique symbols for recent clang version

2022-01-25 Thread Anthony PERARD
clang 6.0 and newer behave like gcc in regards for the FILE symbol, so
only the filename rather than the full path to the source file.

clang 3.8.1-24 (in our debian:stretch container) and 3.5.0-10
(in our debian:jessie container) do store the full path to the source
file in the FILE symbol.

Also, based on commit 81ecb38b83 ("build: provide option to
disambiguate symbol names"), which were using clang 5, the change of
behavior likely happened in clang 6.0.

This means that we also need to check clang version to figure out
which command we need to use to redefine symbol.

Signed-off-by: Anthony PERARD 
---

"enforce unique symbols" works by chance with recent clang version.
The few object built from source in subdir don't pose an issue.
---

Notes:
v9:
- checking for clang 6 instead of clang 4, based on 81ecb38b83, and
  update commit message.

v8:
- new patch, extracted from "build: build everything from the root dir, use 
obj=$subdir"

 xen/Rules.mk   | 2 +-
 xen/scripts/Kbuild.include | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/xen/Rules.mk b/xen/Rules.mk
index 60d1d6c4f583..1e7f47a3d8a8 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -166,7 +166,7 @@ SRCPATH := $(patsubst $(BASEDIR)/%,%,$(CURDIR))
 quiet_cmd_cc_o_c = CC  $@
 ifeq ($(CONFIG_ENFORCE_UNIQUE_SYMBOLS),y)
 cmd_cc_o_c = $(CC) $(c_flags) -c $< -o $(dot-target).tmp -MQ $@
-ifeq ($(CONFIG_CC_IS_CLANG),y)
+ifeq ($(CONFIG_CC_IS_CLANG)$(call clang-ifversion,-lt,600,y),yy)
 cmd_objcopy_fix_sym = $(OBJCOPY) --redefine-sym $<=$(SRCPATH)/$< 
$(dot-target).tmp $@
 else
 cmd_objcopy_fix_sym = $(OBJCOPY) --redefine-sym $(

[XEN PATCH v9 06/30] build: rework test/livepatch/Makefile

2022-01-25 Thread Anthony PERARD
This rework the livepatch/Makefile to make it less repetitive and make
use of the facilities. All the targets to be built are now listed in
$(extra-y) which will allow Rules.mk to build them without the need of
a local target in a future patch.

There are some changes/fixes in this patch:
- when "xen-syms" is used for a target, it is added to the dependency
  list of the target, which allow to rebuild the target when xen-syms
  changes. But if "xen-syms" is missing, make simply fails.
- modinfo.o wasn't removing it's $@.bin file like the other targets,
  this is now done.
- The command to build *.livepatch targets as been fixed to use
  $(XEN_LDFLAGS) rather than just $(LDFLAGS) which is a fallout from
  2740d96efdd3 ("xen/build: have the root Makefile generates the
  CFLAGS")

make will findout the dependencies of the *.livepatch files and thus
what to built by "looking" at the objects listed in the *-objs
variables. The actual dependencies is generated by the new
"multi-depend" macro.

"$(targets)" needs to be updated with the objects listed in the
different *-objs variables to allow make to load the .*.cmd dependency
files.

This patch copies the macro "multi_depend" from Linux 5.12, and rename
it to "multi-depend".

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---

Notes:
v9:
- half acked, still need "livepatch" maintainer ack.

v8:
- rename multi_depend to multi-depend
- use $() for single-letter make variable
- re-indent one line

 xen/scripts/Kbuild.include  |   9 ++
 xen/test/livepatch/Makefile | 213 ++--
 2 files changed, 91 insertions(+), 131 deletions(-)

diff --git a/xen/scripts/Kbuild.include b/xen/scripts/Kbuild.include
index f75d724ed7fd..73caf238d42c 100644
--- a/xen/scripts/Kbuild.include
+++ b/xen/scripts/Kbuild.include
@@ -151,3 +151,12 @@ why =  
  \
 
 echo-why = $(call escsq, $(strip $(why)))
 endif
+
+# Useful for describing the dependency of composite objects
+# Usage:
+#   $(call multi-depend, multi-used-targets, suffix-to-remove, suffix-to-add)
+define multi-depend
+$(foreach m, $(notdir $1), \
+   $(eval $(obj)/$(m): \
+   $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))
+endef
diff --git a/xen/test/livepatch/Makefile b/xen/test/livepatch/Makefile
index 148dddb90473..69fadccd01ea 100644
--- a/xen/test/livepatch/Makefile
+++ b/xen/test/livepatch/Makefile
@@ -12,81 +12,29 @@ CODE_ADDR=$(shell nm --defined $(1) | grep $(2) | awk 
'{print "0x"$$1}')
 CODE_SZ=$(shell nm --defined -S $(1) | grep $(2) | awk '{ print "0x"$$2}')
 
 .PHONY: default
-
-LIVEPATCH := xen_hello_world.livepatch
-LIVEPATCH_BYE := xen_bye_world.livepatch
-LIVEPATCH_REPLACE := xen_replace_world.livepatch
-LIVEPATCH_NOP := xen_nop.livepatch
-LIVEPATCH_NO_XEN_BUILDID := xen_no_xen_buildid.livepatch
-LIVEPATCH_PREPOST_HOOKS := xen_prepost_hooks.livepatch
-LIVEPATCH_PREPOST_HOOKS_FAIL := xen_prepost_hooks_fail.livepatch
-LIVEPATCH_ACTION_HOOKS := xen_action_hooks.livepatch
-LIVEPATCH_ACTION_HOOKS_NOFUNC := xen_action_hooks_nofunc.livepatch
-LIVEPATCH_ACTION_HOOKS_MARKER:= xen_action_hooks_marker.livepatch
-LIVEPATCH_ACTION_HOOKS_NOAPPLY:= xen_action_hooks_noapply.livepatch
-LIVEPATCH_ACTION_HOOKS_NOREVERT:= xen_action_hooks_norevert.livepatch
-LIVEPATCH_EXPECTATIONS:= xen_expectations.livepatch
-LIVEPATCH_EXPECTATIONS_FAIL:= xen_expectations_fail.livepatch
-
-LIVEPATCHES += $(LIVEPATCH)
-LIVEPATCHES += $(LIVEPATCH_BYE)
-LIVEPATCHES += $(LIVEPATCH_REPLACE)
-LIVEPATCHES += $(LIVEPATCH_NOP)
-LIVEPATCHES += $(LIVEPATCH_NO_XEN_BUILDID)
-LIVEPATCHES += $(LIVEPATCH_PREPOST_HOOKS)
-LIVEPATCHES += $(LIVEPATCH_PREPOST_HOOKS_FAIL)
-LIVEPATCHES += $(LIVEPATCH_ACTION_HOOKS)
-LIVEPATCHES += $(LIVEPATCH_ACTION_HOOKS_NOFUNC)
-LIVEPATCHES += $(LIVEPATCH_ACTION_HOOKS_MARKER)
-LIVEPATCHES += $(LIVEPATCH_ACTION_HOOKS_NOAPPLY)
-LIVEPATCHES += $(LIVEPATCH_ACTION_HOOKS_NOREVERT)
-LIVEPATCHES += $(LIVEPATCH_EXPECTATIONS)
-LIVEPATCHES += $(LIVEPATCH_EXPECTATIONS_FAIL)
-
-LIVEPATCH_DEBUG_DIR ?= $(DEBUG_DIR)/xen-livepatch
-
 build default: livepatch
 
-install: livepatch
-   $(INSTALL_DIR) $(DESTDIR)$(LIVEPATCH_DEBUG_DIR)
-   $(INSTALL_DATA) $(LIVEPATCHES) $(DESTDIR)$(LIVEPATCH_DEBUG_DIR)
-
-uninstall:
-   cd $(DESTDIR)$(LIVEPATCH_DEBUG_DIR) && rm -f $(LIVEPATCHES)
-
-.PHONY: clean
-clean::
-   rm -f *.o .*.o.d *.livepatch config.h expect_config.h
+extra-y += xen_hello_world.livepatch
+xen_hello_world-objs := xen_hello_world_func.o xen_hello_world.o note.o 
xen_note.o modinfo.o
+$(obj)/xen_hello_world.o: $(obj)/config.h
 
 #
 # To compute these values we need the binary files: xen-syms
 # and xen_hello_world_func.o to be already compiled.
 #
-.PHONY: config.h
-config.h: OLD_CODE_SZ=$(call CODE_SZ,$(BASEDIR)/xen-syms,xen_extra_version)
-config.h: NEW_CODE_SZ=$(call CODE_SZ,$<,xen_hello_world)
-config.h: MINOR_VERSION_SZ=$(call 
CODE_SZ,$(BASEDIR)/xen-syms,xen_minor_ve

[XEN PATCH v9 07/30] build: rework cloc recipe

2022-01-25 Thread Anthony PERARD
We are going to make other modifications to the cloc recipe, so this
patch prepare make those modification easier.

We replace the Makefile meta programming by just a shell script which
should be easier to read and is actually faster to execute.

Instead of looking for files in "$(BASEDIR)", we use "." which is give
the same result overall. We also avoid the need for a temporary file
as cloc can read the list of files from stdin.

No change intended to the output of `cloc`.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
---

Notes:
v9:
- acked

v8:
- rework cloc patch, move it ahead of problematic changes
  this is only a convertion to shell script with a single other change.

 xen/Makefile | 14 ++
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index d953dc50ac6a..ec24856a5d46 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -473,14 +473,12 @@ _MAP:
 
 .PHONY: cloc
 cloc:
-   $(eval tmpfile := $(shell mktemp))
-   $(foreach f, $(shell find $(BASEDIR) -name *.o.d), \
-   $(eval path := $(dir $(f))) \
-   $(eval names := $(shell grep -o "[a-zA-Z0-9_/-]*\.[cS]" $(f))) \
-   $(foreach sf, $(names), \
-   $(shell if test -f $(path)/$(sf) ; then echo 
$(path)/$(sf) >> $(tmpfile); fi;)))
-   cloc --list-file=$(tmpfile)
-   rm $(tmpfile)
+   find . -name '*.o.d' | while read f; do \
+   for sf in $$(grep -o "[a-zA-Z0-9_/-]*\.[cS]" $$f); do \
+   sf="$$(dirname $$f)/$$sf"; \
+   test -f "$$sf" && echo "$$sf"; \
+   done; \
+   done | cloc --list-file=-
 
 endif #config-build
 
-- 
Anthony PERARD




[XEN PATCH v9 05/30] build: prepare to always invoke $(MAKE) from xen/, use $(obj)

2022-01-25 Thread Anthony PERARD
In a future patch, when building a subdirectory, we will set
"obj=$subdir" rather than change directory.

Before that, we add "$(obj)" and "$(src)" in as many places as
possible where we will need to know which subdirectory is been built.
"$(obj)" is for files been generated during the build, and "$(src)" is
for files present in the source tree.

For now, we set both to "." in Rules.mk and Makefile.clean.

A few places don't tolerate the addition of "./", this is because make
remove the leading "./" in targets and dependencies in rules, so these
will be change later.

Signed-off-by: Anthony PERARD 
Acked-by: Jan Beulich 
Acked-by: Julien Grall 
---

Notes:
v8:
- acked
- rebased

 xen/Rules.mk|  5 ++-
 xen/arch/arm/Makefile   | 14 
 xen/arch/x86/Makefile   | 48 -
 xen/arch/x86/boot/Makefile  | 14 
 xen/arch/x86/efi/Makefile   |  6 ++--
 xen/common/Makefile |  8 ++---
 xen/common/libelf/Makefile  |  4 +--
 xen/common/libfdt/Makefile  |  6 ++--
 xen/include/Makefile| 44 +++
 xen/scripts/Makefile.clean  |  5 ++-
 xen/xsm/flask/Makefile  | 36 +--
 xen/xsm/flask/policy/mkaccess_vector.sh |  7 ++--
 12 files changed, 103 insertions(+), 94 deletions(-)

diff --git a/xen/Rules.mk b/xen/Rules.mk
index 77d359bedaf8..60d1d6c4f583 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -3,6 +3,9 @@
 # Makefile and are consumed by Rules.mk
 #
 
+obj := .
+src := $(obj)
+
 -include $(BASEDIR)/include/config/auto.conf
 
 include $(XEN_ROOT)/Config.mk
@@ -21,7 +24,7 @@ SPECIAL_DATA_SECTIONS := rodata $(foreach a,1 2 4 8 16, \
  $(foreach r,rel rel.ro,data.$(r).local)
 
 # The filename build.mk has precedence over Makefile
-include $(firstword $(wildcard build.mk) Makefile)
+include $(firstword $(wildcard $(src)/build.mk) $(src)/Makefile)
 
 # Linking
 # ---
diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile
index 14952275772b..c993ce72a341 100644
--- a/xen/arch/arm/Makefile
+++ b/xen/arch/arm/Makefile
@@ -90,18 +90,18 @@ ifeq ($(CONFIG_ARM_64),y)
ln -sf $(@F) $@.efi
 endif
 
-$(TARGET)-syms: $(BASEDIR)/prelink.o xen.lds
-   $(LD) $(XEN_LDFLAGS) -T xen.lds -N $< \
+$(TARGET)-syms: $(BASEDIR)/prelink.o $(obj)/xen.lds
+   $(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< \
$(BASEDIR)/common/symbols-dummy.o -o $(@D)/.$(@F).0
$(NM) -pa --format=sysv $(@D)/.$(@F).0 \
| $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort 
>$(@D)/.$(@F).0.S
$(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).0.o
-   $(LD) $(XEN_LDFLAGS) -T xen.lds -N $< \
+   $(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< \
$(@D)/.$(@F).0.o -o $(@D)/.$(@F).1
$(NM) -pa --format=sysv $(@D)/.$(@F).1 \
| $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort 
>$(@D)/.$(@F).1.S
$(MAKE) -f $(BASEDIR)/Rules.mk $(@D)/.$(@F).1.o
-   $(LD) $(XEN_LDFLAGS) -T xen.lds -N $< $(build_id_linker) \
+   $(LD) $(XEN_LDFLAGS) -T $(obj)/xen.lds -N $< $(build_id_linker) \
$(@D)/.$(@F).1.o -o $@
$(NM) -pa --format=sysv $(@D)/$(@F) \
| $(BASEDIR)/tools/symbols --all-symbols --xensyms --sysv 
--sort \
@@ -111,13 +111,13 @@ $(TARGET)-syms: $(BASEDIR)/prelink.o xen.lds
 .PHONY: include
 include:
 
-xen.lds: xen.lds.S FORCE
+$(obj)/xen.lds: $(src)/xen.lds.S FORCE
$(call if_changed,cpp_lds_S)
 
-dtb.o: $(patsubst "%",%,$(CONFIG_DTB_FILE))
+$(obj)/dtb.o: $(patsubst "%",%,$(CONFIG_DTB_FILE))
 
 .PHONY: clean
 clean::
-   rm -f xen.lds
+   rm -f $(obj)/xen.lds
rm -f $(BASEDIR)/.xen-syms.[0-9]*
rm -f $(TARGET).efi
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index a830b5791e8b..db97ae8c07f0 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -81,7 +81,7 @@ extra-y += asm-macros.i
 extra-y += xen.lds
 
 ifneq ($(CONFIG_HVM),y)
-x86_emulate.o: CFLAGS-y += -Wno-unused-label
+$(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
 endif
 
 efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \
@@ -112,8 +112,8 @@ syms-warn-dup-$(CONFIG_SUPPRESS_DUPLICATE_SYMBOL_WARNINGS) 
:=
 syms-warn-dup-$(CONFIG_ENFORCE_UNIQUE_SYMBOLS) := --error-dup
 
 $(TARGET): TMP = $(@D)/.$(@F).elf32
-$(TARGET): $(TARGET)-syms $(efi-y) boot/mkelf32
-   ./boot/mkelf32 $(notes_phdrs) $(TARGET)-syms $(TMP) $(XEN_IMG_OFFSET) \
+$(TARGET): $(TARGET)-syms $(efi-y) $(obj)/boot/mkelf32
+   $(obj)/boot/mkelf32 $(notes_phdrs) $(TARGET)-syms $(TMP) 
$(XEN_IMG_OFFSET) \
   `$(NM) $(TARGET)-syms | sed -ne 's/^\([^ ]*\) . 
__2M_rwdata_end$$/0x\1/p'`
od -t x4 -N 8192 $(TMP)  | grep 1badb002 > /dev/null || \
{ echo "No Multiboot1 he

[XEN PATCH v9 04/30] build: set ALL_OBJS in main Makefile; move prelink.o to main Makefile

2022-01-25 Thread Anthony PERARD
This is to avoid arch/$arch/Makefile having to recurse into parents
directories.

This avoid duplication of the logic to build prelink.o between arches.

In order to do that, we cut the $(TARGET) target in the main Makefile in
two, there is a "prepare" phase/target runned before starting to build
"prelink.o" which will prepare "include/" among other things, then all
the $(ALL_OBJS) will be generated in order to build "prelink.o" and
finally $(TARGET) will be generated by calling into "arch/*/" to make
$(TARGET).

Now we don't need to prefix $(ALL_OBJS) with $(BASEDIR) as it is now
only used from the main Makefile. Other changes is to use "$<" instead
of spelling "prelink.o" in the target "$(TARGET)" in both
arch/*/Makefile.

Beside "prelink.o" been at a different location, no other functional
change intended.

Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- set ALL_OBJS and ALL_LIBS on make command line instead of exporting
  them.
- arm/Rules.mk, have one rule instead of two for head.o
- fix typo in title

v8:
- rebased
- move Arm specific dependencies between $(ALL_OBJS) objects (head.o)
  into Arm specific "Rules.mk" instead of the common "build.mk".

v7:
- change, now things are in build.mk: no more prepare phase needed

 xen/Makefile  | 12 +++-
 xen/Rules.mk  | 13 -
 xen/arch/arm/Makefile | 31 ---
 xen/arch/arm/Rules.mk |  4 
 xen/arch/arm/arch.mk  |  2 ++
 xen/arch/x86/Makefile | 29 ++---
 xen/arch/x86/arch.mk  |  2 ++
 xen/build.mk  | 18 ++
 8 files changed, 47 insertions(+), 64 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index fb37043d08e0..d953dc50ac6a 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -285,6 +285,16 @@ CFLAGS += -flto
 LDFLAGS-$(CONFIG_CC_IS_CLANG) += -plugin LLVMgold.so
 endif
 
+# Note that link order matters!
+ALL_OBJS-y:= common/built_in.o
+ALL_OBJS-y+= drivers/built_in.o
+ALL_OBJS-y+= lib/built_in.o
+ALL_OBJS-y+= xsm/built_in.o
+ALL_OBJS-y+= arch/$(TARGET_ARCH)/built_in.o
+ALL_OBJS-$(CONFIG_CRYPTO) += crypto/built_in.o
+
+ALL_LIBS-y:= lib/lib.a
+
 include $(BASEDIR)/arch/$(TARGET_ARCH)/arch.mk
 
 # define new variables to avoid the ones defined in Config.mk
@@ -407,7 +417,7 @@ $(TARGET): FORCE
$(MAKE) -f $(BASEDIR)/Rules.mk -C include
$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) include
$(MAKE) -f $(BASEDIR)/Rules.mk 
arch/$(TARGET_ARCH)/include/asm/asm-offsets.h
-   $(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) 
MKRELOC=$(MKRELOC) $@
+   $(MAKE) -f $(BASEDIR)/Rules.mk MKRELOC=$(MKRELOC) 
"ALL_OBJS=$(ALL_OBJS-y)" "ALL_LIBS=$(ALL_LIBS-y)" $@
 
 SUBDIRS = xsm arch/$(TARGET_ARCH) common drivers lib test
 define all_sources
diff --git a/xen/Rules.mk b/xen/Rules.mk
index 7b8b9047cfd5..77d359bedaf8 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -8,25 +8,12 @@
 include $(XEN_ROOT)/Config.mk
 include $(BASEDIR)/scripts/Kbuild.include
 
-# Note that link order matters!
-ALL_OBJS-y   += $(BASEDIR)/common/built_in.o
-ALL_OBJS-y   += $(BASEDIR)/drivers/built_in.o
-ALL_OBJS-y   += $(BASEDIR)/lib/built_in.o
-ALL_OBJS-y   += $(BASEDIR)/xsm/built_in.o
-ALL_OBJS-y   += $(BASEDIR)/arch/$(TARGET_ARCH)/built_in.o
-ALL_OBJS-$(CONFIG_CRYPTO)   += $(BASEDIR)/crypto/built_in.o
-
-ALL_LIBS-y   := $(BASEDIR)/lib/lib.a
-
 # Initialise some variables
 lib-y :=
 targets :=
 CFLAGS-y :=
 AFLAGS-y :=
 
-ALL_OBJS := $(ALL_OBJS-y)
-ALL_LIBS := $(ALL_LIBS-y)
-
 SPECIAL_DATA_SECTIONS := rodata $(foreach a,1 2 4 8 16, \
 $(foreach w,1 2 4, \
 rodata.str$(w).$(a)) \
diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile
index d0dee10102b6..14952275772b 100644
--- a/xen/arch/arm/Makefile
+++ b/xen/arch/arm/Makefile
@@ -75,14 +75,6 @@ ifneq ($(CONFIG_DTB_FILE),"")
 obj-y += dtb.o
 endif
 
-ALL_OBJS := $(TARGET_SUBARCH)/head.o $(ALL_OBJS)
-
-# head.o is built by descending into the sub-directory, depends on the part of
-# $(ALL_OBJS) that will eventually recurse into $(TARGET_SUBARCH)/ and build
-# head.o
-$(TARGET_SUBARCH)/head.o: $(BASEDIR)/arch/arm/built_in.o
-$(TARGET_SUBARCH)/head.o: ;
-
 ifdef CONFIG_LIVEPATCH
 all_symbols = --all-symbols
 ifdef CONFIG_FAST_SYMBOL_LOOKUP
@@ -98,33 +90,18 @@ ifeq ($(CONFIG_ARM_64),y)
ln -sf $(@F) $@.efi
 endif
 
-ifeq ($(CONFIG_LTO),y)
-# Gather all LTO objects together
-prelink_lto.o: $(ALL_OBJS) $(ALL_LIBS)
-   $(LD_LTO) -r -o $@ $(filter-out %.a,$^) --start-group $(filter %.a,$^) 
--end-group
-
-# Link it with all the binary objects
-prelink.o: $(patsubst %/built_in.o,%/built_in_bin.o,$(ALL_OBJS)) prelink_lto.o
-   $(call if_changed,ld)
-else
-prelink.o: $(ALL_OBJS) $

[XEN PATCH v9 02/30] build: avoid re-executing the main Makefile by introducing build.mk

2022-01-25 Thread Anthony PERARD
Currently, the xen/Makefile is re-parsed several times: once to start
the build process, and several more time with Rules.mk including it.
This makes it difficult to work with a Makefile used for several
purpose, and it actually slow down the build process.

So this patch introduce "build.mk" which Rules.mk will use when
present instead of the "Makefile" of a directory. (Linux's Kbuild
named that file "Kbuild".)

We have a few targets to move to "build.mk" identified by them been
build via "make -f Rules.mk" without changing directory.

As for the main targets like "build", we can have them depends on
there underscore-prefix targets like "_build" without having to use
"Rules.mk" while still retaining the check for unsupported
architecture. (Those main rules are changed to be single-colon as
there should only be a single recipe for them.)

With nearly everything needed to move to "build.mk" moved, there is a
single dependency left from "Rules.mk": the variable $(TARGET), so its
assignement is moved to the main Makefile.

Signed-off-by: Anthony PERARD 
Reviewed-by: Jan Beulich 
---

Notes:
v9:
- reviewed

v8:
- for $(main-targets) rules, use ; on a single line instead of adding a
  recipe with only @:
- To include build.mk instead of Makefile, use a simpler expresion with
  $(firstword) and remove the use of $(mk-dir) var
- removed an extra blank line, and one when updating $(targets)
- reword patch description

v7:
- new patch

 xen/Makefile | 70 +---
 xen/Rules.mk |  6 ++---
 xen/build.mk | 61 +
 3 files changed, 69 insertions(+), 68 deletions(-)
 create mode 100644 xen/build.mk

diff --git a/xen/Makefile b/xen/Makefile
index f3ff03a7170e..fb37043d08e0 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -47,6 +47,8 @@ export KCONFIG_CONFIG ?= .config
 
 export CC CXX LD
 
+export TARGET := $(BASEDIR)/xen
+
 .PHONY: default
 default: build
 
@@ -293,11 +295,12 @@ export CFLAGS_UBSAN
 
 endif # need-config
 
-.PHONY: build install uninstall clean distclean MAP
-build install uninstall debug clean distclean MAP::
+main-targets := build install uninstall clean distclean MAP
+.PHONY: $(main-targets)
 ifneq ($(XEN_TARGET_ARCH),x86_32)
-   $(MAKE) -f Rules.mk MKRELOC=$(MKRELOC) _$@
+$(main-targets): %: _% ;
 else
+$(main-targets):
echo "*** Xen x86/32 target no longer supported!"
 endif
 
@@ -406,67 +409,6 @@ $(TARGET): FORCE
$(MAKE) -f $(BASEDIR)/Rules.mk 
arch/$(TARGET_ARCH)/include/asm/asm-offsets.h
$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) 
MKRELOC=$(MKRELOC) $@
 
-quiet_cmd_banner = BANNER  $@
-define cmd_banner
-if which figlet >/dev/null 2>&1 ; then \
-   echo " Xen $(XEN_FULLVERSION)" | figlet -f $< > $@.tmp; \
-else \
-   echo " Xen $(XEN_FULLVERSION)" > $@.tmp; \
-fi; \
-mv -f $@.tmp $@
-endef
-
-.banner: tools/xen.flf FORCE
-   $(call if_changed,banner)
-targets += .banner
-
-# Don't refresh this files during e.g., 'sudo make install'
-quiet_cmd_compile.h = UPD $@
-define cmd_compile.h
-if [ ! -r $@ -o -O $@ ]; then \
-   cat .banner; \
-   sed -e 's/@@date@@/$(XEN_BUILD_DATE)/g' \
-   -e 's/@@time@@/$(XEN_BUILD_TIME)/g' \
-   -e 's/@@whoami@@/$(XEN_WHOAMI)/g' \
-   -e 's/@@domain@@/$(XEN_DOMAIN)/g' \
-   -e 's/@@hostname@@/$(XEN_BUILD_HOST)/g' \
-   -e 's!@@compiler@@!$(shell $(CC) $(CFLAGS) --version 2>&1 | head 
-1)!g' \
-   -e 's/@@version@@/$(XEN_VERSION)/g' \
-   -e 's/@@subversion@@/$(XEN_SUBVERSION)/g' \
-   -e 's/@@extraversion@@/$(XEN_EXTRAVERSION)/g' \
-   -e 's!@@changeset@@!$(shell tools/scmversion $(XEN_ROOT) || echo 
"unavailable")!g' \
-   < $< > $(dot-target).tmp; \
-   sed -rf tools/process-banner.sed < .banner >> $(dot-target).tmp; \
-   mv -f $(dot-target).tmp $@; \
-fi
-endef
-
-include/xen/compile.h: include/xen/compile.h.in .banner FORCE
-   $(if $(filter-out FORCE,$?),$(Q)rm -fv $@)
-   $(call if_changed,compile.h)
-
-targets += include/xen/compile.h
-
-asm-offsets.s: arch/$(TARGET_ARCH)/$(TARGET_SUBARCH)/asm-offsets.c
-   $(CC) $(call cpp_flags,$(c_flags)) -S -g0 -o $@.new -MQ $@ $<
-   $(call move-if-changed,$@.new,$@)
-
-arch/$(TARGET_ARCH)/include/asm/asm-offsets.h: asm-offsets.s
-   @(set -e; \
- echo "/*"; \
- echo " * DO NOT MODIFY."; \
- echo " *"; \
- echo " * This file was auto-generated from $<"; \
- echo " *"; \
- echo " */"; \
- echo ""; \
- echo "#ifndef __ASM_OFFSETS_H__"; \
- echo "#define __ASM_OFFSETS_H__"; \
- echo ""; \
- sed -rne "/^[^#].*==>/{s:.*==>(.*)<==.*:\1:; s: [\$$#]: :; p;}"; \
- echo ""; \
- echo "#endif") <$< >$@
-
 SUBDIRS = xsm arch/$(TARGET_ARCH) common drivers lib test
 define all_sources
 ( find include -ty

[XEN PATCH v9 00/30] xen: Build system improvements, now with out-of-tree build!

2022-01-25 Thread Anthony PERARD
Patch series available in this git branch:
https://xenbits.xen.org/git-http/people/aperard/xen-unstable.git 
br.build-system-xen-v9

v9:
One new patch (patch 3).
Otherwise, detailed change logs in patches.

Removed the rfc part about doing out-of-tree build without setting VPATH
(which would have allowed mixed in-tree / out-of-tree builds).

v8:
Mostly rework of v7. With many patch already applied.
Some detail changes that are spread through many patches:
- `make cloc` recipe should now work throughout the series, update of it is
  done in 3 patches.
- new patch "build: fix enforce unique symbols for recent clang version"
  to fix an issue with clang.
- introducing $(srctree) and $(objtree) earlier
- introducing $(srcdir) as shortcut for $(srctree)/$(src)
- introduce usage of -iquote instead of -I in some cases
More detail change log can be found in patches notes.

Also this v8 present a work-in-progress of the ability to do out-of-tree
build without setting VPATH. This is presented as an alternative to force
use of out-of-tree build. As the last patch show, it allows to build the
xen-shim without the linkfarm and we don't need to make any other changes
to any thing that build xen (osstest, distribution packages, xen.git, ...,
and developers finger macros). The patches are only there as WIP / RFC as
they were some concern about the usefulness and extra changes needed.
We can decide whether those changes are good or if this is too much and we
should force out-of-tree build for the hypervisor.

v7:
Out-of-tree build!

This mean many more patches. Everything after patch 27 is new.

There's a few new patch before that, but otherwise are rework of v6.

Hi,

I have work toward building Xen (the hypervisor) with Linux's build system,
Kbuild.

The main reason for that is to be able to have out-of-tree build. It's annoying
when a build fail because of the pvshim. Other benefit is a much faster
rebuild, and `make clean` doesn't take ages, and better dependencies to figure
out what needs to be rebuild.

Cheers,

Anthony PERARD (30):
  build: set XEN_BUILD_EFI earlier
  build: avoid re-executing the main Makefile by introducing build.mk
  build: fix exported variable name CFLAGS_stack_boundary
  build: set ALL_OBJS in main Makefile; move prelink.o to main Makefile
  build: prepare to always invoke $(MAKE) from xen/, use $(obj)
  build: rework test/livepatch/Makefile
  build: rework cloc recipe
  build: fix enforce unique symbols for recent clang version
  build: build everything from the root dir, use obj=$subdir
  build: introduce if_changed_deps
  build: rename __LINKER__ to LINKER_SCRIPT
  build: hook kconfig into xen build system
  xen/tools/kconfig: fix build with -Wdeclaration-after-statement
  build: remove KBUILD_ specific from Makefile.host
  build: handle always-y and hostprogs-always-y
  build: start building the tools with the main makefiles
  build: add headers path to CFLAGS once for all archs
  build: generate x86's asm-macros.h with filechk
  build: clean-up "clean" rules of duplication
  build: rework "clean" to clean from the root dir
  build: use main rune to build host binary x86's mkelf32 and mkreloc
  build: rework coverage and ubsan CFLAGS handling
  build,x86: remove the need for build32.mk
  build: grab common EFI source files in arch specific dir
  build: replace $(BASEDIR) by $(objtree)
  build: replace $(BASEDIR) and use $(srctree)
  build: rework "headers*.chk" prerequisite in include/
  build: specify source tree in include/ for prerequisite
  build: shuffle main Makefile
  build: adding out-of-tree support to the xen build

 .gitignore   |   2 +
 xen/Kconfig  |   4 +-
 xen/Makefile | 367 ++---
 xen/Rules.mk | 227 +++
 xen/arch/arm/Makefile|  53 +--
 xen/arch/arm/arch.mk |   5 +-
 xen/arch/arm/efi/Makefile|   4 +-
 xen/arch/arm/include/asm/config.h|   2 +-
 xen/arch/riscv/arch.mk   |   2 -
 xen/arch/x86/Makefile| 204 --
 xen/arch/x86/Rules.mk|   8 +-
 xen/arch/x86/arch.mk |  59 ++-
 xen/arch/x86/boot/Makefile   |  57 ++-
 xen/arch/x86/boot/build32.mk |  40 --
 xen/arch/x86/efi/Makefile|  11 +-
 xen/build.mk |  90 +
 xen/common/Makefile  |  13 +-
 xen/common/efi/efi-common.mk |  16 +
 xen/common/libelf/Makefile   |   4 +-
 xen/common/libfdt/Makefile   |   8 +-
 xen/include/Makefile |  76 ++--
 xen/scripts/Kbuild.include   |  69 +++-
 xen/scripts/Kconfig.include 

[XEN PATCH v9 03/30] build: fix exported variable name CFLAGS_stack_boundary

2022-01-25 Thread Anthony PERARD
Exporting a variable with a dash doesn't work reliably, they may be
striped from the environment when calling a sub-make or sub-shell.

CFLAGS-stack-boundary start to be removed from env in patch "build:
set ALL_OBJS in main Makefile; move prelink.o to main Makefile" when
running `make "ALL_OBJS=.."` due to the addition of the quote. At
least in my empirical tests.

Fixes: 2740d96efd ("xen/build: have the root Makefile generates the CFLAGS")
Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- new patch

 xen/arch/x86/Rules.mk | 4 ++--
 xen/arch/x86/arch.mk  | 4 ++--
 xen/arch/x86/efi/Makefile | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/xen/arch/x86/Rules.mk b/xen/arch/x86/Rules.mk
index 56fe22c979ea..7aef93f5f3a0 100644
--- a/xen/arch/x86/Rules.mk
+++ b/xen/arch/x86/Rules.mk
@@ -6,5 +6,5 @@ object_label_flags = '-D__OBJECT_LABEL__=$(subst 
$(BASEDIR)/,,$(CURDIR))/$@'
 else
 object_label_flags = '-D__OBJECT_LABEL__=$(subst /,$$,$(subst -,_,$(subst 
$(BASEDIR)/,,$(CURDIR))/$@))'
 endif
-c_flags += $(object_label_flags) $(CFLAGS-stack-boundary)
-a_flags += $(object_label_flags) $(CFLAGS-stack-boundary)
+c_flags += $(object_label_flags) $(CFLAGS_stack_boundary)
+a_flags += $(object_label_flags) $(CFLAGS_stack_boundary)
diff --git a/xen/arch/x86/arch.mk b/xen/arch/x86/arch.mk
index a93fa6d2e4c9..fa7cf3844362 100644
--- a/xen/arch/x86/arch.mk
+++ b/xen/arch/x86/arch.mk
@@ -49,8 +49,8 @@ CFLAGS-$(CONFIG_INDIRECT_THUNK) += -fno-jump-tables
 
 # If supported by the compiler, reduce stack alignment to 8 bytes. But allow
 # this to be overridden elsewhere.
-$(call cc-option-add,CFLAGS-stack-boundary,CC,-mpreferred-stack-boundary=3)
-export CFLAGS-stack-boundary
+$(call cc-option-add,CFLAGS_stack_boundary,CC,-mpreferred-stack-boundary=3)
+export CFLAGS_stack_boundary
 
 ifeq ($(CONFIG_UBSAN),y)
 # Don't enable alignment sanitisation.  x86 has efficient unaligned accesses,
diff --git a/xen/arch/x86/efi/Makefile b/xen/arch/x86/efi/Makefile
index 87b927ed865b..abae493bf344 100644
--- a/xen/arch/x86/efi/Makefile
+++ b/xen/arch/x86/efi/Makefile
@@ -12,7 +12,7 @@ EFIOBJ-y := boot.init.o pe.init.o ebmalloc.o runtime.o
 EFIOBJ-$(CONFIG_COMPAT) += compat.o
 
 $(call cc-option-add,cflags-stack-boundary,CC,-mpreferred-stack-boundary=4)
-$(EFIOBJ-y): CFLAGS-stack-boundary := $(cflags-stack-boundary)
+$(EFIOBJ-y): CFLAGS_stack_boundary := $(cflags-stack-boundary)
 
 obj-y := stub.o
 obj-$(XEN_BUILD_EFI) := $(filter-out %.init.o,$(EFIOBJ-y))
-- 
Anthony PERARD




[XEN PATCH v9 01/30] build: set XEN_BUILD_EFI earlier

2022-01-25 Thread Anthony PERARD
We are going to need the variable XEN_BUILD_EFI earlier.

But a side effect of calculating the value of $(XEN_BUILD_EFI) is to
also to generate "efi/check.o" which is used for further checks.
Thus the whole chain that check for EFI support is moved to
"arch.mk".

Some other changes are made to avoid too much duplication:
- $(efi-check): Used to avoid repeating "efi/check.*". We don't
  set it to the path to the source as it would be wrong as soon
  as we support out-of-tree build.
- $(LD_PE_check_cmd): As it is called twice, with an updated
  $(EFI_LDFLAGS).

$(nr-fixups) is renamed to $(efi-nr-fixups) as the former might be
a bit too generic.

In order to avoid exporting MKRELOC, the variable is added to $(MAKE)
command line. The only modification needed is in target "build", the
modification target "$(TARGET)" will be needed with a following patch
"build: avoid re-executing the main Makefile by introducing build.mk".

We can now revert 24b0ce9a5da2, we don't need to override efi-y on
recursion anymore.

Signed-off-by: Anthony PERARD 
---

Notes:
v9:
- change $(efi-check) to hold the $(basename ) of check.c, and rename
  the variable from $(efi-check-0).
- unexport MKRELOC, pass it down via command line instead.
- remove efi-y override which reverts 24b0ce9a5da2

v8:
- rename to efi-nr-fixups rather than efi-check-relocs
- use := when assigning variable in makefile when recursive expansion
  isn't needed.
- no more check of $(efi-y) value for "CFLAGS-$(XEN_BUILD_EFI) +=
  -DXEN_BUILD_EFI".

v7:
- Do the whole check for EFI support in arch.mk. So efi/check.o is
  produce there and used there, and produce efi/check.efi and use it there.
  Thus avoid the need to repeat the test done for XEN_BUILD_EFI.

 xen/Makefile  |  4 ++--
 xen/arch/x86/Makefile | 46 ++-
 xen/arch/x86/arch.mk  | 42 +++
 3 files changed, 50 insertions(+), 42 deletions(-)

diff --git a/xen/Makefile b/xen/Makefile
index 1e10d9f68080..f3ff03a7170e 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -296,7 +296,7 @@ endif # need-config
 .PHONY: build install uninstall clean distclean MAP
 build install uninstall debug clean distclean MAP::
 ifneq ($(XEN_TARGET_ARCH),x86_32)
-   $(MAKE) -f Rules.mk _$@
+   $(MAKE) -f Rules.mk MKRELOC=$(MKRELOC) _$@
 else
echo "*** Xen x86/32 target no longer supported!"
 endif
@@ -404,7 +404,7 @@ $(TARGET): FORCE
$(MAKE) -f $(BASEDIR)/Rules.mk -C include
$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) include
$(MAKE) -f $(BASEDIR)/Rules.mk 
arch/$(TARGET_ARCH)/include/asm/asm-offsets.h
-   $(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) $@
+   $(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) 
MKRELOC=$(MKRELOC) $@
 
 quiet_cmd_banner = BANNER  $@
 define cmd_banner
diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 8db4cb98edbb..9fc884813cb5 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -121,44 +121,8 @@ $(TARGET): $(TARGET)-syms $(efi-y) boot/mkelf32
{ echo "No Multiboot2 header found" >&2; false; }
mv $(TMP) $(TARGET)
 
-ifneq ($(efi-y),)
-
-# Check if the compiler supports the MS ABI.
-export XEN_BUILD_EFI := $(shell $(CC) $(XEN_CFLAGS) -c efi/check.c -o 
efi/check.o 2>/dev/null && echo y)
 CFLAGS-$(XEN_BUILD_EFI) += -DXEN_BUILD_EFI
 
-# Check if the linker supports PE.
-EFI_LDFLAGS = $(patsubst -m%,-mi386pep,$(XEN_LDFLAGS)) --subsystem=10
-XEN_BUILD_PE := $(if $(XEN_BUILD_EFI),$(call ld-option,$(EFI_LDFLAGS) 
--image-base=0x1 -o efi/check.efi efi/check.o))
-# If the above failed, it may be merely because of the linker not dealing well
-# with debug info. Try again with stripping it.
-ifeq ($(CONFIG_DEBUG_INFO)-$(XEN_BUILD_PE),y-n)
-EFI_LDFLAGS += --strip-debug
-XEN_BUILD_PE := $(call ld-option,$(EFI_LDFLAGS) --image-base=0x1 -o 
efi/check.efi efi/check.o)
-endif
-
-ifeq ($(XEN_BUILD_PE),y)
-
-# Check if the linker produces fixups in PE by default
-nr-fixups := $(shell $(OBJDUMP) -p efi/check.efi | grep 
'^[[:blank:]]*reloc[[:blank:]]*[0-9][[:blank:]].*DIR64$$' | wc -l)
-ifeq ($(nr-fixups),2)
-MKRELOC := :
-relocs-dummy :=
-else
-MKRELOC := efi/mkreloc
-relocs-dummy := efi/relocs-dummy.o
-# If the linker produced fixups but not precisely two of them, we need to
-# disable it doing so.  But if it didn't produce any fixups, it also wouldn't
-# recognize the option.
-ifneq ($(nr-fixups),0)
-EFI_LDFLAGS += --disable-reloc-section
-endif
-endif
-
-endif # $(XEN_BUILD_PE)
-
-endif # $(efi-y)
-
 ALL_OBJS := $(BASEDIR)/arch/x86/boot/built_in.o 
$(BASEDIR)/arch/x86/efi/built_in.o $(ALL_OBJS)
 
 ifeq ($(CONFIG_LTO),y)
@@ -182,13 +146,13 @@ $(TARGET)-syms: prelink.o xen.lds
$(NM) -pa --format=sysv $(@D)/.$(@F).0 \
| $(BASEDIR)/tools/symbols $(all_symbols) --sysv --sort \
>$(@D)

  1   2   >