Subversion Repositories HelenOS-historic

Rev

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

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