Subversion Repositories HelenOS-historic

Rev

Rev 1384 | Rev 1390 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1384 Rev 1389
1
/*
1
/*
2
 * Copyright (C) 2005 Martin Decky
2
 * Copyright (C) 2005 Martin Decky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <arch/mm/page.h>
29
#include <arch/mm/page.h>
30
#include <genarch/mm/page_pt.h>
30
#include <genarch/mm/page_pt.h>
31
#include <arch/mm/frame.h>
31
#include <arch/mm/frame.h>
32
#include <arch/asm.h>
32
#include <arch/asm.h>
33
#include <mm/frame.h>
33
#include <mm/frame.h>
34
#include <mm/page.h>
34
#include <mm/page.h>
35
#include <mm/as.h>
35
#include <mm/as.h>
36
#include <arch.h>
36
#include <arch.h>
37
#include <arch/types.h>
37
#include <arch/types.h>
38
#include <arch/exception.h>
38
#include <arch/exception.h>
39
#include <align.h>
39
#include <align.h>
40
#include <config.h>
40
#include <config.h>
41
#include <print.h>
41
#include <print.h>
42
#include <symtab.h>
42
#include <symtab.h>
43
 
43
 
44
static phte_t *phte;
44
static phte_t *phte;
45
 
45
 
46
 
46
 
47
/** Try to find PTE for faulting address
47
/** Try to find PTE for faulting address
48
 *
48
 *
49
 * Try to find PTE for faulting address.
49
 * Try to find PTE for faulting address.
50
 * The as->lock must be held on entry to this function
50
 * The as->lock must be held on entry to this function
51
 * if lock is true.
51
 * if lock is true.
52
 *
52
 *
53
 * @param as       Address space.
53
 * @param as       Address space.
54
 * @param lock     Lock/unlock the address space.
54
 * @param lock     Lock/unlock the address space.
55
 * @param badvaddr Faulting virtual address.
55
 * @param badvaddr Faulting virtual address.
56
 * @param istate   Pointer to interrupted state.
56
 * @param istate   Pointer to interrupted state.
57
 * @param pfrc     Pointer to variable where as_page_fault() return code will be stored.
57
 * @param pfrc     Pointer to variable where as_page_fault() return code will be stored.
58
 * @return         PTE on success, NULL otherwise.
58
 * @return         PTE on success, NULL otherwise.
59
 *
59
 *
60
 */
60
 */
61
static pte_t *find_mapping_and_check(as_t *as, bool lock, __address badvaddr, istate_t *istate, int *pfcr)
61
static pte_t *find_mapping_and_check(as_t *as, bool lock, __address badvaddr, istate_t *istate, int *pfcr)
62
{
62
{
63
    /*
63
    /*
64
     * Check if the mapping exists in page tables.
64
     * Check if the mapping exists in page tables.
65
     */
65
     */
66
    pte_t *pte = page_mapping_find(as, badvaddr);
66
    pte_t *pte = page_mapping_find(as, badvaddr);
67
    if ((pte) && (pte->p)) {
67
    if ((pte) && (pte->p)) {
68
        /*
68
        /*
69
         * Mapping found in page tables.
69
         * Mapping found in page tables.
70
         * Immediately succeed.
70
         * Immediately succeed.
71
         */
71
         */
72
        return pte;
72
        return pte;
73
    } else {
73
    } else {
74
        int rc;
74
        int rc;
75
   
75
   
76
        /*
76
        /*
77
         * Mapping not found in page tables.
77
         * Mapping not found in page tables.
78
         * Resort to higher-level page fault handler.
78
         * Resort to higher-level page fault handler.
79
         */
79
         */
80
        page_table_unlock(as, lock);
80
        page_table_unlock(as, lock);
81
        switch (rc = as_page_fault(badvaddr, istate)) {
81
        switch (rc = as_page_fault(badvaddr, istate)) {
82
            case AS_PF_OK:
82
            case AS_PF_OK:
83
                /*
83
                /*
84
                 * The higher-level page fault handler succeeded,
84
                 * The higher-level page fault handler succeeded,
85
                 * The mapping ought to be in place.
85
                 * The mapping ought to be in place.
86
                 */
86
                 */
87
                page_table_lock(as, lock);
87
                page_table_lock(as, lock);
88
                pte = page_mapping_find(as, badvaddr);
88
                pte = page_mapping_find(as, badvaddr);
89
                ASSERT((pte) && (pte->p));
89
                ASSERT((pte) && (pte->p));
90
                return pte;
90
                return pte;
91
            case AS_PF_DEFER:
91
            case AS_PF_DEFER:
92
                page_table_lock(as, lock);
92
                page_table_lock(as, lock);
93
                *pfcr = rc;
93
                *pfcr = rc;
94
                return NULL;
94
                return NULL;
95
            case AS_PF_FAULT:
95
            case AS_PF_FAULT:
96
                page_table_lock(as, lock);
96
                page_table_lock(as, lock);
97
                printf("Page fault.\n");
97
                printf("Page fault.\n");
98
                *pfcr = rc;
98
                *pfcr = rc;
99
                return NULL;
99
                return NULL;
100
            default:
100
            default:
101
                panic("unexpected rc (%d)\n", rc);
101
                panic("unexpected rc (%d)\n", rc);
102
        }  
102
        }  
103
    }
103
    }
104
}
104
}
105
 
105
 
106
 
106
 
107
static void pht_refill_fail(__address badvaddr, istate_t *istate)
107
static void pht_refill_fail(__address badvaddr, istate_t *istate)
108
{
108
{
109
    char *symbol = "";
109
    char *symbol = "";
110
    char *sym2 = "";
110
    char *sym2 = "";
111
 
111
 
112
    char *s = get_symtab_entry(istate->pc);
112
    char *s = get_symtab_entry(istate->pc);
113
    if (s)
113
    if (s)
114
        symbol = s;
114
        symbol = s;
115
    s = get_symtab_entry(istate->lr);
115
    s = get_symtab_entry(istate->lr);
116
    if (s)
116
    if (s)
117
        sym2 = s;
117
        sym2 = s;
118
    panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2);
118
    panic("%p: PHT Refill Exception at %p (%s<-%s)\n", badvaddr, istate->pc, symbol, sym2);
119
}
119
}
120
 
120
 
121
 
121
 
122
static void pht_insert(const __address vaddr, const pfn_t pfn)
122
static void pht_insert(const __address vaddr, const pfn_t pfn)
123
{
123
{
124
    __u32 page = (vaddr >> 12) & 0xffff;
124
    __u32 page = (vaddr >> 12) & 0xffff;
125
    __u32 api = (vaddr >> 22) & 0x3f;
125
    __u32 api = (vaddr >> 22) & 0x3f;
126
    __u32 vsid;
126
    __u32 vsid;
127
   
127
   
128
    asm volatile (
128
    asm volatile (
129
        "mfsrin %0, %1\n"
129
        "mfsrin %0, %1\n"
130
        : "=r" (vsid)
130
        : "=r" (vsid)
131
        : "r" (vaddr)
131
        : "r" (vaddr)
132
    );
132
    );
133
   
133
   
134
    /* Primary hash (xor) */
134
    /* Primary hash (xor) */
-
 
135
    __u32 h = 0;
-
 
136
    __u32 hash = vsid ^ page;
135
    __u32 hash = ((vsid ^ page) & 0x3ff) << 3;
137
    __u32 base = (hash & 0x3ff) << 3;
136
   
-
 
137
    __u32 i;
138
    __u32 i;
138
    bool found = false;
139
    bool found = false;
-
 
140
   
139
    /* Find unused PTE in PTEG */
141
    /* Find unused or colliding
-
 
142
       PTE in PTEG */
140
    for (i = 0; i < 8; i++) {
143
    for (i = 0; i < 8; i++) {
141
        if (!phte[hash + i].v) {
144
        if ((!phte[base + i].v) || ((phte[base + i].vsid == vsid) && (phte[base + i].api == api))) {
142
            found = true;
145
            found = true;
143
            break;
146
            break;
144
        }
147
        }
145
    }
148
    }
146
   
149
   
147
    if (!found) {
150
    if (!found) {
148
        /* Secondary hash (not) */
151
        /* Secondary hash (not) */
149
        hash = ~hash;
152
        __u32 base2 = (~hash & 0x3ff) << 3;
150
       
153
       
151
        /* Find unused PTE in PTEG */
154
        /* Find unused or colliding
-
 
155
           PTE in PTEG */
152
        for (i = 0; i < 8; i++) {
156
        for (i = 0; i < 8; i++) {
153
            if (!phte[hash + i].v) {
157
            if (!phte[base2 + i].v) {
154
                found = true;
158
                found = true;
-
 
159
                base = base2;
-
 
160
                h = 1;
155
                break;
161
                break;
156
            }
162
            }
157
        }
163
        }
158
       
164
       
159
        if (!found) {
165
        if (!found) {
160
            // TODO: A/C precedence groups
166
            // TODO: A/C precedence groups
161
            i = page % 8;
167
            i = page % 8;
162
        }
168
        }
163
    }
169
    }
164
   
170
   
165
    phte[hash + i].v = 1;
171
    phte[base + i].v = 1;
166
    phte[hash + i].vsid = vsid;
172
    phte[base + i].vsid = vsid;
167
    phte[hash + i].h = 0;
173
    phte[base + i].h = h;
168
    phte[hash + i].api = api;
174
    phte[base + i].api = api;
169
    phte[hash + i].rpn = pfn;
175
    phte[base + i].rpn = pfn;
170
    phte[hash + i].r = 0;
176
    phte[base + i].r = 0;
171
    phte[hash + i].c = 0;
177
    phte[base + i].c = 0;
172
    phte[hash + i].pp = 2; // FIXME
178
    phte[base + i].pp = 2; // FIXME
173
}
179
}
174
 
