2 * copyright (c) 2006 IBM Corporation
3 * Authored by: Mike D. Day <ncmike@us.ibm.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/kobject.h>
15 #include <asm/xen/hypervisor.h>
16 #include <asm/xen/hypercall.h>
19 #include <xen/xenbus.h>
20 #include <xen/interface/xen.h>
21 #include <xen/interface/version.h>
23 #define HYPERVISOR_ATTR_RO(_name) \
24 static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name)
26 #define HYPERVISOR_ATTR_RW(_name) \
27 static struct hyp_sysfs_attr _name##_attr = \
28 __ATTR(_name, 0644, _name##_show, _name##_store)
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 void xen_sysfs_type_destroy(void)
51 sysfs_remove_file(hypervisor_kobj
, &type_attr
.attr
);
54 /* xen version attributes */
55 static ssize_t
major_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
57 int version
= HYPERVISOR_xen_version(XENVER_version
, NULL
);
59 return sprintf(buffer
, "%d\n", version
>> 16);
63 HYPERVISOR_ATTR_RO(major
);
65 static ssize_t
minor_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
67 int version
= HYPERVISOR_xen_version(XENVER_version
, NULL
);
69 return sprintf(buffer
, "%d\n", version
& 0xff);
73 HYPERVISOR_ATTR_RO(minor
);
75 static ssize_t
extra_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
80 extra
= kmalloc(XEN_EXTRAVERSION_LEN
, GFP_KERNEL
);
82 ret
= HYPERVISOR_xen_version(XENVER_extraversion
, extra
);
84 ret
= sprintf(buffer
, "%s\n", extra
);
91 HYPERVISOR_ATTR_RO(extra
);
93 static struct attribute
*version_attrs
[] = {
100 static struct attribute_group version_group
= {
102 .attrs
= version_attrs
,
105 static int __init
xen_sysfs_version_init(void)
107 return sysfs_create_group(hypervisor_kobj
, &version_group
);
110 static void xen_sysfs_version_destroy(void)
112 sysfs_remove_group(hypervisor_kobj
, &version_group
);
117 static ssize_t
uuid_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
121 extern int xenstored_ready
;
123 if (!xenstored_ready
)
126 vm
= xenbus_read(XBT_NIL
, "vm", "", NULL
);
129 val
= xenbus_read(XBT_NIL
, vm
, "uuid", NULL
);
133 ret
= sprintf(buffer
, "%s\n", val
);
138 HYPERVISOR_ATTR_RO(uuid
);
140 static int __init
xen_sysfs_uuid_init(void)
142 return sysfs_create_file(hypervisor_kobj
, &uuid_attr
.attr
);
145 static void xen_sysfs_uuid_destroy(void)
147 sysfs_remove_file(hypervisor_kobj
, &uuid_attr
.attr
);
150 /* xen compilation attributes */
152 static ssize_t
compiler_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
155 struct xen_compile_info
*info
;
157 info
= kmalloc(sizeof(struct xen_compile_info
), GFP_KERNEL
);
159 ret
= HYPERVISOR_xen_version(XENVER_compile_info
, info
);
161 ret
= sprintf(buffer
, "%s\n", info
->compiler
);
168 HYPERVISOR_ATTR_RO(compiler
);
170 static ssize_t
compiled_by_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
173 struct xen_compile_info
*info
;
175 info
= kmalloc(sizeof(struct xen_compile_info
), GFP_KERNEL
);
177 ret
= HYPERVISOR_xen_version(XENVER_compile_info
, info
);
179 ret
= sprintf(buffer
, "%s\n", info
->compile_by
);
186 HYPERVISOR_ATTR_RO(compiled_by
);
188 static ssize_t
compile_date_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
191 struct xen_compile_info
*info
;
193 info
= kmalloc(sizeof(struct xen_compile_info
), GFP_KERNEL
);
195 ret
= HYPERVISOR_xen_version(XENVER_compile_info
, info
);
197 ret
= sprintf(buffer
, "%s\n", info
->compile_date
);
204 HYPERVISOR_ATTR_RO(compile_date
);
206 static struct attribute
*xen_compile_attrs
[] = {
208 &compiled_by_attr
.attr
,
209 &compile_date_attr
.attr
,
213 static struct attribute_group xen_compilation_group
= {
214 .name
= "compilation",
215 .attrs
= xen_compile_attrs
,
218 static int __init
xen_compilation_init(void)
220 return sysfs_create_group(hypervisor_kobj
, &xen_compilation_group
);
223 static void xen_compilation_destroy(void)
225 sysfs_remove_group(hypervisor_kobj
, &xen_compilation_group
);
228 /* xen properties info */
230 static ssize_t
capabilities_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
235 caps
= kmalloc(XEN_CAPABILITIES_INFO_LEN
, GFP_KERNEL
);
237 ret
= HYPERVISOR_xen_version(XENVER_capabilities
, caps
);
239 ret
= sprintf(buffer
, "%s\n", caps
);
246 HYPERVISOR_ATTR_RO(capabilities
);
248 static ssize_t
changeset_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
253 cset
= kmalloc(XEN_CHANGESET_INFO_LEN
, GFP_KERNEL
);
255 ret
= HYPERVISOR_xen_version(XENVER_changeset
, cset
);
257 ret
= sprintf(buffer
, "%s\n", cset
);
264 HYPERVISOR_ATTR_RO(changeset
);
266 static ssize_t
virtual_start_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
269 struct xen_platform_parameters
*parms
;
271 parms
= kmalloc(sizeof(struct xen_platform_parameters
), GFP_KERNEL
);
273 ret
= HYPERVISOR_xen_version(XENVER_platform_parameters
,
276 ret
= sprintf(buffer
, "%lx\n", parms
->virt_start
);
283 HYPERVISOR_ATTR_RO(virtual_start
);
285 static ssize_t
pagesize_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
289 ret
= HYPERVISOR_xen_version(XENVER_pagesize
, NULL
);
291 ret
= sprintf(buffer
, "%x\n", ret
);
296 HYPERVISOR_ATTR_RO(pagesize
);
298 static ssize_t
xen_feature_show(int index
, char *buffer
)
301 struct xen_feature_info info
;
303 info
.submap_idx
= index
;
304 ret
= HYPERVISOR_xen_version(XENVER_get_features
, &info
);
306 ret
= sprintf(buffer
, "%08x", info
.submap
);
311 static ssize_t
features_show(struct hyp_sysfs_attr
*attr
, char *buffer
)
317 for (i
= XENFEAT_NR_SUBMAPS
-1; i
>= 0; i
--) {
318 int ret
= xen_feature_show(i
, buffer
+ len
);
327 buffer
[len
++] = '\n';
332 HYPERVISOR_ATTR_RO(features
);
334 static struct attribute
*xen_properties_attrs
[] = {
335 &capabilities_attr
.attr
,
336 &changeset_attr
.attr
,
337 &virtual_start_attr
.attr
,
343 static struct attribute_group xen_properties_group
= {
344 .name
= "properties",
345 .attrs
= xen_properties_attrs
,
348 static int __init
xen_properties_init(void)
350 return sysfs_create_group(hypervisor_kobj
, &xen_properties_group
);
353 static void xen_properties_destroy(void)
355 sysfs_remove_group(hypervisor_kobj
, &xen_properties_group
);
358 static int __init
hyper_sysfs_init(void)
365 ret
= xen_sysfs_type_init();
368 ret
= xen_sysfs_version_init();
371 ret
= xen_compilation_init();
374 ret
= xen_sysfs_uuid_init();
377 ret
= xen_properties_init();
384 xen_sysfs_uuid_destroy();
386 xen_compilation_destroy();
388 xen_sysfs_version_destroy();
390 xen_sysfs_type_destroy();
395 static void __exit
hyper_sysfs_exit(void)
397 xen_properties_destroy();
398 xen_compilation_destroy();
399 xen_sysfs_uuid_destroy();
400 xen_sysfs_version_destroy();
401 xen_sysfs_type_destroy();
404 module_init(hyper_sysfs_init
);
405 module_exit(hyper_sysfs_exit
);
407 static ssize_t
hyp_sysfs_show(struct kobject
*kobj
,
408 struct attribute
*attr
,
411 struct hyp_sysfs_attr
*hyp_attr
;
412 hyp_attr
= container_of(attr
, struct hyp_sysfs_attr
, attr
);
414 return hyp_attr
->show(hyp_attr
, buffer
);
418 static ssize_t
hyp_sysfs_store(struct kobject
*kobj
,
419 struct attribute
*attr
,
423 struct hyp_sysfs_attr
*hyp_attr
;
424 hyp_attr
= container_of(attr
, struct hyp_sysfs_attr
, attr
);
426 return hyp_attr
->store(hyp_attr
, buffer
, len
);
430 static const struct sysfs_ops hyp_sysfs_ops
= {
431 .show
= hyp_sysfs_show
,
432 .store
= hyp_sysfs_store
,
435 static struct kobj_type hyp_sysfs_kobj_type
= {
436 .sysfs_ops
= &hyp_sysfs_ops
,
439 static int __init
hypervisor_subsys_init(void)
444 hypervisor_kobj
->ktype
= &hyp_sysfs_kobj_type
;
447 device_initcall(hypervisor_subsys_init
);