自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手把手教你分析 Linux 啟動(dòng)流程

系統(tǒng) Linux
本文主要講解當(dāng)從 bootloader 跳轉(zhuǎn)到 Linux 系統(tǒng)的啟動(dòng)函數(shù) start_kernel 后,此函數(shù)對(duì)系統(tǒng)初始化的流程。

[[424755]]

下載 Linux 內(nèi)核網(wǎng)址:

https://www.kernel.org/

最新 Linux 內(nèi)核是 5.15 版本?,F(xiàn)在常用 Linux 內(nèi)核源碼為4.14、4.19、4.9 等版本,其中 4.14 版本源碼壓縮包大概 90+M,解壓后 700+M,合計(jì) 61350 個(gè)文件。如此眾多的文件,用 source insight 或者 VSCode 查看都會(huì)比較卡,所以可以采用在線(xiàn)查看的方式。

在線(xiàn)查看 Linux 內(nèi)核源碼網(wǎng)址:

https://elixir.bootlin.com/linux/latest/source

在線(xiàn)查看 Android 源碼:

http://androidxref.com/

Android系統(tǒng)是基于Linux 內(nèi)核的,最底層為L(zhǎng)inux內(nèi)核,源碼量翻很多倍。所以用軟件看安卓源碼更卡,可以使用在線(xiàn)網(wǎng)址看源碼。

我們知道,Linux 系統(tǒng)的啟動(dòng),前面有一個(gè)啟動(dòng)引導(dǎo)程序 bootloader,比如常用的 uboot,本文不分析 uboot 的啟動(dòng),只放一張流程圖:

本文主要講解當(dāng)從 bootloader 跳轉(zhuǎn)到 Linux 系統(tǒng)的啟動(dòng)函數(shù) start_kernel 后,此函數(shù)對(duì)系統(tǒng)初始化的流程。

在 linux4.14/arch/arm/kernel/head.S 文件中,是最后匯編階段的初始化,而后會(huì)跳轉(zhuǎn)到 main.c 文件的 start_kernel 函數(shù),在此做 Linux 啟動(dòng)初始化,在這個(gè)函數(shù)中會(huì)調(diào)用將近100個(gè)函數(shù)去完成 Linux 系統(tǒng)的初始化,調(diào)用函數(shù)如下(不同內(nèi)核版本,順序和細(xì)節(jié)有變化):

linux4.14/init/main.c,start_kernel 函數(shù)。

  1. asmlinkage __visible void __init start_kernel(void) 
  2.  char *command_line; 
  3.  char *after_dashes; 
  4.  
  5.  set_task_stack_end_magic(&init_task); 
  6.  smp_setup_processor_id(); 
  7.  debug_objects_early_init(); 
  8.  
  9.  cgroup_init_early(); 
  10.  
  11.  local_irq_disable(); 
  12.  early_boot_irqs_disabled = true
  13.  /* 
  14.   * Interrupts are still disabled. Do necessary setups, then 
  15.   * enable them. 
  16.   */ 
  17.  boot_cpu_init(); 
  18.  page_address_init(); 
  19.  pr_notice("%s", linux_banner); 
  20.  setup_arch(&command_line); 
  21.  /* 
  22.   * Set up the the initial canary and entropy after arch 
  23.   * and after adding latent and command line entropy. 
  24.   */ 
  25.  add_latent_entropy(); 
  26.  add_device_randomness(command_line, strlen(command_line)); 
  27.  boot_init_stack_canary(); 
  28.  mm_init_cpumask(&init_mm); 
  29.  setup_command_line(command_line); 
  30.  setup_nr_cpu_ids(); 
  31.  setup_per_cpu_areas(); 
  32.  smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */ 
  33.  boot_cpu_hotplug_init(); 
  34.  
  35.  build_all_zonelists(NULL); 
  36.  page_alloc_init(); 
  37.  
  38.  pr_notice("Kernel command line: %s\n", boot_command_line); 
  39.  /* parameters may set static keys */ 
  40.  jump_label_init(); 
  41.  parse_early_param(); 
  42.  after_dashes = parse_args("Booting kernel"
  43.       static_command_line, __start___param, 
  44.       __stop___param - __start___param, 
  45.       -1, -1, NULL, &unknown_bootoption); 
  46.  if (!IS_ERR_OR_NULL(after_dashes)) 
  47.   parse_args("Setting init args", after_dashes, NULL, 0, -1, -1, 
  48.       NULL, set_init_arg); 
  49.  /* 
  50.   * These use large bootmem allocations and must precede 
  51.   * kmem_cache_init() 
  52.   */ 
  53.  setup_log_buf(0); 
  54.  pidhash_init(); 
  55.  vfs_caches_init_early(); 
  56.  sort_main_extable(); 
  57.  trap_init(); 
  58.  mm_init(); 
  59.  
  60.  ftrace_init(); 
  61.  
  62.  /* trace_printk can be enabled here */ 
  63.  early_trace_init(); 
  64.  /* 
  65.   * Set up the scheduler prior starting any interrupts (such as the 
  66.   * timer interrupt). Full topology setup happens at smp_init() 
  67.   * time - but meanwhile we still have a functioning scheduler. 
  68.   */ 
  69.  sched_init(); 
  70.  /* 
  71.   * Disable preemption - early bootup scheduling is extremely 
  72.   * fragile until we cpu_idle() for the first time
  73.   */ 
  74.  preempt_disable(); 
  75.  if (WARN(!irqs_disabled(), 
  76.    "Interrupts were enabled *very* early, fixing it\n")) 
  77.   local_irq_disable(); 
  78.  radix_tree_init(); 
  79.  /* 
  80.   * Allow workqueue creation and work item queueing/cancelling 
  81.   * early.  Work item execution depends on kthreads and starts after 
  82.   * workqueue_init(). 
  83.   */ 
  84.  workqueue_init_early(); 
  85.  
  86.  rcu_init(); 
  87.  
  88.  /* Trace events are available after this */ 
  89.  trace_init(); 
  90.  
  91.  context_tracking_init(); 
  92.  /* init some links before init_ISA_irqs() */ 
  93.  early_irq_init(); 
  94.  init_IRQ(); 
  95.  tick_init(); 
  96.  rcu_init_nohz(); 
  97.  init_timers(); 
  98.  hrtimers_init(); 
  99.  softirq_init(); 
  100.  timekeeping_init(); 
  101.  time_init(); 
  102.  sched_clock_postinit(); 
  103.  printk_safe_init(); 
  104.  perf_event_init(); 
  105.  profile_init(); 
  106.  call_function_init(); 
  107.  WARN(!irqs_disabled(), "Interrupts were enabled early\n"); 
  108.  early_boot_irqs_disabled = false
  109.  local_irq_enable(); 
  110.  
  111.  kmem_cache_init_late(); 
  112.  /* 
  113.   * HACK ALERT! This is early. We're enabling the console before 
  114.   * we've done PCI setups etc, and console_init() must be aware of 
  115.   * this. But we do want output early, in case something goes wrong. 
  116.   */ 
  117.  console_init(); 
  118.  if (panic_later) 
  119.   panic("Too many boot %s vars at `%s'", panic_later, 
  120.         panic_param); 
  121.  
  122.  lockdep_info(); 
  123.  /* 
  124.   * Need to run this when irqs are enabled, because it wants 
  125.   * to self-test [hard/soft]-irqs on/off lock inversion bugs 
  126.   * too: 
  127.   */ 
  128.  locking_selftest(); 
  129.  /* 
  130.   * This needs to be called before any devices perform DMA 
  131.   * operations that might use the SWIOTLB bounce buffers. It will 
  132.   * mark the bounce buffers as decrypted so that their usage will 
  133.   * not cause "plain-text" data to be decrypted when accessed. 
  134.   */ 
  135.  mem_encrypt_init(); 
  136.  
  137. #ifdef CONFIG_BLK_DEV_INITRD 
  138.  if (initrd_start && !initrd_below_start_ok && 
  139.      page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) { 
  140.   pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n"
  141.       page_to_pfn(virt_to_page((void *)initrd_start)), 
  142.       min_low_pfn); 
  143.   initrd_start = 0; 
  144.  } 
  145. #endif 
  146.  kmemleak_init(); 
  147.  debug_objects_mem_init(); 
  148.  setup_per_cpu_pageset(); 
  149.  numa_policy_init(); 
  150.  if (late_time_init) 
  151.   late_time_init(); 
  152.  calibrate_delay(); 
  153.  pidmap_init(); 
  154.  anon_vma_init(); 
  155.  acpi_early_init(); 
  156. #ifdef CONFIG_X86 
  157.  if (efi_enabled(EFI_RUNTIME_SERVICES)) 
  158.   efi_enter_virtual_mode(); 
  159. #endif 
  160.  thread_stack_cache_init(); 
  161.  cred_init(); 
  162.  fork_init(); 
  163.  proc_caches_init(); 
  164.  buffer_init(); 
  165.  key_init(); 
  166.  security_init(); 
  167.  dbg_late_init(); 
  168.  vfs_caches_init(); 
  169.  pagecache_init(); 
  170.  signals_init(); 
  171.  proc_root_init(); 
  172.  nsfs_init(); 
  173.  cpuset_init(); 
  174.  cgroup_init(); 
  175.  taskstats_init_early(); 
  176.  delayacct_init(); 
  177.  
  178.  check_bugs(); 
  179.  
  180.  acpi_subsystem_init(); 
  181.  arch_post_acpi_subsys_init(); 
  182.  sfi_init_late(); 
  183.  
  184.  if (efi_enabled(EFI_RUNTIME_SERVICES)) { 
  185.   efi_free_boot_services(); 
  186.  } 
  187.  /* Do the rest non-__init'ed, we're now alive */ 
  188.  rest_init(); 
  189.  
  190.  prevent_tail_call_optimization(); 