180
 
175
 
181
 
176
/** Process Instruction/Data Storage Interrupt
182
/** Process Instruction/Data Storage Interrupt
177
 *
183
 *
178
 * @param data   True if Data Storage Interrupt.
184
 * @param data   True if Data Storage Interrupt.
179
 * @param istate Interrupted register context.
185
 * @param istate Interrupted register context.
180
 *
186
 *
181
 */
187
 */
182
void pht_refill(bool data, istate_t *istate)
188
void pht_refill(bool data, istate_t *istate)
183
{
189
{
184
    __address badvaddr;
190
    __address badvaddr;
185
    pte_t *pte;
191
    pte_t *pte;
186
    int pfcr;
192
    int pfcr;
187
    as_t *as;
193
    as_t *as;
188
    bool lock;
194
    bool lock;
189
   
195
   
190
    if (AS == NULL) {
196
    if (AS == NULL) {
191
        as = AS_KERNEL;
197
        as = AS_KERNEL;
192
        lock = false;
198
        lock = false;
193
    } else {
199
    } else {
194
        as = AS;
200
        as = AS;
195
        lock = true;
201
        lock = true;
196
    }
202
    }
197
   
203
   
198
    if (data) {
204
    if (data) {
199
        asm volatile (
205
        asm volatile (
200
            "mfdar %0\n"
206
            "mfdar %0\n"
201
            : "=r" (badvaddr)
207
            : "=r" (badvaddr)
202
        );
208
        );
203
    } else
209
    } else
204
        badvaddr = istate->pc;
210
        badvaddr = istate->pc;
205
       
211
       
206
    page_table_lock(as, lock);
212
    page_table_lock(as, lock);
207
   
213
   
208
    pte = find_mapping_and_check(as, lock, badvaddr, istate, &pfcr);
214
    pte = find_mapping_and_check(as, lock, badvaddr, istate, &pfcr);
209
    if (!pte) {
215
    if (!pte) {
210
        switch (pfcr) {
216
        switch (pfcr) {
211
            case AS_PF_FAULT:
217
            case AS_PF_FAULT:
212
                goto fail;
218
                goto fail;
213
                break;
219
                break;
214
            case AS_PF_DEFER:
220
            case AS_PF_DEFER:
215
                /*
221
                /*
216
                 * The page fault came during copy_from_uspace()
222
                 * The page fault came during copy_from_uspace()
217
                 * or copy_to_uspace().
223
                 * or copy_to_uspace().
218
                 */
224
                 */
219
                page_table_unlock(as, lock);
225
                page_table_unlock(as, lock);
220
                return;
226
                return;
221
            default:
227
            default:
222
                panic("Unexpected pfrc (%d)\n", pfcr);
228
                panic("Unexpected pfrc (%d)\n", pfcr);
223
        }
229
        }
224
    }
230
    }
225
   
231
   
226
    pte->a = 1; /* Record access to PTE */
232
    pte->a = 1; /* Record access to PTE */
227
    pht_insert(badvaddr, pte->pfn);
233
    pht_insert(badvaddr, pte->pfn);
228
   
234
   
229
    page_table_unlock(as, lock);
235
    page_table_unlock(as, lock);
230
    return;
236
    return;
231
   
237
   
232
fail:
238
fail:
233
    page_table_unlock(as, lock);
239
    page_table_unlock(as, lock);
234
    pht_refill_fail(badvaddr, istate);
240
    pht_refill_fail(badvaddr, istate);
235
}
241
}
236
 
