Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
162 decky 1
/*
2
 * Copyright (C) 2005 Martin Decky
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
 
1726 decky 29
/** @addtogroup ppc32mm
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
162 decky 35
#include <arch/mm/page.h>
684 jermar 36
#include <genarch/mm/page_pt.h>
162 decky 37
#include <arch/mm/frame.h>
1374 decky 38
#include <arch/asm.h>
1609 decky 39
#include <arch/interrupt.h>
162 decky 40
#include <mm/frame.h>
41
#include <mm/page.h>
1374 decky 42
#include <mm/as.h>
43
#include <arch.h>
684 jermar 44
#include <arch/types.h>
1374 decky 45
#include <arch/exception.h>
1383 decky 46
#include <align.h>
1374 decky 47
#include <config.h>
48
#include <print.h>
49
#include <symtab.h>
162 decky 50
 
1374 decky 51
 
52
/** Try to find PTE for faulting address
53
 *
54
 * Try to find PTE for faulting address.
1383 decky 55
 * The as->lock must be held on entry to this function
56
 * if lock is true.
1374 decky 57
 *
1383 decky 58
 * @param as       Address space.
59
 * @param lock     Lock/unlock the address space.
1374 decky 60
 * @param badvaddr Faulting virtual address.
1411 jermar 61
 * @param access   Access mode that caused the fault.
1378 decky 62
 * @param istate   Pointer to interrupted state.
63
 * @param pfrc     Pointer to variable where as_page_fault() return code will be stored.
1374 decky 64
 * @return         PTE on success, NULL otherwise.
65
 *
66
 */
1726 decky 67
static pte_t *find_mapping_and_check(as_t *as, bool lock, __address badvaddr, int access, istate_t *istate, int *pfrc)
1374 decky 68
{
69
    /*
70
     * Check if the mapping exists in page tables.
71
     */
1383 decky 72
    pte_t *pte = page_mapping_find(as, badvaddr);
1374 decky 73
    if ((pte) && (pte->p)) {
74
        /*
75
         * Mapping found in page tables.
76
         * Immediately succeed.
77
         */
78
        return pte;
79
    } else {
80
        int rc;
81
 
82
        /*
83
         * Mapping not found in page tables.
84
         * Resort to higher-level page fault handler.
85
         */
1383 decky 86
        page_table_unlock(as, lock);
1411 jermar 87
        switch (rc = as_page_fault(badvaddr, access, istate)) {
1374 decky 88
            case AS_PF_OK:
89
                /*
90
                 * The higher-level page fault handler succeeded,
91
                 * The mapping ought to be in place.
92
                 */
1383 decky 93
                page_table_lock(as, lock);
94
                pte = page_mapping_find(as, badvaddr);
1374 decky 95
                ASSERT((pte) && (pte->p));
96
                return pte;
97
            case AS_PF_DEFER:
1383 decky 98
                page_table_lock(as, lock);
1708 jermar 99
                *pfrc = rc;
1374 decky 100
                return NULL;
101
            case AS_PF_FAULT:
1383 decky 102
                page_table_lock(as, lock);
1374 decky 103
                printf("Page fault.\n");
1708 jermar 104
                *pfrc = rc;
1374 decky 105
                return NULL;
106
            default:
107
                panic("unexpected rc (%d)\n", rc);
108
        }  
109
    }
110
}
111
 
112
 
113
static void pht_refill_fail(__address badvaddr, istate_t *istate)
114
{
115
    char *symbol = "";
116
    char *sym2 = "";
117
 
118
    char *s = get_symtab_entry(istate->pc);
119
    if (s)
120
        symbol = s;
121
    s = get_symtab_entry(istate->lr);
122
    if (s)
123
        sym2 = s;
124
    panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2);
125
}
126
 
1378 decky 127
 
