xensyms.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/seq_file.h>
  4. #include <linux/fs.h>
  5. #include <linux/mm.h>
  6. #include <linux/proc_fs.h>
  7. #include <linux/slab.h>
  8. #include <xen/interface/platform.h>
  9. #include <asm/xen/hypercall.h>
  10. #include <xen/xen-ops.h>
  11. #include "xenfs.h"
  12. #define XEN_KSYM_NAME_LEN 127 /* Hypervisor may have different name length */
  13. struct xensyms {
  14. struct xen_platform_op op;
  15. char *name;
  16. uint32_t namelen;
  17. };
  18. /* Grab next output page from the hypervisor */
  19. static int xensyms_next_sym(struct xensyms *xs)
  20. {
  21. int ret;
  22. struct xenpf_symdata *symdata = &xs->op.u.symdata;
  23. uint64_t symnum;
  24. memset(xs->name, 0, xs->namelen);
  25. symdata->namelen = xs->namelen;
  26. symnum = symdata->symnum;
  27. ret = HYPERVISOR_dom0_op(&xs->op);
  28. if (ret < 0)
  29. return ret;
  30. /*
  31. * If hypervisor's symbol didn't fit into the buffer then allocate
  32. * a larger buffer and try again.
  33. */
  34. if (unlikely(symdata->namelen > xs->namelen)) {
  35. kfree(xs->name);
  36. xs->namelen = symdata->namelen;
  37. xs->name = kzalloc(xs->namelen, GFP_KERNEL);
  38. if (!xs->name)
  39. return -ENOMEM;
  40. set_xen_guest_handle(symdata->name, xs->name);
  41. symdata->symnum--; /* Rewind */
  42. ret = HYPERVISOR_dom0_op(&xs->op);
  43. if (ret < 0)
  44. return ret;
  45. }
  46. if (symdata->symnum == symnum)
  47. /* End of symbols */
  48. return 1;
  49. return 0;
  50. }
  51. static void *xensyms_start(struct seq_file *m, loff_t *pos)
  52. {
  53. struct xensyms *xs = (struct xensyms *)m->private;
  54. xs->op.u.symdata.symnum = *pos;
  55. if (xensyms_next_sym(xs))
  56. return NULL;
  57. return m->private;
  58. }
  59. static void *xensyms_next(struct seq_file *m, void *p, loff_t *pos)
  60. {
  61. struct xensyms *xs = (struct xensyms *)m->private;
  62. xs->op.u.symdata.symnum = ++(*pos);
  63. if (xensyms_next_sym(xs))
  64. return NULL;
  65. return p;
  66. }
  67. static int xensyms_show(struct seq_file *m, void *p)
  68. {
  69. struct xensyms *xs = (struct xensyms *)m->private;
  70. struct xenpf_symdata *symdata = &xs->op.u.symdata;
  71. seq_printf(m, "%016llx %c %s\n", symdata->address,
  72. symdata->type, xs->name);
  73. return 0;
  74. }
  75. static void xensyms_stop(struct seq_file *m, void *p)
  76. {
  77. }
  78. static const struct seq_operations xensyms_seq_ops = {
  79. .start = xensyms_start,
  80. .next = xensyms_next,
  81. .show = xensyms_show,
  82. .stop = xensyms_stop,
  83. };
  84. static int xensyms_open(struct inode *inode, struct file *file)
  85. {
  86. struct seq_file *m;
  87. struct xensyms *xs;
  88. int ret;
  89. ret = seq_open_private(file, &xensyms_seq_ops,
  90. sizeof(struct xensyms));
  91. if (ret)
  92. return ret;
  93. m = file->private_data;
  94. xs = (struct xensyms *)m->private;
  95. xs->namelen = XEN_KSYM_NAME_LEN + 1;
  96. xs->name = kzalloc(xs->namelen, GFP_KERNEL);
  97. if (!xs->name) {
  98. seq_release_private(inode, file);
  99. return -ENOMEM;
  100. }
  101. set_xen_guest_handle(xs->op.u.symdata.name, xs->name);
  102. xs->op.cmd = XENPF_get_symbol;
  103. xs->op.u.symdata.namelen = xs->namelen;
  104. return 0;
  105. }
  106. static int xensyms_release(struct inode *inode, struct file *file)
  107. {
  108. struct seq_file *m = file->private_data;
  109. struct xensyms *xs = (struct xensyms *)m->private;
  110. kfree(xs->name);
  111. return seq_release_private(inode, file);
  112. }
  113. const struct file_operations xensyms_ops = {
  114. .open = xensyms_open,
  115. .read = seq_read,
  116. .llseek = seq_lseek,
  117. .release = xensyms_release
  118. };