Subversion Repositories HelenOS

Rev

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