Subversion Repositories HelenOS

Rev

Rev 1425 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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