Subversion Repositories HelenOS-historic

Rev

Rev 1358 | Rev 1383 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1358 Rev 1380
Line 52... Line 52...
52
#include <genarch/mm/page_pt.h>
52
#include <genarch/mm/page_pt.h>
53
#include <genarch/mm/page_ht.h>
53
#include <genarch/mm/page_ht.h>
54
#include <mm/asid.h>
54
#include <mm/asid.h>
55
#include <arch/mm/asid.h>
55
#include <arch/mm/asid.h>
56
#include <synch/spinlock.h>
56
#include <synch/spinlock.h>
-
 
57
#include <synch/mutex.h>
57
#include <adt/list.h>
58
#include <adt/list.h>
58
#include <adt/btree.h>
59
#include <adt/btree.h>
59
#include <proc/task.h>
60
#include <proc/task.h>
60
#include <proc/thread.h>
61
#include <proc/thread.h>
61
#include <arch/asm.h>
62
#include <arch/asm.h>
Line 72... Line 73...
72
#include <syscall/copy.h>
73
#include <syscall/copy.h>
73
#include <arch/interrupt.h>
74
#include <arch/interrupt.h>
74
 
75
 
75
as_operations_t *as_operations = NULL;
76
as_operations_t *as_operations = NULL;
76
 
77
 
77
/** Address space lock. It protects inactive_as_with_asid_head. */
78
/** Address space lock. It protects inactive_as_with_asid_head. Must be acquired before as_t mutex. */
78
SPINLOCK_INITIALIZE(as_lock);
79
SPINLOCK_INITIALIZE(as_lock);
79
 
80
 
80
/**
81
/**
81
 * This list contains address spaces that are not active on any
82
 * This list contains address spaces that are not active on any
82
 * processor and that have valid ASID.
83
 * processor and that have valid ASID.
Line 108... Line 109...
108
{
109
{
109
    as_t *as;
110
    as_t *as;
110
 
111
 
111
    as = (as_t *) malloc(sizeof(as_t), 0);
112
    as = (as_t *) malloc(sizeof(as_t), 0);
112
    link_initialize(&as->inactive_as_with_asid_link);
113
    link_initialize(&as->inactive_as_with_asid_link);
113
    spinlock_initialize(&as->lock, "as_lock");
114
    mutex_initialize(&as->lock);
114
    btree_create(&as->as_area_btree);
115
    btree_create(&as->as_area_btree);
115
   
116
   
116
    if (flags & FLAG_AS_KERNEL)
117
    if (flags & FLAG_AS_KERNEL)
117
        as->asid = ASID_KERNEL;
118
        as->asid = ASID_KERNEL;
118
    else
119
    else
Line 160... Line 161...
160
    /* Writeable executable areas are not supported. */
161
    /* Writeable executable areas are not supported. */
161
    if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
162
    if ((flags & AS_AREA_EXEC) && (flags & AS_AREA_WRITE))
162
        return NULL;
163
        return NULL;
163
   
164
   
164
    ipl = interrupts_disable();
165
    ipl = interrupts_disable();
165
    spinlock_lock(&as->lock);
166
    mutex_lock(&as->lock);
166
   
167
   
167
    if (!check_area_conflicts(as, base, size, NULL)) {
168
    if (!check_area_conflicts(as, base, size, NULL)) {
168
        spinlock_unlock(&as->lock);
169
        mutex_unlock(&as->lock);
169
        interrupts_restore(ipl);
170
        interrupts_restore(ipl);
170
        return NULL;
171
        return NULL;
171
    }
172
    }
172
   
173
   
173
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
174
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
174
 
175
 
175
    spinlock_initialize(&a->lock, "as_area_lock");
176
    mutex_initialize(&a->lock);
176
   
177
   
177
    a->flags = flags;
178
    a->flags = flags;
178
    a->attributes = attrs;
179
    a->attributes = attrs;
179
    a->pages = SIZE2FRAMES(size);
180
    a->pages = SIZE2FRAMES(size);
180
    a->base = base;
181
    a->base = base;
181
   
182
   
