]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/helpers/arch.py
session_manager: Stop the container on SIGHUP
[waydroid.git] / tools / helpers / arch.py
1 # Copyright 2021 Oliver Smith
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 import platform
4 import logging
5 import ctypes
6
7 def is_32bit_capable():
8 # man 2 personality
9 personality = ctypes.CDLL(None).personality
10 personality.restype = ctypes.c_int
11 personality.argtypes = [ctypes.c_ulong]
12 # linux/include/uapi/linux/personality.h
13 PER_LINUX32 = 0x0008
14 # mirror e.g. https://github.com/util-linux/util-linux/blob/v2.41/sys-utils/lscpu-cputype.c#L745-L756
15 pers = personality(PER_LINUX32)
16 if pers != -1: # success, revert back to previous persona (typically just PER_LINUX, 0x0000)
17 personality(pers)
18 return True
19 return False # unable to "impersonate" 32-bit host, nothing done
20
21 def host():
22 machine = platform.machine()
23
24 mapping = {
25 "i686": "x86",
26 "x86_64": "x86_64",
27 "aarch64": "arm64",
28 "armv7l": "arm",
29 "armv8l": "arm"
30 }
31 if machine in mapping:
32 return maybe_remap(mapping[machine])
33 raise ValueError("platform.machine '" + machine + "'"
34 " architecture is not supported")
35
36 def maybe_remap(target):
37 if target.startswith("x86"):
38 with open("/proc/cpuinfo") as f:
39 cpuinfo = f.read()
40 if "ssse3" not in cpuinfo:
41 raise ValueError("x86/x86_64 CPU must support SSSE3!")
42 if target == "x86_64" and "sse4_2" not in cpuinfo:
43 logging.info("x86_64 CPU does not support SSE4.2, falling back to x86...")
44 return "x86"
45 elif target == "arm64" and platform.architecture()[0] == "32bit":
46 return "arm"
47 elif target == "arm64" and not is_32bit_capable():
48 logging.info("AArch64 CPU does not appear to support AArch32, assuming arm64_only...")
49 return "arm64_only"
50
51 return target