Linux Device Drivers-Chapter 9 : Interrupt Handling Although
Số trang: 86
Loại file: pdf
Dung lượng: 481.88 KB
Lượt xem: 9
Lượt tải: 0
Xem trước 9 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu linux device drivers-chapter 9 :interrupt handling although, công nghệ thông tin, hệ điều hành phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Linux Device Drivers-Chapter 9 :Interrupt Handling Although Chapter 9 :Interrupt HandlingAlthough some devices can be controlled using nothing but their I/Oregions, most real-world devices are a bit more complicated than that.Devices have to deal with the external world, which often includes thingssuch as spinning disks, moving tape, wires to distant places, and so on.Much has to be done in a time frame that is different, and slower, than thatof the processor. Since it is almost always undesirable to have the processorwait on external events, there must be a way for a device to let the processorknow when something has happened.That way, of course, is interrupts. An interruptis simply a signal that thehardware can send when it wants the processors attention. Linux handlesinterrupts in much the same way that it handles signals in user space. For themost part, a driver need only register a handler for its devices interrupts, andhandle them properly when they arrive. Of course, underneath that simplepicture there is some complexity; in particular, interrupt handlers aresomewhat limited in the actions they can perform as a result of how they arerun.It is difficult to demonstrate the use of interrupts without a real hardwaredevice to generate them. Thus, the sample code used in this chapter workswith the parallel port. Well be working with the short module from theprevious chapter; with some small additions it can generate and handleinterrupts from the parallel port. The modules name, short, actually meansshort int (it is C, isnt it?), to remind us that it handles interrupts.Overall Control of InterruptsThe way that Linux handles interrupts has changed quite a bit over the years,due to changes in design and in the hardware it works with. The PCs viewof interrupts in the early days was quite simple; there were just 16 interruptlines and one processor to deal with them. Modern hardware can have manymore interrupts, and can also be equipped with fancy advancedprogrammable interrupt controllers (APICs), which can distribute interruptsacross multiple processors in an intelligent (and programmable) way.Happily, Linux has been able to deal with all of these changes withrelatively few incompatibilities at the driver level. Thus, the interfacedescribed in this chapter works, with few differences, across many kernelversions. Sometimes things do work out nicely.Unix-like systems have used the functions cli and sti to disable and enableinterrupts for many years. In modern Linux systems, however, using themdirectly is discouraged. It is increasingly impossible for any routine to knowwhether interrupts are enabled when it is called; thus, simply enablinginterrupts with sti before return is a bad practice. Your function may bereturning to a function that expects interrupts to be still disabled.Thus, if you must disable interrupts, it is better to use the following calls:unsigned long flags; save_flags(flags); cli(); /* This code runs with interrupts disabled */ restore_flags(flags);Note that save_flags is a macro, and that it is passed the variable to hold theflags directly -- without an & operator. There is also an important constrainton the use of these macros: save_flagsand restore_flags must be called fromthe same function. In other words, you cannot pass the flags to anotherfunction, unless the other function is inlined. Code that ignores thisrestriction will work on some architectures but will fail on others.Increasingly, however, even code like the previous example is discouragedwherever possible. In a multiprocessor system, critical code cannot beprotected just by disabling interrupts; some sort of locking mechanism mustbe used. Functions such as spin_lock_irqsave (covered in Using Spinlocks,later in this chapter) provide locking and interrupt control together; thesefunctions are the only really safe way to control concurrency in the presenceof interrupts.cli, meanwhile, disables interrupts on all processors on the system, and canthus affect the performance of the system as a whole.[36][36]The truth is just a little more complicated than this. If you are alreadyhandling an interrupt, cli only disables interrupts on the current CPU.Thus, explicit calls to cli and related functions are slowly disappearing frommuch of the kernel. There are occasions where you need them in a devicedriver, but they are rare. Before calling cli, think about whether you reallyneed to disable all interrupts on the system.Preparing the Parallel PortAlthough the parallel interface is simple, it can trigger interrupts. Thiscapability is used by the printer to notify the lp driver that it is ready toaccept the next character in the buffer.Like most devices, the parallel port doesnt actually generate interruptsbefore its instructed to do so; the parallel standard states ...
Nội dung trích xuất từ tài liệu:
Linux Device Drivers-Chapter 9 :Interrupt Handling Although Chapter 9 :Interrupt HandlingAlthough some devices can be controlled using nothing but their I/Oregions, most real-world devices are a bit more complicated than that.Devices have to deal with the external world, which often includes thingssuch as spinning disks, moving tape, wires to distant places, and so on.Much has to be done in a time frame that is different, and slower, than thatof the processor. Since it is almost always undesirable to have the processorwait on external events, there must be a way for a device to let the processorknow when something has happened.That way, of course, is interrupts. An interruptis simply a signal that thehardware can send when it wants the processors attention. Linux handlesinterrupts in much the same way that it handles signals in user space. For themost part, a driver need only register a handler for its devices interrupts, andhandle them properly when they arrive. Of course, underneath that simplepicture there is some complexity; in particular, interrupt handlers aresomewhat limited in the actions they can perform as a result of how they arerun.It is difficult to demonstrate the use of interrupts without a real hardwaredevice to generate them. Thus, the sample code used in this chapter workswith the parallel port. Well be working with the short module from theprevious chapter; with some small additions it can generate and handleinterrupts from the parallel port. The modules name, short, actually meansshort int (it is C, isnt it?), to remind us that it handles interrupts.Overall Control of InterruptsThe way that Linux handles interrupts has changed quite a bit over the years,due to changes in design and in the hardware it works with. The PCs viewof interrupts in the early days was quite simple; there were just 16 interruptlines and one processor to deal with them. Modern hardware can have manymore interrupts, and can also be equipped with fancy advancedprogrammable interrupt controllers (APICs), which can distribute interruptsacross multiple processors in an intelligent (and programmable) way.Happily, Linux has been able to deal with all of these changes withrelatively few incompatibilities at the driver level. Thus, the interfacedescribed in this chapter works, with few differences, across many kernelversions. Sometimes things do work out nicely.Unix-like systems have used the functions cli and sti to disable and enableinterrupts for many years. In modern Linux systems, however, using themdirectly is discouraged. It is increasingly impossible for any routine to knowwhether interrupts are enabled when it is called; thus, simply enablinginterrupts with sti before return is a bad practice. Your function may bereturning to a function that expects interrupts to be still disabled.Thus, if you must disable interrupts, it is better to use the following calls:unsigned long flags; save_flags(flags); cli(); /* This code runs with interrupts disabled */ restore_flags(flags);Note that save_flags is a macro, and that it is passed the variable to hold theflags directly -- without an & operator. There is also an important constrainton the use of these macros: save_flagsand restore_flags must be called fromthe same function. In other words, you cannot pass the flags to anotherfunction, unless the other function is inlined. Code that ignores thisrestriction will work on some architectures but will fail on others.Increasingly, however, even code like the previous example is discouragedwherever possible. In a multiprocessor system, critical code cannot beprotected just by disabling interrupts; some sort of locking mechanism mustbe used. Functions such as spin_lock_irqsave (covered in Using Spinlocks,later in this chapter) provide locking and interrupt control together; thesefunctions are the only really safe way to control concurrency in the presenceof interrupts.cli, meanwhile, disables interrupts on all processors on the system, and canthus affect the performance of the system as a whole.[36][36]The truth is just a little more complicated than this. If you are alreadyhandling an interrupt, cli only disables interrupts on the current CPU.Thus, explicit calls to cli and related functions are slowly disappearing frommuch of the kernel. There are occasions where you need them in a devicedriver, but they are rare. Before calling cli, think about whether you reallyneed to disable all interrupts on the system.Preparing the Parallel PortAlthough the parallel interface is simple, it can trigger interrupts. Thiscapability is used by the printer to notify the lp driver that it is ready toaccept the next character in the buffer.Like most devices, the parallel port doesnt actually generate interruptsbefore its instructed to do so; the parallel standard states ...
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 437 1 0
-
24 trang 365 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 325 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 318 0 0 -
74 trang 307 0 0
-
96 trang 303 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 295 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 289 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 289 1 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0