Subversion Repositories HelenOS-historic

Rev

Rev 1760 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1760 Rev 1780
Line 44... Line 44...
44
#include <arch/types.h>
44
#include <arch/types.h>
45
#include <typedefs.h>
45
#include <typedefs.h>
46
#include <arch/asm.h>
46
#include <arch/asm.h>
47
#include <memstr.h>
47
#include <memstr.h>
48
 
48
 
49
static void pt_mapping_insert(as_t *as, __address page, __address frame, int flags);
49
static void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags);
50
static void pt_mapping_remove(as_t *as, __address page);
50
static void pt_mapping_remove(as_t *as, uintptr_t page);
51
static pte_t *pt_mapping_find(as_t *as, __address page);
51
static pte_t *pt_mapping_find(as_t *as, uintptr_t page);
52
 
52
 
53
page_mapping_operations_t pt_mapping_operations = {
53
page_mapping_operations_t pt_mapping_operations = {
54
    .mapping_insert = pt_mapping_insert,
54
    .mapping_insert = pt_mapping_insert,
55
    .mapping_remove = pt_mapping_remove,
55
    .mapping_remove = pt_mapping_remove,
56
    .mapping_find = pt_mapping_find
56
    .mapping_find = pt_mapping_find
Line 66... Line 66...
66
 * @param as Address space to wich page belongs.
66
 * @param as Address space to wich page belongs.
67
 * @param page Virtual address of the page to be mapped.
67
 * @param page Virtual address of the page to be mapped.
68
 * @param frame Physical address of memory frame to which the mapping is done.
68
 * @param frame Physical address of memory frame to which the mapping is done.
69
 * @param flags Flags to be used for mapping.
69
 * @param flags Flags to be used for mapping.
70
 */
70
 */
71
void pt_mapping_insert(as_t *as, __address page, __address frame, int flags)
71
void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
72
{
72
{
73
    pte_t *ptl0, *ptl1, *ptl2, *ptl3;
73
    pte_t *ptl0, *ptl1, *ptl2, *ptl3;
74
    pte_t *newpt;
74
    pte_t *newpt;
75
 
75
 
76
    ptl0 = (pte_t *) PA2KA((__address) as->page_table);
76
    ptl0 = (pte_t *) PA2KA((uintptr_t) as->page_table);
77
 
77
 
78
    if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
78
    if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
79
        newpt = (pte_t *)frame_alloc(ONE_FRAME, FRAME_KA);
79
        newpt = (pte_t *)frame_alloc(ONE_FRAME, FRAME_KA);
80
        memsetb((__address)newpt, PAGE_SIZE, 0);
80
        memsetb((uintptr_t)newpt, PAGE_SIZE, 0);
81
        SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
81
        SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
82
        SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
82
        SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
83
    }
83
    }
84
 
84
 
85
    ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
85
    ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
86
 
86
 
87
    if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
87
    if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
88
        newpt = (pte_t *)frame_alloc(ONE_FRAME, FRAME_KA);
88
        newpt = (pte_t *)frame_alloc(ONE_FRAME, FRAME_KA);
89
        memsetb((__address)newpt, PAGE_SIZE, 0);
89
        memsetb((uintptr_t)newpt, PAGE_SIZE, 0);
90
        SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
90
        SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
91
        SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
91
        SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
92
    }
92
    }
93
 
93
 
94
    ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
94
    ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
95
 
95
 
96
    if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
96
    if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
97
        newpt = (pte_t *)frame_alloc(ONE_FRAME, FRAME_KA);
97
        newpt = (pte_t *)frame_alloc(ONE_FRAME, FRAME_KA);
98
        memsetb((__address)newpt, PAGE_SIZE, 0);
98
        memsetb((uintptr_t)newpt, PAGE_SIZE, 0);
99
        SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
99
        SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
100
        SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
100
        SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
101
    }
101
    }
102
 
102
 
103
    ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
103
    ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
Line 117... Line 117...
117
 * The page table must be locked and interrupts must be disabled.
117
 * The page table must be locked and interrupts must be disabled.
118
 *
118
 *
119
 * @param as Address space to wich page belongs.
119
 * @param as Address space to wich page belongs.
120
 * @param page Virtual address of the page to be demapped.
120
 * @param page Virtual address of the page to be demapped.
121
 */
121
 */
