]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cpuctl/cpuctl.c
MFC r327597:
[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_eval_cpu_features(int cpu, struct thread *td);
75 static int cpuctl_do_update(int cpu, cpuctl_update_args_t *data,
76     struct thread *td);
77 static int update_intel(int cpu, cpuctl_update_args_t *args,
78     struct thread *td);
79 static int update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td);
80 static int update_via(int cpu, cpuctl_update_args_t *args,
81     struct thread *td);
82
83 static struct cdev **cpuctl_devs;
84 static MALLOC_DEFINE(M_CPUCTL, "cpuctl", "CPUCTL buffer");
85
86 static struct cdevsw cpuctl_cdevsw = {
87         .d_version =    D_VERSION,
88         .d_open =       cpuctl_open,
89         .d_ioctl =      cpuctl_ioctl,
90         .d_name =       "cpuctl",
91 };
92
93 /*
94  * This function checks if specified cpu enabled or not.
95  */
96 static int
97 cpu_enabled(int cpu)
98 {
99
100         return (pmc_cpu_is_disabled(cpu) == 0);
101 }
102
103 /*
104  * Check if the current thread is bound to a specific cpu.
105  */
106 static int
107 cpu_sched_is_bound(struct thread *td)
108 {
109         int ret;
110
111         thread_lock(td);
112         ret = sched_is_bound(td);
113         thread_unlock(td);
114         return (ret);
115 }
116
117 /*
118  * Switch to target cpu to run.
119  */
120 static void
121 set_cpu(int cpu, struct thread *td)
122 {
123
124         KASSERT(cpu >= 0 && cpu <= mp_maxid && cpu_enabled(cpu),
125             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
126         thread_lock(td);
127         sched_bind(td, cpu);
128         thread_unlock(td);
129         KASSERT(td->td_oncpu == cpu,
130             ("[cpuctl,%d]: cannot bind to target cpu %d on cpu %d", __LINE__,
131             cpu, td->td_oncpu));
132 }
133
134 static void
135 restore_cpu(int oldcpu, int is_bound, struct thread *td)
136 {
137
138         KASSERT(oldcpu >= 0 && oldcpu <= mp_maxid && cpu_enabled(oldcpu),
139             ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu));
140         thread_lock(td);
141         if (is_bound == 0)
142                 sched_unbind(td);
143         else
144                 sched_bind(td, oldcpu);
145         thread_unlock(td);
146 }
147
148 int
149 cpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
150     int flags, struct thread *td)
151 {
152         int cpu, ret;
153
154         cpu = dev2unit(dev);
155         if (cpu > mp_maxid || !cpu_enabled(cpu)) {
156                 DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu);
157                 return (ENXIO);
158         }
159         /* Require write flag for "write" requests. */
160         if ((cmd == CPUCTL_MSRCBIT || cmd == CPUCTL_MSRSBIT ||
161             cmd == CPUCTL_UPDATE || cmd == CPUCTL_WRMSR ||
162             cmd == CPUCTL_EVAL_CPU_FEATURES) &&
163             (flags & FWRITE) == 0)
164                 return (EPERM);
165         switch (cmd) {
166         case CPUCTL_RDMSR:
167                 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
168                 break;
169         case CPUCTL_MSRSBIT:
170         case CPUCTL_MSRCBIT:
171         case CPUCTL_WRMSR:
172                 ret = priv_check(td, PRIV_CPUCTL_WRMSR);
173                 if (ret != 0)
174                         goto fail;
175                 ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
176                 break;
177         case CPUCTL_CPUID:
178                 ret = cpuctl_do_cpuid(cpu, (cpuctl_cpuid_args_t *)data, td);
179                 break;
180         case CPUCTL_UPDATE:
181                 ret = priv_check(td, PRIV_CPUCTL_UPDATE);
182                 if (ret != 0)
183                         goto fail;
184                 ret = cpuctl_do_update(cpu, (cpuctl_update_args_t *)data, td);
185                 break;
186         case CPUCTL_CPUID_COUNT:
187                 ret = cpuctl_do_cpuid_count(cpu,
188                     (cpuctl_cpuid_count_args_t *)data, td);
189                 break;
190         case CPUCTL_EVAL_CPU_FEATURES:
191                 ret = cpuctl_do_eval_cpu_features(cpu, td);
192                 break;
193         default:
194                 ret = EINVAL;
195                 break;
196         }
197 fail:
198         return (ret);
199 }
200
201 /*
202  * Actually perform cpuid operation.
203  */
204 static int
205 cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
206     struct thread *td)
207 {
208         int is_bound = 0;
209         int oldcpu;
210
211         KASSERT(cpu >= 0 && cpu <= mp_maxid,
212             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
213
214         /* Explicitly clear cpuid data to avoid returning stale info. */
215         bzero(data->data, sizeof(data->data));
216         DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n",
217             __LINE__, data->level, data->level_type, cpu);
218 #ifdef __i386__
219         if (cpu_id == 0)
220                 return (ENODEV);
221 #endif
222         oldcpu = td->td_oncpu;
223         is_bound = cpu_sched_is_bound(td);
224         set_cpu(cpu, td);
225         cpuid_count(data->level, data->level_type, data->data);
226         restore_cpu(oldcpu, is_bound, td);
227         return (0);
228 }
229
230 static int
231 cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data, struct thread *td)
232 {
233         cpuctl_cpuid_count_args_t cdata;
234         int error;
235
236         cdata.level = data->level;
237         /* Override the level type. */
238         cdata.level_type = 0;
239         error = cpuctl_do_cpuid_count(cpu, &cdata, td);
240         bcopy(cdata.data, data->data, sizeof(data->data)); /* Ignore error */
241         return (error);
242 }
243
244 /*
245  * Actually perform MSR operations.
246  */
247 static int
248 cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
249 {
250         uint64_t reg;
251         int is_bound = 0;
252         int oldcpu;
253         int ret;
254
255         KASSERT(cpu >= 0 && cpu <= mp_maxid,
256             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
257
258         /*
259          * Explicitly clear cpuid data to avoid returning stale
260          * info
261          */
262         DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
263             data->msr, cpu);
264 #ifdef __i386__
265         if ((cpu_feature & CPUID_MSR) == 0)
266                 return (ENODEV);
267 #endif
268         oldcpu = td->td_oncpu;
269         is_bound = cpu_sched_is_bound(td);
270         set_cpu(cpu, td);
271         if (cmd == CPUCTL_RDMSR) {
272                 data->data = 0;
273                 ret = rdmsr_safe(data->msr, &data->data);
274         } else if (cmd == CPUCTL_WRMSR) {
275                 ret = wrmsr_safe(data->msr, data->data);
276         } else if (cmd == CPUCTL_MSRSBIT) {
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 if (cmd == CPUCTL_MSRCBIT) {
283                 critical_enter();
284                 ret = rdmsr_safe(data->msr, &reg);
285                 if (ret == 0)
286                         ret = wrmsr_safe(data->msr, reg & ~data->data);
287                 critical_exit();
288         } else
289                 panic("[cpuctl,%d]: unknown operation requested: %lu",
290                     __LINE__, cmd);
291         restore_cpu(oldcpu, is_bound, td);
292         return (ret);
293 }
294
295 /*
296  * Actually perform microcode update.
297  */
298 static int
299 cpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td)
300 {
301         cpuctl_cpuid_args_t args = {
302                 .level = 0,
303         };
304         char vendor[13];
305         int ret;
306
307         KASSERT(cpu >= 0 && cpu <= mp_maxid,
308             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
309         DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu);
310
311         ret = cpuctl_do_cpuid(cpu, &args, td);
312         if (ret != 0)
313                 return (ret);
314         ((uint32_t *)vendor)[0] = args.data[1];
315         ((uint32_t *)vendor)[1] = args.data[3];
316         ((uint32_t *)vendor)[2] = args.data[2];
317         vendor[12] = '\0';
318         if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) == 0)
319                 ret = update_intel(cpu, data, td);
320         else if(strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) == 0)
321                 ret = update_amd(cpu, data, td);
322         else if(strncmp(vendor, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID))
323             == 0)
324                 ret = update_via(cpu, data, td);
325         else
326                 ret = ENXIO;
327         return (ret);
328 }
329
330 static int
331 update_intel(int cpu, cpuctl_update_args_t *args, struct thread *td)
332 {
333         void *ptr;
334         uint64_t rev0, rev1;
335         uint32_t tmp[4];
336         int is_bound;
337         int oldcpu;
338         int ret;
339
340         if (args->size == 0 || args->data == NULL) {
341                 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
342                 return (EINVAL);
343         }
344         if (args->size > UCODE_SIZE_MAX) {
345                 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
346                 return (EINVAL);
347         }
348
349         /*
350          * 16 byte alignment required.  Rely on the fact that
351          * malloc(9) always returns the pointer aligned at least on
352          * the size of the allocation.
353          */
354         ptr = malloc(args->size + 16, M_CPUCTL, M_WAITOK);
355         if (copyin(args->data, ptr, args->size) != 0) {
356                 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
357                     __LINE__, args->data, ptr, args->size);
358                 ret = EFAULT;
359                 goto fail;
360         }
361         oldcpu = td->td_oncpu;
362         is_bound = cpu_sched_is_bound(td);
363         set_cpu(cpu, td);
364         critical_enter();
365         rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
366
367         /*
368          * Perform update.
369          */
370         wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
371         wrmsr_safe(MSR_BIOS_SIGN, 0);
372
373         /*
374          * Serialize instruction flow.
375          */
376         do_cpuid(0, tmp);
377         critical_exit();
378         rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
379         restore_cpu(oldcpu, is_bound, td);
380         if (rev1 > rev0)
381                 ret = 0;
382         else
383                 ret = EEXIST;
384 fail:
385         free(ptr, M_CPUCTL);
386         return (ret);
387 }
388
389 /*
390  * NB: MSR 0xc0010020, MSR_K8_UCODE_UPDATE, is not documented by AMD.
391  * Coreboot, illumos and Linux source code was used to understand
392  * its workings.
393  */
394 static void
395 amd_ucode_wrmsr(void *ucode_ptr)
396 {
397         uint32_t tmp[4];
398
399         wrmsr_safe(MSR_K8_UCODE_UPDATE, (uintptr_t)ucode_ptr);
400         do_cpuid(0, tmp);
401 }
402
403 static int
404 update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td)
405 {
406         void *ptr;
407         int ret;
408
409         if (args->size == 0 || args->data == NULL) {
410                 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
411                 return (EINVAL);
412         }
413         if (args->size > UCODE_SIZE_MAX) {
414                 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
415                 return (EINVAL);
416         }
417
418         /*
419          * 16 byte alignment required.  Rely on the fact that
420          * malloc(9) always returns the pointer aligned at least on
421          * the size of the allocation.
422          */
423         ptr = malloc(args->size + 16, M_CPUCTL, M_ZERO | M_WAITOK);
424         if (copyin(args->data, ptr, args->size) != 0) {
425                 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
426                     __LINE__, args->data, ptr, args->size);
427                 ret = EFAULT;
428                 goto fail;
429         }
430         smp_rendezvous(NULL, amd_ucode_wrmsr, NULL, ptr);
431         ret = 0;
432 fail:
433         free(ptr, M_CPUCTL);
434         return (ret);
435 }
436
437 static int
438 update_via(int cpu, cpuctl_update_args_t *args, struct thread *td)
439 {
440         void *ptr;
441         uint64_t rev0, rev1, res;
442         uint32_t tmp[4];
443         int is_bound;
444         int oldcpu;
445         int ret;
446
447         if (args->size == 0 || args->data == NULL) {
448                 DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
449                 return (EINVAL);
450         }
451         if (args->size > UCODE_SIZE_MAX) {
452                 DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
453                 return (EINVAL);
454         }
455
456         /*
457          * 4 byte alignment required.
458          */
459         ptr = malloc(args->size, M_CPUCTL, M_WAITOK);
460         if (copyin(args->data, ptr, args->size) != 0) {
461                 DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
462                     __LINE__, args->data, ptr, args->size);
463                 ret = EFAULT;
464                 goto fail;
465         }
466         oldcpu = td->td_oncpu;
467         is_bound = cpu_sched_is_bound(td);
468         set_cpu(cpu, td);
469         critical_enter();
470         rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
471
472         /*
473          * Perform update.
474          */
475         wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
476         do_cpuid(1, tmp);
477
478         /*
479          * Result are in low byte of MSR FCR5:
480          * 0x00: No update has been attempted since RESET.
481          * 0x01: The last attempted update was successful.
482          * 0x02: The last attempted update was unsuccessful due to a bad
483          *       environment. No update was loaded and any preexisting
484          *       patches are still active.
485          * 0x03: The last attempted update was not applicable to this processor.
486          *       No update was loaded and any preexisting patches are still
487          *       active.
488          * 0x04: The last attempted update was not successful due to an invalid
489          *       update data block. No update was loaded and any preexisting
490          *       patches are still active
491          */
492         rdmsr_safe(0x1205, &res);
493         res &= 0xff;
494         critical_exit();
495         rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
496         restore_cpu(oldcpu, is_bound, td);
497
498         DPRINTF("[cpu,%d]: rev0=%x rev1=%x res=%x\n", __LINE__,
499             (unsigned)(rev0 >> 32), (unsigned)(rev1 >> 32), (unsigned)res);
500
501         if (res != 0x01)
502                 ret = EINVAL;
503         else
504                 ret = 0;
505 fail:
506         free(ptr, M_CPUCTL);
507         return (ret);
508 }
509
510 static int
511 cpuctl_do_eval_cpu_features(int cpu, struct thread *td)
512 {
513         int is_bound = 0;
514         int oldcpu;
515
516         KASSERT(cpu >= 0 && cpu <= mp_maxid,
517             ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
518
519 #ifdef __i386__
520         if (cpu_id == 0)
521                 return (ENODEV);
522 #endif
523         oldcpu = td->td_oncpu;
524         is_bound = cpu_sched_is_bound(td);
525         set_cpu(cpu, td);
526         identify_cpu1();
527         identify_cpu2();
528         restore_cpu(oldcpu, is_bound, td);
529         return (0);
530 }
531
532
533 int
534 cpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
535 {
536         int ret = 0;
537         int cpu;
538
539         cpu = dev2unit(dev);
540         if (cpu > mp_maxid || !cpu_enabled(cpu)) {
541                 DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__,
542                     cpu);
543                 return (ENXIO);
544         }
545         if (flags & FWRITE)
546                 ret = securelevel_gt(td->td_ucred, 0);
547         return (ret);
548 }
549
550 static int
551 cpuctl_modevent(module_t mod __unused, int type, void *data __unused)
552 {
553         int cpu;
554
555         switch(type) {
556         case MOD_LOAD:
557                 if (bootverbose)
558                         printf("cpuctl: access to MSR registers/cpuid info.\n");
559                 cpuctl_devs = malloc(sizeof(*cpuctl_devs) * (mp_maxid + 1), M_CPUCTL,
560                     M_WAITOK | M_ZERO);
561                 CPU_FOREACH(cpu)
562                         if (cpu_enabled(cpu))
563                                 cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu,
564                                     UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu);
565                 break;
566         case MOD_UNLOAD:
567                 CPU_FOREACH(cpu) {
568                         if (cpuctl_devs[cpu] != NULL)
569                                 destroy_dev(cpuctl_devs[cpu]);
570                 }
571                 free(cpuctl_devs, M_CPUCTL);
572                 break;
573         case MOD_SHUTDOWN:
574                 break;
575         default:
576                 return (EOPNOTSUPP);
577         }
578         return (0);
579 }
580
581 DEV_MODULE(cpuctl, cpuctl_modevent, NULL);
582 MODULE_VERSION(cpuctl, CPUCTL_VERSION);