Главная » Просмотр файлов » Linux Device Drivers 2nd Edition

Linux Device Drivers 2nd Edition (779877), страница 87

Файл №779877 Linux Device Drivers 2nd Edition (Linux Device Drivers 2nd Edition) 87 страницаLinux Device Drivers 2nd Edition (779877) страница 872018-01-10СтудИзба
Просмтор этого файла доступен только зарегистрированным пользователям. Но у нас супер быстрая регистрация: достаточно только электронной почты!

Текст из файла (страница 87)

The first covers theimplementation of the mmap system call, which allows the mapping of devicememory directly into a user process’s address space. We then cover the kernelkiobuf mechanism, which provides direct access to user memory from kernelspace. The kiobuf system may be used to implement ‘‘raw I/O’’ for certain kindsof devices. The final section covers direct memory access (DMA) I/O operations,which essentially provide peripherals with direct access to system memory.Of course, all of these techniques require an understanding of how Linux memorymanagement works, so we start with an overview of that subsystem.Memory Management in LinuxRather than describing the theory of memory management in operating systems,this section tries to pinpoint the main features of the Linux implementation of thetheory. Although you do not need to be a Linux virtual memory guru to implement mmap, a basic overview of how things work is useful.

What follows is afairly lengthy description of the data structures used by the kernel to managememory. Once the necessary background has been covered, we can get intoworking with these structures.37022 June 2001 16:42http://openlib.org.uaMemory Management in LinuxAddress TypesLinux is, of course, a virtual memory system, meaning that the addresses seen byuser programs do not directly correspond to the physical addresses used by thehardware. Virtual memory introduces a layer of indirection, which allows a number of nice things.

With virtual memory, programs running on the system can allocate far more memory than is physically available; indeed, even a single processcan have a virtual address space larger than the system’s physical memory. Virtualmemory also allows playing a number of tricks with the process’s address space,including mapping in device memory.Thus far, we have talked about virtual and physical addresses, but a number of thedetails have been glossed over. The Linux system deals with several types ofaddresses, each with its own semantics. Unfortunately, the kernel code is notalways very clear on exactly which type of address is being used in each situation,so the programmer must be careful.kernel virtualaddresseshigh memoryuser processlow memoryuser processkernel logicaladdressesKeyphysical memoryaddress spacepage mappingFigur e 13-1. Address types used in LinuxThe following is a list of address types used in Linux. Figure 13-1 shows howthese address types relate to physical memory.User virtual addressesThese are the regular addresses seen by user-space programs.

User addressesare either 32 or 64 bits in length, depending on the underlying hardwarearchitecture, and each process has its own virtual address space.37122 June 2001 16:42http://openlib.org.uaChapter 13: mmap and DMAPhysical addressesThe addresses used between the processor and the system’s memory. Physicaladdresses are 32- or 64-bit quantities; even 32-bit systems can use 64-bit physical addresses in some situations.Bus addressesThe addresses used between peripheral buses and memory.

Often they are thesame as the physical addresses used by the processor, but that is not necessarily the case. Bus addresses are highly architecture dependent, of course.Ker nel logical addressesThese make up the normal address space of the kernel. These addresses mapmost or all of main memory, and are often treated as if they were physicaladdresses. On most architectures, logical addresses and their associated physical addresses differ only by a constant offset.

Logical addresses use the hardware’s native pointer size, and thus may be unable to address all of physicalmemory on heavily equipped 32-bit systems. Logical addresses are usuallystored in variables of type unsigned long or void *. Memory returnedfrom kmalloc has a logical address.Ker nel virtual addressesThese differ from logical addresses in that they do not necessarily have adirect mapping to physical addresses. All logical addresses are kernel virtualaddresses; memory allocated by vmalloc also has a virtual address (but nodirect physical mapping). The function kmap, described later in this chapter,also returns virtual addresses.

