学习内核编程

发布于 2024-07-22 06:35:20 字数 1542 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

﹉夏雨初晴づ 2024-07-29 06:35:20
**TODO** +editPic: Linux Kernel Developer -> (Ring Layer 0)
         +addSection: Kernel Virtualization Engine

KERN_WARN_CODING_STYLE: Do not Loop unless you absolutely have to.

针对未初始化void *i的推荐书籍

“人们只有在拥有一定的生命之后才能理解书籍,或者至少没有人能够理解一本深刻的书,直到他至少看到并经历了其中的一部分内容”。 –埃兹拉·庞德

一千个代码英里的旅程必须从一步开始。 如果您对从以下哪本书开始感到困惑,请不要担心,选择您喜欢的任何一本。 并非所有徘徊的人都迷失了方向。 由于所有道路最终都会连接到高速公路,随着页面的进展,您将在内核旅程中探索新事物,而不会遇到任何死胡同,并最终连接到代码集。 保持清醒的头脑阅读并记住:代码不是文学

剩下的不是事物、情感、图像、心理图片、记忆,甚至不是想法。 它是一个函数。 某种过程。
生命的一个方面,可以被描述为“更大”事物的功能。
因此,它似乎并不是真正与那个东西“分离”
别的。 就像刀的功能——切割某物——事实上并不是分开的
来自刀本身。 该功能目前可能正在使用,也可能没有使用,
但它可能永远不会分离。

Solovay Strassen 素性测试去随机化算法

Solovay Strassen 素性测试去随机算法

阅读时不要反驳和反驳; 也不相信并认为理所当然; 也找不到谈话和话语; 但要权衡和考虑。 有的书是要浅尝的,有的书是要吞咽的,有的书是要咀嚼消化的:也就是说,有的书要读部分,有的书要读,但不要好奇,有的书要全读。 ,并勤奋和专注。

static void tasklet_hi_action(struct softirq_action *a)
{
        struct tasklet_struct *list;

        local_irq_disable();
        list = __this_cpu_read(tasklet_hi_vec.head);
        __this_cpu_write(tasklet_hi_vec.head, NULL);
        __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
        local_irq_enable();

        while (list) {
                struct tasklet_struct *t = list;

                list = list->next;

                if (tasklet_trylock(t)) {
                        if (!atomic_read(&t->count)) {
                                if (!test_and_clear_bit(TASKLET_STATE_SCHED,
                                                        &t->state))
                                        BUG();
                                t->func(t->data);
                                tasklet_unlock(t);
                                continue;
                        }
                        tasklet_unlock(t);
                }

                local_irq_disable();
                t->next = NULL;
                *__this_cpu_read(tasklet_hi_vec.tail) = t;
                __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
                __raise_softirq_irqoff(HI_SOFTIRQ);
                local_irq_enable();
        }
}

核心 Linux(5 -> 1 -> 3 -> 2 -> 7 -> 4 -> 6)

“大自然既没有内核,也没有外壳; 她同时就是一切”——约翰·沃尔夫冈·冯·歌德

读者应该精通操作系统概念; 对长时间运行的进程及其与短时间突发执行的进程的差异有一个公平的理解; 容错同时满足软实时和硬实时约束。 在阅读时,理解并确认 Linux 内核源代码在核心子系统中所做的设计选择非常重要。

线程[和]信号是一条依赖于平台的痛苦、绝望、恐怖和疯狂的踪迹(~安东尼·巴克斯特)。 话虽如此,在深入研究内核之前,您应该成为一名自我评估的 C 专家。 您还应该对链表、堆栈、队列、红黑树、哈希函数等有良好的经验。

volatile int i;
int main(void)
{
    int c;
    for (i=0; i<3; i++) {
        c = i&&&i;
        printf("%d\n", c);    /* find c */
    }
    return 0;
}

Linux 内核源代码的美丽和艺术在于所使用的有意的代码混淆。 这通常是必要的,以便以干净而优雅的方式传达涉及两个或多个操作的计算含义。 在为多核架构编写代码时尤其如此。

实时系统视频讲座任务安排内存压缩内存屏障SMP

#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
  1. Linux 内核开发 - Robert Love
  2. 了解 Linux 内核 - Daniel P. Bovet、Marco Cesati
  3. Linux 内核设计艺术 - 杨丽香
  4. 专业 Linux 内核架构 - Wolfgang Mauerer
  5. UNIX 操作系统的设计 - Maurice J. Bach
  6. 了解 Linux 虚拟内存管理器 - Mel Gorman
  7. Linux 内核内部 - Tigran Aivazian
  8. 嵌入式 Linux 入门 - Christopher Hallinan

Linux 设备驱动程序 ( 1 -> 2 -> 4 -> 3 -> 8 -> ... )

“音乐不会陪伴你前进。你必须严格依靠你的能力来陪伴它,真正专注于情感或故事的那一小部分核心”。 ——黛比·哈利

