kdebugfs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. };
  66. static int __init
  67. create_setup_data_node(struct dentry *parent, int no,
  68. struct setup_data_node *node)
  69. {
  70. struct dentry *d, *type, *data;
  71. char buf[16];
  72. sprintf(buf, "%d", no);
  73. d = debugfs_create_dir(buf, parent);
  74. if (!d)
  75. return -ENOMEM;
  76. type = debugfs_create_x32("type", S_IRUGO, d, &node->type);
  77. if (!type)
  78. goto err_dir;
  79. data = debugfs_create_file("data", S_IRUGO, d, node, &fops_setup_data);
  80. if (!data)
  81. goto err_type;
  82. return 0;
  83. err_type:
  84. debugfs_remove(type);
  85. err_dir:
  86. debugfs_remove(d);
  87. return -ENOMEM;
  88. }
  89. static int __init create_setup_data_nodes(struct dentry *parent)
  90. {
  91. struct setup_data_node *node;
  92. struct setup_data *data;
  93. int error = -ENOMEM;
  94. struct dentry *d;
  95. struct page *pg;
  96. u64 pa_data;
  97. int no = 0;
  98. d = debugfs_create_dir("setup_data", parent);
  99. if (!d)
  100. return -ENOMEM;
  101. pa_data = boot_params.hdr.setup_data;
  102. while (pa_data) {
  103. node = kmalloc(sizeof(*node), GFP_KERNEL);
  104. if (!node)
  105. goto err_dir;
  106. pg = pfn_to_page((pa_data+sizeof(*data)-1) >> PAGE_SHIFT);
  107. if (PageHighMem(pg)) {
  108. data = ioremap_cache(pa_data, sizeof(*data));
  109. if (!data) {
  110. kfree(node);
  111. error = -ENXIO;
  112. goto err_dir;
  113. }
  114. } else
  115. data = __va(pa_data);
  116. node->paddr = pa_data;
  117. node->type = data->type;
  118. node->len = data->len;
  119. error = create_setup_data_node(d, no, node);
  120. pa_data = data->next;
  121. if (PageHighMem(pg))
  122. iounmap(data);
  123. if (error)
  124. goto err_dir;
  125. no++;
  126. }
  127. return 0;
  128. err_dir:
  129. debugfs_remove(d);
  130. return error;
  131. }
  132. static struct debugfs_blob_wrapper boot_params_blob = {
  133. .data = &boot_params,
  134. .size = sizeof(boot_params),
  135. };
  136. static int __init boot_params_kdebugfs_init(void)
  137. {
  138. struct dentry *dbp, *version, *data;
  139. int error = -ENOMEM;
  140. dbp = debugfs_create_dir("boot_params", NULL);
  141. if (!dbp)
  142. return -ENOMEM;
  143. version = debugfs_create_x16("version", S_IRUGO, dbp,
  144. &boot_params.hdr.version);
  145. if (!version)
  146. goto err_dir;
  147. data = debugfs_create_blob("data", S_IRUGO, dbp,
  148. &boot_params_blob);
  149. if (!data)
  150. goto err_version;
  151. error = create_setup_data_nodes(dbp);
  152. if (error)
  153. goto err_data;
  154. return 0;
  155. err_data:
  156. debugfs_remove(data);
  157. err_version:
  158. debugfs_remove(version);
  159. err_dir:
  160. debugfs_remove(dbp);
  161. return error;
  162. }
  163. #endif /* CONFIG_DEBUG_BOOT_PARAMS */
  164. static int __init arch_kdebugfs_init(void)
  165. {
  166. int error = 0;
  167. arch_debugfs_dir = debugfs_create_dir("x86", NULL);
  168. if (!arch_debugfs_dir)
  169. return -ENOMEM;
  170. #ifdef CONFIG_DEBUG_BOOT_PARAMS
  171. error = boot_params_kdebugfs_init();
  172. #endif
  173. return error;
  174. }
  175. arch_initcall(arch_kdebugfs_init);