驅動程式 - Linux Device Driver (LDD) - 使用範例 - C/C++ (QEMU) - 開發環境(aarch64)



參考資訊:
https://cdn.kernel.org/pub/linux/kernel/
https://www.cnblogs.com/polariszg/p/18615278
https://github.com/byte4RR4Y/aarch64-kernel-for-qemu
https://www.qemu.org/docs/master/system/riscv/virt.html
https://jasonblog.github.io/note/arm_emulation/compiling_linux_kernel_for_qemu_arm_emulator.html

$ cd
$ wget https://www.kernel.org/pub/linux/kernel/v6.x/linux-6.1.57.tar.gz
$ tar xvf linux-6.1.57.tar.gz
$ cd linux-6.1.57
$ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make defconfig
$ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make Image -j4

$ cd
$ wget https://busybox.net/downloads/busybox-1.37.0.tar.bz2
$ tar xvf busybox-1.37.0.tar.bz2
$ cd busybox-1.37.0
$ mkdir out
$ ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- make O=out defconfig
$ ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- make O=out menuconfig
    Settings --> [V] Build static binary (no shared libs)
    Settings --> [ ] SHA1: Use hardware accelerated instructions if possible
    Settings --> [ ] SHA256: Use hardware accelerated instructions if possible

$ ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- make O=out -j4
$ ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- make O=out install

$ cd out/_install/
$ vim init
    #!/bin/sh
    mount -t proc none /proc
    mount -t sysfs none /sys
    echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
    exec /bin/sh

$ chmod +x init
$ mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}}
$ find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs-busybox.cpio.gz
$ cd ..

