Subversion Repositories HelenOS-historic

Rev

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

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