您的任务基本上是在硬件设备和软件内核之间建立高速通信接口。 您应该阅读硬件参考数据表/手册,以了解设备的行为及其控制和数据状态以及提供的物理通道。 从长远来看,了解您特定架构的汇编知识以及对 VLSI 硬件描述语言(如 VHDL 或 Verilog)的了解将对您有所帮助。

:但是,为什么我必须阅读硬件规格?

A:因为,“软件无法跨越碳和硅的鸿沟” - Rahul Sonnad

然而,上述情况对于计算算法来说并不构成问题(驱动程序代码 - 下半部处理),因为它可以在 通用图灵机。 如果计算结果在数学领域成立,则可以肯定在物理域中也是如此。

有关 Linux 设备驱动程序的视频讲座(Lec 17 和 18),嵌入式 KMS 驱动程序剖析引脚控制和 GPIO 更新通用时钟框架, 编写真正的 Linux 驱动程序 - Greg KH

static irqreturn_t phy_interrupt(int irq, void *phy_dat)
{
         struct phy_device *phydev = phy_dat;

         if (PHY_HALTED == phydev->state)
                 return IRQ_NONE;                /* It can't be ours.  */

         /* The MDIO bus is not allowed to be written in interrupt
          * context, so we need to disable the irq here.  A work
          * queue will write the PHY to disable and clear the
          * interrupt, and then reenable the irq line.
          */
         disable_irq_nosync(irq);
         atomic_inc(&phydev->irq_disable);

         queue_work(system_power_efficient_wq, &phydev->phy_queue);

         return IRQ_HANDLED;
}
  1. Linux 设备驱动程序 - Jonathan Corbet、Alessandro Rubini 和 Greg Kroah-Hartman
  2. 基本 Linux 设备驱动程序 - Sreekrishnan Venkateswaran
  3. 编写 Linux 设备驱动程序 - Jerry Cooperstein
  4. Linux 内核模块编程指南 - Peter Jay Salzman、Michael Burian、Ori Pomerantz
  5. Linux PCMCIA 程序员指南 - David Hinds
  6. Linux SCSI 编程指南 - Heiko Eibfeldt
  7. POSIX 操作系统串行编程指南 - Michael R. Sweet
  8. Linux 图形驱动程序:简介 - Stéphane Marchesin
  9. Linux USB 设备驱动程序编程指南 - Detlef Fliegl
  10. Linux 内核设备模型 - Patrick Mochel

内核网络(1 -> 2 -> 3 -> ...)

“称其为氏族,称其为网络,称其为部落,称其为家庭:无论您如何称呼它,无论您是谁,您都需要一个。” - 简·霍华德

了解内核中的数据包演练是理解内核网络的关键。 如果我们想了解 Netfilter 或 IPSec 内部结构等,就必须了解它。 两个结构体是:struct sk_buff和struct net_device

static inline int sk_hashed(const struct sock *sk)
{
        return !sk_unhashed(sk);
} 
  1. Linux内核网络层最重要的 Internals-Christian-Benvenuti/dp/0596002556" rel="noreferrer">了解 Linux 网络内部结构 - Christian Benvenuti
  2. Linux 内核网络:实现和理论 - Rami Rosen
  3. UNIX 网络编程 - W. Richard Stevens
  4. Linux 网络编程权威指南 - Keir Davis、John W. Turner、Nathan Yocom
  5. Linux TCP/IP 堆栈:嵌入式系统网络 - Thomas F. Herbert
  6. Linux 套接字编程示例 - Warren W. Gay
  7. Linux 高级路由& 交通控制指南 - Bert Hubert

内核调试(1 -> 4 -> 9 -> ...)

除非在与它沟通时准确说出自己的意思,否则必然会产生麻烦。 ~艾伦·图灵,关于计算机

Brian W. Kernighan 在《Unix for Beginners》(1979)一文中说,“最有效的调试工具仍然是仔细的思考,加上明智地放置打印语句”。 了解要收集的内容将帮助您快速获取正确的数据以进行快速诊断。 伟大的计算机科学家 Edsger Dijkstra 曾经说过,测试可以证明错误的存在,但不能证明错误的不存在。 良好的调查实践应该平衡快速解决问题的需要、培养技能的需要以及主题专家的有效利用。

有时候,当你跌入谷底时,似乎什么都不起作用,你也没有了所有的选择。 然后才是真正的调试开始。 错误可能会为您提供所需的休息时间,让您摆脱对无效解决方案的执着。

有关内核调试和分析的视频讲座核心转储分析使用 GDB 进行多核调试控制多核竞争条件调试电子设备

/* Buggy Code -- Stack frame problem
 * If you require information, do not free memory containing the information
 */
char *initialize() {
  char string[80];
  char* ptr = string;
  return ptr;
}