182
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
183
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
183
 
184
 
184
    spinlock_unlock(&as->lock);
185
    mutex_unlock(&as->lock);
185
    interrupts_restore(ipl);
186
    interrupts_restore(ipl);
186
 
187
 
187
    return a;
188
    return a;
188
}
189
}
189
 
190
 
Line 201... Line 202...
201
    as_area_t *area;
202
    as_area_t *area;
202
    ipl_t ipl;
203
    ipl_t ipl;
203
    size_t pages;
204
    size_t pages;
204
   
205
   
205
    ipl = interrupts_disable();
206
    ipl = interrupts_disable();
206
    spinlock_lock(&as->lock);
207
    mutex_lock(&as->lock);
207
   
208
   
208
    /*
209
    /*
209
     * Locate the area.
210
     * Locate the area.
210
     */
211
     */
211
    area = find_area_and_lock(as, address);
212
    area = find_area_and_lock(as, address);
212
    if (!area) {
213
    if (!area) {
213
        spinlock_unlock(&as->lock);
214
        mutex_unlock(&as->lock);
214
        interrupts_restore(ipl);
215
        interrupts_restore(ipl);
215
        return ENOENT;
216
        return ENOENT;
216
    }
217
    }
217
 
218
 
218
    if (area->flags & AS_AREA_DEVICE) {
219
    if (area->flags & AS_AREA_DEVICE) {
219
        /*
220
        /*
220
         * Remapping of address space areas associated
221
         * Remapping of address space areas associated
221
         * with memory mapped devices is not supported.
222
         * with memory mapped devices is not supported.
222
         */
223
         */
223
        spinlock_unlock(&area->lock);
224
        mutex_unlock(&area->lock);
224
        spinlock_unlock(&as->lock);
225
        mutex_unlock(&as->lock);
225
        interrupts_restore(ipl);
226
        interrupts_restore(ipl);
226
        return ENOTSUP;
227
        return ENOTSUP;
227
    }
228
    }
228
 
229
 
229
    pages = SIZE2FRAMES((address - area->base) + size);
230
    pages = SIZE2FRAMES((address - area->base) + size);
230
    if (!pages) {
231
    if (!pages) {
231
        /*
232
        /*
232
         * Zero size address space areas are not allowed.
233
         * Zero size address space areas are not allowed.
233
         */
234
         */
234
        spinlock_unlock(&area->lock);
235
        mutex_unlock(&area->lock);
235
        spinlock_unlock(&as->lock);
236
        mutex_unlock(&as->lock);
236
        interrupts_restore(ipl);
237
        interrupts_restore(ipl);
237
        return EPERM;
238
        return EPERM;
238
    }
239
    }
239
   
240
   
240
    if (pages < area->pages) {
241
    if (pages < area->pages) {
Line 276... Line 277...
276
        /*
277
        /*
277
         * Growing the area.
278
         * Growing the area.
278
         * Check for overlaps with other address space areas.
279
         * Check for overlaps with other address space areas.
279
         */
280
         */
280
        if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
281
        if (!check_area_conflicts(as, address, pages * PAGE_SIZE, area)) {
281
            spinlock_unlock(&area->lock);
282
            mutex_unlock(&area->lock);
282
            spinlock_unlock(&as->lock);    
283
            mutex_unlock(&as->lock);       
283
            interrupts_restore(ipl);
284
            interrupts_restore(ipl);
284
            return EADDRNOTAVAIL;
285
            return EADDRNOTAVAIL;
285
        }
286
        }
286
    }
287
    }
287
 
288
 
288
    area->pages = pages;
289
    area->pages = pages;
289
   
290
   
290
    spinlock_unlock(&area->lock);
291
    mutex_unlock(&area->lock);
291
    spinlock_unlock(&as->lock);
292
    mutex_unlock(&as->lock);
292
    interrupts_restore(ipl);
293
    interrupts_restore(ipl);
293
 
294
 
294
    return 0;
295
    return 0;
295
}
296
}
296
 
297
 
Line 307... Line 308...
307
    __address base;
308
    __address base;
