On Tue, Aug 8, 2023 at 7:47 PM Richard Henderson <richard.henderson@linaro.org> wrote:
On 8/8/23 18:39, Warner Losh wrote:
>      > +#define __put_user_e(x, hptr, e)                                            \
>      > +    do {                                                                    \
>      > +        PRAGMA_DISABLE_PACKED_WARNING;                                      \
>      > +        (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
>      > +        __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
>      > +        __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
>      > +        __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
>      > +            ((hptr), (x)), (void)0);                                        \
>      > +        PRAGMA_REENABLE_PACKED_WARNING;                                     \
>      > +    } while (0)
>      > +
>      > +#define __get_user_e(x, hptr, e)                                            \
>      > +    do {                                                                    \
>      > +        PRAGMA_DISABLE_PACKED_WARNING;                                      \
>      > +        ((x) = (typeof(*hptr))(                                             \
>      > +        __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
>      > +        __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
>      > +        __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
>      > +        __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
>      > +            (hptr)), (void)0);                                              \
>      > +        PRAGMA_REENABLE_PACKED_WARNING;                                     \
>      > +    } while (0)
>
>     Hmm.  I guess this works.  The typeof cast in __get_user_e being required when
>     sizeof(x) >
>     sizeof(*hptr) in order to get the correct extension.
>
>
> This code was copied 100% from the current linux-user :)

Ha ha indeed!  I should have known.

Yea... It's old-school crazy, and I've done a lot of that in my day, but not
here :)
 
>     Is it clearer with _Generic?
>
>           (x) = _Generic(*(hptr),
>                          int8_t: *(int8_t *)(hptr),
>                          uint8_t: *(uint8_t *)(hptr),
>                          int16_t: (int16_t)lduw_##e##_p(hptr),
>                          uint16_t: lduw_##e##_p(hptr),
>                          int32_t: (int32_t)ldl_##e##_p(hptr),
>                          uint32_t: (uint32_t)ldl_##e##_p(hptr),
>                          int64_t: (int64_t)ldq_##e##_p(hptr),
>                          uint64_t: ldq_##e##_p(hptr));
>
>     In particular I believe the error message will be much prettier.
>
>
> Indeed. That looks cleaner. I'll see if I can test that against the latest bsd-user upstream.

I'll see if we can share this code via common-user.

It seems to work, though I still need the pragmas for clang 16 (see another patch for that).

I tried to implement both get and put in terms of this, but found that it broke the blitz
branch. So why don't we land this as is for bsd-user and then one of us can try to
put it into common-user so as to not create too many waves for our GSoC student Kariim.
There's already enough to fix in this series... Sound fair?

Warner