cpuctl.c revision 315970
1181430Sstas/*-
2181430Sstas * Copyright (c) 2006-2008 Stanislav Sedov <stas@FreeBSD.org>
3181430Sstas * All rights reserved.
4181430Sstas *
5181430Sstas * Redistribution and use in source and binary forms, with or without
6181430Sstas * modification, are permitted provided that the following conditions
7181430Sstas * are met:
8181430Sstas * 1. Redistributions of source code must retain the above copyright
9181430Sstas *    notice, this list of conditions and the following disclaimer.
10181430Sstas * 2. Redistributions in binary form must reproduce the above copyright
11181430Sstas *    notice, this list of conditions and the following disclaimer in the
12181430Sstas *    documentation and/or other materials provided with the distribution.
13181430Sstas *
14181430Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15181430Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16181430Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17181430Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18181430Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19181430Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20181430Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21181430Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22181430Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23181430Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24181430Sstas * SUCH DAMAGE.
25181430Sstas *
26181430Sstas */
27181430Sstas
28181430Sstas#include <sys/cdefs.h>
29181430Sstas__FBSDID("$FreeBSD: stable/11/sys/dev/cpuctl/cpuctl.c 315970 2017-03-26 00:56:24Z kib $");
30181430Sstas
31181430Sstas#include <sys/param.h>
32181430Sstas#include <sys/systm.h>
33181430Sstas#include <sys/conf.h>
34181430Sstas#include <sys/fcntl.h>
35181430Sstas#include <sys/ioccom.h>
36181430Sstas#include <sys/malloc.h>
37181430Sstas#include <sys/module.h>
38181430Sstas#include <sys/mutex.h>
39181430Sstas#include <sys/priv.h>
40181430Sstas#include <sys/proc.h>
41181430Sstas#include <sys/queue.h>
42181430Sstas#include <sys/sched.h>
43181430Sstas#include <sys/kernel.h>
44181430Sstas#include <sys/sysctl.h>
45181430Sstas#include <sys/uio.h>
46181430Sstas#include <sys/pcpu.h>
47181430Sstas#include <sys/smp.h>
48181430Sstas#include <sys/pmckern.h>
49181430Sstas#include <sys/cpuctl.h>
50181430Sstas
51181430Sstas#include <machine/cpufunc.h>
52181430Sstas#include <machine/md_var.h>
53181430Sstas#include <machine/specialreg.h>
54181430Sstas
55181430Sstasstatic d_open_t cpuctl_open;
56181430Sstasstatic d_ioctl_t cpuctl_ioctl;
57181430Sstas
58181430Sstas#define	CPUCTL_VERSION 1
59181430Sstas
60308481Savg#ifdef CPUCTL_DEBUG
61181430Sstas# define	DPRINTF(format,...) printf(format, __VA_ARGS__);
62181430Sstas#else
63181430Sstas# define	DPRINTF(...)
64181430Sstas#endif
65181430Sstas
66308801Skib#define	UCODE_SIZE_MAX	(4 * 1024 * 1024)
67181430Sstas
68181430Sstasstatic int cpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd,
69181430Sstas    struct thread *td);
70301962Skibstatic int cpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data,
71181430Sstas    struct thread *td);
72301962Skibstatic int cpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
73267651Sattilio    struct thread *td);
74181430Sstasstatic int cpuctl_do_update(int cpu, cpuctl_update_args_t *data,
75181430Sstas    struct thread *td);
76181430Sstasstatic int update_intel(int cpu, cpuctl_update_args_t *args,
77181430Sstas    struct thread *td);
78181430Sstasstatic int update_amd(int cpu, cpuctl_update_args_t *args, struct thread *td);
79228436Sfabientstatic int update_via(int cpu, cpuctl_update_args_t *args,
80228436Sfabient    struct thread *td);
81181430Sstas
82181430Sstasstatic struct cdev **cpuctl_devs;
83181430Sstasstatic MALLOC_DEFINE(M_CPUCTL, "cpuctl", "CPUCTL buffer");
84181430Sstas
85181430Sstasstatic struct cdevsw cpuctl_cdevsw = {
86181430Sstas        .d_version =    D_VERSION,
87181430Sstas        .d_open =       cpuctl_open,
88181430Sstas        .d_ioctl =      cpuctl_ioctl,
89181430Sstas        .d_name =       "cpuctl",
90181430Sstas};
91181430Sstas
92181430Sstas/*
93181430Sstas * This function checks if specified cpu enabled or not.
94181430Sstas */
95181430Sstasstatic int
96181430Sstascpu_enabled(int cpu)
97181430Sstas{
98181430Sstas
99181430Sstas	return (pmc_cpu_is_disabled(cpu) == 0);
100181430Sstas}
101181430Sstas
102181430Sstas/*
103181430Sstas * Check if the current thread is bound to a specific cpu.
104181430Sstas */
105181430Sstasstatic int
106181430Sstascpu_sched_is_bound(struct thread *td)
107181430Sstas{
108181430Sstas	int ret;
109181430Sstas
110181430Sstas	thread_lock(td);
111181430Sstas	ret = sched_is_bound(td);
112181430Sstas	thread_unlock(td);
113181430Sstas	return (ret);
114181430Sstas}
115181430Sstas
116181430Sstas/*
117181430Sstas * Switch to target cpu to run.
118181430Sstas */
119181430Sstasstatic void
120181430Sstasset_cpu(int cpu, struct thread *td)
121181430Sstas{
122181430Sstas
123302372Snwhitehorn	KASSERT(cpu >= 0 && cpu <= mp_maxid && cpu_enabled(cpu),
124181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
125181430Sstas	thread_lock(td);
126181430Sstas	sched_bind(td, cpu);
127181430Sstas	thread_unlock(td);
128181430Sstas	KASSERT(td->td_oncpu == cpu,
129315969Skib	    ("[cpuctl,%d]: cannot bind to target cpu %d on cpu %d", __LINE__,
130315969Skib	    cpu, td->td_oncpu));
131181430Sstas}
132181430Sstas
133181430Sstasstatic void
134181430Sstasrestore_cpu(int oldcpu, int is_bound, struct thread *td)
135181430Sstas{
136181430Sstas
137302372Snwhitehorn	KASSERT(oldcpu >= 0 && oldcpu <= mp_maxid && cpu_enabled(oldcpu),
138181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, oldcpu));
139181430Sstas	thread_lock(td);
140181430Sstas	if (is_bound == 0)
141181430Sstas		sched_unbind(td);
142181430Sstas	else
143181430Sstas		sched_bind(td, oldcpu);
144181430Sstas	thread_unlock(td);
145181430Sstas}
146181430Sstas
147181430Sstasint
148181430Sstascpuctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
149315969Skib    int flags, struct thread *td)
150181430Sstas{
151315969Skib	int cpu, ret;
152181430Sstas
153315969Skib	cpu = dev2unit(dev);
154302372Snwhitehorn	if (cpu > mp_maxid || !cpu_enabled(cpu)) {
155181430Sstas		DPRINTF("[cpuctl,%d]: bad cpu number %d\n", __LINE__, cpu);
156181430Sstas		return (ENXIO);
157181430Sstas	}
158181430Sstas	/* Require write flag for "write" requests. */
159315970Skib	if ((cmd == CPUCTL_MSRCBIT || cmd == CPUCTL_MSRSBIT ||
160315970Skib	    cmd == CPUCTL_UPDATE || cmd == CPUCTL_WRMSR) &&
161315970Skib	    (flags & FWRITE) == 0)
162181430Sstas		return (EPERM);
163181430Sstas	switch (cmd) {
164181430Sstas	case CPUCTL_RDMSR:
165181430Sstas		ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
166181430Sstas		break;
167195189Sstas	case CPUCTL_MSRSBIT:
168195189Sstas	case CPUCTL_MSRCBIT:
169181430Sstas	case CPUCTL_WRMSR:
170181430Sstas		ret = priv_check(td, PRIV_CPUCTL_WRMSR);
171181430Sstas		if (ret != 0)
172181430Sstas			goto fail;
173181430Sstas		ret = cpuctl_do_msr(cpu, (cpuctl_msr_args_t *)data, cmd, td);
174181430Sstas		break;
175181430Sstas	case CPUCTL_CPUID:
176301962Skib		ret = cpuctl_do_cpuid(cpu, (cpuctl_cpuid_args_t *)data, td);
177181430Sstas		break;
178181430Sstas	case CPUCTL_UPDATE:
179181430Sstas		ret = priv_check(td, PRIV_CPUCTL_UPDATE);
180181430Sstas		if (ret != 0)
181181430Sstas			goto fail;
182181430Sstas		ret = cpuctl_do_update(cpu, (cpuctl_update_args_t *)data, td);
183181430Sstas		break;
184267651Sattilio	case CPUCTL_CPUID_COUNT:
185301962Skib		ret = cpuctl_do_cpuid_count(cpu,
186301962Skib		    (cpuctl_cpuid_count_args_t *)data, td);
187267651Sattilio		break;
188181430Sstas	default:
189181430Sstas		ret = EINVAL;
190181430Sstas		break;
191181430Sstas	}
192181430Sstasfail:
193181430Sstas	return (ret);
194181430Sstas}
195181430Sstas
196181430Sstas/*
197181430Sstas * Actually perform cpuid operation.
198181430Sstas */
199301962Skibstatic int
200267673Skibcpuctl_do_cpuid_count(int cpu, cpuctl_cpuid_count_args_t *data,
201267673Skib    struct thread *td)
202181430Sstas{
203181430Sstas	int is_bound = 0;
204181430Sstas	int oldcpu;
205181430Sstas
206302372Snwhitehorn	KASSERT(cpu >= 0 && cpu <= mp_maxid,
207181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
208181430Sstas
209181430Sstas	/* Explicitly clear cpuid data to avoid returning stale info. */
210181430Sstas	bzero(data->data, sizeof(data->data));
211267651Sattilio	DPRINTF("[cpuctl,%d]: retrieving cpuid lev %#0x type %#0x for %d cpu\n",
212267651Sattilio	    __LINE__, data->level, data->level_type, cpu);
213301962Skib#ifdef __i386__
214301962Skib	if (cpu_id == 0)
215301962Skib		return (ENODEV);
216301962Skib#endif
217181430Sstas	oldcpu = td->td_oncpu;
218181430Sstas	is_bound = cpu_sched_is_bound(td);
219181430Sstas	set_cpu(cpu, td);
220267651Sattilio	cpuid_count(data->level, data->level_type, data->data);
221181430Sstas	restore_cpu(oldcpu, is_bound, td);
222301962Skib	return (0);
223181430Sstas}
224181430Sstas
225301962Skibstatic int
226267651Sattiliocpuctl_do_cpuid(int cpu, cpuctl_cpuid_args_t *data, struct thread *td)
227267651Sattilio{
228267673Skib	cpuctl_cpuid_count_args_t cdata;
229301962Skib	int error;
230267651Sattilio
231267673Skib	cdata.level = data->level;
232267651Sattilio	/* Override the level type. */
233267673Skib	cdata.level_type = 0;
234301962Skib	error = cpuctl_do_cpuid_count(cpu, &cdata, td);
235267673Skib	bcopy(cdata.data, data->data, sizeof(data->data)); /* Ignore error */
236301962Skib	return (error);
237267651Sattilio}
238267651Sattilio
239181430Sstas/*
240181430Sstas * Actually perform MSR operations.
241181430Sstas */
242181430Sstasstatic int
243181430Sstascpuctl_do_msr(int cpu, cpuctl_msr_args_t *data, u_long cmd, struct thread *td)
244181430Sstas{
245195189Sstas	uint64_t reg;
246181430Sstas	int is_bound = 0;
247181430Sstas	int oldcpu;
248181430Sstas	int ret;
249181430Sstas
250302372Snwhitehorn	KASSERT(cpu >= 0 && cpu <= mp_maxid,
251181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
252181430Sstas
253181430Sstas	/*
254181430Sstas	 * Explicitly clear cpuid data to avoid returning stale
255181430Sstas	 * info
256181430Sstas	 */
257181430Sstas	DPRINTF("[cpuctl,%d]: operating on MSR %#0x for %d cpu\n", __LINE__,
258181430Sstas	    data->msr, cpu);
259301962Skib#ifdef __i386__
260301962Skib	if ((cpu_feature & CPUID_MSR) == 0)
261301962Skib		return (ENODEV);
262301962Skib#endif
263181430Sstas	oldcpu = td->td_oncpu;
264181430Sstas	is_bound = cpu_sched_is_bound(td);
265181430Sstas	set_cpu(cpu, td);
266195081Sstas	if (cmd == CPUCTL_RDMSR) {
267195081Sstas		data->data = 0;
268195081Sstas		ret = rdmsr_safe(data->msr, &data->data);
269195189Sstas	} else if (cmd == CPUCTL_WRMSR) {
270195081Sstas		ret = wrmsr_safe(data->msr, data->data);
271195189Sstas	} else if (cmd == CPUCTL_MSRSBIT) {
272195189Sstas		critical_enter();
273195189Sstas		ret = rdmsr_safe(data->msr, &reg);
274195189Sstas		if (ret == 0)
275195189Sstas			ret = wrmsr_safe(data->msr, reg | data->data);
276195189Sstas		critical_exit();
277195189Sstas	} else if (cmd == CPUCTL_MSRCBIT) {
278195189Sstas		critical_enter();
279195189Sstas		ret = rdmsr_safe(data->msr, &reg);
280195189Sstas		if (ret == 0)
281195189Sstas			ret = wrmsr_safe(data->msr, reg & ~data->data);
282195189Sstas		critical_exit();
283195189Sstas	} else
284315969Skib		panic("[cpuctl,%d]: unknown operation requested: %lu",
285315969Skib		    __LINE__, cmd);
286181430Sstas	restore_cpu(oldcpu, is_bound, td);
287181430Sstas	return (ret);
288181430Sstas}
289181430Sstas
290181430Sstas/*
291181430Sstas * Actually perform microcode update.
292181430Sstas */
293181430Sstasstatic int
294181430Sstascpuctl_do_update(int cpu, cpuctl_update_args_t *data, struct thread *td)
295181430Sstas{
296181430Sstas	cpuctl_cpuid_args_t args = {
297181430Sstas		.level = 0,
298181430Sstas	};
299181430Sstas	char vendor[13];
300181430Sstas	int ret;
301181430Sstas
302302372Snwhitehorn	KASSERT(cpu >= 0 && cpu <= mp_maxid,
303181430Sstas	    ("[cpuctl,%d]: bad cpu number %d", __LINE__, cpu));
304181430Sstas	DPRINTF("[cpuctl,%d]: XXX %d", __LINE__, cpu);
305181430Sstas
306301962Skib	ret = cpuctl_do_cpuid(cpu, &args, td);
307301962Skib	if (ret != 0)
308301962Skib		return (ret);
309181430Sstas	((uint32_t *)vendor)[0] = args.data[1];
310181430Sstas	((uint32_t *)vendor)[1] = args.data[3];
311181430Sstas	((uint32_t *)vendor)[2] = args.data[2];
312181430Sstas	vendor[12] = '\0';
313181430Sstas	if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) == 0)
314181430Sstas		ret = update_intel(cpu, data, td);
315228436Sfabient	else if(strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) == 0)
316181430Sstas		ret = update_amd(cpu, data, td);
317315969Skib	else if(strncmp(vendor, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID))
318315969Skib	    == 0)
319228436Sfabient		ret = update_via(cpu, data, td);
320181430Sstas	else
321181430Sstas		ret = ENXIO;
322181430Sstas	return (ret);
323181430Sstas}
324181430Sstas
325181430Sstasstatic int
326181430Sstasupdate_intel(int cpu, cpuctl_update_args_t *args, struct thread *td)
327181430Sstas{
328255439Skib	void *ptr;
329181430Sstas	uint64_t rev0, rev1;
330181430Sstas	uint32_t tmp[4];
331255439Skib	int is_bound;
332181430Sstas	int oldcpu;
333181430Sstas	int ret;
334181430Sstas
335181430Sstas	if (args->size == 0 || args->data == NULL) {
336181430Sstas		DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
337181430Sstas		return (EINVAL);
338181430Sstas	}
339181430Sstas	if (args->size > UCODE_SIZE_MAX) {
340181430Sstas		DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
341181430Sstas		return (EINVAL);
342181430Sstas	}
343181430Sstas
344181430Sstas	/*
345255439Skib	 * 16 byte alignment required.  Rely on the fact that
346255439Skib	 * malloc(9) always returns the pointer aligned at least on
347255439Skib	 * the size of the allocation.
348181430Sstas	 */
349181430Sstas	ptr = malloc(args->size + 16, M_CPUCTL, M_WAITOK);
350181430Sstas	if (copyin(args->data, ptr, args->size) != 0) {
351181430Sstas		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
352181430Sstas		    __LINE__, args->data, ptr, args->size);
353181430Sstas		ret = EFAULT;
354181430Sstas		goto fail;
355181430Sstas	}
356181430Sstas	oldcpu = td->td_oncpu;
357181430Sstas	is_bound = cpu_sched_is_bound(td);
358181430Sstas	set_cpu(cpu, td);
359181430Sstas	critical_enter();
360252592Srpaulo	rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
361181430Sstas
362181430Sstas	/*
363181430Sstas	 * Perform update.
364181430Sstas	 */
365181430Sstas	wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
366181430Sstas	wrmsr_safe(MSR_BIOS_SIGN, 0);
367181430Sstas
368181430Sstas	/*
369181430Sstas	 * Serialize instruction flow.
370181430Sstas	 */
371181430Sstas	do_cpuid(0, tmp);
372181430Sstas	critical_exit();
373252592Srpaulo	rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
374181430Sstas	restore_cpu(oldcpu, is_bound, td);
375181430Sstas	if (rev1 > rev0)
376181430Sstas		ret = 0;
377181430Sstas	else
378181430Sstas		ret = EEXIST;
379181430Sstasfail:
380254191Skib	free(ptr, M_CPUCTL);
381181430Sstas	return (ret);
382181430Sstas}
383181430Sstas
384308760Savg/*
385308760Savg * NB: MSR 0xc0010020, MSR_K8_UCODE_UPDATE, is not documented by AMD.
386308760Savg * Coreboot, illumos and Linux source code was used to understand
387308760Savg * its workings.
388308760Savg */
389308760Savgstatic void
390308760Savgamd_ucode_wrmsr(void *ucode_ptr)
391308760Savg{
392308760Savg	uint32_t tmp[4];
393308760Savg
394308760Savg	wrmsr_safe(MSR_K8_UCODE_UPDATE, (uintptr_t)ucode_ptr);
395308760Savg	do_cpuid(0, tmp);
396308760Savg}
397308760Savg
398181430Sstasstatic int
399181430Sstasupdate_amd(int cpu, cpuctl_update_args_t *args, struct thread *td)
400181430Sstas{
401308760Savg	void *ptr;
402181430Sstas	int ret;
403181430Sstas
404181430Sstas	if (args->size == 0 || args->data == NULL) {
405181430Sstas		DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
406181430Sstas		return (EINVAL);
407181430Sstas	}
408181430Sstas	if (args->size > UCODE_SIZE_MAX) {
409181430Sstas		DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
410181430Sstas		return (EINVAL);
411181430Sstas	}
412308760Savg
413181430Sstas	/*
414308760Savg	 * 16 byte alignment required.  Rely on the fact that
415308760Savg	 * malloc(9) always returns the pointer aligned at least on
416308760Savg	 * the size of the allocation.
417181430Sstas	 */
418308760Savg	ptr = malloc(args->size + 16, M_CPUCTL, M_ZERO | M_WAITOK);
419181430Sstas	if (copyin(args->data, ptr, args->size) != 0) {
420181430Sstas		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
421181430Sstas		    __LINE__, args->data, ptr, args->size);
422181430Sstas		ret = EFAULT;
423181430Sstas		goto fail;
424181430Sstas	}
425308760Savg	smp_rendezvous(NULL, amd_ucode_wrmsr, NULL, ptr);
426181430Sstas	ret = 0;
427181430Sstasfail:
428308760Savg	free(ptr, M_CPUCTL);
429181430Sstas	return (ret);
430181430Sstas}
431181430Sstas
432228436Sfabientstatic int
433228436Sfabientupdate_via(int cpu, cpuctl_update_args_t *args, struct thread *td)
434228436Sfabient{
435255439Skib	void *ptr;
436228436Sfabient	uint64_t rev0, rev1, res;
437228436Sfabient	uint32_t tmp[4];
438255439Skib	int is_bound;
439228436Sfabient	int oldcpu;
440228436Sfabient	int ret;
441228436Sfabient
442228436Sfabient	if (args->size == 0 || args->data == NULL) {
443228436Sfabient		DPRINTF("[cpuctl,%d]: zero-sized firmware image", __LINE__);
444228436Sfabient		return (EINVAL);
445228436Sfabient	}
446228436Sfabient	if (args->size > UCODE_SIZE_MAX) {
447228436Sfabient		DPRINTF("[cpuctl,%d]: firmware image too large", __LINE__);
448228436Sfabient		return (EINVAL);
449228436Sfabient	}
450228436Sfabient
451228436Sfabient	/*
452228436Sfabient	 * 4 byte alignment required.
453228436Sfabient	 */
454255439Skib	ptr = malloc(args->size, M_CPUCTL, M_WAITOK);
455228436Sfabient	if (copyin(args->data, ptr, args->size) != 0) {
456228436Sfabient		DPRINTF("[cpuctl,%d]: copyin %p->%p of %zd bytes failed",
457228436Sfabient		    __LINE__, args->data, ptr, args->size);
458228436Sfabient		ret = EFAULT;
459228436Sfabient		goto fail;
460228436Sfabient	}
461228436Sfabient	oldcpu = td->td_oncpu;
462228436Sfabient	is_bound = cpu_sched_is_bound(td);
463228436Sfabient	set_cpu(cpu, td);
464228436Sfabient	critical_enter();
465252592Srpaulo	rdmsr_safe(MSR_BIOS_SIGN, &rev0); /* Get current microcode revision. */
466228436Sfabient
467228436Sfabient	/*
468228436Sfabient	 * Perform update.
469228436Sfabient	 */
470228436Sfabient	wrmsr_safe(MSR_BIOS_UPDT_TRIG, (uintptr_t)(ptr));
471228436Sfabient	do_cpuid(1, tmp);
472228436Sfabient
473228436Sfabient	/*
474228436Sfabient	 * Result are in low byte of MSR FCR5:
475228436Sfabient	 * 0x00: No update has been attempted since RESET.
476228436Sfabient	 * 0x01: The last attempted update was successful.
477228436Sfabient	 * 0x02: The last attempted update was unsuccessful due to a bad
478228436Sfabient	 *       environment. No update was loaded and any preexisting
479228436Sfabient	 *       patches are still active.
480228436Sfabient	 * 0x03: The last attempted update was not applicable to this processor.
481228436Sfabient	 *       No update was loaded and any preexisting patches are still
482228436Sfabient	 *       active.
483228436Sfabient	 * 0x04: The last attempted update was not successful due to an invalid
484228436Sfabient	 *       update data block. No update was loaded and any preexisting
485228436Sfabient	 *       patches are still active
486228436Sfabient	 */
487228436Sfabient	rdmsr_safe(0x1205, &res);
488228436Sfabient	res &= 0xff;
489228436Sfabient	critical_exit();
490228436Sfabient	rdmsr_safe(MSR_BIOS_SIGN, &rev1); /* Get new microcode revision. */
491228436Sfabient	restore_cpu(oldcpu, is_bound, td);
492228436Sfabient
493228436Sfabient	DPRINTF("[cpu,%d]: rev0=%x rev1=%x res=%x\n", __LINE__,
494228436Sfabient	    (unsigned)(rev0 >> 32), (unsigned)(rev1 >> 32), (unsigned)res);
495228436Sfabient
496228436Sfabient	if (res != 0x01)
497228436Sfabient		ret = EINVAL;
498228436Sfabient	else
499228436Sfabient		ret = 0;
500228436Sfabientfail:
501254191Skib	free(ptr, M_CPUCTL);
502228436Sfabient	return (ret);
503228436Sfabient}
504228436Sfabient
505181430Sstasint
506181430Sstascpuctl_open(struct cdev *dev, int flags, int fmt __unused, struct thread *td)
507181430Sstas{
508181430Sstas	int ret = 0;
509181430Sstas	int cpu;
510181430Sstas
511183397Sed	cpu = dev2unit(dev);
512302372Snwhitehorn	if (cpu > mp_maxid || !cpu_enabled(cpu)) {
513181430Sstas		DPRINTF("[cpuctl,%d]: incorrect cpu number %d\n", __LINE__,
514181430Sstas		    cpu);
515181430Sstas		return (ENXIO);
516181430Sstas	}
517181430Sstas	if (flags & FWRITE)
518181430Sstas		ret = securelevel_gt(td->td_ucred, 0);
519181430Sstas	return (ret);
520181430Sstas}
521181430Sstas
522181430Sstasstatic int
523181430Sstascpuctl_modevent(module_t mod __unused, int type, void *data __unused)
524181430Sstas{
525181430Sstas	int cpu;
526181430Sstas
527181430Sstas	switch(type) {
528181430Sstas	case MOD_LOAD:
529181430Sstas		if (bootverbose)
530181430Sstas			printf("cpuctl: access to MSR registers/cpuid info.\n");
531302372Snwhitehorn		cpuctl_devs = malloc(sizeof(*cpuctl_devs) * (mp_maxid + 1), M_CPUCTL,
532263080Skib		    M_WAITOK | M_ZERO);
533302372Snwhitehorn		CPU_FOREACH(cpu)
534181430Sstas			if (cpu_enabled(cpu))
535181430Sstas				cpuctl_devs[cpu] = make_dev(&cpuctl_cdevsw, cpu,
536181430Sstas				    UID_ROOT, GID_KMEM, 0640, "cpuctl%d", cpu);
537181430Sstas		break;
538181430Sstas	case MOD_UNLOAD:
539302372Snwhitehorn		CPU_FOREACH(cpu) {
540181430Sstas			if (cpuctl_devs[cpu] != NULL)
541181430Sstas				destroy_dev(cpuctl_devs[cpu]);
542181430Sstas		}
543181430Sstas		free(cpuctl_devs, M_CPUCTL);
544181430Sstas		break;
545181430Sstas	case MOD_SHUTDOWN:
546181430Sstas		break;
547181430Sstas	default:
548181430Sstas		return (EOPNOTSUPP);
549181430Sstas        }
550181430Sstas	return (0);
551181430Sstas}
552181430Sstas
553181430SstasDEV_MODULE(cpuctl, cpuctl_modevent, NULL);
554181430SstasMODULE_VERSION(cpuctl, CPUCTL_VERSION);
555