1 | /* Hardware capability support for run-time dynamic loader. |
2 | Copyright (C) 2017-2023 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _DL_HWCAPS_H |
20 | #define _DL_HWCAPS_H |
21 | |
22 | #include <stdint.h> |
23 | #include <stddef.h> |
24 | |
25 | #include <elf/dl-tunables.h> |
26 | |
27 | #define GLIBC_HWCAPS_SUBDIRECTORY "glibc-hwcaps" |
28 | #define GLIBC_HWCAPS_PREFIX GLIBC_HWCAPS_SUBDIRECTORY "/" |
29 | |
30 | /* Used by _dl_hwcaps_split below, to split strings at ':' |
31 | separators. */ |
32 | struct dl_hwcaps_split |
33 | { |
34 | const char *segment; /* Start of the current segment. */ |
35 | size_t length; /* Number of bytes until ':' or NUL. */ |
36 | }; |
37 | |
38 | /* Prepare *S to parse SUBJECT, for future _dl_hwcaps_split calls. If |
39 | SUBJECT is NULL, it is treated as the empty string. */ |
40 | static inline void |
41 | _dl_hwcaps_split_init (struct dl_hwcaps_split *s, const char *subject) |
42 | { |
43 | s->segment = subject; |
44 | /* The initial call to _dl_hwcaps_split will not skip anything. */ |
45 | s->length = 0; |
46 | } |
47 | |
48 | /* Extract the next non-empty string segment, up to ':' or the null |
49 | terminator. Return true if one more segment was found, or false if |
50 | the end of the string was reached. On success, S->segment is the |
51 | start of the segment found, and S->length is its length. |
52 | (Typically, S->segment[S->length] is not null.) */ |
53 | _Bool _dl_hwcaps_split (struct dl_hwcaps_split *s) attribute_hidden; |
54 | |
55 | /* Similar to dl_hwcaps_split, but with bit-based and name-based |
56 | masking. */ |
57 | struct dl_hwcaps_split_masked |
58 | { |
59 | struct dl_hwcaps_split split; |
60 | |
61 | /* For used by the iterator implementation. */ |
62 | const char *mask; |
63 | uint32_t bitmask; |
64 | }; |
65 | |
66 | /* Prepare *S for iteration with _dl_hwcaps_split_masked. Only HWCAP |
67 | names in SUBJECT whose bit is set in BITMASK and whose name is in |
68 | MASK will be returned. SUBJECT must not contain empty HWCAP names. |
69 | If MASK is NULL, no name-based masking is applied. Likewise for |
70 | BITMASK if BITMASK is -1 (infinite number of bits). */ |
71 | static inline void |
72 | _dl_hwcaps_split_masked_init (struct dl_hwcaps_split_masked *s, |
73 | const char *subject, |
74 | uint32_t bitmask, const char *mask) |
75 | { |
76 | _dl_hwcaps_split_init (&s->split, subject); |
77 | s->bitmask = bitmask; |
78 | s->mask = mask; |
79 | } |
80 | |
81 | /* Like _dl_hwcaps_split, but apply masking. */ |
82 | _Bool _dl_hwcaps_split_masked (struct dl_hwcaps_split_masked *s) |
83 | attribute_hidden; |
84 | |
85 | /* Returns true if the colon-separated HWCAP list HWCAPS contains the |
86 | capability NAME (with length NAME_LENGTH). If HWCAPS is NULL, the |
87 | function returns true. */ |
88 | _Bool _dl_hwcaps_contains (const char *hwcaps, const char *name, |
89 | size_t name_length) attribute_hidden; |
90 | |
91 | /* Colon-separated string of glibc-hwcaps subdirectories, without the |
92 | "glibc-hwcaps/" prefix. The most preferred subdirectory needs to |
93 | be listed first. Up to 32 subdirectories are supported, limited by |
94 | the width of the uint32_t mask. */ |
95 | extern const char _dl_hwcaps_subdirs[] attribute_hidden; |
96 | |
97 | /* Returns a bitmap of active subdirectories in _dl_hwcaps_subdirs. |
98 | Bit 0 (the LSB) corresponds to the first substring in |
99 | _dl_hwcaps_subdirs, bit 1 to the second substring, and so on. |
100 | There is no direct correspondence between HWCAP bitmasks and this |
101 | bitmask. */ |
102 | uint32_t _dl_hwcaps_subdirs_active (void) attribute_hidden; |
103 | |
104 | /* Returns a bitmask that marks the last ACTIVE subdirectories in a |
105 | _dl_hwcaps_subdirs_active string (containing SUBDIRS directories in |
106 | total) as active. Intended for use in _dl_hwcaps_subdirs_active |
107 | implementations (if a contiguous tail of the list in |
108 | _dl_hwcaps_subdirs is selected). */ |
109 | static inline uint32_t |
110 | _dl_hwcaps_subdirs_build_bitmask (int subdirs, int active) |
111 | { |
112 | /* Leading subdirectories that are not active. */ |
113 | int inactive = subdirs - active; |
114 | if (inactive == 32) |
115 | return 0; |
116 | |
117 | uint32_t mask; |
118 | if (subdirs != 32) |
119 | mask = (1U << subdirs) - 1; |
120 | else |
121 | mask = -1; |
122 | return mask ^ ((1U << inactive) - 1); |
123 | } |
124 | |
125 | /* Pre-computed glibc-hwcaps subdirectory priorities. Used in |
126 | dl-cache.c to quickly find the proprieties for the stored HWCAP |
127 | names. */ |
128 | struct dl_hwcaps_priority |
129 | { |
130 | /* The name consists of name_length bytes at name (not necessarily |
131 | null-terminated). */ |
132 | const char *name; |
133 | uint32_t name_length; |
134 | |
135 | /* Priority of this name. A positive number. */ |
136 | uint32_t priority; |
137 | }; |
138 | |
139 | /* Pre-computed hwcaps priorities. Set up by |
140 | _dl_important_hwcaps. */ |
141 | extern struct dl_hwcaps_priority *_dl_hwcaps_priorities attribute_hidden; |
142 | extern uint32_t _dl_hwcaps_priorities_length attribute_hidden; |
143 | |
144 | #endif /* _DL_HWCAPS_H */ |
145 | |