青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 297,  comments - 15,  trackbacks - 0

linux kernel development-chapter 2 getting started with the kernel 

A Beast of a Different Nature

The kernel has several differences compared to normal user-space applications that, although not making it necessarily harder to program than user-space, certainly provide unique challenges to kernel development.

These differences make the kernel a beast of a different nature. Some of the usual rules are bent; other rules are entirely new. Although some of the differences are obvious (we all know the kernel can do anything it wants), others are not so obvious. The most important of these differences are

  • The kernel does not have access to the C library.

  • The kernel is coded in GNU C.

  • The kernel lacks memory protection like user-space.

  • The kernel cannot easily use floating point.

  • The kernel has a small fixed-size stack.

  • Because the kernel has asynchronous interrupts, is preemptive, and supports SMP, synchronization and concurrency are major concerns within the kernel.

  • Portability is important.

Let's briefly look at each of these issues because all kernel development must keep them in mind.

No libc

Unlike a user-space application, the kernel is not linked against the standard C library (or any other library, for that matter). There are multiple reasons for this, including some chicken-and-the-egg situations, but the primary reason is speed and size. The full C libraryor even a decent subset of itis too large and too inefficient for the kernel.

Do not fret: Many of the usual libc functions have been implemented inside the kernel. For example, the common string manipulation functions are in lib/string.c. Just include <linux/string.h> and have at them.

Header Files

When I talk about header files hereor elsewhere in this bookI am referring to the kernel header files that are part of the kernel source tree. Kernel source files cannot include outside headers, just as they cannot use outside libraries.


Of the missing functions, the most familiar is printf(). The kernel does not have access to printf(), but it does have access to printk(). The printk() function copies the formatted string into the kernel log buffer, which is normally read by the syslog program. Usage is similar to printf():

printk("Hello world! A string: %s and an integer: %d\n", a_string, an_integer);

One notable difference between printf() and printk() is that printk() allows you to specify a priority flag. This flag is used by syslogd(8) to decide where to display kernel messages. Here is an example of these priorities:

printk(KERN_ERR "this is an error!\n");

We will use printk() tHRoughout this book. Later chapters have more information on printk().

GNU C

Like any self-respecting Unix kernel, the Linux kernel is programmed in C. Perhaps surprisingly, the kernel is not programmed in strict ANSI C. Instead, where applicable, the kernel developers make use of various language extensions available in gcc (the GNU Compiler Collection, which contains the C compiler used to compile the kernel and most everything else written in C on a Linux system).

The kernel developers use both ISO C99[1] and GNU C extensions to the C language. These changes wed the Linux kernel to gcc, although recently other compilers, such as the Intel C compiler, have sufficiently supported enough gcc features that they too can compile the Linux kernel. The ISO C99 extensions that the kernel uses are nothing special and, because C99 is an official revision of the C language, are slowly cropping up in a lot of other code. The more interesting, and perhaps unfamiliar, deviations from standard ANSI C are those provided by GNU C. Let's look at some of the more interesting extensions that may show up in kernel code.

[1] ISO C99 is the latest major revision to the ISO C standard. C99 adds numerous enhancements to the previous major revision, ISO C90, including named structure initializers and a complex type. The latter of which you cannot use safely from within the kernel.

Inline Functions

GNU C supports inline functions. An inline function is, as its name suggests, inserted inline into each function call site. This eliminates the overhead of function invocation and return (register saving and restore), and allows for potentially more optimization because the compiler can optimize the caller and the called function together. As a downside (nothing in life is free), code size increases because the contents of the function are copied to all the callers, which increases memory consumption and instruction cache footprint. Kernel developers use inline functions for small time-critical functions. Making large functions inline, especially those that are used more than once or are not time critical, is frowned upon by the kernel developers.

An inline function is declared when the keywords static and inline are used as part of the function definition. For example:

static inline void dog(unsigned long tail_size)

