From patchwork Wed Apr 21 19:01:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amanieu d'Antras X-Patchwork-Id: 12216871 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A31CBC43460 for ; Wed, 21 Apr 2021 19:05:07 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F22A361453 for ; Wed, 21 Apr 2021 19:05:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F22A361453 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=Q5ekYer272zfGCwB3jrywrI4Rr9jNjfuuUJkcVSNUek=; b=jbPKX/PUA1MUwVwESUuX1RvH1U I63cXnLSwt0pk7jSuumtqJE0r4jqoXtQPoIXAMgdzPHVltMU9VyIANmEhVQkgdtA5ykCiW1zfZKDE j1CWB3+Veh8HoS3331LX/DbmFK8KcsVevsHAIHb++3X7RiErtJafgHAEvj+n3f6rVjDaNpSE/zARJ 4tAx+MHQby//wcQ8fZlbw2zRolEhC02fhcNbIXpyxx5DXVW/aeUbf6Apii1qvQA8kLhOWK55moPlK ekFxeeqQ8rrNZLVQTMNMz/S0xwsYvsLnbZpem7i5WzA7jkJzJ1T8MMOY8mHMLO2swP0c7ig73DSi5 nNpausOQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lZI7g-00EzKn-VM; Wed, 21 Apr 2021 19:02:53 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lZI7P-00EzJQ-SW for linux-arm-kernel@desiato.infradead.org; Wed, 21 Apr 2021 19:02:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:In-Reply-To:References; bh=dOvjwVx5dfr56lHtn9+AtLh3kYl7CiCHcmbsPPYndzU=; b=bzxY/uO/WX1c3muEhAdCw8q5+/ Plf4XXqUOgFSXGDWd1ymt1a4lPSNpbJAt6qrncT9/1IXANmXkLFbVaZLHVGnEoWXu4XlMuCoyzLky wL9G60Tf5H9a3GItHoQtzQU7k/vrpZPTFE17PybhfOTcWhqW3XJkxB/7jM78c3IxvVzIEwQPxoLWz p3zIjXWnQNVGO26pSD0g72w39C48Fr2QYqUsA+SSQ0i9XDTFbhcyAgrxHCfhCvhOuF/P38fTQZAFw jUS851DC5euewXOt6IoKjF3hOLjWBsnsZB6e/ecBZ8JXGofFH+T837Di+bikdE4aLSycjBLWDkXTU Lt5sB5Lg==; Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lZI7M-00D7SU-PS for linux-arm-kernel@lists.infradead.org; Wed, 21 Apr 2021 19:02:34 +0000 Received: by mail-wm1-x332.google.com with SMTP id y204so21368929wmg.2 for ; Wed, 21 Apr 2021 12:02:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dOvjwVx5dfr56lHtn9+AtLh3kYl7CiCHcmbsPPYndzU=; b=Hm9MKwcNZEx9rz54bp6/L0mPzV+tLEP5q6RGbSudOrE+8zC7vAw00FMg4wgTPnbLYB FCvoS9jlxIrmNa4XxpxIEFzFZU0bqHC3X1MaRZFedCPvw20RYrrBN9oHRR5kZevk/7T9 hyBrHF/vnXE9u4pfoJfzEikn9e++W0QDEzlUMqQJm7tRtFgaAfWZcl/roAOjZzPluRXE jYHKF9pJQ11kvYXVFgiLGkDrdLdhtwEaQORDsJSjAlRD9RPR8XRS/MwoYad+2AtDf4SX XnBL7Nb8jUiSsbX7ZIFx2wISZ7ysjLoO/MeB5w1c+7705ewJv6hddNwX0gwoPgdnoyOA NZCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dOvjwVx5dfr56lHtn9+AtLh3kYl7CiCHcmbsPPYndzU=; b=fPjTDoeH5Qg+0h2umYsQV3KwHm6kCg4jeGd1nV9/+y8pcK5Tk3UW/DttUSvcahC3d8 3aUEdMru9+GsYsLuXxYvhTsaGNwWJc57n5T5IRwXoF7EaJ6aTPDfyCrSxUypzxgjwRKY 56hDPk6GGaUMeQsE6c1cd51JYsrJ7ezavyror/9CoZiXNmeFRUc9MEgklC1Tg3IJJHac UL3FUgMfA/fBpZUiXQ6kUp3SL46e/VIxnQGzE9Zj7qn0JNqxzyGPd1zOTTenVbf3MnQM vvzic4+ppQavkCVZ9z3Cov/u+9T6IEK86s3sXL6+ZPiNWo3jDIMUJqOS7vx8wLdPXQEh e8Gg== X-Gm-Message-State: AOAM5336glFzRmvMWbxsUSXthXtBmclf0CkwnW+wMyU+Ryx7IwZSX8Bz 8K6MW7Cvi1p4erBCTmTsJ+WMtaiNjFiwTcix X-Google-Smtp-Source: ABdhPJxIsql5FrJgfpIpQpl7aJT+yMewBbw88mZ98sGpoLAhjT2rRgl7TAG6qL2+Eqwsa/jLOxc57A== X-Received: by 2002:a05:600c:3796:: with SMTP id o22mr11204618wmr.139.1619031749062; Wed, 21 Apr 2021 12:02:29 -0700 (PDT) Received: from amanieu-desktop.home ([2a00:23c6:f081:f801:93ef:94e:9f8:1f0]) by smtp.gmail.com with ESMTPSA id d10sm310829wri.41.2021.04.21.12.02.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Apr 2021 12:02:28 -0700 (PDT) From: Amanieu d'Antras To: linux-arm-kernel@lists.infradead.org Cc: Amanieu d'Antras , Ryan Houdek , Catalin Marinas , Will Deacon , Mark Rutland , Steven Price , Arnd Bergmann , David Laight , Mark Brown Subject: [RFC PATCH v3 0/8] arm64: Allow 64-bit tasks to invoke compat syscalls Date: Wed, 21 Apr 2021 20:01:59 +0100 Message-Id: <20210421190207.1803139-1-amanieu@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210421_120232_914879_B68D8CDE X-CRM114-Status: GOOD ( 31.95 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This series allows AArch64 tasks to perform 32-bit syscalls by setting the top bit of x8 and using AArch32 compat syscall numbers: syscall(0x80000000 | __ARM_NR_write, 1, "foo\n", 4); Internally, setting this bit does the following: - The remainder of x8 is treated as a compat syscall number and is used to index the compat syscall table. - in_compat_syscall will return true for the duration of the syscall. - VM allocations performed by the syscall will be located in the lower 4G of the address space. A separate compat_mmap_base is used so that these allocations are still properly randomized. - Interrupted compat syscalls are properly restarted as compat syscalls. - Seccomp will treats the syscall as having AUDIT_ARCH_ARM instead of AUDIT_ARCH_AARCH64. This affects the arch value seen by seccomp filters and reported by SIGSYS. - PTRACE_GET_SYSCALL_INFO also treats the syscall as having AUDIT_ARCH_ARM. Recent versions of strace will correctly report the syscall name and parameters when an AArch64 task mixes 32-bit and 64-bit syscalls. This feature is intended for use in software compatibility layers which emulate a 32-bit program on AArch64. This patch has been tested on two such emulators: - Tango [1], which enables AArch32 binaries to run on AArch64 CPUs which do not have hardware support for AArch32. Tango is used to run virtual Android devices on AArch64 servers. - FEX [2], an emulator for running x86 and x86_64 binaries on AArch64. FEX can already run many x86_64 programs including 3D games, but requires kernel support for running running 32-bit x86 binaries. Both FEX and Tango have previously attempted to translate 32-bit syscalls purely in user mode like QEMU does for its user mode emulation. While this works for simple programs, there are many limitations which cannot be solved without kernel support, for example: - There are a huge number of ioctls which behave differently in 32-bit mode. It is impractical and error prone to manually emulate them all in user mode. Specifically, the kernel already has a well-tested and reliable compatibility layer and it makes sense to reuse this. QEMU supports emulating some ioctls in userspace but this still does not cover devices like GPUs which are needed for accelerated rendering. - The 64-bit set_robust_list is not compatible with the 32-bit ABI. The compat version of set_robust_list must be used. Emulating this in user mode is not reliable since SIGKILL cannot be caught. - io_uring uses iovec structures as part of its API, which have different sizes on 32-bit and 64-bit. - ext4 represents positions in directories as 64-bit hashes, which break if they are truncated to 32 bits. There is special support for 32-bit off_t in the ext4 driver but this is only used when in_compat_syscall is true: https://bugzilla.kernel.org/show_bug.cgi?id=205957 - The io_setup syscall allocates a VM area for the AIO context and returns it. But there is no way to control where this context is allocated so it will almost always end up above the 4GB limit. - Some ioctls will also perform VM allocations, with the same issues as io_setup. Search for "vm_mmap" in drivers/. - Some file descriptors have alignment requirements which are not known to userspace. For example, a hugetlbfs file can only be mmaped at a huge page alignment but there is no way for userspace to know this when it needs to manually select an address below 4GB for the mapping. All of these issues are solved in FEX and Tango by invoking compat syscalls directly. In the case of FEX, there remain some differences between the arm and x86 ABIs due to alignment issues, but these are few enough to be individually handled in userspace. There is a precedent for exposing this functionality to userspace: x86_64 has 2 ways to invoke 32-bit syscalls. The first is to use int 0x80 with a 32-bit syscall number and the second is to use __X32_SYSCALL_BIT with a 64-bit syscall number. As such, the generic kernel code is already able to properly handle tasks that invoke both 32-bit and 64-bit syscalls. [1] https://www.amanieusystems.com/ [2] https://github.com/FEX-Emu/FEX Changelog since v2: - Complete rewrite, based on the patch that was previously posted as: [PATCH v2] [RFC] arm64: Exposes support for 32-bit syscalls Amanieu d'Antras (8): mm: Add arch_get_mmap_base_topdown macro hugetlbfs: Use arch_get_mmap_* macros mm: Support mmap_compat_base with the generic layout arm64: Separate in_compat_syscall from is_compat_task arm64: mm: Use HAVE_ARCH_COMPAT_MMAP_BASES arm64: Add a compat syscall flag to thread_info arm64: Forbid calling compat sigreturn from 64-bit tasks arm64: Allow 64-bit tasks to invoke compat syscalls arch/arm64/Kconfig | 1 + arch/arm64/include/asm/compat.h | 25 ++++++++++++--- arch/arm64/include/asm/elf.h | 21 ++++++++++--- arch/arm64/include/asm/ftrace.h | 2 +- arch/arm64/include/asm/processor.h | 32 +++++++++++-------- arch/arm64/include/asm/syscall.h | 6 ++-- arch/arm64/include/asm/thread_info.h | 3 ++ arch/arm64/include/uapi/asm/unistd.h | 2 ++ arch/arm64/kernel/ptrace.c | 2 +- arch/arm64/kernel/signal.c | 5 +++ arch/arm64/kernel/signal32.c | 8 +++++ arch/arm64/kernel/syscall.c | 23 ++++++++++++-- arch/arm64/mm/mmap.c | 33 ++++++++++++++++++++ fs/hugetlbfs/inode.c | 22 ++++++++++--- mm/mmap.c | 14 ++++++--- mm/util.c | 46 +++++++++++++++++++++++----- 16 files changed, 200 insertions(+), 45 deletions(-)