Subversion Repositories HelenOS

Rev

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

Rev 3022 Rev 4055
Line 80... Line 80...
80
 
80
 
81
#ifdef CONFIG_VIRT_IDX_DCACHE
81
#ifdef CONFIG_VIRT_IDX_DCACHE
82
#include <arch/mm/cache.h>
82
#include <arch/mm/cache.h>
83
#endif /* CONFIG_VIRT_IDX_DCACHE */
83
#endif /* CONFIG_VIRT_IDX_DCACHE */
84
 
84
 
85
#ifndef __OBJC__
-
 
86
/**
85
/**
87
 * Each architecture decides what functions will be used to carry out
86
 * Each architecture decides what functions will be used to carry out
88
 * address space operations such as creating or locking page tables.
87
 * address space operations such as creating or locking page tables.
89
 */
88
 */
90
as_operations_t *as_operations = NULL;
89
as_operations_t *as_operations = NULL;
91
 
90
 
92
/**
91
/**
93
 * Slab for as_t objects.
92
 * Slab for as_t objects.
94
 */
93
 */
95
static slab_cache_t *as_slab;
94
static slab_cache_t *as_slab;
96
#endif
-
 
97
 
95
 
98
/**
96
/**
99
 * This lock serializes access to the ASID subsystem.
97
 * This lock serializes access to the ASID subsystem.
100
 * It protects:
98
 * It protects:
101
 * - inactive_as_with_asid_head list
99
 * - inactive_as_with_asid_head list
Line 111... Line 109...
111
LIST_INITIALIZE(inactive_as_with_asid_head);
109
LIST_INITIALIZE(inactive_as_with_asid_head);
112
 
110
 
113
/** Kernel address space. */
111
/** Kernel address space. */
114
as_t *AS_KERNEL = NULL;
112
as_t *AS_KERNEL = NULL;
115
 
113
 
116
static int area_flags_to_page_flags(int aflags);
114
static int area_flags_to_page_flags(int);
117
static as_area_t *find_area_and_lock(as_t *as, uintptr_t va);
115
static as_area_t *find_area_and_lock(as_t *, uintptr_t);
118
static bool check_area_conflicts(as_t *as, uintptr_t va, size_t size,
116
static bool check_area_conflicts(as_t *, uintptr_t, size_t, as_area_t *);
119
    as_area_t *avoid_area);
-
 
120
static void sh_info_remove_reference(share_info_t *sh_info);
117
static void sh_info_remove_reference(share_info_t *);
121
 
118
 
122
#ifndef __OBJC__
-
 
123
static int as_constructor(void *obj, int flags)
119
static int as_constructor(void *obj, int flags)
124
{
120
{
125
    as_t *as = (as_t *) obj;
121
    as_t *as = (as_t *) obj;
126
    int rc;
122
    int rc;
127
 
123
 
128
    link_initialize(&as->inactive_as_with_asid_link);
124
    link_initialize(&as->inactive_as_with_asid_link);
129
    mutex_initialize(&as->lock);   
125
    mutex_initialize(&as->lock, MUTEX_PASSIVE);
130
   
126
   
131
    rc = as_constructor_arch(as, flags);
127
    rc = as_constructor_arch(as, flags);
132
   
128
   
133
    return rc;
129
    return rc;
134
}
130
}
Line 137... Line 133...
137
{
133
{
138
    as_t *as = (as_t *) obj;
134
    as_t *as = (as_t *) obj;
139
 
135
 
140
    return as_destructor_arch(as);
136
    return as_destructor_arch(as);
141
}
137
}
142
#endif
-
 
143
 
138
 
144
/** Initialize address space subsystem. */
139
/** Initialize address space subsystem. */
145
void as_init(void)
140
void as_init(void)
146
{
141
{
147
    as_arch_init();
142
    as_arch_init();
148
 
143
 
149
#ifndef __OBJC__
-
 
150
    as_slab = slab_cache_create("as_slab", sizeof(as_t), 0,
144
    as_slab = slab_cache_create("as_slab", sizeof(as_t), 0,
151
        as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED);
145
        as_constructor, as_destructor, SLAB_CACHE_MAGDEFERRED);
152
#endif
-
 
153
   
146
   
154
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
147
    AS_KERNEL = as_create(FLAG_AS_KERNEL);
155
    if (!AS_KERNEL)
148
    if (!AS_KERNEL)
156
        panic("can't create kernel address space\n");
149
        panic("Cannot create kernel address space.");
157
   
150
   
-
 
151
    /* Make sure the kernel address space
-
 
152
     * reference count never drops to zero.
-
 
153
     */
-
 
154
    atomic_set(&AS_KERNEL->refcount, 1);
158
}
155
}
159
 
156
 
160
/** Create address space.
157
/** Create address space.
161
 *
158
 *
162
 * @param flags Flags that influence way in wich the address space is created.
159
 * @param flags     Flags that influence the way in wich the address space
-
 
160
 *          is created.
163
 */
161
 */
164
as_t *as_create(int flags)
162
as_t *as_create(int flags)
165
{
163
{
166
    as_t *as;
164
    as_t *as;
167
 
165
 
168
#ifdef __OBJC__
-
 
169
    as = [as_t new];
-
 
170
    link_initialize(&as->inactive_as_with_asid_link);
-
 
171
    mutex_initialize(&as->lock);   
-
 
172
    (void) as_constructor_arch(as, flags);
-
 
173
#else
-
 
174
    as = (as_t *) slab_alloc(as_slab, 0);
166
    as = (as_t *) slab_alloc(as_slab, 0);
175
#endif
-
 
176
    (void) as_create_arch(as, 0);
167
    (void) as_create_arch(as, 0);
177
   
168
   
178
    btree_create(&as->as_area_btree);
169
    btree_create(&as->as_area_btree);
179
   
170
   
180
    if (flags & FLAG_AS_KERNEL)
171
    if (flags & FLAG_AS_KERNEL)
Line 187... Line 178...
187
#ifdef AS_PAGE_TABLE
178
#ifdef AS_PAGE_TABLE
188
    as->genarch.page_table = page_table_create(flags);
179
    as->genarch.page_table = page_table_create(flags);
189
#else
180
#else
190
    page_table_create(flags);
181
    page_table_create(flags);
191
#endif
182
#endif
192
 
183
   
193
    return as;
184
    return as;
194
}
185
}
195
 
