Subversion Repositories HelenOS

Rev

Rev 687 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2006 Jakub Jermar
  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. #include <genarch/mm/page_pt.h>
  30. #include <mm/page.h>
  31. #include <mm/frame.h>
  32. #include <arch/mm/page.h>
  33. #include <arch/types.h>
  34. #include <typedefs.h>
  35. #include <arch/asm.h>
  36. #include <memstr.h>
  37.  
  38. static void pt_mapping_insert(__address page, __address frame, int flags, __address root);
  39. static pte_t *pt_mapping_find(__address page, __address root);
  40.  
  41. page_operations_t page_pt_operations = {
  42.     .mapping_insert = pt_mapping_insert,
  43.     .mapping_find = pt_mapping_find
  44. };
  45.  
  46. /** Map page to frame using hierarchical page tables.
  47.  *
  48.  * Map virtual address 'page' to physical address 'frame'
  49.  * using 'flags'.
  50.  *
  51.  * @param page Virtual address of the page to be mapped.
  52.  * @param frame Physical address of memory frame to which the mapping is done.
  53.  * @param flags Flags to be used for mapping.
  54.  * @param root Explicit PTL0 address.
  55.  */
  56. void pt_mapping_insert(__address page, __address frame, int flags, __address root)
  57. {
  58.     pte_t *ptl0, *ptl1, *ptl2, *ptl3;
  59.     __address newpt;
  60.  
  61.     ptl0 = (pte_t *) PA2KA(root ? root : (__address) GET_PTL0_ADDRESS());
  62.  
  63.     if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
  64.         newpt = frame_alloc(FRAME_KA, ONE_FRAME);
  65.         memsetb(newpt, PAGE_SIZE, 0);
  66.         SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
  67.         SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
  68.     }
  69.  
  70.     ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
  71.  
  72.     if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
  73.         newpt = frame_alloc(FRAME_KA, ONE_FRAME);
  74.         memsetb(newpt, PAGE_SIZE, 0);
  75.         SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
  76.         SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
  77.     }
  78.  
  79.     ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
  80.  
  81.     if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
  82.         newpt = frame_alloc(FRAME_KA, ONE_FRAME);
  83.         memsetb(newpt, PAGE_SIZE, 0);
  84.         SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
  85.         SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
  86.     }
  87.  
  88.     ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
  89.  
  90.     SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
  91.     SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags);
  92. }
  93.  
  94. /** Find mapping for virtual page in hierarchical page tables.
  95.  *
  96.  * Find mapping for virtual page.
  97.  *
  98.  * @param page Virtual page.
  99.  * @param root PTL0 address if non-zero.
  100.  *
  101.  * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise.
  102.  */
  103. pte_t *pt_mapping_find(__address page, __address root)
  104. {
  105.     pte_t *ptl0, *ptl1, *ptl2, *ptl3;
  106.  
  107.     ptl0 = (pte_t *) PA2KA(root ? root : (__address) GET_PTL0_ADDRESS());
  108.  
  109.     if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
  110.         return NULL;
  111.  
  112.     ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
  113.  
  114.     if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
  115.         return NULL;
  116.  
  117.     ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
  118.  
  119.     if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
  120.         return NULL;
  121.  
  122.     ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
  123.  
  124.     return &ptl3[PTL3_INDEX(page)];
  125. }
  126.