首页 > 开发 > 综合 > 正文

Mandrake9.0的启动过程(从init开始)(一)

2024-07-21 02:35:18
字体:
来源:转载
供稿:网友

  以阅读源代码的方式研究 linux的启动过程,是我早已有之的心愿。今天总算是开工了。由于理解系统初始化过程要有汇编的基础,所以我只好先从init开始。
  init的源代码在/usr/src/linux-2.4.19-9mdk/init目录下,在这个目录下共有三个文件do_mounts.c、main.c和version.c。其中main.c就是init进程的源代码。这段代码并不长,只有640行。
  首先用ctags -x main.c 生成一个tags文件,用vi 打开后,可以看到各个函数的索引:
  LPS_PREC macro 183 main.c #define LPS_PREC 8
  MAX_INIT_ARGS macro 125 main.c #define MAX_INIT_ARGS 8
  MAX_INIT_ENVS macro 126 main.c #define MAX_INIT_ENVS 8
  __KERNEL_SYSCALLS__ macro 12 main.c #define __KERNEL_SYSCALLS__
  argv_init variable 135 main.c static char *
  argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
  calibrate_delay function 185 main.c void __init
  calibrate_delay(void)
  checksetup function 160 main.c static int __init
  checksetup(char *line)
  child_reaper variable 498 main.c strUCt task_struct
  *child_reaper = &init_task;
  cols variable 131 main.c int rows, cols;
  debug_kernel function 226 main.c static int __init
  debug_kernel(char *str)
  do_basic_setup function 521 main.c static void __init
  do_basic_setup(void)
  do_initcalls function 500 main.c static void __init
  do_initcalls(void)
  envp_init variable 136 main.c char *
  envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
  execute_command variable 133 main.c char *execute_command;
  gr_setup function 148 main.c static int __init
  gr_setup(char *str)
  init function 603 main.c static int init(void
  *unused)
  loops_per_jiffy variable 178 main.c unsigned long
  loops_per_jiffy = (1<<12);
  parse_options function 254 main.c static void __init
  parse_options(char *line)
  profile_setup function 138 main.c static int __init
  profile_setup(char *str)
  quiet_kernel function 234 main.c static int __init
  quiet_kernel(char *str)
  rest_init function 389 main.c static void rest_init(void)
  rows variable 131 main.c int rows, cols;
  smp_init function 349 main.c static void __init
  smp_init(void)
  smp_init function 361 main.c static void __init
  smp_init(void)
  smp_init macro 354 main.c #define smp_init() do { }
  while (0)
  start_kernel function 401 main.c asmlinkage void __init
  start_kernel(void)
  wait_init_idle variable 344 main.c unsigned long
  wait_init_idle;
  有了这个索引后,查找函数就方便了。再用vi 打开main.c,找到init函数,如下:
  源码:-------------------------------------------------------
  static int init(void * unused)
  {
   lock_kernel();
   do_basic_setup();
   prepare_namespace();
  #ifdef CONFIG_GRKERNSEC
   grsecurity_init();
  #endif
   /*
   * Ok, we have completed the initial bootup, and
   * we're essentially up and running. Get rid of the
   * initmem segments and start the user-mode stuff..
   */
   free_initmem();
   unlock_kernel();
   if (open("/dev/console", O_RDWR, 0) < 0)
   printk("Warning: unable to open an initial console./n");
   (void) dup(0);
   (void) dup(0);
   /*
   * We try each of these until one succeeds.
   *
   * The Bourne shell can be used instead of init if we are
   * trying to recover a really broken machine.
   */
   if (execute_command)
   execve(execute_command,argv_init,envp_init);
   execve("/sbin/init",argv_init,envp_init);
   execve("/etc/init",argv_init,envp_init);
   execve("/bin/init",argv_init,envp_init);
   execve("/bin/sh",argv_init,envp_init);
   panic("No init found. Try passing init= option to kernel.");
  -----------------------------------------------------------
  在源代码中,可以看到很多如同#ifdef
  CONFIG_GRKERNSEC的宏定义,这些宏定义可以在/usr/src/linux-2.4.19-9mdk/目录下的.config文件中找到。
用vi
  查看.config文件中的宏定义,发现"# CONFIG_GRKERNSEC is not
  set",也就是没有定义,因此,这个宏定义可以不管它。先来看执行流程。
  一、do_basic_setup()函数
  init进程第一个执行的函数是lock_kernel(),这个函数在很多内核的源代码中都有,但我没有找到它的函数定义,只好放弃。
  第二个执行的函数就是do_basic_setup(),这个函数的内容如下:
  源码:-------------------------------------------------------
  /*
   * Ok, the machine is now initialized. None of the devices
   * have been touched yet, but the CPU subsystem is up and
   * running, and memory and process management works.
   *
   * Now we can finally start doing some real work..
   */
  static void __init do_basic_setup(void)
  {
   /*
   * Tell the world that we're going to be the grim
   * reaper of innocent orphaned children.
   *
   * We don't want people to have to make incorrect
   * assumptions about where in the task array this
   * can be found.
   */
   child_reaper = current;
  
  #if defined(CONFIG_MTRR) /* Do this after SMP initialization */
  /*
   * We should probably create some architecture-dependent "fixup after
   * everything is up" style function where this would belong better
   * than in init/main.c..
   */
   mtrr_init();
  #endif /*mtrr(Memory Type Range Register)是Inter P6系列处理器用来控制处理器读写内存范围的。*/
  #ifdef CONFIG_SYSCTL
   sysctl_init();
  #endif /* 对/proc文件系统和sysctl()系统调用相关部分进行初始化*/
   /*
   * Ok, at this point all CPU's should be initialized, so
   * we can start looking into devices..
   */
  #if defined(CONFIG_ARCH_S390)
   s390_init_machine_check();
  #endif
  #ifdef CONFIG_PCI
   pci_init();
  #endif /* 初始化PCI总线 */
  #ifdef CONFIG_SBUS
   sbus_init();
  #endif
  #if defined(CONFIG_PPC)
   ppc_init();
  #endif
  #ifdef CONFIG_MCA
   mca_init();
  #endif
  #ifdef CONFIG_ARCH_ACORN
   ecard_init();
  #endif
  #ifdef CONFIG_ZORRO
   zorro_init();
  #endif
  #ifdef CONFIG_DIO
   dio_init();
  #endif
  #ifdef CONFIG_NUBUS
   nubus_init();
  #endif
  #ifdef CONFIG_ISAPNP
   isapnp_init();
  #endif /* 对ISA总线即插即用初始化 */
  #ifdef CONFIG_TC
   tc_init();
  #endif
  
   /* Networking initialization needs a process context */
   sock_init(); /* 初始化网络协议栈 */
  
   start_context_thread();
   do_initcalls();
  #ifdef CONFIG_IRDA
   irda_proto_init();
   irda_device_init(); /* Must be done after protocol initialization */
  #endif
  #ifdef CONFIG_PCMCIA
   init_pcmcia_ds(); /* Do this last */
  #endif
  }
  ------------------------------------------------------------
  很明显,这段代码是用来进行对系统初始化的。开头的一段注释告诉我们,系统硬件此时只有cpu子系统在运转,内存治理和进程治理也开始工作了。接下来,就是对硬件的初始化。
  这一部分与硬件密切相关,在编译核心时,将根据配置文件.config来编译相应的部分。用vi查看.config文件,发现定义的项目如下:
  CONFIG_MTRR=y
  CONFIG_SYSCTL=y
  CONFIG_PCI=y
  # CONFIG_PCI_GOBIOS is not set
  # CONFIG_PCI_GODIRECT is not set
  CONFIG_PCI_GOANY=y
  CONFIG_PCI_BIOS=y
  CONFIG_PCI_DIRECT=y
  CONFIG_PCI_NAMES=y
  CONFIG_PCI_HERMES=m
  # CONFIG_SBUS is not set
  # CONFIG_MCA is not set
  CONFIG_ISAPNP=y
  CONFIG_TCIC=y
  CONFIG_TC35815=m
  CONFIG_IRDA=m
  CONFIG_IRDA_ULTRA=y
  CONFIG_IRDA_CACHE_LAST_LSAP=y
  CONFIG_IRDA_FAST_RR=y
  # CONFIG_IRDA_DEBUG is not set
  CONFIG_PCMCIA=m
  CONFIG_PCMCIA_AHA152X=m
  CONFIG_PCMCIA_FDOMAIN=m
  C right">(出处:清风软件下载学院)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表