]> git.proxmox.com Git - systemd.git/blob - src/shared/architecture.c
Enable seccomp support on powerpc, ppc64el, and s390x
[systemd.git] / src / shared / architecture.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/utsname.h>
23
24 #include "architecture.h"
25 #include "string-table.h"
26 #include "string-util.h"
27
28 int uname_architecture(void) {
29
30 /* Return a sanitized enum identifying the architecture we are
31 * running on. This is based on uname(), and the user may
32 * hence control what this returns by using
33 * personality(). This puts the user in control on systems
34 * that can run binaries of multiple architectures.
35 *
36 * We do not translate the string returned by uname()
37 * 1:1. Instead we try to clean it up and break down the
38 * confusion on x86 and arm in particular.
39 *
40 * We do not try to distinguish CPUs not CPU features, but
41 * actual architectures, i.e. that have genuinely different
42 * code. */
43
44 static const struct {
45 const char *machine;
46 int arch;
47 } arch_map[] = {
48 #if defined(__x86_64__) || defined(__i386__)
49 { "x86_64", ARCHITECTURE_X86_64 },
50 { "i686", ARCHITECTURE_X86 },
51 { "i586", ARCHITECTURE_X86 },
52 { "i486", ARCHITECTURE_X86 },
53 { "i386", ARCHITECTURE_X86 },
54 #elif defined(__powerpc__) || defined(__powerpc64__)
55 { "ppc64", ARCHITECTURE_PPC64 },
56 { "ppc64le", ARCHITECTURE_PPC64_LE },
57 { "ppc", ARCHITECTURE_PPC },
58 { "ppcle", ARCHITECTURE_PPC_LE },
59 #elif defined(__ia64__)
60 { "ia64", ARCHITECTURE_IA64 },
61 #elif defined(__hppa__) || defined(__hppa64__)
62 { "parisc64", ARCHITECTURE_PARISC64 },
63 { "parisc", ARCHITECTURE_PARISC },
64 #elif defined(__s390__) || defined(__s390x__)
65 { "s390x", ARCHITECTURE_S390X },
66 { "s390", ARCHITECTURE_S390 },
67 #elif defined(__sparc__) || defined(__sparc64__)
68 { "sparc64", ARCHITECTURE_SPARC64 },
69 { "sparc", ARCHITECTURE_SPARC },
70 #elif defined(__mips__) || defined(__mips64__)
71 { "mips64", ARCHITECTURE_MIPS64 },
72 { "mips", ARCHITECTURE_MIPS },
73 #elif defined(__alpha__)
74 { "alpha" , ARCHITECTURE_ALPHA },
75 #elif defined(__arm__) || defined(__aarch64__)
76 { "aarch64", ARCHITECTURE_ARM64 },
77 { "aarch64_be", ARCHITECTURE_ARM64_BE },
78 { "armv4l", ARCHITECTURE_ARM },
79 { "armv4b", ARCHITECTURE_ARM_BE },
80 { "armv4tl", ARCHITECTURE_ARM },
81 { "armv4tb", ARCHITECTURE_ARM_BE },
82 { "armv5tl", ARCHITECTURE_ARM },
83 { "armv5tb", ARCHITECTURE_ARM_BE },
84 { "armv5tel", ARCHITECTURE_ARM },
85 { "armv5teb" , ARCHITECTURE_ARM_BE },
86 { "armv5tejl", ARCHITECTURE_ARM },
87 { "armv5tejb", ARCHITECTURE_ARM_BE },
88 { "armv6l", ARCHITECTURE_ARM },
89 { "armv6b", ARCHITECTURE_ARM_BE },
90 { "armv7l", ARCHITECTURE_ARM },
91 { "armv7b", ARCHITECTURE_ARM_BE },
92 { "armv7ml", ARCHITECTURE_ARM },
93 { "armv7mb", ARCHITECTURE_ARM_BE },
94 { "armv4l", ARCHITECTURE_ARM },
95 { "armv4b", ARCHITECTURE_ARM_BE },
96 { "armv4tl", ARCHITECTURE_ARM },
97 { "armv4tb", ARCHITECTURE_ARM_BE },
98 { "armv5tl", ARCHITECTURE_ARM },
99 { "armv5tb", ARCHITECTURE_ARM_BE },
100 { "armv5tel", ARCHITECTURE_ARM },
101 { "armv5teb", ARCHITECTURE_ARM_BE },
102 { "armv5tejl", ARCHITECTURE_ARM },
103 { "armv5tejb", ARCHITECTURE_ARM_BE },
104 { "armv6l", ARCHITECTURE_ARM },
105 { "armv6b", ARCHITECTURE_ARM_BE },
106 { "armv7l", ARCHITECTURE_ARM },
107 { "armv7b", ARCHITECTURE_ARM_BE },
108 { "armv7ml", ARCHITECTURE_ARM },
109 { "armv7mb", ARCHITECTURE_ARM_BE },
110 { "armv8l", ARCHITECTURE_ARM },
111 { "armv8b", ARCHITECTURE_ARM_BE },
112 #elif defined(__sh__) || defined(__sh64__)
113 { "sh5", ARCHITECTURE_SH64 },
114 { "sh2", ARCHITECTURE_SH },
115 { "sh2a", ARCHITECTURE_SH },
116 { "sh3", ARCHITECTURE_SH },
117 { "sh4", ARCHITECTURE_SH },
118 { "sh4a", ARCHITECTURE_SH },
119 #elif defined(__m68k__)
120 { "m68k", ARCHITECTURE_M68K },
121 #elif defined(__tilegx__)
122 { "tilegx", ARCHITECTURE_TILEGX },
123 #elif defined(__cris__)
124 { "crisv32", ARCHITECTURE_CRIS },
125 #else
126 #error "Please register your architecture here!"
127 #endif
128 };
129
130 static int cached = _ARCHITECTURE_INVALID;
131 struct utsname u;
132 unsigned i;
133
134 if (cached != _ARCHITECTURE_INVALID)
135 return cached;
136
137 assert_se(uname(&u) >= 0);
138
139 for (i = 0; i < ELEMENTSOF(arch_map); i++)
140 if (streq(arch_map[i].machine, u.machine))
141 return cached = arch_map[i].arch;
142
143 assert_not_reached("Couldn't identify architecture. You need to patch systemd.");
144 return _ARCHITECTURE_INVALID;
145 }
146
147 static const char *const architecture_table[_ARCHITECTURE_MAX] = {
148 [ARCHITECTURE_X86] = "x86",
149 [ARCHITECTURE_X86_64] = "x86-64",
150 [ARCHITECTURE_PPC] = "ppc",
151 [ARCHITECTURE_PPC_LE] = "ppc-le",
152 [ARCHITECTURE_PPC64] = "ppc64",
153 [ARCHITECTURE_PPC64_LE] = "ppc64-le",
154 [ARCHITECTURE_IA64] = "ia64",
155 [ARCHITECTURE_PARISC] = "parisc",
156 [ARCHITECTURE_PARISC64] = "parisc64",
157 [ARCHITECTURE_S390] = "s390",
158 [ARCHITECTURE_S390X] = "s390x",
159 [ARCHITECTURE_SPARC] = "sparc",
160 [ARCHITECTURE_SPARC64] = "sparc64",
161 [ARCHITECTURE_MIPS] = "mips",
162 [ARCHITECTURE_MIPS_LE] = "mips-le",
163 [ARCHITECTURE_MIPS64] = "mips64",
164 [ARCHITECTURE_MIPS64_LE] = "mips64-le",
165 [ARCHITECTURE_ALPHA] = "alpha",
166 [ARCHITECTURE_ARM] = "arm",
167 [ARCHITECTURE_ARM_BE] = "arm-be",
168 [ARCHITECTURE_ARM64] = "arm64",
169 [ARCHITECTURE_ARM64_BE] = "arm64-be",
170 [ARCHITECTURE_SH] = "sh",
171 [ARCHITECTURE_SH64] = "sh64",
172 [ARCHITECTURE_M68K] = "m68k",
173 [ARCHITECTURE_TILEGX] = "tilegx",
174 [ARCHITECTURE_CRIS] = "cris",
175 };
176
177 DEFINE_STRING_TABLE_LOOKUP(architecture, int);