source: mainline/kernel/genarch/src/mm/page_ht.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was 80a3bd9, checked in by Jakub Jermar <jakub@…>, 16 years ago

Initialize the Accessed and Dirty bits when the page hash table is used.

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[6d7ffa65]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[6d7ffa65]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
[f47fd19]29/** @addtogroup genarchmm
[b45c443]30 * @{
31 */
32
[0f27b4c]33/**
[b45c443]34 * @file
[0f27b4c]35 * @brief Virtual Address Translation (VAT) for global page hash table.
36 */
37
[6d7ffa65]38#include <genarch/mm/page_ht.h>
39#include <mm/page.h>
[c7ec94a4]40#include <arch/mm/page.h>
[6d7ffa65]41#include <mm/frame.h>
[5e3757d]42#include <mm/slab.h>
[fc1e4f6]43#include <mm/as.h>
[677a6d5]44#include <arch/mm/asid.h>
[6d7ffa65]45#include <arch/types.h>
46#include <arch/asm.h>
[2a003d5b]47#include <synch/spinlock.h>
48#include <arch.h>
[0c0410b]49#include <debug.h>
[ef67bab]50#include <memstr.h>
[c7ec94a4]51#include <adt/hash_table.h>
[a2a46ba]52#include <align.h>
[c7ec94a4]53
[98000fb]54static size_t hash(unative_t key[]);
55static bool compare(unative_t key[], size_t keys, link_t *item);
[c7ec94a4]56static void remove_callback(link_t *item);
57
[2057572]58static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
59 int flags);
[7f1c620]60static void ht_mapping_remove(as_t *as, uintptr_t page);
61static pte_t *ht_mapping_find(as_t *as, uintptr_t page);
[6d7ffa65]62
[2a003d5b]63/**
[2299914]64 * This lock protects the page hash table. It must be acquired
65 * after address space lock and after any address space area
66 * locks.
[2a003d5b]67 */
[1068f6a]68mutex_t page_ht_lock;
[2a003d5b]69
70/**
[c7ec94a4]71 * Page hash table.
[2a003d5b]72 * The page hash table may be accessed only when page_ht_lock is held.
73 */
[c7ec94a4]74hash_table_t page_ht;
[2a003d5b]75
[c7ec94a4]76/** Hash table operations for page hash table. */
77hash_table_operations_t ht_operations = {
78 .hash = hash,
79 .compare = compare,
80 .remove_callback = remove_callback
81};
[6d7ffa65]82
[f5935ed]83/** Page mapping operations for page hash table architectures. */
84page_mapping_operations_t ht_mapping_operations = {
[6d7ffa65]85 .mapping_insert = ht_mapping_insert,
[8f00329]86 .mapping_remove = ht_mapping_remove,
[071a8ae6]87 .mapping_find = ht_mapping_find
[6d7ffa65]88};
89
[c7ec94a4]90/** Compute page hash table index.
91 *
92 * @param key Array of two keys (i.e. page and address space).
93 *
94 * @return Index into page hash table.
95 */
[98000fb]96size_t hash(unative_t key[])
[c7ec94a4]97{
98 as_t *as = (as_t *) key[KEY_AS];
[7f1c620]99 uintptr_t page = (uintptr_t) key[KEY_PAGE];
[98000fb]100 size_t index;
[c7ec94a4]101
102 /*
103 * Virtual page addresses have roughly the same probability
104 * of occurring. Least significant bits of VPN compose the
105 * hash index.
106 */
[2057572]107 index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
[c7ec94a4]108
109 /*
110 * Address space structures are likely to be allocated from
111 * similar addresses. Least significant bits compose the
112 * hash index.
113 */
[2057572]114 index |= ((unative_t) as) & (PAGE_HT_ENTRIES - 1);
[c7ec94a4]115
116 return index;
117}
118
119/** Compare page hash table item with page and/or address space.
120 *
121 * @param key Array of one or two keys (i.e. page and/or address space).
122 * @param keys Number of keys passed.
123 * @param item Item to compare the keys with.
124 *
125 * @return true on match, false otherwise.
126 */
[98000fb]127bool compare(unative_t key[], size_t keys, link_t *item)
[c7ec94a4]128{
129 pte_t *t;
130
131 ASSERT(item);
132 ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
133
134 /*
135 * Convert item to PTE.
136 */
[f5935ed]137 t = hash_table_get_instance(item, pte_t, link);
[c7ec94a4]138
139 if (keys == PAGE_HT_KEYS) {
[2057572]140 return (key[KEY_AS] == (uintptr_t) t->as) &&
141 (key[KEY_PAGE] == t->page);
[c7ec94a4]142 } else {
[7f1c620]143 return (key[KEY_AS] == (uintptr_t) t->as);
[c7ec94a4]144 }
145}
146
147/** Callback on page hash table item removal.
148 *
149 * @param item Page hash table item being removed.
150 */
151void remove_callback(link_t *item)
152{
153 pte_t *t;
154
155 ASSERT(item);
156
157 /*
158 * Convert item to PTE.
159 */
[f5935ed]160 t = hash_table_get_instance(item, pte_t, link);
[c7ec94a4]161
162 free(t);
163}
164
[6d7ffa65]165/** Map page to frame using page hash table.
166 *
[9179d0a]167 * Map virtual address page to physical address frame
168 * using flags.
[6d7ffa65]169 *
[2299914]170 * The page table must be locked and interrupts must be disabled.
[ef67bab]171 *
[9179d0a]172 * @param as Address space to which page belongs.
[6d7ffa65]173 * @param page Virtual address of the page to be mapped.
174 * @param frame Physical address of memory frame to which the mapping is done.
175 * @param flags Flags to be used for mapping.
176 */
[7f1c620]177void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
[6d7ffa65]178{
[c7ec94a4]179 pte_t *t;
[2057572]180 unative_t key[2] = {
181 (uintptr_t) as,
182 page = ALIGN_DOWN(page, PAGE_SIZE)
183 };
[2a003d5b]184
[c7ec94a4]185 if (!hash_table_find(&page_ht, key)) {
[7e4e532]186 t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
[c7ec94a4]187 ASSERT(t != NULL);
[9ad03fe]188
189 t->g = (flags & PAGE_GLOBAL) != 0;
190 t->x = (flags & PAGE_EXEC) != 0;
191 t->w = (flags & PAGE_WRITE) != 0;
192 t->k = !(flags & PAGE_USER);
193 t->c = (flags & PAGE_CACHEABLE) != 0;
194 t->p = !(flags & PAGE_NOT_PRESENT);
[80a3bd9]195 t->a = false;
196 t->d = false;
[9ad03fe]197
198 t->as = as;
[0ee077ee]199 t->page = ALIGN_DOWN(page, PAGE_SIZE);
200 t->frame = ALIGN_DOWN(frame, FRAME_SIZE);
[9ad03fe]201
[c7ec94a4]202 hash_table_insert(&page_ht, key, &t->link);
[0c0410b]203 }
[6d7ffa65]204}
205
[8f00329]206/** Remove mapping of page from page hash table.
207 *
[9179d0a]208 * Remove any mapping of page within address space as.
[8f00329]209 * TLB shootdown should follow in order to make effects of
210 * this call visible.
211 *
[2299914]212 * The page table must be locked and interrupts must be disabled.
[8f00329]213 *
214 * @param as Address space to wich page belongs.
215 * @param page Virtual address of the page to be demapped.
216 */
[7f1c620]217void ht_mapping_remove(as_t *as, uintptr_t page)
[8f00329]218{
[2057572]219 unative_t key[2] = {
220 (uintptr_t) as,
221 page = ALIGN_DOWN(page, PAGE_SIZE)
222 };
[8f00329]223
224 /*
225 * Note that removed PTE's will be freed
226 * by remove_callback().
227 */
228 hash_table_remove(&page_ht, key, 2);
229}
230
231
[6d7ffa65]232/** Find mapping for virtual page in page hash table.
233 *
234 * Find mapping for virtual page.
235 *
[2299914]236 * The page table must be locked and interrupts must be disabled.
[2a003d5b]237 *
[9179d0a]238 * @param as Address space to wich page belongs.
[6d7ffa65]239 * @param page Virtual page.
240 *
[0c0410b]241 * @return NULL if there is no such mapping; requested mapping otherwise.
[6d7ffa65]242 */
[7f1c620]243pte_t *ht_mapping_find(as_t *as, uintptr_t page)
[6d7ffa65]244{
[c7ec94a4]245 link_t *hlp;
246 pte_t *t = NULL;
[2057572]247 unative_t key[2] = {
248 (uintptr_t) as,
249 page = ALIGN_DOWN(page, PAGE_SIZE)
250 };
[0c0410b]251
[c7ec94a4]252 hlp = hash_table_find(&page_ht, key);
253 if (hlp)
[f5935ed]254 t = hash_table_get_instance(hlp, pte_t, link);
[c7ec94a4]255
256 return t;
[6d7ffa65]257}
[b45c443]258
[f47fd19]259/** @}
[b45c443]260 */
Note: See TracBrowser for help on using the repository browser.