Subversion Repositories HelenOS-historic

Rev

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