Subversion Repositories HelenOS-historic

Rev

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

Rev 727 Rev 754
Line 57... Line 57...
57
 
57
 
58
#define KAS_START_INDEX     PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)
58
#define KAS_START_INDEX     PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)
59
#define KAS_END_INDEX       PTL0_INDEX(KERNEL_ADDRESS_SPACE_END)
59
#define KAS_END_INDEX       PTL0_INDEX(KERNEL_ADDRESS_SPACE_END)
60
#define KAS_INDICES     (1+(KAS_END_INDEX-KAS_START_INDEX))
60
#define KAS_INDICES     (1+(KAS_END_INDEX-KAS_START_INDEX))
61
 
61
 
62
/*
-
 
63
 * Here we assume that PFN (Physical Frame Number) space
-
 
64
 * is smaller than the width of index_t. UNALLOCATED_PFN
-
 
65
 * can be then used to mark mappings wich were not
-
 
66
 * yet allocated a physical frame.
-
 
67
 */
-
 
68
#define UNALLOCATED_PFN     ((index_t) -1)
62
static int get_area_flags(as_area_t *a);
69
 
63
 
70
/** Create address space. */
64
/** Create address space. */
71
/*
65
/*
72
 * FIXME: this interface must be meaningful for all possible VAT
66
 * FIXME: this interface must be meaningful for all possible VAT
73
 *    (Virtual Address Translation) mechanisms.
67
 *    (Virtual Address Translation) mechanisms.
Line 131... Line 125...
131
    /*
125
    /*
132
     * TODO: test as_area which is to be created doesn't overlap with an existing one.
126
     * TODO: test as_area which is to be created doesn't overlap with an existing one.
133
     */
127
     */
134
   
128
   
135
    a = (as_area_t *) malloc(sizeof(as_area_t));
129
    a = (as_area_t *) malloc(sizeof(as_area_t));
136
    if (a) {
130
    if (a) {   
137
        int i;
-
 
138
   
-
 
139
        a->mapping = (index_t *) malloc(size * sizeof(index_t));
-
 
140
        if (!a->mapping) {
-
 
141
            free(a);
-
 
142
            spinlock_unlock(&as->lock);
-
 
143
            interrupts_restore(ipl);
-
 
144
            return NULL;
-
 
145
        }
-
 
146
       
-
 
147
        for (i=0; i<size; i++) {
-
 
148
            /*
-
 
149
             * Frames will be allocated on-demand by
-
 
150
             * as_page_fault() or preloaded by
-
 
151
             * as_area_set_mapping().
-
 
152
             */
-
 
153
            a->mapping[i] = UNALLOCATED_PFN;
-
 
154
        }
-
 
155
       
-
 
156
        spinlock_initialize(&a->lock, "as_area_lock");
131
        spinlock_initialize(&a->lock, "as_area_lock");
157
           
132
           
158
        link_initialize(&a->link);         
133
        link_initialize(&a->link);         
159
        a->type = type;
134
        a->type = type;
160
        a->size = size;
135
        a->size = size;
161
        a->base = base;
136
        a->base = base;
162
       
137
       
163
        list_append(&a->link, &as->as_area_head);
138
        list_append(&a->link, &as->as_area_head);
164
 
-
 
165
    }
139
    }
166
 
140
 
167
    spinlock_unlock(&as->lock);
141
    spinlock_unlock(&as->lock);
168
    interrupts_restore(ipl);
142
    interrupts_restore(ipl);
169
 
143
 
170
    return a;
144
    return a;
171
}
145
}
172
 
146
 
173
/** Load mapping for address space area.
147
/** Initialize mapping for one page of address space.
174
 *
148
 *
-
 
149
 * This functions maps 'page' to 'frame' according
-
 
150
 * to attributes of the address space area to
175
 * Initialize a->mapping.
151
 * wich 'page' belongs.
176
 *
152
 *
177
 * @param a   Target address space area.
153
 * @param a Target address space.
178
 * @param vpn Page number relative to area start.
154
 * @param page Virtual page within the area.
179
 * @param pfn Frame number to map.
155
 * @param frame Physical frame to which page will be mapped.
180
 */
156
 */
181
void as_area_set_mapping(as_area_t *a, index_t vpn, index_t pfn)
157
void as_set_mapping(as_t *as, __address page, __address frame)
182
{
158
{
183
    ASSERT(vpn < a->size);
159
    as_area_t *a, *area = NULL;
184
    ASSERT(a->mapping[vpn] == UNALLOCATED_PFN);
-
 
185
    ASSERT(pfn != UNALLOCATED_PFN);
160
    link_t *cur;
186
   
-
 
187
    ipl_t ipl;
161
    ipl_t ipl;
188
   
162
   
189
    ipl = interrupts_disable();
163
    ipl = interrupts_disable();
190
    spinlock_lock(&a->lock);
164
    spinlock_lock(&as->lock);
-
 
165
   
-
 
166
    /*
-
 
167
     * First, try locate an area.
-
 
168
     */
-
 
169
    for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) {
-
 
170
        a = list_get_instance(cur, as_area_t, link);
-
 
171
        spinlock_lock(&a->lock);
-
 
172
 
-
 
173
        if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
-
 
174
            area = a;
-
 
175
            break;
-
 
176
        }
-
 
177
       
-
 
178
        spinlock_unlock(&a->lock);
-
 
179
    }
-
 
180
   
-
 
181
    if (!area) {
-
 
182
        panic("page not part of any as_area\n");
-
 
183
    }
-
 
184
 
-
 
185
    /*
-
 
186
     * Note: area->lock is held.
-
 
187
     */
191
   
188
   
192
    a->mapping[vpn] = pfn;
189
    page_mapping_insert(page, as->asid, frame, get_area_flags(area), (__address) as->ptl0);
