| 1 |
/* PAFlib hardware capability handling. |
| 2 |
* |
| 3 |
* Copyright IBM Corp. 2013 |
| 4 |
* |
| 5 |
* The MIT License (MIT) |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in |
| 15 |
* all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 |
* THE SOFTWARE. |
| 24 |
* |
| 25 |
* Contributors: |
| 26 |
* IBM Corporation, Adhemerval Zanella - Initial implementation. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <string.h> |
| 30 |
#include <fcntl.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <link.h> |
| 34 |
#include <sys/stat.h> |
| 35 |
|
| 36 |
#include "paf-hwcap.h" |
| 37 |
|
| 38 |
#ifndef AT_HWCAP2 |
| 39 |
# define AT_HWCAP2 26 |
| 40 |
#endif |
| 41 |
|
| 42 |
/* Although glibc 2.16+ provides getauxval, only 2.18+ provides access |
| 43 |
* to AT_HWCAP2. To avoid rely on glibc version to correctly discover if |
| 44 |
* the kernel supports EBB, parse the hardware capabilities bits using the |
| 45 |
* either the /proc/self/auxv or the environment directly (__USE_ENVIRON). */ |
| 46 |
|
| 47 |
/*#define __USE_ENVIRON*/ |
| 48 |
|
| 49 |
typedef void (*auxv_callback)(ElfW(auxv_t) *, void *); |
| 50 |
|
| 51 |
static inline int |
| 52 |
__paf_walk_auxv (auxv_callback callback, void *arg) |
| 53 |
{ |
| 54 |
ElfW(auxv_t) *auxv; |
| 55 |
int i; |
| 56 |
|
| 57 |
#ifdef __USE_ENVIRON |
| 58 |
char **environ; |
| 59 |
|
| 60 |
for (environ = __environ; *environ != NULL; ++environ) |
| 61 |
{ |
| 62 |
} |
| 63 |
auxv = (ElfW (auxv_t) *) ++environ; |
| 64 |
#else |
| 65 |
int auxv_f; |
| 66 |
const size_t page_size = getpagesize(); |
| 67 |
ssize_t bytes; |
| 68 |
|
| 69 |
auxv_f = open ("/proc/self/auxv", O_RDONLY); |
| 70 |
if (auxv_f == -1) |
| 71 |
return 1; |
| 72 |
auxv = (ElfW(auxv_t)*) malloc (page_size); |
| 73 |
if (auxv == NULL) |
| 74 |
return 1; |
| 75 |
bytes = read (auxv_f, (void*)auxv, page_size); |
| 76 |
close (auxv_f); |
| 77 |
if (bytes <= 0) |
| 78 |
{ |
| 79 |
free (auxv); |
| 80 |
return 1; |
| 81 |
} |
| 82 |
#endif |
| 83 |
|
| 84 |
for (i=0; auxv[i].a_type != AT_NULL; ++i) |
| 85 |
callback (&auxv[i], arg); |
| 86 |
|
| 87 |
#ifndef __USE_ENVIRON |
| 88 |
free (auxv); |
| 89 |
#endif |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
static void |
| 96 |
__paf_get_hwcap_callback (ElfW(auxv_t) *auxv, void *arg) |
| 97 |
{ |
| 98 |
struct hwcap_t *hwcap = (struct hwcap_t*)(arg); |
| 99 |
if (auxv->a_type == AT_HWCAP) |
| 100 |
hwcap->hwcap1 = auxv->a_un.a_val; |
| 101 |
else if (auxv->a_type == AT_HWCAP2) |
| 102 |
hwcap->hwcap2 = auxv->a_un.a_val; |
| 103 |
else if (auxv->a_type == AT_BASE_PLATFORM) |
| 104 |
strncpy (hwcap->platform, (const char*)(auxv->a_un.a_val), PAFPLATLEN); |
| 105 |
} |
| 106 |
|
| 107 |
int |
| 108 |
attribute_hidden |
| 109 |
__paf_get_hwcap (struct hwcap_t *hwcap) |
| 110 |
{ |
| 111 |
if (__paf_walk_auxv (__paf_get_hwcap_callback, (void*)(hwcap)) != 0) |
| 112 |
return 1; |
| 113 |
return 0; |
| 114 |
} |