kdebugfs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/module.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. struct page *pg;
  33. void *p;
  34. u64 pa;
  35. if (pos < 0)
  36. return -EINVAL;
  37. if (pos >= node->len)
  38. return 0;
  39. if (count > node->len - pos)
  40. count = node->len - pos;
  41. pa = node->paddr + sizeof(struct setup_data) + pos;
  42. pg = pfn_to_page((pa + count - 1) >> PAGE_SHIFT);
  43. if (PageHighMem(pg)) {
  44. p = ioremap_cache(pa, count);
  45. if (!p)
  46. return -ENXIO;
  47. } else
  48. p = __va(pa);
  49. remain = copy_to_user(user_buf, p, count);
  50. if (PageHighMem(pg))
  51. iounmap(p);
  52. if (remain)
  53. return -EFAULT;
  54. *ppos = pos + count;
  55. return count;
  56. }
  57. static int setup_data_open(struct inode *inode, struct file *file)
  58. {
  59. file->private_data = inode->i_private;
  60. return 0;
  61. }
  62. static const struct file_operations fops_setup_data = {
  63. .read = setup_data_read,
  64. .open = setup_data_open,
  65. .llseek = default_llseek,
  66. };
  67. static int __init
  68. create_setup_data_node(struct dentry *parent, int no,
  69. struct setup_data_node *node)
  70. {
  71. struct dentry *d, *type, *data;
  72. char buf[16];
  73. sprintf(buf, "%d", no);
  74. d = debugfs_create_dir(buf, parent);
  75. if (!d)
  76. return -ENOMEM;
  77. type = debugfs_create_x32("type", S_IRUGO, d, &node->type);
  78. if (!type)
  79. goto err_dir;
  80. data = debugfs_create_file("data", S_IRUGO, d, node, &fops_setup_data);
  81. if (!data)
  82. goto err_type;
  83. return 0;
  84. err_type:
  85. debugfs_remove(type);
  86. err_dir:
  87. debugfs_remove(d);
  88. return -ENOMEM;
  89. }
  90. static int __init create_setup_data_nodes(struct dentry *parent)
  91. {
  92. struct setup_data_node *node;
  93. struct setup_data *data;
  94. int error = -ENOMEM;
  95. struct dentry *d;
  96. struct page *pg;
  97. u64 pa_data;
  98. int no = 0;
  99. d = debugfs_create_dir("setup_data", parent);
  100. if (!d)
  101. return -ENOMEM;
  102. pa_data = boot_params.hdr.setup_data;
  103. while (pa_data) {
  104. node = kmalloc(sizeof(*node), GFP_KERNEL);
  105. if (!node)
  106. goto err_dir;
  107. pg = pfn_to_page((pa_data+sizeof(*data)-1) >> PAGE_SHIFT);
  108. if (PageHighMem(pg)) {
  109. data = ioremap_cache(pa_data, sizeof(*data));
  110. if (!data) {
  111. kfree(node);
  112. error = -ENXIO;
  113. goto err_dir;
  114. }
  115. } else
  116. data = __va(pa_data);
  117. node->paddr = pa_data;
  118. node->type = data->type;
  119. node->len = data->len;
  120. error = create_setup_data_node(d, no, node);
  121. pa_data = data->next;
  122. if (PageHighMem(pg))
  123. iounmap(data);
  124. if (error)
  125. goto err_dir;
  126. no++;
  127. }
  128. return 0;
  129. err_dir:
  130. debugfs_remove(d);
  131. return error;
  132. }
  133. static struct debugfs_blob_wrapper boot_params_blob = {
  134. .data = &boot_params,
  135. .size = sizeof(boot_params),
  136. };
  137. static int __init boot_params_kdebugfs_init(void)
  138. {
  139. struct dentry *dbp, *version, *data;
  140. int error = -ENOMEM;
  141. dbp = debugfs_create_dir("boot_params", NULL);
  142. if (!dbp)
  143. return -ENOMEM;
  144. version = debugfs_create_x16("version", S_IRUGO, dbp,
  145. &boot_params.hdr.version);
  146. if (!version)
  147. goto err_dir;
  148. data = debugfs_create_blob("data", S_IRUGO, dbp,
  149. &boot_params_blob);
  150. if (!data)
  151. goto err_version;
  152. error = create_setup_data_nodes(dbp);
  153. if (error)
  154. goto err_data;
  155. return 0;
  156. err_data:
  157. debugfs_remove(data);
  158. err_version:
  159. debugfs_remove(version);
  160. err_dir:
  161. debugfs_remove(dbp);
  162. return error;
  163. }
  164. #endif /* CONFIG_DEBUG_BOOT_PARAMS */
  165. static int __init arch_kdebugfs_init(void)
  166. {
  167. int error = 0;
  168. arch_debugfs_dir = debugfs_create_dir("x86", NULL);
  169. if (!arch_debugfs_dir)
  170. return -ENOMEM;
  171. #ifdef CONFIG_DEBUG_BOOT_PARAMS
  172. error = boot_params_kdebugfs_init();
  173. #endif
  174. return error;
  175. }
  176. arch_initcall(arch_kdebugfs_init);