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