308
    ipl_t ipl;
309
    ipl_t ipl;
309
    int i;
310
    int i;
310
 
311
 
311
    ipl = interrupts_disable();
312
    ipl = interrupts_disable();
312
    spinlock_lock(&as->lock);
313
    mutex_lock(&as->lock);
313
 
314
 
314
    area = find_area_and_lock(as, address);
315
    area = find_area_and_lock(as, address);
315
    if (!area) {
316
    if (!area) {
316
        spinlock_unlock(&as->lock);
317
        mutex_unlock(&as->lock);
317
        interrupts_restore(ipl);
318
        interrupts_restore(ipl);
318
        return ENOENT;
319
        return ENOENT;
319
    }
320
    }
320
 
321
 
321
    base = area->base; 
322
    base = area->base; 
Line 348... Line 349...
348
    tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages);
349
    tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base, area->pages);
349
    tlb_invalidate_pages(AS->asid, area->base, area->pages);
350
    tlb_invalidate_pages(AS->asid, area->base, area->pages);
350
    tlb_shootdown_finalize();
351
    tlb_shootdown_finalize();
351
 
352
 
352
    area->attributes |= AS_AREA_ATTR_PARTIAL;
353
    area->attributes |= AS_AREA_ATTR_PARTIAL;
353
    spinlock_unlock(&area->lock);
354
    mutex_unlock(&area->lock);
354
 
355
 
355
    /*
356
    /*
356
     * Remove the empty area from address space.
357
     * Remove the empty area from address space.
357
     */
358
     */
358
    btree_remove(&AS->as_area_btree, base, NULL);
359
    btree_remove(&AS->as_area_btree, base, NULL);
359
   
360
   
360
    free(area);
361
    free(area);
361
   
362
   
362
    spinlock_unlock(&AS->lock);
363
    mutex_unlock(&AS->lock);
363
    interrupts_restore(ipl);
364
    interrupts_restore(ipl);
364
    return 0;
365
    return 0;
365
}
366
}
366
 
367
 
367
/** Steal address space area from another task.
368
/** Steal address space area from another task.
Line 395... Line 396...
395
 
396
 
396
    ipl = interrupts_disable();
397
    ipl = interrupts_disable();
397
    spinlock_lock(&src_task->lock);
398
    spinlock_lock(&src_task->lock);
398
    src_as = src_task->as;
399
    src_as = src_task->as;
399
   
400
   
400
    spinlock_lock(&src_as->lock);
401
    mutex_lock(&src_as->lock);
401
    src_area = find_area_and_lock(src_as, src_base);
402
    src_area = find_area_and_lock(src_as, src_base);
402
    if (!src_area) {
403
    if (!src_area) {
403
        /*
404
        /*
404
         * Could not find the source address space area.
405
         * Could not find the source address space area.
405
         */
406
         */
406
        spinlock_unlock(&src_task->lock);
407
        spinlock_unlock(&src_task->lock);
407
        spinlock_unlock(&src_as->lock);
408
        mutex_unlock(&src_as->lock);
408
        interrupts_restore(ipl);
409
        interrupts_restore(ipl);
409
        return ENOENT;
410
        return ENOENT;
410
    }
411
    }
411
    src_size = src_area->pages * PAGE_SIZE;
412
    src_size = src_area->pages * PAGE_SIZE;
412
    src_flags = src_area->flags;
413
    src_flags = src_area->flags;
413
    spinlock_unlock(&src_area->lock);
414
    mutex_unlock(&src_area->lock);
414
    spinlock_unlock(&src_as->lock);
415
    mutex_unlock(&src_as->lock);
415
 
416
 
416
 
417
 