int main() {
  char *myval = initialize();
  do_something_with(myval);
}
/*  “When debugging, novices insert corrective code; experts remove defective code.”
 *     – Richard Pattis
#if DEBUG
 printk("The above can be considered as Development and Review in Industrial Practises");
#endif
 */
  1. Linux 调试和性能调整 - Steve Best
  2. Linux 应用程序调试技术 - Aurelian Melinte
  3. 使用 GDB 进行调试:GNU 源代码级调试器 - Roland H. Pesch
  4. 调试嵌入式 Linux - Christopher Hallinan
  5. 使用 GDB、DDD 和 Eclipse 进行调试的艺术 - Norman S. Matloff
  6. 程序失败的原因:指南系统调试 - Andreas Zeller
  7. 软件驱魔:调试和优化遗留代码的手册 - Bill Blunden
  8. 调试:查找最难以捉摸的软件和硬件问题 - David J. Agans
  9. 思考调试:多学科方法 - Robert Charles Metzger
  10. 查找 Bug:一本错误程序之书 - Adam Barr

文件系统(1 -> 2 -> 6 -> ...)

“我想要虚拟内存,至少因为它与文件系统耦合”。 ——肯·汤普森

在 UNIX 系统上,一切都是文件; 如果某个东西不是文件,那么它就是一个进程,除了命名管道和套接字。 在文件系统中,文件由索引节点表示,索引节点是一种序列号,包含有关构成文件的实际数据的信息。 Linux 虚拟文件系统 VFS 在安装和使用每个文件系统时将信息缓存在内存中。 必须非常小心地正确更新文件系统,因为这些缓存中的数据会随着文件和目录的创建、写入和删除而被修改。 这些缓存中最重要的是缓冲区缓存,它集成到各个文件系统访问其底层块存储设备的方式中。

存储系统视频讲座Flash 友好文件系统

long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
{
        struct open_flags op;
        int fd = build_open_flags(flags, mode, &op);
        struct filename *tmp;

        if (fd)
                return fd;

        tmp = getname(filename);
        if (IS_ERR(tmp))
                return PTR_ERR(tmp);

        fd = get_unused_fd_flags(flags);
        if (fd >= 0) {
                struct file *f = do_filp_open(dfd, tmp, &op);
                if (IS_ERR(f)) {
                        put_unused_fd(fd);
                        fd = PTR_ERR(f);
                } else {
                        fsnotify_open(f);
                        fd_install(fd, f);
                }
        }
        putname(tmp);
        return fd;
}

SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
{
        if (force_o_largefile())
                flags |= O_LARGEFILE;

        return do_sys_open(AT_FDCWD, filename, flags, mode);
}
  1. Linux 文件系统 - Moshe Bar
  2. Linux 文件系统 - William Von Hagen
  3. UNIX 文件系统:演变、设计和实现 - Steve D. Pate
  4. 实用文件系统设计 - Dominic Giampaolo
  5. 文件系统取证分析 - Brian Carrier
  6. Linux 文件系统层次结构 - Binh Nguyen
  7. BTRFS:Linux B 树文件系统< /a> - Ohad Rodeh
  8. StegFS:Linux 的隐写文件系统 - 安德鲁·D·麦克唐纳、马库斯·G·库恩

安全性(1 -> 2 -> 8 -> 4 -> 3 -> ...)

“UNIX 的设计目的并不是为了阻止用户做愚蠢的事情,因为这也会阻止他们做聪明的事情”。 — 道格·格温

如果不使用,任何技术都无法发挥作用。 道德随着技术的变化而改变。

F × S = k”自由与安全的乘积是一个常数。 - 尼文定律

密码学构成了在线信任的基础。 黑客攻击是利用技术、物理或人为因素的安全控制。 保护内核免受其他正在运行的程序的影响是迈向安全稳定系统的第一步,但这显然还不够:不同用户态应用程序之间也必须存在某种程度的保护。 漏洞利用可以针对本地或远程服务。

“你无法用蛮力破解你的命运……你需要一个后门,一个进入生活的侧通道。”
― 克莱德·杜萨扎

计算机不解决问题,而是执行解决方案。 在每个非确定性算法代码的背后,都有一个决心
-- /var/log/dmesg

视频讲座 密码学和网络安全安全命名空间防范远程攻击安全嵌入式 Linux

env x='() { :;}; echo vulnerable' bash -c "echo this is a test for Shellsock"
  1. 黑客攻击:利用的艺术< /a> - Jon Erickson
  2. Rootkit 军械库:逃脱与逃避系统的黑暗角落 - Bill Blunden
  3. 黑客暴露:网络安全秘密 - Stuart McClure、Joel Scambray、George Kurtz
  4. 内核利用指南:攻击核心 - Enrico Perla、Massimiliano Oldani
  5. 记忆取证的艺术 - Michael Hale Ligh、Andrew Case、Jamie Levy、AAron Walters
  6. 实用逆向工程 - Bruce Dang,Alexandre公报,Elias Bachaalany
  7. 实用恶意软件分析 - Michael Sikorski、Andrew Honig
  8. Linux 最高安全性:黑客指南保护您的 Linux 服务器 - 匿名
  9. Linux 安全 - Craig Hunt
  10. 真实世界的 Linux安全性 - Bob Toxen

