elf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/file.h>
  20. #include <linux/elf.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/sections.h>
  24. #include <asm/vdso.h>
  25. #include <arch/sim.h>
  26. /* Notify a running simulator, if any, that an exec just occurred. */
  27. static void sim_notify_exec(const char *binary_name)
  28. {
  29. unsigned char c;
  30. do {
  31. c = *binary_name++;
  32. __insn_mtspr(SPR_SIM_CONTROL,
  33. (SIM_CONTROL_OS_EXEC
  34. | (c << _SIM_CONTROL_OPERATOR_BITS)));
  35. } while (c);
  36. }
  37. static int notify_exec(struct mm_struct *mm)
  38. {
  39. int ret = 0;
  40. char *buf, *path;
  41. struct vm_area_struct *vma;
  42. struct file *exe_file;
  43. if (!sim_is_simulator())
  44. return 1;
  45. buf = (char *) __get_free_page(GFP_KERNEL);
  46. if (buf == NULL)
  47. return 0;
  48. exe_file = get_mm_exe_file(mm);
  49. if (exe_file == NULL)
  50. goto done_free;
  51. path = file_path(exe_file, buf, PAGE_SIZE);
  52. if (IS_ERR(path))
  53. goto done_put;
  54. down_read(&mm->mmap_sem);
  55. for (vma = current->mm->mmap; ; vma = vma->vm_next) {
  56. if (vma == NULL) {
  57. up_read(&mm->mmap_sem);
  58. goto done_put;
  59. }
  60. if (vma->vm_file == exe_file)
  61. break;
  62. }
  63. /*
  64. * Notify simulator of an ET_DYN object so we know the load address.
  65. * The somewhat cryptic overuse of SIM_CONTROL_DLOPEN allows us
  66. * to be backward-compatible with older simulator releases.
  67. */
  68. if (vma->vm_start == (ELF_ET_DYN_BASE & PAGE_MASK)) {
  69. char buf[64];
  70. int i;
  71. snprintf(buf, sizeof(buf), "0x%lx:@", vma->vm_start);
  72. for (i = 0; ; ++i) {
  73. char c = buf[i];
  74. __insn_mtspr(SPR_SIM_CONTROL,
  75. (SIM_CONTROL_DLOPEN
  76. | (c << _SIM_CONTROL_OPERATOR_BITS)));
  77. if (c == '\0') {
  78. ret = 1; /* success */
  79. break;
  80. }
  81. }
  82. }
  83. up_read(&mm->mmap_sem);
  84. sim_notify_exec(path);
  85. done_put:
  86. fput(exe_file);
  87. done_free:
  88. free_page((unsigned long)buf);
  89. return ret;
  90. }
  91. /* Notify a running simulator, if any, that we loaded an interpreter. */
  92. static void sim_notify_interp(unsigned long load_addr)
  93. {
  94. size_t i;
  95. for (i = 0; i < sizeof(load_addr); i++) {
  96. unsigned char c = load_addr >> (i * 8);
  97. __insn_mtspr(SPR_SIM_CONTROL,
  98. (SIM_CONTROL_OS_INTERP
  99. | (c << _SIM_CONTROL_OPERATOR_BITS)));
  100. }
  101. }
  102. int arch_setup_additional_pages(struct linux_binprm *bprm,
  103. int executable_stack)
  104. {
  105. struct mm_struct *mm = current->mm;
  106. int retval = 0;
  107. /*
  108. * Notify the simulator that an exec just occurred.
  109. * If we can't find the filename of the mapping, just use
  110. * whatever was passed as the linux_binprm filename.
  111. */
  112. if (!notify_exec(mm))
  113. sim_notify_exec(bprm->filename);
  114. down_write(&mm->mmap_sem);
  115. retval = setup_vdso_pages();
  116. #ifndef __tilegx__
  117. /*
  118. * Set up a user-interrupt mapping here; the user can't
  119. * create one themselves since it is above TASK_SIZE.
  120. * We make it unwritable by default, so the model for adding
  121. * interrupt vectors always involves an mprotect.
  122. */
  123. if (!retval) {
  124. unsigned long addr = MEM_USER_INTRPT;
  125. addr = mmap_region(NULL, addr, INTRPT_SIZE,
  126. VM_READ|VM_EXEC|
  127. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 0);
  128. if (addr > (unsigned long) -PAGE_SIZE)
  129. retval = (int) addr;
  130. }
  131. #endif
  132. up_write(&mm->mmap_sem);
  133. return retval;
  134. }
  135. void elf_plat_init(struct pt_regs *regs, unsigned long load_addr)
  136. {
  137. /* Zero all registers. */
  138. memset(regs, 0, sizeof(*regs));
  139. /* Report the interpreter's load address. */
  140. sim_notify_interp(load_addr);
  141. }