Skip to content

Commit c34501d

Browse files
committed
arm64: Use generic kernel_thread() implementation
This patch enables CONFIG_GENERIC_KERNEL_THREAD on arm64, changes copy_threads to cope with kernel threads creation and adapts ret_from_fork accordingly. The arm64-specific kernel_thread implementation is no longer needed. Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent ddffeb8 commit c34501d

4 files changed

Lines changed: 29 additions & 59 deletions

File tree

‎arch/arm64/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ config ARM64
66
select GENERIC_IOMAP
77
select GENERIC_IRQ_PROBE
88
select GENERIC_IRQ_SHOW
9+
select GENERIC_KERNEL_THREAD
910
select GENERIC_SMP_IDLE_THREAD
1011
select GENERIC_TIME_VSYSCALL
1112
select HARDIRQS_SW_RESEND

‎arch/arm64/include/asm/processor.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,6 @@ unsigned long get_wchan(struct task_struct *p);
136136
extern struct task_struct *cpu_switch_to(struct task_struct *prev,
137137
struct task_struct *next);
138138

139-
/*
140-
* Create a new kernel thread
141-
*/
142-
extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
143-
144139
#define task_pt_regs(p) \
145140
((struct pt_regs *)(THREAD_START_SP + task_stack_page(p)) - 1)
146141

‎arch/arm64/kernel/entry.S‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,10 @@ ENDPROC(ret_to_user)
611611
*/
612612
ENTRY(ret_from_fork)
613613
bl schedule_tail
614-
get_thread_info tsk
614+
cbz x19, 1f // not a kernel thread
615+
mov x0, x20
616+
blr x19
617+
1: get_thread_info tsk
615618
b ret_to_user
616619
ENDPROC(ret_from_fork)
617620

‎arch/arm64/kernel/process.c‎

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,35 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,
240240
struct pt_regs *childregs = task_pt_regs(p);
241241
unsigned long tls = p->thread.tp_value;
242242

243-
*childregs = *regs;
244-
childregs->regs[0] = 0;
243+
memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
245244

246-
if (is_compat_thread(task_thread_info(p)))
247-
childregs->compat_sp = stack_start;
248-
else {
245+
if (likely(regs)) {
246+
*childregs = *regs;
247+
childregs->regs[0] = 0;
248+
if (is_compat_thread(task_thread_info(p))) {
249+
childregs->compat_sp = stack_start;
250+
} else {
251+
/*
252+
* Read the current TLS pointer from tpidr_el0 as it may be
253+
* out-of-sync with the saved value.
254+
*/
255+
asm("mrs %0, tpidr_el0" : "=r" (tls));
256+
childregs->sp = stack_start;
257+
}
249258
/*
250-
* Read the current TLS pointer from tpidr_el0 as it may be
251-
* out-of-sync with the saved value.
259+
* If a TLS pointer was passed to clone (4th argument), use it
260+
* for the new thread.
252261
*/
253-
asm("mrs %0, tpidr_el0" : "=r" (tls));
254-
childregs->sp = stack_start;
262+
if (clone_flags & CLONE_SETTLS)
263+
tls = regs->regs[3];
264+
} else {
265+
memset(childregs, 0, sizeof(struct pt_regs));
266+
childregs->pstate = PSR_MODE_EL1h;
267+
p->thread.cpu_context.x19 = stack_start;
268+
p->thread.cpu_context.x20 = stk_sz;
255269
}
256-
257-
memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
258-
p->thread.cpu_context.sp = (unsigned long)childregs;
259270
p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
260-
261-
/* If a TLS pointer was passed to clone, use that for the new thread. */
262-
if (clone_flags & CLONE_SETTLS)
263-
tls = regs->regs[3];
271+
p->thread.cpu_context.sp = (unsigned long)childregs;
264272
p->thread.tp_value = tls;
265273

266274
ptrace_hw_copy_thread(p);
@@ -327,43 +335,6 @@ int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
327335
}
328336
EXPORT_SYMBOL(dump_fpu);
329337

330-
/*
331-
* Shuffle the argument into the correct register before calling the
332-
* thread function. x1 is the thread argument, x2 is the pointer to
333-
* the thread function, and x3 points to the exit function.
334-
*/
335-
extern void kernel_thread_helper(void);
336-
asm( ".section .text\n"
337-
" .align\n"
338-
" .type kernel_thread_helper, #function\n"
339-
"kernel_thread_helper:\n"
340-
" mov x0, x1\n"
341-
" mov x30, x3\n"
342-
" br x2\n"
343-
" .size kernel_thread_helper, . - kernel_thread_helper\n"
344-
" .previous");
345-
346-
#define kernel_thread_exit do_exit
347-
348-
/*
349-
* Create a kernel thread.
350-
*/
351-
pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
352-
{
353-
struct pt_regs regs;
354-
355-
memset(&regs, 0, sizeof(regs));
356-
357-
regs.regs[1] = (unsigned long)arg;
358-
regs.regs[2] = (unsigned long)fn;
359-
regs.regs[3] = (unsigned long)kernel_thread_exit;
360-
regs.pc = (unsigned long)kernel_thread_helper;
361-
regs.pstate = PSR_MODE_EL1h;
362-
363-
return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
364-
}
365-
EXPORT_SYMBOL(kernel_thread);
366-
367338
unsigned long get_wchan(struct task_struct *p)
368339
{
369340
struct stackframe frame;

0 commit comments

Comments
 (0)