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 = \
29 __ATTR(_name, 0644, _name##_show, _name##_store)
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);
38 static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
40 return sprintf(buffer, "xen\n");
43 HYPERVISOR_ATTR_RO(type);
45 static int __init xen_sysfs_type_init(void)
47 return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
50 static ssize_t guest_type_show(struct hyp_sysfs_attr *attr, char *buffer)
54 switch (xen_domain_type) {
63 type = xen_pvh_domain() ? "PVH" : "HVM";
69 return sprintf(buffer, "%s\n", type);
72 HYPERVISOR_ATTR_RO(guest_type);
74 static int __init xen_sysfs_guest_type_init(void)
76 return sysfs_create_file(hypervisor_kobj, &guest_type_attr.attr);
79 /* xen version attributes */
80 static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
82 int version = HYPERVISOR_xen_version(XENVER_version, NULL);
84 return sprintf(buffer, "%d\n", version >> 16);
88 HYPERVISOR_ATTR_RO(major);
90 static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
92 int version = HYPERVISOR_xen_version(XENVER_version, NULL);
94 return sprintf(buffer, "%d\n", version & 0xff);
98 HYPERVISOR_ATTR_RO(minor);
100 static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
105 extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
107 ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
109 ret = sprintf(buffer, "%s\n", extra);
116 HYPERVISOR_ATTR_RO(extra);
118 static struct attribute *version_attrs[] = {
125 static const struct attribute_group version_group = {
127 .attrs = version_attrs,
130 static int __init xen_sysfs_version_init(void)
132 return sysfs_create_group(hypervisor_kobj, &version_group);
137 static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer)
141 extern int xenstored_ready;
143 if (!xenstored_ready)
146 vm = xenbus_read(XBT_NIL, "vm", "", NULL);
149 val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
153 ret = sprintf(buffer, "%s\n", val);
158 static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
160 xen_domain_handle_t uuid;
162 ret = HYPERVISOR_xen_version(XENVER_guest_handle, uuid);
164 return uuid_show_fallback(attr, buffer);
165 ret = sprintf(buffer, "%pU\n", uuid);
169 HYPERVISOR_ATTR_RO(uuid);
171 static int __init xen_sysfs_uuid_init(void)
173 return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
176 /* xen compilation attributes */
178 static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer)
181 struct xen_compile_info *info;
183 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
185 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
187 ret = sprintf(buffer, "%s\n", info->compiler);
194 HYPERVISOR_ATTR_RO(compiler);
196 static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer)
199 struct xen_compile_info *info;
201 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
203 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
205 ret = sprintf(buffer, "%s\n", info->compile_by);
212 HYPERVISOR_ATTR_RO(compiled_by);
214 static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
217 struct xen_compile_info *info;
219 info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
221 ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
223 ret = sprintf(buffer, "%s\n", info->compile_date);
230 HYPERVISOR_ATTR_RO(compile_date);
232 static struct attribute *xen_compile_attrs[] = {
234 &compiled_by_attr.attr,
235 &compile_date_attr.attr,
239 static const struct attribute_group xen_compilation_group = {
240 .name = "compilation",
241 .attrs = xen_compile_attrs,
244 static int __init xen_sysfs_compilation_init(void)
246 return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
249 /* xen properties info */
251 static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
256 caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
258 ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
260 ret = sprintf(buffer, "%s\n", caps);
267 HYPERVISOR_ATTR_RO(capabilities);
269 static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
274 cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
276 ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
278 ret = sprintf(buffer, "%s\n", cset);
285 HYPERVISOR_ATTR_RO(changeset);
287 static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
290 struct xen_platform_parameters *parms;
292 parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
294 ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
297 ret = sprintf(buffer, "%"PRI_xen_ulong"\n",
305 HYPERVISOR_ATTR_RO(virtual_start);
307 static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
311 ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
313 ret = sprintf(buffer, "%x\n", ret);
318 HYPERVISOR_ATTR_RO(pagesize);
320 static ssize_t xen_feature_show(int index, char *buffer)
323 struct xen_feature_info info;
325 info.submap_idx = index;
326 ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
328 ret = sprintf(buffer, "%08x", info.submap);
333 static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
339 for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
340 int ret = xen_feature_show(i, buffer + len);
349 buffer[len++] = '\n';
354 HYPERVISOR_ATTR_RO(features);
356 static ssize_t buildid_show(struct hyp_sysfs_attr *attr, char *buffer)
359 struct xen_build_id *buildid;
361 ret = HYPERVISOR_xen_version(XENVER_build_id, NULL);
364 ret = sprintf(buffer, "<denied>");
368 buildid = kmalloc(sizeof(*buildid) + ret, GFP_KERNEL);
373 ret = HYPERVISOR_xen_version(XENVER_build_id, buildid);
375 ret = sprintf(buffer, "%s", buildid->buf);
381 HYPERVISOR_ATTR_RO(buildid);
383 static struct attribute *xen_properties_attrs[] = {
384 &capabilities_attr.attr,
385 &changeset_attr.attr,
386 &virtual_start_attr.attr,
393 static const struct attribute_group xen_properties_group = {
394 .name = "properties",
395 .attrs = xen_properties_attrs,
398 static int __init xen_sysfs_properties_init(void)
400 return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
403 #ifdef CONFIG_XEN_HAVE_VPMU
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}
416 static ssize_t pmu_mode_store(struct hyp_sysfs_attr *attr,
417 const char *buffer, size_t len)
420 struct xen_pmu_params xp;
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;
430 if (i == ARRAY_SIZE(pmu_modes))
433 xp.version.maj = XENPMU_VER_MAJ;
434 xp.version.min = XENPMU_VER_MIN;
435 ret = HYPERVISOR_xenpmu_op(XENPMU_mode_set, &xp);
442 static ssize_t pmu_mode_show(struct hyp_sysfs_attr *attr, char *buffer)
445 struct xen_pmu_params xp;
449 xp.version.maj = XENPMU_VER_MAJ;
450 xp.version.min = XENPMU_VER_MIN;
451 ret = HYPERVISOR_xenpmu_op(XENPMU_mode_get, &xp);
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);
463 HYPERVISOR_ATTR_RW(pmu_mode);
465 static ssize_t pmu_features_store(struct hyp_sysfs_attr *attr,
466 const char *buffer, size_t len)
470 struct xen_pmu_params xp;
472 ret = kstrtou32(buffer, 0, &features);
477 xp.version.maj = XENPMU_VER_MAJ;
478 xp.version.min = XENPMU_VER_MIN;
479 ret = HYPERVISOR_xenpmu_op(XENPMU_feature_set, &xp);
486 static ssize_t pmu_features_show(struct hyp_sysfs_attr *attr, char *buffer)
489 struct xen_pmu_params xp;
491 xp.version.maj = XENPMU_VER_MAJ;
492 xp.version.min = XENPMU_VER_MIN;
493 ret = HYPERVISOR_xenpmu_op(XENPMU_feature_get, &xp);
497 return sprintf(buffer, "0x%x\n", (uint32_t)xp.val);
499 HYPERVISOR_ATTR_RW(pmu_features);
501 static struct attribute *xen_pmu_attrs[] = {
503 &pmu_features_attr.attr,
507 static const struct attribute_group xen_pmu_group = {
509 .attrs = xen_pmu_attrs,
512 static int __init xen_sysfs_pmu_init(void)
514 return sysfs_create_group(hypervisor_kobj, &xen_pmu_group);
518 static int __init hyper_sysfs_init(void)
525 ret = xen_sysfs_type_init();
528 ret = xen_sysfs_guest_type_init();
531 ret = xen_sysfs_version_init();
534 ret = xen_sysfs_compilation_init();
537 ret = xen_sysfs_uuid_init();
540 ret = xen_sysfs_properties_init();
543 #ifdef CONFIG_XEN_HAVE_VPMU
544 if (xen_initial_domain()) {
545 ret = xen_sysfs_pmu_init();
547 sysfs_remove_group(hypervisor_kobj,
548 &xen_properties_group);
556 sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
558 sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
560 sysfs_remove_group(hypervisor_kobj, &version_group);
562 sysfs_remove_file(hypervisor_kobj, &guest_type_attr.attr);
564 sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
568 device_initcall(hyper_sysfs_init);
570 static ssize_t hyp_sysfs_show(struct kobject *kobj,
571 struct attribute *attr,
574 struct hyp_sysfs_attr *hyp_attr;
575 hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
577 return hyp_attr->show(hyp_attr, buffer);
581 static ssize_t hyp_sysfs_store(struct kobject *kobj,
582 struct attribute *attr,
586 struct hyp_sysfs_attr *hyp_attr;
587 hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
589 return hyp_attr->store(hyp_attr, buffer, len);
593 static const struct sysfs_ops hyp_sysfs_ops = {
594 .show = hyp_sysfs_show,
595 .store = hyp_sysfs_store,
598 static struct kobj_type hyp_sysfs_kobj_type = {
599 .sysfs_ops = &hyp_sysfs_ops,
602 static int __init hypervisor_subsys_init(void)
607 hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
610 device_initcall(hypervisor_subsys_init);