$ cp ~/linux-6.1.57/arch/arm64/boot/Image .
$ qemu-system-aarch64 -M virt -cpu cortex-a53 -m 1024M -nographic -kernel Image -append "console=ttyAMA0" --initrd initramfs-busybox.cpio.gz
    [    0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd034]
    [    0.000000] Linux version 6.1.57 (steward@debian) (aarch64-linux-gnu-gcc (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT Wed Jun 11 21:22:18 CST 2025
    [    0.000000] random: crng init done
    [    0.000000] Machine model: linux,dummy-virt
    [    0.000000] efi: UEFI not found.
    [    0.000000] NUMA: No NUMA configuration found
    [    0.000000] NUMA: Faking a node at [mem 0x0000000040000000-0x0000000047ffffff]
    [    0.000000] NUMA: NODE_DATA [mem 0x47fb1a00-0x47fb3fff]
    [    0.000000] Zone ranges:
    [    0.000000]   DMA      [mem 0x0000000040000000-0x0000000047ffffff]
    [    0.000000]   DMA32    empty
    [    0.000000]   Normal   empty
    [    0.000000] Movable zone start for each node
    [    0.000000] Early memory node ranges
    [    0.000000]   node   0: [mem 0x0000000040000000-0x0000000047ffffff]
    [    0.000000] Initmem setup node 0 [mem 0x0000000040000000-0x0000000047ffffff]
    [    0.000000] cma: Reserved 32 MiB at 0x0000000045c00000
    [    0.000000] psci: probing for conduit method from DT.
    [    0.000000] psci: PSCIv1.1 detected in firmware.
    [    0.000000] psci: Using standard PSCI v0.2 function IDs
    [    0.000000] psci: Trusted OS migration not required
    [    0.000000] psci: SMC Calling Convention v1.0
    [    0.000000] percpu: Embedded 20 pages/cpu s44840 r8192 d28888 u81920
    [    0.000000] Detected VIPT I-cache on CPU0
    [    0.000000] CPU features: kernel page table isolation forced ON by KASLR
    [    0.000000] CPU features: detected: Kernel page table isolation (KPTI)
    [    0.000000] CPU features: detected: ARM erratum 843419
    [    0.000000] CPU features: detected: ARM erratum 845719
    [    0.000000] alternatives: applying boot alternatives
    [    0.000000] Fallback order for Node 0: 0 
    [    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 32256
    [    0.000000] Policy zone: DMA
    [    0.000000] Kernel command line: console=ttyAMA0
    [    0.000000] Dentry cache hash table entries: 16384 (order: 5, 131072 bytes, linear)
    [    0.000000] Inode-cache hash table entries: 8192 (order: 4, 65536 bytes, linear)
    [    0.000000] mem auto-init: stack:all(zero), heap alloc:off, heap free:off
    [    0.000000] Memory: 55820K/131072K available (16128K kernel code, 3728K rwdata, 9456K rodata, 7552K init, 609K bss, 42484K reserved, 32768K cma-reserved)
    [    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
    [    0.000000] rcu: Preemptible hierarchical RCU implementation.
    [    0.000000] rcu: 	RCU event tracing is enabled.
    [    0.000000] rcu: 	RCU restricting CPUs from NR_CPUS=256 to nr_cpu_ids=1.
    [    0.000000] 	Trampoline variant of Tasks RCU enabled.
    [    0.000000] 	Tracing variant of Tasks RCU enabled.
    [    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 25 jiffies.
    [    0.000000] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=1
    [    0.000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0
    [    0.000000] Root IRQ handler: gic_handle_irq
    [    0.000000] GICv2m: range[mem 0x08020000-0x08020fff], SPI[80:143]
    [    0.000000] rcu: srcu_init: Setting srcu_struct sizes based on contention.
    [    0.000000] arch_timer: cp15 timer(s) running at 62.50MHz (virt).
    [    0.000000] clocksource: arch_sys_counter: mask: 0x1ffffffffffffff max_cycles: 0x1cd42e208c, max_idle_ns: 881590405314 ns
    [    0.000095] sched_clock: 57 bits at 63MHz, resolution 16ns, wraps every 4398046511096ns
    [    0.009468] Console: colour dummy device 80x25
    [    0.012025] Calibrating delay loop (skipped), value calculated using timer frequency.. 125.00 BogoMIPS (lpj=250000)
    [    0.012199] pid_max: default: 32768 minimum: 301
    [    0.013365] LSM: Security Framework initializing
    [    0.016787] Mount-cache hash table entries: 512 (order: 0, 4096 bytes, linear)
    [    0.016855] Mountpoint-cache hash table entries: 512 (order: 0, 4096 bytes, linear)
    [    0.051103] cacheinfo: Unable to detect cache hierarchy for CPU 0
    [    0.058747] cblist_init_generic: Setting adjustable number of callback queues.
    [    0.058780] cblist_init_generic: Setting shift to 0 and lim to 1.
    [    0.059146] cblist_init_generic: Setting adjustable number of callback queues.
    [    0.059161] cblist_init_generic: Setting shift to 0 and lim to 1.
    [    0.061257] rcu: Hierarchical SRCU implementation.
    [    0.061309] rcu: 	Max phase no-delay instances is 1000.
    [    0.067010] EFI services will not be available.
    [    0.067866] smp: Bringing up secondary CPUs ...
    [    0.067987] smp: Brought up 1 node, 1 CPU
    [    0.068026] SMP: Total of 1 processors activated.
    [    0.068127] CPU features: detected: 32-bit EL0 Support
    [    0.068153] CPU features: detected: 32-bit EL1 Support
    [    0.068236] CPU features: detected: CRC32 instructions
    [    0.070282] CPU: All CPU(s) started at EL1
    [    0.070479] alternatives: applying system-wide alternatives
    [    0.088350] devtmpfs: initialized
    [    0.105774] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 7645041785100000 ns
    [    0.106037] futex hash table entries: 256 (order: 2, 16384 bytes, linear)
    [    0.111798] pinctrl core: initialized pinctrl subsystem
    [    0.125265] DMI not present or invalid.
    [    0.135444] NET: Registered PF_NETLINK/PF_ROUTE protocol family
    [    0.149961] DMA: preallocated 128 KiB GFP_KERNEL pool for atomic allocations
    [    0.150574] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA pool for atomic allocations
    [    0.150841] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA32 pool for atomic allocations
    [    0.151198] audit: initializing netlink subsys (disabled)
    [    0.156299] audit: type=2000 audit(0.144:1): state=initialized audit_enabled=0 res=1
    [    0.161154] thermal_sys: Registered thermal governor 'step_wise'
    [    0.161221] thermal_sys: Registered thermal governor 'power_allocator'
    [    0.161829] cpuidle: using governor menu
    [    0.163237] hw-breakpoint: found 6 breakpoint and 4 watchpoint registers.
    [    0.163881] ASID allocator initialised with 32768 entries
    [    0.171299] Serial: AMBA PL011 UART driver
    [    0.219615] 9000000.pl011: ttyAMA0 at MMIO 0x9000000 (irq = 13, base_baud = 0) is a PL011 rev1
    [    0.236794] printk: console [ttyAMA0] enabled
    [    0.251353] KASLR enabled
    [    0.295393] HugeTLB: registered 1.00 GiB page size, pre-allocated 0 pages
    [    0.295717] HugeTLB: 0 KiB vmemmap can be freed for a 1.00 GiB page
    [    0.295958] HugeTLB: registered 32.0 MiB page size, pre-allocated 0 pages
    [    0.296156] HugeTLB: 0 KiB vmemmap can be freed for a 32.0 MiB page
    [    0.296337] HugeTLB: registered 2.00 MiB page size, pre-allocated 0 pages
    [    0.296525] HugeTLB: 0 KiB vmemmap can be freed for a 2.00 MiB page
    [    0.296864] HugeTLB: registered 64.0 KiB page size, pre-allocated 0 pages
    [    0.297054] HugeTLB: 0 KiB vmemmap can be freed for a 64.0 KiB page
    [    0.308944] ACPI: Interpreter disabled.
    [    0.316926] iommu: Default domain type: Translated 
    [    0.317171] iommu: DMA domain TLB invalidation policy: strict mode 
    [    0.319176] SCSI subsystem initialized
    [    0.322637] usbcore: registered new interface driver usbfs
    [    0.323178] usbcore: registered new interface driver hub
    [    0.323580] usbcore: registered new device driver usb
    [    0.326917] pps_core: LinuxPPS API ver. 1 registered
    [    0.327138] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
    [    0.327502] PTP clock support registered
    [    0.328253] EDAC MC: Ver: 3.0.0
    [    0.334961] FPGA manager framework
    [    0.336228] Advanced Linux Sound Architecture Driver Initialized.
    [    0.349195] vgaarb: loaded
    [    0.353694] clocksource: Switched to clocksource arch_sys_counter
    [    0.357163] VFS: Disk quotas dquot_6.6.0
    [    0.358131] VFS: Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
    [    0.360294] pnp: PnP ACPI: disabled
    [    0.382499] NET: Registered PF_INET protocol family
    [    0.385005] IP idents hash table entries: 2048 (order: 2, 16384 bytes, linear)
    [    0.392033] tcp_listen_portaddr_hash hash table entries: 256 (order: 0, 4096 bytes, linear)
    [    0.392618] Table-perturb hash table entries: 65536 (order: 6, 262144 bytes, linear)
    [    0.392930] TCP established hash table entries: 1024 (order: 1, 8192 bytes, linear)
    [    0.393335] TCP bind hash table entries: 1024 (order: 3, 32768 bytes, linear)
    [    0.393832] TCP: Hash tables configured (established 1024 bind 1024)
    [    0.395392] UDP hash table entries: 256 (order: 1, 8192 bytes, linear)
    [    0.395844] UDP-Lite hash table entries: 256 (order: 1, 8192 bytes, linear)
    [    0.397529] NET: Registered PF_UNIX/PF_LOCAL protocol family
    [    0.400742] RPC: Registered named UNIX socket transport module.
    [    0.401045] RPC: Registered udp transport module.
    [    0.401202] RPC: Registered tcp transport module.
    [    0.401350] RPC: Registered tcp NFSv4.1 backchannel transport module.
    [    0.402140] PCI: CLS 0 bytes, default 64
    [    0.407500] Unpacking initramfs...
    [    0.414428] hw perfevents: enabled with armv8_pmuv3 PMU driver, 7 counters available
    [    0.415696] kvm [1]: HYP mode not available
    [    0.424601] Initialise system trusted keyrings
    [    0.434069] workingset: timestamp_bits=42 max_order=15 bucket_order=0
    [    0.482227] squashfs: version 4.0 (2009/01/31) Phillip Lougher
    [    0.487346] NFS: Registering the id_resolver key type
    [    0.488006] Key type id_resolver registered
    [    0.488200] Key type id_legacy registered
    [    0.488886] nfs4filelayout_init: NFSv4 File Layout Driver Registering...
    [    0.489177] nfs4flexfilelayout_init: NFSv4 Flexfile Layout Driver Registering...
    [    0.495385] 9p: Installing v9fs 9p2000 file system support
    [    0.577320] Key type asymmetric registered
    [    0.581590] Asymmetric key parser 'x509' registered
    [    0.582280] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 245)
    [    0.582710] io scheduler mq-deadline registered
    [    0.583009] io scheduler kyber registered
    [    0.658304] pl061_gpio 9030000.pl061: PL061 GPIO chip registered
    [    0.671417] pci-host-generic 4010000000.pcie: host bridge /pcie@10000000 ranges:
    [    0.672652] pci-host-generic 4010000000.pcie:       IO 0x003eff0000..0x003effffff -> 0x0000000000
    [    0.678126] pci-host-generic 4010000000.pcie:      MEM 0x0010000000..0x003efeffff -> 0x0010000000
    [    0.678616] pci-host-generic 4010000000.pcie:      MEM 0x8000000000..0xffffffffff -> 0x8000000000
    [    0.679660] pci-host-generic 4010000000.pcie: Memory resource size exceeds max for 32 bits
    [    0.680689] pci-host-generic 4010000000.pcie: ECAM at [mem 0x4010000000-0x401fffffff] for [bus 00-ff]
    [    0.683086] Freeing initrd memory: 1128K
    [    0.684830] pci-host-generic 4010000000.pcie: PCI host bridge to bus 0000:00
    [    0.685387] pci_bus 0000:00: root bus resource [bus 00-ff]
    [    0.685921] pci_bus 0000:00: root bus resource [io  0x0000-0xffff]
    [    0.686153] pci_bus 0000:00: root bus resource [mem 0x10000000-0x3efeffff]
    [    0.686364] pci_bus 0000:00: root bus resource [mem 0x8000000000-0xffffffffff]
    [    0.688538] pci 0000:00:00.0: [1b36:0008] type 00 class 0x060000
    [    0.693974] pci 0000:00:01.0: [1af4:1000] type 00 class 0x020000
    [    0.694657] pci 0000:00:01.0: reg 0x10: [io  0x0000-0x001f]
    [    0.694958] pci 0000:00:01.0: reg 0x14: [mem 0x00000000-0x00000fff]
    [    0.695234] pci 0000:00:01.0: reg 0x20: [mem 0x00000000-0x00003fff 64bit pref]
    [    0.695567] pci 0000:00:01.0: reg 0x30: [mem 0x00000000-0x0003ffff pref]
    [    0.699469] pci 0000:00:01.0: BAR 6: assigned [mem 0x10000000-0x1003ffff pref]
    [    0.700177] pci 0000:00:01.0: BAR 4: assigned [mem 0x8000000000-0x8000003fff 64bit pref]
    [    0.700652] pci 0000:00:01.0: BAR 1: assigned [mem 0x10040000-0x10040fff]
    [    0.700907] pci 0000:00:01.0: BAR 0: assigned [io  0x1000-0x101f]
    [    0.708570] EINJ: ACPI disabled.
    [    0.758813] virtio-pci 0000:00:01.0: enabling device (0000 -> 0003)
    [    0.779612] Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled
    [    0.789178] SuperH (H)SCI(F) driver initialized
    [    0.791116] msm_serial: driver initialized
    [    0.796314] cacheinfo: Unable to detect cache hierarchy for CPU 0
    [    0.822383] loop: module loaded
    [    0.826167] megasas: 07.719.03.00-rc1
    [    0.834530] physmap-flash 0.flash: physmap platform flash device: [mem 0x00000000-0x03ffffff]
    [    0.836583] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
    [    0.837586] Intel/Sharp Extended Query Table at 0x0031
    [    0.838745] Using buffer write method
    [    0.839591] physmap-flash 0.flash: physmap platform flash device: [mem 0x04000000-0x07ffffff]
    [    0.840551] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
    [    0.840867] Intel/Sharp Extended Query Table at 0x0031
    [    0.841858] Using buffer write method
    [    0.842123] Concatenating MTD devices:
    [    0.842270] (0): "0.flash"
    [    0.842381] (1): "0.flash"
    [    0.842485] into device "0.flash"
    [    0.941962] tun: Universal TUN/TAP device driver, 1.6
    [    0.960873] thunder_xcv, ver 1.0
    [    0.961221] thunder_bgx, ver 1.0
    [    0.961641] nicpf, ver 1.0
    [    0.965910] hns3: Hisilicon Ethernet Network Driver for Hip08 Family - version
    [    0.966158] hns3: Copyright (c) 2017 Huawei Corporation.
    [    0.966724] hclge is initializing
    [    0.967016] e1000: Intel(R) PRO/1000 Network Driver
    [    0.967177] e1000: Copyright (c) 1999-2006 Intel Corporation.
    [    0.967517] e1000e: Intel(R) PRO/1000 Network Driver
    [    0.967684] e1000e: Copyright(c) 1999 - 2015 Intel Corporation.
    [    0.968030] igb: Intel(R) Gigabit Ethernet Network Driver
    [    0.968204] igb: Copyright (c) 2007-2014 Intel Corporation.
    [    0.968488] igbvf: Intel(R) Gigabit Virtual Function Network Driver
    [    0.968681] igbvf: Copyright (c) 2009 - 2012 Intel Corporation.
    [    0.969875] sky2: driver version 1.30
    [    0.973164] VFIO - User Level meta-driver version: 0.3
    [    0.984387] usbcore: registered new interface driver usb-storage
    [    0.995577] rtc-pl031 9010000.pl031: registered as rtc0
    [    0.996534] rtc-pl031 9010000.pl031: setting system clock to 2025-06-11T13:25:47 UTC (1749648347)
    [    0.999267] i2c_dev: i2c /dev entries driver
    [    1.017260] sdhci: Secure Digital Host Controller Interface driver
    [    1.017683] sdhci: Copyright(c) Pierre Ossman
    [    1.019711] Synopsys Designware Multimedia Card Interface Driver
    [    1.022517] sdhci-pltfm: SDHCI platform and OF driver helper
    [    1.027860] ledtrig-cpu: registered to indicate activity on CPUs
    [    1.034919] usbcore: registered new interface driver usbhid
    [    1.035136] usbhid: USB HID core driver
    [    1.053940] NET: Registered PF_PACKET protocol family
    [    1.055530] 9pnet: Installing 9P2000 support
    [    1.056076] Key type dns_resolver registered
    [    1.058038] registered taskstats version 1
    [    1.058539] Loading compiled-in X.509 certificates
    [    1.088361] input: gpio-keys as /devices/platform/gpio-keys/input/input0
    [    1.100181] ALSA device list:
    [    1.100430]   No soundcards found.
    [    1.104840] uart-pl011 9000000.pl011: no DMA platform data
    [    1.180296] Freeing unused kernel memory: 7552K
    [    1.181545] Run /init as init process

    Boot took 1.24 seconds
    /bin/sh: