process.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * arch/xtensa/kernel/process.c
 *
 * Xtensa Processor version.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2001 - 2005 Tensilica Inc.
 *
 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
 * Chris Zankel <chris@zankel.net>
 * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
 * Kevin Chea
 */

#include <linux/errno.h>
#include <linux/sched.h>
20
#include <linux/sched/debug.h>
21
#include <linux/sched/task.h>
22
#include <linux/sched/task_stack.h>
23 24 25 26 27 28 29
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/stddef.h>
#include <linux/unistd.h>
#include <linux/ptrace.h>
#include <linux/elf.h>
30
#include <linux/hw_breakpoint.h>
31 32 33 34 35
#include <linux/init.h>
#include <linux/prctl.h>
#include <linux/init_task.h>
#include <linux/module.h>
#include <linux/mqueue.h>
36
#include <linux/fs.h>
37
#include <linux/slab.h>
38
#include <linux/rcupdate.h>
39 40

#include <asm/pgtable.h>
41
#include <linux/uaccess.h>
42 43 44 45 46
#include <asm/io.h>
#include <asm/processor.h>
#include <asm/platform.h>
#include <asm/mmu.h>
#include <asm/irq.h>
A
Arun Sharma 已提交
47
#include <linux/atomic.h>
48
#include <asm/asm-offsets.h>
49
#include <asm/regs.h>
50
#include <asm/hw_breakpoint.h>
51 52

extern void ret_from_fork(void);
53
extern void ret_from_kernel_thread(void);
54 55 56

struct task_struct *current_set[NR_CPUS] = {&init_task, };

A
Adrian Bunk 已提交
57 58 59
void (*pm_power_off)(void) = NULL;
EXPORT_SYMBOL(pm_power_off);

60

61
#ifdef CONFIG_STACKPROTECTOR
M
Max Filippov 已提交
62 63 64 65 66
#include <linux/stackprotector.h>
unsigned long __stack_chk_guard __read_mostly;
EXPORT_SYMBOL(__stack_chk_guard);
#endif

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#if XTENSA_HAVE_COPROCESSORS

void coprocessor_release_all(struct thread_info *ti)
{
	unsigned long cpenable;
	int i;

	/* Make sure we don't switch tasks during this operation. */

	preempt_disable();

	/* Walk through all cp owners and release it for the requested one. */

	cpenable = ti->cpenable;

	for (i = 0; i < XCHAL_CP_MAX; i++) {
		if (coprocessor_owner[i] == ti) {
			coprocessor_owner[i] = 0;
			cpenable &= ~(1 << i);
		}
	}

	ti->cpenable = cpenable;
	coprocessor_clear_cpenable();

	preempt_enable();
}

void coprocessor_flush_all(struct thread_info *ti)
{
	unsigned long cpenable;
	int i;

	preempt_disable();

	cpenable = ti->cpenable;

	for (i = 0; i < XCHAL_CP_MAX; i++) {
		if ((cpenable & 1) != 0 && coprocessor_owner[i] == ti)
			coprocessor_flush(ti, i);
		cpenable >>= 1;
	}

	preempt_enable();
}

#endif


116 117 118
/*
 * Powermanagement idle function, if any is provided by the platform.
 */
T
Thomas Gleixner 已提交
119
void arch_cpu_idle(void)
120
{
T
Thomas Gleixner 已提交
121
	platform_idle();
122 123 124
}

/*
125
 * This is called when the thread calls exit().
126
 */
127
void exit_thread(struct task_struct *tsk)
128
{
129
#if XTENSA_HAVE_COPROCESSORS
130
	coprocessor_release_all(task_thread_info(tsk));
131
#endif
132 133
}

134 135 136 137
/*
 * Flush thread state. This is called when a thread does an execve()
 * Note that we flush coprocessor registers for the case execve fails.
 */
138 139
void flush_thread(void)
{
140 141 142 143 144
#if XTENSA_HAVE_COPROCESSORS
	struct thread_info *ti = current_thread_info();
	coprocessor_flush_all(ti);
	coprocessor_release_all(ti);
#endif
145
	flush_ptrace_hw_breakpoint(current);
146 147 148
}

/*
149 150
 * this gets called so that we can store coprocessor state into memory and
 * copy the current task into the new thread.
151
 */
