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