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