Subversion Repositories HelenOS

Rev

Rev 2071 | Rev 2141 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2071 Rev 2089
1
/*
1
/*
2
 * Copyright (c) 2006 Jakub Jermar
2
 * Copyright (c) 2006 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup genarchmm
29
/** @addtogroup genarchmm
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Virtual Address Translation (VAT) for global page hash table.
35
 * @brief   Virtual Address Translation (VAT) for global page hash table.
36
 */
36
 */
37
 
37
 
38
#include <genarch/mm/page_ht.h>
38
#include <genarch/mm/page_ht.h>
39
#include <mm/page.h>
39
#include <mm/page.h>
40
#include <arch/mm/page.h>
40
#include <arch/mm/page.h>
41
#include <mm/frame.h>
41
#include <mm/frame.h>
42
#include <mm/slab.h>
42
#include <mm/slab.h>
43
#include <mm/as.h>
43
#include <mm/as.h>
44
#include <arch/mm/asid.h>
44
#include <arch/mm/asid.h>
45
#include <arch/types.h>
45
#include <arch/types.h>
46
#include <typedefs.h>
-
 
47
#include <arch/asm.h>
46
#include <arch/asm.h>
48
#include <synch/spinlock.h>
47
#include <synch/spinlock.h>
49
#include <arch.h>
48
#include <arch.h>
50
#include <debug.h>
49
#include <debug.h>
51
#include <memstr.h>
50
#include <memstr.h>
52
#include <adt/hash_table.h>
51
#include <adt/hash_table.h>
53
#include <align.h>
52
#include <align.h>
54
 
53
 
55
static index_t hash(unative_t key[]);
54
static index_t hash(unative_t key[]);
56
static bool compare(unative_t key[], count_t keys, link_t *item);
55
static bool compare(unative_t key[], count_t keys, link_t *item);
57
static void remove_callback(link_t *item);
56
static void remove_callback(link_t *item);
58
 
57
 
59
static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags);
58
static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags);
60
static void ht_mapping_remove(as_t *as, uintptr_t page);
59
static void ht_mapping_remove(as_t *as, uintptr_t page);
61
static pte_t *ht_mapping_find(as_t *as, uintptr_t page);
60
static pte_t *ht_mapping_find(as_t *as, uintptr_t page);
62
 
61
 
63
/**
62
/**
64
 * This lock protects the page hash table. It must be acquired
63
 * This lock protects the page hash table. It must be acquired
65
 * after address space lock and after any address space area
64
 * after address space lock and after any address space area
66
 * locks.
65
 * locks.
67
 */
66
 */
68
mutex_t page_ht_lock;
67
mutex_t page_ht_lock;
69
 
68
 
70
/**
69
/**
71
 * Page hash table.
70
 * Page hash table.
72
 * The page hash table may be accessed only when page_ht_lock is held.
71
 * The page hash table may be accessed only when page_ht_lock is held.
73
 */
72
 */
74
hash_table_t page_ht;
73
hash_table_t page_ht;
75
 
74
 
76
/** Hash table operations for page hash table. */
75
/** Hash table operations for page hash table. */
77
hash_table_operations_t ht_operations = {
76
hash_table_operations_t ht_operations = {
78
    .hash = hash,
77
    .hash = hash,
79
    .compare = compare,
78
    .compare = compare,
80
    .remove_callback = remove_callback
79
    .remove_callback = remove_callback
81
};
80
};
82
 
81
 
83
/** Page mapping operations for page hash table architectures. */
82
/** Page mapping operations for page hash table architectures. */
84
page_mapping_operations_t ht_mapping_operations = {
83
page_mapping_operations_t ht_mapping_operations = {
85
    .mapping_insert = ht_mapping_insert,
84
    .mapping_insert = ht_mapping_insert,
86
    .mapping_remove = ht_mapping_remove,
85
    .mapping_remove = ht_mapping_remove,
87
    .mapping_find = ht_mapping_find
86
    .mapping_find = ht_mapping_find
88
};
87
};
89
 
88
 
90
/** Compute page hash table index.
89
/** Compute page hash table index.
91
 *
90
 *
92
 * @param key Array of two keys (i.e. page and address space).
91
 * @param key Array of two keys (i.e. page and address space).
93
 *
92
 *
94
 * @return Index into page hash table.
93
 * @return Index into page hash table.
95
 */
94
 */
