architecture.c revision 592fd144ae313855f48d0ca52a103013b41e5d59
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt/***
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt This file is part of systemd.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
7bd8e95d44977833d0de3fc4e893eb3bc84351d6Patrik Flykt Copyright 2014 Lennart Poettering
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt systemd is free software; you can redistribute it and/or modify it
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt under the terms of the GNU Lesser General Public License as published by
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt the Free Software Foundation; either version 2.1 of the License, or
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt (at your option) any later version.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt systemd is distributed in the hope that it will be useful, but
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt WITHOUT ANY WARRANTY; without even the implied warranty of
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt Lesser General Public License for more details.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt You should have received a copy of the GNU Lesser General Public License
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt along with systemd; If not, see <http://www.gnu.org/licenses/>.
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt***/
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#include <sys/utsname.h>
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt#include "architecture.h"
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flyktint uname_architecture(void) {
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt /* Return a sanitized enum identifying the architecture we are
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt * running on. This is based on uname(), and the user may
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt * hence control what this returns by using
3df3e884ae1237ef0d4d23b0e80f4ffda95ac135Ronny Chevalier * personality(). This puts the user in control on systems
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt * that can run binaries of multiple architectures.
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt *
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt * We do not translate the string returned by uname()
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt * 1:1. Instead we try to clean it up and break down the
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt * confusion on x86 and arm in particular.
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt *
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen * We do not try to distuingish CPUs not CPU features, but
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt * actual architectures, i.e. that have genuinely different
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams * code. */
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt static const struct {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt const char *machine;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt int arch;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt } arch_map[] = {
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#if defined(__x86_64__) || defined(__i386__)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt { "x86_64", ARCHITECTURE_X86_64 },
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt { "i686", ARCHITECTURE_X86 },
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams { "i586", ARCHITECTURE_X86 },
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams { "i486", ARCHITECTURE_X86 },
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams { "i386", ARCHITECTURE_X86 },
f12abb48fc510b8b349c05e35ba048134debaf25Patrik Flykt#elif defined(__powerpc__) || defined(__powerpc64__)
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "ppc64", ARCHITECTURE_PPC64 },
346e13a25dc6f76d3bc9d8decd40dc4782b02d2aPatrik Flykt { "ppc64le", ARCHITECTURE_PPC64_LE },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "ppc", ARCHITECTURE_PPC },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "ppcle", ARCHITECTURE_PPC_LE },
bbfa43ca37df0718287c25a8e39ee7477ebf33f6Patrik Flykt#elif defined(__ia64__)
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt { "ia64", ARCHITECTURE_IA64 },
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt#elif defined(__hppa__) || defined(__hppa64__)
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt { "parisc64", ARCHITECTURE_PARISC64 },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "parisc", ARCHITECTURE_PARISC },
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt#elif defined(__s390__) || defined(__s390x__)
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt { "s390x", ARCHITECTURE_S390X },
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt { "s390", ARCHITECTURE_S390 },
d1b0afe3653b4316a6361d204169620726d468a0Patrik Flykt#elif defined(__sparc__) || defined(__sparc64__)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt { "sparc64", ARCHITECTURE_SPARC64 },
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt { "sparc", ARCHITECTURE_SPARC },
764aad6258eec3bd4ae62ea341ea507bd69ce628Tom Gundersen#elif defined(__mips__) || defined(__mips64__)
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams { "mips64", ARCHITECTURE_MIPS64 },
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt { "mips", ARCHITECTURE_MIPS },
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#elif defined(__alpha__)
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt { "alpha" , ARCHITECTURE_ALPHA },
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt#elif defined(__arm__) || defined(__aarch64__)
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt { "aarch64", ARCHITECTURE_ARM64 },
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt { "aarch64_be", ARCHITECTURE_ARM64_BE },
41e4615d4f4f5c61afa84ba857f23c0ac496687bPatrik Flykt { "armv4l", ARCHITECTURE_ARM },
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt { "armv4b", ARCHITECTURE_ARM_BE },
da6fe470e17fa02f3adedc779585caf8669252bdPatrik Flykt { "armv4tl", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv4tb", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv5tl", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv5tb", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv5tel", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv5teb" , ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv5tejl", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv5tejb", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv6l", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv6b", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv7l", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv7b", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv7ml", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv7mb", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv4l", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv4b", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv4tl", ARCHITECTURE_ARM },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv4tb", ARCHITECTURE_ARM_BE },
a9aff3615b430f86bd0a824214d95f634efaf894Patrik Flykt { "armv5tl", ARCHITECTURE_ARM },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv5tb", ARCHITECTURE_ARM_BE },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv5tel", ARCHITECTURE_ARM },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv5teb", ARCHITECTURE_ARM_BE },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv5tejl", ARCHITECTURE_ARM },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv5tejb", ARCHITECTURE_ARM_BE },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv6l", ARCHITECTURE_ARM },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv6b", ARCHITECTURE_ARM_BE },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv7l", ARCHITECTURE_ARM },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv7b", ARCHITECTURE_ARM_BE },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv7ml", ARCHITECTURE_ARM },
631bbe71298ec892f77f44f94feb612646fe6853Patrik Flykt { "armv7mb", ARCHITECTURE_ARM_BE },
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt { "armv8l", ARCHITECTURE_ARM },
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt { "armv8b", ARCHITECTURE_ARM_BE },
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt#elif defined(__sh__) || defined(__sh64__)
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt { "sh64", ARCHITECTURE_SH64 },
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt { "sh", ARCHITECTURE_SH },
3f0c075f8ef3344da5a6bda524540201f9204e61Patrik Flykt#elif defined(__m68k__)
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt { "m68k", ARCHITECTURE_M68K },
c3e2adeaba8e043caed0ef139eeaea016bd152d0Patrik Flykt#elif defined(__tilegx__)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt { "tilegx", ARCHITECTURE_TILEGX },
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#elif defined(__cris__)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt { "crisv32", ARCHITECTURE_CRIS },
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#else
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#error "Please register your architecture here!"
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt#endif
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt };
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt static int cached = _ARCHITECTURE_INVALID;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt struct utsname u;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt unsigned i;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if (cached != _ARCHITECTURE_INVALID)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt return cached;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt assert_se(uname(&u) >= 0);
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt for (i = 0; i < ELEMENTSOF(arch_map); i++)
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt if (streq(arch_map[i].machine, u.machine))
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt return cached = arch_map[i].arch;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams assert_not_reached("Couldn't identify architecture. You need to patch systemd.");
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams return _ARCHITECTURE_INVALID;
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt}
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williamsstatic const char *const architecture_table[_ARCHITECTURE_MAX] = {
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_X86] = "x86",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_X86_64] = "x86-64",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_PPC] = "ppc",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_PPC_LE] = "ppc-le",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_PPC64] = "ppc64",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_PPC64_LE] = "ppc64-le",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_IA64] = "ia64",
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ARCHITECTURE_PARISC] = "parisc",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_PARISC64] = "parisc64",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_S390] = "s390",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_S390X] = "s390x",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_SPARC] = "sparc",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_SPARC64] = "sparc64",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_MIPS] = "mips",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_MIPS_LE] = "mips-le",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_MIPS64] = "mips64",
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams [ARCHITECTURE_MIPS64_LE] = "mips64-le",
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ARCHITECTURE_ALPHA] = "alpha",
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ARCHITECTURE_ARM] = "arm",
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ARCHITECTURE_ARM_BE] = "arm-be",
139b011ab81ccea1d51f09e0261a1c390115c6ffPatrik Flykt [ARCHITECTURE_ARM64] = "arm64",
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller [ARCHITECTURE_ARM64_BE] = "arm64-be",
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller [ARCHITECTURE_SH] = "sh",
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller [ARCHITECTURE_SH64] = "sh64",
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller [ARCHITECTURE_M68K] = "m68k",
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller [ARCHITECTURE_TILEGX] = "tilegx",
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller [ARCHITECTURE_CRIS] = "cris",
cc22955cfefb4bd6e7a135f1ec95fb5a07ba9ce3Thomas Haller};
ebe207d4acf38165adbc45298662982eecdb9e9fTom Gundersen
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan WilliamsDEFINE_STRING_TABLE_LOOKUP(architecture, int);
66eac1201a9c1596f5901f8dbbf24bda7e350878Dan Williams