f1c1f6f21941dd67ec8d3027aec70eedf161e925
[linux-2.6-microblaze.git] / arch / hexagon / kernel / process.c
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         raw_local_irq_enable();
48 }
49
50 /*
51  * Copy architecture-specific thread state
52  */
53 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
54 {
55         unsigned long clone_flags = args->flags;
56         unsigned long usp = args->stack;
57         unsigned long arg = args->stack_size;
58         unsigned long tls = args->tls;
59         struct thread_info *ti = task_thread_info(p);
60         struct hexagon_switch_stack *ss;
61         struct pt_regs *childregs;
62         asmlinkage void ret_from_fork(void);
63
64         childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
65                                         sizeof(*childregs));
66
67         ti->regs = childregs;
68
69         /*
70          * Establish kernel stack pointer and initial PC for new thread
71          * Note that unlike the usual situation, we do not copy the
72          * parent's callee-saved here; those are in pt_regs and whatever
73          * we leave here will be overridden on return to userland.
74          */
75         ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
76                                                     sizeof(*ss));
77         ss->lr = (unsigned long)ret_from_fork;
78         p->thread.switch_sp = ss;
79         if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
80                 memset(childregs, 0, sizeof(struct pt_regs));
81                 /* r24 <- fn, r25 <- arg */
82                 ss->r24 = usp;
83                 ss->r25 = arg;
84                 pt_set_kmode(childregs);
85                 return 0;
86         }
87         memcpy(childregs, current_pt_regs(), sizeof(*childregs));
88         ss->r2524 = 0;
89
90         if (usp)
91                 pt_set_rte_sp(childregs, usp);
92
93         /* Child sees zero return value */
94         childregs->r00 = 0;
95
96         /*
97          * The clone syscall has the C signature:
98          * int [r0] clone(int flags [r0],
99          *           void *child_frame [r1],
100          *           void *parent_tid [r2],
101          *           void *child_tid [r3],
102          *           void *thread_control_block [r4]);
103          * ugp is used to provide TLS support.
104          */
105         if (clone_flags & CLONE_SETTLS)
106                 childregs->ugp = tls;
107
108         /*
109          * Parent sees new pid -- not necessary, not even possible at
110          * this point in the fork process
111          */
112
113         return 0;
114 }
115
116 /*
117  * Release any architecture-specific resources locked by thread
118  */
119 void release_thread(struct task_struct *dead_task)
120 {
121 }
122
123 /*
124  * Some archs flush debug and FPU info here
125  */
126 void flush_thread(void)
127 {
128 }
129
130 /*
131  * The "wait channel" terminology is archaic, but what we want
132  * is an identification of the point at which the scheduler
133  * was invoked by a blocked thread.
134  */
135 unsigned long __get_wchan(struct task_struct *p)
136 {
137         unsigned long fp, pc;
138         unsigned long stack_page;
139         int count = 0;
140
141         stack_page = (unsigned long)task_stack_page(p);
142         fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
143         do {
144                 if (fp < (stack_page + sizeof(struct thread_info)) ||
145                         fp >= (THREAD_SIZE - 8 + stack_page))
146                         return 0;
147                 pc = ((unsigned long *)fp)[1];
148                 if (!in_sched_functions(pc))
149                         return pc;
150                 fp = *(unsigned long *) fp;
151         } while (count++ < 16);
152
153         return 0;
154 }
155
156 /*
157  * Called on the exit path of event entry; see vm_entry.S
158  *
159  * Interrupts will already be disabled.
160  *
161  * Returns 0 if there's no need to re-check for more work.
162  */
163
164 int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
165 {
166         if (!(thread_info_flags & _TIF_WORK_MASK)) {
167                 return 0;
168         }  /* shortcut -- no work to be done */
169
170         local_irq_enable();
171
172         if (thread_info_flags & _TIF_NEED_RESCHED) {
173                 schedule();
174                 return 1;
175         }
176
177         if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
178                 do_signal(regs);
179                 return 1;
180         }
181
182         if (thread_info_flags & _TIF_NOTIFY_RESUME) {
183                 resume_user_mode_work(regs);
184                 return 1;
185         }
186
187         /* Should not even reach here */
188         panic("%s: bad thread_info flags 0x%08x\n", __func__,
189                 thread_info_flags);
190 }