内核源代码(0.11 -> 2.4 -> 2.6 -> 3.18)

“就像葡萄酒一样,对内核编程的掌握会随着时间的推移而成熟。但是,与葡萄酒不同的是,它在这个过程中变得更加甜蜜”。 ——劳伦斯·穆切卡

你可能不认为程序员是艺​​术家,但编程是一个极具创造性的职业。 这是基于逻辑的创造力。 计算机科学教育不能使任何人成为专家程序员,就像学习画笔和颜料不能使人成为专家画家一样。 正如你已经知道的,知路和行路是有区别的。 卷起袖子亲自动手研究内核源代码是至关重要的。 最后,凭借您由此获得的内核知识,无论您走到哪里,您都会发光

不成熟的程序员会模仿; 成熟的程序员偷窃; 糟糕的程序员会破坏他们所获得的东西,而优秀的程序员会将其变成更好的东西,或者至少是不同的东西。 优秀的程序员将他的盗窃行为焊接成一种独特的整体感觉,与被撕裂的感觉完全不同。

关于内核配方的视频讲座

linux-0.11
├── boot
│   ├── bootsect.s      head.s      setup.s
├── fs
│   ├── bitmap.c    block_dev.c buffer.c        char_dev.c  exec.c
│   ├── fcntl.c     file_dev.c  file_table.c    inode.c     ioctl.c
│   ├── namei.c     open.c      pipe.c          read_write.c
│   ├── stat.c      super.c     truncate.c
├── include
│   ├── a.out.h     const.h     ctype.h     errno.h     fcntl.h
│   ├── signal.h    stdarg.h    stddef.h    string.h    termios.h
│   ├── time.h      unistd.h    utime.h
│   ├── asm
│   │   ├── io.h    memory.h    segment.h   system.h
│   ├── linux
│   │   ├── config.h    fdreg.h fs.h    hdreg.h     head.h
│   │   ├── kernel.h    mm.h    sched.h sys.h       tty.h
│   ├── sys
│   │   ├── stat.h      times.h types.h utsname.h   wait.h
├── init
│   └── main.c
├── kernel
│   ├── asm.s       exit.c      fork.c      mktime.c    panic.c
│   ├── printk.c    sched.c     signal.c    sys.c       system_calls.s
│   ├── traps.c     vsprintf.c
│   ├── blk_drv
│   │   ├── blk.h   floppy.c    hd.c    ll_rw_blk.c     ramdisk.c
│   ├── chr_drv
│   │   ├── console.c   keyboard.S  rs_io.s
│   │   ├── serial.c    tty_io.c    tty_ioctl.c
│   ├── math
│   │   ├── math_emulate.c
├── lib
│   ├── close.c  ctype.c  dup.c     errno.c  execve.c  _exit.c
│   ├── malloc.c open.c   setsid.c  string.c wait.c    write.c
├── Makefile
├── mm
│   ├── memory.c page.s
└── tools
    └── build.c
  1. 初学者从Linux 0.11 源(源代码少于 20,000 行)。 经过20年的发展,与Linux 0.11相比,Linux已经变得非常庞大、复杂、难学。 但设计理念和主体结构没有根本性变化。 学习Linux 0.11还是有重要的现实意义的。
  2. 内核黑客必读 => Linux_source_dir/Documentation/*
  3. 您应该订阅并活跃于至少一个内核邮件列表。 从内核新手开始。
  4. 您不需要阅读完整的源代码。 一旦您熟悉了内核 API 及其用法,就可以直接从您感兴趣的子系统的源代码开始。您也可以从编写自己的即插即用模块开始对内核进行实验。
  5. 设备驱动程序编写者将受益于拥有自己的专用硬件。 从 Raspberry Pi 开始。
**TODO** +editPic: Linux Kernel Developer -> (Ring Layer 0)
         +addSection: Kernel Virtualization Engine

KERN_WARN_CODING_STYLE: Do not Loop unless you absolutely have to.

Recommended Books for the Uninitialized void *i

"Men do not understand books until they have a certain amount of life, or at any rate no man understands a deep book, until he has seen and lived at least part of its contents". –Ezra Pound

A journey of a thousand code-miles must begin with a single step. If you are in confusion about which of the following books to start with, don't worry, pick any one of your choice. Not all those who wander are lost. As all roads ultimately connect to highway, you will explore new things in your kernel journey as the pages progress without meeting any dead ends, and ultimately connect to the code-set. Read with alert mind and remember: Code is not Literature.

What is left is not a thing or an emotion or an image or a mental picture or a memory or even an idea. It is a function. A process of some sort.
An aspect of Life that could be described as a function of something "larger".
And therefore, it appears that it is not really "separate" from that something
else. Like the function of a knife - cutting something - is not, in fact, separate
from the knife itself. The function may or may not be in use at the moment,
but it is potentially NEVER separate.

Solovay Strassen Derandomized Algorithm for Primality Test:

Solovay Strassen Derandomized Algorithm for Primality Test

Read not to contradict and confute; nor to believe and take for granted; nor to find talk and discourse; but to weigh and consider. Some books are to be tasted, others to be swallowed, and some few to be chewed and digested: that is, some books are to be read only in parts, others to be read, but not curiously, and some few to be read wholly, and with diligence and attention.

static void tasklet_hi_action(struct softirq_action *a)
{
        struct tasklet_struct *list;

        local_irq_disable();
        list = __this_cpu_read(tasklet_hi_vec.head);
        __this_cpu_write(tasklet_hi_vec.head, NULL);
        __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
        local_irq_enable();

        while (list) {
                struct tasklet_struct *t = list;

                list = list->next;

                if (tasklet_trylock(t)) {
                        if (!atomic_read(&t->count)) {
                                if (!test_and_clear_bit(TASKLET_STATE_SCHED,
                                                        &t->state))
                                        BUG();
                                t->func(t->data);
                                tasklet_unlock(t);
                                continue;
                        }
                        tasklet_unlock(t);
                }

                local_irq_disable();
                t->next = NULL;
                *__this_cpu_read(tasklet_hi_vec.tail) = t;
                __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
                __raise_softirq_irqoff(HI_SOFTIRQ);
                local_irq_enable();
        }
}

Core Linux ( 5 -> 1 -> 3 -> 2 -> 7 -> 4 -> 6 )

“Nature has neither kernel nor shell; she is everything at once” -- Johann Wolfgang von Goethe

Reader should be well versed with operating system concepts; a fair understanding of long running processes and its differences with processes with short bursts of execution; fault tolerance while meeting soft and hard real time constraints. While reading, it's important to understand and n/ack the design choices made by the linux kernel source in the core subsystems.

Threads [and] signals [are] a platform-dependent trail of misery, despair, horror and madness (~Anthony Baxte). That being said you should be a self-evaluating C expert, before diving into the kernel. You should also have good experience with Linked Lists, Stacks, Queues, Red Blacks Trees, Hash Functions, et al.

volatile int i;
int main(void)
{
    int c;
    for (i=0; i<3; i++) {
        c = i&&&i;
        printf("%d\n", c);    /* find c */
    }
    return 0;
}

