Subversion Repositories HelenOS-historic

Rev

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

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