printk与 uart console关系分析(草稿)

1,了解kernel启动过程

asmlinkage void __init start_kernel(void)
{
        ...
        parse_args("Booting kernel", static_command_line, __start___param,
                   __stop___param - __start___param,
                   -1, -1, &unknown_bootoption);
        ...
        console_init();
        ...
        rest_init();
}

了解下console_setup, console_init, uart_add_one_port.

console_setup如何调用而来:

在kernel/printk.c中console_setup代码:

static int __init console_setup(char *str)
{
        char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
        char *s, *options, *brl_options = NULL;
        int idx;

#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
        if (!memcmp(str, "brl,", 4)) {
                brl_options = "";
                str += 4;
        } else if (!memcmp(str, "brl=", 4)) {
                brl_options = str + 4;
                str = strchr(brl_options, ',');
                if (!str) {
                        printk(KERN_ERR "need port name after brl=\n");
                        return 1;
                }
                *(str++) = 0;
        }
#endif

        /*
         * Decode str into name, index, options.
         */
        if (str[0] >= '0' && str[0] <= '9') {
                strcpy(buf, "ttyS");
                strncpy(buf + 4, str, sizeof(buf) - 5);
        } else {
                strncpy(buf, str, sizeof(buf) - 1);
        }
        buf[sizeof(buf) - 1] = 0;
        if ((options = strchr(str, ',')) != NULL)
                *(options++) = 0;//记录option,例如1152008n
#ifdef __sparc__
        if (!strcmp(str, "ttya"))
                strcpy(buf, "ttyS0");
        if (!strcmp(str, "ttyb"))
                strcpy(buf, "ttyS1");
#endif
        for (s = buf; *s; s++)
                if ((*s >= '0' && *s <= '9') || *s == ',')
                        break;
        idx = simple_strtoul(s, NULL, 10);//得到console=ttySxxx中串口号, 例如0,1,2...
        *s = 0;//使得buf内容不再包含串口号。

        __add_preferred_console(buf, idx, options, brl_options);//完成对console_cmdline数组的填充
        console_set_on_cmdline = 1;
        return 1;
}
__setup("console=", console_setup);

__add_preferred_console函数:

static int __add_preferred_console(char *name, int idx, char *options,
                                   char *brl_options)
{
        struct console_cmdline *c;
        int i;

        /*
         *      See if this tty is not yet registered, and
         *      if we have a slot free.
         */
        for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
                if (strcmp(console_cmdline[i].name, name) == 0 &&
                          console_cmdline[i].index == idx) {
                                if (!brl_options)
                                        selected_console = i;
                                return 0;
                }
        if (i == MAX_CMDLINECONSOLES)
                return -E2BIG;
        if (!brl_options)
                selected_console = i;//记录selected_console的值>=0
        c = &console_cmdline[i];
        strlcpy(c->name, name, sizeof(c->name));//记录串口名字
        c->options = options;//记录串口设置参数
#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
        c->brl_options = brl_options;
#endif
        c->index = idx;//记录作为串口的index号,在console_setup中从console=ttySxxx中得到。
        return 0;
}


__setup跟踪, 在include/linux/init.h

