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