417
    if (src_size != acc_size) {
418
    if (src_size != acc_size) {
418
        spinlock_unlock(&src_task->lock);
419
        spinlock_unlock(&src_task->lock);
419
        interrupts_restore(ipl);
420
        interrupts_restore(ipl);
Line 439... Line 440...
439
   
440
   
440
    /*
441
    /*
441
     * Avoid deadlock by first locking the address space with lower address.
442
     * Avoid deadlock by first locking the address space with lower address.
442
     */
443
     */
443
    if (AS < src_as) {
444
    if (AS < src_as) {
444
        spinlock_lock(&AS->lock);
445
        mutex_lock(&AS->lock);
445
        spinlock_lock(&src_as->lock);
446
        mutex_lock(&src_as->lock);
446
    } else {
447
    } else {
447
        spinlock_lock(&AS->lock);
448
        mutex_lock(&AS->lock);
448
        spinlock_lock(&src_as->lock);
449
        mutex_lock(&src_as->lock);
449
    }
450
    }
450
   
451
   
451
    for (i = 0; i < SIZE2FRAMES(src_size); i++) {
452
    for (i = 0; i < SIZE2FRAMES(src_size); i++) {
452
        pte_t *pte;
453
        pte_t *pte;
453
        __address frame;
454
        __address frame;
Line 473... Line 474...
473
    /*
474
    /*
474
     * Now the destination address space area has been
475
     * Now the destination address space area has been
475
     * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
476
     * fully initialized. Clear the AS_AREA_ATTR_PARTIAL
476
     * attribute.
477
     * attribute.
477
     */
478
     */
478
    spinlock_lock(&dst_area->lock);
479
    mutex_lock(&dst_area->lock);
479
    dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
480
    dst_area->attributes &= ~AS_AREA_ATTR_PARTIAL;
480
    spinlock_unlock(&dst_area->lock);
481
    mutex_unlock(&dst_area->lock);
481
   
482
   
482
    spinlock_unlock(&AS->lock);
483
    mutex_unlock(&AS->lock);
483
    spinlock_unlock(&src_as->lock);
484
    mutex_unlock(&src_as->lock);
484
    interrupts_restore(ipl);
485
    interrupts_restore(ipl);
485
   
486
   
486
    return 0;
487
    return 0;
487
}
488
}
488
 
489
 
Line 509... Line 510...
509
        panic("page not part of any as_area\n");
510
        panic("page not part of any as_area\n");
510
    }
511
    }
511
 
512
 
512
    page_mapping_insert(as, page, frame, get_area_flags(area));
513
    page_mapping_insert(as, page, frame, get_area_flags(area));
513
   
514
   
514
    spinlock_unlock(&area->lock);
515
    mutex_unlock(&area->lock);
515
    page_table_unlock(as, true);
516
    page_table_unlock(as, true);
516
    interrupts_restore(ipl);
517
    interrupts_restore(ipl);
517
}
518
}
518
 
519
 
519
/** Handle page fault within the current address space.
520
/** Handle page fault within the current address space.
Line 530... Line 531...
530
{
531
{
531
    pte_t *pte;
532
    pte_t *pte;
532
    as_area_t *area;
533
    as_area_t *area;
533
    __address frame;
534
    __address frame;
534
   
535
   
-
 
536
    if (!THREAD)
-
 
537
        return 0;
-
 
538
       
535
    ASSERT(AS);
539
    ASSERT(AS);
536
 
540
 
537
    spinlock_lock(&AS->lock);
541
    mutex_lock(&AS->lock);
538
    area = find_area_and_lock(AS, page);   
542
    area = find_area_and_lock(AS, page);   
539
    if (!area) {
543
    if (!area) {
540
        /*
544
        /*
541
         * No area contained mapping for 'page'.
545
         * No area contained mapping for 'page'.
542
         * Signal page fault to low-level handler.
546
         * Signal page fault to low-level handler.
543
         */
547
         */
544
        spinlock_unlock(&AS->lock);
548
        mutex_unlock(&AS->lock);
545
        goto page_fault;
549
        goto page_fault;
546
    }
550
    }
547
 
551
 
548
    if (area->attributes & AS_AREA_ATTR_PARTIAL) {
552
    if (area->attributes & AS_AREA_ATTR_PARTIAL) {
549
        /*
553
        /*
550
         * The address space area is not fully initialized.
554
         * The address space area is not fully initialized.
551
         * Avoid possible race by returning error.
555
         * Avoid possible race by returning error.
552
         */
556
         */
553
        spinlock_unlock(&area->lock);
557
        mutex_unlock(&area->lock);
554
        spinlock_unlock(&AS->lock);
558
        mutex_unlock(&AS->lock);
555
        goto page_fault;       
559
        goto page_fault;       
556
    }
560
    }
