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