Subversion Repositories HelenOS

Rev

Rev 983 | Rev 1044 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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