• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Copyright 2003 PathScale, Inc.
4  * Licensed under the GPL
5  */
6 
7 #include <linux/stddef.h>
8 #include <linux/err.h>
9 #include <linux/hardirq.h>
10 #include <linux/mm.h>
11 #include <linux/module.h>
12 #include <linux/personality.h>
13 #include <linux/proc_fs.h>
14 #include <linux/ptrace.h>
15 #include <linux/random.h>
16 #include <linux/slab.h>
17 #include <linux/sched.h>
18 #include <linux/seq_file.h>
19 #include <linux/tick.h>
20 #include <linux/threads.h>
21 #include <linux/tracehook.h>
22 #include <asm/current.h>
23 #include <asm/pgtable.h>
24 #include <asm/mmu_context.h>
25 #include <asm/uaccess.h>
26 #include <as-layout.h>
27 #include <kern_util.h>
28 #include <os.h>
29 #include <skas.h>
30 
31 /*
32  * This is a per-cpu array.  A processor only modifies its entry and it only
33  * cares about its entry, so it's OK if another processor is modifying its
34  * entry.
35  */
36 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
37 
external_pid(void)38 static inline int external_pid(void)
39 {
40 	/* FIXME: Need to look up userspace_pid by cpu */
41 	return userspace_pid[0];
42 }
43 
pid_to_processor_id(int pid)44 int pid_to_processor_id(int pid)
45 {
46 	int i;
47 
48 	for (i = 0; i < ncpus; i++) {
49 		if (cpu_tasks[i].pid == pid)
50 			return i;
51 	}
52 	return -1;
53 }
54 
free_stack(unsigned long stack,int order)55 void free_stack(unsigned long stack, int order)
56 {
57 	free_pages(stack, order);
58 }
59 
alloc_stack(int order,int atomic)60 unsigned long alloc_stack(int order, int atomic)
61 {
62 	unsigned long page;
63 	gfp_t flags = GFP_KERNEL;
64 
65 	if (atomic)
66 		flags = GFP_ATOMIC;
67 	page = __get_free_pages(flags, order);
68 
69 	return page;
70 }
71 
set_current(struct task_struct * task)72 static inline void set_current(struct task_struct *task)
73 {
74 	cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
75 		{ external_pid(), task });
76 }
77 
78 extern void arch_switch_to(struct task_struct *to);
79 
__switch_to(struct task_struct * from,struct task_struct * to)80 void *__switch_to(struct task_struct *from, struct task_struct *to)
81 {
82 	to->thread.prev_sched = from;
83 	set_current(to);
84 
85 	switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
86 	arch_switch_to(current);
87 
88 	return current->thread.prev_sched;
89 }
90 
interrupt_end(void)91 void interrupt_end(void)
92 {
93 	if (need_resched())
94 		schedule();
95 	if (test_thread_flag(TIF_SIGPENDING))
96 		do_signal();
97 	if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
98 		tracehook_notify_resume(&current->thread.regs);
99 }
100 
get_current_pid(void)101 int get_current_pid(void)
102 {
103 	return task_pid_nr(current);
104 }
105 
106 /*
107  * This is called magically, by its address being stuffed in a jmp_buf
108  * and being longjmp-d to.
109  */
new_thread_handler(void)110 void new_thread_handler(void)
111 {
112 	int (*fn)(void *), n;
113 	void *arg;
114 
115 	if (current->thread.prev_sched != NULL)
116 		schedule_tail(current->thread.prev_sched);
117 	current->thread.prev_sched = NULL;
118 
119 	fn = current->thread.request.u.thread.proc;
120 	arg = current->thread.request.u.thread.arg;
121 
122 	/*
123 	 * callback returns only if the kernel thread execs a process
124 	 */
125 	n = fn(arg);
126 	userspace(&current->thread.regs.regs);
127 }
128 
129 /* Called magically, see new_thread_handler above */
fork_handler(void)130 void fork_handler(void)
131 {
132 	force_flush_all();
133 
134 	schedule_tail(current->thread.prev_sched);
135 
136 	/*
137 	 * XXX: if interrupt_end() calls schedule, this call to
138 	 * arch_switch_to isn't needed. We could want to apply this to
139 	 * improve performance. -bb
140 	 */
141 	arch_switch_to(current);
142 
143 	current->thread.prev_sched = NULL;
144 
145 	userspace(&current->thread.regs.regs);
146 }
147 
copy_thread(unsigned long clone_flags,unsigned long sp,unsigned long arg,struct task_struct * p)148 int copy_thread(unsigned long clone_flags, unsigned long sp,
149 		unsigned long arg, struct task_struct * p)
150 {
151 	void (*handler)(void);
152 	int kthread = current->flags & PF_KTHREAD;
153 	int ret = 0;
154 
155 	p->thread = (struct thread_struct) INIT_THREAD;
156 
157 	if (!kthread) {
158 	  	memcpy(&p->thread.regs.regs, current_pt_regs(),
159 		       sizeof(p->thread.regs.regs));
160 		PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
161 		if (sp != 0)
162 			REGS_SP(p->thread.regs.regs.gp) = sp;
163 
164 		handler = fork_handler;
165 
166 		arch_copy_thread(&current->thread.arch, &p->thread.arch);
167 	} else {
168 		get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
169 		p->thread.request.u.thread.proc = (int (*)(void *))sp;
170 		p->thread.request.u.thread.arg = (void *)arg;
171 		handler = new_thread_handler;
172 	}
173 
174 	new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
175 
176 	if (!kthread) {
177 		clear_flushed_tls(p);
178 
179 		/*
180 		 * Set a new TLS for the child thread?
181 		 */
182 		if (clone_flags & CLONE_SETTLS)
183 			ret = arch_copy_tls(p);
184 	}
185 
186 	return ret;
187 }
188 
initial_thread_cb(void (* proc)(void *),void * arg)189 void initial_thread_cb(void (*proc)(void *), void *arg)
190 {
191 	int save_kmalloc_ok = kmalloc_ok;
192 
193 	kmalloc_ok = 0;
194 	initial_thread_cb_skas(proc, arg);
195 	kmalloc_ok = save_kmalloc_ok;
196 }
197 
arch_cpu_idle(void)198 void arch_cpu_idle(void)
199 {
200 	unsigned long long nsecs;
201 
202 	cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
203 	nsecs = disable_timer();
204 	idle_sleep(nsecs);
205 	local_irq_enable();
206 }
207 
__cant_sleep(void)208 int __cant_sleep(void) {
209 	return in_atomic() || irqs_disabled() || in_interrupt();
210 	/* Is in_interrupt() really needed? */
211 }
212 
user_context(unsigned long sp)213 int user_context(unsigned long sp)
214 {
215 	unsigned long stack;
216 
217 	stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
218 	return stack != (unsigned long) current_thread_info();
219 }
220 
221 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
222 
do_uml_exitcalls(void)223 void do_uml_exitcalls(void)
224 {
225 	exitcall_t *call;
226 
227 	call = &__uml_exitcall_end;
228 	while (--call >= &__uml_exitcall_begin)
229 		(*call)();
230 }
231 
uml_strdup(const char * string)232 char *uml_strdup(const char *string)
233 {
234 	return kstrdup(string, GFP_KERNEL);
235 }
236 EXPORT_SYMBOL(uml_strdup);
237 
copy_to_user_proc(void __user * to,void * from,int size)238 int copy_to_user_proc(void __user *to, void *from, int size)
239 {
240 	return copy_to_user(to, from, size);
241 }
242 
copy_from_user_proc(void * to,void __user * from,int size)243 int copy_from_user_proc(void *to, void __user *from, int size)
244 {
245 	return copy_from_user(to, from, size);
246 }
247 
clear_user_proc(void __user * buf,int size)248 int clear_user_proc(void __user *buf, int size)
249 {
250 	return clear_user(buf, size);
251 }
252 
strlen_user_proc(char __user * str)253 int strlen_user_proc(char __user *str)
254 {
255 	return strlen_user(str);
256 }
257 
smp_sigio_handler(void)258 int smp_sigio_handler(void)
259 {
260 #ifdef CONFIG_SMP
261 	int cpu = current_thread_info()->cpu;
262 	IPI_handler(cpu);
263 	if (cpu != 0)
264 		return 1;
265 #endif
266 	return 0;
267 }
268 
cpu(void)269 int cpu(void)
270 {
271 	return current_thread_info()->cpu;
272 }
273 
274 static atomic_t using_sysemu = ATOMIC_INIT(0);
275 int sysemu_supported;
276 
set_using_sysemu(int value)277 void set_using_sysemu(int value)
278 {
279 	if (value > sysemu_supported)
280 		return;
281 	atomic_set(&using_sysemu, value);
282 }
283 
get_using_sysemu(void)284 int get_using_sysemu(void)
285 {
286 	return atomic_read(&using_sysemu);
287 }
288 
sysemu_proc_show(struct seq_file * m,void * v)289 static int sysemu_proc_show(struct seq_file *m, void *v)
290 {
291 	seq_printf(m, "%d\n", get_using_sysemu());
292 	return 0;
293 }
294 
sysemu_proc_open(struct inode * inode,struct file * file)295 static int sysemu_proc_open(struct inode *inode, struct file *file)
296 {
297 	return single_open(file, sysemu_proc_show, NULL);
298 }
299 
sysemu_proc_write(struct file * file,const char __user * buf,size_t count,loff_t * pos)300 static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
301 				 size_t count, loff_t *pos)
302 {
303 	char tmp[2];
304 
305 	if (copy_from_user(tmp, buf, 1))
306 		return -EFAULT;
307 
308 	if (tmp[0] >= '0' && tmp[0] <= '2')
309 		set_using_sysemu(tmp[0] - '0');
310 	/* We use the first char, but pretend to write everything */
311 	return count;
312 }
313 
314 static const struct file_operations sysemu_proc_fops = {
315 	.owner		= THIS_MODULE,
316 	.open		= sysemu_proc_open,
317 	.read		= seq_read,
318 	.llseek		= seq_lseek,
319 	.release	= single_release,
320 	.write		= sysemu_proc_write,
321 };
322 
make_proc_sysemu(void)323 int __init make_proc_sysemu(void)
324 {
325 	struct proc_dir_entry *ent;
326 	if (!sysemu_supported)
327 		return 0;
328 
329 	ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_fops);
330 
331 	if (ent == NULL)
332 	{
333 		printk(KERN_WARNING "Failed to register /proc/sysemu\n");
334 		return 0;
335 	}
336 
337 	return 0;
338 }
339 
340 late_initcall(make_proc_sysemu);
341 
singlestepping(void * t)342 int singlestepping(void * t)
343 {
344 	struct task_struct *task = t ? t : current;
345 
346 	if (!(task->ptrace & PT_DTRACE))
347 		return 0;
348 
349 	if (task->thread.singlestep_syscall)
350 		return 1;
351 
352 	return 2;
353 }
354 
355 /*
356  * Only x86 and x86_64 have an arch_align_stack().
357  * All other arches have "#define arch_align_stack(x) (x)"
358  * in their asm/exec.h
359  * As this is included in UML from asm-um/system-generic.h,
360  * we can use it to behave as the subarch does.
361  */
362 #ifndef arch_align_stack
arch_align_stack(unsigned long sp)363 unsigned long arch_align_stack(unsigned long sp)
364 {
365 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
366 		sp -= get_random_int() % 8192;
367 	return sp & ~0xf;
368 }
369 #endif
370 
get_wchan(struct task_struct * p)371 unsigned long get_wchan(struct task_struct *p)
372 {
373 	unsigned long stack_page, sp, ip;
374 	bool seen_sched = 0;
375 
376 	if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
377 		return 0;
378 
379 	stack_page = (unsigned long) task_stack_page(p);
380 	/* Bail if the process has no kernel stack for some reason */
381 	if (stack_page == 0)
382 		return 0;
383 
384 	sp = p->thread.switch_buf->JB_SP;
385 	/*
386 	 * Bail if the stack pointer is below the bottom of the kernel
387 	 * stack for some reason
388 	 */
389 	if (sp < stack_page)
390 		return 0;
391 
392 	while (sp < stack_page + THREAD_SIZE) {
393 		ip = *((unsigned long *) sp);
394 		if (in_sched_functions(ip))
395 			/* Ignore everything until we're above the scheduler */
396 			seen_sched = 1;
397 		else if (kernel_text_address(ip) && seen_sched)
398 			return ip;
399 
400 		sp += sizeof(unsigned long);
401 	}
402 
403 	return 0;
404 }
405 
elf_core_copy_fpregs(struct task_struct * t,elf_fpregset_t * fpu)406 int elf_core_copy_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
407 {
408 	int cpu = current_thread_info()->cpu;
409 
410 	return save_fp_registers(userspace_pid[cpu], (unsigned long *) fpu);
411 }
412 
413