1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#![cfg_attr(not(target_arch = "aarch64"), allow(dead_code))]
use mem;
use prelude::v1::*;
use fs::File;
use io::Read;
pub(crate) const AT_HWCAP: usize = 16;
#[cfg(any(target_arch = "arm", target_arch = "powerpc64"))]
pub(crate) const AT_HWCAP2: usize = 26;
#[derive(Debug, Copy, Clone)]
pub(crate) struct AuxVec {
pub hwcap: usize,
#[cfg(any(target_arch = "arm", target_arch = "powerpc64"))]
pub hwcap2: usize,
}
pub(crate) fn auxv() -> Result<AuxVec, ()> {
if let Ok(hwcap) = getauxval(AT_HWCAP) {
#[cfg(any(target_arch = "aarch64", target_arch = "mips",
target_arch = "mips64"))]
{
if hwcap != 0 {
return Ok(AuxVec { hwcap });
}
}
#[cfg(any(target_arch = "arm", target_arch = "powerpc64"))]
{
if let Ok(hwcap2) = getauxval(AT_HWCAP2) {
if hwcap != 0 && hwcap2 != 0 {
return Ok(AuxVec { hwcap, hwcap2 });
}
}
}
drop(hwcap);
}
auxv_from_file("/proc/self/auxv")
}
fn getauxval(key: usize) -> Result<usize, ()> {
use libc;
pub type F = unsafe extern "C" fn(usize) -> usize;
unsafe {
let ptr = libc::dlsym(
libc::RTLD_DEFAULT,
"getauxval\0".as_ptr() as *const _,
);
if ptr.is_null() {
return Err(());
}
let ffi_getauxval: F = mem::transmute(ptr);
Ok(ffi_getauxval(key))
}
}
fn auxv_from_file(file: &str) -> Result<AuxVec, ()> {
let mut file = File::open(file).map_err(|_| ())?;
let mut buf = [0_usize; 64];
{
let raw: &mut [u8; 64 * mem::size_of::<usize>()] =
unsafe { mem::transmute(&mut buf) };
file.read(raw).map_err(|_| ())?;
}
auxv_from_buf(&buf)
}
fn auxv_from_buf(buf: &[usize; 64]) -> Result<AuxVec, ()> {
#[cfg(any(target_arch = "aarch64", target_arch = "mips",
target_arch = "mips64"))]
{
for el in buf.chunks(2) {
match el[0] {
AT_HWCAP => return Ok(AuxVec { hwcap: el[1] }),
_ => (),
}
}
}
#[cfg(any(target_arch = "arm", target_arch = "powerpc64"))]
{
let mut hwcap = None;
let mut hwcap2 = None;
for el in buf.chunks(2) {
match el[0] {
AT_HWCAP => hwcap = Some(el[1]),
AT_HWCAP2 => hwcap2 = Some(el[1]),
_ => (),
}
}
if let (Some(hwcap), Some(hwcap2)) = (hwcap, hwcap2) {
return Ok(AuxVec { hwcap, hwcap2 });
}
}
drop(buf);
Err(())
}
#[cfg(test)]
mod tests {
extern crate auxv as auxv_crate;
use super::*;
fn auxv_crate_getprocfs(key: usize) -> Option<usize> {
use self::auxv_crate::AuxvType;
use self::auxv_crate::procfs::search_procfs_auxv;
let k = key as AuxvType;
match search_procfs_auxv(&[k]) {
Ok(v) => Some(v[&k] as usize),
Err(_) => None,
}
}
#[cfg(not(any(target_arch = "mips", target_arch = "mips64")))]
fn auxv_crate_getauxval(key: usize) -> Option<usize> {
use self::auxv_crate::AuxvType;
use self::auxv_crate::getauxval::Getauxval;
let q = auxv_crate::getauxval::NativeGetauxval {};
match q.getauxval(key as AuxvType) {
Ok(v) => Some(v as usize),
Err(_) => None,
}
}
#[cfg(not(any(target_arch = "mips", target_arch = "mips64", target_arch = "powerpc")))]
#[test]
fn auxv_crate() {
let v = auxv();
if let Some(hwcap) = auxv_crate_getauxval(AT_HWCAP) {
let rt_hwcap = v.expect("failed to find hwcap key").hwcap;
assert_eq!(rt_hwcap, hwcap);
}
#[cfg(any(target_arch = "arm", target_arch = "powerpc64"))]
{
if let Some(hwcap2) = auxv_crate_getauxval(AT_HWCAP2) {
let rt_hwcap2 = v.expect("failed to find hwcap2 key").hwcap2;
assert_eq!(rt_hwcap2, hwcap2);
}
}
}
#[test]
fn auxv_dump() {
if let Ok(auxvec) = auxv() {
println!("{:?}", auxvec);
} else {
println!("both getauxval() and reading /proc/self/auxv failed!");
}
}
cfg_if! {
if #[cfg(target_arch = "arm")] {
#[test]
fn linux_rpi3() {
let v = auxv_from_file(
"../../stdsimd/arch/detect/test_data/linux-rpi3.auxv",
).unwrap();
assert_eq!(v.hwcap, 4174038);
assert_eq!(v.hwcap2, 16);
}
#[test]
#[should_panic]
fn linux_macos_vb() {
let _ = auxv_from_file(
"../../stdsimd/arch/detect/test_data/macos-virtualbox-linux-x86-4850HQ.auxv"
).unwrap();
}
} else if #[cfg(target_arch = "aarch64")] {
#[test]
fn linux_x64() {
let v = auxv_from_file(
"../../stdsimd/arch/detect/test_data/linux-x64-i7-6850k.auxv",
).unwrap();
assert_eq!(v.hwcap, 3219913727);
}
}
}
#[test]
fn auxv_dump_procfs() {
if let Ok(auxvec) = auxv_from_file("/proc/self/auxv") {
println!("{:?}", auxvec);
} else {
println!("reading /proc/self/auxv failed!");
}
}
#[test]
fn auxv_crate_procfs() {
let v = auxv();
if let Some(hwcap) = auxv_crate_getprocfs(AT_HWCAP) {
assert_eq!(v.unwrap().hwcap, hwcap);
}
#[cfg(any(target_arch = "arm", target_arch = "powerpc64"))]
{
if let Some(hwcap2) = auxv_crate_getprocfs(AT_HWCAP2) {
assert_eq!(v.unwrap().hwcap2, hwcap2);
}
}
}
}