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);
36 unsigned long hyp_attr_value
;
40 static ssize_t
type_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
42 return sprintf(buffer
, "xen\n");
45 HYPERVISOR_ATTR_RO(type
);
47 static int __init
xen_sysfs_type_init(void)
49 return sysfs_create_file(hypervisor_kobj
, &type_attr
.attr
);
52 static ssize_t
guest_type_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
56 switch (xen_domain_type
) {
65 type
= xen_pvh_domain() ? "PVH" : "HVM";
71 return sprintf(buffer
, "%s\n", type
);
74 HYPERVISOR_ATTR_RO(guest_type
);
76 static int __init
xen_sysfs_guest_type_init(void)
78 return sysfs_create_file(hypervisor_kobj
, &guest_type_attr
.attr
);
81 /* xen version attributes */
82 static ssize_t
major_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
84 int version
= HYPERVISOR_xen_version(XENVER_version
, NULL
);
86 return sprintf(buffer
, "%d\n", version
>> 16);
90 HYPERVISOR_ATTR_RO(major
);
92 static ssize_t
minor_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
94 int version
= HYPERVISOR_xen_version(XENVER_version
, NULL
);
96 return sprintf(buffer
, "%d\n", version
& 0xff);
100 HYPERVISOR_ATTR_RO(minor
);
102 static ssize_t
extra_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
107 extra
= kmalloc(XEN_EXTRAVERSION_LEN
, GFP_KERNEL
);
109 ret
= HYPERVISOR_xen_version(XENVER_extraversion
, extra
);
111 ret
= sprintf(buffer
, "%s\n", extra
);
118 HYPERVISOR_ATTR_RO(extra
);
120 static struct attribute
*version_attrs
[] = {
127 static const struct attribute_group version_group
= {
129 .attrs
= version_attrs
,
132 static int __init
xen_sysfs_version_init(void)
134 return sysfs_create_group(hypervisor_kobj
, &version_group
);
139 static ssize_t
uuid_show_fallback(struct hyp_sysfs_attr
*attr
, char *buffer
)
143 extern int xenstored_ready
;
145 if (!xenstored_ready
)
148 vm
= xenbus_read(XBT_NIL
, "vm", "", NULL
);
151 val
= xenbus_read(XBT_NIL
, vm
, "uuid", NULL
);
155 ret
= sprintf(buffer
, "%s\n", val
);
160 static ssize_t
uuid_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
162 xen_domain_handle_t uuid
;
164 ret
= HYPERVISOR_xen_version(XENVER_guest_handle
, uuid
);
166 return uuid_show_fallback(attr
, buffer
);
167 ret
= sprintf(buffer
, "%pU\n", uuid
);
171 HYPERVISOR_ATTR_RO(uuid
);
173 static int __init
xen_sysfs_uuid_init(void)
175 return sysfs_create_file(hypervisor_kobj
, &uuid_attr
.attr
);
178 /* xen compilation attributes */
180 static ssize_t
compiler_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
183 struct xen_compile_info
*info
;
185 info
= kmalloc(sizeof(struct xen_compile_info
), GFP_KERNEL
);
187 ret
= HYPERVISOR_xen_version(XENVER_compile_info
, info
);
189 ret
= sprintf(buffer
, "%s\n", info
->compiler
);
196 HYPERVISOR_ATTR_RO(compiler
);
198 static ssize_t
compiled_by_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
201 struct xen_compile_info
*info
;
203 info
= kmalloc(sizeof(struct xen_compile_info
), GFP_KERNEL
);
205 ret
= HYPERVISOR_xen_version(XENVER_compile_info
, info
);
207 ret
= sprintf(buffer
, "%s\n", info
->compile_by
);
214 HYPERVISOR_ATTR_RO(compiled_by
);
216 static ssize_t
compile_date_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
219 struct xen_compile_info
*info
;
221 info
= kmalloc(sizeof(struct xen_compile_info
), GFP_KERNEL
);
223 ret
= HYPERVISOR_xen_version(XENVER_compile_info
, info
);
225 ret
= sprintf(buffer
, "%s\n", info
->compile_date
);
232 HYPERVISOR_ATTR_RO(compile_date
);
234 static struct attribute
*xen_compile_attrs
[] = {
236 &compiled_by_attr
.attr
,
237 &compile_date_attr
.attr
,
241 static const struct attribute_group xen_compilation_group
= {
242 .name
= "compilation",
243 .attrs
= xen_compile_attrs
,
246 static int __init
xen_sysfs_compilation_init(void)
248 return sysfs_create_group(hypervisor_kobj
, &xen_compilation_group
);
251 /* xen properties info */
253 static ssize_t
capabilities_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
258 caps
= kmalloc(XEN_CAPABILITIES_INFO_LEN
, GFP_KERNEL
);
260 ret
= HYPERVISOR_xen_version(XENVER_capabilities
, caps
);
262 ret
= sprintf(buffer
, "%s\n", caps
);
269 HYPERVISOR_ATTR_RO(capabilities
);
271 static ssize_t
changeset_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
276 cset
= kmalloc(XEN_CHANGESET_INFO_LEN
, GFP_KERNEL
);
278 ret
= HYPERVISOR_xen_version(XENVER_changeset
, cset
);
280 ret
= sprintf(buffer
, "%s\n", cset
);
287 HYPERVISOR_ATTR_RO(changeset
);
289 static ssize_t
virtual_start_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
292 struct xen_platform_parameters
*parms
;
294 parms
= kmalloc(sizeof(struct xen_platform_parameters
), GFP_KERNEL
);
296 ret
= HYPERVISOR_xen_version(XENVER_platform_parameters
,
299 ret
= sprintf(buffer
, "%"PRI_xen_ulong
"\n",
307 HYPERVISOR_ATTR_RO(virtual_start
);
309 static ssize_t
pagesize_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
313 ret
= HYPERVISOR_xen_version(XENVER_pagesize
, NULL
);
315 ret
= sprintf(buffer
, "%x\n", ret
);
320 HYPERVISOR_ATTR_RO(pagesize
);
322 static ssize_t
xen_feature_show(int index
, char *buffer
)
325 struct xen_feature_info info
;
327 info
.submap_idx
= index
;
328 ret
= HYPERVISOR_xen_version(XENVER_get_features
, &info
);
330 ret
= sprintf(buffer
, "%08x", info
.submap
);
335 static ssize_t
features_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
341 for (i
= XENFEAT_NR_SUBMAPS
-1; i
>= 0; i
--) {
342 int ret
= xen_feature_show(i
, buffer
+ len
);
351 buffer
[len
++] = '\n';
356 HYPERVISOR_ATTR_RO(features
);
358 static ssize_t
buildid_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
361 struct xen_build_id
*buildid
;
363 ret
= HYPERVISOR_xen_version(XENVER_build_id
, NULL
);
366 ret
= sprintf(buffer
, "<denied>");
370 buildid
= kmalloc(sizeof(*buildid
) + ret
, GFP_KERNEL
);
375 ret
= HYPERVISOR_xen_version(XENVER_build_id
, buildid
);
377 ret
= sprintf(buffer
, "%s", buildid
->buf
);
383 HYPERVISOR_ATTR_RO(buildid
);
385 static struct attribute
*xen_properties_attrs
[] = {
386 &capabilities_attr
.attr
,
387 &changeset_attr
.attr
,
388 &virtual_start_attr
.attr
,
395 static const struct attribute_group xen_properties_group
= {
396 .name
= "properties",
397 .attrs
= xen_properties_attrs
,
400 static int __init
xen_sysfs_properties_init(void)
402 return sysfs_create_group(hypervisor_kobj
, &xen_properties_group
);
405 #define FLAG_UNAME "unknown"
406 #define FLAG_UNAME_FMT FLAG_UNAME "%02u"
407 #define FLAG_UNAME_MAX sizeof(FLAG_UNAME "XX")
408 #define FLAG_COUNT (sizeof(xen_start_flags) * BITS_PER_BYTE)
409 static_assert(sizeof(xen_start_flags
) <=
410 sizeof_field(struct hyp_sysfs_attr
, hyp_attr_value
));
412 static ssize_t
flag_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
416 *p
++ = '0' + ((xen_start_flags
& attr
->hyp_attr_value
) != 0);
421 #define FLAG_NODE(flag, node) \
423 .attr = { .name = #node, .mode = 0444 },\
425 .hyp_attr_value = flag \
429 * Add new, known flags here. No other changes are required, but
430 * note that each known flag wastes one entry in flag_unames[].
431 * The code/complexity machinations to avoid this isn't worth it
432 * for a few entries, but keep it in mind.
434 static struct hyp_sysfs_attr flag_attrs
[FLAG_COUNT
] = {
435 FLAG_NODE(SIF_PRIVILEGED
, privileged
),
436 FLAG_NODE(SIF_INITDOMAIN
, initdomain
)
438 static struct attribute_group xen_flags_group
= {
439 .name
= "start_flags",
440 .attrs
= (struct attribute
*[FLAG_COUNT
+ 1]){}
442 static char flag_unames
[FLAG_COUNT
][FLAG_UNAME_MAX
];
444 static int __init
xen_sysfs_flags_init(void)
446 for (unsigned fnum
= 0; fnum
!= FLAG_COUNT
; fnum
++) {
447 if (likely(flag_attrs
[fnum
].attr
.name
== NULL
)) {
448 sprintf(flag_unames
[fnum
], FLAG_UNAME_FMT
, fnum
);
449 flag_attrs
[fnum
].attr
.name
= flag_unames
[fnum
];
450 flag_attrs
[fnum
].attr
.mode
= 0444;
451 flag_attrs
[fnum
].show
= flag_show
;
452 flag_attrs
[fnum
].hyp_attr_value
= 1 << fnum
;
454 xen_flags_group
.attrs
[fnum
] = &flag_attrs
[fnum
].attr
;
456 return sysfs_create_group(hypervisor_kobj
, &xen_flags_group
);
459 #ifdef CONFIG_XEN_HAVE_VPMU
465 static struct pmu_mode pmu_modes
[] = {
466 {"off", XENPMU_MODE_OFF
},
467 {"self", XENPMU_MODE_SELF
},
468 {"hv", XENPMU_MODE_HV
},
469 {"all", XENPMU_MODE_ALL
}
472 static ssize_t
pmu_mode_store(struct hyp_sysfs_attr
*attr
,
473 const char *buffer
, size_t len
)
476 struct xen_pmu_params xp
;
479 for (i
= 0; i
< ARRAY_SIZE(pmu_modes
); i
++) {
480 if (strncmp(buffer
, pmu_modes
[i
].name
, len
- 1) == 0) {
481 xp
.val
= pmu_modes
[i
].mode
;
486 if (i
== ARRAY_SIZE(pmu_modes
))
489 xp
.version
.maj
= XENPMU_VER_MAJ
;
490 xp
.version
.min
= XENPMU_VER_MIN
;
491 ret
= HYPERVISOR_xenpmu_op(XENPMU_mode_set
, &xp
);
498 static ssize_t
pmu_mode_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
501 struct xen_pmu_params xp
;
505 xp
.version
.maj
= XENPMU_VER_MAJ
;
506 xp
.version
.min
= XENPMU_VER_MIN
;
507 ret
= HYPERVISOR_xenpmu_op(XENPMU_mode_get
, &xp
);
511 mode
= (uint32_t)xp
.val
;
512 for (i
= 0; i
< ARRAY_SIZE(pmu_modes
); i
++) {
513 if (mode
== pmu_modes
[i
].mode
)
514 return sprintf(buffer
, "%s\n", pmu_modes
[i
].name
);
519 HYPERVISOR_ATTR_RW(pmu_mode
);
521 static ssize_t
pmu_features_store(struct hyp_sysfs_attr
*attr
,
522 const char *buffer
, size_t len
)
526 struct xen_pmu_params xp
;
528 ret
= kstrtou32(buffer
, 0, &features
);
533 xp
.version
.maj
= XENPMU_VER_MAJ
;
534 xp
.version
.min
= XENPMU_VER_MIN
;
535 ret
= HYPERVISOR_xenpmu_op(XENPMU_feature_set
, &xp
);
542 static ssize_t
pmu_features_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
545 struct xen_pmu_params xp
;
547 xp
.version
.maj
= XENPMU_VER_MAJ
;
548 xp
.version
.min
= XENPMU_VER_MIN
;
549 ret
= HYPERVISOR_xenpmu_op(XENPMU_feature_get
, &xp
);
553 return sprintf(buffer
, "0x%x\n", (uint32_t)xp
.val
);
555 HYPERVISOR_ATTR_RW(pmu_features
);
557 static struct attribute
*xen_pmu_attrs
[] = {
559 &pmu_features_attr
.attr
,
563 static const struct attribute_group xen_pmu_group
= {
565 .attrs
= xen_pmu_attrs
,
568 static int __init
xen_sysfs_pmu_init(void)
570 return sysfs_create_group(hypervisor_kobj
, &xen_pmu_group
);
574 static int __init
hyper_sysfs_init(void)
581 ret
= xen_sysfs_type_init();
584 ret
= xen_sysfs_guest_type_init();
587 ret
= xen_sysfs_version_init();
590 ret
= xen_sysfs_compilation_init();
593 ret
= xen_sysfs_uuid_init();
596 ret
= xen_sysfs_properties_init();
599 ret
= xen_sysfs_flags_init();
602 #ifdef CONFIG_XEN_HAVE_VPMU
603 if (xen_initial_domain()) {
604 ret
= xen_sysfs_pmu_init();
606 sysfs_remove_group(hypervisor_kobj
, &xen_flags_group
);
614 sysfs_remove_group(hypervisor_kobj
, &xen_properties_group
);
616 sysfs_remove_file(hypervisor_kobj
, &uuid_attr
.attr
);
618 sysfs_remove_group(hypervisor_kobj
, &xen_compilation_group
);
620 sysfs_remove_group(hypervisor_kobj
, &version_group
);
622 sysfs_remove_file(hypervisor_kobj
, &guest_type_attr
.attr
);
624 sysfs_remove_file(hypervisor_kobj
, &type_attr
.attr
);
628 device_initcall(hyper_sysfs_init
);
630 static ssize_t
hyp_sysfs_show(struct kobject
*kobj
,
631 struct attribute
*attr
,
634 struct hyp_sysfs_attr
*hyp_attr
;
635 hyp_attr
= container_of(attr
, struct hyp_sysfs_attr
, attr
);
637 return hyp_attr
->show(hyp_attr
, buffer
);
641 static ssize_t
hyp_sysfs_store(struct kobject
*kobj
,
642 struct attribute
*attr
,
646 struct hyp_sysfs_attr
*hyp_attr
;
647 hyp_attr
= container_of(attr
, struct hyp_sysfs_attr
, attr
);
649 return hyp_attr
->store(hyp_attr
, buffer
, len
);
653 static const struct sysfs_ops hyp_sysfs_ops
= {
654 .show
= hyp_sysfs_show
,
655 .store
= hyp_sysfs_store
,
658 static const struct kobj_type hyp_sysfs_kobj_type
= {
659 .sysfs_ops
= &hyp_sysfs_ops
,
662 static int __init
hypervisor_subsys_init(void)
667 hypervisor_kobj
->ktype
= &hyp_sysfs_kobj_type
;
670 device_initcall(hypervisor_subsys_init
);