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