xref: /titanic_51/usr/src/uts/common/io/cpuid_drv.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/file.h>
31 #include <sys/errno.h>
32 #include <sys/open.h>
33 #include <sys/cred.h>
34 #include <sys/conf.h>
35 #include <sys/stat.h>
36 #include <sys/processor.h>
37 #include <sys/cpuvar.h>
38 #include <sys/kmem.h>
39 #include <sys/modctl.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 
43 #include <sys/auxv.h>
44 #include <sys/cpuid_drv.h>
45 #include <sys/systeminfo.h>
46 
47 #if defined(__x86)
48 #include <sys/x86_archext.h>
49 #endif
50 
51 static dev_info_t *cpuid_devi;
52 
53 /*ARGSUSED*/
54 static int
55 cpuid_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
56 {
57 	switch (cmd) {
58 	case DDI_INFO_DEVT2DEVINFO:
59 	case DDI_INFO_DEVT2INSTANCE:
60 		break;
61 	default:
62 		return (DDI_FAILURE);
63 	}
64 
65 	switch (getminor((dev_t)arg)) {
66 	case CPUID_SELF_CPUID_MINOR:
67 		break;
68 	default:
69 		return (DDI_FAILURE);
70 	}
71 
72 	if (cmd == DDI_INFO_DEVT2INSTANCE)
73 		*result = 0;
74 	else
75 		*result = cpuid_devi;
76 	return (DDI_SUCCESS);
77 }
78 
79 static int
80 cpuid_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
81 {
82 	if (cmd != DDI_ATTACH)
83 		return (DDI_FAILURE);
84 	cpuid_devi = devi;
85 
86 	return (ddi_create_minor_node(devi, CPUID_DRIVER_SELF_NODE, S_IFCHR,
87 	    CPUID_SELF_CPUID_MINOR, DDI_PSEUDO, 0));
88 }
89 
90 static int
91 cpuid_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
92 {
93 	if (cmd != DDI_DETACH)
94 		return (DDI_FAILURE);
95 	ddi_remove_minor_node(devi, NULL);
96 	cpuid_devi = NULL;
97 	return (DDI_SUCCESS);
98 }
99 
100 /*ARGSUSED1*/
101 static int
102 cpuid_open(dev_t *dev, int flag, int otyp, cred_t *cr)
103 {
104 	return (getminor(*dev) == CPUID_SELF_CPUID_MINOR ? 0 : ENXIO);
105 }
106 
107 #if defined(_HAVE_CPUID_INSN)
108 
109 /*ARGSUSED*/
110 static int
111 cpuid_read(dev_t dev, uio_t *uio, cred_t *cr)
112 {
113 	uint32_t crs[4];
114 	int error = 0;
115 
116 	if ((x86_feature & X86_CPUID) == 0)
117 		return (ENXIO);
118 
119 	if (uio->uio_resid & (sizeof (crs) - 1))
120 		return (EINVAL);
121 
122 	while (uio->uio_resid > 0) {
123 		u_offset_t uoff;
124 
125 		if ((uoff = (u_offset_t)uio->uio_loffset) > UINT_MAX) {
126 			error = EINVAL;
127 			break;
128 		}
129 
130 		crs[0] = cpuid_insn(NULL,
131 		    (uint32_t)uoff, &crs[1], &crs[2], &crs[3]);
132 
133 		if ((error = uiomove(crs, sizeof (crs), UIO_READ, uio)) != 0)
134 			break;
135 		uio->uio_loffset = uoff + 1;
136 	}
137 
138 	return (error);
139 }
140 
141 #else
142 
143 #define	cpuid_read	nodev
144 
145 #endif	/* _HAVE_CPUID_INSN */
146 
147 /*ARGSUSED*/
148 static int
149 cpuid_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rval)
150 {
151 	char areq[16];
152 	void *ustr;
153 
154 	switch (cmd) {
155 	case CPUID_GET_HWCAP: {
156 		STRUCT_DECL(cpuid_get_hwcap, h);
157 
158 		STRUCT_INIT(h, mode);
159 		if (ddi_copyin((void *)arg,
160 		    STRUCT_BUF(h), STRUCT_SIZE(h), mode))
161 			return (EFAULT);
162 		if ((ustr = STRUCT_FGETP(h, cgh_archname)) != NULL &&
163 		    copyinstr(ustr, areq, sizeof (areq), NULL) != 0)
164 			return (EFAULT);
165 		areq[sizeof (areq) - 1] = '\0';
166 
167 		if (strcmp(areq, architecture) == 0)
168 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap);
169 #if defined(_SYSCALL32_IMPL)
170 		else if (strcmp(areq, architecture_32) == 0)
171 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap32);
172 #endif
173 		else
174 			STRUCT_FSET(h, cgh_hwcap, 0);
175 		if (ddi_copyout(STRUCT_BUF(h),
176 		    (void *)arg, STRUCT_SIZE(h), mode))
177 			return (EFAULT);
178 		return (0);
179 	}
180 
181 	default:
182 		return (ENOTTY);
183 	}
184 }
185 
186 static struct cb_ops cpuid_cb_ops = {
187 	cpuid_open,
188 	nulldev,	/* close */
189 	nodev,		/* strategy */
190 	nodev,		/* print */
191 	nodev,		/* dump */
192 	cpuid_read,
193 	nodev,		/* write */
194 	cpuid_ioctl,
195 	nodev,		/* devmap */
196 	nodev,		/* mmap */
197 	nodev,		/* segmap */
198 	nochpoll,	/* poll */
199 	ddi_prop_op,
200 	NULL,
201 	D_64BIT | D_NEW | D_MP
202 };
203 
204 static struct dev_ops cpuid_dv_ops = {
205 	DEVO_REV,
206 	0,
207 	cpuid_getinfo,
208 	nulldev,	/* identify */
209 	nulldev,	/* probe */
210 	cpuid_attach,
211 	cpuid_detach,
212 	nodev,		/* reset */
213 	&cpuid_cb_ops,
214 	(struct bus_ops *)0
215 };
216 
217 static struct modldrv modldrv = {
218 	&mod_driverops,
219 	"cpuid driver v%I%",
220 	&cpuid_dv_ops
221 };
222 
223 static struct modlinkage modl = {
224 	MODREV_1,
225 	&modldrv
226 };
227 
228 int
229 _init(void)
230 {
231 	return (mod_install(&modl));
232 }
233 
234 int
235 _fini(void)
236 {
237 	return (mod_remove(&modl));
238 }
239 
240 int
241 _info(struct modinfo *modinfo)
242 {
243 	return (mod_info(&modl, modinfo));
244 }
245