Subversion Repositories HelenOS-historic

Rev

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

Rev 973 Rev 977
1
/*
1
/*
2
 * Copyright (C) 2001-2006 Jakub Jermar
2
 * Copyright (C) 2001-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
/*
29
/*
30
 * This file contains address space manipulation functions.
30
 * This file contains address space manipulation functions.
31
 * Roughly speaking, this is a higher-level client of
31
 * Roughly speaking, this is a higher-level client of
32
 * Virtual Address Translation (VAT) subsystem.
32
 * Virtual Address Translation (VAT) subsystem.
33
 */
33
 */
34
 
34
 
35
#include <mm/as.h>
35
#include <mm/as.h>
36
#include <arch/mm/as.h>
36
#include <arch/mm/as.h>
37
#include <mm/page.h>
37
#include <mm/page.h>
38
#include <mm/frame.h>
38
#include <mm/frame.h>
39
#include <mm/slab.h>
39
#include <mm/slab.h>
40
#include <mm/tlb.h>
40
#include <mm/tlb.h>
41
#include <arch/mm/page.h>
41
#include <arch/mm/page.h>
42
#include <genarch/mm/page_pt.h>
42
#include <genarch/mm/page_pt.h>
43
#include <mm/asid.h>
43
#include <mm/asid.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>
46
#include <typedefs.h>
47
#include <synch/spinlock.h>
47
#include <synch/spinlock.h>
48
#include <config.h>
48
#include <config.h>
49
#include <adt/list.h>
49
#include <adt/list.h>
50
#include <panic.h>
50
#include <panic.h>
51
#include <arch/asm.h>
51
#include <arch/asm.h>
52
#include <debug.h>
52
#include <debug.h>
53
#include <memstr.h>
53
#include <memstr.h>
54
#include <arch.h>
54
#include <arch.h>
55
#include <print.h>
55
#include <print.h>
56
 
56
 
57
as_operations_t *as_operations = NULL;
57
as_operations_t *as_operations = NULL;
58
 
58
 
59
/** Address space lock. It protects inactive_as_with_asid_head. */
59
/** Address space lock. It protects inactive_as_with_asid_head. */
60
SPINLOCK_INITIALIZE(as_lock);
60
SPINLOCK_INITIALIZE(as_lock);
61
 
61
 
62
/**
62
/**
63
 * This list contains address spaces that are not active on any
63
 * This list contains address spaces that are not active on any
64
 * processor and that have valid ASID.
64
 * processor and that have valid ASID.
65
 */
65
 */
66
LIST_INITIALIZE(inactive_as_with_asid_head);
66
LIST_INITIALIZE(inactive_as_with_asid_head);
67
 
67
 
68
/** Kernel address space. */
68
/** Kernel address space. */
69
as_t *AS_KERNEL = NULL;
69
as_t *AS_KERNEL = NULL;
70
 
70
 
71
static int get_area_flags(as_area_t *a);
71
static int get_area_flags(as_area_t *a);
-
 
72
static as_area_t *find_area_and_lock(as_t *as, __address va);
72
 
73
 
73
/** Initialize address space subsystem. */
74
/** Initialize address space subsystem. */
74
void as_init(void)
75
void as_init(void)
75
{
76
{
76
    as_arch_init();
77
    as_arch_init();
77
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
78
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
78
        if (!AS_KERNEL)
79
        if (!AS_KERNEL)
79
                panic("can't create kernel address space\n");
80
                panic("can't create kernel address space\n");
80
}
81
}
81
 
82
 
82
/** Create address space.
83
/** Create address space.
83
 *
84
 *
84
 * @param flags Flags that influence way in wich the address space is created.
85
 * @param flags Flags that influence way in wich the address space is created.
85
 */
86
 */
