Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys Make copystr() a MI C function, part of libkern and ...



details:   https://anonhg.NetBSD.org/src/rev/f3257f910b75
branches:  trunk
changeset: 935337:f3257f910b75
user:      maxv <maxv%NetBSD.org@localhost>
date:      Tue Jun 30 16:20:00 2020 +0000

description:
Make copystr() a MI C function, part of libkern and shared on all
architectures.

Notes:

 - On alpha and ia64 the function is kept but gets renamed locally to avoid
   symbol collision. This is because on these two arches, I am not sure
   whether the ASM callers do not rely on fixed registers, so I prefer to
   keep the ASM body for now.
 - On Vax, only the symbol is removed, because the body is used from other
   functions.
 - On RISC-V, this change fixes a bug: copystr() was just a wrapper around
   strlcpy(), but strlcpy() makes the operation less safe (strlen on the
   source beyond its size).
 - The kASan, kCSan and kMSan wrappers are removed, because now that
   copystr() is in C, the compiler transformations are applied to it,
   without the need for manual wrappers.

Could test on amd64 only, but should be fine.

diffstat:

 sys/arch/aarch64/aarch64/copyinout.S |  32 +----------------
 sys/arch/alpha/alpha/locore.s        |  17 +++-----
 sys/arch/amd64/amd64/copy.S          |  32 +-----------------
 sys/arch/arm/arm/copystr.S           |  36 +-------------------
 sys/arch/hppa/hppa/copy.S            |   5 +-
 sys/arch/i386/i386/copy.S            |  48 +-------------------------
 sys/arch/ia64/ia64/support.S         |  15 +++-----
 sys/arch/m68k/m68k/copy.s            |  32 +-----------------
 sys/arch/mips/mips/copy.S            |  30 +----------------
 sys/arch/powerpc/conf/files.powerpc  |   3 +-
 sys/arch/powerpc/powerpc/copystr.c   |  64 ------------------------------------
 sys/arch/riscv/riscv/trap.c          |  18 +---------
 sys/arch/sh3/sh3/locore_c.c          |  32 +----------------
 sys/arch/sparc/sparc/locore.s        |  37 +--------------------
 sys/arch/sparc64/sparc64/copy.S      |  48 +--------------------------
 sys/arch/usermode/usermode/copy.c    |  14 +------
 sys/arch/vax/vax/subr.S              |   3 +-
 sys/kern/subr_asan.c                 |  14 +------
 sys/kern/subr_csan.c                 |  14 +------
 sys/kern/subr_msan.c                 |  27 +--------------
 sys/lib/libkern/Makefile.libkern     |   3 +-
 sys/lib/libkern/copystr.c            |  54 ++++++++++++++++++++++++++++++
 sys/sys/systm.h                      |  10 +----
 23 files changed, 97 insertions(+), 491 deletions(-)

diffs (truncated from 1073 to 300 lines):

diff -r 136718bb8bb8 -r f3257f910b75 sys/arch/aarch64/aarch64/copyinout.S
--- a/sys/arch/aarch64/aarch64/copyinout.S      Tue Jun 30 16:09:40 2020 +0000
+++ b/sys/arch/aarch64/aarch64/copyinout.S      Tue Jun 30 16:20:00 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: copyinout.S,v 1.9 2018/09/14 13:47:14 ryo Exp $ */
+/* $NetBSD: copyinout.S,v 1.10 2020/06/30 16:20:00 maxv Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #include <aarch64/asm.h>
 #include "assym.h"
 
-RCSID("$NetBSD: copyinout.S,v 1.9 2018/09/14 13:47:14 ryo Exp $");
+RCSID("$NetBSD: copyinout.S,v 1.10 2020/06/30 16:20:00 maxv Exp $");
 
        .macro enter_cpu_onfault
        stp     fp, lr, [sp, #-16]!     /* save fp, lr */
@@ -314,34 +314,6 @@
 END(copyoutstr)
 
 
-/* LINTSTUB: int copystr(const void *s, void *d, size_t len, size_t *done); */
-
-ENTRY(copystr)
-       enter_cpu_onfault
-
-       mov     x8, #0                  /* error = 0 */
-
-       mov     x4, xzr                 /* i = 0 */
-       cbz     x2, copystr_done        /* if (len == 0) goto done */
-copystr_loop:
-       ldrb    w5, [x0], #1            /* ch = src[i] */
-       strb    w5, [x1], #1            /* dst[i] = ch */
-       add     x4, x4, #1              /* i++ */
-       cbz     x5, copystr_done        /* if (ch == '\0') goto done */
-
-       cmp     x4, x2                  /* if (i < len) goto loop */
-       bcc     copystr_loop
-       mov     x8, #ENAMETOOLONG       /* error = ENAMETOOLONG */
-
-copystr_done:
-       cbz     x3, 1f                  /* if (done != NULL) *done = i */
-       str     x4, [x3]
-1:
-       exit_cpu_onfault
-       ret
-END(copystr)
-
-
 /* LINTSTUB: int kcopy(const void *src, void *dst, size_t len); */
 
 ENTRY(kcopy)
