]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/uname.c
linux-user: move uname functions to uname.c
[mirror_qemu.git] / linux-user / uname.c
1 /*
2 * cpu to uname machine name map
3 *
4 * Copyright (c) 2009 Loïc Minier
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
20 #include <stdio.h>
21
22 #include "qemu.h"
23 //#include "qemu-common.h"
24 #include "uname.h"
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 {
33 #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
34
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()
39 */
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;
55 #elif defined(TARGET_X86_64)
56 return "x86-64";
57 #elif defined(TARGET_I386)
58 /* see arch/x86/kernel/cpu/bugs.c: check_bugs(), 386, 486, 586, 686 */
59 CPUState *cpu = ENV_GET_CPU((CPUX86State *)cpu_env);
60 int family = object_property_get_int(OBJECT(cpu), "family", NULL);
61 if (family == 4) {
62 return "i486";
63 }
64 if (family == 5) {
65 return "i586";
66 }
67 return "i686";
68 #else
69 /* default is #define-d in each arch/ subdir */
70 return UNAME_MACHINE;
71 #endif
72 }
73
74
75 #define COPY_UTSNAME_FIELD(dest, src) \
76 do { \
77 /* __NEW_UTS_LEN doesn't include terminating null */ \
78 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
79 (dest)[__NEW_UTS_LEN] = '\0'; \
80 } while (0)
81
82 int sys_uname(struct new_utsname *buf)
83 {
84 struct utsname uts_buf;
85
86 if (uname(&uts_buf) < 0)
87 return (-1);
88
89 /*
90 * Just in case these have some differences, we
91 * translate utsname to new_utsname (which is the
92 * struct linux kernel uses).
93 */
94
95 memset(buf, 0, sizeof(*buf));
96 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
97 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
98 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
99 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
100 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
101 #ifdef _GNU_SOURCE
102 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
103 #endif
104 return (0);
105
106 #undef COPY_UTSNAME_FIELD
107 }
108
109 static int relstr_to_int(const char *s)
110 {
111 /* Convert a uname release string like "2.6.18" to an integer
112 * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
113 */
114 int i, n, tmp;
115
116 tmp = 0;
117 for (i = 0; i < 3; i++) {
118 n = 0;
119 while (*s >= '0' && *s <= '9') {
120 n *= 10;
121 n += *s - '0';
122 s++;
123 }
124 tmp = (tmp << 8) + n;
125 if (*s == '.') {
126 s++;
127 }
128 }
129 return tmp;
130 }
131
132 int get_osversion(void)
133 {
134 static int osversion;
135 struct new_utsname buf;
136 const char *s;
137
138 if (osversion)
139 return osversion;
140 if (qemu_uname_release && *qemu_uname_release) {
141 s = qemu_uname_release;
142 } else {
143 if (sys_uname(&buf))
144 return 0;
145 s = buf.release;
146 }
147 osversion = relstr_to_int(s);
148 return osversion;
149 }
150
151 void init_qemu_uname_release(void)
152 {
153 /* Initialize qemu_uname_release for later use.
154 * If the host kernel is too old and the user hasn't asked for
155 * a specific fake version number, we might want to fake a minimum
156 * target kernel version.
157 */
158 #ifdef UNAME_MINIMUM_RELEASE
159 struct new_utsname buf;
160
161 if (qemu_uname_release && *qemu_uname_release) {
162 return;
163 }
164
165 if (sys_uname(&buf)) {
166 return;
167 }
168
169 if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
170 qemu_uname_release = UNAME_MINIMUM_RELEASE;
171 }
172 #endif
173 }