Subversion Repositories HelenOS-historic

Rev

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

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