blob: d6d3319b36708839ed3e774b3e03a37e4303d47a [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"
46#include "mode.h"
47#include "mode_kern.h"
48#include "choose-mode.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070049#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* This is a per-cpu array. A processor only modifies its entry and it only
52 * cares about its entry, so it's OK if another processor is modifying its
53 * entry.
54 */
55struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
56
Jeff Dike6e21aec2007-05-06 14:51:21 -070057static inline int external_pid(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Jeff Dike6e21aec2007-05-06 14:51:21 -070059 return CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62int pid_to_processor_id(int pid)
63{
64 int i;
65
66 for(i = 0; i < ncpus; i++){
Jeff Dike6e21aec2007-05-06 14:51:21 -070067 if(cpu_tasks[i].pid == pid)
68 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Jeff Dike6e21aec2007-05-06 14:51:21 -070070 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73void free_stack(unsigned long stack, int order)
74{
75 free_pages(stack, order);
76}
77
78unsigned long alloc_stack(int order, int atomic)
79{
80 unsigned long page;
Al Viro53f9fc92005-10-21 03:22:24 -040081 gfp_t flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Paolo 'Blaisorblade' Giarrusso46db4a42005-09-22 21:44:20 -070083 if (atomic)
84 flags = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 page = __get_free_pages(flags, order);
86 if(page == 0)
Jeff Dike6e21aec2007-05-06 14:51:21 -070087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 stack_protections(page);
Jeff Dike6e21aec2007-05-06 14:51:21 -070089 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
93{
94 int pid;
95
96 current->thread.request.u.thread.proc = fn;
97 current->thread.request.u.thread.arg = arg;
Jeff Dikee0877f02005-06-25 14:55:21 -070098 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
99 &current->thread.regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if(pid < 0)
101 panic("do_fork failed in kernel_thread, errno = %d", pid);
Jeff Dike6e21aec2007-05-06 14:51:21 -0700102 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Jeff Dike6e21aec2007-05-06 14:51:21 -0700105static inline void set_current(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Al Viroca9bc0b2006-01-12 01:05:48 -0800107 cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 { external_pid(task), task });
109}
110
111void *_switch_to(void *prev, void *next, void *last)
112{
Jeff Dike995473a2006-09-27 01:50:40 -0700113 struct task_struct *from = prev;
114 struct task_struct *to= next;
Jeff Dikef6e34c62005-09-16 19:27:43 -0700115
Jeff Dike995473a2006-09-27 01:50:40 -0700116 to->thread.prev_sched = from;
117 set_current(to);
Jeff Dikef6e34c62005-09-16 19:27:43 -0700118
Jeff Dike3eddddc2005-09-16 19:27:46 -0700119 do {
120 current->thread.saved_task = NULL ;
121 CHOOSE_MODE_PROC(switch_to_tt, switch_to_skas, prev, next);
122 if(current->thread.saved_task)
123 show_regs(&(current->thread.regs));
124 next= current->thread.saved_task;
125 prev= current;
126 } while(current->thread.saved_task);
Jeff Dikef6e34c62005-09-16 19:27:43 -0700127
Jeff Dike6e21aec2007-05-06 14:51:21 -0700128 return current->thread.prev_sched;
Jeff Dikef6e34c62005-09-16 19:27:43 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132void interrupt_end(void)
133{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700134 if(need_resched())
135 schedule();
136 if(test_tsk_thread_flag(current, TIF_SIGPENDING))
137 do_signal();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140void release_thread(struct task_struct *task)
141{
142 CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
143}
Jeff Dike995473a2006-09-27 01:50:40 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145void exit_thread(void)
146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 unprotect_stack((unsigned long) current_thread);
148}
Jeff Dike995473a2006-09-27 01:50:40 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150void *get_current(void)
151{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700152 return current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
Jeff Dike995473a2006-09-27 01:50:40 -0700156 unsigned long stack_top, struct task_struct * p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct pt_regs *regs)
158{
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800159 int ret;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 p->thread = (struct thread_struct) INIT_THREAD;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800162 ret = CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr,
163 clone_flags, sp, stack_top, p, regs);
164
165 if (ret || !current->thread.forking)
166 goto out;
167
168 clear_flushed_tls(p);
169
170 /*
171 * Set a new TLS for the child thread?
172 */
173 if (clone_flags & CLONE_SETTLS)
174 ret = arch_copy_tls(p);
175
176out:
177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180void initial_thread_cb(void (*proc)(void *), void *arg)
181{
182 int save_kmalloc_ok = kmalloc_ok;
183
184 kmalloc_ok = 0;
Jeff Dike995473a2006-09-27 01:50:40 -0700185 CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 arg);
187 kmalloc_ok = save_kmalloc_ok;
188}
Jeff Dike995473a2006-09-27 01:50:40 -0700189
Jeff Dike6e21aec2007-05-06 14:51:21 -0700190#ifdef CONFIG_MODE_TT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191unsigned long stack_sp(unsigned long page)
192{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700193 return page + PAGE_SIZE - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
Jeff Dike6e21aec2007-05-06 14:51:21 -0700195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197void default_idle(void)
198{
Jeff Dikea6f4e3c2005-06-25 14:55:22 -0700199 CHOOSE_MODE(uml_idle_timer(), (void) 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 while(1){
202 /* endless idle loop with no priority at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /*
205 * although we are an idle CPU, we do not want to
206 * get into the scheduler unnecessarily.
207 */
208 if(need_resched())
209 schedule();
Jeff Dike995473a2006-09-27 01:50:40 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 idle_sleep(10);
212 }
213}
214
215void cpu_idle(void)
216{
217 CHOOSE_MODE(init_idle_tt(), init_idle_skas());
218}
219
220int page_size(void)
221{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700222 return PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Jeff Dike995473a2006-09-27 01:50:40 -0700225void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 pte_t *pte_out)
227{
228 pgd_t *pgd;
229 pud_t *pud;
230 pmd_t *pmd;
231 pte_t *pte;
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700232 pte_t ptent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jeff Dike995473a2006-09-27 01:50:40 -0700234 if(task->mm == NULL)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700235 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 pgd = pgd_offset(task->mm, addr);
237 if(!pgd_present(*pgd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700238 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 pud = pud_offset(pgd, addr);
241 if(!pud_present(*pud))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700242 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 pmd = pmd_offset(pud, addr);
Jeff Dike995473a2006-09-27 01:50:40 -0700245 if(!pmd_present(*pmd))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700246 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 pte = pte_offset_kernel(pmd, addr);
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700249 ptent = *pte;
250 if(!pte_present(ptent))
Jeff Dike6e21aec2007-05-06 14:51:21 -0700251 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if(pte_out != NULL)
Hugh Dickins8f5cd762005-10-29 18:16:38 -0700254 *pte_out = ptent;
Jeff Dike6e21aec2007-05-06 14:51:21 -0700255 return (void *) (pte_val(ptent) & PAGE_MASK) + (addr & ~PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258char *current_cmd(void)
259{
260#if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700261 return "(Unknown)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#else
263 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
264 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
265#endif
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268void dump_thread(struct pt_regs *regs, struct user *u)
269{
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272void *um_kmalloc(int size)
273{
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800274 return kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277void *um_kmalloc_atomic(int size)
278{
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800279 return kmalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282void *um_vmalloc(int size)
283{
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800284 return vmalloc(size);
285}
286
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800287int __cant_sleep(void) {
288 return in_atomic() || irqs_disabled() || in_interrupt();
289 /* Is in_interrupt() really needed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292int user_context(unsigned long sp)
293{
294 unsigned long stack;
295
296 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
Jeff Dike6e21aec2007-05-06 14:51:21 -0700297 return stack != (unsigned long) current_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
301
302void do_uml_exitcalls(void)
303{
304 exitcall_t *call;
305
306 call = &__uml_exitcall_end;
307 while (--call >= &__uml_exitcall_begin)
308 (*call)();
309}
310
311char *uml_strdup(char *string)
312{
Robert Lovedfe52242005-06-23 00:09:04 -0700313 return kstrdup(string, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316int copy_to_user_proc(void __user *to, void *from, int size)
317{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700318 return copy_to_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321int copy_from_user_proc(void *to, void __user *from, int size)
322{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700323 return copy_from_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326int clear_user_proc(void __user *buf, int size)
327{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700328 return clear_user(buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331int strlen_user_proc(char __user *str)
332{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700333 return strlen_user(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336int smp_sigio_handler(void)
337{
338#ifdef CONFIG_SMP
339 int cpu = current_thread->cpu;
340 IPI_handler(cpu);
341 if(cpu != 0)
Jeff Dike6e21aec2007-05-06 14:51:21 -0700342 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#endif
Jeff Dike6e21aec2007-05-06 14:51:21 -0700344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347int cpu(void)
348{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700349 return current_thread->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352static atomic_t using_sysemu = ATOMIC_INIT(0);
353int sysemu_supported;
354
355void set_using_sysemu(int value)
356{
357 if (value > sysemu_supported)
358 return;
359 atomic_set(&using_sysemu, value);
360}
361
362int get_using_sysemu(void)
363{
364 return atomic_read(&using_sysemu);
365}
366
367static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
368{
369 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
370 *eof = 1;
371
372 return strlen(buf);
373}
374
Al Viro4d338e12006-03-31 02:30:15 -0800375static int proc_write_sysemu(struct file *file,const char __user *buf, unsigned long count,void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 char tmp[2];
378
379 if (copy_from_user(tmp, buf, 1))
380 return -EFAULT;
381
382 if (tmp[0] >= '0' && tmp[0] <= '2')
383 set_using_sysemu(tmp[0] - '0');
384 return count; /*We use the first char, but pretend to write everything*/
385}
386
387int __init make_proc_sysemu(void)
388{
389 struct proc_dir_entry *ent;
390 if (!sysemu_supported)
391 return 0;
392
393 ent = create_proc_entry("sysemu", 0600, &proc_root);
394
395 if (ent == NULL)
396 {
Christophe Lucas30f417c2005-07-28 21:16:12 -0700397 printk(KERN_WARNING "Failed to register /proc/sysemu\n");
Jeff Dike6e21aec2007-05-06 14:51:21 -0700398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 ent->read_proc = proc_read_sysemu;
402 ent->write_proc = proc_write_sysemu;
403
404 return 0;
405}
406
407late_initcall(make_proc_sysemu);
408
409int singlestepping(void * t)
410{
411 struct task_struct *task = t ? t : current;
412
413 if ( ! (task->ptrace & PT_DTRACE) )
414 return(0);
415
416 if (task->thread.singlestep_syscall)
417 return(1);
418
419 return 2;
420}
421
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700422/*
423 * Only x86 and x86_64 have an arch_align_stack().
424 * All other arches have "#define arch_align_stack(x) (x)"
425 * in their asm/system.h
426 * As this is included in UML from asm-um/system-generic.h,
427 * we can use it to behave as the subarch does.
428 */
429#ifndef arch_align_stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430unsigned long arch_align_stack(unsigned long sp)
431{
Jeff Dike8f80e942006-09-25 23:33:01 -0700432 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sp -= get_random_int() % 8192;
434 return sp & ~0xf;
435}
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700436#endif