syscall: improve architecture detection
authorDmitry Selyutin <ghostmansd@gmail.com>
Wed, 25 Oct 2023 19:49:11 +0000 (22:49 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Wed, 25 Oct 2023 20:14:32 +0000 (23:14 +0300)
src/openpower/decoder/isa/caller.py
src/openpower/syscalls/__init__.py
src/openpower/syscalls/__main__.py

index e4f06266c8edb7829875dadea12ff18d3ea59d39..716f143cdffa6cd98dff2008d0372423abf6ddeb 100644 (file)
@@ -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):
index fcf247a928e0b24aac918b2be044ba1a372ec0b1..88006fab89de425b0b3614d7d6555de6bfa4e858 100644 (file)
@@ -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"]
 
index 3165e1a3a5b980f1718b8ccd6898b115903674bd..dc4b92cc63d3ab5e73316966ce0fd7e7f91891d2 100644 (file)
@@ -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()))