Subversion Repositories HelenOS-historic

Rev

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

  1. /*
  2.  * Copyright (C) 2001-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    as.c
  31.  * @brief   Address space related functions.
  32.  *
  33.  * This file contains address space manipulation functions.
  34.  * Roughly speaking, this is a higher-level client of
  35.  * Virtual Address Translation (VAT) subsystem.
  36.  *
  37.  * Functionality provided by this file allows one to
  38.  * create address space and create, resize and share
  39.  * address space areas.
  40.  *
  41.  * @see page.c
  42.  *
  43.  */
  44.  
  45. #include <mm/as.h>
  46. #include <arch/mm/as.h>
  47. #include <mm/page.h>
  48. #include <mm/frame.h>
  49. #include <mm/slab.h>
  50. #include <mm/tlb.h>
  51. #include <arch/mm/page.h>
  52. #include <genarch/mm/page_pt.h>
  53. #include <genarch/mm/page_ht.h>
  54. #include <mm/asid.h>
  55. #include <arch/mm/asid.h>
  56. #include <synch/spinlock.h>
  57. #include <synch/mutex.h>
  58. #include <adt/list.h>
  59. #include <adt/btree.h>
  60. #include <proc/task.h>
  61. #include <proc/thread.h>
  62. #include <arch/asm.h>
  63. #include <panic.h>
  64. #include <debug.h>
  65. #include <print.h>
  66. #include <memstr.h>
  67. #include <macros.h>
  68. #include <arch.h>
  69. #include <errno.h>
  70. #include <config.h>
  71. #include <align.h>
  72. #include <arch/types.h>
  73. #include <typedefs.h>
  74. #include <syscall/copy.h>
  75. #include <arch/interrupt.h>
  76.  
  77. /** This structure contains information associated with the shared address space area. */
  78. struct share_info {
  79.     mutex_t lock;       /**< This lock must be acquired only when the as_area lock is held. */
  80.     count_t refcount;   /**< This structure can be deallocated if refcount drops to 0. */
  81.     btree_t pagemap;    /**< B+tree containing complete map of anonymous pages of the shared area. */
  82. };
  83.  
  84. as_operations_t *as_operations = NULL;
  85.  
  86. /** This lock protects inactive_as_with_asid_head list. It must be acquired before as_t mutex. */
  87. SPINLOCK_INITIALIZE(inactive_as_with_asid_lock);
  88.  
  89. /**
  90.  * This list contains address spaces that are not active on any
  91.  * processor and that have valid ASID.
  92.  */
  93. LIST_INITIALIZE(inactive_as_with_asid_head);
  94.  
  95. /** Kernel address space. */
  96. as_t *AS_KERNEL = NULL;
  97.  
  98. static int area_flags_to_page_flags(int aflags);
  99. static as_area_t *find_area_and_lock(as_t *as, __address va);
  100. static bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area);
  101. static void sh_info_remove_reference(share_info_t *sh_info);
  102.  
  103. /** Initialize address space subsystem. */
  104. void as_init(void)
  105. {
  106.     as_arch_init();
  107.     AS_KERNEL = as_create(FLAG_AS_KERNEL);
  108.     if (!AS_KERNEL)
  109.         panic("can't create kernel address space\n");
  110.    
  111. }
  112.  
  113. /** Create address space.
  114.  *
  115.  * @param flags Flags that influence way in wich the address space is created.
  116.  */
  117. as_t *as_create(int flags)
  118. {
  119.     as_t *as;
  120.  
  121.     as = (as_t *) malloc(sizeof(as_t), 0);
  122.     link_initialize(&as->inactive_as_with_asid_link);
  123.     mutex_initialize(&as->lock);
  124.     btree_create(&as->as_area_btree);
  125.    
  126.     if (flags & FLAG_AS_KERNEL)
  127.         as->asid = ASID_KERNEL;
  128.     else
  129.         as->asid = ASID_INVALID;
  130.    
  131.     as->cpu_refcount = 0;
  132.     as->page_table = page_table_create(flags);
  133.  
  134.     return as;
  135. }
  136.  
  137. /** Free Adress space */
  138. void as_free(as_t *as)
  139. {
  140.     ASSERT(as->cpu_refcount == 0);
  141.  
  142.     /* TODO: free as_areas and other resources held by as */
  143.     /* TODO: free page table */
  144.     free(as);
  145. }
  146.  
  147. /** Create address space area of common attributes.
  148.  *
  149.  * The created address space area is added to the target address space.
  150.  *
  151.  * @param as Target address space.
  152.  * @param flags Flags of the area memory.
  153.  * @param size Size of area.
  154.  * @param base Base address of area.
  155.  * @param attrs Attributes of the area.
  156.  * @param backend Address space area backend. NULL if no backend is used.
  157.  * @param backend_data NULL or a pointer to an array holding two void *.
  158.  *
  159.  * @return Address space area on success or NULL on failure.
  160.  */
  161. as_area_t *as_area_create(as_t *as, int flags, size_t size, __address base, int attrs,
  162.            mem_backend_t *backend, void **backend_data)
  163. {
  164.     ipl_t ipl;
  165.     as_area_t *a;
  166.    
  167.     if (base % PAGE_SIZE)
  168.         return NULL;
  169.  
  170.     if (!size)
  171.         return NULL;
  172.  
  173.     /* Writeable executable areas are not supported. */
  174.     if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
  175.         return NULL;
  176.    
  177.     ipl = interrupts_disable();
  178.     mutex_lock(&as->lock);
  179.    
  180.     if (!check_area_conflicts(as, base, size, NULL)) {
  181.         mutex_unlock(&as->lock);
  182.         interrupts_restore(ipl);
  183.         return NULL;
  184.     }
  185.    
  186.     a = (as_area_t *) malloc(sizeof(as_area_t), 0);
  187.  
  188.     mutex_initialize(&a->lock);
  189.    
  190.     a->flags = flags;
  191.     a->attributes = attrs;
  192.     a->pages = SIZE2FRAMES(size);
  193.     a->base = base;
  194.     a->sh_info = NULL;
  195.     a->backend = backend;
  196.     if (backend_data) {
  197.         a->backend_data[0] = backend_data[0];
  198.         a->backend_data[1] = backend_data[1];
  199.     }
  200.     btree_create(&a->used_space);
  201.    
  202.     btree_insert(&as->as_area_btree, base, (void *) a, NULL);
  203.  
  204.     mutex_unlock(&as->lock);
  205.     interrupts_restore(ipl);
  206.  
  207.     return a;
  208. }
  209.  
  210. /** Find address space area and change it.
  211.  *
  212.  * @param as Address space.
  213.  * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
  214.  * @param size New size of the virtual memory block starting at address.
  215.  * @param flags Flags influencing the remap operation. Currently unused.
  216.  *
  217.  * @return Zero on success or a value from @ref errno.h otherwise.
  218.  */
  219. int as_area_resize(as_t *as, __address address, size_t size, int flags)
  220. {
  221.     as_area_t *area;
  222.     ipl_t ipl;
  223.     size_t pages;
  224.    
  225.     ipl = interrupts_disable();
  226.     mutex_lock(&as->lock);
  227.    
  228.     /*
  229.      * Locate the area.
  230.      */
  231.     area = find_area_and_lock(as, address);
  232.     if (!area) {
  233.         mutex_unlock(&as->lock);
  234.         interrupts_restore(ipl);
  235.         return ENOENT;
  236.     }
  237.  
  238.     if (area->flags & AS_AREA_DEVICE) {
  239.         /*
  240.          * Remapping of address space areas associated
  241.          * with memory mapped devices is not supported.
  242.          */
  243.         mutex_unlock(&area->lock);
  244.         mutex_unlock(&as->lock);
  245.         interrupts_restore(ipl);
  246.         return ENOTSUP;
  247.     }
  248.     if (area->sh_info) {
  249.         /*
  250.          * Remapping of shared address space areas
  251.          * is not supported.
  252.          */
  253.         mutex_unlock(&area->lock);
  254.         mutex_unlock(&as->lock);
  255.         interrupts_restore(ipl);
  256.         return ENOTSUP;
  257.     }
  258.  
  259.     pages = SIZE2FRAMES((address - area->base) + size);
  260.     if (!pages) {
  261.         /*
  262.          * Zero size address space areas are not allowed.
  263.          */
  264.         mutex_unlock(&area->lock);
  265.         mutex_unlock(&as->lock);
  266.         interrupts_restore(ipl);
  267.         return EPERM;
  268.     }
  269.    
  270.     if (pages < area->pages) {
  271.         bool cond;
  272.         __address start_free = area->base + pages*PAGE_SIZE;
  273.  
  274.         /*
  275.          * Shrinking the area.
  276.          * No need to check for overlaps.
  277.          */
  278.  
  279.         /*
  280.          * Remove frames belonging to used space starting from
  281.          * the highest addresses downwards until an overlap with
  282.          * the resized address space area is found. Note that this
  283.          * is also the right way to remove part of the used_space
  284.          * B+tree leaf list.
  285.          */    
  286.         for (cond = true; cond;) {
  287.             btree_node_t *node;
  288.        
  289.             ASSERT(!list_empty(&area->used_space.leaf_head));
  290.             node = list_get_instance(area->used_space.leaf_head.prev, btree_node_t, leaf_link);
  291.             if ((cond = (bool) node->keys)) {
  292.                 __address b = node->key[node->keys - 1];
  293.                 count_t c = (count_t) node->value[node->keys - 1];
  294.                 int i = 0;
  295.            
  296.                 if (overlaps(b, c*PAGE_SIZE, area->base, pages*PAGE_SIZE)) {
  297.                    
  298.                     if (b + c*PAGE_SIZE <= start_free) {
  299.                         /*
  300.                          * The whole interval fits completely
  301.                          * in the resized address space area.
  302.                          */
  303.                         break;
  304.                     }
  305.        
  306.                     /*
  307.                      * Part of the interval corresponding to b and c
  308.                      * overlaps with the resized address space area.
  309.                      */
  310.        
  311.                     cond = false;   /* we are almost done */
  312.                     i = (start_free - b) >> PAGE_WIDTH;
  313.                     if (!used_space_remove(area, start_free, c - i))
  314.                         panic("Could not remove used space.");
  315.                 } else {
  316.                     /*
  317.                      * The interval of used space can be completely removed.
  318.                      */
  319.                     if (!used_space_remove(area, b, c))
  320.                         panic("Could not remove used space.\n");
  321.                 }
  322.            
  323.                 for (; i < c; i++) {
  324.                     pte_t *pte;
  325.            
  326.                     page_table_lock(as, false);
  327.                     pte = page_mapping_find(as, b + i*PAGE_SIZE);
  328.                     ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
  329.                     if (area->backend && area->backend->backend_frame_free) {
  330.                         area->backend->backend_frame_free(area,
  331.                             b + i*PAGE_SIZE, PTE_GET_FRAME(pte));
  332.                     }
  333.                     page_mapping_remove(as, b + i*PAGE_SIZE);
  334.                     page_table_unlock(as, false);
  335.                 }
  336.             }
  337.         }
  338.         /*
  339.          * Invalidate TLB's.
  340.          */
  341.         tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
  342.         tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->pages - pages);
  343.         tlb_shootdown_finalize();
  344.     } else {
  345.         /*
  346.          * Growing the area.
  347.          * Check for overlaps with other address space areas.
  348.          */
  349.         if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
  350.             mutex_unlock(&area->lock);
  351.             mutex_unlock(&as->lock);       
  352.             interrupts_restore(ipl);
  353.             return EADDRNOTAVAIL;
  354.         }
  355.     }
  356.  
  357.     area->pages = pages;
  358.    
  359.     mutex_unlock(&area->lock);
  360.     mutex_unlock(&as->lock);
  361.     interrupts_restore(ipl);
  362.  
  363.     return 0;
  364. }
  365.  
  366. /** Destroy address space area.
  367.  *
  368.  * @param as Address space.
  369.  * @param address Address withing the area to be deleted.
  370.  *
  371.  * @return Zero on success or a value from @ref errno.h on failure.
  372.  */
  373. int as_area_destroy(as_t *as, __address address)
  374. {
  375.     as_area_t *area;
  376.     __address base;
  377.     ipl_t ipl;
  378.     bool cond;
  379.  
  380.     ipl = interrupts_disable();
  381.     mutex_lock(&as->lock);
  382.  
  383.     area = find_area_and_lock(as, address);
  384.     if (!area) {
  385.         mutex_unlock(&as->lock);
  386.         interrupts_restore(ipl);
  387.         return ENOENT;
  388.     }
  389.  
  390.     base = area->base;
  391.  
  392.     /*
  393.      * Visit only the pages mapped by used_space B+tree.
  394.      * Note that we must be very careful when walking the tree
  395.      * leaf list and removing used space as the leaf list changes
  396.      * unpredictibly after each remove. The solution is to actually
  397.      * not walk the tree at all, but to remove items from the head
  398.      * of the leaf list until there are some keys left.
  399.      */
  400.     for (cond = true; cond;) {
  401.         btree_node_t *node;
  402.        
  403.         ASSERT(!list_empty(&area->used_space.leaf_head));
  404.         node = list_get_instance(area->used_space.leaf_head.next, btree_node_t, leaf_link);
  405.         if ((cond = (bool) node->keys)) {
  406.             __address b = node->key[0];
  407.             count_t i;
  408.             pte_t *pte;
  409.            
  410.             for (i = 0; i < (count_t) node->value[0]; i++) {
  411.                 page_table_lock(as, false);
  412.                 pte = page_mapping_find(as, b + i*PAGE_SIZE);
  413.                 ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
  414.                 if (area->backend && area->backend->backend_frame_free) {
  415.                     area->backend->backend_frame_free(area,
  416.                         b + i*PAGE_SIZE, PTE_GET_FRAME(pte));
  417.                 }
  418.                 page_mapping_remove(as, b + i*PAGE_SIZE);
  419.                 page_table_unlock(as, false);
  420.             }
  421.             if (!used_space_remove(area, b, i))
  422.                 panic("Could not remove used space.\n");
  423.         }
  424.     }
  425.     btree_destroy(&area->used_space);
  426.  
  427.     /*
  428.      * Invalidate TLB's.
  429.      */
  430.     tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages);
  431.     tlb_invalidate_pages(AS->asid, area->base, area->pages);
  432.     tlb_shootdown_finalize();
  433.  
  434.     area->attributes |= AS_AREA_ATTR_PARTIAL;
  435.    
  436.     if (area->sh_info)
  437.         sh_info_remove_reference(area->sh_info);
  438.        
  439.     mutex_unlock(&area->lock);
  440.  
  441.     /*
  442.      * Remove the empty area from address space.
  443.      */
  444.     btree_remove(&AS->as_area_btree, base, NULL);
  445.    
  446.     free(area);
  447.    
  448.     mutex_unlock(&AS->lock);
  449.     interrupts_restore(ipl);
  450.     return 0;
  451. }
  452.  
  453. /** Share address space area with another or the same address space.
  454.  *
  455.  * Address space area of anonymous memory is shared with a new address
  456.  * space area. If the source address space area has not been shared so
  457.  * far, a new sh_info is created and the original mapping is duplicated
  458.  * in its pagemap B+tree. The new address space are simply gets the
  459.  * sh_info of the source area.
  460.  *
  461.  * @param src_as Pointer to source address space.
  462.  * @param src_base Base address of the source address space area.
  463.  * @param acc_size Expected size of the source area.
  464.  * @param dst_base Target base address.
  465.  * @param dst_flags_mask Destination address space area flags mask.
  466.  *
  467.  * @return Zero on success or ENOENT if there is no such task or
  468.  *     if there is no such address space area,
  469.  *     EPERM if there was a problem in accepting the area or
  470.  *     ENOMEM if there was a problem in allocating destination
  471.  *     address space area. ENOTSUP is returned if an attempt
  472.  *     to share non-anonymous address space area is detected.
  473.  */
  474. int as_area_share(as_t *src_as, __address src_base, size_t acc_size,
  475.           __address dst_base, int dst_flags_mask)
  476. {
  477.     ipl_t ipl;
  478.     int src_flags;
  479.     size_t src_size;
  480.     as_area_t *src_area, *dst_area;
  481.     share_info_t *sh_info;
  482.     link_t *cur;
  483.  
  484.     ipl = interrupts_disable();
  485.     mutex_lock(&src_as->lock);
  486.     src_area = find_area_and_lock(src_as, src_base);
  487.     if (!src_area) {
  488.         /*
  489.          * Could not find the source address space area.
  490.          */
  491.         mutex_unlock(&src_as->lock);
  492.         interrupts_restore(ipl);
  493.         return ENOENT;
  494.     }
  495.    
  496.     if (!src_area->backend || src_area->backend != &anon_backend) {
  497.         /*
  498.          * As of now, only anonymous address space areas can be shared.
  499.          */
  500.         mutex_unlock(&src_area->lock);
  501.         mutex_unlock(&src_as->lock);
  502.         interrupts_restore(ipl);
  503.         return ENOTSUP;
  504.     }
  505.    
  506.     src_size = src_area->pages * PAGE_SIZE;
  507.     src_flags = src_area->flags;
  508.    
  509.     if (src_size != acc_size) {
  510.         mutex_unlock(&src_area->lock);
  511.         mutex_unlock(&src_as->lock);
  512.         interrupts_restore(ipl);
  513.         return EPERM;
  514.     }
  515.  
  516.     /*
  517.      * Now we are committed to sharing the area.
  518.      * First prepare the area for sharing.
  519.      * Then it will be safe to unlock it.
  520.      */
  521.     sh_info = src_area->sh_info;
  522.     if (!sh_info) {
  523.         sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
  524.         mutex_initialize(&sh_info->lock);
  525.         sh_info->refcount = 2;
  526.         btree_create(&sh_info->pagemap);
  527.         src_area->sh_info = sh_info;
  528.     } else {
  529.         mutex_lock(&sh_info->lock);
  530.         sh_info->refcount++;
  531.         mutex_unlock(&sh_info->lock);
  532.     }
  533.  
  534.     /*
  535.      * Copy used portions of the area to sh_info's page map.
  536.      */
  537.     mutex_lock(&sh_info->lock);
  538.     for (cur = src_area->used_space.leaf_head.next; cur != &src_area->used_space.leaf_head; cur = cur->next) {
  539.         btree_node_t *node;
  540.         int i;
  541.        
  542.         node = list_get_instance(cur, btree_node_t, leaf_link);
  543.         for (i = 0; i < node->keys; i++) {
  544.             __address base = node->key[i];
  545.             count_t count = (count_t) node->value[i];
  546.             int j;
  547.            
  548.             for (j = 0; j < count; j++) {
  549.                 pte_t *pte;
  550.            
  551.                 page_table_lock(src_as, false);
  552.                 pte = page_mapping_find(src_as, base + j*PAGE_SIZE);
  553.                 ASSERT(pte && PTE_VALID(pte) && PTE_PRESENT(pte));
  554.                 btree_insert(&sh_info->pagemap, (base + j*PAGE_SIZE) - src_area->base,
  555.                     (void *) PTE_GET_FRAME(pte), NULL);
  556.                 page_table_unlock(src_as, false);
  557.             }
  558.                
  559.         }
  560.     }
  561.     mutex_unlock(&sh_info->lock);
  562.  
  563.     mutex_unlock(&src_area->lock);
  564.     mutex_unlock(&src_as->lock);
  565.  
  566.     /*
  567.      * Create copy of the source address space area.
  568.      * The destination area is created with AS_AREA_ATTR_PARTIAL
  569.      * attribute set which prevents race condition with
  570.      * preliminary as_page_fault() calls.
  571.      * The flags of the source area are masked against dst_flags_mask
  572.      * to support sharing in less privileged mode.
  573.      */
  574.     dst_area = as_area_create(AS, src_flags & dst_flags_mask, src_size, dst_base,
  575.                   AS_AREA_ATTR_PARTIAL, &anon_backend, NULL);
  576.     if (!dst_area) {
  577.         /*
  578.          * Destination address space area could not be created.
  579.          */
  580.         sh_info_remove_reference(sh_info);
  581.        
  582.         interrupts_restore(ipl);
  583.         return ENOMEM;
  584.     }
  585.    
  586.     /*
  587.      * Now the destination address space area has been
  588.      * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
  589.      * attribute and set the sh_info.
  590.      */
  591.     mutex_lock(&dst_area->lock);
  592.     dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
  593.     dst_area->sh_info = sh_info;
  594.     mutex_unlock(&dst_area->lock);
  595.    
  596.     interrupts_restore(ipl);
  597.    
  598.     return 0;
  599. }
  600.  
  601. /** Initialize mapping for one page of address space.
  602.  *
  603.  * This functions maps 'page' to 'frame' according
  604.  * to attributes of the address space area to
  605.  * wich 'page' belongs.
  606.  *
  607.  * @param as Target address space.
  608.  * @param page Virtual page within the area.
  609.  * @param frame Physical frame to which page will be mapped.
  610.  */
  611. void as_set_mapping(as_t *as, __address page, __address frame)
  612. {
  613.     as_area_t *area;
  614.     ipl_t ipl;
  615.    
  616.     ipl = interrupts_disable();
  617.     page_table_lock(as, true);
  618.    
  619.     area = find_area_and_lock(as, page);
  620.     if (!area) {
  621.         panic("Page not part of any as_area.\n");
  622.     }
  623.  
  624.     ASSERT(!area->backend);
  625.    
  626.     page_mapping_insert(as, page, frame, as_area_get_flags(area));
  627.     if (!used_space_insert(area, page, 1))
  628.         panic("Could not insert used space.\n");
  629.    
  630.     mutex_unlock(&area->lock);
  631.     page_table_unlock(as, true);
  632.     interrupts_restore(ipl);
  633. }
  634.  
  635. /** Handle page fault within the current address space.
  636.  *
  637.  * This is the high-level page fault handler. It decides
  638.  * whether the page fault can be resolved by any backend
  639.  * and if so, it invokes the backend to resolve the page
  640.  * fault.
  641.  *
  642.  * Interrupts are assumed disabled.
  643.  *
  644.  * @param page Faulting page.
  645.  * @param access Access mode that caused the fault (i.e. read/write/exec).
  646.  * @param istate Pointer to interrupted state.
  647.  *
  648.  * @return AS_PF_FAULT on page fault, AS_PF_OK on success or AS_PF_DEFER if the
  649.  *     fault was caused by copy_to_uspace() or copy_from_uspace().
  650.  */
  651. int as_page_fault(__address page, pf_access_t access, istate_t *istate)
  652. {
  653.     pte_t *pte;
  654.     as_area_t *area;
  655.    
  656.     if (!THREAD)
  657.         return AS_PF_FAULT;
  658.        
  659.     ASSERT(AS);
  660.  
  661.     mutex_lock(&AS->lock);
  662.     area = find_area_and_lock(AS, page);   
  663.     if (!area) {
  664.         /*
  665.          * No area contained mapping for 'page'.
  666.          * Signal page fault to low-level handler.
  667.          */
  668.         mutex_unlock(&AS->lock);
  669.         goto page_fault;
  670.     }
  671.  
  672.     if (area->attributes & AS_AREA_ATTR_PARTIAL) {
  673.         /*
  674.          * The address space area is not fully initialized.
  675.          * Avoid possible race by returning error.
  676.          */
  677.         mutex_unlock(&area->lock);
  678.         mutex_unlock(&AS->lock);
  679.         goto page_fault;       
  680.     }
  681.  
  682.     if (!area->backend || !area->backend->backend_page_fault) {
  683.         /*
  684.          * The address space area is not backed by any backend
  685.          * or the backend cannot handle page faults.
  686.          */
  687.         mutex_unlock(&area->lock);
  688.         mutex_unlock(&AS->lock);
  689.         goto page_fault;       
  690.     }
  691.  
  692.     page_table_lock(AS, false);
  693.    
  694.     /*
  695.      * To avoid race condition between two page faults
  696.      * on the same address, we need to make sure
  697.      * the mapping has not been already inserted.
  698.      */
  699.     if ((pte = page_mapping_find(AS, page))) {
  700.         if (PTE_PRESENT(pte)) {
  701.             page_table_unlock(AS, false);
  702.             mutex_unlock(&area->lock);
  703.             mutex_unlock(&AS->lock);
  704.             return AS_PF_OK;
  705.         }
  706.     }
  707.    
  708.     /*
  709.      * Resort to the backend page fault handler.
  710.      */
  711.     if (area->backend->backend_page_fault(area, page, access) != AS_PF_OK) {
  712.         page_table_unlock(AS, false);
  713.         mutex_unlock(&area->lock);
  714.         mutex_unlock(&AS->lock);
  715.         goto page_fault;
  716.     }
  717.    
  718.     page_table_unlock(AS, false);
  719.     mutex_unlock(&area->lock);
  720.     mutex_unlock(&AS->lock);
  721.     return AS_PF_OK;
  722.  
  723. page_fault:
  724.     if (THREAD->in_copy_from_uspace) {
  725.         THREAD->in_copy_from_uspace = false;
  726.         istate_set_retaddr(istate, (__address) &memcpy_from_uspace_failover_address);
  727.     } else if (THREAD->in_copy_to_uspace) {
  728.         THREAD->in_copy_to_uspace = false;
  729.         istate_set_retaddr(istate, (__address) &memcpy_to_uspace_failover_address);
  730.     } else {
  731.         return AS_PF_FAULT;
  732.     }
  733.  
  734.     return AS_PF_DEFER;
  735. }
  736.  
  737. /** Switch address spaces.
  738.  *
  739.  * Note that this function cannot sleep as it is essentially a part of
  740.  * scheduling. Sleeping here would lead to deadlock on wakeup.
  741.  *
  742.  * @param old Old address space or NULL.
  743.  * @param new New address space.
  744.  */
  745. void as_switch(as_t *old, as_t *new)
  746. {
  747.     ipl_t ipl;
  748.     bool needs_asid = false;
  749.    
  750.     ipl = interrupts_disable();
  751.     spinlock_lock(&inactive_as_with_asid_lock);
  752.  
  753.     /*
  754.      * First, take care of the old address space.
  755.      */
  756.     if (old) {
  757.         mutex_lock_active(&old->lock);
  758.         ASSERT(old->cpu_refcount);
  759.         if((--old->cpu_refcount == 0) && (old != AS_KERNEL)) {
  760.             /*
  761.              * The old address space is no longer active on
  762.              * any processor. It can be appended to the
  763.              * list of inactive address spaces with assigned
  764.              * ASID.
  765.              */
  766.              ASSERT(old->asid != ASID_INVALID);
  767.              list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
  768.         }
  769.         mutex_unlock(&old->lock);
  770.     }
  771.  
  772.     /*
  773.      * Second, prepare the new address space.
  774.      */
  775.     mutex_lock_active(&new->lock);
  776.     if ((new->cpu_refcount++ == 0) && (new != AS_KERNEL)) {
  777.         if (new->asid != ASID_INVALID)
  778.             list_remove(&new->inactive_as_with_asid_link);
  779.         else
  780.             needs_asid = true;  /* defer call to asid_get() until new->lock is released */
  781.     }
  782.     SET_PTL0_ADDRESS(new->page_table);
  783.     mutex_unlock(&new->lock);
  784.  
  785.     if (needs_asid) {
  786.         /*
  787.          * Allocation of new ASID was deferred
  788.          * until now in order to avoid deadlock.
  789.          */
  790.         asid_t asid;
  791.        
  792.         asid = asid_get();
  793.         mutex_lock_active(&new->lock);
  794.         new->asid = asid;
  795.         mutex_unlock(&new->lock);
  796.     }
  797.     spinlock_unlock(&inactive_as_with_asid_lock);
  798.     interrupts_restore(ipl);
  799.    
  800.     /*
  801.      * Perform architecture-specific steps.
  802.      * (e.g. write ASID to hardware register etc.)
  803.      */
  804.     as_install_arch(new);
  805.    
  806.     AS = new;
  807. }
  808.  
  809. /** Convert address space area flags to page flags.
  810.  *
  811.  * @param aflags Flags of some address space area.
  812.  *
  813.  * @return Flags to be passed to page_mapping_insert().
  814.  */
  815. int area_flags_to_page_flags(int aflags)
  816. {
  817.     int flags;
  818.  
  819.     flags = PAGE_USER | PAGE_PRESENT;
  820.    
  821.     if (aflags & AS_AREA_READ)
  822.         flags |= PAGE_READ;
  823.        
  824.     if (aflags & AS_AREA_WRITE)
  825.         flags |= PAGE_WRITE;
  826.    
  827.     if (aflags & AS_AREA_EXEC)
  828.         flags |= PAGE_EXEC;
  829.    
  830.     if (!(aflags & AS_AREA_DEVICE))
  831.         flags |= PAGE_CACHEABLE;
  832.        
  833.     return flags;
  834. }
  835.  
  836. /** Compute flags for virtual address translation subsytem.
  837.  *
  838.  * The address space area must be locked.
  839.  * Interrupts must be disabled.
  840.  *
  841.  * @param a Address space area.
  842.  *
  843.  * @return Flags to be used in page_mapping_insert().
  844.  */
  845. int as_area_get_flags(as_area_t *a)
  846. {
  847.     return area_flags_to_page_flags(a->flags);
  848. }
  849.  
  850. /** Create page table.
  851.  *
  852.  * Depending on architecture, create either address space
  853.  * private or global page table.
  854.  *
  855.  * @param flags Flags saying whether the page table is for kernel address space.
  856.  *
  857.  * @return First entry of the page table.
  858.  */
  859. pte_t *page_table_create(int flags)
  860. {
  861.         ASSERT(as_operations);
  862.         ASSERT(as_operations->page_table_create);
  863.  
  864.         return as_operations->page_table_create(flags);
  865. }
  866.  
  867. /** Lock page table.
  868.  *
  869.  * This function should be called before any page_mapping_insert(),
  870.  * page_mapping_remove() and page_mapping_find().
  871.  *
  872.  * Locking order is such that address space areas must be locked
  873.  * prior to this call. Address space can be locked prior to this
  874.  * call in which case the lock argument is false.
  875.  *
  876.  * @param as Address space.
  877.  * @param lock If false, do not attempt to lock as->lock.
  878.  */
  879. void page_table_lock(as_t *as, bool lock)
  880. {
  881.     ASSERT(as_operations);
  882.     ASSERT(as_operations->page_table_lock);
  883.  
  884.     as_operations->page_table_lock(as, lock);
  885. }
  886.  
  887. /** Unlock page table.
  888.  *
  889.  * @param as Address space.
  890.  * @param unlock If false, do not attempt to unlock as->lock.
  891.  */
  892. void page_table_unlock(as_t *as, bool unlock)
  893. {
  894.     ASSERT(as_operations);
  895.     ASSERT(as_operations->page_table_unlock);
  896.  
  897.     as_operations->page_table_unlock(as, unlock);
  898. }
  899.  
  900.  
  901. /** Find address space area and lock it.
  902.  *
  903.  * The address space must be locked and interrupts must be disabled.
  904.  *
  905.  * @param as Address space.
  906.  * @param va Virtual address.
  907.  *
  908.  * @return Locked address space area containing va on success or NULL on failure.
  909.  */
  910. as_area_t *find_area_and_lock(as_t *as, __address va)
  911. {
  912.     as_area_t *a;
  913.     btree_node_t *leaf, *lnode;
  914.     int i;
  915.    
  916.     a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
  917.     if (a) {
  918.         /* va is the base address of an address space area */
  919.         mutex_lock(&a->lock);
  920.         return a;
  921.     }
  922.    
  923.     /*
  924.      * Search the leaf node and the righmost record of its left neighbour
  925.      * to find out whether this is a miss or va belongs to an address
  926.      * space area found there.
  927.      */
  928.    
  929.     /* First, search the leaf node itself. */
  930.     for (i = 0; i < leaf->keys; i++) {
  931.         a = (as_area_t *) leaf->value[i];
  932.         mutex_lock(&a->lock);
  933.         if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
  934.             return a;
  935.         }
  936.         mutex_unlock(&a->lock);
  937.     }
  938.  
  939.     /*
  940.      * Second, locate the left neighbour and test its last record.
  941.      * Because of its position in the B+tree, it must have base < va.
  942.      */
  943.     if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
  944.         a = (as_area_t *) lnode->value[lnode->keys - 1];
  945.         mutex_lock(&a->lock);
  946.         if (va < a->base + a->pages * PAGE_SIZE) {
  947.             return a;
  948.         }
  949.         mutex_unlock(&a->lock);
  950.     }
  951.  
  952.     return NULL;
  953. }
  954.  
  955. /** Check area conflicts with other areas.
  956.  *
  957.  * The address space must be locked and interrupts must be disabled.
  958.  *
  959.  * @param as Address space.
  960.  * @param va Starting virtual address of the area being tested.
  961.  * @param size Size of the area being tested.
  962.  * @param avoid_area Do not touch this area.
  963.  *
  964.  * @return True if there is no conflict, false otherwise.
  965.  */
  966. bool check_area_conflicts(as_t *as, __address va, size_t size, as_area_t *avoid_area)
  967. {
  968.     as_area_t *a;
  969.     btree_node_t *leaf, *node;
  970.     int i;
  971.    
  972.     /*
  973.      * We don't want any area to have conflicts with NULL page.
  974.      */
  975.     if (overlaps(va, size, NULL, PAGE_SIZE))
  976.         return false;
  977.    
  978.     /*
  979.      * The leaf node is found in O(log n), where n is proportional to
  980.      * the number of address space areas belonging to as.
  981.      * The check for conflicts is then attempted on the rightmost
  982.      * record in the left neighbour, the leftmost record in the right
  983.      * neighbour and all records in the leaf node itself.
  984.      */
  985.    
  986.     if ((a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf))) {
  987.         if (a != avoid_area)
  988.             return false;
  989.     }
  990.    
  991.     /* First, check the two border cases. */
  992.     if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
  993.         a = (as_area_t *) node->value[node->keys - 1];
  994.         mutex_lock(&a->lock);
  995.         if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
  996.             mutex_unlock(&a->lock);
  997.             return false;
  998.         }
  999.         mutex_unlock(&a->lock);
  1000.     }
  1001.     if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
  1002.         a = (as_area_t *) node->value[0];
  1003.         mutex_lock(&a->lock);
  1004.         if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
  1005.             mutex_unlock(&a->lock);
  1006.             return false;
  1007.         }
  1008.         mutex_unlock(&a->lock);
  1009.     }
  1010.    
  1011.     /* Second, check the leaf node. */
  1012.     for (i = 0; i < leaf->keys; i++) {
  1013.         a = (as_area_t *) leaf->value[i];
  1014.    
  1015.         if (a == avoid_area)
  1016.             continue;
  1017.    
  1018.         mutex_lock(&a->lock);
  1019.         if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
  1020.             mutex_unlock(&a->lock);
  1021.             return false;
  1022.         }
  1023.         mutex_unlock(&a->lock);
  1024.     }
  1025.  
  1026.     /*
  1027.      * So far, the area does not conflict with other areas.
  1028.      * Check if it doesn't conflict with kernel address space.
  1029.      */  
  1030.     if (!KERNEL_ADDRESS_SPACE_SHADOWED) {
  1031.         return !overlaps(va, size,
  1032.             KERNEL_ADDRESS_SPACE_START, KERNEL_ADDRESS_SPACE_END-KERNEL_ADDRESS_SPACE_START);
  1033.     }
  1034.  
  1035.     return true;
  1036. }
  1037.  
  1038. /** Return size of the address space area with given base.  */
  1039. size_t as_get_size(__address base)
  1040. {
  1041.     ipl_t ipl;
  1042.     as_area_t *src_area;
  1043.     size_t size;
  1044.  
  1045.     ipl = interrupts_disable();
  1046.     src_area = find_area_and_lock(AS, base);
  1047.     if (src_area){
  1048.         size = src_area->pages * PAGE_SIZE;
  1049.         mutex_unlock(&src_area->lock);
  1050.     } else {
  1051.         size = 0;
  1052.     }
  1053.     interrupts_restore(ipl);
  1054.     return size;
  1055. }
  1056.  
  1057. /** Mark portion of address space area as used.
  1058.  *
  1059.  * The address space area must be already locked.
  1060.  *
  1061.  * @param a Address space area.
  1062.  * @param page First page to be marked.
  1063.  * @param count Number of page to be marked.
  1064.  *
  1065.  * @return 0 on failure and 1 on success.
  1066.  */
  1067. int used_space_insert(as_area_t *a, __address page, count_t count)
  1068. {
  1069.     btree_node_t *leaf, *node;
  1070.     count_t pages;
  1071.     int i;
  1072.  
  1073.     ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
  1074.     ASSERT(count);
  1075.  
  1076.     pages = (count_t) btree_search(&a->used_space, page, &leaf);
  1077.     if (pages) {
  1078.         /*
  1079.          * We hit the beginning of some used space.
  1080.          */
  1081.         return 0;
  1082.     }
  1083.  
  1084.     node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
  1085.     if (node) {
  1086.         __address left_pg = node->key[node->keys - 1], right_pg = leaf->key[0];
  1087.         count_t left_cnt = (count_t) node->value[node->keys - 1], right_cnt = (count_t) leaf->value[0];
  1088.        
  1089.         /*
  1090.          * Examine the possibility that the interval fits
  1091.          * somewhere between the rightmost interval of
  1092.          * the left neigbour and the first interval of the leaf.
  1093.          */
  1094.          
  1095.         if (page >= right_pg) {
  1096.             /* Do nothing. */
  1097.         } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
  1098.             /* The interval intersects with the left interval. */
  1099.             return 0;
  1100.         } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
  1101.             /* The interval intersects with the right interval. */
  1102.             return 0;          
  1103.         } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
  1104.             /* The interval can be added by merging the two already present intervals. */
  1105.             node->value[node->keys - 1] += count + right_cnt;
  1106.             btree_remove(&a->used_space, right_pg, leaf);
  1107.             return 1;
  1108.         } else if (page == left_pg + left_cnt*PAGE_SIZE) {
  1109.             /* The interval can be added by simply growing the left interval. */
  1110.             node->value[node->keys - 1] += count;
  1111.             return 1;
  1112.         } else if (page + count*PAGE_SIZE == right_pg) {
  1113.             /*
  1114.              * The interval can be addded by simply moving base of the right
  1115.              * interval down and increasing its size accordingly.
  1116.              */
  1117.             leaf->value[0] += count;
  1118.             leaf->key[0] = page;
  1119.             return 1;
  1120.         } else {
  1121.             /*
  1122.              * The interval is between both neigbouring intervals,
  1123.              * but cannot be merged with any of them.
  1124.              */
  1125.             btree_insert(&a->used_space, page, (void *) count, leaf);
  1126.             return 1;
  1127.         }
  1128.     } else if (page < leaf->key[0]) {
  1129.         __address right_pg = leaf->key[0];
  1130.         count_t right_cnt = (count_t) leaf->value[0];
  1131.    
  1132.         /*
  1133.          * Investigate the border case in which the left neighbour does not
  1134.          * exist but the interval fits from the left.
  1135.          */
  1136.          
  1137.         if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
  1138.             /* The interval intersects with the right interval. */
  1139.             return 0;
  1140.         } else if (page + count*PAGE_SIZE == right_pg) {
  1141.             /*
  1142.              * The interval can be added by moving the base of the right interval down
  1143.              * and increasing its size accordingly.
  1144.              */
  1145.             leaf->key[0] = page;
  1146.             leaf->value[0] += count;
  1147.             return 1;
  1148.         } else {
  1149.             /*
  1150.              * The interval doesn't adjoin with the right interval.
  1151.              * It must be added individually.
  1152.              */
  1153.             btree_insert(&a->used_space, page, (void *) count, leaf);
  1154.             return 1;
  1155.         }
  1156.     }
  1157.  
  1158.     node = btree_leaf_node_right_neighbour(&a->used_space, leaf);
  1159.     if (node) {
  1160.         __address left_pg = leaf->key[leaf->keys - 1], right_pg = node->key[0];
  1161.         count_t left_cnt = (count_t) leaf->value[leaf->keys - 1], right_cnt = (count_t) node->value[0];
  1162.        
  1163.         /*
  1164.          * Examine the possibility that the interval fits
  1165.          * somewhere between the leftmost interval of
  1166.          * the right neigbour and the last interval of the leaf.
  1167.          */
  1168.  
  1169.         if (page < left_pg) {
  1170.             /* Do nothing. */
  1171.         } else if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
  1172.             /* The interval intersects with the left interval. */
  1173.             return 0;
  1174.         } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
  1175.             /* The interval intersects with the right interval. */
  1176.             return 0;          
  1177.         } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
  1178.             /* The interval can be added by merging the two already present intervals. */
  1179.             leaf->value[leaf->keys - 1] += count + right_cnt;
  1180.             btree_remove(&a->used_space, right_pg, node);
  1181.             return 1;
  1182.         } else if (page == left_pg + left_cnt*PAGE_SIZE) {
  1183.             /* The interval can be added by simply growing the left interval. */
  1184.             leaf->value[leaf->keys - 1] +=  count;
  1185.             return 1;
  1186.         } else if (page + count*PAGE_SIZE == right_pg) {
  1187.             /*
  1188.              * The interval can be addded by simply moving base of the right
  1189.              * interval down and increasing its size accordingly.
  1190.              */
  1191.             node->value[0] += count;
  1192.             node->key[0] = page;
  1193.             return 1;
  1194.         } else {
  1195.             /*
  1196.              * The interval is between both neigbouring intervals,
  1197.              * but cannot be merged with any of them.
  1198.              */
  1199.             btree_insert(&a->used_space, page, (void *) count, leaf);
  1200.             return 1;
  1201.         }
  1202.     } else if (page >= leaf->key[leaf->keys - 1]) {
  1203.         __address left_pg = leaf->key[leaf->keys - 1];
  1204.         count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
  1205.    
  1206.         /*
  1207.          * Investigate the border case in which the right neighbour does not
  1208.          * exist but the interval fits from the right.
  1209.          */
  1210.          
  1211.         if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
  1212.             /* The interval intersects with the left interval. */
  1213.             return 0;
  1214.         } else if (left_pg + left_cnt*PAGE_SIZE == page) {
  1215.             /* The interval can be added by growing the left interval. */
  1216.             leaf->value[leaf->keys - 1] += count;
  1217.             return 1;
  1218.         } else {
  1219.             /*
  1220.              * The interval doesn't adjoin with the left interval.
  1221.              * It must be added individually.
  1222.              */
  1223.             btree_insert(&a->used_space, page, (void *) count, leaf);
  1224.             return 1;
  1225.         }
  1226.     }
  1227.    
  1228.     /*
  1229.      * Note that if the algorithm made it thus far, the interval can fit only
  1230.      * between two other intervals of the leaf. The two border cases were already
  1231.      * resolved.
  1232.      */
  1233.     for (i = 1; i < leaf->keys; i++) {
  1234.         if (page < leaf->key[i]) {
  1235.             __address left_pg = leaf->key[i - 1], right_pg = leaf->key[i];
  1236.             count_t left_cnt = (count_t) leaf->value[i - 1], right_cnt = (count_t) leaf->value[i];
  1237.  
  1238.             /*
  1239.              * The interval fits between left_pg and right_pg.
  1240.              */
  1241.  
  1242.             if (overlaps(page, count*PAGE_SIZE, left_pg, left_cnt*PAGE_SIZE)) {
  1243.                 /* The interval intersects with the left interval. */
  1244.                 return 0;
  1245.             } else if (overlaps(page, count*PAGE_SIZE, right_pg, right_cnt*PAGE_SIZE)) {
  1246.                 /* The interval intersects with the right interval. */
  1247.                 return 0;          
  1248.             } else if ((page == left_pg + left_cnt*PAGE_SIZE) && (page + count*PAGE_SIZE == right_pg)) {
  1249.                 /* The interval can be added by merging the two already present intervals. */
  1250.                 leaf->value[i - 1] += count + right_cnt;
  1251.                 btree_remove(&a->used_space, right_pg, leaf);
  1252.                 return 1;
  1253.             } else if (page == left_pg + left_cnt*PAGE_SIZE) {
  1254.                 /* The interval can be added by simply growing the left interval. */
  1255.                 leaf->value[i - 1] += count;
  1256.                 return 1;
  1257.             } else if (page + count*PAGE_SIZE == right_pg) {
  1258.                 /*
  1259.                      * The interval can be addded by simply moving base of the right
  1260.                  * interval down and increasing its size accordingly.
  1261.                  */
  1262.                 leaf->value[i] += count;
  1263.                 leaf->key[i] = page;
  1264.                 return 1;
  1265.             } else {
  1266.                 /*
  1267.                  * The interval is between both neigbouring intervals,
  1268.                  * but cannot be merged with any of them.
  1269.                  */
  1270.                 btree_insert(&a->used_space, page, (void *) count, leaf);
  1271.                 return 1;
  1272.             }
  1273.         }
  1274.     }
  1275.  
  1276.     panic("Inconsistency detected while adding %d pages of used space at %P.\n", count, page);
  1277. }
  1278.  
  1279. /** Mark portion of address space area as unused.
  1280.  *
  1281.  * The address space area must be already locked.
  1282.  *
  1283.  * @param a Address space area.
  1284.  * @param page First page to be marked.
  1285.  * @param count Number of page to be marked.
  1286.  *
  1287.  * @return 0 on failure and 1 on success.
  1288.  */
  1289. int used_space_remove(as_area_t *a, __address page, count_t count)
  1290. {
  1291.     btree_node_t *leaf, *node;
  1292.     count_t pages;
  1293.     int i;
  1294.  
  1295.     ASSERT(page == ALIGN_DOWN(page, PAGE_SIZE));
  1296.     ASSERT(count);
  1297.  
  1298.     pages = (count_t) btree_search(&a->used_space, page, &leaf);
  1299.     if (pages) {
  1300.         /*
  1301.          * We are lucky, page is the beginning of some interval.
  1302.          */
  1303.         if (count > pages) {
  1304.             return 0;
  1305.         } else if (count == pages) {
  1306.             btree_remove(&a->used_space, page, leaf);
  1307.             return 1;
  1308.         } else {
  1309.             /*
  1310.              * Find the respective interval.
  1311.              * Decrease its size and relocate its start address.
  1312.              */
  1313.             for (i = 0; i < leaf->keys; i++) {
  1314.                 if (leaf->key[i] == page) {
  1315.                     leaf->key[i] += count*PAGE_SIZE;
  1316.                     leaf->value[i] -= count;
  1317.                     return 1;
  1318.                 }
  1319.             }
  1320.             goto error;
  1321.         }
  1322.     }
  1323.  
  1324.     node = btree_leaf_node_left_neighbour(&a->used_space, leaf);
  1325.     if (node && page < leaf->key[0]) {
  1326.         __address left_pg = node->key[node->keys - 1];
  1327.         count_t left_cnt = (count_t) node->value[node->keys - 1];
  1328.  
  1329.         if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
  1330.             if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
  1331.                 /*
  1332.                  * The interval is contained in the rightmost interval
  1333.                  * of the left neighbour and can be removed by
  1334.                  * updating the size of the bigger interval.
  1335.                  */
  1336.                 node->value[node->keys - 1] -= count;
  1337.                 return 1;
  1338.             } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
  1339.                 count_t new_cnt;
  1340.                
  1341.                 /*
  1342.                  * The interval is contained in the rightmost interval
  1343.                  * of the left neighbour but its removal requires
  1344.                  * both updating the size of the original interval and
  1345.                  * also inserting a new interval.
  1346.                  */
  1347.                 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
  1348.                 node->value[node->keys - 1] -= count + new_cnt;
  1349.                 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
  1350.                 return 1;
  1351.             }
  1352.         }
  1353.         return 0;
  1354.     } else if (page < leaf->key[0]) {
  1355.         return 0;
  1356.     }
  1357.    
  1358.     if (page > leaf->key[leaf->keys - 1]) {
  1359.         __address left_pg = leaf->key[leaf->keys - 1];
  1360.         count_t left_cnt = (count_t) leaf->value[leaf->keys - 1];
  1361.  
  1362.         if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
  1363.             if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
  1364.                 /*
  1365.                  * The interval is contained in the rightmost interval
  1366.                  * of the leaf and can be removed by updating the size
  1367.                  * of the bigger interval.
  1368.                  */
  1369.                 leaf->value[leaf->keys - 1] -= count;
  1370.                 return 1;
  1371.             } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
  1372.                 count_t new_cnt;
  1373.                
  1374.                 /*
  1375.                  * The interval is contained in the rightmost interval
  1376.                  * of the leaf but its removal requires both updating
  1377.                  * the size of the original interval and
  1378.                  * also inserting a new interval.
  1379.                  */
  1380.                 new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
  1381.                 leaf->value[leaf->keys - 1] -= count + new_cnt;
  1382.                 btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
  1383.                 return 1;
  1384.             }
  1385.         }
  1386.         return 0;
  1387.     }  
  1388.    
  1389.     /*
  1390.      * The border cases have been already resolved.
  1391.      * Now the interval can be only between intervals of the leaf.
  1392.      */
  1393.     for (i = 1; i < leaf->keys - 1; i++) {
  1394.         if (page < leaf->key[i]) {
  1395.             __address left_pg = leaf->key[i - 1];
  1396.             count_t left_cnt = (count_t) leaf->value[i - 1];
  1397.  
  1398.             /*
  1399.              * Now the interval is between intervals corresponding to (i - 1) and i.
  1400.              */
  1401.             if (overlaps(left_pg, left_cnt*PAGE_SIZE, page, count*PAGE_SIZE)) {
  1402.                 if (page + count*PAGE_SIZE == left_pg + left_cnt*PAGE_SIZE) {
  1403.                     /*
  1404.                     * The interval is contained in the interval (i - 1)
  1405.                      * of the leaf and can be removed by updating the size
  1406.                      * of the bigger interval.
  1407.                      */
  1408.                     leaf->value[i - 1] -= count;
  1409.                     return 1;
  1410.                 } else if (page + count*PAGE_SIZE < left_pg + left_cnt*PAGE_SIZE) {
  1411.                     count_t new_cnt;
  1412.                
  1413.                     /*
  1414.                      * The interval is contained in the interval (i - 1)
  1415.                      * of the leaf but its removal requires both updating
  1416.                      * the size of the original interval and
  1417.                      * also inserting a new interval.
  1418.                      */
  1419.                     new_cnt = ((left_pg + left_cnt*PAGE_SIZE) - (page + count*PAGE_SIZE)) >> PAGE_WIDTH;
  1420.                     leaf->value[i - 1] -= count + new_cnt;
  1421.                     btree_insert(&a->used_space, page + count*PAGE_SIZE, (void *) new_cnt, leaf);
  1422.                     return 1;
  1423.                 }
  1424.             }
  1425.             return 0;
  1426.         }
  1427.     }
  1428.  
  1429. error:
  1430.     panic("Inconsistency detected while removing %d pages of used space from %P.\n", count, page);
  1431. }
  1432.  
  1433. /** Remove reference to address space area share info.
  1434.  *
  1435.  * If the reference count drops to 0, the sh_info is deallocated.
  1436.  *
  1437.  * @param sh_info Pointer to address space area share info.
  1438.  */
  1439. void sh_info_remove_reference(share_info_t *sh_info)
  1440. {
  1441.     bool dealloc = false;
  1442.  
  1443.     mutex_lock(&sh_info->lock);
  1444.     ASSERT(sh_info->refcount);
  1445.     if (--sh_info->refcount == 0) {
  1446.         dealloc = true;
  1447.         bool cond;
  1448.        
  1449.         /*
  1450.          * Now walk carefully the pagemap B+tree and free/remove
  1451.          * reference from all frames found there.
  1452.          */
  1453.         for (cond = true; cond;) {
  1454.             btree_node_t *node;
  1455.            
  1456.             ASSERT(!list_empty(&sh_info->pagemap.leaf_head));
  1457.             node = list_get_instance(sh_info->pagemap.leaf_head.next, btree_node_t, leaf_link);
  1458.             if ((cond = node->keys)) {
  1459.                 frame_free(ADDR2PFN((__address) node->value[0]));
  1460.                 btree_remove(&sh_info->pagemap, node->key[0], node);
  1461.             }
  1462.         }
  1463.        
  1464.     }
  1465.     mutex_unlock(&sh_info->lock);
  1466.    
  1467.     if (dealloc) {
  1468.         btree_destroy(&sh_info->pagemap);
  1469.         free(sh_info);
  1470.     }
  1471. }
  1472.  
  1473. static int anon_page_fault(as_area_t *area, __address addr, pf_access_t access);
  1474. static void anon_frame_free(as_area_t *area, __address page, __address frame);
  1475.  
  1476. /*
  1477.  * Anonymous memory backend.
  1478.  */
  1479. mem_backend_t anon_backend = {
  1480.     .backend_page_fault = anon_page_fault,
  1481.     .backend_frame_free = anon_frame_free
  1482. };
  1483.  
  1484. /** Service a page fault in the anonymous memory address space area.
  1485.  *
  1486.  * The address space area and page tables must be already locked.
  1487.  *
  1488.  * @param area Pointer to the address space area.
  1489.  * @param addr Faulting virtual address.
  1490.  * @param access Access mode that caused the fault (i.e. read/write/exec).
  1491.  *
  1492.  * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e. serviced).
  1493.  */
  1494. int anon_page_fault(as_area_t *area, __address addr, pf_access_t access)
  1495. {
  1496.     __address frame;
  1497.  
  1498.     if (area->sh_info) {
  1499.         btree_node_t *leaf;
  1500.        
  1501.         /*
  1502.          * The area is shared, chances are that the mapping can be found
  1503.          * in the pagemap of the address space area share info structure.
  1504.          * In the case that the pagemap does not contain the respective
  1505.          * mapping, a new frame is allocated and the mapping is created.
  1506.          */
  1507.         mutex_lock(&area->sh_info->lock);
  1508.         frame = (__address) btree_search(&area->sh_info->pagemap,
  1509.             ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf);
  1510.         if (!frame) {
  1511.             bool allocate = true;
  1512.             int i;
  1513.            
  1514.             /*
  1515.              * Zero can be returned as a valid frame address.
  1516.              * Just a small workaround.
  1517.              */
  1518.             for (i = 0; i < leaf->keys; i++) {
  1519.                 if (leaf->key[i] == ALIGN_DOWN(addr, PAGE_SIZE)) {
  1520.                     allocate = false;
  1521.                     break;
  1522.                 }
  1523.             }
  1524.             if (allocate) {
  1525.                 frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
  1526.                 memsetb(PA2KA(frame), FRAME_SIZE, 0);
  1527.                
  1528.                 /*
  1529.                  * Insert the address of the newly allocated frame to the pagemap.
  1530.                  */
  1531.                 btree_insert(&area->sh_info->pagemap, ALIGN_DOWN(addr, PAGE_SIZE) - area->base, (void *) frame, leaf);
  1532.             }
  1533.         }
  1534.         mutex_unlock(&area->sh_info->lock);
  1535.     } else {
  1536.  
  1537.         /*
  1538.          * In general, there can be several reasons that
  1539.          * can have caused this fault.
  1540.          *
  1541.          * - non-existent mapping: the area is an anonymous
  1542.          *   area (e.g. heap or stack) and so far has not been
  1543.          *   allocated a frame for the faulting page
  1544.          *
  1545.          * - non-present mapping: another possibility,
  1546.          *   currently not implemented, would be frame
  1547.          *   reuse; when this becomes a possibility,
  1548.          *   do not forget to distinguish between
  1549.          *   the different causes
  1550.          */
  1551.         frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
  1552.         memsetb(PA2KA(frame), FRAME_SIZE, 0);
  1553.     }
  1554.    
  1555.     /*
  1556.      * Map 'page' to 'frame'.
  1557.      * Note that TLB shootdown is not attempted as only new information is being
  1558.      * inserted into page tables.
  1559.      */
  1560.     page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
  1561.     if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
  1562.         panic("Could not insert used space.\n");
  1563.        
  1564.     return AS_PF_OK;
  1565. }
  1566.  
  1567. /** Free a frame that is backed by the anonymous memory backend.
  1568.  *
  1569.  * The address space area and page tables must be already locked.
  1570.  *
  1571.  * @param area Ignored.
  1572.  * @param page Ignored.
  1573.  * @param frame Frame to be released.
  1574.  */
  1575. void anon_frame_free(as_area_t *area, __address page, __address frame)
  1576. {
  1577.     frame_free(ADDR2PFN(frame));
  1578. }
  1579.  
  1580. /*
  1581.  * Address space related syscalls.
  1582.  */
  1583.  
  1584. /** Wrapper for as_area_create(). */
  1585. __native sys_as_area_create(__address address, size_t size, int flags)
  1586. {
  1587.     if (as_area_create(AS, flags, size, address, AS_AREA_ATTR_NONE, &anon_backend, NULL))
  1588.         return (__native) address;
  1589.     else
  1590.         return (__native) -1;
  1591. }
  1592.  
  1593. /** Wrapper for as_area_resize. */
  1594. __native sys_as_area_resize(__address address, size_t size, int flags)
  1595. {
  1596.     return (__native) as_area_resize(AS, address, size, 0);
  1597. }
  1598.  
  1599. /** Wrapper for as_area_destroy. */
  1600. __native sys_as_area_destroy(__address address)
  1601. {
  1602.     return (__native) as_area_destroy(AS, address);
  1603. }
  1604.