Subversion Repositories HelenOS

Rev

Rev 2410 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2007 Pavel Jancik, Michal Kebrt
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup arm32mm
  30.  * @{
  31.  */
  32. /** @file
  33.  *  @brief Paging related declarations.
  34.  */
  35.  
  36. #ifndef KERN_arm32_PAGE_H_
  37. #define KERN_arm32_PAGE_H_
  38.  
  39. #include <arch/mm/frame.h>
  40. #include <mm/mm.h>
  41. #include <arch/exception.h>
  42.  
  43.  
  44. #define PAGE_WIDTH  FRAME_WIDTH
  45. #define PAGE_SIZE   FRAME_SIZE
  46.  
  47. #define PAGE_COLOR_BITS 0           /* dummy */
  48.  
  49. #ifndef __ASM__
  50. #   define KA2PA(x) (((uintptr_t) (x)) - 0x80000000)
  51. #   define PA2KA(x) (((uintptr_t) (x)) + 0x80000000)
  52. #else
  53. #   define KA2PA(x) ((x) - 0x80000000)
  54. #   define PA2KA(x) ((x) + 0x80000000)
  55. #endif
  56.  
  57. #ifdef KERNEL
  58.  
  59. #define PTL0_ENTRIES_ARCH   (2<<12)    // 4096
  60. #define PTL1_ENTRIES_ARCH   0
  61. #define PTL2_ENTRIES_ARCH   0
  62. /* coarse page tables used (256*4 = 1KB per page) */
  63. #define PTL3_ENTRIES_ARCH   (2<<8)     // 256
  64.  
  65. #define PTL0_SIZE_ARCH      FOUR_FRAMES
  66. #define PTL1_SIZE_ARCH      0
  67. #define PTL2_SIZE_ARCH      0
  68. #define PTL3_SIZE_ARCH      ONE_FRAME
  69.  
  70. #define PTL0_INDEX_ARCH(vaddr)  (((vaddr) >> 20) & 0xfff)
  71. #define PTL1_INDEX_ARCH(vaddr)  0
  72. #define PTL2_INDEX_ARCH(vaddr)  0
  73. #define PTL3_INDEX_ARCH(vaddr)  (((vaddr) >> 12) & 0x0ff)
  74.  
  75. #define GET_PTL1_ADDRESS_ARCH(ptl0, i)      ((pte_t *)( (((pte_level0_t*)(ptl0))[(i)]).coarse_table_addr << 10 ))
  76. #define GET_PTL2_ADDRESS_ARCH(ptl1, i)      (ptl1)
  77. #define GET_PTL3_ADDRESS_ARCH(ptl2, i)      (ptl2)
  78. #define GET_FRAME_ADDRESS_ARCH(ptl3, i)     ((uintptr_t)( (((pte_level1_t*)(ptl3))[(i)]).frame_base_addr << 12 ))
  79.  
  80. #define SET_PTL0_ADDRESS_ARCH(ptl0)         (set_ptl0_addr((pte_level0_t *)(ptl0)))
  81. #define SET_PTL1_ADDRESS_ARCH(ptl0, i, a)   (((pte_level0_t *)(ptl0))[(i)].coarse_table_addr = (a)>>10)
  82. #define SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
  83. #define SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
  84. #define SET_FRAME_ADDRESS_ARCH(ptl3, i, a)  (((pte_level1_t *)(ptl3))[(i)].frame_base_addr = (a)>>12)
  85.  
  86. #define GET_PTL1_FLAGS_ARCH(ptl0, i)        get_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i))
  87. #define GET_PTL2_FLAGS_ARCH(ptl1, i)        PAGE_PRESENT
  88. #define GET_PTL3_FLAGS_ARCH(ptl2, i)        PAGE_PRESENT
  89. #define GET_FRAME_FLAGS_ARCH(ptl3, i)       get_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i))
  90.  
  91. #define SET_PTL1_FLAGS_ARCH(ptl0, i, x)     set_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i), (x))
  92. #define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
  93. #define SET_PTL3_FLAGS_ARCH(ptl2, i, x)
  94. #define SET_FRAME_FLAGS_ARCH(ptl3, i, x)    set_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i), (x))
  95.  
  96. #define PTE_VALID_ARCH(pte)             (*((uint32_t *) (pte)) != 0)
  97. #define PTE_PRESENT_ARCH(pte)           ( ((pte_level0_t *)(pte))->descriptor_type != 0 )
  98.  
  99. /* pte should point into ptl3 */
  100. #define PTE_GET_FRAME_ARCH(pte)         ( ((pte_level1_t *)(pte))->frame_base_addr << FRAME_WIDTH)
  101. /* pte should point into ptl3 */
  102. #define PTE_WRITABLE_ARCH(pte)          ( ((pte_level1_t *)(pte))->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW )
  103. #define PTE_EXECUTABLE_ARCH(pte)        1
  104.  
  105.  
  106. #ifndef __ASM__
  107.  
  108. /** Level 0 page table entry. */
  109. typedef struct {
  110.  
  111.     /* 01b for coarse tables, see below for details */
  112.     unsigned descriptor_type     : 2;
  113.     unsigned impl_specific       : 3;
  114.     unsigned domain              : 4;
  115.     unsigned should_be_zero      : 1;
  116.  
  117.     /* Pointer to the coarse 2nd level page table (holding entries for small (4KB)
  118.      * or large (64KB) pages. ARM also supports fine 2nd level page tables that
  119.      * may hold even tiny pages (1KB) but they are bigger (4KB per table in comparison
  120.      * with 1KB per the coarse table)
  121.     */
  122.     unsigned coarse_table_addr   : 22;
  123. } __attribute__ ((packed)) pte_level0_t;
  124.  
  125.  
  126. /** Level 1 page table entry (small (4KB) pages used). */
  127. typedef struct {
  128.  
  129.     /* 0b10 for small pages */
  130.     unsigned descriptor_type     : 2;
  131.     unsigned bufferable          : 1;
  132.     unsigned cacheable           : 1;
  133.  
  134.     /* access permissions for each of 4 subparts of a page
  135.      * (for each 1KB when small pages used */
  136.     unsigned access_permission_0 : 2;
  137.     unsigned access_permission_1 : 2;
  138.     unsigned access_permission_2 : 2;
  139.     unsigned access_permission_3 : 2;
  140.     unsigned frame_base_addr     : 20;
  141. } __attribute__ ((packed)) pte_level1_t;
  142.  
  143.  
  144. /* Level 1 page tables access permissions */
  145.  
  146. /** User mode: no access, privileged mode: no access. */
  147. #define PTE_AP_USER_NO_KERNEL_NO 0
  148.  
  149. /** User mode: no access, privileged mode: read/write. */
  150. #define PTE_AP_USER_NO_KERNEL_RW 1
  151.  
  152. /** User mode: read only, privileged mode: read/write. */
  153. #define PTE_AP_USER_RO_KERNEL_RW 2
  154.  
  155. /** User mode: read/write, privileged mode: read/write. */
  156. #define PTE_AP_USER_RW_KERNEL_RW 3
  157.  
  158.  
  159. /* pte_level0_t and pte_level1_t descriptor_type flags */
  160.  
  161. /** pte_level0_t and pte_level1_t "not present" flag (used in descriptor_type). */
  162. #define PTE_DESCRIPTOR_NOT_PRESENT  0
  163.  
  164. /** pte_level0_t coarse page table flag (used in descriptor_type). */
  165. #define PTE_DESCRIPTOR_COARSE_TABLE 1
  166.  
  167. /** pte_level1_t small page table flag (used in descriptor type). */
  168. #define PTE_DESCRIPTOR_SMALL_PAGE   2
  169.  
  170.  
  171. /** Sets the address of level 0 page table.
  172.  *
  173.  * @param pt    Pointer to the page table to set.
  174.  */  
  175. static inline void set_ptl0_addr( pte_level0_t* pt)
  176. {
  177.     asm volatile (
  178.         "mcr p15, 0, %0, c2, c0, 0 \n"
  179.         :
  180.         : "r"(pt)
  181.     );
  182. }
  183.  
  184.  
  185. /** Returns level 0 page table entry flags.
  186.  *
  187.  *  @param pt     Level 0 page table.
  188.  *  @param i      Index of the entry to return.
  189.  */
  190. static inline int get_pt_level0_flags(pte_level0_t *pt, index_t i)
  191. {
  192.     pte_level0_t *p = &pt[i];
  193.  
  194.     return
  195.         ( (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT ) |
  196.         ( 1 << PAGE_USER_SHIFT )  |
  197.         ( 1 << PAGE_READ_SHIFT )  |
  198.         ( 1 << PAGE_WRITE_SHIFT ) |
  199.         ( 1 << PAGE_EXEC_SHIFT )  |
  200.         ( 1 << PAGE_CACHEABLE_SHIFT  )
  201.     ;
  202. }
  203.  
  204.  
  205. /** Returns level 1 page table entry flags.
  206.  *
  207.  *  @param pt     Level 1 page table.
  208.  *  @param i      Index of the entry to return.
  209.  */
  210. static inline int get_pt_level1_flags(pte_level1_t *pt, index_t i)
  211. {
  212.     pte_level1_t *p = &pt[i];
  213.  
  214.     return
  215.         ( (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT)   << PAGE_PRESENT_SHIFT) |
  216.         ( (p->access_permission_0 == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT )  |
  217.         ( (p->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT )  |
  218.         ( (p->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT ) |
  219.         ( (p->access_permission_0 != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT )  |
  220.         ( (p->access_permission_0 == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT )  |
  221.         ( (p->access_permission_0 == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT ) |
  222.         ( 1 << PAGE_EXEC_SHIFT ) |
  223.         ( p->bufferable << PAGE_CACHEABLE )
  224.     ;
  225. }
  226.  
  227.  
  228. /** Sets flags of level 0 page table entry.
  229.  *
  230.  *  @param pt     level 0 page table
  231.  *  @param i      index of the entry to be changed
  232.  *  @param flags  new flags
  233.  */
  234. static inline void set_pt_level0_flags(pte_level0_t *pt, index_t i, int flags)
  235. {
  236.     pte_level0_t *p = &pt[i];
  237.  
  238.     if (flags & PAGE_NOT_PRESENT) {
  239.         p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
  240.         // ensures that the entry will be recognized as valid when PTE_VALID_ARCH applied
  241.         p->should_be_zero  = 1;
  242.     } else {
  243.         p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
  244.         p->should_be_zero  = 0;
  245.     }
  246. }
  247.  
  248.  
  249. /** Sets flags of level 1 page table entry.
  250.  *
  251.  *  We use same access rights for the whole page. When page is not preset we
  252.  *  store 1 in acess_rigts_3 so that at least one bit is 1 (to mark correct
  253.  *  page entry, see #PAGE_VALID_ARCH).
  254.  *
  255.  *  @param pt     Level 1 page table.
  256.  *  @param i      Index of the entry to be changed.
  257.  *  @param flags  New flags.
  258.  */  
  259. static inline void set_pt_level1_flags(pte_level1_t *pt, index_t i, int flags)
  260. {
  261.     pte_level1_t *p = &pt[i];
  262.    
  263.     if (flags & PAGE_NOT_PRESENT) {
  264.         p->descriptor_type      = PTE_DESCRIPTOR_NOT_PRESENT;
  265.         p->access_permission_3  = 1;
  266.     } else {
  267.         p->descriptor_type      = PTE_DESCRIPTOR_SMALL_PAGE;
  268.         p->access_permission_3  = p->access_permission_0;
  269.     }
  270.  
  271.     p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
  272.  
  273.     /* default access permission */
  274.     p->access_permission_0 = p->access_permission_1 =
  275.         p->access_permission_2 = p->access_permission_3 = PTE_AP_USER_NO_KERNEL_RW;
  276.  
  277.     if (flags & PAGE_USER)  {
  278.         if (flags & PAGE_READ) {
  279.             p->access_permission_0 = p->access_permission_1 =
  280.                 p->access_permission_2 = p->access_permission_3 =
  281.                 PTE_AP_USER_RO_KERNEL_RW;
  282.         }
  283.         if (flags & PAGE_WRITE) {
  284.             p->access_permission_0 = p->access_permission_1 =
  285.                 p->access_permission_2 = p->access_permission_3 =
  286.                 PTE_AP_USER_RW_KERNEL_RW;
  287.         }
  288.     }
  289. }
  290.  
  291.  
  292. extern void page_arch_init(void);
  293.  
  294.  
  295. #endif /* __ASM__ */
  296.  
  297. #endif /* KERNEL */
  298.  
  299. #endif
  300.  
  301. /** @}
  302.  */
  303.  
  304.