1 | /* |
---|
2 | * process.c - process related management |
---|
3 | * |
---|
4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
5 | * Mohamed Lamine Karaoui (2015) |
---|
6 | * Alain Greiner (2016,2017) |
---|
7 | * |
---|
8 | * Copyright (c) UPMC Sorbonne Universites |
---|
9 | * |
---|
10 | * This file is part of ALMOS-MKH.. |
---|
11 | * |
---|
12 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
13 | * under the terms of the GNU General Public License as published by |
---|
14 | * the Free Software Foundation; version 2.0 of the License. |
---|
15 | * |
---|
16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | * General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU General Public License |
---|
22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include <kernel_config.h> |
---|
27 | #include <hal_types.h> |
---|
28 | #include <hal_remote.h> |
---|
29 | #include <hal_uspace.h> |
---|
30 | #include <errno.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <memcpy.h> |
---|
33 | #include <bits.h> |
---|
34 | #include <kmem.h> |
---|
35 | #include <page.h> |
---|
36 | #include <vmm.h> |
---|
37 | #include <vfs.h> |
---|
38 | #include <core.h> |
---|
39 | #include <thread.h> |
---|
40 | #include <list.h> |
---|
41 | #include <scheduler.h> |
---|
42 | #include <remote_spinlock.h> |
---|
43 | #include <dqdt.h> |
---|
44 | #include <cluster.h> |
---|
45 | #include <ppm.h> |
---|
46 | #include <boot_info.h> |
---|
47 | #include <process.h> |
---|
48 | #include <elf.h> |
---|
49 | #include <syscalls.h> |
---|
50 | |
---|
51 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
52 | // Extern global variables |
---|
53 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
54 | |
---|
55 | extern process_t process_zero; |
---|
56 | |
---|
57 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
58 | // Process initialisation related functions |
---|
59 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
60 | |
---|
61 | /////////////////////////// |
---|
62 | process_t * process_alloc() |
---|
63 | { |
---|
64 | kmem_req_t req; |
---|
65 | |
---|
66 | req.type = KMEM_PROCESS; |
---|
67 | req.size = sizeof(process_t); |
---|
68 | req.flags = AF_KERNEL; |
---|
69 | |
---|
70 | return (process_t *)kmem_alloc( &req ); |
---|
71 | } |
---|
72 | |
---|
73 | //////////////////////////////////////// |
---|
74 | void process_free( process_t * process ) |
---|
75 | { |
---|
76 | kmem_req_t req; |
---|
77 | |
---|
78 | req.type = KMEM_PROCESS; |
---|
79 | req.ptr = process; |
---|
80 | kmem_free( &req ); |
---|
81 | } |
---|
82 | |
---|
83 | ///////////////////////////////////////////////// |
---|
84 | void process_reference_init( process_t * process, |
---|
85 | pid_t pid, |
---|
86 | xptr_t parent_xp ) |
---|
87 | { |
---|
88 | cxy_t parent_cxy; |
---|
89 | process_t * parent_ptr; |
---|
90 | pid_t parent_pid; |
---|
91 | |
---|
92 | process_dmsg("\n[INFO] %s : enters for process %x in cluster %x\n", |
---|
93 | __FUNCTION__ , pid , local_cxy ); |
---|
94 | |
---|
95 | // get parent process cluster, local pointer, and pid |
---|
96 | // for all processes other than process_zero |
---|
97 | if( process == &process_zero ) |
---|
98 | { |
---|
99 | assert( (pid == 0) , __FUNCTION__ , "process_zero must have PID = 0\n"); |
---|
100 | |
---|
101 | parent_cxy = 0; |
---|
102 | parent_ptr = NULL; |
---|
103 | parent_pid = 0; // process_zero is its own parent... |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | assert( (parent_xp != XPTR_NULL) , __FUNCTION__ , "parent_xp cannot be NULL\n"); |
---|
108 | |
---|
109 | parent_cxy = GET_CXY( parent_xp ); |
---|
110 | parent_ptr = (process_t *)GET_PTR( parent_xp ); |
---|
111 | parent_pid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) ); |
---|
112 | } |
---|
113 | |
---|
114 | // initialize PID and PPID |
---|
115 | process->pid = pid; |
---|
116 | process->ppid = parent_pid; |
---|
117 | |
---|
118 | // reset reference process vmm (not for kernel process) |
---|
119 | if( pid ) vmm_init( process ); |
---|
120 | |
---|
121 | // reset reference process file descriptors array |
---|
122 | process_fd_init( process ); |
---|
123 | |
---|
124 | // reset reference process files structures and cwd_lock |
---|
125 | process->vfs_root_xp = XPTR_NULL; |
---|
126 | process->vfs_bin_xp = XPTR_NULL; |
---|
127 | process->vfs_cwd_xp = XPTR_NULL; |
---|
128 | remote_rwlock_init( XPTR( local_cxy , &process->cwd_lock ) ); |
---|
129 | |
---|
130 | // reset children list root |
---|
131 | xlist_root_init( XPTR( local_cxy , &process->children_root ) ); |
---|
132 | process->children_nr = 0; |
---|
133 | |
---|
134 | // reset semaphore / mutex / barrier / condvar list roots |
---|
135 | xlist_root_init( XPTR( local_cxy , &process->sem_root ) ); |
---|
136 | xlist_root_init( XPTR( local_cxy , &process->mutex_root ) ); |
---|
137 | xlist_root_init( XPTR( local_cxy , &process->barrier_root ) ); |
---|
138 | xlist_root_init( XPTR( local_cxy , &process->condvar_root ) ); |
---|
139 | remote_spinlock_init( XPTR( local_cxy , &process->sync_lock ) ); |
---|
140 | |
---|
141 | // register new process in the parent children list (not for kernel process) |
---|
142 | if( pid ) |
---|
143 | { |
---|
144 | xptr_t entry = XPTR( local_cxy , &process->brothers_list ); |
---|
145 | xptr_t root = XPTR( parent_cxy , &parent_ptr->children_root ); |
---|
146 | xlist_add_first( root , entry ); |
---|
147 | } |
---|
148 | |
---|
149 | // reset th_tbl[] array as empty |
---|
150 | uint32_t i; |
---|
151 | for( i = 0 ; i < CONFIG_THREAD_MAX_PER_CLUSTER ; i++ ) |
---|
152 | { |
---|
153 | process->th_tbl[i] = NULL; |
---|
154 | } |
---|
155 | process->th_nr = 0; |
---|
156 | spinlock_init( &process->th_lock ); |
---|
157 | |
---|
158 | // set ref_xp field |
---|
159 | process->ref_xp = XPTR( local_cxy , process ); |
---|
160 | |
---|
161 | // register new process descriptor in local cluster manager local_list |
---|
162 | cluster_process_local_link( process ); |
---|
163 | |
---|
164 | // register new process descriptor in owner cluster manager copies_list |
---|
165 | cluster_process_copies_link( process ); |
---|
166 | |
---|
167 | // initialize signal manager TODO [AG] |
---|
168 | |
---|
169 | hal_fence(); |
---|
170 | |
---|
171 | process_dmsg("\n[INFO] %s : exit for process %x in cluster %x\n", |
---|
172 | __FUNCTION__ , pid ); |
---|
173 | |
---|
174 | } // process_reference init() |
---|
175 | |
---|
176 | ///////////////////////////////////////////////////// |
---|
177 | error_t process_copy_init( process_t * local_process, |
---|
178 | xptr_t reference_process_xp ) |
---|
179 | { |
---|
180 | // get reference process cluster and local pointer |
---|
181 | cxy_t ref_cxy = GET_CXY( reference_process_xp ); |
---|
182 | process_t * ref_ptr = (process_t *)GET_PTR( reference_process_xp ); |
---|
183 | |
---|
184 | // reset local process vmm |
---|
185 | vmm_init( local_process ); |
---|
186 | |
---|
187 | // reset process file descriptors array |
---|
188 | process_fd_init( local_process ); |
---|
189 | |
---|
190 | // reset vfs_root_xp / vfs_bin_xp / vfs_cwd_xp fields |
---|
191 | local_process->vfs_root_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->vfs_root_xp ) ); |
---|
192 | local_process->vfs_bin_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->vfs_bin_xp ) ); |
---|
193 | local_process->vfs_cwd_xp = XPTR_NULL; |
---|
194 | |
---|
195 | // set the pid, ppid, ref_xp fields |
---|
196 | local_process->pid = hal_remote_lw( XPTR( ref_cxy , &ref_ptr->pid ) ); |
---|
197 | local_process->ppid = hal_remote_lw( XPTR( ref_cxy , &ref_ptr->ppid ) ); |
---|
198 | local_process->ref_xp = reference_process_xp; |
---|
199 | |
---|
200 | process_dmsg("\n[INFO] %s : enter for process %x in cluster %x\n", |
---|
201 | __FUNCTION__ , local_process->pid ); |
---|
202 | |
---|
203 | // reset children list root (not used in a process descriptor copy) |
---|
204 | xlist_root_init( XPTR( local_cxy , &local_process->children_root ) ); |
---|
205 | local_process->children_nr = 0; |
---|
206 | |
---|
207 | // reset brothers list (not used in a process descriptor copy) |
---|
208 | xlist_entry_init( XPTR( local_cxy , &local_process->brothers_list ) ); |
---|
209 | |
---|
210 | // reset semaphores list root (not used in a process descriptor copy) |
---|
211 | xlist_root_init( XPTR( local_cxy , &local_process->sem_root ) ); |
---|
212 | xlist_root_init( XPTR( local_cxy , &local_process->mutex_root ) ); |
---|
213 | xlist_root_init( XPTR( local_cxy , &local_process->barrier_root ) ); |
---|
214 | xlist_root_init( XPTR( local_cxy , &local_process->condvar_root ) ); |
---|
215 | |
---|
216 | // reset th_tbl[] array as empty |
---|
217 | uint32_t i; |
---|
218 | for( i = 0 ; i < CONFIG_THREAD_MAX_PER_CLUSTER ; i++ ) |
---|
219 | { |
---|
220 | local_process->th_tbl[i] = NULL; |
---|
221 | } |
---|
222 | local_process->th_nr = 0; |
---|
223 | spinlock_init( &local_process->th_lock ); |
---|
224 | |
---|
225 | // register new process descriptor in local cluster manager local_list |
---|
226 | cluster_process_local_link( local_process ); |
---|
227 | |
---|
228 | // register new process descriptor in owner cluster manager copies_list |
---|
229 | cluster_process_copies_link( local_process ); |
---|
230 | |
---|
231 | // initialize signal manager TODO [AG] |
---|
232 | |
---|
233 | hal_fence(); |
---|
234 | |
---|
235 | process_dmsg("\n[INFO] %s : exit for process %x in cluster %x\n", |
---|
236 | __FUNCTION__ , local_process->pid ); |
---|
237 | |
---|
238 | return 0; |
---|
239 | |
---|
240 | } // end process_copy_init() |
---|
241 | |
---|
242 | /////////////////////////////////////////// |
---|
243 | void process_destroy( process_t * process ) |
---|
244 | { |
---|
245 | if( process->th_nr != 0 ) |
---|
246 | { |
---|
247 | printk("\n[PANIC] in %s : process %x in cluster %x has still active threads\n", |
---|
248 | __FUNCTION__ , process->pid , local_cxy ); |
---|
249 | hal_core_sleep(); |
---|
250 | } |
---|
251 | |
---|
252 | // get local process manager pointer |
---|
253 | pmgr_t * pmgr = &LOCAL_CLUSTER->pmgr; |
---|
254 | |
---|
255 | // get the lock protecting the list of local process descriptors |
---|
256 | remote_spinlock_lock( XPTR( local_cxy , &pmgr->local_lock ) ); |
---|
257 | |
---|
258 | // remove the process descriptor from local_list in local cluster manager |
---|
259 | xlist_unlink( XPTR( local_cxy , &process->local_list ) ); |
---|
260 | |
---|
261 | // release the lock protecting the list of local process descriptors |
---|
262 | remote_spinlock_unlock( XPTR( local_cxy , &pmgr->local_lock ) ); |
---|
263 | |
---|
264 | // get extended pointer on copies_lock in owner cluster manager |
---|
265 | cxy_t owner_cxy = CXY_FROM_PID( process->pid ); |
---|
266 | lpid_t lpid = LPID_FROM_PID( process->pid ); |
---|
267 | xptr_t copies_lock = XPTR( owner_cxy , &pmgr->copies_lock[lpid] ); |
---|
268 | |
---|
269 | // remove the local process descriptor from copies_list |
---|
270 | remote_spinlock_lock( copies_lock ); |
---|
271 | xlist_unlink( XPTR( local_cxy , &process->copies_list ) ); |
---|
272 | remote_spinlock_unlock( copies_lock ); |
---|
273 | |
---|
274 | // synchronize memory |
---|
275 | hal_fence(); |
---|
276 | |
---|
277 | // From this point, the process descriptor is unreachable |
---|
278 | |
---|
279 | // close all open files and update dirty TODO [AG] |
---|
280 | |
---|
281 | // release signal manager TODO [AG] |
---|
282 | |
---|
283 | // Decrease refcount for bin file, root file and cwd file |
---|
284 | if( process->vfs_bin_xp != XPTR_NULL ) vfs_file_count_down( process->vfs_bin_xp ); |
---|
285 | if( process->vfs_root_xp != XPTR_NULL ) vfs_file_count_down( process->vfs_root_xp ); |
---|
286 | if( process->vfs_cwd_xp != XPTR_NULL ) vfs_file_count_down( process->vfs_cwd_xp ); |
---|
287 | |
---|
288 | // Destroy VMM |
---|
289 | vmm_destroy( process ); |
---|
290 | |
---|
291 | process_dmsg("\n[INFO] %s for pid %d / page_faults = %d\n", |
---|
292 | __FUNCTION__ , process->pid, process->vmm.pgfault_nr ); |
---|
293 | } |
---|
294 | |
---|
295 | //////////////////////////////////////// |
---|
296 | void process_kill( process_t * process ) |
---|
297 | { |
---|
298 | thread_t * thread; // pointer on current thead descriptor |
---|
299 | uint32_t ltid; // index in process th_tbl |
---|
300 | uint32_t count; // thread counter |
---|
301 | |
---|
302 | // get lock protecting th_tbl[] |
---|
303 | spinlock_lock( &process->th_lock ); |
---|
304 | |
---|
305 | // first loop on threads to send the THREAD_SIG_KILL signal to all process threads |
---|
306 | // we use both "ltid" and "count" indexes, because it can exist "holes" in th_tbl |
---|
307 | for( ltid = 0 , count = 0 ; |
---|
308 | (ltid < CONFIG_THREAD_MAX_PER_CLUSTER) && (count < process->th_nr) ; |
---|
309 | ltid++ ) |
---|
310 | { |
---|
311 | thread = process->th_tbl[ltid]; |
---|
312 | |
---|
313 | if( thread != NULL ) |
---|
314 | { |
---|
315 | thread_kill( thread ); |
---|
316 | count++; |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | volatile uint32_t ko; |
---|
321 | |
---|
322 | // second loop on threads to wait acknowledge from scheduler, |
---|
323 | // unlink thread from process and parent thread, and release thread descriptor |
---|
324 | for( ltid = 0 , count = 0 ; |
---|
325 | (ltid < CONFIG_THREAD_MAX_PER_CLUSTER) && (count < process->th_nr) ; |
---|
326 | ltid++ ) |
---|
327 | { |
---|
328 | thread = process->th_tbl[ltid]; |
---|
329 | |
---|
330 | if( thread != NULL ) |
---|
331 | { |
---|
332 | // wait scheduler acknowledge |
---|
333 | do { ko = (thread->signals & THREAD_SIG_KILL); } while( ko ); |
---|
334 | |
---|
335 | // unlink thread from brothers list if required |
---|
336 | if( (thread->flags & THREAD_FLAG_DETACHED) == 0 ) |
---|
337 | xlist_unlink( XPTR( local_cxy , &thread->brothers_list ) ); |
---|
338 | |
---|
339 | // unlink thread from process |
---|
340 | process_remove_thread( thread ); |
---|
341 | |
---|
342 | // release memory for thread descriptor |
---|
343 | thread_destroy( thread ); |
---|
344 | |
---|
345 | count++; |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | // release lock protecting th_tbl[] |
---|
350 | spinlock_unlock( &process->th_lock ); |
---|
351 | |
---|
352 | // release memory allocated for process descriptor |
---|
353 | process_destroy( process ); |
---|
354 | } |
---|
355 | |
---|
356 | /////////////////////////////////////////////// |
---|
357 | process_t * process_get_local_copy( pid_t pid ) |
---|
358 | { |
---|
359 | error_t error; |
---|
360 | process_t * process_ptr; // local pointer on process |
---|
361 | xptr_t process_xp; // extended pointer on process |
---|
362 | |
---|
363 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
364 | |
---|
365 | // get lock protecting local list of processes |
---|
366 | remote_spinlock_lock( XPTR( local_cxy , &cluster->pmgr.local_lock ) ); |
---|
367 | |
---|
368 | // scan the local list of process descriptors to find the process |
---|
369 | xptr_t iter; |
---|
370 | bool_t found = false; |
---|
371 | XLIST_FOREACH( XPTR( local_cxy , &cluster->pmgr.local_root ) , iter ) |
---|
372 | { |
---|
373 | process_xp = XLIST_ELEMENT( iter , process_t , local_list ); |
---|
374 | process_ptr = (process_t *)GET_PTR( process_xp ); |
---|
375 | if( process_ptr->pid == pid ) |
---|
376 | { |
---|
377 | found = true; |
---|
378 | break; |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | // release lock protecting local list of processes |
---|
383 | remote_spinlock_unlock( XPTR( local_cxy , &cluster->pmgr.local_lock ) ); |
---|
384 | |
---|
385 | // allocate memory for a new local process descriptor |
---|
386 | // and initialise it from reference cluster if required |
---|
387 | if( !found ) |
---|
388 | { |
---|
389 | // get extended pointer on reference process descriptor |
---|
390 | xptr_t ref_xp = cluster_get_reference_process_from_pid( pid ); |
---|
391 | |
---|
392 | assert( (ref_xp != XPTR_NULL) , __FUNCTION__ , "illegal pid\n" ); |
---|
393 | |
---|
394 | // allocate memory for local process descriptor |
---|
395 | process_ptr = process_alloc(); |
---|
396 | if( process_ptr == NULL ) return NULL; |
---|
397 | |
---|
398 | // initialize local process descriptor copy |
---|
399 | error = process_copy_init( process_ptr , ref_xp ); |
---|
400 | if( error ) return NULL; |
---|
401 | } |
---|
402 | |
---|
403 | return process_ptr; |
---|
404 | } |
---|
405 | |
---|
406 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
407 | // File descriptor array related functions |
---|
408 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
409 | |
---|
410 | /////////////////////////////////////////// |
---|
411 | void process_fd_init( process_t * process ) |
---|
412 | { |
---|
413 | uint32_t fd; |
---|
414 | |
---|
415 | remote_spinlock_init( XPTR( local_cxy , &process->fd_array.lock ) ); |
---|
416 | |
---|
417 | process->fd_array.current = 0; |
---|
418 | |
---|
419 | // initialize array |
---|
420 | for ( fd = 0 ; fd < CONFIG_PROCESS_FILE_MAX_NR ; fd++ ) |
---|
421 | { |
---|
422 | process->fd_array.array[fd] = XPTR_NULL; |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | ////////////////////////////// |
---|
427 | bool_t process_fd_array_full() |
---|
428 | { |
---|
429 | // get extended pointer on reference process |
---|
430 | xptr_t ref_xp = CURRENT_THREAD->process->ref_xp; |
---|
431 | |
---|
432 | // get reference process cluster and local pointer |
---|
433 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
434 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
435 | |
---|
436 | // get number of open file descriptors from reference fd_array |
---|
437 | uint32_t current = hal_remote_lw( XPTR( ref_cxy , &ref_ptr->fd_array.current ) ); |
---|
438 | |
---|
439 | return ( current >= CONFIG_PROCESS_FILE_MAX_NR ); |
---|
440 | } |
---|
441 | |
---|
442 | ///////////////////////////////////////////////// |
---|
443 | error_t process_fd_register( xptr_t file_xp, |
---|
444 | uint32_t * file_id ) |
---|
445 | { |
---|
446 | bool_t found; |
---|
447 | uint32_t id; |
---|
448 | xptr_t xp; |
---|
449 | |
---|
450 | // get extended pointer on reference process |
---|
451 | xptr_t ref_xp = CURRENT_THREAD->process->ref_xp; |
---|
452 | |
---|
453 | // get reference process cluster and local pointer |
---|
454 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
455 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
456 | |
---|
457 | // take lock protecting reference fd_array |
---|
458 | remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->fd_array.lock ) ); |
---|
459 | |
---|
460 | found = false; |
---|
461 | |
---|
462 | for ( id = 0; id < CONFIG_PROCESS_FILE_MAX_NR ; id++ ) |
---|
463 | { |
---|
464 | xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->fd_array.array[id] ) ); |
---|
465 | if ( xp == XPTR_NULL ) |
---|
466 | { |
---|
467 | found = true; |
---|
468 | hal_remote_swd( XPTR( ref_cxy , &ref_ptr->fd_array.array[id] ) , file_xp ); |
---|
469 | hal_remote_atomic_add( XPTR( ref_cxy , &ref_ptr->fd_array.current ) , 1 ); |
---|
470 | *file_id = id; |
---|
471 | break; |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | // release lock protecting reference fd_array |
---|
476 | remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->fd_array.lock ) ); |
---|
477 | |
---|
478 | if ( !found ) return EMFILE; |
---|
479 | else return 0; |
---|
480 | } |
---|
481 | |
---|
482 | //////////////////////////////////////////////// |
---|
483 | xptr_t process_fd_get_xptr( process_t * process, |
---|
484 | uint32_t file_id ) |
---|
485 | { |
---|
486 | xptr_t file_xp; |
---|
487 | |
---|
488 | // access local copy of process descriptor |
---|
489 | file_xp = process->fd_array.array[file_id]; |
---|
490 | |
---|
491 | if( file_xp == XPTR_NULL ) |
---|
492 | { |
---|
493 | // get reference process cluster and local pointer |
---|
494 | xptr_t ref_xp = process->ref_xp; |
---|
495 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
496 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
497 | |
---|
498 | // access reference process descriptor |
---|
499 | file_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->fd_array.array[file_id] ) ); |
---|
500 | |
---|
501 | // update local fd_array if found |
---|
502 | if( file_xp != XPTR_NULL ) |
---|
503 | { |
---|
504 | process->fd_array.array[file_id] = file_xp; |
---|
505 | } |
---|
506 | } |
---|
507 | |
---|
508 | return file_xp; |
---|
509 | } |
---|
510 | |
---|
511 | /////////////////////////////////////////// |
---|
512 | void process_fd_remote_copy( xptr_t dst_xp, |
---|
513 | xptr_t src_xp ) |
---|
514 | { |
---|
515 | uint32_t fd; |
---|
516 | xptr_t entry; |
---|
517 | |
---|
518 | // get cluster and local pointer for src fd_array |
---|
519 | cxy_t src_cxy = GET_CXY( src_xp ); |
---|
520 | fd_array_t * src_ptr = (fd_array_t *)GET_PTR( src_xp ); |
---|
521 | |
---|
522 | // get cluster and local pointer for dst fd_array |
---|
523 | cxy_t dst_cxy = GET_CXY( dst_xp ); |
---|
524 | fd_array_t * dst_ptr = (fd_array_t *)GET_PTR( dst_xp ); |
---|
525 | |
---|
526 | // get the remote lock protecting the src fd_array |
---|
527 | remote_spinlock_lock( XPTR( src_cxy , &src_ptr->lock ) ); |
---|
528 | |
---|
529 | // loop on all entries in source process fd_array |
---|
530 | for( fd = 0 ; fd < CONFIG_PROCESS_FILE_MAX_NR ; fd++ ) |
---|
531 | { |
---|
532 | entry = (xptr_t)hal_remote_lwd( XPTR( src_cxy , &src_ptr->array[fd] ) ); |
---|
533 | |
---|
534 | if( entry != XPTR_NULL ) |
---|
535 | { |
---|
536 | // increment file descriptor ref count |
---|
537 | vfs_file_count_up( entry ); |
---|
538 | |
---|
539 | // copy entry in destination process fd_array |
---|
540 | hal_remote_swd( XPTR( dst_cxy , &dst_ptr->array[fd] ) , entry ); |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | // release lock on source process fd_array |
---|
545 | remote_spinlock_unlock( XPTR( src_cxy , &src_ptr->lock ) ); |
---|
546 | } |
---|
547 | |
---|
548 | //////////////////////////////////////////////////////////////////////////////////// |
---|
549 | // Thread related functions |
---|
550 | //////////////////////////////////////////////////////////////////////////////////// |
---|
551 | |
---|
552 | ///////////////////////////////////////////////////// |
---|
553 | error_t process_register_thread( process_t * process, |
---|
554 | thread_t * thread, |
---|
555 | trdid_t * trdid ) |
---|
556 | { |
---|
557 | ltid_t ltid; |
---|
558 | bool_t found; |
---|
559 | |
---|
560 | assert( (process != NULL) , __FUNCTION__ , "process argument is NULL" ); |
---|
561 | |
---|
562 | assert( (thread != NULL) , __FUNCTION__ , "thread argument is NULL" ); |
---|
563 | |
---|
564 | // search a free slot in th_tbl[] |
---|
565 | found = false; |
---|
566 | for( ltid = 0 ; ltid < CONFIG_THREAD_MAX_PER_CLUSTER ; ltid++ ) |
---|
567 | { |
---|
568 | if( process->th_tbl[ltid] == NULL ) |
---|
569 | { |
---|
570 | found = true; |
---|
571 | break; |
---|
572 | } |
---|
573 | } |
---|
574 | |
---|
575 | if( found ) |
---|
576 | { |
---|
577 | // register thread in th_tbl[] |
---|
578 | process->th_tbl[ltid] = thread; |
---|
579 | process->th_nr++; |
---|
580 | |
---|
581 | // returns trdid |
---|
582 | *trdid = TRDID( local_cxy , ltid ); |
---|
583 | } |
---|
584 | |
---|
585 | return (found) ? 0 : ENOMEM; |
---|
586 | |
---|
587 | } // end process_register_thread() |
---|
588 | |
---|
589 | /////////////////////////////////////////////// |
---|
590 | void process_remove_thread( thread_t * thread ) |
---|
591 | { |
---|
592 | if( thread == NULL ) |
---|
593 | { |
---|
594 | printk("\n[PANIC] in %s : thread argument is NULL\n", __FUNCTION__ ); |
---|
595 | hal_core_sleep(); |
---|
596 | } |
---|
597 | |
---|
598 | process_t * process = thread->process; |
---|
599 | |
---|
600 | // get thread local index |
---|
601 | ltid_t ltid = LTID_FROM_TRDID( thread->trdid ); |
---|
602 | |
---|
603 | // remove thread from th_tbl[] |
---|
604 | process->th_tbl[ltid] = NULL; |
---|
605 | process->th_nr--; |
---|
606 | |
---|
607 | } // process_remove_thread() |
---|
608 | |
---|
609 | ///////////////////////////////////////////////////// |
---|
610 | error_t process_make_exec( exec_info_t * exec_info ) |
---|
611 | { |
---|
612 | char * path; // pathname to .elf file |
---|
613 | process_t * process; // local pointer on new process |
---|
614 | pid_t pid; // new process pid |
---|
615 | xptr_t parent_xp; // extended pointer on parent process |
---|
616 | cxy_t parent_cxy; |
---|
617 | process_t * parent_ptr; |
---|
618 | uint32_t parent_pid; |
---|
619 | thread_t * thread; // pointer on new thread |
---|
620 | pthread_attr_t attr; // main thread attributes |
---|
621 | core_t * core; // pointer on selected core |
---|
622 | lid_t lid; // selected core local index |
---|
623 | error_t error; |
---|
624 | |
---|
625 | // get parent and .elf pathname from exec_info |
---|
626 | path = exec_info->path; |
---|
627 | parent_xp = exec_info->parent_xp; |
---|
628 | |
---|
629 | // get parent process cluster and local pointer |
---|
630 | parent_cxy = GET_CXY( parent_xp ); |
---|
631 | parent_ptr = (process_t *)GET_PTR( parent_xp ); |
---|
632 | parent_pid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) ); |
---|
633 | |
---|
634 | exec_dmsg("\n[INFO] %s : enters in cluster %x for path = %s\n", |
---|
635 | __FUNCTION__ , local_cxy , path ); |
---|
636 | |
---|
637 | // create new process descriptor |
---|
638 | process = process_alloc(); |
---|
639 | |
---|
640 | if( process == NULL ) |
---|
641 | { |
---|
642 | printk("\n[ERROR] in %s : no memory / cluster = %x / ppid = %x / path = %s\n", |
---|
643 | __FUNCTION__ , local_cxy , parent_pid , path ); |
---|
644 | return ENOMEM; |
---|
645 | } |
---|
646 | |
---|
647 | // get a pid from the local cluster |
---|
648 | error = cluster_pid_alloc( XPTR( local_cxy , process ) , &pid ); |
---|
649 | |
---|
650 | if( error ) |
---|
651 | { |
---|
652 | printk("\n[ERROR] in %s : cannot get PID / cluster = %x / ppid = %x / path = %s\n", |
---|
653 | __FUNCTION__ , local_cxy , parent_pid , path ); |
---|
654 | process_free( process ); |
---|
655 | return ENOMEM; |
---|
656 | } |
---|
657 | |
---|
658 | // initialize the process descriptor as the reference |
---|
659 | process_reference_init( process , pid , parent_xp ); |
---|
660 | |
---|
661 | exec_dmsg("\n[INFO] %s : created process %x in cluster %x / path = %s\n", |
---|
662 | __FUNCTION__, pid , local_cxy , path ); |
---|
663 | |
---|
664 | // initialize vfs_root and vfs_cwd from parent process |
---|
665 | xptr_t vfs_root_xp = hal_remote_lwd( XPTR( parent_cxy , &parent_ptr->vfs_root_xp ) ); |
---|
666 | vfs_file_count_up( vfs_root_xp ); |
---|
667 | process->vfs_root_xp = vfs_root_xp; |
---|
668 | |
---|
669 | xptr_t vfs_cwd_xp = hal_remote_lwd( XPTR( parent_cxy , &parent_ptr->vfs_cwd_xp ) ); |
---|
670 | vfs_file_count_up( vfs_cwd_xp ); |
---|
671 | process->vfs_cwd_xp = vfs_cwd_xp; |
---|
672 | |
---|
673 | // initialize embedded fd_array from parent process |
---|
674 | process_fd_remote_copy( XPTR( local_cxy , &process->fd_array ), |
---|
675 | XPTR( parent_cxy , &parent_ptr->fd_array) ); |
---|
676 | |
---|
677 | exec_dmsg("\n[INFO] %s : fd_array copied from process %x to process %x\n", |
---|
678 | __FUNCTION__, parent_pid , pid ); |
---|
679 | |
---|
680 | // initialize signal manager TODO ??? [AG] |
---|
681 | // signal_manager_init( process ); |
---|
682 | |
---|
683 | // register "code" and "data" vsegs as well as the process entry-point in VMM, |
---|
684 | // using information contained in the elf file. |
---|
685 | error = elf_load_process( path , process ); |
---|
686 | |
---|
687 | if( error ) |
---|
688 | { |
---|
689 | printk("\n[ERROR] in %s : failed to access elf file for process %x / path = %s\n", |
---|
690 | __FUNCTION__, pid , path ); |
---|
691 | process_destroy( process ); |
---|
692 | return error; |
---|
693 | } |
---|
694 | |
---|
695 | exec_dmsg("\n[INFO] %s : code and data vsegs from <%s> registered for process %x\n", |
---|
696 | __FUNCTION__ , path , pid ); |
---|
697 | |
---|
698 | // select a core in cluster |
---|
699 | lid = cluster_select_local_core(); |
---|
700 | core = &LOCAL_CLUSTER->core_tbl[lid]; |
---|
701 | |
---|
702 | // initialize pthread attributes for main thread |
---|
703 | attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED; |
---|
704 | attr.cxy = local_cxy; |
---|
705 | attr.lid = lid; |
---|
706 | |
---|
707 | // create and initialize thread descriptor |
---|
708 | error = thread_user_create( pid, |
---|
709 | (void *)process->vmm.entry_point, |
---|
710 | exec_info->args_pointers, |
---|
711 | &attr, |
---|
712 | &thread ); |
---|
713 | if( error ) |
---|
714 | { |
---|
715 | printk("\n[ERROR] in %s : cannot create thread for process %x / path = %s\n", |
---|
716 | __FUNCTION__, pid ); |
---|
717 | process_destroy( process ); |
---|
718 | return error; |
---|
719 | } |
---|
720 | |
---|
721 | exec_dmsg("\n[INFO] %s : thread created for process %x on core %d in cluster %x\n", |
---|
722 | __FUNCTION__ , pid , core->lid , local_cxy ); |
---|
723 | |
---|
724 | // update children list in parent process |
---|
725 | xlist_add_last( XPTR( parent_cxy , &parent_ptr->children_root ), |
---|
726 | XPTR( local_cxy , &process->brothers_list ) ); |
---|
727 | hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr) , 1 ); |
---|
728 | |
---|
729 | // activate new thread |
---|
730 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
731 | |
---|
732 | exec_dmsg("\n[INFO] %s : exit for process %x\n", |
---|
733 | __FUNCTION__, process->pid ); |
---|
734 | |
---|
735 | return 0; |
---|
736 | |
---|
737 | } // end proces_make_exec() |
---|
738 | |
---|
739 | ////////////////////////// |
---|
740 | void process_init_create() |
---|
741 | { |
---|
742 | exec_info_t exec_info; // structure to be passed to process_make_exec() |
---|
743 | |
---|
744 | error_t error1; |
---|
745 | error_t error2; |
---|
746 | error_t error3; |
---|
747 | xptr_t stdin_xp; |
---|
748 | xptr_t stdout_xp; |
---|
749 | xptr_t stderr_xp; |
---|
750 | uint32_t stdin_id; |
---|
751 | uint32_t stdout_id; |
---|
752 | uint32_t stderr_id; |
---|
753 | |
---|
754 | process_dmsg("\n[INFO] %s : enters in cluster %x\n", __FUNCTION__ , local_cxy ); |
---|
755 | |
---|
756 | // open stdin / stdout / stderr pseudo-files |
---|
757 | error1 = vfs_open( XPTR_NULL, CONFIG_DEV_STDIN , O_RDONLY, 0, &stdin_xp , &stdin_id ); |
---|
758 | error2 = vfs_open( XPTR_NULL, CONFIG_DEV_STDOUT, O_WRONLY, 0, &stdout_xp, &stdout_id ); |
---|
759 | error3 = vfs_open( XPTR_NULL, CONFIG_DEV_STDERR, O_WRONLY, 0, &stderr_xp, &stderr_id ); |
---|
760 | |
---|
761 | assert( ((error1 == 0) && (error2 == 0) && (error3 == 0)) , __FUNCTION__ , |
---|
762 | "cannot open stdin/stdout/stderr pseudo files\n"); |
---|
763 | |
---|
764 | assert( ((stdin_id == 0) && (stdout_id == 1) && (stderr_id == 2)) , __FUNCTION__ , |
---|
765 | "bad indexes for stdin/stdout/stderr\n"); |
---|
766 | |
---|
767 | // initialize the exec_info structure |
---|
768 | exec_info.parent_xp = XPTR( local_cxy , &process_zero ); |
---|
769 | strcpy( exec_info.path , CONFIG_PROCESS_INIT_PATH ); |
---|
770 | exec_info.args_nr = 0; |
---|
771 | exec_info.envs_nr = 0; |
---|
772 | |
---|
773 | // create process_init and thread_init |
---|
774 | error1 = process_make_exec( &exec_info ); |
---|
775 | |
---|
776 | assert( (error1 == 0) , __FUNCTION__ , "cannot create process_init\n"); |
---|
777 | |
---|
778 | process_dmsg("\n[INFO] %s : exit in cluster %x\n", __FUNCTION__ , local_cxy ); |
---|
779 | |
---|
780 | hal_fence(); |
---|
781 | |
---|
782 | } // end process_init_create() |
---|
783 | |
---|