bugs.c 4.1 KB
Newer Older
J
Jeff Dike 已提交
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include <errno.h>
J
Jeff Dike 已提交
7
#include <signal.h>
L
Linus Torvalds 已提交
8
#include <string.h>
J
Jeff Dike 已提交
9
#include "kern_constants.h"
L
Linus Torvalds 已提交
10
#include "os.h"
J
Jeff Dike 已提交
11 12
#include "task.h"
#include "user.h"
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26

#define MAXTOKEN 64

/* Set during early boot */
int host_has_cmov = 1;

static char token(int fd, char *buf, int len, char stop)
{
	int n;
	char *ptr, *end, c;

	ptr = buf;
	end = &buf[len];
	do {
27
		n = os_read_file(fd, ptr, sizeof(*ptr));
L
Linus Torvalds 已提交
28
		c = *ptr++;
J
Jeff Dike 已提交
29 30
		if (n != sizeof(*ptr)) {
			if (n == 0)
J
Jeff Dike 已提交
31
				return 0;
J
Jeff Dike 已提交
32 33 34
			printk(UM_KERN_ERR "Reading /proc/cpuinfo failed, "
			       "err = %d\n", -n);
			if (n < 0)
J
Jeff Dike 已提交
35 36
				return n;
			else return -EIO;
L
Linus Torvalds 已提交
37
		}
J
Jeff Dike 已提交
38
	} while ((c != '\n') && (c != stop) && (ptr < end));
L
Linus Torvalds 已提交
39

J
Jeff Dike 已提交
40 41 42
	if (ptr == end) {
		printk(UM_KERN_ERR "Failed to find '%c' in /proc/cpuinfo\n",
		       stop);
J
Jeff Dike 已提交
43
		return -1;
L
Linus Torvalds 已提交
44 45
	}
	*(ptr - 1) = '\0';
J
Jeff Dike 已提交
46
	return c;
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54
}

static int find_cpuinfo_line(int fd, char *key, char *scratch, int len)
{
	int n;
	char c;

	scratch[len - 1] = '\0';
J
Jeff Dike 已提交
55
	while (1) {
L
Linus Torvalds 已提交
56
		c = token(fd, scratch, len - 1, ':');
J
Jeff Dike 已提交
57
		if (c <= 0)
J
Jeff Dike 已提交
58
			return 0;
J
Jeff Dike 已提交
59 60 61
		else if (c != ':') {
			printk(UM_KERN_ERR "Failed to find ':' in "
			       "/proc/cpuinfo\n");
J
Jeff Dike 已提交
62
			return 0;
L
Linus Torvalds 已提交
63 64
		}

J
Jeff Dike 已提交
65
		if (!strncmp(scratch, key, strlen(key)))
J
Jeff Dike 已提交
66
			return 1;
L
Linus Torvalds 已提交
67 68

		do {
69
			n = os_read_file(fd, &c, sizeof(c));
J
Jeff Dike 已提交
70 71
			if (n != sizeof(c)) {
				printk(UM_KERN_ERR "Failed to find newline in "
L
Linus Torvalds 已提交
72
				       "/proc/cpuinfo, err = %d\n", -n);
J
Jeff Dike 已提交
73
				return 0;
L
Linus Torvalds 已提交
74
			}
J
Jeff Dike 已提交
75
		} while (c != '\n');
L
Linus Torvalds 已提交
76
	}
J
Jeff Dike 已提交
77
	return 0;
L
Linus Torvalds 已提交
78 79 80 81 82
}

static int check_cpu_flag(char *feature, int *have_it)
{
	char buf[MAXTOKEN], c;
83
	int fd, len = ARRAY_SIZE(buf);
L
Linus Torvalds 已提交
84

J
Jeff Dike 已提交
85 86
	printk(UM_KERN_INFO "Checking for host processor %s support...",
	       feature);
L
Linus Torvalds 已提交
87
	fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0);
J
Jeff Dike 已提交
88 89 90
	if (fd < 0) {
		printk(UM_KERN_ERR "Couldn't open /proc/cpuinfo, err = %d\n",
		       -fd);
91
		return 0;
L
Linus Torvalds 已提交
92 93 94
	}

	*have_it = 0;
J
Jeff Dike 已提交
95
	if (!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf)))
L
Linus Torvalds 已提交
96 97 98
		goto out;

	c = token(fd, buf, len - 1, ' ');
