Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <errno.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 7 | #include <signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <string.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 9 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "os.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 11 | #include "task.h" |
| 12 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | #define MAXTOKEN 64 |
| 15 | |
| 16 | /* Set during early boot */ |
| 17 | int host_has_cmov = 1; |
| 18 | int host_has_xmm = 0; |
| 19 | |
| 20 | static char token(int fd, char *buf, int len, char stop) |
| 21 | { |
| 22 | int n; |
| 23 | char *ptr, *end, c; |
| 24 | |
| 25 | ptr = buf; |
| 26 | end = &buf[len]; |
| 27 | do { |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 28 | n = os_read_file(fd, ptr, sizeof(*ptr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | c = *ptr++; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 30 | if (n != sizeof(*ptr)) { |
| 31 | if (n == 0) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 32 | return 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 33 | printk(UM_KERN_ERR "Reading /proc/cpuinfo failed, " |
| 34 | "err = %d\n", -n); |
| 35 | if (n < 0) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 36 | return n; |
| 37 | else return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 39 | } while ((c != '\n') && (c != stop) && (ptr < end)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 41 | if (ptr == end) { |
| 42 | printk(UM_KERN_ERR "Failed to find '%c' in /proc/cpuinfo\n", |
| 43 | stop); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 44 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | *(ptr - 1) = '\0'; |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 47 | return c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static int find_cpuinfo_line(int fd, char *key, char *scratch, int len) |
| 51 | { |
| 52 | int n; |
| 53 | char c; |
| 54 | |
| 55 | scratch[len - 1] = '\0'; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 56 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | c = token(fd, scratch, len - 1, ':'); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 58 | if (c <= 0) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 59 | return 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 60 | else if (c != ':') { |
| 61 | printk(UM_KERN_ERR "Failed to find ':' in " |
| 62 | "/proc/cpuinfo\n"); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 63 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 66 | if (!strncmp(scratch, key, strlen(key))) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 67 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | do { |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 70 | n = os_read_file(fd, &c, sizeof(c)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 71 | if (n != sizeof(c)) { |
| 72 | printk(UM_KERN_ERR "Failed to find newline in " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | "/proc/cpuinfo, err = %d\n", -n); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 74 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 76 | } while (c != '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 78 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | static int check_cpu_flag(char *feature, int *have_it) |
| 82 | { |
| 83 | char buf[MAXTOKEN], c; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 84 | int fd, len = ARRAY_SIZE(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 86 | printk(UM_KERN_INFO "Checking for host processor %s support...", |
| 87 | feature); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | fd = os_open_file("/proc/cpuinfo", of_read(OPENFLAGS()), 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 89 | if (fd < 0) { |
| 90 | printk(UM_KERN_ERR "Couldn't open /proc/cpuinfo, err = %d\n", |
| 91 | -fd); |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 92 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | *have_it = 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 96 | if (!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | goto out; |
| 98 | |
| 99 | c = token(fd, buf, len - 1, ' '); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 100 | if (c < 0) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 101 | goto out; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 102 | else if (c != ' ') { |
| 103 | printk(UM_KERN_ERR "Failed to find ' ' in /proc/cpuinfo\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | goto out; |
| 105 | } |
| 106 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 107 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | c = token(fd, buf, len - 1, ' '); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 109 | if (c < 0) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 110 | goto out; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 111 | else if (c == '\n') |
| 112 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 114 | if (!strcmp(buf, feature)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | *have_it = 1; |
| 116 | goto out; |
| 117 | } |
| 118 | } |
| 119 | out: |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 120 | if (*have_it == 0) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 121 | printk("No\n"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 122 | else if (*have_it == 1) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 123 | printk("Yes\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | os_close_file(fd); |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 125 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 128 | #if 0 /* |
| 129 | * This doesn't work in tt mode, plus it's causing compilation problems |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | * for some people. |
| 131 | */ |
| 132 | static void disable_lcall(void) |
| 133 | { |
| 134 | struct modify_ldt_ldt_s ldt; |
| 135 | int err; |
| 136 | |
| 137 | bzero(&ldt, sizeof(ldt)); |
| 138 | ldt.entry_number = 7; |
| 139 | ldt.base_addr = 0; |
| 140 | ldt.limit = 0; |
| 141 | err = modify_ldt(1, &ldt, sizeof(ldt)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 142 | if (err) |
| 143 | printk(UM_KERN_ERR "Failed to disable lcall7 - errno = %d\n", |
| 144 | errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | #endif |
| 147 | |
| 148 | void arch_init_thread(void) |
| 149 | { |
| 150 | #if 0 |
| 151 | disable_lcall(); |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | void arch_check_bugs(void) |
| 156 | { |
| 157 | int have_it; |
| 158 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 159 | if (os_access("/proc/cpuinfo", OS_ACC_R_OK) < 0) { |
| 160 | printk(UM_KERN_ERR "/proc/cpuinfo not available - skipping CPU " |
| 161 | "capability checks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return; |
| 163 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 164 | if (check_cpu_flag("cmov", &have_it)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | host_has_cmov = have_it; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 166 | if (check_cpu_flag("xmm", &have_it)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | host_has_xmm = have_it; |
| 168 | } |
| 169 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 170 | int arch_handle_signal(int sig, struct uml_pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | unsigned char tmp[2]; |
| 173 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 174 | /* |
| 175 | * This is testing for a cmov (0x0f 0x4x) instruction causing a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * SIGILL in init. |
| 177 | */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 178 | if ((sig != SIGILL) || (TASK_PID(get_current()) != 1)) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 179 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | if (copy_from_user_proc(tmp, (void *) UPT_IP(regs), 2)) |
| 182 | panic("SIGILL in init, could not read instructions!\n"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 183 | if ((tmp[0] != 0x0f) || ((tmp[1] & 0xf0) != 0x40)) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 184 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 186 | if (host_has_cmov == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | panic("SIGILL caused by cmov, which this processor doesn't " |
| 188 | "implement, boot a filesystem compiled for older " |
| 189 | "processors"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 190 | else if (host_has_cmov == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | panic("SIGILL caused by cmov, which this processor claims to " |
| 192 | "implement"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame^] | 193 | else if (host_has_cmov == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | panic("SIGILL caused by cmov, couldn't tell if this processor " |
| 195 | "implements it, boot a filesystem compiled for older " |
| 196 | "processors"); |
| 197 | else panic("Bad value for host_has_cmov (%d)", host_has_cmov); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 198 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |