简体   繁体   English

理解分页虚拟内存的第一步:在初始页面错误时创建页表条目

[英]First Step in Understanding Paged Virtual Memory: Creating Page Table Entry upon Initial Page Fault

I am trying to understand virtual memory paging. 我试图了解虚拟内存分页。 I have the following code snippet that represents the first step in the process. 我有以下代码片段,代表了该过程的第一步。 Here search_tbl is called from the main program for each logical address in order to check if the page table already has an entry that maps the provided logical address to a location in physical memory. 这里,从主程序为每个逻辑地址调用search_tbl ,以检查页表是否已经具有将提供的逻辑地址映射到物理存储器中的位置的条目。 vfn is the virtual frame number. vfn是虚拟帧号。

EDITED: Does this implementation make any sense? 编辑:这个实现是否有意义? Or am I going down the wrong road? 还是我走错了路?

Any help/suggestion would be greatly appreciated. 任何帮助/建议将不胜感激。 Thank you. 谢谢。

uint vfn_bits;//virtual frame number
static tbl_entry **tbl;
uint page_bits = log_2(pagesize);
vfn_bits = addr_space_bits - page_bits;
tbl = calloc(pow_2(vfn_bits), sizeof (tbl_entry*));

tbl_entry *search_tbl(uint vfn) {

    uint index = vfn;
    if (tbl[index] == NULL) {
        /* Initial miss */
        tbl[index] = create_tbl_entry(vfn);
    }
    return tbl[index];
}

tbl_entry *create_tbl_entry(uint vfn) {
    tbl_entry *te;
    te = (tbl_entry*) (malloc(sizeof (tbl_entry)));

    te->vfn = vfn;
    te->pfn = -1;
    te->valid = FALSE;
    te->modified = FALSE;
    te->reference = 0;

    return te;
}

The only real issue I can see with it is that search_tbl() 's return type is tbl_entry* but it is actually returning a tbl_entry . 我能看到的唯一真正的问题是search_tbl()的返回类型是tbl_entry*但它实际上返回了一个tbl_entry That could be a major issue though, thinking about, it if the page table is really an array of pointers to page table entries. 这可能是一个主要问题,考虑一下,如果页表实际上是一个指向页表项的指针数组。 Also, if sizeof(tbl_entry) > sizeof(tbl_entry*) you are not allocating enough space for the table. 此外,如果sizeof(tbl_entry) > sizeof(tbl_entry*)您没有为表分配足够的空间。

Another issue might be getbits() . 另一个问题可能是getbits() It's normal practice to number the bits of an n-bit integer type with 0 as the least significant bit and n - 1 as the most significant bit. 通常的做法是对n位整数类型的位进行编号,其中0表示最低有效位,n - 1表示最高有效位。 If that is the case for the getbits() API, you are calculating the index based on the wrong part of the address. 如果是getbits() API的情况,则根据地址的错误部分计算索引。

Edit 编辑

The above was true for the original version of the code in the question which has been edited out. 对于已经删除的问题中的代码的原始版本,上述情况属实。

As for the getbits question in the comment, if the following is used (assuming 32 bit addresses) 至于评论中的getbits问题,如果使用以下内容(假设32位地址)

uint32_t getbits(uint32_t x, unsigned int p, unsigned int n)
{
    return (x >> (p + 1-n)) & ~(~0 << n);
}

That assumes that the most significant bit is the one with the highest number ie bit 31 is the highest bit. 这假设最高有效位是具有最高位的位,即位31是最高位。 So, if you assume a page size of 4096 bytes, the frame number of an address can be obtained like this: 因此,如果您假设页面大小为4096字节,则可以像下面这样获取地址的帧编号:

vfn = getbits(x, 31, 20); // 31 is the top bit, number of bits is 32 - log2(4096)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 CPU 看到虚拟地址,页面错误处理程序如何用物理地址填充页表条目? - How does page fault handler fill up the page table entry with a physical address if CPU sees virtual addresses? 对象WMA(虚拟内存区域:)和PTE(页表项)之间有什么区别? - What is the difference between objects WMA (Virtual Memory Area:) and PTE (Page Table Entry)? 虚拟内存系统,页表和TLB - Virtual memory system, page table and TLB 释放的内存不会导致页面错误 - Freed memory not causing page fault 第一次将页面加载到物理 memory 会导致重大页面错误吗? - Does loading a page into physical memory for the first time cause a major page fault? 当存在页面错误时,虚拟内存区域结构是否仅进入画面? - Does Virtual Memory area struct only comes into picture when there is a page fault? 是否可以更改虚拟内存页面大小? - Is it possible to change virtual memory page size? 如何在虚拟内存 - Linux 中查看每个进程维护的页表? - How can I see a page-table maintained by each process in Virtual Memory - Linux? 虚拟 memory 管理器使用 TLB 和页表将逻辑地址转换为物理地址 - virtual memory manager convert logical to phyiscal address useing TLB and page table 如何知道指针是在物理内存中还是会触发Page Fault? - How to know whether a pointer is in physical memory or it will trigger a Page Fault?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM