Linux Kernel Development Notes - 2

2 Getting Started with the Kernel

  2.1

  2.2 The Kernel Source Tree

                   Directory                                      Description

            arch                            Architecture-specific source

            crypto                          Crypto API

            Documentation                   Kernel source documentation

            drivers                         Device drivers

            fs                              The VFS and the individual file systems

            include                         Kernel headers

            init                            Kernel boot and initialization

            ipc                             Interprocess communication code

            kernel                          Core subsystems, such as the scheduler

            lib                             Helper routines(通用内核函数)

            mm                              Memory management subsystem and the VM

            net                             Networking subsystem

            scripts                         Scripts used to build the kernel

            security                        Linux Security Module

            sound                           Sound subsystem

            usr                             Early user-space code (called initramfs)

  2.3 编译内核

  2.4 内核开发的特点

    2.4.1 No libc

        内核空间不能链接使用标准C函数库。最主要原因在于速度和大小。

        如,内核代码无法调用printf(),但可以调用printk()函数。printk()函数负责把格式化好的字符串拷贝到内核日志缓冲区(syslog程序读取缓冲区获取内核信息)。与printf()用法相似。一个显著区别在于printk()允许通过指定一个标志来设置优先级。Syslog会依此标志位决定何处现实此系统消息。

        eg: printk(KERN_ERR  “this is an error!\n”);

    2.4.2 GNU C

        1) inline 函数

          定义一个inline函数需要使用static关键字,并且用inline限定:

          eg: static inline void dog(unsigned long tail_size);

          由于使用了static关键字,所以编译时不会为inline函数单独建立一个函数体。内核中,为了类型安全,优先使用inline函数而不是复杂的宏。

        2) inline 汇编

          Linux的内核混合使用了C和汇编。在偏近体系结构的底层或对执行时间要求严格的地方,一般使用汇编。

        3) 分支声明

          见likely() and unlikely()

    2.4.3 No Memory Protection

        如果用户程序非法访问内存,内核会发现这个错误,发送SIGSEGV,并且结束这个进程。内核中发生的内存错误会导致oops.

        内核中的内存不分页(用掉一个字节,物理内存就减少一个字节)。

    2.4.4 No (Easy) Use of Floating Point

        不要在内核中使用浮点数。

    2.4.5 Small, Fixed-Size Stack

        容积小而固定的栈

    2.4.6 Synchronization and Concurrency

        内核很容易产生竞争条件(Linux抢占多任务进程调度,SMP可能同时访问同一资源,中断是异步到来不顾及当前执行的代码,内核中一段正在执行的代码可能被另一段代码抢占)。内核的许多特性都要求能够并发的访问共享数据,这就要求有同步机制保证不出现竞争条件。

    2.4.7 Portability Is Important

  2.5 

    The kernel is indeed a unique and inimitable beast: No memory protection, no tried-and-true libc, a small stack, a huge source tree. The Linux kernel plays by its own rules, running with the big boys and stopping just long enough to break the customs with which we are familiar. Despite this, however, the kernel is just a program. It is not very different from the usual, the accustomed, the status quo. Do not be afraid: Stand up to it, call it names, push it around.

    Realizing that the kernel is not as daunting as first appearances might suggest is the first step on the road to having everything just make sense. To reach that utopia, however, you have to jump in, read the source, hack the source, and not be disheartened.

posted @ 2010-10-04 15:15  yangzd  阅读(251)  评论(0编辑  收藏  举报