我的操作系统类中有一个项目到期,我应该在其中模拟翻译后备缓冲区。
我正在编写一个将在 TLB 未命中后调用的方法。它应该在 TLB 中找到下一个为空或一段时间未命中的条目,删除该条目,并将其替换为上次调用的页表中的条目。调用该方法时会给出页表条目中的数据。
Void tlb_insert(VPAGE_NUMBER new_vpage, PAGEFRAME_NUMBER new_pframe, BOOL new_mbit, BOOL new_rbit)
{
// Starting at the clock_hand'th entry, find first entry to
// evict with either valid bit = 0 or the R bit = 0. If there
// is no such entry, then just evict the entry pointed to by
// the clock hand.
int m;
int evct = clock_hand;
for (m = clock_hand; m < (num_tlb_entries); m++){
if (tlb[m].vbit_and_vpage & VBIT_MASK == 0 || tlb[m].mr_pframe & RBIT_MASK == 0){
evct = m;
break;
}
}
// Then, if the entry to evict has a valid bit = 1,
// write the M and R bits of the of entry back to the M and R
// bitmaps, respectively, in the MMU (see mmu_modify_rbit_bitmap, etc.
// in mmu.h)
if (tlb[evct].vbit_and_vpage & VBIT_MASK == 1){
PAGEFRAME_NUMBER pfr = tlb[evct].mr_pframe & PFRAME_MASK;
int val1 = tlb[evct].mr_pframe & RBIT_MASK;
int val2 = tlb[evct].mr_pframe & MBIT_MASK;
mmu_modify_rbit_bitmap (pfr, val1);
mmu_modify_mbit_bitmap(pfr, val2);
}
// Then, insert the new vpage, pageframe, M bit, and R bit into the
// TLB entry that was just found (and possibly evicted).
tlb[evct].vbit_and_vpage = VBIT_MASK | new_vpage;
tlb[evct].mr_pframe = new_mbit | (new_rbit | new_pframe);
// Finally, set clock_hand to point to the next entry after the
// entry found above.
clock_hand = evct + 1;
}
//Writes the M & R bits in the each valid TLB
//entry back to the M & R MMU bitmaps.
void tlb_write_back()
{
int n;
for (n = 0; n < num_tlb_entries; n++){
if (tlb[n].vbit_and_vpage & VBIT_MASK == 1){
PAGEFRAME_NUMBER pfr = tlb[n].mr_pframe & PFRAME_MASK;
int val1 = tlb[n].mr_pframe & RBIT_MASK;
int val2 = tlb[n].mr_pframe & MBIT_MASK;
mmu_modify_rbit_bitmap (pfr, val1);
mmu_modify_mbit_bitmap(pfr, val2);
}
}
}
我从以下几行中得到一个段错误:
tlb[evct].vbit_and_vpage = VBIT_MASK | new_vpage;
tlb[evct].mr_pframe = new_mbit | (new_rbit | new_pframe);
VBIT_MASK 是一个先前定义的变量,用于屏蔽我现在要插入的位。我不确定我是否误解了如何使用位掩码,或者我的代码是否存在更严重的问题。我意识到要求任何人详细了解整个事情太过分了,但是如果有人对我应该考虑的解决这个问题的方向有任何建议,我将不胜感激!