Skip to content

Commit 3ffe326

Browse files
philmdvivier
authored andcommitted
linux-user: Simplify host <-> target errno conversion using macros
Convert the host_to_target_errno_table[] array to a switch case to allow compiler optimizations (such noticing the identity function when host and guest errnos match). Extract the errnos list as to a new includible unit, using a generic macro. Remove the code related to target_to_host_errno_table[] initialization. Suggested-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Reviewed-by: Laurent Vivier <laurent@vivier.eu> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Message-Id: <20210708170550.1846343-8-f4bug@amsat.org> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
1 parent f317c0e commit 3ffe326

File tree

2 files changed

+154
-145
lines changed

2 files changed

+154
-145
lines changed

linux-user/errnos.c.inc

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* This list is the union of errno values overridden in asm-<arch>/errno.h
3+
* minus the errnos that are not actually generic to all archs.
4+
*
5+
* Please keep this list sorted alphabetically.
6+
*
7+
* Copyright (c) 2003 Fabrice Bellard
8+
*
9+
* This program is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
21+
*
22+
* SPDX-License-Identifier: GPL-2.0-or-later
23+
*/
24+
25+
E(EADDRINUSE)
26+
E(EADDRNOTAVAIL)
27+
E(EADV)
28+
E(EAFNOSUPPORT)
29+
E(EAGAIN)
30+
E(EALREADY)
31+
E(EBADE)
32+
E(EBADFD)
33+
E(EBADMSG)
34+
E(EBADR)
35+
E(EBADRQC)
36+
E(EBADSLT)
37+
E(EBFONT)
38+
E(ECANCELED)
39+
E(ECHRNG)
40+
E(ECOMM)
41+
E(ECONNABORTED)
42+
E(ECONNREFUSED)
43+
E(ECONNRESET)
44+
E(EDEADLK)
45+
E(EDESTADDRREQ)
46+
E(EDOTDOT)
47+
E(EDQUOT)
48+
E(EHOSTDOWN)
49+
E(EHOSTUNREACH)
50+
#ifdef EHWPOISON
51+
E(EHWPOISON)
52+
#endif
53+
E(EIDRM)
54+
E(EILSEQ)
55+
E(EINPROGRESS)
56+
E(EISCONN)
57+
E(EISNAM)
58+
#ifdef EKEYEXPIRED
59+
E(EKEYEXPIRED)
60+
#endif
61+
#ifdef EKEYREJECTED
62+
E(EKEYREJECTED)
63+
#endif
64+
#ifdef EKEYREVOKED
65+
E(EKEYREVOKED)
66+
#endif
67+
E(EL2HLT)
68+
E(EL2NSYNC)
69+
E(EL3HLT)
70+
E(EL3RST)
71+
E(ELIBACC)
72+
E(ELIBBAD)
73+
E(ELIBEXEC)
74+
E(ELIBMAX)
75+
E(ELIBSCN)
76+
E(ELNRNG)
77+
E(ELOOP)
78+
E(EMEDIUMTYPE)
79+
E(EMSGSIZE)
80+
E(EMULTIHOP)
81+
E(ENAMETOOLONG)
82+
E(ENAVAIL)
83+
E(ENETDOWN)
84+
E(ENETRESET)
85+
E(ENETUNREACH)
86+
E(ENOANO)
87+
E(ENOBUFS)
88+
E(ENOCSI)
89+
E(ENODATA)
90+
#ifdef ENOKEY
91+
E(ENOKEY)
92+
#endif
93+
E(ENOLCK)
94+
E(ENOLINK)
95+
E(ENOMEDIUM)
96+
#ifdef ENOMSG
97+
E(ENOMSG)
98+
#endif
99+
E(ENONET)
100+
E(ENOPKG)
101+
E(ENOPROTOOPT)
102+
E(ENOSR)
103+
E(ENOSTR)
104+
E(ENOSYS)
105+
E(ENOTCONN)
106+
E(ENOTEMPTY)
107+
E(ENOTNAM)
108+
#ifdef ENOTRECOVERABLE
109+
E(ENOTRECOVERABLE)
110+
#endif
111+
E(ENOTSOCK)
112+
E(ENOTUNIQ)
113+
E(EOPNOTSUPP)
114+
E(EOVERFLOW)
115+
#ifdef EOWNERDEAD
116+
E(EOWNERDEAD)
117+
#endif
118+
E(EPFNOSUPPORT)
119+
E(EPROTO)
120+
E(EPROTONOSUPPORT)
121+
E(EPROTOTYPE)
122+
E(EREMCHG)
123+
E(EREMOTE)
124+
E(EREMOTEIO)
125+
E(ERESTART)
126+
#ifdef ERFKILL
127+
E(ERFKILL)
128+
#endif
129+
E(ESHUTDOWN)
130+
E(ESOCKTNOSUPPORT)
131+
E(ESRMNT)
132+
E(ESTALE)
133+
E(ESTRPIPE)
134+
E(ETIME)
135+
E(ETIMEDOUT)
136+
E(ETOOMANYREFS)
137+
E(EUCLEAN)
138+
E(EUNATCH)
139+
E(EUSERS)
140+
E(EXFULL)

