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