machine.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/string.h>
  4. #include <stdlib.h>
  5. #include "../../util/machine.h"
  6. #include "../../util/map.h"
  7. #include "../../util/symbol.h"
  8. #include "../../util/sane_ctype.h"
  9. #include <symbol/kallsyms.h>
  10. #if defined(__x86_64__)
  11. struct extra_kernel_map_info {
  12. int cnt;
  13. int max_cnt;
  14. struct extra_kernel_map *maps;
  15. bool get_entry_trampolines;
  16. u64 entry_trampoline;
  17. };
  18. static int add_extra_kernel_map(struct extra_kernel_map_info *mi, u64 start,
  19. u64 end, u64 pgoff, const char *name)
  20. {
  21. if (mi->cnt >= mi->max_cnt) {
  22. void *buf;
  23. size_t sz;
  24. mi->max_cnt = mi->max_cnt ? mi->max_cnt * 2 : 32;
  25. sz = sizeof(struct extra_kernel_map) * mi->max_cnt;
  26. buf = realloc(mi->maps, sz);
  27. if (!buf)
  28. return -1;
  29. mi->maps = buf;
  30. }
  31. mi->maps[mi->cnt].start = start;
  32. mi->maps[mi->cnt].end = end;
  33. mi->maps[mi->cnt].pgoff = pgoff;
  34. strlcpy(mi->maps[mi->cnt].name, name, KMAP_NAME_LEN);
  35. mi->cnt += 1;
  36. return 0;
  37. }
  38. static int find_extra_kernel_maps(void *arg, const char *name, char type,
  39. u64 start)
  40. {
  41. struct extra_kernel_map_info *mi = arg;
  42. if (!mi->entry_trampoline && kallsyms2elf_binding(type) == STB_GLOBAL &&
  43. !strcmp(name, "_entry_trampoline")) {
  44. mi->entry_trampoline = start;
  45. return 0;
  46. }
  47. if (is_entry_trampoline(name)) {
  48. u64 end = start + page_size;
  49. return add_extra_kernel_map(mi, start, end, 0, name);
  50. }
  51. return 0;
  52. }
  53. int machine__create_extra_kernel_maps(struct machine *machine,
  54. struct dso *kernel)
  55. {
  56. struct extra_kernel_map_info mi = { .cnt = 0, };
  57. char filename[PATH_MAX];
  58. int ret;
  59. int i;
  60. machine__get_kallsyms_filename(machine, filename, PATH_MAX);
  61. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  62. return 0;
  63. ret = kallsyms__parse(filename, &mi, find_extra_kernel_maps);
  64. if (ret)
  65. goto out_free;
  66. if (!mi.entry_trampoline)
  67. goto out_free;
  68. for (i = 0; i < mi.cnt; i++) {
  69. struct extra_kernel_map *xm = &mi.maps[i];
  70. xm->pgoff = mi.entry_trampoline;
  71. ret = machine__create_extra_kernel_map(machine, kernel, xm);
  72. if (ret)
  73. goto out_free;
  74. }
  75. machine->trampolines_mapped = mi.cnt;
  76. out_free:
  77. free(mi.maps);
  78. return ret;
  79. }
  80. #endif