linux-user/syscall.c

Lines changed: 14 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -509,150 +509,26 @@ static inline int next_free_host_timer(void)
509509

510510
#define ERRNO_TABLE_SIZE 1200
511511

512-
/* target_to_host_errno_table[] is initialized from
513-
* host_to_target_errno_table[] in syscall_init(). */
514-
static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
515-
};
516-
517-
/*
518-
* This list is the union of errno values overridden in asm-<arch>/errno.h
519-
* minus the errnos that are not actually generic to all archs.
520-
*/
521-
static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
522-
[EAGAIN] = TARGET_EAGAIN,
523-
[EIDRM] = TARGET_EIDRM,
524-
[ECHRNG] = TARGET_ECHRNG,
525-
[EL2NSYNC] = TARGET_EL2NSYNC,
526-
[EL3HLT] = TARGET_EL3HLT,
527-
[EL3RST] = TARGET_EL3RST,
528-
[ELNRNG] = TARGET_ELNRNG,
529-
[EUNATCH] = TARGET_EUNATCH,
530-
[ENOCSI] = TARGET_ENOCSI,
531-
[EL2HLT] = TARGET_EL2HLT,
532-
[EDEADLK] = TARGET_EDEADLK,
533-
[ENOLCK] = TARGET_ENOLCK,
534-
[EBADE] = TARGET_EBADE,
535-
[EBADR] = TARGET_EBADR,
536-
[EXFULL] = TARGET_EXFULL,
537-
[ENOANO] = TARGET_ENOANO,
538-
[EBADRQC] = TARGET_EBADRQC,
539-
[EBADSLT] = TARGET_EBADSLT,
540-
[EBFONT] = TARGET_EBFONT,
541-
[ENOSTR] = TARGET_ENOSTR,
542-
[ENODATA] = TARGET_ENODATA,
543-
[ETIME] = TARGET_ETIME,
544-
[ENOSR] = TARGET_ENOSR,
545-
[ENONET] = TARGET_ENONET,
546-
[ENOPKG] = TARGET_ENOPKG,
547-
[EREMOTE] = TARGET_EREMOTE,
548-
[ENOLINK] = TARGET_ENOLINK,
549-
[EADV] = TARGET_EADV,
550-
[ESRMNT] = TARGET_ESRMNT,
551-
[ECOMM] = TARGET_ECOMM,
552-
[EPROTO] = TARGET_EPROTO,
553-
[EDOTDOT] = TARGET_EDOTDOT,
554-
[EMULTIHOP] = TARGET_EMULTIHOP,
555-
[EBADMSG] = TARGET_EBADMSG,
556-
[ENAMETOOLONG] = TARGET_ENAMETOOLONG,
557-
[EOVERFLOW] = TARGET_EOVERFLOW,
558-
[ENOTUNIQ] = TARGET_ENOTUNIQ,
559-
[EBADFD] = TARGET_EBADFD,
560-
[EREMCHG] = TARGET_EREMCHG,
561-
[ELIBACC] = TARGET_ELIBACC,
562-
[ELIBBAD] = TARGET_ELIBBAD,
563-
[ELIBSCN] = TARGET_ELIBSCN,
564-
[ELIBMAX] = TARGET_ELIBMAX,
565-
[ELIBEXEC] = TARGET_ELIBEXEC,
566-
[EILSEQ] = TARGET_EILSEQ,
567-
[ENOSYS] = TARGET_ENOSYS,
568-
[ELOOP] = TARGET_ELOOP,
569-
[ERESTART] = TARGET_ERESTART,
570-
[ESTRPIPE] = TARGET_ESTRPIPE,
571-
[ENOTEMPTY] = TARGET_ENOTEMPTY,
572-
[EUSERS] = TARGET_EUSERS,
573-
[ENOTSOCK] = TARGET_ENOTSOCK,
574-
[EDESTADDRREQ] = TARGET_EDESTADDRREQ,
575-
[EMSGSIZE] = TARGET_EMSGSIZE,
576-
[EPROTOTYPE] = TARGET_EPROTOTYPE,
577-
[ENOPROTOOPT] = TARGET_ENOPROTOOPT,
578-
[EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
579-
[ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
580-
[EOPNOTSUPP] = TARGET_EOPNOTSUPP,
581-
[EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
582-
[EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
583-
[EADDRINUSE] = TARGET_EADDRINUSE,
584-
[EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
585-
[ENETDOWN] = TARGET_ENETDOWN,
586-
[ENETUNREACH] = TARGET_ENETUNREACH,
587-
[ENETRESET] = TARGET_ENETRESET,
588-
[ECONNABORTED] = TARGET_ECONNABORTED,
589-
[ECONNRESET] = TARGET_ECONNRESET,
590-
[ENOBUFS] = TARGET_ENOBUFS,
591-
[EISCONN] = TARGET_EISCONN,
592-
[ENOTCONN] = TARGET_ENOTCONN,
593-
[EUCLEAN] = TARGET_EUCLEAN,
594-
[ENOTNAM] = TARGET_ENOTNAM,
595-
[ENAVAIL] = TARGET_ENAVAIL,
596-
[EISNAM] = TARGET_EISNAM,
597-
[EREMOTEIO] = TARGET_EREMOTEIO,
598-
[EDQUOT] = TARGET_EDQUOT,
599-
[ESHUTDOWN] = TARGET_ESHUTDOWN,
600-
[ETOOMANYREFS] = TARGET_ETOOMANYREFS,
601-
[ETIMEDOUT] = TARGET_ETIMEDOUT,
602-
[ECONNREFUSED] = TARGET_ECONNREFUSED,
603-
[EHOSTDOWN] = TARGET_EHOSTDOWN,
604-
[EHOSTUNREACH] = TARGET_EHOSTUNREACH,
605-
[EALREADY] = TARGET_EALREADY,
606-
[EINPROGRESS] = TARGET_EINPROGRESS,
607-
[ESTALE] = TARGET_ESTALE,
608-
[ECANCELED] = TARGET_ECANCELED,
609-
[ENOMEDIUM] = TARGET_ENOMEDIUM,
610-
[EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
611-
#ifdef ENOKEY
612-
[ENOKEY] = TARGET_ENOKEY,
613-
#endif
614-
#ifdef EKEYEXPIRED
615-
[EKEYEXPIRED] = TARGET_EKEYEXPIRED,
616-
#endif
617-
#ifdef EKEYREVOKED
618-
[EKEYREVOKED] = TARGET_EKEYREVOKED,
619-
#endif
620-
#ifdef EKEYREJECTED
621-
[EKEYREJECTED] = TARGET_EKEYREJECTED,
622-
#endif
623-
#ifdef EOWNERDEAD
624-
[EOWNERDEAD] = TARGET_EOWNERDEAD,
625-
#endif
626-
#ifdef ENOTRECOVERABLE
627-
[ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
628-
#endif
629-
#ifdef ENOMSG
630-
[ENOMSG] = TARGET_ENOMSG,
631-
#endif
632-
#ifdef ERFKILL
633-
[ERFKILL] = TARGET_ERFKILL,
634-
#endif
635-
#ifdef EHWPOISON
636-
[EHWPOISON] = TARGET_EHWPOISON,
637-
#endif
638-
};
639-
640-
static inline int host_to_target_errno(int err)
512+
static inline int host_to_target_errno(int host_errno)
641513
{
642-
if (err >= 0 && err < ERRNO_TABLE_SIZE &&
643-
host_to_target_errno_table[err]) {
644-
return host_to_target_errno_table[err];
514+
switch (host_errno) {
515+
#define E(X) case X: return TARGET_##X;
516+
#include "errnos.c.inc"
517+
#undef E
518+
default:
519+
return host_errno;
645520
}
646-
return err;
647521
}
648522

649-
static inline int target_to_host_errno(int err)
523+
static inline int target_to_host_errno(int target_errno)
650524
{
651-
if (err >= 0 && err < ERRNO_TABLE_SIZE &&
652-
target_to_host_errno_table[err]) {
653-
return target_to_host_errno_table[err];
525+
switch (target_errno) {
526+
#define E(X) case TARGET_##X: return X;
527+
#include "errnos.c.inc"
528+
#undef E
529+
default:
530+
return target_errno;
654531
}
655-
return err;
656532
}
657533

658534
static inline abi_long get_errno(abi_long ret)
@@ -7102,7 +6978,6 @@ void syscall_init(void)
71026978
IOCTLEntry *ie;
71036979
const argtype *arg_type;
71046980
int size;
7105-
int i;
71066981

71076982
thunk_init(STRUCT_MAX);
71086983

@@ -7112,12 +6987,6 @@ void syscall_init(void)
71126987
#undef STRUCT
71136988
#undef STRUCT_SPECIAL
71146989

7115-
/* Build target_to_host_errno_table[] table from
7116-
* host_to_target_errno_table[]. */
7117-
for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
7118-
target_to_host_errno_table[host_to_target_errno_table[i]] = i;
7119-
}
7120-
71216990
/* we patch the ioctl size if necessary. We rely on the fact that
71226991
no ioctl has all the bits at '1' in the size field */
71236992
ie = ioctl_entries;

0 commit comments

Comments
 (0)