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