bugs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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) return(0);
  31. printk("Reading /proc/cpuinfo failed, err = %d\n", -n);
  32. if(n < 0)
  33. return(n);
  34. else
  35. 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. int cpu_feature(char *what, char *buf, int len)
  72. {
  73. int fd, ret = 0;
  74. fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
  75. if(fd < 0){
  76. printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd);
  77. return(0);
  78. }
  79. if(!find_cpuinfo_line(fd, what, buf, len)){
  80. printk("Couldn't find '%s' line in /proc/cpuinfo\n", what);
  81. goto out_close;
  82. }
  83. token(fd, buf, len, '\n');
  84. ret = 1;
  85. out_close:
  86. os_close_file(fd);
  87. return(ret);
  88. }
  89. static int check_cpu_flag(char *feature, int *have_it)
  90. {
  91. char buf[MAXTOKEN], c;
  92. int fd, len = ARRAY_SIZE(buf);
  93. printk("Checking for host processor %s support...", feature);
  94. fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
  95. if(fd < 0){
  96. printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd);
  97. return 0;
  98. }
  99. *have_it = 0;
  100. if(!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf)))
  101. goto out;
  102. c = token(fd, buf, len - 1, ' ');
  103. if(c < 0) goto out;
  104. else if(c != ' '){
  105. printk("Failed to find ' ' in /proc/cpuinfo\n");
  106. goto out;
  107. }
  108. while(1){
  109. c = token(fd, buf, len - 1, ' ');
  110. if(c < 0) goto out;
  111. else if(c == '\n') break;
  112. if(!strcmp(buf, feature)){
  113. *have_it = 1;
  114. goto out;
  115. }
  116. }
  117. out:
  118. if(*have_it == 0) printk("No\n");
  119. else if(*have_it == 1) printk("Yes\n");
  120. os_close_file(fd);
  121. return 1;
  122. }
  123. #if 0 /* This doesn't work in tt mode, plus it's causing compilation problems
  124. * for some people.
  125. */
  126. static void disable_lcall(void)
  127. {
  128. struct modify_ldt_ldt_s ldt;
  129. int err;
  130. bzero(&ldt, sizeof(ldt));
  131. ldt.entry_number = 7;
  132. ldt.base_addr = 0;
  133. ldt.limit = 0;
  134. err = modify_ldt(1, &ldt, sizeof(ldt));
  135. if(err)
  136. printk("Failed to disable lcall7 - errno = %d\n", errno);
  137. }
  138. #endif
  139. void arch_init_thread(void)
  140. {
  141. #if 0
  142. disable_lcall();
  143. #endif
  144. }
  145. void arch_check_bugs(void)
  146. {
  147. int have_it;
  148. if(os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0){
  149. printk("/proc/cpuinfo not available - skipping CPU capability "
  150. "checks\n");
  151. return;
  152. }
  153. if(check_cpu_flag("cmov", &have_it))
  154. host_has_cmov = have_it;
  155. if(check_cpu_flag("xmm", &have_it))
  156. host_has_xmm = have_it;
  157. }
  158. int arch_handle_signal(int sig, union uml_pt_regs *regs)
  159. {
  160. unsigned char tmp[2];
  161. /* This is testing for a cmov (0x0f 0x4x) instruction causing a
  162. * SIGILL in init.
  163. */
  164. if((sig != SIGILL) || (TASK_PID(get_current()) != 1)) return(0);
  165. if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
  166. panic("SIGILL in init, could not read instructions!\n");
  167. if((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
  168. return(0);
  169. if(host_has_cmov == 0)
  170. panic("SIGILL caused by cmov, which this processor doesn't "
  171. "implement, boot a filesystem compiled for older "
  172. "processors");
  173. else if(host_has_cmov == 1)
  174. panic("SIGILL caused by cmov, which this processor claims to "
  175. "implement");
  176. else if(host_has_cmov == -1)
  177. panic("SIGILL caused by cmov, couldn't tell if this processor "
  178. "implements it, boot a filesystem compiled for older "
  179. "processors");
  180. else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
  181. return(0);
  182. }
  183. /*
  184. * Overrides for Emacs so that we follow Linus's tabbing style.
  185. * Emacs will notice this stuff at the end of the file and automatically
  186. * adjust the settings for this buffer only. This must remain at the end
  187. * of the file.
  188. * ---------------------------------------------------------------------------
  189. * Local variables:
  190. * c-file-style: "linux"
  191. * End:
  192. */