Subversion Repositories HelenOS-historic

Rev

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