Subversion Repositories HelenOS-historic

Rev

Rev 741 | 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.  * ASID management.
  31.  *
  32.  * Modern processor architectures optimize TLB utilization
  33.  * by using ASIDs (a.k.a. memory contexts on sparc64 and
  34.  * region identifiers on ia64). These ASIDs help to associate
  35.  * each TLB item with an address space, thus making
  36.  * finer-grained TLB invalidation possible.
  37.  *
  38.  * Unfortunatelly, there are usually less ASIDs available than
  39.  * there can be unique as_t structures (i.e. address spaces
  40.  * recognized by the kernel).
  41.  *
  42.  * When system runs short of ASIDs, it will attempt to steal
  43.  * ASID from an address space that has not been active for
  44.  * a while.
  45.  *
  46.  * Architectures that don't have hardware support for address
  47.  * spaces do not compile with this file.
  48.  */
  49.  
  50. #include <mm/asid.h>
  51. #include <mm/as.h>
  52. #include <mm/tlb.h>
  53. #include <arch/mm/asid.h>
  54. #include <synch/spinlock.h>
  55. #include <arch.h>
  56. #include <list.h>
  57. #include <debug.h>
  58.  
  59. /**
  60.  * asidlock protects both the asids_allocated counter
  61.  * and the list of address spaces that were already
  62.  * assigned ASID.
  63.  */
  64. SPINLOCK_INITIALIZE(asidlock);
  65.  
  66. static count_t asids_allocated = 0;
  67.  
  68. /**
  69.  * List of address spaces with assigned ASID.
  70.  * When the system runs short of allocable
  71.  * ASIDS, inactive address spaces are guaranteed
  72.  * to be at the beginning of the list.
  73.  */
  74. LIST_INITIALIZE(as_with_asid_head);
  75.  
  76.  
  77. /** Allocate free address space identifier.
  78.  *
  79.  * This code depends on the fact that ASIDS_ALLOCABLE
  80.  * is greater than number of supported CPUs.
  81.  *
  82.  * @return New ASID.
  83.  */
  84. asid_t asid_get(void)
  85. {
  86.     ipl_t ipl;
  87.     asid_t asid;
  88.     link_t *tmp;
  89.     as_t *as;
  90.  
  91.     /*
  92.      * Check if there is an unallocated ASID.
  93.      */
  94.    
  95.     ipl = interrupts_disable();
  96.     spinlock_lock(&asidlock);
  97.     if (asids_allocated == ASIDS_ALLOCABLE) {
  98.  
  99.         /*
  100.          * All ASIDs are already allocated.
  101.          * Resort to stealing.
  102.          */
  103.        
  104.         /*
  105.          * Remove the first item on the list.
  106.          * It is guaranteed to belong to an
  107.          * inactive address space.
  108.          */
  109.         tmp = as_with_asid_head.next;
  110.         ASSERT(tmp != &as_with_asid_head);
  111.         list_remove(tmp);
  112.        
  113.         as = list_get_instance(tmp, as_t, as_with_asid_link);
  114.         spinlock_lock(&as->lock);
  115.  
  116.         /*
  117.          * Steal the ASID.
  118.          * Note that the stolen ASID is not active.
  119.          */
  120.         asid = as->asid;
  121.         ASSERT(asid != ASID_INVALID);
  122.  
  123.         /*
  124.          * Get the system rid of the stolen ASID.
  125.          */
  126.         tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
  127.         tlb_shootdown_finalize();
  128.         as->asid = ASID_INVALID;
  129.        
  130.         spinlock_unlock(&as->lock);
  131.     } else {
  132.  
  133.         /*
  134.          * There is at least one unallocated ASID.
  135.          * Find it and assign it.
  136.          */
  137.  
  138.         asid = asid_find_free();
  139.         asids_allocated++;
  140.     }
  141.    
  142.     spinlock_unlock(&asidlock);
  143.     interrupts_restore(ipl);
  144.    
  145.     return asid;
  146. }
  147.  
  148. /** Release address space identifier.
  149.  *
  150.  * This code relies on architecture
  151.  * dependent functionality.
  152.  *
  153.  * @param asid ASID to be released.
  154.  */
  155. void asid_put(asid_t asid)
  156. {
  157.     ipl_t ipl;
  158.  
  159.     ipl = interrupts_disable();
  160.     spinlock_lock(&asidlock);
  161.  
  162.     asids_allocated--;
  163.     asid_put_arch(asid);
  164.    
  165.     spinlock_unlock(&asidlock);
  166.     interrupts_restore(ipl);
  167. }
  168.  
  169. /** Install ASID.
  170.  *
  171.  * This function is to be executed on each address space switch.
  172.  *
  173.  * @param as Address space.
  174.  */
  175. void asid_install(as_t *as)
  176. {
  177.     ipl_t ipl;
  178.    
  179.     ipl = interrupts_disable();
  180.     spinlock_lock(&asidlock);
  181.     spinlock_lock(&as->lock);
  182.    
  183.     if (as->asid != ASID_KERNEL) {
  184.         if (as->asid != ASID_INVALID) {
  185.             /*
  186.              * This address space has valid ASID.
  187.              * Remove 'as' from the list of address spaces
  188.              * with assigned ASID, so that it can be later
  189.              * appended to the tail of the same list.
  190.              * This is to prevent stealing of ASIDs from
  191.              * recently installed address spaces.
  192.              */
  193.             list_remove(&as->as_with_asid_link);
  194.         } else {
  195.             spinlock_unlock(&as->lock);
  196.             spinlock_unlock(&asidlock);
  197.    
  198.             /*
  199.              * This address space doesn't have ASID assigned.
  200.              * It was stolen or the address space is being
  201.              * installed for the first time.
  202.              * Allocate new ASID for it.
  203.              */
  204.             as->asid = asid_get();
  205.             spinlock_lock(&asidlock);
  206.             spinlock_lock(&as->lock);
  207.         }
  208.    
  209.         /*
  210.          * Now it is sure that 'as' has ASID.
  211.          * It is therefore appended to the list
  212.          * of address spaces from which it can
  213.          * be stolen.
  214.          */
  215.         list_append(&as->as_with_asid_link, &as_with_asid_head);
  216.     }
  217.    
  218.     spinlock_unlock(&as->lock);
  219.     spinlock_unlock(&asidlock);
  220.     interrupts_restore(ipl);
  221. }
  222.