Subversion Repositories HelenOS

Rev

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

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