Subversion Repositories HelenOS-historic

Rev

Rev 814 | Rev 832 | 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 <mm/as.h>
  33. #include <arch/mm/page.h>
  34. #include <arch/mm/as.h>
  35. #include <arch/types.h>
  36. #include <typedefs.h>
  37. #include <arch/asm.h>
  38. #include <memstr.h>
  39.  
  40. static void pt_mapping_insert(as_t *as, __address page, __address frame, int flags);
  41. static void pt_mapping_remove(as_t *as, __address page);
  42. static pte_t *pt_mapping_find(as_t *as, __address page);
  43.  
  44. page_mapping_operations_t pt_mapping_operations = {
  45.     .mapping_insert = pt_mapping_insert,
  46.     .mapping_remove = pt_mapping_remove,
  47.     .mapping_find = pt_mapping_find
  48. };
  49.  
  50. /** Map page to frame using hierarchical page tables.
  51.  *
  52.  * Map virtual address 'page' to physical address 'frame'
  53.  * using 'flags'.
  54.  *
  55.  * The address space must be locked and interrupts must be disabled.
  56.  *
  57.  * @param as Address space to wich page belongs.
  58.  * @param page Virtual address of the page to be mapped.
  59.  * @param frame Physical address of memory frame to which the mapping is done.
  60.  * @param flags Flags to be used for mapping.
  61.  */
  62. void pt_mapping_insert(as_t *as, __address page, __address frame, int flags)
  63. {
  64.     pte_t *ptl0, *ptl1, *ptl2, *ptl3;
  65.     __address newpt;
  66.  
  67.     ptl0 = (pte_t *) PA2KA((__address) as->page_table);
  68.  
  69.     if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
  70.         newpt = PA2KA(PFN2ADDR(frame_alloc(ONE_FRAME, FRAME_KA)));
  71.         memsetb(newpt, PAGE_SIZE, 0);
  72.         SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
  73.         SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
  74.     }
  75.  
  76.     ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
  77.  
  78.     if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
  79.         newpt = PA2KA(PFN2ADDR(frame_alloc(ONE_FRAME, FRAME_KA)));
  80.         memsetb(newpt, PAGE_SIZE, 0);
  81.         SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
  82.         SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
  83.     }
  84.  
  85.     ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
  86.  
  87.     if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
  88.         newpt = PA2KA(PFN2ADDR(frame_alloc(ONE_FRAME, FRAME_KA)));
  89.         memsetb(newpt, PAGE_SIZE, 0);
  90.         SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
  91.         SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
  92.     }
  93.  
  94.     ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
  95.  
  96.     SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
  97.     SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags);
  98. }
  99.  
  100. /** Remove mapping of page from hierarchical page tables.
  101.  *
  102.  * Remove any mapping of 'page' within address space 'as'.
  103.  * TLB shootdown should follow in order to make effects of
  104.  * this call visible.
  105.  *
  106.  * The address space must be locked and interrupts must be disabled.
  107.  *
  108.  * @param as Address space to wich page belongs.
  109.  * @param page Virtual address of the page to be demapped.
  110.  */
  111. void pt_mapping_remove(as_t *as, __address page)
  112. {
  113.     pte_t *ptl0, *ptl1, *ptl2, *ptl3;
  114.  
  115.     ptl0 = (pte_t *) PA2KA((__address) as->page_table);
  116.  
  117.     if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
  118.         return;
  119.  
  120.     ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
  121.  
  122.     if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
  123.         return;
  124.  
  125.     ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
  126.  
  127.     if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
  128.         return;
  129.  
  130.     ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
  131.  
  132.     /* Destroy the mapping. Setting to PAGE_NOT_PRESENT is not sufficient. */
  133.     memsetb((__address) &ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
  134. }
  135.  
  136. /** Find mapping for virtual page in hierarchical page tables.
  137.  *
  138.  * Find mapping for virtual page.
  139.  *
  140.  * The address space must be locked and interrupts must be disabled.
  141.  *
  142.  * @param as Address space to which page belongs.
  143.  * @param page Virtual page.
  144.  *
  145.  * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise.
  146.  */
  147. pte_t *pt_mapping_find(as_t *as, __address page)
  148. {
  149.     pte_t *ptl0, *ptl1, *ptl2, *ptl3;
  150.  
  151.     ptl0 = (pte_t *) PA2KA((__address) as->page_table);
  152.  
  153.     if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
  154.         return NULL;
  155.  
  156.     ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
  157.  
  158.     if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
  159.         return NULL;
  160.  
  161.     ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
  162.  
  163.     if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
  164.         return NULL;
  165.  
  166.     ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
  167.  
  168.     return &ptl3[PTL3_INDEX(page)];
  169. }
  170.