1374 decky 128
static void pht_insert(const __address vaddr, const pfn_t pfn)
129
{
130
    __u32 page = (vaddr >> 12) & 0xffff;
131
    __u32 api = (vaddr >> 22) & 0x3f;
1726 decky 132
 
1374 decky 133
    __u32 vsid;
134
    asm volatile (
135
        "mfsrin %0, %1\n"
136
        : "=r" (vsid)
137
        : "r" (vaddr)
138
    );
139
 
1726 decky 140
    __u32 sdr1;
141
    asm volatile (
142
        "mfsdr1 %0\n"
143
        : "=r" (sdr1)
144
    );
145
    phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
146
 
1374 decky 147
    /* Primary hash (xor) */
1389 decky 148
    __u32 h = 0;
149
    __u32 hash = vsid ^ page;
150
    __u32 base = (hash & 0x3ff) << 3;
1374 decky 151
    __u32 i;
1378 decky 152
    bool found = false;
1389 decky 153
 
154
    /* Find unused or colliding
155
       PTE in PTEG */
1374 decky 156
    for (i = 0; i < 8; i++) {
1389 decky 157
        if ((!phte[base + i].v) || ((phte[base + i].vsid == vsid) && (phte[base + i].api == api))) {
1378 decky 158
            found = true;
1374 decky 159
            break;
1378 decky 160
        }
1374 decky 161
    }
162
 
1378 decky 163
    if (!found) {
164
        /* Secondary hash (not) */
1389 decky 165
        __u32 base2 = (~hash & 0x3ff) << 3;
1378 decky 166
 
1389 decky 167
        /* Find unused or colliding
168
           PTE in PTEG */
1378 decky 169
        for (i = 0; i < 8; i++) {
1390 decky 170
            if ((!phte[base2 + i].v) || ((phte[base2 + i].vsid == vsid) && (phte[base2 + i].api == api))) {
1378 decky 171
                found = true;
1389 decky 172
                base = base2;
173
                h = 1;
1378 decky 174
                break;
175
            }
176
        }
177
 
178
        if (!found) {
179
            // TODO: A/C precedence groups
180
            i = page % 8;
181
        }
182
    }
1374 decky 183
 
1389 decky 184
    phte[base + i].v = 1;
185
    phte[base + i].vsid = vsid;
186
    phte[base + i].h = h;
187
    phte[base + i].api = api;
188
    phte[base + i].rpn = pfn;
189
    phte[base + i].r = 0;
190
    phte[base + i].c = 0;
191
    phte[base + i].pp = 2; // FIXME
1374 decky 192
}
193
 
194
 
1726 decky 195
static void pht_real_insert(const __address vaddr, const pfn_t pfn)
196
{
197
    __u32 page = (vaddr >> 12) & 0xffff;
198
    __u32 api = (vaddr >> 22) & 0x3f;
199
 
200
    __u32 vsid;
201
    asm volatile (
202
        "mfsrin %0, %1\n"
203
        : "=r" (vsid)
204
        : "r" (vaddr)
205
    );
206
 
207
    __u32 sdr1;
208
    asm volatile (
209
        "mfsdr1 %0\n"
210
        : "=r" (sdr1)
211
    );
212
    phte_t *phte_physical = (phte_t *) (sdr1 & 0xffff0000);
213
 
214
    /* Primary hash (xor) */
215
    __u32 h = 0;
216
    __u32 hash = vsid ^ page;
217
    __u32 base = (hash & 0x3ff) << 3;
218
    __u32 i;
219
    bool found = false;
220
 
221
    /* Find unused or colliding
222
       PTE in PTEG */
223
    for (i = 0; i < 8; i++) {
224
        if ((!phte_physical[base + i].v) || ((phte_physical[base + i].vsid == vsid) && (phte_physical[base + i].api == api))) {
225
            found = true;
226
            break;
227
        }
228
    }
229
 
230
    if (!found) {
231
        /* Secondary hash (not) */
232
        __u32 base2 = (~hash & 0x3ff) << 3;
233
 
234
        /* Find unused or colliding
235
           PTE in PTEG */
236
        for (i = 0; i < 8; i++) {
237
            if ((!phte_physical[base2 + i].v) || ((phte_physical[base2 + i].vsid == vsid) && (phte_physical[base2 + i].api == api))) {
238
                found = true;
239
                base = base2;
240
                h = 1;
241
                break;
242
            }
243
        }
244
 
245
        if (!found) {
246
            // TODO: A/C precedence groups
247
            i = page % 8;
248
        }
249
    }
250
 
251
    phte_physical[base + i].v = 1;
252
    phte_physical[base + i].vsid = vsid;
253
    phte_physical[base + i].h = h;
254
    phte_physical[base + i].api = api;
255
    phte_physical[base + i].rpn = pfn;
256
    phte_physical[base + i].r = 0;
257
    phte_physical[base + i].c = 0;
258
    phte_physical[base + i].pp = 2; // FIXME
259
}
260
 
261
 
1374 decky 262
/** Process Instruction/Data Storage Interrupt
263
 *
1708 jermar 264
 * @param n Interrupt vector number.
1374 decky 265
 * @param istate Interrupted register context.
266
 *
267
 */
