1 | /* Initialize cpu feature data. PowerPC version. |
2 | Copyright (C) 2017-2024 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 | #include <array_length.h> |
20 | #include <stdint.h> |
21 | #include <cpu-features.h> |
22 | #include <elf/dl-tunables.h> |
23 | #include <dl-tunables-parse.h> |
24 | #include <unistd.h> |
25 | #include <string.h> |
26 | |
27 | static void |
28 | TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp) |
29 | { |
30 | /* The current IFUNC selection is always using the most recent |
31 | features which are available via AT_HWCAP or AT_HWCAP2. But in |
32 | some scenarios it is useful to adjust this selection. |
33 | |
34 | The environment variable: |
35 | |
36 | GLIBC_TUNABLES=glibc.cpu.hwcaps=-xxx,yyy,.... |
37 | |
38 | Can be used to enable HWCAP/HWCAP2 feature yyy, disable HWCAP/HWCAP2 |
39 | feature xxx, where the feature name is case-sensitive and has to match |
40 | the ones mentioned in the file{sysdeps/powerpc/dl-procinfo.c}. */ |
41 | |
42 | /* Copy the features from dl_powerpc_cpu_features, which contains the |
43 | features provided by AT_HWCAP and AT_HWCAP2. */ |
44 | struct cpu_features *cpu_features = &GLRO(dl_powerpc_cpu_features); |
45 | unsigned long int tcbv_hwcap = cpu_features->hwcap; |
46 | unsigned long int tcbv_hwcap2 = cpu_features->hwcap2; |
47 | |
48 | struct tunable_str_comma_state_t ts; |
49 | tunable_str_comma_init (state: &ts, valp); |
50 | |
51 | struct tunable_str_comma_t t; |
52 | while (tunable_str_comma_next (state: &ts, str: &t)) |
53 | { |
54 | if (t.len == 0) |
55 | continue; |
56 | |
57 | size_t offset = 0; |
58 | for (int i = 0; i < array_length (hwcap_tunables); ++i) |
59 | { |
60 | const char *hwcap_name = hwcap_names + offset; |
61 | size_t hwcap_name_len = strlen (hwcap_name); |
62 | /* Check the tunable name on the supported list. */ |
63 | if (tunable_str_comma_strcmp (&t, hwcap_name, hwcap_name_len)) |
64 | { |
65 | /* Update the hwcap and hwcap2 bits. */ |
66 | if (t.disable) |
67 | { |
68 | /* Id is 1 for hwcap2 tunable. */ |
69 | if (hwcap_tunables[i].id) |
70 | cpu_features->hwcap2 &= ~(hwcap_tunables[i].mask); |
71 | else |
72 | cpu_features->hwcap &= ~(hwcap_tunables[i].mask); |
73 | } |
74 | else |
75 | { |
76 | /* Enable the features and also check that no unsupported |
77 | features were enabled by user. */ |
78 | if (hwcap_tunables[i].id) |
79 | cpu_features->hwcap2 |= (tcbv_hwcap2 & hwcap_tunables[i].mask); |
80 | else |
81 | cpu_features->hwcap |= (tcbv_hwcap & hwcap_tunables[i].mask); |
82 | } |
83 | break; |
84 | } |
85 | offset += hwcap_name_len + 1; |
86 | } |
87 | } |
88 | } |
89 | |
90 | static inline void |
91 | init_cpu_features (struct cpu_features *cpu_features, uint64_t hwcaps[]) |
92 | { |
93 | /* Fill the cpu_features with the supported hwcaps |
94 | which are set by __tcb_parse_hwcap_and_convert_at_platform. */ |
95 | cpu_features->hwcap = hwcaps[0]; |
96 | cpu_features->hwcap2 = hwcaps[1]; |
97 | cpu_features->hwcap3 = hwcaps[2]; |
98 | cpu_features->hwcap4 = hwcaps[3]; |
99 | /* Default is to use aligned memory access on optimized function unless |
100 | tunables is enable, since for this case user can explicit disable |
101 | unaligned optimizations. */ |
102 | int32_t cached_memfunc = TUNABLE_GET (glibc, cpu, cached_memopt, int32_t, |
103 | NULL); |
104 | cpu_features->use_cached_memopt = (cached_memfunc > 0); |
105 | TUNABLE_GET (glibc, cpu, hwcaps, tunable_val_t *, |
106 | TUNABLE_CALLBACK (set_hwcaps)); |
107 | } |
108 | |