其中有七個(gè)函數(shù)較為重要,分別為:

  1. setup_arch(&command_line); 
  2.  
  3. mm_init(); 
  4.  
  5. sched_init(); 
  6.  
  7. init_IRQ(); 
  8.  
  9. console_init(); 
  10.  
  11. vfs_caches_init(); 
  12.  
  13. rest_init(); 

1、setup_arch(&command_line)

此函數(shù)是系統(tǒng)架構(gòu)初始化函數(shù),處理 uboot 傳遞進(jìn)來(lái)的參數(shù),不同的架構(gòu)進(jìn)行不同的初始化,也就是說(shuō)每個(gè)架構(gòu)都會(huì)有一個(gè) setup_arch 函數(shù)。

linux4.14/arch/arm/kernel/setup.c

2、mm_init

內(nèi)存初始化函數(shù)

linux4.14/init/main.c

3、sched_init

核心進(jìn)程調(diào)度器初始化。Linux 內(nèi)核實(shí)現(xiàn)了四種調(diào)度方式,一般是采用 CFS 調(diào)度方式。作為一個(gè)普適性的操作系統(tǒng),必須考慮各種需求,我們不能只按照中斷優(yōu)先級(jí)或者時(shí)間輪轉(zhuǎn)片來(lái)規(guī)定進(jìn)程運(yùn)行的時(shí)間。作為一個(gè)多用戶(hù)操作系統(tǒng),必須考慮到每個(gè)用戶(hù)的公平性。不能因?yàn)橐粋€(gè)用戶(hù)沒(méi)有高級(jí)權(quán)限,就限制他的進(jìn)程的運(yùn)行時(shí)間,要考慮每個(gè)用戶(hù)擁有公平的時(shí)間。

linux4.14/kernel/sched/core.c

4、init_IRQ

中斷初始化函數(shù),這個(gè)很好理解,大家都用過(guò)中斷。

linux4.14/arch/arm/kernel/irq.c

5、console_init

在這個(gè)函數(shù)初始化之前,你所有寫(xiě)的內(nèi)核打印函數(shù) printk 都打印不出東西。在這個(gè)函數(shù)初始化之前,所有打印都會(huì)存在 buf 里,此函數(shù)初始化以后,會(huì)將 buf里面的數(shù)據(jù)打印出來(lái),你才能在終端看到 printk 打印的東西。

tty 是 Linux 中的終端, _con_initcall_start 和_con_initcall_end 這兩句的意思是執(zhí)行所有兩者之間的 initcall 函數(shù)。

linux4.14/kernel/printk/printk.c

6、vfs_caches_init