J
Jeff Dike 已提交
99
	if (c < 0)
J
Jeff Dike 已提交
100
		goto out;
J
Jeff Dike 已提交
101 102
	else if (c != ' ') {
		printk(UM_KERN_ERR "Failed to find ' ' in /proc/cpuinfo\n");
L
Linus Torvalds 已提交
103 104 105
		goto out;
	}

J
Jeff Dike 已提交
106
	while (1) {
L
Linus Torvalds 已提交
107
		c = token(fd, buf, len - 1, ' ');
J
Jeff Dike 已提交
108
		if (c < 0)
J
Jeff Dike 已提交
109
			goto out;
J
Jeff Dike 已提交
110 111
		else if (c == '\n')
			break;
L
Linus Torvalds 已提交
112

J
Jeff Dike 已提交
113
		if (!strcmp(buf, feature)) {
L
Linus Torvalds 已提交
114 115 116 117 118
			*have_it = 1;
			goto out;
		}
	}
 out:
J
Jeff Dike 已提交
119
	if (*have_it == 0)
J
Jeff Dike 已提交
120
		printk("No\n");
J
Jeff Dike 已提交
121
	else if (*have_it == 1)
J
Jeff Dike 已提交
122
		printk("Yes\n");
L
Linus Torvalds 已提交
123
	os_close_file(fd);
124
	return 1;
L
Linus Torvalds 已提交
125 126
}

J
Jeff Dike 已提交
127 128
#if 0 /*
       * This doesn't work in tt mode, plus it's causing compilation problems
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140
       * for some people.
       */
static void disable_lcall(void)
{
	struct modify_ldt_ldt_s ldt;
	int err;

	bzero(&ldt, sizeof(ldt));
	ldt.entry_number = 7;
	ldt.base_addr = 0;
	ldt.limit = 0;
	err = modify_ldt(1, &ldt, sizeof(ldt));
J
Jeff Dike 已提交
141 142 143
	if (err)
		printk(UM_KERN_ERR "Failed to disable lcall7 - errno = %d\n",
		       errno);
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157
}
#endif

void arch_init_thread(void)
{
#if 0
	disable_lcall();
#endif
}

void arch_check_bugs(void)
{
	int have_it;

J
Jeff Dike 已提交
158 159 160
	if (os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0) {
		printk(UM_KERN_ERR "/proc/cpuinfo not available - skipping CPU "
		       "capability checks\n");
L
Linus Torvalds 已提交
161 162
		return;
	}
J
Jeff Dike 已提交
163
	if (check_cpu_flag("cmov", &have_it))
L
Linus Torvalds 已提交
164 165 166
		host_has_cmov = have_it;
}

167
int arch_handle_signal(int sig, struct uml_pt_regs *regs)
L
Linus Torvalds 已提交
168 169 170
{
	unsigned char tmp[2];

J
Jeff Dike 已提交
171 172
	/*
	 * This is testing for a cmov (0x0f 0x4x) instruction causing a
L
Linus Torvalds 已提交
173 174
	 * SIGILL in init.
	 */
J
Jeff Dike 已提交
175
	if ((sig != SIGILL) || (TASK_PID(get_current()) != 1))
J
Jeff Dike 已提交
176
		return 0;
L
Linus Torvalds 已提交
177 178 179

	if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2))
		panic("SIGILL in init, could not read instructions!\n");
J
Jeff Dike 已提交
180
	if ((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40))
J
Jeff Dike 已提交
181
		return 0;
L
Linus Torvalds 已提交
182

J
Jeff Dike 已提交
183
	if (host_has_cmov == 0)
L
Linus Torvalds 已提交
184 185 186
		panic("SIGILL caused by cmov, which this processor doesn't "
		      "implement, boot a filesystem compiled for older "
		      "processors");
J
Jeff Dike 已提交
187
	else if (host_has_cmov == 1)
L
Linus Torvalds 已提交
188 189
		panic("SIGILL caused by cmov, which this processor claims to "
		      "implement");
J
Jeff Dike 已提交
190
	else if (host_has_cmov == -1)
L
Linus Torvalds 已提交
191 192 193 194
		panic("SIGILL caused by cmov, couldn't tell if this processor "
		      "implements it, boot a filesystem compiled for older "
		      "processors");
	else panic("Bad value for host_has_cmov (%d)", host_has_cmov);
J
Jeff Dike 已提交
195
	return 0;
L
Linus Torvalds 已提交
196
}