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