hypfs_vm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hypervisor filesystem for Linux on s390. z/VM implementation.
  4. *
  5. * Copyright IBM Corp. 2006
  6. * Author(s): Michael Holzheu <[email protected]>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/string.h>
  11. #include <linux/vmalloc.h>
  12. #include <asm/extable.h>
  13. #include <asm/diag.h>
  14. #include <asm/ebcdic.h>
  15. #include <asm/timex.h>
  16. #include "hypfs.h"
  17. #define NAME_LEN 8
  18. #define DBFS_D2FC_HDR_VERSION 0
  19. static char local_guest[] = " ";
  20. static char all_guests[] = "* ";
  21. static char *all_groups = all_guests;
  22. static char *guest_query;
  23. struct diag2fc_data {
  24. __u32 version;
  25. __u32 flags;
  26. __u64 used_cpu;
  27. __u64 el_time;
  28. __u64 mem_min_kb;
  29. __u64 mem_max_kb;
  30. __u64 mem_share_kb;
  31. __u64 mem_used_kb;
  32. __u32 pcpus;
  33. __u32 lcpus;
  34. __u32 vcpus;
  35. __u32 ocpus;
  36. __u32 cpu_max;
  37. __u32 cpu_shares;
  38. __u32 cpu_use_samp;
  39. __u32 cpu_delay_samp;
  40. __u32 page_wait_samp;
  41. __u32 idle_samp;
  42. __u32 other_samp;
  43. __u32 total_samp;
  44. char guest_name[NAME_LEN];
  45. };
  46. struct diag2fc_parm_list {
  47. char userid[NAME_LEN];
  48. char aci_grp[NAME_LEN];
  49. __u64 addr;
  50. __u32 size;
  51. __u32 fmt;
  52. };
  53. static int diag2fc(int size, char* query, void *addr)
  54. {
  55. unsigned long residual_cnt;
  56. unsigned long rc;
  57. struct diag2fc_parm_list parm_list;
  58. memcpy(parm_list.userid, query, NAME_LEN);
  59. ASCEBC(parm_list.userid, NAME_LEN);
  60. memcpy(parm_list.aci_grp, all_groups, NAME_LEN);
  61. ASCEBC(parm_list.aci_grp, NAME_LEN);
  62. parm_list.addr = (unsigned long)addr;
  63. parm_list.size = size;
  64. parm_list.fmt = 0x02;
  65. rc = -1;
  66. diag_stat_inc(DIAG_STAT_X2FC);
  67. asm volatile(
  68. " diag %0,%1,0x2fc\n"
  69. "0: nopr %%r7\n"
  70. EX_TABLE(0b,0b)
  71. : "=d" (residual_cnt), "+d" (rc) : "0" (&parm_list) : "memory");
  72. if ((rc != 0 ) && (rc != -2))
  73. return rc;
  74. else
  75. return -residual_cnt;
  76. }
  77. /*
  78. * Allocate buffer for "query" and store diag 2fc at "offset"
  79. */
  80. static void *diag2fc_store(char *query, unsigned int *count, int offset)
  81. {
  82. void *data;
  83. int size;
  84. do {
  85. size = diag2fc(0, query, NULL);
  86. if (size < 0)
  87. return ERR_PTR(-EACCES);
  88. data = vmalloc(size + offset);
  89. if (!data)
  90. return ERR_PTR(-ENOMEM);
  91. if (diag2fc(size, query, data + offset) == 0)
  92. break;
  93. vfree(data);
  94. } while (1);
  95. *count = (size / sizeof(struct diag2fc_data));
  96. return data;
  97. }
  98. static void diag2fc_free(const void *data)
  99. {
  100. vfree(data);
  101. }
  102. #define ATTRIBUTE(dir, name, member) \
  103. do { \
  104. void *rc; \
  105. rc = hypfs_create_u64(dir, name, member); \
  106. if (IS_ERR(rc)) \
  107. return PTR_ERR(rc); \
  108. } while(0)
  109. static int hypfs_vm_create_guest(struct dentry *systems_dir,
  110. struct diag2fc_data *data)
  111. {
  112. char guest_name[NAME_LEN + 1] = {};
  113. struct dentry *guest_dir, *cpus_dir, *samples_dir, *mem_dir;
  114. int dedicated_flag, capped_value;
  115. capped_value = (data->flags & 0x00000006) >> 1;
  116. dedicated_flag = (data->flags & 0x00000008) >> 3;
  117. /* guest dir */
  118. memcpy(guest_name, data->guest_name, NAME_LEN);
  119. EBCASC(guest_name, NAME_LEN);
  120. strim(guest_name);
  121. guest_dir = hypfs_mkdir(systems_dir, guest_name);
  122. if (IS_ERR(guest_dir))
  123. return PTR_ERR(guest_dir);
  124. ATTRIBUTE(guest_dir, "onlinetime_us", data->el_time);
  125. /* logical cpu information */
  126. cpus_dir = hypfs_mkdir(guest_dir, "cpus");
  127. if (IS_ERR(cpus_dir))
  128. return PTR_ERR(cpus_dir);
  129. ATTRIBUTE(cpus_dir, "cputime_us", data->used_cpu);
  130. ATTRIBUTE(cpus_dir, "capped", capped_value);
  131. ATTRIBUTE(cpus_dir, "dedicated", dedicated_flag);
  132. ATTRIBUTE(cpus_dir, "count", data->vcpus);
  133. /*
  134. * Note: The "weight_min" attribute got the wrong name.
  135. * The value represents the number of non-stopped (operating)
  136. * CPUS.
  137. */
  138. ATTRIBUTE(cpus_dir, "weight_min", data->ocpus);
  139. ATTRIBUTE(cpus_dir, "weight_max", data->cpu_max);
  140. ATTRIBUTE(cpus_dir, "weight_cur", data->cpu_shares);
  141. /* memory information */
  142. mem_dir = hypfs_mkdir(guest_dir, "mem");
  143. if (IS_ERR(mem_dir))
  144. return PTR_ERR(mem_dir);
  145. ATTRIBUTE(mem_dir, "min_KiB", data->mem_min_kb);
  146. ATTRIBUTE(mem_dir, "max_KiB", data->mem_max_kb);
  147. ATTRIBUTE(mem_dir, "used_KiB", data->mem_used_kb);
  148. ATTRIBUTE(mem_dir, "share_KiB", data->mem_share_kb);
  149. /* samples */
  150. samples_dir = hypfs_mkdir(guest_dir, "samples");
  151. if (IS_ERR(samples_dir))
  152. return PTR_ERR(samples_dir);
  153. ATTRIBUTE(samples_dir, "cpu_using", data->cpu_use_samp);
  154. ATTRIBUTE(samples_dir, "cpu_delay", data->cpu_delay_samp);
  155. ATTRIBUTE(samples_dir, "mem_delay", data->page_wait_samp);
  156. ATTRIBUTE(samples_dir, "idle", data->idle_samp);
  157. ATTRIBUTE(samples_dir, "other", data->other_samp);
  158. ATTRIBUTE(samples_dir, "total", data->total_samp);
  159. return 0;
  160. }
  161. int hypfs_vm_create_files(struct dentry *root)
  162. {
  163. struct dentry *dir, *file;
  164. struct diag2fc_data *data;
  165. unsigned int count = 0;
  166. int rc, i;
  167. data = diag2fc_store(guest_query, &count, 0);
  168. if (IS_ERR(data))
  169. return PTR_ERR(data);
  170. /* Hypervisor Info */
  171. dir = hypfs_mkdir(root, "hyp");
  172. if (IS_ERR(dir)) {
  173. rc = PTR_ERR(dir);
  174. goto failed;
  175. }
  176. file = hypfs_create_str(dir, "type", "z/VM Hypervisor");
  177. if (IS_ERR(file)) {
  178. rc = PTR_ERR(file);
  179. goto failed;
  180. }
  181. /* physical cpus */
  182. dir = hypfs_mkdir(root, "cpus");
  183. if (IS_ERR(dir)) {
  184. rc = PTR_ERR(dir);
  185. goto failed;
  186. }
  187. file = hypfs_create_u64(dir, "count", data->lcpus);
  188. if (IS_ERR(file)) {
  189. rc = PTR_ERR(file);
  190. goto failed;
  191. }
  192. /* guests */
  193. dir = hypfs_mkdir(root, "systems");
  194. if (IS_ERR(dir)) {
  195. rc = PTR_ERR(dir);
  196. goto failed;
  197. }
  198. for (i = 0; i < count; i++) {
  199. rc = hypfs_vm_create_guest(dir, &(data[i]));
  200. if (rc)
  201. goto failed;
  202. }
  203. diag2fc_free(data);
  204. return 0;
  205. failed:
  206. diag2fc_free(data);
  207. return rc;
  208. }
  209. struct dbfs_d2fc_hdr {
  210. u64 len; /* Length of d2fc buffer without header */
  211. u16 version; /* Version of header */
  212. union tod_clock tod_ext; /* TOD clock for d2fc */
  213. u64 count; /* Number of VM guests in d2fc buffer */
  214. char reserved[30];
  215. } __attribute__ ((packed));
  216. struct dbfs_d2fc {
  217. struct dbfs_d2fc_hdr hdr; /* 64 byte header */
  218. char buf[]; /* d2fc buffer */
  219. } __attribute__ ((packed));
  220. static int dbfs_diag2fc_create(void **data, void **data_free_ptr, size_t *size)
  221. {
  222. struct dbfs_d2fc *d2fc;
  223. unsigned int count;
  224. d2fc = diag2fc_store(guest_query, &count, sizeof(d2fc->hdr));
  225. if (IS_ERR(d2fc))
  226. return PTR_ERR(d2fc);
  227. store_tod_clock_ext(&d2fc->hdr.tod_ext);
  228. d2fc->hdr.len = count * sizeof(struct diag2fc_data);
  229. d2fc->hdr.version = DBFS_D2FC_HDR_VERSION;
  230. d2fc->hdr.count = count;
  231. memset(&d2fc->hdr.reserved, 0, sizeof(d2fc->hdr.reserved));
  232. *data = d2fc;
  233. *data_free_ptr = d2fc;
  234. *size = d2fc->hdr.len + sizeof(struct dbfs_d2fc_hdr);
  235. return 0;
  236. }
  237. static struct hypfs_dbfs_file dbfs_file_2fc = {
  238. .name = "diag_2fc",
  239. .data_create = dbfs_diag2fc_create,
  240. .data_free = diag2fc_free,
  241. };
  242. int hypfs_vm_init(void)
  243. {
  244. if (!MACHINE_IS_VM)
  245. return 0;
  246. if (diag2fc(0, all_guests, NULL) > 0)
  247. guest_query = all_guests;
  248. else if (diag2fc(0, local_guest, NULL) > 0)
  249. guest_query = local_guest;
  250. else
  251. return -EACCES;
  252. hypfs_dbfs_create_file(&dbfs_file_2fc);
  253. return 0;
  254. }
  255. void hypfs_vm_exit(void)
  256. {
  257. if (!MACHINE_IS_VM)
  258. return;
  259. hypfs_dbfs_remove_file(&dbfs_file_2fc);
  260. }