[PATCH] linux-user: Add support for LoongArch64's old world ABI

2023-01-23 Thread WANG Xuerui
From: WANG Xuerui 

This patch adds a "loongarch64ow-linux-user" target and a
corresponding "qemu-loongarch64ow" binary, for supporting user-mode
emulation of old-world LoongArch applications in the wild.

Although the old-world LoongArch is already being (slowly) phased out,
there are already a number of deployments (mainly as a result of
LoongArch's early commercial growth), whose migration path is something
software developers have to care about. Support for user-mode emulation
in addition to system-level emulation would help development of such
migration & compatibility solutions.

The differences we have to care about are:

- TARGET_NSIG: 64 for new-world, 128 for old-world
- System calls: fstat, newfstatat, getrlimit and setrlimit only present
  on old-world

Other incompatibilities exist, but they are irrelevant to emulators like
QEMU, as these are entirely contained within the particular firmware
image or sysroot in use.

Corresponding binfmt_misc magic has been updated to allow somewhat
proper classification of LoongArch executables based on the object ABI
version bitfield [1] in the ELF header's e_flags field. We make the
assumption that all old-world binaries are compiled with an older
toolchain that produce v0 binaries, and that all new-world users have
migrated to object ABI v1 by now -- the initial new-world userbase is
small and technical enough that anyone who has not migrated by now is
simply advised to rebuild world or reinstall.

As for why this cannot be done within one binary: currently TARGET_NSIG
is a compile-time constant, hence it is not currently possible for us
to implement an allegedly more elegant solution, of checking the
.note.ABI-tag section (then falling back to e_flags heuristics if the
optional section is absent).

[1]: 
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_e_flags_identifies_abi_type_and_version

Signed-off-by: WANG Xuerui 
---

Tested by successfully emulating a Loongnix chroot, running old-world
LoongArch apps such as the bundled Kingsoft Office 2019, in addition to
common CLI utilities and some of the MATE apps included in the distro's
live ISO.

 configs/targets/loongarch64ow-linux-user.mak |   4 +
 linux-user/loongarch64/syscall_base_nr.h | 312 ++
 linux-user/loongarch64/syscall_nr.h  | 316 +--
 linux-user/syscall_defs.h|   9 +-
 scripts/gensyscalls.sh   |   2 +-
 scripts/qemu-binfmt-conf.sh  |  12 +-
 6 files changed, 340 insertions(+), 315 deletions(-)
 create mode 100644 configs/targets/loongarch64ow-linux-user.mak
 create mode 100644 linux-user/loongarch64/syscall_base_nr.h

diff --git a/configs/targets/loongarch64ow-linux-user.mak 
b/configs/targets/loongarch64ow-linux-user.mak
new file mode 100644
index 00..41eb8c16a7
--- /dev/null
+++ b/configs/targets/loongarch64ow-linux-user.mak
@@ -0,0 +1,4 @@
+# Default configuration for loongarch64-linux-user
+TARGET_ARCH=loongarch64
+TARGET_BASE_ARCH=loongarch
+TARGET_ABI_LOONGARCH64_OW=y
diff --git a/linux-user/loongarch64/syscall_base_nr.h 
b/linux-user/loongarch64/syscall_base_nr.h
new file mode 100644
index 00..f84434bfa2
--- /dev/null
+++ b/linux-user/loongarch64/syscall_base_nr.h
@@ -0,0 +1,312 @@
+/*
+ * This file contains the system call numbers.
+ * Do not modify.
+ * This file is generated by scripts/gensyscalls.sh
+ */
+#ifndef LINUX_USER_LOONGARCH_SYSCALL_BASE_NR_H
+#define LINUX_USER_LOONGARCH_SYSCALL_BASE_NR_H
+
+#define TARGET_NR_io_setup 0
+#define TARGET_NR_io_destroy 1
+#define TARGET_NR_io_submit 2
+#define TARGET_NR_io_cancel 3
+#define TARGET_NR_io_getevents 4
+#define TARGET_NR_setxattr 5
+#define TARGET_NR_lsetxattr 6
+#define TARGET_NR_fsetxattr 7
+#define TARGET_NR_getxattr 8
+#define TARGET_NR_lgetxattr 9
+#define TARGET_NR_fgetxattr 10
+#define TARGET_NR_listxattr 11
+#define TARGET_NR_llistxattr 12
+#define TARGET_NR_flistxattr 13
+#define TARGET_NR_removexattr 14
+#define TARGET_NR_lremovexattr 15
+#define TARGET_NR_fremovexattr 16
+#define TARGET_NR_getcwd 17
+#define TARGET_NR_lookup_dcookie 18
+#define TARGET_NR_eventfd2 19
+#define TARGET_NR_epoll_create1 20
+#define TARGET_NR_epoll_ctl 21
+#define TARGET_NR_epoll_pwait 22
+#define TARGET_NR_dup 23
+#define TARGET_NR_dup3 24
+#define TARGET_NR_fcntl 25
+#define TARGET_NR_inotify_init1 26
+#define TARGET_NR_inotify_add_watch 27
+#define TARGET_NR_inotify_rm_watch 28
+#define TARGET_NR_ioctl 29
+#define TARGET_NR_ioprio_set 30
+#define TARGET_NR_ioprio_get 31
+#define TARGET_NR_flock 32
+#define TARGET_NR_mknodat 33
+#define TARGET_NR_mkdirat 34
+#define TARGET_NR_unlinkat 35
+#define TARGET_NR_symlinkat 36
+#define TARGET_NR_linkat 37
+#define TARGET_NR_umount2 39
+#define TARGET_NR_mount 40
+#define TARGET_NR_pivot_root 41
+#define TARGET_NR_nfsservctl 42
+#define TARGET_NR_statfs 43
+#define TARGET_NR_fstatfs 44
+#define TARGET_NR_truncate 45
+#define TARGET_NR_ftruncate 46

Re: [PATCH] linux-user: Add support for LoongArch64's old world ABI

2023-01-23 Thread WANG Xuerui

On 1/23/23 18:47, Peter Maydell wrote:

On Mon, 23 Jan 2023 at 10:27, WANG Xuerui  wrote:


From: WANG Xuerui 

This patch adds a "loongarch64ow-linux-user" target and a
corresponding "qemu-loongarch64ow" binary, for supporting user-mode
emulation of old-world LoongArch applications in the wild.

Although the old-world LoongArch is already being (slowly) phased out,
there are already a number of deployments (mainly as a result of
LoongArch's early commercial growth), whose migration path is something
software developers have to care about. Support for user-mode emulation
in addition to system-level emulation would help development of such
migration & compatibility solutions.


Is this 'old-world' ABI supported by the upstream Linux kernel?
I can't see signs of it from a quick grep. If it isn't, then
I'm not sure if we should support it in QEMU user-mode emulation.
We've always set "upstream Linux" as our definition of what the
official ABI and featureset is for usermode emulation.


No, the old-world is not, and will not be, supported by upstream Linux, 
as it is strictly the premature state of this architecture only born for 
commercial reasons.


After sending the patch I've discussed with several other maintainers of 
various LoongArch ports, and it seems even Loongson the corporation 
itself is seeing to completely abandon old-world development in favor of 
a 100% new world future (which is even more courageous than me, an 
unaffiliated hobbyist user). Given the current old-world deployments all 
have commercial support, it's probably best to leave the migration work 
for them and keep upstream clean of any such legacy.


So I'm dropping this patch now, and thanks for the feedback!



Re: [PATCH] linux-user: Add support for LoongArch64's old world ABI

2023-01-23 Thread Peter Maydell
On Mon, 23 Jan 2023 at 10:27, WANG Xuerui  wrote:
>
> From: WANG Xuerui 
>
> This patch adds a "loongarch64ow-linux-user" target and a
> corresponding "qemu-loongarch64ow" binary, for supporting user-mode
> emulation of old-world LoongArch applications in the wild.
>
> Although the old-world LoongArch is already being (slowly) phased out,
> there are already a number of deployments (mainly as a result of
> LoongArch's early commercial growth), whose migration path is something
> software developers have to care about. Support for user-mode emulation
> in addition to system-level emulation would help development of such
> migration & compatibility solutions.

Is this 'old-world' ABI supported by the upstream Linux kernel?
I can't see signs of it from a quick grep. If it isn't, then
I'm not sure if we should support it in QEMU user-mode emulation.
We've always set "upstream Linux" as our definition of what the
official ABI and featureset is for usermode emulation.

thanks
-- PMM