简体   繁体   English

使用简单的字符驱动程序作为控制台启动 linux 内核?

[英]Booting linux kernel using a simple char driver as console?

I'm trying to boot the linux kernel (v3.16.1) on a simulation model of a Sparc v8 processor for an academic project.我正在尝试在用于学术项目的 Sparc v8 处理器的模拟模型上启动 linux 内核 (v3.16.1)。

The simulation model consists of a cpu, memory, timer and a simple polling based output device.仿真模型由一个 CPU、内存、定时器和一个简单的基于轮询的输出设备组成。 We've modified the kernel so that a bootloader is not necessary.我们修改了内核,因此不需要引导加载程序。 We directly put the kernel image in memory, set up some necessary variables and jump into kernel code.我们直接将内核镜像放入内存中,设置一些必要的变量并跳转到内核代码中。 We have a rudimentary polling based output-device, and we've been able to direct output of printk to this device.我们有一个基本的基于轮询的输出设备,我们已经能够将 printk 的输出定向到这个设备。

The kernel boots all the way up to the start of "/init".内核一直启动到“/init”的开头。 After this point no output is visible.在此之后,没有输出可见。 Just before this point, there's a warning displayed : "Warning: unable to open an initial console."就在这之前,会显示一条警告:“警告:无法打开初始控制台。” My filesystem image seems to be fine, and contains a /dev/console node (I checked this with Qemu).我的文件系统映像似乎很好,并且包含一个 /dev/console 节点(我用 Qemu 进行了检查)。

My understanding is that while printk works fine (using an early console), user processes need a device node with a proper device driver to be set up.我的理解是,虽然 printk 工作正常(使用早期的控制台),但用户进程需要一个具有适当设备驱动程序的设备节点来设置。 Printk works fine, so is there a way to see all writes by user processes to the console via printk ? Printk 工作正常,那么有没有办法通过 printk 查看用户进程对控制台的所有写入? There's an existing driver called "ttyprintk" which sends all writes to printk.有一个名为“ttyprintk”的现有驱动程序,它将所有写入发送到printk。 I enabled it and tried using it by passing "console=ttyprintk" kernel argument, but this gives the same warning.我启用它并尝试通过传递“console=ttyprintk”内核参数来使用它,但这给出了相同的警告。 The kernel is not able to open "/dev/console" for writing.内核无法打开“/dev/console”进行写入。

My questions are :我的问题是:

  1. Can I write a simple character device driver and use it as my console ?我可以编写一个简单的字符设备驱动程序并将其用作我的控制台吗? Inside this driver I plan to send all writes to printk.在这个驱动程序中,我计划将所有写入发送到 printk。 Is this possible ?这可能吗 ?

  2. How can I ask the kernel to use this as my console ?我怎样才能让内核使用它作为我的控制台? Would kernel argument "console = /dev/MyDriver" work ?内核参数“console = /dev/MyDriver”会起作用吗?

  3. Is there a simpler way to have /init and other user processes use my rudimentary output device as a console ?有没有更简单的方法让 /init 和其他用户进程使用我的基本输出设备作为控制台?

4.Is there some other reason that could be causing the "Warning: unable to open an initial console." 4.是否有其他原因可能导致“警告:无法打开初始控制台”。 message ?信息 ?

Thanks for any hints.感谢您的任何提示。 I am new to kernel programming.我是内核编程的新手。

-neha -neha

They way the /dev/console driver works is to attach to some other tty device (or devices!), which is/are then used for the console. /dev/console驱动程序的工作方式是附加到其他一些 tty 设备(或设备!),然后将其用于控制​​台。 The sysfs attribute active of the console device (try /sys/class/tty/console/active ) will tell you what device the console is attached to at the moment.控制台设备的 sysfs 属性active (尝试/sys/class/tty/console/active )将告诉您控制台当前连接到哪个设备。

The kernel also tends to log console changes:内核还倾向于记录控制台更改:

[    0.186989] dw-apb-uart ffc02000.serial0: ttyS0 at MMIO 0xffc02000 (irq = 194, base_baud = 6250000) is a 16550A
[    0.755529] console [ttyS0] enabled

In the above log, once the serial port device was created the kernel decided to use it as a console.在上面的日志中,一旦创建了串行端口设备,内核就决定将其用作控制台。 This refers to the binding of the device to the driver in the kernel, not the device node in /dev.这是指设备绑定到内核中的驱动程序,而不是/dev 中的设备节点。 The latter does not matter here.后者在这里无关紧要。 Also understand that the attachment of the console device to a tty happens in the kernel.还要了解控制台设备与 tty 的连接发生在内核中。 /dev/console is not a symlink to another device node. /dev/console不是指向另一个设备节点的符号链接。

The kernel has chosen ttyS0 because I told it to via the kernel command line, console=ttyS0,115200n8 .内核选择了 ttyS0,因为我通过内核命令行告诉它, console=ttyS0,115200n8 Without a console argument, the kernel uses the first console that registers with register_console() .如果没有控制台参数,内核将使用第一个通过register_console()控制台。

So the question here is how can one get /dev/ttyprintk to be attached to /dev/console.所以这里的问题是如何将 /dev/ttyprintk 附加到 /dev/console。 And the answer appears to be you can't.答案似乎是你不能。

A work around might be to create a custom initramfs that changes the /dev/console device node from major 5 minor 1 to use minor 3, thus changing it into /dev/ttyprintk.解决方法可能是创建一个自定义 initramfs,将 /dev/console 设备节点从主要 5 次要 1 更改为使用次要 3,从而将其更改为 /dev/ttyprintk。 Or symlink to achieve the same thing.或者符号链接来实现同样的事情。 This should get init to use ttyprintk as its stdin/stdout/stderr.这应该让 init 使用 ttyprintk 作为它的 stdin/stdout/stderr。

In your example, writing a tty/console driver for your output device would be the right way.在您的示例中,为您的输出设备编写 tty/控制台驱动程序将是正确的方法。 Make it the console and then the kernel sends printk there.使其成为控制台,然后内核将printk 发送到那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM