sys-hypervisor.c 13 KB

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