Subversion Repositories HelenOS

Rev

Rev 3397 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3397 Rev 3862
1
/*
1
/*
2
 * Copyright (c) 2006 Jakub Jermar
2
 * Copyright (c) 2006 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
/** @addtogroup genericmm
29
/** @addtogroup genericmm
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Backend for anonymous memory address space areas.
35
 * @brief   Backend for anonymous memory address space areas.
36
 *
36
 *
37
 */
37
 */
38
 
38
 
39
#include <mm/as.h>
39
#include <mm/as.h>
40
#include <mm/page.h>
40
#include <mm/page.h>
41
#include <genarch/mm/page_pt.h>
41
#include <genarch/mm/page_pt.h>
42
#include <genarch/mm/page_ht.h>
42
#include <genarch/mm/page_ht.h>
43
#include <mm/frame.h>
43
#include <mm/frame.h>
44
#include <mm/slab.h>
44
#include <mm/slab.h>
45
#include <synch/mutex.h>
45
#include <synch/mutex.h>
46
#include <adt/list.h>
46
#include <adt/list.h>
47
#include <adt/btree.h>
47
#include <adt/btree.h>
48
#include <errno.h>
48
#include <errno.h>
-
 
49
#include <arch/asm.h>
49
#include <arch/types.h>
50
#include <arch/types.h>
50
#include <align.h>
51
#include <align.h>
51
#include <arch.h>
52
#include <arch.h>
52
 
53
 
53
#ifdef CONFIG_VIRT_IDX_DCACHE
54
#ifdef CONFIG_VIRT_IDX_DCACHE
54
#include <arch/mm/cache.h>
55
#include <arch/mm/cache.h>
55
#endif
56
#endif
56
 
57
 
57
static int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access);
58
static int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access);
58
static void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame);
59
static void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame);
59
static void anon_share(as_area_t *area);
60
static void anon_share(as_area_t *area);
60
 
61
 
61
mem_backend_t anon_backend = {
62
mem_backend_t anon_backend = {
62
    .page_fault = anon_page_fault,
63
    .page_fault = anon_page_fault,
63
    .frame_free = anon_frame_free,
64
    .frame_free = anon_frame_free,
64
    .share = anon_share
65
    .share = anon_share
65
};
66
};
66
 
67
 
67
/** Service a page fault in the anonymous memory address space area.
68
/** Service a page fault in the anonymous memory address space area.
68
 *
69
 *
69
 * The address space area and page tables must be already locked.
70
 * The address space area and page tables must be already locked.
70
 *
71
 *
71
 * @param area Pointer to the address space area.
72
 * @param area Pointer to the address space area.
72
 * @param addr Faulting virtual address.
73
 * @param addr Faulting virtual address.
73
 * @param access Access mode that caused the fault (i.e. read/write/exec).
74
 * @param access Access mode that caused the fault (i.e. read/write/exec).
74
 *
75
 *
75
 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
76
 * @return AS_PF_FAULT on failure (i.e. page fault) or AS_PF_OK on success (i.e.
76
 *     serviced).
77
 *     serviced).
77
 */
78
 */
