Subversion Repositories HelenOS

Rev

Rev 3007 | 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. /** @addtogroup genericmm
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  * @brief   Backend for address space areas backed by an ELF image.
  36.  */
  37.  
  38. #include <lib/elf.h>
  39. #include <debug.h>
  40. #include <arch/types.h>
  41. #include <mm/as.h>
  42. #include <mm/frame.h>
  43. #include <mm/slab.h>
  44. #include <mm/page.h>
  45. #include <genarch/mm/page_pt.h>
  46. #include <genarch/mm/page_ht.h>
  47. #include <align.h>
  48. #include <memstr.h>
  49. #include <macros.h>
  50. #include <arch.h>
  51.  
  52. #ifdef CONFIG_VIRT_IDX_DCACHE
  53. #include <arch/mm/cache.h>
  54. #endif
  55.  
  56. static int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access);
  57. static void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame);
  58. static void elf_share(as_area_t *area);
  59.  
  60. mem_backend_t elf_backend = {
  61.     .page_fault = elf_page_fault,
  62.     .frame_free = elf_frame_free,
  63.     .share = elf_share
  64. };
  65.  
  66. /** Service a page fault in the ELF backend address space area.
  67.  *
  68.  * The address space area and page tables must be already locked.
  69.  *
  70.  * @param area Pointer to the address space area.
  71.  * @param addr Faulting virtual address.
  72.  * @param access Access mode that caused the fault (i.e. read/write/exec).
  73.  *
  74.  * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
  75.  *     serviced).
  76.  */
  77. int elf_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
  78. {
  79.     elf_header_t *elf = area->backend_data.elf;
  80.     elf_segment_header_t *entry = area->backend_data.segment;
  81.     btree_node_t *leaf;
  82.     uintptr_t base, frame, page, start_anon;
  83.     index_t i;
  84.     bool dirty = false;
  85.  
  86.     if (!as_area_check_access(area, access))
  87.         return AS_PF_FAULT;
  88.  
  89.     ASSERT((addr >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) &&
  90.         (addr < entry->p_vaddr + entry->p_memsz));
  91.     i = (addr - ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) >> PAGE_WIDTH;
  92.     base = (uintptr_t)
  93.         (((void *) elf) + ALIGN_DOWN(entry->p_offset, PAGE_SIZE));
  94.  
  95.     /* Virtual address of faulting page*/
  96.     page = ALIGN_DOWN(addr, PAGE_SIZE);
  97.  
  98.     /* Virtual address of the end of initialized part of segment */
  99.     start_anon = entry->p_vaddr + entry->p_filesz;
  100.  
  101.     if (area->sh_info) {
  102.         bool found = false;
  103.  
  104.         /*
  105.          * The address space area is shared.
  106.          */
  107.        
  108.         mutex_lock(&area->sh_info->lock);
  109.         frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
  110.             page - area->base, &leaf);
  111.         if (!frame) {
  112.             unsigned int i;
  113.  
  114.             /*
  115.              * Workaround for valid NULL address.
  116.              */
  117.  
  118.             for (i = 0; i < leaf->keys; i++) {
  119.                 if (leaf->key[i] == page) {
  120.                     found = true;
  121.                     break;
  122.                 }
  123.             }
  124.         }
  125.         if (frame || found) {
  126.             frame_reference_add(ADDR2PFN(frame));
  127.             page_mapping_insert(AS, addr, frame,
  128.                 as_area_get_flags(area));
  129.             if (!used_space_insert(area, page, 1))
  130.                 panic("Could not insert used space.\n");
  131.             mutex_unlock(&area->sh_info->lock);
  132.             return AS_PF_OK;
  133.         }
  134.     }
  135.  
  136.     /*
  137.      * The area is either not shared or the pagemap does not contain the
  138.      * mapping.
  139.      */
  140.     if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
  141.         /*
  142.          * Initialized portion of the segment. The memory is backed
  143.          * directly by the content of the ELF image. Pages are
  144.          * only copied if the segment is writable so that there
  145.          * can be more instantions of the same memory ELF image
  146.          * used at a time. Note that this could be later done
  147.          * as COW.
  148.          */
  149.         if (entry->p_flags & PF_W) {
  150.             frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
  151.             memcpy((void *) PA2KA(frame),
  152.                 (void *) (base + i * FRAME_SIZE), FRAME_SIZE);
  153.             dirty = true;
  154.         } else {
  155.             frame = KA2PA(base + i * FRAME_SIZE);
  156.         }  
  157.     } else if (page >= start_anon) {
  158.         /*
  159.          * This is the uninitialized portion of the segment.
  160.          * It is not physically present in the ELF image.
  161.          * To resolve the situation, a frame must be allocated
  162.          * and cleared.
  163.          */
  164.         frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
  165.         memsetb(PA2KA(frame), FRAME_SIZE, 0);
  166.         dirty = true;
  167.     } else {
  168.         size_t pad_lo, pad_hi;
  169.         /*
  170.          * The mixed case.
  171.          *
  172.          * The middle part is backed by the ELF image and
  173.          * the lower and upper parts are anonymous memory.
  174.          * (The segment can be and often is shorter than 1 page).
  175.          */
  176.         if (page < entry->p_vaddr)
  177.             pad_lo = entry->p_vaddr - page;
  178.         else
  179.             pad_lo = 0;
  180.  
  181.         if (start_anon < page + PAGE_SIZE)
  182.             pad_hi = page + PAGE_SIZE - start_anon;
  183.         else
  184.             pad_hi = 0;
  185.  
  186.         frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
  187.         memcpy((void *) (PA2KA(frame) + pad_lo),
  188.             (void *) (base + i * FRAME_SIZE + pad_lo),
  189.             FRAME_SIZE - pad_lo - pad_hi);
  190.         memsetb(PA2KA(frame), pad_lo, 0);
  191.         memsetb(PA2KA(frame) + FRAME_SIZE - pad_hi, pad_hi, 0);
  192.         dirty = true;
  193.     }
  194.  
  195.     if (dirty && area->sh_info) {
  196.         frame_reference_add(ADDR2PFN(frame));
  197.         btree_insert(&area->sh_info->pagemap, page - area->base,
  198.             (void *) frame, leaf);
  199.     }
  200.  
  201.     if (area->sh_info)
  202.         mutex_unlock(&area->sh_info->lock);
  203.  
  204.     page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
  205.     if (!used_space_insert(area, page, 1))
  206.         panic("Could not insert used space.\n");
  207.  
  208.     return AS_PF_OK;
  209. }
  210.  
  211. /** Free a frame that is backed by the ELF backend.
  212.  *
  213.  * The address space area and page tables must be already locked.
  214.  *
  215.  * @param area Pointer to the address space area.
  216.  * @param page Page that is mapped to frame. Must be aligned to PAGE_SIZE.
  217.  * @param frame Frame to be released.
  218.  *
  219.  */
  220. void elf_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
  221. {
  222.     elf_header_t *elf = area->backend_data.elf;
  223.     elf_segment_header_t *entry = area->backend_data.segment;
  224.     uintptr_t base, start_anon;
  225.     index_t i;
  226.  
  227.     ASSERT((page >= ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) &&
  228.         (page < entry->p_vaddr + entry->p_memsz));
  229.     i = (page - ALIGN_DOWN(entry->p_vaddr, PAGE_SIZE)) >> PAGE_WIDTH;
  230.     base = (uintptr_t) (((void *) elf) +
  231.         ALIGN_DOWN(entry->p_offset, FRAME_SIZE));
  232.     start_anon = entry->p_vaddr + entry->p_filesz;
  233.  
  234.     if (page >= entry->p_vaddr && page + PAGE_SIZE <= start_anon) {
  235.         if (entry->p_flags & PF_W) {
  236.             /*
  237.              * Free the frame with the copy of writable segment
  238.              * data.
  239.              */
  240.             frame_free(frame);
  241.         }
  242.     } else {
  243.         /*
  244.          * The frame is either anonymous memory or the mixed case (i.e.
  245.          * lower part is backed by the ELF image and the upper is
  246.          * anonymous). In any case, a frame needs to be freed.
  247.          */
  248.         frame_free(frame);
  249.     }
  250. }
  251.  
  252. /** Share ELF image backed address space area.
  253.  *
  254.  * If the area is writable, then all mapped pages are duplicated in the pagemap.
  255.  * Otherwise only portions of the area that are not backed by the ELF image
  256.  * are put into the pagemap.
  257.  *
  258.  * The address space and address space area must be locked prior to the call.
  259.  *
  260.  * @param area Address space area.
  261.  */
  262. void elf_share(as_area_t *area)
  263. {
  264.     elf_segment_header_t *entry = area->backend_data.segment;
  265.     link_t *cur;
  266.     btree_node_t *leaf, *node;
  267.     uintptr_t start_anon = entry->p_vaddr + entry->p_filesz;
  268.  
  269.     /*
  270.      * Find the node in which to start linear search.
  271.      */
  272.     if (area->flags & AS_AREA_WRITE) {
  273.         node = list_get_instance(area->used_space.leaf_head.next,
  274.             btree_node_t, leaf_link);
  275.     } else {
  276.         (void) btree_search(&area->sh_info->pagemap, start_anon, &leaf);
  277.         node = btree_leaf_node_left_neighbour(&area->sh_info->pagemap,
  278.             leaf);
  279.         if (!node)
  280.             node = leaf;
  281.     }
  282.  
  283.     /*
  284.      * Copy used anonymous portions of the area to sh_info's page map.
  285.      */
  286.     mutex_lock(&area->sh_info->lock);
  287.     for (cur = &node->leaf_link; cur != &area->used_space.leaf_head;
  288.         cur = cur->next) {
  289.         unsigned int i;
  290.        
  291.         node = list_get_instance(cur, btree_node_t, leaf_link);
  292.        
  293.         for (i = 0; i < node->keys; i++) {
  294.             uintptr_t base = node->key[i];
  295.             count_t count = (count_t) node->value[i];
  296.             unsigned int j;
  297.            
  298.             /*
  299.              * Skip read-only areas of used space that are backed
  300.              * by the ELF image.
  301.              */
  302.             if (!(area->flags & AS_AREA_WRITE))
  303.                 if (base >= entry->p_vaddr &&
  304.                     base + count * PAGE_SIZE <= start_anon)
  305.                     continue;
  306.            
  307.             for (j = 0; j < count; j++) {
  308.                 pte_t *pte;
  309.            
  310.                 /*
  311.                  * Skip read-only pages that are backed by the
  312.                  * ELF image.
  313.                  */
  314.                 if (!(area->flags & AS_AREA_WRITE))
  315.                     if (base >= entry->p_vaddr &&
  316.                         base + (j + 1) * PAGE_SIZE <=
  317.                         start_anon)
  318.                         continue;
  319.                
  320.                 page_table_lock(area->as, false);
  321.                 pte = page_mapping_find(area->as,
  322.                     base + j * PAGE_SIZE);
  323.                 ASSERT(pte && PTE_VALID(pte) &&
  324.                     PTE_PRESENT(pte));
  325.                 btree_insert(&area->sh_info->pagemap,
  326.                     (base + j * PAGE_SIZE) - area->base,
  327.                     (void *) PTE_GET_FRAME(pte), NULL);
  328.                 page_table_unlock(area->as, false);
  329.  
  330.                 pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
  331.                 frame_reference_add(pfn);
  332.             }
  333.                
  334.         }
  335.     }
  336.     mutex_unlock(&area->sh_info->lock);
  337. }
  338.  
  339. /** @}
  340.  */
  341.