When the syscall number has to be loaded from a pc-relative location,
authormiod <miod@openbsd.org>
Wed, 27 Mar 2024 20:03:29 +0000 (20:03 +0000)
committermiod <miod@openbsd.org>
Wed, 27 Mar 2024 20:03:29 +0000 (20:03 +0000)
abuse END macros to place the number at the end of the syscall wrapper
rather than in the middle of it, so that there is no need to branch
around it; this saves two instructions per syscall number >= 128.

While there, also tweak the error return (SET_ERRNO_AND_RETURN) to only
return a 64-bit value for lseek; this saves another instruction for
all other syscalls.

With input from guenther@; "Anything that makes the machine faster" deraadt@

lib/libc/arch/sh/SYS.h
lib/libc/arch/sh/sys/brk.S
lib/libc/arch/sh/sys/sbrk.S
lib/libc/arch/sh/sys/sigprocmask.S
lib/libc/arch/sh/sys/sigsuspend.S
lib/libc/arch/sh/sys/tfork_thread.S

index cb3fd7e..684ac52 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: SYS.h,v 1.15 2023/12/11 22:24:16 kettenis Exp $       */
+/*     $OpenBSD: SYS.h,v 1.16 2024/03/27 20:03:29 miod Exp $   */
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
  * All rights reserved.
 #define        SYSENTRY_HIDDEN(x)                              \
        ENTRY(_thread_sys_ ## x)
 
-#define        __END_HIDDEN(x)                                 \
-       SET_ENTRY_SIZE(_thread_sys_ ## x);              \
-       _HIDDEN_FALIAS(x,_thread_sys_ ## x);            \
-       SET_ENTRY_SIZE(_HIDDEN(x))
-#define        __END(x)                                        \
-       __END_HIDDEN(x); SET_ENTRY_SIZE(x)
-
 #define PINSYSCALL(sysno, label)                                       \
        .pushsection .openbsd.syscalls,"",@progbits;                    \
        .p2align 2;                                                     \
@@ -95,7 +88,8 @@
 #ifdef __ASSEMBLER__
 /*
  * If the system call number fits in a 8-bit signed value (i.e. fits in 7 bits),
- * then we can use the #imm8 addressing mode.
+ * then we can use the #imm8 addressing mode. Otherwise, we'll load the number
+ * from memory at the end of the system call wrapper.
  */
 
 .macro systrap num
        mov.l   903f, r0
 97:    trapa   #0x80
        PINSYSCALL(\num, 97b)
-       bra     904f
-        nop
+.endif
+.endm
+
+.macro systrap_data num
+.iflt \num - 128
+.else
+       .text
        .align  2
  903:  .long   \num
- 904:
 .endif
 .endm
+
+.macro syscall_error num
+.ifeq \num - SYS_lseek
+       mov     #-1, r1
+.endif
+       rts
+        mov    #-1, r0
+.endm
+
 #endif
 
 #define SYSTRAP(x)                                     \
                SYSENTRY_HIDDEN(x);                     \
                SYSTRAP(y)
 
-#define SET_ERRNO_AND_RETURN                           \
+#define SET_ERRNO_AND_RETURN(y)                                \
                stc     gbr,r1;                         \
                mov     #TCB_OFFSET_ERRNO_NEG,r2;       \
                sub     r2,r1;                          \
                mov.l   r0,@r1;                         \
-               mov     #-1, r1;        /* for lseek */ \
-               rts;                                    \
-                mov    #-1, r0
+               syscall_error SYS_ ## y
 
 #define _SYSCALL(x,y)                                  \
                .text;                                  \
-       911:    SET_ERRNO_AND_RETURN;                   \
+       911:    SET_ERRNO_AND_RETURN(y);                \
                _SYSCALL_NOERROR(x,y);                  \
                bf      911b
 #define _SYSCALL_HIDDEN(x,y)                           \
                .text;                                  \
-       911:    SET_ERRNO_AND_RETURN;                   \
+       911:    SET_ERRNO_AND_RETURN(y);                \
                _SYSCALL_HIDDEN_NOERROR(x,y);           \
                bf      911b
 
+#define        __END_HIDDEN(x,y)                               \
+               systrap_data SYS_ ## y;                 \
+               SET_ENTRY_SIZE(_thread_sys_ ## x);      \
+               _HIDDEN_FALIAS(x,_thread_sys_ ## x);    \
+               SET_ENTRY_SIZE(_HIDDEN(x))
+#define        __END(x,y)                                      \
+               __END_HIDDEN(x,y); SET_ENTRY_SIZE(x)
+
 #define SYSCALL_NOERROR(x)                             \
                _SYSCALL_NOERROR(x,x)
 
                _SYSCALL_NOERROR(x,y);                  \
                rts;                                    \
                 nop;                                   \
-               __END(x)
+               __END(x,y)
 
 #define PSEUDO(x,y)                                    \
                _SYSCALL(x,y);                          \
                rts;                                    \
                 nop;                                   \
-               __END(x)
+               __END(x,y)
 
 #define PSEUDO_HIDDEN(x,y)                             \
                _SYSCALL_HIDDEN(x,y);                   \
                rts;                                    \
                 nop;                                   \
-               __END_HIDDEN(x)
+               __END_HIDDEN(x,y)
 
 #define RSYSCALL_NOERROR(x)            PSEUDO_NOERROR(x,x)
 #define RSYSCALL(x)                    PSEUDO(x,x)
 #define RSYSCALL_HIDDEN(x)             PSEUDO_HIDDEN(x,x)
-#define SYSCALL_END(x)                 __END(x)
-#define SYSCALL_END_HIDDEN(x)          __END_HIDDEN(x)
+#define SYSCALL_END(x)                 __END(x,x)
+#define SYSCALL_END_HIDDEN(x)          __END_HIDDEN(x,x)
index 5a98da3..5701f48 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: brk.S,v 1.9 2023/12/10 16:45:52 deraadt Exp $ */
+/*     $OpenBSD: brk.S,v 1.10 2024/03/27 20:03:29 miod Exp $   */
 /*     $NetBSD: brk.S,v 1.10 2006/01/06 03:58:31 uwe Exp $     */
 
 /*-
@@ -64,13 +64,7 @@ ENTRY(brk)
        bf      1f
        mov     r0, r4
 1:
-#if SYS_break >= 128
-       mov.l   LSYS_break, r0
-#else
-       mov     #SYS_break, r0
-#endif
-99:    trapa   #0x80
-       PINSYSCALL(SYS_break, 99b)
+       systrap SYS_break
        bf      2f
 #ifdef __PIC__
        mov.l   Lcurbrk, r0
@@ -83,12 +77,10 @@ ENTRY(brk)
        rts
         mov.l  r4, @r1
 2:
-       SET_ERRNO_AND_RETURN
+       SET_ERRNO_AND_RETURN(break)
 
        .align  2
-#if SYS_break >= 128
-LSYS_break:    .long   SYS_break
-#endif
+       systrap_data SYS_break
 #ifdef __PIC__
 L_GOT:         .long   _GLOBAL_OFFSET_TABLE_
 Lminbrk:       .long   __minbrk@GOT
index b86d0eb..a250c8d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: sbrk.S,v 1.9 2023/12/10 16:45:52 deraadt Exp $        */
+/*     $OpenBSD: sbrk.S,v 1.10 2024/03/27 20:03:29 miod Exp $  */
 /*     $NetBSD: sbrk.S,v 1.9 2006/01/06 03:58:31 uwe Exp $     */
 
 /*-
@@ -64,13 +64,7 @@ ENTRY(sbrk)
 #endif
        mov.l   @r0, r0
        add     r0, r4
-#if SYS_break >= 128
-       mov.l   LSYS_break, r0
-#else
-       mov     #SYS_break, r0
-#endif
-99:    trapa   #0x80
-       PINSYSCALL(SYS_break, 99b)
+       systrap SYS_break
        bf      1f
 #ifdef __PIC__
        mov.l   Lcurbrk, r0
@@ -83,12 +77,10 @@ ENTRY(sbrk)
        rts
         mov.l  r2, @r1
 1:
-       SET_ERRNO_AND_RETURN
+       SET_ERRNO_AND_RETURN(break)
 
        .align  2
-#if SYS_break >= 128
-LSYS_break:    .long   SYS_break
-#endif
+       systrap_data SYS_break
 #ifdef __PIC__
 L_GOT:         .long   _GLOBAL_OFFSET_TABLE_
 Lcurbrk:       .long   __curbrk@GOT
index 28841e6..34421e4 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: sigprocmask.S,v 1.7 2023/12/10 16:45:52 deraadt Exp $ */
+/*     $OpenBSD: sigprocmask.S,v 1.8 2024/03/27 20:03:29 miod Exp $    */
 /*     $NetBSD: sigprocmask.S,v 1.6 2003/08/07 16:42:21 agc Exp $      */
 
 /*-
@@ -47,13 +47,7 @@ SYSENTRY_HIDDEN(sigprocmask)
 1:     mov.l   @r2, r2                 /* fetch indirect ... */
        mov     r2, r5                  /* to new mask arg */
 2:     
-#if SYS_sigprocmask >= 128
-       mov.l   LSYS_sigprocmask, r0
-#else
-       mov     #SYS_sigprocmask, r0
-#endif
-99:    trapa   #0x80
-       PINSYSCALL(SYS_sigprocmask, 99b)
+       systrap SYS_sigprocmask
        bf      4f
        mov     r6, r2                  /* fetch old mask requested */
        tst     r2, r2                  /* test if old mask requested */
@@ -64,11 +58,6 @@ SYSENTRY_HIDDEN(sigprocmask)
        rts
         nop
 4:
-       SET_ERRNO_AND_RETURN
-
-       .align  2
-#if SYS_sigprocmask >= 128
-LSYS_sigprocmask:
-       .long   SYS_sigprocmask
-#endif
+       SET_ERRNO_AND_RETURN(sigprocmask)
+       systrap_data(SYS_sigprocmask)
 SYSCALL_END_HIDDEN(sigprocmask)
index b2e2b25..6e2f6ad 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: sigsuspend.S,v 1.6 2023/12/10 16:45:52 deraadt Exp $  */
+/*     $OpenBSD: sigsuspend.S,v 1.7 2024/03/27 20:03:29 miod Exp $     */
 /*     $NetBSD: sigsuspend.S,v 1.5 2003/08/07 16:42:21 agc Exp $       */
 
 /*-
@@ -41,18 +41,7 @@ SYSENTRY_HIDDEN(sigsuspend)
        mov     r4, r0          /* fetch mask arg */
        mov.l   @r0, r0         /* indirect to mask arg */
        mov     r0, r4
-#if SYS_sigsuspend >= 128
-       mov.l   LSYS_sigsuspend, r0
-#else
-       mov     #SYS_sigsuspend, r0
-#endif
-99:    trapa   #0x80
-       PINSYSCALL(SYS_sigsuspend, 99b)
-       SET_ERRNO_AND_RETURN
-
-       .align  2
-#if SYS_sigsuspend >= 128
-LSYS_sigsuspend:
-       .long   SYS_sigsuspend
-#endif
+       systrap SYS_sigsuspend
+       SET_ERRNO_AND_RETURN(sigsuspend)
+       systrap_data SYS_sigsuspend
 SYSCALL_END_HIDDEN(sigsuspend)
index b17f2cf..fb2d9e4 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: tfork_thread.S,v 1.5 2023/12/10 16:45:52 deraadt Exp $        */
+/*     $OpenBSD: tfork_thread.S,v 1.6 2024/03/27 20:03:29 miod Exp $   */
 
 /*
  * Copyright (c) 2007 Miodrag Vallat.
  *                     r4                      r5                 r6           r7
  */
 ENTRY(__tfork_thread)
-#if SYS___tfork >= 128
-       mov.l   .LSYS___tfork, r0
-#else
-       mov     #SYS___tfork, r0
-#endif
-99:    trapa   #0x80
-       PINSYSCALL(SYS___tfork, 99b)
+       systrap SYS___tfork
        bf      9f
 
        tst     r0, r0
@@ -42,6 +36,8 @@ ENTRY(__tfork_thread)
        rts
         nop
 
+       systrap_data SYS___tfork
+
 1:
        /*
         * In child process: invoke function, then exit.
@@ -49,26 +45,13 @@ ENTRY(__tfork_thread)
        jsr     @r6
         mov    r7, r4
 
-#if SYS___threxit >= 128
-       mov.l   .LSYS___threxit, r0
-#else
-       mov     #SYS___threxit, r0
-#endif
-98:    trapa   #0x80
-       PINSYSCALL(SYS___threxit, 98b)
+       systrap SYS___threxit
+       systrap_data SYS___threxit
 
 9:
        /*
         * System call failure.
         */
-       SET_ERRNO_AND_RETURN
-
-       .align 2
-#if SYS___tfork >= 128
-.LSYS___tfork: .long SYS___tfork
-#endif
-#if SYS___threxit >= 128
-.LSYS___threxit:       .long SYS___threxit
-#endif
+       SET_ERRNO_AND_RETURN(__tfork)
 
        SET_ENTRY_SIZE(__tfork_thread)