The beauty and art of the Linux Kernel source lies in the deliberate code obfuscation used along. This is often necessitated as to convey the computational meaning involving two or more operations in a clean and elegant way. This is especially true when writing code for multi-core architecture.

Video Lectures on Real-Time Systems, Task Scheduling, Memory Compression, Memory Barriers, SMP

#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
  1. Linux Kernel Development - Robert Love
  2. Understanding the Linux Kernel - Daniel P. Bovet, Marco Cesati
  3. The Art of Linux KerneL Design - Yang Lixiang
  4. Professional Linux Kernel Architecture - Wolfgang Mauerer
  5. Design of the UNIX Operating System - Maurice J. Bach
  6. Understanding the Linux Virtual Memory Manager - Mel Gorman
  7. Linux Kernel Internals - Tigran Aivazian
  8. Embedded Linux Primer - Christopher Hallinan

Linux Device Drivers ( 1 -> 2 -> 4 -> 3 -> 8 -> ... )

"Music does not carry you along. You have to carry it along strictly by your ability to really just focus on that little small kernel of emotion or story". -- Debbie Harry

Your task is basically to establish a high speed communication interface between the hardware device and the software kernel. You should read the hardware reference datasheet/manual to understand the behavior of the device and it's control and data states and provided physical channels. Knowledge of Assembly for your particular architecture and a fair knowledge of VLSI Hardware Description Languages like VHDL or Verilog will help you in the long run.

Q: But, why do I have to read the hardware specs?

A: Because, "There is a chasm of carbon and silicon the software can't bridge" - Rahul Sonnad

However, the above doesn't poses a problem for Computational Algorithms (Driver code - bottom-half processing), as it can be fully simulated on a Universal Turing Machine. If the computed result holds true in the mathematical domain, it's a certainty that it is also true in the physical domain.

Video Lectures on Linux Device Drivers (Lec. 17 & 18), Anatomy of an Embedded KMS Driver, Pin Control and GPIO Update, Common Clock Framework, Write a Real Linux Driver - Greg KH