152
int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
153 154
{
#if XTENSA_HAVE_COPROCESSORS
155
	coprocessor_flush_all(task_thread_info(src));
156
#endif
157 158
	*dst = *src;
	return 0;
159 160 161 162 163
}

/*
 * Copy thread.
 *
164 165 166 167 168 169 170 171 172 173
 * There are two modes in which this function is called:
 * 1) Userspace thread creation,
 *    regs != NULL, usp_thread_fn is userspace stack pointer.
 *    It is expected to copy parent regs (in case CLONE_VM is not set
 *    in the clone_flags) and set up passed usp in the childregs.
 * 2) Kernel thread creation,
 *    regs == NULL, usp_thread_fn is the function to run in the new thread
 *    and thread_fn_arg is its parameter.
 *    childregs are not used for the kernel threads.
 *
174 175
 * The stack layout for the new thread looks like this:
 *
176
 *	+------------------------+
177 178 179 180 181
 *	|       childregs        |
 *	+------------------------+ <- thread.sp = sp in dummy-frame
 *	|      dummy-frame       |    (saved in dummy-frame spill-area)
 *	+------------------------+
 *
182 183 184
 * We create a dummy frame to return to either ret_from_fork or
 *   ret_from_kernel_thread:
 *   a0 points to ret_from_fork/ret_from_kernel_thread (simulating a call4)
185
 *   sp points to itself (thread.sp)
186 187
 *   a2, a3 are unused for userspace threads,
 *   a2 points to thread_fn, a3 holds thread_fn arg for kernel threads.
188 189 190
 *
 * Note: This is a pristine frame, so we don't need any spill region on top of
 *       childregs.
191 192 193 194 195 196 197 198 199 200
 *
 * The fun part:  if we're keeping the same VM (i.e. cloning a thread,
 * not an entire process), we're normally given a new usp, and we CANNOT share
 * any live address register windows.  If we just copy those live frames over,
 * the two threads (parent and child) will overflow the same frames onto the
 * parent stack at different times, likely corrupting the parent stack (esp.
 * if the parent returns from functions that called clone() and calls new
 * ones, before the child overflows its now old copies of its parent windows).
 * One solution is to spill windows to the parent stack, but that's fairly
 * involved.  Much simpler to just not copy those live frames across.
201 202
 */

203
int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
204
		unsigned long thread_fn_arg, struct task_struct *p)
205
{
206
	struct pt_regs *childregs = task_pt_regs(p);
207

C
Chris Zankel 已提交
208 209 210 211
#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
	struct thread_info *ti;
#endif

212
	/* Create a call4 dummy-frame: a0 = 0, a1 = childregs. */
213 214
	SPILL_SLOT(childregs, 1) = (unsigned long)childregs;
	SPILL_SLOT(childregs, 0) = 0;
215 216

	p->thread.sp = (unsigned long)childregs;
217

218 219 220 221 222 223 224
	if (!(p->flags & PF_KTHREAD)) {
		struct pt_regs *regs = current_pt_regs();
		unsigned long usp = usp_thread_fn ?
			usp_thread_fn : regs->areg[1];

		p->thread.ra = MAKE_RA_FOR_CALL(
				(unsigned long)ret_from_fork, 0x1);
225

226 227 228 229 230
		/* This does not copy all the regs.
		 * In a bout of brilliance or madness,
		 * ARs beyond a0-a15 exist past the end of the struct.
		 */
		*childregs = *regs;
231
		childregs->areg[1] = usp;
232
		childregs->areg[2] = 0;
233 234 235 236 237 238 239 240 241 242 243 244 245 246

		/* When sharing memory with the parent thread, the child
		   usually starts on a pristine stack, so we have to reset
		   windowbase, windowstart and wmask.
		   (Note that such a new thread is required to always create
		   an initial call4 frame)
		   The exception is vfork, where the new thread continues to
		   run on the parent's stack until it calls execve. This could
		   be a call8 or call12, which requires a legal stack frame
		   of the previous caller for the overflow handlers to work.
		   (Note that it's always legal to overflow live registers).
		   In this case, ensure to spill at least the stack pointer
		   of that frame. */

247
		if (clone_flags & CLONE_VM) {
248 249 250 251 252 253 254 255 256 257 258
			/* check that caller window is live and same stack */
			int len = childregs->wmask & ~0xf;
			if (regs->areg[1] == usp && len != 0) {
				int callinc = (regs->areg[0] >> 30) & 3;
				int caller_ars = XCHAL_NUM_AREGS - callinc * 4;
				put_user(regs->areg[caller_ars+1],
					 (unsigned __user*)(usp - 12));
			}
			childregs->wmask = 1;
			childregs->windowstart = 1;
			childregs->windowbase = 0;
259 260 261 262 263
		} else {
			int len = childregs->wmask & ~0xf;
			memcpy(&childregs->areg[XCHAL_NUM_AREGS - len/4],
			       &regs->areg[XCHAL_NUM_AREGS - len/4], len);
		}
C
Chris Zankel 已提交
264 265

		/* The thread pointer is passed in the '4th argument' (= a5) */
266
		if (clone_flags & CLONE_SETTLS)
C
Chris Zankel 已提交
267
			childregs->threadptr = childregs->areg[5];
268
	} else {
269 270 271 272 273 274
		p->thread.ra = MAKE_RA_FOR_CALL(
				(unsigned long)ret_from_kernel_thread, 1);

		/* pass parameters to ret_from_kernel_thread:
		 * a2 = thread_fn, a3 = thread_fn arg
		 */
275 276
		SPILL_SLOT(childregs, 3) = thread_fn_arg;
		SPILL_SLOT(childregs, 2) = usp_thread_fn;
277 278 279 280

		/* Childregs are only used when we're going to userspace
		 * in which case start_thread will set them up.
		 */
281
	}
282 283 284 285 286 287

#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
	ti = task_thread_info(p);
	ti->cpenable = 0;
#endif

288 289
	clear_ptrace_hw_breakpoint(p);

290 291 292 293 294 295 296 297 298 299 300
	return 0;
}


