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