hypfs_vm.c 6.7 KB

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