How to pass a value to a builtin Linux kernel module at boot time?

后端 未结 3 1331
轮回少年
轮回少年 2021-02-04 11:14

I want to pass a custom parameter to the kernel at boot time, which my new code will use. This parameter is a number.

I know how to pass value to kernel module using ker

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 12:19

    Linux source documentation

    I prefer it from the hourse's mouth v4.12/Documentation/admin-guide/kernel-parameters.rst:

    Module parameters can be specified in two ways: via the kernel command
    line with a module name prefix, or via modprobe, e.g.:
    
        (kernel command line) usbcore.blinkenlights=1
        (modprobe command line) modprobe usbcore blinkenlights=1
    
    Parameters for modules which are built into the kernel need to be
    specified on the kernel command line.  modprobe looks through the
    kernel command line (/proc/cmdline) and collects module parameters
    when it loads a module, so the kernel command line can be used for
    loadable modules too.
    

    Easy way to try it out

    CONFIG_DUMMY_IRQ=y
    

    then on the command line:

    dummy-irq.irq=12
    

    and when the kernel boots you see:

    dummy-irq: registered for IRQ 12
    

    which is printed from the init of dummy-irq.c.

    Code path

    I didn't manage to follow the full code path yet, but I think the . is encoded at https://github.com/torvalds/linux/blob/v4.12/include/linux/moduleparam.h#L13:

    #define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
    

    which gets expanded in the module_param macro waterfall, one step of which contains a comment by Linus that indicates how clear that code is:

    /* Lazy bastard, eh? */
    

    The QEMU GDB watch backtrace that ends up setting it for dummy-irq.c:irq is:

    #0  kstrtouint (s=, base=, res=0xffffffff81a8d820 ) at lib/kstrtox.c:225
    #1  0xffffffff8106e124 in param_set_uint (val=, kp=) at kernel/params.c:295
    #2  0xffffffff8106ed98 in parse_one (handle_unknown=, arg=, max_level=, min_level=, num_params=, params=, doing=, val=, param=) at kernel/params.c:148
    #3  parse_args (doing=, args=0xffff880007fdb99f "", params=, num=, min_level=, max_level=, arg=0x0 , unknown=0xffffffff81aeb8e5 ) at kernel/params.c:243
    #4  0xffffffff81aebc6d in start_kernel () at init/main.c:518
    

提交回复
热议问题