Changeset 407 for trunk/kernel/kern/process.c
- Timestamp:
- Nov 7, 2017, 3:08:12 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/kern/process.c
r406 r407 39 39 #include <thread.h> 40 40 #include <list.h> 41 #include <string.h> 41 42 #include <scheduler.h> 42 43 #include <remote_spinlock.h> … … 90 91 pid_t parent_pid; 91 92 92 process_dmsg("\n[DMSG] %s : core[%x,%d] enters for process %x\n", 93 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , pid ); 93 error_t error1; 94 error_t error2; 95 error_t error3; 96 xptr_t stdin_xp; 97 xptr_t stdout_xp; 98 xptr_t stderr_xp; 99 uint32_t stdin_id; 100 uint32_t stdout_id; 101 uint32_t stderr_id; 102 103 process_dmsg("\n[DBG] %s : core[%x,%d] enters for process %x\n", 104 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , pid ); 94 105 95 106 // get parent process cluster, local pointer, and pid 96 // for all processes other than process_zero97 if( process == &process_zero ) 107 // for all processes other than kernel process 108 if( process == &process_zero ) // kernel process 98 109 { 99 110 assert( (pid == 0) , __FUNCTION__ , "process_zero must have PID = 0\n"); … … 101 112 parent_cxy = 0; 102 113 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 114 parent_pid = 0; 115 } 116 else // user process 117 { 109 118 parent_cxy = GET_CXY( parent_xp ); 110 119 parent_ptr = (process_t *)GET_PTR( parent_xp ); … … 112 121 } 113 122 114 // initialize PID and PPID 115 process->pid = pid; 116 process->ppid = parent_pid; 117 118 // initialize 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) 123 // initialize PID, PPID, and REF 124 process->pid = pid; 125 process->ppid = parent_pid; 126 process->ref_xp = XPTR( local_cxy , process ); 127 128 // initialize vmm, fd array and others structures for user processes. 129 // These structures are not used by the kernel process. 142 130 if( pid ) 143 131 { 132 // initialize vmm (not for kernel) 133 vmm_init( process ); 134 135 process_dmsg("\n[DBG] %s : core[%x,%d] / vmm initialised for process %x\n", 136 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , pid ); 137 138 // initialize fd_array (not for kernel) 139 process_fd_init( process ); 140 141 // create stdin / stdout / stderr pseudo-files (not for kernel) 142 if( parent_pid == 0 ) // process_init 143 { 144 error1 = vfs_open( process, 145 CONFIG_INIT_STDIN, 146 O_RDONLY, 147 0, // FIXME chmod 148 &stdin_xp, 149 &stdin_id ); 150 151 error2 = vfs_open( process, 152 CONFIG_INIT_STDOUT, 153 O_WRONLY, 154 0, // FIXME chmod 155 &stdout_xp, 156 &stdout_id ); 157 158 error3 = vfs_open( process, 159 CONFIG_INIT_STDERR, 160 O_WRONLY, 161 0, // FIXME chmod 162 &stderr_xp, 163 &stderr_id ); 164 } 165 else // user process 166 { 167 error1 = vfs_open( process, 168 CONFIG_USER_STDIN, 169 O_RDONLY, 170 0, // FIXME chmod 171 &stdin_xp, 172 &stdin_id ); 173 174 error2 = vfs_open( process, 175 CONFIG_USER_STDOUT, 176 O_WRONLY, 177 0, // FIXME chmod 178 &stdout_xp, 179 &stdout_id ); 180 181 error3 = vfs_open( process, 182 CONFIG_USER_STDERR, 183 O_WRONLY, 184 0, // FIXME chmod 185 &stderr_xp, 186 &stderr_id ); 187 } 188 189 assert( ((error1 == 0) && (error2 == 0) && (error3 == 0)) , __FUNCTION__ , 190 "cannot open stdin/stdout/stderr pseudo files\n"); 191 192 assert( ((stdin_id == 0) && (stdout_id == 1) && (stderr_id == 2)) , __FUNCTION__ , 193 "bad indexes : stdin %d / stdout %d / stderr %d \n", stdin_id , stdout_id , stderr_id ); 194 195 process_dmsg("\n[DBG] %s : core[%x,%d] / fd array initialised for process %x\n", 196 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , pid ); 197 198 199 // reset reference process files structures and cwd_lock (not for kernel) 200 process->vfs_root_xp = XPTR_NULL; 201 process->vfs_bin_xp = XPTR_NULL; 202 process->vfs_cwd_xp = XPTR_NULL; 203 remote_rwlock_init( XPTR( local_cxy , &process->cwd_lock ) ); 204 205 // reset children list root (not for kernel) 206 xlist_root_init( XPTR( local_cxy , &process->children_root ) ); 207 process->children_nr = 0; 208 209 // reset semaphore / mutex / barrier / condvar list roots (nor for kernel) 210 xlist_root_init( XPTR( local_cxy , &process->sem_root ) ); 211 xlist_root_init( XPTR( local_cxy , &process->mutex_root ) ); 212 xlist_root_init( XPTR( local_cxy , &process->barrier_root ) ); 213 xlist_root_init( XPTR( local_cxy , &process->condvar_root ) ); 214 remote_spinlock_init( XPTR( local_cxy , &process->sync_lock ) ); 215 216 // register new process in the parent children list (nor for kernel) 144 217 xptr_t entry = XPTR( local_cxy , &process->brothers_list ); 145 218 xptr_t root = XPTR( parent_cxy , &parent_ptr->children_root ); … … 156 229 spinlock_init( &process->th_lock ); 157 230 158 // set ref_xp field159 process->ref_xp = XPTR( local_cxy , process );160 161 231 // register new process descriptor in local cluster manager local_list 162 232 cluster_process_local_link( process ); … … 169 239 hal_fence(); 170 240 171 process_dmsg("\n[DMSG] %s : exit for process %x in cluster%x\n",172 __FUNCTION__, pid );241 process_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x\n", 242 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , pid ); 173 243 174 244 } // process_reference init() … … 181 251 cxy_t ref_cxy = GET_CXY( reference_process_xp ); 182 252 process_t * ref_ptr = (process_t *)GET_PTR( reference_process_xp ); 253 254 // set the pid, ppid, ref_xp fields in local process 255 local_process->pid = hal_remote_lw( XPTR( ref_cxy , &ref_ptr->pid ) ); 256 local_process->ppid = hal_remote_lw( XPTR( ref_cxy , &ref_ptr->ppid ) ); 257 local_process->ref_xp = reference_process_xp; 258 259 process_dmsg("\n[DBG] %s : core[%x,%d] enters for process %x in cluster %x\n", 260 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , local_process->pid ); 183 261 184 262 // reset local process vmm … … 192 270 local_process->vfs_bin_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->vfs_bin_xp ) ); 193 271 local_process->vfs_cwd_xp = XPTR_NULL; 194 195 // set the pid, ppid, ref_xp fields196 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[DMSG] %s : enter for process %x in cluster %x\n",201 __FUNCTION__ , local_process->pid );202 272 203 273 // reset children list root (not used in a process descriptor copy) … … 233 303 hal_fence(); 234 304 235 process_dmsg("\n[DMSG] %s :exit for process %x in cluster %x\n",236 __FUNCTION__, local_process->pid );305 process_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x in cluster %x\n", 306 __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , local_process->pid ); 237 307 238 308 return 0; … … 288 358 vmm_destroy( process ); 289 359 290 process_dmsg("\n[D MSG] %s for pid %d / page_faults = %d\n",360 process_dmsg("\n[DBG] %s for pid %d / page_faults = %d\n", 291 361 __FUNCTION__ , process->pid, process->vmm.pgfault_nr ); 292 } 362 363 } // end process_destroy() 293 364 294 365 //////////////////////////////////////// … … 298 369 uint32_t ltid; // index in process th_tbl 299 370 uint32_t count; // thread counter 371 372 printk("\n@@@ %s enter\n", __FUNCTION__ ); 300 373 301 374 // get lock protecting th_tbl[] … … 317 390 } 318 391 319 volatile uint32_t ko;392 printk("\n@@@ %s : %d signal(s) sent\n", __FUNCTION__, count ); 320 393 321 394 // second loop on threads to wait acknowledge from scheduler, … … 329 402 if( thread != NULL ) 330 403 { 331 // wait scheduler acknowledge 332 do { ko = (thread->signals & THREAD_SIG_KILL); } while( ko ); 333 334 // unlink thread from brothers list if required 335 if( (thread->flags & THREAD_FLAG_DETACHED) == 0 ) 336 xlist_unlink( XPTR( local_cxy , &thread->brothers_list ) ); 404 405 printk("\n@@@ %s start polling at cycle %d\n", __FUNCTION__ , hal_time_stamp() ); 406 407 // poll the THREAD_SIG_KILL bit until reset 408 while( thread->signals & THREAD_SIG_KILL ) asm volatile( "nop" ); 409 410 printk("\n@@@ %s exit polling\n", __FUNCTION__ ); 411 412 // detach target thread from parent if attached 413 if( (thread->flags & THREAD_FLAG_DETACHED) != 0 ) 414 thread_child_parent_unlink( thread->parent , XPTR( local_cxy , thread ) ); 337 415 338 416 // unlink thread from process … … 346 424 } 347 425 426 printk("\n@@@ %s : %d ack(s) received\n", __FUNCTION__, count ); 427 348 428 // release lock protecting th_tbl[] 349 429 spinlock_unlock( &process->th_lock ); … … 351 431 // release memory allocated for process descriptor 352 432 process_destroy( process ); 353 } 433 434 printk("\n[@@@] %s : core[%x,%d] exit\n", 435 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid ); 436 437 } // end process_kill() 354 438 355 439 /////////////////////////////////////////////// … … 440 524 441 525 ///////////////////////////////////////////////// 442 error_t process_fd_register( xptr_t file_xp, 443 uint32_t * file_id ) 526 error_t process_fd_register( process_t * process, 527 xptr_t file_xp, 528 uint32_t * fdid ) 444 529 { 445 530 bool_t found; … … 447 532 xptr_t xp; 448 533 449 // get extended pointer on reference process450 xptr_t ref_xp = CURRENT_THREAD->process->ref_xp;451 452 534 // get reference process cluster and local pointer 535 xptr_t ref_xp = process->ref_xp; 453 536 process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); 454 537 cxy_t ref_cxy = GET_CXY( ref_xp ); … … 467 550 hal_remote_swd( XPTR( ref_cxy , &ref_ptr->fd_array.array[id] ) , file_xp ); 468 551 hal_remote_atomic_add( XPTR( ref_cxy , &ref_ptr->fd_array.current ) , 1 ); 469 *f ile_id = id;552 *fdid = id; 470 553 break; 471 554 } … … 481 564 //////////////////////////////////////////////// 482 565 xptr_t process_fd_get_xptr( process_t * process, 483 uint32_t f ile_id )566 uint32_t fdid ) 484 567 { 485 568 xptr_t file_xp; 486 569 487 570 // access local copy of process descriptor 488 file_xp = process->fd_array.array[f ile_id];571 file_xp = process->fd_array.array[fdid]; 489 572 490 573 if( file_xp == XPTR_NULL ) … … 496 579 497 580 // access reference process descriptor 498 file_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->fd_array.array[f ile_id] ) );581 file_xp = hal_remote_lwd( XPTR( ref_cxy , &ref_ptr->fd_array.array[fdid] ) ); 499 582 500 583 // update local fd_array if found 501 584 if( file_xp != XPTR_NULL ) 502 585 { 503 process->fd_array.array[f ile_id] = file_xp;586 process->fd_array.array[fdid] = file_xp; 504 587 } 505 588 } 506 589 507 590 return file_xp; 508 } 591 592 } // end process_fd_get_xptr() 509 593 510 594 /////////////////////////////////////////// … … 543 627 // release lock on source process fd_array 544 628 remote_spinlock_unlock( XPTR( src_cxy , &src_ptr->lock ) ); 545 } 629 630 } // end process_fd_remote_copy() 546 631 547 632 //////////////////////////////////////////////////////////////////////////////////// … … 561 646 assert( (thread != NULL) , __FUNCTION__ , "thread argument is NULL" ); 562 647 563 // search a free slot in th_tbl[] 648 // search a free slot in th_tbl[] 649 // 0 is not a valid ltid value 564 650 found = false; 565 for( ltid = 0; ltid < CONFIG_THREAD_MAX_PER_CLUSTER ; ltid++ )651 for( ltid = 1 ; ltid < CONFIG_THREAD_MAX_PER_CLUSTER ; ltid++ ) 566 652 { 567 653 if( process->th_tbl[ltid] == NULL ) … … 606 692 { 607 693 char * path; // pathname to .elf file 694 bool_t keep_pid; // new process keep parent PID if true 608 695 process_t * process; // local pointer on new process 609 696 pid_t pid; // new process pid 610 697 xptr_t parent_xp; // extended pointer on parent process 611 cxy_t parent_cxy; 612 process_t * parent_ptr; 613 uint32_t parent_pid; 698 cxy_t parent_cxy; // parent process local cluster 699 process_t * parent_ptr; // local pointer on parent process 700 uint32_t parent_pid; // parent process identifier 614 701 thread_t * thread; // pointer on new thread 615 702 pthread_attr_t attr; // main thread attributes … … 618 705 error_t error; 619 706 620 // get parent and .elf pathnamefrom exec_info707 // get .elf pathname, parent_xp, and keep_pid flag from exec_info 621 708 path = exec_info->path; 622 709 parent_xp = exec_info->parent_xp; 710 keep_pid = exec_info->keep_pid; 711 712 process_dmsg("\n[DBG] %s : core[%x,%d] enters for path = %s\n", 713 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, path ); 623 714 624 715 // get parent process cluster and local pointer … … 627 718 parent_pid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) ); 628 719 629 exec_dmsg("\n[DMSG] %s : thread %x on core[%x,%d] enters for path = %s\n", 630 __FUNCTION__, CURRENT_THREAD->trdid, local_cxy, CURRENT_THREAD->core->lid , path ); 631 632 // create new process descriptor 633 process = process_alloc(); 634 635 if( process == NULL ) 636 { 637 printk("\n[ERROR] in %s : no memory / cluster = %x / ppid = %x / path = %s\n", 638 __FUNCTION__ , local_cxy , parent_pid , path ); 639 return ENOMEM; 640 } 641 642 // get a pid from the local cluster 643 error = cluster_pid_alloc( XPTR( local_cxy , process ) , &pid ); 644 645 if( error ) 646 { 647 printk("\n[ERROR] in %s : cannot get PID / cluster = %x / ppid = %x / path = %s\n", 648 __FUNCTION__ , local_cxy , parent_pid , path ); 649 process_free( process ); 650 return ENOMEM; 651 } 720 // allocates memory for process descriptor 721 process = process_alloc(); 722 if( process == NULL ) return -1; 723 724 // get PID 725 if( keep_pid ) // keep parent PID 726 { 727 pid = parent_pid; 728 } 729 else // get new PID from local cluster 730 { 731 error = cluster_pid_alloc( XPTR( local_cxy , process ) , &pid ); 732 if( error ) return -1; 733 } 734 735 process_dmsg("\n[DBG] %s : core[%x,%d] created process %x for path = %s\n", 736 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, pid, path ); 652 737 653 738 // initialize the process descriptor as the reference 654 739 process_reference_init( process , pid , parent_xp ); 655 740 656 exec_dmsg("\n[DMSG] %s : thread %x on core[%x,%d] created process %x / path = %s\n",657 __FUNCTION__, CURRENT_THREAD->trdid, local_cxy, CURRENT_THREAD->core->lid, pid, path );741 process_dmsg("\n[DBG] %s : core[%x,%d] initialized process %x / path = %s\n", 742 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, pid, path ); 658 743 659 744 // initialize vfs_root and vfs_cwd from parent process … … 670 755 XPTR( parent_cxy , &parent_ptr->fd_array) ); 671 756 672 exec_dmsg("\n[DMSG] %s : fd_array copied from process %x to process %x\n", 673 __FUNCTION__, parent_pid , pid ); 674 675 // initialize signal manager TODO ??? [AG] 676 // signal_manager_init( process ); 757 process_dmsg("\n[DBG] %s : core[%x,%d] copied fd_array for process %x\n", 758 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, pid ); 677 759 678 760 // register "code" and "data" vsegs as well as the process entry-point in VMM, … … 682 764 if( error ) 683 765 { 684 printk("\n[ERROR] in %s : failed to access elf file for process %x / path = %s\n",766 printk("\n[ERROR] in %s : failed to access .elf file for process %x / path = %s\n", 685 767 __FUNCTION__, pid , path ); 686 768 process_destroy( process ); … … 688 770 } 689 771 690 exec_dmsg("\n[DMSG] %s : code and data vsegs registeredfor process %x / path = %s\n",691 __FUNCTION__ , pid, path );772 process_dmsg("\n[DBG] %s : core[%x,%d] registered code/data vsegs for process %x / path = %s\n", 773 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, pid, path ); 692 774 693 775 // select a core in cluster … … 709 791 { 710 792 printk("\n[ERROR] in %s : cannot create thread for process %x / path = %s\n", 711 __FUNCTION__, pid );793 __FUNCTION__, pid , path ); 712 794 process_destroy( process ); 713 795 return error; 714 796 } 715 797 716 exec_dmsg("\n[DMSG] %s : thread created for process %x on core %d in cluster %x\n", 717 __FUNCTION__ , pid , core->lid , local_cxy ); 718 719 #if CONFIG_EXEC_DEBUG 720 if( hal_time_stamp() > CONFIG_EXEC_DEBUG ) 721 { 722 grdxt_print( &process->vmm.grdxt , GRDXT_TYPE_VSEG , process->pid ); 723 hal_gpt_print( &process->vmm.gpt , process->pid ); 724 } 725 #endif 798 process_dmsg("\n[DBG] %s : core[%x,%d] created thread %x for process %x / path = %s\n", 799 __FUNCTION__ , local_cxy, CURRENT_THREAD->core->lid, thread->trdid, pid, path ); 726 800 727 801 // update children list in parent process … … 733 807 thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_GLOBAL ); 734 808 735 exec_dmsg("\n[DMSG] %s : exit for process %x\n",736 __FUNCTION__, process->pid);809 process_dmsg("\n[DBG] %s : core[%x,%d] exit for path = %s\n", 810 __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, path ); 737 811 738 812 return 0; 739 813 740 } // end proces _make_exec()814 } // end process_make_exec() 741 815 742 816 ////////////////////////// … … 744 818 { 745 819 exec_info_t exec_info; // structure to be passed to process_make_exec() 746 747 error_t error1; 748 error_t error2; 749 error_t error3; 750 xptr_t stdin_xp; 751 xptr_t stdout_xp; 752 xptr_t stderr_xp; 753 uint32_t stdin_id; 754 uint32_t stdout_id; 755 uint32_t stderr_id; 756 757 process_dmsg("\n[DMSG] %s : enters in cluster %x\n", __FUNCTION__ , local_cxy ); 758 759 // open stdin / stdout / stderr pseudo-files 760 error1 = vfs_open( XPTR_NULL, CONFIG_DEV_STDIN , O_RDONLY, 0, &stdin_xp , &stdin_id ); 761 error2 = vfs_open( XPTR_NULL, CONFIG_DEV_STDOUT, O_WRONLY, 0, &stdout_xp, &stdout_id ); 762 error3 = vfs_open( XPTR_NULL, CONFIG_DEV_STDERR, O_WRONLY, 0, &stderr_xp, &stderr_id ); 763 764 assert( ((error1 == 0) && (error2 == 0) && (error3 == 0)) , __FUNCTION__ , 765 "cannot open stdin/stdout/stderr pseudo files\n"); 766 767 assert( ((stdin_id == 0) && (stdout_id == 1) && (stderr_id == 2)) , __FUNCTION__ , 768 "bad indexes for stdin/stdout/stderr\n"); 820 xptr_t parent_xp; // extended pointer on parent process. 821 error_t error; 822 823 process_dmsg("\n[DBG] %s : enters in cluster %x\n", 824 __FUNCTION__ , local_cxy ); 825 826 // parent process is local kernel process 827 parent_xp = XPTR( local_cxy , &process_zero ); 769 828 770 829 // initialize the exec_info structure 771 exec_info.parent_xp = XPTR( local_cxy , &process_zero ); 830 exec_info.keep_pid = false; 831 exec_info.parent_xp = parent_xp; 772 832 strcpy( exec_info.path , CONFIG_PROCESS_INIT_PATH ); 773 833 exec_info.args_nr = 0; 774 834 exec_info.envs_nr = 0; 775 835 776 // create process_init and thread_init 777 error1 = process_make_exec( &exec_info ); 778 779 assert( (error1 == 0) , __FUNCTION__ , "cannot create process_init\n"); 780 781 process_dmsg("\n[DMSG] %s : exit in cluster %x\n", __FUNCTION__ , local_cxy ); 836 // initialize process_init and create thread_init 837 error = process_make_exec( &exec_info ); 838 839 if( error ) panic("cannot initialize process_init in cluster %x", local_cxy ); 840 841 process_dmsg("\n[DBG] %s : exit in cluster %x\n", 842 __FUNCTION__ , local_cxy ); 782 843 783 844 hal_fence();
Note: See TracChangeset
for help on using the changeset viewer.