xref: /linux/arch/hexagon/kernel/process.c (revision d90d90a1978af6530c7d8b201c4ab117d0506b1a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Process creation support for Hexagon
4  *
5  * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
6  */
7 
8 #include <linux/sched.h>
9 #include <linux/sched/debug.h>
10 #include <linux/sched/task.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/tick.h>
15 #include <linux/uaccess.h>
16 #include <linux/slab.h>
17 #include <linux/resume_user_mode.h>
18 
19 /*
20  * Program thread launch.  Often defined as a macro in processor.h,
21  * but we're shooting for a small footprint and it's not an inner-loop
22  * performance-critical operation.
23  *
24  * The Hexagon ABI specifies that R28 is zero'ed before program launch,
25  * so that gets automatically done here.  If we ever stop doing that here,
26  * we'll probably want to define the ELF_PLAT_INIT macro.
27  */
28 void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
29 {
30 	/* We want to zero all data-containing registers. Is this overkill? */
31 	memset(regs, 0, sizeof(*regs));
32 	/* We might want to also zero all Processor registers here */
33 	pt_set_usermode(regs);
34 	pt_set_elr(regs, pc);
35 	pt_set_rte_sp(regs, sp);
36 }
37 
38 /*
39  *  Spin, or better still, do a hardware or VM wait instruction
40  *  If hardware or VM offer wait termination even though interrupts
41  *  are disabled.
42  */
43 void arch_cpu_idle(void)
44 {
45 	__vmwait();
46 	/*  interrupts wake us up, but irqs are still disabled */
47 }
48 
49 /*
50  * Copy architecture-specific thread state
51  */
52 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
53 {
54 	unsigned long clone_flags = args->flags;
55 	unsigned long usp = args->stack;
56 	unsigned long tls = args->tls;
57 	struct thread_info *ti = task_thread_info(p);
58 	struct hexagon_switch_stack *ss;
59 	struct pt_regs *childregs;
60 	asmlinkage void ret_from_fork(void);
61 
62 	childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
63 					sizeof(*childregs));
64 
65 	ti->regs = childregs;
66 
67 	/*
68 	 * Establish kernel stack pointer and initial PC for new thread
69 	 * Note that unlike the usual situation, we do not copy the
70 	 * parent's callee-saved here; those are in pt_regs and whatever
71 	 * we leave here will be overridden on return to userland.
72 	 */
73 	ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
74 						    sizeof(*ss));
75 	ss->lr = (unsigned long)ret_from_fork;
76 	p->thread.switch_sp = ss;
77 	if (unlikely(args->fn)) {
78 		memset(childregs, 0, sizeof(struct pt_regs));
79 		/* r24 <- fn, r25 <- arg */
80 		ss->r24 = (unsigned long)args->fn;
81 		ss->r25 = (unsigned long)args->fn_arg;
82 		pt_set_kmode(childregs);
83 		return 0;
84 	}
85 	memcpy(childregs, current_pt_regs(), sizeof(*childregs));
86 	ss->r2524 = 0;
87 
88 	if (usp)
89 		pt_set_rte_sp(childregs, usp);
90 
91 	/* Child sees zero return value */
92 	childregs->r00 = 0;
93 
94 	/*
95 	 * The clone syscall has the C signature:
96 	 * int [r0] clone(int flags [r0],
97 	 *           void *child_frame [r1],
98 	 *           void *parent_tid [r2],
99 	 *           void *child_tid [r3],
100 	 *           void *thread_control_block [r4]);
101 	 * ugp is used to provide TLS support.
102 	 */
103 	if (clone_flags & CLONE_SETTLS)
104 		childregs->ugp = tls;
105 
106 	/*
107 	 * Parent sees new pid -- not necessary, not even possible at
108 	 * this point in the fork process
109 	 */
110 
111 	return 0;
112 }
113 
114 /*
115  * Some archs flush debug and FPU info here
116  */
117 void flush_thread(void)
118 {
119 }
120 
121 /*
122  * The "wait channel" terminology is archaic, but what we want
123  * is an identification of the point at which the scheduler
124  * was invoked by a blocked thread.
125  */
126 unsigned long __get_wchan(struct task_struct *p)
127 {
128 	unsigned long fp, pc;
129 	unsigned long stack_page;
130 	int count = 0;
131 
132 	stack_page = (unsigned long)task_stack_page(p);
133 	fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
134 	do {
135 		if (fp < (stack_page + sizeof(struct thread_info)) ||
136 			fp >= (THREAD_SIZE - 8 + stack_page))
137 			return 0;
138 		pc = ((unsigned long *)fp)[1];
139 		if (!in_sched_functions(pc))
140 			return pc;
141 		fp = *(unsigned long *) fp;
142 	} while (count++ < 16);
143 
144 	return 0;
145 }
146 
147 /*
148  * Called on the exit path of event entry; see vm_entry.S
149  *
150  * Interrupts will already be disabled.
151  *
152  * Returns 0 if there's no need to re-check for more work.
153  */
154 
155 int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
156 {
157 	if (!(thread_info_flags & _TIF_WORK_MASK)) {
158 		return 0;
159 	}  /* shortcut -- no work to be done */
160 
161 	local_irq_enable();
162 
163 	if (thread_info_flags & _TIF_NEED_RESCHED) {
164 		schedule();
165 		return 1;
166 	}
167 
168 	if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
169 		do_signal(regs);
170 		return 1;
171 	}
172 
173 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
174 		resume_user_mode_work(regs);
175 		return 1;
176 	}
177 
178 	/* Should not even reach here */
179 	panic("%s: bad thread_info flags 0x%08x\n", __func__,
180 		thread_info_flags);
181 }
182