86
as_t *as_create(int flags)
87
as_t *as_create(int flags)
87
{
88
{
88
    as_t *as;
89
    as_t *as;
89
 
90
 
90
    as = (as_t *) malloc(sizeof(as_t), 0);
91
    as = (as_t *) malloc(sizeof(as_t), 0);
91
    link_initialize(&as->inactive_as_with_asid_link);
92
    link_initialize(&as->inactive_as_with_asid_link);
92
    spinlock_initialize(&as->lock, "as_lock");
93
    spinlock_initialize(&as->lock, "as_lock");
93
    list_initialize(&as->as_area_head);
94
    list_initialize(&as->as_area_head);
94
   
95
   
95
    if (flags & FLAG_AS_KERNEL)
96
    if (flags & FLAG_AS_KERNEL)
96
        as->asid = ASID_KERNEL;
97
        as->asid = ASID_KERNEL;
97
    else
98
    else
98
        as->asid = ASID_INVALID;
99
        as->asid = ASID_INVALID;
99
   
100
   
100
    as->refcount = 0;
101
    as->refcount = 0;
101
    as->page_table = page_table_create(flags);
102
    as->page_table = page_table_create(flags);
102
 
103
 
103
    return as;
104
    return as;
104
}
105
}
105
 
106
 
106
/** Free Adress space */
107
/** Free Adress space */
107
void as_free(as_t *as)
108
void as_free(as_t *as)
108
{
109
{
109
    ASSERT(as->refcount == 0);
110
    ASSERT(as->refcount == 0);
110
 
111
 
111
    /* TODO: free as_areas and other resources held by as */
112
    /* TODO: free as_areas and other resources held by as */
112
    /* TODO: free page table */
113
    /* TODO: free page table */
113
    free(as);
114
    free(as);
114
}
115
}
115
 
116
 
116
/** Create address space area of common attributes.
117
/** Create address space area of common attributes.
117
 *
118
 *
118
 * The created address space area is added to the target address space.
119
 * The created address space area is added to the target address space.
119
 *
120
 *
120
 * @param as Target address space.
121
 * @param as Target address space.
121
 * @param type Type of area.
122
 * @param type Type of area.
122
 * @param size Size of area in multiples of PAGE_SIZE.
123
 * @param size Size of area in multiples of PAGE_SIZE.
123
 * @param base Base address of area.
124
 * @param base Base address of area.
124
 *
125
 *
125
 * @return Address space area on success or NULL on failure.
126
 * @return Address space area on success or NULL on failure.
126
 */
127
 */
127
as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base)
128
as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base)
128
{
129
{
129
    ipl_t ipl;
130
    ipl_t ipl;
130
    as_area_t *a;
131
    as_area_t *a;
131
   
132
   
132
    if (base % PAGE_SIZE)
133
    if (base % PAGE_SIZE)
133
        panic("addr not aligned to a page boundary");
134
        panic("addr not aligned to a page boundary");
134
   
135
   
135
    ipl = interrupts_disable();
136
    ipl = interrupts_disable();
136
    spinlock_lock(&as->lock);
137
    spinlock_lock(&as->lock);
137
   
138
   
138
    /*
139
    /*
139
     * TODO: test as_area which is to be created doesn't overlap with an existing one.
140
     * TODO: test as_area which is to be created doesn't overlap with an existing one.
140
     */
141
     */
141
   
142
   
142
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
143
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
143
 
144
 
144
    spinlock_initialize(&a->lock, "as_area_lock");
145
    spinlock_initialize(&a->lock, "as_area_lock");
145
   
146
   
146
    link_initialize(&a->link);         
147
    link_initialize(&a->link);         
147
    a->type = type;
148
    a->type = type;
148
    a->size = size;
149
    a->size = size;
149
    a->base = base;
150
    a->base = base;
150
   
151
   
151
    list_append(&a->link, &as->as_area_head);
152
    list_append(&a->link, &as->as_area_head);
152
 
153
 
153
    spinlock_unlock(&as->lock);
154
    spinlock_unlock(&as->lock);
154
    interrupts_restore(ipl);
155
    interrupts_restore(ipl);
155
 
156
 
156
    return a;
157
    return a;
157
}
158
}
158
 
159
 
159
/** Initialize mapping for one page of address space.
160
/** Initialize mapping for one page of address space.
160
 *
161
 *
161
 * This functions maps 'page' to 'frame' according
162
 * This functions maps 'page' to 'frame' according
162
 * to attributes of the address space area to
163
 * to attributes of the address space area to
163
 * wich 'page' belongs.
164
 * wich 'page' belongs.
164
 *
165
 *
165
 * @param as Target address space.
166
 * @param as Target address space.
166
 * @param page Virtual page within the area.
167
 * @param page Virtual page within the area.
167
 * @param frame Physical frame to which page will be mapped.
168
 * @param frame Physical frame to which page will be mapped.
168
 */
