blob: 797945cd3df2433f29643fbf95bcf7e0bdc29c55 [file] [log] [blame]
Jeff Dikea5ed1ff2007-05-06 14:50:58 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <errno.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07007#include <signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <string.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07009#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "os.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include "task.h"
12#include "user.h"
Karol Swietlicki235a6f02008-02-04 22:30:38 -080013#include "sysdep/archsetjmp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#define MAXTOKEN 64
16
17/* Set during early boot */
18int host_has_cmov = 1;
Karol Swietlicki235a6f02008-02-04 22:30:38 -080019static jmp_buf cmov_test_return;
20
21static void cmov_sigill_test_handler(int sig)
22{
23 host_has_cmov = 0;
24 longjmp(cmov_test_return, 1);
25}
26
27static void test_for_host_cmov(void)
28{
29 struct sigaction old, new;
30
31 printk(UM_KERN_INFO "Checking for host processor cmov support...");
32 new.sa_handler = cmov_sigill_test_handler;
33
34 /* Make sure that SIGILL is enabled after the handler longjmps back */
35 new.sa_flags = SA_NODEFER;
36 sigemptyset(&new.sa_mask);
37 sigaction(SIGILL, &new, &old);
38
39 if (setjmp(cmov_test_return) == 0) {
40 unsigned long foo = 0;
41 __asm__ __volatile__("cmovz %0, %1" : "=r" (foo) : "0" (foo));
42 printk(UM_KERN_CONT "Yes\n");
43 } else
44 printk(UM_KERN_CONT "No\n");
45
46 sigaction(SIGILL, &old, &new);
47}
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static char token(int fd, char *buf, int len, char stop)
50{
51 int n;
52 char *ptr, *end, c;
53
54 ptr = buf;
55 end = &buf[len];
56 do {
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070057 n = os_read_file(fd, ptr, sizeof(*ptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 c = *ptr++;
Jeff Dikeba180fd2007-10-16 01:27:00 -070059 if (n != sizeof(*ptr)) {
60 if (n == 0)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070061 return 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -070062 printk(UM_KERN_ERR "Reading /proc/cpuinfo failed, "
63 "err = %d\n", -n);
64 if (n < 0)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070065 return n;
66 else return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Jeff Dikeba180fd2007-10-16 01:27:00 -070068 } while ((c != '\n') && (c != stop) && (ptr < end));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jeff Dikeba180fd2007-10-16 01:27:00 -070070 if (ptr == end) {
71 printk(UM_KERN_ERR "Failed to find '%c' in /proc/cpuinfo\n",
72 stop);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070073 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 *(ptr - 1) = '\0';
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070076 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static int find_cpuinfo_line(int fd, char *key, char *scratch, int len)
80{
81 int n;
82 char c;
83
84 scratch[len - 1] = '\0';
Jeff Dikeba180fd2007-10-16 01:27:00 -070085 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 c = token(fd, scratch, len - 1, ':');
Jeff Dikeba180fd2007-10-16 01:27:00 -070087 if (c <= 0)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070088 return 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -070089 else if (c != ':') {
90 printk(UM_KERN_ERR "Failed to find ':' in "
91 "/proc/cpuinfo\n");
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Jeff Dikeba180fd2007-10-16 01:27:00 -070095 if (!strncmp(scratch, key, strlen(key)))
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070096 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 do {
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070099 n = os_read_file(fd, &c, sizeof(c));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700100 if (n != sizeof(c)) {
101 printk(UM_KERN_ERR "Failed to find newline in "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 "/proc/cpuinfo, err = %d\n", -n);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700105 } while (c != '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int check_cpu_flag(char *feature, int *have_it)
111{
112 char buf[MAXTOKEN], c;
Jeff Dike91b165c2006-09-25 23:33:00 -0700113 int fd, len = ARRAY_SIZE(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jeff Dikeba180fd2007-10-16 01:27:00 -0700115 printk(UM_KERN_INFO "Checking for host processor %s support...",
116 feature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700118 if (fd < 0) {
119 printk(UM_KERN_ERR "Couldn't open /proc/cpuinfo, err = %d\n",
120 -fd);
Jeff Dike91b165c2006-09-25 23:33:00 -0700121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
124 *have_it = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700125 if (!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 goto out;
127
128 c = token(fd, buf, len - 1, ' ');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 if (c < 0)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700130 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700131 else if (c != ' ') {
132 printk(UM_KERN_ERR "Failed to find ' ' in /proc/cpuinfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto out;
134 }
135
Jeff Dikeba180fd2007-10-16 01:27:00 -0700136 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 c = token(fd, buf, len - 1, ' ');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700138 if (c < 0)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700139 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700140 else if (c == '\n')
141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jeff Dikeba180fd2007-10-16 01:27:00 -0700143 if (!strcmp(buf, feature)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 *have_it = 1;
145 goto out;
146 }
147 }
148 out:
Jeff Dikeba180fd2007-10-16 01:27:00 -0700149 if (*have_it == 0)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700150 printk("No\n");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700151 else if (*have_it == 1)
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700152 printk("Yes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 os_close_file(fd);
Jeff Dike91b165c2006-09-25 23:33:00 -0700154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Jeff Dikeba180fd2007-10-16 01:27:00 -0700157#if 0 /*
158 * This doesn't work in tt mode, plus it's causing compilation problems
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * for some people.
160 */
161static void disable_lcall(void)
162{
163 struct modify_ldt_ldt_s ldt;
164 int err;
165
166 bzero(&ldt, sizeof(ldt));
167 ldt.entry_number = 7;
168 ldt.base_addr = 0;
169 ldt.limit = 0;
170 err = modify_ldt(1, &ldt, sizeof(ldt));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700171 if (err)
172 printk(UM_KERN_ERR "Failed to disable lcall7 - errno = %d\n",
173 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175#endif
176
177void arch_init_thread(void)
178{
179#if 0
180 disable_lcall();
181#endif
182}
183
184void arch_check_bugs(void)
185{
Karol Swietlicki235a6f02008-02-04 22:30:38 -0800186 test_for_host_cmov();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Jeff Dike77bf4402007-10-16 01:26:58 -0700189int arch_handle_signal(int sig, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 unsigned char tmp[2];
192
Jeff Dikeba180fd2007-10-16 01:27:00 -0700193 /*
194 * This is testing for a cmov (0x0f 0x4x) instruction causing a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * SIGILL in init.
196 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700197 if ((sig != SIGILL) || (TASK_PID(get_current()) != 1))
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
201 panic("SIGILL in init, could not read instructions!\n");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700202 if ((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Jeff Dikeba180fd2007-10-16 01:27:00 -0700205 if (host_has_cmov == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 panic("SIGILL caused by cmov, which this processor doesn't "
207 "implement, boot a filesystem compiled for older "
208 "processors");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700209 else if (host_has_cmov == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 panic("SIGILL caused by cmov, which this processor claims to "
211 "implement");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700212 else if (host_has_cmov == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 panic("SIGILL caused by cmov, couldn't tell if this processor "
214 "implements it, boot a filesystem compiled for older "
215 "processors");
216 else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}