557
 
561
 
558
    ASSERT(!(area->flags & AS_AREA_DEVICE));
562
    ASSERT(!(area->flags & AS_AREA_DEVICE));
559
 
563
 
Line 565... Line 569...
565
     * the mapping has not been already inserted.
569
     * the mapping has not been already inserted.
566
     */
570
     */
567
    if ((pte = page_mapping_find(AS, page))) {
571
    if ((pte = page_mapping_find(AS, page))) {
568
        if (PTE_PRESENT(pte)) {
572
        if (PTE_PRESENT(pte)) {
569
            page_table_unlock(AS, false);
573
            page_table_unlock(AS, false);
570
            spinlock_unlock(&area->lock);
574
            mutex_unlock(&area->lock);
571
            spinlock_unlock(&AS->lock);
575
            mutex_unlock(&AS->lock);
572
            return 1;
576
            return 1;
573
        }
577
        }
574
    }
578
    }
575
 
579
 
576
    /*
580
    /*
Line 596... Line 600...
596
     * inserted into page tables.
600
     * inserted into page tables.
597
     */
601
     */
598
    page_mapping_insert(AS, page, frame, get_area_flags(area));
602
    page_mapping_insert(AS, page, frame, get_area_flags(area));
599
    page_table_unlock(AS, false);
603
    page_table_unlock(AS, false);
600
   
604
   
601
    spinlock_unlock(&area->lock);
605
    mutex_unlock(&area->lock);
602
    spinlock_unlock(&AS->lock);
606
    mutex_unlock(&AS->lock);
603
    return AS_PF_OK;
607
    return AS_PF_OK;
604
 
608
 
605
page_fault:
609
page_fault:
606
    if (!THREAD)
610
    if (!THREAD)
607
        return AS_PF_FAULT;
611
        return AS_PF_FAULT;
Line 619... Line 623...
619
    return AS_PF_DEFER;
623
    return AS_PF_DEFER;
620
}
624
}
621
 
625
 
622
/** Switch address spaces.
626
/** Switch address spaces.
623
 *
627
 *
-
 
628
 * Note that this function cannot sleep as it is essentially a part of
-
 
629
 * the scheduling. Sleeping here would lead to deadlock on wakeup.
-
 
630
 *
624
 * @param old Old address space or NULL.
631
 * @param old Old address space or NULL.
625
 * @param new New address space.
632
 * @param new New address space.
626
 */
633
 */
627
void as_switch(as_t *old, as_t *new)
634
void as_switch(as_t *old, as_t *new)
628
{
635
{
Line 634... Line 641...
634
 
641
 
635
    /*
642
    /*
636
     * First, take care of the old address space.
643
     * First, take care of the old address space.
637
     */
644
     */
638
    if (old) {
645
    if (old) {
639
        spinlock_lock(&old->lock);
646
        mutex_lock_active(&old->lock);
640
        ASSERT(old->refcount);
647
        ASSERT(old->refcount);
641
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
648
        if((--old->refcount == 0) && (old != AS_KERNEL)) {
642
            /*
649
            /*
643
             * The old address space is no longer active on
650
             * The old address space is no longer active on
644
             * any processor. It can be appended to the
651
             * any processor. It can be appended to the
Line 646... Line 653...
646
             * ASID.
653
             * ASID.
647
             */
654
             */
648
             ASSERT(old->asid != ASID_INVALID);
655
             ASSERT(old->asid != ASID_INVALID);
649
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
656
             list_append(&old->inactive_as_with_asid_link, &inactive_as_with_asid_head);
650
        }
657
        }
651
        spinlock_unlock(&old->lock);
658
        mutex_unlock(&old->lock);
652
    }
659
    }
653
 
660
 
654
    /*
661
    /*
655
     * Second, prepare the new address space.
662
     * Second, prepare the new address space.
656
     */
663
     */
657
    spinlock_lock(&new->lock);
664
    mutex_lock_active(&new->lock);
658
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
665
    if ((new->refcount++ == 0) && (new != AS_KERNEL)) {
659
        if (new->asid != ASID_INVALID)
666
        if (new->asid != ASID_INVALID)
660
            list_remove(&new->inactive_as_with_asid_link);
667
            list_remove(&new->inactive_as_with_asid_link);
661
        else
668
        else
662
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
669
            needs_asid = true;  /* defer call to asid_get() until new->lock is released */
663
    }
670
    }
