sys-hypervisor.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. #include <linux/slab.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/kobject.h>
  13. #include <asm/xen/hypervisor.h>
  14. #include <asm/xen/hypercall.h>
  15. #include <xen/xen.h>
  16. #include <xen/xenbus.h>
  17. #include <xen/interface/xen.h>
  18. #include <xen/interface/version.h>
  19. #define HYPERVISOR_ATTR_RO(_name) \
  20. static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name)
  21. #define HYPERVISOR_ATTR_RW(_name) \
  22. static struct hyp_sysfs_attr _name##_attr = \
  23. __ATTR(_name, 0644, _name##_show, _name##_store)
  24. struct hyp_sysfs_attr {
  25. struct attribute attr;
  26. ssize_t (*show)(struct hyp_sysfs_attr *, char *);
  27. ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t);
  28. void *hyp_attr_data;
  29. };
  30. static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
  31. {
  32. return sprintf(buffer, "xen\n");
  33. }
  34. HYPERVISOR_ATTR_RO(type);
  35. static int __init xen_sysfs_type_init(void)
  36. {
  37. return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
  38. }
  39. static void xen_sysfs_type_destroy(void)
  40. {
  41. sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
  42. }
  43. /* xen version attributes */
  44. static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
  45. {
  46. int version = HYPERVISOR_xen_version(XENVER_version, NULL);
  47. if (version)
  48. return sprintf(buffer, "%d\n", version >> 16);
  49. return -ENODEV;
  50. }
  51. HYPERVISOR_ATTR_RO(major);
  52. static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
  53. {
  54. int version = HYPERVISOR_xen_version(XENVER_version, NULL);
  55. if (version)
  56. return sprintf(buffer, "%d\n", version & 0xff);
  57. return -ENODEV;
  58. }
  59. HYPERVISOR_ATTR_RO(minor);
  60. static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
  61. {
  62. int ret = -ENOMEM;
  63. char *extra;
  64. extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
  65. if (extra) {
  66. ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
  67. if (!ret)
  68. ret = sprintf(buffer, "%s\n", extra);
  69. kfree(extra);
  70. }
  71. return ret;
  72. }
  73. HYPERVISOR_ATTR_RO(extra);
  74. static struct attribute *version_attrs[] = {
  75. &major_attr.attr,
  76. &minor_attr.attr,
  77. &extra_attr.attr,
  78. NULL
  79. };
  80. static const struct attribute_group version_group = {
  81. .name = "version",
  82. .attrs = version_attrs,
  83. };
  84. static int __init xen_sysfs_version_init(void)
  85. {
  86. return sysfs_create_group(hypervisor_kobj, &version_group);
  87. }
  88. static void xen_sysfs_version_destroy(void)
  89. {
  90. sysfs_remove_group(hypervisor_kobj, &version_group);
  91. }
  92. /* UUID */
  93. static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
  94. {
  95. char *vm, *val;
  96. int ret;
  97. extern int xenstored_ready;
  98. if (!xenstored_ready)
  99. return -EBUSY;
  100. vm = xenbus_read(XBT_NIL, "vm", "", NULL);
  101. if (IS_ERR(vm))
  102. return PTR_ERR(vm);
  103. val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
  104. kfree(vm);
  105. if (IS_ERR(val))
  106. return PTR_ERR(val);
  107. ret = sprintf(buffer, "%s\n", val);
  108. kfree(val);
  109. return ret;
  110. }
  111. HYPERVISOR_ATTR_RO(uuid);
  112. static int __init xen_sysfs_uuid_init(void)
  113. {
  114. return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
  115. }
  116. static void xen_sysfs_uuid_destroy(void)
  117. {
  118. sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
  119. }
  120. /* xen compilation attributes */
  121. static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer)
  122. {
  123. int ret = -ENOMEM;
  124. struct xen_compile_info *info;
  125. info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
  126. if (info) {
  127. ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
  128. if (!ret)
  129. ret = sprintf(buffer, "%s\n", info->compiler);
  130. kfree(info);
  131. }
  132. return ret;
  133. }
  134. HYPERVISOR_ATTR_RO(compiler);
  135. static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer)
  136. {
  137. int ret = -ENOMEM;
  138. struct xen_compile_info *info;
  139. info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
  140. if (info) {
  141. ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
  142. if (!ret)
  143. ret = sprintf(buffer, "%s\n", info->compile_by);
  144. kfree(info);
  145. }
  146. return ret;
  147. }
  148. HYPERVISOR_ATTR_RO(compiled_by);
  149. static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
  150. {
  151. int ret = -ENOMEM;
  152. struct xen_compile_info *info;
  153. info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
  154. if (info) {
  155. ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
  156. if (!ret)
  157. ret = sprintf(buffer, "%s\n", info->compile_date);
  158. kfree(info);
  159. }
  160. return ret;
  161. }
  162. HYPERVISOR_ATTR_RO(compile_date);
  163. static struct attribute *xen_compile_attrs[] = {
  164. &compiler_attr.attr,
  165. &compiled_by_attr.attr,
  166. &compile_date_attr.attr,
  167. NULL
  168. };
  169. static const struct attribute_group xen_compilation_group = {
  170. .name = "compilation",
  171. .attrs = xen_compile_attrs,
  172. };
  173. static int __init xen_compilation_init(void)
  174. {
  175. return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
  176. }
  177. static void xen_compilation_destroy(void)
  178. {
  179. sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
  180. }
  181. /* xen properties info */
  182. static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
  183. {
  184. int ret = -ENOMEM;
  185. char *caps;
  186. caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
  187. if (caps) {
  188. ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
  189. if (!ret)
  190. ret = sprintf(buffer, "%s\n", caps);
  191. kfree(caps);
  192. }
  193. return ret;
  194. }
  195. HYPERVISOR_ATTR_RO(capabilities);
  196. static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
  197. {
  198. int ret = -ENOMEM;
  199. char *cset;
  200. cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
  201. if (cset) {
  202. ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
  203. if (!ret)
  204. ret = sprintf(buffer, "%s\n", cset);
  205. kfree(cset);
  206. }
  207. return ret;
  208. }
  209. HYPERVISOR_ATTR_RO(changeset);
  210. static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
  211. {
  212. int ret = -ENOMEM;
  213. struct xen_platform_parameters *parms;
  214. parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
  215. if (parms) {
  216. ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
  217. parms);
  218. if (!ret)
  219. ret = sprintf(buffer, "%lx\n", parms->virt_start);
  220. kfree(parms);
  221. }
  222. return ret;
  223. }
  224. HYPERVISOR_ATTR_RO(virtual_start);
  225. static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
  226. {
  227. int ret;
  228. ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
  229. if (ret > 0)
  230. ret = sprintf(buffer, "%x\n", ret);
  231. return ret;
  232. }
  233. HYPERVISOR_ATTR_RO(pagesize);
  234. static ssize_t xen_feature_show(int index, char *buffer)
  235. {
  236. ssize_t ret;
  237. struct xen_feature_info info;
  238. info.submap_idx = index;
  239. ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
  240. if (!ret)
  241. ret = sprintf(buffer, "%08x", info.submap);
  242. return ret;
  243. }
  244. static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
  245. {
  246. ssize_t len;
  247. int i;
  248. len = 0;
  249. for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
  250. int ret = xen_feature_show(i, buffer + len);
  251. if (ret < 0) {
  252. if (len == 0)
  253. len = ret;
  254. break;
  255. }
  256. len += ret;
  257. }
  258. if (len > 0)
  259. buffer[len++] = '\n';
  260. return len;
  261. }
  262. HYPERVISOR_ATTR_RO(features);
  263. static struct attribute *xen_properties_attrs[] = {
  264. &capabilities_attr.attr,
  265. &changeset_attr.attr,
  266. &virtual_start_attr.attr,
  267. &pagesize_attr.attr,
  268. &features_attr.attr,
  269. NULL
  270. };
  271. static const struct attribute_group xen_properties_group = {
  272. .name = "properties",
  273. .attrs = xen_properties_attrs,
  274. };
  275. static int __init xen_properties_init(void)
  276. {
  277. return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
  278. }
  279. static void xen_properties_destroy(void)
  280. {
  281. sysfs_remove_group(hypervisor_kobj, &xen_properties_group);
  282. }
  283. static int __init hyper_sysfs_init(void)
  284. {
  285. int ret;
  286. if (!xen_domain())
  287. return -ENODEV;
  288. ret = xen_sysfs_type_init();
  289. if (ret)
  290. goto out;
  291. ret = xen_sysfs_version_init();
  292. if (ret)
  293. goto version_out;
  294. ret = xen_compilation_init();
  295. if (ret)
  296. goto comp_out;
  297. ret = xen_sysfs_uuid_init();
  298. if (ret)
  299. goto uuid_out;
  300. ret = xen_properties_init();
  301. if (ret)
  302. goto prop_out;
  303. goto out;
  304. prop_out:
  305. xen_sysfs_uuid_destroy();
  306. uuid_out:
  307. xen_compilation_destroy();
  308. comp_out:
  309. xen_sysfs_version_destroy();
  310. version_out:
  311. xen_sysfs_type_destroy();
  312. out:
  313. return ret;
  314. }
  315. static void __exit hyper_sysfs_exit(void)
  316. {
  317. xen_properties_destroy();
  318. xen_compilation_destroy();
  319. xen_sysfs_uuid_destroy();
  320. xen_sysfs_version_destroy();
  321. xen_sysfs_type_destroy();
  322. }
  323. module_init(hyper_sysfs_init);
  324. module_exit(hyper_sysfs_exit);
  325. static ssize_t hyp_sysfs_show(struct kobject *kobj,
  326. struct attribute *attr,
  327. char *buffer)
  328. {
  329. struct hyp_sysfs_attr *hyp_attr;
  330. hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
  331. if (hyp_attr->show)
  332. return hyp_attr->show(hyp_attr, buffer);
  333. return 0;
  334. }
  335. static ssize_t hyp_sysfs_store(struct kobject *kobj,
  336. struct attribute *attr,
  337. const char *buffer,
  338. size_t len)
  339. {
  340. struct hyp_sysfs_attr *hyp_attr;
  341. hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
  342. if (hyp_attr->store)
  343. return hyp_attr->store(hyp_attr, buffer, len);
  344. return 0;
  345. }
  346. static const struct sysfs_ops hyp_sysfs_ops = {
  347. .show = hyp_sysfs_show,
  348. .store = hyp_sysfs_store,
  349. };
  350. static struct kobj_type hyp_sysfs_kobj_type = {
  351. .sysfs_ops = &hyp_sysfs_ops,
  352. };
  353. static int __init hypervisor_subsys_init(void)
  354. {
  355. if (!xen_domain())
  356. return -ENODEV;
  357. hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
  358. return 0;
  359. }
  360. device_initcall(hypervisor_subsys_init);