Subversion Repositories HelenOS

Rev

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

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