blob: d3b9c62e73c7faeade621fa021a8401de245ec41 [file] [log] [blame]
Jeff Dike995473a2006-09-27 01:50:40 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Copyright 2003 PathScale, Inc.
4 * Licensed under the GPL
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/kernel.h"
8#include "linux/sched.h"
9#include "linux/interrupt.h"
Robert Lovedfe52242005-06-23 00:09:04 -070010#include "linux/string.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "linux/mm.h"
12#include "linux/slab.h"
13#include "linux/utsname.h"
14#include "linux/fs.h"
15#include "linux/utime.h"
16#include "linux/smp_lock.h"
17#include "linux/module.h"
18#include "linux/init.h"
19#include "linux/capability.h"
20#include "linux/vmalloc.h"
21#include "linux/spinlock.h"
22#include "linux/proc_fs.h"
23#include "linux/ptrace.h"
24#include "linux/random.h"
Jeff Dike8f80e942006-09-25 23:33:01 -070025#include "linux/personality.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "asm/unistd.h"
27#include "asm/mman.h"
28#include "asm/segment.h"
29#include "asm/stat.h"
30#include "asm/pgtable.h"
31#include "asm/processor.h"
32#include "asm/tlbflush.h"
33#include "asm/uaccess.h"
34#include "asm/user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "kern_util.h"
Jeff Dike4ff83ce2007-05-06 14:51:08 -070036#include "as-layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "kern.h"
38#include "signal_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "init.h"
40#include "irq_user.h"
41#include "mem_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "tlb.h"
43#include "frame_kern.h"
44#include "sigcontext.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "os.h"
Jeff Dike77bf4402007-10-16 01:26:58 -070046#include "skas.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* This is a per-cpu array. A processor only modifies its entry and it only
49 * cares about its entry, so it's OK if another processor is modifying its
50 * entry.
51 */
52struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
53
Jeff Dike6e21aec2007-05-06 14:51:21 -070054static inline int external_pid(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jeff Dike77bf4402007-10-16 01:26:58 -070056 /* FIXME: Need to look up userspace_pid by cpu */
57 return(userspace_pid[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60int pid_to_processor_id(int pid)
61{
62 int i;
63
64 for(i = 0; i < ncpus; i++){
Jeff Dike6e21aec2007-05-06 14:51:21 -070065 if(cpu_tasks[i].pid == pid)
66 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Jeff Dike6e21aec2007-05-06 14:51:21 -070068 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71void free_stack(unsigned long stack, int order)
72{
73 free_pages(stack, order);
74}
75
76unsigned long alloc_stack(int order, int atomic)
77{
78 unsigned long page;
Al Viro53f9fc92005-10-21 03:22:24 -040079 gfp_t flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Paolo 'Blaisorblade' Giarrusso46db4a42005-09-22 21:44:20 -070081 if (atomic)
82 flags = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 page = __get_free_pages(flags, order);
Jeff Dike5c8aace2007-10-16 01:26:46 -070084 if (page == 0)
Jeff Dike6e21aec2007-05-06 14:51:21 -070085 return 0;
Jeff Dike5c8aace2007-10-16 01:26:46 -070086
Jeff Dike6e21aec2007-05-06 14:51:21 -070087 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
91{
92 int pid;
93
94 current->thread.request.u.thread.proc = fn;
95 current->thread.request.u.thread.arg = arg;
Jeff Dikee0877f02005-06-25 14:55:21 -070096 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
97 &current->thread.regs, 0, NULL, NULL);
Jeff Dike6e21aec2007-05-06 14:51:21 -070098 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Jeff Dike6e21aec2007-05-06 14:51:21 -0700101static inline void set_current(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Al Viroca9bc0b2006-01-12 01:05:48 -0800103 cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 { external_pid(task), task });
105}
106
Jeff Dike77bf4402007-10-16 01:26:58 -0700107extern void arch_switch_to(struct task_struct *from, struct task_struct *to);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109void *_switch_to(void *prev, void *next, void *last)
110{
Jeff Dike995473a2006-09-27 01:50:40 -0700111 struct task_struct *from = prev;
112 struct task_struct *to= next;
Jeff Dikef6e34c62005-09-16 19:27:43 -0700113
Jeff Dike995473a2006-09-27 01:50:40 -0700114 to->thread.prev_sched = from;
115 set_current(to);
Jeff Dikef6e34c62005-09-16 19:27:43 -0700116
Jeff Dike3eddddc2005-09-16 19:27:46 -0700117 do {
Jeff Dike6aa802c2007-10-16 01:26:56 -0700118 current->thread.saved_task = NULL;
Jeff Dike77bf4402007-10-16 01:26:58 -0700119
120 /* XXX need to check runqueues[cpu].idle */
121 if(current->pid == 0)
122 switch_timers(0);
123
124 switch_threads(&from->thread.switch_buf,
125 &to->thread.switch_buf);
126
127 arch_switch_to(current->thread.prev_sched, current);
128
129 if(current->pid == 0)
130 switch_timers(1);
131
Jeff Dike3eddddc2005-09-16 19:27:46 -0700132 if(current->thread.saved_task)
133 show_regs(&(current->thread.regs));
134 next= current->thread.saved_task;
135 prev= current;
136 } while(current->thread.saved_task);
Jeff Dikef6e34c62005-09-16 19:27:43 -0700137
Jeff Dike6e21aec2007-05-06 14:51:21 -0700138 return current->thread.prev_sched;
Jeff Dikef6e34c62005-09-16 19:27:43 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142void interrupt_end(void)
143{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700144 if(need_resched())
145 schedule();
146 if(test_tsk_thread_flag(current, TIF_SIGPENDING))
147 do_signal();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150void exit_thread(void)
151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
Jeff Dike995473a2006-09-27 01:50:40 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154void *get_current(void)
155{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700156 return current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Jeff Dike77bf4402007-10-16 01:26:58 -0700159extern void schedule_tail(struct task_struct *prev);
160
161/* This is called magically, by its address being stuffed in a jmp_buf
162 * and being longjmp-d to.
163 */
164void new_thread_handler(void)
165{
166 int (*fn)(void *), n;
167 void *arg;
168
169 if(current->thread.prev_sched != NULL)
170 schedule_tail(current->thread.prev_sched);
171 current->thread.prev_sched = NULL;
172
173 fn = current->thread.request.u.thread.proc;
174 arg = current->thread.request.u.thread.arg;
175
176 /* The return value is 1 if the kernel thread execs a process,
177 * 0 if it just exits
178 */
179 n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
180 if(n == 1){
181 /* Handle any immediate reschedules or signals */
182 interrupt_end();
183 userspace(&current->thread.regs.regs);
184 }
185 else do_exit(0);
186}
187
188/* Called magically, see new_thread_handler above */
189void fork_handler(void)
190{
191 force_flush_all();
192 if(current->thread.prev_sched == NULL)
193 panic("blech");
194
195 schedule_tail(current->thread.prev_sched);
196
197 /* XXX: if interrupt_end() calls schedule, this call to
198 * arch_switch_to isn't needed. We could want to apply this to
199 * improve performance. -bb */
200 arch_switch_to(current->thread.prev_sched, current);
201
202 current->thread.prev_sched = NULL;
203
204 /* Handle any immediate reschedules or signals */
205 interrupt_end();
206
207 userspace(&current->thread.regs.regs);
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
Jeff Dike995473a2006-09-27 01:50:40 -0700211 unsigned long stack_top, struct task_struct * p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 struct pt_regs *regs)
213{
Jeff Dike77bf4402007-10-16 01:26:58 -0700214 void (*handler)(void);
215 int ret = 0;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 p->thread = (struct thread_struct) INIT_THREAD;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800218
Jeff Dike77bf4402007-10-16 01:26:58 -0700219 if(current->thread.forking){
220 memcpy(&p->thread.regs.regs, &regs->regs,
221 sizeof(p->thread.regs.regs));
222 REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.regs, 0);
223 if(sp != 0)
224 REGS_SP(p->thread.regs.regs.regs) = sp;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800225
Jeff Dike77bf4402007-10-16 01:26:58 -0700226 handler = fork_handler;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800227
Jeff Dike77bf4402007-10-16 01:26:58 -0700228 arch_copy_thread(&current->thread.arch, &p->thread.arch);
229 }
230 else {
231 init_thread_registers(&p->thread.regs.regs);
232 p->thread.request.u.thread = current->thread.request.u.thread;
233 handler = new_thread_handler;
234 }
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800235
Jeff Dike77bf4402007-10-16 01:26:58 -0700236 new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
237
238 if (current->thread.forking) {
239 clear_flushed_tls(p);
240
241 /*
242 * Set a new TLS for the child thread?
243 */
244 if (clone_flags & CLONE_SETTLS)
245 ret = arch_copy_tls(p);
246 }
247
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251void initial_thread_cb(void (*proc)(void *), void *arg)
252{
253 int save_kmalloc_ok = kmalloc_ok;
254
255 kmalloc_ok = 0;
Jeff Dike6aa802c2007-10-16 01:26:56 -0700256 initial_thread_cb_skas(proc, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 kmalloc_ok = save_kmalloc_ok;
258}
Jeff Dike995473a2006-09-27 01:50:40 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260void default_idle(void)
261{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 while(1){
263 /* endless idle loop with no priority at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
266 * although we are an idle CPU, we do not want to
267 * get into the scheduler unnecessarily.
268 */
269 if(need_resched())
270 schedule();
Jeff Dike995473a2006-09-27 01:50:40 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 idle_sleep(10);
273 }
274}
275
276void cpu_idle(void)
277{
Jeff Dike77bf4402007-10-16 01:26:58 -0700278 cpu_tasks[current_thread->cpu].pid = os_getpid();
279 default_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Jeff Dike995473a2006-09-27 01:50:40 -0700282void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 pte_t *pte_out)
284{
285 pgd_t *pgd;
286 pud_t *pud;
287 pmd_t *pmd;
288 pte_t *pte;
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700289 pte_t ptent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jeff Dike995473a2006-09-27 01:50:40 -0700291 if(task->mm == NULL)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700292 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 pgd = pgd_offset(task->mm, addr);
294 if(!pgd_present(*pgd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700295 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 pud = pud_offset(pgd, addr);
298 if(!pud_present(*pud))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700299 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 pmd = pmd_offset(pud, addr);
Jeff Dike995473a2006-09-27 01:50:40 -0700302 if(!pmd_present(*pmd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700303 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 pte = pte_offset_kernel(pmd, addr);
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700306 ptent = *pte;
307 if(!pte_present(ptent))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700308 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if(pte_out != NULL)
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700311 *pte_out = ptent;
Jeff Dike6e21aec2007-05-06 14:51:21 -0700312 return (void *) (pte_val(ptent) & PAGE_MASK) + (addr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315char *current_cmd(void)
316{
317#if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700318 return "(Unknown)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#else
320 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
321 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
322#endif
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325void dump_thread(struct pt_regs *regs, struct user *u)
326{
327}
328
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800329int __cant_sleep(void) {
330 return in_atomic() || irqs_disabled() || in_interrupt();
331 /* Is in_interrupt() really needed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334int user_context(unsigned long sp)
335{
336 unsigned long stack;
337
338 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
Jeff Dike6e21aec2007-05-06 14:51:21 -0700339 return stack != (unsigned long) current_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
343
344void do_uml_exitcalls(void)
345{
346 exitcall_t *call;
347
348 call = &__uml_exitcall_end;
349 while (--call >= &__uml_exitcall_begin)
350 (*call)();
351}
352
353char *uml_strdup(char *string)
354{
Robert Lovedfe52242005-06-23 00:09:04 -0700355 return kstrdup(string, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358int copy_to_user_proc(void __user *to, void *from, int size)
359{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700360 return copy_to_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363int copy_from_user_proc(void *to, void __user *from, int size)
364{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700365 return copy_from_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368int clear_user_proc(void __user *buf, int size)
369{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700370 return clear_user(buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373int strlen_user_proc(char __user *str)
374{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700375 return strlen_user(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378int smp_sigio_handler(void)
379{
380#ifdef CONFIG_SMP
381 int cpu = current_thread->cpu;
382 IPI_handler(cpu);
383 if(cpu != 0)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700384 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#endif
Jeff Dike6e21aec2007-05-06 14:51:21 -0700386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389int cpu(void)
390{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700391 return current_thread->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394static atomic_t using_sysemu = ATOMIC_INIT(0);
395int sysemu_supported;
396
397void set_using_sysemu(int value)
398{
399 if (value > sysemu_supported)
400 return;
401 atomic_set(&using_sysemu, value);
402}
403
404int get_using_sysemu(void)
405{
406 return atomic_read(&using_sysemu);
407}
408
409static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
410{
411 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
412 *eof = 1;
413
414 return strlen(buf);
415}
416
Al Viro4d338e12006-03-31 02:30:15 -0800417static int proc_write_sysemu(struct file *file,const char __user *buf, unsigned long count,void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 char tmp[2];
420
421 if (copy_from_user(tmp, buf, 1))
422 return -EFAULT;
423
424 if (tmp[0] >= '0' && tmp[0] <= '2')
425 set_using_sysemu(tmp[0] - '0');
426 return count; /*We use the first char, but pretend to write everything*/
427}
428
429int __init make_proc_sysemu(void)
430{
431 struct proc_dir_entry *ent;
432 if (!sysemu_supported)
433 return 0;
434
435 ent = create_proc_entry("sysemu", 0600, &proc_root);
436
437 if (ent == NULL)
438 {
Christophe Lucas30f417c2005-07-28 21:16:12 -0700439 printk(KERN_WARNING "Failed to register /proc/sysemu\n");
Jeff Dike6e21aec2007-05-06 14:51:21 -0700440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 ent->read_proc = proc_read_sysemu;
444 ent->write_proc = proc_write_sysemu;
445
446 return 0;
447}
448
449late_initcall(make_proc_sysemu);
450
451int singlestepping(void * t)
452{
453 struct task_struct *task = t ? t : current;
454
455 if ( ! (task->ptrace & PT_DTRACE) )
456 return(0);
457
458 if (task->thread.singlestep_syscall)
459 return(1);
460
461 return 2;
462}
463
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700464/*
465 * Only x86 and x86_64 have an arch_align_stack().
466 * All other arches have "#define arch_align_stack(x) (x)"
467 * in their asm/system.h
468 * As this is included in UML from asm-um/system-generic.h,
469 * we can use it to behave as the subarch does.
470 */
471#ifndef arch_align_stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472unsigned long arch_align_stack(unsigned long sp)
473{
Jeff Dike8f80e942006-09-25 23:33:01 -0700474 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 sp -= get_random_int() % 8192;
476 return sp & ~0xf;
477}
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700478#endif