bugs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 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) 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) goto out;
  93. else if(c == '\n') break;
  94. if(!strcmp(buf, feature)){
  95. *have_it = 1;
  96. goto out;
  97. }
  98. }
  99. out:
  100. if(*have_it == 0) printk("No\n");
  101. else if(*have_it == 1) printk("Yes\n");
  102. os_close_file(fd);
  103. return 1;
  104. }
  105. #if 0 /* This doesn't work in tt mode, plus it's causing compilation problems
  106. * for some people.
  107. */
  108. static void disable_lcall(void)
  109. {
  110. struct modify_ldt_ldt_s ldt;
  111. int err;
  112. bzero(&ldt, sizeof(ldt));
  113. ldt.entry_number = 7;
  114. ldt.base_addr = 0;
  115. ldt.limit = 0;
  116. err = modify_ldt(1, &ldt, sizeof(ldt));
  117. if(err)
  118. printk("Failed to disable lcall7 - errno = %d\n", errno);
  119. }
  120. #endif
  121. void arch_init_thread(void)
  122. {
  123. #if 0
  124. disable_lcall();
  125. #endif
  126. }
  127. void arch_check_bugs(void)
  128. {
  129. int have_it;
  130. if(os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0){
  131. printk("/proc/cpuinfo not available - skipping CPU capability "
  132. "checks\n");
  133. return;
  134. }
  135. if(check_cpu_flag("cmov", &have_it))
  136. host_has_cmov = have_it;
  137. if(check_cpu_flag("xmm", &have_it))
  138. host_has_xmm = have_it;
  139. }
  140. int arch_handle_signal(int sig, union uml_pt_regs *regs)
  141. {
  142. unsigned char tmp[2];
  143. /* This is testing for a cmov (0x0f 0x4x) instruction causing a
  144. * SIGILL in init.
  145. */
  146. if((sig != SIGILL) || (TASK_PID(get_current()) != 1)) return(0);
  147. if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
  148. panic("SIGILL in init, could not read instructions!\n");
  149. if((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
  150. return(0);
  151. if(host_has_cmov == 0)
  152. panic("SIGILL caused by cmov, which this processor doesn't "
  153. "implement, boot a filesystem compiled for older "
  154. "processors");
  155. else if(host_has_cmov == 1)
  156. panic("SIGILL caused by cmov, which this processor claims to "
  157. "implement");
  158. else if(host_has_cmov == -1)
  159. panic("SIGILL caused by cmov, couldn't tell if this processor "
  160. "implements it, boot a filesystem compiled for older "
  161. "processors");
  162. else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
  163. return(0);
  164. }
  165. /*
  166. * Overrides for Emacs so that we follow Linus's tabbing style.
  167. * Emacs will notice this stuff at the end of the file and automatically
  168. * adjust the settings for this buffer only. This must remain at the end
  169. * of the file.
  170. * ---------------------------------------------------------------------------
  171. * Local variables:
  172. * c-file-style: "linux"
  173. * End:
  174. */