664
    SET_PTL0_ADDRESS(new->page_table);
671
    SET_PTL0_ADDRESS(new->page_table);
665
    spinlock_unlock(&new->lock);
672
    mutex_unlock(&new->lock);
666
 
673
 
667
    if (needs_asid) {
674
    if (needs_asid) {
668
        /*
675
        /*
669
         * Allocation of new ASID was deferred
676
         * Allocation of new ASID was deferred
670
         * until now in order to avoid deadlock.
677
         * until now in order to avoid deadlock.
671
         */
678
         */
672
        asid_t asid;
679
        asid_t asid;
673
       
680
       
674
        asid = asid_get();
681
        asid = asid_get();
675
        spinlock_lock(&new->lock);
682
        mutex_lock_active(&new->lock);
676
        new->asid = asid;
683
        new->asid = asid;
677
        spinlock_unlock(&new->lock);
684
        mutex_unlock(&new->lock);
678
    }
685
    }
679
    spinlock_unlock(&as_lock);
686
    spinlock_unlock(&as_lock);
680
    interrupts_restore(ipl);
687
    interrupts_restore(ipl);
681
   
688
   
682
    /*
689
    /*
Line 796... Line 803...
796
    int i;
803
    int i;
797
   
804
   
798
    a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
805
    a = (as_area_t *) btree_search(&as->as_area_btree, va, &leaf);
799
    if (a) {
806
    if (a) {
800
        /* va is the base address of an address space area */
807
        /* va is the base address of an address space area */
801
        spinlock_lock(&a->lock);
808
        mutex_lock(&a->lock);
802
        return a;
809
        return a;
803
    }
810
    }
804
   
811
   
805
    /*
812
    /*
806
     * Search the leaf node and the righmost record of its left neighbour
813
     * Search the leaf node and the righmost record of its left neighbour
Line 809... Line 816...
809
     */
816
     */
810
   
817
   
811
    /* First, search the leaf node itself. */
818
    /* First, search the leaf node itself. */
812
    for (i = 0; i < leaf->keys; i++) {
819
    for (i = 0; i < leaf->keys; i++) {
813
        a = (as_area_t *) leaf->value[i];
820
        a = (as_area_t *) leaf->value[i];
814
        spinlock_lock(&a->lock);
821
        mutex_lock(&a->lock);
815
        if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
822
        if ((a->base <= va) && (va < a->base + a->pages * PAGE_SIZE)) {
816
            return a;
823
            return a;
817
        }
824
        }
818
        spinlock_unlock(&a->lock);
825
        mutex_unlock(&a->lock);
819
    }
826
    }
820
 
827
 
821
    /*
828
    /*
822
     * Second, locate the left neighbour and test its last record.
829
     * Second, locate the left neighbour and test its last record.
823
     * Because of its position in the B+tree, it must have base < va.
830
     * Because of its position in the B+tree, it must have base < va.
824
     */
831
     */
825
    if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
832
    if ((lnode = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
826
        a = (as_area_t *) lnode->value[lnode->keys - 1];
833
        a = (as_area_t *) lnode->value[lnode->keys - 1];
827
        spinlock_lock(&a->lock);
834
        mutex_lock(&a->lock);
828
        if (va < a->base + a->pages * PAGE_SIZE) {
835
        if (va < a->base + a->pages * PAGE_SIZE) {
829
            return a;
836
            return a;
830
        }
837
        }
831
        spinlock_unlock(&a->lock);
838
        mutex_unlock(&a->lock);
832
    }
839
    }
