Linux|Kernel启动流程源码分析

一起学嵌入式 2023-03-30 11:16

点击下方一起学嵌入式】关注,一起学习,一起成长

内核生命周期

uboot 打印完 Starting kernel . . .,就完成了自己的使命,控制权便交给了 kernel 的第一条指令,也就是下面这个函数。

init/main.c

    asmlinkage __visible void __init start_kernel(void)
    {
     ...
     rest_init();
    }

start_kernel 相当于内核的 main 函数,内核的生命周期就是从执行这个函数的第一条语句开始的,直到最后一个函数 reset_init(),内核将不再从这个函数中返回,而是陷入这个函数里面的一个 while(1) 死循环,这个死循环被作为 idle 进程,也就是 0 号进程。

所以,内核的生命周期,就是一个完整的 start_kernel 函数。始于 start_kernel 函数的第一条语句,停留在最后的死循环。

init 进程

kernel 会创建众多内核线程,来持续致力于内存、磁盘、CPU 的管理,其中有两个内核线程比较重要,需要我们重点讲解,那就是 1 号内核线程 kernel_init 和 2 号内核线程 kthreadd。1 号内核线程最终会被用户的第一个进程 init 代替,也就成了 1 号进程。如下:

    # ps
    PID   USER     COMMAND
        1 root     init
        2 root     [kthreadd]
        3 root     [rcu_gp]
        4 root     [rcu_par_gp]
        7 root     [kworker/u4:0-ev]
        8 root     [mm_percpu_wq]
        9 root     [ksoftirqd/0]
    ...

COMMAND 这一列,带中括号的是内核线程,不带中括号的是用户进程。从 PID 统一编址就可以看出,它俩地位是一样的。
下面我们深入分析一下从 start_kernel 到最终运行 init 进程,kernel 都经历了什么

打印

添加打印,是分析流程的好方法。

    asmlinkage __visible void __init start_kernel(void)
    {
     char *command_line;
     char *after_dashes;
    
        set_task_stack_end_magic(&init_task);
        smp_setup_processor_id();
        debug_objects_early_init();
    
        cgroup_init_early();
    
        local_irq_disable();
        early_boot_irqs_disabled = true;
    
        /*
         * Interrupts are still disabled. Do necessary setups, then
         * enable them.
         */
        boot_cpu_init();
        page_address_init();
        pr_notice("%s", linux_banner);
        setup_arch(&command_line);
        /*
         * Set up the the initial canary and entropy after arch
         * and after adding latent and command line entropy.
         */
        add_latent_entropy();
        add_device_randomness(command_line, strlen(command_line));
        boot_init_stack_canary();
        mm_init_cpumask(&init_mm);
        setup_command_line(command_line);
        setup_nr_cpu_ids();
        setup_per_cpu_areas();
        smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
        boot_cpu_hotplug_init();
    
        build_all_zonelists(NULL);
        page_alloc_init();
    
        pr_notice("Kernel command line: %s\n", boot_command_line);
        parse_early_param();
        after_dashes = parse_args("Booting kernel",
             static_command_line, __start___param,
             __stop___param - __start___param,
             -1, -1, NULL, &unknown_bootoption);
        if (!IS_ERR_OR_NULL(after_dashes))
         parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
             NULL, set_init_arg);
    
        jump_label_init();
    
        /*
         * These use large bootmem allocations and must precede
         * kmem_cache_init()
         */
        setup_log_buf(0);
        vfs_caches_init_early();
        sort_main_extable();
        trap_init();
        mm_init();
    
        ftrace_init();
    
        /* trace_printk can be enabled here */
        early_trace_init();
    
        /*
         * Set up the scheduler prior starting any interrupts (such as the
         * timer interrupt). Full topology setup happens at smp_init()
         * time - but meanwhile we still have a functioning scheduler.
         */
        sched_init();
        /*
         * Disable preemption - early bootup scheduling is extremely
         * fragile until we cpu_idle() for the first time.
         */
        preempt_disable();
        if (WARN(!irqs_disabled(),
          "Interrupts were enabled *very* early, fixing it\n"))
         local_irq_disable();
        radix_tree_init();
    
        /*
         * Set up housekeeping before setting up workqueues to allow the unbound
         * workqueue to take non-housekeeping into account.
         */
        housekeeping_init();
    
        /*
         * Allow workqueue creation and work item queueing/cancelling
         * early.  Work item execution depends on kthreads and starts after
         * workqueue_init().
         */
        workqueue_init_early();
    
        rcu_init();
    
        /* Trace events are available after this */
        trace_init();
    
        if (initcall_debug)
         initcall_debug_enable();
    
        context_tracking_init();
        /* init some links before init_ISA_irqs() */
        early_irq_init();
        init_IRQ();
        tick_init();
        rcu_init_nohz();
        init_timers();
        hrtimers_init();
        softirq_init();
        timekeeping_init();
        time_init();
        sched_clock_postinit();
        printk_safe_init();
        perf_event_init();
        profile_init();
        call_function_init();
        WARN(!irqs_disabled(), "Interrupts were enabled early\n");
        early_boot_irqs_disabled = false;
        local_irq_enable();
    
        kmem_cache_init_late();
    
        /*
         * HACK ALERT! This is early. We're enabling the console before
         * we'
ve done PCI setups etc, and console_init() must be aware of
         * this. But we do want output early, in case something goes wrong.
         */
        console_init();
    
    printk("## start_kernel() --> console_init()\n");
    if (panic_later)
    panic("Too many boot %s vars at `%s'", panic_later,
    panic_param);
    
        lockdep_info();
    
        /*
         * Need to run this when irqs are enabled, because it wants
         * to self-test [hard/soft]-irqs on/off lock inversion bugs
         * too:
         */
        locking_selftest();
    
        /*
         * This needs to be called before any devices perform DMA
         * operations that might use the SWIOTLB bounce buffers. It will
         * mark the bounce buffers as decrypted so that their usage will
         * not cause "plain-text" data to be decrypted when accessed.
         */
        mem_encrypt_init();
    
    #ifdef CONFIG_BLK_DEV_INITRD
    if (initrd_start && !initrd_below_start_ok &&
    page_to_pfn(virt_to_page((void _)initrd_start)) < min_low_pfn) {
    pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
    page_to_pfn(virt_to_page((void _)initrd_start)),
    min_low_pfn);
    initrd_start = 0;
    }
    #endif
    page_ext_init();
    kmemleak_init();
    debug_objects_mem_init();
    setup_per_cpu_pageset();
    numa_policy_init();
    acpi_early_init();
    if (late_time_init)
    late_time_init();
    calibrate_delay();
    pid_idr_init();
    anon_vma_init();
    #ifdef CONFIG_X86
    if (efi_enabled(EFI_RUNTIME_SERVICES))
    efi_enter_virtual_mode();
    #endif
    thread_stack_cache_init();
    cred_init();
    fork_init();
    proc_caches_init();
    uts_ns_init();
    buffer_init();
    key_init();
    security_init();
    dbg_late_init();
    vfs_caches_init();
    pagecache_init();
    signals_init();
    seq_file_init();
    proc_root_init();
    nsfs_init();
    cpuset_init();
    cgroup_init();
    taskstats_init_early();
    delayacct_init();
    
        check_bugs();
    
        acpi_subsystem_init();
        arch_post_acpi_subsys_init();
        sfi_init_late();
    
        if (efi_enabled(EFI_RUNTIME_SERVICES)) {
         efi_free_boot_services();
        }
    
    printk("## run rest_init()\n");
    /_ Do the rest non-\_\_init'ed, we're now alive _/
    rest_init();
    printk("## after rest_init()\n");
    }
    

