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> |
| 31 | #include <sys/auxv.h> |
| 32 | |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 33 | static inline bool __bionic_is_oryon(unsigned long hwcap) { |
| 34 | if (!(hwcap & HWCAP_CPUID)) return false; |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 35 | |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 36 | // Extract the implementor and variant bits from MIDR_EL1. |
| 37 | // https://www.kernel.org/doc/html/latest/arch/arm64/cpu-feature-registers.html#list-of-registers-with-visible-features |
| 38 | unsigned long midr; |
| 39 | __asm__ __volatile__("mrs %0, MIDR_EL1" : "=r"(midr)); |
| 40 | uint16_t cpu = (midr >> 20) & 0xfff; |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 41 | |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 42 | auto make_cpu = [](unsigned implementor, unsigned variant) { |
| 43 | return (implementor << 4) | variant; |
| 44 | }; |
| 45 | |
| 46 | // Check for implementor Qualcomm's variants 0x1..0x5 (Oryon). |
| 47 | return cpu >= make_cpu('Q', 0x1) && cpu <= make_cpu('Q', 0x5); |
| 48 | } |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 49 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 50 | extern "C" { |
| 51 | |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 52 | typedef void* memchr_func(const void*, int, size_t); |
| 53 | DEFINE_IFUNC_FOR(memchr) { |
Peter Collingbourne | 7e20117 | 2020-12-21 14:08:38 -0800 | [diff] [blame] | 54 | if (arg->_hwcap2 & HWCAP2_MTE) { |
Peter Collingbourne | 2361d4e | 2020-06-03 16:55:37 -0700 | [diff] [blame] | 55 | RETURN_FUNC(memchr_func, __memchr_aarch64_mte); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 56 | } else { |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 57 | RETURN_FUNC(memchr_func, __memchr_aarch64); |
| 58 | } |
| 59 | } |
| 60 | |
Elliott Hughes | 20f9d67 | 2023-05-22 19:28:33 +0000 | [diff] [blame] | 61 | typedef int memcmp_func(const void*, const void*, size_t); |
Elliott Hughes | 3d8e98f | 2023-01-25 23:33:39 +0000 | [diff] [blame] | 62 | DEFINE_IFUNC_FOR(memcmp) { |
| 63 | // TODO: enable the SVE version. |
| 64 | RETURN_FUNC(memcmp_func, __memcmp_aarch64); |
| 65 | } |
| 66 | |
Elliott Hughes | 7daf459 | 2022-11-17 00:34:13 +0000 | [diff] [blame] | 67 | typedef void* memcpy_func(void*, const void*, size_t); |
| 68 | DEFINE_IFUNC_FOR(memcpy) { |
Tamas Zsoldos | 4b6a89b | 2023-11-15 15:53:15 +0100 | [diff] [blame] | 69 | if (arg->_hwcap2 & HWCAP2_MOPS) { |
| 70 | RETURN_FUNC(memcpy_func, __memmove_aarch64_mops); |
| 71 | } else if (__bionic_is_oryon(arg->_hwcap)) { |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 72 | RETURN_FUNC(memcpy_func, __memcpy_aarch64_nt); |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 73 | } else if (arg->_hwcap & HWCAP_ASIMD) { |
| 74 | RETURN_FUNC(memcpy_func, __memcpy_aarch64_simd); |
| 75 | } else { |
Elliott Hughes | 7daf459 | 2022-11-17 00:34:13 +0000 | [diff] [blame] | 76 | RETURN_FUNC(memcpy_func, __memcpy_aarch64); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | typedef void* memmove_func(void*, const void*, size_t); |
| 81 | DEFINE_IFUNC_FOR(memmove) { |
Tamas Zsoldos | 4b6a89b | 2023-11-15 15:53:15 +0100 | [diff] [blame] | 82 | if (arg->_hwcap2 & HWCAP2_MOPS) { |
| 83 | RETURN_FUNC(memmove_func, __memmove_aarch64_mops); |
| 84 | } else if (__bionic_is_oryon(arg->_hwcap)) { |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 85 | RETURN_FUNC(memcpy_func, __memmove_aarch64_nt); |
| 86 | } else if (arg->_hwcap & HWCAP_ASIMD) { |
Vaisakh K V | 83e5584 | 2024-03-29 12:47:39 +0530 | [diff] [blame] | 87 | RETURN_FUNC(memmove_func, __memmove_aarch64_simd); |
| 88 | } else { |
| 89 | RETURN_FUNC(memmove_func, __memmove_aarch64); |
| 90 | } |
Elliott Hughes | 7daf459 | 2022-11-17 00:34:13 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 93 | typedef int memrchr_func(const void*, int, size_t); |
| 94 | DEFINE_IFUNC_FOR(memrchr) { |
| 95 | RETURN_FUNC(memrchr_func, __memrchr_aarch64); |
| 96 | } |
| 97 | |
| 98 | typedef int memset_func(void*, int, size_t); |
| 99 | DEFINE_IFUNC_FOR(memset) { |
Tamas Zsoldos | 4b6a89b | 2023-11-15 15:53:15 +0100 | [diff] [blame] | 100 | if (arg->_hwcap2 & HWCAP2_MOPS) { |
| 101 | RETURN_FUNC(memset_func, __memset_aarch64_mops); |
| 102 | } else if (__bionic_is_oryon(arg->_hwcap)) { |
Vaisakh K V | 54a6121 | 2024-03-29 13:32:45 +0530 | [diff] [blame] | 103 | RETURN_FUNC(memset_func, __memset_aarch64_nt); |
Vaisakh K V | 54a6121 | 2024-03-29 13:32:45 +0530 | [diff] [blame] | 104 | } else { |
Elliott Hughes | f978a85 | 2024-04-16 00:37:38 +0000 | [diff] [blame] | 105 | RETURN_FUNC(memset_func, __memset_aarch64); |
Vaisakh K V | 54a6121 | 2024-03-29 13:32:45 +0530 | [diff] [blame] | 106 | } |
Elliott Hughes | cb47a4f | 2024-03-25 13:44:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Elliott Hughes | 20f9d67 | 2023-05-22 19:28:33 +0000 | [diff] [blame] | 109 | typedef char* stpcpy_func(char*, const char*, size_t); |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 110 | DEFINE_IFUNC_FOR(stpcpy) { |
Elliott Hughes | 5ec0bfa | 2023-01-25 18:12:18 +0000 | [diff] [blame] | 111 | // TODO: enable the SVE version. |
| 112 | RETURN_FUNC(stpcpy_func, __stpcpy_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | typedef char* strchr_func(const char*, int); |
| 116 | DEFINE_IFUNC_FOR(strchr) { |
Peter Collingbourne | 7e20117 | 2020-12-21 14:08:38 -0800 | [diff] [blame] | 117 | if (arg->_hwcap2 & HWCAP2_MTE) { |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 118 | RETURN_FUNC(strchr_func, __strchr_aarch64_mte); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 119 | } else { |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 120 | RETURN_FUNC(strchr_func, __strchr_aarch64); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | typedef char* strchrnul_func(const char*, int); |
| 125 | DEFINE_IFUNC_FOR(strchrnul) { |
Peter Collingbourne | 7e20117 | 2020-12-21 14:08:38 -0800 | [diff] [blame] | 126 | if (arg->_hwcap2 & HWCAP2_MTE) { |
Peter Collingbourne | 2361d4e | 2020-06-03 16:55:37 -0700 | [diff] [blame] | 127 | RETURN_FUNC(strchrnul_func, __strchrnul_aarch64_mte); |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 128 | } else { |
| 129 | RETURN_FUNC(strchrnul_func, __strchrnul_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | typedef int strcmp_func(const char*, const char*); |
| 134 | DEFINE_IFUNC_FOR(strcmp) { |
Elliott Hughes | 5ec0bfa | 2023-01-25 18:12:18 +0000 | [diff] [blame] | 135 | // TODO: enable the SVE version. |
| 136 | RETURN_FUNC(strcmp_func, __strcmp_aarch64); |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Elliott Hughes | 20f9d67 | 2023-05-22 19:28:33 +0000 | [diff] [blame] | 139 | typedef char* strcpy_func(char*, const char*); |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 140 | DEFINE_IFUNC_FOR(strcpy) { |
Elliott Hughes | 5ec0bfa | 2023-01-25 18:12:18 +0000 | [diff] [blame] | 141 | // TODO: enable the SVE version. |
| 142 | RETURN_FUNC(strcpy_func, __strcpy_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | typedef size_t strlen_func(const char*); |
| 146 | DEFINE_IFUNC_FOR(strlen) { |
Peter Collingbourne | 7e20117 | 2020-12-21 14:08:38 -0800 | [diff] [blame] | 147 | if (arg->_hwcap2 & HWCAP2_MTE) { |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 148 | RETURN_FUNC(strlen_func, __strlen_aarch64_mte); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 149 | } else { |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 150 | RETURN_FUNC(strlen_func, __strlen_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Elliott Hughes | a197406 | 2023-05-18 13:30:35 -0700 | [diff] [blame] | 154 | typedef int strncmp_func(const char*, const char*, size_t); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 155 | DEFINE_IFUNC_FOR(strncmp) { |
Elliott Hughes | 5ec0bfa | 2023-01-25 18:12:18 +0000 | [diff] [blame] | 156 | // TODO: enable the SVE version. |
| 157 | RETURN_FUNC(strncmp_func, __strncmp_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Elliott Hughes | a197406 | 2023-05-18 13:30:35 -0700 | [diff] [blame] | 160 | typedef size_t strnlen_func(const char*, size_t); |
Elliott Hughes | 3d8e98f | 2023-01-25 23:33:39 +0000 | [diff] [blame] | 161 | DEFINE_IFUNC_FOR(strnlen) { |
| 162 | // TODO: enable the SVE version. |
| 163 | RETURN_FUNC(strnlen_func, __strnlen_aarch64); |
| 164 | } |
| 165 | |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 166 | typedef char* strrchr_func(const char*, int); |
| 167 | DEFINE_IFUNC_FOR(strrchr) { |
Peter Collingbourne | 7e20117 | 2020-12-21 14:08:38 -0800 | [diff] [blame] | 168 | if (arg->_hwcap2 & HWCAP2_MTE) { |
Peter Collingbourne | 2361d4e | 2020-06-03 16:55:37 -0700 | [diff] [blame] | 169 | RETURN_FUNC(strrchr_func, __strrchr_aarch64_mte); |
Peter Collingbourne | 337a5b3 | 2020-02-21 12:11:02 -0800 | [diff] [blame] | 170 | } else { |
| 171 | RETURN_FUNC(strrchr_func, __strrchr_aarch64); |
Peter Collingbourne | 900d07d | 2019-10-28 13:11:00 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | } // extern "C" |