uml: rename os_{read_write}_file_k back to os_{read_write}_file
[usb.git] / arch / um / sys-i386 / bugs.c
blob0393e44813e775e1adf6e4274789caabb3d09493
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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"
17 #define MAXTOKEN 64
19 /* Set during early boot */
20 int host_has_cmov = 1;
21 int host_has_xmm = 0;
23 static char token(int fd, char *buf, int len, char stop)
25 int n;
26 char *ptr, *end, c;
28 ptr = buf;
29 end = &buf[len];
30 do {
31 n = os_read_file(fd, ptr, sizeof(*ptr));
32 c = *ptr++;
33 if(n != sizeof(*ptr)){
34 if(n == 0)
35 return 0;
36 printk("Reading /proc/cpuinfo failed, err = %d\n", -n);
37 if(n < 0)
38 return n;
39 else return -EIO;
41 } while((c != '\n') && (c != stop) && (ptr < end));
43 if(ptr == end){
44 printk("Failed to find '%c' in /proc/cpuinfo\n", stop);
45 return -1;
47 *(ptr - 1) = '\0';
48 return c;
51 static int find_cpuinfo_line(int fd, char *key, char *scratch, int len)
53 int n;
54 char c;
56 scratch[len - 1] = '\0';
57 while(1){
58 c = token(fd, scratch, len - 1, ':');
59 if(c <= 0)
60 return 0;
61 else if(c != ':'){
62 printk("Failed to find ':' in /proc/cpuinfo\n");
63 return 0;
66 if(!strncmp(scratch, key, strlen(key)))
67 return 1;
69 do {
70 n = os_read_file(fd, &c, sizeof(c));
71 if(n != sizeof(c)){
72 printk("Failed to find newline in "
73 "/proc/cpuinfo, err = %d\n", -n);
74 return 0;
76 } while(c != '\n');
78 return 0;
81 static int check_cpu_flag(char *feature, int *have_it)
83 char buf[MAXTOKEN], c;
84 int fd, len = ARRAY_SIZE(buf);
86 printk("Checking for host processor %s support...", feature);
87 fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
88 if(fd < 0){
89 printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd);
90 return 0;
93 *have_it = 0;
94 if(!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf)))
95 goto out;
97 c = token(fd, buf, len - 1, ' ');
98 if(c < 0)
99 goto out;
100 else if(c != ' '){
101 printk("Failed to find ' ' in /proc/cpuinfo\n");
102 goto out;
105 while(1){
106 c = token(fd, buf, len - 1, ' ');
107 if(c < 0)
108 goto out;
109 else if(c == '\n') break;
111 if(!strcmp(buf, feature)){
112 *have_it = 1;
113 goto out;
116 out:
117 if(*have_it == 0)
118 printk("No\n");
119 else if(*have_it == 1)
120 printk("Yes\n");
121 os_close_file(fd);
122 return 1;
125 #if 0 /* This doesn't work in tt mode, plus it's causing compilation problems
126 * for some people.
128 static void disable_lcall(void)
130 struct modify_ldt_ldt_s ldt;
131 int err;
133 bzero(&ldt, sizeof(ldt));
134 ldt.entry_number = 7;
135 ldt.base_addr = 0;
136 ldt.limit = 0;
137 err = modify_ldt(1, &ldt, sizeof(ldt));
138 if(err)
139 printk("Failed to disable lcall7 - errno = %d\n", errno);
141 #endif
143 void arch_init_thread(void)
145 #if 0
146 disable_lcall();
147 #endif
150 void arch_check_bugs(void)
152 int have_it;
154 if(os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0){
155 printk("/proc/cpuinfo not available - skipping CPU capability "
156 "checks\n");
157 return;
159 if(check_cpu_flag("cmov", &have_it))
160 host_has_cmov = have_it;
161 if(check_cpu_flag("xmm", &have_it))
162 host_has_xmm = have_it;
165 int arch_handle_signal(int sig, union uml_pt_regs *regs)
167 unsigned char tmp[2];
169 /* This is testing for a cmov (0x0f 0x4x) instruction causing a
170 * SIGILL in init.
172 if((sig != SIGILL) || (TASK_PID(get_current()) != 1))
173 return 0;
175 if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
176 panic("SIGILL in init, could not read instructions!\n");
177 if((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
178 return 0;
180 if(host_has_cmov == 0)
181 panic("SIGILL caused by cmov, which this processor doesn't "
182 "implement, boot a filesystem compiled for older "
183 "processors");
184 else if(host_has_cmov == 1)
185 panic("SIGILL caused by cmov, which this processor claims to "
186 "implement");
187 else if(host_has_cmov == -1)
188 panic("SIGILL caused by cmov, couldn't tell if this processor "
189 "implements it, boot a filesystem compiled for older "
190 "processors");
191 else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
192 return 0;