Changeset 662 for trunk/kernel/kern/process.c
- Timestamp:
- Oct 10, 2020, 4:50:41 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/kern/process.c
r657 r662 1077 1077 1078 1078 /////////////////////////////////////////// 1079 char * process_fd_type_str( uint32_t type ) 1080 { 1081 switch( type ) 1082 { 1083 case INODE_TYPE_FILE : return "FILE"; 1084 case INODE_TYPE_DIR : return "DIR"; 1085 case INODE_TYPE_FIFO : return "FIFO"; 1086 case INODE_TYPE_PIPE : return "PIPE"; 1087 case INODE_TYPE_SOCK : return "SOCK"; 1088 case INODE_TYPE_DEV : return "DEV"; 1089 case INODE_TYPE_BLK : return "BLK"; 1090 case INODE_TYPE_SYML : return "SYML"; 1091 1092 default : return "undefined"; 1093 } 1094 } 1095 1096 /////////////////////////////////////////// 1079 1097 void process_fd_init( process_t * process ) 1080 1098 { … … 1085 1103 1086 1104 // initialize number of open files 1087 process->fd_array. current= 0;1105 process->fd_array.max = 0; 1088 1106 1089 1107 // initialize array … … 1101 1119 bool_t found; 1102 1120 uint32_t id; 1103 xptr_t xp; 1121 uint32_t max; // current value of max non-free slot index 1122 xptr_t entry_xp; // current value of one fd_array entry 1123 xptr_t lock_xp; // extended pointer on lock protecting fd_array 1124 xptr_t max_xp; // extended pointer on max field in fd_array 1104 1125 1105 1126 // get target process cluster and local pointer … … 1107 1128 cxy_t process_cxy = GET_CXY( process_xp ); 1108 1129 1109 // check target process is referenceprocess1110 assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr-> ref_xp ) ) ),1111 " client process must be referenceprocess\n" );1130 // check target process is owner process 1131 assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ) ) ), 1132 "process must be owner process\n" ); 1112 1133 1113 1134 #if DEBUG_PROCESS_FD_REGISTER … … 1120 1141 #endif 1121 1142 1122 // build extended pointer on lock protecting reference fd_array 1123 xptr_t lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock ); 1143 // build extended pointers on lock & max 1144 lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock ); 1145 max_xp = XPTR( process_cxy , &process_ptr->fd_array.max ); 1124 1146 1125 1147 // take lock protecting reference fd_array … … 1128 1150 found = false; 1129 1151 1152 // get current value of max_fdid 1153 max = hal_remote_l32( max_xp ); 1154 1130 1155 for ( id = 0; id < CONFIG_PROCESS_FILE_MAX_NR ; id++ ) 1131 1156 { 1132 xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) ); 1133 if ( xp == XPTR_NULL ) 1157 // get fd_array entry 1158 entry_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) ); 1159 1160 if ( entry_xp == XPTR_NULL ) 1134 1161 { 1135 // update referencefd_array1162 // update fd_array 1136 1163 hal_remote_s64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) , file_xp ); 1137 hal_remote_atomic_add( XPTR( process_cxy , &process_ptr->fd_array.current ) , 1 ); 1138 1139 // exit 1164 1165 // update max when required 1166 if( id > max ) hal_remote_s32( max_xp , id ); 1167 1168 // increase file refcount 1169 vfs_file_count_up( file_xp ); 1170 1171 // exit loop 1140 1172 *fdid = id; 1141 1173 found = true; … … 1165 1197 pid_t pid; // target process PID 1166 1198 lpid_t lpid; // target process LPID 1199 xptr_t file_xp; // extended pointer on file descriptor 1167 1200 xptr_t iter_xp; // iterator for list of process copies 1168 1201 xptr_t copy_xp; // extended pointer on process copy … … 1170 1203 cxy_t copy_cxy; // process copy cluster identifier 1171 1204 1172 // check process_xp argument1173 assert( (process_xp != XPTR_NULL), "process_xp argument cannot be XPTR_NULL");1174 1175 1205 // get target process cluster and local pointer 1176 1206 process_t * process_ptr = GET_PTR( process_xp ); 1177 1207 cxy_t process_cxy = GET_CXY( process_xp ); 1178 1208 1209 // check target process is owner process 1210 assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ) ) ), 1211 "process must be owner process\n" ); 1212 1179 1213 // get target process pid and lpid 1180 1214 pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid) ); 1181 1215 lpid = LPID_FROM_PID( pid ); 1182 1183 // get process descriptor in owner cluster and in reference cluster1184 xptr_t owner_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp ));1185 xptr_t ref_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->ref_xp ));1186 1187 // check target process in in owner cluster1188 assert( (process_xp == owner_xp), "target process must be in owner process\n" );1189 1216 1190 1217 #if DEBUG_PROCESS_FD_REMOVE … … 1196 1223 #endif 1197 1224 1225 // get extended pointer on file descriptor 1226 file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[fdid] )); 1227 1198 1228 // build extended pointers on list_of_copies root and lock (in owner cluster) 1199 1229 xptr_t copies_root_xp = XPTR( process_cxy , &LOCAL_CLUSTER->pmgr.copies_root[lpid] ); 1200 1230 xptr_t copies_lock_xp = XPTR( process_cxy , &LOCAL_CLUSTER->pmgr.copies_lock[lpid] ); 1201 1231 1202 // get reference process cluster and local pointer 1203 process_t * ref_ptr = GET_PTR( ref_xp ); 1204 cxy_t ref_cxy = GET_CXY( ref_xp ); 1205 1206 // build extended pointer on lock protecting reference fd_array 1207 xptr_t fd_lock_xp = XPTR( ref_cxy , &ref_ptr->fd_array.lock ); 1208 1209 // take lock protecting reference fd_array 1232 // build extended pointer on fd_array lock and max 1233 xptr_t fd_lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock ); 1234 xptr_t fd_max_xp = XPTR( process_cxy , &process_ptr->fd_array.max ); 1235 1236 // take lock protecting fd_array 1210 1237 remote_queuelock_acquire( fd_lock_xp ); 1211 1238 1212 1239 // take the lock protecting the list of copies 1213 1240 remote_queuelock_acquire( copies_lock_xp ); 1241 1242 // get max value 1243 uint32_t max = hal_remote_l32( fd_max_xp ); 1214 1244 1215 1245 // loop on list of process copies … … 1223 1253 // release the fd_array entry in process copy 1224 1254 hal_remote_s64( XPTR( copy_cxy , ©_ptr->fd_array.array[fdid] ), XPTR_NULL ); 1225 } 1255 1256 // decrease file refcount 1257 vfs_file_count_down( file_xp ); 1258 } 1259 1260 // update max when required 1261 if( fdid == max ) hal_remote_s32( fd_max_xp , max-1 ); 1226 1262 1227 1263 // release the lock protecting reference fd_array … … 1240 1276 } // end process_fd_remove() 1241 1277 1242 //////////////////////////////////////////////// 1243 xptr_t process_fd_get_xptr( process_t * process, 1244 uint32_t fdid ) 1278 ////////////////////////////////////////////// 1279 void process_fd_clean_all( xptr_t process_xp ) 1280 { 1281 uint32_t id; 1282 xptr_t file_xp; // one fd_array entry 1283 xptr_t lock_xp; // extendad pointer on lock protecting fd_array 1284 uint32_t max; // number of registered files 1285 error_t error; 1286 1287 // get process cluster, local pointer and PID 1288 process_t * process_ptr = GET_PTR( process_xp ); 1289 cxy_t process_cxy = GET_CXY( process_xp ); 1290 pid_t pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid) ); 1291 1292 // check target process is owner process 1293 assert( (process_xp == hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp )) ), 1294 "process must be owner process\n" ); 1295 1296 #if DEBUG_PROCESS_FD_CLEAN_ALL 1297 thread_t * this = CURRENT_THREAD; 1298 uint32_t cycle = (uint32_t)hal_get_cycles(); 1299 if( DEBUG_PROCESS_FD_CLEAN_ALL < cycle ) 1300 printk("\n[%s] thread[%x,%x] enter for process %x / cycle %d\n", 1301 __FUNCTION__, this->process->pid, this->trdid, pid, cycle ); 1302 1303 process_fd_display( process_xp ); 1304 #endif 1305 1306 // build extended pointer on lock protecting the fd_array 1307 lock_xp = XPTR( process_cxy , &process_ptr->fd_array.lock ); 1308 1309 // get max index for fd_array 1310 max = hal_remote_l32( XPTR( process_cxy , &process_ptr->fd_array.max )); 1311 1312 // take lock protecting fd_array 1313 remote_queuelock_acquire( lock_xp ); 1314 1315 for ( id = 0; id <= max ; id++ ) 1316 { 1317 // get fd_array entry 1318 file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[id] ) ); 1319 1320 if ( file_xp != XPTR_NULL ) 1321 { 1322 // close the file or socket 1323 error = sys_close( id ); 1324 1325 if( error ) 1326 printk("/n[ERROR] in %s : cannot close the file %d for process %x\n", 1327 __FUNCTION__, id, pid ); 1328 } 1329 } 1330 1331 // release lock protecting fd_array 1332 remote_queuelock_release( lock_xp ); 1333 1334 #if DEBUG_PROCESS_FD_CLEAN_ALL 1335 cycle = (uint32_t)hal_get_cycles(); 1336 if( DEBUG_PROCESS_FD_CLEAN_ALL < cycle ) 1337 printk("\n[%s] thread[%x,%x] exit for process %x / cycle %d\n", 1338 __FUNCTION__, this->process->pid, this->trdid, pid, cycle ); 1339 #endif 1340 1341 } // end process_fd_clean_all() 1342 1343 ////////////////////////////////////////////////////////////// 1344 xptr_t process_fd_get_xptr_from_owner( xptr_t process_xp, 1345 uint32_t fdid ) 1346 { 1347 cxy_t process_cxy = GET_CXY( process_xp ); 1348 process_t * process_ptr = GET_PTR( process_xp ); 1349 1350 assert( (hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp )) == process_xp), 1351 "process_xp argument must be the owner process" ); 1352 1353 // access owner process fd_array 1354 return hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[fdid] )); 1355 1356 } // end process_fd_get_xptr_from_owner() 1357 1358 /////////////////////////////////////////////////////////// 1359 xptr_t process_fd_get_xptr_from_local( process_t * process, 1360 uint32_t fdid ) 1245 1361 { 1246 1362 xptr_t file_xp; … … 1252 1368 if( file_xp == XPTR_NULL ) 1253 1369 { 1254 // get referenceprocess cluster and local pointer1255 xptr_t ref_xp = process->ref_xp;1256 cxy_t ref_cxy = GET_CXY( ref_xp );1257 process_t * ref_ptr = GET_PTR( ref_xp );1258 1259 // build extended pointer on lock protecting referencefd_array1260 lock_xp = XPTR( ref_cxy , &ref_ptr->fd_array.lock );1261 1262 // take lock protecting referencefd_array1370 // get owner process cluster and local pointer 1371 xptr_t owner_xp = process->owner_xp; 1372 cxy_t owner_cxy = GET_CXY( owner_xp ); 1373 process_t * owner_ptr = GET_PTR( owner_xp ); 1374 1375 // build extended pointer on lock protecting fd_array 1376 lock_xp = XPTR( owner_cxy , &owner_ptr->fd_array.lock ); 1377 1378 // take lock protecting fd_array 1263 1379 remote_queuelock_acquire( lock_xp ); 1264 1380 1265 1381 // access reference process descriptor 1266 file_xp = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->fd_array.array[fdid] ) ); 1267 1268 // update local fd_array if found 1269 if( file_xp != XPTR_NULL ) process->fd_array.array[fdid] = file_xp; 1382 file_xp = hal_remote_l64( XPTR( owner_cxy , &owner_ptr->fd_array.array[fdid] ) ); 1383 1384 if( file_xp != XPTR_NULL ) 1385 { 1386 // update local fd_array 1387 process->fd_array.array[fdid] = file_xp; 1270 1388 1271 // release lock protecting reference fd_array 1389 // increase file refcount 1390 vfs_file_count_up( file_xp ); 1391 } 1392 1393 // release lock protecting fd_array 1272 1394 remote_queuelock_release( lock_xp ); 1273 1395 } … … 1275 1397 return file_xp; 1276 1398 1277 } // end process_fd_get_xptr ()1399 } // end process_fd_get_xptr_from_local() 1278 1400 1279 1401 /////////////////////////////////////////// … … 1319 1441 bool_t process_fd_array_full( void ) 1320 1442 { 1321 // get extended pointer on referenceprocess1322 xptr_t ref_xp = CURRENT_THREAD->process->ref_xp;1323 1324 // get referenceprocess cluster and local pointer1325 process_t * ref_ptr = GET_PTR( ref_xp );1326 cxy_t ref_cxy = GET_CXY( ref_xp );1327 1328 // get number of open file descriptors from referencefd_array1329 uint32_t current = hal_remote_l32( XPTR( ref_cxy , &ref_ptr->fd_array.current ));1330 1331 return ( current >= CONFIG_PROCESS_FILE_MAX_NR);1443 // get extended pointer on owner process 1444 xptr_t owner_xp = CURRENT_THREAD->process->owner_xp; 1445 1446 // get owner process cluster and local pointer 1447 process_t * owner_ptr = GET_PTR( owner_xp ); 1448 cxy_t owner_cxy = GET_CXY( owner_xp ); 1449 1450 // get number of open file descriptors from fd_array 1451 uint32_t max = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->fd_array.max )); 1452 1453 return ( max == CONFIG_PROCESS_FILE_MAX_NR - 1 ); 1332 1454 } 1333 1455 1456 //////////////////////////////////////////// 1457 void process_fd_display( xptr_t process_xp ) 1458 { 1459 uint32_t fdid; 1460 xptr_t file_xp; 1461 vfs_file_t * file_ptr; 1462 cxy_t file_cxy; 1463 uint32_t file_type; 1464 xptr_t inode_xp; 1465 vfs_inode_t * inode_ptr; 1466 1467 char name[CONFIG_VFS_MAX_NAME_LENGTH]; 1468 1469 // get process cluster and local pointer 1470 process_t * process_ptr = GET_PTR( process_xp ); 1471 cxy_t process_cxy = GET_CXY( process_xp ); 1472 1473 // get process PID 1474 pid_t pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid )); 1475 1476 // get pointers on owner process descriptor 1477 xptr_t owner_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->owner_xp )); 1478 process_t * owner_ptr = GET_PTR( owner_xp ); 1479 cxy_t owner_cxy = GET_CXY( owner_xp ); 1480 1481 // get max fdid from owner process descriptor 1482 uint32_t max = hal_remote_l32( XPTR( owner_cxy , &owner_ptr->fd_array.max )); 1483 1484 printk("\n***** fd_array for pid %x in cluster %x / max %d *****\n", 1485 pid, process_cxy, max ); 1486 1487 for( fdid = 0 ; fdid <= max ; fdid++ ) 1488 { 1489 // get pointers on file descriptor 1490 file_xp = hal_remote_l64( XPTR( process_cxy , &process_ptr->fd_array.array[fdid] )); 1491 file_ptr = GET_PTR( file_xp ); 1492 file_cxy = GET_CXY( file_xp ); 1493 1494 if( file_xp != XPTR_NULL ) 1495 { 1496 // get file type 1497 file_type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type )); 1498 1499 // get file name for a true file 1500 if( file_type == INODE_TYPE_FILE ) 1501 { 1502 // get inode pointers 1503 inode_ptr = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode )); 1504 inode_xp = XPTR( file_cxy , inode_ptr ); 1505 1506 // get file name 1507 vfs_inode_get_name( inode_xp , name ); 1508 1509 // display relevant file decriptor info 1510 printk(" - %d : type %s (%s)\n", 1511 fdid , process_fd_type_str(file_type), name ); 1512 } 1513 else 1514 { 1515 // display relevant file decriptor info 1516 printk(" - %d : type %s\n", 1517 fdid , process_fd_type_str(file_type) ); 1518 } 1519 } 1520 else 1521 { 1522 // display relevant file decriptor info 1523 printk(" - %d : empty slot\n", 1524 fdid ); 1525 } 1526 } 1527 } // end process_fd_display() 1334 1528 1335 1529 ////////////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset
for help on using the changeset viewer.