Commit | Line | Data |
---|---|---|
867e359b CM |
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> | |
20 | #include <linux/elf.h> | |
21 | #include <asm/pgtable.h> | |
22 | #include <asm/pgalloc.h> | |
0707ad30 | 23 | #include <asm/sections.h> |
0c1d1917 | 24 | #include <arch/sim.h> |
867e359b CM |
25 | |
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 | ||
36 | } while (c); | |
37 | } | |
38 | ||
2dd8ad81 | 39 | static int notify_exec(struct mm_struct *mm) |
867e359b | 40 | { |
0c1d1917 CM |
41 | char *buf, *path; |
42 | struct vm_area_struct *vma; | |
2dd8ad81 | 43 | |
0c1d1917 CM |
44 | if (!sim_is_simulator()) |
45 | return 1; | |
46 | ||
47 | if (mm->exe_file == NULL) | |
48 | return 0; | |
49 | ||
50 | for (vma = current->mm->mmap; ; vma = vma->vm_next) { | |
51 | if (vma == NULL) | |
52 | return 0; | |
53 | if (vma->vm_file == mm->exe_file) | |
54 | break; | |
55 | } | |
56 | ||
57 | buf = (char *) __get_free_page(GFP_KERNEL); | |
58 | if (buf == NULL) | |
59 | return 0; | |
60 | ||
61 | path = d_path(&mm->exe_file->f_path, buf, PAGE_SIZE); | |
62 | if (IS_ERR(path)) { | |
63 | free_page((unsigned long)buf); | |
64 | return 0; | |
65 | } | |
66 | ||
67 | /* | |
68 | * Notify simulator of an ET_DYN object so we know the load address. | |
69 | * The somewhat cryptic overuse of SIM_CONTROL_DLOPEN allows us | |
70 | * to be backward-compatible with older simulator releases. | |
71 | */ | |
72 | if (vma->vm_start == (ELF_ET_DYN_BASE & PAGE_MASK)) { | |
73 | char buf[64]; | |
74 | int i; | |
75 | ||
76 | snprintf(buf, sizeof(buf), "0x%lx:@", vma->vm_start); | |
77 | for (i = 0; ; ++i) { | |
78 | char c = buf[i]; | |
79 | __insn_mtspr(SPR_SIM_CONTROL, | |
80 | (SIM_CONTROL_DLOPEN | |
81 | | (c << _SIM_CONTROL_OPERATOR_BITS))); | |
82 | if (c == '\0') | |
83 | break; | |
867e359b CM |
84 | } |
85 | } | |
0c1d1917 CM |
86 | |
87 | sim_notify_exec(path); | |
88 | free_page((unsigned long)buf); | |
89 | return 1; | |
867e359b CM |
90 | } |
91 | ||
92 | /* Notify a running simulator, if any, that we loaded an interpreter. */ | |
93 | static void sim_notify_interp(unsigned long load_addr) | |
94 | { | |
95 | size_t i; | |
96 | for (i = 0; i < sizeof(load_addr); i++) { | |
97 | unsigned char c = load_addr >> (i * 8); | |
98 | __insn_mtspr(SPR_SIM_CONTROL, | |
99 | (SIM_CONTROL_OS_INTERP | |
100 | | (c << _SIM_CONTROL_OPERATOR_BITS))); | |
101 | } | |
102 | } | |
103 | ||
104 | ||
105 | /* Kernel address of page used to map read-only kernel data into userspace. */ | |
106 | static void *vdso_page; | |
107 | ||
108 | /* One-entry array used for install_special_mapping. */ | |
109 | static struct page *vdso_pages[1]; | |
110 | ||
0707ad30 | 111 | static int __init vdso_setup(void) |
867e359b | 112 | { |
867e359b CM |
113 | vdso_page = (void *)get_zeroed_page(GFP_ATOMIC); |
114 | memcpy(vdso_page, __rt_sigreturn, __rt_sigreturn_end - __rt_sigreturn); | |
115 | vdso_pages[0] = virt_to_page(vdso_page); | |
116 | return 0; | |
117 | } | |
118 | device_initcall(vdso_setup); | |
119 | ||
120 | const char *arch_vma_name(struct vm_area_struct *vma) | |
121 | { | |
122 | if (vma->vm_private_data == vdso_pages) | |
123 | return "[vdso]"; | |
124 | #ifndef __tilegx__ | |
125 | if (vma->vm_start == MEM_USER_INTRPT) | |
126 | return "[intrpt]"; | |
127 | #endif | |
128 | return NULL; | |
129 | } | |
130 | ||
131 | int arch_setup_additional_pages(struct linux_binprm *bprm, | |
132 | int executable_stack) | |
133 | { | |
134 | struct mm_struct *mm = current->mm; | |
135 | unsigned long vdso_base; | |
136 | int retval = 0; | |
137 | ||
2dd8ad81 KK |
138 | down_write(&mm->mmap_sem); |
139 | ||
867e359b CM |
140 | /* |
141 | * Notify the simulator that an exec just occurred. | |
142 | * If we can't find the filename of the mapping, just use | |
143 | * whatever was passed as the linux_binprm filename. | |
144 | */ | |
2dd8ad81 | 145 | if (!notify_exec(mm)) |
867e359b CM |
146 | sim_notify_exec(bprm->filename); |
147 | ||
867e359b CM |
148 | /* |
149 | * MAYWRITE to allow gdb to COW and set breakpoints | |
867e359b CM |
150 | */ |
151 | vdso_base = VDSO_BASE; | |
152 | retval = install_special_mapping(mm, vdso_base, PAGE_SIZE, | |
153 | VM_READ|VM_EXEC| | |
909af768 | 154 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, |
867e359b CM |
155 | vdso_pages); |
156 | ||
157 | #ifndef __tilegx__ | |
158 | /* | |
159 | * Set up a user-interrupt mapping here; the user can't | |
160 | * create one themselves since it is above TASK_SIZE. | |
161 | * We make it unwritable by default, so the model for adding | |
162 | * interrupt vectors always involves an mprotect. | |
163 | */ | |
164 | if (!retval) { | |
165 | unsigned long addr = MEM_USER_INTRPT; | |
166 | addr = mmap_region(NULL, addr, INTRPT_SIZE, | |
867e359b CM |
167 | VM_READ|VM_EXEC| |
168 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 0); | |
169 | if (addr > (unsigned long) -PAGE_SIZE) | |
170 | retval = (int) addr; | |
171 | } | |
172 | #endif | |
173 | ||
174 | up_write(&mm->mmap_sem); | |
175 | ||
176 | return retval; | |
177 | } | |
178 | ||
179 | ||
180 | void elf_plat_init(struct pt_regs *regs, unsigned long load_addr) | |
181 | { | |
182 | /* Zero all registers. */ | |
183 | memset(regs, 0, sizeof(*regs)); | |
184 | ||
185 | /* Report the interpreter's load address. */ | |
186 | sim_notify_interp(load_addr); | |
187 | } |