162306a36Sopenharmony_ciRamoops oops/panic logger 262306a36Sopenharmony_ci========================= 362306a36Sopenharmony_ci 462306a36Sopenharmony_ciSergiu Iordache <sergiu@chromium.org> 562306a36Sopenharmony_ci 662306a36Sopenharmony_ciUpdated: 10 Feb 2021 762306a36Sopenharmony_ci 862306a36Sopenharmony_ciIntroduction 962306a36Sopenharmony_ci------------ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ciRamoops is an oops/panic logger that writes its logs to RAM before the system 1262306a36Sopenharmony_cicrashes. It works by logging oopses and panics in a circular buffer. Ramoops 1362306a36Sopenharmony_cineeds a system with persistent RAM so that the content of that area can 1462306a36Sopenharmony_cisurvive after a restart. 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciRamoops concepts 1762306a36Sopenharmony_ci---------------- 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ciRamoops uses a predefined memory area to store the dump. The start and size 2062306a36Sopenharmony_ciand type of the memory area are set using three variables: 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci * ``mem_address`` for the start 2362306a36Sopenharmony_ci * ``mem_size`` for the size. The memory size will be rounded down to a 2462306a36Sopenharmony_ci power of two. 2562306a36Sopenharmony_ci * ``mem_type`` to specify if the memory type (default is pgprot_writecombine). 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ciTypically the default value of ``mem_type=0`` should be used as that sets the pstore 2862306a36Sopenharmony_cimapping to pgprot_writecombine. Setting ``mem_type=1`` attempts to use 2962306a36Sopenharmony_ci``pgprot_noncached``, which only works on some platforms. This is because pstore 3062306a36Sopenharmony_cidepends on atomic operations. At least on ARM, pgprot_noncached causes the 3162306a36Sopenharmony_cimemory to be mapped strongly ordered, and atomic operations on strongly ordered 3262306a36Sopenharmony_cimemory are implementation defined, and won't work on many ARMs such as omaps. 3362306a36Sopenharmony_ciSetting ``mem_type=2`` attempts to treat the memory region as normal memory, 3462306a36Sopenharmony_ciwhich enables full cache on it. This can improve the performance. 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ciThe memory area is divided into ``record_size`` chunks (also rounded down to 3762306a36Sopenharmony_cipower of two) and each kmesg dump writes a ``record_size`` chunk of 3862306a36Sopenharmony_ciinformation. 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ciLimiting which kinds of kmsg dumps are stored can be controlled via 4162306a36Sopenharmony_cithe ``max_reason`` value, as defined in include/linux/kmsg_dump.h's 4262306a36Sopenharmony_ci``enum kmsg_dump_reason``. For example, to store both Oopses and Panics, 4362306a36Sopenharmony_ci``max_reason`` should be set to 2 (KMSG_DUMP_OOPS), to store only Panics 4462306a36Sopenharmony_ci``max_reason`` should be set to 1 (KMSG_DUMP_PANIC). Setting this to 0 4562306a36Sopenharmony_ci(KMSG_DUMP_UNDEF), means the reason filtering will be controlled by the 4662306a36Sopenharmony_ci``printk.always_kmsg_dump`` boot param: if unset, it'll be KMSG_DUMP_OOPS, 4762306a36Sopenharmony_ciotherwise KMSG_DUMP_MAX. 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ciThe module uses a counter to record multiple dumps but the counter gets reset 5062306a36Sopenharmony_cion restart (i.e. new dumps after the restart will overwrite old ones). 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ciRamoops also supports software ECC protection of persistent memory regions. 5362306a36Sopenharmony_ciThis might be useful when a hardware reset was used to bring the machine back 5462306a36Sopenharmony_cito life (i.e. a watchdog triggered). In such cases, RAM may be somewhat 5562306a36Sopenharmony_cicorrupt, but usually it is restorable. 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ciSetting the parameters 5862306a36Sopenharmony_ci---------------------- 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciSetting the ramoops parameters can be done in several different manners: 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci A. Use the module parameters (which have the names of the variables described 6362306a36Sopenharmony_ci as before). For quick debugging, you can also reserve parts of memory during 6462306a36Sopenharmony_ci boot and then use the reserved memory for ramoops. For example, assuming a 6562306a36Sopenharmony_ci machine with > 128 MB of memory, the following kernel command line will tell 6662306a36Sopenharmony_ci the kernel to use only the first 128 MB of memory, and place ECC-protected 6762306a36Sopenharmony_ci ramoops region at 128 MB boundary:: 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci mem=128M ramoops.mem_address=0x8000000 ramoops.ecc=1 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci B. Use Device Tree bindings, as described in 7262306a36Sopenharmony_ci ``Documentation/devicetree/bindings/reserved-memory/ramoops.yaml``. 7362306a36Sopenharmony_ci For example:: 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci reserved-memory { 7662306a36Sopenharmony_ci #address-cells = <2>; 7762306a36Sopenharmony_ci #size-cells = <2>; 7862306a36Sopenharmony_ci ranges; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci ramoops@8f000000 { 8162306a36Sopenharmony_ci compatible = "ramoops"; 8262306a36Sopenharmony_ci reg = <0 0x8f000000 0 0x100000>; 8362306a36Sopenharmony_ci record-size = <0x4000>; 8462306a36Sopenharmony_ci console-size = <0x4000>; 8562306a36Sopenharmony_ci }; 8662306a36Sopenharmony_ci }; 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci C. Use a platform device and set the platform data. The parameters can then 8962306a36Sopenharmony_ci be set through that platform data. An example of doing that is: 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci .. code-block:: c 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci #include <linux/pstore_ram.h> 9462306a36Sopenharmony_ci [...] 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci static struct ramoops_platform_data ramoops_data = { 9762306a36Sopenharmony_ci .mem_size = <...>, 9862306a36Sopenharmony_ci .mem_address = <...>, 9962306a36Sopenharmony_ci .mem_type = <...>, 10062306a36Sopenharmony_ci .record_size = <...>, 10162306a36Sopenharmony_ci .max_reason = <...>, 10262306a36Sopenharmony_ci .ecc = <...>, 10362306a36Sopenharmony_ci }; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci static struct platform_device ramoops_dev = { 10662306a36Sopenharmony_ci .name = "ramoops", 10762306a36Sopenharmony_ci .dev = { 10862306a36Sopenharmony_ci .platform_data = &ramoops_data, 10962306a36Sopenharmony_ci }, 11062306a36Sopenharmony_ci }; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci [... inside a function ...] 11362306a36Sopenharmony_ci int ret; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci ret = platform_device_register(&ramoops_dev); 11662306a36Sopenharmony_ci if (ret) { 11762306a36Sopenharmony_ci printk(KERN_ERR "unable to register platform device\n"); 11862306a36Sopenharmony_ci return ret; 11962306a36Sopenharmony_ci } 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ciYou can specify either RAM memory or peripheral devices' memory. However, when 12262306a36Sopenharmony_cispecifying RAM, be sure to reserve the memory by issuing memblock_reserve() 12362306a36Sopenharmony_civery early in the architecture code, e.g.:: 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci #include <linux/memblock.h> 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci memblock_reserve(ramoops_data.mem_address, ramoops_data.mem_size); 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ciDump format 13062306a36Sopenharmony_ci----------- 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ciThe data dump begins with a header, currently defined as ``====`` followed by a 13362306a36Sopenharmony_citimestamp and a new line. The dump then continues with the actual data. 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciReading the data 13662306a36Sopenharmony_ci---------------- 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciThe dump data can be read from the pstore filesystem. The format for these 13962306a36Sopenharmony_cifiles is ``dmesg-ramoops-N``, where N is the record number in memory. To delete 14062306a36Sopenharmony_cia stored record from RAM, simply unlink the respective pstore file. 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ciPersistent function tracing 14362306a36Sopenharmony_ci--------------------------- 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciPersistent function tracing might be useful for debugging software or hardware 14662306a36Sopenharmony_cirelated hangs. The functions call chain log is stored in a ``ftrace-ramoops`` 14762306a36Sopenharmony_cifile. Here is an example of usage:: 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci # mount -t debugfs debugfs /sys/kernel/debug/ 15062306a36Sopenharmony_ci # echo 1 > /sys/kernel/debug/pstore/record_ftrace 15162306a36Sopenharmony_ci # reboot -f 15262306a36Sopenharmony_ci [...] 15362306a36Sopenharmony_ci # mount -t pstore pstore /mnt/ 15462306a36Sopenharmony_ci # tail /mnt/ftrace-ramoops 15562306a36Sopenharmony_ci 0 ffffffff8101ea64 ffffffff8101bcda native_apic_mem_read <- disconnect_bsp_APIC+0x6a/0xc0 15662306a36Sopenharmony_ci 0 ffffffff8101ea44 ffffffff8101bcf6 native_apic_mem_write <- disconnect_bsp_APIC+0x86/0xc0 15762306a36Sopenharmony_ci 0 ffffffff81020084 ffffffff8101a4b5 hpet_disable <- native_machine_shutdown+0x75/0x90 15862306a36Sopenharmony_ci 0 ffffffff81005f94 ffffffff8101a4bb iommu_shutdown_noop <- native_machine_shutdown+0x7b/0x90 15962306a36Sopenharmony_ci 0 ffffffff8101a6a1 ffffffff8101a437 native_machine_emergency_restart <- native_machine_restart+0x37/0x40 16062306a36Sopenharmony_ci 0 ffffffff811f9876 ffffffff8101a73a acpi_reboot <- native_machine_emergency_restart+0xaa/0x1e0 16162306a36Sopenharmony_ci 0 ffffffff8101a514 ffffffff8101a772 mach_reboot_fixups <- native_machine_emergency_restart+0xe2/0x1e0 16262306a36Sopenharmony_ci 0 ffffffff811d9c54 ffffffff8101a7a0 __const_udelay <- native_machine_emergency_restart+0x110/0x1e0 16362306a36Sopenharmony_ci 0 ffffffff811d9c34 ffffffff811d9c80 __delay <- __const_udelay+0x30/0x40 16462306a36Sopenharmony_ci 0 ffffffff811d9d14 ffffffff811d9c3f delay_tsc <- __delay+0xf/0x20 165