diff -r 136718bb8bb8 -r f3257f910b75 sys/arch/alpha/alpha/locore.s
--- a/sys/arch/alpha/alpha/locore.s     Tue Jun 30 16:09:40 2020 +0000
+++ b/sys/arch/alpha/alpha/locore.s     Tue Jun 30 16:20:00 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: locore.s,v 1.124 2020/01/08 20:59:18 skrll Exp $ */
+/* $NetBSD: locore.s,v 1.125 2020/06/30 16:20:00 maxv Exp $ */
 
 /*-
  * Copyright (c) 1999, 2000, 2019 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
 
 #include <machine/asm.h>
 
-__KERNEL_RCSID(0, "$NetBSD: locore.s,v 1.124 2020/01/08 20:59:18 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.s,v 1.125 2020/06/30 16:20:00 maxv Exp $");
 
 #include "assym.h"
 
@@ -744,12 +744,9 @@
 /**************************************************************************/
 
 /*
- * Copy a null-terminated string within the kernel's address space.
- * If lenp is not NULL, store the number of chars copied in *lenp
- *
- * int copystr(char *from, char *to, size_t len, size_t *lenp);
+ * XXX XXX XXX: Should be removed?
  */
-LEAF(copystr, 4)
+LEAF(alpha_copystr, 4)
        LDGP(pv)
 
        mov     a2, t0                  /* t0 = i = len */
@@ -781,7 +778,7 @@
 
 4:     mov     zero, v0                /* return 0. */
        RET
-       END(copystr)
+       END(alpha_copystr)
 
 NESTED(copyinstr, 4, 16, ra, IM_RA|IM_S0, 0)
        LDGP(pv)
@@ -800,7 +797,7 @@
        ldq     at_reg, L_PCB(at_reg)
        stq     v0, PCB_ONFAULT(at_reg)
        .set at
-       CALL(copystr)                           /* do the copy.              */
+       CALL(alpha_copystr)                     /* do the copy.              */
        .set noat
        ldq     at_reg, 0(s0)                   /* kill the fault handler.   */
        ldq     at_reg, L_PCB(at_reg)
@@ -829,7 +826,7 @@
        ldq     at_reg, L_PCB(at_reg)
        stq     v0, PCB_ONFAULT(at_reg)
        .set at
-       CALL(copystr)                           /* do the copy.              */
+       CALL(alpha_copystr)                     /* do the copy.              */
        .set noat
        ldq     at_reg, 0(s0)                   /* kill the fault handler.   */
        ldq     at_reg, L_PCB(at_reg)
diff -r 136718bb8bb8 -r f3257f910b75 sys/arch/amd64/amd64/copy.S
--- a/sys/arch/amd64/amd64/copy.S       Tue Jun 30 16:09:40 2020 +0000
+++ b/sys/arch/amd64/amd64/copy.S       Tue Jun 30 16:20:00 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: copy.S,v 1.33 2019/05/04 08:50:39 maxv Exp $   */
+/*     $NetBSD: copy.S,v 1.34 2020/06/30 16:20:00 maxv Exp $   */
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -355,36 +355,6 @@
 8:     ret
 END(copystr_fault)
 
-ENTRY(copystr)
-       xchgq   %rdi,%rsi
-       movq    %rdx,%r8
-
-       incq    %rdx
-
-1:     decq    %rdx
-       jz      4f
-       lodsb
-       stosb
-       testb   %al,%al
-       jnz     1b
-
-       /* Success -- 0 byte reached. */
-       decq    %rdx
-       xorl    %eax,%eax
-       jmp     6f
-
-4:     /* rdx is zero -- return ENAMETOOLONG. */
-       movl    $ENAMETOOLONG,%eax
-
-6:     /* Set *lencopied and return %eax. */
-       testq   %rcx,%rcx
-       jz      7f
-       subq    %rdx,%r8
-       movq    %r8,(%rcx)
-
-7:     ret
-END(copystr)
-
 /**************************************************************************/
 
 #define        UFETCHSTORE_PROLOGUE(x)                                         \