一开始尝试在函数刚开始就添加 printk 打印,结果发现添加完 printk 后内核起不来,最后保守起见,在 131 行 console_init(); 后才开始添加打印。

    
    Starting kernel ...
    
    [ 0.000000] Booting Linux on physical CPU 0x0
    [ 0.000000] Linux version 4.18.12 (liyongjun@Box) (gcc version 9.3.0 (Buildroot 2021.05)) #14 SMP Thu Nov 25 00:37:30 CST 2021
    [ 0.000000] CPU: ARMv7 Processor [410fc074] revision 4 (ARMv7), cr=10c5387d
    [ 0.000000] CPU: div instructions available: patching division code
    [ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
    [ 0.000000] OF: fdt: Machine model: LeMaker Banana Pi
    [ 0.000000] Memory policy: Data cache writealloc
    [ 0.000000] cma: Reserved 16 MiB at 0x7ec00000
    [ 0.000000] psci: probing for conduit method from DT.
    [ 0.000000] psci: Using PSCI v0.1 Function IDs from DT
    [ 0.000000] random: get_random_bytes called from start_kernel+0xa0/0x430 with crng_init=0
    [ 0.000000] percpu: Embedded 16 pages/cpu @(ptrval) s34444 r8192 d22900 u65536
    [ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 260202
    [ 0.000000] Kernel command line: console=ttyS0,57600 earlyprintk root=/dev/mmcblk0p2 rootwait
    [ 0.000000] Dentry cache hash table entries: 131072 (order: 7, 524288 bytes)
    [ 0.000000] Inode-cache hash table entries: 65536 (order: 6, 262144 bytes)
    [ 0.000000] Memory: 1011460K/1046952K available (6144K kernel code, 418K rwdata, 1524K rodata, 1024K init, 240K bss, 19108K reserved, 16384K cma-reserved, 244136K highmem)
    [ 0.000000] Virtual kernel memory layout:
    [ 0.000000] vector : 0xffff0000 - 0xffff1000 ( 4 kB)
    [ 0.000000] fixmap : 0xffc00000 - 0xfff00000 (3072 kB)
    [ 0.000000] vmalloc : 0xf0800000 - 0xff800000 ( 240 MB)
    [ 0.000000] lowmem : 0xc0000000 - 0xf0000000 ( 768 MB)
    [ 0.000000] pkmap : 0xbfe00000 - 0xc0000000 ( 2 MB)
    [ 0.000000] modules : 0xbf000000 - 0xbfe00000 ( 14 MB)
    [ 0.000000] .text : 0x(ptrval) - 0x(ptrval) (7136 kB)
    [ 0.000000] .init : 0x(ptrval) - 0x(ptrval) (1024 kB)
    [ 0.000000] .data : 0x(ptrval) - 0x(ptrval) ( 419 kB)
    [ 0.000000] .bss : 0x(ptrval) - 0x(ptrval) ( 241 kB)
    [ 0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=2, Nodes=1
    [ 0.000000] Hierarchical RCU implementation.
    [ 0.000000] RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=2.
    [ 0.000000] RCU: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=2
    [ 0.000000] NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16
    [ 0.000000] GIC: Using split EOI/Deactivate mode
    [ 0.000000] arch_timer: cp15 timer(s) running at 24.00MHz (phys).
    [ 0.000000] clocksource: arch_sys_counter: mask: 0xffffffffffffff max_cycles: 0x588fe9dc0, max_idle_ns: 440795202592 ns
    [ 0.000007] sched_clock: 56 bits at 24MHz, resolution 41ns, wraps every 4398046511097ns
    [ 0.000021] Switching to timer-based delay loop, resolution 41ns
    [ 0.000334] clocksource: timer: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 79635851949 ns
    [ 0.000586] clocksource: hstimer: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 6370868154 ns
    [ 0.000822] Console: colour dummy device 80x30
    [ 0.000844] ## start_kernel() --> console_init() // 132 行的打印
    
  

可是,发现在 console_init() 之前就有不少打印了,这个地方还是有些不解。猜测要么是在 console_init() 之前就已经可以打印了;要么是在 console_init() 之前先将打印缓存着,等初始化之后再打印。这点以后再研究吧。先插个眼👁。

接着看打印

    [    0.000844] ## start_kernel() --> console_init()
    [    0.000872] Calibrating delay loop (skipped), value calculated using timer frequency.. 48.00 BogoMIPS (lpj=240000)
    [    0.000886] pid_max: default: 32768 minimum: 301
    [    0.001054] Mount-cache hash table entries: 2048 (order: 1, 8192 bytes)
    [    0.001070] Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes)
    [    0.001727] CPU: Testing write buffer coherency: ok
    [    0.001779] ## run rest_init()
    

从 133 行到 206 行,代码不少,打印却只有寥寥 5 行。少就说明不重要,那就不分析了,哈哈😁。
看下面的代码,乖乖,就剩一句了:
rest_init();,并且第 210 行的 printk 等到系统完全起来都没有打印,说明 rest_init() 就没返回。看来是个扛把子。

    static noinline void __ref rest_init(void)
    {
     struct task_struct *tsk;
     int pid;
    printk("## rcu_scheduler_starting()\n");
     rcu_scheduler_starting();
     /*
      * We need to spawn init first so that it obtains pid 1, however
      * the init task will end up wanting to create kthreads, whichif
      * we schedule it before we create kthreadd, will OOPS.
      */
     pid = kernel_thread(kernel_init, NULL, CLONE_FS);
     /*
      * Pin init on the boot CPU. Task migration is not properly working
      * until sched_init_smp() has been run. It will set the allowed
      * CPUs for init to the non isolated CPUs.
      */
     rcu_read_lock();
     tsk = find_task_by_pid_ns(pid, &init_pid_ns);
     set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
     rcu_read_unlock();
    printk("## bb\n");
     numa_default_policy();
     pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
     rcu_read_lock();
     kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
     rcu_read_unlock();
    printk("## cc\n");
     /*
      * Enable might_sleep() and smp_processor_id() checks.
      * They cannot be enabled earlier because with CONFIG_PREEMPT=y
      * kernel_thread() would trigger might_sleep() splats. With
      * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
      * already, but it's stuck on the kthreadd_done completion.
      */
     system_state = SYSTEM_SCHEDULING;
    printk("## dd\n");
     complete(&kthreadd_done);
    
        /*
         * The boot idle thread must execute schedule()
         * at least once to get things moving:
         */
    
    printk("## ee\n");
    schedule_preempt_disabled();
    printk("## ff\n");
    /_ Call into cpu_idle with preempt disabled _/
    cpu_startup_entry(CPUHP_ONLINE);
    // printk("## ff\n");
    }
    
    

对应打印如下:

   [    0.001779] ## run rest_init()
    [    0.001786] ## rcu_scheduler_starting()
    [    0.002030] ## bb
    [    0.002089] ## cc
    [    0.002097] ## dd
    [    0.002104] ## ee
    [    0.002150] ## kernel_init()
    [    0.002199] /cpus/cpu@0 missing clock-frequency property
    [    0.002214] /cpus/cpu@1 missing clock-frequency property
    [    0.002228] CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
    [    0.002799] Setting up static identity map for 0x40100000 - 0x40100060
    [    0.002972] Hierarchical SRCU implementation.
    [    0.003776] smp: Bringing up secondary CPUs ...
    [    0.014335] ## ff
    

打印了 kernel_init(),是因为调用了 schedule_preempt_disabled(),其内部调用了 schedule() 执行进程切换,进入睡眠。当然,在当前进程被唤醒时,程序也是从这个断点开始运行。

   void __sched schedule_preempt_disabled(void)
    {
     sched_preempt_enable_no_resched();
     schedule();
     preempt_disable();
    }
    

执行进程切换后,kernel_init() 才有机会运行

   static int __ref kernel_init(void *unused)
    {
     int ret;
    printk("## kernel_init()\n");
     kernel_init_freeable();
     /* need to finish all async __init code before freeing the memory */
     async_synchronize_full();
    
        ftrace_free_init_mem();
    
        jump_label_invalidate_initmem();
        free_initmem();
    
    printk("## 2.\n");
    mark_readonly();
    printk("## 3.\n");
    system_state = SYSTEM_RUNNING;
    numa_default_policy();
    
        rcu_end_inkernel_boot();
    
    printk("## 4.\n");
    if (ramdisk_execute_command) {
    ret = run_init_process(ramdisk_execute_command);
    if (!ret)
    return 0;
    pr_err("Failed to execute %s (error %d)\n",
    ramdisk_execute_command, ret);
    }
    
        /*
         * 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) {
         ret = run_init_process(execute_command);
         if (!ret)
          return 0;
         panic("Requested init %s failed (error %d).",
               execute_command, ret);
        }
        if (!try_to_run_init_process("/sbin/init") ||
            !try_to_run_init_process("/etc/init") ||
            !try_to_run_init_process("/bin/init") ||
            !try_to_run_init_process("/bin/sh")) {
    
    printk("## kernel_init() --> /sbin/init\n");
    return 0;
    }
    
        panic("No working init found.  Try passing init= option to kernel. "
              "See Linux Documentation/admin-guide/init.rst for guidance.");
    
    }
    
    

对应打印如下:


    [    0.002150] ## kernel_init()
    [    0.002199] /cpus/cpu@0 missing clock-frequency property
    [    0.002214] /cpus/cpu@1 missing clock-frequency property
    [    0.002228] CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
    [    0.002799] Setting up static identity map for 0x40100000 - 0x40100060
    [    0.002972] Hierarchical SRCU implementation.
    [    0.003776] smp: Bringing up secondary CPUs ...
    [    0.014335] ## ff
    [    0.014540] CPU1: thread -1, cpu 1, socket 0, mpidr 80000001
    [    0.014681] smp: Brought up 1 node, 2 CPUs
    [    0.014697] SMP: Total of 2 processors activated (96.00 BogoMIPS).
    [    0.014704] CPU: All CPU(s) started in HYP mode.
    [    0.014709] CPU: Virtualization extensions available.
    [    0.015730] devtmpfs: initialized
    [    0.015786] ## 11.
    [    0.015802] ## 22.
    [    0.015811] ## 33.
    [    0.015820] ## 44.
    [    0.015825] ## 55.
    [    0.015918] ## 66.
    [    0.015999] ## 77.
    [    0.016006] ## 88.
    [    0.016058] ## 99.
    [    0.021654] ## AA.
    [    0.021836] ### AAA.
    [    0.022450] VFP support v0.3: implementor 41 architecture 2 part 30 variant 7 rev 4
    [    0.022721] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
    [    0.022747] futex hash table entries: 512 (order: 3, 32768 bytes)
    [    0.023540] pinctrl core: initialized pinctrl subsystem
    [    0.024726] NET: Registered protocol family 16
    [    0.026025] DMA: preallocated 256 KiB pool for atomic coherent allocations
    [    0.027158] hw-breakpoint: found 5 (+1 reserved) breakpoint and 4 watchpoint registers.
    [    0.027173] hw-breakpoint: maximum watchpoint size is 8 bytes.
    [    0.042711] reg-fixed-voltage gmac-3v3: could not find pctldev for node /soc@1c00000/pinctrl@1c20800/gmac_power_pin@0, deferring probe
    [    0.043233] SCSI subsystem initialized
    [    0.043811] usbcore: registered new interface driver usbfs
    [    0.043864] usbcore: registered new interface driver hub
    [    0.043933] usbcore: registered new device driver usb
    [    0.044199] pps_core: LinuxPPS API ver. 1 registered
    [    0.044209] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti 
    [    0.044230] PTP clock support registered
    [    0.044563] Advanced Linux Sound Architecture Driver Initialized.
    [    0.045501] clocksource: Switched to clocksource arch_sys_counter
    [    0.046238] simple-framebuffer 7fe79000.framebuffer: framebuffer at 0x7fe79000, 0x178e00 bytes, mapped to 0x(ptrval)
    [    0.046258] simple-framebuffer 7fe79000.framebuffer: format=x8r8g8b8, mode=656x536x32, linelength=2880
    [    0.052472] Console: switching to colour frame buffer device 82x33
    [    0.058369] simple-framebuffer 7fe79000.framebuffer: fb0: simplefb registered!
    [    0.066686] NET: Registered protocol family 2
    [    0.067281] tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 6144 bytes)
    [    0.067316] TCP established hash table entries: 8192 (order: 3, 32768 bytes)
    [    0.067395] TCP bind hash table entries: 8192 (order: 4, 65536 bytes)
    [    0.067522] TCP: Hash tables configured (established 8192 bind 8192)
    [    0.067655] UDP hash table entries: 512 (order: 2, 16384 bytes)
    [    0.067722] UDP-Lite hash table entries: 512 (order: 2, 16384 bytes)
    [    0.067964] NET: Registered protocol family 1
    [    0.068567] RPC: Registered named UNIX socket transport module.
    [    0.068583] RPC: Registered udp transport module.
    [    0.068590] RPC: Registered tcp transport module.
    [    0.068596] RPC: Registered tcp NFSv4.1 backchannel transport module.
    [    0.069204] hw perfevents: no interrupt-affinity property for /pmu, guessing.
    [    0.069499] hw perfevents: enabled with armv7_cortex_a7 PMU driver, 5 counters available
    [    0.071072] workingset: timestamp_bits=30 max_order=18 bucket_order=0
    [    0.077754] NFS: Registering the id_resolver key type
    [    0.077803] Key type id_resolver registered
    [    0.077812] Key type id_legacy registered
    [    0.079006] bounce: pool size: 64 pages
    [    0.079106] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 248)
    [    0.079119] io scheduler noop registered
    [    0.079127] io scheduler deadline registered
    [    0.079295] io scheduler cfq registered (default)
    [    0.079307] io scheduler mq-deadline registered
    [    0.079315] io scheduler kyber registered
    [    0.080074] sun4i-usb-phy 1c13400.phy: could not find pctldev for node /soc@1c00000/pinctrl@1c20800/usb0_id_detect_pin@0, deferring probe
    [    0.083501] sun4i-pinctrl 1c20800.pinctrl: initialized sunXi PIO driver
    [    0.140590] Serial: 8250/16550 driver, 8 ports, IRQ sharing disabled
    [    0.143240] console [ttyS0] disabled
    [    0.163431] 1c28000.serial: ttyS0 at MMIO 0x1c28000 (irq = 48, base_baud = 1500000) is a U6_16550A
    [    1.630962] console [ttyS0] enabled
    [    1.661099] 1c28c00.serial: ttyS1 at MMIO 0x1c28c00 (irq = 49, base_baud = 1500000) is a U6_16550A
    [    1.702024] 1c29c00.serial: ttyS2 at MMIO 0x1c29c00 (irq = 50, base_baud = 1500000) is a U6_16550A
    [    1.723737] sun4i-backend 1e60000.display-backend: Couldn't find matching frontend, frontend features disabled
    [    1.744080] sun4i-drm display-engine: bound 1e60000.display-backend (ops 0xc07482cc)
    [    1.759592] sun4i-backend 1e40000.display-backend: Couldn'
t find matching frontend, frontend features disabled
    [    1.779827] sun4i-drm display-engine: bound 1e40000.display-backend (ops 0xc07482cc)
    [    1.795785] sun4i-drm display-engine: No panel or bridge found... RGB output disabled
    [    1.811456] sun4i-drm display-engine: bound 1c0c000.lcd-controller (ops 0xc074724c)
    [    1.827130] sun4i-drm display-engine: No panel or bridge found... RGB output disabled
    [    1.842825] sun4i-drm display-engine: bound 1c0d000.lcd-controller (ops 0xc074724c)
    [    1.858540] sun4i-drm display-engine: bound 1c16000.hdmi (ops 0xc0748cd0)
    [    1.872123] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
    [    1.885344] [drm] No driver support for vblank timestamp query.
    [    1.897182] fb: switching to sun4i-drm-fb from simple
    [    1.907391] Console: switching to colour dummy device 80x30
    [    2.418984] [drm] Cannot find any crtc or sizes
    [    2.428711] [drm] Initialized sun4i-drm 1.0.0 20150629 for display-engine on minor 0
    [    2.545528] ahci-sunxi 1c18000.sata: controller can't do PMP, turning off CAP_PMP
    [    2.560532] ahci-sunxi 1c18000.sata: SSS flag set, parallel bus scan disabled
    [    2.574817] ahci-sunxi 1c18000.sata: AHCI 0001.0100 32 slots 1 ports 3 Gbps 0x1 impl platform mode
    [    2.592721] ahci-sunxi 1c18000.sata: flags: ncq sntf stag pm led clo only pio slum part ccc
    [    2.610642] scsi host0: ahci-sunxi
    [    2.617874] ata1: SATA max UDMA/133 mmio [mem 0x01c18000-0x01c18fff] port 0x100 irq 36
    [    2.635546] libphy: Fixed MDIO Bus: probed
    [    2.643735] CAN device driver interface
    [    2.652152] sun7i-dwmac 1c50000.ethernet: PTP uses main clock
    [    2.663680] sun7i-dwmac 1c50000.ethernet: no reset control found
    [    2.676345] ehci_hcd: USB 2.0 '
Enhanced' Host Controller (EHCI) Driver
    [    2.689416] ehci-platform: EHCI generic platform driver
    [    2.700248] ohci_hcd: USB 1.1 '
Open' Host Controller (OHCI) Driver
    [    2.712647] ohci-platform: OHCI generic platform driver
    [    2.725114] sunxi-rtc 1c20d00.rtc: registered as rtc0
    [    2.735252] sunxi-rtc 1c20d00.rtc: RTC enabled
    [    2.744274] i2c /dev entries driver
    [    2.752374] axp20x-i2c 1-0034: AXP20x variant AXP209 found
    [    2.778365] input: axp20x-pek as /devices/platform/soc@1c00000/1c2ac00.i2c/i2c-1/1-0034/axp20x-pek/input/input0
    [    2.799556] ldo1: supplied by regulator-dummy
    [    2.808582] ldo2: supplied by regulator-dummy
    [    2.818392] ldo3: supplied by regulator-dummy
    [    2.827795] ldo4: supplied by regulator-dummy
    [    2.836730] ldo5: supplied by regulator-dummy
    [    2.846544] dcdc2: supplied by regulator-dummy
    [    2.856093] dcdc3: supplied by regulator-dummy
    [    2.866705] axp20x-i2c 1-0034: AXP20X driver loaded
    [    2.877918] Registered IR keymap rc-empty
    [    2.886099] rc rc0: sunxi-ir as /devices/platform/soc@1c00000/1c21800.ir/rc/rc0
    [    2.900854] input: sunxi-ir as /devices/platform/soc@1c00000/1c21800.ir/rc/rc0/input1
    [    2.916966] sunxi-ir 1c21800.ir: initialized sunXi IR driver
    [    2.930370] sunxi-wdt 1c20c90.watchdog: Watchdog enabled (timeout=16 sec, nowayout=0)
    [    2.949908] sunxi-mmc 1c0f000.mmc: Got CD GPIO
    [    2.957174] ata1: SATA link down (SStatus 0 SControl 300)
    [    2.984443] sunxi-mmc 1c0f000.mmc: base:0x(ptrval) irq:30
    [    2.996403] sun4i-ss 1c15000.crypto-engine: Die ID 0
    [    3.008208] usbcore: registered new interface driver usbhid
    [    3.019411] usbhid: USB HID core driver
    [    3.028477] random: fast init done
    [    3.038082] sun4i-codec 1c22c00.codec: ASoC: Failed to create component debugfs directory
    [    3.055988] sun4i-codec 1c22c00.codec: Codec <-> 1c22c00.codec mapping ok
    [    3.070975] NET: Registered protocol family 17
    [    3.079931] can: controller area network core (rev 20170425 abi 9)
    [    3.092458] NET: Registered protocol family 29
    [    3.101361] can: raw protocol (rev 20170425)
    [    3.109908] can: broadcast manager protocol (rev 20170425 t)
    [    3.121240] can: netlink gateway (rev 20170425) max_hops=1
    [    3.132444] Key type dns_resolver registered
    [    3.141113] Registering SWP/SWPB emulation handler
    [    3.155150] mmc0: host does not support reading read-only switch, assuming write-enable
    [    3.162888] sun7i-dwmac 1c50000.ethernet: PTP uses main clock
    [    3.182777] sun7i-dwmac 1c50000.ethernet: no reset control found
    [    3.194864] mmc0: new high speed SDHC card at address 59b4
    [    3.206939] mmcblk0: mmc0:59b4 USD00 7.32 GiB
    [    3.217882]  mmcblk0: p1 p2
    [    3.305735] sun7i-dwmac 1c50000.ethernet: Version ID not available
    [    3.318137] sun7i-dwmac 1c50000.ethernet:    DWMAC1000
    [    3.328068] sun7i-dwmac 1c50000.ethernet: DMA HW capability register supported
    [    3.342498] sun7i-dwmac 1c50000.ethernet: Normal descriptors
    [    3.353804] sun7i-dwmac 1c50000.ethernet: Ring mode enabled
    [    3.372879] libphy: stmmac: probed
    [    3.379711] mdio_bus stmmac-0:00: attached PHY driver [unbound] (mii_bus:phy_addr=stmmac-0:00, irq=POLL)
    [    3.398646] mdio_bus stmmac-0:01: attached PHY driver [unbound] (mii_bus:phy_addr=stmmac-0:01, irq=POLL)
    [    3.437826] ehci-platform 1c14000.usb: EHCI Host Controller
    [    3.449048] ehci-platform 1c14000.usb: new USB bus registered, assigned bus number 1
    [    4.445622] [drm] Cannot find any crtc or sizes
    [    4.454972] ehci-platform 1c14000.usb: irq 32, io mem 0x01c14000
    [    4.495500] ehci-platform 1c14000.usb: USB 2.0 started, EHCI 1.00
    [    4.508602] hub 1-0:1.0: USB hub found
    [    4.516156] hub 1-0:1.0: 1 port detected
    [    4.524842] ehci-platform 1c1c000.usb: EHCI Host Controller
    [    4.536034] ehci-platform 1c1c000.usb: new USB bus registered, assigned bus number 2
    [    4.551813] ehci-platform 1c1c000.usb: irq 37, io mem 0x01c1c000
    [    4.585504] ehci-platform 1c1c000.usb: USB 2.0 started, EHCI 1.00
    [    4.598505] hub 2-0:1.0: USB hub found
    [    4.606055] hub 2-0:1.0: 1 port detected
    [    4.614695] ohci-platform 1c14400.usb: Generic Platform OHCI controller
    [    4.627974] ohci-platform 1c14400.usb: new USB bus registered, assigned bus number 3
    [    4.643687] ohci-platform 1c14400.usb: irq 33, io mem 0x01c14400
    [    4.730281] hub 3-0:1.0: USB hub found
    [    4.737832] hub 3-0:1.0: 1 port detected
    [    4.746524] ohci-platform 1c1c400.usb: Generic Platform OHCI controller
    [    4.759793] ohci-platform 1c1c400.usb: new USB bus registered, assigned bus number 4
    [    4.775542] ohci-platform 1c1c400.usb: irq 38, io mem 0x01c1c400
    [    4.860311] hub 4-0:1.0: USB hub found
    [    4.867876] hub 4-0:1.0: 1 port detected
    [    4.876658] usb_phy_generic usb_phy_generic.0.auto: usb_phy_generic.0.auto supply vcc not found, using dummy regulator
    [    4.898584] musb-hdrc musb-hdrc.1.auto: MUSB HDRC host driver
    [    4.910112] musb-hdrc musb-hdrc.1.auto: new USB bus registered, assigned bus number 5
    [    4.926629] hub 5-0:1.0: USB hub found
    [    4.934193] hub 5-0:1.0: 1 port detected
    [    4.942801] sunxi-rtc 1c20d00.rtc: setting system clock to 1970-01-01 00:00:11 UTC (11)
    [    4.959189] vcc3v0: disabling
    [    4.965121] vcc5v0: disabling
    [    4.971086] usb0-vbus: disabling
    [    4.977556] ALSA device list:
    [    4.983479]   #0: sun4i-codec
    [    4.989417] ### BBB.
    [    4.997149] EXT4-fs (mmcblk0p2): INFO: recovery required on readonly filesystem
    [    5.011809] EXT4-fs (mmcblk0p2): write access will be enabled during recovery
    [    5.029755] EXT4-fs (mmcblk0p2): recovery complete
    [    5.090424] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
    [    5.106669] VFS: Mounted root (ext4 filesystem) readonly on device 179:2.
    [    5.121202] devtmpfs: mounted
    [    5.127186] ## 111.
    [    5.132783] Freeing unused kernel memory: 1024K
    [    5.141851] ## 2.
    
    

可以看到从第 5 行到第 12 行区区几个函数,竟然打印了那么多,而且还看到了众多驱动相关的打印,如 usbConsoleKeyiopinctrlSerialCANRTCinputmmcmdio_bus,说明我们整天开发的驱动,是在这个阶段初始化的呀。这里我直接给出调用关系,可以看到,驱动相关的打印是在 do_initcalls() 中产生的

kernel_init()
kernel_init_freeable()
do_basic_setup()
printk("### AAA.\n");
do_initcalls();
printk("### BBB.\n");    

static void __init do_initcalls(void)
{
  int level;

  for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
    do_initcall_level(level);
}
    
do_initcalls() 函数循环将编译进内核的内核模块的初始化函数依次调用一遍。这些初始化函数也是有等级的,总共有 7 个级别 1-7,数字越大,级别越低,被调用的就越晚。其中我们最长用的 module_init() 为第 6 级,等级算是比较低的了,调用也会较晚,想想也是,好多外部模块都是使用的 module_init(),它们可都是在内核完全启动后,再手动安装的呀,更晚。
这个阶段打印了很多 log,说明内核中大部分的代码都是驱动,内核中写了大量的内核模块初始化的函数。

驱动初始化完,内核也就接近尾声了

    [    4.989417] ### BBB.
    [    4.997149] EXT4-fs (mmcblk0p2): INFO: recovery required on readonly filesystem
    [    5.011809] EXT4-fs (mmcblk0p2): write access will be enabled during recovery
    [    5.029755] EXT4-fs (mmcblk0p2): recovery complete
    [    5.090424] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
    [    5.106669] VFS: Mounted root (ext4 filesystem) readonly on device 179:2.
    [    5.121202] devtmpfs: mounted
    [    5.127186] ## 111.
    [    5.132783] Freeing unused kernel memory: 1024K
    [    5.141851] ## 2.
    [    5.145862] ## 3.
    [    5.149703] ## 4.
    [    5.162188] ## kernel_init() --> /sbin/init
    ## init_main()
    [    5.232554] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
    Starting syslogd: OK
    Starting klogd: OK
    Running sysctl: OK
    Initializing random number generator: OK
    Saving random seed: [    5.377468] random: dd: uninitialized urandom read (512 bytes read)
    OK
    Starting network: OK
    
    Welcome to Bananapi M1
    buildroot login:
    
    
  • 挂载根文件系统
  • 内核收尾工作
  • kernel_init 内核线程调用 do_execve 函数,去执行 init 程序,产生 init 进程(1 号进程)。该内核线程终结。

rest_init() 函数的最后一句 cpu_startup_entry(CPUHP_ONLINE); 最终变成了 idle 进程(0 号进程)。

   void cpu_startup_entry(enum cpuhp_state state)
    {
     /*
      * This #ifdef needs to die, but it's too late in the cycle to
      * make this generic (ARM and SH have never invoked the canary
      * init for the non boot CPUs!). Will be fixed in 3.11
      */
    #ifdef CONFIG_X86
     /*
      * If we're the non-boot CPU, nothing set the stack canary up
      * for us. The boot CPU already has it initialized but no harm
      * in doing it again. This is a good place for updating it, as
      * we wont ever return from this function (so the invalid
      * canaries already on the stack wont ever trigger).
      */
     boot_init_stack_canary();
    #endif
     arch_cpu_idle_prepare();
     cpuhp_online_idle(state);
     while (1)
      do_idle();
    }
    

而在 kernel_init 同期稍晚创建的 kthreadd 内核线程,则一直存在,成为 2 号进程

    # ps    PID   USER     COMMAND        1 root     init        2 root     [kthreadd]        3 root     [rcu_gp]        4 root     [rcu_par_gp]        7 root     [kworker/u4:0-ev]        8 root     [mm_percpu_wq]        9 root     [ksoftirqd/0]    ...

原文:https://liyongjun.blog.csdn.net/article/details/121529063

文章来源于网络,版权归原作者所有,如有侵权,请联系删除。



关注我【一起学嵌入式】,一起学习,一起成长。



觉得文章不错,点击“分享”、“”、“在看” 呗!

一起学嵌入式 公众号【一起学嵌入式】,RTOS、Linux编程、C/C++,以及经验分享、行业资讯、物联网等技术知
评论 (0)
  • 多功能电锅长什么样子,主视图如下图所示。侧视图如下图所示。型号JZ-18A,额定功率600W,额定电压220V,产自潮州市潮安区彩塘镇精致电子配件厂,铭牌如下图所示。有两颗螺丝固定底盖,找到合适的工具,拆开底盖如下图所示。可见和大部分市场的加热锅一样的工作原理,手绘原理图,根据原理图进一步理解和分析。F1为保险,250V/10A,185℃,CPGXLD 250V10A TF185℃ RY 是一款温度保险丝,额定电压是250V,额定电流是10A,动作温度是185℃。CPGXLD是温度保险丝电器元件
    liweicheng 2025-05-05 18:36 438浏览
  • 文/郭楚妤编辑/cc孙聪颖‍相较于一众措辞谨慎、毫无掌舵者个人风格的上市公司财报,利亚德的财报显得尤为另类。利亚德光电集团成立于1995年,是一家以LED显示、液晶显示产品设计、生产、销售及服务为主业的高新技术企业。自2016年年报起,无论业绩优劣,董事长李军每年都会在财报末尾附上一首七言打油诗,抒发其对公司当年业绩的感悟。从“三年翻番顺大势”“智能显示我第一”“披荆斩棘幸从容”等词句中,不难窥见李军的雄心壮志。2012年,利亚德(300296.SZ)在深交所创业板上市。成立以来,该公司在细分领
    华尔街科技眼 2025-05-07 19:25 366浏览
  • 温度传感器的工作原理依据其类型可分为以下几种主要形式:一、热电阻温度传感器利用金属或半导体材料的电阻值随温度变化的特性实现测温:l ‌金属热电阻‌(如铂电阻 Pt100、Pt1000):高温下电阻值呈线性增长,稳定性高,适用于工业精密测温。l ‌热敏电阻‌(NTC/PTC):NTC 热敏电阻阻值随温度升高而下降,PTC 则相反;灵敏度高但线性范围较窄,常用于电子设备温控。二、热电偶传感器基于‌塞贝克效应‌(Seebeck effect):两种不同
    锦正茂科技 2025-05-09 13:31 136浏览
  • 文/Leon编辑/cc孙聪颖‍《中国家族企业传承研究报告》显示,超四成“企二代” 明确表达接班意愿,展现出对家族企业延续发展的主动担当。中国研究数据服务平台(CNRDS)提供的精准数据进一步佐证:截至 2022 年,已有至少 280 家上市家族企业完成权杖交接,其中八成新任掌门人为创始人之子,凸显家族企业代际传承中 “子承父业” 的主流模式。然而,对于“企二代” 而言,接棒掌舵绝非易事。在瞬息万变的商业环境中,他们既要在白热化的市场竞争中开拓创新、引领企业突破发展瓶颈,又需应对来自父辈管理层的经
    华尔街科技眼 2025-05-06 18:17 28浏览
  • 后摄像头是长这个样子,如下图。5孔(D-,D+,5V,12V,GND),说的是连接线的个数,如下图。4LED,+12V驱动4颗LED灯珠,给摄像头补光用的,如下图。打开后盖,发现里面有透明白胶(防水)和白色硬胶(固定),用合适的工具,清理其中的胶状物。BOT层,AN3860,Panasonic Semiconductor (松下电器)制造的,Cylinder Motor Driver IC for Video Camera,如下图。TOP层,感光芯片和广角聚焦镜头组合,如下图。感光芯片,看着是玻
    liweicheng 2025-05-07 23:55 335浏览
  • Matter协议是一个由Amazon Alexa、Apple HomeKit、Google Home和Samsung SmartThings等全球科技巨头与CSA联盟共同制定的开放性标准,它就像一份“共生契约”,能让原本相互独立的家居生态在应用层上握手共存,同时它并非另起炉灶,而是以IP(互联网协议)为基础框架,将不同通信协议下的家居设备统一到同一套“语义规则”之下。作为应用层上的互通标准,Matter协议正在重新定义智能家居行业的运行逻辑,它不仅能向下屏蔽家居设备制造商的生态和系统,让设备、平
    华普微HOPERF 2025-05-08 11:40 300浏览
  • 硅二极管温度传感器是一种基于硅半导体材料特性的测温装置,其核心原理是利用硅二极管的电学参数(如正向压降或电阻)随温度变化的特性实现温度检测。以下是其工作原理、技术特点及典型应用:一、工作原理1、‌PN结温度特性‌硅二极管由PN结构成,当温度变化时,其正向电压 VF与温度呈线性负相关关系。例如,温度每升高1℃,VF约下降2 mV。2、‌电压—温度关系‌通过jing确测量正向电压的微小变化,可推算出环境温度值。部分型号(如SI410)在宽温域内(如1.4 K至475 K)仍能保持高线性度。
    锦正茂科技 2025-05-09 13:52 152浏览
  • 随着智能驾驶时代到来,汽车正转变为移动计算平台。车载AI技术对存储器提出新挑战:既要高性能,又需低功耗和车规级可靠性。贞光科技代理的紫光国芯车规级LPDDR4存储器,以其卓越性能成为国产芯片产业链中的关键一环,为智能汽车提供坚实的"记忆力"支持。作为官方授权代理商,贞光科技通过专业技术团队和完善供应链,让这款国产存储器更好地服务国内汽车厂商。本文将探讨车载AI算力需求现状及贞光科技如何通过紫光国芯LPDDR4产品满足市场需求。 车载AI算力需求激增的背景与挑战智能驾驶推动算力需求爆发式
    贞光科技 2025-05-07 16:54 190浏览
  • ‌一、高斯计的正确选择‌1、‌明确测量需求‌‌磁场类型‌:区分直流或交流磁场,选择对应仪器(如交流高斯计需支持交变磁场测量)。‌量程范围‌:根据被测磁场强度选择覆盖范围,例如地球磁场(0.3–0.5 G)或工业磁体(数百至数千高斯)。‌精度与分辨率‌:高精度场景(如科研)需选择误差低于1%的仪器,分辨率需匹配微小磁场变化检测需求。2、‌仪器类型选择‌‌手持式‌:便携性强,适合现场快速检测;‌台式‌:精度更高,适用于实验室或工业环境。‌探头类型‌:‌横向/轴向探头‌:根据磁场方向选择,轴向探头适合
    锦正茂科技 2025-05-06 11:36 440浏览
  • 2024年初,OpenAI公布的Sora AI视频生成模型,震撼了国产大模型行业。随后国产厂商集体发力视频大模型,快手发布视频生成大模型可灵,字节跳动发布豆包视频生成模型,正式打响了国内AI视频生成领域第一枪。众多企业匆忙入局,只为在这片新兴市场中抢占先机,却往往忽视了技术成熟度与应用规范的打磨。以社交平台上泛滥的 AI 伪造视频为例,全红婵家人被恶意仿冒博流量卖货,明星们也纷纷中招,刘晓庆、张馨予等均曾反馈有人在视频号上通过AI生成视频假冒她。这些伪造视频不仅严重侵犯他人权
    用户1742991715177 2025-05-05 23:08 130浏览
  • UNISOC Miracle Gaming奇迹手游引擎亮点:• 高帧稳帧:支持《王者荣耀》等主流手游90帧高画质模式,连续丢帧率最高降低85%;• 丝滑操控:游戏冷启动速度提升50%,《和平精英》开镜开枪操作延迟降低80%;• 极速网络:专属游戏网络引擎,使《王者荣耀》平均延迟降低80%;• 智感语音:与腾讯GVoice联合,弱网环境仍能保持清晰通话;• 超高画质:游戏画质增强、超级HDR画质、游戏超分技术,优化游戏视效。全球手游市场规模日益壮大,游戏玩家对极致体验的追求愈发苛刻。紫光展锐全新U
    紫光展锐 2025-05-07 17:07 287浏览
  • 在过去的很长一段时间里,外卖市场呈现出美团和饿了么双寡头垄断的局面。美团凭借先发优势、强大的地推团队以及精细化的运营策略,在市场份额上长期占据领先地位。数据显示,截至2024年上半年,美团外卖以68.2%的市场份额领跑外卖行业,成为当之无愧的行业老大。其业务广泛覆盖,从一线城市的繁华商圈到二三线城市的大街小巷,几乎无处不在,为无数消费者提供便捷的外卖服务。饿了么作为阿里本地生活服务的重要一环,依托阿里强大的资金和技术支持,也在市场中站稳脚跟,以25.4%的份额位居第二。尽管市场份额上与美团有一定
    用户1742991715177 2025-05-06 19:43 90浏览
  • 二位半 5线数码管的驱动方法这个2位半的7段数码管只用5个管脚驱动。如果用常规的7段+共阳/阴则需要用10个管脚。如果把每个段看成独立的灯。5个管脚来点亮,任选其中一个作为COM端时,另外4条线可以单独各控制一个灯。所以实际上最多能驱动5*4 = 20个段。但是这里会有一个小问题。如果想点亮B1,可以让第3条线(P3)置高,P4 置低,其它阳极连P3的灯对应阴极P2 P1都应置高,此时会发现C1也会点亮。实际操作时,可以把COM端线P3设置为PP输出,其它线为OD输出。就可以单独控制了。实际的驱
    southcreek 2025-05-07 15:06 452浏览
  • 这款无线入耳式蓝牙耳机是长这个样子的,如下图。侧面特写,如下图。充电接口来个特写,用的是卡座卡在PCB板子上的,上下夹紧PCB的正负极,如下图。撬开耳机喇叭盖子,如下图。精致的喇叭(HY),如下图。喇叭是由电学产生声学的,具体结构如下图。电池包(AFS 451012  21 12),用黄色耐高温胶带进行包裹(安规需求),加强隔离绝缘的,如下图。451012是电池包的型号,聚合物锂电池+3.7V 35mAh,详细如下图。电路板是怎么拿出来的呢,剪断喇叭和电池包的连接线,底部抽出PCB板子
    liweicheng 2025-05-06 22:58 552浏览
  • 飞凌嵌入式作为龙芯合作伙伴,隆重推出FET-2K0300i-S全国产自主可控工业级核心板!FET-2K0300i-S核心板基于龙芯2K0300i工业级处理器开发设计,集成1个64位LA264处理器,主频1GHz,提供高效的计算能力;支持硬件ECC;2K0300i还具备丰富的连接接口USB、SDIO、UART、SPI、CAN-FD、Ethernet、ADC等一应俱全,龙芯2K0300i支持四路CAN-FD接口,具备良好的可靠性、实时性和灵活性,可满足用户多路CAN需求。除性价比超高的国产处理器外,
    飞凌嵌入式 2025-05-07 11:54 78浏览
我要评论
0
1
点击右上角,分享到朋友圈 我知道啦
请使用浏览器分享功能 我知道啦