Virtual addresses are usually stored in pointervariables.If you have a logical address, the macro _ _pa( ) (defined in <asm/page.h>) willreturn its associated physical address. Physical addresses can be mapped back tological addresses with _ _va( ), but only for low-memory pages.Different kernel functions require different types of addresses. It would be nice ifthere were different C types defined so that the required address type wereexplicit, but we have no such luck. In this chapter, we will be clear on whichtypes of addresses are used where.High and Low MemoryThe difference between logical and kernel virtual addresses is highlighted on32-bit systems that are equipped with large amounts of memory.

With 32 bits, it ispossible to address 4 GB of memory. Linux on 32-bit systems has, until recently,been limited to substantially less memory than that, however, because of the wayit sets up the virtual address space. The system was unable to handle more memory than it could set up logical addresses for, since it needed directly mapped kernel addresses for all memory.37222 June 2001 16:42http://openlib.org.uaMemory Management in LinuxRecent developments have eliminated the limitations on memory, and 32-bit systems can now work with well over 4 GB of system memory (assuming, of course,that the processor itself can address that much memory).

The limitation on howmuch memory can be directly mapped with logical addresses remains, however.Only the lowest portion of memory (up to 1 or 2 GB, depending on the hardwareand the kernel configuration) has logical addresses; the rest (high memory) doesnot. High memory can require 64-bit physical addresses, and the kernel must setup explicit virtual address mappings to manipulate it. Thus, many kernel functionsare limited to low memory only; high memory tends to be reserved for user-spaceprocess pages.The term “high memory” can be confusing to some, especially since it has othermeanings in the PC world.

So, to make things clear, we’ll define the terms here:Low memoryMemory for which logical addresses exist in kernel space. On almost everysystem you will likely encounter, all memory is low memory.High memoryMemory for which logical addresses do not exist, because the system containsmore physical memory than can be addressed with 32 bits.On i386 systems, the boundary between low and high memory is usually set at justunder 1 GB. This boundary is not related in any way to the old 640 KB limit foundon the original PC. It is, instead, a limit set by the kernel itself as it splits the 32-bitaddress space between kernel and user space.We will point out high-memory limitations as we come to them in this chapter.The Memory Map and struct pageHistorically, the kernel has used logical addresses to refer to explicit pages ofmemory. The addition of high-memory support, however, has exposed an obviousproblem with that approach — logical addresses are not available for high memory.Thus kernel functions that deal with memory are increasingly using pointers tostruct page instead.

This data structure is used to keep track of just abouteverything the kernel needs to know about physical memory; there is onestruct page for each physical page on the system. Some of the fields of thisstructure include the following:atomic_t count;The number of references there are to this page. When the count drops tozero, the page is returned to the free list.37322 June 2001 16:42http://openlib.org.uaChapter 13: mmap and DMAwait_queue_head_t wait;A list of processes waiting on this page. Processes can wait on a page when akernel function has locked it for some reason; drivers need not normallyworry about waiting on pages, though.void *virtual;The kernel virtual address of the page, if it is mapped; NULL, otherwise.

Lowmemory pages are always mapped; high-memory pages usually are not.unsigned long flags;A set of bit flags describing the status of the page. These include PG_locked,which indicates that the page has been locked in memory, andPG_reserved, which prevents the memory management system from working with the page at all.There is much more information within struct page, but it is part of the deeperblack magic of memory management and is not of concern to driver writers.The kernel maintains one or more arrays of struct page entries, which track allof the physical memory on the system.

On most systems, there is a single array,called mem_map. On some systems, however, the situation is more complicated.Nonuniform memory access (NUMA) systems and those with widely discontiguousphysical memory may have more than one memory map array, so code that ismeant to be portable should avoid direct access to the array whenever possible.Fortunately, it is usually quite easy to just work with struct page pointers without worrying about where they come from.Some functions and macros are defined for translating between struct pagepointers and virtual addresses:struct page *virt_to_page(void *kaddr);This macro, defined in <asm/page.h>, takes a kernel logical address andreturns its associated struct page pointer.

Характеристики

Тип файла
PDF-файл
Размер
7,36 Mb
Тип материала
Высшее учебное заведение

