xref: /openbmc/linux/drivers/xen/sys-hypervisor.c (revision 3a35093a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  copyright (c) 2006 IBM Corporation
4  *  Authored by: Mike D. Day <ncmike@us.ibm.com>
5  */
6 
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/kobject.h>
11 #include <linux/err.h>
12 
13 #include <asm/xen/hypervisor.h>
14 #include <asm/xen/hypercall.h>
15 
16 #include <xen/xen.h>
17 #include <xen/xenbus.h>
18 #include <xen/interface/xen.h>
19 #include <xen/interface/version.h>
20 #ifdef CONFIG_XEN_HAVE_VPMU
21 #include <xen/interface/xenpmu.h>
22 #endif
23 
24 #define HYPERVISOR_ATTR_RO(_name) \
25 static struct hyp_sysfs_attr  _name##_attr = __ATTR_RO(_name)
26 
27 #define HYPERVISOR_ATTR_RW(_name) \
28 static struct hyp_sysfs_attr _name##_attr = \
29 	__ATTR(_name, 0644, _name##_show, _name##_store)
30 
31 struct hyp_sysfs_attr {
32 	struct attribute attr;
33 	ssize_t (*show)(struct hyp_sysfs_attr *, char *);
34 	ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t);
35 	void *hyp_attr_data;
36 };
37 
38 static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
39 {
40 	return sprintf(buffer, "xen\n");
41 }
42 
43 HYPERVISOR_ATTR_RO(type);
44 
45 static int __init xen_sysfs_type_init(void)
46 {
47 	return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
48 }
49 
50 static ssize_t guest_type_show(struct hyp_sysfs_attr *attr, char *buffer)
51 {
52 	const char *type;
53 
54 	switch (xen_domain_type) {
55 	case XEN_NATIVE:
56 		/* ARM only. */
57 		type = "Xen";
58 		break;
59 	case XEN_PV_DOMAIN:
60 		type = "PV";
61 		break;
62 	case XEN_HVM_DOMAIN:
63 		type = xen_pvh_domain() ? "PVH" : "HVM";
64 		break;
65 	default:
66 		return -EINVAL;
67 	}
68 
69 	return sprintf(buffer, "%s\n", type);
70 }
71 
72 HYPERVISOR_ATTR_RO(guest_type);
73 
74 static int __init xen_sysfs_guest_type_init(void)
75 {
76 	return sysfs_create_file(hypervisor_kobj, &guest_type_attr.attr);
77 }
78 
79 /* xen version attributes */
80 static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
81 {
82 	int version = HYPERVISOR_xen_version(XENVER_version, NULL);
83 	if (version)
84 		return sprintf(buffer, "%d\n", version >> 16);
85 	return -ENODEV;
86 }
87 
88 HYPERVISOR_ATTR_RO(major);
89 
90 static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
91 {
92 	int version = HYPERVISOR_xen_version(XENVER_version, NULL);
93 	if (version)
94 		return sprintf(buffer, "%d\n", version & 0xff);
95 	return -ENODEV;
96 }
97 
98 HYPERVISOR_ATTR_RO(minor);
99 
100 static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
101 {
102 	int ret = -ENOMEM;
103 	char *extra;
104 
105 	extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
106 	if (extra) {
107 		ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
108 		if (!ret)
109 			ret = sprintf(buffer, "%s\n", extra);
110 		kfree(extra);
111 	}
112 
113 	return ret;
114 }
115 
116 HYPERVISOR_ATTR_RO(extra);
117 
118 static struct attribute *version_attrs[] = {
119 	&major_attr.attr,
120 	&minor_attr.attr,
121 	&extra_attr.attr,
122 	NULL
123 };
124 
125 static const struct attribute_group version_group = {
126 	.name = "version",
127 	.attrs = version_attrs,
128 };
129 
130 static int __init xen_sysfs_version_init(void)
131 {
132 	return sysfs_create_group(hypervisor_kobj, &version_group);
133 }
134 
135 /* UUID */
136 
137 static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer)
138 {
139 	char *vm, *val;
140 	int ret;
141 	extern int xenstored_ready;
142 
143 	if (!xenstored_ready)
144 		return -EBUSY;
145 
146 	vm = xenbus_read(XBT_NIL, "vm", "", NULL);
147 	if (IS_ERR(vm))
148 		return PTR_ERR(vm);
149 	val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
150 	kfree(vm);
151 	if (IS_ERR(val))
152 		return PTR_ERR(val);
153 	ret = sprintf(buffer, "%s\n", val);
154 	kfree(val);
155 	return ret;
156 }
157 
158 static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
159 {
160 	xen_domain_handle_t uuid;
161 	int ret;
162 	ret = HYPERVISOR_xen_version(XENVER_guest_handle, uuid);
163 	if (ret)
164 		return uuid_show_fallback(attr, buffer);
165 	ret = sprintf(buffer, "%pU\n", uuid);
166 	return ret;
167 }
168 
169 HYPERVISOR_ATTR_RO(uuid);
170 
171 static int __init xen_sysfs_uuid_init(void)
172 {
173 	return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
174 }
175 
176 /* xen compilation attributes */
177 
178 static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer)
179 {
180 	int ret = -ENOMEM;
181 	struct xen_compile_info *info;
182 
183 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
184 	if (info) {
185 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
186 		if (!ret)
187 			ret = sprintf(buffer, "%s\n", info->compiler);
188 		kfree(info);
189 	}
190 
191 	return ret;
192 }
193 
194 HYPERVISOR_ATTR_RO(compiler);
195 
196 static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer)
197 {
198 	int ret = -ENOMEM;
199 	struct xen_compile_info *info;
200 
201 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
202 	if (info) {
203 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
204 		if (!ret)
205 			ret = sprintf(buffer, "%s\n", info->compile_by);
206 		kfree(info);
207 	}
208 
209 	return ret;
210 }
211 
212 HYPERVISOR_ATTR_RO(compiled_by);
213 
214 static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
215 {
216 	int ret = -ENOMEM;
217 	struct xen_compile_info *info;
218 
219 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
220 	if (info) {
221 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
222 		if (!ret)
223 			ret = sprintf(buffer, "%s\n", info->compile_date);
224 		kfree(info);
225 	}
226 
227 	return ret;
228 }
229 
230 HYPERVISOR_ATTR_RO(compile_date);
231 
232 static struct attribute *xen_compile_attrs[] = {
233 	&compiler_attr.attr,
234 	&compiled_by_attr.attr,
235 	&compile_date_attr.attr,
236 	NULL
237 };
238 
239 static const struct attribute_group xen_compilation_group = {
240 	.name = "compilation",
241 	.attrs = xen_compile_attrs,
242 };
243 
244 static int __init xen_sysfs_compilation_init(void)
245 {
246 	return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
247 }
248 
249 /* xen properties info */
250 
251 static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
252 {
253 	int ret = -ENOMEM;
254 	char *caps;
255 
256 	caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
257 	if (caps) {
258 		ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
259 		if (!ret)
260 			ret = sprintf(buffer, "%s\n", caps);
261 		kfree(caps);
262 	}
263 
264 	return ret;
265 }
266 
267 HYPERVISOR_ATTR_RO(capabilities);
268 
269 static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
270 {
271 	int ret = -ENOMEM;
272 	char *cset;
273 
274 	cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
275 	if (cset) {
276 		ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
277 		if (!ret)
278 			ret = sprintf(buffer, "%s\n", cset);
279 		kfree(cset);
280 	}
281 
282 	return ret;
283 }
284 
285 HYPERVISOR_ATTR_RO(changeset);
286 
287 static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
288 {
289 	int ret = -ENOMEM;
290 	struct xen_platform_parameters *parms;
291 
292 	parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
293 	if (parms) {
294 		ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
295 					     parms);
296 		if (!ret)
297 			ret = sprintf(buffer, "%"PRI_xen_ulong"\n",
298 				      parms->virt_start);
299 		kfree(parms);
300 	}
301 
302 	return ret;
303 }
304 
305 HYPERVISOR_ATTR_RO(virtual_start);
306 
307 static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
308 {
309 	int ret;
310 
311 	ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
312 	if (ret > 0)
313 		ret = sprintf(buffer, "%x\n", ret);
314 
315 	return ret;
316 }
317 
318 HYPERVISOR_ATTR_RO(pagesize);
319 
320 static ssize_t xen_feature_show(int index, char *buffer)
321 {
322 	ssize_t ret;
323 	struct xen_feature_info info;
324 
325 	info.submap_idx = index;
326 	ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
327 	if (!ret)
328 		ret = sprintf(buffer, "%08x", info.submap);
329 
330 	return ret;
331 }
332 
333 static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
334 {
335 	ssize_t len;
336 	int i;
337 
338 	len = 0;
339 	for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
340 		int ret = xen_feature_show(i, buffer + len);
341 		if (ret < 0) {
342 			if (len == 0)
343 				len = ret;
344 			break;
345 		}
346 		len += ret;
347 	}
348 	if (len > 0)
349 		buffer[len++] = '\n';
350 
351 	return len;
352 }
353 
354 HYPERVISOR_ATTR_RO(features);
355 
356 static ssize_t buildid_show(struct hyp_sysfs_attr *attr, char *buffer)
357 {
358 	ssize_t ret;
359 	struct xen_build_id *buildid;
360 
361 	ret = HYPERVISOR_xen_version(XENVER_build_id, NULL);
362 	if (ret < 0) {
363 		if (ret == -EPERM)
364 			ret = sprintf(buffer, "<denied>");
365 		return ret;
366 	}
367 
368 	buildid = kmalloc(sizeof(*buildid) + ret, GFP_KERNEL);
369 	if (!buildid)
370 		return -ENOMEM;
371 
372 	buildid->len = ret;
373 	ret = HYPERVISOR_xen_version(XENVER_build_id, buildid);
374 	if (ret > 0)
375 		ret = sprintf(buffer, "%s", buildid->buf);
376 	kfree(buildid);
377 
378 	return ret;
379 }
380 
381 HYPERVISOR_ATTR_RO(buildid);
382 
383 static struct attribute *xen_properties_attrs[] = {
384 	&capabilities_attr.attr,
385 	&changeset_attr.attr,
386 	&virtual_start_attr.attr,
387 	&pagesize_attr.attr,
388 	&features_attr.attr,
389 	&buildid_attr.attr,
390 	NULL
391 };
392 
393 static const struct attribute_group xen_properties_group = {
394 	.name = "properties",
395 	.attrs = xen_properties_attrs,
396 };
397 
398 static int __init xen_sysfs_properties_init(void)
399 {
400 	return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
401 }
402 
403 #ifdef CONFIG_XEN_HAVE_VPMU
404 struct pmu_mode {
405 	const char *name;
406 	uint32_t mode;
407 };
408 
409 static struct pmu_mode pmu_modes[] = {
410 	{"off", XENPMU_MODE_OFF},
411 	{"self", XENPMU_MODE_SELF},
412 	{"hv", XENPMU_MODE_HV},
413 	{"all", XENPMU_MODE_ALL}
414 };
415 
416 static ssize_t pmu_mode_store(struct hyp_sysfs_attr *attr,
417 			      const char *buffer, size_t len)
418 {
419 	int ret;
420 	struct xen_pmu_params xp;
421 	int i;
422 
423 	for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
424 		if (strncmp(buffer, pmu_modes[i].name, len - 1) == 0) {
425 			xp.val = pmu_modes[i].mode;
426 			break;
427 		}
428 	}
429 
430 	if (i == ARRAY_SIZE(pmu_modes))
431 		return -EINVAL;
432 
433 	xp.version.maj = XENPMU_VER_MAJ;
434 	xp.version.min = XENPMU_VER_MIN;
435 	ret = HYPERVISOR_xenpmu_op(XENPMU_mode_set, &xp);
436 	if (ret)
437 		return ret;
438 
439 	return len;
440 }
441 
442 static ssize_t pmu_mode_show(struct hyp_sysfs_attr *attr, char *buffer)
443 {
444 	int ret;
445 	struct xen_pmu_params xp;
446 	int i;
447 	uint32_t mode;
448 
449 	xp.version.maj = XENPMU_VER_MAJ;
450 	xp.version.min = XENPMU_VER_MIN;
451 	ret = HYPERVISOR_xenpmu_op(XENPMU_mode_get, &xp);
452 	if (ret)
453 		return ret;
454 
455 	mode = (uint32_t)xp.val;
456 	for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
457 		if (mode == pmu_modes[i].mode)
458 			return sprintf(buffer, "%s\n", pmu_modes[i].name);
459 	}
460 
461 	return -EINVAL;
462 }
463 HYPERVISOR_ATTR_RW(pmu_mode);
464 
465 static ssize_t pmu_features_store(struct hyp_sysfs_attr *attr,
466 				  const char *buffer, size_t len)
467 {
468 	int ret;
469 	uint32_t features;
470 	struct xen_pmu_params xp;
471 
472 	ret = kstrtou32(buffer, 0, &features);
473 	if (ret)
474 		return ret;
475 
476 	xp.val = features;
477 	xp.version.maj = XENPMU_VER_MAJ;
478 	xp.version.min = XENPMU_VER_MIN;
479 	ret = HYPERVISOR_xenpmu_op(XENPMU_feature_set, &xp);
480 	if (ret)
481 		return ret;
482 
483 	return len;
484 }
485 
486 static ssize_t pmu_features_show(struct hyp_sysfs_attr *attr, char *buffer)
487 {
488 	int ret;
489 	struct xen_pmu_params xp;
490 
491 	xp.version.maj = XENPMU_VER_MAJ;
492 	xp.version.min = XENPMU_VER_MIN;
493 	ret = HYPERVISOR_xenpmu_op(XENPMU_feature_get, &xp);
494 	if (ret)
495 		return ret;
496 
497 	return sprintf(buffer, "0x%x\n", (uint32_t)xp.val);
498 }
499 HYPERVISOR_ATTR_RW(pmu_features);
500 
501 static struct attribute *xen_pmu_attrs[] = {
502 	&pmu_mode_attr.attr,
503 	&pmu_features_attr.attr,
504 	NULL
505 };
506 
507 static const struct attribute_group xen_pmu_group = {
508 	.name = "pmu",
509 	.attrs = xen_pmu_attrs,
510 };
511 
512 static int __init xen_sysfs_pmu_init(void)
513 {
514 	return sysfs_create_group(hypervisor_kobj, &xen_pmu_group);
515 }
516 #endif
517 
518 static int __init hyper_sysfs_init(void)
519 {
520 	int ret;
521 
522 	if (!xen_domain())
523 		return -ENODEV;
524 
525 	ret = xen_sysfs_type_init();
526 	if (ret)
527 		goto out;
528 	ret = xen_sysfs_guest_type_init();
529 	if (ret)
530 		goto guest_type_out;
531 	ret = xen_sysfs_version_init();
532 	if (ret)
533 		goto version_out;
534 	ret = xen_sysfs_compilation_init();
535 	if (ret)
536 		goto comp_out;
537 	ret = xen_sysfs_uuid_init();
538 	if (ret)
539 		goto uuid_out;
540 	ret = xen_sysfs_properties_init();
541 	if (ret)
542 		goto prop_out;
543 #ifdef CONFIG_XEN_HAVE_VPMU
544 	if (xen_initial_domain()) {
545 		ret = xen_sysfs_pmu_init();
546 		if (ret) {
547 			sysfs_remove_group(hypervisor_kobj,
548 					   &xen_properties_group);
549 			goto prop_out;
550 		}
551 	}
552 #endif
553 	goto out;
554 
555 prop_out:
556 	sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
557 uuid_out:
558 	sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
559 comp_out:
560 	sysfs_remove_group(hypervisor_kobj, &version_group);
561 version_out:
562 	sysfs_remove_file(hypervisor_kobj, &guest_type_attr.attr);
563 guest_type_out:
564 	sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
565 out:
566 	return ret;
567 }
568 device_initcall(hyper_sysfs_init);
569 
570 static ssize_t hyp_sysfs_show(struct kobject *kobj,
571 			      struct attribute *attr,
572 			      char *buffer)
573 {
574 	struct hyp_sysfs_attr *hyp_attr;
575 	hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
576 	if (hyp_attr->show)
577 		return hyp_attr->show(hyp_attr, buffer);
578 	return 0;
579 }
580 
581 static ssize_t hyp_sysfs_store(struct kobject *kobj,
582 			       struct attribute *attr,
583 			       const char *buffer,
584 			       size_t len)
585 {
586 	struct hyp_sysfs_attr *hyp_attr;
587 	hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
588 	if (hyp_attr->store)
589 		return hyp_attr->store(hyp_attr, buffer, len);
590 	return 0;
591 }
592 
593 static const struct sysfs_ops hyp_sysfs_ops = {
594 	.show = hyp_sysfs_show,
595 	.store = hyp_sysfs_store,
596 };
597 
598 static struct kobj_type hyp_sysfs_kobj_type = {
599 	.sysfs_ops = &hyp_sysfs_ops,
600 };
601 
602 static int __init hypervisor_subsys_init(void)
603 {
604 	if (!xen_domain())
605 		return -ENODEV;
606 
607 	hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
608 	return 0;
609 }
610 device_initcall(hypervisor_subsys_init);
611