Commit | Line | Data |
---|---|---|
a5ed1ffa | 1 | /* |
1da177e4 LT |
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 | ||
17 | #define MAXTOKEN 64 | |
18 | ||
19 | /* Set during early boot */ | |
20 | int host_has_cmov = 1; | |
21 | int host_has_xmm = 0; | |
22 | ||
23 | static char token(int fd, char *buf, int len, char stop) | |
24 | { | |
25 | int n; | |
26 | char *ptr, *end, c; | |
27 | ||
28 | ptr = buf; | |
29 | end = &buf[len]; | |
30 | do { | |
a6ea4cce | 31 | n = os_read_file(fd, ptr, sizeof(*ptr)); |
1da177e4 LT |
32 | c = *ptr++; |
33 | if(n != sizeof(*ptr)){ | |
a5ed1ffa JD |
34 | if(n == 0) |
35 | return 0; | |
1da177e4 LT |
36 | printk("Reading /proc/cpuinfo failed, err = %d\n", -n); |
37 | if(n < 0) | |
a5ed1ffa JD |
38 | return n; |
39 | else return -EIO; | |
1da177e4 LT |
40 | } |
41 | } while((c != '\n') && (c != stop) && (ptr < end)); | |
42 | ||
43 | if(ptr == end){ | |
44 | printk("Failed to find '%c' in /proc/cpuinfo\n", stop); | |
a5ed1ffa | 45 | return -1; |
1da177e4 LT |
46 | } |
47 | *(ptr - 1) = '\0'; | |
a5ed1ffa | 48 | return c; |
1da177e4 LT |
49 | } |
50 | ||
51 | static int find_cpuinfo_line(int fd, char *key, char *scratch, int len) | |
52 | { | |
53 | int n; | |
54 | char c; | |
55 | ||
56 | scratch[len - 1] = '\0'; | |
57 | while(1){ | |
58 | c = token(fd, scratch, len - 1, ':'); | |
59 | if(c <= 0) | |
a5ed1ffa | 60 | return 0; |
1da177e4 LT |
61 | else if(c != ':'){ |
62 | printk("Failed to find ':' in /proc/cpuinfo\n"); | |
a5ed1ffa | 63 | return 0; |
1da177e4 LT |
64 | } |
65 | ||
66 | if(!strncmp(scratch, key, strlen(key))) | |
a5ed1ffa | 67 | return 1; |
1da177e4 LT |
68 | |
69 | do { | |
a6ea4cce | 70 | n = os_read_file(fd, &c, sizeof(c)); |
1da177e4 LT |
71 | if(n != sizeof(c)){ |
72 | printk("Failed to find newline in " | |
73 | "/proc/cpuinfo, err = %d\n", -n); | |
a5ed1ffa | 74 | return 0; |
1da177e4 LT |
75 | } |
76 | } while(c != '\n'); | |
77 | } | |
a5ed1ffa | 78 | return 0; |
1da177e4 LT |
79 | } |
80 | ||
1da177e4 LT |
81 | static int check_cpu_flag(char *feature, int *have_it) |
82 | { | |
83 | char buf[MAXTOKEN], c; | |
91b165c0 | 84 | int fd, len = ARRAY_SIZE(buf); |
1da177e4 LT |
85 | |
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); | |
91b165c0 | 90 | return 0; |
1da177e4 LT |
91 | } |
92 | ||
93 | *have_it = 0; | |
91b165c0 | 94 | if(!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf))) |
1da177e4 LT |
95 | goto out; |
96 | ||
97 | c = token(fd, buf, len - 1, ' '); | |
a5ed1ffa JD |
98 | if(c < 0) |
99 | goto out; | |
1da177e4 LT |
100 | else if(c != ' '){ |
101 | printk("Failed to find ' ' in /proc/cpuinfo\n"); | |
102 | goto out; | |
103 | } | |
104 | ||
105 | while(1){ | |
106 | c = token(fd, buf, len - 1, ' '); | |
a5ed1ffa JD |
107 | if(c < 0) |
108 | goto out; | |
1da177e4 LT |
109 | else if(c == '\n') break; |
110 | ||
111 | if(!strcmp(buf, feature)){ | |
112 | *have_it = 1; | |
113 | goto out; | |
114 | } | |
115 | } | |
116 | out: | |
a5ed1ffa JD |
117 | if(*have_it == 0) |
118 | printk("No\n"); | |
119 | else if(*have_it == 1) | |
120 | printk("Yes\n"); | |
1da177e4 | 121 | os_close_file(fd); |
91b165c0 | 122 | return 1; |
1da177e4 LT |
123 | } |
124 | ||
125 | #if 0 /* This doesn't work in tt mode, plus it's causing compilation problems | |
126 | * for some people. | |
127 | */ | |
128 | static void disable_lcall(void) | |
129 | { | |
130 | struct modify_ldt_ldt_s ldt; | |
131 | int err; | |
132 | ||
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); | |
140 | } | |
141 | #endif | |
142 | ||
143 | void arch_init_thread(void) | |
144 | { | |
145 | #if 0 | |
146 | disable_lcall(); | |
147 | #endif | |
148 | } | |
149 | ||
150 | void arch_check_bugs(void) | |
151 | { | |
152 | int have_it; | |
153 | ||
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; | |
158 | } | |
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; | |
163 | } | |
164 | ||
165 | int arch_handle_signal(int sig, union uml_pt_regs *regs) | |
166 | { | |
167 | unsigned char tmp[2]; | |
168 | ||
169 | /* This is testing for a cmov (0x0f 0x4x) instruction causing a | |
170 | * SIGILL in init. | |
171 | */ | |
a5ed1ffa JD |
172 | if((sig != SIGILL) || (TASK_PID(get_current()) != 1)) |
173 | return 0; | |
1da177e4 LT |
174 | |
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)) | |
a5ed1ffa | 178 | return 0; |
1da177e4 LT |
179 | |
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); | |
a5ed1ffa | 192 | return 0; |
1da177e4 | 193 | } |