242
 
237
 
243
 
238
void pht_init(void)
244
void pht_init(void)
239
{
245
{
240
    memsetb((__address) phte, 1 << PHT_BITS, 0);
246
    memsetb((__address) phte, 1 << PHT_BITS, 0);
241
}
247
}
242
 
248
 
243
 
249
 
244
void page_arch_init(void)
250
void page_arch_init(void)
245
{
251
{
246
    if (config.cpu_active == 1) {
252
    if (config.cpu_active == 1) {
247
        page_mapping_operations = &pt_mapping_operations;
253
        page_mapping_operations = &pt_mapping_operations;
248
       
254
       
-
 
255
        __address cur;
-
 
256
        int flags;
-
 
257
       
-
 
258
        /* Pages below 128 MB are mapped using BAT,
-
 
259
           map rest of the physical memory */
-
 
260
        for (cur = 128 << 20; cur < last_frame; cur += FRAME_SIZE) {
-
 
261
            flags = PAGE_CACHEABLE;
-
 
262
            if ((PA2KA(cur) >= config.base) && (PA2KA(cur) < config.base + config.kernel_size))
-
 
263
                flags |= PAGE_GLOBAL;
-
 
264
            page_mapping_insert(AS_KERNEL, PA2KA(cur), cur, flags);
-
 
265
        }
-
 
266
       
249
        /* Allocate page hash table */
267
        /* Allocate page hash table */
250
        phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC));
268
        phte_t *physical_phte = (phte_t *) PFN2ADDR(frame_alloc(PHT_ORDER, FRAME_KA | FRAME_PANIC));
251
        phte = (phte_t *) PA2KA((__address) physical_phte);
269
        phte = (phte_t *) PA2KA((__address) physical_phte);
252
       
270
       
253
        ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0);
271
        ASSERT((__address) physical_phte % (1 << PHT_BITS) == 0);
254
        pht_init();
272
        pht_init();
255
       
273
       
256
        asm volatile (
274
        asm volatile (
257
            "mtsdr1 %0\n"
275
            "mtsdr1 %0\n"
258
            :
276
            :
259
            : "r" ((__address) physical_phte)
277
            : "r" ((__address) physical_phte)
260
        );
278
        );
261
    }
279
    }
262
}
280
}
263
 
281
 
264
 
282
 
265
__address hw_map(__address physaddr, size_t size)
283
__address hw_map(__address physaddr, size_t size)
266
{
284
{
267
    if (last_frame + ALIGN_UP(size, PAGE_SIZE) > KA2PA(KERNEL_ADDRESS_SPACE_END_ARCH))
285
    if (last_frame + ALIGN_UP(size, PAGE_SIZE) > KA2PA(KERNEL_ADDRESS_SPACE_END_ARCH))
268
        panic("Unable to map physical memory %p (%d bytes)", physaddr, size)
286
        panic("Unable to map physical memory %p (%d bytes)", physaddr, size)
269
   
287
   
270
    __address virtaddr = PA2KA(last_frame);
288
    __address virtaddr = PA2KA(last_frame);
271
    pfn_t i;
289
    pfn_t i;
272
    for (i = 0; i < ADDR2PFN(ALIGN_UP(size, PAGE_SIZE)); i++)
290
    for (i = 0; i < ADDR2PFN(ALIGN_UP(size, PAGE_SIZE)); i++)
273
        page_mapping_insert(AS_KERNEL, virtaddr + PFN2ADDR(i), physaddr + PFN2ADDR(i), PAGE_NOT_CACHEABLE);
291
        page_mapping_insert(AS_KERNEL, virtaddr + PFN2ADDR(i), physaddr + PFN2ADDR(i), PAGE_NOT_CACHEABLE);
274
   
292
   
275
    last_frame = ALIGN_UP(last_frame + size, FRAME_SIZE);
293
    last_frame = ALIGN_UP(last_frame + size, FRAME_SIZE);
276
   
294
   
277
    return virtaddr;
295
    return virtaddr;
278
}
296
}
279
 
297