[427] | 1 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : init.c |
---|
| 3 | // Date : January 2018 |
---|
| 4 | // Author : Alain Greiner |
---|
| 5 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 6 | // This single thread application implement the "init" process for ALMOS-MKH. |
---|
[434] | 7 | // It uses the fork/exec syscalls to create N KSH child processes |
---|
| 8 | // (one child process per user terminal). |
---|
| 9 | // Then calls the wait() function to block, and reactivate any child KSH process |
---|
| 10 | // that has been deleted, using a new fork/exec. |
---|
[440] | 11 | // It includes the hard_config.h file to get th NB_TXT_CHANNELS parameter. |
---|
[427] | 12 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 13 | |
---|
| 14 | #include <hard_config.h> |
---|
[444] | 15 | #include <unistd.h> |
---|
[427] | 16 | #include <stdlib.h> |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <pthread.h> |
---|
[445] | 19 | #include <almosmkh.h> |
---|
[574] | 20 | #include <hal_macros.h> |
---|
[444] | 21 | #include <sys/wait.h> |
---|
[427] | 22 | |
---|
[581] | 23 | #define DEBUG_PROCESS_INIT 1 |
---|
[440] | 24 | |
---|
[445] | 25 | // TODO improve the get_config() syscall to return nb_txt_channels |
---|
| 26 | // and avoid the hard_config include [AG] |
---|
| 27 | |
---|
[574] | 28 | // TODO introduce a communication channel between INIT and KSH |
---|
| 29 | // to allow KSH to signal INIT the exec completion. |
---|
| 30 | |
---|
| 31 | //////////////// |
---|
[475] | 32 | int main( void ) |
---|
[427] | 33 | { |
---|
[438] | 34 | int i; |
---|
| 35 | int ret_fork; // fork return value |
---|
| 36 | int ret_exec; // exec return value |
---|
| 37 | int rcv_pid; // pid received from the wait syscall |
---|
| 38 | int status; // used by the wait syscall |
---|
| 39 | char string[64]; // log messages on kernel TXT0 |
---|
[427] | 40 | |
---|
[574] | 41 | #if DEBUG_PROCESS_INIT |
---|
| 42 | display_string("[INIT] process enters"); |
---|
| 43 | #endif |
---|
| 44 | |
---|
[427] | 45 | // check number of TXT channels |
---|
[445] | 46 | if( NB_TXT_CHANNELS < 2 ) |
---|
| 47 | { |
---|
| 48 | printf("\n[ERROR] in init process : number of TXT channels must be larger than 1\n"); |
---|
| 49 | exit( EXIT_FAILURE ); |
---|
| 50 | } |
---|
[427] | 51 | |
---|
| 52 | // create the KSH processes (one per user terminal) |
---|
[434] | 53 | for( i = 1 ; i < NB_TXT_CHANNELS ; i++ ) |
---|
[427] | 54 | { |
---|
[434] | 55 | // INIT process fork process CHILD[i] |
---|
| 56 | ret_fork = fork(); |
---|
| 57 | |
---|
[436] | 58 | if( ret_fork < 0 ) // error in fork |
---|
[427] | 59 | { |
---|
[437] | 60 | // INIT display error message |
---|
[457] | 61 | snprintf( string , 64 , "[INIT ERROR] cannot fork child[%d] => suicide" , i ); |
---|
[427] | 62 | display_string( string ); |
---|
| 63 | |
---|
[436] | 64 | // INIT suicide |
---|
[434] | 65 | exit( 0 ); |
---|
[427] | 66 | } |
---|
[434] | 67 | else if( ret_fork == 0 ) // we are in CHILD[i] process |
---|
[427] | 68 | { |
---|
[434] | 69 | // CHILD[i] process exec process KSH[i] |
---|
[444] | 70 | ret_exec = execve( "/bin/user/ksh.elf" , NULL , NULL ); |
---|
[434] | 71 | |
---|
| 72 | if ( ret_exec ) // error in exec |
---|
[427] | 73 | { |
---|
[437] | 74 | // CHILD[i] display error message |
---|
[435] | 75 | snprintf( string , 64 , |
---|
[440] | 76 | "[INIT ERROR] CHILD[%d] cannot exec KSH / ret_exec = %d" , i , ret_exec ); |
---|
[434] | 77 | display_string( string ); |
---|
[427] | 78 | } |
---|
| 79 | } |
---|
[434] | 80 | else // we are in INIT process |
---|
| 81 | { |
---|
[457] | 82 | // INIT display CHILD[i] process PID |
---|
| 83 | snprintf( string , 64 , "[INIT] created KSH[%d] / pid = %x", i , ret_fork ); |
---|
| 84 | display_string( string ); |
---|
[434] | 85 | } |
---|
| 86 | } |
---|
[440] | 87 | |
---|
[442] | 88 | #if DEBUG_PROCESS_INIT |
---|
[528] | 89 | { |
---|
| 90 | unsigned int x_size; // number of clusters in a row |
---|
| 91 | unsigned int y_size; // number of clusters in a column |
---|
| 92 | unsigned int ncores; // number of cores per cluster |
---|
| 93 | unsigned int x; // cluster x coordinate |
---|
| 94 | unsigned int y; // cluster y coordinate |
---|
| 95 | unsigned int cxy; // cluster identifier |
---|
| 96 | unsigned int lid; // core local index |
---|
[434] | 97 | |
---|
[528] | 98 | // get hardware config |
---|
| 99 | get_config( &x_size , &y_size , &ncores ); |
---|
| 100 | |
---|
| 101 | // INIT displays processes and threads in all clusters |
---|
| 102 | for( x = 0 ; x < x_size ; x++ ) |
---|
[440] | 103 | { |
---|
[581] | 104 | for( y = 0 ; y < x_size ; y++ ) |
---|
| 105 | { |
---|
| 106 | cxy = HAL_CXY_FROM_XY( x , y ); |
---|
[528] | 107 | display_cluster_processes( cxy ); |
---|
[581] | 108 | for( lid = 0 ; lid < ncores ; lid++ ) |
---|
| 109 | { |
---|
| 110 | display_sched( cxy , lid ); |
---|
| 111 | } |
---|
[440] | 112 | } |
---|
| 113 | } |
---|
[457] | 114 | } |
---|
[440] | 115 | #endif |
---|
| 116 | |
---|
[435] | 117 | // This loop detects the termination of the KSH[i] processes, |
---|
[436] | 118 | // and recreate a new KSH[i] process when required. |
---|
[427] | 119 | while( 1 ) |
---|
| 120 | { |
---|
[435] | 121 | // block on child processes termination |
---|
[436] | 122 | rcv_pid = wait( &status ); |
---|
[427] | 123 | |
---|
[435] | 124 | if( WIFSTOPPED( status ) ) // stopped => unblock it |
---|
| 125 | { |
---|
| 126 | // display string to report unexpected KSH process block |
---|
[440] | 127 | snprintf( string , 64 , "[INIT] KSH process %x stopped => unblock it" , rcv_pid ); |
---|
[435] | 128 | display_string( string ); |
---|
| 129 | |
---|
[440] | 130 | // TODO : unblock KSH [AG] |
---|
[435] | 131 | |
---|
[436] | 132 | } // end KSH stopped handling |
---|
| 133 | |
---|
[435] | 134 | if( WIFSIGNALED( status ) || WIFEXITED( status ) ) // killed => recreate it |
---|
| 135 | { |
---|
[437] | 136 | // display string to report KSH process termination |
---|
[440] | 137 | snprintf( string , 64 , "[INIT] KSH process %x terminated => recreate", rcv_pid ); |
---|
[435] | 138 | display_string( string ); |
---|
[436] | 139 | |
---|
| 140 | // INIT process fork a new CHILD process |
---|
| 141 | ret_fork = fork(); |
---|
| 142 | |
---|
| 143 | if( ret_fork < 0 ) // error in fork |
---|
| 144 | { |
---|
[437] | 145 | // INIT display error message |
---|
[440] | 146 | snprintf( string , 64 , "[INIT ERROR] cannot fork child => suicide"); |
---|
[436] | 147 | display_string( string ); |
---|
| 148 | |
---|
| 149 | // INIT suicide |
---|
| 150 | exit( 0 ); |
---|
| 151 | } |
---|
| 152 | else if( ret_fork == 0 ) // we are in CHILD process |
---|
| 153 | { |
---|
| 154 | // CHILD process exec process KSH |
---|
[444] | 155 | ret_exec = execve( "/bin/user/ksh.elf" , NULL , NULL ); |
---|
[436] | 156 | |
---|
| 157 | if ( ret_exec ) // error in exec |
---|
| 158 | { |
---|
| 159 | // CHILD display error message on TXT0 terminal |
---|
[440] | 160 | snprintf( string , 64 , "[INIT ERROR] CHILD cannot exec KSH" ); |
---|
[436] | 161 | display_string( string ); |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | else // we are in INIT process |
---|
| 165 | { |
---|
[442] | 166 | // INIT display new KSH process PID |
---|
| 167 | snprintf( string , 64 , "[INIT] re-created KSH / pid = %x", ret_fork ); |
---|
[436] | 168 | display_string( string ); |
---|
| 169 | } |
---|
[438] | 170 | } // end KSH kill handling |
---|
[427] | 171 | |
---|
[438] | 172 | } // end while waiting KSH[i] termination |
---|
| 173 | |
---|
[427] | 174 | } // end main() |
---|
| 175 | |
---|