blob: 29e47b399c4bb070e4803cd0b3e4c1022014c6a4 [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>
Peter Collingbourne900d07d2019-10-28 13:11:00 -070031
Elliott Hughesf978a852024-04-16 00:37:38 +000032static inline bool __bionic_is_oryon(unsigned long hwcap) {
Elliott Hughesadc41712024-08-16 13:08:11 +000033 if (!(hwcap & HWCAP_CPUID)) return false;
Vaisakh K V83e55842024-03-29 12:47:39 +053034
Elliott Hughesadc41712024-08-16 13:08:11 +000035 // 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 V83e55842024-03-29 12:47:39 +053040
Elliott Hughesadc41712024-08-16 13:08:11 +000041 auto make_cpu = [](unsigned implementor, unsigned variant) {
42 return (implementor << 4) | variant;
43 };
Elliott Hughesf978a852024-04-16 00:37:38 +000044
Elliott Hughesadc41712024-08-16 13:08:11 +000045 // Check for implementor Qualcomm's variants 0x1..0x5 (Oryon).
46 return cpu >= make_cpu('Q', 0x1) && cpu <= make_cpu('Q', 0x5);
Elliott Hughesf978a852024-04-16 00:37:38 +000047}
Vaisakh K V83e55842024-03-29 12:47:39 +053048
Peter Collingbourne900d07d2019-10-28 13:11:00 -070049extern "C" {
50
Peter Collingbourne900d07d2019-10-28 13:11:00 -070051DEFINE_IFUNC_FOR(memchr) {
Elliott Hughesadc41712024-08-16 13:08:11 +000052 if (arg->_hwcap2 & HWCAP2_MTE) {
53 RETURN_FUNC(memchr_func_t, __memchr_aarch64_mte);
Vaisakh K V83e55842024-03-29 12:47:39 +053054 } else {
Elliott Hughesadc41712024-08-16 13:08:11 +000055 RETURN_FUNC(memchr_func_t, __memchr_aarch64);
Vaisakh K V83e55842024-03-29 12:47:39 +053056 }
Elliott Hughes7daf4592022-11-17 00:34:13 +000057}
Elliott Hughesadc41712024-08-16 13:08:11 +000058MEMCHR_SHIM()
Elliott Hughes7daf4592022-11-17 00:34:13 +000059
Elliott Hughesadc41712024-08-16 13:08:11 +000060DEFINE_IFUNC_FOR(memcmp) {
61 // TODO: enable the SVE version.
62 RETURN_FUNC(memcmp_func_t, __memcmp_aarch64);
63}
64MEMCMP_SHIM()
65
66DEFINE_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}
77MEMCPY_SHIM()
78
79DEFINE_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}
90MEMMOVE_SHIM()
91
Elliott Hughescb47a4f2024-03-25 13:44:36 -070092DEFINE_IFUNC_FOR(memrchr) {
Elliott Hughesadc41712024-08-16 13:08:11 +000093 RETURN_FUNC(memrchr_func_t, __memrchr_aarch64);
Elliott Hughescb47a4f2024-03-25 13:44:36 -070094}
Elliott Hughesadc41712024-08-16 13:08:11 +000095MEMRCHR_SHIM()
Elliott Hughescb47a4f2024-03-25 13:44:36 -070096
Elliott Hughescb47a4f2024-03-25 13:44:36 -070097DEFINE_IFUNC_FOR(memset) {
Elliott Hughesadc41712024-08-16 13:08:11 +000098 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 Hughescb47a4f2024-03-25 13:44:36 -0700105}
Elliott Hughesadc41712024-08-16 13:08:11 +0000106MEMSET_SHIM()
Elliott Hughescb47a4f2024-03-25 13:44:36 -0700107
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800108DEFINE_IFUNC_FOR(stpcpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000109 // TODO: enable the SVE version.
110 RETURN_FUNC(stpcpy_func_t, __stpcpy_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700111}
Elliott Hughesadc41712024-08-16 13:08:11 +0000112STPCPY_SHIM()
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700113
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700114DEFINE_IFUNC_FOR(strchr) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000115 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 Collingbourne337a5b32020-02-21 12:11:02 -0800120}
Elliott Hughesadc41712024-08-16 13:08:11 +0000121STRCHR_SHIM()
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800122
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800123DEFINE_IFUNC_FOR(strchrnul) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000124 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 Collingbourne900d07d2019-10-28 13:11:00 -0700129}
Elliott Hughesadc41712024-08-16 13:08:11 +0000130STRCHRNUL_SHIM()
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700131
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700132DEFINE_IFUNC_FOR(strcmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000133 // TODO: enable the SVE version.
134 RETURN_FUNC(strcmp_func_t, __strcmp_aarch64);
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800135}
Elliott Hughesadc41712024-08-16 13:08:11 +0000136STRCMP_SHIM()
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800137
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800138DEFINE_IFUNC_FOR(strcpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000139 // TODO: enable the SVE version.
140 RETURN_FUNC(strcpy_func_t, __strcpy_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700141}
Elliott Hughesadc41712024-08-16 13:08:11 +0000142STRCPY_SHIM()
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700143
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700144DEFINE_IFUNC_FOR(strlen) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000145 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 Collingbourne900d07d2019-10-28 13:11:00 -0700150}
Elliott Hughesadc41712024-08-16 13:08:11 +0000151STRLEN_SHIM()
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700152
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700153DEFINE_IFUNC_FOR(strncmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000154 // TODO: enable the SVE version.
155 RETURN_FUNC(strncmp_func_t, __strncmp_aarch64);
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700156}
Elliott Hughesadc41712024-08-16 13:08:11 +0000157STRNCMP_SHIM()
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700158
Elliott Hughes3d8e98f2023-01-25 23:33:39 +0000159DEFINE_IFUNC_FOR(strnlen) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000160 // TODO: enable the SVE version.
161 RETURN_FUNC(strnlen_func_t, __strnlen_aarch64);
Elliott Hughes3d8e98f2023-01-25 23:33:39 +0000162}
Elliott Hughesadc41712024-08-16 13:08:11 +0000163STRNLEN_SHIM()
Elliott Hughes3d8e98f2023-01-25 23:33:39 +0000164
Peter Collingbourne337a5b32020-02-21 12:11:02 -0800165DEFINE_IFUNC_FOR(strrchr) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000166 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 Collingbourne900d07d2019-10-28 13:11:00 -0700171}
Elliott Hughesadc41712024-08-16 13:08:11 +0000172STRRCHR_SHIM()
Peter Collingbourne900d07d2019-10-28 13:11:00 -0700173
174} // extern "C"