96
index_t hash(unative_t key[])
95
index_t hash(unative_t key[])
97
{
96
{
98
    as_t *as = (as_t *) key[KEY_AS];
97
    as_t *as = (as_t *) key[KEY_AS];
99
    uintptr_t page = (uintptr_t) key[KEY_PAGE];
98
    uintptr_t page = (uintptr_t) key[KEY_PAGE];
100
    index_t index;
99
    index_t index;
101
   
100
   
102
    /*
101
    /*
103
     * Virtual page addresses have roughly the same probability
102
     * Virtual page addresses have roughly the same probability
104
     * of occurring. Least significant bits of VPN compose the
103
     * of occurring. Least significant bits of VPN compose the
105
     * hash index.
104
     * hash index.
106
     */
105
     */
107
    index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1));
106
    index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1));
108
   
107
   
109
    /*
108
    /*
110
     * Address space structures are likely to be allocated from
109
     * Address space structures are likely to be allocated from
111
     * similar addresses. Least significant bits compose the
110
     * similar addresses. Least significant bits compose the
112
     * hash index.
111
     * hash index.
113
     */
112
     */
114
    index |= ((unative_t) as) & (PAGE_HT_ENTRIES-1);
113
    index |= ((unative_t) as) & (PAGE_HT_ENTRIES-1);
115
   
114
   
116
    return index;
115
    return index;
117
}
116
}
118
 
117
 
119
/** Compare page hash table item with page and/or address space.
118
/** Compare page hash table item with page and/or address space.
120
 *
119
 *
121
 * @param key Array of one or two keys (i.e. page and/or address space).
120
 * @param key Array of one or two keys (i.e. page and/or address space).
122
 * @param keys Number of keys passed.
121
 * @param keys Number of keys passed.
123
 * @param item Item to compare the keys with.
122
 * @param item Item to compare the keys with.
124
 *
123
 *
125
 * @return true on match, false otherwise.
124
 * @return true on match, false otherwise.
126
 */
125
 */
127
bool compare(unative_t key[], count_t keys, link_t *item)
126
bool compare(unative_t key[], count_t keys, link_t *item)
128
{
127
{
129
    pte_t *t;
128
    pte_t *t;
130
 
129
 
131
    ASSERT(item);
130
    ASSERT(item);
132
    ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
131
    ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
133
 
132
 
134
    /*
133
    /*
135
     * Convert item to PTE.
134
     * Convert item to PTE.
136
     */
135
     */
137
    t = hash_table_get_instance(item, pte_t, link);
136
    t = hash_table_get_instance(item, pte_t, link);
138
 
137
 
139
    if (keys == PAGE_HT_KEYS) {
138
    if (keys == PAGE_HT_KEYS) {
140
        return (key[KEY_AS] == (uintptr_t) t->as) && (key[KEY_PAGE] == t->page);
139
        return (key[KEY_AS] == (uintptr_t) t->as) && (key[KEY_PAGE] == t->page);
141
    } else {
140
    } else {
142
        return (key[KEY_AS] == (uintptr_t) t->as);
141
        return (key[KEY_AS] == (uintptr_t) t->as);
143
    }
142
    }
144
}
143
}
145
 
144
 
146
/** Callback on page hash table item removal.
145
/** Callback on page hash table item removal.
147
 *
146
 *
148
 * @param item Page hash table item being removed.
147
 * @param item Page hash table item being removed.
149
 */
148
 */
150
void remove_callback(link_t *item)
149
void remove_callback(link_t *item)
151
{
150
{
152
    pte_t *t;
151
    pte_t *t;
153
 
152
 
154
    ASSERT(item);
153
    ASSERT(item);
155
 
154
 
156
    /*
155
    /*
157
     * Convert item to PTE.
156
     * Convert item to PTE.
158
     */
157
     */
159
    t = hash_table_get_instance(item, pte_t, link);
158
    t = hash_table_get_instance(item, pte_t, link);
160
 
159
 
161
    free(t);
160
    free(t);
162
}
161
}
163
 
162
 
164
/** Map page to frame using page hash table.
163
/** Map page to frame using page hash table.
165
 *
164
 *
166
 * Map virtual address page to physical address frame
165
 * Map virtual address page to physical address frame
167
 * using flags.
166
 * using flags.
168
 *
167
 *
169
 * The page table must be locked and interrupts must be disabled.
168
 * The page table must be locked and interrupts must be disabled.
170
 *
169
 *
171
 * @param as Address space to which page belongs.
170
 * @param as Address space to which page belongs.
172
 * @param page Virtual address of the page to be mapped.
171
 * @param page Virtual address of the page to be mapped.
173
 * @param frame Physical address of memory frame to which the mapping is done.
172
 * @param frame Physical address of memory frame to which the mapping is done.
174
 * @param flags Flags to be used for mapping.
173
 * @param flags Flags to be used for mapping.
175
 */
174
 */
