Re: [PATCH v2] x32: Implement prctl in assembly

"H.J. Lu" <[email protected]> Mon, 8 Dec 2025 17:01:14 +0800
Newsgroups org.kernel.vger.util-linux
Message-ID <CAMe9rOq6mx_ZQs_z2QazC2pbvJZDZzD7kLCo=_o9eTj3cUh8nA@mail.gmail.com>
On Mon, Dec 8, 2025 at 4:11 PM Florian Weimer <[email protected]> wrote:
>
> * H. J. Lu:
> > Here is the v2 patch to implement prctl in assembly for x32.
> >
> > Since the variadic prctl function takes at most 5 integer arguments which
> > are passed in the same integer registers on x32 as the function with 5
> > integer arguments, we can use assembly for prctl.  Since upper 32-bits in
> > the last 4 arguments of prctl must be cleared to match the x32 prctl
> > syscall interface where the last 4 arguments are unsigned 64 bit longs,
> > implement prctl in assembly to clear upper 32-bits in the last 4 arguments
> > and add a test to verify it.
>
> What's the advantage of the assembler implementation over the C
> implementation?  I'm missing the context for this change.
>

It is inspired by

commit 6a04404521ac4119ae36827eeb288ea84eee7cf6
Author: Florian Weimer <[email protected]>
Date:   Sat Feb 17 09:17:04 2024 +0100

    Linux: Switch back to assembly syscall wrapper for prctl (bug 29770)

The difference is

00000000 <__GI___prctl>:
   0: f3 0f 1e fa          endbr64
   4: 8d 44 24 08          lea    0x8(%rsp),%eax
   8: 48 89 74 24 d0        mov    %rsi,-0x30(%rsp)
   d: 48 63 ff              movslq %edi,%rdi
  10: 8b 74 24 d0          mov    -0x30(%rsp),%esi
  14: 89 44 24 c0          mov    %eax,-0x40(%rsp)
  18: 8d 44 24 c8          lea    -0x38(%rsp),%eax
  1c: 48 89 54 24 d8        mov    %rdx,-0x28(%rsp)
  21: 8b 54 24 d8          mov    -0x28(%rsp),%edx
  25: 48 89 4c 24 e0        mov    %rcx,-0x20(%rsp)
  2a: 44 8b 54 24 e0        mov    -0x20(%rsp),%r10d
  2f: 4c 89 44 24 e8        mov    %r8,-0x18(%rsp)
  34: 44 8b 44 24 e8        mov    -0x18(%rsp),%r8d
  39: 89 44 24 c4          mov    %eax,-0x3c(%rsp)
  3d: b8 9d 00 00 40        mov    $0x4000009d,%eax
  42: c7 44 24 b8 08 00 00 00 movl   $0x8,-0x48(%rsp)
  4a: 0f 05                syscall
  4c: 3d 00 f0 ff ff        cmp    $0xfffff000,%eax
  51: 77 05                ja     58 <__GI___prctl+0x58>
  53: c3                    ret
  54: 0f 1f 40 00          nopl   0x0(%rax)
  58: f7 d8                neg    %eax
  5a: 64 8b 14 25 00 00 00 00 mov    %fs:0x0,%edx
  62: 40 03 15 00 00 00 00 rex add 0x0(%rip),%edx        # 69
<__GI___prctl+0x69>
  69: 67 89 02              mov    %eax,(%edx)
  6c: b8 ff ff ff ff        mov    $0xffffffff,%eax
  71: c3                    ret

vs

00000000 <__GI___prctl>:
   0: 89 f6                mov    %esi,%esi
   2: 89 d2                mov    %edx,%edx
   4: 41 89 ca              mov    %ecx,%r10d
   7: 45 89 c0              mov    %r8d,%r8d
   a: b8 9d 00 00 40        mov    $0x4000009d,%eax
   f: 0f 05                syscall
  11: 48 3d 01 f0 ff ff    cmp    $0xfffffffffffff001,%rax
  17: 73 01                jae    1a <__GI___prctl+0x1a>
  19: c3                    ret
  1a: 48 8b 0d 00 00 00 00 mov    0x0(%rip),%rcx        # 21 <__GI___prctl+0x21>
  21: f7 d8                neg    %eax
  23: 64 89 01              mov    %eax,%fs:(%rcx)
  26: 83 c8 ff              or     $0xffffffff,%eax
  29: c3                    ret


-- 
H.J.