cpuctl.c (301962) | cpuctl.c (302372) |
---|---|
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 --- 12 unchanged lines hidden (view full) --- 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> | 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 --- 12 unchanged lines hidden (view full) --- 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: head/sys/dev/cpuctl/cpuctl.c 301962 2016-06-16 12:07:40Z kib $"); | 29__FBSDID("$FreeBSD: head/sys/dev/cpuctl/cpuctl.c 302372 2016-07-06 14:09:49Z nwhitehorn $"); |
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> --- 77 unchanged lines hidden (view full) --- 115 116/* 117 * Switch to target cpu to run. 118 */ 119static void 120set_cpu(int cpu, struct thread *td) 121{ 122 | 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> --- 77 unchanged lines hidden (view full) --- 115 116/* 117 * Switch to target cpu to run. 118 */ 119static void 120set_cpu(int cpu, struct thread *td) 121{ 122 |
123 KASSERT(cpu >= 0 && cpu < mp_ncpus && cpu_enabled(cpu), | 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__, cpu, td->td_oncpu)); 130} 131 132static void 133restore_cpu(int oldcpu, int is_bound, struct thread *td) 134{ 135 | 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__, cpu, td->td_oncpu)); 130} 131 132static void 133restore_cpu(int oldcpu, int is_bound, struct thread *td) 134{ 135 |
136 KASSERT(oldcpu >= 0 && oldcpu < mp_ncpus && cpu_enabled(oldcpu), | 136 KASSERT(oldcpu >= 0 && oldcpu <= mp_maxid && cpu_enabled(oldcpu), |
137 ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu)); 138 thread_lock(td); 139 if (is_bound == 0) 140 sched_unbind(td); 141 else 142 sched_bind(td, oldcpu); 143 thread_unlock(td); 144} 145 146int 147cpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 148 int flags, struct thread *td) 149{ 150 int ret; 151 int cpu = dev2unit(dev); 152 | 137 ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu)); 138 thread_lock(td); 139 if (is_bound == 0) 140 sched_unbind(td); 141 else 142 sched_bind(td, oldcpu); 143 thread_unlock(td); 144} 145 146int 147cpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 148 int flags, struct thread *td) 149{ 150 int ret; 151 int cpu = dev2unit(dev); 152 |
153 if (cpu >= mp_ncpus || !cpu_enabled(cpu)) { | 153 if (cpu > mp_maxid || !cpu_enabled(cpu)) { |
154 DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu); 155 return (ENXIO); 156 } 157 /* Require write flag for "write" requests. */ 158 if ((cmd == CPUCTL_WRMSR || cmd == CPUCTL_UPDATE) && 159 ((flags & FWRITE) == 0)) 160 return (EPERM); 161 switch (cmd) { --- 34 unchanged lines hidden (view full) --- 196 */ 197static int 198cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data, 199 struct thread *td) 200{ 201 int is_bound = 0; 202 int oldcpu; 203 | 154 DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu); 155 return (ENXIO); 156 } 157 /* Require write flag for "write" requests. */ 158 if ((cmd == CPUCTL_WRMSR || cmd == CPUCTL_UPDATE) && 159 ((flags & FWRITE) == 0)) 160 return (EPERM); 161 switch (cmd) { --- 34 unchanged lines hidden (view full) --- 196 */ 197static int 198cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data, 199 struct thread *td) 200{ 201 int is_bound = 0; 202 int oldcpu; 203 |
204 KASSERT(cpu >= 0 && cpu < mp_ncpus, | 204 KASSERT(cpu >= 0 && cpu <= mp_maxid, |
205 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu)); 206 207 /* Explicitly clear cpuid data to avoid returning stale info. */ 208 bzero(data->data, sizeof(data->data)); 209 DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n", 210 __LINE__, data->level, data->level_type, cpu); 211#ifdef __i386__ 212 if (cpu_id == 0) --- 27 unchanged lines hidden (view full) --- 240static int 241cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td) 242{ 243 uint64_t reg; 244 int is_bound = 0; 245 int oldcpu; 246 int ret; 247 | 205 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu)); 206 207 /* Explicitly clear cpuid data to avoid returning stale info. */ 208 bzero(data->data, sizeof(data->data)); 209 DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n", 210 __LINE__, data->level, data->level_type, cpu); 211#ifdef __i386__ 212 if (cpu_id == 0) --- 27 unchanged lines hidden (view full) --- 240static int 241cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td) 242{ 243 uint64_t reg; 244 int is_bound = 0; 245 int oldcpu; 246 int ret; 247 |
248 KASSERT(cpu >= 0 && cpu < mp_ncpus, | 248 KASSERT(cpu >= 0 && cpu <= mp_maxid, |
249 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu)); 250 251 /* 252 * Explicitly clear cpuid data to avoid returning stale 253 * info 254 */ 255 DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__, 256 data->msr, cpu); --- 34 unchanged lines hidden (view full) --- 291cpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td) 292{ 293 cpuctl_cpuid_args_t args = { 294 .level = 0, 295 }; 296 char vendor[13]; 297 int ret; 298 | 249 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu)); 250 251 /* 252 * Explicitly clear cpuid data to avoid returning stale 253 * info 254 */ 255 DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__, 256 data->msr, cpu); --- 34 unchanged lines hidden (view full) --- 291cpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td) 292{ 293 cpuctl_cpuid_args_t args = { 294 .level = 0, 295 }; 296 char vendor[13]; 297 int ret; 298 |
299 KASSERT(cpu >= 0 && cpu < mp_ncpus, | 299 KASSERT(cpu >= 0 && cpu <= mp_maxid, |
300 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu)); 301 DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu); 302 303 ret = cpuctl_do_cpuid(cpu, &args, td); 304 if (ret != 0) 305 return (ret); 306 ((uint32_t *)vendor)[0] = args.data[1]; 307 ((uint32_t *)vendor)[1] = args.data[3]; --- 199 unchanged lines hidden (view full) --- 507 508int 509cpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td) 510{ 511 int ret = 0; 512 int cpu; 513 514 cpu = dev2unit(dev); | 300 ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu)); 301 DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu); 302 303 ret = cpuctl_do_cpuid(cpu, &args, td); 304 if (ret != 0) 305 return (ret); 306 ((uint32_t *)vendor)[0] = args.data[1]; 307 ((uint32_t *)vendor)[1] = args.data[3]; --- 199 unchanged lines hidden (view full) --- 507 508int 509cpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td) 510{ 511 int ret = 0; 512 int cpu; 513 514 cpu = dev2unit(dev); |
515 if (cpu >= mp_ncpus || !cpu_enabled(cpu)) { | 515 if (cpu > mp_maxid || !cpu_enabled(cpu)) { |
516 DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__, 517 cpu); 518 return (ENXIO); 519 } 520 if (flags & FWRITE) 521 ret = securelevel_gt(td->td_ucred, 0); 522 return (ret); 523} 524 525static int 526cpuctl_modevent(module_t mod __unused, int type, void *data __unused) 527{ 528 int cpu; 529 530 switch(type) { 531 case MOD_LOAD: 532 if (bootverbose) 533 printf("cpuctl: access to MSR registers/cpuid info.\n"); | 516 DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__, 517 cpu); 518 return (ENXIO); 519 } 520 if (flags & FWRITE) 521 ret = securelevel_gt(td->td_ucred, 0); 522 return (ret); 523} 524 525static int 526cpuctl_modevent(module_t mod __unused, int type, void *data __unused) 527{ 528 int cpu; 529 530 switch(type) { 531 case MOD_LOAD: 532 if (bootverbose) 533 printf("cpuctl: access to MSR registers/cpuid info.\n"); |
534 cpuctl_devs = malloc(sizeof(*cpuctl_devs) * mp_ncpus, M_CPUCTL, | 534 cpuctl_devs = malloc(sizeof(*cpuctl_devs) * (mp_maxid + 1), M_CPUCTL, |
535 M_WAITOK | M_ZERO); | 535 M_WAITOK | M_ZERO); |
536 for (cpu = 0; cpu < mp_ncpus; cpu++) | 536 CPU_FOREACH(cpu) |
537 if (cpu_enabled(cpu)) 538 cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu, 539 UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu); 540 break; 541 case MOD_UNLOAD: | 537 if (cpu_enabled(cpu)) 538 cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu, 539 UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu); 540 break; 541 case MOD_UNLOAD: |
542 for (cpu = 0; cpu < mp_ncpus; cpu++) { | 542 CPU_FOREACH(cpu) { |
543 if (cpuctl_devs[cpu] != NULL) 544 destroy_dev(cpuctl_devs[cpu]); 545 } 546 free(cpuctl_devs, M_CPUCTL); 547 break; 548 case MOD_SHUTDOWN: 549 break; 550 default: 551 return (EOPNOTSUPP); 552 } 553 return (0); 554} 555 556DEV_MODULE(cpuctl, cpuctl_modevent, NULL); 557MODULE_VERSION(cpuctl, CPUCTL_VERSION); | 543 if (cpuctl_devs[cpu] != NULL) 544 destroy_dev(cpuctl_devs[cpu]); 545 } 546 free(cpuctl_devs, M_CPUCTL); 547 break; 548 case MOD_SHUTDOWN: 549 break; 550 default: 551 return (EOPNOTSUPP); 552 } 553 return (0); 554} 555 556DEV_MODULE(cpuctl, cpuctl_modevent, NULL); 557MODULE_VERSION(cpuctl, CPUCTL_VERSION); |