ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / sys-i386 / bugs.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <sys/signal.h>
11 #include "kern_util.h"
12 #include "user.h"
13 #include "sysdep/ptrace.h"
14 #include "task.h"
15
16 #define MAXTOKEN 64
17
18 /* Set during early boot */
19 int cpu_has_cmov = 1;
20 int cpu_has_xmm = 0;
21
22 static char token(int fd, char *buf, int len, char stop)
23 {
24         int n;
25         char *ptr, *end, c;
26
27         ptr = buf;
28         end = &buf[len];
29         do {
30                 n = read(fd, ptr, sizeof(*ptr));
31                 c = *ptr++;
32                 if(n == 0) return(0);
33                 else if(n != sizeof(*ptr)){
34                         printk("Reading /proc/cpuinfo failed, "
35                                "errno = %d\n", errno);
36                         return(-errno);
37                 }
38         } while((c != '\n') && (c != stop) && (ptr < end));
39
40         if(ptr == end){
41                 printk("Failed to find '%c' in /proc/cpuinfo\n", stop);
42                 return(-1);
43         }
44         *(ptr - 1) = '\0';
45         return(c);
46 }
47
48 static int check_cpu_feature(char *feature, int *have_it)
49 {
50         char buf[MAXTOKEN], c;
51         int fd, len = sizeof(buf)/sizeof(buf[0]), n;
52
53         printk("Checking for host processor %s support...", feature);
54         fd = open("/proc/cpuinfo", O_RDONLY);
55         if(fd < 0){
56                 printk("Couldn't open /proc/cpuinfo, errno = %d\n", errno);
57                 return(0);
58         }
59
60         *have_it = 0;
61         buf[len - 1] = '\0';
62         while(1){
63                 c = token(fd, buf, len - 1, ':');
64                 if(c <= 0) goto out;
65                 else if(c != ':'){
66                         printk("Failed to find ':' in /proc/cpuinfo\n");
67                         goto out;
68                 }
69
70                 if(!strncmp(buf, "flags", strlen("flags"))) break;
71
72                 do {
73                         n = read(fd, &c, sizeof(c));
74                         if(n != sizeof(c)){
75                                 printk("Failed to find newline in "
76                                        "/proc/cpuinfo, n = %d, errno = %d\n",
77                                        n, errno);
78                                 goto out;
79                         }
80                 } while(c != '\n');
81         }
82
83         c = token(fd, buf, len - 1, ' ');
84         if(c < 0) goto out;
85         else if(c != ' '){
86                 printk("Failed to find ':' in /proc/cpuinfo\n");
87                 goto out;
88         }
89
90         while(1){
91                 c = token(fd, buf, len - 1, ' ');
92                 if(c < 0) goto out;
93                 else if(c == '\n') break;
94
95                 if(!strcmp(buf, feature)){
96                         *have_it = 1;
97                         goto out;
98                 }
99         }
100  out:
101         if(*have_it == 0) printk("No\n");
102         else if(*have_it == 1) printk("Yes\n");
103         close(fd);
104         return(1);
105 }
106
107 void arch_check_bugs(void)
108 {
109         int have_it;
110
111         if(access("/proc/cpuinfo", R_OK)){
112                 printk("/proc/cpuinfo not available - skipping CPU capability "
113                        "checks\n");
114                 return;
115         }
116         if(check_cpu_feature("cmov", &have_it)) cpu_has_cmov = have_it;
117         if(check_cpu_feature("xmm", &have_it)) cpu_has_xmm = have_it;
118 }
119
120 int arch_handle_signal(int sig, union uml_pt_regs *regs)
121 {
122         unsigned long ip;
123
124         /* This is testing for a cmov (0x0f 0x4x) instruction causing a
125          * SIGILL in init.
126          */
127         if((sig != SIGILL) || (TASK_PID(get_current()) != 1)) return(0);
128
129         ip = UPT_IP(regs);
130         if((*((char *) ip) != 0x0f) || ((*((char *) (ip + 1)) & 0xf0) != 0x40))
131                 return(0);
132
133         if(cpu_has_cmov == 0)
134                 panic("SIGILL caused by cmov, which this processor doesn't "
135                       "implement, boot a filesystem compiled for older "
136                       "processors");
137         else if(cpu_has_cmov == 1)
138                 panic("SIGILL caused by cmov, which this processor claims to "
139                       "implement");
140         else if(cpu_has_cmov == -1)
141                 panic("SIGILL caused by cmov, couldn't tell if this processor "
142                       "implements it, boot a filesystem compiled for older "
143                       "processors");
144         else panic("Bad value for cpu_has_cmov (%d)", cpu_has_cmov);
145         return(0);
146 }
147
148 /*
149  * Overrides for Emacs so that we follow Linus's tabbing style.
150  * Emacs will notice this stuff at the end of the file and automatically
151  * adjust the settings for this buffer only.  This must remain at the end
152  * of the file.
153  * ---------------------------------------------------------------------------
154  * Local variables:
155  * c-file-style: "linux"
156  * End:
157  */