Linux篇-proc文件系统
1 概述/proc 文件系统(procfs, process filesystem)是 Linux 内核对外暴露内核与进程信息的一种虚拟文件系统.它不是一个真正存在于磁盘上的文件系统,而是由内核在运行时动态生成的,用于让用户空间(用户、程序、脚本等)读取甚至控制内核状态. 目录结构. 1234567891011121314151617181920/proc├── 1/ # 进程号目录 (PID=1)│ ├── cmdline # 启动命令行参数│ ├── environ # 环境变量│ ├── fd/ # 打开的文件描述符│ ├── maps # 内存映射信息│ ├── stat # 进程状态│ ├── status # 状态信息│ └── exe -> /sbin/init│├── cpuinfo # CPU 信息├── meminfo # 内存信息├──...
Linux篇-insmod观察
1 概述本篇为 Linux 调试篇,主要是观察 Linux 常见的各种调用过程. 这里我简单书写了一个内核模块. 123456789101112131415161718192021// helloworld.c#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("GPL");MODULE_AUTHOR("Jvle");MODULE_DESCRIPTION("Hello World Kernel Module for Linux 6.6.109");static int __init hello_init(void){ pr_info("Hello, kernel world!\n"); return 0;}static void __exit hello_exit(void){ pr_info("Goodbye, kernel...
Linux篇-platform
1 数据结构1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071// 通过 platform_bus_init 函数进行注册const struct bus_type platform_bus_type = { .name = "platform", .dev_groups = platform_dev_groups, .match = platform_match, .uevent = platform_uevent, .probe = platform_probe, .remove = platform_remove, .shutdown = platform_shutdown, .dma_configure = platform_dma_configure, .dma_cleanup =...
Linux篇-设备树
1 概念 DTS: 设备树的源文件,以 .dts 结尾. DTSI: 设备树源文件的头文件, .dtsi 为扩展名. DTC: 设备树的编译器,将 DTS 和 DTSI 生成 DTB 文件. DTB: 二进制文件,以 .dtb 结尾. Linux 内核启动的时候会在 /proc/device-tree 目录下根据节点的名字创建不同的文件夹. 一般存放在 arch/<arch>/boot/dts/ 中. 编译方式. 123# 以 arm64 为例# 结果在: arch/<arch>/boot/dts/*.dtbmake ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- dtbs 也可以使用 dtc 直接编译. 1234# 编译dtc -I dts -O dtb -o output.dtb input.dts# 反编译dtc -I dtb -O dts -o output.dts input.dtb 2 节点介绍2.1 根节点根节点是整个设备树的起点. 123/ {...} 2.2...
Linux篇-driver设备模型
1 概念123456789101112131415161718192021222324252627struct device_driver { const char *name; const struct bus_type *bus; // 必须指定 bus_type struct module *owner; const char *mod_name; /* used for built-in modules */ bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ enum probe_type probe_type; const struct of_device_id *of_match_table; const struct acpi_device_id *acpi_match_table; int (*probe) (struct device *dev); void (*sync_state)(struct device *dev); int (*remove) (struct...
Linux篇-device设备模型
1 概念123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110struct device { struct kobject kobj; struct device *parent; struct device_private *p; const char *init_name; /* initial name of the device */ const struct device_type *type; const struct bus_type *bus; /* type of bus device is on */ struct device_driver...
Linux篇-bus设备模型
1 概念总线是设备模型当中的基础组件,简化了驱动的编写和维护,总线可以是物理总线(如PCI,USB)或虚拟总线. 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455struct bus_type { const char *name; // 总线类型名称 const char *dev_name; // 老版本内核这里可能不一样 const struct attribute_group **bus_groups; // 总线属性 const struct attribute_group **dev_groups; // 设备属性 const struct attribute_group **drv_groups; // 驱动属性 int (*match)(struct device *dev, const struct device_driver *drv); // 匹配检查 int...
Linux篇-sysfs
概念sysfs 是 Linux 提供的一种虚拟文件系统,他以一种层次的方式组织数据,向用户空间提供内核中的信息,让用户可以通过文件系统接口访问和操作内核对象的属性. 他提供了一种统一管理的接口,通常在 /sys 目录下挂载. /sys/class: 包含了设备类别的子目录. 123456789jvle@jvle-ThinkPad-X1-Carbon-Gen-8:~/Desktop/works/temp/linux_files/modules/kobjs$ ls /sys/class/accel bsg drm hwmon lirc nd platform-profile pwm scsi_device tpm vcata_device devcoredump drm_dp_aux_dev i2c-dev mdio_bus net ...
Linux篇-kobj
1 概念kobj 是内核抽象出来的通用对象模型,用于表示内核中的实体. 结构体: https://elixir.bootlin.com/linux/v6.17.1/source/include/linux/kobject.h#L64. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// kobjstruct kobject { const char *name; // kobj 对象的名称, 跟 sys/<父 name>/<name> 一致 struct list_head entry; // 与父 kobj 建立链接的链表, 是父 kobj 的子链表 struct kobject *parent; // 指向父 kobj struct kset *kset; // 指向包含该 kobj 的 set 集合 const struct kobj_type *ktype;...
Grub篇(调试grub2)
1 调试grub21.1 编译 grub2首先从 https://cgit.git.savannah.gnu.org/cgit/grub.git 拉下压缩包. 1234# 在grub2安装目录下# --with-platform=efi 可以编译 UEFI 版本../configure --with-platform=pc --target=i386 --disable-werrormake -j$(nproc) 编译完成后,grub-core/ 下会有很多文件. 各种 *.mod 模块 kernel.exec(GRUB 核心可执行) 若干 .image / .img 文件 gdb_grub, 调试用脚本. 1.2 构建 img12# 创建一个空的磁盘镜像dd if=/dev/zero of=disk.img bs=1M count=256 建立分区并格式化. 123456789101112131415161718192021222324# 建立分区parted disk.img --script mklabel msdosparted...