Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 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 | |
Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
| 26 | |
| 27 | #ifdef CONFIG_GETAUXVAL |
| 28 | /* Don't inline this in qemu/osdep.h, because pulling in <sys/auxv.h> for |
| 29 | the system declaration of getauxval pulls in the system <elf.h>, which |
| 30 | conflicts with qemu's version. */ |
| 31 | |
| 32 | #include <sys/auxv.h> |
| 33 | |
| 34 | unsigned long qemu_getauxval(unsigned long key) |
| 35 | { |
| 36 | return getauxval(key); |
| 37 | } |
| 38 | #elif defined(__linux__) |
| 39 | #include "elf.h" |
| 40 | |
| 41 | /* Our elf.h doesn't contain Elf32_auxv_t and Elf64_auxv_t, which is ok because |
| 42 | that just makes it easier to define it properly for the host here. */ |
| 43 | typedef struct { |
| 44 | unsigned long a_type; |
| 45 | unsigned long a_val; |
| 46 | } ElfW_auxv_t; |
| 47 | |
| 48 | static const ElfW_auxv_t *auxv; |
| 49 | |
Richard Henderson | 2b45c3f | 2014-04-30 13:49:11 -0700 | [diff] [blame] | 50 | static const ElfW_auxv_t *qemu_init_auxval(void) |
Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 51 | { |
Richard Henderson | 2b45c3f | 2014-04-30 13:49:11 -0700 | [diff] [blame] | 52 | ElfW_auxv_t *a; |
| 53 | ssize_t size = 512, r, ofs; |
| 54 | int fd; |
| 55 | |
| 56 | /* Allocate some initial storage. Make sure the first entry is set |
| 57 | to end-of-list, so that we've got a valid list in case of error. */ |
| 58 | auxv = a = g_malloc(size); |
| 59 | a[0].a_type = 0; |
| 60 | a[0].a_val = 0; |
| 61 | |
| 62 | fd = open("/proc/self/auxv", O_RDONLY); |
| 63 | if (fd < 0) { |
| 64 | return a; |
Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 65 | } |
Richard Henderson | 2b45c3f | 2014-04-30 13:49:11 -0700 | [diff] [blame] | 66 | |
| 67 | /* Read the first SIZE bytes. Hopefully, this covers everything. */ |
| 68 | r = read(fd, a, size); |
| 69 | |
| 70 | if (r == size) { |
| 71 | /* Continue to expand until we do get a partial read. */ |
| 72 | do { |
| 73 | ofs = size; |
| 74 | size *= 2; |
| 75 | auxv = a = g_realloc(a, size); |
| 76 | r = read(fd, (char *)a + ofs, ofs); |
| 77 | } while (r == ofs); |
| 78 | } |
| 79 | |
| 80 | close(fd); |
| 81 | return a; |
Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | unsigned long qemu_getauxval(unsigned long type) |
| 85 | { |
Richard Henderson | 2b45c3f | 2014-04-30 13:49:11 -0700 | [diff] [blame] | 86 | const ElfW_auxv_t *a = auxv; |
| 87 | |
| 88 | if (unlikely(a == NULL)) { |
| 89 | a = qemu_init_auxval(); |
| 90 | } |
| 91 | |
| 92 | for (; a->a_type != 0; a++) { |
| 93 | if (a->a_type == type) { |
| 94 | return a->a_val; |
Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Vivian Wang | 22531d8 | 2024-07-23 18:05:44 +0800 | [diff] [blame] | 98 | errno = ENOENT; |
Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
Fam Zheng | f6e0830 | 2014-09-03 11:44:55 +0800 | [diff] [blame] | 101 | |
Brad Smith | 27fca0a | 2024-07-27 23:58:55 -0400 | [diff] [blame] | 102 | #elif defined(CONFIG_ELF_AUX_INFO) |
David CARLIER | ae2b720 | 2020-06-12 09:46:06 +0100 | [diff] [blame] | 103 | #include <sys/auxv.h> |
| 104 | |
| 105 | unsigned long qemu_getauxval(unsigned long type) |
| 106 | { |
| 107 | unsigned long aux = 0; |
Vivian Wang | 22531d8 | 2024-07-23 18:05:44 +0800 | [diff] [blame] | 108 | int ret = elf_aux_info(type, &aux, sizeof(aux)); |
| 109 | if (ret != 0) { |
| 110 | errno = ret; |
| 111 | } |
David CARLIER | ae2b720 | 2020-06-12 09:46:06 +0100 | [diff] [blame] | 112 | return aux; |
| 113 | } |
| 114 | |
Fam Zheng | f6e0830 | 2014-09-03 11:44:55 +0800 | [diff] [blame] | 115 | #else |
| 116 | |
| 117 | unsigned long qemu_getauxval(unsigned long type) |
| 118 | { |
Vivian Wang | 22531d8 | 2024-07-23 18:05:44 +0800 | [diff] [blame] | 119 | errno = ENOSYS; |
Fam Zheng | f6e0830 | 2014-09-03 11:44:55 +0800 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Richard Henderson | b6a3e69 | 2013-06-04 11:24:49 -0700 | [diff] [blame] | 123 | #endif |