Advertisement
iocoder

kernel/arch/proc.c

Nov 22nd, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. /*
  2.  *        +----------------------------------------------------------+
  3.  *        | +------------------------------------------------------+ |
  4.  *        | |  Quafios Kernel 1.0.2.                               | |
  5.  *        | |  -> i386: process operations.                        | |
  6.  *        | +------------------------------------------------------+ |
  7.  *        +----------------------------------------------------------+
  8.  *
  9.  * This file is part of Quafios 1.0.2 source code.
  10.  * Copyright (C) 2014  Mostafa Abd El-Aziz Mohamed.
  11.  *
  12.  * This program is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with Quafios.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * Visit http://www.quafios.com/ for contact information.
  26.  *
  27.  */
  28.  
  29. #include <arch/type.h>
  30. #include <sys/proc.h>
  31. #include <sys/scheduler.h>
  32. #include <sys/error.h>
  33.  
  34. #include <i386/stack.h>
  35. #include <i386/asm.h>
  36.  
  37. void umode_jmp(int32_t vaddr, int32_t sp) {
  38.     /* used by: exec(). */
  39.     __asm__("pushl $0x2B; # 0x28 + 3 (user mode) \n \
  40.             pushl %%ebx;                        \n \
  41.             pushf      ;                        \n \
  42.             pushl $0x23; # 0x20 + 3 (user mode) \n \
  43.             pushl %%eax;                        \n \
  44.             pushw $0x2B;                        \n \
  45.             pushw $0x2B;                        \n \
  46.             pushw $0x2B;                        \n \
  47.             pushw $0x2B;                        \n \
  48.             popw   %%ds;                        \n \
  49.             popw   %%es;                        \n \
  50.             popw   %%fs;                        \n \
  51.             popw   %%gs;                        \n \
  52.             iret       ; "::"a"(vaddr), "b"(sp));
  53. }
  54.  
  55. void copy_context(proc_t *child) {
  56.     /* store the context of current process into child.
  57.      * used by: fork().
  58.      */
  59.     Regs *regs = (Regs *) child->kstack;
  60.     *regs = *((Regs *) curproc->context);
  61.     child->context = (void *) regs;
  62.     child->reg1 = (uint32_t) regs;
  63.     child->reg2 = (uint32_t) regs;
  64.     regs->esp = (uint32_t) &(regs->err);
  65.     regs->eax = 0; /* 0 should be returned to the child. */
  66.     return;
  67. }
  68.  
  69. void arch_proc_switch(proc_t *oldproc, proc_t *newproc) {
  70.     /* switch between two processes! */
  71.  
  72.     /* update Task Segment: */
  73.     ts.esp0 = (uint32_t) &newproc->kstack[KERNEL_STACK_SIZE];
  74.  
  75.     /* update CR3: */
  76.     arch_vmswitch(&(newproc->umem));
  77.  
  78.     /* store stack parameters: */
  79.     __asm__("mov %%ebp, %%eax":"=a"(oldproc->reg1));
  80.     __asm__("mov %%esp, %%eax":"=a"(oldproc->reg2));
  81.  
  82.     /* retrieve stack parameters of the new process: */
  83.     if (!(newproc->after_fork)) {
  84.         /* get stack parameters: */
  85.         int32_t ebp = newproc->reg1;
  86.         int32_t esp = newproc->reg2;
  87.         __asm__("mov %%eax, %%ebp; \n\
  88.                 mov %%ebx, %%esp;"::"a"(ebp), "b"(esp));
  89.         return;
  90.     }
  91.  
  92.     /* special case: a process that has just forked: */
  93.     newproc->after_fork = 0;
  94.  
  95.     /* Send End of Interrupt Command (very tricky): */
  96.     end_irq0();
  97.  
  98.     /* get the context of the new process */
  99.     __asm__("mov %%eax, %%esp"::"a"(newproc->reg2));
  100.  
  101.     /* Clear interrupt flag temporarily. */
  102.     cli();
  103.  
  104.     /* return */
  105.     restore_reg();
  106.     __asm__("add $4, %esp");
  107.     iret();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement