Subversion Repositories HelenOS-historic

Rev

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

Rev 894 Rev 895
1
/*
1
/*
2
 * Copyright (C) 2005 Jakub Jermar
2
 * Copyright (C) 2005 Jakub Jermar
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/tlb.h>
29
#include <arch/mm/tlb.h>
30
#include <mm/tlb.h>
30
#include <mm/tlb.h>
31
#include <arch/mm/frame.h>
31
#include <arch/mm/frame.h>
32
#include <arch/mm/page.h>
32
#include <arch/mm/page.h>
33
#include <arch/mm/mmu.h>
33
#include <arch/mm/mmu.h>
34
#include <mm/asid.h>
34
#include <mm/asid.h>
35
#include <print.h>
35
#include <print.h>
36
#include <arch/types.h>
36
#include <arch/types.h>
37
#include <typedefs.h>
37
#include <typedefs.h>
38
#include <config.h>
38
#include <config.h>
39
#include <arch/trap/trap.h>
39
#include <arch/trap/trap.h>
40
#include <panic.h>
40
#include <panic.h>
41
#include <arch/asm.h>
41
#include <arch/asm.h>
42
#include <symtab.h>
42
#include <symtab.h>
43
 
43
 
44
#include <arch/drivers/fb.h>
44
#include <arch/drivers/fb.h>
45
#include <arch/drivers/keyboard.h>
45
#include <arch/drivers/i8042.h>
46
 
46
 
47
char *context_encoding[] = {
47
char *context_encoding[] = {
48
    "Primary",
48
    "Primary",
49
    "Secondary",
49
    "Secondary",
50
    "Nucleus",
50
    "Nucleus",
51
    "Reserved"
51
    "Reserved"
52
};
52
};
53
 
53
 
54
/** Initialize ITLB and DTLB.
54
/** Initialize ITLB and DTLB.
55
 *
55
 *
56
 * The goal of this function is to disable MMU
56
 * The goal of this function is to disable MMU
57
 * so that both TLBs can be purged and new
57
 * so that both TLBs can be purged and new
58
 * kernel 4M locked entry can be installed.
58
 * kernel 4M locked entry can be installed.
59
 * After TLB is initialized, MMU is enabled
59
 * After TLB is initialized, MMU is enabled
60
 * again.
60
 * again.
61
 *
61
 *
62
 * Switching MMU off imposes the requirement for
62
 * Switching MMU off imposes the requirement for
63
 * the kernel to run in identity mapped environment.
63
 * the kernel to run in identity mapped environment.
64
 */
64
 */
65
void tlb_arch_init(void)
65
void tlb_arch_init(void)
66
{
66
{
67
    tlb_tag_access_reg_t tag;
67
    tlb_tag_access_reg_t tag;
68
    tlb_data_t data;
68
    tlb_data_t data;
69
    frame_address_t fr;
69
    frame_address_t fr;
70
    page_address_t pg;
70
    page_address_t pg;
71
 
71
 
72
    fr.address = config.base;
72
    fr.address = config.base;
73
    pg.address = config.base;
73
    pg.address = config.base;
74
 
74
 
75
    immu_disable();
75
    immu_disable();
76
    dmmu_disable();
76
    dmmu_disable();
77
   
77
   
78
    /*
78
    /*
79
     * We do identity mapping of 4M-page at 4M.
79
     * We do identity mapping of 4M-page at 4M.
80
     */
80
     */
81
    tag.value = ASID_KERNEL;
81
    tag.value = ASID_KERNEL;
82
    tag.vpn = pg.vpn;
82
    tag.vpn = pg.vpn;
83
 
83
 
84
    itlb_tag_access_write(tag.value);
84
    itlb_tag_access_write(tag.value);
85
    dtlb_tag_access_write(tag.value);
85
    dtlb_tag_access_write(tag.value);
86
 
86
 
87
    data.value = 0;
87
    data.value = 0;
88
    data.v = true;
88
    data.v = true;
89
    data.size = PAGESIZE_4M;
89
    data.size = PAGESIZE_4M;
90
    data.pfn = fr.pfn;
90
    data.pfn = fr.pfn;
91
    data.l = true;
91
    data.l = true;
92
    data.cp = 1;
92
    data.cp = 1;
93
    data.cv = 1;
93
    data.cv = 1;
94
    data.p = true;
94
    data.p = true;
95
    data.w = true;
95
    data.w = true;
96
    data.g = true;
96
    data.g = true;
97
 
97
 
98
    itlb_data_in_write(data.value);
98
    itlb_data_in_write(data.value);
99
    dtlb_data_in_write(data.value);
99
    dtlb_data_in_write(data.value);
100
 
100
 
101
    /*
101
    /*
102
     * Register window traps can occur before MMU is enabled again.
102
     * Register window traps can occur before MMU is enabled again.
103
     * This ensures that any such traps will be handled from
103
     * This ensures that any such traps will be handled from
104
     * kernel identity mapped trap handler.
104
     * kernel identity mapped trap handler.
105
     */
105
     */
106
    trap_switch_trap_table();
106
    trap_switch_trap_table();
107
   
107
   
108
    tlb_invalidate_all();
108
    tlb_invalidate_all();
109
 
109
 
110
    dmmu_enable();
110
    dmmu_enable();
111
    immu_enable();
111
    immu_enable();
112
   
112
   
113
    /*
113
    /*
114
     * Quick hack: map frame buffer
114
     * Quick hack: map frame buffer
115
     */
115
     */
116
    fr.address = FB_PHYS_ADDRESS;
116
    fr.address = FB_PHYS_ADDRESS;
117
    pg.address = FB_VIRT_ADDRESS;
117
    pg.address = FB_VIRT_ADDRESS;
118
 
118
 
119
    tag.value = ASID_KERNEL;
119
    tag.value = ASID_KERNEL;
120
    tag.vpn = pg.vpn;
120
    tag.vpn = pg.vpn;
121
 
121
 
122
    dtlb_tag_access_write(tag.value);
122
    dtlb_tag_access_write(tag.value);
123
 
123
 
124
    data.value = 0;
124
    data.value = 0;
125
    data.v = true;
125
    data.v = true;
126
    data.size = PAGESIZE_4M;
126
    data.size = PAGESIZE_4M;
127
    data.pfn = fr.pfn;
127
    data.pfn = fr.pfn;
128
    data.l = true;
128
    data.l = true;
129
    data.cp = 0;
129
    data.cp = 0;
130
    data.cv = 0;
130
    data.cv = 0;
131
    data.p = true;
131
    data.p = true;
132
    data.w = true;
132
    data.w = true;
133
    data.g = true;
133
    data.g = true;
134
 
134
 
135
    dtlb_data_in_write(data.value);
135
    dtlb_data_in_write(data.value);
136
   
136
   
137
    /*
137
    /*
138
     * Quick hack: map keyboard
138
     * Quick hack: map keyboard
139
     */
139
     */
140
    fr.address = KBD_PHYS_ADDRESS;
140
    fr.address = KBD_PHYS_ADDRESS;
141
    pg.address = KBD_VIRT_ADDRESS;
141
    pg.address = KBD_VIRT_ADDRESS;
142
 
142
 
143
    tag.value = ASID_KERNEL;
143
    tag.value = ASID_KERNEL;
144
    tag.vpn = pg.vpn;
144
    tag.vpn = pg.vpn;
145
 
145
 
146
    dtlb_tag_access_write(tag.value);
146
    dtlb_tag_access_write(tag.value);
147
 
147
 
148
    data.value = 0;
148
    data.value = 0;
149
    data.v = true;
149
    data.v = true;
150
    data.size = PAGESIZE_8K;
150
    data.size = PAGESIZE_8K;
151
    data.pfn = fr.pfn;
151
    data.pfn = fr.pfn;
152
    data.l = true;
152
    data.l = true;
153
    data.cp = 0;
153
    data.cp = 0;
154
    data.cv = 0;
154
    data.cv = 0;
155
    data.p = true;
155
    data.p = true;
156
    data.w = true;
156
    data.w = true;
157
    data.g = true;
157
    data.g = true;
158
 
158
 
159
    dtlb_data_in_write(data.value);
159
    dtlb_data_in_write(data.value);
160
}
160
}
161
 
161
 
162
/** ITLB miss handler. */
162
/** ITLB miss handler. */
163
void fast_instruction_access_mmu_miss(void)
163
void fast_instruction_access_mmu_miss(void)
164
{
164
{
165
    panic("%s\n", __FUNCTION__);
165
    panic("%s\n", __FUNCTION__);
166
}
166
}
167
 
167
 
168
/** DTLB miss handler. */
168
/** DTLB miss handler. */
169
void fast_data_access_mmu_miss(void)
169
void fast_data_access_mmu_miss(void)
170
{
170
{
171
    tlb_tag_access_reg_t tag;
171
    tlb_tag_access_reg_t tag;
172
    tlb_data_t data;
172
    tlb_data_t data;
173
    __address tpc;
173
    __address tpc;
174
    char *tpc_str;
174
    char *tpc_str;
175
 
175
 
176
    tag.value = dtlb_tag_access_read();
176
    tag.value = dtlb_tag_access_read();
177
    if (tag.context != ASID_KERNEL || tag.vpn == 0) {
177
    if (tag.context != ASID_KERNEL || tag.vpn == 0) {
178
        tpc = tpc_read();
178
        tpc = tpc_read();
179
        tpc_str = get_symtab_entry(tpc);
179
        tpc_str = get_symtab_entry(tpc);
180
 
180
 
181
        printf("Faulting page: %P, ASID=%d\n", tag.vpn * PAGE_SIZE, tag.context);
181
        printf("Faulting page: %P, ASID=%d\n", tag.vpn * PAGE_SIZE, tag.context);
182
        printf("TPC=%P, (%s)\n", tpc, tpc_str ? tpc_str : "?");
182
        printf("TPC=%P, (%s)\n", tpc, tpc_str ? tpc_str : "?");
183
        panic("%s\n", __FUNCTION__);
183
        panic("%s\n", __FUNCTION__);
184
    }
184
    }
185
 
185
 
186
    /*
186
    /*
187
     * Identity map piece of faulting kernel address space.
187
     * Identity map piece of faulting kernel address space.
188
     */
188
     */
189
    data.value = 0;
189
    data.value = 0;
190
    data.v = true;
190
    data.v = true;
191
    data.size = PAGESIZE_8K;
191
    data.size = PAGESIZE_8K;
192
    data.pfn = tag.vpn;
192
    data.pfn = tag.vpn;
193
    data.l = false;
193
    data.l = false;
194
    data.cp = 1;
194
    data.cp = 1;
195
    data.cv = 1;
195
    data.cv = 1;
196
    data.p = true;
196
    data.p = true;
197
    data.w = true;
197
    data.w = true;
198
    data.g = true;
198
    data.g = true;
199
 
199
 
200
    dtlb_data_in_write(data.value);
200
    dtlb_data_in_write(data.value);
201
}
201
}
202
 
202
 
203
/** DTLB protection fault handler. */
203
/** DTLB protection fault handler. */
204
void fast_data_access_protection(void)
204
void fast_data_access_protection(void)
205
{
205
{
206
    panic("%s\n", __FUNCTION__);
206
    panic("%s\n", __FUNCTION__);
207
}
207
}
208
 
208
 
209
/** Print contents of both TLBs. */
209
/** Print contents of both TLBs. */
210
void tlb_print(void)
210
void tlb_print(void)
211
{
211
{
212
    int i;
212
    int i;
213
    tlb_data_t d;
213
    tlb_data_t d;
214
    tlb_tag_read_reg_t t;
214
    tlb_tag_read_reg_t t;
215
   
215
   
216
    printf("I-TLB contents:\n");
216
    printf("I-TLB contents:\n");
217
    for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
217
    for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
218
        d.value = itlb_data_access_read(i);
218
        d.value = itlb_data_access_read(i);
219
        t.value = itlb_tag_read_read(i);
219
        t.value = itlb_tag_read_read(i);
220
       
220
       
221
        printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n",
221
        printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n",
222
            i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
222
            i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
223
    }
223
    }
224
 
224
 
225
    printf("D-TLB contents:\n");
225
    printf("D-TLB contents:\n");
226
    for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
226
    for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
227
        d.value = dtlb_data_access_read(i);
227
        d.value = dtlb_data_access_read(i);
228
        t.value = dtlb_tag_read_read(i);
228
        t.value = dtlb_tag_read_read(i);
229
       
229
       
230
        printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n",
230
        printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n",
231
            i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
231
            i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
232
    }
232
    }
233
 
233
 
234
}
234
}
235
 
235
 
236
/** Invalidate all unlocked ITLB and DTLB entries. */
236
/** Invalidate all unlocked ITLB and DTLB entries. */
237
void tlb_invalidate_all(void)
237
void tlb_invalidate_all(void)
238
{
238
{
239
    int i;
239
    int i;
240
    tlb_data_t d;
240
    tlb_data_t d;
241
    tlb_tag_read_reg_t t;
241
    tlb_tag_read_reg_t t;
242
 
242
 
243
    for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
243
    for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
244
        d.value = itlb_data_access_read(i);
244
        d.value = itlb_data_access_read(i);
245
        if (!d.l) {
245
        if (!d.l) {
246
            t.value = itlb_tag_read_read(i);
246
            t.value = itlb_tag_read_read(i);
247
            d.v = false;
247
            d.v = false;
248
            itlb_tag_access_write(t.value);
248
            itlb_tag_access_write(t.value);
249
            itlb_data_access_write(i, d.value);
249
            itlb_data_access_write(i, d.value);
250
        }
250
        }
251
    }
251
    }
252
   
252
   
253
    for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
253
    for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
254
        d.value = dtlb_data_access_read(i);
254
        d.value = dtlb_data_access_read(i);
255
        if (!d.l) {
255
        if (!d.l) {
256
            t.value = dtlb_tag_read_read(i);
256
            t.value = dtlb_tag_read_read(i);
257
            d.v = false;
257
            d.v = false;
258
            dtlb_tag_access_write(t.value);
258
            dtlb_tag_access_write(t.value);
259
            dtlb_data_access_write(i, d.value);
259
            dtlb_data_access_write(i, d.value);
260
        }
260
        }
261
    }
261
    }
262
   
262
   
263
}
263
}
264
 
264
 
265
/** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context).
265
/** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context).
266
 *
266
 *
267
 * @param asid Address Space ID.
267
 * @param asid Address Space ID.
268
 */
268
 */
269
void tlb_invalidate_asid(asid_t asid)
269
void tlb_invalidate_asid(asid_t asid)
270
{
270
{
271
    /* TODO: write asid to some Context register and encode the register in second parameter below. */
271
    /* TODO: write asid to some Context register and encode the register in second parameter below. */
272
    itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
272
    itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
273
    dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
273
    dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
274
}
274
}
275
 
275
 
276
/** Invalidate all ITLB and DTLB entries for specified page range in specified address space.
276
/** Invalidate all ITLB and DTLB entries for specified page range in specified address space.
277
 *
277
 *
278
 * @param asid Address Space ID.
278
 * @param asid Address Space ID.
279
 * @param page First page which to sweep out from ITLB and DTLB.
279
 * @param page First page which to sweep out from ITLB and DTLB.
280
 * @param cnt Number of ITLB and DTLB entries to invalidate.
280
 * @param cnt Number of ITLB and DTLB entries to invalidate.
281
 */
281
 */
282
void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt)
282
void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt)
283
{
283
{
284
    int i;
284
    int i;
285
   
285
   
286
    for (i = 0; i < cnt; i++) {
286
    for (i = 0; i < cnt; i++) {
287
        /* TODO: write asid to some Context register and encode the register in second parameter below. */
287
        /* TODO: write asid to some Context register and encode the register in second parameter below. */
288
        itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
288
        itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
289
        dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
289
        dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
290
    }
290
    }
291
}
291
}
292
 
292