kdebugfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Architecture specific debugfs files
  4. *
  5. * Copyright (C) 2007, Intel Corp.
  6. * Huang Ying <[email protected]>
  7. */
  8. #include <linux/debugfs.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/stat.h>
  14. #include <linux/io.h>
  15. #include <linux/mm.h>
  16. #include <asm/setup.h>
  17. struct dentry *arch_debugfs_dir;
  18. EXPORT_SYMBOL(arch_debugfs_dir);
  19. #ifdef CONFIG_DEBUG_BOOT_PARAMS
  20. struct setup_data_node {
  21. u64 paddr;
  22. u32 type;
  23. u32 len;
  24. };
  25. static ssize_t setup_data_read(struct file *file, char __user *user_buf,
  26. size_t count, loff_t *ppos)
  27. {
  28. struct setup_data_node *node = file->private_data;
  29. unsigned long remain;
  30. loff_t pos = *ppos;
  31. void *p;
  32. u64 pa;
  33. if (pos < 0)
  34. return -EINVAL;
  35. if (pos >= node->len)
  36. return 0;
  37. if (count > node->len - pos)
  38. count = node->len - pos;
  39. pa = node->paddr + pos;
  40. /* Is it direct data or invalid indirect one? */
  41. if (!(node->type & SETUP_INDIRECT) || node->type == SETUP_INDIRECT)
  42. pa += sizeof(struct setup_data);
  43. p = memremap(pa, count, MEMREMAP_WB);
  44. if (!p)
  45. return -ENOMEM;
  46. remain = copy_to_user(user_buf, p, count);
  47. memunmap(p);
  48. if (remain)
  49. return -EFAULT;
  50. *ppos = pos + count;
  51. return count;
  52. }
  53. static const struct file_operations fops_setup_data = {
  54. .read = setup_data_read,
  55. .open = simple_open,
  56. .llseek = default_llseek,
  57. };
  58. static void __init
  59. create_setup_data_node(struct dentry *parent, int no,
  60. struct setup_data_node *node)
  61. {
  62. struct dentry *d;
  63. char buf[16];
  64. sprintf(buf, "%d", no);
  65. d = debugfs_create_dir(buf, parent);
  66. debugfs_create_x32("type", S_IRUGO, d, &node->type);
  67. debugfs_create_file("data", S_IRUGO, d, node, &fops_setup_data);
  68. }
  69. static int __init create_setup_data_nodes(struct dentry *parent)
  70. {
  71. struct setup_indirect *indirect;
  72. struct setup_data_node *node;
  73. struct setup_data *data;
  74. u64 pa_data, pa_next;
  75. struct dentry *d;
  76. int error;
  77. u32 len;
  78. int no = 0;
  79. d = debugfs_create_dir("setup_data", parent);
  80. pa_data = boot_params.hdr.setup_data;
  81. while (pa_data) {
  82. node = kmalloc(sizeof(*node), GFP_KERNEL);
  83. if (!node) {
  84. error = -ENOMEM;
  85. goto err_dir;
  86. }
  87. data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
  88. if (!data) {
  89. kfree(node);
  90. error = -ENOMEM;
  91. goto err_dir;
  92. }
  93. pa_next = data->next;
  94. if (data->type == SETUP_INDIRECT) {
  95. len = sizeof(*data) + data->len;
  96. memunmap(data);
  97. data = memremap(pa_data, len, MEMREMAP_WB);
  98. if (!data) {
  99. kfree(node);
  100. error = -ENOMEM;
  101. goto err_dir;
  102. }
  103. indirect = (struct setup_indirect *)data->data;
  104. if (indirect->type != SETUP_INDIRECT) {
  105. node->paddr = indirect->addr;
  106. node->type = indirect->type;
  107. node->len = indirect->len;
  108. } else {
  109. node->paddr = pa_data;
  110. node->type = data->type;
  111. node->len = data->len;
  112. }
  113. } else {
  114. node->paddr = pa_data;
  115. node->type = data->type;
  116. node->len = data->len;
  117. }
  118. create_setup_data_node(d, no, node);
  119. pa_data = pa_next;
  120. memunmap(data);
  121. no++;
  122. }
  123. return 0;
  124. err_dir:
  125. debugfs_remove_recursive(d);
  126. return error;
  127. }
  128. static struct debugfs_blob_wrapper boot_params_blob = {
  129. .data = &boot_params,
  130. .size = sizeof(boot_params),
  131. };
  132. static int __init boot_params_kdebugfs_init(void)
  133. {
  134. struct dentry *dbp;
  135. int error;
  136. dbp = debugfs_create_dir("boot_params", arch_debugfs_dir);
  137. debugfs_create_x16("version", S_IRUGO, dbp, &boot_params.hdr.version);
  138. debugfs_create_blob("data", S_IRUGO, dbp, &boot_params_blob);
  139. error = create_setup_data_nodes(dbp);
  140. if (error)
  141. debugfs_remove_recursive(dbp);
  142. return error;
  143. }
  144. #endif /* CONFIG_DEBUG_BOOT_PARAMS */
  145. static int __init arch_kdebugfs_init(void)
  146. {
  147. int error = 0;
  148. arch_debugfs_dir = debugfs_create_dir("x86", NULL);
  149. #ifdef CONFIG_DEBUG_BOOT_PARAMS
  150. error = boot_params_kdebugfs_init();
  151. #endif
  152. return error;
  153. }
  154. arch_initcall(arch_kdebugfs_init);