176
void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
175
void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
177
{
176
{
178
    pte_t *t;
177
    pte_t *t;
179
    unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
178
    unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
180
   
179
   
181
    if (!hash_table_find(&page_ht, key)) {
180
    if (!hash_table_find(&page_ht, key)) {
182
        t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
181
        t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
183
        ASSERT(t != NULL);
182
        ASSERT(t != NULL);
184
 
183
 
185
        t->g = (flags & PAGE_GLOBAL) != 0;
184
        t->g = (flags & PAGE_GLOBAL) != 0;
186
        t->x = (flags & PAGE_EXEC) != 0;
185
        t->x = (flags & PAGE_EXEC) != 0;
187
        t->w = (flags & PAGE_WRITE) != 0;
186
        t->w = (flags & PAGE_WRITE) != 0;
188
        t->k = !(flags & PAGE_USER);
187
        t->k = !(flags & PAGE_USER);
189
        t->c = (flags & PAGE_CACHEABLE) != 0;
188
        t->c = (flags & PAGE_CACHEABLE) != 0;
190
        t->p = !(flags & PAGE_NOT_PRESENT);
189
        t->p = !(flags & PAGE_NOT_PRESENT);
191
 
190
 
192
        t->as = as;
191
        t->as = as;
193
        t->page = ALIGN_DOWN(page, PAGE_SIZE);
192
        t->page = ALIGN_DOWN(page, PAGE_SIZE);
194
        t->frame = ALIGN_DOWN(frame, FRAME_SIZE);
193
        t->frame = ALIGN_DOWN(frame, FRAME_SIZE);
195
 
194
 
196
        hash_table_insert(&page_ht, key, &t->link);
195
        hash_table_insert(&page_ht, key, &t->link);
197
    }
196
    }
198
}
197
}
199
 
198
 
200
/** Remove mapping of page from page hash table.
199
/** Remove mapping of page from page hash table.
201
 *
200
 *
202
 * Remove any mapping of page within address space as.
201
 * Remove any mapping of page within address space as.
203
 * TLB shootdown should follow in order to make effects of
202
 * TLB shootdown should follow in order to make effects of
204
 * this call visible.
203
 * this call visible.
205
 *
204
 *
206
 * The page table must be locked and interrupts must be disabled.
205
 * The page table must be locked and interrupts must be disabled.
207
 *
206
 *
208
 * @param as Address space to wich page belongs.
207
 * @param as Address space to wich page belongs.
209
 * @param page Virtual address of the page to be demapped.
208
 * @param page Virtual address of the page to be demapped.
210
 */
209
 */
211
void ht_mapping_remove(as_t *as, uintptr_t page)
210
void ht_mapping_remove(as_t *as, uintptr_t page)
212
{
211
{
213
    unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
212
    unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
214
   
213
   
215
    /*
214
    /*
216
     * Note that removed PTE's will be freed
215
     * Note that removed PTE's will be freed
217
     * by remove_callback().
216
     * by remove_callback().
218
     */
217
     */
219
    hash_table_remove(&page_ht, key, 2);
218
    hash_table_remove(&page_ht, key, 2);
220
}
219
}
221
 
220
 
222
 
221
 
223
/** Find mapping for virtual page in page hash table.
222
/** Find mapping for virtual page in page hash table.
224
 *
223
 *
225
 * Find mapping for virtual page.
224
 * Find mapping for virtual page.
226
 *
225
 *
227
 * The page table must be locked and interrupts must be disabled.
226
 * The page table must be locked and interrupts must be disabled.
228
 *
227
 *
229
 * @param as Address space to wich page belongs.
228
 * @param as Address space to wich page belongs.
230
 * @param page Virtual page.
229
 * @param page Virtual page.
231
 *
230
 *
232
 * @return NULL if there is no such mapping; requested mapping otherwise.
231
 * @return NULL if there is no such mapping; requested mapping otherwise.
233
 */
232
 */
234
pte_t *ht_mapping_find(as_t *as, uintptr_t page)
233
pte_t *ht_mapping_find(as_t *as, uintptr_t page)
235
{
234
{
236
    link_t *hlp;
235
    link_t *hlp;
237
    pte_t *t = NULL;
236
    pte_t *t = NULL;
238
    unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
237
    unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
239
   
238
   
240
    hlp = hash_table_find(&page_ht, key);
239
    hlp = hash_table_find(&page_ht, key);
241
    if (hlp)
240
    if (hlp)
242
        t = hash_table_get_instance(hlp, pte_t, link);
241
        t = hash_table_get_instance(hlp, pte_t, link);
243
 
242
 
244
    return t;
243
    return t;
245
}
244
}
246
 
245
 
247
/** @}
246
/** @}
248
 */
247
 */
249
 
248