debuggers.hg

view xenolinux-2.4.21-sparse/arch/xeno/kernel/process.c @ 648:cda951fc1bef

bitkeeper revision 1.341 (3f1120a2WW6KGE81TArq_p654xy38Q)

Merge labyrinth.cl.cam.ac.uk:/auto/groups/xeno/BK/xeno.bk
into labyrinth.cl.cam.ac.uk:/auto/anfs/scratch/labyrinth/iap10/xeno-clone/xeno.bk
author iap10@labyrinth.cl.cam.ac.uk
date Sun Jul 13 09:04:34 2003 +0000 (2003-07-13)
parents 6361e72ebf4c 9339f3942f4e
children
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 /* endless idle loop with no priority at all */
84 init_idle();
85 current->nice = 20;
86 current->counter = -100;
88 while (1) {
89 while (!current->need_resched)
90 HYPERVISOR_yield();
91 schedule();
92 check_pgt_cache();
93 }
94 }
96 void machine_restart(char * __unused)
97 {
98 HYPERVISOR_exit();
99 }
101 void machine_halt(void)
102 {
103 HYPERVISOR_exit();
104 }
106 void machine_power_off(void)
107 {
108 HYPERVISOR_exit();
109 }
111 extern void show_trace(unsigned long* esp);
113 void show_regs(struct pt_regs * regs)
114 {
115 printk("\n");
116 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
117 printk("EIP: %04x:[<%08lx>] CPU: %d",0xffff & regs->xcs,regs->eip, smp_processor_id());
118 if (regs->xcs & 2)
119 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
120 printk(" EFLAGS: %08lx %s\n",regs->eflags, print_tainted());
121 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
122 regs->eax,regs->ebx,regs->ecx,regs->edx);
123 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
124 regs->esi, regs->edi, regs->ebp);
125 printk(" DS: %04x ES: %04x\n",
126 0xffff & regs->xds,0xffff & regs->xes);
128 show_trace(&regs->esp);
129 }
131 /*
132 * No need to lock the MM as we are the last user
133 */
134 void release_segments(struct mm_struct *mm)
135 {
136 void * ldt = mm->context.segments;
138 /*
139 * free the LDT
140 */
141 if (ldt) {
142 mm->context.segments = NULL;
143 clear_LDT();
144 make_pages_writeable(ldt, (LDT_ENTRIES*LDT_ENTRY_SIZE)/PAGE_SIZE);
145 flush_page_update_queue();
146 vfree(ldt);
147 }
148 }
150 /*
151 * Create a kernel thread
152 */
153 int arch_kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
154 {
155 long retval, d0;
157 __asm__ __volatile__(
158 "movl %%esp,%%esi\n\t"
159 "int $0x80\n\t" /* Linux/i386 system call */
160 "cmpl %%esp,%%esi\n\t" /* child or parent? */
161 "je 1f\n\t" /* parent - jump */
162 /* Load the argument into eax, and push it. That way, it does
163 * not matter whether the called function is compiled with
164 * -mregparm or not. */
165 "movl %4,%%eax\n\t"
166 "pushl %%eax\n\t"
167 "call *%5\n\t" /* call fn */
168 "movl %3,%0\n\t" /* exit */
169 "int $0x80\n"
170 "1:\t"
171 :"=&a" (retval), "=&S" (d0)
172 :"0" (__NR_clone), "i" (__NR_exit),
173 "r" (arg), "r" (fn),
174 "b" (flags | CLONE_VM)
175 : "memory");
177 return retval;
178 }
180 /*
181 * Free current thread data structures etc..
182 */
183 void exit_thread(void)
184 {
185 /* nothing to do ... */
186 }
188 void flush_thread(void)
189 {
190 struct task_struct *tsk = current;
192 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
194 /*
195 * Forget coprocessor state..
196 */
197 clear_fpu(tsk);
198 tsk->used_math = 0;
199 }
201 void release_thread(struct task_struct *dead_task)
202 {
203 if (dead_task->mm) {
204 void * ldt = dead_task->mm->context.segments;
206 // temporary debugging check
207 if (ldt) {
208 printk("WARNING: dead process %8s still has LDT? <%p>\n",
209 dead_task->comm, ldt);
210 BUG();
211 }
212 }
213 }
215 /*
216 * we do not have to muck with descriptors here, that is
217 * done in switch_mm() as needed.
218 */
219 void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
220 {
221 struct mm_struct * old_mm;
222 void *old_ldt, *ldt;
224 ldt = NULL;
225 old_mm = current->mm;
226 if (old_mm && (old_ldt = old_mm->context.segments) != NULL) {
227 /*
228 * Completely new LDT, we initialize it from the parent:
229 */
230 ldt = vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
231 if ( ldt == NULL )
232 {
233 printk(KERN_WARNING "ldt allocation failed\n");
234 }
235 else
236 {
237 memcpy(ldt, old_ldt, LDT_ENTRIES*LDT_ENTRY_SIZE);
238 make_pages_readonly(ldt, (LDT_ENTRIES*LDT_ENTRY_SIZE)/PAGE_SIZE);
239 }
240 }
241 new_mm->context.segments = ldt;
242 new_mm->context.cpuvalid = ~0UL; /* valid on all CPU's - they can't have stale data */
243 }
245 /*
246 * Save a segment.
247 */
248 #define savesegment(seg,value) \
249 asm volatile("movl %%" #seg ",%0":"=m" (*(int *)&(value)))
251 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
252 unsigned long unused,
253 struct task_struct * p, struct pt_regs * regs)
254 {
255 struct pt_regs * childregs;
256 unsigned long eflags;
258 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p)) - 1;
259 struct_cpy(childregs, regs);
260 childregs->eax = 0;
261 childregs->esp = esp;
263 p->thread.esp = (unsigned long) childregs;
264 p->thread.esp0 = (unsigned long) (childregs+1);
266 p->thread.eip = (unsigned long) ret_from_fork;
268 savesegment(fs,p->thread.fs);
269 savesegment(gs,p->thread.gs);
271 unlazy_fpu(current);
272 struct_cpy(&p->thread.i387, &current->thread.i387);
275 __asm__ __volatile__ ( "pushfl; popl %0" : "=r" (eflags) : );
276 p->thread.io_pl = (eflags >> 12) & 3;
278 return 0;
279 }
281 /*
282 * fill in the user structure for a core dump..
283 */
284 void dump_thread(struct pt_regs * regs, struct user * dump)
285 {
286 int i;
288 /* changed the size calculations - should hopefully work better. lbt */
289 dump->magic = CMAGIC;
290 dump->start_code = 0;
291 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
292 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
293 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
294 dump->u_dsize -= dump->u_tsize;
295 dump->u_ssize = 0;
296 for (i = 0; i < 8; i++)
297 dump->u_debugreg[i] = current->thread.debugreg[i];
299 if (dump->start_stack < TASK_SIZE)
300 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
302 dump->regs.ebx = regs->ebx;
303 dump->regs.ecx = regs->ecx;
304 dump->regs.edx = regs->edx;
305 dump->regs.esi = regs->esi;
306 dump->regs.edi = regs->edi;
307 dump->regs.ebp = regs->ebp;
308 dump->regs.eax = regs->eax;
309 dump->regs.ds = regs->xds;
310 dump->regs.es = regs->xes;
311 savesegment(fs,dump->regs.fs);
312 savesegment(gs,dump->regs.gs);
313 dump->regs.orig_eax = regs->orig_eax;
314 dump->regs.eip = regs->eip;
315 dump->regs.cs = regs->xcs;
316 dump->regs.eflags = regs->eflags;
317 dump->regs.esp = regs->esp;
318 dump->regs.ss = regs->xss;
320 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
321 }
323 /*
324 * switch_to(x,yn) should switch tasks from x to y.
325 *
326 * We fsave/fwait so that an exception goes off at the right time
327 * (as a call from the fsave or fwait in effect) rather than to
328 * the wrong process. Lazy FP saving no longer makes any sense
329 * with modern CPU's, and this simplifies a lot of things (SMP
330 * and UP become the same).
331 *
332 * NOTE! We used to use the x86 hardware context switching. The
333 * reason for not using it any more becomes apparent when you
334 * try to recover gracefully from saved state that is no longer
335 * valid (stale segment register values in particular). With the
336 * hardware task-switch, there is no way to fix up bad state in
337 * a reasonable manner.
338 *
339 * The fact that Intel documents the hardware task-switching to
340 * be slow is a fairly red herring - this code is not noticeably
341 * faster. However, there _is_ some room for improvement here,
342 * so the performance issues may eventually be a valid point.
343 * More important, however, is the fact that this allows us much
344 * more flexibility.
345 */
346 void __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
347 {
348 struct thread_struct *next = &next_p->thread;
350 __cli();
352 MULTICALL_flush_page_update_queue();
354 /*
355 * This is basically 'unlazy_fpu', except that we queue a multicall to
356 * indicate FPU task switch, rather than synchronously trapping to Xen.
357 */
358 if ( prev_p->flags & PF_USEDFPU )
359 {
360 if ( cpu_has_fxsr )
361 asm volatile( "fxsave %0 ; fnclex"
362 : "=m" (prev_p->thread.i387.fxsave) );
363 else
364 asm volatile( "fnsave %0 ; fwait"
365 : "=m" (prev_p->thread.i387.fsave) );
366 prev_p->flags &= ~PF_USEDFPU;
367 queue_multicall0(__HYPERVISOR_fpu_taskswitch);
368 }
370 queue_multicall2(__HYPERVISOR_stack_switch, __KERNEL_DS, next->esp0);
371 if ( start_info.flags & SIF_PRIVILEGED )
372 {
373 dom0_op_t op;
374 op.cmd = DOM0_IOPL;
375 op.u.iopl.domain = start_info.dom_id;
376 op.u.iopl.iopl = next->io_pl;
377 queue_multicall1(__HYPERVISOR_dom0_op, (unsigned long)&op);
378 }
380 /* EXECUTE ALL TASK SWITCH XEN SYSCALLS AT THIS POINT. */
381 execute_multicall_list();
382 __sti();
384 /*
385 * Restore %fs and %gs.
386 */
387 loadsegment(fs, next->fs);
388 loadsegment(gs, next->gs);
390 /*
391 * Now maybe reload the debug registers
392 */
393 if ( next->debugreg[7] != 0 )
394 {
395 HYPERVISOR_set_debugreg(0, next->debugreg[0]);
396 HYPERVISOR_set_debugreg(1, next->debugreg[1]);
397 HYPERVISOR_set_debugreg(2, next->debugreg[2]);
398 HYPERVISOR_set_debugreg(3, next->debugreg[3]);
399 /* no 4 and 5 */
400 HYPERVISOR_set_debugreg(6, next->debugreg[6]);
401 HYPERVISOR_set_debugreg(7, next->debugreg[7]);
402 }
403 }
405 asmlinkage int sys_fork(struct pt_regs regs)
406 {
407 return do_fork(SIGCHLD, regs.esp, &regs, 0);
408 }
410 asmlinkage int sys_clone(struct pt_regs regs)
411 {
412 unsigned long clone_flags;
413 unsigned long newsp;
415 clone_flags = regs.ebx;
416 newsp = regs.ecx;
417 if (!newsp)
418 newsp = regs.esp;
419 return do_fork(clone_flags, newsp, &regs, 0);
420 }
422 /*
423 * This is trivial, and on the face of it looks like it
424 * could equally well be done in user mode.
425 *
426 * Not so, for quite unobvious reasons - register pressure.
427 * In user mode vfork() cannot have a stack frame, and if
428 * done by calling the "clone()" system call directly, you
429 * do not have enough call-clobbered registers to hold all
430 * the information you need.
431 */
432 asmlinkage int sys_vfork(struct pt_regs regs)
433 {
434 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0);
435 }
437 /*
438 * sys_execve() executes a new program.
439 */
440 asmlinkage int sys_execve(struct pt_regs regs)
441 {
442 int error;
443 char * filename;
445 filename = getname((char *) regs.ebx);
446 error = PTR_ERR(filename);
447 if (IS_ERR(filename))
448 goto out;
449 error = do_execve(filename, (char **) regs.ecx, (char **) regs.edx, &regs);
450 if (error == 0)
451 current->ptrace &= ~PT_DTRACE;
452 putname(filename);
453 out:
454 return error;
455 }
457 /*
458 * These bracket the sleeping functions..
459 */
460 extern void scheduling_functions_start_here(void);
461 extern void scheduling_functions_end_here(void);
462 #define first_sched ((unsigned long) scheduling_functions_start_here)
463 #define last_sched ((unsigned long) scheduling_functions_end_here)
465 unsigned long get_wchan(struct task_struct *p)
466 {
467 unsigned long ebp, esp, eip;
468 unsigned long stack_page;
469 int count = 0;
470 if (!p || p == current || p->state == TASK_RUNNING)
471 return 0;
472 stack_page = (unsigned long)p;
473 esp = p->thread.esp;
474 if (!stack_page || esp < stack_page || esp > 8188+stack_page)
475 return 0;
476 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
477 ebp = *(unsigned long *) esp;
478 do {
479 if (ebp < stack_page || ebp > 8184+stack_page)
480 return 0;
481 eip = *(unsigned long *) (ebp+4);
482 if (eip < first_sched || eip >= last_sched)
483 return eip;
484 ebp = *(unsigned long *) ebp;
485 } while (count++ < 16);
486 return 0;
487 }
488 #undef last_sched
489 #undef first_sched