1 | /* |
---|
2 | * mapper.c - Map memory, file or device in process virtual address space. |
---|
3 | * |
---|
4 | * Authors Mohamed Lamine Karaoui (2015) |
---|
5 | * Alain Greiner (2016) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #include <kernel_config.h> |
---|
26 | #include <hal_types.h> |
---|
27 | #include <hal_special.h> |
---|
28 | #include <hal_uspace.h> |
---|
29 | #include <grdxt.h> |
---|
30 | #include <rwlock.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <thread.h> |
---|
33 | #include <core.h> |
---|
34 | #include <process.h> |
---|
35 | #include <kmem.h> |
---|
36 | #include <kcm.h> |
---|
37 | #include <page.h> |
---|
38 | #include <cluster.h> |
---|
39 | #include <vfs.h> |
---|
40 | #include <mapper.h> |
---|
41 | |
---|
42 | ////////////////////////// |
---|
43 | mapper_t * mapper_create() |
---|
44 | { |
---|
45 | mapper_t * mapper; |
---|
46 | kmem_req_t req; |
---|
47 | error_t error; |
---|
48 | |
---|
49 | // allocate memory for associated mapper |
---|
50 | req.type = KMEM_MAPPER; |
---|
51 | req.size = sizeof(mapper_t); |
---|
52 | req.flags = AF_KERNEL | AF_ZERO; |
---|
53 | mapper = (mapper_t *)kmem_alloc( &req ); |
---|
54 | |
---|
55 | if( mapper == NULL ) |
---|
56 | { |
---|
57 | printk("\n[ERROR] in %s : no memory for mapper descriptor\n", __FUNCTION__ ); |
---|
58 | return NULL; |
---|
59 | } |
---|
60 | |
---|
61 | // initialize refcount & inode |
---|
62 | mapper->refcount = 0; |
---|
63 | mapper->inode = NULL; |
---|
64 | |
---|
65 | // initialize radix tree |
---|
66 | error = grdxt_init( &mapper->radix, |
---|
67 | CONFIG_VMM_GRDXT_W1, |
---|
68 | CONFIG_VMM_GRDXT_W2, |
---|
69 | CONFIG_VMM_GRDXT_W3 ); |
---|
70 | |
---|
71 | if( error ) |
---|
72 | { |
---|
73 | printk("\n[ERROR] in %s : cannot initialize radix tree\n", __FUNCTION__ ); |
---|
74 | req.type = KMEM_MAPPER; |
---|
75 | req.ptr = mapper; |
---|
76 | kmem_free( &req ); |
---|
77 | return NULL; |
---|
78 | } |
---|
79 | |
---|
80 | // initialize mapper lock |
---|
81 | rwlock_init( &mapper->lock ); |
---|
82 | |
---|
83 | // initialize waiting threads xlist (empty) |
---|
84 | xlist_root_init( XPTR( local_cxy , &mapper->wait_root ) ); |
---|
85 | |
---|
86 | // initialize vsegs xlist (empty) |
---|
87 | xlist_root_init( XPTR( local_cxy , &mapper->vsegs_root ) ); |
---|
88 | |
---|
89 | return mapper; |
---|
90 | } |
---|
91 | |
---|
92 | /////////////////////////////////////////// |
---|
93 | error_t mapper_destroy( mapper_t * mapper ) |
---|
94 | { |
---|
95 | page_t * page; |
---|
96 | uint32_t found_index = 0; |
---|
97 | uint32_t start_index = 0; |
---|
98 | kmem_req_t req; |
---|
99 | error_t error; |
---|
100 | |
---|
101 | // scan radix three and release all registered pages to PPM |
---|
102 | do |
---|
103 | { |
---|
104 | // get page from radix tree |
---|
105 | page = (page_t *)grdxt_get_first( &mapper->radix , start_index , &found_index ); |
---|
106 | |
---|
107 | if( page != NULL ) |
---|
108 | { |
---|
109 | // remove page from mapper and release to PPM |
---|
110 | error = mapper_release_page( mapper , page ); |
---|
111 | |
---|
112 | if ( error ) return error; |
---|
113 | |
---|
114 | // update start_key value for next page |
---|
115 | start_index = found_index; |
---|
116 | } |
---|
117 | } |
---|
118 | while( page != NULL ); |
---|
119 | |
---|
120 | // release the memory allocated to radix-tree itself |
---|
121 | grdxt_destroy( &mapper->radix ); |
---|
122 | |
---|
123 | // release memory for mapper descriptor |
---|
124 | req.type = KMEM_MAPPER; |
---|
125 | req.ptr = mapper; |
---|
126 | kmem_free( &req ); |
---|
127 | |
---|
128 | return 0; |
---|
129 | } |
---|
130 | |
---|
131 | //////////////////////////////////////////// |
---|
132 | page_t * mapper_get_page( mapper_t * mapper, |
---|
133 | uint32_t index ) |
---|
134 | { |
---|
135 | kmem_req_t req; |
---|
136 | page_t * page; |
---|
137 | error_t error; |
---|
138 | |
---|
139 | thread_t * this = CURRENT_THREAD; |
---|
140 | |
---|
141 | // take mapper lock in READ_MODE |
---|
142 | rwlock_rd_lock( &mapper->lock ); |
---|
143 | |
---|
144 | // search page in radix tree |
---|
145 | page = (page_t *)grdxt_lookup( &mapper->radix , index ); |
---|
146 | |
---|
147 | // test if page available in mapper |
---|
148 | if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) ) // page not available |
---|
149 | { |
---|
150 | // release the lock in READ_MODE and take it in WRITE_MODE |
---|
151 | rwlock_rd_unlock( &mapper->lock ); |
---|
152 | rwlock_wr_lock( &mapper->lock ); |
---|
153 | |
---|
154 | // second test on missing page because the page status can have been modified |
---|
155 | // by another thread, when passing from READ_MODE to WRITE_MODE. |
---|
156 | // from this point there is no concurrent accesses to mapper. |
---|
157 | |
---|
158 | page = grdxt_lookup( &mapper->radix , index ); |
---|
159 | |
---|
160 | if ( page == NULL ) // missing page => load it from file system |
---|
161 | { |
---|
162 | // allocate one page from PPM |
---|
163 | req.type = KMEM_PAGE; |
---|
164 | req.size = 0; |
---|
165 | req.flags = AF_NONE; |
---|
166 | page = kmem_alloc( &req ); |
---|
167 | |
---|
168 | if( page == NULL ) |
---|
169 | { |
---|
170 | printk("\n[ERROR] in %s : thread %x cannot allocate a page in cluster %x\n", |
---|
171 | __FUNCTION__ , this->trdid , local_cxy ); |
---|
172 | rwlock_wr_unlock( &mapper->lock ); |
---|
173 | return NULL; |
---|
174 | } |
---|
175 | |
---|
176 | // initialize the page descriptor |
---|
177 | page_init( page ); |
---|
178 | page_set_flag( page , PG_INIT ); |
---|
179 | page_set_flag( page , PG_INLOAD ); |
---|
180 | page_refcount_up( page ); |
---|
181 | page->mapper = mapper; |
---|
182 | page->index = index; |
---|
183 | |
---|
184 | // insert page in mapper radix tree |
---|
185 | error = grdxt_insert( &mapper->radix, index , page ); |
---|
186 | |
---|
187 | // release mapper lock from WRITE_MODE |
---|
188 | rwlock_wr_unlock( &mapper->lock ); |
---|
189 | |
---|
190 | if( error ) |
---|
191 | { |
---|
192 | printk("\n[ERROR] in %s : thread %x cannot insert page in mapper\n", |
---|
193 | __FUNCTION__ , this->trdid ); |
---|
194 | mapper_release_page( mapper , page ); |
---|
195 | page_clear_flag( page , PG_ALL ); |
---|
196 | req.ptr = page; |
---|
197 | req.type = KMEM_PAGE; |
---|
198 | kmem_free(&req); |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | |
---|
202 | // launch I/O operation to load page from file system |
---|
203 | error = vfs_move_page_to_mapper( page ); |
---|
204 | |
---|
205 | if( error ) |
---|
206 | { |
---|
207 | printk("\n[ERROR] in %s : thread %x cannot load page from device\n", |
---|
208 | __FUNCTION__ , this->trdid ); |
---|
209 | mapper_release_page( mapper , page ); |
---|
210 | page_clear_flag( page , PG_ALL ); |
---|
211 | req.ptr = page; |
---|
212 | req.type = KMEM_PAGE; |
---|
213 | kmem_free( &req ); |
---|
214 | return NULL; |
---|
215 | } |
---|
216 | |
---|
217 | // update the mapper and index fields in page descriptor |
---|
218 | page->mapper = mapper; |
---|
219 | page->index = index; |
---|
220 | |
---|
221 | // reset the page INLOAD flag to make the page available to all readers |
---|
222 | page_clear_flag( page , PG_INLOAD ); |
---|
223 | |
---|
224 | } |
---|
225 | else if( page_is_flag( page , PG_INLOAD ) ) // page is loaded by another thread |
---|
226 | { |
---|
227 | // release mapper lock from WRITE_MODE |
---|
228 | rwlock_wr_unlock( &mapper->lock ); |
---|
229 | |
---|
230 | // deschedule to wait load completion |
---|
231 | while( 1 ) |
---|
232 | { |
---|
233 | // exit waiting loop when loaded |
---|
234 | if( page_is_flag( page , PG_INLOAD ) ) break; |
---|
235 | |
---|
236 | // deschedule |
---|
237 | sched_yield(); |
---|
238 | } |
---|
239 | |
---|
240 | } |
---|
241 | |
---|
242 | return page; |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | // release lock from READ_MODE |
---|
247 | rwlock_rd_unlock( &mapper->lock ); |
---|
248 | |
---|
249 | return page; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | /////////////////////////////////////////////// |
---|
254 | error_t mapper_release_page( mapper_t * mapper, |
---|
255 | page_t * page ) |
---|
256 | { |
---|
257 | error_t error; |
---|
258 | |
---|
259 | // lauch IO operation to update page to file system |
---|
260 | error = vfs_move_page_from_mapper( page ); |
---|
261 | |
---|
262 | if( error ) |
---|
263 | { |
---|
264 | printk("\n[ERROR] in %s : cannot update file system\n", __FUNCTION__ ); |
---|
265 | return EIO; |
---|
266 | } |
---|
267 | |
---|
268 | // take mapper lock in WRITE_MODE |
---|
269 | rwlock_wr_lock( &mapper->lock ); |
---|
270 | |
---|
271 | // remove physical page from radix tree |
---|
272 | grdxt_remove( &mapper->radix , page->index ); |
---|
273 | |
---|
274 | // release mapper lock from WRITE_MODE |
---|
275 | rwlock_wr_unlock( &mapper->lock ); |
---|
276 | |
---|
277 | // release page to PPM |
---|
278 | kmem_req_t req; |
---|
279 | req.type = KMEM_PAGE; |
---|
280 | req.ptr = page; |
---|
281 | kmem_free( &req ); |
---|
282 | |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|
286 | ///////////////////////////////////////// |
---|
287 | error_t mapper_move( mapper_t * mapper, |
---|
288 | bool_t to_buffer, |
---|
289 | uint32_t file_offset, |
---|
290 | void * buffer, |
---|
291 | uint32_t size ) |
---|
292 | { |
---|
293 | uint32_t page_offset; // first byte to move to/from a mapper page |
---|
294 | uint32_t page_count; // number of bytes to move to/from a mapper page |
---|
295 | uint32_t index; // current mapper page index |
---|
296 | uint32_t done; // number of moved bytes |
---|
297 | page_t * page; // current mapper page descriptor |
---|
298 | uint8_t * map_ptr; // current mapper address |
---|
299 | uint8_t * buf_ptr; // current buffer address |
---|
300 | |
---|
301 | // compute offsets of first and last bytes in file |
---|
302 | uint32_t min_byte = file_offset; |
---|
303 | uint32_t max_byte = file_offset + size -1; |
---|
304 | |
---|
305 | // compute indexes of pages for first and last byte in mapper |
---|
306 | uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
307 | uint32_t last = max_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
308 | |
---|
309 | done = 0; |
---|
310 | |
---|
311 | // loop on pages in mapper |
---|
312 | for( index = first ; index <= last ; index++ ) |
---|
313 | { |
---|
314 | // compute page_offset |
---|
315 | if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK; |
---|
316 | else page_offset = 0; |
---|
317 | |
---|
318 | // compute page_count |
---|
319 | if ( first == last ) page_count = size; |
---|
320 | else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset; |
---|
321 | else if ( index == last ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1; |
---|
322 | else page_count = CONFIG_PPM_PAGE_SIZE; |
---|
323 | |
---|
324 | // get page descriptor |
---|
325 | page = mapper_get_page( mapper , index ); |
---|
326 | |
---|
327 | if ( page == NULL ) return EINVAL; |
---|
328 | |
---|
329 | // compute pointer in mapper |
---|
330 | map_ptr = (uint8_t *)ppm_page2vaddr( page ) + page_offset; |
---|
331 | |
---|
332 | // compute pointer in buffer |
---|
333 | buf_ptr = (uint8_t *)buffer + done; |
---|
334 | |
---|
335 | // move fragment |
---|
336 | if( to_buffer ) |
---|
337 | { |
---|
338 | hal_copy_to_uspace( buf_ptr , map_ptr , page_count ); |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | page_do_dirty( page ); |
---|
343 | hal_copy_from_uspace( map_ptr , buf_ptr , page_count ); |
---|
344 | } |
---|
345 | |
---|
346 | done += page_count; |
---|
347 | } |
---|
348 | |
---|
349 | return 0; |
---|
350 | } |
---|
351 | |
---|