xref: /openbmc/linux/drivers/xen/sys-hypervisor.c (revision 1ccbf5344c3daef046d2323190cc6807c44f1917)
1cff7e81bSJeremy Fitzhardinge /*
2cff7e81bSJeremy Fitzhardinge  *  copyright (c) 2006 IBM Corporation
3cff7e81bSJeremy Fitzhardinge  *  Authored by: Mike D. Day <ncmike@us.ibm.com>
4cff7e81bSJeremy Fitzhardinge  *
5cff7e81bSJeremy Fitzhardinge  *  This program is free software; you can redistribute it and/or modify
6cff7e81bSJeremy Fitzhardinge  *  it under the terms of the GNU General Public License version 2 as
7cff7e81bSJeremy Fitzhardinge  *  published by the Free Software Foundation.
8cff7e81bSJeremy Fitzhardinge  */
9cff7e81bSJeremy Fitzhardinge 
10cff7e81bSJeremy Fitzhardinge #include <linux/kernel.h>
11cff7e81bSJeremy Fitzhardinge #include <linux/module.h>
12cff7e81bSJeremy Fitzhardinge #include <linux/kobject.h>
13cff7e81bSJeremy Fitzhardinge 
14cff7e81bSJeremy Fitzhardinge #include <asm/xen/hypervisor.h>
15cff7e81bSJeremy Fitzhardinge #include <asm/xen/hypercall.h>
16cff7e81bSJeremy Fitzhardinge 
17*1ccbf534SJeremy Fitzhardinge #include <xen/xen.h>
18cff7e81bSJeremy Fitzhardinge #include <xen/xenbus.h>
19cff7e81bSJeremy Fitzhardinge #include <xen/interface/xen.h>
20cff7e81bSJeremy Fitzhardinge #include <xen/interface/version.h>
21cff7e81bSJeremy Fitzhardinge 
22cff7e81bSJeremy Fitzhardinge #define HYPERVISOR_ATTR_RO(_name) \
23cff7e81bSJeremy Fitzhardinge static struct hyp_sysfs_attr  _name##_attr = __ATTR_RO(_name)
24cff7e81bSJeremy Fitzhardinge 
25cff7e81bSJeremy Fitzhardinge #define HYPERVISOR_ATTR_RW(_name) \
26cff7e81bSJeremy Fitzhardinge static struct hyp_sysfs_attr _name##_attr = \
27cff7e81bSJeremy Fitzhardinge 	__ATTR(_name, 0644, _name##_show, _name##_store)
28cff7e81bSJeremy Fitzhardinge 
29cff7e81bSJeremy Fitzhardinge struct hyp_sysfs_attr {
30cff7e81bSJeremy Fitzhardinge 	struct attribute attr;
31cff7e81bSJeremy Fitzhardinge 	ssize_t (*show)(struct hyp_sysfs_attr *, char *);
32cff7e81bSJeremy Fitzhardinge 	ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t);
33cff7e81bSJeremy Fitzhardinge 	void *hyp_attr_data;
34cff7e81bSJeremy Fitzhardinge };
35cff7e81bSJeremy Fitzhardinge 
36cff7e81bSJeremy Fitzhardinge static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
37cff7e81bSJeremy Fitzhardinge {
38cff7e81bSJeremy Fitzhardinge 	return sprintf(buffer, "xen\n");
39cff7e81bSJeremy Fitzhardinge }
40cff7e81bSJeremy Fitzhardinge 
41cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(type);
42cff7e81bSJeremy Fitzhardinge 
43cff7e81bSJeremy Fitzhardinge static int __init xen_sysfs_type_init(void)
44cff7e81bSJeremy Fitzhardinge {
45cff7e81bSJeremy Fitzhardinge 	return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
46cff7e81bSJeremy Fitzhardinge }
47cff7e81bSJeremy Fitzhardinge 
48cff7e81bSJeremy Fitzhardinge static void xen_sysfs_type_destroy(void)
49cff7e81bSJeremy Fitzhardinge {
50cff7e81bSJeremy Fitzhardinge 	sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
51cff7e81bSJeremy Fitzhardinge }
52cff7e81bSJeremy Fitzhardinge 
53cff7e81bSJeremy Fitzhardinge /* xen version attributes */
54cff7e81bSJeremy Fitzhardinge static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
55cff7e81bSJeremy Fitzhardinge {
56cff7e81bSJeremy Fitzhardinge 	int version = HYPERVISOR_xen_version(XENVER_version, NULL);
57cff7e81bSJeremy Fitzhardinge 	if (version)
58cff7e81bSJeremy Fitzhardinge 		return sprintf(buffer, "%d\n", version >> 16);
59cff7e81bSJeremy Fitzhardinge 	return -ENODEV;
60cff7e81bSJeremy Fitzhardinge }
61cff7e81bSJeremy Fitzhardinge 
62cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(major);
63cff7e81bSJeremy Fitzhardinge 
64cff7e81bSJeremy Fitzhardinge static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
65cff7e81bSJeremy Fitzhardinge {
66cff7e81bSJeremy Fitzhardinge 	int version = HYPERVISOR_xen_version(XENVER_version, NULL);
67cff7e81bSJeremy Fitzhardinge 	if (version)
68cff7e81bSJeremy Fitzhardinge 		return sprintf(buffer, "%d\n", version & 0xff);
69cff7e81bSJeremy Fitzhardinge 	return -ENODEV;
70cff7e81bSJeremy Fitzhardinge }
71cff7e81bSJeremy Fitzhardinge 
72cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(minor);
73cff7e81bSJeremy Fitzhardinge 
74cff7e81bSJeremy Fitzhardinge static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
75cff7e81bSJeremy Fitzhardinge {
76cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
77cff7e81bSJeremy Fitzhardinge 	char *extra;
78cff7e81bSJeremy Fitzhardinge 
79cff7e81bSJeremy Fitzhardinge 	extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
80cff7e81bSJeremy Fitzhardinge 	if (extra) {
81cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
82cff7e81bSJeremy Fitzhardinge 		if (!ret)
83cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", extra);
84cff7e81bSJeremy Fitzhardinge 		kfree(extra);
85cff7e81bSJeremy Fitzhardinge 	}
86cff7e81bSJeremy Fitzhardinge 
87cff7e81bSJeremy Fitzhardinge 	return ret;
88cff7e81bSJeremy Fitzhardinge }
89cff7e81bSJeremy Fitzhardinge 
90cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(extra);
91cff7e81bSJeremy Fitzhardinge 
92cff7e81bSJeremy Fitzhardinge static struct attribute *version_attrs[] = {
93cff7e81bSJeremy Fitzhardinge 	&major_attr.attr,
94cff7e81bSJeremy Fitzhardinge 	&minor_attr.attr,
95cff7e81bSJeremy Fitzhardinge 	&extra_attr.attr,
96cff7e81bSJeremy Fitzhardinge 	NULL
97cff7e81bSJeremy Fitzhardinge };
98cff7e81bSJeremy Fitzhardinge 
99cff7e81bSJeremy Fitzhardinge static struct attribute_group version_group = {
100cff7e81bSJeremy Fitzhardinge 	.name = "version",
101cff7e81bSJeremy Fitzhardinge 	.attrs = version_attrs,
102cff7e81bSJeremy Fitzhardinge };
103cff7e81bSJeremy Fitzhardinge 
104cff7e81bSJeremy Fitzhardinge static int __init xen_sysfs_version_init(void)
105cff7e81bSJeremy Fitzhardinge {
106cff7e81bSJeremy Fitzhardinge 	return sysfs_create_group(hypervisor_kobj, &version_group);
107cff7e81bSJeremy Fitzhardinge }
108cff7e81bSJeremy Fitzhardinge 
109cff7e81bSJeremy Fitzhardinge static void xen_sysfs_version_destroy(void)
110cff7e81bSJeremy Fitzhardinge {
111cff7e81bSJeremy Fitzhardinge 	sysfs_remove_group(hypervisor_kobj, &version_group);
112cff7e81bSJeremy Fitzhardinge }
113cff7e81bSJeremy Fitzhardinge 
114cff7e81bSJeremy Fitzhardinge /* UUID */
115cff7e81bSJeremy Fitzhardinge 
116cff7e81bSJeremy Fitzhardinge static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
117cff7e81bSJeremy Fitzhardinge {
118cff7e81bSJeremy Fitzhardinge 	char *vm, *val;
119cff7e81bSJeremy Fitzhardinge 	int ret;
120cff7e81bSJeremy Fitzhardinge 	extern int xenstored_ready;
121cff7e81bSJeremy Fitzhardinge 
122cff7e81bSJeremy Fitzhardinge 	if (!xenstored_ready)
123cff7e81bSJeremy Fitzhardinge 		return -EBUSY;
124cff7e81bSJeremy Fitzhardinge 
125cff7e81bSJeremy Fitzhardinge 	vm = xenbus_read(XBT_NIL, "vm", "", NULL);
126cff7e81bSJeremy Fitzhardinge 	if (IS_ERR(vm))
127cff7e81bSJeremy Fitzhardinge 		return PTR_ERR(vm);
128cff7e81bSJeremy Fitzhardinge 	val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
129cff7e81bSJeremy Fitzhardinge 	kfree(vm);
130cff7e81bSJeremy Fitzhardinge 	if (IS_ERR(val))
131cff7e81bSJeremy Fitzhardinge 		return PTR_ERR(val);
132cff7e81bSJeremy Fitzhardinge 	ret = sprintf(buffer, "%s\n", val);
133cff7e81bSJeremy Fitzhardinge 	kfree(val);
134cff7e81bSJeremy Fitzhardinge 	return ret;
135cff7e81bSJeremy Fitzhardinge }
136cff7e81bSJeremy Fitzhardinge 
137cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(uuid);
138cff7e81bSJeremy Fitzhardinge 
139cff7e81bSJeremy Fitzhardinge static int __init xen_sysfs_uuid_init(void)
140cff7e81bSJeremy Fitzhardinge {
141cff7e81bSJeremy Fitzhardinge 	return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
142cff7e81bSJeremy Fitzhardinge }
143cff7e81bSJeremy Fitzhardinge 
144cff7e81bSJeremy Fitzhardinge static void xen_sysfs_uuid_destroy(void)
145cff7e81bSJeremy Fitzhardinge {
146cff7e81bSJeremy Fitzhardinge 	sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
147cff7e81bSJeremy Fitzhardinge }
148cff7e81bSJeremy Fitzhardinge 
149cff7e81bSJeremy Fitzhardinge /* xen compilation attributes */
150cff7e81bSJeremy Fitzhardinge 
151cff7e81bSJeremy Fitzhardinge static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer)
152cff7e81bSJeremy Fitzhardinge {
153cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
154cff7e81bSJeremy Fitzhardinge 	struct xen_compile_info *info;
155cff7e81bSJeremy Fitzhardinge 
156cff7e81bSJeremy Fitzhardinge 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
157cff7e81bSJeremy Fitzhardinge 	if (info) {
158cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
159cff7e81bSJeremy Fitzhardinge 		if (!ret)
160cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", info->compiler);
161cff7e81bSJeremy Fitzhardinge 		kfree(info);
162cff7e81bSJeremy Fitzhardinge 	}
163cff7e81bSJeremy Fitzhardinge 
164cff7e81bSJeremy Fitzhardinge 	return ret;
165cff7e81bSJeremy Fitzhardinge }
166cff7e81bSJeremy Fitzhardinge 
167cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(compiler);
168cff7e81bSJeremy Fitzhardinge 
169cff7e81bSJeremy Fitzhardinge static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer)
170cff7e81bSJeremy Fitzhardinge {
171cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
172cff7e81bSJeremy Fitzhardinge 	struct xen_compile_info *info;
173cff7e81bSJeremy Fitzhardinge 
174cff7e81bSJeremy Fitzhardinge 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
175cff7e81bSJeremy Fitzhardinge 	if (info) {
176cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
177cff7e81bSJeremy Fitzhardinge 		if (!ret)
178cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", info->compile_by);
179cff7e81bSJeremy Fitzhardinge 		kfree(info);
180cff7e81bSJeremy Fitzhardinge 	}
181cff7e81bSJeremy Fitzhardinge 
182cff7e81bSJeremy Fitzhardinge 	return ret;
183cff7e81bSJeremy Fitzhardinge }
184cff7e81bSJeremy Fitzhardinge 
185cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(compiled_by);
186cff7e81bSJeremy Fitzhardinge 
187cff7e81bSJeremy Fitzhardinge static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
188cff7e81bSJeremy Fitzhardinge {
189cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
190cff7e81bSJeremy Fitzhardinge 	struct xen_compile_info *info;
191cff7e81bSJeremy Fitzhardinge 
192cff7e81bSJeremy Fitzhardinge 	info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
193cff7e81bSJeremy Fitzhardinge 	if (info) {
194cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
195cff7e81bSJeremy Fitzhardinge 		if (!ret)
196cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", info->compile_date);
197cff7e81bSJeremy Fitzhardinge 		kfree(info);
198cff7e81bSJeremy Fitzhardinge 	}
199cff7e81bSJeremy Fitzhardinge 
200cff7e81bSJeremy Fitzhardinge 	return ret;
201cff7e81bSJeremy Fitzhardinge }
202cff7e81bSJeremy Fitzhardinge 
203cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(compile_date);
204cff7e81bSJeremy Fitzhardinge 
205cff7e81bSJeremy Fitzhardinge static struct attribute *xen_compile_attrs[] = {
206cff7e81bSJeremy Fitzhardinge 	&compiler_attr.attr,
207cff7e81bSJeremy Fitzhardinge 	&compiled_by_attr.attr,
208cff7e81bSJeremy Fitzhardinge 	&compile_date_attr.attr,
209cff7e81bSJeremy Fitzhardinge 	NULL
210cff7e81bSJeremy Fitzhardinge };
211cff7e81bSJeremy Fitzhardinge 
212cff7e81bSJeremy Fitzhardinge static struct attribute_group xen_compilation_group = {
213cff7e81bSJeremy Fitzhardinge 	.name = "compilation",
214cff7e81bSJeremy Fitzhardinge 	.attrs = xen_compile_attrs,
215cff7e81bSJeremy Fitzhardinge };
216cff7e81bSJeremy Fitzhardinge 
217cff7e81bSJeremy Fitzhardinge int __init static xen_compilation_init(void)
218cff7e81bSJeremy Fitzhardinge {
219cff7e81bSJeremy Fitzhardinge 	return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
220cff7e81bSJeremy Fitzhardinge }
221cff7e81bSJeremy Fitzhardinge 
222cff7e81bSJeremy Fitzhardinge static void xen_compilation_destroy(void)
223cff7e81bSJeremy Fitzhardinge {
224cff7e81bSJeremy Fitzhardinge 	sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
225cff7e81bSJeremy Fitzhardinge }
226cff7e81bSJeremy Fitzhardinge 
227cff7e81bSJeremy Fitzhardinge /* xen properties info */
228cff7e81bSJeremy Fitzhardinge 
229cff7e81bSJeremy Fitzhardinge static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
230cff7e81bSJeremy Fitzhardinge {
231cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
232cff7e81bSJeremy Fitzhardinge 	char *caps;
233cff7e81bSJeremy Fitzhardinge 
234cff7e81bSJeremy Fitzhardinge 	caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
235cff7e81bSJeremy Fitzhardinge 	if (caps) {
236cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
237cff7e81bSJeremy Fitzhardinge 		if (!ret)
238cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", caps);
239cff7e81bSJeremy Fitzhardinge 		kfree(caps);
240cff7e81bSJeremy Fitzhardinge 	}
241cff7e81bSJeremy Fitzhardinge 
242cff7e81bSJeremy Fitzhardinge 	return ret;
243cff7e81bSJeremy Fitzhardinge }
244cff7e81bSJeremy Fitzhardinge 
245cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(capabilities);
246cff7e81bSJeremy Fitzhardinge 
247cff7e81bSJeremy Fitzhardinge static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
248cff7e81bSJeremy Fitzhardinge {
249cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
250cff7e81bSJeremy Fitzhardinge 	char *cset;
251cff7e81bSJeremy Fitzhardinge 
252cff7e81bSJeremy Fitzhardinge 	cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
253cff7e81bSJeremy Fitzhardinge 	if (cset) {
254cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
255cff7e81bSJeremy Fitzhardinge 		if (!ret)
256cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%s\n", cset);
257cff7e81bSJeremy Fitzhardinge 		kfree(cset);
258cff7e81bSJeremy Fitzhardinge 	}
259cff7e81bSJeremy Fitzhardinge 
260cff7e81bSJeremy Fitzhardinge 	return ret;
261cff7e81bSJeremy Fitzhardinge }
262cff7e81bSJeremy Fitzhardinge 
263cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(changeset);
264cff7e81bSJeremy Fitzhardinge 
265cff7e81bSJeremy Fitzhardinge static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
266cff7e81bSJeremy Fitzhardinge {
267cff7e81bSJeremy Fitzhardinge 	int ret = -ENOMEM;
268cff7e81bSJeremy Fitzhardinge 	struct xen_platform_parameters *parms;
269cff7e81bSJeremy Fitzhardinge 
270cff7e81bSJeremy Fitzhardinge 	parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
271cff7e81bSJeremy Fitzhardinge 	if (parms) {
272cff7e81bSJeremy Fitzhardinge 		ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
273cff7e81bSJeremy Fitzhardinge 					     parms);
274cff7e81bSJeremy Fitzhardinge 		if (!ret)
275cff7e81bSJeremy Fitzhardinge 			ret = sprintf(buffer, "%lx\n", parms->virt_start);
276cff7e81bSJeremy Fitzhardinge 		kfree(parms);
277cff7e81bSJeremy Fitzhardinge 	}
278cff7e81bSJeremy Fitzhardinge 
279cff7e81bSJeremy Fitzhardinge 	return ret;
280cff7e81bSJeremy Fitzhardinge }
281cff7e81bSJeremy Fitzhardinge 
282cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(virtual_start);
283cff7e81bSJeremy Fitzhardinge 
284cff7e81bSJeremy Fitzhardinge static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
285cff7e81bSJeremy Fitzhardinge {
286cff7e81bSJeremy Fitzhardinge 	int ret;
287cff7e81bSJeremy Fitzhardinge 
288cff7e81bSJeremy Fitzhardinge 	ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
289cff7e81bSJeremy Fitzhardinge 	if (ret > 0)
290cff7e81bSJeremy Fitzhardinge 		ret = sprintf(buffer, "%x\n", ret);
291cff7e81bSJeremy Fitzhardinge 
292cff7e81bSJeremy Fitzhardinge 	return ret;
293cff7e81bSJeremy Fitzhardinge }
294cff7e81bSJeremy Fitzhardinge 
295cff7e81bSJeremy Fitzhardinge HYPERVISOR_ATTR_RO(pagesize);
296cff7e81bSJeremy Fitzhardinge 
297cff7e81bSJeremy Fitzhardinge static ssize_t xen_feature_show(int index, char *buffer)
298cff7e81bSJeremy Fitzhardinge {
299a649b720SJeremy Fitzhardinge 	ssize_t ret;
300a649b720SJeremy Fitzhardinge 	struct xen_feature_info info;
301cff7e81bSJeremy Fitzhardinge 
302a649b720SJeremy Fitzhardinge 	info.submap_idx = index;
303a649b720SJeremy Fitzhardinge 	ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
304cff7e81bSJeremy Fitzhardinge 	if (!ret)
305a649b720SJeremy Fitzhardinge 		ret = sprintf(buffer, "%08x", info.submap);
306cff7e81bSJeremy Fitzhardinge 
307cff7e81bSJeremy Fitzhardinge 	return ret;
308cff7e81bSJeremy Fitzhardinge }
309cff7e81bSJeremy Fitzhardinge 
310a649b720SJeremy Fitzhardinge static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
311cff7e81bSJeremy Fitzhardinge {
312a649b720SJeremy Fitzhardinge 	ssize_t len;
313a649b720SJeremy Fitzhardinge 	int i;
314a649b720SJeremy Fitzhardinge 
315a649b720SJeremy Fitzhardinge 	len = 0;
316a649b720SJeremy Fitzhardinge 	for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
317a649b720SJeremy Fitzhardinge 		int ret = xen_feature_show(i, buffer + len);
318a649b720SJeremy Fitzhardinge 		if (ret < 0) {
319a649b720SJeremy Fitzhardinge 			if (len == 0)
320a649b720SJeremy Fitzhardinge 				len = ret;
321a649b720SJeremy Fitzhardinge 			break;
322a649b720SJeremy Fitzhardinge 		}
323a649b720SJeremy Fitzhardinge 		len += ret;
324a649b720SJeremy Fitzhardinge 	}
325a649b720SJeremy Fitzhardinge 	if (len > 0)
326a649b720SJeremy Fitzhardinge 		buffer[len++] = '\n';
327a649b720SJeremy Fitzhardinge 
328a649b720SJeremy Fitzhardinge 	return len;
329cff7e81bSJeremy Fitzhardinge }
330cff7e81bSJeremy Fitzhardinge 
331a649b720SJeremy Fitzhardinge HYPERVISOR_ATTR_RO(features);
332cff7e81bSJeremy Fitzhardinge 
333cff7e81bSJeremy Fitzhardinge static struct attribute *xen_properties_attrs[] = {
334cff7e81bSJeremy Fitzhardinge 	&capabilities_attr.attr,
335cff7e81bSJeremy Fitzhardinge 	&changeset_attr.attr,
336cff7e81bSJeremy Fitzhardinge 	&virtual_start_attr.attr,
337cff7e81bSJeremy Fitzhardinge 	&pagesize_attr.attr,
338a649b720SJeremy Fitzhardinge 	&features_attr.attr,
339cff7e81bSJeremy Fitzhardinge 	NULL
340cff7e81bSJeremy Fitzhardinge };
341cff7e81bSJeremy Fitzhardinge 
342cff7e81bSJeremy Fitzhardinge static struct attribute_group xen_properties_group = {
343cff7e81bSJeremy Fitzhardinge 	.name = "properties",
344cff7e81bSJeremy Fitzhardinge 	.attrs = xen_properties_attrs,
345cff7e81bSJeremy Fitzhardinge };
346cff7e81bSJeremy Fitzhardinge 
347cff7e81bSJeremy Fitzhardinge static int __init xen_properties_init(void)
348cff7e81bSJeremy Fitzhardinge {
349cff7e81bSJeremy Fitzhardinge 	return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
350cff7e81bSJeremy Fitzhardinge }
351cff7e81bSJeremy Fitzhardinge 
352cff7e81bSJeremy Fitzhardinge static void xen_properties_destroy(void)
353cff7e81bSJeremy Fitzhardinge {
354cff7e81bSJeremy Fitzhardinge 	sysfs_remove_group(hypervisor_kobj, &xen_properties_group);
355cff7e81bSJeremy Fitzhardinge }
356cff7e81bSJeremy Fitzhardinge 
357cff7e81bSJeremy Fitzhardinge static int __init hyper_sysfs_init(void)
358cff7e81bSJeremy Fitzhardinge {
359cff7e81bSJeremy Fitzhardinge 	int ret;
360cff7e81bSJeremy Fitzhardinge 
361cff7e81bSJeremy Fitzhardinge 	if (!xen_domain())
362cff7e81bSJeremy Fitzhardinge 		return -ENODEV;
363cff7e81bSJeremy Fitzhardinge 
364cff7e81bSJeremy Fitzhardinge 	ret = xen_sysfs_type_init();
365cff7e81bSJeremy Fitzhardinge 	if (ret)
366cff7e81bSJeremy Fitzhardinge 		goto out;
367cff7e81bSJeremy Fitzhardinge 	ret = xen_sysfs_version_init();
368cff7e81bSJeremy Fitzhardinge 	if (ret)
369cff7e81bSJeremy Fitzhardinge 		goto version_out;
370cff7e81bSJeremy Fitzhardinge 	ret = xen_compilation_init();
371cff7e81bSJeremy Fitzhardinge 	if (ret)
372cff7e81bSJeremy Fitzhardinge 		goto comp_out;
373cff7e81bSJeremy Fitzhardinge 	ret = xen_sysfs_uuid_init();
374cff7e81bSJeremy Fitzhardinge 	if (ret)
375cff7e81bSJeremy Fitzhardinge 		goto uuid_out;
376cff7e81bSJeremy Fitzhardinge 	ret = xen_properties_init();
377cff7e81bSJeremy Fitzhardinge 	if (ret)
378cff7e81bSJeremy Fitzhardinge 		goto prop_out;
379cff7e81bSJeremy Fitzhardinge 
380cff7e81bSJeremy Fitzhardinge 	goto out;
381cff7e81bSJeremy Fitzhardinge 
382cff7e81bSJeremy Fitzhardinge prop_out:
383cff7e81bSJeremy Fitzhardinge 	xen_sysfs_uuid_destroy();
384cff7e81bSJeremy Fitzhardinge uuid_out:
385cff7e81bSJeremy Fitzhardinge 	xen_compilation_destroy();
386cff7e81bSJeremy Fitzhardinge comp_out:
387cff7e81bSJeremy Fitzhardinge 	xen_sysfs_version_destroy();
388cff7e81bSJeremy Fitzhardinge version_out:
389cff7e81bSJeremy Fitzhardinge 	xen_sysfs_type_destroy();
390cff7e81bSJeremy Fitzhardinge out:
391cff7e81bSJeremy Fitzhardinge 	return ret;
392cff7e81bSJeremy Fitzhardinge }
393cff7e81bSJeremy Fitzhardinge 
394cff7e81bSJeremy Fitzhardinge static void __exit hyper_sysfs_exit(void)
395cff7e81bSJeremy Fitzhardinge {
396cff7e81bSJeremy Fitzhardinge 	xen_properties_destroy();
397cff7e81bSJeremy Fitzhardinge 	xen_compilation_destroy();
398cff7e81bSJeremy Fitzhardinge 	xen_sysfs_uuid_destroy();
399cff7e81bSJeremy Fitzhardinge 	xen_sysfs_version_destroy();
400cff7e81bSJeremy Fitzhardinge 	xen_sysfs_type_destroy();
401cff7e81bSJeremy Fitzhardinge 
402cff7e81bSJeremy Fitzhardinge }
403cff7e81bSJeremy Fitzhardinge module_init(hyper_sysfs_init);
404cff7e81bSJeremy Fitzhardinge module_exit(hyper_sysfs_exit);
405cff7e81bSJeremy Fitzhardinge 
406cff7e81bSJeremy Fitzhardinge static ssize_t hyp_sysfs_show(struct kobject *kobj,
407cff7e81bSJeremy Fitzhardinge 			      struct attribute *attr,
408cff7e81bSJeremy Fitzhardinge 			      char *buffer)
409cff7e81bSJeremy Fitzhardinge {
410cff7e81bSJeremy Fitzhardinge 	struct hyp_sysfs_attr *hyp_attr;
411cff7e81bSJeremy Fitzhardinge 	hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
412cff7e81bSJeremy Fitzhardinge 	if (hyp_attr->show)
413cff7e81bSJeremy Fitzhardinge 		return hyp_attr->show(hyp_attr, buffer);
414cff7e81bSJeremy Fitzhardinge 	return 0;
415cff7e81bSJeremy Fitzhardinge }
416cff7e81bSJeremy Fitzhardinge 
417cff7e81bSJeremy Fitzhardinge static ssize_t hyp_sysfs_store(struct kobject *kobj,
418cff7e81bSJeremy Fitzhardinge 			       struct attribute *attr,
419cff7e81bSJeremy Fitzhardinge 			       const char *buffer,
420cff7e81bSJeremy Fitzhardinge 			       size_t len)
421cff7e81bSJeremy Fitzhardinge {
422cff7e81bSJeremy Fitzhardinge 	struct hyp_sysfs_attr *hyp_attr;
423cff7e81bSJeremy Fitzhardinge 	hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
424cff7e81bSJeremy Fitzhardinge 	if (hyp_attr->store)
425cff7e81bSJeremy Fitzhardinge 		return hyp_attr->store(hyp_attr, buffer, len);
426cff7e81bSJeremy Fitzhardinge 	return 0;
427cff7e81bSJeremy Fitzhardinge }
428cff7e81bSJeremy Fitzhardinge 
429cff7e81bSJeremy Fitzhardinge static struct sysfs_ops hyp_sysfs_ops = {
430cff7e81bSJeremy Fitzhardinge 	.show = hyp_sysfs_show,
431cff7e81bSJeremy Fitzhardinge 	.store = hyp_sysfs_store,
432cff7e81bSJeremy Fitzhardinge };
433cff7e81bSJeremy Fitzhardinge 
434cff7e81bSJeremy Fitzhardinge static struct kobj_type hyp_sysfs_kobj_type = {
435cff7e81bSJeremy Fitzhardinge 	.sysfs_ops = &hyp_sysfs_ops,
436cff7e81bSJeremy Fitzhardinge };
437cff7e81bSJeremy Fitzhardinge 
438cff7e81bSJeremy Fitzhardinge static int __init hypervisor_subsys_init(void)
439cff7e81bSJeremy Fitzhardinge {
440cff7e81bSJeremy Fitzhardinge 	if (!xen_domain())
441cff7e81bSJeremy Fitzhardinge 		return -ENODEV;
442cff7e81bSJeremy Fitzhardinge 
443cff7e81bSJeremy Fitzhardinge 	hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
444cff7e81bSJeremy Fitzhardinge 	return 0;
445cff7e81bSJeremy Fitzhardinge }
446cff7e81bSJeremy Fitzhardinge device_initcall(hypervisor_subsys_init);
447