Linux Device Drivers-Chapter 2 : Building and Running Modules
Số trang: 75
Loại file: pdf
Dung lượng: 468.61 KB
Lượt xem: 20
Lượt tải: 0
Xem trước 8 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Chapter 2 : Building and Running ModulesIts high time now to begin programming. This chapter introduces all the essential concepts about modules and kernel programming. In these few pages, we build and run a complete module. Developing such expertise is an essential foundation for any kind of modularized driver. To avoid throwing in too many concepts at once, this chapter talks only about modules, without referring to any specific device class. All the kernel items (functions, variables, header files, and macros) that are introduced here are described in a reference section at the end of the chapter. For the impatient...
Nội dung trích xuất từ tài liệu:
Linux Device Drivers-Chapter 2 : Building and Running Modules Chapter 2 : Building and Running ModulesIts high time now to begin programming. This chapter introduces all theessential concepts about modules and kernel programming. In these fewpages, we build and run a complete module. Developing such expertise is anessential foundation for any kind of modularized driver. To avoid throwingin too many concepts at once, this chapter talks only about modules, withoutreferring to any specific device class.All the kernel items (functions, variables, header files, and macros) that areintroduced here are described in a reference section at the end of the chapter.For the impatient reader, the following code is a complete Hello, Worldmodule (which does nothing in particular). This code will compile and rununder Linux kernel versions 2.0 through 2.4.[4][4]This example, and all the others presented in this book, is available on theOReilly FTP site, as explained in Chapter 1, An Introduction to DeviceDrivers.#define MODULE#include int init_module(void) { printk(Hello,world ); return 0; }void cleanup_module(void) { printk(Goodbyecruel world ); }The printk function is defined in the Linux kernel and behaves similarly tothe standard C library function printf. The kernel needs its own printingfunction because it runs by itself, without the help of the C library. Themodule can call printk because, after insmod has loaded it, the module islinked to the kernel and can access the kernels public symbols (functionsand variables, as detailed in the next section). The string is the priorityof the message. Weve specified a high priority (low cardinal number) in thismodule because a message with the default priority might not show on theconsole, depending on the kernel version you are running, the version of theklogd daemon, and your configuration. You can ignore this issue for now;well explain it in the section printk in Chapter 4, DebuggingTechniques.You can test the module by calling insmodand rmmod, as shown in thescreen dump in the following paragraph. Note that only the superuser canload and unload a module.The source file shown earlier can be loaded and unloaded as shown only ifthe running kernel has module version support disabled; however, mostdistributions preinstall versioned kernels (versioning is discussed inVersion Control in Modules in Chapter 11, kmod and AdvancedModularization). Although older modutils allowed loading nonversionedmodules to versioned kernels, this is no longer possible. To solve theproblem with hello.c, the source in the misc-modules directory of the samplecode includes a few more lines to be able to run both under versioned andnonversioned kernels. However, we strongly suggest you compile and runyour own kernel (without version support) before you run the samplecode.[5][5]If you are new to building kernels, Alessandro has posted an article athttp://www.linux.it/kerneldocs/kconf that should help you get started.root# gcc -c hello.croot# insmod ./hello.oHello, worldroot# rmmod helloGoodbye cruel worldroot#According to the mechanism your system uses to deliver the message lines,your output may be different. In particular, the previous screen dump wastaken from a text console; if you are running insmod and rmmodfrom anxterm, you wont see anything on your TTY. Instead, it may go to one of thesystem log files, such as /var/log/messages (the name of the actual file variesbetween Linux distributions). The mechanism used to deliver kernelmessages is described in How Messages Get Logged in Chapter 4,Debugging Techniques.As you can see, writing a module is not as difficult as you might expect. Thehard part is understanding your device and how to maximize performance.Well go deeper into modularization throughout this chapter and leavedevice-specific issues to later chapters.Kernel Modules Versus ApplicationsBefore we go further, its worth underlining the various differences betweena kernel module and an application.Whereas an application performs a single task from beginning to end, amodule registers itself in order to serve future requests, and its mainfunction terminates immediately. In other words, the task of the functioninit_module (the modules entry point) is to prepare for later invocation ofthe modules functions; its as though the module were saying, Here I am,and this is what I can do. The second entry point of a module,cleanup_module, gets invoked just before the module is unloaded. It shouldtell the kernel, Im not there anymore; dont ask me to do anything else.The ability to unload a module is one of the features of modularization thatyoull most appreciate, because it helps cut down development time; you cantest successive versions of your new driver without going through thelengthy shutdown/reboot cycle each time.As a program ...
Nội dung trích xuất từ tài liệu:
Linux Device Drivers-Chapter 2 : Building and Running Modules Chapter 2 : Building and Running ModulesIts high time now to begin programming. This chapter introduces all theessential concepts about modules and kernel programming. In these fewpages, we build and run a complete module. Developing such expertise is anessential foundation for any kind of modularized driver. To avoid throwingin too many concepts at once, this chapter talks only about modules, withoutreferring to any specific device class.All the kernel items (functions, variables, header files, and macros) that areintroduced here are described in a reference section at the end of the chapter.For the impatient reader, the following code is a complete Hello, Worldmodule (which does nothing in particular). This code will compile and rununder Linux kernel versions 2.0 through 2.4.[4][4]This example, and all the others presented in this book, is available on theOReilly FTP site, as explained in Chapter 1, An Introduction to DeviceDrivers.#define MODULE#include int init_module(void) { printk(Hello,world ); return 0; }void cleanup_module(void) { printk(Goodbyecruel world ); }The printk function is defined in the Linux kernel and behaves similarly tothe standard C library function printf. The kernel needs its own printingfunction because it runs by itself, without the help of the C library. Themodule can call printk because, after insmod has loaded it, the module islinked to the kernel and can access the kernels public symbols (functionsand variables, as detailed in the next section). The string is the priorityof the message. Weve specified a high priority (low cardinal number) in thismodule because a message with the default priority might not show on theconsole, depending on the kernel version you are running, the version of theklogd daemon, and your configuration. You can ignore this issue for now;well explain it in the section printk in Chapter 4, DebuggingTechniques.You can test the module by calling insmodand rmmod, as shown in thescreen dump in the following paragraph. Note that only the superuser canload and unload a module.The source file shown earlier can be loaded and unloaded as shown only ifthe running kernel has module version support disabled; however, mostdistributions preinstall versioned kernels (versioning is discussed inVersion Control in Modules in Chapter 11, kmod and AdvancedModularization). Although older modutils allowed loading nonversionedmodules to versioned kernels, this is no longer possible. To solve theproblem with hello.c, the source in the misc-modules directory of the samplecode includes a few more lines to be able to run both under versioned andnonversioned kernels. However, we strongly suggest you compile and runyour own kernel (without version support) before you run the samplecode.[5][5]If you are new to building kernels, Alessandro has posted an article athttp://www.linux.it/kerneldocs/kconf that should help you get started.root# gcc -c hello.croot# insmod ./hello.oHello, worldroot# rmmod helloGoodbye cruel worldroot#According to the mechanism your system uses to deliver the message lines,your output may be different. In particular, the previous screen dump wastaken from a text console; if you are running insmod and rmmodfrom anxterm, you wont see anything on your TTY. Instead, it may go to one of thesystem log files, such as /var/log/messages (the name of the actual file variesbetween Linux distributions). The mechanism used to deliver kernelmessages is described in How Messages Get Logged in Chapter 4,Debugging Techniques.As you can see, writing a module is not as difficult as you might expect. Thehard part is understanding your device and how to maximize performance.Well go deeper into modularization throughout this chapter and leavedevice-specific issues to later chapters.Kernel Modules Versus ApplicationsBefore we go further, its worth underlining the various differences betweena kernel module and an application.Whereas an application performs a single task from beginning to end, amodule registers itself in order to serve future requests, and its mainfunction terminates immediately. In other words, the task of the functioninit_module (the modules entry point) is to prepare for later invocation ofthe modules functions; its as though the module were saying, Here I am,and this is what I can do. The second entry point of a module,cleanup_module, gets invoked just before the module is unloaded. It shouldtell the kernel, Im not there anymore; dont ask me to do anything else.The ability to unload a module is one of the features of modularization thatyoull most appreciate, because it helps cut down development time; you cantest successive versions of your new driver without going through thelengthy shutdown/reboot cycle each time.As a program ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin thủ thuật máy tính tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 433 1 0
-
24 trang 359 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 320 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 310 0 0 -
74 trang 303 0 0
-
96 trang 297 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 285 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0