| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*  | 
 | 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 { | 
 | 31 | 		n = os_read_file(fd, ptr, sizeof(*ptr)); | 
 | 32 | 		c = *ptr++; | 
 | 33 | 		if(n != sizeof(*ptr)){ | 
 | 34 | 			if(n == 0) return(0); | 
 | 35 | 			printk("Reading /proc/cpuinfo failed, err = %d\n", -n); | 
 | 36 | 			if(n < 0) | 
 | 37 | 				return(n); | 
 | 38 | 			else | 
 | 39 | 				return(-EIO); | 
 | 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); | 
 | 45 | 		return(-1); | 
 | 46 | 	} | 
 | 47 | 	*(ptr - 1) = '\0'; | 
 | 48 | 	return(c); | 
 | 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) | 
 | 60 | 			return(0); | 
 | 61 | 		else if(c != ':'){ | 
 | 62 | 			printk("Failed to find ':' in /proc/cpuinfo\n"); | 
 | 63 | 			return(0); | 
 | 64 | 		} | 
 | 65 |  | 
 | 66 | 		if(!strncmp(scratch, key, strlen(key))) | 
 | 67 | 			return(1); | 
 | 68 |  | 
 | 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); | 
 | 75 | 			} | 
 | 76 | 		} while(c != '\n'); | 
 | 77 | 	} | 
 | 78 | 	return(0); | 
 | 79 | } | 
 | 80 |  | 
 | 81 | int cpu_feature(char *what, char *buf, int len) | 
 | 82 | { | 
 | 83 | 	int fd, ret = 0; | 
 | 84 |  | 
 | 85 | 	fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0); | 
 | 86 | 	if(fd < 0){ | 
 | 87 | 		printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd); | 
 | 88 | 		return(0); | 
 | 89 | 	} | 
 | 90 |  | 
 | 91 | 	if(!find_cpuinfo_line(fd, what, buf, len)){ | 
 | 92 | 		printk("Couldn't find '%s' line in /proc/cpuinfo\n", what); | 
 | 93 | 		goto out_close; | 
 | 94 | 	} | 
 | 95 |  | 
 | 96 | 	token(fd, buf, len, '\n'); | 
 | 97 | 	ret = 1; | 
 | 98 |  | 
 | 99 |  out_close: | 
 | 100 | 	os_close_file(fd); | 
 | 101 | 	return(ret); | 
 | 102 | } | 
 | 103 |  | 
 | 104 | static int check_cpu_flag(char *feature, int *have_it) | 
 | 105 | { | 
 | 106 | 	char buf[MAXTOKEN], c; | 
 | 107 | 	int fd, len = sizeof(buf)/sizeof(buf[0]); | 
 | 108 |  | 
 | 109 | 	printk("Checking for host processor %s support...", feature); | 
 | 110 | 	fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0); | 
 | 111 | 	if(fd < 0){ | 
 | 112 | 		printk("Couldn't open /proc/cpuinfo, err = %d\n", -fd); | 
 | 113 | 		return(0); | 
 | 114 | 	} | 
 | 115 |  | 
 | 116 | 	*have_it = 0; | 
 | 117 | 	if(!find_cpuinfo_line(fd, "flags", buf, sizeof(buf) / sizeof(buf[0]))) | 
 | 118 | 		goto out; | 
 | 119 |  | 
 | 120 | 	c = token(fd, buf, len - 1, ' '); | 
 | 121 | 	if(c < 0) goto out; | 
 | 122 | 	else if(c != ' '){ | 
 | 123 | 		printk("Failed to find ' ' in /proc/cpuinfo\n"); | 
 | 124 | 		goto out; | 
 | 125 | 	} | 
 | 126 |  | 
 | 127 | 	while(1){ | 
 | 128 | 		c = token(fd, buf, len - 1, ' '); | 
 | 129 | 		if(c < 0) goto out; | 
 | 130 | 		else if(c == '\n') break; | 
 | 131 |  | 
 | 132 | 		if(!strcmp(buf, feature)){ | 
 | 133 | 			*have_it = 1; | 
 | 134 | 			goto out; | 
 | 135 | 		} | 
 | 136 | 	} | 
 | 137 |  out: | 
 | 138 | 	if(*have_it == 0) printk("No\n"); | 
 | 139 | 	else if(*have_it == 1) printk("Yes\n"); | 
 | 140 | 	os_close_file(fd); | 
 | 141 | 	return(1); | 
 | 142 | } | 
 | 143 |  | 
 | 144 | #if 0 /* This doesn't work in tt mode, plus it's causing compilation problems | 
 | 145 |        * for some people. | 
 | 146 |        */ | 
 | 147 | static void disable_lcall(void) | 
 | 148 | { | 
 | 149 | 	struct modify_ldt_ldt_s ldt; | 
 | 150 | 	int err; | 
 | 151 |  | 
 | 152 | 	bzero(&ldt, sizeof(ldt)); | 
 | 153 | 	ldt.entry_number = 7; | 
 | 154 | 	ldt.base_addr = 0; | 
 | 155 | 	ldt.limit = 0; | 
 | 156 | 	err = modify_ldt(1, &ldt, sizeof(ldt)); | 
 | 157 | 	if(err) | 
 | 158 | 		printk("Failed to disable lcall7 - errno = %d\n", errno); | 
 | 159 | } | 
 | 160 | #endif | 
 | 161 |  | 
 | 162 | void arch_init_thread(void) | 
 | 163 | { | 
 | 164 | #if 0 | 
 | 165 | 	disable_lcall(); | 
 | 166 | #endif | 
 | 167 | } | 
 | 168 |  | 
 | 169 | void arch_check_bugs(void) | 
 | 170 | { | 
 | 171 | 	int have_it; | 
 | 172 |  | 
 | 173 | 	if(os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0){ | 
 | 174 | 		printk("/proc/cpuinfo not available - skipping CPU capability " | 
 | 175 | 		       "checks\n"); | 
 | 176 | 		return; | 
 | 177 | 	} | 
 | 178 | 	if(check_cpu_flag("cmov", &have_it)) | 
 | 179 | 		host_has_cmov = have_it; | 
 | 180 | 	if(check_cpu_flag("xmm", &have_it)) | 
 | 181 | 		host_has_xmm = have_it; | 
 | 182 | } | 
 | 183 |  | 
 | 184 | int arch_handle_signal(int sig, union uml_pt_regs *regs) | 
 | 185 | { | 
 | 186 | 	unsigned char tmp[2]; | 
 | 187 |  | 
 | 188 | 	/* This is testing for a cmov (0x0f 0x4x) instruction causing a | 
 | 189 | 	 * SIGILL in init. | 
 | 190 | 	 */ | 
 | 191 | 	if((sig != SIGILL) || (TASK_PID(get_current()) != 1)) return(0); | 
 | 192 |  | 
 | 193 | 	if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2)) | 
 | 194 | 		panic("SIGILL in init, could not read instructions!\n"); | 
 | 195 | 	if((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40)) | 
 | 196 | 		return(0); | 
 | 197 |  | 
 | 198 | 	if(host_has_cmov == 0) | 
 | 199 | 		panic("SIGILL caused by cmov, which this processor doesn't " | 
 | 200 | 		      "implement, boot a filesystem compiled for older " | 
 | 201 | 		      "processors"); | 
 | 202 | 	else if(host_has_cmov == 1) | 
 | 203 | 		panic("SIGILL caused by cmov, which this processor claims to " | 
 | 204 | 		      "implement"); | 
 | 205 | 	else if(host_has_cmov == -1) | 
 | 206 | 		panic("SIGILL caused by cmov, couldn't tell if this processor " | 
 | 207 | 		      "implements it, boot a filesystem compiled for older " | 
 | 208 | 		      "processors"); | 
 | 209 | 	else panic("Bad value for host_has_cmov (%d)", host_has_cmov); | 
 | 210 | 	return(0); | 
 | 211 | } | 
 | 212 |  | 
 | 213 | /* | 
 | 214 |  * Overrides for Emacs so that we follow Linus's tabbing style. | 
 | 215 |  * Emacs will notice this stuff at the end of the file and automatically | 
 | 216 |  * adjust the settings for this buffer only.  This must remain at the end | 
 | 217 |  * of the file. | 
 | 218 |  * --------------------------------------------------------------------------- | 
 | 219 |  * Local variables: | 
 | 220 |  * c-file-style: "linux" | 
 | 221 |  * End: | 
 | 222 |  */ |