Re: [PATCH 05/10] efi/libstub: distinguish between native/mixed not 32/64 bit

From: Arvind Sankar
Date: Sat Dec 14 2019 - 18:02:12 EST


On Sat, Dec 14, 2019 at 10:14:38PM +0000, Ard Biesheuvel wrote:
> On Sat, 14 Dec 2019 at 22:30, Ard Biesheuvel <ard.biesheuvel@xxxxxxxxxx> wrote:
> >
> > On Sat, 14 Dec 2019 at 22:17, Arvind Sankar <nivedita@xxxxxxxxxxxx> wrote:
> > >
> > > On Sat, Dec 14, 2019 at 08:27:50PM +0000, Ard Biesheuvel wrote:
> > > > On Sat, 14 Dec 2019 at 21:13, Arvind Sankar <nivedita@xxxxxxxxxxxx> wrote:
> > > > >
> > > > > On Sat, Dec 14, 2019 at 07:54:25PM +0000, Ard Biesheuvel wrote:
> > > > > > On Sat, 14 Dec 2019 at 20:49, Arvind Sankar <nivedita@xxxxxxxxxxxx> wrote:
> > > > > > >
> > > > > > > On Sat, Dec 14, 2019 at 02:46:27PM -0500, Arvind Sankar wrote:
> > > > > > > > On Sat, Dec 14, 2019 at 06:57:30PM +0100, Ard Biesheuvel wrote:
> > > > > > > > > +
> > > > > > > > > +#define efi_table_attr(table, attr, instance) ({ \
> > > > > > > > > + __typeof__(((table##_t *)0)->attr) __ret; \
> > > > > > > > > + if (efi_is_native()) { \
> > > > > > > > > + __ret = ((table##_t *)instance)->attr; \
> > > > > > > > > + } else { \
> > > > > > > > > + __typeof__(((table##_32_t *)0)->attr) at; \
> > > > > > > > > + at = (((table##_32_t *)(unsigned long)instance)->attr); \
> > > > > > > > > + __ret = (__typeof__(__ret))(unsigned long)at; \
> > > > > > > > > + } \
> > > > > > > > > + __ret; \
> > > > > > > > > +})
> > > > > > > >
> > > > Yes. I'm open to suggestions on how to improve this, but mixed mode is
> > > > somewhat of a maintenance burden, so if new future functionality needs
> > > > to leave mixed mode behind, I'm not too bothered.
> > > >
> > >
> > > Maybe just do
> > > if (sizeof(at) < sizeof(__ret))
> > > __ret = (__typeof__(__ret))(uintptr_t)at;
> > > else
> > > __ret = (__typeof__(__ret))at;
> > > That should cover most of the cases.
> >
> > But the compiler will still be unhappy about the else clause if __ret
> > is a pointer type, since we'll be casting an u32 to a pointer,
>
> I think the answer is to have efi_table_ptr() for pointers and
> efi_table_attr() for other types.

Using __builtin_choose_expr avoids the warning:
__ret = (__typeof__(__ret))
__builtin_choose_expr(sizeof(at) < sizeof(ret),
(uintptr_t)at, at);

But having different efi_table_ macros sounds cleaner.