bugs.c 4.0 KB

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