78
int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
79
int anon_page_fault(as_area_t *area, uintptr_t addr, pf_access_t access)
79
{
80
{
80
    uintptr_t frame;
81
    uintptr_t frame;
81
 
82
 
82
    if (!as_area_check_access(area, access))
83
    if (!as_area_check_access(area, access))
83
        return AS_PF_FAULT;
84
        return AS_PF_FAULT;
84
 
85
 
85
    if (area->sh_info) {
86
    if (area->sh_info) {
86
        btree_node_t *leaf;
87
        btree_node_t *leaf;
87
       
88
       
88
        /*
89
        /*
89
         * The area is shared, chances are that the mapping can be found
90
         * The area is shared, chances are that the mapping can be found
90
         * in the pagemap of the address space area share info
91
         * in the pagemap of the address space area share info
91
         * structure.
92
         * structure.
92
         * In the case that the pagemap does not contain the respective
93
         * In the case that the pagemap does not contain the respective
93
         * mapping, a new frame is allocated and the mapping is created.
94
         * mapping, a new frame is allocated and the mapping is created.
94
         */
95
         */
95
        mutex_lock(&area->sh_info->lock);
96
        mutex_lock(&area->sh_info->lock);
96
        frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
97
        frame = (uintptr_t) btree_search(&area->sh_info->pagemap,
97
            ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf);
98
            ALIGN_DOWN(addr, PAGE_SIZE) - area->base, &leaf);
98
        if (!frame) {
99
        if (!frame) {
99
            bool allocate = true;
100
            bool allocate = true;
100
            unsigned int i;
101
            unsigned int i;
101
           
102
           
102
            /*
103
            /*
103
             * Zero can be returned as a valid frame address.
104
             * Zero can be returned as a valid frame address.
104
             * Just a small workaround.
105
             * Just a small workaround.
105
             */
106
             */
106
            for (i = 0; i < leaf->keys; i++) {
107
            for (i = 0; i < leaf->keys; i++) {
107
                if (leaf->key[i] ==
108
                if (leaf->key[i] ==
108
                    ALIGN_DOWN(addr, PAGE_SIZE) - area->base) {
109
                    ALIGN_DOWN(addr, PAGE_SIZE) - area->base) {
109
                    allocate = false;
110
                    allocate = false;
110
                    break;
111
                    break;
111
                }
112
                }
112
            }
113
            }
113
            if (allocate) {
114
            if (allocate) {
114
                frame = (uintptr_t) frame_alloc(ONE_FRAME, 0);
115
                frame = (uintptr_t) frame_alloc(ONE_FRAME, 0);
115
                memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
116
                memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
116
               
117
               
117
                /*
118
                /*
118
                 * Insert the address of the newly allocated
119
                 * Insert the address of the newly allocated
119
                 * frame to the pagemap.
120
                 * frame to the pagemap.
120
                 */
121
                 */
121
                btree_insert(&area->sh_info->pagemap,
122
                btree_insert(&area->sh_info->pagemap,
122
                    ALIGN_DOWN(addr, PAGE_SIZE) - area->base,
123
                    ALIGN_DOWN(addr, PAGE_SIZE) - area->base,
123
                    (void *) frame, leaf);
124
                    (void *) frame, leaf);
124
            }
125
            }
125
        }
126
        }
126
        frame_reference_add(ADDR2PFN(frame));
127
        frame_reference_add(ADDR2PFN(frame));
127
        mutex_unlock(&area->sh_info->lock);
128
        mutex_unlock(&area->sh_info->lock);
128
    } else {
129
    } else {
129
 
130
 
130
        /*
131
        /*
131
         * In general, there can be several reasons that
132
         * In general, there can be several reasons that
132
         * can have caused this fault.
133
         * can have caused this fault.
133
         *
134
         *
134
         * - non-existent mapping: the area is an anonymous
135
         * - non-existent mapping: the area is an anonymous
135
         *   area (e.g. heap or stack) and so far has not been
136
         *   area (e.g. heap or stack) and so far has not been
136
         *   allocated a frame for the faulting page
137
         *   allocated a frame for the faulting page
137
         *
138
         *
138
         * - non-present mapping: another possibility,
139
         * - non-present mapping: another possibility,
139
         *   currently not implemented, would be frame
140
         *   currently not implemented, would be frame
140
         *   reuse; when this becomes a possibility,
141
         *   reuse; when this becomes a possibility,
141
         *   do not forget to distinguish between
142
         *   do not forget to distinguish between
142
         *   the different causes
143
         *   the different causes
143
         */
144
         */
144
        frame = (uintptr_t) frame_alloc(ONE_FRAME, 0);
145
        frame = (uintptr_t) frame_alloc(ONE_FRAME, 0);
145
        memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
146
        memsetb((void *) PA2KA(frame), FRAME_SIZE, 0);
146
    }
147
    }
147
   
148
   
148
    /*
149
    /*
149
     * Map 'page' to 'frame'.
150
     * Map 'page' to 'frame'.
150
     * Note that TLB shootdown is not attempted as only new information is
151
     * Note that TLB shootdown is not attempted as only new information is
151
     * being inserted into page tables.
152
     * being inserted into page tables.
152
     */
153
     */
153
    page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
154
    page_mapping_insert(AS, addr, frame, as_area_get_flags(area));
154
    if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
155
    if (!used_space_insert(area, ALIGN_DOWN(addr, PAGE_SIZE), 1))
155
        panic("Could not insert used space.\n");
156
        panic("Could not insert used space.\n");
156
       
157
       
157
    return AS_PF_OK;
158
    return AS_PF_OK;
158
}
159
}
159
 
160
 