#define __setup_param(str, unique_id, fn, early)                        \
        static const char __setup_str_##unique_id[] __initconst \
                __aligned(1) = str; \
        static struct obs_kernel_param __setup_##unique_id      \
                __used __section(.init.setup)                   \
                __attribute__((aligned((sizeof(long)))))        \
                = { __setup_str_##unique_id, fn, early }

#define __setup(str, fn)                                        \
        __setup_param(str, fn, fn, 0)
知道该类函数定义在*.init.setup区。并由start_kernel一直调用到console_setup.

console_init()函数定义在drivers/tty/tty_io.c,主要完成对console_initcall 声明函数的调用:

void __init console_init(void)
{
        initcall_t *call;

        /* Setup the default TTY line discipline. */
        tty_ldisc_begin();

        /*
         * set up the console device so that later boot sequences can
         * inform about problems etc..
         */
        call = __con_initcall_start;
        while (call < __con_initcall_end) {
                (*call)();
                call++;//依次调用console_initcall声明的函数,like:console_initcall(sirfsoc_uart_console_init);
        }
}
在sirfsoc_uart_console_init中:

static int __init sirfsoc_uart_console_init(void)
{
        register_console(&sirfsoc_uart_console);
        return 0;
}
console_initcall(sirfsoc_uart_console_init);
下面对register_console函数进行分析:

void register_console(struct console *newcon)
{
        int i;
        unsigned long flags;
        struct console *bcon = NULL;

        /*
         * before we register a new CON_BOOT console, make sure we don't
         * already have a valid console
         */
        if (console_drivers && newcon->flags & CON_BOOT) {//boot console 如果开启了early_printk hacking,可以跟踪分析arch/arm/kernel/early_printk.c
                /* find the last or real console */
                for_each_console(bcon) {
                        if (!(bcon->flags & CON_BOOT)) {
                                printk(KERN_INFO "Too late to register bootconsole %s%d\n",
                                        newcon->name, newcon->index);
                                return;
                        }
                }
        }

        if (console_drivers && console_drivers->flags & CON_BOOT)//boot console
                bcon = console_drivers;

        if (preferred_console < 0 || bcon || !console_drivers)
                preferred_console = selected_console;//对于real console preferred_console >=0

        if (newcon->early_setup)
                newcon->early_setup();

        /*
         *      See if we want to use this console driver. If we
         *      didn't select a console we take the first one
         *      that registers here.
         */
        if (preferred_console < 0) {
                if (newcon->index < 0)
                        newcon->index = 0;
                if (newcon->setup == NULL ||
                    newcon->setup(newcon, NULL) == 0) {
                        newcon->flags |= CON_ENABLED;
                        if (newcon->device) {
                                newcon->flags |= CON_CONSDEV;
                                preferred_console = 0;
                        }
                }
        }

        /*
         *      See if this console matches one we selected on
         *      the command line.
         */
        for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
                        i++) {
                if (strcmp(console_cmdline[i].name, newcon->name) != 0)
                        continue;
                if (newcon->index >= 0 &&
                    newcon->index != console_cmdline[i].index)
                        continue;
                if (newcon->index < 0)
                        newcon->index = console_cmdline[i].index;//对于console_initcall类型函数调用的register_console, 对console的index
                                                                 //附上console=ttySxxx传过来的串口号。
 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
                if (console_cmdline[i].brl_options) {
                        newcon->flags |= CON_BRL;
                        braille_register_console(newcon,
                                        console_cmdline[i].index,
                                        console_cmdline[i].options,
                                        console_cmdline[i].brl_options);
                        return;
                }
#endif
                if (newcon->setup &&
                    newcon->setup(newcon, console_cmdline[i].options) != 0)
                        break;//第一次register_cosole不能成功从这里退出,因为port->mapbase == 0, mapbase 没有在probe中被设置。
                newcon->flags |= CON_ENABLED;//如果setup成功设置CON_ENABLED标记。
                newcon->index = console_cmdline[i].index;
                if (i == selected_console) {
                        newcon->flags |= CON_CONSDEV;
                        preferred_console = selected_console;
                }
                break;
        }

        if (!(newcon->flags & CON_ENABLED))
                return;

        /*
         * If we have a bootconsole, and are switching to a real console,
         * don't print everything out again, since when the boot console, and
         * the real console are the same physical device, it's annoying to
         * see the beginning boot messages twice
         */
        if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
                newcon->flags &= ~CON_PRINTBUFFER;//如注释所示,避免了real-console再次打印log信息
        /*
         *      Put this console in the list - keep the
         *      preferred driver at the head of the list.
         */
        console_lock();
        if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
                newcon->next = console_drivers;
                console_drivers = newcon;
                if (newcon->next)
                        newcon->next->flags &= ~CON_CONSDEV;
        } else {
                newcon->next = console_drivers->next;
                console_drivers->next = newcon;
        }
        if (newcon->flags & CON_PRINTBUFFER) {
                /*
                 * console_unlock(); will print out the buffered messages
                 * for us.
                 */
                raw_spin_lock_irqsave(&logbuf_lock, flags);
                console_seq = syslog_seq;
                console_idx = syslog_idx;
                console_prev = syslog_prev;
                raw_spin_unlock_irqrestore(&logbuf_lock, flags);
                /*
                 * We're about to replay the log buffer.  Only do this to the
                 * just-registered console to avoid excessive message spam to
                 * the already-registered consoles.
                 */
                exclusive_console = newcon;
        }
        console_unlock();
        console_sysfs_notify();

        /*
         * By unregistering the bootconsoles after we enable the real console
         * we get the "console xxx enabled" message on all the consoles -
         * boot consoles, real consoles, etc - this is to ensure that end
         * users know there might be something in the kernel's log buffer that
         * went to the bootconsole (that they do not see on the real console)
         */
        if (bcon &&
            ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
            !keep_bootcon) {
                /* we need to iterate through twice, to make sure we print
                 * everything out, before we unregister the console(s)
                 */
                printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
                        newcon->name, newcon->index);//开启了early_printk
                for_each_console(bcon)
                        if (bcon->flags & CON_BOOT)
                                unregister_console(bcon);
        } else {
                printk(KERN_INFO "%sconsole [%s%d] enabled\n",
                        (newcon->flags & CON_BOOT) ? "boot" : "" ,
                        newcon->name, newcon->index);//未开启early_printk
        }
}
EXPORT_SYMBOL(register_console);

