Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 31 | n = os_read_file(fd, ptr, sizeof(*ptr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | c = *ptr++; |
| 33 | if(n != sizeof(*ptr)){ |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 34 | if(n == 0) |
| 35 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | printk("Reading /proc/cpuinfo failed, err = %d\n", -n); |
| 37 | if(n < 0) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 38 | return n; |
| 39 | else return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 45 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | *(ptr - 1) = '\0'; |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 48 | return c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 60 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | else if(c != ':'){ |
| 62 | printk("Failed to find ':' in /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 | |
| 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)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if(n != sizeof(c)){ |
| 72 | printk("Failed to find newline in " |
| 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 | } |
| 76 | } while(c != '\n'); |
| 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 | |
| 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); |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 90 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | *have_it = 0; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 94 | if(!find_cpuinfo_line(fd, "flags", buf, ARRAY_SIZE(buf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | goto out; |
| 96 | |
| 97 | c = token(fd, buf, len - 1, ' '); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 98 | if(c < 0) |
| 99 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, ' '); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 107 | if(c < 0) |
| 108 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | else if(c == '\n') break; |
| 110 | |
| 111 | if(!strcmp(buf, feature)){ |
| 112 | *have_it = 1; |
| 113 | goto out; |
| 114 | } |
| 115 | } |
| 116 | out: |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 117 | if(*have_it == 0) |
| 118 | printk("No\n"); |
| 119 | else if(*have_it == 1) |
| 120 | printk("Yes\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | os_close_file(fd); |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 122 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 172 | if((sig != SIGILL) || (TASK_PID(get_current()) != 1)) |
| 173 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 178 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Jeff Dike | a5ed1ff | 2007-05-06 14:50:58 -0700 | [diff] [blame] | 192 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |