Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 29 | #include <private/bionic_ifuncs.h> |
| 30 | #include <stddef.h> |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 32 | static inline bool __bionic_is_oryon(unsigned long hwcap) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 33 | if (!(hwcap & HWCAP_CPUID)) return false; |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 34 | |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 35 | // Extract the implementor and variant bits from MIDR_EL1. |
| 36 | // https://www.kernel.org/doc/html/latest/arch/arm64/cpu-feature-registers.html#list-of-registers-with-visible-features |
| 37 | unsigned long midr; |
| 38 | __asm__ __volatile__("mrs %0, MIDR_EL1" : "=r"(midr)); |
| 39 | uint16_t cpu = (midr >> 20) & 0xfff; |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 40 | |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 41 | auto make_cpu = [](unsigned implementor, unsigned variant) { |
| 42 | return (implementor << 4) | variant; |
| 43 | }; |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 44 | |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 45 | // Check for implementor Qualcomm's variants 0x1..0x5 (Oryon). |
| 46 | return cpu >= make_cpu('Q', 0x1) && cpu <= make_cpu('Q', 0x5); |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 47 | } |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 48 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 49 | extern "C" { |
| 50 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 51 | DEFINE_IFUNC_FOR(memchr) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 52 | if (arg->_hwcap2 & HWCAP2_MTE) { |
| 53 | RETURN_FUNC(memchr_func_t, __memchr_aarch64_mte); |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 54 | } else { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 55 | RETURN_FUNC(memchr_func_t, __memchr_aarch64); |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 56 | } |
Elliott Hughes | 7daf459 | 2022-11-17 00:34:13 +0000 | [diff] [blame] | 57 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 58 | MEMCHR_SHIM() |
Elliott Hughes | 7daf459 | 2022-11-17 00:34:13 +0000 | [diff] [blame] | 59 | |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 60 | DEFINE_IFUNC_FOR(memcmp) { |
| 61 | // TODO: enable the SVE version. |
| 62 | RETURN_FUNC(memcmp_func_t, __memcmp_aarch64); |
| 63 | } |
| 64 | MEMCMP_SHIM() |
| 65 | |
| 66 | DEFINE_IFUNC_FOR(memcpy) { |
| 67 | if (arg->_hwcap2 & HWCAP2_MOPS) { |
| 68 | RETURN_FUNC(memcpy_func_t, __memmove_aarch64_mops); |
| 69 | } else if (__bionic_is_oryon(arg->_hwcap)) { |
| 70 | RETURN_FUNC(memcpy_func_t, __memcpy_aarch64_nt); |
| 71 | } else if (arg->_hwcap & HWCAP_ASIMD) { |
| 72 | RETURN_FUNC(memcpy_func_t, __memcpy_aarch64_simd); |
| 73 | } else { |
| 74 | RETURN_FUNC(memcpy_func_t, __memcpy_aarch64); |
| 75 | } |
| 76 | } |
| 77 | MEMCPY_SHIM() |
| 78 | |
| 79 | DEFINE_IFUNC_FOR(memmove) { |
| 80 | if (arg->_hwcap2 & HWCAP2_MOPS) { |
| 81 | RETURN_FUNC(memmove_func_t, __memmove_aarch64_mops); |
| 82 | } else if (__bionic_is_oryon(arg->_hwcap)) { |
| 83 | RETURN_FUNC(memmove_func_t, __memmove_aarch64_nt); |
| 84 | } else if (arg->_hwcap & HWCAP_ASIMD) { |
| 85 | RETURN_FUNC(memmove_func_t, __memmove_aarch64_simd); |
| 86 | } else { |
| 87 | RETURN_FUNC(memmove_func_t, __memmove_aarch64); |
| 88 | } |
| 89 | } |
| 90 | MEMMOVE_SHIM() |
| 91 | |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 92 | DEFINE_IFUNC_FOR(memrchr) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 93 | RETURN_FUNC(memrchr_func_t, __memrchr_aarch64); |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 94 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 95 | MEMRCHR_SHIM() |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 96 | |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 97 | DEFINE_IFUNC_FOR(memset) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 98 | if (arg->_hwcap2 & HWCAP2_MOPS) { |
| 99 | RETURN_FUNC(memset_func_t, __memset_aarch64_mops); |
| 100 | } else if (__bionic_is_oryon(arg->_hwcap)) { |
| 101 | RETURN_FUNC(memset_func_t, __memset_aarch64_nt); |
| 102 | } else { |
| 103 | RETURN_FUNC(memset_func_t, __memset_aarch64); |
| 104 | } |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 105 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 106 | MEMSET_SHIM() |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 107 | |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 108 | DEFINE_IFUNC_FOR(stpcpy) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 109 | // TODO: enable the SVE version. |
| 110 | RETURN_FUNC(stpcpy_func_t, __stpcpy_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 111 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 112 | STPCPY_SHIM() |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 113 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 114 | DEFINE_IFUNC_FOR(strchr) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 115 | if (arg->_hwcap2 & HWCAP2_MTE) { |
| 116 | RETURN_FUNC(strchr_func_t, __strchr_aarch64_mte); |
| 117 | } else { |
| 118 | RETURN_FUNC(strchr_func_t, __strchr_aarch64); |
| 119 | } |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 120 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 121 | STRCHR_SHIM() |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 122 | |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 123 | DEFINE_IFUNC_FOR(strchrnul) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 124 | if (arg->_hwcap2 & HWCAP2_MTE) { |
| 125 | RETURN_FUNC(strchrnul_func_t, __strchrnul_aarch64_mte); |
| 126 | } else { |
| 127 | RETURN_FUNC(strchrnul_func_t, __strchrnul_aarch64); |
| 128 | } |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 129 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 130 | STRCHRNUL_SHIM() |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 131 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 132 | DEFINE_IFUNC_FOR(strcmp) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 133 | // TODO: enable the SVE version. |
| 134 | RETURN_FUNC(strcmp_func_t, __strcmp_aarch64); |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 135 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 136 | STRCMP_SHIM() |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 137 | |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 138 | DEFINE_IFUNC_FOR(strcpy) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 139 | // TODO: enable the SVE version. |
| 140 | RETURN_FUNC(strcpy_func_t, __strcpy_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 141 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 142 | STRCPY_SHIM() |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 143 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 144 | DEFINE_IFUNC_FOR(strlen) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 145 | if (arg->_hwcap2 & HWCAP2_MTE) { |
| 146 | RETURN_FUNC(strlen_func_t, __strlen_aarch64_mte); |
| 147 | } else { |
| 148 | RETURN_FUNC(strlen_func_t, __strlen_aarch64); |
| 149 | } |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 150 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 151 | STRLEN_SHIM() |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 152 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 153 | DEFINE_IFUNC_FOR(strncmp) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 154 | // TODO: enable the SVE version. |
| 155 | RETURN_FUNC(strncmp_func_t, __strncmp_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 156 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 157 | STRNCMP_SHIM() |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 158 | |
Elliott Hughes | 3d8e98f | 2023-01-25 23:33:39 +0000 | [diff] [blame] | 159 | DEFINE_IFUNC_FOR(strnlen) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 160 | // TODO: enable the SVE version. |
| 161 | RETURN_FUNC(strnlen_func_t, __strnlen_aarch64); |
Elliott Hughes | 3d8e98f | 2023-01-25 23:33:39 +0000 | [diff] [blame] | 162 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 163 | STRNLEN_SHIM() |
Elliott Hughes | 3d8e98f | 2023-01-25 23:33:39 +0000 | [diff] [blame] | 164 | |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 165 | DEFINE_IFUNC_FOR(strrchr) { |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 166 | if (arg->_hwcap2 & HWCAP2_MTE) { |
| 167 | RETURN_FUNC(strrchr_func_t, __strrchr_aarch64_mte); |
| 168 | } else { |
| 169 | RETURN_FUNC(strrchr_func_t, __strrchr_aarch64); |
| 170 | } |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 171 | } |
Elliott Hughes | adc4171 | 2024-08-16 13:08:11 +0000 | [diff] [blame] | 172 | STRRCHR_SHIM() |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 173 | |
| 174 | } // extern "C" |