PageTable* LongModePaging::allocate_page_table(unsigned pml4_entry_id , unsigned pdpt_entry_id , unsigned pdt_entry_id) { assert( mode == LongMode_Normal); PageTable* pdp_table=get_next_level_address<PageTable>(pml4 , pml4_entry_id); if( pdp_table ) { PageTable* pd_table=get_next_level_address<PageTable>(pdp_table , pdpt_entry_id); if(pd_table) { if(is_present(pd_table , pdt_entry_id)) { PageTable* table = get_next_level_address<PageTable>(pd_table,pdt_entry_id); return table; } else { PageTable* table_tmp= gm_memalign<PageTable>( CACHE_LINE_BYTES, 1); PageTable* table = new (table_tmp)PageTable(ENTRY_512); validate_entry(pd_table , pdt_entry_id ,table ); cur_pt_num++; return table; } } //page_direcory doesn't exist allocate else { if( allocate_page_directory(pml4_entry_id,pdpt_entry_id)) { //get page directory PageTable* page_dir = get_next_level_address<PageTable>( pdp_table , pdpt_entry_id); PageTable* table= gm_memalign<PageTable>( CACHE_LINE_BYTES, 1); PageTable* pg_table=new (table)PageTable(ENTRY_512); validate_entry(page_dir , pdt_entry_id , pg_table ); cur_pt_num++; return pg_table; } } } else { PageTable* g_tables = gm_memalign<PageTable>(CACHE_LINE_BYTES,3); PageTable* pdp_table=new (&g_tables[0])PageTable(ENTRY_512); //std::cout<<"validate pg dir pointer in pml4"<<std::dec<<pml4_entry_id<<std::endl; validate_entry(pml4,pml4_entry_id,pdp_table); cur_pdp_num++; PageTable* pd_table=new (&g_tables[1])PageTable(ENTRY_512); //std::cout<<"validate page directory table in pg dir pointer"<<std::dec<<pdpt_entry_id<<std::endl; validate_entry(pdp_table,pdpt_entry_id , pd_table); cur_pd_num++; PageTable* pg_table=new (&g_tables[2])PageTable(ENTRY_512); //std::cout<<"new page table"<<std::endl; validate_entry(pd_table , pdt_entry_id , pg_table); //std::cout<<"validate: "<<std::dec<<pml4_entry_id<<","<<std::dec<<pdpt_entry_id<<","<<std::dec<<pdt_entry_id<<std::endl; cur_pt_num++; return pg_table; } return NULL; }
/*****-----functional interface of LongMode-Paging----*****/ bool LongModePaging::map_page_table(Address addr , void* pg_ptr , bool pbuffer) { //std::cout<<"map:"<<std::hex<<addr<<std::endl; unsigned pml4,pdp,pd,pt; get_domains(addr , pml4 , pdp , pd , pt , mode); unsigned buffer_entry_id = get_buffer_table_off(addr,buffer_table_shift,mode); assert( (pml4!=(unsigned)(-1)) && (pdp!=(unsigned)(-1))); PageTable* table; if( mode == LongMode_Normal) { assert( (pd!=(unsigned)(-1)) &&(pt!=(unsigned)(-1))); table = allocate_page_table(pml4,pdp,pd); if( !table ) { debug_printf("allocate page table for LongMode_Normal failed!"); return false; } if( pbuffer) { //std::cout<<"extend one buffer map"<<std::endl; extend_one_buffer_map(addr,table,pg_ptr , pt, buffer_entry_id, buffer_table_entry_num); } else { validate_entry(table , pt , pg_ptr); } } else if( mode == LongMode_Middle ) { table = allocate_page_directory( pml4 , pdp); if(!table) { debug_printf("allocate page directory for LongMode_Middle failed!"); return false; } if( pbuffer ) extend_one_buffer_map(addr , table , pg_ptr , pd , buffer_entry_id , buffer_table_entry_num); else validate_entry( table,pd, pg_ptr); } else if( mode == LongMode_Huge ) { table = allocate_page_directory_pointer(pml4); if(!table) { debug_printf("allocate page directory pointer for LongMode_Huge failed!"); return false; } if(pbuffer) extend_one_buffer_map(addr , table ,pg_ptr, pdp,buffer_entry_id , buffer_table_entry_num); else validate_entry(table,pdp,pg_ptr); } return true; }
bool LongModePaging::allocate_page_directory(pair_list high_level_entry) { bool succeed=true; for( pair_list_ptr it=high_level_entry.begin(); it!=high_level_entry.end() ; it++) { if( allocate_page_directory((*it).first,(*it).second)==false) { debug_printf("allocate (pml4_entry_id , page directory pointer entry id)---(%d,%d) failed",(*it).first , (*it).second ); succeed = false; } } return succeed; }
/********************** ************************/ int main(int argc,char *argv[]) { int n_pages = 0; //int n_frames = 0; ram_info_t ram_info;// ram_info.n_frames = 0;// ram_info.algorithm = NULL;// int n_cache = 0; int n_tlb = 0; char *access_file = NULL; FILE *access_fd = NULL; addr_t virtual_addr = 0; addr_t physical_addr; pid_t pid = 0; char mode; /* * Parse arguments */ if( 0 != parse_args(argc, argv, &access_file, &n_pages, &ram_info.n_frames, &n_cache, &n_tlb, &ram_info.algorithm) ) {// return -1; } /* * Setup data structures */ srand(time(NULL)); current_ref = (char *)malloc(sizeof(char) * MAX_LINE); clear_stats(); stats.cache_size = n_cache; stats.tlb_size = n_tlb; stats.num_pages = n_pages; stats.num_frames = ram_info.n_frames;// printf("Allocating resources...\n"); allocate_cache(n_cache); allocate_tlb(n_tlb); allocate_page_table(n_pages); allocate_page_directory(n_pages); //allocate_ram(n_frames); allocate_ram(ram_info); /* * Open the file that we are going to read */ if( NULL == (access_fd = fopen(access_file, "r") ) ) { fprintf(stderr, "Error: Unable to open the access file <%s>\n", access_file); return -1; } /* * Read page requests from the file */ gettimeofday(&stats.start, NULL); while(0 == feof(access_fd) ) { /* Read one line */ current_ref[0] = '\0'; if( NULL == fgets(current_ref, MAX_LINE, access_fd) ) { break; } /* Strip off the newline */ if( '\n' == current_ref[strlen(current_ref)-1] ) { current_ref[strlen(current_ref)-1] = '\0'; } extract_args(current_ref, &pid, &mode, &virtual_addr); /* * Memory management operations to access the page */ if(VERBOSE){ printf("-----------------------------------------------------------\n"); printf("%s: Process %*d \t Access [Page %4d, Offset %#05x] (%#010x)\n", current_ref, MAX_PID_LEN, pid, GET_PAGE(virtual_addr), GET_OFFSET(virtual_addr), virtual_addr); } access_page(pid, mode, virtual_addr, &physical_addr); if(VERBOSE){ printf("%s: Process %*d \t Access [Page %4d, Offset %#05x] (%#010x) --> (%#010x) [Frame %4d, Offset %#05x]\n", current_ref, MAX_PID_LEN, pid, GET_PAGE(virtual_addr), GET_OFFSET(virtual_addr), virtual_addr, physical_addr, GET_FRAME(physical_addr), GET_OFFSET(physical_addr)); } } gettimeofday(&stats.end, NULL); display_stats(); /* * Cleanup */ fclose(access_fd); if( NULL != current_ref ) { free(current_ref); current_ref = NULL; } free_ram(); free_page_dir(); free_tlb(); free_cache(); return 0; }