arm64: Exposes support for 32bit syscalls
Subsystems (3)
RFC PATCH 1/1 arm64: Exposes support for 32bit syscalls
From: Ryan Houdek <[email protected]>
This is a continuation of https://lkml.org/lkml/2021/1/6/47
This patch is currently based against a 5.10 kernel but rebasing against latest HEAD is trivial
Specifically Amanieu pointed out a couple of problem spaces that would show up around memory management and various other bits.
This convinced me that the previous path of only having an ioctl32 syscall is only a bandage on a much larger problem.
This takes a patch from the Tango support tree and modifies it a bit to not rely on a Tango specific quirk.
Original patch:
https://github.com/Amanieu/linux/commit/b4783002afb027ae702da8f56e43e45c7332d226
This patch changes the KSTK_EIP and KSTK_ESP helpers to only fall to Tango specific behaviour once a "Tango" syscall has been invoked. I'm working on a backwards compatibility project that is unrelated to Tango, but it would be nice to have a solution that works for both of us. Since we are both working on projects that run 32bit applications inside of a 64bit process for compatibility purposes.
Some notes for what I'm looking for here.
- Is the way of invoking the compat syscall table amenable to upstream? Currently I'm using svc #1 for a compat syscall and svc 2 for a Tango syscall
Everything else falls down to regular syscall table
There is zero documentation for userspace around if the kernel is only allowed to handle syscalls in svc #0 or any svc ISS will suffice.
- Alternatively we could reserve a high bit in the syscall number like x32 to get to this compat table?
This is how the original Tango patch worked. If the high bit of the 32bit syscall number was set then it goes down the compat table path.
Talking with Amanieu, both paths will work for our projects, just need to standardize on one of them.
Considering I'm not a regular contributor to the Linux kernel, I won't be able to bikeshed to choose a particular path. Both are equally easy to support in userspace.
- If going down the SVC ISS path for the table, how to detect if the kernel is compiled with the option?
Currently this is unimplemented in my patch because it wasn't a blocker for the RFC.
If it is compiled out I can see it being fine to just return -ENOSYS for {1,2} SVC ISS numbers.
Alternatively we could call some syscalls that do pointer validation for compat and check for -EINVAL or something similar.
Obviously the -ENOSYS path is cleaner and maps to someone trying to use 0x8000'0000 from a syscall number impelementation.
- Should the 32bit compat and Tango paths be separated?
Even with garbage data in x15 and x13 for EIP/ESP then using the single Tango path isn't a killer for my project FEX.
This seemingly will only break things like seccomp and stat? Alternatively before my 32bit syscall I can do a rsp and PC calculation before the svc instruction for it to be correct.
All the options are easy to support here, just seemed nicer to keep them separated because the behaviour is slightly different here.
- Which syscalls are we leaning on to require this patch? This is required for a couple of syscalls
- ioctl - Physically impossible to support emulating in all situations
- Even full ioctl emulation in userspace has broken use cases
- mmap, mremap, shmat - Emulating this is userspace changes behaviour Specifically since userspace doesn't have a full VMA view we must generate a pessimistic memory allocator that uses FIXED_NOREPLACE to see if we can fit an allocation.
This changes behaviour and can actually break code. A good example is ARM's kbase kernel driver actually doesn't support FIXED placement so would never work without this OR changing their downstream code. If even ARM breaks then I don't want to know about random other things that will end up breaking in this case. Not to mention that with a bad userspace allocator, it ends up having worst performance because it needs to be pessimistic about allocations
- recvmsg,sendmsg,recvmmsg,sendmmsg
This isn't necessarily 100% required but these are complex and very easy to break. Evident that implementing this I already found bugs in my userspace implementation. The flex array members for the various message types are a bit of a pain point.
Now some more background from the original patch
Problem presented: "FEX" project point of view
A backwards compatibility layer that allows running x86-64 and x86 Processes inside of an AArch64 process.
- CPU is emulated
- syscall interface is mostly passthrough
- Some syscalls require patching or emulation depending on mismatched structures between architectures
- Not viable from the emulator design to use an AArch64 host process
x86-64 and x86 userspace emulator source:
https://github.com/FEX-Emu/FEX
Cross documentation:
https://github.com/FEX-Emu/FEX/wiki/32Bit-x86-Woes
ioctl interface problems presented
ioctls are opaque from the emulator perspective and the data wants to be passed through a syscall as unimpeded as possible.
Sadly due to ioctl struct differences between x86 and x86-64, we need a syscall that exposes the compatibility ioctl handler to userspace in a 64bit process.
This is necessary behaves of the behaviour differences that occur between an x86 process doing an ioctl and an x86-64 process doing an ioctl.
This ioctl gets captured in the emulation and then passed through the compatibility syscall interface
The only supported hosts where we care about this currently is AArch64 and x86-64 (For testing purposes).
PPC64LE, MIPS64LE, and RISC-V64 might be interesting to support in the future; But I don't have any platforms that get anywhere near Cortex-A77 performance in those architectures. Nor do I have the time to bring up the emulator on them.
This does not solve the following problems:
1) compat_alloc_user_space inside ioctl (Apparently this is nearly gone)
2) struct packing problems between architectures
Workarounds for the problems presented:
1a) Do a stack pivot to the lower 32bits from userspace
- Forces host 64bit process to have its thread stacks to live in 32bit space. Not ideal.
- Only do a stack pivot on ioctl to save previous 32bit VA space
1b) Teach kernel that compat_alloc_userspace can return a 64bit pointer
- x86-64 truncates stack from this function
- AArch64 returns the full stack pointer
- Only around 29 users. Validating all of them support a 64bit stack is trivial?
2a) Blocklist any application using ioctls that have different struct packing across the boundary
- Can happen when struct packing of 32bit x86 application goes down the aarch64 compat_ioctl path
- Userspace is a AArch64 process passing 32bit x86 ioctl structures through the compat_ioctl path which is typically for AArch32 processes
- None currently identified
2b) Work with upstream kernel and userspace projects to evaluate and fix
- Identify the problem ioctls
- Implement a new ioctl with more sane struct packing that matches cross-arch
- Implement new ioctl while maintaining backwards compatibility with previous ioctl handler
- Change upstream project to use the new compatibility ioctl
- ioctl deprecation will be case by case per device and project
2b) Userspace implements a full ioctl emulation layer
- Parses the full ioctl tree
- Either passes through ioctls that it doesn't understand or transforms ioctls that it knows are trouble
- Has the downside that it can still run in to edge cases that will fail
- Performance of additional tracking is a concern
- Prone to failure keeping the kernel ioctl and userspace ioctl handling in sync
- Really want to have it in the kernel space as much as possible
- Haven't encountered this yet, but it is sure to happen eventually
- Some amount of userspace parsing is expected to be done at some point
--- arch/arm64/Kconfig | 19 ++ arch/arm64/include/asm/compat.h | 31 ++++ arch/arm64/include/asm/exception.h | 2 +- arch/arm64/include/asm/mmu.h | 7 + arch/arm64/include/asm/pgtable.h | 10 ++ arch/arm64/include/asm/processor.h | 28 ++- arch/arm64/include/asm/thread_info.h | 9 + arch/arm64/kernel/asm-offsets.c | 3 + arch/arm64/kernel/entry-common.c | 9 +- arch/arm64/kernel/fpsimd.c | 2 +- arch/arm64/kernel/hw_breakpoint.c | 2 +- arch/arm64/kernel/perf_regs.c | 2 +- arch/arm64/kernel/process.c | 13 +- arch/arm64/kernel/ptrace.c | 6 +- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/syscall.c | 29 +++- arch/arm64/mm/mmap.c | 249 +++++++++++++++++++++++++++ 17 files changed, 402 insertions(+), 21 deletions(-)
arch/arm64/Kconfig +19 -0
@@ -1147,6 +1147,25 @@ config XEN help Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64. +config ARM_COMPAT_DISPATCH + bool "32bit syscall dispatch table" + depends on COMPAT && ARM64 + default y + help + Kernel support for exposing the 32-bit syscall dispatch table to + userspace. + For dynamically translating 32-bit applications to a 64-bit process. + +config TANGO_BT + bool "Tango binary translator support" + depends on ARM_COMPAT_DISPATCH + select CHECKPOINT_RESTORE + select BINFMT_MISC + default y + help + Kernel support for the Tango binary translator which dynamically + translates 32-bit ARM binaries into 64-bit code. + config FORCE_MAX_ZONEORDER int default "14" if (ARM64_64K_PAGES && TRANSPARENT_HUGEPAGE)
arch/arm64/include/asm/compat.h +31 -0
@@ -180,14 +180,45 @@ struct compat_shmid64_ds {static inline int is_compat_task (void ) { +#ifdef CONFIG_ARM_COMPAT_DISPATCH +/* It is compatible if Tango, 32bit compat, or 32bit thread */ +return current_thread_info ()->compat_syscall_flags !=0 ||test_thread_flag (TIF_32BIT ); +#else return test_thread_flag (TIF_32BIT ); +#endif }static inline int is_compat_thread (struct thread_info *thread) +{ +#ifdef CONFIG_ARM_COMPAT_DISPATCH +/* It is compatible if Tango, 32bit compat, or 32bit thread */ +return thread->compat_syscall_flags !=0 ||test_ti_thread_flag (thread,TIF_32BIT ); +#else +return test_ti_thread_flag (thread,TIF_32BIT ); +#endif +} + +static inline int is_aarch32_compat_task (void ) +{ +return test_thread_flag (TIF_32BIT ); +} + +static inline int is_aarch32_compat_thread (struct thread_info *thread) {return test_ti_thread_flag (thread,TIF_32BIT ); } +#ifdef CONFIG_TANGO_BT +static inline int is_tango_compat_task (void ) +{ +return current_thread_info ()->compat_syscall_flags & _TIF_COMPAT_TANGOSYSCALL; +} +static inline int is_tango_compat_thread (struct thread_info *thread) +{ +return thread->compat_syscall_flags & _TIF_COMPAT_TANGOSYSCALL; +} +#endif +#else /* !CONFIG_COMPAT */ static inline int is_compat_thread (struct thread_info *thread)
arch/arm64/include/asm/exception.h +1 -1
@@ -45,7 +45,7 @@ void do_sysinstr(unsigned int esr, struct pt_regs *regs);void do_sp_pc_abort (unsigned long addr,unsigned int esr,struct pt_regs *regs);void bad_el0_sync (struct pt_regs *regs,int reason,unsigned int esr);void do_cp15instr (unsigned int esr,struct pt_regs *regs); -void do_el0_svc (struct pt_regs *regs); +void do_el0_svc (struct pt_regs *regs,unsigned int iss);void do_el0_svc_compat (struct pt_regs *regs);void do_ptrauth_fault (struct pt_regs *regs,unsigned int esr);#endif /* __ASM_EXCEPTION_H */
arch/arm64/include/asm/mmu.h +7 -0
@@ -27,6 +27,9 @@ typedef struct {refcount_t pinned;void *vdso;unsigned long flags; +#ifdef CONFIG_ARM_COMPAT_DISPATCH +unsigned long compat_mmap_base; +#endif } mm_context_t;/*
@@ -79,6 +82,10 @@ extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot);extern void mark_linear_text_alias_ro (void );extern bool kaslr_requires_kpti (void ); +#ifdef CONFIG_ARM_COMPAT_DISPATCH +extern void process_init_compat_mmap (void ); +#endif +#define INIT_MM_CONTEXT (name) \ .pgd = init_pg_dir,
arch/arm64/include/asm/pgtable.h +10 -0
@@ -974,6 +974,16 @@ static inline bool arch_faults_on_old_pte(void) }#define arch_faults_on_old_pte arch_faults_on_old_pte +/* +* We provide our own arch_get_unmapped_area to handle 32-bit mmap calls from +* tango. +*/ +#ifdef CONFIG_ARM_COMPAT_DISPATCH +#define HAVE_ARCH_UNMAPPED_AREA +#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN +#define HAVE_ARCH_HUGETLB_UNMAPPED_AREA +#endif +#endif /* !__ASSEMBLY__ */ #endif /* __ASM_PGTABLE_H */
arch/arm64/include/asm/processor.h +25 -3
@@ -175,7 +175,7 @@ static inline void arch_thread_struct_whitelist(unsigned long *offset,#define task_user_tls (t) \ ({ \ unsigned long *__tls; \ - if (is_compat_thread(task_thread_info(t))) \ + if (is_aarch32_compat_thread(task_thread_info(t))) \ __tls = &(t)->thread.uw.tp2_value; \ else \ __tls = &(t)->thread.uw.tp_value; \
@@ -256,8 +256,30 @@ extern struct task_struct *cpu_switch_to(struct task_struct *prev,#define task_pt_regs (p) \ ((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1) -#define KSTK_EIP (tsk) ((unsigned long)task_pt_regs(tsk)->pc) -#define KSTK_ESP (tsk) user_stack_pointer(task_pt_regs(tsk)) +#ifdef CONFIG_TANGO_BT +#define KSTK_EIP (tsk) \ +({ \ + unsigned long __out; \ + if (is_tango_compat_thread(task_thread_info(tsk))) \ + __out = (unsigned long)task_pt_regs(tsk)->regs[15]; \ + else \ + __out = (unsigned long)task_pt_regs(tsk)->pc; \ + __out; \ +}) +#define KSTK_ESP (tsk) \ +({ \ + unsigned long __out; \ + if (is_tango_compat_thread(task_thread_info(tsk))) \ + __out = (unsigned long)task_pt_regs(tsk)->regs[13]; \ + else \ + __out = user_stack_pointer(task_pt_regs(tsk)); \ + __out; \ +}) + +#else +#define KSTK_EIP (tsk) ((unsigned long)task_pt_regs(tsk)->pc) +#define KSTK_ESP (tsk) user_stack_pointer(task_pt_regs(tsk)) +#endif /* * Prefetching support
arch/arm64/include/asm/thread_info.h +9 -0
@@ -41,6 +41,9 @@ struct thread_info {#endif } preempt; }; +#ifdef CONFIG_ARM_COMPAT_DISPATCH +int compat_syscall_flags;/* 32-bit compat syscall */ +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base;void *scs_sp;
@@ -107,6 +110,12 @@ void arch_release_task_struct(struct task_struct *tsk); _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \ _TIF_SYSCALL_EMU) +#define TIF_COMPAT_32BITSYSCALL 0/* Trivial 32bit compatible syscall */ +#define TIF_COMPAT_TANGOSYSCALL 1/* Tango 32bit compatible syscall */ + +#define _TIF_COMPAT_32BITSYSCALL (1 << TIF_COMPAT_32BITSYSCALL) +#define _TIF_COMPAT_TANGOSYSCALL (1 << TIF_COMPAT_TANGOSYSCALL) +#ifdef CONFIG_SHADOW_CALL_STACK #define INIT_SCS \ .scs_base = init_shadow_call_stack, \
arch/arm64/kernel/asm-offsets.c +3 -0
@@ -34,6 +34,9 @@ int main(void)#ifdef CONFIG_ARM64_SW_TTBR0_PAN DEFINE(TSK_TI_TTBR0, offsetof(struct task_struct, thread_info.ttbr0));#endif +#ifdef CONFIG_ARM_COMPAT_DISPATCH + DEFINE(TI_COMPAT_SYSCALL, offsetof(struct task_struct, thread_info.compat_syscall_flags)); +#endif #ifdef CONFIG_SHADOW_CALL_STACK DEFINE(TSK_TI_SCS_BASE, offsetof(struct task_struct, thread_info.scs_base)); DEFINE(TSK_TI_SCS_SP, offsetof(struct task_struct, thread_info.scs_sp));
arch/arm64/kernel/entry-common.c +6 -3
@@ -228,12 +228,12 @@ static void notrace el0_dbg(struct pt_regs *regs, unsigned long esr) }NOKPROBE_SYMBOL (el0_dbg); -static void notrace el0_svc(struct pt_regs *regs) +static void notrace el0_svc(struct pt_regs *regs, unsigned int iss) {if (system_uses_irq_prio_masking ())gic_write_pmr (GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ); -do_el0_svc (regs); +do_el0_svc (regs, iss); }NOKPROBE_SYMBOL (el0_svc);
@@ -251,7 +251,10 @@ asmlinkage void notrace el0_sync_handler(struct pt_regs *regs)switch (ESR_ELx_EC (esr)) {case ESR_ELx_EC_SVC64: -el0_svc (regs); +/* Redundant masking here to show we are getting ISS mask +* Then we are pulling the imm16 out of it for SVC64 +*/ +el0_svc (regs, (esr & ESR_ELx_ISS_MASK) &0xffff );break ;case ESR_ELx_EC_DABT_LOW:el0_da (regs, esr);
arch/arm64/kernel/fpsimd.c +1 -1
@@ -937,7 +937,7 @@ void fpsimd_release_task(struct task_struct *dead_task)void do_sve_acc (unsigned int esr,struct pt_regs *regs) {/* Even if we chose not to use SVE, the hardware could still trap: */ -if (unlikely (!system_supports_sve ()) ||WARN_ON (is_compat_task ())) { +if (unlikely (!system_supports_sve ()) ||WARN_ON (is_aarch32_compat_task ())) {force_signal_inject (SIGILL ,ILL_ILLOPC , regs->pc,0 );return ; }
arch/arm64/kernel/hw_breakpoint.c +1 -1
@@ -168,7 +168,7 @@ static int is_compat_bp(struct perf_event *bp)* deprecated behaviour if we use unaligned watchpoints in * AArch64 state. */ -return tsk &&is_compat_thread (task_thread_info (tsk)); +return tsk &&is_aarch32_compat_thread (task_thread_info (tsk)); }/**
arch/arm64/kernel/perf_regs.c +1 -1
@@ -66,7 +66,7 @@ int perf_reg_validate(u64 mask)u64 perf_reg_abi (struct task_struct *task) { -if (is_compat_thread (task_thread_info (task))) +if (is_aarch32_compat_thread (task_thread_info (task)))return PERF_SAMPLE_REGS_ABI_32 ;else return PERF_SAMPLE_REGS_ABI_64 ;
arch/arm64/kernel/process.c +9 -4
@@ -314,7 +314,7 @@ static void tls_thread_flush(void) {write_sysreg (0 , tpidr_el0); -if (is_compat_task ()) { +if (is_aarch32_compat_task ()) { current->thread.uw.tp_value =0 ;/*
@@ -409,7 +409,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start, *task_user_tls (p) =read_sysreg (tpidr_el0);if (stack_start) { -if (is_compat_thread (task_thread_info (p))) +if (is_aarch32_compat_thread (task_thread_info (p))) childregs->compat_sp = stack_start;else childregs->sp = stack_start;
@@ -453,7 +453,7 @@ static void tls_thread_switch(struct task_struct *next) {tls_preserve_current_state (); -if (is_compat_thread (task_thread_info (next))) +if (is_aarch32_compat_thread (task_thread_info (next)))write_sysreg (next->thread.uw.tp_value, tpidrro_el0);else if (!arm64_kernel_unmapped_at_el0 ())write_sysreg (0 , tpidrro_el0);
@@ -619,7 +619,12 @@ unsigned long arch_align_stack(unsigned long sp)*/ void arch_setup_new_exec (void ) { - current->mm->context.flags =is_compat_task () ?MMCF_AARCH32 :0 ; +#ifdef CONFIG_ARM_COMPAT_DISPATCH +process_init_compat_mmap (); +current_thread_info ()->compat_syscall_flags =0 ; +#endif + + current->mm->context.flags =is_aarch32_compat_task () ?MMCF_AARCH32 :0 ;ptrauth_thread_init_user (current);
arch/arm64/kernel/ptrace.c +3 -3
@@ -175,7 +175,7 @@ static void ptrace_hbptriggered(struct perf_event *bp,const char *desc ="Hardware breakpoint trap (ptrace)" ;#ifdef CONFIG_COMPAT -if (is_compat_task ()) { +if (is_aarch32_compat_task ()) {int si_errno =0 ;int i;
@@ -1725,7 +1725,7 @@ const struct user_regset_view *task_user_regset_view(struct task_struct *task)*/ if (is_compat_task ())return &user_aarch32_view; -else if (is_compat_thread (task_thread_info (task))) +else if (is_aarch32_compat_thread (task_thread_info (task)))return &user_aarch32_ptrace_view;#endif return &user_aarch64_view;
@@ -1906,7 +1906,7 @@ int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)/* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */ user_regs_reset_single_step (regs, task); -if (is_compat_thread (task_thread_info (task))) +if (is_aarch32_compat_thread (task_thread_info (task)))return valid_compat_regs (regs);else return valid_native_regs (regs);
arch/arm64/kernel/signal.c +1 -1
@@ -813,7 +813,7 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)/* * Set up the stack frame */ -if (is_compat_task ()) { +if (is_aarch32_compat_task ()) {if (ksig->ka.sa.sa_flags &SA_SIGINFO ) ret =compat_setup_rt_frame (usig, ksig, oldset, regs);else
arch/arm64/kernel/syscall.c +26 -3
@@ -21,7 +21,7 @@ static long do_ni_syscall(struct pt_regs *regs, int scno) {#ifdef CONFIG_COMPAT long ret; -if (is_compat_task ()) { +if (is_aarch32_compat_task ()) { ret =compat_arm_syscall (regs, scno);if (ret != -ENOSYS )return ret;
@@ -167,6 +167,9 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr,local_daif_mask (); flags =current_thread_info ()->flags;if (!has_syscall_work (flags) && !(flags & _TIF_SINGLESTEP)) { +#ifdef CONFIG_ARM_COMPAT_DISPATCH +current_thread_info ()->compat_syscall_flags =0 ; +#endif /* * We're off to userspace, where interrupts are * always enabled after we restore the flags from
@@ -180,6 +183,9 @@ static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr, trace_exit:syscall_trace_exit (regs); +#ifdef CONFIG_ARM_COMPAT_DISPATCH +current_thread_info ()->compat_syscall_flags =0 ; +#endif }static inline void sve_user_discard (void )
@@ -199,10 +205,27 @@ static inline void sve_user_discard(void)sve_user_disable (); } -void do_el0_svc (struct pt_regs *regs) +void do_el0_svc (struct pt_regs *regs,unsigned int iss) {sve_user_discard (); -el0_svc_common (regs, regs->regs[8 ], __NR_syscalls, sys_call_table); +switch (iss) { +#ifdef CONFIG_ARM_COMPAT_DISPATCH +case 1 : +current_thread_info ()->compat_syscall_flags = _TIF_COMPAT_32BITSYSCALL; +el0_svc_common (regs, regs->regs[7 ], __NR_compat_syscalls, + compat_sys_call_table); +break ; +#endif +#ifdef CONFIG_TANGO_BT +case 2 : +current_thread_info ()->compat_syscall_flags = _TIF_COMPAT_TANGOSYSCALL; +el0_svc_common (regs, regs->regs[7 ], __NR_compat_syscalls, + compat_sys_call_table); +break ; +#endif +default : +el0_svc_common (regs, regs->regs[8 ], __NR_syscalls, sys_call_table); + } }#ifdef CONFIG_COMPAT
arch/arm64/mm/mmap.c +249 -0
258 lines not on this page · raw message
Signature · 7 lines
2.27.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
maintainerHi,
Quoted 9 lines · On Tue, Feb 02, 2021 at 08:54:23AM -0800, [email protected] wrote:
From: Ryan Houdek <[email protected]>
This is a continuation of https://lkml.org/lkml/2021/1/6/47
This patch is currently based against a 5.10 kernel but rebasing against latest HEAD is trivialSpecifically Amanieu pointed out a couple of problem spaces that would show up around memory management and various other bits.
Quoted 8 lines
This convinced me that the previous path of only having an ioctl32 syscall is only a bandage on a much larger problem.
This takes a patch from the Tango support tree and modifies it a bit to not rely on a Tango specific quirk.
Original patch:
https://github.com/Amanieu/linux/commit/b4783002afb027ae702da8f56e43e45c7332d226
Please reconsider the presentation of this commit message, because as it stands it's practically unreviewable, and for the vast majority of people CC'd this is noise.
You need to write this such that someone can read this from start to finish and understand each step without jumping back-and-forth, without the reader having to read external links. Introduce the big picture first (what problem are you trying to solve? Who does this matter to?), then context (There's a constraint ..., someone previously tried this but there was a specific problem ...), then give an overview and justification for the code, with any caveats that reviewers may need to take into account.
As-is, this commit message doesn't follow that flow, and that places a burden on reviewers to expend significant effort to reverse-engineer and extract the information they need.
You'll find it's much easier to make progress if you optimize for the reviewers. Please look at the general way commits in the Linux source tree are written, and try to follow that approach.
Quoted 2 lines
This patch changes the
KSTK_EIPandKSTK_ESPhelpers to only fall to Tango specific behaviour once a "Tango" syscall has been invoked.
At this point the reader has no idea what a "Tango" syscall is, nor why this should affect KSTK_EIP or KSTK_ESP.
What is a "Tango" syscall?
Who is this useful for?
Why does this matter to the upstream kernel?
Quoted 4 lines
I'm working on a backwards compatibility project that is unrelated to Tango, but it would be nice to have a solution that works for both of us. Since we are both working on projects that run 32bit applications inside of a 64bit process for compatibility purposes.
Only here do reviewers discover this something to do with running 32-bit applications somehow, when really that should be in the first couple of sentences.
Generally, I have significant misgivings about exposing a new syscall interface (or exposing an existing interface to different callers). It's a maintainability nightmare, and since it's liable to violate implicit assumptions made in syscall implementations there's huge scope for error including bugs and exploitable behaviour.
So without significant justification, my view is to NAK this sort of change.
Thanks,
Mark.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
RFC PATCH v2 1/1 arm64: Exposes support for 32-bit syscalls
In reply to [email protected]From: Ryan Houdek <[email protected]>
Sorry about the noise. I obviously don't work in this ecosystem.
The problem:
We need to support 32-bit processes running under a userspace compatibility layer. The compatibility layer is a AArch64 process. This means exposing the 32bit compatibility syscalls to userspace.
Why do we need compatibility layers?
There are ARMv8 CPUs that only support AArch64 but still need to run AArch32 applications.
Cortex-A34/R82 and other cores are prime examples of this. Additionally if a user is needing to run legacy 32-bit x86 software, it needs the same compatibility layer.
Who does this matter to?
Any user that has a specific need to run legacy 32-bit software under a compatibility layer.
Not all software is open source or easy to convert to 64bit, it's something we need to live with.
Professional software and the gaming ecosystem is rife with this.
What applications have tried to work around this problem? FEX emulator (1) - Userspace x86 to AArch64 compatibility layer Tango binary translator (2) - AArch32 to AArch64 compatibility layer QEmu (3) - Not really but they do some userspace ioctl emulation
What problems did they hit?
FEX and Tango hit problems with emulating memory related syscalls.
- Emulating 32-bit mmap, mremap, shmat in userspace changes behaviour All three hit issues with ioctl emulation
- ioctls are free to do what they want including allocating memory and returning opaque structures with pointers.
With this patch we will be exposing the compatibility syscall table through the regular syscall svc API. There is prior art here where on x86-64 they also expose the compatibility tables.
The justification for this is that we need to maintain support for 32bit application compatibility going in to the foreseeable future. Userspace does almost all of the heavy lifting here, especially when the hardware no longer supports the 32bit use case.
A couple of caveats to this approach.
Userspace must know that this doesn't solve structure alignment problems for the x86->AArch64 (1) case.
The API for this changes from syscall number in x8 to x7 to match AArch32 syscall semantics
This is now exposing the compat syscalls to userspace, but for the sake of userspace compatibility it is a necessary evil.
Why does the upstream kernel care?
I believe every user wants to have their software ecosystem continue working if they are in a mixed AArch32/AArch64 world even when they are running AArch64 only hardware. The kernel should facilitate a good user experience.
External Resources
(1) https://github.com/FEX-Emu/FEX
(2) https://www.amanieusystems.com/
(3) https://www.qemu.org/
Further reading
- https://github.com/FEX-Emu/FEX/wiki/32Bit-x86-Woes
- Original patch: https://github.com/Amanieu/linux/commit/b4783002afb0
Changes in v2:
- Removed a tangential code path to make this more concise
- Now doesn't cover Tango's full use case
- This is purely for conciseness sake, easy enough to add back
- Cleaned up commit message
--- arch/arm64/Kconfig | 9 + arch/arm64/include/asm/compat.h | 20 +++ arch/arm64/include/asm/exception.h | 2 +- arch/arm64/include/asm/mmu.h | 7 + arch/arm64/include/asm/pgtable.h | 10 ++ arch/arm64/include/asm/processor.h | 6 +- arch/arm64/include/asm/thread_info.h | 7 + arch/arm64/kernel/asm-offsets.c | 3 + arch/arm64/kernel/entry-common.c | 9 +- arch/arm64/kernel/fpsimd.c | 2 +- arch/arm64/kernel/hw_breakpoint.c | 2 +- arch/arm64/kernel/perf_regs.c | 2 +- arch/arm64/kernel/process.c | 13 +- arch/arm64/kernel/ptrace.c | 6 +- arch/arm64/kernel/signal.c | 2 +- arch/arm64/kernel/syscall.c | 41 ++++- arch/arm64/mm/mmap.c | 249 +++++++++++++++++++++++++++ 17 files changed, 369 insertions(+), 21 deletions(-)
arch/arm64/Kconfig +9 -0
@@ -1147,6 +1147,15 @@ config XEN help Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64. +config ARM_COMPAT_DISPATCH + bool "32bit syscall dispatch table" + depends on COMPAT && ARM64 + default y + help + Kernel support for exposing the 32-bit syscall dispatch table to + userspace. + For dynamically translating 32-bit applications to a 64-bit process. + config FORCE_MAX_ZONEORDER int default "14" if (ARM64_64K_PAGES && TRANSPARENT_HUGEPAGE)
arch/arm64/include/asm/compat.h +20 -0
@@ -180,10 +180,30 @@ struct compat_shmid64_ds {static inline int is_compat_task (void ) { +#ifdef CONFIG_ARM_COMPAT_DISPATCH +/* It is compatible if Tango, 32bit compat, or 32bit thread */ +return current_thread_info ()->compat_syscall_flags !=0 ||test_thread_flag (TIF_32BIT ); +#else return test_thread_flag (TIF_32BIT ); +#endif }static inline int is_compat_thread (struct thread_info *thread) +{ +#ifdef CONFIG_ARM_COMPAT_DISPATCH +/* It is compatible if Tango, 32bit compat, or 32bit thread */ +return thread->compat_syscall_flags !=0 ||test_ti_thread_flag (thread,TIF_32BIT ); +#else +return test_ti_thread_flag (thread,TIF_32BIT ); +#endif +} + +static inline int is_aarch32_compat_task (void ) +{ +return test_thread_flag (TIF_32BIT ); +} + +static inline int is_aarch32_compat_thread (struct thread_info *thread) {return test_ti_thread_flag (thread,TIF_32BIT ); }
arch/arm64/include/asm/exception.h +1 -1
@@ -45,7 +45,7 @@ void do_sysinstr(unsigned int esr, struct pt_regs *regs);void do_sp_pc_abort (unsigned long addr,unsigned int esr,struct pt_regs *regs);void bad_el0_sync (struct pt_regs *regs,int reason,unsigned int esr);void do_cp15instr (unsigned int esr,struct pt_regs *regs); -void do_el0_svc (struct pt_regs *regs); +void do_el0_svc (struct pt_regs *regs,unsigned int iss);void do_el0_svc_compat (struct pt_regs *regs);void do_ptrauth_fault (struct pt_regs *regs,unsigned int esr);#endif /* __ASM_EXCEPTION_H */
arch/arm64/include/asm/mmu.h +7 -0
@@ -27,6 +27,9 @@ typedef struct {refcount_t pinned;void *vdso;unsigned long flags; +#ifdef CONFIG_ARM_COMPAT_DISPATCH +unsigned long compat_mmap_base; +#endif } mm_context_t;/*
@@ -79,6 +82,10 @@ extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot);extern void mark_linear_text_alias_ro (void );extern bool kaslr_requires_kpti (void ); +#ifdef CONFIG_ARM_COMPAT_DISPATCH +extern void process_init_compat_mmap (void ); +#endif +#define INIT_MM_CONTEXT (name) \ .pgd = init_pg_dir,
arch/arm64/include/asm/pgtable.h +10 -0
@@ -974,6 +974,16 @@ static inline bool arch_faults_on_old_pte(void) }#define arch_faults_on_old_pte arch_faults_on_old_pte +/* +* We provide our own arch_get_unmapped_area to handle 32-bit mmap calls from +* tango. +*/ +#ifdef CONFIG_ARM_COMPAT_DISPATCH +#define HAVE_ARCH_UNMAPPED_AREA +#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN +#define HAVE_ARCH_HUGETLB_UNMAPPED_AREA +#endif +#endif /* !__ASSEMBLY__ */ #endif /* __ASM_PGTABLE_H */
arch/arm64/include/asm/processor.h +3 -3
@@ -175,7 +175,7 @@ static inline void arch_thread_struct_whitelist(unsigned long *offset,#define task_user_tls (t) \ ({ \ unsigned long *__tls; \ - if (is_compat_thread(task_thread_info(t))) \ + if (is_aarch32_compat_thread(task_thread_info(t))) \ __tls = &(t)->thread.uw.tp2_value; \ else \ __tls = &(t)->thread.uw.tp_value; \
@@ -256,8 +256,8 @@ extern struct task_struct *cpu_switch_to(struct task_struct *prev,#define task_pt_regs (p) \ ((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1) -#define KSTK_EIP (tsk) ((unsigned long)task_pt_regs(tsk)->pc) -#define KSTK_ESP (tsk) user_stack_pointer(task_pt_regs(tsk)) +#define KSTK_EIP (tsk) ((unsigned long)task_pt_regs(tsk)->pc) +#define KSTK_ESP (tsk) user_stack_pointer(task_pt_regs(tsk))/* * Prefetching support
arch/arm64/include/asm/thread_info.h +7 -0
@@ -41,6 +41,9 @@ struct thread_info {#endif } preempt; }; +#ifdef CONFIG_ARM_COMPAT_DISPATCH +int compat_syscall_flags;/* 32-bit compat syscall */ +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base;void *scs_sp;
@@ -107,6 +110,10 @@ void arch_release_task_struct(struct task_struct *tsk); _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \ _TIF_SYSCALL_EMU) +#define TIF_COMPAT_32BITSYSCALL 0/* Trivial 32bit compatible syscall */ + +#define _TIF_COMPAT_32BITSYSCALL (1 << TIF_COMPAT_32BITSYSCALL) +#ifdef CONFIG_SHADOW_CALL_STACK #define INIT_SCS \ .scs_base = init_shadow_call_stack, \
arch/arm64/kernel/asm-offsets.c +3 -0
@@ -34,6 +34,9 @@ int main(void)#ifdef CONFIG_ARM64_SW_TTBR0_PAN DEFINE(TSK_TI_TTBR0, offsetof(struct task_struct, thread_info.ttbr0));#endif +#ifdef CONFIG_ARM_COMPAT_DISPATCH + DEFINE(TI_COMPAT_SYSCALL, offsetof(struct task_struct, thread_info.compat_syscall_flags)); +#endif #ifdef CONFIG_SHADOW_CALL_STACK DEFINE(TSK_TI_SCS_BASE, offsetof(struct task_struct, thread_info.scs_base)); DEFINE(TSK_TI_SCS_SP, offsetof(struct task_struct, thread_info.scs_sp));
arch/arm64/kernel/entry-common.c +6 -3
@@ -228,12 +228,12 @@ static void notrace el0_dbg(struct pt_regs *regs, unsigned long esr) }NOKPROBE_SYMBOL (el0_dbg); -static void notrace el0_svc(struct pt_regs *regs) +static void notrace el0_svc(struct pt_regs *regs, unsigned int iss) {if (system_uses_irq_prio_masking ())gic_write_pmr (GIC_PRIO_IRQON |GIC_PRIO_PSR_I_SET ); -do_el0_svc (regs); +do_el0_svc (regs, iss); }NOKPROBE_SYMBOL (el0_svc);
@@ -251,7 +251,10 @@ asmlinkage void notrace el0_sync_handler(struct pt_regs *regs)switch (ESR_ELx_EC (esr)) {case ESR_ELx_EC_SVC64: -el0_svc (regs); +/* Redundant masking here to show we are getting ISS mask +* Then we are pulling the imm16 out of it for SVC64 +*/ +el0_svc (regs, (esr & ESR_ELx_ISS_MASK) &0xffff );break ;case ESR_ELx_EC_DABT_LOW:el0_da (regs, esr);
arch/arm64/kernel/fpsimd.c +1 -1
@@ -937,7 +937,7 @@ void fpsimd_release_task(struct task_struct *dead_task)void do_sve_acc (unsigned int esr,struct pt_regs *regs) {/* Even if we chose not to use SVE, the hardware could still trap: */ -if (unlikely (!system_supports_sve ()) ||WARN_ON (is_compat_task ())) { +if (unlikely (!system_supports_sve ()) ||WARN_ON (is_aarch32_compat_task ())) {force_signal_inject (SIGILL ,ILL_ILLOPC , regs->pc,0 );return ; }
arch/arm64/kernel/hw_breakpoint.c +1 -1
@@ -168,7 +168,7 @@ static int is_compat_bp(struct perf_event *bp)* deprecated behaviour if we use unaligned watchpoints in * AArch64 state. */ -return tsk &&is_compat_thread (task_thread_info (tsk)); +return tsk &&is_aarch32_compat_thread (task_thread_info (tsk)); }/**
arch/arm64/kernel/perf_regs.c +1 -1
@@ -66,7 +66,7 @@ int perf_reg_validate(u64 mask)u64 perf_reg_abi (struct task_struct *task) { -if (is_compat_thread (task_thread_info (task))) +if (is_aarch32_compat_thread (task_thread_info (task)))return PERF_SAMPLE_REGS_ABI_32 ;else return PERF_SAMPLE_REGS_ABI_64 ;
arch/arm64/kernel/process.c +9 -4
@@ -314,7 +314,7 @@ static void tls_thread_flush(void) {write_sysreg (0 , tpidr_el0); -if (is_compat_task ()) { +if (is_aarch32_compat_task ()) { current->thread.uw.tp_value =0 ;/*
@@ -409,7 +409,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start, *task_user_tls (p) =read_sysreg (tpidr_el0);if (stack_start) { -if (is_compat_thread (task_thread_info (p))) +if (is_aarch32_compat_thread (task_thread_info (p))) childregs->compat_sp = stack_start;else childregs->sp = stack_start;
@@ -453,7 +453,7 @@ static void tls_thread_switch(struct task_struct *next) {tls_preserve_current_state (); -if (is_compat_thread (task_thread_info (next))) +if (is_aarch32_compat_thread (task_thread_info (next)))write_sysreg (next->thread.uw.tp_value, tpidrro_el0);else if (!arm64_kernel_unmapped_at_el0 ())write_sysreg (0 , tpidrro_el0);
@@ -619,7 +619,12 @@ unsigned long arch_align_stack(unsigned long sp)*/ void arch_setup_new_exec (void ) { - current->mm->context.flags =is_compat_task () ?MMCF_AARCH32 :0 ; +#ifdef CONFIG_ARM_COMPAT_DISPATCH +process_init_compat_mmap (); +current_thread_info ()->compat_syscall_flags =0 ; +#endif + + current->mm->context.flags =is_aarch32_compat_task () ?MMCF_AARCH32 :0 ;ptrauth_thread_init_user (current);
arch/arm64/kernel/ptrace.c +3 -3
@@ -175,7 +175,7 @@ static void ptrace_hbptriggered(struct perf_event *bp,const char *desc ="Hardware breakpoint trap (ptrace)" ;#ifdef CONFIG_COMPAT -if (is_compat_task ()) { +if (is_aarch32_compat_task ()) {int si_errno =0 ;int i;
@@ -1725,7 +1725,7 @@ const struct user_regset_view *task_user_regset_view(struct task_struct *task)*/ if (is_compat_task ())return &user_aarch32_view; -else if (is_compat_thread (task_thread_info (task))) +else if (is_aarch32_compat_thread (task_thread_info (task)))return &user_aarch32_ptrace_view;#endif return &user_aarch64_view;
@@ -1906,7 +1906,7 @@ int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)/* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */ user_regs_reset_single_step (regs, task); -if (is_compat_thread (task_thread_info (task))) +if (is_aarch32_compat_thread (task_thread_info (task)))return valid_compat_regs (regs);else return valid_native_regs (regs);
arch/arm64/kernel/signal.c +1 -1
@@ -813,7 +813,7 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)/* * Set up the stack frame */ -if (is_compat_task ()) { +if (is_aarch32_compat_task ()) {if (ksig->ka.sa.sa_flags &SA_SIGINFO ) ret =compat_setup_rt_frame (usig, ksig, oldset, regs);else
arch/arm64/kernel/syscall.c +38 -3
67 lines not on this page · raw message
arch/arm64/mm/mmap.c +249 -0
258 lines not on this page · raw message
Signature · 1 line
2.27.0