122
void pt_mapping_remove(as_t *as, __address page)
122
void pt_mapping_remove(as_t *as, uintptr_t page)
123
{
123
{
124
    pte_t *ptl0, *ptl1, *ptl2, *ptl3;
124
    pte_t *ptl0, *ptl1, *ptl2, *ptl3;
125
    bool empty = true;
125
    bool empty = true;
126
    int i;
126
    int i;
127
 
127
 
128
    /*
128
    /*
129
     * First, remove the mapping, if it exists.
129
     * First, remove the mapping, if it exists.
130
     */
130
     */
131
 
131
 
132
    ptl0 = (pte_t *) PA2KA((__address) as->page_table);
132
    ptl0 = (pte_t *) PA2KA((uintptr_t) as->page_table);
133
 
133
 
134
    if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
134
    if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
135
        return;
135
        return;
136
 
136
 
137
    ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
137
    ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
Line 145... Line 145...
145
        return;
145
        return;
146
 
146
 
147
    ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
147
    ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
148
 
148
 
149
    /* Destroy the mapping. Setting to PAGE_NOT_PRESENT is not sufficient. */
149
    /* Destroy the mapping. Setting to PAGE_NOT_PRESENT is not sufficient. */
150
    memsetb((__address) &ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
150
    memsetb((uintptr_t) &ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
151
 
151
 
152
    /*
152
    /*
153
     * Second, free all empty tables along the way from PTL3 down to PTL0.
153
     * Second, free all empty tables along the way from PTL3 down to PTL0.
154
     */
154
     */
155
   
155
   
Line 163... Line 163...
163
    if (empty) {
163
    if (empty) {
164
        /*
164
        /*
165
         * PTL3 is empty.
165
         * PTL3 is empty.
166
         * Release the frame and remove PTL3 pointer from preceding table.
166
         * Release the frame and remove PTL3 pointer from preceding table.
167
         */
167
         */
168
        frame_free(KA2PA((__address) ptl3));
168
        frame_free(KA2PA((uintptr_t) ptl3));
169
        if (PTL2_ENTRIES)
169
        if (PTL2_ENTRIES)
170
            memsetb((__address) &ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
170
            memsetb((uintptr_t) &ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
171
        else if (PTL1_ENTRIES)
171
        else if (PTL1_ENTRIES)
172
            memsetb((__address) &ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
172
            memsetb((uintptr_t) &ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
173
        else
173
        else
174
            memsetb((__address) &ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
174
            memsetb((uintptr_t) &ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
175
    } else {
175
    } else {
176
        /*
176
        /*
177
         * PTL3 is not empty.
177
         * PTL3 is not empty.
178
         * Therefore, there must be a path from PTL0 to PTL3 and
178
         * Therefore, there must be a path from PTL0 to PTL3 and
179
         * thus nothing to free in higher levels.
179
         * thus nothing to free in higher levels.
Line 192... Line 192...
192
        if (empty) {
192
        if (empty) {
193
            /*
193
            /*
194
             * PTL2 is empty.
194
             * PTL2 is empty.
195
             * Release the frame and remove PTL2 pointer from preceding table.
195
             * Release the frame and remove PTL2 pointer from preceding table.
196
             */
196
             */
197
            frame_free(KA2PA((__address) ptl2));
197
            frame_free(KA2PA((uintptr_t) ptl2));
198
            if (PTL1_ENTRIES)
198
            if (PTL1_ENTRIES)
199
                memsetb((__address) &ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
199
                memsetb((uintptr_t) &ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
200
            else
200
            else
201
                memsetb((__address) &ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
201
                memsetb((uintptr_t) &ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
202
        }
202
        }
203
        else {
203
        else {
204
            /*
204
            /*
205
             * PTL2 is not empty.
205
             * PTL2 is not empty.
206
             * Therefore, there must be a path from PTL0 to PTL2 and
206
             * Therefore, there must be a path from PTL0 to PTL2 and
Line 221... Line 221...
221
        if (empty) {
221
        if (empty) {
222
            /*
222
            /*
223
             * PTL1 is empty.
223
             * PTL1 is empty.
224
             * Release the frame and remove PTL1 pointer from preceding table.
224
             * Release the frame and remove PTL1 pointer from preceding table.
225
             */
225
             */
226
            frame_free(KA2PA((__address) ptl1));
226
            frame_free(KA2PA((uintptr_t) ptl1));
227
            memsetb((__address) &ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
227
            memsetb((uintptr_t) &ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
228
        }
228
        }
229
    }
229
    }
230
 
230
 
231
}
231
}
232
 
232
 
Line 239... Line 239...
239
 * @param as Address space to which page belongs.
239
 * @param as Address space to which page belongs.
240
 * @param page Virtual page.
240
 * @param page Virtual page.
241
 *
241
 *
242
 * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise.
242
 * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise.
243
 */
243
 */
244
pte_t *pt_mapping_find(as_t *as, __address page)
244
pte_t *pt_mapping_find(as_t *as, uintptr_t page)
245
{
245
{
246
    pte_t *ptl0, *ptl1, *ptl2, *ptl3;
246
    pte_t *ptl0, *ptl1, *ptl2, *ptl3;
247
 
247
 
248
    ptl0 = (pte_t *) PA2KA((__address) as->page_table);
248
    ptl0 = (pte_t *) PA2KA((uintptr_t) as->page_table);
249
 
249
 
250
    if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
250
    if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
251
        return NULL;
251
        return NULL;
252
 
252
 
253
    ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
253
    ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));