hypfs_vm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <holzheu@de.ibm.com>
  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_vm.h"
  17. #include "hypfs.h"
  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. char *diag2fc_guest_query;
  23. static int diag2fc(int size, char* query, void *addr)
  24. {
  25. unsigned long residual_cnt;
  26. unsigned long rc;
  27. struct diag2fc_parm_list parm_list;
  28. memcpy(parm_list.userid, query, DIAG2FC_NAME_LEN);
  29. ASCEBC(parm_list.userid, DIAG2FC_NAME_LEN);
  30. memcpy(parm_list.aci_grp, all_groups, DIAG2FC_NAME_LEN);
  31. ASCEBC(parm_list.aci_grp, DIAG2FC_NAME_LEN);
  32. parm_list.addr = (unsigned long)addr;
  33. parm_list.size = size;
  34. parm_list.fmt = 0x02;
  35. rc = -1;
  36. diag_stat_inc(DIAG_STAT_X2FC);
  37. asm volatile(
  38. " diag %0,%1,0x2fc\n"
  39. "0: nopr %%r7\n"
  40. EX_TABLE(0b,0b)
  41. : "=d" (residual_cnt), "+d" (rc) : "0" (&parm_list) : "memory");
  42. if ((rc != 0 ) && (rc != -2))
  43. return rc;
  44. else
  45. return -residual_cnt;
  46. }
  47. /*
  48. * Allocate buffer for "query" and store diag 2fc at "offset"
  49. */
  50. void *diag2fc_store(char *query, unsigned int *count, int offset)
  51. {
  52. void *data;
  53. int size;
  54. do {
  55. size = diag2fc(0, query, NULL);
  56. if (size < 0)
  57. return ERR_PTR(-EACCES);
  58. data = vmalloc(size + offset);
  59. if (!data)
  60. return ERR_PTR(-ENOMEM);
  61. if (diag2fc(size, query, data + offset) == 0)
  62. break;
  63. vfree(data);
  64. } while (1);
  65. *count = (size / sizeof(struct diag2fc_data));
  66. return data;
  67. }
  68. void diag2fc_free(const void *data)
  69. {
  70. vfree(data);
  71. }
  72. struct dbfs_d2fc_hdr {
  73. u64 len; /* Length of d2fc buffer without header */
  74. u16 version; /* Version of header */
  75. union tod_clock tod_ext; /* TOD clock for d2fc */
  76. u64 count; /* Number of VM guests in d2fc buffer */
  77. char reserved[30];
  78. } __attribute__ ((packed));
  79. struct dbfs_d2fc {
  80. struct dbfs_d2fc_hdr hdr; /* 64 byte header */
  81. char buf[]; /* d2fc buffer */
  82. } __attribute__ ((packed));
  83. static int dbfs_diag2fc_create(void **data, void **data_free_ptr, size_t *size)
  84. {
  85. struct dbfs_d2fc *d2fc;
  86. unsigned int count;
  87. d2fc = diag2fc_store(diag2fc_guest_query, &count, sizeof(d2fc->hdr));
  88. if (IS_ERR(d2fc))
  89. return PTR_ERR(d2fc);
  90. store_tod_clock_ext(&d2fc->hdr.tod_ext);
  91. d2fc->hdr.len = count * sizeof(struct diag2fc_data);
  92. d2fc->hdr.version = DBFS_D2FC_HDR_VERSION;
  93. d2fc->hdr.count = count;
  94. memset(&d2fc->hdr.reserved, 0, sizeof(d2fc->hdr.reserved));
  95. *data = d2fc;
  96. *data_free_ptr = d2fc;
  97. *size = d2fc->hdr.len + sizeof(struct dbfs_d2fc_hdr);
  98. return 0;
  99. }
  100. static struct hypfs_dbfs_file dbfs_file_2fc = {
  101. .name = "diag_2fc",
  102. .data_create = dbfs_diag2fc_create,
  103. .data_free = diag2fc_free,
  104. };
  105. int hypfs_vm_init(void)
  106. {
  107. if (!MACHINE_IS_VM)
  108. return 0;
  109. if (diag2fc(0, all_guests, NULL) > 0)
  110. diag2fc_guest_query = all_guests;
  111. else if (diag2fc(0, local_guest, NULL) > 0)
  112. diag2fc_guest_query = local_guest;
  113. else
  114. return -EACCES;
  115. hypfs_dbfs_create_file(&dbfs_file_2fc);
  116. return 0;
  117. }
  118. void hypfs_vm_exit(void)
  119. {
  120. if (!MACHINE_IS_VM)
  121. return;
  122. hypfs_dbfs_remove_file(&dbfs_file_2fc);
  123. }