blob: 804c6bbdf67c157e5e28b6a9be9a76c9d8c0ce04 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Copyright 2003 PathScale, Inc.
4 * Licensed under the GPL
5 */
6
7#include "linux/config.h"
8#include "linux/kernel.h"
9#include "linux/sched.h"
10#include "linux/interrupt.h"
11#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"
25#include "asm/unistd.h"
26#include "asm/mman.h"
27#include "asm/segment.h"
28#include "asm/stat.h"
29#include "asm/pgtable.h"
30#include "asm/processor.h"
31#include "asm/tlbflush.h"
32#include "asm/uaccess.h"
33#include "asm/user.h"
34#include "user_util.h"
35#include "kern_util.h"
36#include "kern.h"
37#include "signal_kern.h"
38#include "signal_user.h"
39#include "init.h"
40#include "irq_user.h"
41#include "mem_user.h"
42#include "time_user.h"
43#include "tlb.h"
44#include "frame_kern.h"
45#include "sigcontext.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "os.h"
47#include "mode.h"
48#include "mode_kern.h"
49#include "choose-mode.h"
50
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057int external_pid(void *t)
58{
59 struct task_struct *task = t ? t : current;
60
61 return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
62}
63
64int pid_to_processor_id(int pid)
65{
66 int i;
67
68 for(i = 0; i < ncpus; i++){
69 if(cpu_tasks[i].pid == pid) return(i);
70 }
71 return(-1);
72}
73
74void free_stack(unsigned long stack, int order)
75{
76 free_pages(stack, order);
77}
78
79unsigned long alloc_stack(int order, int atomic)
80{
81 unsigned long page;
82 int flags = GFP_KERNEL;
83
84 if(atomic) flags |= GFP_ATOMIC;
85 page = __get_free_pages(flags, order);
86 if(page == 0)
87 return(0);
88 stack_protections(page);
89 return(page);
90}
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;
98 pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0, NULL, 0, NULL,
99 NULL);
100 if(pid < 0)
101 panic("do_fork failed in kernel_thread, errno = %d", pid);
102 return(pid);
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void set_current(void *t)
106{
107 struct task_struct *task = t;
108
109 cpu_tasks[task->thread_info->cpu] = ((struct cpu_task)
110 { external_pid(task), task });
111}
112
113void *_switch_to(void *prev, void *next, void *last)
114{
115 return(CHOOSE_MODE(switch_to_tt(prev, next),
116 switch_to_skas(prev, next)));
117}
118
119void interrupt_end(void)
120{
121 if(need_resched()) schedule();
122 if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal();
123}
124
125void release_thread(struct task_struct *task)
126{
127 CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
128}
129
130void exit_thread(void)
131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unprotect_stack((unsigned long) current_thread);
133}
134
135void *get_current(void)
136{
137 return(current);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
141 unsigned long stack_top, struct task_struct * p,
142 struct pt_regs *regs)
143{
144 p->thread = (struct thread_struct) INIT_THREAD;
145 return(CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr,
146 clone_flags, sp, stack_top, p, regs));
147}
148
149void initial_thread_cb(void (*proc)(void *), void *arg)
150{
151 int save_kmalloc_ok = kmalloc_ok;
152
153 kmalloc_ok = 0;
154 CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc,
155 arg);
156 kmalloc_ok = save_kmalloc_ok;
157}
158
159unsigned long stack_sp(unsigned long page)
160{
161 return(page + PAGE_SIZE - sizeof(void *));
162}
163
164int current_pid(void)
165{
166 return(current->pid);
167}
168
169void default_idle(void)
170{
171 uml_idle_timer();
172
173 atomic_inc(&init_mm.mm_count);
174 current->mm = &init_mm;
175 current->active_mm = &init_mm;
176
177 while(1){
178 /* endless idle loop with no priority at all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /*
181 * although we are an idle CPU, we do not want to
182 * get into the scheduler unnecessarily.
183 */
184 if(need_resched())
185 schedule();
186
187 idle_sleep(10);
188 }
189}
190
191void cpu_idle(void)
192{
193 CHOOSE_MODE(init_idle_tt(), init_idle_skas());
194}
195
196int page_size(void)
197{
198 return(PAGE_SIZE);
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
202 pte_t *pte_out)
203{
204 pgd_t *pgd;
205 pud_t *pud;
206 pmd_t *pmd;
207 pte_t *pte;
208
209 if(task->mm == NULL)
210 return(ERR_PTR(-EINVAL));
211 pgd = pgd_offset(task->mm, addr);
212 if(!pgd_present(*pgd))
213 return(ERR_PTR(-EINVAL));
214
215 pud = pud_offset(pgd, addr);
216 if(!pud_present(*pud))
217 return(ERR_PTR(-EINVAL));
218
219 pmd = pmd_offset(pud, addr);
220 if(!pmd_present(*pmd))
221 return(ERR_PTR(-EINVAL));
222
223 pte = pte_offset_kernel(pmd, addr);
224 if(!pte_present(*pte))
225 return(ERR_PTR(-EINVAL));
226
227 if(pte_out != NULL)
228 *pte_out = *pte;
229 return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
230}
231
232char *current_cmd(void)
233{
234#if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
235 return("(Unknown)");
236#else
237 void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
238 return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
239#endif
240}
241
242void force_sigbus(void)
243{
244 printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
245 current->pid);
246 lock_kernel();
247 sigaddset(&current->pending.signal, SIGBUS);
248 recalc_sigpending();
249 current->flags |= PF_SIGNALED;
250 do_exit(SIGBUS | 0x80);
251}
252
253void dump_thread(struct pt_regs *regs, struct user *u)
254{
255}
256
257void enable_hlt(void)
258{
259 panic("enable_hlt");
260}
261
262EXPORT_SYMBOL(enable_hlt);
263
264void disable_hlt(void)
265{
266 panic("disable_hlt");
267}
268
269EXPORT_SYMBOL(disable_hlt);
270
271void *um_kmalloc(int size)
272{
273 return(kmalloc(size, GFP_KERNEL));
274}
275
276void *um_kmalloc_atomic(int size)
277{
278 return(kmalloc(size, GFP_ATOMIC));
279}
280
281void *um_vmalloc(int size)
282{
283 return(vmalloc(size));
284}
285
286unsigned long get_fault_addr(void)
287{
288 return((unsigned long) current->thread.fault_addr);
289}
290
291EXPORT_SYMBOL(get_fault_addr);
292
293void not_implemented(void)
294{
295 printk(KERN_DEBUG "Something isn't implemented in here\n");
296}
297
298EXPORT_SYMBOL(not_implemented);
299
300int user_context(unsigned long sp)
301{
302 unsigned long stack;
303
304 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
305 return(stack != (unsigned long) current_thread);
306}
307
308extern void remove_umid_dir(void);
309
310__uml_exitcall(remove_umid_dir);
311
312extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
313
314void do_uml_exitcalls(void)
315{
316 exitcall_t *call;
317
318 call = &__uml_exitcall_end;
319 while (--call >= &__uml_exitcall_begin)
320 (*call)();
321}
322
323char *uml_strdup(char *string)
324{
325 char *new;
326
327 new = kmalloc(strlen(string) + 1, GFP_KERNEL);
328 if(new == NULL) return(NULL);
329 strcpy(new, string);
330 return(new);
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333int copy_to_user_proc(void __user *to, void *from, int size)
334{
335 return(copy_to_user(to, from, size));
336}
337
338int copy_from_user_proc(void *to, void __user *from, int size)
339{
340 return(copy_from_user(to, from, size));
341}
342
343int clear_user_proc(void __user *buf, int size)
344{
345 return(clear_user(buf, size));
346}
347
348int strlen_user_proc(char __user *str)
349{
350 return(strlen_user(str));
351}
352
353int smp_sigio_handler(void)
354{
355#ifdef CONFIG_SMP
356 int cpu = current_thread->cpu;
357 IPI_handler(cpu);
358 if(cpu != 0)
359 return(1);
360#endif
361 return(0);
362}
363
364int um_in_interrupt(void)
365{
366 return(in_interrupt());
367}
368
369int cpu(void)
370{
371 return(current_thread->cpu);
372}
373
374static atomic_t using_sysemu = ATOMIC_INIT(0);
375int sysemu_supported;
376
377void set_using_sysemu(int value)
378{
379 if (value > sysemu_supported)
380 return;
381 atomic_set(&using_sysemu, value);
382}
383
384int get_using_sysemu(void)
385{
386 return atomic_read(&using_sysemu);
387}
388
389static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
390{
391 if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
392 *eof = 1;
393
394 return strlen(buf);
395}
396
397static int proc_write_sysemu(struct file *file,const char *buf, unsigned long count,void *data)
398{
399 char tmp[2];
400
401 if (copy_from_user(tmp, buf, 1))
402 return -EFAULT;
403
404 if (tmp[0] >= '0' && tmp[0] <= '2')
405 set_using_sysemu(tmp[0] - '0');
406 return count; /*We use the first char, but pretend to write everything*/
407}
408
409int __init make_proc_sysemu(void)
410{
411 struct proc_dir_entry *ent;
412 if (!sysemu_supported)
413 return 0;
414
415 ent = create_proc_entry("sysemu", 0600, &proc_root);
416
417 if (ent == NULL)
418 {
419 printk("Failed to register /proc/sysemu\n");
420 return(0);
421 }
422
423 ent->read_proc = proc_read_sysemu;
424 ent->write_proc = proc_write_sysemu;
425
426 return 0;
427}
428
429late_initcall(make_proc_sysemu);
430
431int singlestepping(void * t)
432{
433 struct task_struct *task = t ? t : current;
434
435 if ( ! (task->ptrace & PT_DTRACE) )
436 return(0);
437
438 if (task->thread.singlestep_syscall)
439 return(1);
440
441 return 2;
442}
443
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700444/*
445 * Only x86 and x86_64 have an arch_align_stack().
446 * All other arches have "#define arch_align_stack(x) (x)"
447 * in their asm/system.h
448 * As this is included in UML from asm-um/system-generic.h,
449 * we can use it to behave as the subarch does.
450 */
451#ifndef arch_align_stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452unsigned long arch_align_stack(unsigned long sp)
453{
454 if (randomize_va_space)
455 sp -= get_random_int() % 8192;
456 return sp & ~0xf;
457}
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700458#endif