169
 */
169
void as_set_mapping(as_t *as, __address page, __address frame)
170
void as_set_mapping(as_t *as, __address page, __address frame)
170
{
171
{
171
    as_area_t *a, *area = NULL;
172
    as_area_t *area;
172
    link_t *cur;
-
 
173
    ipl_t ipl;
173
    ipl_t ipl;
174
   
174
   
175
    ipl = interrupts_disable();
175
    ipl = interrupts_disable();
176
    spinlock_lock(&as->lock);
176
    spinlock_lock(&as->lock);
177
   
177
   
178
    /*
-
 
179
     * First, try locate an area.
-
 
180
     */
-
 
181
    for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) {
-
 
182
        a = list_get_instance(cur, as_area_t, link);
178
    area = find_area_and_lock(as, page);
183
        spinlock_lock(&a->lock);
-
 
184
 
-
 
185
        if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
-
 
186
            area = a;
-
 
187
            break;
-
 
188
        }
-
 
189
       
-
 
190
        spinlock_unlock(&a->lock);
-
 
191
    }
-
 
192
   
-
 
193
    if (!area) {
179
    if (!area) {
194
        panic("page not part of any as_area\n");
180
        panic("page not part of any as_area\n");
195
    }
181
    }
196
 
182
 
197
    /*
-
 
198
     * Note: area->lock is held.
-
 
199
     */
-
 
200
   
-
 
201
    page_mapping_insert(as, page, frame, get_area_flags(area));
183
    page_mapping_insert(as, page, frame, get_area_flags(area));
202
   
184
   
203
    spinlock_unlock(&area->lock);
185
    spinlock_unlock(&area->lock);
204
    spinlock_unlock(&as->lock);
186
    spinlock_unlock(&as->lock);
205
    interrupts_restore(ipl);
187
    interrupts_restore(ipl);
206
}
188
}
207
 
189
 
208
/** Handle page fault within the current address space.
190
/** Handle page fault within the current address space.
209
 *
191
 *
210
 * This is the high-level page fault handler.
192
 * This is the high-level page fault handler.
211
 * Interrupts are assumed disabled.
193
 * Interrupts are assumed disabled.
212
 *
194
 *
213
 * @param page Faulting page.
195
 * @param page Faulting page.
214
 *
196
 *
215
 * @return 0 on page fault, 1 on success.
197
 * @return 0 on page fault, 1 on success.
216
 */
198
 */
217
int as_page_fault(__address page)
199
int as_page_fault(__address page)
218
{
200
{
219
    link_t *cur;
-
 
220
    as_area_t *a, *area = NULL;
201
    as_area_t *area;
221
    __address frame;
202
    __address frame;
222
   
203
   
223
    ASSERT(AS);
204
    ASSERT(AS);
224
    spinlock_lock(&AS->lock);
205
    spinlock_lock(&AS->lock);
225
   
206
   
226
    /*
-
 
227
     * Search this areas of this address space for presence of 'page'.
-
 
228
     */
-
 
229
    for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) {
-
 
230
        a = list_get_instance(cur, as_area_t, link);
-
 
231
        spinlock_lock(&a->lock);
-
 
232
 
-
 
233
        if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
-
 
234
 
-
 
235
            /*
-
 
236
             * We found the area containing 'page'.
207
    area = find_area_and_lock(AS, page);   
237
             * TODO: access checking
-
 
238
             */
-
 
239
            area = a;
-
 
240
            break;
-
 
241
        }
-
 
242
       
-
 
243
        spinlock_unlock(&a->lock);
-
 
244
    }
-
 
245
   
-
 
246
    if (!area) {
208
    if (!area) {
247
        /*
209
        /*
248
         * No area contained mapping for 'page'.
210
         * No area contained mapping for 'page'.
249
         * Signal page fault to low-level handler.
211
         * Signal page fault to low-level handler.
250
         */
212
         */
251
        spinlock_unlock(&AS->lock);
213
        spinlock_unlock(&AS->lock);
252
        return 0;
214
        return 0;
253
    }
215
    }