186
 
196
/** Destroy adress space.
187
/** Destroy adress space.
197
 *
188
 *
198
 * When there are no tasks referencing this address space (i.e. its refcount is
189
 * When there are no tasks referencing this address space (i.e. its refcount is
199
 * zero), the address space can be destroyed.
190
 * zero), the address space can be destroyed.
200
 *
191
 *
201
 * We know that we don't hold any spinlock.
192
 * We know that we don't hold any spinlock.
-
 
193
 *
-
 
194
 * @param as        Address space to be destroyed.
202
 */
195
 */
203
void as_destroy(as_t *as)
196
void as_destroy(as_t *as)
204
{
197
{
205
    ipl_t ipl;
198
    ipl_t ipl;
206
    bool cond;
199
    bool cond;
Line 261... Line 254...
261
    page_table_destroy(NULL);
254
    page_table_destroy(NULL);
262
#endif
255
#endif
263
 
256
 
264
    interrupts_restore(ipl);
257
    interrupts_restore(ipl);
265
 
258
 
266
#ifdef __OBJC__
-
 
267
    [as free];
-
 
268
#else
-
 
269
    slab_free(as_slab, as);
259
    slab_free(as_slab, as);
270
#endif
-
 
271
}
260
}
272
 
261
 
273
/** Create address space area of common attributes.
262
/** Create address space area of common attributes.
274
 *
263
 *
275
 * The created address space area is added to the target address space.
264
 * The created address space area is added to the target address space.
276
 *
265
 *
277
 * @param as Target address space.
266
 * @param as        Target address space.
278
 * @param flags Flags of the area memory.
267
 * @param flags     Flags of the area memory.
279
 * @param size Size of area.
268
 * @param size      Size of area.
280
 * @param base Base address of area.
269
 * @param base      Base address of area.
281
 * @param attrs Attributes of the area.
270
 * @param attrs     Attributes of the area.
282
 * @param backend Address space area backend. NULL if no backend is used.
271
 * @param backend   Address space area backend. NULL if no backend is used.
283
 * @param backend_data NULL or a pointer to an array holding two void *.
272
 * @param backend_data  NULL or a pointer to an array holding two void *.
284
 *
273
 *
285
 * @return Address space area on success or NULL on failure.
274
 * @return      Address space area on success or NULL on failure.
286
 */
275
 */
287
as_area_t *
276
as_area_t *
288
as_area_create(as_t *as, int flags, size_t size, uintptr_t base, int attrs,
277
as_area_create(as_t *as, int flags, size_t size, uintptr_t base, int attrs,
289
           mem_backend_t *backend, mem_backend_data_t *backend_data)
278
    mem_backend_t *backend, mem_backend_data_t *backend_data)
290
{
279
{
291
    ipl_t ipl;
280
    ipl_t ipl;
292
    as_area_t *a;
281
    as_area_t *a;
293
   
282
   
294
    if (base % PAGE_SIZE)
283
    if (base % PAGE_SIZE)
Line 310... Line 299...
310
        return NULL;
299
        return NULL;
311
    }
300
    }
312
   
301
   
313
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
302
    a = (as_area_t *) malloc(sizeof(as_area_t), 0);
314
 
303
 
315
    mutex_initialize(&a->lock);
304
    mutex_initialize(&a->lock, MUTEX_PASSIVE);
316
   
305
   
317
    a->as = as;
306
    a->as = as;
318
    a->flags = flags;
307
    a->flags = flags;
319
    a->attributes = attrs;
308
    a->attributes = attrs;
320
    a->pages = SIZE2FRAMES(size);
309
    a->pages = SIZE2FRAMES(size);
Line 322... Line 311...
322
    a->sh_info = NULL;
311
    a->sh_info = NULL;
323
    a->backend = backend;
312
    a->backend = backend;
324
    if (backend_data)
313
    if (backend_data)
325
        a->backend_data = *backend_data;
314
        a->backend_data = *backend_data;
326
    else
315
    else
327
        memsetb((uintptr_t) &a->backend_data, sizeof(a->backend_data),
316
        memsetb(&a->backend_data, sizeof(a->backend_data), 0);
328
            0);
-
 
329
 
317
 
330
    btree_create(&a->used_space);
318
    btree_create(&a->used_space);
331
   
319
   
332
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
320
    btree_insert(&as->as_area_btree, base, (void *) a, NULL);
333
 
321
 
Line 337... Line 325...
337
    return a;
325
    return a;
338
}
326
}
339
 
327
 
340
/** Find address space area and change it.
328
/** Find address space area and change it.
341
 *
329
 *
342
 * @param as Address space.
330
 * @param as        Address space.
343
 * @param address Virtual address belonging to the area to be changed. Must be
331
 * @param address   Virtual address belonging to the area to be changed.
344
 *     page-aligned.
332
 *          Must be page-aligned.
345
 * @param size New size of the virtual memory block starting at address.
333
 * @param size      New size of the virtual memory block starting at
-
 
334
 *          address.
346
 * @param flags Flags influencing the remap operation. Currently unused.
335
 * @param flags     Flags influencing the remap operation. Currently unused.
347
 *
336
 *
348
 * @return Zero on success or a value from @ref errno.h otherwise.
337
 * @return      Zero on success or a value from @ref errno.h otherwise.
349
 */
