• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2000-2003  Axis Communications AB
4  *
5  *  Authors:   Bjorn Wesen (bjornw@axis.com)
6  *             Mikael Starvik (starvik@axis.com)
7  *             Tobias Anderberg (tobiasa@axis.com), CRISv32 port.
8  *
9  * This file handles the architecture-dependent parts of process handling..
10  */
11 
12 #include <linux/sched.h>
13 #include <linux/sched/debug.h>
14 #include <linux/sched/task.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/fs.h>
19 #include <hwregs/reg_rdwr.h>
20 #include <hwregs/reg_map.h>
21 #include <hwregs/timer_defs.h>
22 #include <hwregs/intr_vect_defs.h>
23 #include <linux/ptrace.h>
24 
25 extern void stop_watchdog(void);
26 
27 /* We use this if we don't have any better idle routine. */
default_idle(void)28 void default_idle(void)
29 {
30 	local_irq_enable();
31 	/* Halt until exception. */
32 	__asm__ volatile("halt");
33 }
34 
35 /*
36  * Free current thread data structures etc..
37  */
38 
39 extern void deconfigure_bp(long pid);
exit_thread(struct task_struct * tsk)40 void exit_thread(struct task_struct *tsk)
41 {
42 	deconfigure_bp(tsk->pid);
43 }
44 
45 /*
46  * If the watchdog is enabled, disable interrupts and enter an infinite loop.
47  * The watchdog will reset the CPU after 0.1s. If the watchdog isn't enabled
48  * then enable it and wait.
49  */
50 extern void arch_enable_nmi(void);
51 
52 void
hard_reset_now(void)53 hard_reset_now(void)
54 {
55 	/*
56 	 * Don't declare this variable elsewhere.  We don't want any other
57 	 * code to know about it than the watchdog handler in entry.S and
58 	 * this code, implementing hard reset through the watchdog.
59 	 */
60 #if defined(CONFIG_ETRAX_WATCHDOG)
61 	extern int cause_of_death;
62 #endif
63 
64 	printk("*** HARD RESET ***\n");
65 	local_irq_disable();
66 
67 #if defined(CONFIG_ETRAX_WATCHDOG)
68 	cause_of_death = 0xbedead;
69 #else
70 {
71 	reg_timer_rw_wd_ctrl wd_ctrl = {0};
72 
73 	stop_watchdog();
74 
75 	wd_ctrl.key = 16;	/* Arbitrary key. */
76 	wd_ctrl.cnt = 1;	/* Minimum time. */
77 	wd_ctrl.cmd = regk_timer_start;
78 
79         arch_enable_nmi();
80 	REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
81 }
82 #endif
83 
84 	while (1)
85 		; /* Wait for reset. */
86 }
87 
88 /*
89  * Setup the child's kernel stack with a pt_regs and call switch_stack() on it.
90  * It will be unnested during _resume and _ret_from_sys_call when the new thread
91  * is scheduled.
92  *
93  * Also setup the thread switching structure which is used to keep
94  * thread-specific data during _resumes.
95  */
96 
97 extern asmlinkage void ret_from_fork(void);
98 extern asmlinkage void ret_from_kernel_thread(void);
99 
100 int
copy_thread(unsigned long clone_flags,unsigned long usp,unsigned long arg,struct task_struct * p)101 copy_thread(unsigned long clone_flags, unsigned long usp,
102 	unsigned long arg, struct task_struct *p)
103 {
104 	struct pt_regs *childregs = task_pt_regs(p);
105 	struct switch_stack *swstack = ((struct switch_stack *) childregs) - 1;
106 
107 	/*
108 	 * Put the pt_regs structure at the end of the new kernel stack page and
109 	 * fix it up. Note: the task_struct doubles as the kernel stack for the
110 	 * task.
111 	 */
112 	if (unlikely(p->flags & PF_KTHREAD)) {
113 		memset(swstack, 0,
114 			sizeof(struct switch_stack) + sizeof(struct pt_regs));
115 		swstack->r1 = usp;
116 		swstack->r2 = arg;
117 		childregs->ccs = 1 << (I_CCS_BITNR + CCS_SHIFT);
118 		swstack->return_ip = (unsigned long) ret_from_kernel_thread;
119 		p->thread.ksp = (unsigned long) swstack;
120 		p->thread.usp = 0;
121 		return 0;
122 	}
123 	*childregs = *current_pt_regs();	/* Struct copy of pt_regs. */
124         childregs->r10 = 0;	/* Child returns 0 after a fork/clone. */
125 
126 	/* Set a new TLS ?
127 	 * The TLS is in $mof because it is the 5th argument to sys_clone.
128 	 */
129 	if (p->mm && (clone_flags & CLONE_SETTLS)) {
130 		task_thread_info(p)->tls = childregs->mof;
131 	}
132 
133 	/* Put the switch stack right below the pt_regs. */
134 
135 	/* Parameter to ret_from_sys_call. 0 is don't restart the syscall. */
136 	swstack->r9 = 0;
137 
138 	/*
139 	 * We want to return into ret_from_sys_call after the _resume.
140 	 * ret_from_fork will call ret_from_sys_call.
141 	 */
142 	swstack->return_ip = (unsigned long) ret_from_fork;
143 
144 	/* Fix the user-mode and kernel-mode stackpointer. */
145 	p->thread.usp = usp ?: rdusp();
146 	p->thread.ksp = (unsigned long) swstack;
147 
148 	return 0;
149 }
150 
151 unsigned long
get_wchan(struct task_struct * p)152 get_wchan(struct task_struct *p)
153 {
154 	/* TODO */
155 	return 0;
156 }
157 #undef last_sched
158 #undef first_sched
159 
show_regs(struct pt_regs * regs)160 void show_regs(struct pt_regs * regs)
161 {
162 	unsigned long usp = rdusp();
163 
164 	show_regs_print_info(KERN_DEFAULT);
165 
166         printk("ERP: %08lx SRP: %08lx  CCS: %08lx USP: %08lx MOF: %08lx\n",
167 		regs->erp, regs->srp, regs->ccs, usp, regs->mof);
168 
169 	printk(" r0: %08lx  r1: %08lx   r2: %08lx  r3: %08lx\n",
170 		regs->r0, regs->r1, regs->r2, regs->r3);
171 
172 	printk(" r4: %08lx  r5: %08lx   r6: %08lx  r7: %08lx\n",
173 		regs->r4, regs->r5, regs->r6, regs->r7);
174 
175 	printk(" r8: %08lx  r9: %08lx  r10: %08lx r11: %08lx\n",
176 		regs->r8, regs->r9, regs->r10, regs->r11);
177 
178 	printk("r12: %08lx r13: %08lx oR10: %08lx\n",
179 		regs->r12, regs->r13, regs->orig_r10);
180 }
181