Fork me on GitHub

root/kernel/init.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. init_trampoline
  2. init_init

   1 /*
   2  * fiwix/kernel/init.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/asm.h>
   9 #include <fiwix/kernel.h>
  10 #include <fiwix/system.h>
  11 #include <fiwix/mm.h>
  12 #include <fiwix/timer.h>
  13 #include <fiwix/sched.h>
  14 #include <fiwix/fcntl.h>
  15 #include <fiwix/stat.h>
  16 #include <fiwix/process.h>
  17 #include <fiwix/syscalls.h>
  18 #include <fiwix/unistd.h>
  19 #include <fiwix/stdio.h>
  20 #include <fiwix/string.h>
  21 
  22 #define INIT_TRAMPOLINE_SIZE    128     /* max. size of init_trampoline() */
  23 
  24 char *init_argv[] = { INIT_PROGRAM, NULL, NULL };
  25 char *init_envp[] = { "HOME=/", "TERM=linux", NULL };
  26 
  27 static void init_trampoline(void)
  28 {
  29         USER_SYSCALL(SYS_open, "/dev/console", O_RDWR, 0);      /* stdin */
  30         USER_SYSCALL(SYS_dup, 0, NULL, NULL);                   /* stdout */
  31         USER_SYSCALL(SYS_dup, 0, NULL, NULL);                   /* stderr */
  32         USER_SYSCALL(SYS_execve, INIT_PROGRAM, init_argv, init_envp);
  33 
  34         /* only reached in case of error in sys_execve() */
  35         USER_SYSCALL(SYS_exit, NULL, NULL, NULL);
  36 }
  37 
  38 void init_init(void)
  39 {
  40         int n;
  41         unsigned int page;
  42         struct inode *i;
  43         unsigned int *pgdir;
  44         struct proc *init;
  45 
  46         if(namei(INIT_PROGRAM, &i, NULL, FOLLOW_LINKS)) {
  47                 PANIC("can't find %s.\n", INIT_PROGRAM);
  48         }
  49         if(!S_ISREG(i->i_mode)) {
  50                 PANIC("%s is not a regular file.\n", INIT_PROGRAM);
  51         }
  52         iput(i);
  53 
  54         /* INIT slot was already created in main.c */
  55         init = &proc_table[INIT];
  56 
  57         /* INIT process starts with the current (kernel) Page Directory */
  58         if(!(pgdir = (void *)kmalloc())) {
  59                 goto init_init__die;
  60         }
  61         init->rss++;
  62         memcpy_b(pgdir, kpage_dir, PAGE_SIZE);
  63         init->tss.cr3 = V2P((unsigned int)pgdir);
  64 
  65         memset_b(init->vma, NULL, sizeof(init->vma));
  66         init->ppid = 0;
  67         init->pgid = 0;
  68         init->sid = 0;
  69         init->flags = 0;
  70         init->children = 0;
  71         init->priority = DEF_PRIORITY;
  72         init->start_time = CURRENT_TICKS;
  73         init->sleep_address = NULL;
  74         init->uid = init->gid = 0;
  75         init->euid = init->egid = 0;
  76         init->suid = init->sgid = 0;
  77         memset_b(init->fd, NULL, sizeof(init->fd));
  78         memset_b(init->fd_flags, NULL, sizeof(init->fd_flags));
  79         init->root = current->root;
  80         init->pwd = current->pwd;
  81         strcpy(init->argv0, init_argv[0]);
  82         init_argv[1] = init_args;
  83         sprintk(init->pidstr, "%d", init->pid);
  84         init->sigpending = 0;
  85         init->sigblocked = 0;
  86         init->sigexecuting = 0;
  87         memset_b(init->sigaction, NULL, sizeof(init->sigaction));
  88         memset_b(&init->usage, NULL, sizeof(struct rusage));
  89         memset_b(&init->cusage, NULL, sizeof(struct rusage));
  90         init->timeout = 0;
  91         for(n = 0; n < RLIM_NLIMITS; n++) {
  92                 init->rlim[n].rlim_cur = init->rlim[n].rlim_max = RLIM_INFINITY;
  93         }
  94         init->rlim[RLIMIT_NOFILE].rlim_cur = OPEN_MAX;
  95         init->rlim[RLIMIT_NOFILE].rlim_max = NR_OPENS;
  96         init->rlim[RLIMIT_NPROC].rlim_cur = CHILD_MAX;
  97         init->rlim[RLIMIT_NPROC].rlim_cur = NR_PROCS;
  98         init->umask = 0022;
  99 
 100         /* setup the stack */
 101         if(!(init->tss.esp0 = kmalloc())) {
 102                 goto init_init__die;
 103         }
 104         init->tss.esp0 += PAGE_SIZE - 4;
 105         init->rss++;
 106         init->tss.ss0 = KERNEL_DS;
 107 
 108         /* setup the init_trampoline */
 109         page = map_page(init, KERNEL_BASE_ADDR - PAGE_SIZE, 0, PROT_READ | PROT_WRITE);
 110         memcpy_b((void *)page, init_trampoline, INIT_TRAMPOLINE_SIZE);
 111 
 112         init->tss.eip = (unsigned int)switch_to_user_mode;
 113         init->tss.esp = page + PAGE_SIZE - 4;
 114 
 115         init->state = PROC_RUNNING;
 116         nr_processes++;
 117         return;
 118 
 119 init_init__die:
 120         PANIC("unable to run init process.\n");
 121 }

/* [previous][next][first][last][top][bottom][index][help] */