2 * Process creation support for Hexagon
4 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <linux/sched.h>
22 #include <linux/types.h>
23 #include <linux/module.h>
24 #include <linux/tick.h>
25 #include <linux/uaccess.h>
26 #include <linux/slab.h>
29 * Kernel thread creation. The desired kernel function is "wrapped"
30 * in the kernel_thread_helper function, which does cleanup
33 static void __noreturn
kernel_thread_helper(void *arg
, int (*fn
)(void *))
38 int kernel_thread(int (*fn
)(void *), void *arg
, unsigned long flags
)
42 memset(®s
, 0, sizeof(regs
));
44 * Yes, we're exploting illicit knowledge of the ABI here.
46 regs
.r00
= (unsigned long) arg
;
47 regs
.r01
= (unsigned long) fn
;
48 pt_set_elr(®s
, (unsigned long)kernel_thread_helper
);
51 return do_fork(flags
|CLONE_VM
|CLONE_UNTRACED
, 0, ®s
, 0, NULL
, NULL
);
53 EXPORT_SYMBOL(kernel_thread
);
56 * Program thread launch. Often defined as a macro in processor.h,
57 * but we're shooting for a small footprint and it's not an inner-loop
58 * performance-critical operation.
60 * The Hexagon ABI specifies that R28 is zero'ed before program launch,
61 * so that gets automatically done here. If we ever stop doing that here,
62 * we'll probably want to define the ELF_PLAT_INIT macro.
64 void start_thread(struct pt_regs
*regs
, unsigned long pc
, unsigned long sp
)
66 /* Set to run with user-mode data segmentation */
68 /* We want to zero all data-containing registers. Is this overkill? */
69 memset(regs
, 0, sizeof(*regs
));
70 /* We might want to also zero all Processor registers here */
71 pt_set_usermode(regs
);
73 pt_set_rte_sp(regs
, sp
);
77 * Spin, or better still, do a hardware or VM wait instruction
78 * If hardware or VM offer wait termination even though interrupts
81 static void default_idle(void)
86 void (*idle_sleep
)(void) = default_idle
;
91 tick_nohz_stop_sched_tick(1);
93 while (!need_resched()) {
95 /* interrupts wake us up, but aren't serviced */
96 local_irq_enable(); /* service interrupt */
100 tick_nohz_restart_sched_tick();
106 * Return saved PC of a blocked thread
108 unsigned long thread_saved_pc(struct task_struct
*tsk
)
114 * Copy architecture-specific thread state
116 int copy_thread(unsigned long clone_flags
, unsigned long usp
,
117 unsigned long unused
, struct task_struct
*p
,
118 struct pt_regs
*regs
)
120 struct thread_info
*ti
= task_thread_info(p
);
121 struct hexagon_switch_stack
*ss
;
122 struct pt_regs
*childregs
;
123 asmlinkage
void ret_from_fork(void);
125 childregs
= (struct pt_regs
*) (((unsigned long) ti
+ THREAD_SIZE
) -
128 memcpy(childregs
, regs
, sizeof(*childregs
));
129 ti
->regs
= childregs
;
132 * Establish kernel stack pointer and initial PC for new thread
134 ss
= (struct hexagon_switch_stack
*) ((unsigned long) childregs
-
136 ss
->lr
= (unsigned long)ret_from_fork
;
137 p
->thread
.switch_sp
= ss
;
139 /* If User mode thread, set pt_reg stack pointer as per parameter */
140 if (user_mode(childregs
)) {
141 pt_set_rte_sp(childregs
, usp
);
143 /* Child sees zero return value */
147 * The clone syscall has the C signature:
148 * int [r0] clone(int flags [r0],
149 * void *child_frame [r1],
150 * void *parent_tid [r2],
151 * void *child_tid [r3],
152 * void *thread_control_block [r4]);
153 * ugp is used to provide TLS support.
155 if (clone_flags
& CLONE_SETTLS
)
156 childregs
->ugp
= childregs
->r04
;
159 * Parent sees new pid -- not necessary, not even possible at
160 * this point in the fork process
161 * Might also want to set things like ti->addr_limit
165 * If kernel thread, resume stack is kernel stack base.
166 * Note that this is pointer arithmetic on pt_regs *
168 pt_set_rte_sp(childregs
, (unsigned long)(childregs
+ 1));
170 * We need the current thread_info fast path pointer
171 * set up in pt_regs. The register to be used is
172 * parametric for assembler code, but the mechanism
173 * doesn't drop neatly into C. Needs to be fixed.
175 childregs
->THREADINFO_REG
= (unsigned long) ti
;
179 * thread_info pointer is pulled out of task_struct "stack"
180 * field on switch_to.
182 p
->stack
= (void *)ti
;
188 * Release any architecture-specific resources locked by thread
190 void release_thread(struct task_struct
*dead_task
)
195 * Free any architecture-specific thread data structures, etc.
197 void exit_thread(void)
202 * Some archs flush debug and FPU info here
204 void flush_thread(void)
209 * The "wait channel" terminology is archaic, but what we want
210 * is an identification of the point at which the scheduler
211 * was invoked by a blocked thread.
213 unsigned long get_wchan(struct task_struct
*p
)
215 unsigned long fp
, pc
;
216 unsigned long stack_page
;
218 if (!p
|| p
== current
|| p
->state
== TASK_RUNNING
)
221 stack_page
= (unsigned long)task_stack_page(p
);
222 fp
= ((struct hexagon_switch_stack
*)p
->thread
.switch_sp
)->fp
;
224 if (fp
< (stack_page
+ sizeof(struct thread_info
)) ||
225 fp
>= (THREAD_SIZE
- 8 + stack_page
))
227 pc
= ((unsigned long *)fp
)[1];
228 if (!in_sched_functions(pc
))
230 fp
= *(unsigned long *) fp
;
231 } while (count
++ < 16);
237 * Borrowed from PowerPC -- basically allow smaller kernel stacks if we
238 * go crazy with the page sizes.
240 #if THREAD_SHIFT < PAGE_SHIFT
242 static struct kmem_cache
*thread_info_cache
;
244 struct thread_info
*alloc_thread_info_node(struct task_struct
*tsk
, int node
)
246 struct thread_info
*ti
;
248 ti
= kmem_cache_alloc_node(thread_info_cache
, GFP_KERNEL
, node
);
249 if (unlikely(ti
== NULL
))
251 #ifdef CONFIG_DEBUG_STACK_USAGE
252 memset(ti
, 0, THREAD_SIZE
);
257 void free_thread_info(struct thread_info
*ti
)
259 kmem_cache_free(thread_info_cache
, ti
);
262 /* Weak symbol; called by init/main.c */
264 void thread_info_cache_init(void)
266 thread_info_cache
= kmem_cache_create("thread_info", THREAD_SIZE
,
267 THREAD_SIZE
, 0, NULL
);
268 BUG_ON(thread_info_cache
== NULL
);
271 #endif /* THREAD_SHIFT < PAGE_SHIFT */
274 * Required placeholder.
276 int dump_fpu(struct pt_regs
*regs
, elf_fpregset_t
*fpu
)