虛擬文件系統(tǒng)初始化,比如 sysfs,根文件系統(tǒng)等,就是在這一步進(jìn)行掛載,proc 是內(nèi)核虛擬的,用來(lái)輸出內(nèi)核數(shù)據(jù)結(jié)構(gòu)信息,不算在這里。

vfs虛擬文件系統(tǒng),屏蔽了底層硬件的不同,提供了統(tǒng)一了接口,方便系統(tǒng)的移植和使用。使用戶(hù)在不用更改應(yīng)用代碼的情況下直接移植代碼到其他平臺(tái)。

linux4.14/fs/dcache.c

這里的掛載主要在mnt_init()函數(shù)中:

linux4.14/fs/namespace.c

7、rest_init

這個(gè)函數(shù)可以算是 start_kernel函數(shù)調(diào)用的最后一個(gè)函數(shù),在這里產(chǎn)生了最重要的兩個(gè)內(nèi)核進(jìn)程 kernel_init 和 kthreadd,kernel_init后面會(huì)從內(nèi)核空間跳轉(zhuǎn)到用戶(hù)空間,變成用戶(hù)空間的 init 進(jìn)程,PID=1,而 kthreadd ,PID=2,是內(nèi)核進(jìn)程,專(zhuān)門(mén)用來(lái)監(jiān)聽(tīng)創(chuàng)建內(nèi)核進(jìn)程的請(qǐng)求,它維護(hù)了一個(gè)鏈表,如果有創(chuàng)建內(nèi)核進(jìn)程的需求,就會(huì)在鏈表上創(chuàng)建。

至此,用戶(hù)空間最重要的 init 進(jìn)程已經(jīng)出來(lái),后面用戶(hù)空間的進(jìn)程都由 init進(jìn)程來(lái) fork。如果是安卓系統(tǒng),init 進(jìn)程會(huì) fork 出一個(gè) zygote 進(jìn)程,他是所有安卓系統(tǒng)進(jìn)程的父進(jìn)程。

linux4.14/init.main.c

上圖,400 行創(chuàng)建了 kernel_init 進(jìn)程,412 行創(chuàng)建了 kthreadd 進(jìn)程,這兩個(gè)都是內(nèi)核進(jìn)程。426 行通知 kernel_init 進(jìn)程 kthreadd 已經(jīng)創(chuàng)建完畢。也就是說(shuō),實(shí)際上是 kthreadd 先運(yùn)行,kernel_init 再運(yùn)行。

其余的函數(shù)大家可以參照下面的文章去理解:

https://www.cnblogs.com/andyfly/p/9410441.html

https://www.cnblogs.com/lifexy/p/7366782.html 

https://www.cnblogs.com/yanzs/p/13910344.html#radix_tree:init

本文轉(zhuǎn)載自微信公眾號(hào)「嵌入式Linux系統(tǒng)開(kāi)發(fā)」

 

責(zé)任編輯:姜華 來(lái)源: 嵌入式Linux系統(tǒng)開(kāi)發(fā)
相關(guān)推薦

2021-09-22 08:51:34

Android

2011-01-10 14:41:26

2020-04-14 10:20:12

MySQL數(shù)據(jù)庫(kù)死鎖

2024-10-16 11:40:47

2022-01-17 07:50:37

Linux Patch項(xiàng)目

2021-07-14 09:00:00

JavaFX開(kāi)發(fā)應(yīng)用

2011-05-03 15:59:00

黑盒打印機(jī)

2017-05-18 12:45:35

數(shù)據(jù)分析數(shù)據(jù)理解數(shù)據(jù)

2009-06-15 16:58:57

Java安裝Linux

2020-06-01 16:25:43

WindowsLinux命令

2025-02-26 07:40:25

運(yùn)營(yíng)分析體系運(yùn)營(yíng)策略

2022-11-06 14:46:28

腳本windows文件

2022-01-08 20:04:20

攔截系統(tǒng)調(diào)用

2022-03-14 14:47:21

HarmonyOS操作系統(tǒng)鴻蒙

2023-04-26 12:46:43

DockerSpringKubernetes

2022-12-07 08:42:35

2022-07-27 08:16:22

搜索引擎Lucene

2021-02-26 11:54:38

MyBatis 插件接口

2011-02-22 13:46:27

微軟SQL.NET

2021-12-28 08:38:26

Linux 中斷喚醒系統(tǒng)Linux 系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)