README.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. Memory management for CRIS/MMU
  2. ------------------------------
  3. HISTORY:
  4. $Log: README.mm,v $
  5. Revision 1.1 2001/12/17 13:59:27 bjornw
  6. Initial revision
  7. Revision 1.1 2000/07/10 16:25:21 bjornw
  8. Initial revision
  9. Revision 1.4 2000/01/17 02:31:59 bjornw
  10. Added discussion of paging and VM.
  11. Revision 1.3 1999/12/03 16:43:23 hp
  12. Blurb about that the 3.5G-limitation is not a MMU limitation
  13. Revision 1.2 1999/12/03 16:04:21 hp
  14. Picky comment about not mapping the first page
  15. Revision 1.1 1999/12/03 15:41:30 bjornw
  16. First version of CRIS/MMU memory layout specification.
  17. ------------------------------
  18. See the ETRAX-NG HSDD for reference.
  19. We use the page-size of 8 kbytes, as opposed to the i386 page-size of 4 kbytes.
  20. The MMU can, apart from the normal mapping of pages, also do a top-level
  21. segmentation of the kernel memory space. We use this feature to avoid having
  22. to use page-tables to map the physical memory into the kernel's address
  23. space. We also use it to keep the user-mode virtual mapping in the same
  24. map during kernel-mode, so that the kernel easily can access the corresponding
  25. user-mode process' data.
  26. As a comparison, the Linux/i386 2.0 puts the kernel and physical RAM at
  27. address 0, overlapping with the user-mode virtual space, so that descriptor
  28. registers are needed for each memory access to specify which MMU space to
  29. map through. That changed in 2.2, putting the kernel/physical RAM at
  30. 0xc0000000, to co-exist with the user-mode mapping. We will do something
  31. quite similar, but with the additional complexity of having to map the
  32. internal chip I/O registers and the flash memory area (including SRAM
  33. and peripherial chip-selets).
  34. The kernel-mode segmentation map:
  35. ------------------------ ------------------------
  36. FFFFFFFF| | => cached | |
  37. | kernel seg_f | flash | |
  38. F0000000|______________________| | |
  39. EFFFFFFF| | => uncached | |
  40. | kernel seg_e | flash | |
  41. E0000000|______________________| | DRAM |
  42. DFFFFFFF| | paged to any | Un-cached |
  43. | kernel seg_d | =======> | |
  44. D0000000|______________________| | |
  45. CFFFFFFF| | | |
  46. | kernel seg_c |==\ | |
  47. C0000000|______________________| \ |______________________|
  48. BFFFFFFF| | uncached | |
  49. | kernel seg_b |=====\=========>| Registers |
  50. B0000000|______________________| \c |______________________|
  51. AFFFFFFF| | \a | |
  52. | | \c | FLASH/SRAM/Peripheral|
  53. | | \h |______________________|
  54. | | \e | |
  55. | | \d | |
  56. | kernel seg_0 - seg_a | \==>| DRAM |
  57. | | | Cached |
  58. | | paged to any | |
  59. | | =======> |______________________|
  60. | | | |
  61. | | | Illegal |
  62. | | |______________________|
  63. | | | |
  64. | | | FLASH/SRAM/Peripheral|
  65. 00000000|______________________| |______________________|
  66. In user-mode it looks the same except that only the space 0-AFFFFFFF is
  67. available. Therefore, in this model, the virtual address space per process
  68. is limited to 0xb0000000 bytes (minus 8192 bytes, since the first page,
  69. 0..8191, is never mapped, in order to trap NULL references).
  70. It also means that the total physical RAM that can be mapped is 256 MB
  71. (kseg_c above). More RAM can be mapped by choosing a different segmentation
  72. and shrinking the user-mode memory space.
  73. The MMU can map all 4 GB in user mode, but doing that would mean that a
  74. few extra instructions would be needed for each access to user mode
  75. memory.
  76. The kernel needs access to both cached and uncached flash. Uncached is
  77. necessary because of the special write/erase sequences. Also, the
  78. peripherial chip-selects are decoded from that region.
  79. The kernel also needs its own virtual memory space. That is kseg_d. It
  80. is used by the vmalloc() kernel function to allocate virtual contiguous
  81. chunks of memory not possible using the normal kmalloc physical RAM
  82. allocator.
  83. The setting of the actual MMU control registers to use this layout would
  84. be something like this:
  85. R_MMU_KSEG = ( ( seg_f, seg ) | // Flash cached
  86. ( seg_e, seg ) | // Flash uncached
  87. ( seg_d, page ) | // kernel vmalloc area
  88. ( seg_c, seg ) | // kernel linear segment
  89. ( seg_b, seg ) | // kernel linear segment
  90. ( seg_a, page ) |
  91. ( seg_9, page ) |
  92. ( seg_8, page ) |
  93. ( seg_7, page ) |
  94. ( seg_6, page ) |
  95. ( seg_5, page ) |
  96. ( seg_4, page ) |
  97. ( seg_3, page ) |
  98. ( seg_2, page ) |
  99. ( seg_1, page ) |
  100. ( seg_0, page ) );
  101. R_MMU_KBASE_HI = ( ( base_f, 0x0 ) | // flash/sram/periph cached
  102. ( base_e, 0x8 ) | // flash/sram/periph uncached
  103. ( base_d, 0x0 ) | // don't care
  104. ( base_c, 0x4 ) | // physical RAM cached area
  105. ( base_b, 0xb ) | // uncached on-chip registers
  106. ( base_a, 0x0 ) | // don't care
  107. ( base_9, 0x0 ) | // don't care
  108. ( base_8, 0x0 ) ); // don't care
  109. R_MMU_KBASE_LO = ( ( base_7, 0x0 ) | // don't care
  110. ( base_6, 0x0 ) | // don't care
  111. ( base_5, 0x0 ) | // don't care
  112. ( base_4, 0x0 ) | // don't care
  113. ( base_3, 0x0 ) | // don't care
  114. ( base_2, 0x0 ) | // don't care
  115. ( base_1, 0x0 ) | // don't care
  116. ( base_0, 0x0 ) ); // don't care
  117. NOTE: while setting up the MMU, we run in a non-mapped mode in the DRAM (0x40
  118. segment) and need to setup the seg_4 to a unity mapping, so that we don't get
  119. a fault before we have had time to jump into the real kernel segment (0xc0). This
  120. is done in head.S temporarily, but fixed by the kernel later in paging_init.
  121. Paging - PTE's, PMD's and PGD's
  122. -------------------------------
  123. [ References: asm/pgtable.h, asm/page.h, asm/mmu.h ]
  124. The paging mechanism uses virtual addresses to split a process memory-space into
  125. pages, a page being the smallest unit that can be freely remapped in memory. On
  126. Linux/CRIS, a page is 8192 bytes (for technical reasons not equal to 4096 as in
  127. most other 32-bit architectures). It would be inefficient to let a virtual memory
  128. mapping be controlled by a long table of page mappings, so it is broken down into
  129. a 2-level structure with a Page Directory containing pointers to Page Tables which
  130. each have maps of up to 2048 pages (8192 / sizeof(void *)). Linux can actually
  131. handle 3-level structures as well, with a Page Middle Directory in between, but
  132. in many cases, this is folded into a two-level structure by excluding the Middle
  133. Directory.
  134. We'll take a look at how an address is translated while we discuss how it's handled
  135. in the Linux kernel.
  136. The example address is 0xd004000c; in binary this is:
  137. 31 23 15 7 0
  138. 11010000 00000100 00000000 00001100
  139. |______| |__________||____________|
  140. PGD PTE page offset
  141. Given the top-level Page Directory, the offset in that directory is calculated
  142. using the upper 8 bits:
  143. static inline pgd_t * pgd_offset(struct mm_struct * mm, unsigned long address)
  144. {
  145. return mm->pgd + (address >> PGDIR_SHIFT);
  146. }
  147. PGDIR_SHIFT is the log2 of the amount of memory an entry in the PGD can map; in our
  148. case it is 24, corresponding to 16 MB. This means that each entry in the PGD
  149. corresponds to 16 MB of virtual memory.
  150. The pgd_t from our example will therefore be the 208'th (0xd0) entry in mm->pgd.
  151. Since the Middle Directory does not exist, it is a unity mapping:
  152. static inline pmd_t * pmd_offset(pgd_t * dir, unsigned long address)
  153. {
  154. return (pmd_t *) dir;
  155. }
  156. The Page Table provides the final lookup by using bits 13 to 23 as index:
  157. static inline pte_t * pte_offset(pmd_t * dir, unsigned long address)
  158. {
  159. return (pte_t *) pmd_page(*dir) + ((address >> PAGE_SHIFT) &
  160. (PTRS_PER_PTE - 1));
  161. }
  162. PAGE_SHIFT is the log2 of the size of a page; 13 in our case. PTRS_PER_PTE is
  163. the number of pointers that fit in a Page Table and is used to mask off the
  164. PGD-part of the address.
  165. The so-far unused bits 0 to 12 are used to index inside a page linearily.
  166. The VM system
  167. -------------
  168. The kernels own page-directory is the swapper_pg_dir, cleared in paging_init,
  169. and contains the kernels virtual mappings (the kernel itself is not paged - it
  170. is mapped linearily using kseg_c as described above). Architectures without
  171. kernel segments like the i386, need to setup swapper_pg_dir directly in head.S
  172. to map the kernel itself. swapper_pg_dir is pointed to by init_mm.pgd as the
  173. init-task's PGD.
  174. To see what support functions are used to setup a page-table, let's look at the
  175. kernel's internal paged memory system, vmalloc/vfree.
  176. void * vmalloc(unsigned long size)
  177. The vmalloc-system keeps a paged segment in kernel-space at 0xd0000000. What
  178. happens first is that a virtual address chunk is allocated to the request using
  179. get_vm_area(size). After that, physical RAM pages are allocated and put into
  180. the kernel's page-table using alloc_area_pages(addr, size).
  181. static int alloc_area_pages(unsigned long address, unsigned long size)
  182. First the PGD entry is found using init_mm.pgd. This is passed to
  183. alloc_area_pmd (remember the 3->2 folding). It uses pte_alloc_kernel to
  184. check if the PGD entry points anywhere - if not, a page table page is
  185. allocated and the PGD entry updated. Then the alloc_area_pte function is
  186. used just like alloc_area_pmd to check which page table entry is desired,
  187. and a physical page is allocated and the table entry updated. All of this
  188. is repeated at the top-level until the entire address range specified has
  189. been mapped.