From 066fe2eae8514911ee8ff882ab7a8c98642662a7 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Wed, 25 Oct 2023 22:49:11 +0300 Subject: [PATCH] syscall: improve architecture detection --- src/openpower/decoder/isa/caller.py | 12 ++++----- src/openpower/syscalls/__init__.py | 41 ++++++++++++++++++----------- src/openpower/syscalls/__main__.py | 10 +++---- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/openpower/decoder/isa/caller.py b/src/openpower/decoder/isa/caller.py index e4f06266..716f143c 100644 --- a/src/openpower/decoder/isa/caller.py +++ b/src/openpower/decoder/isa/caller.py @@ -16,9 +16,10 @@ related bugs: from collections import namedtuple from copy import deepcopy from functools import wraps +import os +import sys from nmigen.sim import Settle -from os import uname import openpower.syscalls from openpower.consts import (MSRb, PIb, # big-endian (PowerISA versions) SVP64CROffs, SVP64MODEb) @@ -1135,13 +1136,12 @@ class StepLoop: class SyscallEmulator(openpower.syscalls.Dispatcher): def __init__(self, isacaller): - machine = uname().machine - host = { - "x86_64": "amd64", - }.get(machine, machine) - self.__isacaller = isacaller + host = os.uname().machine + bits = (64 if (sys.maxsize > (2**32)) else 32) + host = openpower.syscalls.architecture(arch=host, bits=bits) + return super().__init__(guest="ppc64", host=host) def __call__(self, identifier, *arguments): diff --git a/src/openpower/syscalls/__init__.py b/src/openpower/syscalls/__init__.py index fcf247a9..88006fab 100644 --- a/src/openpower/syscalls/__init__.py +++ b/src/openpower/syscalls/__init__.py @@ -6,18 +6,27 @@ import pathlib -ARCH = { - "powerpc": "ppc", - "powerpc64": "ppc64", - "ppc64le": "ppc64", - "i686": "i386", - "x86_64": "amd64", - "x64": "amd64", - "arm64": "aarch64", - "aarch64_be": "aarch64", - "armv8b": "aarch64", - "armv8l": "aarch64", -} +def architecture(arch, bits=0): + assert bits in (0, 32, 64) + multilib = { + ("aarch64", 32): "arm", + ("amd64", 32): "i386", + ("ppc64", 32): "ppc", + } + arch = { + "powerpc": "ppc", + "powerpc64": "ppc64", + "ppc64le": "ppc64", + "i686": "i386", + "x86_64": "amd64", + "x64": "amd64", + "arm64": "aarch64", + "aarch64_be": "aarch64", + "armv8b": "aarch64", + "armv8l": "aarch64", + }.get(arch, arch) + + return multilib.get((arch, bits), arch) class Syscall: @@ -105,9 +114,6 @@ class Dispatcher: if logger is None: logger = lambda *args, **kwargs: None - guest = ARCH.get(guest, guest) - host = ARCH.get(host, host) - def i386(sysnums): yield from sysnums["x86-32"]["i386"].items() @@ -147,6 +153,11 @@ class Dispatcher: "riscv32": riscv32, "riscv64": riscv64, } + if guest not in arch: + raise ValueError(guest) + if host not in arch: + raise ValueError(host) + sysnums = table["sysnums"] sysargs = table["sysargs"] diff --git a/src/openpower/syscalls/__main__.py b/src/openpower/syscalls/__main__.py index 3165e1a3..dc4b92cc 100644 --- a/src/openpower/syscalls/__main__.py +++ b/src/openpower/syscalls/__main__.py @@ -6,9 +6,9 @@ import pathlib import re -from . import ARCH -from . import Dispatcher -from . import UnknownSyscall +from openpower.syscalls import architecture +from openpower.syscalls import Dispatcher +from openpower.syscalls import UnknownSyscall def rename_entry(entry): @@ -262,10 +262,10 @@ def main(): ecall_parser = main_subparsers.add_parser("ecall") ecall_parser.add_argument("guest", help="guest architecture", - type=lambda arch: ARCH.get(arch, arch)) + type=architecture) ecall_parser.add_argument("host", help="amd64 architecture", - type=lambda arch: ARCH.get(arch, arch)) + type=architecture) ecall_parser.set_defaults(generate=ECallGenerator()) arguments = dict(vars(main_parser.parse_args())) -- 2.30.2