Список файлов книги

Свежие статьи
Популярно сейчас
Как Вы думаете, сколько людей до Вас делали точно такое же задание? 99% студентов выполняют точно такие же задания, как и их предшественники год назад. Найдите нужный учебный материал на СтудИзбе!
Ответы на популярные вопросы
Да! Наши авторы собирают и выкладывают те работы, которые сдаются в Вашем учебном заведении ежегодно и уже проверены преподавателями.
Да! У нас любой человек может выложить любую учебную работу и зарабатывать на её продажах! Но каждый учебный материал публикуется только после тщательной проверки администрацией.
Вернём деньги! А если быть более точными, то автору даётся немного времени на исправление, а если не исправит или выйдет время, то вернём деньги в полном объёме!
Да! На равне с готовыми студенческими работами у нас продаются услуги. Цены на услуги видны сразу, то есть Вам нужно только указать параметры и сразу можно оплачивать.
Отзывы студентов
Ставлю 10/10
Все нравится, очень удобный сайт, помогает в учебе. Кроме этого, можно заработать самому, выставляя готовые учебные материалы на продажу здесь. Рейтинги и отзывы на преподавателей очень помогают сориентироваться в начале нового семестра. Спасибо за такую функцию. Ставлю максимальную оценку.
Лучшая платформа для успешной сдачи сессии
Познакомился со СтудИзбой благодаря своему другу, очень нравится интерфейс, количество доступных файлов, цена, в общем, все прекрасно. Даже сам продаю какие-то свои работы.
Студизба ван лав ❤
Очень офигенный сайт для студентов. Много полезных учебных материалов. Пользуюсь студизбой с октября 2021 года. Серьёзных нареканий нет. Хотелось бы, что бы ввели подписочную модель и сделали материалы дешевле 300 рублей в рамках подписки бесплатными.
Отличный сайт
Лично меня всё устраивает - и покупка, и продажа; и цены, и возможность предпросмотра куска файла, и обилие бесплатных файлов (в подборках по авторам, читай, ВУЗам и факультетам). Есть определённые баги, но всё решаемо, да и администраторы реагируют в течение суток.
Маленький отзыв о большом помощнике!
Студизба спасает в те моменты, когда сроки горят, а работ накопилось достаточно. Довольно удобный сайт с простой навигацией и огромным количеством материалов.
Студ. Изба как крупнейший сборник работ для студентов
Тут дофига бывает всего полезного. Печально, что бывают предметы по которым даже одного бесплатного решения нет, но это скорее вопрос к студентам. В остальном всё здорово.
Спасательный островок
Если уже не успеваешь разобраться или застрял на каком-то задание поможет тебе быстро и недорого решить твою проблему.
Всё и так отлично
Всё очень удобно. Особенно круто, что есть система бонусов и можно выводить остатки денег. Очень много качественных бесплатных файлов.
Отзыв о системе "Студизба"
Отличная платформа для распространения работ, востребованных студентами. Хорошо налаженная и качественная работа сайта, огромная база заданий и аудитория.
Отличный помощник
Отличный сайт с кучей полезных файлов, позволяющий найти много методичек / учебников / отзывов о вузах и преподователях.
Отлично помогает студентам в любой момент для решения трудных и незамедлительных задач
Хотелось бы больше конкретной информации о преподавателях. А так в принципе хороший сайт, всегда им пользуюсь и ни разу не было желания прекратить. Хороший сайт для помощи студентам, удобный и приятный интерфейс. Из недостатков можно выделить только отсутствия небольшого количества файлов.
Спасибо за шикарный сайт
Великолепный сайт на котором студент за не большие деньги может найти помощь с дз, проектами курсовыми, лабораторными, а также узнать отзывы на преподавателей и бесплатно скачать пособия.
Популярные преподаватели
Добавляйте материалы
и зарабатывайте!
Продажи идут автоматически
6353
Авторов
на СтудИзбе
311
Средний доход
с одного платного файла
Обучение Подробнее