1 // SPDX-License-Identifier: GPL-2.0-only
3 * copyright (c) 2006 IBM Corporation
4 * Authored by: Mike D. Day <ncmike@us.ibm.com>
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>
13 #include <asm/xen/hypervisor.h>
14 #include <asm/xen/hypercall.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>
24 #define HYPERVISOR_ATTR_RO(_name) \
25 static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name)
27 #define HYPERVISOR_ATTR_RW(_name) \
28 static struct hyp_sysfs_attr _name##_attr = __ATTR_RW(_name)
30 struct hyp_sysfs_attr {
31 struct attribute attr;
32 ssize_t (*show)(struct hyp_sysfs_attr *, char *);
33 ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t);
37 static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
39 return sprintf(buffer, "xen\n");
42 HYPERVISOR_ATTR_RO(type);
44 static int __init xen_sysfs_type_init(void)
46 return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
49 static ssize_t guest_type_show(struct hyp_sysfs_attr *attr, char *buffer)
53 switch (xen_domain_type) {
62 type = xen_pvh_domain() ? "PVH" : "HVM";
68 return sprintf(buffer, "%s\n", type);
71 HYPERVISOR_ATTR_RO(guest_type);
73 static int __init xen_sysfs_guest_type_init(void)
75 return sysfs_create_file(hypervisor_kobj, &guest_type_attr.attr);
78 /* xen version attributes */
79 static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
81 int version = HYPERVISOR_xen_version(XENVER_version, NULL);
83 return sprintf(buffer, "%d\n", version >> 16);
87 HYPERVISOR_ATTR_RO(major);
89 static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
91 int version = HYPERVISOR_xen_version(XENVER_version, NULL);
93 return sprintf(buffer, "%d\n", version & 0xff);
97 HYPERVISOR_ATTR_RO(minor);
99 static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
104 extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
106 ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
108 ret = sprintf(buffer, "%s\n", extra);
115 HYPERVISOR_ATTR_RO(extra);
117 static struct attribute *version_attrs[] = {
124 static const struct attribute_group version_group = {
126 .attrs = version_attrs,
129 static int __init xen_sysfs_version_init(void)
131 return sysfs_create_group(hypervisor_kobj, &version_group);
136 static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer)
140 extern int xenstored_ready;
142 if (!xenstored_ready)
145 vm = xenbus_read(XBT_NIL, "vm", "", NULL);
148 val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
152 ret = sprintf(buffer, "%s\n", val);
157 static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
159 xen_domain_handle_t uuid;
161 ret = HYPERVISOR_xen_version(XENVER_guest_handle, uuid);
163 return uuid_show_fallback(attr, buffer);
164 ret = sprintf(buffer, "%pU\n", uuid);
168 HYPERVISOR_ATTR_RO(uuid);
170 static int __init xen_sysfs_uuid_init(void)
172 return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
175 /* xen compilation attributes */
177 static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer)
180 struct xen_compile_info *info;
182 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
184 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
186 ret = sprintf(buffer, "%s\n", info->compiler);
193 HYPERVISOR_ATTR_RO(compiler);
195 static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer)
198 struct xen_compile_info *info;
200 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
202 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
204 ret = sprintf(buffer, "%s\n", info->compile_by);
211 HYPERVISOR_ATTR_RO(compiled_by);
213 static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
216 struct xen_compile_info *info;
218 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
220 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
222 ret = sprintf(buffer, "%s\n", info->compile_date);
229 HYPERVISOR_ATTR_RO(compile_date);
231 static struct attribute *xen_compile_attrs[] = {
233 &compiled_by_attr.attr,
234 &compile_date_attr.attr,
238 static const struct attribute_group xen_compilation_group = {
239 .name = "compilation",
240 .attrs = xen_compile_attrs,
243 static int __init xen_sysfs_compilation_init(void)
245 return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
248 /* xen properties info */
250 static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
255 caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
257 ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
259 ret = sprintf(buffer, "%s\n", caps);
266 HYPERVISOR_ATTR_RO(capabilities);
268 static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
273 cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
275 ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
277 ret = sprintf(buffer, "%s\n", cset);
284 HYPERVISOR_ATTR_RO(changeset);
286 static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
289 struct xen_platform_parameters *parms;
291 parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
293 ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
296 ret = sprintf(buffer, "%"PRI_xen_ulong"\n",
304 HYPERVISOR_ATTR_RO(virtual_start);
306 static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
310 ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
312 ret = sprintf(buffer, "%x\n", ret);
317 HYPERVISOR_ATTR_RO(pagesize);
319 static ssize_t xen_feature_show(int index, char *buffer)
322 struct xen_feature_info info;
324 info.submap_idx = index;
325 ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
327 ret = sprintf(buffer, "%08x", info.submap);
332 static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
338 for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
339 int ret = xen_feature_show(i, buffer + len);
348 buffer[len++] = '\n';
353 HYPERVISOR_ATTR_RO(features);
355 static ssize_t buildid_show(struct hyp_sysfs_attr *attr, char *buffer)
358 struct xen_build_id *buildid;
360 ret = HYPERVISOR_xen_version(XENVER_build_id, NULL);
363 ret = sprintf(buffer, "<denied>");
367 buildid = kmalloc(sizeof(*buildid) + ret, GFP_KERNEL);
372 ret = HYPERVISOR_xen_version(XENVER_build_id, buildid);
374 ret = sprintf(buffer, "%s", buildid->buf);
380 HYPERVISOR_ATTR_RO(buildid);
382 static struct attribute *xen_properties_attrs[] = {
383 &capabilities_attr.attr,
384 &changeset_attr.attr,
385 &virtual_start_attr.attr,
392 static const struct attribute_group xen_properties_group = {
393 .name = "properties",
394 .attrs = xen_properties_attrs,
397 static int __init xen_sysfs_properties_init(void)
399 return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
402 #ifdef CONFIG_XEN_HAVE_VPMU
408 static struct pmu_mode pmu_modes[] = {
409 {"off", XENPMU_MODE_OFF},
410 {"self", XENPMU_MODE_SELF},
411 {"hv", XENPMU_MODE_HV},
412 {"all", XENPMU_MODE_ALL}
415 static ssize_t pmu_mode_store(struct hyp_sysfs_attr *attr,
416 const char *buffer, size_t len)
419 struct xen_pmu_params xp;
422 for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
423 if (strncmp(buffer, pmu_modes[i].name, len - 1) == 0) {
424 xp.val = pmu_modes[i].mode;
429 if (i == ARRAY_SIZE(pmu_modes))
432 xp.version.maj = XENPMU_VER_MAJ;
433 xp.version.min = XENPMU_VER_MIN;
434 ret = HYPERVISOR_xenpmu_op(XENPMU_mode_set, &xp);
441 static ssize_t pmu_mode_show(struct hyp_sysfs_attr *attr, char *buffer)
444 struct xen_pmu_params xp;
448 xp.version.maj = XENPMU_VER_MAJ;
449 xp.version.min = XENPMU_VER_MIN;
450 ret = HYPERVISOR_xenpmu_op(XENPMU_mode_get, &xp);
454 mode = (uint32_t)xp.val;
455 for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
456 if (mode == pmu_modes[i].mode)
457 return sprintf(buffer, "%s\n", pmu_modes[i].name);
462 HYPERVISOR_ATTR_RW(pmu_mode);
464 static ssize_t pmu_features_store(struct hyp_sysfs_attr *attr,
465 const char *buffer, size_t len)
469 struct xen_pmu_params xp;
471 ret = kstrtou32(buffer, 0, &features);
476 xp.version.maj = XENPMU_VER_MAJ;
477 xp.version.min = XENPMU_VER_MIN;
478 ret = HYPERVISOR_xenpmu_op(XENPMU_feature_set, &xp);
485 static ssize_t pmu_features_show(struct hyp_sysfs_attr *attr, char *buffer)
488 struct xen_pmu_params xp;
490 xp.version.maj = XENPMU_VER_MAJ;
491 xp.version.min = XENPMU_VER_MIN;
492 ret = HYPERVISOR_xenpmu_op(XENPMU_feature_get, &xp);
496 return sprintf(buffer, "0x%x\n", (uint32_t)xp.val);
498 HYPERVISOR_ATTR_RW(pmu_features);
500 static struct attribute *xen_pmu_attrs[] = {
502 &pmu_features_attr.attr,
506 static const struct attribute_group xen_pmu_group = {
508 .attrs = xen_pmu_attrs,
511 static int __init xen_sysfs_pmu_init(void)
513 return sysfs_create_group(hypervisor_kobj, &xen_pmu_group);
517 static int __init hyper_sysfs_init(void)
524 ret = xen_sysfs_type_init();
527 ret = xen_sysfs_guest_type_init();
530 ret = xen_sysfs_version_init();
533 ret = xen_sysfs_compilation_init();
536 ret = xen_sysfs_uuid_init();
539 ret = xen_sysfs_properties_init();
542 #ifdef CONFIG_XEN_HAVE_VPMU
543 if (xen_initial_domain()) {
544 ret = xen_sysfs_pmu_init();
546 sysfs_remove_group(hypervisor_kobj,
547 &xen_properties_group);
555 sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
557 sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
559 sysfs_remove_group(hypervisor_kobj, &version_group);
561 sysfs_remove_file(hypervisor_kobj, &guest_type_attr.attr);
563 sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
567 device_initcall(hyper_sysfs_init);
569 static ssize_t hyp_sysfs_show(struct kobject *kobj,
570 struct attribute *attr,
573 struct hyp_sysfs_attr *hyp_attr;
574 hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
576 return hyp_attr->show(hyp_attr, buffer);
580 static ssize_t hyp_sysfs_store(struct kobject *kobj,
581 struct attribute *attr,
585 struct hyp_sysfs_attr *hyp_attr;
586 hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
588 return hyp_attr->store(hyp_attr, buffer, len);
592 static const struct sysfs_ops hyp_sysfs_ops = {
593 .show = hyp_sysfs_show,
594 .store = hyp_sysfs_store,
597 static struct kobj_type hyp_sysfs_kobj_type = {
598 .sysfs_ops = &hyp_sysfs_ops,
601 static int __init hypervisor_subsys_init(void)
606 hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
609 device_initcall(hypervisor_subsys_init);