hypfs_sprp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hypervisor filesystem for Linux on s390.
  4. * Set Partition-Resource Parameter interface.
  5. *
  6. * Copyright IBM Corp. 2013
  7. * Author(s): Martin Schwidefsky <[email protected]>
  8. */
  9. #include <linux/compat.h>
  10. #include <linux/errno.h>
  11. #include <linux/gfp.h>
  12. #include <linux/string.h>
  13. #include <linux/types.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/diag.h>
  16. #include <asm/sclp.h>
  17. #include "hypfs.h"
  18. #define DIAG304_SET_WEIGHTS 0
  19. #define DIAG304_QUERY_PRP 1
  20. #define DIAG304_SET_CAPPING 2
  21. #define DIAG304_CMD_MAX 2
  22. static inline unsigned long __hypfs_sprp_diag304(void *data, unsigned long cmd)
  23. {
  24. union register_pair r1 = { .even = (unsigned long)data, };
  25. asm volatile("diag %[r1],%[r3],0x304\n"
  26. : [r1] "+&d" (r1.pair)
  27. : [r3] "d" (cmd)
  28. : "memory");
  29. return r1.odd;
  30. }
  31. static unsigned long hypfs_sprp_diag304(void *data, unsigned long cmd)
  32. {
  33. diag_stat_inc(DIAG_STAT_X304);
  34. return __hypfs_sprp_diag304(data, cmd);
  35. }
  36. static void hypfs_sprp_free(const void *data)
  37. {
  38. free_page((unsigned long) data);
  39. }
  40. static int hypfs_sprp_create(void **data_ptr, void **free_ptr, size_t *size)
  41. {
  42. unsigned long rc;
  43. void *data;
  44. data = (void *) get_zeroed_page(GFP_KERNEL);
  45. if (!data)
  46. return -ENOMEM;
  47. rc = hypfs_sprp_diag304(data, DIAG304_QUERY_PRP);
  48. if (rc != 1) {
  49. *data_ptr = *free_ptr = NULL;
  50. *size = 0;
  51. free_page((unsigned long) data);
  52. return -EIO;
  53. }
  54. *data_ptr = *free_ptr = data;
  55. *size = PAGE_SIZE;
  56. return 0;
  57. }
  58. static int __hypfs_sprp_ioctl(void __user *user_area)
  59. {
  60. struct hypfs_diag304 *diag304;
  61. unsigned long cmd;
  62. void __user *udata;
  63. void *data;
  64. int rc;
  65. rc = -ENOMEM;
  66. data = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  67. diag304 = kzalloc(sizeof(*diag304), GFP_KERNEL);
  68. if (!data || !diag304)
  69. goto out;
  70. rc = -EFAULT;
  71. if (copy_from_user(diag304, user_area, sizeof(*diag304)))
  72. goto out;
  73. rc = -EINVAL;
  74. if ((diag304->args[0] >> 8) != 0 || diag304->args[1] > DIAG304_CMD_MAX)
  75. goto out;
  76. rc = -EFAULT;
  77. udata = (void __user *)(unsigned long) diag304->data;
  78. if (diag304->args[1] == DIAG304_SET_WEIGHTS ||
  79. diag304->args[1] == DIAG304_SET_CAPPING)
  80. if (copy_from_user(data, udata, PAGE_SIZE))
  81. goto out;
  82. cmd = *(unsigned long *) &diag304->args[0];
  83. diag304->rc = hypfs_sprp_diag304(data, cmd);
  84. if (diag304->args[1] == DIAG304_QUERY_PRP)
  85. if (copy_to_user(udata, data, PAGE_SIZE)) {
  86. rc = -EFAULT;
  87. goto out;
  88. }
  89. rc = copy_to_user(user_area, diag304, sizeof(*diag304)) ? -EFAULT : 0;
  90. out:
  91. kfree(diag304);
  92. free_page((unsigned long) data);
  93. return rc;
  94. }
  95. static long hypfs_sprp_ioctl(struct file *file, unsigned int cmd,
  96. unsigned long arg)
  97. {
  98. void __user *argp;
  99. if (!capable(CAP_SYS_ADMIN))
  100. return -EACCES;
  101. if (is_compat_task())
  102. argp = compat_ptr(arg);
  103. else
  104. argp = (void __user *) arg;
  105. switch (cmd) {
  106. case HYPFS_DIAG304:
  107. return __hypfs_sprp_ioctl(argp);
  108. default: /* unknown ioctl number */
  109. return -ENOTTY;
  110. }
  111. return 0;
  112. }
  113. static struct hypfs_dbfs_file hypfs_sprp_file = {
  114. .name = "diag_304",
  115. .data_create = hypfs_sprp_create,
  116. .data_free = hypfs_sprp_free,
  117. .unlocked_ioctl = hypfs_sprp_ioctl,
  118. };
  119. void hypfs_sprp_init(void)
  120. {
  121. if (!sclp.has_sprp)
  122. return;
  123. hypfs_dbfs_create_file(&hypfs_sprp_file);
  124. }
  125. void hypfs_sprp_exit(void)
  126. {
  127. if (!sclp.has_sprp)
  128. return;
  129. hypfs_dbfs_remove_file(&hypfs_sprp_file);
  130. }