The function declaration must precede any usage, or else the compiler cannot make the function inline. Common practice is to place inline functions in header files. Because they are marked static, an exported function is not created. If an inline function is used by only one file, it can instead be placed toward the top of just that file.

In the kernel, using inline functions is preferred over complicated macros for reasons of type safety.

Inline Assembly

The gcc C compiler enables the embedding of assembly instructions in otherwise normal C functions. This feature, of course, is used in only those parts of the kernel that are unique to a given system architecture.

The asm() compiler directive is used to inline assembly code.

The Linux kernel is programmed in a mixture of C and assembly, with assembly relegated to low-level architecture and fast path code. The vast majority of kernel code is programmed in straight C.

Branch Annotation

The gcc C compiler has a built-in directive that optimizes conditional branches as either very likely taken or very unlikely taken. The compiler uses the directive to appropriately optimize the branch. The kernel wraps the directive in very easy-to-use macros, likely() and unlikely().

For example, consider an if statement such as the following:

if (foo) {
/* ... */
}

To mark this branch as very unlikely taken (that is, likely not taken):

/* we predict foo is nearly always zero ... */
if (unlikely(foo)) {
/* ... */
}

Conversely, to mark a branch as very likely taken:

/* we predict foo is nearly always nonzero ... */
if (likely(foo)) {
/* ... */
}

You should only use these directives when the branch direction is overwhelmingly a known priori or when you want to optimize a specific case at the cost of the other case. This is an important point: These directives result in a performance boost when the branch is correctly predicted, but a performance loss when the branch is mispredicted. A very common usage for unlikely() and likely() is error conditions. As one might expect, unlikely() finds much more use in the kernel because if statements tend to indicate a special case.

No Memory Protection

When a user-space application attempts an illegal memory access, the kernel can trap the error, send SIGSEGV, and kill the process. If the kernel attempts an illegal memory access, however, the results are less controlled. (After all, who is going to look after the kernel?) Memory violations in the kernel result in an oops, which is a major kernel error. It should go without saying that you must not illegally access memory, such as dereferencing a NULL pointerbut within the kernel, the stakes are much higher!

Additionally, kernel memory is not pageable. Therefore, every byte of memory you consume is one less byte of available physical memory. Keep that in mind next time you have to add one more feature to the kernel!

No (Easy) Use of Floating Point

When a user-space process uses floating-point instructions, the kernel manages the transition from integer to floating point mode. What the kernel has to do when using floating-point instructions varies by architecture, but the kernel normally catches a trap and does something in response.

Unlike user-space, the kernel does not have the luxury of seamless support for floating point because it cannot trap itself. Using floating point inside the kernel requires manually saving and restoring the floating point registers, among possible other chores. The short answer is: Don't do it; no floating point in the kernel.

Small, Fixed-Size Stack

User-space can get away with statically allocating tons of variables on the stack, including huge structures and many-element arrays. This behavior is legal because user-space has a large stack that can grow in size dynamically (developers of older, less intelligent operating systemssay, DOSmight recall a time when even user-space had a fixed-sized stack).

The kernel stack is neither large nor dynamic; it is small and fixed in size. The exact size of the kernel's stack varies by architecture. On x86, the stack size is configurable at compile-time and can be either 4 or 8KB. Historically, the kernel stack is two pages, which generally implies that it is 8KB on 32-bit architectures and 16KB on 64-bit architecturesthis size is fixed and absolute. Each process receives its own stack.

The kernel stack is discussed in much greater detail in later chapters.

Synchronization and Concurrency

The kernel is susceptible to race conditions. Unlike a single-threaded user-space application, a number of properties of the kernel allow for concurrent access of shared resources and thus require synchronization to prevent races. Specifically,

  • Linux is a preemptive multi-tasking operating system. Processes are scheduled and rescheduled at the whim of the kernel's process scheduler. The kernel must synchronize between these tasks.

  • The Linux kernel supports multiprocessing. Therefore, without proper protection, kernel code executing on two or more processors can access the same resource.

  • Interrupts occur asynchronously with respect to the currently executing code. Therefore, without proper protection, an interrupt can occur in the midst of accessing a shared resource and the interrupt handler can then access the same resource.

  • The Linux kernel is preemptive. Therefore, without protection, kernel code can be preempted in favor of different code that then accesses the same resource.

