2
0

uname.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "qemu/osdep.h"
  20. #include "qemu.h"
  21. #include "user-internals.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(CPUArchState *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. #if TARGET_BIG_ENDIAN
  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_cpu(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. memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
  69. (dest)[sizeof(dest) - 1] = '\0'; \
  70. } while (0)
  71. int sys_uname(struct new_utsname *buf)
  72. {
  73. struct utsname uts_buf;
  74. if (uname(&uts_buf) < 0)
  75. return (-1);
  76. /*
  77. * Just in case these have some differences, we
  78. * translate utsname to new_utsname (which is the
  79. * struct linux kernel uses).
  80. */
  81. memset(buf, 0, sizeof(*buf));
  82. COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
  83. COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
  84. COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
  85. COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
  86. COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
  87. #ifdef _GNU_SOURCE
  88. COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
  89. #endif
  90. return (0);
  91. #undef COPY_UTSNAME_FIELD
  92. }
  93. static int relstr_to_int(const char *s)
  94. {
  95. /* Convert a uname release string like "2.6.18" to an integer
  96. * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
  97. */
  98. int i, n, tmp;
  99. tmp = 0;
  100. for (i = 0; i < 3; i++) {
  101. n = 0;
  102. while (*s >= '0' && *s <= '9') {
  103. n *= 10;
  104. n += *s - '0';
  105. s++;
  106. }
  107. tmp = (tmp << 8) + n;
  108. if (*s == '.') {
  109. s++;
  110. }
  111. }
  112. return tmp;
  113. }
  114. int get_osversion(void)
  115. {
  116. static int osversion;
  117. struct new_utsname buf;
  118. const char *s;
  119. if (osversion)
  120. return osversion;
  121. if (qemu_uname_release && *qemu_uname_release) {
  122. s = qemu_uname_release;
  123. } else {
  124. if (sys_uname(&buf))
  125. return 0;
  126. s = buf.release;
  127. }
  128. osversion = relstr_to_int(s);
  129. return osversion;
  130. }
  131. void init_qemu_uname_release(void)
  132. {
  133. /* Initialize qemu_uname_release for later use.
  134. * If the host kernel is too old and the user hasn't asked for
  135. * a specific fake version number, we might want to fake a minimum
  136. * target kernel version.
  137. */
  138. struct new_utsname buf;
  139. if (qemu_uname_release && *qemu_uname_release) {
  140. return;
  141. }
  142. if (sys_uname(&buf)) {
  143. return;
  144. }
  145. if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
  146. qemu_uname_release = UNAME_MINIMUM_RELEASE;
  147. }
  148. }