3

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. vfn is the virtual frame number.

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;
}
moejoe
  • 145
  • 2
  • 11
  • 1
    From your calloc call and tbl_search function, it looks like you probably meant `static tbl_entry **tbl;`? Otherwise you want `sizeof(tbl_entry)` in the calloc call and search should be completely different. – Chris Dodd Apr 13 '11 at 17:02
  • @Chris Thank you Chris. You are correct. I changed the calloc call to `sizeof(tbl_entry)`. If I were to use 'tbl_entry **tbl', how would the search be different - and would doing the latter make more sense? – moejoe Apr 13 '11 at 17:11
  • @Chris I have edited the code and updated my question. Any help will be greatly appreciated. – moejoe Apr 14 '11 at 05:11

1 Answers1

2

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. 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.

Another issue might be 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. If that is the case for the getbits() API, you are calculating the index based on the wrong part of the address.

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)

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 i.e. bit 31 is the highest bit. So, if you assume a page size of 4096 bytes, the frame number of an address can be obtained like this:

vfn = getbits(x, 31, 20); // 31 is the top bit, number of bits is 32 - log2(4096)
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • 1
    Thanks for the pointers Jeremy. The code finally compiled! getbits() is just getting n bits from position p via this code `(x >> (p+1-n)) & ~(~0 << n)`. And I am converting logical address `vaddr` to virtual page number using this: `getbits(vaddr, addr_space_bits-1, (addr_space_bits - page_bits))` – moejoe Apr 13 '11 at 17:03
  • @moejoe: Added a bit about `getbits()` – JeremyP Apr 14 '11 at 07:58
  • Jeremy that is exactly what I have `uint vfn = getbits(vaddr, addr_space_bits - 1, vfn_bits)` - in the next step, I am trying to figure out how I can use this setup to do LRU fault handler. I believe its going to be a linked list of `tbl_entry` that I have to use. Perhaps using pointer-to-pointer will pay off there. Any comment on the setup of the page table so far? I will also share how I am planning to add and evict pages from the physical memory. Thank you for your help so far :) – moejoe Apr 14 '11 at 13:37
  • @moejoe: Good luck with that. LRU is hard to do without hardware support. Check out this Wikipedia page for more info on paging algorithms: http://en.wikipedia.org/wiki/Page_replacement_algorithm – JeremyP Apr 14 '11 at 13:56