]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cpuctl/cpuctl.c
MFC r315586:
[FreeBSD/FreeBSD.git] / sys / dev / cpuctl / cpuctl.c
1 /*-
2  * Copyright (c) 2006-2008 Stanislav Sedov <stas@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/fcntl.h>
35 #include <sys/ioccom.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/priv.h>
40 #include <sys/proc.h>
41 #include <sys/queue.h>
42 #include <sys/sched.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/uio.h>
46 #include <sys/pcpu.h>
47 #include <sys/smp.h>
48 #include <sys/pmckern.h>
49 #include <sys/cpuctl.h>
50
51 #include <machine/cpufunc.h>
52 #include <machine/md_var.h>
53 #include <machine/specialreg.h>
54
55 static d_open_t cpuctl_open;
56 static d_ioctl_t cpuctl_ioctl;
57
58 #define CPUCTL_VERSION 1
59
60 #ifdef CPUCTL_DEBUG
61 # define        DPRINTF(format,...) printf(format, __VA_ARGS__);
62 #else
63 # define        DPRINTF(...)
64 #endif
65
66 #define UCODE_SIZE_MAX  (4 * 1024 * 1024)
67
68 static int cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd,
69     struct thread *td);
70 static int cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data,
71     struct thread *td);
72 static int cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
73     struct thread *td);
74 static int cpuctl_do_update(int cpu, cpuctl_update_args_t *data,
75     struct thread *td);
76 static int update_intel(int cpu, cpuctl_update_args_t *args,
77     struct thread *td);
78 static int update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td);
79 static int update_via(int cpu, cpuctl_update_args_t *args,
80     struct thread *td);
81
82 static struct cdev **cpuctl_devs;
83 static MALLOC_DEFINE(M_CPUCTL, "cpuctl", "CPUCTL buffer");
84
85 static struct cdevsw cpuctl_cdevsw = {
86         .d_version =    D_VERSION,
87         .d_open =       cpuctl_open,
88         .d_ioctl =      cpuctl_ioctl,
89         .d_name =       "cpuctl",
90 };
91
92 /*
93  * This function checks if specified cpu enabled or not.
94  */
95 static int
96 cpu_enabled(int cpu)
97 {
98
99         return (pmc_cpu_is_disabled(cpu) == 0);
100 }
101
102 /*
103  * Check if the current thread is bound to a specific cpu.
104  */
105 static int
106 cpu_sched_is_bound(struct thread *td)
107 {
108         int ret;
109
110         thread_lock(td);
111         ret = sched_is_bound(td);
112         thread_unlock(td);
113         return (ret);
114 }
115
116 /*
117  * Switch to target cpu to run.
118  */
119 static void
120 set_cpu(int cpu, struct thread *td)
121 {
122
123         KASSERT(cpu >= 0 && cpu <= mp_maxid && cpu_enabled(cpu),
124             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
125         thread_lock(td);
126         sched_bind(td, cpu);
127         thread_unlock(td);
128         KASSERT(td->td_oncpu == cpu,
129             ("[cpuctl,%d]: cannot bind to target cpu %d on cpu %d", __LINE__,
130             cpu, td->td_oncpu));
131 }
132
133 static void
134 restore_cpu(int oldcpu, int is_bound, struct thread *td)
135 {
136
137         KASSERT(oldcpu >= 0 && oldcpu <= mp_maxid && cpu_enabled(oldcpu),
138             ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu));
139         thread_lock(td);
140         if (is_bound == 0)
141                 sched_unbind(td);
142         else
143                 sched_bind(td, oldcpu);
144         thread_unlock(td);
145 }
146
147 int
148 cpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
149     int flags, struct thread *td)
150 {
151         int cpu, ret;
152
153         cpu = dev2unit(dev);
154         if (cpu > mp_maxid || !cpu_enabled(cpu)) {
155                 DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu);
156                 return (ENXIO);
157         }
158         /* Require write flag for "write" requests. */
159         if ((cmd == CPUCTL_WRMSR || cmd == CPUCTL_UPDATE) &&
160             ((flags & FWRITE) == 0))
161                 return (EPERM);
162         switch (cmd) {
163         case CPUCTL_RDMSR:
164                 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
165                 break;
166         case CPUCTL_MSRSBIT:
167         case CPUCTL_MSRCBIT:
168         case CPUCTL_WRMSR:
169                 ret = priv_check(td, PRIV_CPUCTL_WRMSR);
170                 if (ret != 0)
171                         goto fail;
172                 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
173                 break;
174         case CPUCTL_CPUID:
175                 ret = cpuctl_do_cpuid(cpu, (cpuctl_cpuid_args_t *)data, td);
176                 break;
177         case CPUCTL_UPDATE:
178                 ret = priv_check(td, PRIV_CPUCTL_UPDATE);
179                 if (ret != 0)
180                         goto fail;
181                 ret = cpuctl_do_update(cpu, (cpuctl_update_args_t *)data, td);
182                 break;
183         case CPUCTL_CPUID_COUNT:
184                 ret = cpuctl_do_cpuid_count(cpu,
185                     (cpuctl_cpuid_count_args_t *)data, td);
186                 break;
187         default:
188                 ret = EINVAL;
189                 break;
190         }
191 fail:
192         return (ret);
193 }
194
195 /*
196  * Actually perform cpuid operation.
197  */
198 static int
199 cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
200     struct thread *td)
201 {
202         int is_bound = 0;
203         int oldcpu;
204
205         KASSERT(cpu >= 0 && cpu <= mp_maxid,
206             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
207
208         /* Explicitly clear cpuid data to avoid returning stale info. */
209         bzero(data->data, sizeof(data->data));
210         DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n",
211             __LINE__, data->level, data->level_type, cpu);
212 #ifdef __i386__
213         if (cpu_id == 0)
214                 return (ENODEV);
215 #endif
216         oldcpu = td->td_oncpu;
217         is_bound = cpu_sched_is_bound(td);
218         set_cpu(cpu, td);
219         cpuid_count(data->level, data->level_type, data->data);
220         restore_cpu(oldcpu, is_bound, td);
221         return (0);
222 }
223
224 static int
225 cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data, struct thread *td)
226 {
227         cpuctl_cpuid_count_args_t cdata;
228         int error;
229
230         cdata.level = data->level;
231         /* Override the level type. */
232         cdata.level_type = 0;
233         error = cpuctl_do_cpuid_count(cpu, &cdata, td);
234         bcopy(cdata.data, data->data, sizeof(data->data)); /* Ignore error */
235         return (error);
236 }
237
238 /*
239  * Actually perform MSR operations.
240  */
241 static int
242 cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
243 {
244         uint64_t reg;
245         int is_bound = 0;
246         int oldcpu;
247         int ret;
248
249         KASSERT(cpu >= 0 && cpu <= mp_maxid,
250             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
251
252         /*
253          * Explicitly clear cpuid data to avoid returning stale
254          * info
255          */
256         DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
257             data->msr, cpu);
258 #ifdef __i386__
259         if ((cpu_feature & CPUID_MSR) == 0)
260                 return (ENODEV);
261 #endif
262         oldcpu = td->td_oncpu;
263         is_bound = cpu_sched_is_bound(td);
264         set_cpu(cpu, td);
265         if (cmd == CPUCTL_RDMSR) {
266                 data->data = 0;
267                 ret = rdmsr_safe(data->msr, &data->data);
268         } else if (cmd == CPUCTL_WRMSR) {
269                 ret = wrmsr_safe(data->msr, data->data);
270         } else if (cmd == CPUCTL_MSRSBIT) {
271                 critical_enter();
272                 ret = rdmsr_safe(data->msr, &reg);
273                 if (ret == 0)
274                         ret = wrmsr_safe(data->msr, reg | data->data);
275                 critical_exit();
276         } else if (cmd == CPUCTL_MSRCBIT) {
277                 critical_enter();
278                 ret = rdmsr_safe(data->msr, &reg);
279                 if (ret == 0)
280                         ret = wrmsr_safe(data->msr, reg & ~data->data);
281                 critical_exit();
282         } else
283                 panic("[cpuctl,%d]: unknown operation requested: %lu",
284                     __LINE__, cmd);
285         restore_cpu(oldcpu, is_bound, td);
286         return (ret);
287 }
288
289 /*
290  * Actually perform microcode update.
291  */
292 static int
293 cpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td)
294 {
295         cpuctl_cpuid_args_t args = {
296                 .level = 0,
297         };
298         char vendor[13];
299         int ret;
300
301         KASSERT(cpu >= 0 && cpu <= mp_maxid,
302             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
303         DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu);
304
305         ret = cpuctl_do_cpuid(cpu, &args, td);
306         if (ret != 0)
307                 return (ret);
308         ((uint32_t *)vendor)[0] = args.data[1];
309         ((uint32_t *)vendor)[1] = args.data[3];
310         ((uint32_t *)vendor)[2] = args.data[2];
311         vendor[12] = '\0';
312         if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) == 0)
313                 ret = update_intel(cpu, data, td);
314         else if(strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) == 0)
315                 ret = update_amd(cpu, data, td);
316         else if(strncmp(vendor, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID))
317             == 0)
318                 ret = update_via(cpu, data, td);
319         else
320                 ret = ENXIO;
321         return (ret);
322 }
323
324 static int
325 update_intel(int cpu, cpuctl_update_args_t *args, struct thread *td)
326 {
327         void *ptr;
328         uint64_t rev0, rev1;
329         uint32_t tmp[4];
330         int is_bound;
331         int oldcpu;
332         int ret;
333
334         if (args->size == 0 || args->data == NULL) {
335                 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
336                 return (EINVAL);
337         }
338         if (args->size > UCODE_SIZE_MAX) {
339                 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
340                 return (EINVAL);
341         }
342
343         /*
344          * 16 byte alignment required.  Rely on the fact that
345          * malloc(9) always returns the pointer aligned at least on
346          * the size of the allocation.
347          */
348         ptr = malloc(args->size + 16, M_CPUCTL, M_WAITOK);
349         if (copyin(args->data, ptr, args->size) != 0) {
350                 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
351                     __LINE__, args->data, ptr, args->size);
352                 ret = EFAULT;
353                 goto fail;
354         }
355         oldcpu = td->td_oncpu;
356         is_bound = cpu_sched_is_bound(td);
357         set_cpu(cpu, td);
358         critical_enter();
359         rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
360
361         /*
362          * Perform update.
363          */
364         wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
365         wrmsr_safe(MSR_BIOS_SIGN, 0);
366
367         /*
368          * Serialize instruction flow.
369          */
370         do_cpuid(0, tmp);
371         critical_exit();
372         rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
373         restore_cpu(oldcpu, is_bound, td);
374         if (rev1 > rev0)
375                 ret = 0;
376         else
377                 ret = EEXIST;
378 fail:
379         free(ptr, M_CPUCTL);
380         return (ret);
381 }
382
383 /*
384  * NB: MSR 0xc0010020, MSR_K8_UCODE_UPDATE, is not documented by AMD.
385  * Coreboot, illumos and Linux source code was used to understand
386  * its workings.
387  */
388 static void
389 amd_ucode_wrmsr(void *ucode_ptr)
390 {
391         uint32_t tmp[4];
392
393         wrmsr_safe(MSR_K8_UCODE_UPDATE, (uintptr_t)ucode_ptr);
394         do_cpuid(0, tmp);
395 }
396
397 static int
398 update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td)
399 {
400         void *ptr;
401         int ret;
402
403         if (args->size == 0 || args->data == NULL) {
404                 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
405                 return (EINVAL);
406         }
407         if (args->size > UCODE_SIZE_MAX) {
408                 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
409                 return (EINVAL);
410         }
411
412         /*
413          * 16 byte alignment required.  Rely on the fact that
414          * malloc(9) always returns the pointer aligned at least on
415          * the size of the allocation.
416          */
417         ptr = malloc(args->size + 16, M_CPUCTL, M_ZERO | M_WAITOK);
418         if (copyin(args->data, ptr, args->size) != 0) {
419                 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
420                     __LINE__, args->data, ptr, args->size);
421                 ret = EFAULT;
422                 goto fail;
423         }
424         smp_rendezvous(NULL, amd_ucode_wrmsr, NULL, ptr);
425         ret = 0;
426 fail:
427         free(ptr, M_CPUCTL);
428         return (ret);
429 }
430
431 static int
432 update_via(int cpu, cpuctl_update_args_t *args, struct thread *td)
433 {
434         void *ptr;
435         uint64_t rev0, rev1, res;
436         uint32_t tmp[4];
437         int is_bound;
438         int oldcpu;
439         int ret;
440
441         if (args->size == 0 || args->data == NULL) {
442                 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
443                 return (EINVAL);
444         }
445         if (args->size > UCODE_SIZE_MAX) {
446                 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
447                 return (EINVAL);
448         }
449
450         /*
451          * 4 byte alignment required.
452          */
453         ptr = malloc(args->size, M_CPUCTL, M_WAITOK);
454         if (copyin(args->data, ptr, args->size) != 0) {
455                 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
456                     __LINE__, args->data, ptr, args->size);
457                 ret = EFAULT;
458                 goto fail;
459         }
460         oldcpu = td->td_oncpu;
461         is_bound = cpu_sched_is_bound(td);
462         set_cpu(cpu, td);
463         critical_enter();
464         rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
465
466         /*
467          * Perform update.
468          */
469         wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
470         do_cpuid(1, tmp);
471
472         /*
473          * Result are in low byte of MSR FCR5:
474          * 0x00: No update has been attempted since RESET.
475          * 0x01: The last attempted update was successful.
476          * 0x02: The last attempted update was unsuccessful due to a bad
477          *       environment. No update was loaded and any preexisting
478          *       patches are still active.
479          * 0x03: The last attempted update was not applicable to this processor.
480          *       No update was loaded and any preexisting patches are still
481          *       active.
482          * 0x04: The last attempted update was not successful due to an invalid
483          *       update data block. No update was loaded and any preexisting
484          *       patches are still active
485          */
486         rdmsr_safe(0x1205, &res);
487         res &= 0xff;
488         critical_exit();
489         rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
490         restore_cpu(oldcpu, is_bound, td);
491
492         DPRINTF("[cpu,%d]: rev0=%x rev1=%x res=%x\n", __LINE__,
493             (unsigned)(rev0 >> 32), (unsigned)(rev1 >> 32), (unsigned)res);
494
495         if (res != 0x01)
496                 ret = EINVAL;
497         else
498                 ret = 0;
499 fail:
500         free(ptr, M_CPUCTL);
501         return (ret);
502 }
503
504 int
505 cpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
506 {
507         int ret = 0;
508         int cpu;
509
510         cpu = dev2unit(dev);
511         if (cpu > mp_maxid || !cpu_enabled(cpu)) {
512                 DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__,
513                     cpu);
514                 return (ENXIO);
515         }
516         if (flags & FWRITE)
517                 ret = securelevel_gt(td->td_ucred, 0);
518         return (ret);
519 }
520
521 static int
522 cpuctl_modevent(module_t mod __unused, int type, void *data __unused)
523 {
524         int cpu;
525
526         switch(type) {
527         case MOD_LOAD:
528                 if (bootverbose)
529                         printf("cpuctl: access to MSR registers/cpuid info.\n");
530                 cpuctl_devs = malloc(sizeof(*cpuctl_devs) * (mp_maxid + 1), M_CPUCTL,
531                     M_WAITOK | M_ZERO);
532                 CPU_FOREACH(cpu)
533                         if (cpu_enabled(cpu))
534                                 cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu,
535                                     UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu);
536                 break;
537         case MOD_UNLOAD:
538                 CPU_FOREACH(cpu) {
539                         if (cpuctl_devs[cpu] != NULL)
540                                 destroy_dev(cpuctl_devs[cpu]);
541                 }
542                 free(cpuctl_devs, M_CPUCTL);
543                 break;
544         case MOD_SHUTDOWN:
545                 break;
546         default:
547                 return (EOPNOTSUPP);
548         }
549         return (0);
550 }
551
552 DEV_MODULE(cpuctl, cpuctl_modevent, NULL);
553 MODULE_VERSION(cpuctl, CPUCTL_VERSION);