GNU Linux-libre 4.9.309-gnu1
[releases.git] / arch / arc / kernel / process.c
1 /*
2  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Amit Bhor, Kanika Nema: Codito Technologies 2004
9  */
10
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/fs.h>
16 #include <linux/unistd.h>
17 #include <linux/ptrace.h>
18 #include <linux/slab.h>
19 #include <linux/syscalls.h>
20 #include <linux/elf.h>
21 #include <linux/tick.h>
22
23 SYSCALL_DEFINE1(arc_settls, void *, user_tls_data_ptr)
24 {
25         task_thread_info(current)->thr_ptr = (unsigned int)user_tls_data_ptr;
26         return 0;
27 }
28
29 /*
30  * We return the user space TLS data ptr as sys-call return code
31  * Ideally it should be copy to user.
32  * However we can cheat by the fact that some sys-calls do return
33  * absurdly high values
34  * Since the tls dat aptr is not going to be in range of 0xFFFF_xxxx
35  * it won't be considered a sys-call error
36  * and it will be loads better than copy-to-user, which is a definite
37  * D-TLB Miss
38  */
39 SYSCALL_DEFINE0(arc_gettls)
40 {
41         return task_thread_info(current)->thr_ptr;
42 }
43
44 SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)
45 {
46         struct pt_regs *regs = current_pt_regs();
47         u32 uval;
48         int ret;
49
50         /*
51          * This is only for old cores lacking LLOCK/SCOND, which by defintion
52          * can't possibly be SMP. Thus doesn't need to be SMP safe.
53          * And this also helps reduce the overhead for serializing in
54          * the UP case
55          */
56         WARN_ON_ONCE(IS_ENABLED(CONFIG_SMP));
57
58         /* Z indicates to userspace if operation succeded */
59         regs->status32 &= ~STATUS_Z_MASK;
60
61         ret = access_ok(VERIFY_WRITE, uaddr, sizeof(*uaddr));
62         if (!ret)
63                  goto fail;
64
65 again:
66         preempt_disable();
67
68         ret = __get_user(uval, uaddr);
69         if (ret)
70                  goto fault;
71
72         if (uval != expected)
73                  goto out;
74
75         ret = __put_user(new, uaddr);
76         if (ret)
77                  goto fault;
78
79         regs->status32 |= STATUS_Z_MASK;
80
81 out:
82         preempt_enable();
83         return uval;
84
85 fault:
86         preempt_enable();
87
88         if (unlikely(ret != -EFAULT))
89                  goto fail;
90
91         down_read(&current->mm->mmap_sem);
92         ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
93                                FAULT_FLAG_WRITE, NULL);
94         up_read(&current->mm->mmap_sem);
95
96         if (likely(!ret))
97                  goto again;
98
99 fail:
100         force_sig(SIGSEGV, current);
101         return ret;
102 }
103
104 void arch_cpu_idle(void)
105 {
106         /* sleep, but enable all interrupts before committing */
107         __asm__ __volatile__(
108                 "sleep %0       \n"
109                 :
110                 :"I"(ISA_SLEEP_ARG)); /* can't be "r" has to be embedded const */
111 }
112
113 asmlinkage void ret_from_fork(void);
114
115 /*
116  * Copy architecture-specific thread state
117  *
118  * Layout of Child kernel mode stack as setup at the end of this function is
119  *
120  * |     ...        |
121  * |     ...        |
122  * |    unused      |
123  * |                |
124  * ------------------
125  * |     r25        |   <==== top of Stack (thread.ksp)
126  * ~                ~
127  * |    --to--      |   (CALLEE Regs of kernel mode)
128  * |     r13        |
129  * ------------------
130  * |     fp         |
131  * |    blink       |   @ret_from_fork
132  * ------------------
133  * |                |
134  * ~                ~
135  * ~                ~
136  * |                |
137  * ------------------
138  * |     r12        |
139  * ~                ~
140  * |    --to--      |   (scratch Regs of user mode)
141  * |     r0         |
142  * ------------------
143  * |      SP        |
144  * |    orig_r0     |
145  * |    event/ECR   |
146  * |    user_r25    |
147  * ------------------  <===== END of PAGE
148  */
149 int copy_thread(unsigned long clone_flags,
150                 unsigned long usp, unsigned long kthread_arg,
151                 struct task_struct *p)
152 {
153         struct pt_regs *c_regs;        /* child's pt_regs */
154         unsigned long *childksp;       /* to unwind out of __switch_to() */
155         struct callee_regs *c_callee;  /* child's callee regs */
156         struct callee_regs *parent_callee;  /* paren't callee */
157         struct pt_regs *regs = current_pt_regs();
158
159         /* Mark the specific anchors to begin with (see pic above) */
160         c_regs = task_pt_regs(p);
161         childksp = (unsigned long *)c_regs - 2;  /* 2 words for FP/BLINK */
162         c_callee = ((struct callee_regs *)childksp) - 1;
163
164         /*
165          * __switch_to() uses thread.ksp to start unwinding stack
166          * For kernel threads we don't need to create callee regs, the
167          * stack layout nevertheless needs to remain the same.
168          * Also, since __switch_to anyways unwinds callee regs, we use
169          * this to populate kernel thread entry-pt/args into callee regs,
170          * so that ret_from_kernel_thread() becomes simpler.
171          */
172         p->thread.ksp = (unsigned long)c_callee;        /* THREAD_KSP */
173
174         /* __switch_to expects FP(0), BLINK(return addr) at top */
175         childksp[0] = 0;                        /* fp */
176         childksp[1] = (unsigned long)ret_from_fork; /* blink */
177
178         if (unlikely(p->flags & PF_KTHREAD)) {
179                 memset(c_regs, 0, sizeof(struct pt_regs));
180
181                 c_callee->r13 = kthread_arg;
182                 c_callee->r14 = usp;  /* function */
183
184                 return 0;
185         }
186
187         /*--------- User Task Only --------------*/
188
189         /* __switch_to expects FP(0), BLINK(return addr) at top of stack */
190         childksp[0] = 0;                                /* for POP fp */
191         childksp[1] = (unsigned long)ret_from_fork;     /* for POP blink */
192
193         /* Copy parents pt regs on child's kernel mode stack */
194         *c_regs = *regs;
195
196         if (usp)
197                 c_regs->sp = usp;
198
199         c_regs->r0 = 0;         /* fork returns 0 in child */
200
201         parent_callee = ((struct callee_regs *)regs) - 1;
202         *c_callee = *parent_callee;
203
204         if (unlikely(clone_flags & CLONE_SETTLS)) {
205                 /*
206                  * set task's userland tls data ptr from 4th arg
207                  * clone C-lib call is difft from clone sys-call
208                  */
209                 task_thread_info(p)->thr_ptr = regs->r3;
210         } else {
211                 /* Normal fork case: set parent's TLS ptr in child */
212                 task_thread_info(p)->thr_ptr =
213                 task_thread_info(current)->thr_ptr;
214         }
215
216
217         /*
218          * setup usermode thread pointer #1:
219          * when child is picked by scheduler, __switch_to() uses @c_callee to
220          * populate usermode callee regs: this works (despite being in a kernel
221          * function) since special return path for child @ret_from_fork()
222          * ensures those regs are not clobbered all the way to RTIE to usermode
223          */
224         c_callee->r25 = task_thread_info(p)->thr_ptr;
225
226 #ifdef CONFIG_ARC_CURR_IN_REG
227         /*
228          * setup usermode thread pointer #2:
229          * however for this special use of r25 in kernel, __switch_to() sets
230          * r25 for kernel needs and only in the final return path is usermode
231          * r25 setup, from pt_regs->user_r25. So set that up as well
232          */
233         c_regs->user_r25 = c_callee->r25;
234 #endif
235
236         return 0;
237 }
238
239 /*
240  * Do necessary setup to start up a new user task
241  */
242 void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long usp)
243 {
244         regs->sp = usp;
245         regs->ret = pc;
246
247         /*
248          * [U]ser Mode bit set
249          * [L] ZOL loop inhibited to begin with - cleared by a LP insn
250          * Interrupts enabled
251          */
252         regs->status32 = STATUS_U_MASK | STATUS_L_MASK | ISA_INIT_STATUS_BITS;
253
254         /* bogus seed values for debugging */
255         regs->lp_start = 0x10;
256         regs->lp_end = 0x80;
257 }
258
259 /*
260  * Some archs flush debug and FPU info here
261  */
262 void flush_thread(void)
263 {
264 }
265
266 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
267 {
268         return 0;
269 }
270
271 int elf_check_arch(const struct elf32_hdr *x)
272 {
273         unsigned int eflags;
274
275         if (x->e_machine != EM_ARC_INUSE) {
276                 pr_err("ELF not built for %s ISA\n",
277                         is_isa_arcompact() ? "ARCompact":"ARCv2");
278                 return 0;
279         }
280
281         eflags = x->e_flags;
282         if ((eflags & EF_ARC_OSABI_MSK) != EF_ARC_OSABI_CURRENT) {
283                 pr_err("ABI mismatch - you need newer toolchain\n");
284                 force_sigsegv(SIGSEGV, current);
285                 return 0;
286         }
287
288         return 1;
289 }
290 EXPORT_SYMBOL(elf_check_arch);