elf.c 3.9 KB

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