sys-hypervisor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 <linux/err.h>
  14. #include <asm/xen/hypervisor.h>
  15. #include <asm/xen/hypercall.h>
  16. #include <xen/xen.h>
  17. #include <xen/xenbus.h>
  18. #include <xen/interface/xen.h>
  19. #include <xen/interface/version.h>
  20. #ifdef CONFIG_XEN_HAVE_VPMU
  21. #include <xen/interface/xenpmu.h>
  22. #endif
  23. #define HYPERVISOR_ATTR_RO(_name) \
  24. static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name)
  25. #define HYPERVISOR_ATTR_RW(_name) \
  26. static struct hyp_sysfs_attr _name##_attr = \
  27. __ATTR(_name, 0644, _name##_show, _name##_store)
  28. struct hyp_sysfs_attr {
  29. struct attribute attr;
  30. ssize_t (*show)(struct hyp_sysfs_attr *, char *);
  31. ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t);
  32. void *hyp_attr_data;
  33. };
  34. static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer)
  35. {
  36. return sprintf(buffer, "xen\n");
  37. }
  38. HYPERVISOR_ATTR_RO(type);
  39. static int __init xen_sysfs_type_init(void)
  40. {
  41. return sysfs_create_file(hypervisor_kobj, &type_attr.attr);
  42. }
  43. static void xen_sysfs_type_destroy(void)
  44. {
  45. sysfs_remove_file(hypervisor_kobj, &type_attr.attr);
  46. }
  47. /* xen version attributes */
  48. static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer)
  49. {
  50. int version = HYPERVISOR_xen_version(XENVER_version, NULL);
  51. if (version)
  52. return sprintf(buffer, "%d\n", version >> 16);
  53. return -ENODEV;
  54. }
  55. HYPERVISOR_ATTR_RO(major);
  56. static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer)
  57. {
  58. int version = HYPERVISOR_xen_version(XENVER_version, NULL);
  59. if (version)
  60. return sprintf(buffer, "%d\n", version & 0xff);
  61. return -ENODEV;
  62. }
  63. HYPERVISOR_ATTR_RO(minor);
  64. static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer)
  65. {
  66. int ret = -ENOMEM;
  67. char *extra;
  68. extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL);
  69. if (extra) {
  70. ret = HYPERVISOR_xen_version(XENVER_extraversion, extra);
  71. if (!ret)
  72. ret = sprintf(buffer, "%s\n", extra);
  73. kfree(extra);
  74. }
  75. return ret;
  76. }
  77. HYPERVISOR_ATTR_RO(extra);
  78. static struct attribute *version_attrs[] = {
  79. &major_attr.attr,
  80. &minor_attr.attr,
  81. &extra_attr.attr,
  82. NULL
  83. };
  84. static const struct attribute_group version_group = {
  85. .name = "version",
  86. .attrs = version_attrs,
  87. };
  88. static int __init xen_sysfs_version_init(void)
  89. {
  90. return sysfs_create_group(hypervisor_kobj, &version_group);
  91. }
  92. static void xen_sysfs_version_destroy(void)
  93. {
  94. sysfs_remove_group(hypervisor_kobj, &version_group);
  95. }
  96. /* UUID */
  97. static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer)
  98. {
  99. char *vm, *val;
  100. int ret;
  101. extern int xenstored_ready;
  102. if (!xenstored_ready)
  103. return -EBUSY;
  104. vm = xenbus_read(XBT_NIL, "vm", "", NULL);
  105. if (IS_ERR(vm))
  106. return PTR_ERR(vm);
  107. val = xenbus_read(XBT_NIL, vm, "uuid", NULL);
  108. kfree(vm);
  109. if (IS_ERR(val))
  110. return PTR_ERR(val);
  111. ret = sprintf(buffer, "%s\n", val);
  112. kfree(val);
  113. return ret;
  114. }
  115. static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer)
  116. {
  117. xen_domain_handle_t uuid;
  118. int ret;
  119. ret = HYPERVISOR_xen_version(XENVER_guest_handle, uuid);
  120. if (ret)
  121. return uuid_show_fallback(attr, buffer);
  122. ret = sprintf(buffer, "%pU\n", uuid);
  123. return ret;
  124. }
  125. HYPERVISOR_ATTR_RO(uuid);
  126. static int __init xen_sysfs_uuid_init(void)
  127. {
  128. return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr);
  129. }
  130. static void xen_sysfs_uuid_destroy(void)
  131. {
  132. sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr);
  133. }
  134. /* xen compilation attributes */
  135. static ssize_t compiler_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->compiler);
  144. kfree(info);
  145. }
  146. return ret;
  147. }
  148. HYPERVISOR_ATTR_RO(compiler);
  149. static ssize_t compiled_by_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_by);
  158. kfree(info);
  159. }
  160. return ret;
  161. }
  162. HYPERVISOR_ATTR_RO(compiled_by);
  163. static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer)
  164. {
  165. int ret = -ENOMEM;
  166. struct xen_compile_info *info;
  167. info = kmalloc(sizeof(struct xen_compile_info), GFP_KERNEL);
  168. if (info) {
  169. ret = HYPERVISOR_xen_version(XENVER_compile_info, info);
  170. if (!ret)
  171. ret = sprintf(buffer, "%s\n", info->compile_date);
  172. kfree(info);
  173. }
  174. return ret;
  175. }
  176. HYPERVISOR_ATTR_RO(compile_date);
  177. static struct attribute *xen_compile_attrs[] = {
  178. &compiler_attr.attr,
  179. &compiled_by_attr.attr,
  180. &compile_date_attr.attr,
  181. NULL
  182. };
  183. static const struct attribute_group xen_compilation_group = {
  184. .name = "compilation",
  185. .attrs = xen_compile_attrs,
  186. };
  187. static int __init xen_compilation_init(void)
  188. {
  189. return sysfs_create_group(hypervisor_kobj, &xen_compilation_group);
  190. }
  191. static void xen_compilation_destroy(void)
  192. {
  193. sysfs_remove_group(hypervisor_kobj, &xen_compilation_group);
  194. }
  195. /* xen properties info */
  196. static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer)
  197. {
  198. int ret = -ENOMEM;
  199. char *caps;
  200. caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL);
  201. if (caps) {
  202. ret = HYPERVISOR_xen_version(XENVER_capabilities, caps);
  203. if (!ret)
  204. ret = sprintf(buffer, "%s\n", caps);
  205. kfree(caps);
  206. }
  207. return ret;
  208. }
  209. HYPERVISOR_ATTR_RO(capabilities);
  210. static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer)
  211. {
  212. int ret = -ENOMEM;
  213. char *cset;
  214. cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL);
  215. if (cset) {
  216. ret = HYPERVISOR_xen_version(XENVER_changeset, cset);
  217. if (!ret)
  218. ret = sprintf(buffer, "%s\n", cset);
  219. kfree(cset);
  220. }
  221. return ret;
  222. }
  223. HYPERVISOR_ATTR_RO(changeset);
  224. static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer)
  225. {
  226. int ret = -ENOMEM;
  227. struct xen_platform_parameters *parms;
  228. parms = kmalloc(sizeof(struct xen_platform_parameters), GFP_KERNEL);
  229. if (parms) {
  230. ret = HYPERVISOR_xen_version(XENVER_platform_parameters,
  231. parms);
  232. if (!ret)
  233. ret = sprintf(buffer, "%"PRI_xen_ulong"\n",
  234. parms->virt_start);
  235. kfree(parms);
  236. }
  237. return ret;
  238. }
  239. HYPERVISOR_ATTR_RO(virtual_start);
  240. static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer)
  241. {
  242. int ret;
  243. ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL);
  244. if (ret > 0)
  245. ret = sprintf(buffer, "%x\n", ret);
  246. return ret;
  247. }
  248. HYPERVISOR_ATTR_RO(pagesize);
  249. static ssize_t xen_feature_show(int index, char *buffer)
  250. {
  251. ssize_t ret;
  252. struct xen_feature_info info;
  253. info.submap_idx = index;
  254. ret = HYPERVISOR_xen_version(XENVER_get_features, &info);
  255. if (!ret)
  256. ret = sprintf(buffer, "%08x", info.submap);
  257. return ret;
  258. }
  259. static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer)
  260. {
  261. ssize_t len;
  262. int i;
  263. len = 0;
  264. for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) {
  265. int ret = xen_feature_show(i, buffer + len);
  266. if (ret < 0) {
  267. if (len == 0)
  268. len = ret;
  269. break;
  270. }
  271. len += ret;
  272. }
  273. if (len > 0)
  274. buffer[len++] = '\n';
  275. return len;
  276. }
  277. HYPERVISOR_ATTR_RO(features);
  278. static struct attribute *xen_properties_attrs[] = {
  279. &capabilities_attr.attr,
  280. &changeset_attr.attr,
  281. &virtual_start_attr.attr,
  282. &pagesize_attr.attr,
  283. &features_attr.attr,
  284. NULL
  285. };
  286. static const struct attribute_group xen_properties_group = {
  287. .name = "properties",
  288. .attrs = xen_properties_attrs,
  289. };
  290. static int __init xen_properties_init(void)
  291. {
  292. return sysfs_create_group(hypervisor_kobj, &xen_properties_group);
  293. }
  294. static void xen_properties_destroy(void)
  295. {
  296. sysfs_remove_group(hypervisor_kobj, &xen_properties_group);
  297. }
  298. #ifdef CONFIG_XEN_HAVE_VPMU
  299. struct pmu_mode {
  300. const char *name;
  301. uint32_t mode;
  302. };
  303. static struct pmu_mode pmu_modes[] = {
  304. {"off", XENPMU_MODE_OFF},
  305. {"self", XENPMU_MODE_SELF},
  306. {"hv", XENPMU_MODE_HV},
  307. {"all", XENPMU_MODE_ALL}
  308. };
  309. static ssize_t pmu_mode_store(struct hyp_sysfs_attr *attr,
  310. const char *buffer, size_t len)
  311. {
  312. int ret;
  313. struct xen_pmu_params xp;
  314. int i;
  315. for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
  316. if (strncmp(buffer, pmu_modes[i].name, len - 1) == 0) {
  317. xp.val = pmu_modes[i].mode;
  318. break;
  319. }
  320. }
  321. if (i == ARRAY_SIZE(pmu_modes))
  322. return -EINVAL;
  323. xp.version.maj = XENPMU_VER_MAJ;
  324. xp.version.min = XENPMU_VER_MIN;
  325. ret = HYPERVISOR_xenpmu_op(XENPMU_mode_set, &xp);
  326. if (ret)
  327. return ret;
  328. return len;
  329. }
  330. static ssize_t pmu_mode_show(struct hyp_sysfs_attr *attr, char *buffer)
  331. {
  332. int ret;
  333. struct xen_pmu_params xp;
  334. int i;
  335. uint32_t mode;
  336. xp.version.maj = XENPMU_VER_MAJ;
  337. xp.version.min = XENPMU_VER_MIN;
  338. ret = HYPERVISOR_xenpmu_op(XENPMU_mode_get, &xp);
  339. if (ret)
  340. return ret;
  341. mode = (uint32_t)xp.val;
  342. for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) {
  343. if (mode == pmu_modes[i].mode)
  344. return sprintf(buffer, "%s\n", pmu_modes[i].name);
  345. }
  346. return -EINVAL;
  347. }
  348. HYPERVISOR_ATTR_RW(pmu_mode);
  349. static ssize_t pmu_features_store(struct hyp_sysfs_attr *attr,
  350. const char *buffer, size_t len)
  351. {
  352. int ret;
  353. uint32_t features;
  354. struct xen_pmu_params xp;
  355. ret = kstrtou32(buffer, 0, &features);
  356. if (ret)
  357. return ret;
  358. xp.val = features;
  359. xp.version.maj = XENPMU_VER_MAJ;
  360. xp.version.min = XENPMU_VER_MIN;
  361. ret = HYPERVISOR_xenpmu_op(XENPMU_feature_set, &xp);
  362. if (ret)
  363. return ret;
  364. return len;
  365. }
  366. static ssize_t pmu_features_show(struct hyp_sysfs_attr *attr, char *buffer)
  367. {
  368. int ret;
  369. struct xen_pmu_params xp;
  370. xp.version.maj = XENPMU_VER_MAJ;
  371. xp.version.min = XENPMU_VER_MIN;
  372. ret = HYPERVISOR_xenpmu_op(XENPMU_feature_get, &xp);
  373. if (ret)
  374. return ret;
  375. return sprintf(buffer, "0x%x\n", (uint32_t)xp.val);
  376. }
  377. HYPERVISOR_ATTR_RW(pmu_features);
  378. static struct attribute *xen_pmu_attrs[] = {
  379. &pmu_mode_attr.attr,
  380. &pmu_features_attr.attr,
  381. NULL
  382. };
  383. static const struct attribute_group xen_pmu_group = {
  384. .name = "pmu",
  385. .attrs = xen_pmu_attrs,
  386. };
  387. static int __init xen_pmu_init(void)
  388. {
  389. return sysfs_create_group(hypervisor_kobj, &xen_pmu_group);
  390. }
  391. static void xen_pmu_destroy(void)
  392. {
  393. sysfs_remove_group(hypervisor_kobj, &xen_pmu_group);
  394. }
  395. #endif
  396. static int __init hyper_sysfs_init(void)
  397. {
  398. int ret;
  399. if (!xen_domain())
  400. return -ENODEV;
  401. ret = xen_sysfs_type_init();
  402. if (ret)
  403. goto out;
  404. ret = xen_sysfs_version_init();
  405. if (ret)
  406. goto version_out;
  407. ret = xen_compilation_init();
  408. if (ret)
  409. goto comp_out;
  410. ret = xen_sysfs_uuid_init();
  411. if (ret)
  412. goto uuid_out;
  413. ret = xen_properties_init();
  414. if (ret)
  415. goto prop_out;
  416. #ifdef CONFIG_XEN_HAVE_VPMU
  417. if (xen_initial_domain()) {
  418. ret = xen_pmu_init();
  419. if (ret) {
  420. xen_properties_destroy();
  421. goto prop_out;
  422. }
  423. }
  424. #endif
  425. goto out;
  426. prop_out:
  427. xen_sysfs_uuid_destroy();
  428. uuid_out:
  429. xen_compilation_destroy();
  430. comp_out:
  431. xen_sysfs_version_destroy();
  432. version_out:
  433. xen_sysfs_type_destroy();
  434. out:
  435. return ret;
  436. }
  437. static void __exit hyper_sysfs_exit(void)
  438. {
  439. #ifdef CONFIG_XEN_HAVE_VPMU
  440. xen_pmu_destroy();
  441. #endif
  442. xen_properties_destroy();
  443. xen_compilation_destroy();
  444. xen_sysfs_uuid_destroy();
  445. xen_sysfs_version_destroy();
  446. xen_sysfs_type_destroy();
  447. }
  448. module_init(hyper_sysfs_init);
  449. module_exit(hyper_sysfs_exit);
  450. static ssize_t hyp_sysfs_show(struct kobject *kobj,
  451. struct attribute *attr,
  452. char *buffer)
  453. {
  454. struct hyp_sysfs_attr *hyp_attr;
  455. hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
  456. if (hyp_attr->show)
  457. return hyp_attr->show(hyp_attr, buffer);
  458. return 0;
  459. }
  460. static ssize_t hyp_sysfs_store(struct kobject *kobj,
  461. struct attribute *attr,
  462. const char *buffer,
  463. size_t len)
  464. {
  465. struct hyp_sysfs_attr *hyp_attr;
  466. hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr);
  467. if (hyp_attr->store)
  468. return hyp_attr->store(hyp_attr, buffer, len);
  469. return 0;
  470. }
  471. static const struct sysfs_ops hyp_sysfs_ops = {
  472. .show = hyp_sysfs_show,
  473. .store = hyp_sysfs_store,
  474. };
  475. static struct kobj_type hyp_sysfs_kobj_type = {
  476. .sysfs_ops = &hyp_sysfs_ops,
  477. };
  478. static int __init hypervisor_subsys_init(void)
  479. {
  480. if (!xen_domain())
  481. return -ENODEV;
  482. hypervisor_kobj->ktype = &hyp_sysfs_kobj_type;
  483. return 0;
  484. }
  485. device_initcall(hypervisor_subsys_init);