Skip to the content.

crash 内核实现

仔细看看这个文档吧

内核实现

kernel 镜像有指定的入口地址,kernel 镜像要加载到入口地址位置才能正常启动,而这块内存正在被当前内核使用,所以 kernel 镜像需要临时存放的内存,在跳转前要将内容搬移到入口地址。

之前分配的所有的物理内存直接踩掉就可以了。

kexec -s uImage --ramdisk=./ramdisk.bin

为此,kexec 引入了 entry 的概念,其实就是一个 unsigned long 对象,记录申请到的 page 的物理地址,并利用低位的 4bit 来对 entry 进行分类,共计有如下 4 类 entry。

当 kexec -l 的时候

而当 kexec -e 的时候,

知乎专栏:linux 系统内存 dump 机制介绍(一)–kdump

知乎专栏:很详细,也很混乱

kdump

when linux crashed, kexec and kdump will dump kernel memory to vmcore

PG_reserved is set for special pages. The “struct page” of such a page should in general not be touched (e.g. set dirty) except by its owner. Pages marked as PG_reserved include:

kdump/kdump.txt

On x86 machines, the first 640 KB of physical memory is needed to boot, regardless of where the kernel loads. Therefore, kexec backs up this region just before rebooting into the dump-capture kernel.

All of the necessary information about the system kernel’s core image is encoded in the ELF format, and stored in a reserved area of memory before a crash. The physical address of the start of the ELF header is passed to the dump-capture kernel through the elfcorehdr= boot parameter. Optionally the size of the ELF header can also be passed when using the elfcorehdr=[size[KMG]@]offset[KMG] syntax.

With the dump-capture kernel, you can access the memory image through /proc/vmcore. This exports the dump as an ELF-format file that you can write out using file copy commands such as cp or scp. Further, you can use analysis tools such as the GNU Debugger (GDB) and the Crash tool to debug the dump file. This method ensures that the dump pages are correctly ordered.

There are two possible methods of using Kdump.

1) Build a separate custom dump-capture kernel for capturing the kernel core dump.

2) Or use the system kernel binary itself as dump-capture kernel and there is no need to build a separate dump-capture kernel. This is possible only with the architectures which support a relocatable kernel. As of today, i386, x86_64, ppc64, ia64, arm and arm64 architectures support relocatable kernel.

man kexec(2)

It’s also possible to invoke kexec without an option parameter. In that case, kexec loads the specified kernel and then invokes shutdown(8). If the shutdown scripts of your Linux distribu‐ tion support kexec-based rebooting, they then call kexec -e just before actually rebooting the machine. That way, the machine does a clean shutdown including all shutdown scripts.

kexec.c

kexec_core.c

以及一些 crash 开头的函数。

kexec_file.c

https://man7.org/linux/man-pages/man2/kexec_load.2.html

这是 x86 特有的系统调用:

config KEXEC_FILE
    bool "kexec file based system call"
    select KEXEC_CORE
    select BUILD_BIN2C
    depends on X86_64
    depends on CRYPTO=y
    depends on CRYPTO_SHA256=y
    ---help---
      This is new version of kexec system call. This system call is
      file based and takes file descriptors as system call argument
      for kernel and initramfs as opposed to list of segments as
      accepted by previous system call.

分析触发 core dump 之后的结果

分析 x86 kdump 的流程

[ ] kexec_crash_image 是如何组装的

似乎 is panic 是一个很关键的参数

acpi 是如何探测的

[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x0000000000000fff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000001000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000b3000000-0x00000000bef5cfff] usable
[    0.000000] BIOS-e820: [mem 0x00000000befffc00-0x00000000beffffff] usable
[    0.000000] BIOS-e820: [mem 0x00000000bffe0000-0x00000000bfffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved

0x0000000000001000-0x000000000009fbff + 0x00000000b3000000-0x00000000bef5cfff + 0x00000000befffc00-0x00000000beffffff

计算了一下,正好是:

Out[5]: 191.98437213897705

—-> 这个 zhihu 参考有问题啊

kexec 的常规路径是什么

kexec-tools 中间:

reboot(LINUX_REBOOT_CMD_KEXEC);

进入到内核中间:

crash 源码分析

主要是解析各种格式:

和 kdump 关联的几个文件

本站所有文章转发 CSDN 将按侵权追究法律责任,其它情况随意。