blob: a42c36118836e7ed86a42e75f838b9b7d6fa3125 [file] [log] [blame]
Peter Collingbourne900d07d2019-10-28 13:11:00 -07001/*
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 Collingbourne900d07d2019-10-28 13:11:00 -070029#include <private/bionic_ifuncs.h>
30#include <stddef.h>
31#include <sys/auxv.h>
32
Elliott Hughesf978a852024-04-16 00:37:38 +000033static inline bool __bionic_is_oryon(unsigned long hwcap) {
34 if (!(hwcap & HWCAP_CPUID)) return false;
Vaisakh K V83e55842024-03-29 12:47:39 +053035
Elliott Hughesf978a852024-04-16 00:37:38 +000036 // 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 V83e55842024-03-29 12:47:39 +053041
Elliott Hughesf978a852024-04-16 00:37:38 +000042 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 V83e55842024-03-29 12:47:39 +053049
Peter Collingbourne900d07d2019-10-28 13:11:00 -070050extern "C" {
51
Peter Collingbourne900d07d2019-10-28 13:11:00 -070052typedef void* memchr_func(const void*, int, size_t);
53DEFINE_IFUNC_FOR(memchr) {
Peter Collingbourne7e201172020-12-21 14:08:38 -080054 if (arg->_hwcap2 & HWCAP2_MTE) {
Peter Collingbourne2361d4e2020-06-03 16:55:37 -070055 RETURN_FUNC(memchr_func, __memchr_aarch64_mte);
Peter Collingbourne900d07d2019-10-28 13:11:00 -070056 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -080057 RETURN_FUNC(memchr_func, __memchr_aarch64);
58 }
59}
60
Elliott Hughes20f9d672023-05-22 19:28:33 +000061typedef int memcmp_func(const void*, const void*, size_t);
Elliott Hughes3d8e98f2023-01-25 23:33:39 +000062DEFINE_IFUNC_FOR(memcmp) {
63 // TODO: enable the SVE version.
64 RETURN_FUNC(memcmp_func, __memcmp_aarch64);
65}
66
Elliott Hughes7daf4592022-11-17 00:34:13 +000067typedef void* memcpy_func(void*, const void*, size_t);
68DEFINE_IFUNC_FOR(memcpy) {
Elliott Hughesf978a852024-04-16 00:37:38 +000069 if (__bionic_is_oryon(arg->_hwcap)) {
Vaisakh K V83e55842024-03-29 12:47:39 +053070 RETURN_FUNC(memcpy_func, __memcpy_aarch64_nt);
Elliott Hughesf978a852024-04-16 00:37:38 +000071 } else if (arg->_hwcap & HWCAP_ASIMD) {
72 RETURN_FUNC(memcpy_func, __memcpy_aarch64_simd);
73 } else {
Elliott Hughes7daf4592022-11-17 00:34:13 +000074 RETURN_FUNC(memcpy_func, __memcpy_aarch64);
75 }
76}
77
78typedef void* memmove_func(void*, const void*, size_t);
79DEFINE_IFUNC_FOR(memmove) {
Elliott Hughesf978a852024-04-16 00:37:38 +000080 if (__bionic_is_oryon(arg->_hwcap)) {
81 RETURN_FUNC(memcpy_func, __memmove_aarch64_nt);
82 } else if (arg->_hwcap & HWCAP_ASIMD) {
Vaisakh K V83e55842024-03-29 12:47:39 +053083 RETURN_FUNC(memmove_func, __memmove_aarch64_simd);
84 } else {
85 RETURN_FUNC(memmove_func, __memmove_aarch64);
86 }
Elliott Hughes7daf4592022-11-17 00:34:13 +000087}
88
Elliott Hughescb47a4f2024-03-25 13:44:36 -070089typedef int memrchr_func(const void*, int, size_t);
90DEFINE_IFUNC_FOR(memrchr) {
91 RETURN_FUNC(memrchr_func, __memrchr_aarch64);
92}
93
94typedef int memset_func(void*, int, size_t);
95DEFINE_IFUNC_FOR(memset) {
Elliott Hughesf978a852024-04-16 00:37:38 +000096 if (__bionic_is_oryon(arg->_hwcap)) {
Vaisakh K V54a61212024-03-29 13:32:45 +053097 RETURN_FUNC(memset_func, __memset_aarch64_nt);
Vaisakh K V54a61212024-03-29 13:32:45 +053098 } else {
Elliott Hughesf978a852024-04-16 00:37:38 +000099 RETURN_FUNC(memset_func, __memset_aarch64);
Vaisakh K V54a61212024-03-29 13:32:45 +0530100 }
Elliott Hughescb47a4f2024-03-25 13:44:36 -0700101}
102
Elliott Hughes20f9d672023-05-22 19:28:33 +0000103typedef char* stpcpy_func(char*, const char*, size_t);
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800104DEFINE_IFUNC_FOR(stpcpy) {
Elliott Hughes5ec0bfa2023-01-25 18:12:18 +0000105 // TODO: enable the SVE version.
106 RETURN_FUNC(stpcpy_func, __stpcpy_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700107}
108
109typedef char* strchr_func(const char*, int);
110DEFINE_IFUNC_FOR(strchr) {
Peter Collingbourne7e201172020-12-21 14:08:38 -0800111 if (arg->_hwcap2 & HWCAP2_MTE) {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800112 RETURN_FUNC(strchr_func, __strchr_aarch64_mte);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700113 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800114 RETURN_FUNC(strchr_func, __strchr_aarch64);
115 }
116}
117
118typedef char* strchrnul_func(const char*, int);
119DEFINE_IFUNC_FOR(strchrnul) {
Peter Collingbourne7e201172020-12-21 14:08:38 -0800120 if (arg->_hwcap2 & HWCAP2_MTE) {
Peter Collingbourne2361d4e2020-06-03 16:55:37 -0700121 RETURN_FUNC(strchrnul_func, __strchrnul_aarch64_mte);
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800122 } else {
123 RETURN_FUNC(strchrnul_func, __strchrnul_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700124 }
125}
126
127typedef int strcmp_func(const char*, const char*);
128DEFINE_IFUNC_FOR(strcmp) {
Elliott Hughes5ec0bfa2023-01-25 18:12:18 +0000129 // TODO: enable the SVE version.
130 RETURN_FUNC(strcmp_func, __strcmp_aarch64);
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800131}
132
Elliott Hughes20f9d672023-05-22 19:28:33 +0000133typedef char* strcpy_func(char*, const char*);
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800134DEFINE_IFUNC_FOR(strcpy) {
Elliott Hughes5ec0bfa2023-01-25 18:12:18 +0000135 // TODO: enable the SVE version.
136 RETURN_FUNC(strcpy_func, __strcpy_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700137}
138
139typedef size_t strlen_func(const char*);
140DEFINE_IFUNC_FOR(strlen) {
Peter Collingbourne7e201172020-12-21 14:08:38 -0800141 if (arg->_hwcap2 & HWCAP2_MTE) {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800142 RETURN_FUNC(strlen_func, __strlen_aarch64_mte);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700143 } else {
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800144 RETURN_FUNC(strlen_func, __strlen_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700145 }
146}
147
Elliott Hughesa1974062023-05-18 13:30:35 -0700148typedef int strncmp_func(const char*, const char*, size_t);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700149DEFINE_IFUNC_FOR(strncmp) {
Elliott Hughes5ec0bfa2023-01-25 18:12:18 +0000150 // TODO: enable the SVE version.
151 RETURN_FUNC(strncmp_func, __strncmp_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700152}
153
Elliott Hughesa1974062023-05-18 13:30:35 -0700154typedef size_t strnlen_func(const char*, size_t);
Elliott Hughes3d8e98f2023-01-25 23:33:39 +0000155DEFINE_IFUNC_FOR(strnlen) {
156 // TODO: enable the SVE version.
157 RETURN_FUNC(strnlen_func, __strnlen_aarch64);
158}
159
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800160typedef char* strrchr_func(const char*, int);
161DEFINE_IFUNC_FOR(strrchr) {
Peter Collingbourne7e201172020-12-21 14:08:38 -0800162 if (arg->_hwcap2 & HWCAP2_MTE) {
Peter Collingbourne2361d4e2020-06-03 16:55:37 -0700163 RETURN_FUNC(strrchr_func, __strrchr_aarch64_mte);
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800164 } else {
165 RETURN_FUNC(strrchr_func, __strrchr_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700166 }
167}
168
169} // extern "C"