elf.c 3.7 KB

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