[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> |
---|
| 15 | |
---|
| 16 | #include <stdlib.h> |
---|
| 17 | #include <stdio.h> |
---|
| 18 | #include <pthread.h> |
---|
| 19 | |
---|
[442] | 20 | #define DEBUG_PROCESS_INIT 0 |
---|
[440] | 21 | |
---|
| 22 | // TODO make the cxy computation portable [AG] |
---|
| 23 | #define CXY_FROM_XY( x , y ) ((x<<4) + y) |
---|
| 24 | |
---|
[427] | 25 | ////////// |
---|
| 26 | int main() |
---|
| 27 | { |
---|
[438] | 28 | int i; |
---|
| 29 | int ret_fork; // fork return value |
---|
| 30 | int ret_exec; // exec return value |
---|
| 31 | int rcv_pid; // pid received from the wait syscall |
---|
| 32 | int status; // used by the wait syscall |
---|
| 33 | char string[64]; // log messages on kernel TXT0 |
---|
[427] | 34 | |
---|
| 35 | // check number of TXT channels |
---|
| 36 | assert( NB_TXT_CHANNELS > 1 ); |
---|
| 37 | |
---|
| 38 | // create the KSH processes (one per user terminal) |
---|
[434] | 39 | for( i = 1 ; i < NB_TXT_CHANNELS ; i++ ) |
---|
[427] | 40 | { |
---|
[434] | 41 | // INIT process fork process CHILD[i] |
---|
| 42 | ret_fork = fork(); |
---|
| 43 | |
---|
[436] | 44 | if( ret_fork < 0 ) // error in fork |
---|
[427] | 45 | { |
---|
[437] | 46 | // INIT display error message |
---|
[440] | 47 | snprintf( string , 64 , "[INIT] cannot fork child[%d] => suicide" , i ); |
---|
[427] | 48 | display_string( string ); |
---|
| 49 | |
---|
[436] | 50 | // INIT suicide |
---|
[434] | 51 | exit( 0 ); |
---|
[427] | 52 | } |
---|
[434] | 53 | else if( ret_fork == 0 ) // we are in CHILD[i] process |
---|
[427] | 54 | { |
---|
[434] | 55 | // CHILD[i] process exec process KSH[i] |
---|
| 56 | ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); |
---|
| 57 | |
---|
| 58 | if ( ret_exec ) // error in exec |
---|
[427] | 59 | { |
---|
[437] | 60 | // CHILD[i] display error message |
---|
[435] | 61 | snprintf( string , 64 , |
---|
[440] | 62 | "[INIT ERROR] CHILD[%d] cannot exec KSH / ret_exec = %d" , i , ret_exec ); |
---|
[434] | 63 | display_string( string ); |
---|
[427] | 64 | } |
---|
| 65 | } |
---|
[434] | 66 | else // we are in INIT process |
---|
| 67 | { |
---|
| 68 | // INIT display CHILD[i] process PID |
---|
[440] | 69 | snprintf( string , 64 , "[INIT] created KSH[%d] / pid = %x", i , ret_fork ); |
---|
[434] | 70 | display_string( string ); |
---|
| 71 | } |
---|
| 72 | } |
---|
[440] | 73 | |
---|
[442] | 74 | #if DEBUG_PROCESS_INIT |
---|
[434] | 75 | |
---|
[440] | 76 | unsigned int x_size; // number of clusters in a row |
---|
| 77 | unsigned int y_size; // number of clusters in a column |
---|
| 78 | unsigned int ncores; // number of cores per cluster |
---|
| 79 | unsigned int x; // cluster x coordinate |
---|
| 80 | unsigned int y; // cluster y coordinate |
---|
| 81 | unsigned int cxy; // cluster identifier |
---|
| 82 | unsigned int lid; // core local index |
---|
[437] | 83 | |
---|
[440] | 84 | // get hardware config |
---|
| 85 | get_config( &x_size , &y_size , &ncores ); |
---|
| 86 | |
---|
| 87 | // INIT displays processes and threads in all clusters |
---|
| 88 | for( x = 0 ; x < x_size ; x++ ) |
---|
| 89 | { |
---|
| 90 | for( y = 0 ; y < y_size ; y++ ) |
---|
| 91 | { |
---|
| 92 | cxy = CXY_FROM_XY( x , y ); |
---|
| 93 | display_cluster_processes( cxy ); |
---|
| 94 | for( lid = 0 ; lid < ncores ; lid++ ) |
---|
| 95 | { |
---|
| 96 | display_sched( cxy , lid ); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | #endif |
---|
| 102 | |
---|
[435] | 103 | // This loop detects the termination of the KSH[i] processes, |
---|
[436] | 104 | // and recreate a new KSH[i] process when required. |
---|
[427] | 105 | while( 1 ) |
---|
| 106 | { |
---|
[435] | 107 | // block on child processes termination |
---|
[436] | 108 | rcv_pid = wait( &status ); |
---|
[427] | 109 | |
---|
[435] | 110 | if( WIFSTOPPED( status ) ) // stopped => unblock it |
---|
| 111 | { |
---|
| 112 | // display string to report unexpected KSH process block |
---|
[440] | 113 | snprintf( string , 64 , "[INIT] KSH process %x stopped => unblock it" , rcv_pid ); |
---|
[435] | 114 | display_string( string ); |
---|
| 115 | |
---|
[440] | 116 | // TODO : unblock KSH [AG] |
---|
[435] | 117 | |
---|
[436] | 118 | } // end KSH stopped handling |
---|
| 119 | |
---|
[435] | 120 | if( WIFSIGNALED( status ) || WIFEXITED( status ) ) // killed => recreate it |
---|
| 121 | { |
---|
[437] | 122 | // display string to report KSH process termination |
---|
[440] | 123 | snprintf( string , 64 , "[INIT] KSH process %x terminated => recreate", rcv_pid ); |
---|
[435] | 124 | display_string( string ); |
---|
[436] | 125 | |
---|
| 126 | // INIT process fork a new CHILD process |
---|
| 127 | ret_fork = fork(); |
---|
| 128 | |
---|
| 129 | if( ret_fork < 0 ) // error in fork |
---|
| 130 | { |
---|
[437] | 131 | // INIT display error message |
---|
[440] | 132 | snprintf( string , 64 , "[INIT ERROR] cannot fork child => suicide"); |
---|
[436] | 133 | display_string( string ); |
---|
| 134 | |
---|
| 135 | // INIT suicide |
---|
| 136 | exit( 0 ); |
---|
| 137 | } |
---|
| 138 | else if( ret_fork == 0 ) // we are in CHILD process |
---|
| 139 | { |
---|
| 140 | // CHILD process exec process KSH |
---|
| 141 | ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); |
---|
| 142 | |
---|
| 143 | if ( ret_exec ) // error in exec |
---|
| 144 | { |
---|
| 145 | // CHILD display error message on TXT0 terminal |
---|
[440] | 146 | snprintf( string , 64 , "[INIT ERROR] CHILD cannot exec KSH" ); |
---|
[436] | 147 | display_string( string ); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | else // we are in INIT process |
---|
| 151 | { |
---|
[442] | 152 | // INIT display new KSH process PID |
---|
| 153 | snprintf( string , 64 , "[INIT] re-created KSH / pid = %x", ret_fork ); |
---|
[436] | 154 | display_string( string ); |
---|
| 155 | } |
---|
[438] | 156 | } // end KSH kill handling |
---|
[427] | 157 | |
---|
[442] | 158 | #if DEBUG_PROCESS_INIT |
---|
[438] | 159 | |
---|
[440] | 160 | // INIT displays processes and threads in all clusters |
---|
| 161 | for( x = 0 ; x < x_size ; x++ ) |
---|
| 162 | { |
---|
| 163 | for( y = 0 ; y < y_size ; y++ ) |
---|
| 164 | { |
---|
| 165 | cxy = CXY_FROM_XY( x , y ); |
---|
| 166 | display_cluster_processes( cxy ); |
---|
| 167 | for( lid = 0 ; lid < ncores ; lid++ ) |
---|
| 168 | { |
---|
| 169 | display_sched( cxy , lid ); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | } |
---|
[438] | 173 | |
---|
[440] | 174 | #endif |
---|
| 175 | |
---|
[438] | 176 | } // end while waiting KSH[i] termination |
---|
| 177 | |
---|
[427] | 178 | } // end main() |
---|
| 179 | |
---|