254
 
216
 
255
    /*
217
    /*
256
     * Note: area->lock is held.
-
 
257
     */
-
 
258
   
-
 
259
    /*
-
 
260
     * In general, there can be several reasons that
218
     * In general, there can be several reasons that
261
     * can have caused this fault.
219
     * can have caused this fault.
262
     *
220
     *
263
     * - non-existent mapping: the area is a scratch
221
     * - non-existent mapping: the area is a scratch
264
     *   area (e.g. stack) and so far has not been
222
     *   area (e.g. stack) and so far has not been
265
     *   allocated a frame for the faulting page
223
     *   allocated a frame for the faulting page
266
     *
224
     *
267
     * - non-present mapping: another possibility,
225
     * - non-present mapping: another possibility,
268
     *   currently not implemented, would be frame
226
     *   currently not implemented, would be frame
269
     *   reuse; when this becomes a possibility,
227
     *   reuse; when this becomes a possibility,
270
     *   do not forget to distinguish between
228
     *   do not forget to distinguish between
271
     *   the different causes
229
     *   the different causes
272
     */
230
     */
273
    frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
231
    frame = PFN2ADDR(frame_alloc(ONE_FRAME, 0));
274
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
232
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
275
   
233
   
276
    /*
234
    /*
277
     * Map 'page' to 'frame'.
235
     * Map 'page' to 'frame'.
278
     * Note that TLB shootdown is not attempted as only new information is being
236
     * Note that TLB shootdown is not attempted as only new information is being
279
     * inserted into page tables.
237
     * inserted into page tables.
280
     */
238
     */
281
    page_mapping_insert(AS, page, frame, get_area_flags(area));
239
    page_mapping_insert(AS, page, frame, get_area_flags(area));
282
   
240
   
283
    spinlock_unlock(&area->lock);
241
    spinlock_unlock(&area->lock);
284
    spinlock_unlock(&AS->lock);
242
    spinlock_unlock(&AS->lock);
285
 
243
 
286
    return 1;
244
    return 1;
287
}
245
}
288
 
246
 
289
/** Switch address spaces.
247
/** Switch address spaces.
290
 *
248
 *
291
 * @param old Old address space or NULL.
249
 * @param old Old address space or NULL.
292
 * @param new New address space.
250
 * @param new New address space.
293
 */
251
 */
294
void as_switch(as_t *old, as_t *new)
252
void as_switch(as_t *old, as_t *new)
295
{
253
{
296
    ipl_t ipl;
254
    ipl_t ipl;
297
    bool needs_asid = false;
255
    bool needs_asid = false;
298
   
256
   
299
    ipl = interrupts_disable();
257
    ipl = interrupts_disable();
300
    spinlock_lock(&as_lock);
258
    spinlock_lock(&as_lock);
301
 
259
 
302
    /*
260
    /*
303
     * First, take care of the old address space.
261
     * First, take care of the old address space.
304
     */
262
     */
305
    if (old) {
263
    if (old) {
306
        spinlock_lock(&old->lock);
264
        spinlock_lock(&old->lock);
307
        ASSERT(old->refcount);
265
        ASSERT(old->refcount);
308
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
266
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
309
            /*
267
            /*
310
             * The old address space is no longer active on
268
             * The old address space is no longer active on
311
             * any processor. It can be appended to the
269
             * any processor. It can be appended to the
312
             * list of inactive address spaces with assigned
270
             * list of inactive address spaces with assigned
313
             * ASID.
271
             * ASID.
314
             */
272
             */
315
             ASSERT(old->asid != ASID_INVALID);
273
             ASSERT(old->asid != ASID_INVALID);
316
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
274
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
317
        }
275
        }
318
        spinlock_unlock(&old->lock);
276
        spinlock_unlock(&old->lock);
319
    }
277
    }
320
 
278
 
321
    /*
279
    /*
322
     * Second, prepare the new address space.
280
     * Second, prepare the new address space.
323
     */
281
     */
324
    spinlock_lock(&new->lock);