1609 decky 268
void pht_refill(int n, istate_t *istate)
1374 decky 269
{
270
    __address badvaddr;
271
    pte_t *pte;
1708 jermar 272
    int pfrc;
1383 decky 273
    as_t *as;
274
    bool lock;
1374 decky 275
 
1383 decky 276
    if (AS == NULL) {
277
        as = AS_KERNEL;
278
        lock = false;
279
    } else {
280
        as = AS;
281
        lock = true;
282
    }
283
 
1609 decky 284
    if (n == VECTOR_DATA_STORAGE) {
1374 decky 285
        asm volatile (
286
            "mfdar %0\n"
287
            : "=r" (badvaddr)
288
        );
289
    } else
290
        badvaddr = istate->pc;
291
 
1383 decky 292
    page_table_lock(as, lock);
1374 decky 293
 
1708 jermar 294
    pte = find_mapping_and_check(as, lock, badvaddr, PF_ACCESS_READ /* FIXME */, istate, &pfrc);
1374 decky 295
    if (!pte) {
1708 jermar 296
        switch (pfrc) {
1378 decky 297
            case AS_PF_FAULT:
298
                goto fail;
299
                break;
300
            case AS_PF_DEFER:
301
                /*
302
                 * The page fault came during copy_from_uspace()
303
                 * or copy_to_uspace().
304
                 */
1383 decky 305
                page_table_unlock(as, lock);
1378 decky 306
                return;
307
            default:
1708 jermar 308
                panic("Unexpected pfrc (%d)\n", pfrc);
1374 decky 309
        }
310
    }
311
 
312
    pte->a = 1; /* Record access to PTE */
313
    pht_insert(badvaddr, pte->pfn);
314
 
1383 decky 315
    page_table_unlock(as, lock);
1374 decky 316
    return;
317
 
318
fail:
1383 decky 319
    page_table_unlock(as, lock);
1374 decky 320
    pht_refill_fail(badvaddr, istate);
321
}
322
 
323
 
1726 decky 324
/** Process Instruction/Data Storage Interrupt in Real Mode
325
 *
326
 * @param n Interrupt vector number.
327
 * @param istate Interrupted register context.
328
 *
329
 */
330
bool pht_real_refill(int n, istate_t *istate)
331
{
332
    __address badvaddr;
333
 
334
    if (n == VECTOR_DATA_STORAGE) {
335
        asm volatile (
336
            "mfdar %0\n"
337
            : "=r" (badvaddr)
338
        );
339
    } else
340
        badvaddr = istate->pc;
341
 
342
    __u32 physmem;
343
    asm volatile (
344
        "mfsprg3 %0\n"
345
        : "=r" (physmem)
346
    );
347
 
348
    if ((badvaddr >= PA2KA(0)) && (badvaddr <= PA2KA(physmem))) {
349
        pht_real_insert(badvaddr, KA2PA(badvaddr) >> 12);
350
        return true;
351
    }
352
 
353
    return false;
354
}
355
 
356
 
1374 decky 357
void pht_init(void)
358
{
1726 decky 359
    // FIXME
360
 
361
    __u32 sdr1;
362
    asm volatile (
363
        "mfsdr1 %0\n"
364
        : "=r" (sdr1)
365
    );
366
    phte_t *phte = (phte_t *) PA2KA(sdr1 & 0xffff0000);
367
 
368
    memsetb((__address) phte, 65536, 0);
1374 decky 369
}
370
 
371
 
162 decky 372
void page_arch_init(void)
373
{
1726 decky 374
    if (config.cpu_active == 1)
1374 decky 375
        page_mapping_operations = &pt_mapping_operations;
162 decky 376
}
1383 decky 377
 
378
 
379
__address hw_map(__address physaddr, size_t size)
380
{
381
    if (last_frame + ALIGN_UP(size, PAGE_SIZE) > KA2PA(KERNEL_ADDRESS_SPACE_END_ARCH))
382
        panic("Unable to map physical memory %p (%d bytes)", physaddr, size)
383
 
384
    __address virtaddr = PA2KA(last_frame);
385
    pfn_t i;
386
    for (i = 0; i < ADDR2PFN(ALIGN_UP(size, PAGE_SIZE)); i++)
387
        page_mapping_insert(AS_KERNEL, virtaddr + PFN2ADDR(i), physaddr + PFN2ADDR(i), PAGE_NOT_CACHEABLE);
388
 
389
    last_frame = ALIGN_UP(last_frame + size, FRAME_SIZE);
390
 
391
    return virtaddr;
392
}
1702 cejka 393
 
1726 decky 394
/** @}
1702 cejka 395
 */