root/kernel/sysproc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_exit
  2. sys_getpid
  3. sys_fork
  4. sys_wait
  5. sys_sbrk
  6. sys_pause
  7. sys_kill
  8. sys_uptime

   1 #include "types.h"
   2 #include "riscv.h"
   3 #include "defs.h"
   4 #include "param.h"
   5 #include "memlayout.h"
   6 #include "spinlock.h"
   7 #include "proc.h"
   8 #include "vm.h"
   9 
  10 uint64
  11 sys_exit(void)
  12 {
  13   int n;
  14   argint(0, &n);
  15   kexit(n);
  16   return 0; // not reached
  17 }
  18 
  19 uint64
  20 sys_getpid(void)
  21 {
  22   return myproc()->pid;
  23 }
  24 
  25 uint64
  26 sys_fork(void)
  27 {
  28   return kfork();
  29 }
  30 
  31 uint64
  32 sys_wait(void)
  33 {
  34   uint64 p;
  35   argaddr(0, &p);
  36   return kwait(p);
  37 }
  38 
  39 uint64
  40 sys_sbrk(void)
  41 {
  42   uint64 addr;
  43   int t;
  44   int n;
  45 
  46   argint(0, &n);
  47   argint(1, &t);
  48   addr = myproc()->sz;
  49 
  50   if (t == SBRK_EAGER || n < 0) {
  51     if (growproc(n) < 0) {
  52       return -1;
  53     }
  54   } else {
  55     // Lazily allocate memory for this process: increase its memory
  56     // size but don't allocate memory. If the processes uses the
  57     // memory, vmfault() will allocate it.
  58     if (addr + n < addr)
  59       return -1;
  60     if (addr + n > TRAPFRAME)
  61       return -1;
  62     myproc()->sz += n;
  63   }
  64   return addr;
  65 }
  66 
  67 uint64
  68 sys_pause(void)
  69 {
  70   int n;
  71   uint ticks0;
  72 
  73   argint(0, &n);
  74   if (n < 0)
  75     n = 0;
  76   acquire(&tickslock);
  77   ticks0 = ticks;
  78   while (ticks - ticks0 < n) {
  79     if (killed(myproc())) {
  80       release(&tickslock);
  81       return -1;
  82     }
  83     sleep_prepare(&ticks);
  84     release(&tickslock);
  85     sleep();
  86     acquire(&tickslock);
  87   }
  88   release(&tickslock);
  89   return 0;
  90 }
  91 
  92 uint64
  93 sys_kill(void)
  94 {
  95   int pid;
  96 
  97   argint(0, &pid);
  98   return kkill(pid);
  99 }
 100 
 101 // return how many clock tick interrupts have occurred
 102 // since start.
 103 uint64
 104 sys_uptime(void)
 105 {
 106   uint xticks;
 107 
 108   acquire(&tickslock);
 109   xticks = ticks;
 110   release(&tickslock);
 111   return xticks;
 112 }

/* [<][>][^][v][top][bottom][index][help] */