blob: 59aa43cb146e732e01544790913f0b018a923cc6 [file] [log] [blame]
Vineet Gupta4adeefe2013-01-18 15:12:18 +05301/*
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
23SYSCALL_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 */
39SYSCALL_DEFINE0(arc_gettls)
40{
41 return task_thread_info(current)->thr_ptr;
42}
Vineet Guptabf90e1e2013-01-18 15:12:18 +053043
Vineet Gupta91e040a2016-10-20 07:39:45 -070044SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)
45{
46 int uval;
47 int ret;
48
49 /*
50 * This is only for old cores lacking LLOCK/SCOND, which by defintion
51 * can't possibly be SMP. Thus doesn't need to be SMP safe.
52 * And this also helps reduce the overhead for serializing in
53 * the UP case
54 */
55 WARN_ON_ONCE(IS_ENABLED(CONFIG_SMP));
56
57 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(int)))
58 return -EFAULT;
59
60 preempt_disable();
61
62 ret = __get_user(uval, uaddr);
63 if (ret)
64 goto done;
65
66 if (uval != expected)
67 ret = -EAGAIN;
68 else
69 ret = __put_user(new, uaddr);
70
71done:
72 preempt_enable();
73
74 return ret;
75}
76
Thomas Gleixnerfa35e422013-03-21 22:49:36 +010077void arch_cpu_idle(void)
Vineet Guptabf90e1e2013-01-18 15:12:18 +053078{
79 /* sleep, but enable all interrupts before committing */
Vineet Gupta512b5b82015-11-16 13:52:07 +053080 __asm__ __volatile__(
81 "sleep %0 \n"
82 :
83 :"I"(ISA_SLEEP_ARG)); /* can't be "r" has to be embedded const */
Vineet Guptabf90e1e2013-01-18 15:12:18 +053084}
85
Vineet Guptabf90e1e2013-01-18 15:12:18 +053086asmlinkage void ret_from_fork(void);
87
Alex Dowad18e1d1b2015-03-13 20:04:18 +020088/*
89 * Copy architecture-specific thread state
90 *
91 * Layout of Child kernel mode stack as setup at the end of this function is
Vineet Guptabf90e1e2013-01-18 15:12:18 +053092 *
93 * | ... |
94 * | ... |
95 * | unused |
96 * | |
Vineet Guptabf90e1e2013-01-18 15:12:18 +053097 * ------------------
Vineet Gupta16f9afe2013-05-27 21:43:41 +053098 * | r25 | <==== top of Stack (thread.ksp)
Vineet Guptabf90e1e2013-01-18 15:12:18 +053099 * ~ ~
Vineet Gupta09074952015-08-19 17:23:58 +0530100 * | --to-- | (CALLEE Regs of kernel mode)
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530101 * | r13 |
102 * ------------------
103 * | fp |
104 * | blink | @ret_from_fork
105 * ------------------
106 * | |
107 * ~ ~
108 * ~ ~
109 * | |
110 * ------------------
111 * | r12 |
112 * ~ ~
113 * | --to-- | (scratch Regs of user mode)
114 * | r0 |
Vineet Gupta3ebedbb2013-05-28 13:24:43 +0530115 * ------------------
116 * | SP |
117 * | orig_r0 |
Vineet Gupta502a0c72013-06-11 18:56:54 +0530118 * | event/ECR |
Vineet Gupta359105b2013-05-28 13:50:41 +0530119 * | user_r25 |
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530120 * ------------------ <===== END of PAGE
121 */
122int copy_thread(unsigned long clone_flags,
Alex Dowad18e1d1b2015-03-13 20:04:18 +0200123 unsigned long usp, unsigned long kthread_arg,
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530124 struct task_struct *p)
125{
126 struct pt_regs *c_regs; /* child's pt_regs */
127 unsigned long *childksp; /* to unwind out of __switch_to() */
128 struct callee_regs *c_callee; /* child's callee regs */
129 struct callee_regs *parent_callee; /* paren't callee */
130 struct pt_regs *regs = current_pt_regs();
131
132 /* Mark the specific anchors to begin with (see pic above) */
133 c_regs = task_pt_regs(p);
134 childksp = (unsigned long *)c_regs - 2; /* 2 words for FP/BLINK */
135 c_callee = ((struct callee_regs *)childksp) - 1;
136
137 /*
138 * __switch_to() uses thread.ksp to start unwinding stack
139 * For kernel threads we don't need to create callee regs, the
140 * stack layout nevertheless needs to remain the same.
141 * Also, since __switch_to anyways unwinds callee regs, we use
142 * this to populate kernel thread entry-pt/args into callee regs,
143 * so that ret_from_kernel_thread() becomes simpler.
144 */
145 p->thread.ksp = (unsigned long)c_callee; /* THREAD_KSP */
146
147 /* __switch_to expects FP(0), BLINK(return addr) at top */
148 childksp[0] = 0; /* fp */
149 childksp[1] = (unsigned long)ret_from_fork; /* blink */
150
151 if (unlikely(p->flags & PF_KTHREAD)) {
152 memset(c_regs, 0, sizeof(struct pt_regs));
153
Alex Dowad18e1d1b2015-03-13 20:04:18 +0200154 c_callee->r13 = kthread_arg;
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530155 c_callee->r14 = usp; /* function */
156
157 return 0;
158 }
159
160 /*--------- User Task Only --------------*/
161
162 /* __switch_to expects FP(0), BLINK(return addr) at top of stack */
163 childksp[0] = 0; /* for POP fp */
164 childksp[1] = (unsigned long)ret_from_fork; /* for POP blink */
165
166 /* Copy parents pt regs on child's kernel mode stack */
167 *c_regs = *regs;
168
169 if (usp)
170 c_regs->sp = usp;
171
172 c_regs->r0 = 0; /* fork returns 0 in child */
173
174 parent_callee = ((struct callee_regs *)regs) - 1;
175 *c_callee = *parent_callee;
176
177 if (unlikely(clone_flags & CLONE_SETTLS)) {
178 /*
179 * set task's userland tls data ptr from 4th arg
180 * clone C-lib call is difft from clone sys-call
181 */
182 task_thread_info(p)->thr_ptr = regs->r3;
183 } else {
184 /* Normal fork case: set parent's TLS ptr in child */
185 task_thread_info(p)->thr_ptr =
186 task_thread_info(current)->thr_ptr;
187 }
188
189 return 0;
190}
191
192/*
Vineet Gupta2ab402d2014-04-18 12:19:59 +0530193 * Do necessary setup to start up a new user task
194 */
195void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long usp)
196{
Vineet Gupta2ab402d2014-04-18 12:19:59 +0530197 regs->sp = usp;
198 regs->ret = pc;
199
200 /*
201 * [U]ser Mode bit set
202 * [L] ZOL loop inhibited to begin with - cleared by a LP insn
203 * Interrupts enabled
204 */
Vineet Gupta1f6ccff2013-05-13 18:30:41 +0530205 regs->status32 = STATUS_U_MASK | STATUS_L_MASK | ISA_INIT_STATUS_BITS;
Vineet Gupta2ab402d2014-04-18 12:19:59 +0530206
207 /* bogus seed values for debugging */
208 regs->lp_start = 0x10;
209 regs->lp_end = 0x80;
210}
211
212/*
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530213 * Some archs flush debug and FPU info here
214 */
215void flush_thread(void)
216{
217}
218
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530219int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
220{
221 return 0;
222}
223
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530224int elf_check_arch(const struct elf32_hdr *x)
225{
226 unsigned int eflags;
227
Vineet Gupta1f6ccff2013-05-13 18:30:41 +0530228 if (x->e_machine != EM_ARC_INUSE) {
229 pr_err("ELF not built for %s ISA\n",
230 is_isa_arcompact() ? "ARCompact":"ARCv2");
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530231 return 0;
Vineet Gupta1f6ccff2013-05-13 18:30:41 +0530232 }
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530233
234 eflags = x->e_flags;
Vineet Gupta840c0542016-08-10 14:10:57 -0700235 if ((eflags & EF_ARC_OSABI_MSK) != EF_ARC_OSABI_CURRENT) {
Vineet Guptabf90e1e2013-01-18 15:12:18 +0530236 pr_err("ABI mismatch - you need newer toolchain\n");
237 force_sigsegv(SIGSEGV, current);
238 return 0;
239 }
240
241 return 1;
242}
243EXPORT_SYMBOL(elf_check_arch);