elf.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <arch/sim_def.h>
  24. /* Notify a running simulator, if any, that an exec just occurred. */
  25. static void sim_notify_exec(const char *binary_name)
  26. {
  27. unsigned char c;
  28. do {
  29. c = *binary_name++;
  30. __insn_mtspr(SPR_SIM_CONTROL,
  31. (SIM_CONTROL_OS_EXEC
  32. | (c << _SIM_CONTROL_OPERATOR_BITS)));
  33. } while (c);
  34. }
  35. static int notify_exec(void)
  36. {
  37. int retval = 0; /* failure */
  38. struct vm_area_struct *vma = current->mm->mmap;
  39. while (vma) {
  40. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
  41. break;
  42. vma = vma->vm_next;
  43. }
  44. if (vma) {
  45. char *buf = (char *) __get_free_page(GFP_KERNEL);
  46. if (buf) {
  47. char *path = d_path(&vma->vm_file->f_path,
  48. buf, PAGE_SIZE);
  49. if (!IS_ERR(path)) {
  50. sim_notify_exec(path);
  51. retval = 1;
  52. }
  53. free_page((unsigned long)buf);
  54. }
  55. }
  56. return retval;
  57. }
  58. /* Notify a running simulator, if any, that we loaded an interpreter. */
  59. static void sim_notify_interp(unsigned long load_addr)
  60. {
  61. size_t i;
  62. for (i = 0; i < sizeof(load_addr); i++) {
  63. unsigned char c = load_addr >> (i * 8);
  64. __insn_mtspr(SPR_SIM_CONTROL,
  65. (SIM_CONTROL_OS_INTERP
  66. | (c << _SIM_CONTROL_OPERATOR_BITS)));
  67. }
  68. }
  69. /* Kernel address of page used to map read-only kernel data into userspace. */
  70. static void *vdso_page;
  71. /* One-entry array used for install_special_mapping. */
  72. static struct page *vdso_pages[1];
  73. static int __init vdso_setup(void)
  74. {
  75. vdso_page = (void *)get_zeroed_page(GFP_ATOMIC);
  76. memcpy(vdso_page, __rt_sigreturn, __rt_sigreturn_end - __rt_sigreturn);
  77. vdso_pages[0] = virt_to_page(vdso_page);
  78. return 0;
  79. }
  80. device_initcall(vdso_setup);
  81. const char *arch_vma_name(struct vm_area_struct *vma)
  82. {
  83. if (vma->vm_private_data == vdso_pages)
  84. return "[vdso]";
  85. #ifndef __tilegx__
  86. if (vma->vm_start == MEM_USER_INTRPT)
  87. return "[intrpt]";
  88. #endif
  89. return NULL;
  90. }
  91. int arch_setup_additional_pages(struct linux_binprm *bprm,
  92. int executable_stack)
  93. {
  94. struct mm_struct *mm = current->mm;
  95. unsigned long vdso_base;
  96. int retval = 0;
  97. /*
  98. * Notify the simulator that an exec just occurred.
  99. * If we can't find the filename of the mapping, just use
  100. * whatever was passed as the linux_binprm filename.
  101. */
  102. if (!notify_exec())
  103. sim_notify_exec(bprm->filename);
  104. down_write(&mm->mmap_sem);
  105. /*
  106. * MAYWRITE to allow gdb to COW and set breakpoints
  107. */
  108. vdso_base = VDSO_BASE;
  109. retval = install_special_mapping(mm, vdso_base, PAGE_SIZE,
  110. VM_READ|VM_EXEC|
  111. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  112. vdso_pages);
  113. #ifndef __tilegx__
  114. /*
  115. * Set up a user-interrupt mapping here; the user can't
  116. * create one themselves since it is above TASK_SIZE.
  117. * We make it unwritable by default, so the model for adding
  118. * interrupt vectors always involves an mprotect.
  119. */
  120. if (!retval) {
  121. unsigned long addr = MEM_USER_INTRPT;
  122. addr = mmap_region(NULL, addr, INTRPT_SIZE,
  123. MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
  124. VM_READ|VM_EXEC|
  125. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 0);
  126. if (addr > (unsigned long) -PAGE_SIZE)
  127. retval = (int) addr;
  128. }
  129. #endif
  130. up_write(&mm->mmap_sem);
  131. return retval;
  132. }
  133. void elf_plat_init(struct pt_regs *regs, unsigned long load_addr)
  134. {
  135. /* Zero all registers. */
  136. memset(regs, 0, sizeof(*regs));
  137. /* Report the interpreter's load address. */
  138. sim_notify_interp(load_addr);
  139. }