/*
 * These bracket the sleeping functions..
 */

unsigned long get_wchan(struct task_struct *p)
{
	unsigned long sp, pc;
301
	unsigned long stack_page = (unsigned long) task_stack_page(p);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	int count = 0;

	if (!p || p == current || p->state == TASK_RUNNING)
		return 0;

	sp = p->thread.sp;
	pc = MAKE_PC_FROM_RA(p->thread.ra, p->thread.sp);

	do {
		if (sp < stack_page + sizeof(struct task_struct) ||
		    sp >= (stack_page + THREAD_SIZE) ||
		    pc == 0)
			return 0;
		if (!in_sched_functions(pc))
			return pc;

		/* Stack layout: sp-4: ra, sp-3: sp' */

		pc = MAKE_PC_FROM_RA(*(unsigned long*)sp - 4, sp);
		sp = *(unsigned long *)sp - 3;
	} while (count++ < 16);
	return 0;
}

/*
 * xtensa_gregset_t and 'struct pt_regs' are vastly different formats
 * of processor registers.  Besides different ordering,
 * xtensa_gregset_t contains non-live register information that
 * 'struct pt_regs' does not.  Exception handling (primarily) uses
 * 'struct pt_regs'.  Core files and ptrace use xtensa_gregset_t.
 *
 */

335
void xtensa_elf_core_copy_regs (xtensa_gregset_t *elfregs, struct pt_regs *regs)
336
{
337 338 339 340 341 342 343 344 345 346
	unsigned long wb, ws, wm;
	int live, last;

	wb = regs->windowbase;
	ws = regs->windowstart;
	wm = regs->wmask;
	ws = ((ws >> wb) | (ws << (WSBITS - wb))) & ((1 << WSBITS) - 1);

	/* Don't leak any random bits. */

A
Alan Cox 已提交
347
	memset(elfregs, 0, sizeof(*elfregs));
348

349 350 351 352 353
	/* Note:  PS.EXCM is not set while user task is running; its
	 * being set in regs->ps is for exception handling convenience.
	 */

	elfregs->pc		= regs->pc;
354
	elfregs->ps		= (regs->ps & ~(1 << PS_EXCM_BIT));
355 356 357 358
	elfregs->lbeg		= regs->lbeg;
	elfregs->lend		= regs->lend;
	elfregs->lcount		= regs->lcount;
	elfregs->sar		= regs->sar;
359
	elfregs->windowstart	= ws;
360

361 362 363 364
	live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
	last = XCHAL_NUM_AREGS - (wm >> 4) * 4;
	memcpy(elfregs->a, regs->areg, live * 4);
	memcpy(elfregs->a + last, regs->areg + last, (wm >> 4) * 16);
365 366
}

367
int dump_fpu(void)
368 369 370
{
	return 0;
}