int getClockFrame(int notme) { int frame; // iterate through rpts (0x2400 - LC3_RPT_END) int maxWrap = 20; for (;maxWrap; cBigHand += 2, cLittleHand = 0) { int i; int rpte1; int upta, upte1; if (cBigHand >= LC3_RPT_END) { DEBUG_STATEMENT(DEBUG_MMU,"\nClock is wrapping"); cBigHand = LC3_RPT; maxWrap--; } rpte1 = memory[cBigHand]; if (DEFINED(rpte1) && REFERENCED(rpte1)) { // clear reference DEBUG_STATEMENT(DEBUG_MMU,"\nclearing ref for rpte"); memory[cBigHand] = rpte1 = CLEAR_REF(rpte1); } else if (DEFINED(rpte1)) { // if one is non-referenced go to the user page table // scout out the upt!! note that this has not been referenced if (DEBUG_MMU) { outPTE("\nRPT entry being checked - ", cBigHand); } upta = (FRAME(rpte1)<<6); for (i = cLittleHand % 64; i < 64;i += 2, cLittleHand = i % 64) { // - iterate over userpage table upte1 = memory[upta + (i)]; if (PINNED(upte1) || FRAME(upte1) == notme) { DEBUG_STATEMENT(DEBUG_MMU,"\nupte1 frame was the notme frame (%x = %x) or pinned", FRAME(upte1), notme); if (DEBUG_MMU) { outPTE("UPT entry being checked - ", upta + i); } continue; } if (DEFINED(upte1) && REFERENCED(upte1)) { // - if entry is referenced un-reference and move on // clear reference DEBUG_STATEMENT(DEBUG_MMU,"\nclearing ref for upte %d", cLittleHand); memory[cBigHand] = rpte1 = SET_PINNED(rpte1); memory[upta + (i)] = upte1 = CLEAR_REF(upte1); } else if (DEFINED(upte1)) { // - otherwise prep it for being put into swap // we can use the frame referenced by upte1 if (DEBUG_MMU) { outPTE("UPT entry being checked - ", upta + 1); } DEBUG_STATEMENT(DEBUG_MMU,"\nUpte1 %x, Upte2 %x", upte1, memory[upta + (i + 1)]); memory[cBigHand] = rpte1 = SET_DIRTY(rpte1); frame = FRAME(upte1); swapOutFrame(upta + i); cLittleHand += 2; DEBUG_STATEMENT(DEBUG_MMU,"\nFound a data frame that can be used! (%d, %x)", frame, frame); return frame; } } cLittleHand = 0; if (!REFERENCED(rpte1) && !PINNED(rpte1) && FRAME(rpte1) != notme) { // if we only replaced or did nothing to upte entries // we can use the frame referenced by rpte1 frame = FRAME(rpte1); DEBUG_STATEMENT(DEBUG_MMU,"\nFound a upt frame that can be used! (%d, %x)", frame, frame); swapOutFrame(cBigHand); cBigHand += 2; return frame; } else { // otherwise remove the pin flag memory[cBigHand] = rpte1 = CLEAR_PINNED(rpte1); } } } // when you get the bottom start at the top again DEBUG_STATEMENT(DEBUG_MMU,"\n No valid frame found notme = %d", notme); if (DEBUG_MMU) { displayTableHierarchy(); } return -1; }
int clockRunner(int notme) { //extern int curRPT; //extern TCB tcb[MAX_TASKS]; int i,j,k; int rpta, rpte1, rpte2; int upta, upte1, upte2; int victimFrame; int tupta, tupte1; //printf("\nClock running...%d\n",clockCount++); //victimEntry = 0; while (1) { //rpta = tcb[curRPT].RPT; for (i = curRPTE; i < 0x3000; i+=2) { //rpte1 = memory[rpta + i]; //rpte2 = memory[rpta + i + 1]; rpte1 = memory[i]; rpte2 = memory[i + 1]; if (DEFINED(rpte1)) { if (curUPTE == 0) { if(PINNED(rpte1)) { //memory[rpta + i] = rpte1 = CLEAR_PINNED(rpte1); memory[i] = rpte1 = CLEAR_PINNED(rpte1); } } upta = (FRAME(rpte1)<<6); for (j = curUPTE; j < 64; j+=2) { upte1 = memory[upta + j]; upte2 = memory[upta + j + 1]; if (DEFINED(upte1)) { //memory[rpta + i] = rpte1 = SET_PINNED(rpte1); memory[i] = rpte1 = SET_PINNED(rpte1); if (REFERENCED(upte1)) { memory[upta + j] = upte1 = CLEAR_REF(upte1); } else { //victimEntry = upte1; // if end of UPT, reset upte and move to next UPT //break; // because this is the victim // instead of break, do swapping and return victim frame number // immunity from notme //if (FRAME(upte1) != notme) //{ if (DIRTY(upte1)) { int page; //stat pageWrites++; if(PAGED(upte2)) { page = accessPage(SWAPPAGE(upte2),FRAME(upte1),PAGE_OLD_WRITE); } else { page = accessPage(0,FRAME(upte1),PAGE_NEW_WRITE); } upte2 = page; memory[upta + j + 1] = upte2 = SET_PAGED(upte2); } // save frame number before obliterating first word victimFrame = FRAME(upte1); //memory[upta + j] = upte1 = CLEAR_DEFINED(upte1); memory[upta + j] = upte1 = 0; curUPTE = (j + 2) % 64; //printf("\nChose a UPT victim: %d\n",victimFrame); return victimFrame; //} } } } // immunity from notme if ((FRAME(rpte1) != notme) && (!PINNED(rpte1))) { //victimEntry = rpte1; // if end of RPT, reset rpte and move to next RPT //break; // because this is the victim // instead of break, do swapping and return victim frame number //victimEntry = FRAME(rpte1); // sanity check tupta = (FRAME(rpte1)<<6); for (k = 0; k < 64; k+=2) { tupte1 = memory[tupta + k]; if (DEFINED(tupte1)) printf("\nEvicting a UPT that has defined entries!!!\n"); } //if (DIRTY(rpte1)) { int page; //stat pageWrites++; if(PAGED(rpte2)) { page = accessPage(SWAPPAGE(rpte2),FRAME(rpte1),PAGE_OLD_WRITE); } else { page = accessPage(0,FRAME(rpte1),PAGE_NEW_WRITE); } rpte2 = page; //memory[rpta + i + 1] = rpte2 = SET_PAGED(rpte2); memory[i + 1] = rpte2 = SET_PAGED(rpte2); } // save frame number before obliterating first word victimFrame = FRAME(rpte1); //memory[rpta + i] = rpte1 = CLEAR_DEFINED(rpte1); //memory[rpta + i] = rpte1 = 0; memory[i] = rpte1 = 0; //curRPTE = (i + 2) % 64; //printf("\nChose a RPT victim: %d\n",victimFrame); return victimFrame; } curUPTE = 0; } } //curRPT = (curRPT + 1) % MAX_TASKS; curRPTE = 0x2400; } }