1 /* 2 * QEMU access to the auxiliary vector 3 * 4 * Copyright (C) 2013 Red Hat, Inc 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu-common.h" 26 #include "qemu/osdep.h" 27 28 #ifdef CONFIG_GETAUXVAL 29 /* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for 30 the system declaration of getauxval pulls in the system <elf.h>, which 31 conflicts with qemu's version. */ 32 33 #include <sys/auxv.h> 34 35 unsigned long qemu_getauxval(unsigned long key) 36 { 37 return getauxval(key); 38 } 39 #elif defined(__linux__) 40 #include "elf.h" 41 42 /* Our elf.h doesn't contain Elf32_auxv_t and Elf64_auxv_t, which is ok because 43 that just makes it easier to define it properly for the host here. */ 44 typedef struct { 45 unsigned long a_type; 46 unsigned long a_val; 47 } ElfW_auxv_t; 48 49 static const ElfW_auxv_t *auxv; 50 51 static const ElfW_auxv_t *qemu_init_auxval(void) 52 { 53 ElfW_auxv_t *a; 54 ssize_t size = 512, r, ofs; 55 int fd; 56 57 /* Allocate some initial storage. Make sure the first entry is set 58 to end-of-list, so that we've got a valid list in case of error. */ 59 auxv = a = g_malloc(size); 60 a[0].a_type = 0; 61 a[0].a_val = 0; 62 63 fd = open("/proc/self/auxv", O_RDONLY); 64 if (fd < 0) { 65 return a; 66 } 67 68 /* Read the first SIZE bytes. Hopefully, this covers everything. */ 69 r = read(fd, a, size); 70 71 if (r == size) { 72 /* Continue to expand until we do get a partial read. */ 73 do { 74 ofs = size; 75 size *= 2; 76 auxv = a = g_realloc(a, size); 77 r = read(fd, (char *)a + ofs, ofs); 78 } while (r == ofs); 79 } 80 81 close(fd); 82 return a; 83 } 84 85 unsigned long qemu_getauxval(unsigned long type) 86 { 87 const ElfW_auxv_t *a = auxv; 88 89 if (unlikely(a == NULL)) { 90 a = qemu_init_auxval(); 91 } 92 93 for (; a->a_type != 0; a++) { 94 if (a->a_type == type) { 95 return a->a_val; 96 } 97 } 98 99 return 0; 100 } 101 102 #else 103 104 unsigned long qemu_getauxval(unsigned long type) 105 { 106 return 0; 107 } 108 109 #endif 110