Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 11 | #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 Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 18 | #include <xen/xen.h> |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 19 | #include <xen/xenbus.h> |
| 20 | #include <xen/interface/xen.h> |
| 21 | #include <xen/interface/version.h> |
| 22 | |
| 23 | #define HYPERVISOR_ATTR_RO(_name) \ |
| 24 | static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name) |
| 25 | |
| 26 | #define HYPERVISOR_ATTR_RW(_name) \ |
| 27 | static struct hyp_sysfs_attr _name##_attr = \ |
| 28 | __ATTR(_name, 0644, _name##_show, _name##_store) |
| 29 | |
| 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); |
| 34 | void *hyp_attr_data; |
| 35 | }; |
| 36 | |
| 37 | static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 38 | { |
| 39 | return sprintf(buffer, "xen\n"); |
| 40 | } |
| 41 | |
| 42 | HYPERVISOR_ATTR_RO(type); |
| 43 | |
| 44 | static int __init xen_sysfs_type_init(void) |
| 45 | { |
| 46 | return sysfs_create_file(hypervisor_kobj, &type_attr.attr); |
| 47 | } |
| 48 | |
| 49 | static void xen_sysfs_type_destroy(void) |
| 50 | { |
| 51 | sysfs_remove_file(hypervisor_kobj, &type_attr.attr); |
| 52 | } |
| 53 | |
| 54 | /* xen version attributes */ |
| 55 | static 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 | |
| 63 | HYPERVISOR_ATTR_RO(major); |
| 64 | |
| 65 | static 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 | |
| 73 | HYPERVISOR_ATTR_RO(minor); |
| 74 | |
| 75 | static 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 | |
| 91 | HYPERVISOR_ATTR_RO(extra); |
| 92 | |
| 93 | static struct attribute *version_attrs[] = { |
| 94 | &major_attr.attr, |
| 95 | &minor_attr.attr, |
| 96 | &extra_attr.attr, |
| 97 | NULL |
| 98 | }; |
| 99 | |
Jan Beulich | ead1d01 | 2012-03-14 12:34:19 -0400 | [diff] [blame] | 100 | static const struct attribute_group version_group = { |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 101 | .name = "version", |
| 102 | .attrs = version_attrs, |
| 103 | }; |
| 104 | |
| 105 | static int __init xen_sysfs_version_init(void) |
| 106 | { |
| 107 | return sysfs_create_group(hypervisor_kobj, &version_group); |
| 108 | } |
| 109 | |
| 110 | static void xen_sysfs_version_destroy(void) |
| 111 | { |
| 112 | sysfs_remove_group(hypervisor_kobj, &version_group); |
| 113 | } |
| 114 | |
| 115 | /* UUID */ |
| 116 | |
Daniel De Graaf | 5c13f80 | 2012-08-16 16:40:26 -0400 | [diff] [blame] | 117 | static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer) |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 118 | { |
| 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 Graaf | 5c13f80 | 2012-08-16 16:40:26 -0400 | [diff] [blame] | 138 | static 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 Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 149 | HYPERVISOR_ATTR_RO(uuid); |
| 150 | |
| 151 | static int __init xen_sysfs_uuid_init(void) |
| 152 | { |
| 153 | return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr); |
| 154 | } |
| 155 | |
| 156 | static void xen_sysfs_uuid_destroy(void) |
| 157 | { |
| 158 | sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr); |
| 159 | } |
| 160 | |
| 161 | /* xen compilation attributes */ |
| 162 | |
| 163 | static 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 | |
| 179 | HYPERVISOR_ATTR_RO(compiler); |
| 180 | |
| 181 | static 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 | |
| 197 | HYPERVISOR_ATTR_RO(compiled_by); |
| 198 | |
| 199 | static 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 | |
| 215 | HYPERVISOR_ATTR_RO(compile_date); |
| 216 | |
| 217 | static struct attribute *xen_compile_attrs[] = { |
| 218 | &compiler_attr.attr, |
| 219 | &compiled_by_attr.attr, |
| 220 | &compile_date_attr.attr, |
| 221 | NULL |
| 222 | }; |
| 223 | |
Jan Beulich | ead1d01 | 2012-03-14 12:34:19 -0400 | [diff] [blame] | 224 | static const struct attribute_group xen_compilation_group = { |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 225 | .name = "compilation", |
| 226 | .attrs = xen_compile_attrs, |
| 227 | }; |
| 228 | |
Daniel Kiper | 887cb45 | 2011-05-04 20:19:49 +0200 | [diff] [blame] | 229 | static int __init xen_compilation_init(void) |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 230 | { |
| 231 | return sysfs_create_group(hypervisor_kobj, &xen_compilation_group); |
| 232 | } |
| 233 | |
| 234 | static void xen_compilation_destroy(void) |
| 235 | { |
| 236 | sysfs_remove_group(hypervisor_kobj, &xen_compilation_group); |
| 237 | } |
| 238 | |
| 239 | /* xen properties info */ |
| 240 | |
| 241 | static 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 | |
| 257 | HYPERVISOR_ATTR_RO(capabilities); |
| 258 | |
| 259 | static 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 | |
| 275 | HYPERVISOR_ATTR_RO(changeset); |
| 276 | |
| 277 | static 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 | |
| 294 | HYPERVISOR_ATTR_RO(virtual_start); |
| 295 | |
| 296 | static 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 | |
| 307 | HYPERVISOR_ATTR_RO(pagesize); |
| 308 | |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 309 | static ssize_t xen_feature_show(int index, char *buffer) |
| 310 | { |
Jeremy Fitzhardinge | a649b72 | 2009-03-10 17:17:41 -0700 | [diff] [blame] | 311 | ssize_t ret; |
| 312 | struct xen_feature_info info; |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 313 | |
Jeremy Fitzhardinge | a649b72 | 2009-03-10 17:17:41 -0700 | [diff] [blame] | 314 | info.submap_idx = index; |
| 315 | ret = HYPERVISOR_xen_version(XENVER_get_features, &info); |
| 316 | if (!ret) |
| 317 | ret = sprintf(buffer, "%08x", info.submap); |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
Jeremy Fitzhardinge | a649b72 | 2009-03-10 17:17:41 -0700 | [diff] [blame] | 322 | static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer) |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 323 | { |
Jeremy Fitzhardinge | a649b72 | 2009-03-10 17:17:41 -0700 | [diff] [blame] | 324 | 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 Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Jeremy Fitzhardinge | a649b72 | 2009-03-10 17:17:41 -0700 | [diff] [blame] | 343 | HYPERVISOR_ATTR_RO(features); |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 344 | |
| 345 | static struct attribute *xen_properties_attrs[] = { |
| 346 | &capabilities_attr.attr, |
| 347 | &changeset_attr.attr, |
| 348 | &virtual_start_attr.attr, |
| 349 | &pagesize_attr.attr, |
Jeremy Fitzhardinge | a649b72 | 2009-03-10 17:17:41 -0700 | [diff] [blame] | 350 | &features_attr.attr, |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 351 | NULL |
| 352 | }; |
| 353 | |
Jan Beulich | ead1d01 | 2012-03-14 12:34:19 -0400 | [diff] [blame] | 354 | static const struct attribute_group xen_properties_group = { |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 355 | .name = "properties", |
| 356 | .attrs = xen_properties_attrs, |
| 357 | }; |
| 358 | |
| 359 | static int __init xen_properties_init(void) |
| 360 | { |
| 361 | return sysfs_create_group(hypervisor_kobj, &xen_properties_group); |
| 362 | } |
| 363 | |
| 364 | static void xen_properties_destroy(void) |
| 365 | { |
| 366 | sysfs_remove_group(hypervisor_kobj, &xen_properties_group); |
| 367 | } |
| 368 | |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 369 | static 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 Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 391 | |
| 392 | goto out; |
| 393 | |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 394 | prop_out: |
| 395 | xen_sysfs_uuid_destroy(); |
| 396 | uuid_out: |
| 397 | xen_compilation_destroy(); |
| 398 | comp_out: |
| 399 | xen_sysfs_version_destroy(); |
| 400 | version_out: |
| 401 | xen_sysfs_type_destroy(); |
| 402 | out: |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static void __exit hyper_sysfs_exit(void) |
| 407 | { |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 408 | xen_properties_destroy(); |
| 409 | xen_compilation_destroy(); |
| 410 | xen_sysfs_uuid_destroy(); |
| 411 | xen_sysfs_version_destroy(); |
| 412 | xen_sysfs_type_destroy(); |
| 413 | |
| 414 | } |
| 415 | module_init(hyper_sysfs_init); |
| 416 | module_exit(hyper_sysfs_exit); |
| 417 | |
| 418 | static 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 | |
| 429 | static 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 Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 441 | static const struct sysfs_ops hyp_sysfs_ops = { |
Jeremy Fitzhardinge | cff7e81 | 2009-03-10 14:39:59 -0700 | [diff] [blame] | 442 | .show = hyp_sysfs_show, |
| 443 | .store = hyp_sysfs_store, |
| 444 | }; |
| 445 | |
| 446 | static struct kobj_type hyp_sysfs_kobj_type = { |
| 447 | .sysfs_ops = &hyp_sysfs_ops, |
| 448 | }; |
| 449 | |
| 450 | static 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 | } |
| 458 | device_initcall(hypervisor_subsys_init); |