linux内核启动过程中__set_up的作用!

__set_up是一个宏

#define __setup(str, fn)                    \
    __setup_param(str, fn, fn, 0)

#define __setup_param(str, unique_id, fn, early)            \
    static char __setup_str_##unique_id[] __initdata = str;    \
    static struct obs_kernel_param __setup_##unique_id    \
        __attribute_used__                \
        __attribute__((__section__(".init.setup")))    \
        __attribute__((aligned((sizeof(long)))))    \
        = { __setup_str_##unique_id, fn, early }
struct obs_kernel_param {
    const char *str;
    int (*setup_func)(char *);
    int early;
};

#define __init        __attribute__ ((__section__ (".init.text")))
#define __initdata    __attribute__ ((__section__ (".init.data")))
#define __exitdata    __attribute__ ((__section__(".exit.data")))

举例:

static int __init init_setup(char *str)
{
    unsigned int i;

    execute_command = str;

    for (i = 1; i < MAX_INIT_ARGS; i++)
        argv_init[i] = NULL;
    return 1;
}
__setup("init=", init_setup);
//转换为
    static char __setup_str_init_setup[] __attribute__ ((__section__ (".init.data"))) ="init=";    
    static struct obs_kernel_param __setup_init_setup    
        __attribute_used__                
        __attribute__((__section__(".init.setup")))    
        __attribute__((aligned((sizeof(long)))))    
        = { __setup_str_init_setup, init_setup, 0 }
/*vmlinux.lds的.init段中
{
  ...
  __setup_start = .;
   *(.init.setup)
   __setup_end = .;
...
}*/
static
int __init obsolete_checksetup(char *line)/* Handle obsolete-style parameters */ { struct obs_kernel_param *p; int had_early_param = 0; p = __setup_start; /*从.init.setup段的开始分析*/ do { int n = strlen(p->str); /*计算出.init.setup中字符串的长度*/ if (!strncmp(line, p->str, n)) { if (p->early) { /*这里的都是early属性为0的*/ /* Already done in parse_early_param? * (Needs exact match on param part). * Keep iterating, as we can have early * params and __setups of same names 8( */ if (line[n] == '\0' || line[n] == '=') had_early_param = 1; } else if (!p->setup_func) { printk(KERN_WARNING "Parameter %s is obsolete," " ignored\n", p->str); return 1; } else if (p->setup_func(line + n))/*调用参数中对应函数,参数为偏移n个(初始化是参数长度)后的字符串*/ return 1; } p++; /*继续处理,直到结束*/ } while (p < __setup_end); return had_early_param; }
static int __init init_setup(char *str)
{
    unsigned int i;

    execute_command = str;
    /*
     * In case LILO is going to boot us with default command line,
     * it prepends "auto" before the whole cmdline which makes
     * the shell think it should execute a script with such name.
     * So we ignore all arguments entered _before_ init=... [MJ]
     */
    for (i = 1; i < MAX_INIT_ARGS; i++)
        argv_init[i] = NULL;
    return 1;
}
//作用就是把“=”后边的传给execute_command。
//比如设置了init=/linuxrc   则execute_command=&“/linuxrc” 表示不正确 就是那个结构体的str参数偏移n个的地址
static int noinline init_post(void)
{
        ...
    if (ramdisk_execute_command) {
        run_init_process(ramdisk_execute_command);
        printk(KERN_WARNING "Failed to execute %s\n",
                ramdisk_execute_command);
    }
        ...
    if (execute_command) {
        run_init_process(execute_command);
        printk(KERN_WARNING "Failed to execute %s.  Attempting "
                    "defaults...\n", execute_command);
    }
    run_init_process("/sbin/init");
    run_init_process("/etc/init");
    run_init_process("/bin/init");
    run_init_process("/bin/sh");

    panic("No init found.  Try passing init= option to kernel.");
}        

 

转载于:https://www.cnblogs.com/Rainingday/p/8647572.html

__crash_kexec函数是Linux内核的一个函数,用于在系统崩溃时进行重启。下面是对该函数的源码逐行注释: ```c void __crash_kexec(struct pt_regs *regs) { struct resource *res; unsigned long flags; const char *s; int i; unsigned long kexec_flags = (unsigned long)panic_timeout; ``` 定义了一些变量,其kexec_flags存储了panic_timeout的值,该值会在后面被用于设置kexec的一些标志。 ```c if (regs) { printk(KERN_EMERG "Kernel panic - not syncing: "); print_modules(); print_ip_sym(regs->ip); dump_trace(regs, NULL); } else { printk(KERN_EMERG "Kernel panic - not syncing: Attempted to kill init!\n"); } ``` 如果regs不为NULL,说明系统崩溃的原因是一个内核错误,需要打印一些错误信息。否则,说明试图kill init进程导致崩溃,需要打印“Attempted to kill init!”的错误信息。 ```c /* * We may have crashed before we've had a chance to initialize * the console, so be prepared for this. */ console_lock(); if (console_may_schedule()) panic_print = 1; console_unlock(); ``` 由于在发生崩溃之前,可能还没有初始化控制台,所以需要在这里进行初始化。 ```c smp_send_stop(); local_irq_disable(); local_fiq_disable(); ``` 停止其他CPU的调度,并禁用本地断和快速断。 ```c if (regs) { bad_mode(regs); dump_instr(regs, NULL); } ``` 如果regs不为NULL,说明有内核错误,需要调用bad_mode函数和dump_instr函数来打印一些错误信息。 ```c /* Stop the machine */ machine_shutdown(); ``` 停止机器运行。 ```c /* * Save the panic record before unmapping the kernel memory, * which we are about to do. This is necessary because the * panic record is stored in kernel memory. */ save_panic(__FILE__, __LINE__, __func__, "Crash_kexec"); ``` 保存崩溃记录。 ```c /* Free all unused memory */ free_unused_memmap(); free_initmem(); free_initrd_mem(); free_all_bootmem(); free_all_pages(); ``` 释放所有未使用的内存。 ```c /* * Turn off the caches to try to make sure that everything * written so far is flushed to memory. */ flush_cache_all(); ``` 关闭缓存以确保所有写入的数据都被刷新到内存。 ```c /* * Unmap kernel memory. */ unmap_kernel_range((unsigned long)end, 0xffffffff - PAGE_OFFSET); ``` 取消内核内存的映射。 ```c /* Release resources */ release_resource(&root_resource); for (i = 0; i < MAX_IORESOURCES; i++) release_resource(&ioport_resource[i]); for (i = 0; i < MAX_RESOURCE_TYPE; i++) { res = resource_list[i].next; while (res != &resource_list[i]) { release_resource(res); res = res->next; } } ``` 释放资源。 ```c /* * Try to set up kexec to reboot into a crash kernel. * If that fails, just reboot directly. */ flags = kexec_flags | KEXEC_RESTART; s = "kexec"; if (kexec_load(flags, NULL, NULL) < 0) { flags = kexec_flags | KEXEC_PRESERVE_RTC | KEXEC_ON_CRASH; if (kexec_load(flags, NULL, NULL) < 0) { s = "reboot"; panic_blink(); machine_restart(s); } } ``` 尝试设置kexec以重启到一个崩溃内核。如果失败,则直接重启。 ```c /* * If we get here, kexec has been set up successfully. * Just jump to the new kernel. */ kexec_do_shutdown(s); } ``` 如果成功,则跳转到新内核
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值