Changeset 101 for trunk/kernel/kern/process.c
- Timestamp:
- Jun 29, 2017, 4:44:52 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/kern/process.c
r23 r101 81 81 } 82 82 83 ////////////////////////////////////////////84 void process_zero_init( boot_info_t * info )85 {86 // reset process descriptor87 memset( &process_zero , 0 , sizeof(process_t) );88 89 // initialize kernel code & data vsegs base addresses90 process_zero.vmm.code_vpn_base = (vpn_t)(info->kernel_code_start >> CONFIG_PPM_PAGE_SHIFT);91 process_zero.vmm.data_vpn_base = (vpn_t)(info->kernel_data_start >> CONFIG_PPM_PAGE_SHIFT);92 93 // reset threads and childs number94 process_zero.th_nr = 0;95 process_zero.children_nr = 0;96 97 // set PID98 process_zero.pid = 0;99 }100 101 83 ///////////////////////////////////////////////// 102 84 void process_reference_init( process_t * process, 103 85 pid_t pid, 104 pid_t ppid ) 105 { 106 // reset reference process vmm 107 vmm_init( process ); 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 / parent_xp = %l\n", 93 __FUNCTION__ , pid , parent_xp ); 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_pid = 0; // process_zero is its own parent... 102 } 103 else 104 { 105 assert( (parent_xp != XPTR_NULL) , __FUNCTION__ , "parent_xp cannot be NULL\n"); 106 107 parent_cxy = GET_CXY( parent_xp ); 108 parent_ptr = (process_t *)GET_PTR( parent_xp ); 109 parent_pid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) ); 110 } 111 112 // reset reference process vmm (not for kernel process) 113 if( pid ) vmm_init( process ); 108 114 109 115 // reset reference process file descriptors array 110 116 process_fd_init( process ); 111 117 112 // reset reference process files structures and c d_lock118 // reset reference process files structures and cwd_lock 113 119 process->vfs_root_xp = XPTR_NULL; 114 120 process->vfs_bin_xp = XPTR_NULL; … … 127 133 remote_spinlock_init( XPTR( local_cxy , &process->sync_lock ) ); 128 134 129 // register new process in the parent children list 130 xptr_t entry = XPTR( local_cxy , &process->brothers_list ); 131 xptr_t root = XPTR( local_cxy , &process->children_root ); 132 xlist_add_first( root , entry ); 135 // register new process in the parent children list (not for kernel process) 136 if( pid ) 137 { 138 xptr_t entry = XPTR( local_cxy , &process->brothers_list ); 139 xptr_t root = XPTR( parent_cxy , &parent_ptr->children_root ); 140 xlist_add_first( root , entry ); 141 } 133 142 134 143 // reset th_tbl[] array as empty … … 143 152 // initialize PID and PPID 144 153 process->pid = pid; 145 process->ppid = p pid;154 process->ppid = parent_pid; 146 155 147 156 // set ref_xp field … … 157 166 158 167 hal_wbflush(); 168 169 process_dmsg("\n[INFO] %s : exit for process %x in cluster %x\n", 170 __FUNCTION__ , pid ); 159 171 160 172 } // end process_reference_init() … … 576 588 577 589 return (found) ? 0 : ENOMEM; 578 } 590 591 } // process_register_thread() 579 592 580 593 /////////////////////////////////////////////// … … 595 608 process->th_tbl[ltid] = NULL; 596 609 process->th_nr--; 597 } 610 611 } // process_remove_thread() 612 598 613 599 614 ///////////////////////////////////////////////////// … … 601 616 { 602 617 char * path; // pathname to .elf file 603 process_t * process; // pointer on new process618 process_t * process; // local pointer on new process 604 619 pid_t pid; // new process pid 620 xptr_t parent_xp; // extended pointer on parent process 621 cxy_t parent_cxy; 622 process_t * parent_ptr; 623 uint32_t parent_pid; 605 624 thread_t * thread; // pointer on new thread 606 625 pthread_attr_t attr; // main thread attributes … … 609 628 error_t error; 610 629 611 // get pid and pathname to .elf file 612 path = exec_info->path; 613 pid = exec_info->pid; 630 // get parent and .elf pathname from exec_info 631 path = exec_info->path; 632 parent_xp = exec_info->parent_xp; 633 634 // get parent process cluster and local pointer 635 parent_cxy = GET_CXY( parent_xp ); 636 parent_ptr = (process_t *)GET_PTR( parent_xp ); 637 parent_pid = hal_remote_lw( XPTR( parent_cxy , &parent_ptr->pid ) ); 614 638 615 assert( (CXY_FROM_PID( pid ) == local_cxy) , __FUNCTION__ , "illegal PID\n" ); 616 617 exec_dmsg("\n[INFO] %s enters in cluster %x for process %x / path = %s\n", 618 __FUNCTION__ , local_cxy , pid , path ); 639 exec_dmsg("\n[INFO] %s enters in cluster %x for path = %s\n", 640 __FUNCTION__ , local_cxy , path ); 619 641 620 642 // create new process descriptor … … 623 645 if( process == NULL ) 624 646 { 625 printk("\n[ERROR] in %s : no memory in cluster %x for process%x / path = %s\n",626 __FUNCTION__ , local_cxy , p id , path );647 printk("\n[ERROR] in %s : no memory / cluster = %x / ppid = %x / path = %s\n", 648 __FUNCTION__ , local_cxy , parent_pid , path ); 627 649 return ENOMEM; 628 650 } 629 651 652 // get a pid from the local cluster 653 error = cluster_pid_alloc( XPTR( local_cxy , process ) , &pid ); 654 655 if( error ) 656 { 657 printk("\n[ERROR] in %s : cannot get PID / cluster = %x / ppid = %x / path = %s\n", 658 __FUNCTION__ , local_cxy , parent_pid , path ); 659 return ENOMEM; 660 } 661 630 662 // initialize the process descriptor as the reference 631 process_reference_init( process , pid , exec_info->ppid);663 process_reference_init( process , pid , parent_xp ); 632 664 633 // restore from exec_info the extended pointer on vfs root, cwd, and bin 634 process->vfs_root_xp = exec_info->vfs_root_xp; 635 process->vfs_cwd_xp = exec_info->vfs_cwd_xp; 636 process->vfs_bin_xp = exec_info->vfs_bin_xp; 637 638 // restore from exec_info the embedded fd_array 639 process_fd_remote_copy( XPTR( local_cxy , &process->fd_array ), exec_info->fd_array_xp ); 640 641 exec_dmsg("\n[INFO] %s restaured fd-array in cluster %x for process %x / path = %s\n", 642 __FUNCTION__, local_cxy , pid , path ); 665 exec_dmsg("\n[INFO] %s created process %x cluster %x / path = %s\n", 666 __FUNCTION__, parent_pid , local_cxy , path ); 667 668 // initializes vfs_root and vfs_cwd from parent process 669 xptr_t vfs_root_xp = hal_remote_lwd( XPTR( parent_cxy , &parent_ptr->vfs_root_xp ) ); 670 vfs_file_count_up( vfs_root_xp ); 671 process->vfs_root_xp = vfs_root_xp; 672 673 xptr_t vfs_cwd_xp = hal_remote_lwd( XPTR( parent_cxy , &parent_ptr->vfs_cwd_xp ) ); 674 vfs_file_count_up( vfs_cwd_xp ); 675 process->vfs_cwd_xp = vfs_cwd_xp; 676 677 // initialize embedded fd_array from parent process 678 process_fd_remote_copy( XPTR( local_cxy , &process->fd_array ), 679 XPTR( parent_cxy , &parent_ptr->fd_array) ); 643 680 644 681 // initialize signal manager TODO ??? [AG] … … 663 700 } 664 701 665 // create "heap" vseg descriptor702 // register "heap" vseg descriptor in VMM 666 703 vseg_t * heap_vseg = vmm_create_vseg( process, 667 704 CONFIG_VMM_HEAP_BASE, … … 699 736 } 700 737 738 // update children list in parent process 739 xlist_add_last( XPTR( parent_cxy , &parent_ptr->children_root ), 740 XPTR( local_cxy , &process->brothers_list ) ); 741 hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr) , 1 ); 742 701 743 // Register thread in scheduler 702 744 sched_register_thread( core , thread ); 703 745 704 746 exec_dmsg("\n[INFO] %s created thread for process %x on core %d in cluster %x\n", 705 __FUNCTION__ , p rocess->pid , core->lid , local_cxy );747 __FUNCTION__ , pid , core->lid , local_cxy ); 706 748 707 749 // activate new thread … … 715 757 void process_init_create() 716 758 { 717 process_t * process_init; // process_init descriptor718 pid_t init_pid; // process_init pid719 759 exec_info_t exec_info; // structure to be passed to process_make_exec() 720 760 721 error_t error1 = 0; 722 error_t error2 = 0; 723 error_t error3 = 0; 724 725 process_dmsg("\n[INFO] %s enters in cluster %x\n", __FUNCTION__ , local_cxy ); 726 727 // allocate memory for process descriptor 728 process_init = process_alloc(); 729 if( process_init == NULL ) 730 { 731 printk("\n[PANIC] in %s : no memory for process descriptor in cluster %x\n", 732 __FUNCTION__ , local_cxy ); 733 hal_core_sleep(); 734 } 735 736 // get a pid from the local cluster 737 xptr_t xp_process = XPTR( local_cxy , process_init ); 738 error1 = cluster_pid_alloc( xp_process , &init_pid ); 739 if( error1 ) 740 { 741 printk("\n[PANIC] in %s : cannot get PID in cluster %x\n", 742 __FUNCTION__ , local_cxy ); 743 hal_core_sleep(); 744 } 745 746 // initializes process_init descriptor as the reference 747 process_reference_init( process_init , init_pid , process_zero.pid ); 748 749 // initialize process_init VMM 750 vmm_init( process_init ); 751 752 // initializes vfs_root and vfs_cwd from process_zero 753 vfs_file_count_up( process_zero.vfs_root_xp ); 754 process_init->vfs_root_xp = process_zero.vfs_root_xp; 755 756 vfs_file_count_up( process_zero.vfs_cwd_xp ); 757 process_init->vfs_cwd_xp = process_zero.vfs_cwd_xp; 758 759 // update children list in process_zero 760 xlist_add_last( XPTR( local_cxy , &process_zero.children_root ), 761 XPTR( local_cxy , &process_init->brothers_list ) ); 762 process_zero.children_nr = 1; 763 764 // TODO create inodes for stdin / stdout / stderr pseudo-files 765 // these inodes should be created in the cluster containing the relevant TXT chdev 766 767 // open stdin / stdout / stderr pseudo-files 761 error_t error1; 762 error_t error2; 763 error_t error3; 768 764 xptr_t stdin_xp; 769 765 xptr_t stdout_xp; … … 773 769 uint32_t stderr_id; 774 770 771 process_dmsg("\n[INFO] %s enters in cluster %x\n", __FUNCTION__ , local_cxy ); 772 773 // open stdin / stdout / stderr pseudo-files 775 774 error1 = vfs_open( XPTR_NULL, CONFIG_DEV_STDIN , O_RDONLY, 0, &stdin_xp , &stdin_id ); 776 775 error2 = vfs_open( XPTR_NULL, CONFIG_DEV_STDOUT, O_WRONLY, 0, &stdout_xp, &stdout_id ); … … 782 781 if( !error2 ) vfs_close( stdout_xp , stdout_id ); 783 782 if( !error3 ) vfs_close( stderr_xp , stderr_id ); 784 785 783 printk("\n[PANIC] in %s : cannot open stdin/stdout/stderr in cluster %x\n", 786 784 __FUNCTION__ , local_cxy ); … … 791 789 if( (stdin_id != 0) || (stdout_id != 1) || (stderr_id != 2) ) 792 790 { 791 vfs_close( stdin_xp , stdin_id ); 792 vfs_close( stdout_xp , stdout_id ); 793 vfs_close( stderr_xp , stderr_id ); 793 794 printk("\n[PANIC] in %s : bad indexes for stdin/stdout/stderr in cluster %x\n", 794 795 __FUNCTION__ , local_cxy ); … … 797 798 798 799 // initialize the exec_info structure 799 exec_info.pid = process_init->pid; 800 exec_info.ppid = process_init->ppid; 801 exec_info.fd_array_xp = XPTR( local_cxy , &process_init->fd_array ); 802 exec_info.vfs_root_xp = process_init->vfs_root_xp; 803 exec_info.vfs_cwd_xp = process_init->vfs_cwd_xp; 804 exec_info.vfs_bin_xp = process_init->vfs_bin_xp; 805 806 // TODO thread_init ??? 807 // exec_info.args_nr = 1; 808 // exec_info.envs_nr = 1; 809 // strcpy( exec_info.args[0] , "init" ); 810 // strcpy( exec_info.envs[0] , "ALMOS-MKH.CONFIG = "CONFIG_ALMOS_VERSION ); 811 // strcpy( exec_info.path , INIT_PATHNAME ); 800 exec_info.parent_xp = XPTR( local_cxy , &process_zero ); 801 strcpy( exec_info.path , CONFIG_PROCESS_INIT_PATH ); 802 exec_info.args_nr = 0; 803 exec_info.envs_nr = 0; 812 804 813 805 // create process_init and thread_init 814 806 error1 = process_make_exec( &exec_info ); 807 815 808 if( error1 ) 816 809 {
Note: See TracChangeset
for help on using the changeset viewer.