338
 */
350
int as_area_resize(as_t *as, uintptr_t address, size_t size, int flags)
339
int as_area_resize(as_t *as, uintptr_t address, size_t size, int flags)
351
{
340
{
352
    as_area_t *area;
341
    as_area_t *area;
353
    ipl_t ipl;
342
    ipl_t ipl;
Line 398... Line 387...
398
        return EPERM;
387
        return EPERM;
399
    }
388
    }
400
   
389
   
401
    if (pages < area->pages) {
390
    if (pages < area->pages) {
402
        bool cond;
391
        bool cond;
403
        uintptr_t start_free = area->base + pages*PAGE_SIZE;
392
        uintptr_t start_free = area->base + pages * PAGE_SIZE;
404
 
393
 
405
        /*
394
        /*
406
         * Shrinking the area.
395
         * Shrinking the area.
407
         * No need to check for overlaps.
396
         * No need to check for overlaps.
408
         */
397
         */
409
 
398
 
410
        /*
399
        /*
411
         * Start TLB shootdown sequence.
400
         * Start TLB shootdown sequence.
412
         */
401
         */
413
        tlb_shootdown_start(TLB_INVL_PAGES, AS->asid, area->base +
402
        tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base +
414
            pages * PAGE_SIZE, area->pages - pages);
403
            pages * PAGE_SIZE, area->pages - pages);
415
 
404
 
416
        /*
405
        /*
417
         * Remove frames belonging to used space starting from
406
         * Remove frames belonging to used space starting from
418
         * the highest addresses downwards until an overlap with
407
         * the highest addresses downwards until an overlap with
Line 453... Line 442...
453
       
442
       
454
                    cond = false;   /* we are almost done */
443
                    cond = false;   /* we are almost done */
455
                    i = (start_free - b) >> PAGE_WIDTH;
444
                    i = (start_free - b) >> PAGE_WIDTH;
456
                    if (!used_space_remove(area, start_free,
445
                    if (!used_space_remove(area, start_free,
457
                        c - i))
446
                        c - i))
458
                        panic("Could not remove used "
447
                        panic("Cannot remove used "
459
                            "space.\n");
448
                            "space.");
460
                } else {
449
                } else {
461
                    /*
450
                    /*
462
                     * The interval of used space can be
451
                     * The interval of used space can be
463
                     * completely removed.
452
                     * completely removed.
464
                     */
453
                     */
465
                    if (!used_space_remove(area, b, c))
454
                    if (!used_space_remove(area, b, c))
466
                        panic("Could not remove used "
455
                        panic("Cannot remove used "
467
                            "space.\n");
456
                            "space.");
468
                }
457
                }
469
           
458
           
470
                for (; i < c; i++) {
459
                for (; i < c; i++) {
471
                    pte_t *pte;
460
                    pte_t *pte;
472
           
461
           
Line 524... Line 513...
524
    return 0;
513
    return 0;
525
}
514
}
526
 
515
 
527
/** Destroy address space area.
516
/** Destroy address space area.
528
 *
517
 *
529
 * @param as Address space.
518
 * @param as        Address space.
530
 * @param address Address withing the area to be deleted.
519
 * @param address   Address within the area to be deleted.
531
 *
520
 *
532
 * @return Zero on success or a value from @ref errno.h on failure.
521
 * @return      Zero on success or a value from @ref errno.h on failure.
533
 */
522
 */
