xref: /illumos-gate/usr/src/uts/common/io/cpuid_drv.c (revision 8949bcd619d78e849deef983cb8310bc3aa3e242)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*8949bcd6Sandrei  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/file.h>
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/open.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/processor.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
447c478bd9Sstevel@tonic-gate #include <sys/cpuid_drv.h>
457c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #if defined(__x86)
487c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static dev_info_t *cpuid_devi;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
547c478bd9Sstevel@tonic-gate static int
557c478bd9Sstevel@tonic-gate cpuid_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	switch (cmd) {
587c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
597c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
607c478bd9Sstevel@tonic-gate 		break;
617c478bd9Sstevel@tonic-gate 	default:
627c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	switch (getminor((dev_t)arg)) {
667c478bd9Sstevel@tonic-gate 	case CPUID_SELF_CPUID_MINOR:
677c478bd9Sstevel@tonic-gate 		break;
687c478bd9Sstevel@tonic-gate 	default:
697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (cmd == DDI_INFO_DEVT2INSTANCE)
737c478bd9Sstevel@tonic-gate 		*result = 0;
747c478bd9Sstevel@tonic-gate 	else
757c478bd9Sstevel@tonic-gate 		*result = cpuid_devi;
767c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static int
807c478bd9Sstevel@tonic-gate cpuid_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
837c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
847c478bd9Sstevel@tonic-gate 	cpuid_devi = devi;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	return (ddi_create_minor_node(devi, CPUID_DRIVER_SELF_NODE, S_IFCHR,
877c478bd9Sstevel@tonic-gate 	    CPUID_SELF_CPUID_MINOR, DDI_PSEUDO, 0));
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int
917c478bd9Sstevel@tonic-gate cpuid_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
947c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
957c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
967c478bd9Sstevel@tonic-gate 	cpuid_devi = NULL;
977c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1017c478bd9Sstevel@tonic-gate static int
1027c478bd9Sstevel@tonic-gate cpuid_open(dev_t *dev, int flag, int otyp, cred_t *cr)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	return (getminor(*dev) == CPUID_SELF_CPUID_MINOR ? 0 : ENXIO);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #if defined(_HAVE_CPUID_INSN)
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1107c478bd9Sstevel@tonic-gate static int
1117c478bd9Sstevel@tonic-gate cpuid_read(dev_t dev, uio_t *uio, cred_t *cr)
1127c478bd9Sstevel@tonic-gate {
113*8949bcd6Sandrei 	struct cpuid_regs crs;
1147c478bd9Sstevel@tonic-gate 	int error = 0;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0)
1177c478bd9Sstevel@tonic-gate 		return (ENXIO);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (uio->uio_resid & (sizeof (crs) - 1))
1207c478bd9Sstevel@tonic-gate 		return (EINVAL);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	while (uio->uio_resid > 0) {
1237c478bd9Sstevel@tonic-gate 		u_offset_t uoff;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		if ((uoff = (u_offset_t)uio->uio_loffset) > UINT_MAX) {
1267c478bd9Sstevel@tonic-gate 			error = EINVAL;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 
130*8949bcd6Sandrei 		crs.cp_eax = (uint32_t)uoff;
131*8949bcd6Sandrei 		crs.cp_ebx = crs.cp_ecx = crs.cp_edx = 0;
132*8949bcd6Sandrei 		(void) cpuid_insn(NULL, &crs);
1337c478bd9Sstevel@tonic-gate 
134*8949bcd6Sandrei 		if ((error = uiomove(&crs, sizeof (crs), UIO_READ, uio)) != 0)
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		uio->uio_loffset = uoff + 1;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (error);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #else
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #define	cpuid_read	nodev
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #endif	/* _HAVE_CPUID_INSN */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1497c478bd9Sstevel@tonic-gate static int
1507c478bd9Sstevel@tonic-gate cpuid_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rval)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	char areq[16];
1537c478bd9Sstevel@tonic-gate 	void *ustr;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	switch (cmd) {
1567c478bd9Sstevel@tonic-gate 	case CPUID_GET_HWCAP: {
1577c478bd9Sstevel@tonic-gate 		STRUCT_DECL(cpuid_get_hwcap, h);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		STRUCT_INIT(h, mode);
1607c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)arg,
1617c478bd9Sstevel@tonic-gate 		    STRUCT_BUF(h), STRUCT_SIZE(h), mode))
1627c478bd9Sstevel@tonic-gate 			return (EFAULT);
1637c478bd9Sstevel@tonic-gate 		if ((ustr = STRUCT_FGETP(h, cgh_archname)) != NULL &&
1647c478bd9Sstevel@tonic-gate 		    copyinstr(ustr, areq, sizeof (areq), NULL) != 0)
1657c478bd9Sstevel@tonic-gate 			return (EFAULT);
1667c478bd9Sstevel@tonic-gate 		areq[sizeof (areq) - 1] = '\0';
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		if (strcmp(areq, architecture) == 0)
1697c478bd9Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap);
1707c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
1717c478bd9Sstevel@tonic-gate 		else if (strcmp(areq, architecture_32) == 0)
1727c478bd9Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap32);
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate 		else
1757c478bd9Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, 0);
1767c478bd9Sstevel@tonic-gate 		if (ddi_copyout(STRUCT_BUF(h),
1777c478bd9Sstevel@tonic-gate 		    (void *)arg, STRUCT_SIZE(h), mode))
1787c478bd9Sstevel@tonic-gate 			return (EFAULT);
1797c478bd9Sstevel@tonic-gate 		return (0);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	default:
1837c478bd9Sstevel@tonic-gate 		return (ENOTTY);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static struct cb_ops cpuid_cb_ops = {
1887c478bd9Sstevel@tonic-gate 	cpuid_open,
1897c478bd9Sstevel@tonic-gate 	nulldev,	/* close */
1907c478bd9Sstevel@tonic-gate 	nodev,		/* strategy */
1917c478bd9Sstevel@tonic-gate 	nodev,		/* print */
1927c478bd9Sstevel@tonic-gate 	nodev,		/* dump */
1937c478bd9Sstevel@tonic-gate 	cpuid_read,
1947c478bd9Sstevel@tonic-gate 	nodev,		/* write */
1957c478bd9Sstevel@tonic-gate 	cpuid_ioctl,
1967c478bd9Sstevel@tonic-gate 	nodev,		/* devmap */
1977c478bd9Sstevel@tonic-gate 	nodev,		/* mmap */
1987c478bd9Sstevel@tonic-gate 	nodev,		/* segmap */
1997c478bd9Sstevel@tonic-gate 	nochpoll,	/* poll */
2007c478bd9Sstevel@tonic-gate 	ddi_prop_op,
2017c478bd9Sstevel@tonic-gate 	NULL,
2027c478bd9Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP
2037c478bd9Sstevel@tonic-gate };
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static struct dev_ops cpuid_dv_ops = {
2067c478bd9Sstevel@tonic-gate 	DEVO_REV,
2077c478bd9Sstevel@tonic-gate 	0,
2087c478bd9Sstevel@tonic-gate 	cpuid_getinfo,
2097c478bd9Sstevel@tonic-gate 	nulldev,	/* identify */
2107c478bd9Sstevel@tonic-gate 	nulldev,	/* probe */
2117c478bd9Sstevel@tonic-gate 	cpuid_attach,
2127c478bd9Sstevel@tonic-gate 	cpuid_detach,
2137c478bd9Sstevel@tonic-gate 	nodev,		/* reset */
2147c478bd9Sstevel@tonic-gate 	&cpuid_cb_ops,
2157c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0
2167c478bd9Sstevel@tonic-gate };
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2197c478bd9Sstevel@tonic-gate 	&mod_driverops,
2207c478bd9Sstevel@tonic-gate 	"cpuid driver v%I%",
2217c478bd9Sstevel@tonic-gate 	&cpuid_dv_ops
2227c478bd9Sstevel@tonic-gate };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static struct modlinkage modl = {
2257c478bd9Sstevel@tonic-gate 	MODREV_1,
2267c478bd9Sstevel@tonic-gate 	&modldrv
2277c478bd9Sstevel@tonic-gate };
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate int
2307c478bd9Sstevel@tonic-gate _init(void)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	return (mod_install(&modl));
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate int
2367c478bd9Sstevel@tonic-gate _fini(void)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	return (mod_remove(&modl));
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate int
2427c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfo)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	return (mod_info(&modl, modinfo));
2457c478bd9Sstevel@tonic-gate }
246