static irqreturn_t phy_interrupt(int irq, void *phy_dat)
{
         struct phy_device *phydev = phy_dat;

         if (PHY_HALTED == phydev->state)
                 return IRQ_NONE;                /* It can't be ours.  */

         /* The MDIO bus is not allowed to be written in interrupt
          * context, so we need to disable the irq here.  A work
          * queue will write the PHY to disable and clear the
          * interrupt, and then reenable the irq line.
          */
         disable_irq_nosync(irq);
         atomic_inc(&phydev->irq_disable);

         queue_work(system_power_efficient_wq, &phydev->phy_queue);

         return IRQ_HANDLED;
}
  1. Linux Device Drivers - Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman
  2. Essential Linux Device Drivers - Sreekrishnan Venkateswaran
  3. Writing Linux Device Drivers - Jerry Cooperstein
  4. The Linux Kernel Module Programming Guide - Peter Jay Salzman, Michael Burian, Ori Pomerantz
  5. Linux PCMCIA Programmer's Guide - David Hinds
  6. Linux SCSI Programming Howto - Heiko Eibfeldt
  7. Serial Programming Guide for POSIX Operating Systems - Michael R. Sweet
  8. Linux Graphics Drivers: an Introduction - Stéphane Marchesin
  9. Programming Guide for Linux USB Device Drivers - Detlef Fliegl
  10. The Linux Kernel Device Model - Patrick Mochel

Kernel Networking ( 1 -> 2 -> 3 -> ... )

“Call it a clan, call it a network, call it a tribe, call it a family: Whatever you call it, whoever you are, you need one.” - Jane Howard

Understanding a packet walk-through in the kernel is a key to understanding kernel networking. Understanding it is a must if we want to understand Netfilter or IPSec internals, and more. The two most important structures of linux kernel network layer are: struct sk_buff and struct net_device

static inline int sk_hashed(const struct sock *sk)
{
        return !sk_unhashed(sk);
} 
  1. Understanding Linux Network Internals - Christian Benvenuti
  2. Linux Kernel Networking: Implementation and Theory - Rami Rosen
  3. UNIX Network Programming - W. Richard Stevens
  4. The Definitive Guide to Linux Network Programming - Keir Davis, John W. Turner, Nathan Yocom
  5. The Linux TCP/IP Stack: Networking for Embedded Systems - Thomas F. Herbert
  6. Linux Socket Programming by Example - Warren W. Gay
  7. Linux Advanced Routing & Traffic Control HOWTO - Bert Hubert

Kernel Debugging ( 1 -> 4 -> 9 -> ... )

Unless in communicating with it one says exactly what one means, trouble is bound to result. ~Alan Turing, about computers

Brian W. Kernighan, in the paper Unix for Beginners (1979) said, "The most effective debugging tool is still careful thought, coupled with judiciously placed print statements". Knowing what to collect will help you to get the right data quickly for a fast diagnosis. The great computer scientist Edsger Dijkstra once said that testing can demonstrate the presence of bugs but not their absence. Good investigation practices should balance the need to solve problems quickly, the need to build your skills, and the effective use of subject matter experts.

There are times when you hit rock-bottom, nothing seems to work and you run out of all your options. Its then that the real debugging begins. A bug may provide the break you need to disengage from a fixation on the ineffective solution.

Video Lectures on Kernel Debug and Profiling, Core Dump Analysis, Multicore Debugging with GDB, Controlling Multi-Core Race Conditions, Debugging Electronics

/* Buggy Code -- Stack frame problem
 * If you require information, do not free memory containing the information
 */
char *initialize() {
  char string[80];
  char* ptr = string;
  return ptr;
}

int main() {
  char *myval = initialize();
  do_something_with(myval);
}
/*  “When debugging, novices insert corrective code; experts remove defective code.”
 *     – Richard Pattis
#if DEBUG
 printk("The above can be considered as Development and Review in Industrial Practises");
#endif
 */
  1. Linux Debugging and Performance Tuning - Steve Best
  2. Linux Applications Debugging Techniques - Aurelian Melinte
  3. Debugging with GDB: The GNU Source-Level Debugger - Roland H. Pesch
  4. Debugging Embedded Linux - Christopher Hallinan
  5. The Art of Debugging with GDB, DDD, and Eclipse - Norman S. Matloff
  6. Why Programs Fail: A Guide to Systematic Debugging - Andreas Zeller
  7. Software Exorcism: A Handbook for Debugging and Optimizing Legacy Code - Bill Blunden
  8. Debugging: Finding most Elusive Software and Hardware Problems - David J. Agans
  9. Debugging by Thinking: A Multidisciplinary Approach - Robert Charles Metzger
  10. Find the Bug: A Book of Incorrect Programs - Adam Barr

File Systems ( 1 -> 2 -> 6 -> ... )

"I wanted to have virtual memory, at least as it's coupled with file systems". -- Ken Thompson