282
    spinlock_lock(&new->lock);
325
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
283
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
326
        if (new->asid != ASID_INVALID)
284
        if (new->asid != ASID_INVALID)
327
            list_remove(&new->inactive_as_with_asid_link);
285
            list_remove(&new->inactive_as_with_asid_link);
328
        else
286
        else
329
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
287
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
330
    }
288
    }
331
    SET_PTL0_ADDRESS(new->page_table);
289
    SET_PTL0_ADDRESS(new->page_table);
332
    spinlock_unlock(&new->lock);
290
    spinlock_unlock(&new->lock);
333
 
291
 
334
    if (needs_asid) {
292
    if (needs_asid) {
335
        /*
293
        /*
336
         * Allocation of new ASID was deferred
294
         * Allocation of new ASID was deferred
337
         * until now in order to avoid deadlock.
295
         * until now in order to avoid deadlock.
338
         */
296
         */
339
        asid_t asid;
297
        asid_t asid;
340
       
298
       
341
        asid = asid_get();
299
        asid = asid_get();
342
        spinlock_lock(&new->lock);
300
        spinlock_lock(&new->lock);
343
        new->asid = asid;
301
        new->asid = asid;
344
        spinlock_unlock(&new->lock);
302
        spinlock_unlock(&new->lock);
345
    }
303
    }
346
    spinlock_unlock(&as_lock);
304
    spinlock_unlock(&as_lock);
347
    interrupts_restore(ipl);
305
    interrupts_restore(ipl);
348
   
306
   
349
    /*
307
    /*
350
     * Perform architecture-specific steps.
308
     * Perform architecture-specific steps.
351
     * (e.g. write ASID to hardware register etc.)
309
     * (e.g. write ASID to hardware register etc.)
352
     */
310
     */
353
    as_install_arch(new);
311
    as_install_arch(new);
354
   
312
   
355
    AS = new;
313
    AS = new;
356
}
314
}
357
 
315
 
358
/** Compute flags for virtual address translation subsytem.
316
/** Compute flags for virtual address translation subsytem.
359
 *
317
 *
360
 * The address space area must be locked.
318
 * The address space area must be locked.
361
 * Interrupts must be disabled.
319
 * Interrupts must be disabled.
362
 *
320
 *
363
 * @param a Address space area.
321
 * @param a Address space area.
364
 *
322
 *
365
 * @return Flags to be used in page_mapping_insert().
323
 * @return Flags to be used in page_mapping_insert().
366
 */
324
 */
367
int get_area_flags(as_area_t *a)
325
int get_area_flags(as_area_t *a)
368
{
326
{
369
    int flags;
327
    int flags;
370
 
328
 
371
    switch (a->type) {
329
    switch (a->type) {
372
        case AS_AREA_TEXT:
330
        case AS_AREA_TEXT:
373
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
331
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
374
            break;
332
            break;
375
        case AS_AREA_DATA:
333
        case AS_AREA_DATA:
376
        case AS_AREA_STACK:
334
        case AS_AREA_STACK:
377
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
335
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
378
            break;
336
            break;
379
        default:
337
        default:
380
            panic("unexpected as_area_type_t %d", a->type);
338
            panic("unexpected as_area_type_t %d", a->type);
381
    }
339
    }
382
   
340
   
383
    return flags;
341
    return flags;
384
}
342
}
385
 
343
 
386
/** Create page table.
344
/** Create page table.
387
 *
345
 *
388
 * Depending on architecture, create either address space
346
 * Depending on architecture, create either address space
389
 * private or global page table.
347
 * private or global page table.
390
 *
348
 *
391
 * @param flags Flags saying whether the page table is for kernel address space.
349
 * @param flags Flags saying whether the page table is for kernel address space.
392
 *
350
 *
393
 * @return First entry of the page table.
351
 * @return First entry of the page table.
394
 */
352
 */
395
pte_t *page_table_create(int flags)
353
pte_t *page_table_create(int flags)
396
{
354
{
397
        ASSERT(as_operations);
355
        ASSERT(as_operations);
398
        ASSERT(as_operations->page_table_create);
356
        ASSERT(as_operations->page_table_create);
399
 
357
 
400
        return as_operations->page_table_create(flags);
358
        return as_operations->page_table_create(flags);
401
}
359
}
-
 
