elf.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/binfmts.h>
  17. #include <linux/compat.h>
  18. #include <linux/mman.h>
  19. #include <linux/elf.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/sections.h>
  23. /* Notify a running simulator, if any, that an exec just occurred. */
  24. static void sim_notify_exec(const char *binary_name)
  25. {
  26. unsigned char c;
  27. do {
  28. c = *binary_name++;
  29. __insn_mtspr(SPR_SIM_CONTROL,
  30. (SIM_CONTROL_OS_EXEC
  31. | (c << _SIM_CONTROL_OPERATOR_BITS)));
  32. } while (c);
  33. }
  34. static int notify_exec(void)
  35. {
  36. int retval = 0; /* failure */
  37. struct vm_area_struct *vma = current->mm->mmap;
  38. while (vma) {
  39. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
  40. break;
  41. vma = vma->vm_next;
  42. }
  43. if (vma) {
  44. char *buf = (char *) __get_free_page(GFP_KERNEL);
  45. if (buf) {
  46. char *path = d_path(&vma->vm_file->f_path,
  47. buf, PAGE_SIZE);
  48. if (!IS_ERR(path)) {
  49. sim_notify_exec(path);
  50. retval = 1;
  51. }
  52. free_page((unsigned long)buf);
  53. }
  54. }
  55. return retval;
  56. }
  57. /* Notify a running simulator, if any, that we loaded an interpreter. */
  58. static void sim_notify_interp(unsigned long load_addr)
  59. {
  60. size_t i;
  61. for (i = 0; i < sizeof(load_addr); i++) {
  62. unsigned char c = load_addr >> (i * 8);
  63. __insn_mtspr(SPR_SIM_CONTROL,
  64. (SIM_CONTROL_OS_INTERP
  65. | (c << _SIM_CONTROL_OPERATOR_BITS)));
  66. }
  67. }
  68. /* Kernel address of page used to map read-only kernel data into userspace. */
  69. static void *vdso_page;
  70. /* One-entry array used for install_special_mapping. */
  71. static struct page *vdso_pages[1];
  72. static int __init vdso_setup(void)
  73. {
  74. vdso_page = (void *)get_zeroed_page(GFP_ATOMIC);
  75. memcpy(vdso_page, __rt_sigreturn, __rt_sigreturn_end - __rt_sigreturn);
  76. vdso_pages[0] = virt_to_page(vdso_page);
  77. return 0;
  78. }
  79. device_initcall(vdso_setup);
  80. const char *arch_vma_name(struct vm_area_struct *vma)
  81. {
  82. if (vma->vm_private_data == vdso_pages)
  83. return "[vdso]";
  84. #ifndef __tilegx__
  85. if (vma->vm_start == MEM_USER_INTRPT)
  86. return "[intrpt]";
  87. #endif
  88. return NULL;
  89. }
  90. int arch_setup_additional_pages(struct linux_binprm *bprm,
  91. int executable_stack)
  92. {
  93. struct mm_struct *mm = current->mm;
  94. unsigned long vdso_base;
  95. int retval = 0;
  96. /*
  97. * Notify the simulator that an exec just occurred.
  98. * If we can't find the filename of the mapping, just use
  99. * whatever was passed as the linux_binprm filename.
  100. */
  101. if (!notify_exec())
  102. sim_notify_exec(bprm->filename);
  103. down_write(&mm->mmap_sem);
  104. /*
  105. * MAYWRITE to allow gdb to COW and set breakpoints
  106. *
  107. * Make sure the vDSO gets into every core dump. Dumping its
  108. * contents makes post-mortem fully interpretable later
  109. * without matching up the same kernel and hardware config to
  110. * see what PC values meant.
  111. */
  112. vdso_base = VDSO_BASE;
  113. retval = install_special_mapping(mm, vdso_base, PAGE_SIZE,
  114. VM_READ|VM_EXEC|
  115. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
  116. VM_ALWAYSDUMP,
  117. vdso_pages);
  118. #ifndef __tilegx__
  119. /*
  120. * Set up a user-interrupt mapping here; the user can't
  121. * create one themselves since it is above TASK_SIZE.
  122. * We make it unwritable by default, so the model for adding
  123. * interrupt vectors always involves an mprotect.
  124. */
  125. if (!retval) {
  126. unsigned long addr = MEM_USER_INTRPT;
  127. addr = mmap_region(NULL, addr, INTRPT_SIZE,
  128. MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
  129. VM_READ|VM_EXEC|
  130. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 0);
  131. if (addr > (unsigned long) -PAGE_SIZE)
  132. retval = (int) addr;
  133. }
  134. #endif
  135. up_write(&mm->mmap_sem);
  136. return retval;
  137. }
  138. void elf_plat_init(struct pt_regs *regs, unsigned long load_addr)
  139. {
  140. /* Zero all registers. */
  141. memset(regs, 0, sizeof(*regs));
  142. /* Report the interpreter's load address. */
  143. sim_notify_interp(load_addr);
  144. }