hypfs_vm.c 3.3 KB

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