bugs.c 4.8 KB

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