[0/3] x32: Properly pass long to syscall [BZ #25810]

Message ID 20200413142515.16619-1-hjl.tools@gmail.com
Headers
Series x32: Properly pass long to syscall [BZ #25810] |

Message

H.J. Lu April 13, 2020, 2:25 p.m. UTC
  X32 has 32-bit long and pointer with 64-bit off_t.  Since x32 psABI
requires that pointers passed in registers must be zero-extended to
64bit, x32 can share many syscall interfaces with LP64.  When a LP64
syscall with long and unsigned long arguments is used for x32, these
arguments must be properly extended to 64-bit.  Otherwise if the upper
32 bits of the register have undefined value, such a syscall will be
rejected by kernel.

For syscalls implemented in assembly codes, 'U' is added to syscall
signature key letters for unsigned long.  SYSCALL_ULONG_ARG_1 and
SYSCALL_ULONG_ARG_2 are passed to syscall-template.S for the first
and the second unsigned long arguments if PSEUDOS_HAVE_4_ARGS is
defined.  They are used by x32 to zero-extend 32-bit arguments to
64 bits.

For x32 INLINE_SYSCALLs, cast

1. pointer to unsigned long (32 bit).
2. 32-bit unsigned integer to unsigned long long (64 bit).
3. 32-bit integer to long long (64 bit).

For

       void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);

we now generate

   0:	41 f7 c1 ff 0f 00 00 	test   $0xfff,%r9d
   7:	75 1f                	jne    28 <__mmap64+0x28>
   9:	48 63 d2             	movslq %edx,%rdx
   c:	89 f6                	mov    %esi,%esi
   e:	4d 63 c0             	movslq %r8d,%r8
  11:	4c 63 d1             	movslq %ecx,%r10
  14:	b8 09 00 00 40       	mov    $0x40000009,%eax
  19:	0f 05                	syscall

That is

1. addr is unchanged.
2. length is zero-extend to 64 bits.
3. prot is sign-extend to 64 bits.
4. flags is sign-extend to 64 bits.
5. fd is sign-extend to 64 bits.
6. offset is unchanged.

For int arguments, since kernel uses only the lower 32 bits and ignores
the upper 32 bits in 64-bit registers, these work correctly.

Tested on i386, x86-64 and x32 as well as with build-many-glibcs.py.

H.J. Lu (3):
  Add SYSCALL_ULONG_ARG_[12] to pass long to syscall [BZ #25810]
  x32: Properly pass long to syscall [BZ #25810]
  Add a syscall test for [BZ #25810]

 misc/Makefile                               |   2 +-
 misc/tst-syscalls.c                         | 146 ++++++++++++++++++++
 sysdeps/unix/make-syscalls.sh               |  88 ++++++++++++
 sysdeps/unix/syscall-template.S             |  43 +++++-
 sysdeps/unix/syscalls.list                  |   6 +-
 sysdeps/unix/sysv/linux/syscalls.list       |  14 +-
 sysdeps/unix/sysv/linux/x86_64/sysdep.h     |  80 ++++++++---
 sysdeps/unix/sysv/linux/x86_64/x32/sysdep.h |  38 +++++
 8 files changed, 385 insertions(+), 32 deletions(-)
 create mode 100644 misc/tst-syscalls.c