Subversion Repositories HelenOS

Rev

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

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