833
 
840
 
834
    return NULL;
841
    return NULL;
835
}
842
}
836
 
843
 
Line 871... Line 878...
871
    }
878
    }
872
   
879
   
873
    /* First, check the two border cases. */
880
    /* First, check the two border cases. */
874
    if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
881
    if ((node = btree_leaf_node_left_neighbour(&as->as_area_btree, leaf))) {
875
        a = (as_area_t *) node->value[node->keys - 1];
882
        a = (as_area_t *) node->value[node->keys - 1];
876
        spinlock_lock(&a->lock);
883
        mutex_lock(&a->lock);
877
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
884
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
878
            spinlock_unlock(&a->lock);
885
            mutex_unlock(&a->lock);
879
            return false;
886
            return false;
880
        }
887
        }
881
        spinlock_unlock(&a->lock);
888
        mutex_unlock(&a->lock);
882
    }
889
    }
883
    if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
890
    if ((node = btree_leaf_node_right_neighbour(&as->as_area_btree, leaf))) {
884
        a = (as_area_t *) node->value[0];
891
        a = (as_area_t *) node->value[0];
885
        spinlock_lock(&a->lock);
892
        mutex_lock(&a->lock);
886
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
893
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
887
            spinlock_unlock(&a->lock);
894
            mutex_unlock(&a->lock);
888
            return false;
895
            return false;
889
        }
896
        }
890
        spinlock_unlock(&a->lock);
897
        mutex_unlock(&a->lock);
891
    }
898
    }
892
   
899
   
893
    /* Second, check the leaf node. */
900
    /* Second, check the leaf node. */
894
    for (i = 0; i < leaf->keys; i++) {
901
    for (i = 0; i < leaf->keys; i++) {
895
        a = (as_area_t *) leaf->value[i];
902
        a = (as_area_t *) leaf->value[i];
896
   
903
   
897
        if (a == avoid_area)
904
        if (a == avoid_area)
898
            continue;
905
            continue;
899
   
906
   
900
        spinlock_lock(&a->lock);
907
        mutex_lock(&a->lock);
901
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
908
        if (overlaps(va, size, a->base, a->pages * PAGE_SIZE)) {
902
            spinlock_unlock(&a->lock);
909
            mutex_unlock(&a->lock);
903
            return false;
910
            return false;
904
        }
911
        }
905
        spinlock_unlock(&a->lock);
912
        mutex_unlock(&a->lock);
906
    }
913
    }
907
 
914
 
908
    /*
915
    /*
909
     * So far, the area does not conflict with other areas.
916
     * So far, the area does not conflict with other areas.
910
     * Check if it doesn't conflict with kernel address space.
917
     * Check if it doesn't conflict with kernel address space.
Line 915... Line 922...
915
    }
922
    }
916
 
923
 
917
    return true;
924
    return true;
918
}
925
}
919
 
926
 
920
/** Return size of address space of current task pointed to by base */
927
/** Return size of the address space area with given base.  */
921
size_t as_get_size(__address base)
928
size_t as_get_size(__address base)
922
{
929
{
923
    ipl_t ipl;
930
    ipl_t ipl;
924
    as_area_t *src_area;
931
    as_area_t *src_area;
925
    size_t size;
932
    size_t size;
926
 
933
 
927
    ipl = interrupts_disable();
934
    ipl = interrupts_disable();
928
    src_area = find_area_and_lock(AS, base);
935
    src_area = find_area_and_lock(AS, base);
929
    if (src_area){
936
    if (src_area){
930
        size = src_area->pages * PAGE_SIZE;
937
        size = src_area->pages * PAGE_SIZE;
931
        spinlock_unlock(&src_area->lock);
938
        mutex_unlock(&src_area->lock);
932
    } else {
939
    } else {
933
        size = 0;
940
        size = 0;
934
    }
941
    }
935
    interrupts_restore(ipl);
942
    interrupts_restore(ipl);
936
    return size;
943
    return size;