blob: 3cfa98bf9125215ae2fc7ea8fdc287fd84245285 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
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>
Chris Metcalf0707ad32010-06-25 17:04:17 -040023#include <asm/sections.h>
David Howellsbd119c62012-03-28 18:30:03 +010024#include <arch/sim_def.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040025
26/* Notify a running simulator, if any, that an exec just occurred. */
27static 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
Konstantin Khlebnikov2dd8ad82012-10-08 16:28:51 -070039static int notify_exec(struct mm_struct *mm)
Chris Metcalf867e3592010-05-28 23:09:12 -040040{
41 int retval = 0; /* failure */
Konstantin Khlebnikov2dd8ad82012-10-08 16:28:51 -070042
43 if (mm->exe_file) {
Chris Metcalf867e3592010-05-28 23:09:12 -040044 char *buf = (char *) __get_free_page(GFP_KERNEL);
45 if (buf) {
Konstantin Khlebnikov2dd8ad82012-10-08 16:28:51 -070046 char *path = d_path(&mm->exe_file->f_path,
Chris Metcalf867e3592010-05-28 23:09:12 -040047 buf, PAGE_SIZE);
48 if (!IS_ERR(path)) {
49 sim_notify_exec(path);
50 retval = 1;
51 }
52 free_page((unsigned long)buf);
53 }
54 }
55 return retval;
56}
57
58/* Notify a running simulator, if any, that we loaded an interpreter. */
59static void sim_notify_interp(unsigned long load_addr)
60{
61 size_t i;
62 for (i = 0; i < sizeof(load_addr); i++) {
63 unsigned char c = load_addr >> (i * 8);
64 __insn_mtspr(SPR_SIM_CONTROL,
65 (SIM_CONTROL_OS_INTERP
66 | (c << _SIM_CONTROL_OPERATOR_BITS)));
67 }
68}
69
70
71/* Kernel address of page used to map read-only kernel data into userspace. */
72static void *vdso_page;
73
74/* One-entry array used for install_special_mapping. */
75static struct page *vdso_pages[1];
76
Chris Metcalf0707ad32010-06-25 17:04:17 -040077static int __init vdso_setup(void)
Chris Metcalf867e3592010-05-28 23:09:12 -040078{
Chris Metcalf867e3592010-05-28 23:09:12 -040079 vdso_page = (void *)get_zeroed_page(GFP_ATOMIC);
80 memcpy(vdso_page, __rt_sigreturn, __rt_sigreturn_end - __rt_sigreturn);
81 vdso_pages[0] = virt_to_page(vdso_page);
82 return 0;
83}
84device_initcall(vdso_setup);
85
86const char *arch_vma_name(struct vm_area_struct *vma)
87{
88 if (vma->vm_private_data == vdso_pages)
89 return "[vdso]";
90#ifndef __tilegx__
91 if (vma->vm_start == MEM_USER_INTRPT)
92 return "[intrpt]";
93#endif
94 return NULL;
95}
96
97int arch_setup_additional_pages(struct linux_binprm *bprm,
98 int executable_stack)
99{
100 struct mm_struct *mm = current->mm;
101 unsigned long vdso_base;
102 int retval = 0;
103
Konstantin Khlebnikov2dd8ad82012-10-08 16:28:51 -0700104 down_write(&mm->mmap_sem);
105
Chris Metcalf867e3592010-05-28 23:09:12 -0400106 /*
107 * Notify the simulator that an exec just occurred.
108 * If we can't find the filename of the mapping, just use
109 * whatever was passed as the linux_binprm filename.
110 */
Konstantin Khlebnikov2dd8ad82012-10-08 16:28:51 -0700111 if (!notify_exec(mm))
Chris Metcalf867e3592010-05-28 23:09:12 -0400112 sim_notify_exec(bprm->filename);
113
Chris Metcalf867e3592010-05-28 23:09:12 -0400114 /*
115 * MAYWRITE to allow gdb to COW and set breakpoints
Chris Metcalf867e3592010-05-28 23:09:12 -0400116 */
117 vdso_base = VDSO_BASE;
118 retval = install_special_mapping(mm, vdso_base, PAGE_SIZE,
119 VM_READ|VM_EXEC|
Jason Baron909af762012-03-23 15:02:51 -0700120 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
Chris Metcalf867e3592010-05-28 23:09:12 -0400121 vdso_pages);
122
123#ifndef __tilegx__
124 /*
125 * Set up a user-interrupt mapping here; the user can't
126 * create one themselves since it is above TASK_SIZE.
127 * We make it unwritable by default, so the model for adding
128 * interrupt vectors always involves an mprotect.
129 */
130 if (!retval) {
131 unsigned long addr = MEM_USER_INTRPT;
132 addr = mmap_region(NULL, addr, INTRPT_SIZE,
133 MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
134 VM_READ|VM_EXEC|
135 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 0);
136 if (addr > (unsigned long) -PAGE_SIZE)
137 retval = (int) addr;
138 }
139#endif
140
141 up_write(&mm->mmap_sem);
142
143 return retval;
144}
145
146
147void elf_plat_init(struct pt_regs *regs, unsigned long load_addr)
148{
149 /* Zero all registers. */
150 memset(regs, 0, sizeof(*regs));
151
152 /* Report the interpreter's load address. */
153 sim_notify_interp(load_addr);
154}