Subversion Repositories HelenOS-historic

Rev

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

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