fedora core 6 1.2949 + vserver 2.2.0
[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 <errno.h>
8 #include <string.h>
9 #include <sys/signal.h>
10 #include <asm/ldt.h>
11 #include "kern_util.h"
12 #include "user.h"
13 #include "sysdep/ptrace.h"
14 #include "task.h"
15 #include "os.h"
16 #include "user_util.h"
17
18 #define MAXTOKEN 64
19
20 /* Set during early boot */
21 int host_has_cmov = 1;
22 int host_has_xmm = 0;
23
24 static char token(int fd, char *buf, int len, char stop)
25 {
26         int n;
27         char *ptr, *end, c;
28
29         ptr = buf;
30         end = &buf[len];
31         do {
32                 n = os_read_file(fd, ptr, sizeof(*ptr));
33                 c = *ptr++;
34                 if(n != sizeof(*ptr)){
35                         if(n == 0) return(0);
36                         printk("Reading /proc/cpuinfo failed, err = %d\n", -n);
37                         if(n < 0)
38                                 return(n);
39                         else
40                                 return(-EIO);
41                 }
42         } while((c != '\n') && (c != stop) && (ptr < end));
43
44         if(ptr == end){
45                 printk("Failed to find '%c' in /proc/cpuinfo\n", stop);
46                 return(-1);
47         }
48         *(ptr - 1) = '\0';
49         return(c);
50 }
51
52 static int find_cpuinfo_line(int fd, char *key, char *scratch, int len)
53 {
54         int n;
55         char c;
56
57         scratch[len - 1] = '\0';
58         while(1){
59                 c = token(fd, scratch, len - 1, ':');
60                 if(c <= 0)
61                         return(0);
62                 else if(c != ':'){
63                         printk("Failed to find ':' in /proc/cpuinfo\n");
64                         return(0);
65                 }
66
67                 if(!strncmp(scratch, key, strlen(key)))
68                         return(1);
69
70                 do {
71                         n = os_read_file(fd, &c, sizeof(c));
72                         if(n != sizeof(c)){
73                                 printk("Failed to find newline in "
74                                        "/proc/cpuinfo, err = %d\n", -n);
75                                 return(0);
76                         }
77                 } while(c != '\n');
78         }
79         return(0);
80 }
81
82 int cpu_feature(char *what, char *buf, int len)
83 {
84         int fd, ret = 0;
85
86         fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
87         if(fd < 0){
88                 printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd);
89                 return(0);
90         }
91
92         if(!find_cpuinfo_line(fd, what, buf, len)){
93                 printk("Couldn't find '%s' line in /proc/cpuinfo\n", what);
94                 goto out_close;
95         }
96
97         token(fd, buf, len, '\n');
98         ret = 1;
99
100  out_close:
101         os_close_file(fd);
102         return(ret);
103 }
104
105 static int check_cpu_flag(char *feature, int *have_it)
106 {
107         char buf[MAXTOKEN], c;
108         int fd, len = ARRAY_SIZE(buf);
109
110         printk("Checking for host processor %s support...", feature);
111         fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
112         if(fd < 0){
113                 printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd);
114                 return 0;
115         }
116
117         *have_it = 0;
118         if(!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf)))
119                 goto out;
120
121         c = token(fd, buf, len - 1, ' ');
122         if(c < 0) goto out;
123         else if(c != ' '){
124                 printk("Failed to find ' ' in /proc/cpuinfo\n");
125                 goto out;
126         }
127
128         while(1){
129                 c = token(fd, buf, len - 1, ' ');
130                 if(c < 0) goto out;
131                 else if(c == '\n') break;
132
133                 if(!strcmp(buf, feature)){
134                         *have_it = 1;
135                         goto out;
136                 }
137         }
138  out:
139         if(*have_it == 0) printk("No\n");
140         else if(*have_it == 1) printk("Yes\n");
141         os_close_file(fd);
142         return 1;
143 }
144
145 #if 0 /* This doesn't work in tt mode, plus it's causing compilation problems
146        * for some people.
147        */
148 static void disable_lcall(void)
149 {
150         struct modify_ldt_ldt_s ldt;
151         int err;
152
153         bzero(&ldt, sizeof(ldt));
154         ldt.entry_number = 7;
155         ldt.base_addr = 0;
156         ldt.limit = 0;
157         err = modify_ldt(1, &ldt, sizeof(ldt));
158         if(err)
159                 printk("Failed to disable lcall7 - errno = %d\n", errno);
160 }
161 #endif
162
163 void arch_init_thread(void)
164 {
165 #if 0
166         disable_lcall();
167 #endif
168 }
169
170 void arch_check_bugs(void)
171 {
172         int have_it;
173
174         if(os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0){
175                 printk("/proc/cpuinfo not available - skipping CPU capability "
176                        "checks\n");
177                 return;
178         }
179         if(check_cpu_flag("cmov", &have_it))
180                 host_has_cmov = have_it;
181         if(check_cpu_flag("xmm", &have_it))
182                 host_has_xmm = have_it;
183 }
184
185 int arch_handle_signal(int sig, union uml_pt_regs *regs)
186 {
187         unsigned char tmp[2];
188
189         /* This is testing for a cmov (0x0f 0x4x) instruction causing a
190          * SIGILL in init.
191          */
192         if((sig != SIGILL) || (TASK_PID(get_current()) != 1)) return(0);
193
194         if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
195                 panic("SIGILL in init, could not read instructions!\n");
196         if((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
197                 return(0);
198
199         if(host_has_cmov == 0)
200                 panic("SIGILL caused by cmov, which this processor doesn't "
201                       "implement, boot a filesystem compiled for older "
202                       "processors");
203         else if(host_has_cmov == 1)
204                 panic("SIGILL caused by cmov, which this processor claims to "
205                       "implement");
206         else if(host_has_cmov == -1)
207                 panic("SIGILL caused by cmov, couldn't tell if this processor "
208                       "implements it, boot a filesystem compiled for older "
209                       "processors");
210         else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
211         return(0);
212 }
213
214 /*
215  * Overrides for Emacs so that we follow Linus's tabbing style.
216  * Emacs will notice this stuff at the end of the file and automatically
217  * adjust the settings for this buffer only.  This must remain at the end
218  * of the file.
219  * ---------------------------------------------------------------------------
220  * Local variables:
221  * c-file-style: "linux"
222  * End:
223  */