On a UNIX system, everything is a file; if something is not a file, it is a process, except for named pipes and sockets. In a file system, a file is represented by an inode, a kind of serial number containing information about the actual data that makes up the file. The Linux Virtual File System VFS caches information in memory from each file system as it is mounted and used. A lot of care must be taken to update the file system correctly as data within these caches is modified as files and directories are created, written to and deleted. The most important of these caches is the Buffer Cache, which is integrated into the way that the individual file systems access their underlying block storage devices.

Video Lectures on Storage Systems, Flash Friendly File System

long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
{
        struct open_flags op;
        int fd = build_open_flags(flags, mode, &op);
        struct filename *tmp;

        if (fd)
                return fd;

        tmp = getname(filename);
        if (IS_ERR(tmp))
                return PTR_ERR(tmp);

        fd = get_unused_fd_flags(flags);
        if (fd >= 0) {
                struct file *f = do_filp_open(dfd, tmp, &op);
                if (IS_ERR(f)) {
                        put_unused_fd(fd);
                        fd = PTR_ERR(f);
                } else {
                        fsnotify_open(f);
                        fd_install(fd, f);
                }
        }
        putname(tmp);
        return fd;
}

SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
{
        if (force_o_largefile())
                flags |= O_LARGEFILE;

        return do_sys_open(AT_FDCWD, filename, flags, mode);
}
  1. Linux File Systems - Moshe Bar
  2. Linux Filesystems - William Von Hagen
  3. UNIX Filesystems: Evolution, Design, and Implementation - Steve D. Pate
  4. Practical File System Design - Dominic Giampaolo
  5. File System Forensic Analysis - Brian Carrier
  6. Linux Filesystem Hierarchy - Binh Nguyen
  7. BTRFS: The Linux B-tree Filesystem - Ohad Rodeh
  8. StegFS: A Steganographic File System for Linux - Andrew D. McDonald, Markus G. Kuhn

Security ( 1 -> 2 -> 8 -> 4 -> 3 -> ... )

"UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things". — Doug Gwyn

No technique works if it isn't used. Ethics change with technology.

"F × S = k" the product of freedom and security is a constant. - Niven's Laws

Cryptography forms the basis of trust online. Hacking is exploiting security controls either in a technical, physical or a human-based element. Protecting the kernel from other running programs is a first step toward a secure and stable system, but this is obviously not enough: some degree of protection must exist between different user-land applications as well. Exploits can target local or remote services.

“You can't hack your destiny, brute force...you need a back door, a side channel into Life."
― Clyde Dsouza

Computers do not solve problems, they execute solutions. Behind every non-deterministic algorithmic code, there is a determined mind.
-- /var/log/dmesg

Video Lectures on Cryptography and Network Security, Namespaces for Security, Protection Against Remote Attacks, Secure Embedded Linux

env x='() { :;}; echo vulnerable' bash -c "echo this is a test for Shellsock"
  1. Hacking: The Art of Exploitation - Jon Erickson
  2. The Rootkit Arsenal: Escape and Evasion in the Dark Corners of the System - Bill Blunden
  3. Hacking Exposed: Network Security Secrets - Stuart McClure, Joel Scambray, George Kurtz
  4. A Guide to Kernel Exploitation: Attacking the Core - Enrico Perla, Massimiliano Oldani
  5. The Art of Memory Forensics - Michael Hale Ligh, Andrew Case, Jamie Levy, AAron Walters
  6. Practical Reverse Engineering - Bruce Dang, Alexandre Gazet, Elias Bachaalany
  7. Practical Malware Analysis - Michael Sikorski, Andrew Honig
  8. Maximum Linux Security: A Hacker's Guide to Protecting Your Linux Server - Anonymous
  9. Linux Security - Craig Hunt
  10. Real World Linux Security - Bob Toxen

Kernel Source ( 0.11 -> 2.4 -> 2.6 -> 3.18 )

"Like wine, the mastery of kernel programming matures with time. But, unlike wine, it gets sweeter in the process". --Lawrence Mucheka

You might not think that programmers are artists, but programming is an extremely creative profession. It's logic-based creativity. Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. As you already know, there is a difference between knowing the path and walking the path; it is of utmost importance to roll up your sleeves and get your hands dirty with kernel source code. Finally, with your thus gained kernel knowledge, wherever you go, you will shine.

Immature coders imitate; mature coders steal; bad coders deface what they take, and good coders make it into something better, or at least something different. The good coder welds his theft into a whole of feeling which is unique, utterly different from that from which it was torn.

Video Lectures on Kernel Recipes