193
   
190
   
-
 
191
    spinlock_unlock(&area->lock);
194
    spinlock_unlock(&a->lock);
192
    spinlock_unlock(&as->lock);
195
    interrupts_restore(ipl);
193
    interrupts_restore(ipl);
196
}
194
}
197
 
195
 
198
/** Handle page fault within the current address space.
196
/** Handle page fault within the current address space.
199
 *
197
 *
Line 204... Line 202...
204
 *
202
 *
205
 * @return 0 on page fault, 1 on success.
203
 * @return 0 on page fault, 1 on success.
206
 */
204
 */
207
int as_page_fault(__address page)
205
int as_page_fault(__address page)
208
{
206
{
209
    int flags;
-
 
210
    link_t *cur;
207
    link_t *cur;
211
    as_area_t *a, *area = NULL;
208
    as_area_t *a, *area = NULL;
212
    index_t vpn;
-
 
213
    __address frame;
209
    __address frame;
214
   
210
   
215
    ASSERT(AS);
211
    ASSERT(AS);
216
    spinlock_lock(&AS->lock);
212
    spinlock_lock(&AS->lock);
217
   
213
   
Line 226... Line 222...
226
 
222
 
227
            /*
223
            /*
228
             * We found the area containing 'page'.
224
             * We found the area containing 'page'.
229
             * TODO: access checking
225
             * TODO: access checking
230
             */
226
             */
231
           
-
 
232
            vpn = (page - a->base) / PAGE_SIZE;
-
 
233
            area = a;
227
            area = a;
234
            break;
228
            break;
235
        }
229
        }
236
       
230
       
237
        spinlock_unlock(&a->lock);
231
        spinlock_unlock(&a->lock);
Line 249... Line 243...
249
    /*
243
    /*
250
     * Note: area->lock is held.
244
     * Note: area->lock is held.
251
     */
245
     */
252
   
246
   
253
    /*
247
    /*
-
 
248
     * In general, there can be several reasons that
-
 
249
     * can have caused this fault.
-
 
250
     *
-
 
251
     * - non-existent mapping: the area is a scratch
254
     * Decide if a frame needs to be allocated.
252
     *   area (e.g. stack) and so far has not been
255
     * If so, allocate it and adjust area->mapping map.
253
     *   allocated a frame for the faulting page
-
 
254
     *
-
 
255
     * - non-present mapping: another possibility,
-
 
256
     *   currently not implemented, would be frame
-
 
257
     *   reuse; when this becomes a possibility,
-
 
258
     *   do not forget to distinguish between
-
 
259
     *   the different causes
256
     */
260
     */
257
    if (area->mapping[vpn] == UNALLOCATED_PFN) {
-
 
258
        frame = frame_alloc(0, ONE_FRAME, NULL);
261
    frame = frame_alloc(0, ONE_FRAME, NULL);
259
        memsetb(PA2KA(frame), FRAME_SIZE, 0);
262
    memsetb(PA2KA(frame), FRAME_SIZE, 0);
260
        area->mapping[vpn] = frame / FRAME_SIZE;
-
 
261
        ASSERT(area->mapping[vpn] != UNALLOCATED_PFN);
-
 
262
    } else
-
 
263
        frame = area->mapping[vpn] * FRAME_SIZE;
-
 
264
   
263
   
265
    switch (area->type) {
-
 
266
        case AS_AREA_TEXT:
-
 
267
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
-
 
268
            break;
-
 
269
        case AS_AREA_DATA:
-
 
270
        case AS_AREA_STACK:
-
 
271
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
-
 
272
            break;
-
 
273
        default:
-
 
274
            panic("unexpected as_area_type_t %d", area->type);
-
 
275
    }
-
 
276
 
-
 
277
    /*
264
    /*
278
     * Map 'page' to 'frame'.
265
     * Map 'page' to 'frame'.
279
     * Note that TLB shootdown is not attempted as only new information is being
266
     * Note that TLB shootdown is not attempted as only new information is being
280
     * inserted into page tables.
267
     * inserted into page tables.
281
     */
268
     */
282
    page_mapping_insert(page, AS->asid, frame, flags, (__address) AS->ptl0);
269
    page_mapping_insert(page, AS->asid, frame, get_area_flags(area), (__address) AS->ptl0);
283
   
270
   
284
    spinlock_unlock(&area->lock);
271
    spinlock_unlock(&area->lock);
285
    spinlock_unlock(&AS->lock);
272
    spinlock_unlock(&AS->lock);
286
 
273
 
287
    return 1;
274
    return 1;
Line 310... Line 297...
310
     */
297
     */
311
    as_install_arch(as);
298
    as_install_arch(as);
312
   
299
   
313
    AS = as;
300
    AS = as;
314
}
301
}
-
 
302
 
-
 
303
/** Compute flags for virtual address translation subsytem.
-
 
304
 *
-
 
305
 * The address space area must be locked.
-
 
306
 * Interrupts must be disabled.
-
 
307
 *
-
 
308
 * @param a Address space area.
-
 
309
 *
-
 
310
 * @return Flags to be used in page_mapping_insert().
-
 
311
 */
-
 
312
int get_area_flags(as_area_t *a)
-
 
313
{
-
 
314
    int flags;
-
 
315
 
-
 
316
    switch (a->type) {
-
 
317
        case AS_AREA_TEXT:
-
 
318
            flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
-
 
319
            break;
-
 
320
        case AS_AREA_DATA:
-
 
321
        case AS_AREA_STACK:
-
 
322
            flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
-
 
323
            break;
-
 
324
        default:
-
 
325
            panic("unexpected as_area_type_t %d", a->type);
-
 
326
    }
-
 
327
   
-
 
328
    return flags;
-
 
329
}