kdebugfs.c 3.6 KB

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