uname.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <stdio.h>
  20. #include "qemu.h"
  21. //#include "qemu-common.h"
  22. #include "uname.h"
  23. /* return highest utsname machine name for emulated instruction set
  24. *
  25. * NB: the default emulated CPU ("any") might not match any existing CPU, e.g.
  26. * on ARM it has all features turned on, so there is no perfect arch string to
  27. * return here */
  28. const char *cpu_to_uname_machine(void *cpu_env)
  29. {
  30. #if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
  31. /* utsname machine name on linux arm is CPU arch name + endianness, e.g.
  32. * armv7l; to get a list of CPU arch names from the linux source, use:
  33. * grep arch_name: -A1 linux/arch/arm/mm/proc-*.S
  34. * see arch/arm/kernel/setup.c: setup_processor()
  35. */
  36. /* in theory, endianness is configurable on some ARM CPUs, but this isn't
  37. * used in user mode emulation */
  38. #ifdef TARGET_WORDS_BIGENDIAN
  39. #define utsname_suffix "b"
  40. #else
  41. #define utsname_suffix "l"
  42. #endif
  43. if (arm_feature(cpu_env, ARM_FEATURE_V7))
  44. return "armv7" utsname_suffix;
  45. if (arm_feature(cpu_env, ARM_FEATURE_V6))
  46. return "armv6" utsname_suffix;
  47. /* earliest emulated CPU is ARMv5TE; qemu can emulate the 1026, but not its
  48. * Jazelle support */
  49. return "armv5te" utsname_suffix;
  50. #elif defined(TARGET_I386) && !defined(TARGET_X86_64)
  51. /* see arch/x86/kernel/cpu/bugs.c: check_bugs(), 386, 486, 586, 686 */
  52. CPUState *cpu = ENV_GET_CPU((CPUX86State *)cpu_env);
  53. int family = object_property_get_int(OBJECT(cpu), "family", NULL);
  54. if (family == 4) {
  55. return "i486";
  56. }
  57. if (family == 5) {
  58. return "i586";
  59. }
  60. return "i686";
  61. #else
  62. /* default is #define-d in each arch/ subdir */
  63. return UNAME_MACHINE;
  64. #endif
  65. }
  66. #define COPY_UTSNAME_FIELD(dest, src) \
  67. do { \
  68. /* __NEW_UTS_LEN doesn't include terminating null */ \
  69. (void) strncpy((dest), (src), __NEW_UTS_LEN); \
  70. (dest)[__NEW_UTS_LEN] = '\0'; \
  71. } while (0)
  72. int sys_uname(struct new_utsname *buf)
  73. {
  74. struct utsname uts_buf;
  75. if (uname(&uts_buf) < 0)
  76. return (-1);
  77. /*
  78. * Just in case these have some differences, we
  79. * translate utsname to new_utsname (which is the
  80. * struct linux kernel uses).
  81. */
  82. memset(buf, 0, sizeof(*buf));
  83. COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
  84. COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
  85. COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
  86. COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
  87. COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
  88. #ifdef _GNU_SOURCE
  89. COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
  90. #endif
  91. return (0);
  92. #undef COPY_UTSNAME_FIELD
  93. }
  94. static int relstr_to_int(const char *s)
  95. {
  96. /* Convert a uname release string like "2.6.18" to an integer
  97. * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
  98. */
  99. int i, n, tmp;
  100. tmp = 0;
  101. for (i = 0; i < 3; i++) {
  102. n = 0;
  103. while (*s >= '0' && *s <= '9') {
  104. n *= 10;
  105. n += *s - '0';
  106. s++;
  107. }
  108. tmp = (tmp << 8) + n;
  109. if (*s == '.') {
  110. s++;
  111. }
  112. }
  113. return tmp;
  114. }
  115. int get_osversion(void)
  116. {
  117. static int osversion;
  118. struct new_utsname buf;
  119. const char *s;
  120. if (osversion)
  121. return osversion;
  122. if (qemu_uname_release && *qemu_uname_release) {
  123. s = qemu_uname_release;
  124. } else {
  125. if (sys_uname(&buf))
  126. return 0;
  127. s = buf.release;
  128. }
  129. osversion = relstr_to_int(s);
  130. return osversion;
  131. }
  132. void init_qemu_uname_release(void)
  133. {
  134. /* Initialize qemu_uname_release for later use.
  135. * If the host kernel is too old and the user hasn't asked for
  136. * a specific fake version number, we might want to fake a minimum
  137. * target kernel version.
  138. */
  139. struct new_utsname buf;
  140. if (qemu_uname_release && *qemu_uname_release) {
  141. return;
  142. }
  143. if (sys_uname(&buf)) {
  144. return;
  145. }
  146. if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
  147. qemu_uname_release = UNAME_MINIMUM_RELEASE;
  148. }
  149. }