# 2020q1 Final Project Linux `/dev/dem` 裝置 contributed by < `oucs638` > ## 作業說明 - [Linux 核心的 /dev/mem 裝置](/iQ5WKewvT9uddccT5PWcYA?both) ## 開發環境 - 作業系統 ```shell $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.4 LTS Release: 18.04 Codename: bionic ``` - 核心版本 ```shell $ uname -r 5.3.0-42-generic ``` - 記憶體 ( 約 16G) ```shell $ sudo cat /proc/meminfo MemTotal: 16243148 kB MemFree: 11694092 kB MemAvailable: 13322988 kB Buffers: 151984 kB Cached: 1832296 kB SwapCached: 0 kB Active: 2700800 kB Inactive: 1252680 kB Active(anon): 1975284 kB Inactive(anon): 169680 kB Active(file): 725516 kB Inactive(file): 1083000 kB Unevictable: 41184 kB Mlocked: 64 kB SwapTotal: 3999740 kB SwapFree: 3999740 kB Dirty: 164 kB Writeback: 0 kB AnonPages: 2010472 kB Mapped: 747160 kB Shmem: 230112 kB KReclaimable: 155928 kB Slab: 249436 kB SReclaimable: 155928 kB SUnreclaim: 93508 kB KernelStack: 16912 kB PageTables: 71360 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 12121312 kB Committed_AS: 10381220 kB VmallocTotal: 34359738367 kB VmallocUsed: 61172 kB VmallocChunk: 0 kB Percpu: 5568 kB HardwareCorrupted: 0 kB AnonHugePages: 0 kB ShmemHugePages: 0 kB ShmemPmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB Hugetlb: 0 kB DirectMap4k: 447128 kB DirectMap2M: 7788544 kB DirectMap1G: 8388608 kB ``` - CPU ```shell $ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 12 On-line CPU(s) list: 0-11 Thread(s) per core: 2 Core(s) per socket: 6 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 158 Model name: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Stepping: 13 CPU MHz: 800.068 CPU max MHz: 4500.0000 CPU min MHz: 800.0000 BogoMIPS: 5199.98 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 12288K NUMA node0 CPU(s): 0-11 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d arch_capabilities ``` - gcc version ```shell $ gcc --version gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ``` ## `/dev/mem` - 是系統實體記憶體的映像檔案 - 查看映像 ```shell $ sudo cat /proc/iomem 00000000-00000fff : Reserved 00001000-0009efff : System RAM 0009f000-000fffff : Reserved 000a0000-000bffff : PCI Bus 0000:00 000e0000-000e3fff : PCI Bus 0000:00 000e4000-000e7fff : PCI Bus 0000:00 000e8000-000ebfff : PCI Bus 0000:00 000ec000-000effff : PCI Bus 0000:00 000f0000-000fffff : PCI Bus 0000:00 000f0000-000fffff : System ROM 00100000-75426fff : System RAM 75427000-75485fff : Reserved 75486000-7554afff : System RAM 7554b000-7554bfff : Reserved 7554c000-78aa4fff : System RAM 78aa5000-7a31bfff : Reserved 7a31c000-7a398fff : ACPI Tables 7a399000-7a4aefff : ACPI Non-volatile Storage 7a4af000-7ac34fff : Reserved 7ac35000-7ad0dfff : Unknown E820 type 7ad0e000-7ad0efff : System RAM 7ad0f000-7fffffff : Reserved 7c000000-7fffffff : Graphics Stolen Memory 80000000-dfffffff : PCI Bus 0000:00 80000000-8fffffff : 0000:00:02.0 90000000-a20fffff : PCI Bus 0000:01 90000000-9fffffff : 0000:01:00.0 a0000000-a1ffffff : 0000:01:00.0 a2000000-a203ffff : 0000:01:00.2 a2000000-a203ffff : xhci-hcd a2040000-a204ffff : 0000:01:00.2 a2100000-a2100fff : 0000:00:15.0 a2100000-a21001ff : lpss_dev a2100000-a21001ff : i2c_designware.0 a2100200-a21002ff : lpss_priv a2100800-a2100fff : idma64.0 a2100800-a2100fff : idma64.0 a2101000-a2101fff : 0000:00:15.1 a2101000-a21011ff : lpss_dev a2101000-a21011ff : i2c_designware.1 a2101200-a21012ff : lpss_priv a2101800-a2101fff : idma64.1 a2101800-a2101fff : idma64.1 a3000000-a3ffffff : 0000:00:02.0 a4000000-a50fffff : PCI Bus 0000:01 a4000000-a4ffffff : 0000:01:00.0 a4000000-a4ffffff : nvidia a5000000-a507ffff : 0000:01:00.0 a5080000-a5083fff : 0000:01:00.1 a5080000-a5083fff : ICH HD audio a5084000-a5084fff : 0000:01:00.3 a5100000-a51fffff : 0000:00:1f.3 a5100000-a51fffff : ICH HD audio a5200000-a52fffff : PCI Bus 0000:03 a5200000-a5203fff : 0000:03:00.0 a5204000-a5204fff : 0000:03:00.0 a5204000-a5204fff : r8169 a5300000-a53fffff : PCI Bus 0000:02 a5300000-a5303fff : 0000:02:00.0 a5300000-a5303fff : nvme a5400000-a540ffff : 0000:00:14.0 a5400000-a540ffff : xhci-hcd a5410000-a5417fff : 0000:00:04.0 a5410000-a5417fff : proc_thermal a5418000-a541bfff : 0000:00:1f.3 a5418000-a541bfff : ICH HD audio a541c000-a541ffff : 0000:00:14.3 a541c000-a541ffff : iwlwifi a5420000-a5421fff : 0000:00:17.0 a5420000-a5421fff : ahci a5422000-a5423fff : 0000:00:14.2 a5424000-a54240ff : 0000:00:1f.4 a5425000-a54257ff : 0000:00:17.0 a5425000-a54257ff : ahci a5426000-a54260ff : 0000:00:17.0 a5426000-a54260ff : ahci a5427000-a5427fff : 0000:00:16.0 a5427000-a5427fff : mei_me a542a000-a542afff : 0000:00:14.2 a542b000-a542bfff : 0000:00:12.0 a542b000-a542bfff : Intel PCH thermal driver a542c000-a542cfff : 0000:00:08.0 e0000000-efffffff : PCI MMCONFIG 0000 [bus 00-ff] e0000000-efffffff : Reserved e0000000-efffffff : pnp 00:04 fc800000-fe7fffff : PCI Bus 0000:00 fd000000-fd69ffff : pnp 00:05 fd6a0000-fd6affff : INT3450:00 fd6a0000-fd6affff : INT3450:00 fd6b0000-fd6bffff : INT3450:00 fd6b0000-fd6bffff : INT3450:00 fd6c0000-fd6cffff : pnp 00:05 fd6d0000-fd6dffff : INT3450:00 fd6d0000-fd6dffff : INT3450:00 fd6e0000-fd6effff : INT3450:00 fd6e0000-fd6effff : INT3450:00 fd6f0000-fdffffff : pnp 00:05 fe000000-fe010fff : Reserved fe010000-fe010fff : 0000:00:1f.5 fe200000-fe7fffff : pnp 00:05 fec00000-fec00fff : Reserved fec00000-fec003ff : IOAPIC 0 fed00000-fed03fff : Reserved fed00000-fed003ff : HPET 0 fed00000-fed003ff : PNP0103:00 fed10000-fed17fff : pnp 00:04 fed18000-fed18fff : pnp 00:04 fed19000-fed19fff : pnp 00:04 fed20000-fed3ffff : pnp 00:04 fed40000-fed44fff : MSFT0101:00 fed40000-fed44fff : MSFT0101:00 fed45000-fed8ffff : pnp 00:04 fed90000-fed90fff : dmar0 fed91000-fed91fff : dmar1 fee00000-fee00fff : Local APIC fee00000-fee00fff : Reserved ff000000-ffffffff : Reserved ff000000-ffffffff : pnp 00:05 100000000-47dffffff : System RAM 1c7000000-1c7e00e80 : Kernel code 1c7e00e81-1c88507bf : Kernel data 1c8b0b000-1c8ffffff : Kernel bss 47e000000-47fffffff : RAM buffer ``` ## 映射系統保留的記憶體 ### 不經過記憶體管理系統而直接使用記憶體 - 在 `cmdline` 中設置啟動參數 `mem=11G` - 查看目前 `cmdline` 的參數 ```shell $ sudo cat /proc/cmdline BOOT_IMAGE=/boot/vmlinuz-5.3.0-42-generic root=UUID=0715e2ed-f5f1-4da9-9666-61afa4d7ce10 ro quiet splash vt.handoff=1 ``` - 觀察加入 `mem=11G` 前 `free` 跟 `/proc/iomem` 的狀態 ```shell $ free total used free shared buff/cache available Mem: 16243148 2195640 11868116 177908 2179392 13585076 Swap: 3999740 0 3999740 $ sudo cat /proc/iomem | grep RAM 00001000-0009efff : System RAM 00100000-75426fff : System RAM 75486000-7554afff : System RAM 7554c000-78aa4fff : System RAM 7ad0e000-7ad0efff : System RAM 100000000-47dffffff : System RAM 47e000000-47fffffff : RAM buffer ``` - 參考 [ubuntu wiki](https://wiki.ubuntu.com/Kernel/KernelBootParameters) 編輯 `/etc/default/grub` 檔案中的 `GRUB_CMDLINE_LINUX_DEFAULT` - 使用 `sudo update-grub` 指令更新改動後,重啟系統 - 確認改動成功 ```shell $ sudo cat /proc/cmdline BOOT_IMAGE=/boot/vmlinuz-5.3.0-42-generic root=UUID=0715e2ed-f5f1-4da9-9666-61afa4d7ce10 ro quiet splash mem=11G vt.handoff=1 ``` - 觀察加入 `mem=11G` 後 `free` 跟 `/proc/iomem` 的狀態 ```shell $ free total used free shared buff/cache available Mem: 9050576 1517928 6401688 166768 1130960 7106172 Swap: 3999740 0 3999740 $ sudo cat /proc/iomem | grep RAM 00001000-0009efff : System RAM 00100000-75426fff : System RAM 75486000-7554afff : System RAM 7554c000-78aa4fff : System RAM 7ad0e000-7ad0efff : System RAM 100000000-2bfffffff : System RAM ``` - 可以看到顯示的記憶體空間減少,但是原本預期不被核心管理的記憶體大小應該是 5G,但減少的空間卻達到 7G 左右 ### 使用 Crash 工具 - 由於第一次使用 Crash 工具,安裝花了點時間 ```shell $ sudo apt-get update $ sudo apt-get install crash $ sudo apt-get install kexec-tools $ sudo apt-get install linux-crashdump ``` ```shell $ cat /proc/cmdline BOOT_IMAGE=/boot/vmlinuz-5.3.0-42-generic root=UUID=0715e2ed-f5f1-4da9-9666-61afa4d7ce10 ro quiet splash mem=11G crashkernel=512M-:192M vt.handoff=1 $ dmesg | grep -i crash [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.3.0-42-generic root=UUID=0715e2ed-f5f1-4da9-9666-61afa4d7ce10 ro quiet splash mem=11G crashkernel=512M-:192M vt.handoff=1 [ 0.017583] Reserving 192MB of memory at 1584MB for crashkernel (System RAM: 9098MB) [ 0.056325] Kernel command line: BOOT_IMAGE=/boot/vmlinuz-5.3.0-42-generic root=UUID=0715e2ed-f5f1-4da9-9666-61afa4d7ce10 ro quiet splash mem=11G crashkernel=512M-:192M vt.handoff=1 $ sudo sysctl -w kernel.sysrq=1 kernel.sysrq = 1 $ cat /proc/sys/kernel/sysrq 1 $ sudo -s $ echo c > /proc/sysrq-trigger ```