对probe函数中uart_add_one_port的分析:



1, 名词解析:

console_sem:

/*
 * console_sem protects the console_drivers list, and also
 * provides serialisation for access to the entire console
 * driver system.
 */

console_drivers:

static DEFINE_SEMAPHORE(console_sem);
struct console *console_drivers;
EXPORT_SYMBOL_GPL(console_drivers);

在这里将console_drivers设置成export_symbol的原因是在include/linux/console.h中定义了一个宏:

#define for_each_console(con) \
        for (con = console_drivers; con != NULL; con = con->next)

用来便利console_drivers的列表。

2,函数解析

register_console:

arch/arm/kernel/early_printk.c:

static int __init setup_early_printk(char *buf)
{
        early_console = &early_console_dev;
        register_console(&early_console_dev);
        return 0;
}
early_param("earlyprintk", setup_early_printk); 

static struct console early_console_dev = {
        .name =         "earlycon",
        .write =        early_console_write,
        .flags =        CON_PRINTBUFFER | CON_BOOT,
        .index =        -1,
};

在boot/uboot/include/configs/atlas6cb.h中设置bootargs:

"devargs=" \
                "setenv bootargs no_console_suspend retain_initrd " \
                "earlyprintk resumewait " \
                "mem=${meminfo} " \
                "console=ttySiRF3 " \
                "lpj=7995392 " \
                "real_root=/dev/${bootinf}blk${bootdev}p${rootpart} " \
                "resume=/dev/${bootinf}blk${bootdev}p${resumepart} "\
                "scert_start=0x3800 " \
                "scert_size=0x1000\0" \

与pr_notice("Kernel command line: %s\n", boot_command_line);

3, 解压过程中log 打印“Uncompressing Linux... done, booting the kernel.”

make kmenuconfig->Kernel Hacking->Kernel low-level debugging functions->Platform ownself debug console.

由于选择了自己平台的console口,所以在选中CONFIG_DEBUG_LL的同时就没有选中Kernel low-level debug output via semihosting I/O,这就是CONFIG_DEBUG_SEMIHOSTING选项,开启DEBUG_LL只能选择一个debug口,因而CONFIG_DEBUG_SEMIHOSTING没有打开。代开arch/arm/Kconfig.debug文件:

choice
        prompt "Kernel low-level debugging port"
        depends on DEBUG_LL
        config DEBUG_SEMIHOSTING
                bool "Kernel low-level debug output via semihosting I/O"
                help
                  Semihosting enables code running on an ARM target to use
                  the I/O facilities on a host debugger/emulator through a
                  simple SVC call. The host debugger or emulator must have
                  semihosting enabled for the special svc call to be trapped
                  otherwise the kernel will crash.

                  This is known to work with OpenOCD, as well as
                  ARM's Fast Models, or any other controlling environment
                  that implements semihosting.

                  For more details about semihosting, please see
                  chapter 8 of DUI0203I_rvct_developer_guide.pdf from ARM Ltd.

endchoice

config中的choice选项只能选择一种。

接着打开arch/arm/kernel/debug.S

#if !defined(CONFIG_DEBUG_SEMIHOSTING)
#include CONFIG_DEBUG_LL_INCLUDE
#endif

第一句就是要包含DEBUG_LL_INCLUDE config选项:即arch/arm/Kconfig.debug文件中:

config DEBUG_LL_INCLUDE
        string
        default "debug/bcm2835.S" if DEBUG_BCM2835
        default "debug/cns3xxx.S" if DEBUG_CNS3XXX
        default "debug/exynos.S" if DEBUG_EXYNOS_UART
        default "debug/highbank.S" if DEBUG_HIGHBANK_UART
        default "debug/icedcc.S" if DEBUG_ICEDCC
        default "debug/imx.S" if DEBUG_IMX1_UART || \
                                 DEBUG_IMX25_UART || \
                                 DEBUG_IMX21_IMX27_UART || \
                                 DEBUG_IMX31_UART || \
                                 DEBUG_IMX35_UART || \
                                 DEBUG_IMX51_UART || \
                                 DEBUG_IMX53_UART ||\
                                 DEBUG_IMX6Q_UART
        default "debug/mvebu.S" if DEBUG_MVEBU_UART
        default "debug/mxs.S" if DEBUG_IMX23_UART || DEBUG_IMX28_UART
        default "debug/nomadik.S" if DEBUG_NOMADIK_UART
        default "debug/omap2plus.S" if DEBUG_OMAP2PLUS_UART
        default "debug/picoxcell.S" if DEBUG_PICOXCELL_UART
        default "debug/pxa.S" if DEBUG_PXA_UART1 || DEBUG_MMP_UART2 || \
                                 DEBUG_MMP_UART3
        default "debug/sirf.S" if DEBUG_SIRFPRIMA2_UART1 || DEBUG_SIRFMARCO_UART1
        default "debug/socfpga.S" if DEBUG_SOCFPGA_UART
        default "debug/sunxi.S" if DEBUG_SUNXI_UART0 || DEBUG_SUNXI_UART1
        default "debug/tegra.S" if DEBUG_TEGRA_UART
        default "debug/ux500.S" if DEBUG_UX500_UART
        default "debug/vexpress.S" if DEBUG_VEXPRESS_UART0_DETECT || \
                DEBUG_VEXPRESS_UART0_CA9 || DEBUG_VEXPRESS_UART0_RS1
        default "debug/vt8500.S" if DEBUG_VT8500_UART0
        default "debug/zynq.S" if DEBUG_ZYNQ_UART0 || DEBUG_ZYNQ_UART1
        default "mach/debug-macro.S"