360
 
-
 
361
/** Find address space area and change it.
-
 
362
 *
-
 
363
 * @param as Address space.
-
 
364
 * @param address Virtual address belonging to the area to be changed. Must be page-aligned.
-
 
365
 * @param size New size of the virtual memory block starting at address.
-
 
366
 * @param flags Flags influencing the remap operation. Currently unused.
-
 
367
 *
-
 
368
 * @return address on success, (__address) -1 otherwise.
-
 
369
 */
-
 
370
__address as_remap(as_t *as, __address address, size_t size, int flags)
-
 
371
{
-
 
372
    as_area_t *area = NULL;
-
 
373
    ipl_t ipl;
-
 
374
    size_t pages;
-
 
375
   
-
 
376
    ipl = interrupts_disable();
-
 
377
    spinlock_lock(&as->lock);
-
 
378
   
-
 
379
    /*
-
 
380
     * Locate the area.
-
 
381
     */
-
 
382
    area = find_area_and_lock(as, address);
-
 
383
    if (!area) {
-
 
384
        spinlock_unlock(&as->lock);
-
 
385
        return (__address) -1;
-
 
386
    }
-
 
387
 
-
 
388
    pages = SIZE2FRAMES((address - area->base) + size);
-
 
389
    if (pages < area->size) {
-
 
390
        int i;
-
 
391
 
-
 
392
        /*
-
 
393
         * Shrinking the area.
-
 
394
         */
-
 
395
        for (i = pages; i < area->size; i++) {
-
 
396
            pte_t *pte;
-
 
397
           
-
 
398
            /*
-
 
399
             * Releasing physical memory.
-
 
400
             * This depends on the fact that the memory was allocated using frame_alloc().
-
 
401
             */
-
 
402
            pte = page_mapping_find(as, area->base + i*PAGE_SIZE);
-
 
403
            if (pte) {
-
 
404
                ASSERT(PTE_PRESENT(pte));
-
 
405
                frame_free(ADDR2PFN(PTE_GET_FRAME(pte)));
-
 
406
            }
-
 
407
            page_mapping_remove(as, area->base + i*PAGE_SIZE);
-
 
408
        }
-
 
409
        /*
-
 
410
         * Invalidate TLB's.
-
 
411
         */
-
 
412
        tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base + pages*PAGE_SIZE, area->size - pages);
-
 
413
        tlb_invalidate_pages(AS->asid, area->base + pages*PAGE_SIZE, area->size - pages);
-
 
414
        tlb_shootdown_finalize();
-
 
415
    } else {
-
 
416
        /*
-
 
417
         * Growing the area.
-
 
418
         */
-
 
419
        area->size = size;
-
 
420
    }
-
 
421
   
-
 
422
    spinlock_unlock(&area->lock);
-
 
423
    spinlock_unlock(&as->lock);
-
 
424
    interrupts_restore(ipl);
-
 
425
 
-
 
426
    return address;
-
 
427
}
-
 
428
 
-
 
429
/** Find address space area and lock it.
-
 
430
 *
-
 
431
 * The address space must be locked and interrupts must be disabled.
-
 
432
 *
-
 
433
 * @param as Address space.
-
 
434
 * @param va Virtual address.
-
 
435
 *
-
 
436
 * @return Locked address space area containing va on success or NULL on failure.
-
 
437
 */
-
 
438
as_area_t *find_area_and_lock(as_t *as, __address va)
-
 
439
{
-
 
440
    link_t *cur;
-
 
441
    as_area_t *a;
-
 
442
   
-
 
443
    for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) {
-
 
444
        a = list_get_instance(cur, as_area_t, link);
-
 
445
        spinlock_lock(&a->lock);
-
 
446
 
-
 
447
        if ((va >= a->base) && (va < a->base + a->size * PAGE_SIZE))
-
 
448
             return a;
-
 
449
       
-
 
450
        spinlock_unlock(&a->lock);
-
 
451
    }
-
 
452
 
-
 
453
    return NULL;
-
 
454
}
402
 
455