Typical solutions to race conditions include spinlocks and semaphores.

Later chapters provide a thorough discussion of synchronization and concurrency.

Portability Is Important

Although user-space applications do not have to aim for portability, Linux is a portable operating system and should remain one. This means that architecture-independent C code must correctly compile and run on a wide range of systems, and that architecture-dependent code must be properly segregated in system-specific directories in the kernel source tree.

A handful of rulessuch as remain endian neutral, be 64-bit clean, do not assume the word or page size, and so ongo a long way. Portability is discussed in extreme depth in a later chapter.


posted on 2010-05-22 21:09 chatler 閱讀(611) 評論(0)  編輯 收藏 引用 所屬分類: linux kernel
<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用鏈接

留言簿(10)

隨筆分類(307)

隨筆檔案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美成人精品在线视频| 激情av一区| 亚洲乱码日产精品bd| 国产精品午夜国产小视频| 欧美精品一级| 欧美va日韩va| 欧美国产日韩精品免费观看| 美日韩免费视频| 你懂的亚洲视频| 欧美日韩精品在线视频| 欧美日韩一级片在线观看| 欧美日韩一区二区在线视频| 欧美日韩视频在线一区二区| 国产精品久久久久国产a级| 国产精品外国| 精品动漫3d一区二区三区| 亚洲国产精品成人| 亚洲精品五月天| 亚洲小视频在线观看| 久久激情婷婷| 嫩草伊人久久精品少妇av杨幂| 欧美黄色一区| 亚洲午夜在线观看| 久久久久久久高潮| 欧美性天天影院| 亚洲高清在线观看| 亚洲在线成人精品| 免费在线视频一区| 中国成人黄色视屏| 鲁大师影院一区二区三区| 国产精品盗摄久久久| 一色屋精品视频免费看| 亚洲婷婷综合久久一本伊一区| 久久精品国产69国产精品亚洲| 亚洲电影中文字幕| 亚洲理论电影网| 欧美一区2区三区4区公司二百| 欧美精品一区二区蜜臀亚洲| 国产永久精品大片wwwapp| 亚洲一区二区三区免费视频| 久久亚洲色图| 这里只有精品在线播放| 欧美成人激情在线| 国产一区二区久久| 亚洲欧美视频一区| 亚洲精品女人| 欧美在线观看www| 欧美性jizz18性欧美| 激情另类综合| 久久精品亚洲热| 亚洲视频一区二区| 免费在线成人av| 精品福利av| 久久gogo国模啪啪人体图| 99re6这里只有精品| 欧美成人福利视频| 最新国产成人在线观看| 久久最新视频| 久久精品30| 久久激情五月婷婷| 国产精品久久国产愉拍| 一区二区三区日韩精品| 亚洲国产精品va在线看黑人| 久久久久久久一区| 国内精品久久久久久久影视蜜臀 | 欧美大色视频| 性欧美暴力猛交69hd| 国产精品网红福利| 欧美一区二区三区久久精品茉莉花| 日韩午夜在线观看视频| 欧美日产一区二区三区在线观看| 亚洲免费av网站| 亚洲欧洲一区二区三区久久| 欧美成年人网站| 99精品欧美一区二区三区综合在线 | 久久日韩粉嫩一区二区三区| 欧美一区二区视频在线| 黄色免费成人| 欧美福利视频在线观看| 久热成人在线视频| 亚洲欧洲视频| 一区二区三区精密机械公司 | 午夜欧美理论片| 国产亚洲欧美另类一区二区三区| 亚洲欧美一区二区三区久久| 亚洲一级片在线观看| 国产美女精品视频| 麻豆国产精品va在线观看不卡| 男同欧美伦乱| 一本色道久久88综合亚洲精品ⅰ | 亚洲免费观看在线视频| 亚洲欧洲精品一区二区三区波多野1战4 | 欧美激情免费观看| 在线亚洲美日韩| 亚洲视频网站在线观看| 国产日韩欧美91| 欧美凹凸一区二区三区视频| 欧美精品在线一区| 欧美在线播放高清精品| 另类天堂视频在线观看| 亚洲一区二区三区中文字幕在线| 亚洲在线一区| 1000部精品久久久久久久久| 亚洲精品色婷婷福利天堂| 午夜精品久久久久影视| 在线观看视频一区二区| 日韩一级网站| 狠狠网亚洲精品| 一本色道久久综合狠狠躁篇的优点 | 国外精品视频| 欧美aa国产视频| 欧美视频中文一区二区三区在线观看| 欧美一区观看| 女主播福利一区| 久久精品一区二区三区不卡牛牛| 欧美成人第一页| 久久综合999| 欧美午夜性色大片在线观看| 久久免费精品视频| 国产精品久久久久一区二区| 亚洲国产欧美在线人成| 国产午夜亚洲精品羞羞网站| 亚洲国产精品va在线看黑人动漫| 国产一区二区三区四区hd| 在线一区二区三区四区| 亚洲精品一区二区三区在线观看| 午夜日韩在线| 亚洲国产成人久久| 久久精品久久综合| 一二三区精品福利视频| 欧美一区二区三区视频免费| 好看的日韩av电影| 欧美+日本+国产+在线a∨观看| 亚洲精品日日夜夜| 欧美尤物巨大精品爽| 伊人精品成人久久综合软件| 免费一级欧美片在线观看| 一区二区激情| 免费观看国产成人| 亚洲天堂久久| 黄色一区三区| 国产精品wwwwww| 久久久久九九视频| 一本久道久久久| 久久综合国产精品| 亚洲午夜在线观看视频在线| 亚洲电影av在线| 欧美性开放视频| 老鸭窝91久久精品色噜噜导演| 亚洲最新视频在线播放| 奶水喷射视频一区| 亚洲一区三区视频在线观看| 一区二区三区在线观看国产| 国产精品第一区| 久久综合久久久久88| 亚洲欧美www| 99国产精品久久久久老师| 欧美国产欧美亚洲国产日韩mv天天看完整| 亚洲一二三四久久| 亚洲日本va午夜在线影院| 国产色综合网| 国产精品视频久久久| 欧美日韩一区综合| 欧美国产精品一区| 久久综合中文| 久久久久久国产精品mv| 欧美专区亚洲专区| 午夜精品久久一牛影视| 亚洲欧美在线一区二区| 一本色道久久综合一区| 亚洲人成网站影音先锋播放| 黄色精品一区| 黄色精品网站| 尤物yw午夜国产精品视频明星| 国产美女精品视频| 国产嫩草一区二区三区在线观看| 欧美偷拍一区二区| 欧美午夜免费影院| 欧美剧在线免费观看网站| 免费精品视频| 欧美黄色一区| 欧美极品一区| 欧美日韩在线精品| 欧美亚州一区二区三区| 欧美色综合网| 国产精品久久久久久影院8一贰佰 国产精品久久久久久影视 | 亚洲欧美三级伦理| 亚洲免费人成在线视频观看| 99re6热只有精品免费观看| 亚洲美女视频| 一区二区三区四区五区精品| 一区二区三区**美女毛片| 9l国产精品久久久久麻豆| 一区二区不卡在线视频 午夜欧美不卡在 | 午夜精品久久久久久久99樱桃| 欧美肥婆在线| 欧美人交a欧美精品| 欧美午夜www高清视频| 国产精品自在线| 国内成+人亚洲|