bugs.c 4.0 KB

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