Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * cpu to uname machine name map |
| 3 | * |
Peter Maydell | 7ff60e1 | 2011-12-02 19:30:44 +0000 | [diff] [blame] | 4 | * Copyright (c) 2009 Loïc Minier |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Peter Maydell | d39594e | 2016-01-26 18:17:02 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 21 | |
| 22 | #include "qemu.h" |
| 23 | //#include "qemu-common.h" |
Riku Voipio | 18cb008 | 2014-02-19 12:59:58 +0200 | [diff] [blame] | 24 | #include "uname.h" |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 25 | |
| 26 | /* return highest utsname machine name for emulated instruction set |
| 27 | * |
| 28 | * NB: the default emulated CPU ("any") might not match any existing CPU, e.g. |
| 29 | * on ARM it has all features turned on, so there is no perfect arch string to |
| 30 | * return here */ |
| 31 | const char *cpu_to_uname_machine(void *cpu_env) |
| 32 | { |
Alexander Graf | 067d983 | 2013-09-03 20:12:12 +0100 | [diff] [blame] | 33 | #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) |
| 34 | |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 35 | /* utsname machine name on linux arm is CPU arch name + endianness, e.g. |
| 36 | * armv7l; to get a list of CPU arch names from the linux source, use: |
| 37 | * grep arch_name: -A1 linux/arch/arm/mm/proc-*.S |
| 38 | * see arch/arm/kernel/setup.c: setup_processor() |
Peter Maydell | b2d06f9 | 2012-06-20 11:57:23 +0000 | [diff] [blame] | 39 | */ |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 40 | |
| 41 | /* in theory, endianness is configurable on some ARM CPUs, but this isn't |
| 42 | * used in user mode emulation */ |
| 43 | #ifdef TARGET_WORDS_BIGENDIAN |
| 44 | #define utsname_suffix "b" |
| 45 | #else |
| 46 | #define utsname_suffix "l" |
| 47 | #endif |
| 48 | if (arm_feature(cpu_env, ARM_FEATURE_V7)) |
| 49 | return "armv7" utsname_suffix; |
| 50 | if (arm_feature(cpu_env, ARM_FEATURE_V6)) |
| 51 | return "armv6" utsname_suffix; |
| 52 | /* earliest emulated CPU is ARMv5TE; qemu can emulate the 1026, but not its |
| 53 | * Jazelle support */ |
| 54 | return "armv5te" utsname_suffix; |
Peter Maydell | 4d13be8 | 2014-05-10 12:25:53 +0100 | [diff] [blame] | 55 | #elif defined(TARGET_I386) && !defined(TARGET_X86_64) |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 56 | /* see arch/x86/kernel/cpu/bugs.c: check_bugs(), 386, 486, 586, 686 */ |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 57 | CPUState *cpu = env_cpu((CPUX86State *)cpu_env); |
Andreas Färber | 6f152e9 | 2012-05-18 00:01:58 +0200 | [diff] [blame] | 58 | int family = object_property_get_int(OBJECT(cpu), "family", NULL); |
| 59 | if (family == 4) { |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 60 | return "i486"; |
Andreas Färber | 6f152e9 | 2012-05-18 00:01:58 +0200 | [diff] [blame] | 61 | } |
| 62 | if (family == 5) { |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 63 | return "i586"; |
Andreas Färber | 6f152e9 | 2012-05-18 00:01:58 +0200 | [diff] [blame] | 64 | } |
Loïc Minier | da79030 | 2009-12-29 22:06:13 +0100 | [diff] [blame] | 65 | return "i686"; |
| 66 | #else |
| 67 | /* default is #define-d in each arch/ subdir */ |
| 68 | return UNAME_MACHINE; |
| 69 | #endif |
| 70 | } |
Riku Voipio | 6d30db1 | 2014-02-19 15:35:35 +0200 | [diff] [blame] | 71 | |
| 72 | |
| 73 | #define COPY_UTSNAME_FIELD(dest, src) \ |
| 74 | do { \ |
Daniel P. Berrangé | b2acfb5 | 2019-05-01 15:46:46 +0100 | [diff] [blame] | 75 | memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \ |
| 76 | (dest)[sizeof(dest) - 1] = '\0'; \ |
Riku Voipio | 6d30db1 | 2014-02-19 15:35:35 +0200 | [diff] [blame] | 77 | } while (0) |
| 78 | |
| 79 | int sys_uname(struct new_utsname *buf) |
| 80 | { |
| 81 | struct utsname uts_buf; |
| 82 | |
| 83 | if (uname(&uts_buf) < 0) |
| 84 | return (-1); |
| 85 | |
| 86 | /* |
| 87 | * Just in case these have some differences, we |
| 88 | * translate utsname to new_utsname (which is the |
| 89 | * struct linux kernel uses). |
| 90 | */ |
| 91 | |
| 92 | memset(buf, 0, sizeof(*buf)); |
| 93 | COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname); |
| 94 | COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename); |
| 95 | COPY_UTSNAME_FIELD(buf->release, uts_buf.release); |
| 96 | COPY_UTSNAME_FIELD(buf->version, uts_buf.version); |
| 97 | COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine); |
| 98 | #ifdef _GNU_SOURCE |
| 99 | COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname); |
| 100 | #endif |
| 101 | return (0); |
| 102 | |
| 103 | #undef COPY_UTSNAME_FIELD |
| 104 | } |
| 105 | |
| 106 | static int relstr_to_int(const char *s) |
| 107 | { |
| 108 | /* Convert a uname release string like "2.6.18" to an integer |
| 109 | * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.) |
| 110 | */ |
| 111 | int i, n, tmp; |
| 112 | |
| 113 | tmp = 0; |
| 114 | for (i = 0; i < 3; i++) { |
| 115 | n = 0; |
| 116 | while (*s >= '0' && *s <= '9') { |
| 117 | n *= 10; |
| 118 | n += *s - '0'; |
| 119 | s++; |
| 120 | } |
| 121 | tmp = (tmp << 8) + n; |
| 122 | if (*s == '.') { |
| 123 | s++; |
| 124 | } |
| 125 | } |
| 126 | return tmp; |
| 127 | } |
| 128 | |
| 129 | int get_osversion(void) |
| 130 | { |
| 131 | static int osversion; |
| 132 | struct new_utsname buf; |
| 133 | const char *s; |
| 134 | |
| 135 | if (osversion) |
| 136 | return osversion; |
| 137 | if (qemu_uname_release && *qemu_uname_release) { |
| 138 | s = qemu_uname_release; |
| 139 | } else { |
| 140 | if (sys_uname(&buf)) |
| 141 | return 0; |
| 142 | s = buf.release; |
| 143 | } |
| 144 | osversion = relstr_to_int(s); |
| 145 | return osversion; |
| 146 | } |
| 147 | |
| 148 | void init_qemu_uname_release(void) |
| 149 | { |
| 150 | /* Initialize qemu_uname_release for later use. |
| 151 | * If the host kernel is too old and the user hasn't asked for |
| 152 | * a specific fake version number, we might want to fake a minimum |
| 153 | * target kernel version. |
| 154 | */ |
Riku Voipio | 6d30db1 | 2014-02-19 15:35:35 +0200 | [diff] [blame] | 155 | struct new_utsname buf; |
| 156 | |
| 157 | if (qemu_uname_release && *qemu_uname_release) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (sys_uname(&buf)) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) { |
| 166 | qemu_uname_release = UNAME_MINIMUM_RELEASE; |
| 167 | } |
Riku Voipio | 6d30db1 | 2014-02-19 15:35:35 +0200 | [diff] [blame] | 168 | } |