home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / arch / i386 / kernel / process.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-01  |  6.2 KB  |  271 lines

  1. /*
  2.  *  linux/arch/i386/kernel/process.c
  3.  *
  4.  *  Copyright (C) 1995  Linus Torvalds
  5.  */
  6.  
  7. /*
  8.  * This file handles the architecture-dependent parts of process handling..
  9.  */
  10.  
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/stddef.h>
  16. #include <linux/unistd.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/malloc.h>
  19. #include <linux/ldt.h>
  20. #include <linux/user.h>
  21. #include <linux/a.out.h>
  22.  
  23. #include <asm/segment.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/system.h>
  26. #include <asm/io.h>
  27.  
  28. asmlinkage void ret_from_sys_call(void) __asm__("ret_from_sys_call");
  29.  
  30. static int hlt_counter=0;
  31.  
  32. void disable_hlt(void)
  33. {
  34.     hlt_counter++;
  35. }
  36.  
  37. void enable_hlt(void)
  38. {
  39.     hlt_counter--;
  40. }
  41.  
  42. /*
  43.  * The idle loop on a i386..
  44.  */
  45. asmlinkage int sys_idle(void)
  46. {
  47.     int i;
  48.  
  49.     if (current->pid != 0)
  50.         return -EPERM;
  51.  
  52.     /* Map out the low memory: it's no longer needed */
  53.     for (i = 0 ; i < 768 ; i++)
  54.         pgd_clear(swapper_pg_dir + i);
  55.  
  56.     /* endless idle loop with no priority at all */
  57.     current->counter = -100;
  58.     for (;;) {
  59.         if (hlt_works_ok && !hlt_counter && !need_resched)
  60.             __asm__("hlt");
  61.         schedule();
  62.     }
  63. }
  64.  
  65. /*
  66.  * This routine reboots the machine by asking the keyboard
  67.  * controller to pulse the reset-line low. We try that for a while,
  68.  * and if it doesn't work, we do some other stupid things.
  69.  */
  70. static long no_idt[2] = {0, 0};
  71.  
  72. static inline void kb_wait(void)
  73. {
  74.     int i;
  75.  
  76.     for (i=0; i<0x10000; i++)
  77.         if ((inb_p(0x64) & 0x02) == 0)
  78.             break;
  79. }
  80.  
  81. void hard_reset_now(void)
  82. {
  83.     int i, j;
  84.  
  85.     sti();
  86. /* rebooting needs to touch the page at absolute addr 0 */
  87.     pg0[0] = 7;
  88.     *((unsigned short *)0x472) = 0x1234;
  89.     for (;;) {
  90.         for (i=0; i<100; i++) {
  91.             kb_wait();
  92.             for(j = 0; j < 100000 ; j++)
  93.                 /* nothing */;
  94.             outb(0xfe,0x64);     /* pulse reset low */
  95.         }
  96.         __asm__ __volatile__("\tlidt %0": "=m" (no_idt));
  97.     }
  98. }
  99.  
  100. void show_regs(struct pt_regs * regs)
  101. {
  102.     printk("\n");
  103.     printk("EIP: %04x:%08lx",0xffff & regs->cs,regs->eip);
  104.     if (regs->cs & 3)
  105.         printk(" ESP: %04x:%08lx",0xffff & regs->ss,regs->esp);
  106.     printk(" EFLAGS: %08lx\n",regs->eflags);
  107.     printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  108.         regs->eax,regs->ebx,regs->ecx,regs->edx);
  109.     printk("ESI: %08lx EDI: %08lx EBP: %08lx",
  110.         regs->esi, regs->edi, regs->ebp);
  111.     printk(" DS: %04x ES: %04x FS: %04x GS: %04x\n",
  112.         0xffff & regs->ds,0xffff & regs->es,
  113.         0xffff & regs->fs,0xffff & regs->gs);
  114. }
  115.  
  116. /*
  117.  * Free current thread data structures etc..
  118.  */
  119. void exit_thread(void)
  120. {
  121.     /* forget local segments */
  122.     __asm__ __volatile__("mov %w0,%%fs ; mov %w0,%%gs ; lldt %w0"
  123.         : /* no outputs */
  124.         : "r" (0));
  125.     current->tss.ldt = 0;
  126.     if (current->ldt) {
  127.         void * ldt = current->ldt;
  128.         current->ldt = NULL;
  129.         vfree(ldt);
  130.     }
  131. }
  132.  
  133. void flush_thread(void)
  134. {
  135.     int i;
  136.  
  137.     if (current->ldt) {
  138.         free_page((unsigned long) current->ldt);
  139.         current->ldt = NULL;
  140.         for (i=1 ; i<NR_TASKS ; i++) {
  141.             if (task[i] == current)  {
  142.                 set_ldt_desc(gdt+(i<<1)+
  143.                          FIRST_LDT_ENTRY,&default_ldt, 1);
  144.                 load_ldt(i);
  145.             }
  146.         }    
  147.     }
  148.  
  149.     for (i=0 ; i<8 ; i++)
  150.         current->debugreg[i] = 0;
  151. }
  152.  
  153. void copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
  154.     struct task_struct * p, struct pt_regs * regs)
  155. {
  156.     int i;
  157.     struct pt_regs * childregs;
  158.  
  159.     p->tss.es = KERNEL_DS;
  160.     p->tss.cs = KERNEL_CS;
  161.     p->tss.ss = KERNEL_DS;
  162.     p->tss.ds = KERNEL_DS;
  163.     p->tss.fs = USER_DS;
  164.     p->tss.gs = KERNEL_DS;
  165.     p->tss.ss0 = KERNEL_DS;
  166.     p->tss.esp0 = p->kernel_stack_page + PAGE_SIZE;
  167.     p->tss.tr = _TSS(nr);
  168.     childregs = ((struct pt_regs *) (p->kernel_stack_page + PAGE_SIZE)) - 1;
  169.     p->tss.esp = (unsigned long) childregs;
  170.     p->tss.eip = (unsigned long) ret_from_sys_call;
  171.     *childregs = *regs;
  172.     childregs->eax = 0;
  173.     childregs->esp = esp;
  174.     p->tss.back_link = 0;
  175.     p->tss.eflags = regs->eflags & 0xffffcfff;    /* iopl is always 0 for a new process */
  176.     p->tss.ldt = _LDT(nr);
  177.     if (p->ldt) {
  178.         p->ldt = (struct desc_struct*) vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
  179.         if (p->ldt != NULL)
  180.             memcpy(p->ldt, current->ldt, LDT_ENTRIES*LDT_ENTRY_SIZE);
  181.     }
  182.     set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
  183.     if (p->ldt)
  184.         set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,p->ldt, 512);
  185.     else
  186.         set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&default_ldt, 1);
  187.     p->tss.bitmap = offsetof(struct thread_struct,io_bitmap);
  188.     for (i = 0; i < IO_BITMAP_SIZE+1 ; i++) /* IO bitmap is actually SIZE+1 */
  189.         p->tss.io_bitmap[i] = ~0;
  190.     if (last_task_used_math == current)
  191.         __asm__("clts ; fnsave %0 ; frstor %0":"=m" (p->tss.i387));
  192. }
  193.  
  194. /*
  195.  * fill in the user structure for a core dump..
  196.  */
  197. void dump_thread(struct pt_regs * regs, struct user * dump)
  198. {
  199.     int i;
  200.  
  201. /* changed the size calculations - should hopefully work better. lbt */
  202.     dump->magic = CMAGIC;
  203.     dump->start_code = 0;
  204.     dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
  205.     dump->u_tsize = ((unsigned long) current->mm->end_code) >> 12;
  206.     dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> 12;
  207.     dump->u_dsize -= dump->u_tsize;
  208.     dump->u_ssize = 0;
  209.     for (i = 0; i < 8; i++)
  210.         dump->u_debugreg[i] = current->debugreg[i];  
  211.  
  212.     if (dump->start_stack < TASK_SIZE)
  213.         dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> 12;
  214.  
  215.     dump->regs = *regs;
  216.  
  217. /* Flag indicating the math stuff is valid. We don't support this for the
  218.    soft-float routines yet */
  219.     if (hard_math) {
  220.         if ((dump->u_fpvalid = current->used_math) != 0) {
  221.             if (last_task_used_math == current)
  222.                 __asm__("clts ; fnsave %0": :"m" (dump->i387));
  223.             else
  224.                 memcpy(&dump->i387,¤t->tss.i387.hard,sizeof(dump->i387));
  225.         }
  226.     } else {
  227.         /* we should dump the emulator state here, but we need to
  228.            convert it into standard 387 format first.. */
  229.         dump->u_fpvalid = 0;
  230.     }
  231. }
  232.  
  233. asmlinkage int sys_fork(struct pt_regs regs)
  234. {
  235.     return do_fork(COPYVM | SIGCHLD, regs.esp, ®s);
  236. }
  237.  
  238. asmlinkage int sys_clone(struct pt_regs regs)
  239. {
  240. #ifdef CLONE_ACTUALLY_WORKS_OK
  241.     unsigned long clone_flags;
  242.     unsigned long newsp;
  243.  
  244.     newsp = regs.ebx;
  245.     clone_flags = regs.ecx;
  246.     if (!newsp)
  247.         newsp = regs.esp;
  248.     if (newsp == regs.esp)
  249.         clone_flags |= COPYVM;
  250.     return do_fork(clone_flags, newsp, ®s);
  251. #else
  252.     return -ENOSYS;
  253. #endif
  254. }
  255.  
  256. /*
  257.  * sys_execve() executes a new program.
  258.  */
  259. asmlinkage int sys_execve(struct pt_regs regs)
  260. {
  261.     int error;
  262.     char * filename;
  263.  
  264.     error = getname((char *) regs.ebx, &filename);
  265.     if (error)
  266.         return error;
  267.     error = do_execve(filename, (char **) regs.ecx, (char **) regs.edx, ®s);
  268.     putname(filename);
  269.     return error;
  270. }
  271.