xref: /aosp_15_r20/bionic/libc/arch-riscv64/dynamic_function_dispatch.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*
2  * Copyright (C) 2023 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 
29 #include <fcntl.h>
30 #include <private/bionic_ifuncs.h>
31 #include <stddef.h>
32 #include <sys/syscall.h>
33 #include <unistd.h>
34 
35 extern "C" {
36 
ifunc_faccessat(int dir_fd,const char * path,int mode)37 static inline __always_inline int ifunc_faccessat(int dir_fd, const char* path, int mode) {
38   register long a0 __asm__("a0") = dir_fd;
39   register long a1 __asm__("a1") = reinterpret_cast<long>(path);
40   register long a2 __asm__("a2") = mode;
41   register long a7 __asm__("a7") = __NR_faccessat;
42   __asm__("ecall" : "=r"(a0) : "r"(a0), "r"(a1), "r"(a2), "r"(a7) : "memory");
43   return a0;
44 }
45 
have_fast_v()46 static bool have_fast_v() {
47   static bool result = []() {
48     // We don't want to do a full "bogomips" test, so just check for the
49     // presence of a file that would indicate that we're running in qemu.
50     return ifunc_faccessat(AT_FDCWD, "/dev/hvc0", F_OK) != 0;
51   }();
52   return result;
53 }
54 
DEFINE_IFUNC_FOR(memchr)55 DEFINE_IFUNC_FOR(memchr) {
56   if (have_fast_v()) RETURN_FUNC(memchr_func_t, memchr_v);
57   RETURN_FUNC(memchr_func_t, memchr_gc);
58 }
59 MEMCHR_SHIM()
60 
DEFINE_IFUNC_FOR(memcmp)61 DEFINE_IFUNC_FOR(memcmp) {
62   if (have_fast_v()) RETURN_FUNC(memcmp_func_t, memcmp_v);
63   RETURN_FUNC(memcmp_func_t, memcmp_gc);
64 }
65 MEMCMP_SHIM()
66 
DEFINE_IFUNC_FOR(memcpy)67 DEFINE_IFUNC_FOR(memcpy) {
68   if (have_fast_v()) RETURN_FUNC(memcpy_func_t, memcpy_v);
69   RETURN_FUNC(memcpy_func_t, memcpy_gc);
70 }
71 MEMCPY_SHIM()
72 
DEFINE_IFUNC_FOR(memmove)73 DEFINE_IFUNC_FOR(memmove) {
74   if (have_fast_v()) RETURN_FUNC(memmove_func_t, memmove_v);
75   RETURN_FUNC(memmove_func_t, memmove_gc);
76 }
77 MEMMOVE_SHIM()
78 
DEFINE_IFUNC_FOR(memset)79 DEFINE_IFUNC_FOR(memset) {
80   if (have_fast_v()) RETURN_FUNC(memset_func_t, memset_v);
81   RETURN_FUNC(memset_func_t, memset_gc);
82 }
83 MEMSET_SHIM()
84 
DEFINE_IFUNC_FOR(stpcpy)85 DEFINE_IFUNC_FOR(stpcpy) {
86   if (have_fast_v()) RETURN_FUNC(stpcpy_func_t, stpcpy_v);
87   RETURN_FUNC(stpcpy_func_t, stpcpy_gc);
88 }
89 STPCPY_SHIM()
90 
DEFINE_IFUNC_FOR(strcat)91 DEFINE_IFUNC_FOR(strcat) {
92   if (have_fast_v()) RETURN_FUNC(strcat_func_t, strcat_v);
93   RETURN_FUNC(strcat_func_t, strcat_gc);
94 }
95 STRCAT_SHIM()
96 
DEFINE_IFUNC_FOR(strchr)97 DEFINE_IFUNC_FOR(strchr) {
98   if (have_fast_v()) RETURN_FUNC(strchr_func_t, strchr_v);
99   RETURN_FUNC(strchr_func_t, strchr_gc);
100 }
101 STRCHR_SHIM()
102 
DEFINE_IFUNC_FOR(strcmp)103 DEFINE_IFUNC_FOR(strcmp) {
104   if (have_fast_v()) RETURN_FUNC(strcmp_func_t, strcmp_v);
105   RETURN_FUNC(strcmp_func_t, strcmp_gc);
106 }
107 STRCMP_SHIM()
108 
DEFINE_IFUNC_FOR(strcpy)109 DEFINE_IFUNC_FOR(strcpy) {
110   if (have_fast_v()) RETURN_FUNC(strcpy_func_t, strcpy_v);
111   RETURN_FUNC(strcpy_func_t, strcpy_gc);
112 }
113 STRCPY_SHIM()
114 
DEFINE_IFUNC_FOR(strlen)115 DEFINE_IFUNC_FOR(strlen) {
116   if (have_fast_v()) RETURN_FUNC(strlen_func_t, strlen_v);
117   RETURN_FUNC(strlen_func_t, strlen_gc);
118 }
119 STRLEN_SHIM()
120 
DEFINE_IFUNC_FOR(strncat)121 DEFINE_IFUNC_FOR(strncat) {
122   if (have_fast_v()) RETURN_FUNC(strncat_func_t, strncat_v);
123   RETURN_FUNC(strncat_func_t, strncat_gc);
124 }
125 STRNCAT_SHIM()
126 
DEFINE_IFUNC_FOR(strncmp)127 DEFINE_IFUNC_FOR(strncmp) {
128   if (have_fast_v()) RETURN_FUNC(strncmp_func_t, strncmp_v);
129   RETURN_FUNC(strncmp_func_t, strncmp_gc);
130 }
131 STRNCMP_SHIM()
132 
DEFINE_IFUNC_FOR(strncpy)133 DEFINE_IFUNC_FOR(strncpy) {
134   if (have_fast_v()) RETURN_FUNC(strncpy_func_t, strncpy_v);
135   RETURN_FUNC(strncpy_func_t, strncpy_gc);
136 }
137 STRNCPY_SHIM()
138 
DEFINE_IFUNC_FOR(strnlen)139 DEFINE_IFUNC_FOR(strnlen) {
140   if (have_fast_v()) RETURN_FUNC(strnlen_func_t, strnlen_v);
141   RETURN_FUNC(strnlen_func_t, strnlen_gc);
142 }
143 STRNLEN_SHIM()
144 
145 }  // extern "C"
146