diff -r 136718bb8bb8 -r f3257f910b75 sys/arch/arm/arm/copystr.S
--- a/sys/arch/arm/arm/copystr.S        Tue Jun 30 16:09:40 2020 +0000
+++ b/sys/arch/arm/arm/copystr.S        Tue Jun 30 16:20:00 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: copystr.S,v 1.12 2018/01/24 09:04:44 skrll Exp $       */
+/*     $NetBSD: copystr.S,v 1.13 2020/06/30 16:20:00 maxv Exp $        */
 
 /*
  * Copyright (c) 1995 Mark Brinicombe.
@@ -47,44 +47,12 @@
 
 #include <arm/locore.h>
 
-RCSID("$NetBSD: copystr.S,v 1.12 2018/01/24 09:04:44 skrll Exp $")
+RCSID("$NetBSD: copystr.S,v 1.13 2020/06/30 16:20:00 maxv Exp $")
 
 #include <sys/errno.h>
 
        .text
        .align  0
-/*
- * r0 - from
- * r1 - to
- * r2 - maxlens
- * r3 - lencopied
- *
- * Copy string from r0 to r1
- */
-ENTRY(copystr)
-       push    {r4-r5}                 /* stack is 8 byte aligned */
-       teq     r2, #0x00000000
-       mov     r5, #0x00000000
-       moveq   r0, #ENAMETOOLONG
-       beq     2f
-
-1:     ldrb    r4, [r0], #0x0001
-       add     r5, r5, #0x00000001
-       teq     r4, #0x00000000
-       strb    r4, [r1], #0x0001
-       teqne   r5, r2
-       bne     1b
-
-       teq     r4, #0x00000000
-       moveq   r0, #0x00000000
-       movne   r0, #ENAMETOOLONG
-
-2:     teq     r3, #0x00000000
-       strne   r5, [r3]
-
-       pop     {r4-r5}                 /* stack is 8 byte aligned */
-       RET
-END(copystr)
 
 #define SAVE_REGS      push    {r3-r6}
 #define RESTORE_REGS   pop     {r3-r6}
diff -r 136718bb8bb8 -r f3257f910b75 sys/arch/hppa/hppa/copy.S
--- a/sys/arch/hppa/hppa/copy.S Tue Jun 30 16:09:40 2020 +0000
+++ b/sys/arch/hppa/hppa/copy.S Tue Jun 30 16:20:00 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: copy.S,v 1.24 2019/04/15 20:45:08 skrll Exp $  */
+/*     $NetBSD: copy.S,v 1.25 2020/06/30 16:20:01 maxv Exp $   */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -256,9 +256,8 @@
 
 /*
  * int kcopy(const void *src, void *dst, size_t len);
- * int copystr(const void *src, void *dst, size_t size, size_t *lenp);
  */
-SPCOPY(kcopy, copystr,
+_SPCOPY(kcopy, CALL_SPCOPY,
        SPACE_KERNEL(%arg0) ! SPACE_KERNEL(%arg2))
 
 /*
diff -r 136718bb8bb8 -r f3257f910b75 sys/arch/i386/i386/copy.S
--- a/sys/arch/i386/i386/copy.S Tue Jun 30 16:09:40 2020 +0000
+++ b/sys/arch/i386/i386/copy.S Tue Jun 30 16:20:00 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: copy.S,v 1.31 2019/05/04 08:50:39 maxv Exp $   */
+/*     $NetBSD: copy.S,v 1.32 2020/06/30 16:20:01 maxv Exp $   */
 
 /*
  * Copyright (c) 1998, 2000, 2004, 2008 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include <machine/asm.h>
-__KERNEL_RCSID(0, "$NetBSD: copy.S,v 1.31 2019/05/04 08:50:39 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: copy.S,v 1.32 2020/06/30 16:20:01 maxv Exp $");
 
 #include "assym.h"
 
@@ -455,50 +455,6 @@
        ret
 END(copystr_fault)
 
-/*
- * int copystr(const void *from, void *to, size_t maxlen, size_t *lencopied);
- * Copy a NUL-terminated string, at most maxlen characters long.  Return the
- * number of characters copied (including the NUL) in *lencopied.  If the
- * string is too long, return ENAMETOOLONG; else return 0.
- * see copystr(9)
- */
-ENTRY(copystr)
-       pushl   %esi
-       pushl   %edi
-
-       movl    12(%esp),%esi           /* esi = from */
-       movl    16(%esp),%edi           /* edi = to */
-       movl    20(%esp),%edx           /* edx = maxlen */
-       incl    %edx
-
-1:     decl    %edx
-       jz      4f
-       lodsb
-       stosb
-       testb   %al,%al
-       jnz     1b
-
-       /* Success -- 0 byte reached. */
-       decl    %edx
-       xorl    %eax,%eax
-       jmp     6f
-
-4:     /* edx is zero -- return ENAMETOOLONG. */
-       movl    $ENAMETOOLONG,%eax
-
-6:     /* Set *lencopied and return %eax. */
-       movl    20(%esp),%ecx
-       subl    %edx,%ecx
-       movl    24(%esp),%edx
-       testl   %edx,%edx
-       jz      7f
-       movl    %ecx,(%edx)
-
-7:     popl    %edi
-       popl    %esi
-       ret
-END(copystr)
-



Home | Main Index | Thread Index | Old Index