2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-06-05 03:30:12 +08:00
|
|
|
/*
|
|
|
|
* OpenRISC process.c
|
|
|
|
*
|
|
|
|
* Linux architectural port borrowing liberally from similar works of
|
|
|
|
* others. All original copyrights apply as per the original source
|
|
|
|
* declaration.
|
|
|
|
*
|
|
|
|
* Modifications for the OpenRISC architecture:
|
|
|
|
* Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
|
|
|
|
* Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
|
|
|
|
*
|
|
|
|
* This file handles the architecture-dependent parts of process handling...
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define __KERNEL_SYSCALLS__
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
2017-02-09 01:51:35 +08:00
|
|
|
#include <linux/sched/debug.h>
|
2017-02-09 01:51:36 +08:00
|
|
|
#include <linux/sched/task.h>
|
2017-02-09 01:51:37 +08:00
|
|
|
#include <linux/sched/task_stack.h>
|
2011-06-05 03:30:12 +08:00
|
|
|
#include <linux/kernel.h>
|
2017-03-14 21:54:22 +08:00
|
|
|
#include <linux/export.h>
|
2011-06-05 03:30:12 +08:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/unistd.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/elfcore.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/init_task.h>
|
|
|
|
#include <linux/mqueue.h>
|
|
|
|
#include <linux/fs.h>
|
2020-12-27 17:14:46 +08:00
|
|
|
#include <linux/reboot.h>
|
2011-06-05 03:30:12 +08:00
|
|
|
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2011-06-05 03:30:12 +08:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/spr_defs.h>
|
|
|
|
|
|
|
|
#include <linux/smp.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pointer to Current thread info structure.
|
|
|
|
*
|
|
|
|
* Used at user space -> kernel transitions.
|
|
|
|
*/
|
|
|
|
struct thread_info *current_thread_info_set[NR_CPUS] = { &init_thread_info, };
|
|
|
|
|
2020-12-27 17:14:46 +08:00
|
|
|
void machine_restart(char *cmd)
|
2011-06-05 03:30:12 +08:00
|
|
|
{
|
2020-12-27 17:14:46 +08:00
|
|
|
do_kernel_restart(cmd);
|
|
|
|
|
2022-04-28 19:11:39 +08:00
|
|
|
__asm__("l.nop 13");
|
|
|
|
|
2020-12-27 17:14:46 +08:00
|
|
|
/* Give a grace period for failure to restart of 1s */
|
|
|
|
mdelay(1000);
|
|
|
|
|
|
|
|
/* Whoops - the platform was unable to reboot. Tell the user! */
|
|
|
|
pr_emerg("Reboot failed -- System halted\n");
|
|
|
|
while (1);
|
2011-06-05 03:30:12 +08:00
|
|
|
}
|
|
|
|
|
2022-05-21 09:39:30 +08:00
|
|
|
/*
|
|
|
|
* This is used if pm_power_off has not been set by a power management
|
|
|
|
* driver, in this case we can assume we are on a simulator. On
|
|
|
|
* OpenRISC simulators l.nop 1 will trigger the simulator exit.
|
|
|
|
*/
|
|
|
|
static void default_power_off(void)
|
|
|
|
{
|
|
|
|
__asm__("l.nop 1");
|
|
|
|
}
|
|
|
|
|
2011-06-05 03:30:12 +08:00
|
|
|
/*
|
|
|
|
* Similar to machine_power_off, but don't shut off power. Add code
|
|
|
|
* here to freeze the system for e.g. post-mortem debug purpose when
|
|
|
|
* possible. This halt has nothing to do with the idle halt.
|
|
|
|
*/
|
|
|
|
void machine_halt(void)
|
|
|
|
{
|
|
|
|
printk(KERN_INFO "*** MACHINE HALT ***\n");
|
|
|
|
__asm__("l.nop 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If or when software power-off is implemented, add code here. */
|
|
|
|
void machine_power_off(void)
|
|
|
|
{
|
|
|
|
printk(KERN_INFO "*** MACHINE POWER OFF ***\n");
|
2022-05-21 09:39:30 +08:00
|
|
|
if (pm_power_off != NULL)
|
|
|
|
pm_power_off();
|
|
|
|
else
|
|
|
|
default_power_off();
|
2011-06-05 03:30:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-20 23:13:31 +08:00
|
|
|
/*
|
|
|
|
* Send the doze signal to the cpu if available.
|
|
|
|
* Make sure, that all interrupts are enabled
|
|
|
|
*/
|
|
|
|
void arch_cpu_idle(void)
|
|
|
|
{
|
2020-11-20 18:50:35 +08:00
|
|
|
raw_local_irq_enable();
|
2014-07-20 23:13:31 +08:00
|
|
|
if (mfspr(SPR_UPR) & SPR_UPR_PMP)
|
|
|
|
mtspr(SPR_PMR, mfspr(SPR_PMR) | SPR_PMR_DME);
|
|
|
|
}
|
|
|
|
|
2022-05-21 09:39:30 +08:00
|
|
|
void (*pm_power_off)(void) = NULL;
|
2017-03-14 21:52:49 +08:00
|
|
|
EXPORT_SYMBOL(pm_power_off);
|
2011-06-05 03:30:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When a process does an "exec", machine state like FPU and debug
|
|
|
|
* registers need to be reset. This is a hook function for that.
|
|
|
|
* Currently we don't have any such state to reset, so this is empty.
|
|
|
|
*/
|
|
|
|
void flush_thread(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_regs(struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
extern void show_registers(struct pt_regs *regs);
|
|
|
|
|
dump_stack: unify debug information printed by show_regs()
show_regs() is inherently arch-dependent but it does make sense to print
generic debug information and some archs already do albeit in slightly
different forms. This patch introduces a generic function to print debug
information from show_regs() so that different archs print out the same
information and it's much easier to modify what's printed.
show_regs_print_info() prints out the same debug info as dump_stack()
does plus task and thread_info pointers.
* Archs which didn't print debug info now do.
alpha, arc, blackfin, c6x, cris, frv, h8300, hexagon, ia64, m32r,
metag, microblaze, mn10300, openrisc, parisc, score, sh64, sparc,
um, xtensa
* Already prints debug info. Replaced with show_regs_print_info().
The printed information is superset of what used to be there.
arm, arm64, avr32, mips, powerpc, sh32, tile, unicore32, x86
* s390 is special in that it used to print arch-specific information
along with generic debug info. Heiko and Martin think that the
arch-specific extra isn't worth keeping s390 specfic implementation.
Converted to use the generic version.
Note that now all archs print the debug info before actual register
dumps.
An example BUG() dump follows.
kernel BUG at /work/os/work/kernel/workqueue.c:4841!
invalid opcode: 0000 [#1] PREEMPT SMP DEBUG_PAGEALLOC
Modules linked in:
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 3.9.0-rc1-work+ #7
Hardware name: empty empty/S3992, BIOS 080011 10/26/2007
task: ffff88007c85e040 ti: ffff88007c860000 task.ti: ffff88007c860000
RIP: 0010:[<ffffffff8234a07e>] [<ffffffff8234a07e>] init_workqueues+0x4/0x6
RSP: 0000:ffff88007c861ec8 EFLAGS: 00010246
RAX: ffff88007c861fd8 RBX: ffffffff824466a8 RCX: 0000000000000001
RDX: 0000000000000046 RSI: 0000000000000001 RDI: ffffffff8234a07a
RBP: ffff88007c861ec8 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000001 R11: 0000000000000000 R12: ffffffff8234a07a
R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
FS: 0000000000000000(0000) GS:ffff88007dc00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
CR2: ffff88015f7ff000 CR3: 00000000021f1000 CR4: 00000000000007f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
Stack:
ffff88007c861ef8 ffffffff81000312 ffffffff824466a8 ffff88007c85e650
0000000000000003 0000000000000000 ffff88007c861f38 ffffffff82335e5d
ffff88007c862080 ffffffff8223d8c0 ffff88007c862080 ffffffff81c47760
Call Trace:
[<ffffffff81000312>] do_one_initcall+0x122/0x170
[<ffffffff82335e5d>] kernel_init_freeable+0x9b/0x1c8
[<ffffffff81c47760>] ? rest_init+0x140/0x140
[<ffffffff81c4776e>] kernel_init+0xe/0xf0
[<ffffffff81c6be9c>] ret_from_fork+0x7c/0xb0
[<ffffffff81c47760>] ? rest_init+0x140/0x140
...
v2: Typo fix in x86-32.
v3: CPU number dropped from show_regs_print_info() as
dump_stack_print_info() has been updated to print it. s390
specific implementation dropped as requested by s390 maintainers.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: David S. Miller <davem@davemloft.net>
Acked-by: Jesper Nilsson <jesper.nilsson@axis.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Fengguang Wu <fengguang.wu@intel.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Cc: Vineet Gupta <vgupta@synopsys.com>
Cc: Sam Ravnborg <sam@ravnborg.org>
Acked-by: Chris Metcalf <cmetcalf@tilera.com> [tile bits]
Acked-by: Richard Kuo <rkuo@codeaurora.org> [hexagon bits]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-05-01 06:27:17 +08:00
|
|
|
show_regs_print_info(KERN_DEFAULT);
|
2011-06-05 03:30:12 +08:00
|
|
|
/* __PHX__ cleanup this mess */
|
|
|
|
show_registers(regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void release_thread(struct task_struct *dead_task)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the thread-specific (arch specific) info from the current
|
|
|
|
* process to the new one p
|
|
|
|
*/
|
|
|
|
extern asmlinkage void ret_from_fork(void);
|
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
/*
|
2020-06-11 17:04:15 +08:00
|
|
|
* copy_thread
|
2012-10-20 00:07:44 +08:00
|
|
|
* @clone_flags: flags
|
|
|
|
* @usp: user stack pointer or fn for kernel thread
|
|
|
|
* @arg: arg to fn for kernel thread; always NULL for userspace thread
|
|
|
|
* @p: the newly created task
|
2020-02-25 18:04:17 +08:00
|
|
|
* @tls: the Thread Local Storage pointer for the new process
|
2012-10-20 00:07:44 +08:00
|
|
|
*
|
|
|
|
* At the top of a newly initialized kernel stack are two stacked pt_reg
|
|
|
|
* structures. The first (topmost) is the userspace context of the thread.
|
|
|
|
* The second is the kernelspace context of the thread.
|
|
|
|
*
|
|
|
|
* A kernel thread will not be returning to userspace, so the topmost pt_regs
|
|
|
|
* struct can be uninitialized; it _does_ need to exist, though, because
|
|
|
|
* a kernel thread can become a userspace thread by doing a kernel_execve, in
|
|
|
|
* which case the topmost context will be initialized and used for 'returning'
|
|
|
|
* to userspace.
|
|
|
|
*
|
|
|
|
* The second pt_reg struct needs to be initialized to 'return' to
|
|
|
|
* ret_from_fork. A kernel thread will need to set r20 to the address of
|
|
|
|
* a function to call into (with arg in r22); userspace threads need to set
|
|
|
|
* r20 to NULL in which case ret_from_fork will just continue a return to
|
|
|
|
* userspace.
|
|
|
|
*
|
|
|
|
* A kernel thread 'fn' may return; this is effectively what happens when
|
|
|
|
* kernel_execve is called. In that case, the userspace pt_regs must have
|
|
|
|
* been initialized (which kernel_execve takes care of, see start_thread
|
|
|
|
* below); ret_from_fork will then continue its execution causing the
|
|
|
|
* 'kernel thread' to return to userspace as a userspace thread.
|
|
|
|
*/
|
|
|
|
|
2011-06-05 03:30:12 +08:00
|
|
|
int
|
2022-04-09 07:07:50 +08:00
|
|
|
copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
|
2011-06-05 03:30:12 +08:00
|
|
|
{
|
2022-04-09 07:07:50 +08:00
|
|
|
unsigned long clone_flags = args->flags;
|
|
|
|
unsigned long usp = args->stack;
|
|
|
|
unsigned long tls = args->tls;
|
2012-10-20 00:07:44 +08:00
|
|
|
struct pt_regs *userregs;
|
2011-06-05 03:30:12 +08:00
|
|
|
struct pt_regs *kregs;
|
|
|
|
unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
|
|
|
|
unsigned long top_of_kernel_stack;
|
|
|
|
|
|
|
|
top_of_kernel_stack = sp;
|
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
/* Locate userspace context on stack... */
|
|
|
|
sp -= STACK_FRAME_OVERHEAD; /* redzone */
|
2011-06-05 03:30:12 +08:00
|
|
|
sp -= sizeof(struct pt_regs);
|
2012-10-20 00:07:44 +08:00
|
|
|
userregs = (struct pt_regs *) sp;
|
2011-06-05 03:30:12 +08:00
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
/* ...and kernel context */
|
|
|
|
sp -= STACK_FRAME_OVERHEAD; /* redzone */
|
|
|
|
sp -= sizeof(struct pt_regs);
|
|
|
|
kregs = (struct pt_regs *)sp;
|
2011-06-05 03:30:12 +08:00
|
|
|
|
2022-04-12 23:18:48 +08:00
|
|
|
if (unlikely(args->fn)) {
|
2012-10-20 00:07:44 +08:00
|
|
|
memset(kregs, 0, sizeof(struct pt_regs));
|
2022-04-12 23:18:48 +08:00
|
|
|
kregs->gpr[20] = (unsigned long)args->fn;
|
|
|
|
kregs->gpr[22] = (unsigned long)args->fn_arg;
|
2011-06-05 03:30:12 +08:00
|
|
|
} else {
|
2012-10-27 07:37:49 +08:00
|
|
|
*userregs = *current_pt_regs();
|
2011-06-05 03:30:12 +08:00
|
|
|
|
2012-10-27 07:37:49 +08:00
|
|
|
if (usp)
|
|
|
|
userregs->sp = usp;
|
2014-01-25 23:48:54 +08:00
|
|
|
|
|
|
|
/*
|
2020-02-25 18:04:17 +08:00
|
|
|
* For CLONE_SETTLS set "tp" (r10) to the TLS pointer.
|
2014-01-25 23:48:54 +08:00
|
|
|
*/
|
|
|
|
if (clone_flags & CLONE_SETTLS)
|
2020-02-25 18:04:17 +08:00
|
|
|
userregs->gpr[10] = tls;
|
2014-01-25 23:48:54 +08:00
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
userregs->gpr[11] = 0; /* Result from fork() */
|
2011-06-05 03:30:12 +08:00
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
kregs->gpr[20] = 0; /* Userspace thread */
|
|
|
|
}
|
2011-06-05 03:30:12 +08:00
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
/*
|
|
|
|
* _switch wants the kernel stack page in pt_regs->sp so that it
|
|
|
|
* can restore it to thread_info->ksp... see _switch for details.
|
2011-06-05 03:30:12 +08:00
|
|
|
*/
|
|
|
|
kregs->sp = top_of_kernel_stack;
|
|
|
|
kregs->gpr[9] = (unsigned long)ret_from_fork;
|
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
task_thread_info(p)->ksp = (unsigned long)kregs;
|
|
|
|
|
2011-06-05 03:30:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a thread for executing a new program
|
|
|
|
*/
|
|
|
|
void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
|
|
|
|
{
|
2012-10-20 00:07:44 +08:00
|
|
|
unsigned long sr = mfspr(SPR_SR) & ~SPR_SR_SM;
|
2011-06-05 03:30:12 +08:00
|
|
|
|
2012-10-20 00:07:44 +08:00
|
|
|
memset(regs, 0, sizeof(struct pt_regs));
|
2011-06-05 03:30:12 +08:00
|
|
|
|
|
|
|
regs->pc = pc;
|
|
|
|
regs->sr = sr;
|
|
|
|
regs->sp = sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern struct thread_info *_switch(struct thread_info *old_ti,
|
|
|
|
struct thread_info *new_ti);
|
2014-11-03 20:28:14 +08:00
|
|
|
extern int lwa_flag;
|
2011-06-05 03:30:12 +08:00
|
|
|
|
|
|
|
struct task_struct *__switch_to(struct task_struct *old,
|
|
|
|
struct task_struct *new)
|
|
|
|
{
|
|
|
|
struct task_struct *last;
|
|
|
|
struct thread_info *new_ti, *old_ti;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
/* current_set is an array of saved current pointers
|
|
|
|
* (one for each cpu). we need them at user->kernel transition,
|
|
|
|
* while we save them at kernel->user transition
|
|
|
|
*/
|
|
|
|
new_ti = new->stack;
|
|
|
|
old_ti = old->stack;
|
|
|
|
|
2014-11-03 20:28:14 +08:00
|
|
|
lwa_flag = 0;
|
|
|
|
|
2011-06-05 03:30:12 +08:00
|
|
|
current_thread_info_set[smp_processor_id()] = new_ti;
|
|
|
|
last = (_switch(old_ti, new_ti))->task;
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out registers in core dump format, as defined by the
|
|
|
|
* struct user_regs_struct
|
|
|
|
*/
|
|
|
|
void dump_elf_thread(elf_greg_t *dest, struct pt_regs* regs)
|
|
|
|
{
|
|
|
|
dest[0] = 0; /* r0 */
|
|
|
|
memcpy(dest+1, regs->gpr+1, 31*sizeof(unsigned long));
|
|
|
|
dest[32] = regs->pc;
|
|
|
|
dest[33] = regs->sr;
|
|
|
|
dest[34] = 0;
|
|
|
|
dest[35] = 0;
|
|
|
|
}
|
|
|
|
|
2021-09-30 06:02:14 +08:00
|
|
|
unsigned long __get_wchan(struct task_struct *p)
|
2011-06-05 03:30:12 +08:00
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|