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