diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/Makefile.objs | 1 | ||||
-rw-r--r-- | util/cache-utils.c | 51 | ||||
-rw-r--r-- | util/getauxval.c | 74 |
3 files changed, 95 insertions, 31 deletions
diff --git a/util/Makefile.objs b/util/Makefile.objs index 2bb13a2a59..af3e5cb157 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -12,3 +12,4 @@ util-obj-y += qemu-option.o qemu-progress.o util-obj-y += hexdump.o util-obj-y += crc32c.o util-obj-y += throttle.o +util-obj-y += getauxval.o diff --git a/util/cache-utils.c b/util/cache-utils.c index b94013a8cb..047003008b 100644 --- a/util/cache-utils.c +++ b/util/cache-utils.c @@ -1,3 +1,4 @@ +#include "qemu-common.h" #include "qemu/cache-utils.h" #if defined(_ARCH_PPC) @@ -9,31 +10,33 @@ struct qemu_cache_conf qemu_cache_conf = { #if defined _AIX #include <sys/systemcfg.h> -static void ppc_init_cacheline_sizes(void) +void qemu_cache_utils_init(void) { qemu_cache_conf.icache_bsize = _system_configuration.icache_line; qemu_cache_conf.dcache_bsize = _system_configuration.dcache_line; } #elif defined __linux__ +#include "qemu/osdep.h" +#include "elf.h" -#define QEMU_AT_NULL 0 -#define QEMU_AT_DCACHEBSIZE 19 -#define QEMU_AT_ICACHEBSIZE 20 - -static void ppc_init_cacheline_sizes(char **envp) +void qemu_cache_utils_init(void) { - unsigned long *auxv; - - while (*envp++); + unsigned long dsize = qemu_getauxval(AT_DCACHEBSIZE); + unsigned long isize = qemu_getauxval(AT_ICACHEBSIZE); - for (auxv = (unsigned long *) envp; *auxv != QEMU_AT_NULL; auxv += 2) { - switch (*auxv) { - case QEMU_AT_DCACHEBSIZE: qemu_cache_conf.dcache_bsize = auxv[1]; break; - case QEMU_AT_ICACHEBSIZE: qemu_cache_conf.icache_bsize = auxv[1]; break; - default: break; + if (dsize == 0 || isize == 0) { + if (dsize == 0) { + fprintf(stderr, "getauxval AT_DCACHEBSIZE failed\n"); + } + if (isize == 0) { + fprintf(stderr, "getauxval AT_ICACHEBSIZE failed\n"); } + exit(1); + } + qemu_cache_conf.dcache_bsize = dsize; + qemu_cache_conf.icache_bsize = isize; } #elif defined __APPLE__ @@ -41,7 +44,7 @@ static void ppc_init_cacheline_sizes(char **envp) #include <sys/types.h> #include <sys/sysctl.h> -static void ppc_init_cacheline_sizes(void) +void qemu_cache_utils_init(void) { size_t len; unsigned cacheline; @@ -55,9 +58,8 @@ static void ppc_init_cacheline_sizes(void) qemu_cache_conf.icache_bsize = cacheline; } } -#endif -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -65,7 +67,7 @@ static void ppc_init_cacheline_sizes(void) #include <sys/types.h> #include <sys/sysctl.h> -static void ppc_init_cacheline_sizes(void) +void qemu_cache_utils_init(void) { size_t len = 4; unsigned cacheline; @@ -81,17 +83,4 @@ static void ppc_init_cacheline_sizes(void) } #endif -#ifdef __linux__ -void qemu_cache_utils_init(char **envp) -{ - ppc_init_cacheline_sizes(envp); -} -#else -void qemu_cache_utils_init(char **envp) -{ - (void) envp; - ppc_init_cacheline_sizes(); -} -#endif - #endif /* _ARCH_PPC */ diff --git a/util/getauxval.c b/util/getauxval.c new file mode 100644 index 0000000000..476c883b32 --- /dev/null +++ b/util/getauxval.c @@ -0,0 +1,74 @@ +/* + * QEMU access to the auxiliary vector + * + * Copyright (C) 2013 Red Hat, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "qemu-common.h" +#include "qemu/osdep.h" + +#ifdef CONFIG_GETAUXVAL +/* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for + the system declaration of getauxval pulls in the system <elf.h>, which + conflicts with qemu's version. */ + +#include <sys/auxv.h> + +unsigned long qemu_getauxval(unsigned long key) +{ + return getauxval(key); +} +#elif defined(__linux__) +#include "elf.h" + +/* Our elf.h doesn't contain Elf32_auxv_t and Elf64_auxv_t, which is ok because + that just makes it easier to define it properly for the host here. */ +typedef struct { + unsigned long a_type; + unsigned long a_val; +} ElfW_auxv_t; + +static const ElfW_auxv_t *auxv; + +void qemu_init_auxval(char **envp) +{ + /* The auxiliary vector is located just beyond the initial environment. */ + while (*envp++ != NULL) { + continue; + } + auxv = (const ElfW_auxv_t *)envp; +} + +unsigned long qemu_getauxval(unsigned long type) +{ + /* If we were able to find the auxiliary vector, use it. */ + if (auxv) { + const ElfW_auxv_t *a; + for (a = auxv; a->a_type != 0; a++) { + if (a->a_type == type) { + return a->a_val; + } + } + } + + return 0; +} +#endif |