rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * String handling functions. |
| 4 | * |
| 5 | * Copyright IBM Corp. 2012 |
| 6 | */ |
| 7 | |
| 8 | #include <linux/linkage.h> |
| 9 | #include <asm/export.h> |
| 10 | #include <asm/nospec-insn.h> |
| 11 | |
| 12 | GEN_BR_THUNK %r14 |
| 13 | |
| 14 | /* |
| 15 | * void *memmove(void *dest, const void *src, size_t n) |
| 16 | */ |
| 17 | ENTRY(memmove) |
| 18 | ltgr %r4,%r4 |
| 19 | lgr %r1,%r2 |
| 20 | jz .Lmemmove_exit |
| 21 | aghi %r4,-1 |
| 22 | clgr %r2,%r3 |
| 23 | jnh .Lmemmove_forward |
| 24 | la %r5,1(%r4,%r3) |
| 25 | clgr %r2,%r5 |
| 26 | jl .Lmemmove_reverse |
| 27 | .Lmemmove_forward: |
| 28 | srlg %r0,%r4,8 |
| 29 | ltgr %r0,%r0 |
| 30 | jz .Lmemmove_forward_remainder |
| 31 | .Lmemmove_forward_loop: |
| 32 | mvc 0(256,%r1),0(%r3) |
| 33 | la %r1,256(%r1) |
| 34 | la %r3,256(%r3) |
| 35 | brctg %r0,.Lmemmove_forward_loop |
| 36 | .Lmemmove_forward_remainder: |
| 37 | larl %r5,.Lmemmove_mvc |
| 38 | ex %r4,0(%r5) |
| 39 | .Lmemmove_exit: |
| 40 | BR_EX %r14 |
| 41 | .Lmemmove_reverse: |
| 42 | ic %r0,0(%r4,%r3) |
| 43 | stc %r0,0(%r4,%r1) |
| 44 | brctg %r4,.Lmemmove_reverse |
| 45 | ic %r0,0(%r4,%r3) |
| 46 | stc %r0,0(%r4,%r1) |
| 47 | BR_EX %r14 |
| 48 | .Lmemmove_mvc: |
| 49 | mvc 0(1,%r1),0(%r3) |
| 50 | EXPORT_SYMBOL(memmove) |
| 51 | |
| 52 | /* |
| 53 | * memset implementation |
| 54 | * |
| 55 | * This code corresponds to the C construct below. We do distinguish |
| 56 | * between clearing (c == 0) and setting a memory array (c != 0) simply |
| 57 | * because nearly all memset invocations in the kernel clear memory and |
| 58 | * the xc instruction is preferred in such cases. |
| 59 | * |
| 60 | * void *memset(void *s, int c, size_t n) |
| 61 | * { |
| 62 | * if (likely(c == 0)) |
| 63 | * return __builtin_memset(s, 0, n); |
| 64 | * return __builtin_memset(s, c, n); |
| 65 | * } |
| 66 | */ |
| 67 | ENTRY(memset) |
| 68 | ltgr %r4,%r4 |
| 69 | jz .Lmemset_exit |
| 70 | ltgr %r3,%r3 |
| 71 | jnz .Lmemset_fill |
| 72 | aghi %r4,-1 |
| 73 | srlg %r3,%r4,8 |
| 74 | ltgr %r3,%r3 |
| 75 | lgr %r1,%r2 |
| 76 | jz .Lmemset_clear_remainder |
| 77 | .Lmemset_clear_loop: |
| 78 | xc 0(256,%r1),0(%r1) |
| 79 | la %r1,256(%r1) |
| 80 | brctg %r3,.Lmemset_clear_loop |
| 81 | .Lmemset_clear_remainder: |
| 82 | larl %r3,.Lmemset_xc |
| 83 | ex %r4,0(%r3) |
| 84 | .Lmemset_exit: |
| 85 | BR_EX %r14 |
| 86 | .Lmemset_fill: |
| 87 | stc %r3,0(%r2) |
| 88 | cghi %r4,1 |
| 89 | lgr %r1,%r2 |
| 90 | je .Lmemset_fill_exit |
| 91 | aghi %r4,-2 |
| 92 | srlg %r3,%r4,8 |
| 93 | ltgr %r3,%r3 |
| 94 | jz .Lmemset_fill_remainder |
| 95 | .Lmemset_fill_loop: |
| 96 | mvc 1(256,%r1),0(%r1) |
| 97 | la %r1,256(%r1) |
| 98 | brctg %r3,.Lmemset_fill_loop |
| 99 | .Lmemset_fill_remainder: |
| 100 | larl %r3,.Lmemset_mvc |
| 101 | ex %r4,0(%r3) |
| 102 | .Lmemset_fill_exit: |
| 103 | BR_EX %r14 |
| 104 | .Lmemset_xc: |
| 105 | xc 0(1,%r1),0(%r1) |
| 106 | .Lmemset_mvc: |
| 107 | mvc 1(1,%r1),0(%r1) |
| 108 | EXPORT_SYMBOL(memset) |
| 109 | |
| 110 | /* |
| 111 | * memcpy implementation |
| 112 | * |
| 113 | * void *memcpy(void *dest, const void *src, size_t n) |
| 114 | */ |
| 115 | ENTRY(memcpy) |
| 116 | ltgr %r4,%r4 |
| 117 | jz .Lmemcpy_exit |
| 118 | aghi %r4,-1 |
| 119 | srlg %r5,%r4,8 |
| 120 | ltgr %r5,%r5 |
| 121 | lgr %r1,%r2 |
| 122 | jnz .Lmemcpy_loop |
| 123 | .Lmemcpy_remainder: |
| 124 | larl %r5,.Lmemcpy_mvc |
| 125 | ex %r4,0(%r5) |
| 126 | .Lmemcpy_exit: |
| 127 | BR_EX %r14 |
| 128 | .Lmemcpy_loop: |
| 129 | mvc 0(256,%r1),0(%r3) |
| 130 | la %r1,256(%r1) |
| 131 | la %r3,256(%r3) |
| 132 | brctg %r5,.Lmemcpy_loop |
| 133 | j .Lmemcpy_remainder |
| 134 | .Lmemcpy_mvc: |
| 135 | mvc 0(1,%r1),0(%r3) |
| 136 | EXPORT_SYMBOL(memcpy) |