bugs.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <string.h>
  8. #include "kern_constants.h"
  9. #include "os.h"
  10. #include "task.h"
  11. #include "user.h"
  12. #define MAXTOKEN 64
  13. /* Set during early boot */
  14. int host_has_cmov = 1;
  15. int host_has_xmm = 0;
  16. static char token(int fd, char *buf, int len, char stop)
  17. {
  18. int n;
  19. char *ptr, *end, c;
  20. ptr = buf;
  21. end = &buf[len];
  22. do {
  23. n = os_read_file(fd, ptr, sizeof(*ptr));
  24. c = *ptr++;
  25. if (n != sizeof(*ptr)) {
  26. if (n == 0)
  27. return 0;
  28. printk(UM_KERN_ERR "Reading /proc/cpuinfo failed, "
  29. "err = %d\n", -n);
  30. if (n < 0)
  31. return n;
  32. else return -EIO;
  33. }
  34. } while ((c != '\n') && (c != stop) && (ptr < end));
  35. if (ptr == end) {
  36. printk(UM_KERN_ERR "Failed to find '%c' in /proc/cpuinfo\n",
  37. stop);
  38. return -1;
  39. }
  40. *(ptr - 1) = '\0';
  41. return c;
  42. }
  43. static int find_cpuinfo_line(int fd, char *key, char *scratch, int len)
  44. {
  45. int n;
  46. char c;
  47. scratch[len - 1] = '\0';
  48. while (1) {
  49. c = token(fd, scratch, len - 1, ':');
  50. if (c <= 0)
  51. return 0;
  52. else if (c != ':') {
  53. printk(UM_KERN_ERR "Failed to find ':' in "
  54. "/proc/cpuinfo\n");
  55. return 0;
  56. }
  57. if (!strncmp(scratch, key, strlen(key)))
  58. return 1;
  59. do {
  60. n = os_read_file(fd, &c, sizeof(c));
  61. if (n != sizeof(c)) {
  62. printk(UM_KERN_ERR "Failed to find newline in "
  63. "/proc/cpuinfo, err = %d\n", -n);
  64. return 0;
  65. }
  66. } while (c != '\n');
  67. }
  68. return 0;
  69. }
  70. static int check_cpu_flag(char *feature, int *have_it)
  71. {
  72. char buf[MAXTOKEN], c;
  73. int fd, len = ARRAY_SIZE(buf);
  74. printk(UM_KERN_INFO "Checking for host processor %s support...",
  75. feature);
  76. fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
  77. if (fd < 0) {
  78. printk(UM_KERN_ERR "Couldn't open /proc/cpuinfo, err = %d\n",
  79. -fd);
  80. return 0;
  81. }
  82. *have_it = 0;
  83. if (!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf)))
  84. goto out;
  85. c = token(fd, buf, len - 1, ' ');
  86. if (c < 0)
  87. goto out;
  88. else if (c != ' ') {
  89. printk(UM_KERN_ERR "Failed to find ' ' in /proc/cpuinfo\n");
  90. goto out;
  91. }
  92. while (1) {
  93. c = token(fd, buf, len - 1, ' ');
  94. if (c < 0)
  95. goto out;
  96. else if (c == '\n')
  97. break;
  98. if (!strcmp(buf, feature)) {
  99. *have_it = 1;
  100. goto out;
  101. }
  102. }
  103. out:
  104. if (*have_it == 0)
  105. printk("No\n");
  106. else if (*have_it == 1)
  107. printk("Yes\n");
  108. os_close_file(fd);
  109. return 1;
  110. }
  111. #if 0 /*
  112. * This doesn't work in tt mode, plus it's causing compilation problems
  113. * for some people.
  114. */
  115. static void disable_lcall(void)
  116. {
  117. struct modify_ldt_ldt_s ldt;
  118. int err;
  119. bzero(&ldt, sizeof(ldt));
  120. ldt.entry_number = 7;
  121. ldt.base_addr = 0;
  122. ldt.limit = 0;
  123. err = modify_ldt(1, &ldt, sizeof(ldt));
  124. if (err)
  125. printk(UM_KERN_ERR "Failed to disable lcall7 - errno = %d\n",
  126. errno);
  127. }
  128. #endif
  129. void arch_init_thread(void)
  130. {
  131. #if 0
  132. disable_lcall();
  133. #endif
  134. }
  135. void arch_check_bugs(void)
  136. {
  137. int have_it;
  138. if (os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0) {
  139. printk(UM_KERN_ERR "/proc/cpuinfo not available - skipping CPU "
  140. "capability checks\n");
  141. return;
  142. }
  143. if (check_cpu_flag("cmov", &have_it))
  144. host_has_cmov = have_it;
  145. if (check_cpu_flag("xmm", &have_it))
  146. host_has_xmm = have_it;
  147. }
  148. int arch_handle_signal(int sig, struct uml_pt_regs *regs)
  149. {
  150. unsigned char tmp[2];
  151. /*
  152. * This is testing for a cmov (0x0f 0x4x) instruction causing a
  153. * SIGILL in init.
  154. */
  155. if ((sig != SIGILL) || (TASK_PID(get_current()) != 1))
  156. return 0;
  157. if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
  158. panic("SIGILL in init, could not read instructions!\n");
  159. if ((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
  160. return 0;
  161. if (host_has_cmov == 0)
  162. panic("SIGILL caused by cmov, which this processor doesn't "
  163. "implement, boot a filesystem compiled for older "
  164. "processors");
  165. else if (host_has_cmov == 1)
  166. panic("SIGILL caused by cmov, which this processor claims to "
  167. "implement");
  168. else if (host_has_cmov == -1)
  169. panic("SIGILL caused by cmov, couldn't tell if this processor "
  170. "implements it, boot a filesystem compiled for older "
  171. "processors");
  172. else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
  173. return 0;
  174. }