xref: /freebsd/sys/dev/cpuctl/cpuctl.c (revision 82431678fce5c893ef9c7418ad6d998ad4187de6)
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 DEBUG
61 # define	DPRINTF(format,...) printf(format, __VA_ARGS__);
62 #else
63 # define	DPRINTF(...)
64 #endif
65 
66 #define	UCODE_SIZE_MAX	(10 * 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_update(int cpu, cpuctl_update_args_t *data,
73     struct thread *td);
74 static int update_intel(int cpu, cpuctl_update_args_t *args,
75     struct thread *td);
76 static int update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td);
77 
78 static struct cdev **cpuctl_devs;
79 static MALLOC_DEFINE(M_CPUCTL, "cpuctl", "CPUCTL buffer");
80 
81 static struct cdevsw cpuctl_cdevsw = {
82         .d_version =    D_VERSION,
83         .d_open =       cpuctl_open,
84         .d_ioctl =      cpuctl_ioctl,
85         .d_name =       "cpuctl",
86 };
87 
88 /*
89  * This function checks if specified cpu enabled or not.
90  */
91 static int
92 cpu_enabled(int cpu)
93 {
94 
95 	return (pmc_cpu_is_disabled(cpu) == 0);
96 }
97 
98 /*
99  * Check if the current thread is bound to a specific cpu.
100  */
101 static int
102 cpu_sched_is_bound(struct thread *td)
103 {
104 	int ret;
105 
106 	thread_lock(td);
107 	ret = sched_is_bound(td);
108 	thread_unlock(td);
109 	return (ret);
110 }
111 
112 /*
113  * Switch to target cpu to run.
114  */
115 static void
116 set_cpu(int cpu, struct thread *td)
117 {
118 
119 	KASSERT(cpu >= 0 && cpu < mp_ncpus && cpu_enabled(cpu),
120 	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
121 	thread_lock(td);
122 	sched_bind(td, cpu);
123 	thread_unlock(td);
124 	KASSERT(td->td_oncpu == cpu,
125 	    ("[cpuctl,%d]: cannot bind to target cpu %d", __LINE__, cpu));
126 }
127 
128 static void
129 restore_cpu(int oldcpu, int is_bound, struct thread *td)
130 {
131 
132 	KASSERT(oldcpu >= 0 && oldcpu < mp_ncpus && cpu_enabled(oldcpu),
133 	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu));
134 	thread_lock(td);
135 	if (is_bound == 0)
136 		sched_unbind(td);
137 	else
138 		sched_bind(td, oldcpu);
139 	thread_unlock(td);
140 }
141 
142 int
143 cpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
144 	int flags, struct thread *td)
145 {
146 	int ret;
147 	int cpu = dev2unit(dev);
148 
149 	if (cpu >= mp_ncpus || !cpu_enabled(cpu)) {
150 		DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu);
151 		return (ENXIO);
152 	}
153 	/* Require write flag for "write" requests. */
154 	if ((cmd == CPUCTL_WRMSR || cmd == CPUCTL_UPDATE) &&
155 	    ((flags & FWRITE) == 0))
156 		return (EPERM);
157 	switch (cmd) {
158 	case CPUCTL_RDMSR:
159 		ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
160 		break;
161 	case CPUCTL_WRMSR:
162 		ret = priv_check(td, PRIV_CPUCTL_WRMSR);
163 		if (ret != 0)
164 			goto fail;
165 		ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
166 		break;
167 	case CPUCTL_CPUID:
168 		ret = cpuctl_do_cpuid(cpu, (cpuctl_cpuid_args_t *)data, td);
169 		break;
170 	case CPUCTL_UPDATE:
171 		ret = priv_check(td, PRIV_CPUCTL_UPDATE);
172 		if (ret != 0)
173 			goto fail;
174 		ret = cpuctl_do_update(cpu, (cpuctl_update_args_t *)data, td);
175 		break;
176 	default:
177 		ret = EINVAL;
178 		break;
179 	}
180 fail:
181 	return (ret);
182 }
183 
184 /*
185  * Actually perform cpuid operation.
186  */
187 static int
188 cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data, struct thread *td)
189 {
190 	int is_bound = 0;
191 	int oldcpu;
192 
193 	KASSERT(cpu >= 0 && cpu < mp_ncpus,
194 	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
195 
196 	/* Explicitly clear cpuid data to avoid returning stale info. */
197 	bzero(data->data, sizeof(data->data));
198 	DPRINTF("[cpuctl,%d]: retriving cpuid level %#0x for %d cpu\n",
199 	    __LINE__, data->level, cpu);
200 	oldcpu = td->td_oncpu;
201 	is_bound = cpu_sched_is_bound(td);
202 	set_cpu(cpu, td);
203 	do_cpuid(data->level, data->data);
204 	restore_cpu(oldcpu, is_bound, td);
205 	return (0);
206 }
207 
208 /*
209  * Actually perform MSR operations.
210  */
211 static int
212 cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
213 {
214 	int is_bound = 0;
215 	int oldcpu;
216 	int ret;
217 
218 	KASSERT(cpu >= 0 && cpu < mp_ncpus,
219 	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
220 
221 	/*
222 	 * Explicitly clear cpuid data to avoid returning stale
223 	 * info
224 	 */
225 	data->data = 0;
226 	DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
227 	    data->msr, cpu);
228 	oldcpu = td->td_oncpu;
229 	is_bound = cpu_sched_is_bound(td);
230 	set_cpu(cpu, td);
231 	ret = cmd == CPUCTL_RDMSR ? rdmsr_safe(data->msr, &data->data) :
232 	    wrmsr_safe(data->msr, data->data);
233 	restore_cpu(oldcpu, is_bound, td);
234 	return (ret);
235 }
236 
237 /*
238  * Actually perform microcode update.
239  */
240 static int
241 cpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td)
242 {
243 	cpuctl_cpuid_args_t args = {
244 		.level = 0,
245 	};
246 	char vendor[13];
247 	int ret;
248 
249 	KASSERT(cpu >= 0 && cpu < mp_ncpus,
250 	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
251 	DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu);
252 
253 	ret = cpuctl_do_cpuid(cpu, &args, td);
254 	if (ret != 0) {
255 		DPRINTF("[cpuctl,%d]: cannot retrive cpuid info for cpu %d",
256 		    __LINE__, cpu);
257 		return (ENXIO);
258 	}
259 	((uint32_t *)vendor)[0] = args.data[1];
260 	((uint32_t *)vendor)[1] = args.data[3];
261 	((uint32_t *)vendor)[2] = args.data[2];
262 	vendor[12] = '\0';
263 	if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) == 0)
264 		ret = update_intel(cpu, data, td);
265 	else if(strncmp(vendor, INTEL_VENDOR_ID, sizeof(AMD_VENDOR_ID)) == 0)
266 		ret = update_amd(cpu, data, td);
267 	else
268 		ret = ENXIO;
269 	return (ret);
270 }
271 
272 static int
273 update_intel(int cpu, cpuctl_update_args_t *args, struct thread *td)
274 {
275 	void *ptr = NULL;
276 	uint64_t rev0, rev1;
277 	uint32_t tmp[4];
278 	int is_bound = 0;
279 	int oldcpu;
280 	int ret;
281 
282 	if (args->size == 0 || args->data == NULL) {
283 		DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
284 		return (EINVAL);
285 	}
286 	if (args->size > UCODE_SIZE_MAX) {
287 		DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
288 		return (EINVAL);
289 	}
290 
291 	/*
292 	 * 16 byte alignment required.
293 	 */
294 	ptr = malloc(args->size + 16, M_CPUCTL, M_WAITOK);
295 	ptr = (void *)(16 + ((intptr_t)ptr & ~0xf));
296 	if (copyin(args->data, ptr, args->size) != 0) {
297 		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
298 		    __LINE__, args->data, ptr, args->size);
299 		ret = EFAULT;
300 		goto fail;
301 	}
302 	oldcpu = td->td_oncpu;
303 	is_bound = cpu_sched_is_bound(td);
304 	set_cpu(cpu, td);
305 	critical_enter();
306 	rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current micorcode revision. */
307 
308 	/*
309 	 * Perform update.
310 	 */
311 	wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
312 	wrmsr_safe(MSR_BIOS_SIGN, 0);
313 
314 	/*
315 	 * Serialize instruction flow.
316 	 */
317 	do_cpuid(0, tmp);
318 	critical_exit();
319 	rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new micorcode revision. */
320 	restore_cpu(oldcpu, is_bound, td);
321 	if (rev1 > rev0)
322 		ret = 0;
323 	else
324 		ret = EEXIST;
325 fail:
326 	if (ptr != NULL)
327 		contigfree(ptr, args->size, M_CPUCTL);
328 	return (ret);
329 }
330 
331 static int
332 update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td)
333 {
334 	void *ptr = NULL;
335 	uint32_t tmp[4];
336 	int is_bound = 0;
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 	 * XXX Might not require contignous address space - needs check
350 	 */
351 	ptr = contigmalloc(args->size, M_CPUCTL, 0, 0, 0xffffffff, 16, 0);
352 	if (ptr == NULL) {
353 		DPRINTF("[cpuctl,%d]: cannot allocate %zd bytes of memory",
354 		    __LINE__, args->size);
355 		return (ENOMEM);
356 	}
357 	if (copyin(args->data, ptr, args->size) != 0) {
358 		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
359 		    __LINE__, args->data, ptr, args->size);
360 		ret = EFAULT;
361 		goto fail;
362 	}
363 	oldcpu = td->td_oncpu;
364 	is_bound = cpu_sched_is_bound(td);
365 	set_cpu(cpu, td);
366 	critical_enter();
367 
368 	/*
369 	 * Perform update.
370 	 */
371 	wrmsr_safe(MSR_K8_UCODE_UPDATE, (uintptr_t)args->data);
372 
373 	/*
374 	 * Serialize instruction flow.
375 	 */
376 	do_cpuid(0, tmp);
377 	critical_exit();
378 	restore_cpu(oldcpu, is_bound, td);
379 	ret = 0;
380 fail:
381 	if (ptr != NULL)
382 		contigfree(ptr, args->size, M_CPUCTL);
383 	return (ret);
384 }
385 
386 int
387 cpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
388 {
389 	int ret = 0;
390 	int cpu;
391 
392 	cpu = dev2unit(dev);
393 	if (cpu >= mp_ncpus || !cpu_enabled(cpu)) {
394 		DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__,
395 		    cpu);
396 		return (ENXIO);
397 	}
398 	if (flags & FWRITE)
399 		ret = securelevel_gt(td->td_ucred, 0);
400 	return (ret);
401 }
402 
403 static int
404 cpuctl_modevent(module_t mod __unused, int type, void *data __unused)
405 {
406 	int cpu;
407 
408 	switch(type) {
409 	case MOD_LOAD:
410 		if ((cpu_feature & CPUID_MSR) == 0) {
411 			if (bootverbose)
412 				printf("cpuctl: not available.\n");
413 			return (ENODEV);
414 		}
415 		if (bootverbose)
416 			printf("cpuctl: access to MSR registers/cpuid info.\n");
417 		cpuctl_devs = (struct cdev **)malloc(sizeof(void *) * mp_ncpus,
418 		    M_CPUCTL, M_WAITOK | M_ZERO);
419 		if (cpuctl_devs == NULL) {
420 			DPRINTF("[cpuctl,%d]: cannot allocate memory\n",
421 			    __LINE__);
422 			return (ENOMEM);
423 		}
424 		for (cpu = 0; cpu < mp_ncpus; cpu++)
425 			if (cpu_enabled(cpu))
426 				cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu,
427 				    UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu);
428 		break;
429 	case MOD_UNLOAD:
430 		for (cpu = 0; cpu < mp_ncpus; cpu++) {
431 			if (cpuctl_devs[cpu] != NULL)
432 				destroy_dev(cpuctl_devs[cpu]);
433 		}
434 		free(cpuctl_devs, M_CPUCTL);
435 		break;
436 	case MOD_SHUTDOWN:
437 		break;
438 	default:
439 		return (EOPNOTSUPP);
440         }
441 	return (0);
442 }
443 
444 DEV_MODULE(cpuctl, cpuctl_modevent, NULL);
445 MODULE_VERSION(cpuctl, CPUCTL_VERSION);
446