534
int as_area_destroy(as_t *as, uintptr_t address)
523
int as_area_destroy(as_t *as, uintptr_t address)
535
{
524
{
536
    as_area_t *area;
525
    as_area_t *area;
537
    uintptr_t base;
526
    uintptr_t base;
Line 624... Line 613...
624
 * If the source address space area has not been shared so far,
613
 * If the source address space area has not been shared so far,
625
 * a new sh_info is created. The new address space area simply gets the
614
 * a new sh_info is created. The new address space area simply gets the
626
 * sh_info of the source area. The process of duplicating the
615
 * sh_info of the source area. The process of duplicating the
627
 * mapping is done through the backend share function.
616
 * mapping is done through the backend share function.
628
 *
617
 *
629
 * @param src_as Pointer to source address space.
618
 * @param src_as    Pointer to source address space.
630
 * @param src_base Base address of the source address space area.
619
 * @param src_base  Base address of the source address space area.
631
 * @param acc_size Expected size of the source area.
620
 * @param acc_size  Expected size of the source area.
632
 * @param dst_as Pointer to destination address space.
621
 * @param dst_as    Pointer to destination address space.
633
 * @param dst_base Target base address.
622
 * @param dst_base  Target base address.
634
 * @param dst_flags_mask Destination address space area flags mask.
623
 * @param dst_flags_mask Destination address space area flags mask.
635
 *
624
 *
636
 * @return Zero on success or ENOENT if there is no such task or if there is no
625
 * @return      Zero on success or ENOENT if there is no such task or if
637
 * such address space area, EPERM if there was a problem in accepting the area
626
 *          there is no such address space area, EPERM if there was
-
 
627
 *          a problem in accepting the area or ENOMEM if there was a
638
 * or ENOMEM if there was a problem in allocating destination address space
628
 *          problem in allocating destination address space area.
639
 * area. ENOTSUP is returned if the address space area backend does not support
629
 *          ENOTSUP is returned if the address space area backend
640
 * sharing.
630
 *          does not support sharing.
641
 */
631
 */
642
int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
632
int as_area_share(as_t *src_as, uintptr_t src_base, size_t acc_size,
643
    as_t *dst_as, uintptr_t dst_base, int dst_flags_mask)
633
    as_t *dst_as, uintptr_t dst_base, int dst_flags_mask)
644
{
634
{
645
    ipl_t ipl;
635
    ipl_t ipl;
Line 696... Line 686...
696
     * Then it will be safe to unlock it.
686
     * Then it will be safe to unlock it.
697
     */
687
     */
698
    sh_info = src_area->sh_info;
688
    sh_info = src_area->sh_info;
699
    if (!sh_info) {
689
    if (!sh_info) {
700
        sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
690
        sh_info = (share_info_t *) malloc(sizeof(share_info_t), 0);
701
        mutex_initialize(&sh_info->lock);
691
        mutex_initialize(&sh_info->lock, MUTEX_PASSIVE);
702
        sh_info->refcount = 2;
692
        sh_info->refcount = 2;
703
        btree_create(&sh_info->pagemap);
693
        btree_create(&sh_info->pagemap);
704
        src_area->sh_info = sh_info;
694
        src_area->sh_info = sh_info;
705
        /*
695
        /*
706
         * Call the backend to setup sharing.
696
         * Call the backend to setup sharing.
Line 754... Line 744...
754
 
744
 
755
/** Check access mode for address space area.
745
/** Check access mode for address space area.
756
 *
746
 *
757
 * The address space area must be locked prior to this call.
747
 * The address space area must be locked prior to this call.
758
 *
748
 *
759
 * @param area Address space area.
749
 * @param area      Address space area.
760
 * @param access Access mode.
750
 * @param access    Access mode.
761
 *
751
 *
762
 * @return False if access violates area's permissions, true otherwise.
752
 * @return      False if access violates area's permissions, true
-
 
753
 *          otherwise.
763
 */
754
 */
764
bool as_area_check_access(as_area_t *area, pf_access_t access)
755
bool as_area_check_access(as_area_t *area, pf_access_t access)
765
{
756
{
766
    int flagmap[] = {
757
    int flagmap[] = {
767
        [PF_ACCESS_READ] = AS_AREA_READ,
758
        [PF_ACCESS_READ] = AS_AREA_READ,
Line 773... Line 764...
773
        return false;
764
        return false;
774
   
765
   
775
    return true;
766
    return true;
776
}
767
}
777
 
768
 
-
 
769
/** Change adress space area flags.
-
 
770
 *
-
 
771
 * The idea is to have the same data, but with a different access mode.
-
 
772
 * This is needed e.g. for writing code into memory and then executing it.
-
 
773
 * In order for this to work properly, this may copy the data
-
 
774
 * into private anonymous memory (unless it's already there).
-
 
775
 *
-
 
776
 * @param as      Address space.
-
 
777
 * @param flags   Flags of the area memory.
-
 
778
 * @param address Address within the area to be changed.
-
 
779
 *
-
 
780
 * @return Zero on success or a value from @ref errno.h on failure.
-
 
781
 *
-
 
782
 */
-
 
783
int as_area_change_flags(as_t *as, int flags, uintptr_t address)
-
 
784
{
-
 
785
    as_area_t *area;
-
 
786
    uintptr_t base;
-
 
787
    link_t *cur;
-
 
788
    ipl_t ipl;
-
 
789
    int page_flags;
-
 
790
    uintptr_t *old_frame;
-
 
791
    index_t frame_idx;
-
 
792
    count_t used_pages;
-
 
793
   
-
 
794
    /* Flags for the new memory mapping */
-
 
795
    page_flags = area_flags_to_page_flags(flags);
-
 
796
 
-
 
797
    ipl = interrupts_disable();
-
 
798
    mutex_lock(&as->lock);
-
 
799
 
-
 
800
    area = find_area_and_lock(as, address);
-
 
801
    if (!area) {
-
 
802
        mutex_unlock(&as->lock);
-
 
803
        interrupts_restore(ipl);
-
 
804
        return ENOENT;
-
 
805
    }
-
 
806
 
-
 
807
    if ((area->sh_info) || (area->backend != &anon_backend)) {
-
 
808
        /* Copying shared areas not supported yet */
-
 
809
        /* Copying non-anonymous memory not supported yet */
-
 
810
        mutex_unlock(&area->lock);
-
 
811
        mutex_unlock(&as->lock);
-
 
812
        interrupts_restore(ipl);
-
 
813
        return ENOTSUP;
-
 
814
    }
-
 
815
 
-
 
816
    base = area->base;
-
 
817
 
-
 
818
    /*
-
 
819
     * Compute total number of used pages in the used_space B+tree
-
 
820
     */
-
 
821
    used_pages = 0;
-
 
822
 
-
 
823
    for (cur = area->used_space.leaf_head.next;
-
 
824
        cur != &area->used_space.leaf_head; cur = cur->next) {
-
 
825
        btree_node_t *node;
-
 
826
        unsigned int i;
-
 
827
       
-
 
828
        node = list_get_instance(cur, btree_node_t, leaf_link);
-
 
829
        for (i = 0; i < node->keys; i++) {
-
 
830
            used_pages += (count_t) node->value[i];
-
 
831
        }
-
 
832
    }
-
 
833
 
-
 
834
    /* An array for storing frame numbers */
-
 
835
    old_frame = malloc(used_pages * sizeof(uintptr_t), 0);
-
 
836
 
-
 
837
    /*
-
 
838
     * Start TLB shootdown sequence.
-
 
839
     */
-
 
840
    tlb_shootdown_start(TLB_INVL_PAGES, as->asid, area->base, area->pages);
-
 
841
 
-
 
842
    /*
-
 
843
     * Remove used pages from page tables and remember their frame
-
 
844
     * numbers.
-
 
845
     */
-
 
846
    frame_idx = 0;
-
 
847
 
-
 
848
    for (cur = area->used_space.leaf_head.next;
-
 
849
        cur != &area->used_space.leaf_head; cur = cur->next) {
-
 
850
        btree_node_t *node;
-
 
851
        unsigned int i;
-
 
852
       
-
 
853
        node = list_get_instance(cur, btree_node_t, leaf_link);
-
 
854
        for (i = 0; i < node->keys; i++) {
-
 
855
            uintptr_t b = node->key[i];
-
 
856
            count_t j;
-
 
857
            pte_t *pte;
-
 
858
           
-
 
859
            for (j = 0; j < (count_t) node->value[i]; j++) {
-
 
860
                page_table_lock(as, false);
-
 
861
                pte = page_mapping_find(as, b + j * PAGE_SIZE);
-
 
862
                ASSERT(pte && PTE_VALID(pte) &&
-
 
863
                    PTE_PRESENT(pte));
-
 
864
                old_frame[frame_idx++] = PTE_GET_FRAME(pte);
-
 
865
 
-
 
866
                /* Remove old mapping */
-
 
867
                page_mapping_remove(as, b + j * PAGE_SIZE);
-
 
868
                page_table_unlock(as, false);
-
 
869
            }
-
 
870
        }
-
 
871
    }
-
 
872
 
-
 
873
    /*
-
 
874
     * Finish TLB shootdown sequence.
-
 
875
     */
-
 
876
 
-
 
877
    tlb_invalidate_pages(as->asid, area->base, area->pages);
-
 
878
   
-
 
879
    /*
-
 
880
     * Invalidate potential software translation caches (e.g. TSB on
-
 
881
     * sparc64).
-
 
882
     */
-
 
883
    as_invalidate_translation_cache(as, area->base, area->pages);
-
 
884
    tlb_shootdown_finalize();
-
 
885
 
-
 
886
    /*
-
 
887
     * Set the new flags.
-
 
888
     */
-
 
889
    area->flags = flags;
-
 
890
 
-
 
891
    /*
-
 
892
     * Map pages back in with new flags. This step is kept separate
-
 
893
     * so that the memory area could not be accesed with both the old and
-
 
894
     * the new flags at once.
-
 
895
     */
-
 
896
    frame_idx = 0;
-
 
897
 
-
 
898
    for (cur = area->used_space.leaf_head.next;
-
 
899
        cur != &area->used_space.leaf_head; cur = cur->next) {
-
 
900
        btree_node_t *node;
-
 
901
        unsigned int i;
-
 
902
       
-
 
903
        node = list_get_instance(cur, btree_node_t, leaf_link);
-
 
904
        for (i = 0; i < node->keys; i++) {
-
 
905
            uintptr_t b = node->key[i];
-
 
906
            count_t j;
-
 
907
           
-
 
908
            for (j = 0; j < (count_t) node->value[i]; j++) {
-
 
909
                page_table_lock(as, false);
-
 
910
 
-
 
911
                /* Insert the new mapping */
-
 
912
                page_mapping_insert(as, b + j * PAGE_SIZE,
-
 
913
                    old_frame[frame_idx++], page_flags);
-
 
914
 
-
 
915
                page_table_unlock(as, false);
-
 
916
            }
-
 
917
        }
-
 
918
    }
-
 
919
 
-
 
920
    free(old_frame);
-
 
921
 
-
 
922
    mutex_unlock(&area->lock);
-
 
923
    mutex_unlock(&as->lock);
-
 
924
    interrupts_restore(ipl);
-
 
925
 
-
 
926
    return 0;
-
 
927
}
-
 
928
 
-
 
929
 
778
/** Handle page fault within the current address space.
930
/** Handle page fault within the current address space.
779
 *
931
 *
780
 * This is the high-level page fault handler. It decides
932
 * This is the high-level page fault handler. It decides whether the page fault
781
 * whether the page fault can be resolved by any backend
-
 
782
 * and if so, it invokes the backend to resolve the page
933
 * can be resolved by any backend and if so, it invokes the backend to resolve
783
 * fault.
934
 * the page fault.
784
 *
935
 *
785
 * Interrupts are assumed disabled.
936
 * Interrupts are assumed disabled.
786
 *
937
 *
787
 * @param page Faulting page.
938
 * @param page      Faulting page.
788
 * @param access Access mode that caused the fault (i.e. read/write/exec).
939
 * @param access    Access mode that caused the page fault (i.e.
-
 
940
 *          read/write/exec).
789
 * @param istate Pointer to interrupted state.
941
 * @param istate    Pointer to the interrupted state.
790
 *
942
 *
791
 * @return AS_PF_FAULT on page fault, AS_PF_OK on success or AS_PF_DEFER if the
943
 * @return      AS_PF_FAULT on page fault, AS_PF_OK on success or
792
 *     fault was caused by copy_to_uspace() or copy_from_uspace().
944
 *          AS_PF_DEFER if the fault was caused by copy_to_uspace()
-
 
945
 *          or copy_from_uspace().
793
 */
946
 */
794
int as_page_fault(uintptr_t page, pf_access_t access, istate_t *istate)
947
int as_page_fault(uintptr_t page, pf_access_t access, istate_t *istate)
795
{
948
{
796
    pte_t *pte;
949
    pte_t *pte;
797
    as_area_t *area;
950
    as_area_t *area;
Line 833... Line 986...
833
    }
986
    }
834
 
987
 
835
    page_table_lock(AS, false);
988
    page_table_lock(AS, false);
836
   
989
   
837
    /*
990
    /*
838
     * To avoid race condition between two page faults
991
     * To avoid race condition between two page faults on the same address,
839
     * on the same address, we need to make sure
-
 
840
     * the mapping has not been already inserted.
992
     * we need to make sure the mapping has not been already inserted.
841
     */
993
     */
842
    if ((pte = page_mapping_find(AS, page))) {
994
    if ((pte = page_mapping_find(AS, page))) {
843
        if (PTE_PRESENT(pte)) {
995
        if (PTE_PRESENT(pte)) {
844
            if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) ||
996
            if (((access == PF_ACCESS_READ) && PTE_READABLE(pte)) ||
845
                (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) ||
997
                (access == PF_ACCESS_WRITE && PTE_WRITABLE(pte)) ||
Line 889... Line 1041...
889
 * scheduling. Sleeping here would lead to deadlock on wakeup. Another
1041
 * scheduling. Sleeping here would lead to deadlock on wakeup. Another
890
 * thing which is forbidden in this context is locking the address space.
1042
 * thing which is forbidden in this context is locking the address space.
891
 *
1043
 *
892
 * When this function is enetered, no spinlocks may be held.
1044
 * When this function is enetered, no spinlocks may be held.
893
 *
1045
 *
894
 * @param old Old address space or NULL.
1046
 * @param old       Old address space or NULL.
895
 * @param new New address space.
1047
 * @param new       New address space.
896
 */
1048
 */
897
void as_switch(as_t *old_as, as_t *new_as)
1049
void as_switch(as_t *old_as, as_t *new_as)
898
{
1050
{
899
    DEADLOCK_PROBE_INIT(p_asidlock);
1051
    DEADLOCK_PROBE_INIT(p_asidlock);
900
    preemption_disable();
1052
    preemption_disable();
Line 961... Line 1113...
961
    AS = new_as;
1113
    AS = new_as;
962
}
1114
}
963
 
1115
 
964
/** Convert address space area flags to page flags.
1116
/** Convert address space area flags to page flags.
965
 *
1117
 *
966
 * @param aflags Flags of some address space area.
1118
 * @param aflags    Flags of some address space area.
967
 *
1119
 *
968
 * @return Flags to be passed to page_mapping_insert().
1120
 * @return      Flags to be passed to page_mapping_insert().
969
 */
1121
 */
970
int area_flags_to_page_flags(int aflags)
1122
int area_flags_to_page_flags(int aflags)
971
{
1123
{
972
    int flags;
1124
    int flags;
973
 
1125
 
Line 991... Line 1143...
991
/** Compute flags for virtual address translation subsytem.
1143
/** Compute flags for virtual address translation subsytem.
992
 *
1144
 *
993
 * The address space area must be locked.
1145
 * The address space area must be locked.
994
 * Interrupts must be disabled.
1146
 * Interrupts must be disabled.
995
 *
1147
 *
996
 * @param a Address space area.
1148
 * @param a     Address space area.
997
 *
1149
 *
998
 * @return Flags to be used in page_mapping_insert().
1150
 * @return      Flags to be used in page_mapping_insert().
999
 */
1151
 */
1000
int as_area_get_flags(as_area_t *a)
1152
int as_area_get_flags(as_area_t *a)
1001
{
1153
{
1002
    return area_flags_to_page_flags(a->flags);
1154
    return area_flags_to_page_flags(a->flags);
1003
}
1155
}
1004
 
1156
 
1005
/** Create page table.
1157
/** Create page table.
1006
 *
1158
 *
1007
 * Depending on architecture, create either address space
1159
 * Depending on architecture, create either address space private or global page
1008
 * private or global page table.
1160
 * table.
1009
 *
1161
 *
1010
 * @param flags Flags saying whether the page table is for kernel address space.
1162
 * @param flags     Flags saying whether the page table is for the kernel
-
 
1163
 *          address space.
1011
 *
1164
 *
1012
 * @return First entry of the page table.
1165
 * @return      First entry of the page table.
1013
 */
1166
 */
1014
pte_t *page_table_create(int flags)
1167
pte_t *page_table_create(int flags)
1015
{
1168
{
1016
#ifdef __OBJC__
-
 
1017
    return [as_t page_table_create: flags];
-
 
1018
#else
-
 
1019
    ASSERT(as_operations);
1169
    ASSERT(as_operations);
1020
    ASSERT(as_operations->page_table_create);
1170
    ASSERT(as_operations->page_table_create);
1021
   
1171
   
1022
    return as_operations->page_table_create(flags);
1172
    return as_operations->page_table_create(flags);
1023
#endif
-
 
1024
}
1173
}
1025
 
1174
 
1026
/** Destroy page table.
1175
/** Destroy page table.
1027
 *
1176
 *
1028
 * Destroy page table in architecture specific way.
1177
 * Destroy page table in architecture specific way.
1029
 *
1178
 *
1030
 * @param page_table Physical address of PTL0.
1179
 * @param page_table    Physical address of PTL0.
1031
 */
1180
 */
1032
void page_table_destroy(pte_t *page_table)
1181
void page_table_destroy(pte_t *page_table)
1033
{
1182
{
1034
#ifdef __OBJC__
-
 
1035
    return [as_t page_table_destroy: page_table];
-
 
1036
#else
-
 
1037
    ASSERT(as_operations);
1183
    ASSERT(as_operations);
1038
    ASSERT(as_operations->page_table_destroy);
1184
    ASSERT(as_operations->page_table_destroy);
1039
   
1185
   
1040
    as_operations->page_table_destroy(page_table);
1186
    as_operations->page_table_destroy(page_table);
1041
#endif
-
 
1042
}
1187
}
1043
 
1188
 
1044
/** Lock page table.
1189
/** Lock page table.
1045
 *
1190
 *
1046
 * This function should be called before any page_mapping_insert(),
1191
 * This function should be called before any page_mapping_insert(),
Line 1048... Line 1193...
1048
 *
1193
 *
1049
 * Locking order is such that address space areas must be locked
1194
 * Locking order is such that address space areas must be locked
1050
 * prior to this call. Address space can be locked prior to this
1195
 * prior to this call. Address space can be locked prior to this
1051
 * call in which case the lock argument is false.
1196
 * call in which case the lock argument is false.
1052
 *
1197
 *
1053
 * @param as Address space.
1198
 * @param as        Address space.
1054
 * @param lock If false, do not attempt to lock as->lock.
1199
 * @param lock      If false, do not attempt to lock as->lock.
1055
 */
1200
 */
1056
void page_table_lock(as_t *as, bool lock)
1201
void page_table_lock(as_t *as, bool lock)
1057
{
1202
{
1058
#ifdef __OBJC__
-
 
1059
    [as page_table_lock: lock];
-
 
1060
#else
-
 
1061
    ASSERT(as_operations);
1203
    ASSERT(as_operations);
1062
    ASSERT(as_operations->page_table_lock);
1204
    ASSERT(as_operations->page_table_lock);
1063
   
1205
   
1064
    as_operations->page_table_lock(as, lock);
1206
    as_operations->page_table_lock(as, lock);
1065
#endif
-
 
1066
}
1207
}
1067
 
1208
 
1068
/** Unlock page table.
1209
/** Unlock page table.
1069
 *
1210
 *
1070
 * @param as Address space.
1211
 * @param as        Address space.
1071
 * @param unlock If false, do not attempt to unlock as->lock.
1212
 * @param unlock    If false, do not attempt to unlock as->lock.
1072
 */
1213
 */
1073
void page_table_unlock(as_t *as, bool unlock)
1214
void page_table_unlock(as_t *as, bool unlock)
1074
{
1215
{
1075
#ifdef __OBJC__
-
 
1076
    [as page_table_unlock: unlock];
-
 
1077
#else
-
 
1078
    ASSERT(as_operations);
1216
    ASSERT(as_operations);
1079
    ASSERT(as_operations->page_table_unlock);
1217
    ASSERT(as_operations->page_table_unlock);
1080
   
1218
   
1081
    as_operations->page_table_unlock(as, unlock);
1219
    as_operations->page_table_unlock(as, unlock);
1082
#endif
-
 
1083
}
1220
}
1084
 
1221
 
1085
 
1222
 
1086
/** Find address space area and lock it.
1223
/** Find address space area and lock it.
1087
 *
1224
 *
1088
 * The address space must be locked and interrupts must be disabled.
1225
 * The address space must be locked and interrupts must be disabled.
1089
 *
1226
 *
1090
 * @param as Address space.
1227
 * @param as        Address space.
1091
 * @param va Virtual address.
1228
 * @param va        Virtual address.
1092
 *
1229
 *
1093
 * @return Locked address space area containing va on success or NULL on
1230
 * @return      Locked address space area containing va on success or
1094
 *     failure.
1231
 *          NULL on failure.
1095
 */
1232
 */
1096
as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
1233
as_area_t *find_area_and_lock(as_t *as, uintptr_t va)
1097
{
1234
{
1098
    as_area_t *a;
1235
    as_area_t *a;
1099
    btree_node_t *leaf, *lnode;
1236
    btree_node_t *leaf, *lnode;
Line 1141... Line 1278...
1141
 
1278
 
1142
/** Check area conflicts with other areas.
1279
/** Check area conflicts with other areas.
1143
 *
1280
 *
1144
 * The address space must be locked and interrupts must be disabled.
1281
 * The address space must be locked and interrupts must be disabled.
1145
 *
1282
 *
1146
 * @param as Address space.
1283
 * @param as        Address space.
1147
 * @param va Starting virtual address of the area being tested.
1284
 * @param va        Starting virtual address of the area being tested.
1148
 * @param size Size of the area being tested.
1285
 * @param size      Size of the area being tested.
1149
 * @param avoid_area Do not touch this area.
1286
 * @param avoid_area    Do not touch this area.
1150
 *
1287
 *
1151
 * @return True if there is no conflict, false otherwise.
1288
 * @return      True if there is no conflict, false otherwise.
1152
 */
1289
 */
-
 
1290
bool
1153
bool check_area_conflicts(as_t *as, uintptr_t va, size_t size,
1291
check_area_conflicts(as_t *as, uintptr_t va, size_t size, as_area_t *avoid_area)
1154
              as_area_t *avoid_area)
-
 
1155
{
1292
{
1156
    as_area_t *a;
1293
    as_area_t *a;
1157
    btree_node_t *leaf, *node;
1294
    btree_node_t *leaf, *node;
1158
    unsigned int i;
1295
    unsigned int i;
1159
   
1296
   
Line 1238... Line 1375...
1238
    as_area_t *src_area;
1375
    as_area_t *src_area;
1239
    size_t size;
1376
    size_t size;
1240
 
1377
 
1241
    ipl = interrupts_disable();
1378
    ipl = interrupts_disable();
1242
    src_area = find_area_and_lock(AS, base);
1379
    src_area = find_area_and_lock(AS, base);
1243
    if (src_area){
1380
    if (src_area) {
1244
        size = src_area->pages * PAGE_SIZE;
1381
        size = src_area->pages * PAGE_SIZE;
1245
        mutex_unlock(&src_area->lock);
1382
        mutex_unlock(&src_area->lock);
1246
    } else {
1383
    } else {
1247
        size = 0;
1384
        size = 0;
1248
    }
1385
    }
Line 1252... Line 1389...
1252
 
1389
 
1253
/** Mark portion of address space area as used.
1390
/** Mark portion of address space area as used.
1254
 *
1391
 *
1255
 * The address space area must be already locked.
1392
 * The address space area must be already locked.
1256
 *
1393
 *
1257
 * @param a Address space area.
1394
 * @param a     Address space area.
1258
 * @param page First page to be marked.
1395
 * @param page      First page to be marked.
1259
 * @param count Number of page to be marked.
1396
 * @param count     Number of page to be marked.
1260
 *
1397
 *
1261
 * @return 0 on failure and 1 on success.
1398
 * @return      Zero on failure and non-zero on success.
1262
 */
1399
 */
1263
int used_space_insert(as_area_t *a, uintptr_t page, count_t count)
1400
int used_space_insert(as_area_t *a, uintptr_t page, count_t count)
1264
{
1401
{
1265
    btree_node_t *leaf, *node;
1402
    btree_node_t *leaf, *node;
1266
    count_t pages;
1403
    count_t pages;
Line 1526... Line 1663...
1526
                return 1;
1663
                return 1;
1527
            }
1664
            }
1528
        }
1665
        }
1529
    }
1666
    }
1530
 
1667
 
1531
    panic("Inconsistency detected while adding %d pages of used space at "
1668
    panic("Inconsistency detected while adding %" PRIc " pages of used "
1532
        "%p.\n", count, page);
1669
        "space at %p.", count, page);
1533
}
1670
}
1534
 
1671
 
1535
/** Mark portion of address space area as unused.
1672
/** Mark portion of address space area as unused.
1536
 *
1673
 *
1537
 * The address space area must be already locked.
1674
 * The address space area must be already locked.
1538
 *
1675
 *
1539
 * @param a Address space area.
1676
 * @param a     Address space area.
1540
 * @param page First page to be marked.
1677
 * @param page      First page to be marked.
1541
 * @param count Number of page to be marked.
1678
 * @param count     Number of page to be marked.
1542
 *
1679
 *
1543
 * @return 0 on failure and 1 on success.
1680
 * @return      Zero on failure and non-zero on success.
1544
 */
1681
 */
1545
int used_space_remove(as_area_t *a, uintptr_t page, count_t count)
1682
int used_space_remove(as_area_t *a, uintptr_t page, count_t count)
1546
{
1683
{
1547
    btree_node_t *leaf, *node;
1684
    btree_node_t *leaf, *node;
1548
    count_t pages;
1685
    count_t pages;
Line 1705... Line 1842...
1705
            return 0;
1842
            return 0;
1706
        }
1843
        }
1707
    }
1844
    }
1708
 
1845
 
1709
error:
1846
error:
1710
    panic("Inconsistency detected while removing %d pages of used space "
1847
    panic("Inconsistency detected while removing %" PRIc " pages of used "
1711
        "from %p.\n", count, page);
1848
        "space from %p.", count, page);
1712
}
1849
}
1713
 
1850
 
1714
/** Remove reference to address space area share info.
1851
/** Remove reference to address space area share info.
1715
 *
1852
 *
1716
 * If the reference count drops to 0, the sh_info is deallocated.
1853
 * If the reference count drops to 0, the sh_info is deallocated.
1717
 *
1854
 *
1718
 * @param sh_info Pointer to address space area share info.
1855
 * @param sh_info   Pointer to address space area share info.
1719
 */
1856
 */
1720
void sh_info_remove_reference(share_info_t *sh_info)
1857
void sh_info_remove_reference(share_info_t *sh_info)
1721
{
1858
{
1722
    bool dealloc = false;
1859
    bool dealloc = false;
1723
 
1860
 
Line 1768... Line 1905...
1768
unative_t sys_as_area_resize(uintptr_t address, size_t size, int flags)
1905
unative_t sys_as_area_resize(uintptr_t address, size_t size, int flags)
1769
{
1906
{
1770
    return (unative_t) as_area_resize(AS, address, size, 0);
1907
    return (unative_t) as_area_resize(AS, address, size, 0);
1771
}
1908
}
1772
 
1909
 
-
 
1910
/** Wrapper for as_area_change_flags(). */
-
 
1911
unative_t sys_as_area_change_flags(uintptr_t address, int flags)
-
 
1912
{
-
 
1913
    return (unative_t) as_area_change_flags(AS, flags, address);
-
 
1914
}
-
 
1915
 
1773
/** Wrapper for as_area_destroy(). */
1916
/** Wrapper for as_area_destroy(). */
1774
unative_t sys_as_area_destroy(uintptr_t address)
1917
unative_t sys_as_area_destroy(uintptr_t address)
1775
{
1918
{
1776
    return (unative_t) as_area_destroy(AS, address);
1919
    return (unative_t) as_area_destroy(AS, address);
1777
}
1920
}
1778
 
1921
 
1779
/** Print out information about address space.
1922
/** Print out information about address space.
1780
 *
1923
 *
1781
 * @param as Address space.
1924
 * @param as        Address space.
1782
 */
1925
 */
1783
void as_print(as_t *as)
1926
void as_print(as_t *as)
1784
{
1927
{
1785
    ipl_t ipl;
1928
    ipl_t ipl;
1786
   
1929
   
Line 1798... Line 1941...
1798
        unsigned int i;
1941
        unsigned int i;
1799
        for (i = 0; i < node->keys; i++) {
1942
        for (i = 0; i < node->keys; i++) {
1800
            as_area_t *area = node->value[i];
1943
            as_area_t *area = node->value[i];
1801
       
1944
       
1802
            mutex_lock(&area->lock);
1945
            mutex_lock(&area->lock);
1803
            printf("as_area: %p, base=%p, pages=%d (%p - %p)\n",
1946
            printf("as_area: %p, base=%p, pages=%" PRIc
1804
                area, area->base, area->pages, area->base,
1947
                " (%p - %p)\n", area, area->base, area->pages,
1805
                area->base + area->pages*PAGE_SIZE);
1948
                area->base, area->base + FRAMES2SIZE(area->pages));
1806
            mutex_unlock(&area->lock);
1949
            mutex_unlock(&area->lock);
1807
        }
1950
        }
1808
    }
1951
    }
1809
   
1952
   
1810
    mutex_unlock(&as->lock);
1953
    mutex_unlock(&as->lock);