ia64/xen-unstable

view xenolinux-2.4.16-sparse/arch/xeno/kernel/process.c @ 86:4a10fe9b20ec

bitkeeper revision 1.15 (3e24a984iRiWWcgfKCxu2p5q3YbxXw)

Many files:
First half of support for per-domain GDTs and LDTs
author kaf24@labyrinth.cl.cam.ac.uk
date Wed Jan 15 00:21:24 2003 +0000 (2003-01-15)
parents 6b1edd19b627
children b0d356ed774b
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>
36 #include <linux/kdb.h>
38 #include <asm/uaccess.h>
39 #include <asm/pgtable.h>
40 #include <asm/system.h>
41 #include <asm/io.h>
42 #include <asm/ldt.h>
43 #include <asm/processor.h>
44 #include <asm/i387.h>
45 #include <asm/desc.h>
46 #include <asm/mmu_context.h>
48 #include <linux/irq.h>
50 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
52 int hlt_counter;
54 /*
55 * Powermanagement idle function, if any..
56 */
57 void (*pm_idle)(void);
59 /*
60 * Power off function, if any
61 */
62 void (*pm_power_off)(void);
64 void disable_hlt(void)
65 {
66 hlt_counter++;
67 }
69 void enable_hlt(void)
70 {
71 hlt_counter--;
72 }
74 /*
75 * The idle thread. There's no useful work to be
76 * done, so just try to conserve power and have a
77 * low exit latency (ie sit in a loop waiting for
78 * somebody to say that they'd like to reschedule)
79 */
80 void cpu_idle (void)
81 {
82 /* endless idle loop with no priority at all */
83 init_idle();
84 current->nice = 20;
85 current->counter = -100;
87 while (1) {
88 while (!current->need_resched)
89 HYPERVISOR_yield();
90 schedule();
91 check_pgt_cache();
92 }
93 }
95 void machine_restart(char * __unused)
96 {
97 HYPERVISOR_exit();
98 }
100 void machine_halt(void)
101 {
102 HYPERVISOR_exit();
103 }
105 void machine_power_off(void)
106 {
107 HYPERVISOR_exit();
108 }
110 extern void show_trace(unsigned long* esp);
112 void show_regs(struct pt_regs * regs)
113 {
114 printk("\n");
115 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
116 printk("EIP: %04x:[<%08lx>] CPU: %d",0xffff & regs->xcs,regs->eip, smp_processor_id());
117 if (regs->xcs & 2)
118 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
119 printk(" EFLAGS: %08lx %s\n",regs->eflags, print_tainted());
120 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
121 regs->eax,regs->ebx,regs->ecx,regs->edx);
122 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
123 regs->esi, regs->edi, regs->ebp);
124 printk(" DS: %04x ES: %04x\n",
125 0xffff & regs->xds,0xffff & regs->xes);
127 show_trace(&regs->esp);
128 }
130 /*
131 * No need to lock the MM as we are the last user
132 */
133 void release_segments(struct mm_struct *mm)
134 {
135 void * ldt = mm->context.segments;
137 /*
138 * free the LDT
139 */
140 if (ldt) {
141 mm->context.segments = NULL;
142 clear_LDT();
143 vfree(ldt);
144 }
145 }
147 /*
148 * Create a kernel thread
149 */
150 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
151 {
152 long retval, d0;
154 __asm__ __volatile__(
155 "movl %%esp,%%esi\n\t"
156 "int $0x80\n\t" /* Linux/i386 system call */
157 "cmpl %%esp,%%esi\n\t" /* child or parent? */
158 "je 1f\n\t" /* parent - jump */
159 /* Load the argument into eax, and push it. That way, it does
160 * not matter whether the called function is compiled with
161 * -mregparm or not. */
162 "movl %4,%%eax\n\t"
163 "pushl %%eax\n\t"
164 "call *%5\n\t" /* call fn */
165 "movl %3,%0\n\t" /* exit */
166 "int $0x80\n"
167 "1:\t"
168 :"=&a" (retval), "=&S" (d0)
169 :"0" (__NR_clone), "i" (__NR_exit),
170 "r" (arg), "r" (fn),
171 "b" (flags | CLONE_VM)
172 : "memory");
174 return retval;
175 }
177 /*
178 * Free current thread data structures etc..
179 */
180 void exit_thread(void)
181 {
182 /* nothing to do ... */
183 }
185 void flush_thread(void)
186 {
187 struct task_struct *tsk = current;
189 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
191 /*
192 * Forget coprocessor state..
193 */
194 clear_fpu(tsk);
195 tsk->used_math = 0;
196 }
198 void release_thread(struct task_struct *dead_task)
199 {
200 if (dead_task->mm) {
201 void * ldt = dead_task->mm->context.segments;
203 // temporary debugging check
204 if (ldt) {
205 printk("WARNING: dead process %8s still has LDT? <%p>\n",
206 dead_task->comm, ldt);
207 BUG();
208 }
209 }
210 }
212 /*
213 * we do not have to muck with descriptors here, that is
214 * done in switch_mm() as needed.
215 */
216 void copy_segments(struct task_struct *p, struct mm_struct *new_mm)
217 {
218 struct mm_struct * old_mm;
219 void *old_ldt, *ldt;
221 ldt = NULL;
222 old_mm = current->mm;
223 if (old_mm && (old_ldt = old_mm->context.segments) != NULL) {
224 /*
225 * Completely new LDT, we initialize it from the parent:
226 */
227 ldt = vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
228 if (!ldt)
229 printk(KERN_WARNING "ldt allocation failed\n");
230 else
231 memcpy(ldt, old_ldt, LDT_ENTRIES*LDT_ENTRY_SIZE);
232 }
233 new_mm->context.segments = ldt;
234 new_mm->context.cpuvalid = ~0UL; /* valid on all CPU's - they can't have stale data */
235 }
237 /*
238 * Save a segment.
239 */
240 #define savesegment(seg,value) \
241 asm volatile("movl %%" #seg ",%0":"=m" (*(int *)&(value)))
243 int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
244 unsigned long unused,
245 struct task_struct * p, struct pt_regs * regs)
246 {
247 struct pt_regs * childregs;
249 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p)) - 1;
250 struct_cpy(childregs, regs);
251 childregs->eax = 0;
252 childregs->esp = esp;
254 p->thread.esp = (unsigned long) childregs;
255 p->thread.esp0 = (unsigned long) (childregs+1);
257 p->thread.eip = (unsigned long) ret_from_fork;
259 savesegment(fs,p->thread.fs);
260 savesegment(gs,p->thread.gs);
262 unlazy_fpu(current);
263 struct_cpy(&p->thread.i387, &current->thread.i387);
265 return 0;
266 }
268 /*
269 * fill in the user structure for a core dump..
270 */
271 void dump_thread(struct pt_regs * regs, struct user * dump)
272 {
273 int i;
275 /* changed the size calculations - should hopefully work better. lbt */
276 dump->magic = CMAGIC;
277 dump->start_code = 0;
278 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
279 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
280 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
281 dump->u_dsize -= dump->u_tsize;
282 dump->u_ssize = 0;
283 for (i = 0; i < 8; i++)
284 dump->u_debugreg[i] = current->thread.debugreg[i];
286 if (dump->start_stack < TASK_SIZE)
287 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
289 dump->regs.ebx = regs->ebx;
290 dump->regs.ecx = regs->ecx;
291 dump->regs.edx = regs->edx;
292 dump->regs.esi = regs->esi;
293 dump->regs.edi = regs->edi;
294 dump->regs.ebp = regs->ebp;
295 dump->regs.eax = regs->eax;
296 dump->regs.ds = regs->xds;
297 dump->regs.es = regs->xes;
298 savesegment(fs,dump->regs.fs);
299 savesegment(gs,dump->regs.gs);
300 dump->regs.orig_eax = regs->orig_eax;
301 dump->regs.eip = regs->eip;
302 dump->regs.cs = regs->xcs;
303 dump->regs.eflags = regs->eflags;
304 dump->regs.esp = regs->esp;
305 dump->regs.ss = regs->xss;
307 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
308 }
310 /*
311 * switch_to(x,yn) should switch tasks from x to y.
312 *
313 * We fsave/fwait so that an exception goes off at the right time
314 * (as a call from the fsave or fwait in effect) rather than to
315 * the wrong process. Lazy FP saving no longer makes any sense
316 * with modern CPU's, and this simplifies a lot of things (SMP
317 * and UP become the same).
318 *
319 * NOTE! We used to use the x86 hardware context switching. The
320 * reason for not using it any more becomes apparent when you
321 * try to recover gracefully from saved state that is no longer
322 * valid (stale segment register values in particular). With the
323 * hardware task-switch, there is no way to fix up bad state in
324 * a reasonable manner.
325 *
326 * The fact that Intel documents the hardware task-switching to
327 * be slow is a fairly red herring - this code is not noticeably
328 * faster. However, there _is_ some room for improvement here,
329 * so the performance issues may eventually be a valid point.
330 * More important, however, is the fact that this allows us much
331 * more flexibility.
332 */
333 void __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
334 {
335 struct thread_struct *prev = &prev_p->thread,
336 *next = &next_p->thread;
338 unlazy_fpu(prev_p);
340 HYPERVISOR_stack_and_ldt_switch(__KERNEL_DS, next->esp0, 0);
342 /*
343 * Save away %fs and %gs. No need to save %es and %ds, as
344 * those are always kernel segments while inside the kernel.
345 */
346 asm volatile("movl %%fs,%0":"=m" (*(int *)&prev->fs));
347 asm volatile("movl %%gs,%0":"=m" (*(int *)&prev->gs));
349 /*
350 * Restore %fs and %gs.
351 */
352 loadsegment(fs, next->fs);
353 loadsegment(gs, next->gs);
355 /*
356 * Now maybe reload the debug registers
357 */
358 if ( next->debugreg[7] != 0 )
359 {
360 HYPERVISOR_set_debugreg(0, next->debugreg[0]);
361 HYPERVISOR_set_debugreg(1, next->debugreg[1]);
362 HYPERVISOR_set_debugreg(2, next->debugreg[2]);
363 HYPERVISOR_set_debugreg(3, next->debugreg[3]);
364 /* no 4 and 5 */
365 HYPERVISOR_set_debugreg(6, next->debugreg[6]);
366 HYPERVISOR_set_debugreg(7, next->debugreg[7]);
367 }
368 }
370 asmlinkage int sys_fork(struct pt_regs regs)
371 {
372 return do_fork(SIGCHLD, regs.esp, &regs, 0);
373 }
375 asmlinkage int sys_clone(struct pt_regs regs)
376 {
377 unsigned long clone_flags;
378 unsigned long newsp;
380 clone_flags = regs.ebx;
381 newsp = regs.ecx;
382 if (!newsp)
383 newsp = regs.esp;
384 return do_fork(clone_flags, newsp, &regs, 0);
385 }
387 /*
388 * This is trivial, and on the face of it looks like it
389 * could equally well be done in user mode.
390 *
391 * Not so, for quite unobvious reasons - register pressure.
392 * In user mode vfork() cannot have a stack frame, and if
393 * done by calling the "clone()" system call directly, you
394 * do not have enough call-clobbered registers to hold all
395 * the information you need.
396 */
397 asmlinkage int sys_vfork(struct pt_regs regs)
398 {
399 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0);
400 }
402 /*
403 * sys_execve() executes a new program.
404 */
405 asmlinkage int sys_execve(struct pt_regs regs)
406 {
407 int error;
408 char * filename;
410 filename = getname((char *) regs.ebx);
411 error = PTR_ERR(filename);
412 if (IS_ERR(filename))
413 goto out;
414 error = do_execve(filename, (char **) regs.ecx, (char **) regs.edx, &regs);
415 if (error == 0)
416 current->ptrace &= ~PT_DTRACE;
417 putname(filename);
418 out:
419 return error;
420 }
422 /*
423 * These bracket the sleeping functions..
424 */
425 extern void scheduling_functions_start_here(void);
426 extern void scheduling_functions_end_here(void);
427 #define first_sched ((unsigned long) scheduling_functions_start_here)
428 #define last_sched ((unsigned long) scheduling_functions_end_here)
430 unsigned long get_wchan(struct task_struct *p)
431 {
432 unsigned long ebp, esp, eip;
433 unsigned long stack_page;
434 int count = 0;
435 if (!p || p == current || p->state == TASK_RUNNING)
436 return 0;
437 stack_page = (unsigned long)p;
438 esp = p->thread.esp;
439 if (!stack_page || esp < stack_page || esp > 8188+stack_page)
440 return 0;
441 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
442 ebp = *(unsigned long *) esp;
443 do {
444 if (ebp < stack_page || ebp > 8184+stack_page)
445 return 0;
446 eip = *(unsigned long *) (ebp+4);
447 if (eip < first_sched || eip >= last_sched)
448 return eip;
449 ebp = *(unsigned long *) ebp;
450 } while (count++ < 16);
451 return 0;
452 }
453 #undef last_sched
454 #undef first_sched