ia64/xen-unstable

view linux-2.4.27-xen-sparse/arch/xen/kernel/process.c @ 2621:9402048e2325

bitkeeper revision 1.1159.1.218 (416a8128OiHXHyk_Sy8FsA0YUQcEnA)

Merge freefall.cl.cam.ac.uk:/auto/groups/xeno/users/cl349/BK/xeno.bk-26dom0
into freefall.cl.cam.ac.uk:/local/scratch/cl349/xeno.bk-26dom0
author cl349@freefall.cl.cam.ac.uk
date Mon Oct 11 12:48:40 2004 +0000 (2004-10-11)
parents ff4e7a241335
children b914ff7d73b5 3f929065a1d1
line source
1 /*
2 * linux/arch/i386/kernel/process.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 *
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 */
10 /*
11 * This file handles the architecture-dependent parts of process handling..
12 */
14 #define __KERNEL_SYSCALLS__
15 #include <stdarg.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/user.h>
29 #include <linux/a.out.h>
30 #include <linux/interrupt.h>
31 #include <linux/config.h>
32 #include <linux/delay.h>
33 #include <linux/reboot.h>
34 #include <linux/init.h>
35 #include <linux/mc146818rtc.h>
37 #include <asm/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/ldt.h>
42 #include <asm/processor.h>
43 #include <asm/i387.h>
44 #include <asm/desc.h>
45 #include <asm/mmu_context.h>
46 #include <asm/multicall.h>
47 #include <asm/hypervisor-ifs/dom0_ops.h>
49 #include <linux/irq.h>
51 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
53 int hlt_counter;
55 /*
56 * Powermanagement idle function, if any..
57 */
58 void (*pm_idle)(void);
60 /*
61 * Power off function, if any
62 */
63 void (*pm_power_off)(void);
65 void disable_hlt(void)
66 {
67 hlt_counter++;
68 }
70 void enable_hlt(void)
71 {
72 hlt_counter--;
73 }
75 /*
76 * The idle thread. There's no useful work to be
77 * done, so just try to conserve power and have a
78 * low exit latency (ie sit in a loop waiting for
79 * somebody to say that they'd like to reschedule)
80 */
81 void cpu_idle (void)
82 {
83 extern int set_timeout_timer(void);
85 /* Endless idle loop with no priority at all. */
86 init_idle();
87 current->nice = 20;
88 current->counter = -100;
90 for ( ; ; )
91 {
92 while ( !current->need_resched )
93 {
94 __cli();
95 if ( current->need_resched )
96 {
97 /* The race-free check for events failed. */
98 __sti();
99 break;
100 }
101 else if ( set_timeout_timer() == 0 )
102 {
103 /* NB. Blocking reenable events in a race-free manner. */
104 HYPERVISOR_block();
105 }
106 else
107 {
108 /* No race here: yielding will get us the CPU again anyway. */
109 __sti();
110 HYPERVISOR_yield();
111 }
112 }
113 schedule();
114 check_pgt_cache();
115 }
116 }
118 extern void show_trace(unsigned long* esp);
120 void show_regs(struct pt_regs * regs)
121 {
122 printk("\n");
123 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
124 printk("EIP: %04x:[<%08lx>] CPU: %d",0xffff & regs->xcs,regs->eip, smp_processor_id());
125 if (regs->xcs & 2)
126 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
127 printk(" EFLAGS: %08lx %s\n",regs->eflags, print_tainted());
128 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
129 regs->eax,regs->ebx,regs->ecx,regs->edx);
130 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
131 regs->esi, regs->edi, regs->ebp);
132 printk(" DS: %04x ES: %04x\n",
133 0xffff & regs->xds,0xffff & regs->xes);
135 show_trace(&regs->esp);
136 }
139 /*
140 * Create a kernel thread
141 */
142 int arch_kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
143 {
144 long retval, d0;
146 __asm__ __volatile__(
147 "movl %%esp,%%esi\n\t"
148 "int $0x80\n\t" /* Linux/i386 system call */
149 "cmpl %%esp,%%esi\n\t" /* child or parent? */
150 "je 1f\n\t" /* parent - jump */
151 /* Load the argument into eax, and push it. That way, it does
152 * not matter whether the called function is compiled with
153 * -mregparm or not. */
154 "movl %4,%%eax\n\t"
155 "pushl %%eax\n\t"
156 "call *%5\n\t" /* call fn */
157 "movl %3,%0\n\t" /* exit */
158 "int $0x80\n"
159 "1:\t"
160 :"=&a" (retval), "=&S" (d0)
161 :"0" (__NR_clone), "i" (__NR_exit),
162 "r" (arg), "r" (fn),
163 "b" (flags | CLONE_VM)
164 : "memory");
166 return retval;
167 }
169 /*
170 * Free current thread data structures etc..
171 */
172 void exit_thread(void)
173 {
174 /* nothing to do ... */
175 }
177 void flush_thread(void)
178 {
179 struct task_struct *tsk = current;
181 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
183 /*
184 * Forget coprocessor state..
185 */
186 clear_fpu(tsk);
187 tsk->used_math = 0;
188 }
190 void release_thread(struct task_struct *dead_task)
191 {
192 if (dead_task->mm) {
193 // temporary debugging check
194 if (dead_task->mm->context.size) {
195 printk("WARNING: dead process %8s still has LDT? <%p/%08x>\n",
196 dead_task->comm,
197 dead_task->mm->context.ldt,
198 dead_task->mm->context.size);
199 BUG();
200 }
201 }
202 //release_x86_irqs(dead_task);
203 }
206 /*
207 * Save a segment.
208 */
209 #define savesegment(seg,value) \
210 asm volatile("movl %%" #seg ",%0":"=m" (*(int *)&(value)))
212 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
213 unsigned long unused,
214 struct task_struct * p, struct pt_regs * regs)
215 {
216 struct pt_regs * childregs;
217 unsigned long eflags;
219 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p)) - 1;
220 struct_cpy(childregs, regs);
221 childregs->eax = 0;
222 childregs->esp = esp;
224 p->thread.esp = (unsigned long) childregs;
225 p->thread.esp0 = (unsigned long) (childregs+1);
227 p->thread.eip = (unsigned long) ret_from_fork;
229 savesegment(fs,p->thread.fs);
230 savesegment(gs,p->thread.gs);
232 unlazy_fpu(current);
233 struct_cpy(&p->thread.i387, &current->thread.i387);
236 __asm__ __volatile__ ( "pushfl; popl %0" : "=r" (eflags) : );
237 p->thread.io_pl = (eflags >> 12) & 3;
239 return 0;
240 }
242 /*
243 * fill in the user structure for a core dump..
244 */
245 void dump_thread(struct pt_regs * regs, struct user * dump)
246 {
247 int i;
249 /* changed the size calculations - should hopefully work better. lbt */
250 dump->magic = CMAGIC;
251 dump->start_code = 0;
252 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
253 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
254 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
255 dump->u_dsize -= dump->u_tsize;
256 dump->u_ssize = 0;
257 for (i = 0; i < 8; i++)
258 dump->u_debugreg[i] = current->thread.debugreg[i];
260 if (dump->start_stack < TASK_SIZE)
261 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
263 dump->regs.ebx = regs->ebx;
264 dump->regs.ecx = regs->ecx;
265 dump->regs.edx = regs->edx;
266 dump->regs.esi = regs->esi;
267 dump->regs.edi = regs->edi;
268 dump->regs.ebp = regs->ebp;
269 dump->regs.eax = regs->eax;
270 dump->regs.ds = regs->xds;
271 dump->regs.es = regs->xes;
272 savesegment(fs,dump->regs.fs);
273 savesegment(gs,dump->regs.gs);
274 dump->regs.orig_eax = regs->orig_eax;
275 dump->regs.eip = regs->eip;
276 dump->regs.cs = regs->xcs;
277 dump->regs.eflags = regs->eflags;
278 dump->regs.esp = regs->esp;
279 dump->regs.ss = regs->xss;
281 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
282 }
284 /*
285 * switch_to(x,yn) should switch tasks from x to y.
286 *
287 * We fsave/fwait so that an exception goes off at the right time
288 * (as a call from the fsave or fwait in effect) rather than to
289 * the wrong process. Lazy FP saving no longer makes any sense
290 * with modern CPU's, and this simplifies a lot of things (SMP
291 * and UP become the same).
292 *
293 * NOTE! We used to use the x86 hardware context switching. The
294 * reason for not using it any more becomes apparent when you
295 * try to recover gracefully from saved state that is no longer
296 * valid (stale segment register values in particular). With the
297 * hardware task-switch, there is no way to fix up bad state in
298 * a reasonable manner.
299 *
300 * The fact that Intel documents the hardware task-switching to
301 * be slow is a fairly red herring - this code is not noticeably
302 * faster. However, there _is_ some room for improvement here,
303 * so the performance issues may eventually be a valid point.
304 * More important, however, is the fact that this allows us much
305 * more flexibility.
306 */
307 void __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
308 {
309 struct thread_struct *next = &next_p->thread;
311 __cli();
313 /*
314 * We clobber FS and GS here so that we avoid a GPF when restoring previous
315 * task's FS/GS values in Xen when the LDT is switched. If we don't do this
316 * then we can end up erroneously re-flushing the page-update queue when
317 * we 'execute_multicall_list'.
318 */
319 __asm__ __volatile__ (
320 "xorl %%eax,%%eax; movl %%eax,%%fs; movl %%eax,%%gs" : : : "eax" );
322 MULTICALL_flush_page_update_queue();
324 /*
325 * This is basically 'unlazy_fpu', except that we queue a multicall to
326 * indicate FPU task switch, rather than synchronously trapping to Xen.
327 */
328 if ( prev_p->flags & PF_USEDFPU )
329 {
330 if ( cpu_has_fxsr )
331 asm volatile( "fxsave %0 ; fnclex"
332 : "=m" (prev_p->thread.i387.fxsave) );
333 else
334 asm volatile( "fnsave %0 ; fwait"
335 : "=m" (prev_p->thread.i387.fsave) );
336 prev_p->flags &= ~PF_USEDFPU;
337 queue_multicall0(__HYPERVISOR_fpu_taskswitch);
338 }
340 queue_multicall2(__HYPERVISOR_stack_switch, __KERNEL_DS, next->esp0);
341 if ( xen_start_info.flags & SIF_PRIVILEGED )
342 {
343 dom0_op_t op;
344 op.cmd = DOM0_IOPL;
345 op.u.iopl.domain = DOMID_SELF;
346 op.u.iopl.iopl = next->io_pl;
347 queue_multicall1(__HYPERVISOR_dom0_op, (unsigned long)&op);
348 }
350 /* EXECUTE ALL TASK SWITCH XEN SYSCALLS AT THIS POINT. */
351 execute_multicall_list();
352 __sti();
354 /*
355 * Restore %fs and %gs.
356 */
357 loadsegment(fs, next->fs);
358 loadsegment(gs, next->gs);
360 /*
361 * Now maybe reload the debug registers
362 */
363 if ( next->debugreg[7] != 0 )
364 {
365 HYPERVISOR_set_debugreg(0, next->debugreg[0]);
366 HYPERVISOR_set_debugreg(1, next->debugreg[1]);
367 HYPERVISOR_set_debugreg(2, next->debugreg[2]);
368 HYPERVISOR_set_debugreg(3, next->debugreg[3]);
369 /* no 4 and 5 */
370 HYPERVISOR_set_debugreg(6, next->debugreg[6]);
371 HYPERVISOR_set_debugreg(7, next->debugreg[7]);
372 }
373 }
375 asmlinkage int sys_fork(struct pt_regs regs)
376 {
377 return do_fork(SIGCHLD, regs.esp, &regs, 0);
378 }
380 asmlinkage int sys_clone(struct pt_regs regs)
381 {
382 unsigned long clone_flags;
383 unsigned long newsp;
385 clone_flags = regs.ebx;
386 newsp = regs.ecx;
387 if (!newsp)
388 newsp = regs.esp;
389 return do_fork(clone_flags, newsp, &regs, 0);
390 }
392 /*
393 * This is trivial, and on the face of it looks like it
394 * could equally well be done in user mode.
395 *
396 * Not so, for quite unobvious reasons - register pressure.
397 * In user mode vfork() cannot have a stack frame, and if
398 * done by calling the "clone()" system call directly, you
399 * do not have enough call-clobbered registers to hold all
400 * the information you need.
401 */
402 asmlinkage int sys_vfork(struct pt_regs regs)
403 {
404 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0);
405 }
407 /*
408 * sys_execve() executes a new program.
409 */
410 asmlinkage int sys_execve(struct pt_regs regs)
411 {
412 int error;
413 char * filename;
415 filename = getname((char *) regs.ebx);
416 error = PTR_ERR(filename);
417 if (IS_ERR(filename))
418 goto out;
419 error = do_execve(filename, (char **) regs.ecx, (char **) regs.edx, &regs);
420 if (error == 0)
421 current->ptrace &= ~PT_DTRACE;
422 putname(filename);
423 out:
424 return error;
425 }
427 /*
428 * These bracket the sleeping functions..
429 */
430 extern void scheduling_functions_start_here(void);
431 extern void scheduling_functions_end_here(void);
432 #define first_sched ((unsigned long) scheduling_functions_start_here)
433 #define last_sched ((unsigned long) scheduling_functions_end_here)
435 unsigned long get_wchan(struct task_struct *p)
436 {
437 unsigned long ebp, esp, eip;
438 unsigned long stack_page;
439 int count = 0;
440 if (!p || p == current || p->state == TASK_RUNNING)
441 return 0;
442 stack_page = (unsigned long)p;
443 esp = p->thread.esp;
444 if (!stack_page || esp < stack_page || esp > 8188+stack_page)
445 return 0;
446 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
447 ebp = *(unsigned long *) esp;
448 do {
449 if (ebp < stack_page || ebp > 8184+stack_page)
450 return 0;
451 eip = *(unsigned long *) (ebp+4);
452 if (eip < first_sched || eip >= last_sched)
453 return eip;
454 ebp = *(unsigned long *) ebp;
455 } while (count++ < 16);
456 return 0;
457 }
458 #undef last_sched
459 #undef first_sched