hypfs_vm.c 6.7 KB

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