Subversion Repositories HelenOS

Rev

Rev 2132 | Rev 2141 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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