Subversion Repositories HelenOS

Rev

Rev 2259 | 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.  */
  34.  
  35. #ifndef KERN_arm32_PAGE_H_
  36. #define KERN_arm32_PAGE_H_
  37.  
  38. #include <arch/mm/frame.h>
  39. #include <mm/mm.h>
  40. #include <arch/exception.h>
  41.  
  42.  
  43. #define PAGE_WIDTH  FRAME_WIDTH
  44. #define PAGE_SIZE   FRAME_SIZE
  45.  
  46. #define PAGE_COLOR_BITS 0           /* dummy */
  47.  
  48. #ifndef __ASM__
  49. #   define KA2PA(x) (((uintptr_t) (x)) - 0x80000000)
  50. #   define PA2KA(x) (((uintptr_t) (x)) + 0x80000000)
  51. #else
  52. #   define KA2PA(x) ((x) - 0x80000000)
  53. #   define PA2KA(x) ((x) + 0x80000000)
  54. #endif
  55.  
  56. #ifdef KERNEL
  57.  
  58. #define PTL0_ENTRIES_ARCH    (2<<12)    // 4096
  59. #define PTL1_ENTRIES_ARCH     0
  60. #define PTL2_ENTRIES_ARCH     0
  61. /* coarse page tables used (256*4 = 1KB per page) */
  62. #define PTL3_ENTRIES_ARCH    (2<<8)     // 256
  63.  
  64. #define PTL0_SIZE_ARCH       FOUR_FRAMES
  65. #define PTL1_SIZE_ARCH       0
  66. #define PTL2_SIZE_ARCH       0
  67. #define PTL3_SIZE_ARCH       ONE_FRAME
  68.  
  69. #define PTL0_INDEX_ARCH(vaddr)    (((vaddr) >> 20) & 0xfff)
  70. #define PTL1_INDEX_ARCH(vaddr)    0
  71. #define PTL2_INDEX_ARCH(vaddr)    0
  72. #define PTL3_INDEX_ARCH(vaddr)    (((vaddr) >> 12) & 0x0ff)
  73.  
  74. #define GET_PTL1_ADDRESS_ARCH(ptl0, i)      ((pte_t *)( (((pte_level0_t*)(ptl0))[(i)]).coarse_table_addr << 10 ))
  75. #define GET_PTL2_ADDRESS_ARCH(ptl1, i)      (ptl1)
  76. #define GET_PTL3_ADDRESS_ARCH(ptl2, i)      (ptl2)
  77. #define GET_FRAME_ADDRESS_ARCH(ptl3, i)     ((uintptr_t)( (((pte_level1_t*)(ptl3))[(i)]).frame_base_addr << 12 ))
  78.  
  79. #define SET_PTL0_ADDRESS_ARCH(ptl0)         (set_ptl0_addr((pte_level0_t *)(ptl0)))
  80. #define SET_PTL1_ADDRESS_ARCH(ptl0, i, a)   (((pte_level0_t *)(ptl0))[(i)].coarse_table_addr = (a)>>10)
  81. #define SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
  82. #define SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
  83. #define SET_FRAME_ADDRESS_ARCH(ptl3, i, a)  (((pte_level1_t *)(ptl3))[(i)].frame_base_addr = (a)>>12)
  84.  
  85. #define GET_PTL1_FLAGS_ARCH(ptl0, i)        get_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i))
  86. #define GET_PTL2_FLAGS_ARCH(ptl1, i)        PAGE_PRESENT
  87. #define GET_PTL3_FLAGS_ARCH(ptl2, i)        PAGE_PRESENT
  88. #define GET_FRAME_FLAGS_ARCH(ptl3, i)       get_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i))
  89.  
  90. #define SET_PTL1_FLAGS_ARCH(ptl0, i, x)     set_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i), (x))
  91. #define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
  92. #define SET_PTL3_FLAGS_ARCH(ptl2, i, x)
  93. #define SET_FRAME_FLAGS_ARCH(ptl3, i, x)    set_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i), (x))
  94.  
  95. #define PTE_VALID_ARCH(pte)                 (*((uint32_t *) (pte)) != 0)
  96. #define PTE_PRESENT_ARCH(pte)               ( ((pte_level0_t *)(pte))->descriptor_type != 0 )
  97.  
  98. /* pte should point into ptl3 */
  99. #define PTE_GET_FRAME_ARCH(pte)             ( ((pte_level1_t *)(pte))->frame_base_addr << FRAME_WIDTH)
  100. /* pte should point into ptl3 */
  101. #define PTE_WRITABLE_ARCH(pte)              ( ((pte_level1_t *)(pte))->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW )
  102.  
  103. #define PTE_EXECUTABLE_ARCH(pte)            1
  104.  
  105. #ifndef __ASM__
  106.  
  107. /**
  108.  * Sets the address of level 0 page table.
  109.  *
  110.  * \param pt    pointer to the page table to set
  111.  */  
  112. static inline void set_ptl0_addr( pte_level0_t* pt)
  113. {
  114.     asm volatile ( "mcr p15, 0, %0, c2, c0, 0 \n"
  115.         :
  116.         : "r"(pt)
  117.     );
  118.    
  119. }
  120.  
  121. /** Returns level 0 page table entry flags.
  122.  *
  123.  *  \param pt     level 0 page table
  124.  *  \param i      index of the entry to return
  125.  */
  126. static inline int get_pt_level0_flags(pte_level0_t *pt, index_t i)
  127. {
  128.     pte_level0_t *p = &pt[i];
  129.  
  130.     return
  131.         ( (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT) << PAGE_PRESENT_SHIFT ) |
  132.         ( 1 << PAGE_USER_SHIFT )  |
  133.         ( 1 << PAGE_READ_SHIFT )  |
  134.         ( 1 << PAGE_WRITE_SHIFT ) |
  135.         ( 1 << PAGE_EXEC_SHIFT )  |
  136.         ( 1 << PAGE_CACHEABLE_SHIFT  )
  137.     ;
  138. }
  139.  
  140. /** Returns level 1 page table entry flags.
  141.  *
  142.  *  \param pt     level 1 page table
  143.  *  \param i      index of the entry to return
  144.  */
  145. static inline int get_pt_level1_flags(pte_level1_t *pt, index_t i)
  146. {
  147.     pte_level1_t *p = &pt[i];
  148.  
  149.     return
  150.         ( (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT)   << PAGE_PRESENT_SHIFT) |
  151.         ( (p->access_permission_0 == PTE_AP_USER_RO_KERNEL_RW) << PAGE_READ_SHIFT )  |
  152.         ( (p->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW) << PAGE_READ_SHIFT )  |
  153.         ( (p->access_permission_0 == PTE_AP_USER_RW_KERNEL_RW) << PAGE_WRITE_SHIFT ) |
  154.         ( (p->access_permission_0 != PTE_AP_USER_NO_KERNEL_RW) << PAGE_USER_SHIFT )  |
  155.         ( (p->access_permission_0 == PTE_AP_USER_NO_KERNEL_RW) << PAGE_READ_SHIFT )  |
  156.         ( (p->access_permission_0 == PTE_AP_USER_NO_KERNEL_RW) << PAGE_WRITE_SHIFT ) |
  157.         ( 1 << PAGE_EXEC_SHIFT ) |
  158.         ( p->bufferable << PAGE_CACHEABLE )
  159.     ;
  160. }
  161.  
  162. /** Sets flags of level 0 page table entry.
  163.  *
  164.  *  \param pt     level 0 page table
  165.  *  \param i      index of the entry to be changed
  166.  *  \param flags  new flags
  167.  */
  168. static inline void set_pt_level0_flags(pte_level0_t *pt, index_t i, int flags)
  169. {
  170.     pte_level0_t *p = &pt[i];
  171.  
  172.     if (flags & PAGE_NOT_PRESENT) {
  173.         p->descriptor_type = PTE_DESCRIPTOR_NOT_PRESENT;
  174.         // ensures that the entry will be recognized as valid when PTE_VALID_ARCH applied
  175.         p->should_be_zero  = 1;
  176.     } else {
  177.         p->descriptor_type = PTE_DESCRIPTOR_COARSE_TABLE;
  178.         p->should_be_zero  = 0;
  179.     }
  180. }
  181.  
  182. /** Sets flags of level 1 page table entry.
  183.  *
  184.  *  We use same access rights for the whole page. When page is not preset we
  185.  *  store 1 in acess_rigts_3 so that at least one bit is 1 (to mark correct
  186.  *  page entry, see #PAGE_VALID_ARCH).
  187.  *
  188.  *  \param pt     level 1 page table
  189.  *  \param i      index of the entry to be changed
  190.  *  \param flags  new flags
  191.  */  
  192. static inline void set_pt_level1_flags(pte_level1_t *pt, index_t i, int flags)
  193. {
  194.     pte_level1_t *p = &pt[i];
  195.    
  196.     if (flags & PAGE_NOT_PRESENT) {
  197.         p->descriptor_type      = PTE_DESCRIPTOR_NOT_PRESENT;
  198.         p->access_permission_3  = 1;
  199.     } else {
  200.         p->descriptor_type      = PTE_DESCRIPTOR_SMALL_PAGE;
  201.         p->access_permission_3  = p->access_permission_0;
  202.     }
  203.  
  204.     p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
  205.  
  206.     /* default access permission */
  207.     p->access_permission_0 = p->access_permission_1 =
  208.         p->access_permission_2 = p->access_permission_3 = PTE_AP_USER_NO_KERNEL_RW;
  209.  
  210.     if (flags & PAGE_USER)  {
  211.         if (flags & PAGE_READ) {
  212.             p->access_permission_0 = p->access_permission_1 =
  213.                 p->access_permission_2 = p->access_permission_3 =
  214.                 PTE_AP_USER_RO_KERNEL_RW;
  215.         }
  216.         if (flags & PAGE_WRITE) {
  217.             p->access_permission_0 = p->access_permission_1 =
  218.                 p->access_permission_2 = p->access_permission_3 =
  219.                 PTE_AP_USER_RW_KERNEL_RW;
  220.         }
  221.     }
  222. }
  223.  
  224.  
  225. extern void page_arch_init(void);
  226.  
  227. extern void prefetch_abourt(int n, istate_t *istate);
  228. extern void data_abourt(int n, istate_t *istate);
  229.  
  230. #endif /* __ASM__ */
  231.  
  232. #endif /* KERNEL */
  233.  
  234. #endif
  235.  
  236. /** @}
  237.  */
  238.  
  239.