17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
51939740Sherry Moore * Common Development and Distribution License (the "License").
61939740Sherry Moore * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
227417cfdKuriakose Kuruvilla * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bdstevel@tonic-gate */
24ebb8ac0Robert Mustacchi/*
251259653John Levon * Copyright 2019 Joyent, Inc.
26ebb8ac0Robert Mustacchi */
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include <sys/types.h>
307c478bdstevel@tonic-gate#include <sys/file.h>
317c478bdstevel@tonic-gate#include <sys/errno.h>
327c478bdstevel@tonic-gate#include <sys/open.h>
337c478bdstevel@tonic-gate#include <sys/cred.h>
347c478bdstevel@tonic-gate#include <sys/conf.h>
357c478bdstevel@tonic-gate#include <sys/stat.h>
367c478bdstevel@tonic-gate#include <sys/processor.h>
377c478bdstevel@tonic-gate#include <sys/cpuvar.h>
387c478bdstevel@tonic-gate#include <sys/kmem.h>
397c478bdstevel@tonic-gate#include <sys/modctl.h>
407c478bdstevel@tonic-gate#include <sys/ddi.h>
417c478bdstevel@tonic-gate#include <sys/sunddi.h>
421259653John Levon#include <sys/policy.h>
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate#include <sys/auxv.h>
457c478bdstevel@tonic-gate#include <sys/cpuid_drv.h>
467c478bdstevel@tonic-gate#include <sys/systeminfo.h>
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate#if defined(__x86)
497c478bdstevel@tonic-gate#include <sys/x86_archext.h>
507c478bdstevel@tonic-gate#endif
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gatestatic dev_info_t *cpuid_devi;
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate/*ARGSUSED*/
557c478bdstevel@tonic-gatestatic int
567c478bdstevel@tonic-gatecpuid_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
577c478bdstevel@tonic-gate{
587c478bdstevel@tonic-gate	switch (cmd) {
597c478bdstevel@tonic-gate	case DDI_INFO_DEVT2DEVINFO:
607c478bdstevel@tonic-gate	case DDI_INFO_DEVT2INSTANCE:
617c478bdstevel@tonic-gate		break;
627c478bdstevel@tonic-gate	default:
637c478bdstevel@tonic-gate		return (DDI_FAILURE);
647c478bdstevel@tonic-gate	}
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate	switch (getminor((dev_t)arg)) {
677c478bdstevel@tonic-gate	case CPUID_SELF_CPUID_MINOR:
687c478bdstevel@tonic-gate		break;
697c478bdstevel@tonic-gate	default:
707c478bdstevel@tonic-gate		return (DDI_FAILURE);
717c478bdstevel@tonic-gate	}
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate	if (cmd == DDI_INFO_DEVT2INSTANCE)
747c478bdstevel@tonic-gate		*result = 0;
757c478bdstevel@tonic-gate	else
767c478bdstevel@tonic-gate		*result = cpuid_devi;
777c478bdstevel@tonic-gate	return (DDI_SUCCESS);
787c478bdstevel@tonic-gate}
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gatestatic int
817c478bdstevel@tonic-gatecpuid_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
827c478bdstevel@tonic-gate{
837c478bdstevel@tonic-gate	if (cmd != DDI_ATTACH)
847c478bdstevel@tonic-gate		return (DDI_FAILURE);
857c478bdstevel@tonic-gate	cpuid_devi = devi;
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate	return (ddi_create_minor_node(devi, CPUID_DRIVER_SELF_NODE, S_IFCHR,
887c478bdstevel@tonic-gate	    CPUID_SELF_CPUID_MINOR, DDI_PSEUDO, 0));
897c478bdstevel@tonic-gate}
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gatestatic int
927c478bdstevel@tonic-gatecpuid_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
937c478bdstevel@tonic-gate{
947c478bdstevel@tonic-gate	if (cmd != DDI_DETACH)
957c478bdstevel@tonic-gate		return (DDI_FAILURE);
967c478bdstevel@tonic-gate	ddi_remove_minor_node(devi, NULL);
977c478bdstevel@tonic-gate	cpuid_devi = NULL;
987c478bdstevel@tonic-gate	return (DDI_SUCCESS);
997c478bdstevel@tonic-gate}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate/*ARGSUSED1*/
1027c478bdstevel@tonic-gatestatic int
1037c478bdstevel@tonic-gatecpuid_open(dev_t *dev, int flag, int otyp, cred_t *cr)
1047c478bdstevel@tonic-gate{
1057c478bdstevel@tonic-gate	return (getminor(*dev) == CPUID_SELF_CPUID_MINOR ? 0 : ENXIO);
1067c478bdstevel@tonic-gate}
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate#if defined(_HAVE_CPUID_INSN)
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate/*ARGSUSED*/
1117c478bdstevel@tonic-gatestatic int
1127c478bdstevel@tonic-gatecpuid_read(dev_t dev, uio_t *uio, cred_t *cr)
1137c478bdstevel@tonic-gate{
1148949bcdandrei	struct cpuid_regs crs;
1157c478bdstevel@tonic-gate	int error = 0;
1167c478bdstevel@tonic-gate
1177417cfdKuriakose Kuruvilla	if (!is_x86_feature(x86_featureset, X86FSET_CPUID))
1187c478bdstevel@tonic-gate		return (ENXIO);
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate	if (uio->uio_resid & (sizeof (crs) - 1))
1217c478bdstevel@tonic-gate		return (EINVAL);
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate	while (uio->uio_resid > 0) {
1247c478bdstevel@tonic-gate		u_offset_t uoff;
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate		if ((uoff = (u_offset_t)uio->uio_loffset) > UINT_MAX) {
1277c478bdstevel@tonic-gate			error = EINVAL;
1287c478bdstevel@tonic-gate			break;
1297c478bdstevel@tonic-gate		}
1307c478bdstevel@tonic-gate
1318949bcdandrei		crs.cp_eax = (uint32_t)uoff;
1328949bcdandrei		crs.cp_ebx = crs.cp_ecx = crs.cp_edx = 0;
1338949bcdandrei		(void) cpuid_insn(NULL, &crs);
1347c478bdstevel@tonic-gate
1358949bcdandrei		if ((error = uiomove(&crs, sizeof (crs), UIO_READ, uio)) != 0)
1367c478bdstevel@tonic-gate			break;
1377c478bdstevel@tonic-gate		uio->uio_loffset = uoff + 1;
1387c478bdstevel@tonic-gate	}
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gate	return (error);
1417c478bdstevel@tonic-gate}
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate#else
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate#define	cpuid_read	nodev
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate#endif	/* _HAVE_CPUID_INSN */
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gate/*ARGSUSED*/
1507c478bdstevel@tonic-gatestatic int
1517c478bdstevel@tonic-gatecpuid_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rval)
1527c478bdstevel@tonic-gate{
1537c478bdstevel@tonic-gate	char areq[16];
1547c478bdstevel@tonic-gate	void *ustr;
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate	switch (cmd) {
1577c478bdstevel@tonic-gate	case CPUID_GET_HWCAP: {
1587c478bdstevel@tonic-gate		STRUCT_DECL(cpuid_get_hwcap, h);
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate		STRUCT_INIT(h, mode);
1617c478bdstevel@tonic-gate		if (ddi_copyin((void *)arg,
1627c478bdstevel@tonic-gate		    STRUCT_BUF(h), STRUCT_SIZE(h), mode))
1637c478bdstevel@tonic-gate			return (EFAULT);
1647c478bdstevel@tonic-gate		if ((ustr = STRUCT_FGETP(h, cgh_archname)) != NULL &&
1657c478bdstevel@tonic-gate		    copyinstr(ustr, areq, sizeof (areq), NULL) != 0)
1667c478bdstevel@tonic-gate			return (EFAULT);
1677c478bdstevel@tonic-gate		areq[sizeof (areq) - 1] = '\0';
1687c478bdstevel@tonic-gate
169ebb8ac0Robert Mustacchi		if (strcmp(areq, architecture) == 0) {
170ebb8ac0Robert Mustacchi			STRUCT_FSET(h, cgh_hwcap[0], auxv_hwcap);
171ebb8ac0Robert Mustacchi			STRUCT_FSET(h, cgh_hwcap[1], auxv_hwcap_2);
1727c478bdstevel@tonic-gate#if defined(_SYSCALL32_IMPL)
173ebb8ac0Robert Mustacchi		} else if (strcmp(areq, architecture_32) == 0) {
174ebb8ac0Robert Mustacchi			STRUCT_FSET(h, cgh_hwcap[0], auxv_hwcap32);
175ebb8ac0Robert Mustacchi			STRUCT_FSET(h, cgh_hwcap[1], auxv_hwcap32_2);
1767c478bdstevel@tonic-gate#endif
177ebb8ac0Robert Mustacchi		} else {
178ebb8ac0Robert Mustacchi			STRUCT_FSET(h, cgh_hwcap[0], 0);
179ebb8ac0Robert Mustacchi			STRUCT_FSET(h, cgh_hwcap[1], 0);
180ebb8ac0Robert Mustacchi		}
1817c478bdstevel@tonic-gate		if (ddi_copyout(STRUCT_BUF(h),
1827c478bdstevel@tonic-gate		    (void *)arg, STRUCT_SIZE(h), mode))
1837c478bdstevel@tonic-gate			return (EFAULT);
1847c478bdstevel@tonic-gate		return (0);
1857c478bdstevel@tonic-gate	}
1867c478bdstevel@tonic-gate
1871259653John Levon#ifdef __x86
1881259653John Levon	case CPUID_RDMSR: {
1891259653John Levon		struct cpuid_rdmsr crm = { 0, };
1901259653John Levon		label_t label;
1911259653John Levon
1921259653John Levon		if (secpolicy_sys_config(cr, B_FALSE) != 0)
1931259653John Levon			return (EPERM);
1941259653John Levon
1951259653John Levon		if (ddi_copyin((void *)arg, &crm, sizeof (crm), mode))
1961259653John Levon			return (EFAULT);
1971259653John Levon
1981259653John Levon		kpreempt_disable();
1991259653John Levon
2001259653John Levon		if (on_fault(&label)) {
2011259653John Levon			kpreempt_enable();
2021259653John Levon			return (ENOENT);
2031259653John Levon		}
2041259653John Levon
2051259653John Levon		crm.cr_msr_val = rdmsr(crm.cr_msr_nr);
2061259653John Levon
2071259653John Levon		no_fault();
2081259653John Levon		kpreempt_enable();
2091259653John Levon
2101259653John Levon		if (ddi_copyout(&crm, (void *)arg, sizeof (crm), mode))
2111259653John Levon			return (EFAULT);
2121259653John Levon		return (0);
2131259653John Levon	}
2141259653John Levon#endif
2151259653John Levon
2167c478bdstevel@tonic-gate	default:
2177c478bdstevel@tonic-gate		return (ENOTTY);
2187c478bdstevel@tonic-gate	}
2197c478bdstevel@tonic-gate}
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gatestatic struct cb_ops cpuid_cb_ops = {
2227c478bdstevel@tonic-gate	cpuid_open,
2237c478bdstevel@tonic-gate	nulldev,	/* close */
2247c478bdstevel@tonic-gate	nodev,		/* strategy */
2257c478bdstevel@tonic-gate	nodev,		/* print */
2267c478bdstevel@tonic-gate	nodev,		/* dump */
2277c478bdstevel@tonic-gate	cpuid_read,
2287c478bdstevel@tonic-gate	nodev,		/* write */
2297c478bdstevel@tonic-gate	cpuid_ioctl,
2307c478bdstevel@tonic-gate	nodev,		/* devmap */
2317c478bdstevel@tonic-gate	nodev,		/* mmap */
2327c478bdstevel@tonic-gate	nodev,		/* segmap */
2337c478bdstevel@tonic-gate	nochpoll,	/* poll */
2347c478bdstevel@tonic-gate	ddi_prop_op,
2357c478bdstevel@tonic-gate	NULL,
2367c478bdstevel@tonic-gate	D_64BIT | D_NEW | D_MP
2377c478bdstevel@tonic-gate};
2387c478bdstevel@tonic-gate
2397c478bdstevel@tonic-gatestatic struct dev_ops cpuid_dv_ops = {
2407c478bdstevel@tonic-gate	DEVO_REV,
2417c478bdstevel@tonic-gate	0,
2427c478bdstevel@tonic-gate	cpuid_getinfo,
2437c478bdstevel@tonic-gate	nulldev,	/* identify */
2447c478bdstevel@tonic-gate	nulldev,	/* probe */
2457c478bdstevel@tonic-gate	cpuid_attach,
2467c478bdstevel@tonic-gate	cpuid_detach,
2477c478bdstevel@tonic-gate	nodev,		/* reset */
2487c478bdstevel@tonic-gate	&cpuid_cb_ops,
2491939740Sherry Moore	(struct bus_ops *)0,
2501939740Sherry Moore	NULL,
2511939740Sherry Moore	ddi_quiesce_not_needed,		/* quiesce */
2527c478bdstevel@tonic-gate};
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gatestatic struct modldrv modldrv = {
2557c478bdstevel@tonic-gate	&mod_driverops,
2561939740Sherry Moore	"cpuid driver",
2577c478bdstevel@tonic-gate	&cpuid_dv_ops
2587c478bdstevel@tonic-gate};
2597c478bdstevel@tonic-gate
2607c478bdstevel@tonic-gatestatic struct modlinkage modl = {
2617c478bdstevel@tonic-gate	MODREV_1,
2627c478bdstevel@tonic-gate	&modldrv
2637c478bdstevel@tonic-gate};
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gateint
2667c478bdstevel@tonic-gate_init(void)
2677c478bdstevel@tonic-gate{
2687c478bdstevel@tonic-gate	return (mod_install(&modl));
2697c478bdstevel@tonic-gate}
2707c478bdstevel@tonic-gate
2717c478bdstevel@tonic-gateint
2727c478bdstevel@tonic-gate_fini(void)
2737c478bdstevel@tonic-gate{
2747c478bdstevel@tonic-gate	return (mod_remove(&modl));
2757c478bdstevel@tonic-gate}
2767c478bdstevel@tonic-gate
2777c478bdstevel@tonic-gateint
2787c478bdstevel@tonic-gate_info(struct modinfo *modinfo)
2797c478bdstevel@tonic-gate{
2807c478bdstevel@tonic-gate	return (mod_info(&modl, modinfo));
2817c478bdstevel@tonic-gate}
282