blob: cae0af14903ac11454c4c982de064a6c78cc8528 [file] [log] [blame]
Jeff Dikea5ed1ff2007-05-06 14:50:58 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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"
Jeff Dike91b165c2006-09-25 23:33:00 -070016#include "user_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#define MAXTOKEN 64
19
20/* Set during early boot */
21int host_has_cmov = 1;
22int host_has_xmm = 0;
23
24static 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)){
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070035 if(n == 0)
36 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 printk("Reading /proc/cpuinfo failed, err = %d\n", -n);
38 if(n < 0)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070039 return n;
40 else return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42 } while((c != '\n') && (c != stop) && (ptr < end));
43
44 if(ptr == end){
45 printk("Failed to find '%c' in /proc/cpuinfo\n", stop);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070046 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48 *(ptr - 1) = '\0';
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070049 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static 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)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 else if(c != ':'){
63 printk("Failed to find ':' in /proc/cpuinfo\n");
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66
67 if(!strncmp(scratch, key, strlen(key)))
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070068 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
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);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 } while(c != '\n');
78 }
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static int check_cpu_flag(char *feature, int *have_it)
83{
84 char buf[MAXTOKEN], c;
Jeff Dike91b165c2006-09-25 23:33:00 -070085 int fd, len = ARRAY_SIZE(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 printk("Checking for host processor %s support...", feature);
88 fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
89 if(fd < 0){
90 printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd);
Jeff Dike91b165c2006-09-25 23:33:00 -070091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
94 *have_it = 0;
Jeff Dike91b165c2006-09-25 23:33:00 -070095 if(!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 goto out;
97
98 c = token(fd, buf, len - 1, ' ');
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070099 if(c < 0)
100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 else if(c != ' '){
102 printk("Failed to find ' ' in /proc/cpuinfo\n");
103 goto out;
104 }
105
106 while(1){
107 c = token(fd, buf, len - 1, ' ');
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700108 if(c < 0)
109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 else if(c == '\n') break;
111
112 if(!strcmp(buf, feature)){
113 *have_it = 1;
114 goto out;
115 }
116 }
117 out:
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700118 if(*have_it == 0)
119 printk("No\n");
120 else if(*have_it == 1)
121 printk("Yes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 os_close_file(fd);
Jeff Dike91b165c2006-09-25 23:33:00 -0700123 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126#if 0 /* This doesn't work in tt mode, plus it's causing compilation problems
127 * for some people.
128 */
129static void disable_lcall(void)
130{
131 struct modify_ldt_ldt_s ldt;
132 int err;
133
134 bzero(&ldt, sizeof(ldt));
135 ldt.entry_number = 7;
136 ldt.base_addr = 0;
137 ldt.limit = 0;
138 err = modify_ldt(1, &ldt, sizeof(ldt));
139 if(err)
140 printk("Failed to disable lcall7 - errno = %d\n", errno);
141}
142#endif
143
144void arch_init_thread(void)
145{
146#if 0
147 disable_lcall();
148#endif
149}
150
151void arch_check_bugs(void)
152{
153 int have_it;
154
155 if(os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0){
156 printk("/proc/cpuinfo not available - skipping CPU capability "
157 "checks\n");
158 return;
159 }
160 if(check_cpu_flag("cmov", &have_it))
161 host_has_cmov = have_it;
162 if(check_cpu_flag("xmm", &have_it))
163 host_has_xmm = have_it;
164}
165
166int arch_handle_signal(int sig, union uml_pt_regs *regs)
167{
168 unsigned char tmp[2];
169
170 /* This is testing for a cmov (0x0f 0x4x) instruction causing a
171 * SIGILL in init.
172 */
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700173 if((sig != SIGILL) || (TASK_PID(get_current()) != 1))
174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
177 panic("SIGILL in init, could not read instructions!\n");
178 if((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if(host_has_cmov == 0)
182 panic("SIGILL caused by cmov, which this processor doesn't "
183 "implement, boot a filesystem compiled for older "
184 "processors");
185 else if(host_has_cmov == 1)
186 panic("SIGILL caused by cmov, which this processor claims to "
187 "implement");
188 else if(host_has_cmov == -1)
189 panic("SIGILL caused by cmov, couldn't tell if this processor "
190 "implements it, boot a filesystem compiled for older "
191 "processors");
192 else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}