kdebugfs.c 4.0 KB

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