linux-0.11
├── boot
│   ├── bootsect.s      head.s      setup.s
├── fs
│   ├── bitmap.c    block_dev.c buffer.c        char_dev.c  exec.c
│   ├── fcntl.c     file_dev.c  file_table.c    inode.c     ioctl.c
│   ├── namei.c     open.c      pipe.c          read_write.c
│   ├── stat.c      super.c     truncate.c
├── include
│   ├── a.out.h     const.h     ctype.h     errno.h     fcntl.h
│   ├── signal.h    stdarg.h    stddef.h    string.h    termios.h
│   ├── time.h      unistd.h    utime.h
│   ├── asm
│   │   ├── io.h    memory.h    segment.h   system.h
│   ├── linux
│   │   ├── config.h    fdreg.h fs.h    hdreg.h     head.h
│   │   ├── kernel.h    mm.h    sched.h sys.h       tty.h
│   ├── sys
│   │   ├── stat.h      times.h types.h utsname.h   wait.h
├── init
│   └── main.c
├── kernel
│   ├── asm.s       exit.c      fork.c      mktime.c    panic.c
│   ├── printk.c    sched.c     signal.c    sys.c       system_calls.s
│   ├── traps.c     vsprintf.c
│   ├── blk_drv
│   │   ├── blk.h   floppy.c    hd.c    ll_rw_blk.c     ramdisk.c
│   ├── chr_drv
│   │   ├── console.c   keyboard.S  rs_io.s
│   │   ├── serial.c    tty_io.c    tty_ioctl.c
│   ├── math
│   │   ├── math_emulate.c
├── lib
│   ├── close.c  ctype.c  dup.c     errno.c  execve.c  _exit.c
│   ├── malloc.c open.c   setsid.c  string.c wait.c    write.c
├── Makefile
├── mm
│   ├── memory.c page.s
└── tools
    └── build.c
  1. Beginner's start with Linux 0.11 source (less than 20,000 lines of source code). After 20 years of development, compared with Linux 0.11, Linux has become very huge, complex, and difficult to learn. But the design concept and main structure have no fundamental changes. Learning Linux 0.11 still has important practical significance.
  2. Mandatory Reading for Kernel Hackers => Linux_source_dir/Documentation/*
  3. You should be subscribed and active on at-least one kernel mailing list. Start with kernel newbies.
  4. You do not need to read the full source code. Once you are familiar with the kernel API's and its usage, directly start with the source code of the sub-system you are interested in. You can also start with writing your own plug-n-play modules to experiment with the kernel.
  5. Device Driver writers would benefit by having their own dedicated hardware. Start with Raspberry Pi.
三五鸿雁 2024-07-29 06:35:20

尝试获取 Robert Love 的有关 Linux 内核编程的书。 它非常简洁且易于理解。

之后或同时,您可能想看一下“理解 Linux 内核”。但我不建议在早期阶段阅读它。

另外,请查看 Linux 内核编程指南。 由于可以从内核模块编程中学到很多东西,因此该指南将对您有所帮助。 是的,要了解更多信息,请查阅内核源代码 tarball 的“文档”子目录。

Try to get hold of Robert Love's book on Linux Kernel Programming. Its very concise and easy to follow.

After that or along with that, you may want to take a look at "Understanding the Linux kernel".But I wouldn't recommend it during the early stages.

Also, look at the Linux kernel programming guide. Since a lot can be learnt from programing kernel modules, that guide will help you. And yes, for a lot of information, consult the 'documentation' sub-directory of the Kernel sources tarball.

静水深流 2024-07-29 06:35:20

查看 Linux 内核 Janitor 项目

''我们检查 Linux 内核源代码,进行代码审查,修复未维护的代码并进行其他清理和 API 转换。 这是内核黑客攻击的良好开端。”

Check out The Linux Kernel Janitor Project

''We go through the Linux kernel source code, doing code reviews, fixing up unmaintained code and doing other cleanups and API conversions. It is a good start to kernel hacking.''

讽刺将军 2024-07-29 06:35:20

我不得不说:“学习C”。 :)

尝试这本免费的在线书籍。

Linux内核模块编程指南
http://www.linuxhq.com/guides/LKMPG/mpg.html

I would have to say:"learn C". :)

Try this free online book.

Linux Kernel Module Programming Guide
http://www.linuxhq.com/guides/LKMPG/mpg.html

怂人 2024-07-29 06:35:20

检查 kernelnewbies.org,订阅 Kernelnewbies 邮件列表,访问 irc.oftc.org #kernelnewbies

Check kernelnewbies.org, subscribe to the Kernelnewbies mailing list, got to irc.oftc.org #kernelnewbies

离不开的别离 2024-07-29 06:35:20

一些资源:

  • 书籍:Robert Love,Linux Kernel Development,第 3 版
  • 书籍:Corbet、Rubini、Linux Device Drivers,第 3 版(免费版本 此处)
  • 书籍:Linux 内核简介(免费版本 此处
  • Free Electrons 提供的免费材料

Some resources:

  • Book: Robert Love, Linux Kernel Development, 3rd edition
  • Book: Corbet, Rubini, Linux Device Drivers, 3rd edition (free version here)
  • Book: Linux kernel in a nutshell (free version here)
  • Free material provided by Free Electrons
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文