Subversion Repositories HelenOS-historic

Rev

Rev 823 | Rev 922 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
684 jermar 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>
792 jermar 31
#include <arch/mm/page.h>
684 jermar 32
#include <mm/frame.h>
815 jermar 33
#include <mm/slab.h>
755 jermar 34
#include <mm/as.h>
687 jermar 35
#include <arch/mm/asid.h>
684 jermar 36
#include <arch/types.h>
37
#include <typedefs.h>
38
#include <arch/asm.h>
746 jermar 39
#include <synch/spinlock.h>
40
#include <arch.h>
699 jermar 41
#include <debug.h>
756 jermar 42
#include <memstr.h>
792 jermar 43
#include <adt/hash_table.h>
684 jermar 44
 
792 jermar 45
static index_t hash(__native key[]);
46
static bool compare(__native key[], count_t keys, link_t *item);
47
static void remove_callback(link_t *item);
48
 
49
static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags);
826 jermar 50
static void ht_mapping_remove(as_t *as, __address page);
792 jermar 51
static pte_t *ht_mapping_find(as_t *as, __address page);
52
 
746 jermar 53
/**
756 jermar 54
 * This lock protects the page hash table.
746 jermar 55
 */
56
SPINLOCK_INITIALIZE(page_ht_lock);
57
 
58
/**
792 jermar 59
 * Page hash table.
746 jermar 60
 * The page hash table may be accessed only when page_ht_lock is held.
61
 */
792 jermar 62
hash_table_t page_ht;
746 jermar 63
 
792 jermar 64
/** Hash table operations for page hash table. */
65
hash_table_operations_t ht_operations = {
66
    .hash = hash,
67
    .compare = compare,
68
    .remove_callback = remove_callback
69
};
684 jermar 70
 
793 jermar 71
/** Page mapping operations for page hash table architectures. */
72
page_mapping_operations_t ht_mapping_operations = {
684 jermar 73
    .mapping_insert = ht_mapping_insert,
826 jermar 74
    .mapping_remove = ht_mapping_remove,
757 jermar 75
    .mapping_find = ht_mapping_find
684 jermar 76
};
77
 
792 jermar 78
/** Compute page hash table index.
79
 *
80
 * @param key Array of two keys (i.e. page and address space).
81
 *
82
 * @return Index into page hash table.
83
 */
84
index_t hash(__native key[])
85
{
86
    as_t *as = (as_t *) key[KEY_AS];
87
    __address page = (__address) key[KEY_PAGE];
88
    index_t index;
89
 
90
    /*
91
     * Virtual page addresses have roughly the same probability
92
     * of occurring. Least significant bits of VPN compose the
93
     * hash index.
94
     */
95
    index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1));
96
 
97
    /*
98
     * Address space structures are likely to be allocated from
99
     * similar addresses. Least significant bits compose the
100
     * hash index.
101
     */
102
    index |= ((__native) as) & (PAGE_HT_ENTRIES-1);
103
 
104
    return index;
105
}
106
 
107
/** Compare page hash table item with page and/or address space.
108
 *
109
 * @param key Array of one or two keys (i.e. page and/or address space).
110
 * @param keys Number of keys passed.
111
 * @param item Item to compare the keys with.
112
 *
113
 * @return true on match, false otherwise.
114
 */
115
bool compare(__native key[], count_t keys, link_t *item)
116
{
117
    pte_t *t;
118
 
119
    ASSERT(item);
120
    ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
121
 
122
    /*
123
     * Convert item to PTE.
124
     */
793 jermar 125
    t = hash_table_get_instance(item, pte_t, link);
792 jermar 126
 
127
    if (keys == PAGE_HT_KEYS) {
128
        return (key[KEY_AS] == (__address) t->as) && (key[KEY_PAGE] == t->page);
129
    } else {
130
        return (key[KEY_AS] == (__address) t->as);
131
    }
132
}
133
 
134
/** Callback on page hash table item removal.
135
 *
136
 * @param item Page hash table item being removed.
137
 */
138
void remove_callback(link_t *item)
139
{
140
    pte_t *t;
141
 
142
    ASSERT(item);
143
 
144
    /*
145
     * Convert item to PTE.
146
     */
793 jermar 147
    t = hash_table_get_instance(item, pte_t, link);
792 jermar 148
 
149
    free(t);
150
}
151
 
684 jermar 152
/** Map page to frame using page hash table.
153
 *
154
 * Map virtual address 'page' to physical address 'frame'
756 jermar 155
 * using 'flags'.
684 jermar 156
 *
756 jermar 157
 * The address space must be locked and interruptsmust be disabled.
158
 *
159
 * @param as Address space to which page belongs.
684 jermar 160
 * @param page Virtual address of the page to be mapped.
161
 * @param frame Physical address of memory frame to which the mapping is done.
162
 * @param flags Flags to be used for mapping.
163
 */
756 jermar 164
void ht_mapping_insert(as_t *as, __address page, __address frame, int flags)
684 jermar 165
{
792 jermar 166
    pte_t *t;
167
    __native key[2] = { (__address) as, page };
699 jermar 168
 
746 jermar 169
    spinlock_lock(&page_ht_lock);
755 jermar 170
 
792 jermar 171
    if (!hash_table_find(&page_ht, key)) {
823 jermar 172
        t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
792 jermar 173
        ASSERT(t != NULL);
746 jermar 174
 
792 jermar 175
        hash_table_insert(&page_ht, key, &t->link);
699 jermar 176
    }
746 jermar 177
 
178
    spinlock_unlock(&page_ht_lock);
684 jermar 179
}
180
 
826 jermar 181
/** Remove mapping of page from page hash table.
182
 *
183
 * Remove any mapping of 'page' within address space 'as'.
184
 * TLB shootdown should follow in order to make effects of
185
 * this call visible.
186
 *
187
 * The address space must be locked and interrupts must be disabled.
188
 *
189
 * @param as Address space to wich page belongs.
190
 * @param page Virtual address of the page to be demapped.
191
 */
192
void ht_mapping_remove(as_t *as, __address page)
193
{
194
    __native key[2] = { (__address) as, page };
195
 
196
    spinlock_lock(&page_ht_lock);
197
 
198
    /*
199
     * Note that removed PTE's will be freed
200
     * by remove_callback().
201
     */
202
    hash_table_remove(&page_ht, key, 2);
203
 
204
    spinlock_unlock(&page_ht_lock);
205
}
206
 
207
 
684 jermar 208
/** Find mapping for virtual page in page hash table.
209
 *
210
 * Find mapping for virtual page.
211
 *
756 jermar 212
 * The address space must be locked and interrupts must be disabled.
746 jermar 213
 *
756 jermar 214
 * @param as Address space to wich page belongs.
684 jermar 215
 * @param page Virtual page.
216
 *
699 jermar 217
 * @return NULL if there is no such mapping; requested mapping otherwise.
684 jermar 218
 */
756 jermar 219
pte_t *ht_mapping_find(as_t *as, __address page)
684 jermar 220
{
792 jermar 221
    link_t *hlp;
222
    pte_t *t = NULL;
223
    __native key[2] = { (__address) as, page };
699 jermar 224
 
746 jermar 225
    spinlock_lock(&page_ht_lock);
792 jermar 226
 
227
    hlp = hash_table_find(&page_ht, key);
228
    if (hlp)
793 jermar 229
        t = hash_table_get_instance(hlp, pte_t, link);
792 jermar 230
 
746 jermar 231
    spinlock_unlock(&page_ht_lock);
232
    return t;
233
}