• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
4  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
5  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6  * Copyright 2003 PathScale, Inc.
7  */
8 
9 #include <linux/stddef.h>
10 #include <linux/err.h>
11 #include <linux/hardirq.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/personality.h>
15 #include <linux/proc_fs.h>
16 #include <linux/ptrace.h>
17 #include <linux/random.h>
18 #include <linux/cpu.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/sched/debug.h>
22 #include <linux/sched/task.h>
23 #include <linux/sched/task_stack.h>
24 #include <linux/seq_file.h>
25 #include <linux/tick.h>
26 #include <linux/threads.h>
27 #include <linux/resume_user_mode.h>
28 #include <asm/current.h>
29 #include <asm/mmu_context.h>
30 #include <asm/switch_to.h>
31 #include <asm/exec.h>
32 #include <linux/uaccess.h>
33 #include <as-layout.h>
34 #include <kern_util.h>
35 #include <os.h>
36 #include <skas.h>
37 #include <registers.h>
38 #include <linux/time-internal.h>
39 #include <linux/elfcore.h>
40 
41 /*
42  * This is a per-cpu array.  A processor only modifies its entry and it only
43  * cares about its entry, so it's OK if another processor is modifying its
44  * entry.
45  */
46 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { NULL } };
47 
free_stack(unsigned long stack,int order)48 void free_stack(unsigned long stack, int order)
49 {
50 	free_pages(stack, order);
51 }
52 
alloc_stack(int order,int atomic)53 unsigned long alloc_stack(int order, int atomic)
54 {
55 	unsigned long page;
56 	gfp_t flags = GFP_KERNEL;
57 
58 	if (atomic)
59 		flags = GFP_ATOMIC;
60 	page = __get_free_pages(flags, order);
61 
62 	return page;
63 }
64 
set_current(struct task_struct * task)65 static inline void set_current(struct task_struct *task)
66 {
67 	cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task) { task });
68 }
69 
__switch_to(struct task_struct * from,struct task_struct * to)70 struct task_struct *__switch_to(struct task_struct *from, struct task_struct *to)
71 {
72 	to->thread.prev_sched = from;
73 	set_current(to);
74 
75 	switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
76 	arch_switch_to(current);
77 
78 	return current->thread.prev_sched;
79 }
80 
interrupt_end(void)81 void interrupt_end(void)
82 {
83 	struct pt_regs *regs = &current->thread.regs;
84 	unsigned long thread_flags;
85 
86 	thread_flags = read_thread_flags();
87 	while (thread_flags & _TIF_WORK_MASK) {
88 		if (thread_flags & _TIF_NEED_RESCHED)
89 			schedule();
90 		if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
91 			do_signal(regs);
92 		if (thread_flags & _TIF_NOTIFY_RESUME)
93 			resume_user_mode_work(regs);
94 		thread_flags = read_thread_flags();
95 	}
96 }
97 
get_current_pid(void)98 int get_current_pid(void)
99 {
100 	return task_pid_nr(current);
101 }
102 
103 /*
104  * This is called magically, by its address being stuffed in a jmp_buf
105  * and being longjmp-d to.
106  */
new_thread_handler(void)107 void new_thread_handler(void)
108 {
109 	int (*fn)(void *);
110 	void *arg;
111 
112 	if (current->thread.prev_sched != NULL)
113 		schedule_tail(current->thread.prev_sched);
114 	current->thread.prev_sched = NULL;
115 
116 	fn = current->thread.request.thread.proc;
117 	arg = current->thread.request.thread.arg;
118 
119 	/*
120 	 * callback returns only if the kernel thread execs a process
121 	 */
122 	fn(arg);
123 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
124 }
125 
126 /* Called magically, see new_thread_handler above */
fork_handler(void)127 static void fork_handler(void)
128 {
129 	schedule_tail(current->thread.prev_sched);
130 
131 	/*
132 	 * XXX: if interrupt_end() calls schedule, this call to
133 	 * arch_switch_to isn't needed. We could want to apply this to
134 	 * improve performance. -bb
135 	 */
136 	arch_switch_to(current);
137 
138 	current->thread.prev_sched = NULL;
139 
140 	userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
141 }
142 
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)143 int copy_thread(struct task_struct * p, const struct kernel_clone_args *args)
144 {
145 	unsigned long clone_flags = args->flags;
146 	unsigned long sp = args->stack;
147 	unsigned long tls = args->tls;
148 	void (*handler)(void);
149 	int ret = 0;
150 
151 	p->thread = (struct thread_struct) INIT_THREAD;
152 
153 	if (!args->fn) {
154 	  	memcpy(&p->thread.regs.regs, current_pt_regs(),
155 		       sizeof(p->thread.regs.regs));
156 		PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
157 		if (sp != 0)
158 			REGS_SP(p->thread.regs.regs.gp) = sp;
159 
160 		handler = fork_handler;
161 
162 		arch_copy_thread(&current->thread.arch, &p->thread.arch);
163 	} else {
164 		get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
165 		p->thread.request.thread.proc = args->fn;
166 		p->thread.request.thread.arg = args->fn_arg;
167 		handler = new_thread_handler;
168 	}
169 
170 	new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
171 
172 	if (!args->fn) {
173 		clear_flushed_tls(p);
174 
175 		/*
176 		 * Set a new TLS for the child thread?
177 		 */
178 		if (clone_flags & CLONE_SETTLS)
179 			ret = arch_set_tls(p, tls);
180 	}
181 
182 	return ret;
183 }
184 
initial_thread_cb(void (* proc)(void *),void * arg)185 void initial_thread_cb(void (*proc)(void *), void *arg)
186 {
187 	int save_kmalloc_ok = kmalloc_ok;
188 
189 	kmalloc_ok = 0;
190 	initial_thread_cb_skas(proc, arg);
191 	kmalloc_ok = save_kmalloc_ok;
192 }
193 
um_idle_sleep(void)194 void um_idle_sleep(void)
195 {
196 	if (time_travel_mode != TT_MODE_OFF)
197 		time_travel_sleep();
198 	else
199 		os_idle_sleep();
200 }
201 
arch_cpu_idle(void)202 void arch_cpu_idle(void)
203 {
204 	um_idle_sleep();
205 }
206 
__uml_cant_sleep(void)207 int __uml_cant_sleep(void) {
208 	return in_atomic() || irqs_disabled() || in_interrupt();
209 	/* Is in_interrupt() really needed? */
210 }
211 
user_context(unsigned long sp)212 int user_context(unsigned long sp)
213 {
214 	unsigned long stack;
215 
216 	stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
217 	return stack != (unsigned long) current_thread_info();
218 }
219 
220 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
221 
do_uml_exitcalls(void)222 void do_uml_exitcalls(void)
223 {
224 	exitcall_t *call;
225 
226 	call = &__uml_exitcall_end;
227 	while (--call >= &__uml_exitcall_begin)
228 		(*call)();
229 }
230 
uml_strdup(const char * string)231 char *uml_strdup(const char *string)
232 {
233 	return kstrdup(string, GFP_KERNEL);
234 }
235 EXPORT_SYMBOL(uml_strdup);
236 
copy_from_user_proc(void * to,void __user * from,int size)237 int copy_from_user_proc(void *to, void __user *from, int size)
238 {
239 	return copy_from_user(to, from, size);
240 }
241 
singlestepping(void)242 int singlestepping(void)
243 {
244 	return test_thread_flag(TIF_SINGLESTEP);
245 }
246 
247 /*
248  * Only x86 and x86_64 have an arch_align_stack().
249  * All other arches have "#define arch_align_stack(x) (x)"
250  * in their asm/exec.h
251  * As this is included in UML from asm-um/system-generic.h,
252  * we can use it to behave as the subarch does.
253  */
254 #ifndef arch_align_stack
arch_align_stack(unsigned long sp)255 unsigned long arch_align_stack(unsigned long sp)
256 {
257 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
258 		sp -= get_random_u32_below(8192);
259 	return sp & ~0xf;
260 }
261 #endif
262 
__get_wchan(struct task_struct * p)263 unsigned long __get_wchan(struct task_struct *p)
264 {
265 	unsigned long stack_page, sp, ip;
266 	bool seen_sched = 0;
267 
268 	stack_page = (unsigned long) task_stack_page(p);
269 	/* Bail if the process has no kernel stack for some reason */
270 	if (stack_page == 0)
271 		return 0;
272 
273 	sp = p->thread.switch_buf->JB_SP;
274 	/*
275 	 * Bail if the stack pointer is below the bottom of the kernel
276 	 * stack for some reason
277 	 */
278 	if (sp < stack_page)
279 		return 0;
280 
281 	while (sp < stack_page + THREAD_SIZE) {
282 		ip = *((unsigned long *) sp);
283 		if (in_sched_functions(ip))
284 			/* Ignore everything until we're above the scheduler */
285 			seen_sched = 1;
286 		else if (kernel_text_address(ip) && seen_sched)
287 			return ip;
288 
289 		sp += sizeof(unsigned long);
290 	}
291 
292 	return 0;
293 }
294 
elf_core_copy_task_fpregs(struct task_struct * t,elf_fpregset_t * fpu)295 int elf_core_copy_task_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
296 {
297 	int cpu = current_thread_info()->cpu;
298 
299 	return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu) == 0;
300 }
301 
302