包含板子内部所带的debug文件。

同时在arch/arm/kernel/debug.S中:

#ifndef CONFIG_DEBUG_SEMIHOSTING

ENTRY(printascii)
                addruart_current r3, r1, r2
                b       2f
1:              waituart r2, r3
                senduart r1, r3
                busyuart r2, r3
                teq     r1, #'\n'
                moveq   r1, #'\r'
                beq     1b
2:              teq     r0, #0
                ldrneb  r1, [r0], #1
                teqne   r1, #0
                bne     1b
                mov     pc, lr
ENDPROC(printascii)

ENTRY(printch)
                addruart_current r3, r1, r2
                mov     r1, r0
                mov     r0, #0
                b       1b
ENDPROC(printch)

定义了printascii 和 printch函数,并调用了waituart,senduart,busyuart等函数。这些函数就是DEBUG_LL_INCLUDE中要包含的板子调试信息中所定义,如:

在arch/arm/include/debug/sirf.S

#if defined(CONFIG_DEBUG_SIRFPRIMA2_UART1)
#define SIRFSOC_UART1_PA_BASE          0xb0060000
#elif defined(CONFIG_DEBUG_SIRFMARCO_UART1)
#define SIRFSOC_UART1_PA_BASE          0xcc060000
#else
#define SIRFSOC_UART1_PA_BASE          0
#endif

#define SIRFSOC_UART1_VA_BASE           0xFEC60000

#define SIRFSOC_UART_TXFIFO_STATUS      0x0114
#define SIRFSOC_UART_TXFIFO_DATA        0x0118

#define SIRFSOC_UART1_TXFIFO_FULL                       (1 << 5)
#define SIRFSOC_UART1_TXFIFO_EMPTY                      (1 << 6)

        .macro  addruart, rp, rv, tmp
        ldr     \rp, =SIRFSOC_UART1_PA_BASE             @ physical
        ldr     \rv, =SIRFSOC_UART1_VA_BASE             @ virtual
        .endm

        .macro  senduart,rd,rx
        str     \rd, [\rx, #SIRFSOC_UART_TXFIFO_DATA]
        .endm

        .macro  busyuart,rd,rx
        .endm

        .macro  waituart,rd,rx
1001:   ldr     \rd, [\rx, #SIRFSOC_UART_TXFIFO_STATUS]
        tst     \rd, #SIRFSOC_UART1_TXFIFO_EMPTY
        beq     1001b
        .endm

定义了寄存器的地址,以及上述*uart的含义,直接想硬件寄存器放字符去打印出来。这就是lowlevel打印的函数形成的过程。

putc形成过程:

arch/arm/Kconfig.debug文件:

config DEBUG_UNCOMPRESS
        bool
        default y if ARCH_MULTIPLATFORM && DEBUG_LL && \
                     !DEBUG_OMAP2PLUS_UART && \
                     !DEBUG_TEGRA_UART

在打开DEBUG_LL后,默认应该打开了DEBUG_UNCOMPRESS config 选项。在arch/arm/boot/compressd/Makefile文件中:

ifeq ($(CONFIG_DEBUG_UNCOMPRESS),y)
OBJS    += debug.o

即编译arch/arm/boot/compressed/debug.S文件。该文件对putc函数进行了定义同样是使用arch/arm/include/debug/sirf.S中定义的*uart 函数。

在arch/arm/include/debug/uncompress.h文件中包含putc()函数的定义

#ifdef CONFIG_DEBUG_UNCOMPRESS
extern void putc(int c);
#else
static inline void putc(int c) {}
#endif

因而在真正的uart console启动起来之前,putc和printascii均调用的是直接想串口丢数据到txfifo,数据打印出来。

在sirf平台默认的就是uart1口。


4, console_setup:

__setup("console=", console_setup);
/*
         * Decode str into name, index, options.
*/
__add_preferred_console(buf, idx, options, brl_options);
console_set_on_cmdline = 1; 




  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值