160
/** Free a frame that is backed by the anonymous memory backend.
161
/** Free a frame that is backed by the anonymous memory backend.
161
 *
162
 *
162
 * The address space area and page tables must be already locked.
163
 * The address space area and page tables must be already locked.
163
 *
164
 *
164
 * @param area Ignored.
165
 * @param area Ignored.
165
 * @param page Virtual address of the page corresponding to the frame.
166
 * @param page Virtual address of the page corresponding to the frame.
166
 * @param frame Frame to be released.
167
 * @param frame Frame to be released.
167
 */
168
 */
168
void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
169
void anon_frame_free(as_area_t *area, uintptr_t page, uintptr_t frame)
169
{
170
{
170
    frame_free(frame);
171
    frame_free(frame);
171
}
172
}
172
 
173
 
173
/** Share the anonymous address space area.
174
/** Share the anonymous address space area.
174
 *
175
 *
175
 * Sharing of anonymous area is done by duplicating its entire mapping
176
 * Sharing of anonymous area is done by duplicating its entire mapping
176
 * to the pagemap. Page faults will primarily search for frames there.
177
 * to the pagemap. Page faults will primarily search for frames there.
177
 *
178
 *
178
 * The address space and address space area must be already locked.
179
 * The address space and address space area must be already locked.
179
 *
180
 *
180
 * @param area Address space area to be shared.
181
 * @param area Address space area to be shared.
181
 */
182
 */
182
void anon_share(as_area_t *area)
183
void anon_share(as_area_t *area)
183
{
184
{
184
    link_t *cur;
185
    link_t *cur;
185
 
186
 
186
    /*
187
    /*
187
     * Copy used portions of the area to sh_info's page map.
188
     * Copy used portions of the area to sh_info's page map.
188
     */
189
     */
189
    mutex_lock(&area->sh_info->lock);
190
    mutex_lock(&area->sh_info->lock);
190
    for (cur = area->used_space.leaf_head.next;
191
    for (cur = area->used_space.leaf_head.next;
191
        cur != &area->used_space.leaf_head; cur = cur->next) {
192
        cur != &area->used_space.leaf_head; cur = cur->next) {
192
        btree_node_t *node;
193
        btree_node_t *node;
193
        unsigned int i;
194
        unsigned int i;
194
       
195
       
195
        node = list_get_instance(cur, btree_node_t, leaf_link);
196
        node = list_get_instance(cur, btree_node_t, leaf_link);
196
        for (i = 0; i < node->keys; i++) {
197
        for (i = 0; i < node->keys; i++) {
197
            uintptr_t base = node->key[i];
198
            uintptr_t base = node->key[i];
198
            count_t count = (count_t) node->value[i];
199
            count_t count = (count_t) node->value[i];
199
            unsigned int j;
200
            unsigned int j;
200
           
201
           
201
            for (j = 0; j < count; j++) {
202
            for (j = 0; j < count; j++) {
202
                pte_t *pte;
203
                pte_t *pte;
203
           
204
           
204
                page_table_lock(area->as, false);
205
                page_table_lock(area->as, false);
205
                pte = page_mapping_find(area->as,
206
                pte = page_mapping_find(area->as,
206
                    base + j * PAGE_SIZE);
207
                    base + j * PAGE_SIZE);
207
                ASSERT(pte && PTE_VALID(pte) &&
208
                ASSERT(pte && PTE_VALID(pte) &&
208
                    PTE_PRESENT(pte));
209
                    PTE_PRESENT(pte));
209
                btree_insert(&area->sh_info->pagemap,
210
                btree_insert(&area->sh_info->pagemap,
210
                    (base + j * PAGE_SIZE) - area->base,
211
                    (base + j * PAGE_SIZE) - area->base,
211
                    (void *) PTE_GET_FRAME(pte), NULL);
212
                    (void *) PTE_GET_FRAME(pte), NULL);
212
                page_table_unlock(area->as, false);
213
                page_table_unlock(area->as, false);
213
 
214
 
214
                pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
215
                pfn_t pfn = ADDR2PFN(PTE_GET_FRAME(pte));
215
                frame_reference_add(pfn);
216
                frame_reference_add(pfn);
216
            }
217
            }
217
 
218
 
218
        }
219
        }
219
    }
220
    }
220
    mutex_unlock(&area->sh_info->lock);
221
    mutex_unlock(&area->sh_info->lock);
221
}
222
}
222
 
223
 
223
/** @}
224
/** @}
224
 */
225
 */
225
 
226