source: mainline/genarch/src/mm/page_ht.c@ 9b2729c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9b2729c was f5935ed, checked in by Jakub Jermar <jakub@…>, 20 years ago

Use hash_table_get_instance instead of list_get_instance.
Rename page_operations to page_mapping_operations.
Rename page_pt_operations to pt_mapping_operations.
Rename page_ht_operations to ht_mapping_operations.

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[6d7ffa65]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>
[c7ec94a4]31#include <arch/mm/page.h>
[6d7ffa65]32#include <mm/frame.h>
[0c0410b]33#include <mm/heap.h>
[fc1e4f6]34#include <mm/as.h>
[677a6d5]35#include <arch/mm/asid.h>
[6d7ffa65]36#include <arch/types.h>
37#include <typedefs.h>
38#include <arch/asm.h>
[2a003d5b]39#include <synch/spinlock.h>
40#include <arch.h>
[0c0410b]41#include <debug.h>
[ef67bab]42#include <memstr.h>
[c7ec94a4]43#include <adt/hash_table.h>
44
45static index_t hash(__native key[]);
46static bool compare(__native key[], count_t keys, link_t *item);
47static void remove_callback(link_t *item);
48
49static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags);
50static pte_t *ht_mapping_find(as_t *as, __address page);
[6d7ffa65]51
[2a003d5b]52/**
[ef67bab]53 * This lock protects the page hash table.
[2a003d5b]54 */
55SPINLOCK_INITIALIZE(page_ht_lock);
56
57/**
[c7ec94a4]58 * Page hash table.
[2a003d5b]59 * The page hash table may be accessed only when page_ht_lock is held.
60 */
[c7ec94a4]61hash_table_t page_ht;
[2a003d5b]62
[c7ec94a4]63/** Hash table operations for page hash table. */
64hash_table_operations_t ht_operations = {
65 .hash = hash,
66 .compare = compare,
67 .remove_callback = remove_callback
68};
[6d7ffa65]69
[f5935ed]70/** Page mapping operations for page hash table architectures. */
71page_mapping_operations_t ht_mapping_operations = {
[6d7ffa65]72 .mapping_insert = ht_mapping_insert,
[071a8ae6]73 .mapping_find = ht_mapping_find
[6d7ffa65]74};
75
[c7ec94a4]76/** Compute page hash table index.
77 *
78 * @param key Array of two keys (i.e. page and address space).
79 *
80 * @return Index into page hash table.
81 */
82index_t hash(__native key[])
83{
84 as_t *as = (as_t *) key[KEY_AS];
85 __address page = (__address) key[KEY_PAGE];
86 index_t index;
87
88 /*
89 * Virtual page addresses have roughly the same probability
90 * of occurring. Least significant bits of VPN compose the
91 * hash index.
92 */
93 index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1));
94
95 /*
96 * Address space structures are likely to be allocated from
97 * similar addresses. Least significant bits compose the
98 * hash index.
99 */
100 index |= ((__native) as) & (PAGE_HT_ENTRIES-1);
101
102 return index;
103}
104
105/** Compare page hash table item with page and/or address space.
106 *
107 * @param key Array of one or two keys (i.e. page and/or address space).
108 * @param keys Number of keys passed.
109 * @param item Item to compare the keys with.
110 *
111 * @return true on match, false otherwise.
112 */
113bool compare(__native key[], count_t keys, link_t *item)
114{
115 pte_t *t;
116
117 ASSERT(item);
118 ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
119
120 /*
121 * Convert item to PTE.
122 */
[f5935ed]123 t = hash_table_get_instance(item, pte_t, link);
[c7ec94a4]124
125 if (keys == PAGE_HT_KEYS) {
126 return (key[KEY_AS] == (__address) t->as) && (key[KEY_PAGE] == t->page);
127 } else {
128 return (key[KEY_AS] == (__address) t->as);
129 }
130}
131
132/** Callback on page hash table item removal.
133 *
134 * @param item Page hash table item being removed.
135 */
136void remove_callback(link_t *item)
137{
138 pte_t *t;
139
140 ASSERT(item);
141
142 /*
143 * Convert item to PTE.
144 */
[f5935ed]145 t = hash_table_get_instance(item, pte_t, link);
[c7ec94a4]146
147 free(t);
148}
149
[6d7ffa65]150/** Map page to frame using page hash table.
151 *
152 * Map virtual address 'page' to physical address 'frame'
[ef67bab]153 * using 'flags'.
[6d7ffa65]154 *
[ef67bab]155 * The address space must be locked and interruptsmust be disabled.
156 *
157 * @param as Address space to which page belongs.
[6d7ffa65]158 * @param page Virtual address of the page to be mapped.
159 * @param frame Physical address of memory frame to which the mapping is done.
160 * @param flags Flags to be used for mapping.
161 */
[ef67bab]162void ht_mapping_insert(as_t *as, __address page, __address frame, int flags)
[6d7ffa65]163{
[c7ec94a4]164 pte_t *t;
[2a003d5b]165 ipl_t ipl;
[c7ec94a4]166 __native key[2] = { (__address) as, page };
[2a003d5b]167
168 ipl = interrupts_disable();
169 spinlock_lock(&page_ht_lock);
[fc1e4f6]170
[c7ec94a4]171 if (!hash_table_find(&page_ht, key)) {
172 t = (pte_t *) malloc(sizeof(pte_t));
173 ASSERT(t != NULL);
[2a003d5b]174
[c7ec94a4]175 hash_table_insert(&page_ht, key, &t->link);
[0c0410b]176 }
[2a003d5b]177
178 spinlock_unlock(&page_ht_lock);
179 interrupts_restore(ipl);
[6d7ffa65]180}
181
182/** Find mapping for virtual page in page hash table.
183 *
184 * Find mapping for virtual page.
185 *
[ef67bab]186 * The address space must be locked and interrupts must be disabled.
[2a003d5b]187 *
[ef67bab]188 * @param as Address space to wich page belongs.
[6d7ffa65]189 * @param page Virtual page.
190 *
[0c0410b]191 * @return NULL if there is no such mapping; requested mapping otherwise.
[6d7ffa65]192 */
[ef67bab]193pte_t *ht_mapping_find(as_t *as, __address page)
[6d7ffa65]194{
[c7ec94a4]195 link_t *hlp;
196 pte_t *t = NULL;
197 __native key[2] = { (__address) as, page };
[0c0410b]198
[2a003d5b]199 spinlock_lock(&page_ht_lock);
200
[c7ec94a4]201 hlp = hash_table_find(&page_ht, key);
202 if (hlp)
[f5935ed]203 t = hash_table_get_instance(hlp, pte_t, link);
[c7ec94a4]204
[2a003d5b]205 spinlock_unlock(&page_ht_lock);
[c7ec94a4]206 return t;
[6d7ffa65]207}
Note: See TracBrowser for help on using the repository browser.