Subversion Repositories HelenOS-historic

Rev

Rev 823 | Rev 1380 | 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    asid.c
  31.  * @brief   ASID management.
  32.  *
  33.  * Modern processor architectures optimize TLB utilization
  34.  * by using ASIDs (a.k.a. memory contexts on sparc64 and
  35.  * region identifiers on ia64). These ASIDs help to associate
  36.  * each TLB item with an address space, thus making
  37.  * finer-grained TLB invalidation possible.
  38.  *
  39.  * Unfortunatelly, there are usually less ASIDs available than
  40.  * there can be unique as_t structures (i.e. address spaces
  41.  * recognized by the kernel).
  42.  *
  43.  * When system runs short of ASIDs, it will attempt to steal
  44.  * ASID from an address space that has not been active for
  45.  * a while.
  46.  *
  47.  * This code depends on the fact that ASIDS_ALLOCABLE
  48.  * is greater than number of supported CPUs (i.e. the
  49.  * amount of concurently active address spaces).
  50.  *
  51.  * Architectures that don't have hardware support for address
  52.  * spaces do not compile with this file.
  53.  */
  54.  
  55. #include <mm/asid.h>
  56. #include <mm/as.h>
  57. #include <mm/tlb.h>
  58. #include <arch/mm/asid.h>
  59. #include <synch/spinlock.h>
  60. #include <arch.h>
  61. #include <adt/list.h>
  62. #include <debug.h>
  63.  
  64. /**
  65.  * asidlock protects the asids_allocated counter.
  66.  */
  67. SPINLOCK_INITIALIZE(asidlock);
  68.  
  69. static count_t asids_allocated = 0;
  70.  
  71. /** Allocate free address space identifier.
  72.  *
  73.  * Interrupts must be disabled and as_lock must be held
  74.  * prior to this call
  75.  *
  76.  * @return New ASID.
  77.  */
  78. asid_t asid_get(void)
  79. {
  80.     asid_t asid;
  81.     link_t *tmp;
  82.     as_t *as;
  83.  
  84.     /*
  85.      * Check if there is an unallocated ASID.
  86.      */
  87.    
  88.     spinlock_lock(&asidlock);
  89.     if (asids_allocated == ASIDS_ALLOCABLE) {
  90.  
  91.         /*
  92.          * All ASIDs are already allocated.
  93.          * Resort to stealing.
  94.          */
  95.        
  96.         /*
  97.          * Remove the first item on the list.
  98.          * It is guaranteed to belong to an
  99.          * inactive address space.
  100.          */
  101.         ASSERT(!list_empty(&inactive_as_with_asid_head));
  102.         tmp = inactive_as_with_asid_head.next;
  103.         list_remove(tmp);
  104.        
  105.         as = list_get_instance(tmp, as_t, inactive_as_with_asid_link);
  106.         spinlock_lock(&as->lock);
  107.  
  108.         /*
  109.          * Steal the ASID.
  110.          * Note that the stolen ASID is not active.
  111.          */
  112.         asid = as->asid;
  113.         ASSERT(asid != ASID_INVALID);
  114.  
  115.         /*
  116.          * Notify the address space from wich the ASID
  117.          * was stolen by invalidating its asid member.
  118.          */
  119.         as->asid = ASID_INVALID;
  120.         spinlock_unlock(&as->lock);
  121.  
  122.         /*
  123.          * Get the system rid of the stolen ASID.
  124.          */
  125.         tlb_shootdown_start(TLB_INVL_ASID, asid, 0, 0);
  126.         tlb_shootdown_finalize();
  127.         tlb_invalidate_asid(asid);
  128.     } else {
  129.  
  130.         /*
  131.          * There is at least one unallocated ASID.
  132.          * Find it and assign it.
  133.          */
  134.  
  135.         asid = asid_find_free();
  136.         asids_allocated++;
  137.     }
  138.    
  139.     spinlock_unlock(&asidlock);
  140.    
  141.     return asid;
  142. }
  143.  
  144. /** Release address space identifier.
  145.  *
  146.  * This code relies on architecture
  147.  * dependent functionality.
  148.  *
  149.  * @param asid ASID to be released.
  150.  */
  151. void asid_put(asid_t asid)
  152. {
  153.     ipl_t ipl;
  154.  
  155.     ipl = interrupts_disable();
  156.     spinlock_lock(&asidlock);
  157.  
  158.     asids_allocated--;
  159.     asid_put_arch(asid);
  160.    
  161.     spinlock_unlock(&asidlock);
  162.     interrupts_restore(ipl);
  163. }
  164.