12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
288ef7f3fSJonas Bonn /*
388ef7f3fSJonas Bonn * OpenRISC process.c
488ef7f3fSJonas Bonn *
588ef7f3fSJonas Bonn * Linux architectural port borrowing liberally from similar works of
688ef7f3fSJonas Bonn * others. All original copyrights apply as per the original source
788ef7f3fSJonas Bonn * declaration.
888ef7f3fSJonas Bonn *
988ef7f3fSJonas Bonn * Modifications for the OpenRISC architecture:
1088ef7f3fSJonas Bonn * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
1188ef7f3fSJonas Bonn * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
1288ef7f3fSJonas Bonn *
1388ef7f3fSJonas Bonn * This file handles the architecture-dependent parts of process handling...
1488ef7f3fSJonas Bonn */
1588ef7f3fSJonas Bonn
1688ef7f3fSJonas Bonn #define __KERNEL_SYSCALLS__
17136a2d89SStafford Horne #include <linux/cpu.h>
1888ef7f3fSJonas Bonn #include <linux/errno.h>
1988ef7f3fSJonas Bonn #include <linux/sched.h>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
2129930025SIngo Molnar #include <linux/sched/task.h>
2268db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
2388ef7f3fSJonas Bonn #include <linux/kernel.h>
241938852dSStafford Horne #include <linux/export.h>
2588ef7f3fSJonas Bonn #include <linux/mm.h>
2688ef7f3fSJonas Bonn #include <linux/stddef.h>
2788ef7f3fSJonas Bonn #include <linux/unistd.h>
2888ef7f3fSJonas Bonn #include <linux/ptrace.h>
2988ef7f3fSJonas Bonn #include <linux/slab.h>
3088ef7f3fSJonas Bonn #include <linux/elfcore.h>
3188ef7f3fSJonas Bonn #include <linux/interrupt.h>
3288ef7f3fSJonas Bonn #include <linux/delay.h>
3388ef7f3fSJonas Bonn #include <linux/init_task.h>
3488ef7f3fSJonas Bonn #include <linux/mqueue.h>
3588ef7f3fSJonas Bonn #include <linux/fs.h>
36131172a4SJoel Stanley #include <linux/reboot.h>
3788ef7f3fSJonas Bonn
387c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
39*4dc70e1aSStafford Horne #include <asm/fpu.h>
4088ef7f3fSJonas Bonn #include <asm/io.h>
4188ef7f3fSJonas Bonn #include <asm/processor.h>
4288ef7f3fSJonas Bonn #include <asm/spr_defs.h>
43136a2d89SStafford Horne #include <asm/switch_to.h>
4488ef7f3fSJonas Bonn
4588ef7f3fSJonas Bonn #include <linux/smp.h>
4688ef7f3fSJonas Bonn
4788ef7f3fSJonas Bonn /*
4888ef7f3fSJonas Bonn * Pointer to Current thread info structure.
4988ef7f3fSJonas Bonn *
5088ef7f3fSJonas Bonn * Used at user space -> kernel transitions.
5188ef7f3fSJonas Bonn */
5288ef7f3fSJonas Bonn struct thread_info *current_thread_info_set[NR_CPUS] = { &init_thread_info, };
5388ef7f3fSJonas Bonn
machine_restart(char * cmd)54131172a4SJoel Stanley void machine_restart(char *cmd)
5588ef7f3fSJonas Bonn {
56131172a4SJoel Stanley do_kernel_restart(cmd);
57131172a4SJoel Stanley
58635267b7SJason A. Donenfeld __asm__("l.nop 13");
59635267b7SJason A. Donenfeld
60131172a4SJoel Stanley /* Give a grace period for failure to restart of 1s */
61131172a4SJoel Stanley mdelay(1000);
62131172a4SJoel Stanley
63131172a4SJoel Stanley /* Whoops - the platform was unable to reboot. Tell the user! */
64131172a4SJoel Stanley pr_emerg("Reboot failed -- System halted\n");
65131172a4SJoel Stanley while (1);
6688ef7f3fSJonas Bonn }
6788ef7f3fSJonas Bonn
6888ef7f3fSJonas Bonn /*
69c94195a3SStafford Horne * This is used if a sys-off handler was not set by a power management
7083da38d8SStafford Horne * driver, in this case we can assume we are on a simulator. On
7183da38d8SStafford Horne * OpenRISC simulators l.nop 1 will trigger the simulator exit.
7283da38d8SStafford Horne */
default_power_off(void)7383da38d8SStafford Horne static void default_power_off(void)
7483da38d8SStafford Horne {
7583da38d8SStafford Horne __asm__("l.nop 1");
7683da38d8SStafford Horne }
7783da38d8SStafford Horne
7883da38d8SStafford Horne /*
7988ef7f3fSJonas Bonn * Similar to machine_power_off, but don't shut off power. Add code
8088ef7f3fSJonas Bonn * here to freeze the system for e.g. post-mortem debug purpose when
8188ef7f3fSJonas Bonn * possible. This halt has nothing to do with the idle halt.
8288ef7f3fSJonas Bonn */
machine_halt(void)8388ef7f3fSJonas Bonn void machine_halt(void)
8488ef7f3fSJonas Bonn {
8588ef7f3fSJonas Bonn printk(KERN_INFO "*** MACHINE HALT ***\n");
8688ef7f3fSJonas Bonn __asm__("l.nop 1");
8788ef7f3fSJonas Bonn }
8888ef7f3fSJonas Bonn
8988ef7f3fSJonas Bonn /* If or when software power-off is implemented, add code here. */
machine_power_off(void)9088ef7f3fSJonas Bonn void machine_power_off(void)
9188ef7f3fSJonas Bonn {
9288ef7f3fSJonas Bonn printk(KERN_INFO "*** MACHINE POWER OFF ***\n");
93c94195a3SStafford Horne do_kernel_power_off();
9483da38d8SStafford Horne default_power_off();
9588ef7f3fSJonas Bonn }
9688ef7f3fSJonas Bonn
97e29d11c6SSebastian Macke /*
98e29d11c6SSebastian Macke * Send the doze signal to the cpu if available.
99e29d11c6SSebastian Macke * Make sure, that all interrupts are enabled
100e29d11c6SSebastian Macke */
arch_cpu_idle(void)101e29d11c6SSebastian Macke void arch_cpu_idle(void)
102e29d11c6SSebastian Macke {
10358c644baSPeter Zijlstra raw_local_irq_enable();
104e29d11c6SSebastian Macke if (mfspr(SPR_UPR) & SPR_UPR_PMP)
105e29d11c6SSebastian Macke mtspr(SPR_PMR, mfspr(SPR_PMR) | SPR_PMR_DME);
10689b30987SPeter Zijlstra raw_local_irq_disable();
107e29d11c6SSebastian Macke }
108e29d11c6SSebastian Macke
10983da38d8SStafford Horne void (*pm_power_off)(void) = NULL;
110363dad58SStafford Horne EXPORT_SYMBOL(pm_power_off);
11188ef7f3fSJonas Bonn
11288ef7f3fSJonas Bonn /*
11388ef7f3fSJonas Bonn * When a process does an "exec", machine state like FPU and debug
11488ef7f3fSJonas Bonn * registers need to be reset. This is a hook function for that.
11588ef7f3fSJonas Bonn * Currently we don't have any such state to reset, so this is empty.
11688ef7f3fSJonas Bonn */
flush_thread(void)11788ef7f3fSJonas Bonn void flush_thread(void)
11888ef7f3fSJonas Bonn {
11988ef7f3fSJonas Bonn }
12088ef7f3fSJonas Bonn
show_regs(struct pt_regs * regs)12188ef7f3fSJonas Bonn void show_regs(struct pt_regs *regs)
12288ef7f3fSJonas Bonn {
123a43cb95dSTejun Heo show_regs_print_info(KERN_DEFAULT);
12488ef7f3fSJonas Bonn /* __PHX__ cleanup this mess */
12588ef7f3fSJonas Bonn show_registers(regs);
12688ef7f3fSJonas Bonn }
12788ef7f3fSJonas Bonn
12888ef7f3fSJonas Bonn /*
12988ef7f3fSJonas Bonn * Copy the thread-specific (arch specific) info from the current
13088ef7f3fSJonas Bonn * process to the new one p
13188ef7f3fSJonas Bonn */
13288ef7f3fSJonas Bonn extern asmlinkage void ret_from_fork(void);
13388ef7f3fSJonas Bonn
134cbf23cf1SJonas Bonn /*
135714acdbdSChristian Brauner * copy_thread
136cbf23cf1SJonas Bonn * @clone_flags: flags
137cbf23cf1SJonas Bonn * @usp: user stack pointer or fn for kernel thread
138cbf23cf1SJonas Bonn * @arg: arg to fn for kernel thread; always NULL for userspace thread
139cbf23cf1SJonas Bonn * @p: the newly created task
1400d4e1bb1SStafford Horne * @tls: the Thread Local Storage pointer for the new process
141cbf23cf1SJonas Bonn *
142cbf23cf1SJonas Bonn * At the top of a newly initialized kernel stack are two stacked pt_reg
143cbf23cf1SJonas Bonn * structures. The first (topmost) is the userspace context of the thread.
144cbf23cf1SJonas Bonn * The second is the kernelspace context of the thread.
145cbf23cf1SJonas Bonn *
146cbf23cf1SJonas Bonn * A kernel thread will not be returning to userspace, so the topmost pt_regs
147cbf23cf1SJonas Bonn * struct can be uninitialized; it _does_ need to exist, though, because
148cbf23cf1SJonas Bonn * a kernel thread can become a userspace thread by doing a kernel_execve, in
149cbf23cf1SJonas Bonn * which case the topmost context will be initialized and used for 'returning'
150cbf23cf1SJonas Bonn * to userspace.
151cbf23cf1SJonas Bonn *
152cbf23cf1SJonas Bonn * The second pt_reg struct needs to be initialized to 'return' to
153cbf23cf1SJonas Bonn * ret_from_fork. A kernel thread will need to set r20 to the address of
154cbf23cf1SJonas Bonn * a function to call into (with arg in r22); userspace threads need to set
155cbf23cf1SJonas Bonn * r20 to NULL in which case ret_from_fork will just continue a return to
156cbf23cf1SJonas Bonn * userspace.
157cbf23cf1SJonas Bonn *
158cbf23cf1SJonas Bonn * A kernel thread 'fn' may return; this is effectively what happens when
159cbf23cf1SJonas Bonn * kernel_execve is called. In that case, the userspace pt_regs must have
160cbf23cf1SJonas Bonn * been initialized (which kernel_execve takes care of, see start_thread
161cbf23cf1SJonas Bonn * below); ret_from_fork will then continue its execution causing the
162cbf23cf1SJonas Bonn * 'kernel thread' to return to userspace as a userspace thread.
163cbf23cf1SJonas Bonn */
164cbf23cf1SJonas Bonn
16588ef7f3fSJonas Bonn int
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)166c5febea0SEric W. Biederman copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
16788ef7f3fSJonas Bonn {
168c5febea0SEric W. Biederman unsigned long clone_flags = args->flags;
169c5febea0SEric W. Biederman unsigned long usp = args->stack;
170c5febea0SEric W. Biederman unsigned long tls = args->tls;
171cbf23cf1SJonas Bonn struct pt_regs *userregs;
17288ef7f3fSJonas Bonn struct pt_regs *kregs;
17388ef7f3fSJonas Bonn unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
17488ef7f3fSJonas Bonn unsigned long top_of_kernel_stack;
17588ef7f3fSJonas Bonn
17688ef7f3fSJonas Bonn top_of_kernel_stack = sp;
17788ef7f3fSJonas Bonn
178cbf23cf1SJonas Bonn /* Locate userspace context on stack... */
179cbf23cf1SJonas Bonn sp -= STACK_FRAME_OVERHEAD; /* redzone */
18088ef7f3fSJonas Bonn sp -= sizeof(struct pt_regs);
181cbf23cf1SJonas Bonn userregs = (struct pt_regs *) sp;
18288ef7f3fSJonas Bonn
183cbf23cf1SJonas Bonn /* ...and kernel context */
184cbf23cf1SJonas Bonn sp -= STACK_FRAME_OVERHEAD; /* redzone */
18588ef7f3fSJonas Bonn sp -= sizeof(struct pt_regs);
18688ef7f3fSJonas Bonn kregs = (struct pt_regs *)sp;
18788ef7f3fSJonas Bonn
1885bd2e97cSEric W. Biederman if (unlikely(args->fn)) {
189cbf23cf1SJonas Bonn memset(kregs, 0, sizeof(struct pt_regs));
1905bd2e97cSEric W. Biederman kregs->gpr[20] = (unsigned long)args->fn;
1915bd2e97cSEric W. Biederman kregs->gpr[22] = (unsigned long)args->fn_arg;
192cbf23cf1SJonas Bonn } else {
19339d91a9eSAl Viro *userregs = *current_pt_regs();
19488ef7f3fSJonas Bonn
19539d91a9eSAl Viro if (usp)
196cbf23cf1SJonas Bonn userregs->sp = usp;
197e60aa2fbSChristian Svensson
198e60aa2fbSChristian Svensson /*
1990d4e1bb1SStafford Horne * For CLONE_SETTLS set "tp" (r10) to the TLS pointer.
200e60aa2fbSChristian Svensson */
201e60aa2fbSChristian Svensson if (clone_flags & CLONE_SETTLS)
2020d4e1bb1SStafford Horne userregs->gpr[10] = tls;
203e60aa2fbSChristian Svensson
204cbf23cf1SJonas Bonn userregs->gpr[11] = 0; /* Result from fork() */
205cbf23cf1SJonas Bonn
206cbf23cf1SJonas Bonn kregs->gpr[20] = 0; /* Userspace thread */
207cbf23cf1SJonas Bonn }
208cbf23cf1SJonas Bonn
209cbf23cf1SJonas Bonn /*
210cbf23cf1SJonas Bonn * _switch wants the kernel stack page in pt_regs->sp so that it
211cbf23cf1SJonas Bonn * can restore it to thread_info->ksp... see _switch for details.
21288ef7f3fSJonas Bonn */
21388ef7f3fSJonas Bonn kregs->sp = top_of_kernel_stack;
21488ef7f3fSJonas Bonn kregs->gpr[9] = (unsigned long)ret_from_fork;
21588ef7f3fSJonas Bonn
216cbf23cf1SJonas Bonn task_thread_info(p)->ksp = (unsigned long)kregs;
217cbf23cf1SJonas Bonn
21888ef7f3fSJonas Bonn return 0;
21988ef7f3fSJonas Bonn }
22088ef7f3fSJonas Bonn
22188ef7f3fSJonas Bonn /*
22288ef7f3fSJonas Bonn * Set up a thread for executing a new program
22388ef7f3fSJonas Bonn */
start_thread(struct pt_regs * regs,unsigned long pc,unsigned long sp)22488ef7f3fSJonas Bonn void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
22588ef7f3fSJonas Bonn {
226cbf23cf1SJonas Bonn unsigned long sr = mfspr(SPR_SR) & ~SPR_SR_SM;
22788ef7f3fSJonas Bonn
228cbf23cf1SJonas Bonn memset(regs, 0, sizeof(struct pt_regs));
22988ef7f3fSJonas Bonn
23088ef7f3fSJonas Bonn regs->pc = pc;
23188ef7f3fSJonas Bonn regs->sr = sr;
23288ef7f3fSJonas Bonn regs->sp = sp;
23388ef7f3fSJonas Bonn }
23488ef7f3fSJonas Bonn
23588ef7f3fSJonas Bonn extern struct thread_info *_switch(struct thread_info *old_ti,
23688ef7f3fSJonas Bonn struct thread_info *new_ti);
23763104c06SStefan Kristiansson extern int lwa_flag;
23888ef7f3fSJonas Bonn
__switch_to(struct task_struct * old,struct task_struct * new)23988ef7f3fSJonas Bonn struct task_struct *__switch_to(struct task_struct *old,
24088ef7f3fSJonas Bonn struct task_struct *new)
24188ef7f3fSJonas Bonn {
24288ef7f3fSJonas Bonn struct task_struct *last;
24388ef7f3fSJonas Bonn struct thread_info *new_ti, *old_ti;
24488ef7f3fSJonas Bonn unsigned long flags;
24588ef7f3fSJonas Bonn
24688ef7f3fSJonas Bonn local_irq_save(flags);
24788ef7f3fSJonas Bonn
248*4dc70e1aSStafford Horne save_fpu(current);
249*4dc70e1aSStafford Horne
25088ef7f3fSJonas Bonn /* current_set is an array of saved current pointers
25188ef7f3fSJonas Bonn * (one for each cpu). we need them at user->kernel transition,
25288ef7f3fSJonas Bonn * while we save them at kernel->user transition
25388ef7f3fSJonas Bonn */
25488ef7f3fSJonas Bonn new_ti = new->stack;
25588ef7f3fSJonas Bonn old_ti = old->stack;
25688ef7f3fSJonas Bonn
25763104c06SStefan Kristiansson lwa_flag = 0;
25863104c06SStefan Kristiansson
25988ef7f3fSJonas Bonn current_thread_info_set[smp_processor_id()] = new_ti;
26088ef7f3fSJonas Bonn last = (_switch(old_ti, new_ti))->task;
26188ef7f3fSJonas Bonn
262*4dc70e1aSStafford Horne restore_fpu(current);
263*4dc70e1aSStafford Horne
26488ef7f3fSJonas Bonn local_irq_restore(flags);
26588ef7f3fSJonas Bonn
26688ef7f3fSJonas Bonn return last;
26788ef7f3fSJonas Bonn }
26888ef7f3fSJonas Bonn
26988ef7f3fSJonas Bonn /*
27088ef7f3fSJonas Bonn * Write out registers in core dump format, as defined by the
27188ef7f3fSJonas Bonn * struct user_regs_struct
27288ef7f3fSJonas Bonn */
dump_elf_thread(elf_greg_t * dest,struct pt_regs * regs)27388ef7f3fSJonas Bonn void dump_elf_thread(elf_greg_t *dest, struct pt_regs* regs)
27488ef7f3fSJonas Bonn {
27588ef7f3fSJonas Bonn dest[0] = 0; /* r0 */
27688ef7f3fSJonas Bonn memcpy(dest+1, regs->gpr+1, 31*sizeof(unsigned long));
27788ef7f3fSJonas Bonn dest[32] = regs->pc;
27888ef7f3fSJonas Bonn dest[33] = regs->sr;
27988ef7f3fSJonas Bonn dest[34] = 0;
28088ef7f3fSJonas Bonn dest[35] = 0;
28188ef7f3fSJonas Bonn }
28288ef7f3fSJonas Bonn
__get_wchan(struct task_struct * p)28342a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
28488ef7f3fSJonas Bonn {
28588ef7f3fSJonas Bonn /* TODO */
28688ef7f3fSJonas Bonn
28788ef7f3fSJonas Bonn return 0;
28888ef7f3fSJonas Bonn }
289