Subversion Repositories HelenOS

Rev

Rev 756 | Rev 792 | 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. #include <genarch/mm/page_ht.h>
  30. #include <mm/page.h>
  31. #include <mm/frame.h>
  32. #include <mm/heap.h>
  33. #include <mm/as.h>
  34. #include <arch/mm/asid.h>
  35. #include <arch/types.h>
  36. #include <typedefs.h>
  37. #include <arch/asm.h>
  38. #include <synch/spinlock.h>
  39. #include <arch.h>
  40. #include <debug.h>
  41. #include <memstr.h>
  42.  
  43. /**
  44.  * This lock protects the page hash table.
  45.  */
  46. SPINLOCK_INITIALIZE(page_ht_lock);
  47.  
  48. /**
  49.  * Page hash table pointer.
  50.  * The page hash table may be accessed only when page_ht_lock is held.
  51.  */
  52. pte_t *page_ht = NULL;
  53.  
  54. static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags);
  55. static pte_t *ht_mapping_find(as_t *as, __address page);
  56.  
  57. page_operations_t page_ht_operations = {
  58.     .mapping_insert = ht_mapping_insert,
  59.     .mapping_find = ht_mapping_find
  60. };
  61.  
  62. /** Map page to frame using page hash table.
  63.  *
  64.  * Map virtual address 'page' to physical address 'frame'
  65.  * using 'flags'.
  66.  *
  67.  * The address space must be locked and interruptsmust be disabled.
  68.  *
  69.  * @param as Address space to which page belongs.
  70.  * @param page Virtual address of the page to be mapped.
  71.  * @param frame Physical address of memory frame to which the mapping is done.
  72.  * @param flags Flags to be used for mapping.
  73.  */
  74. void ht_mapping_insert(as_t *as, __address page, __address frame, int flags)
  75. {
  76.     pte_t *t, *u;
  77.     ipl_t ipl;
  78.    
  79.     ipl = interrupts_disable();
  80.     spinlock_lock(&page_ht_lock);
  81.  
  82.     t = HT_HASH(page, as->asid);
  83.     if (!HT_SLOT_EMPTY(t)) {
  84.    
  85.         /*
  86.          * The slot is occupied.
  87.          * Walk through the collision chain and append the mapping to its end.
  88.          */
  89.          
  90.         do {
  91.             u = t;
  92.             if (HT_COMPARE(page, as->asid, t)) {
  93.                 /*
  94.                  * Nothing to do,
  95.                  * the record is already there.
  96.                  */
  97.                 spinlock_unlock(&page_ht_lock);
  98.                 interrupts_restore(ipl);
  99.                 return;
  100.             }
  101.         } while ((t = HT_GET_NEXT(t)));
  102.    
  103.         t = (pte_t *) malloc(sizeof(pte_t));    /* FIXME: use slab allocator for this */
  104.         if (!t)
  105.             panic("could not allocate memory\n");
  106.  
  107.         HT_SET_NEXT(u, t);
  108.     }
  109.    
  110.     HT_SET_RECORD(t, page, as->asid, frame, flags);
  111.     HT_SET_NEXT(t, NULL);
  112.    
  113.     spinlock_unlock(&page_ht_lock);
  114.     interrupts_restore(ipl);
  115. }
  116.  
  117. /** Find mapping for virtual page in page hash table.
  118.  *
  119.  * Find mapping for virtual page.
  120.  *
  121.  * The address space must be locked and interrupts must be disabled.
  122.  *
  123.  * @param as Address space to wich page belongs.
  124.  * @param page Virtual page.
  125.  *
  126.  * @return NULL if there is no such mapping; requested mapping otherwise.
  127.  */
  128. pte_t *ht_mapping_find(as_t *as, __address page)
  129. {
  130.     pte_t *t;
  131.    
  132.     spinlock_lock(&page_ht_lock);
  133.     t = HT_HASH(page, as->asid);
  134.     if (!HT_SLOT_EMPTY(t)) {
  135.         while (!HT_COMPARE(page, as->asid, t) && HT_GET_NEXT(t))
  136.             t = HT_GET_NEXT(t);
  137.         t = HT_COMPARE(page, as->asid, t) ? t : NULL;
  138.     } else {
  139.         t = NULL;
  140.     }
  141.     spinlock_unlock(&page_ht_lock);
  142.     return t;
  143. }
  144.  
  145. /** Invalidate page hash table.
  146.  *
  147.  * Interrupts must be disabled.
  148.  */
  149. void ht_invalidate_all(void)
  150. {
  151.     pte_t *t, *u;
  152.     int i;
  153.    
  154.     spinlock_lock(&page_ht_lock);
  155.     for (i = 0; i < HT_ENTRIES; i++) {
  156.         if (!HT_SLOT_EMPTY(&page_ht[i])) {
  157.             t = HT_GET_NEXT(&page_ht[i]);
  158.             while (t) {
  159.                 u = t;
  160.                 t = HT_GET_NEXT(t);
  161.                 free(u);        /* FIXME: use slab allocator for this */
  162.             }
  163.             HT_INVALIDATE_SLOT(&page_ht[i]);
  164.         }
  165.     }
  166.     spinlock_unlock(&page_ht_lock);
  167. }
  168.