Brendan wrote:Stacks can also be done where the "next" field for the linked list is inside the free page itself, and the free pages still don't need to be mapped into any virtual address space.
I am at the point where I am going to allocate the frame pointed to by topOfStack, so I need to pop the next address. To do that, I need to map the physical address pointed to by topOfStack into virtual memory so I can read the address stored there, which will become the new topOfStack value. My question concerns how to get a free virtual memory page to map the physical address to.Brendan wrote:Kernel has a "physical address for the page on the top of the stack" variable. To free a page at a virtual address:To allocate a page at a virtual address:
- kernel stores the current "top of stack" in the page (which is still mapped)
- kernel unmaps the page and flushes the TLB for that page (INVLPG)
- kernel sets the "top of stack" to the physical address from the page table
- kernel maps the "top of stack" page into the page table
- kernel flushes the TLB for that page (INVLPG)
- kernel gets the "next" field from the (now mapped) page and puts stores it in it's "top of stack" variable for next time
At first I thought it would be appropriate to use the kernel heap to allocate a page aligned address, but then I realized any address the heap would return would be already mapped to a physical frame, and that frame would be lost if I remapped it another. I realized that I had no mechanism for allocating virtual memory addresses beyond those managed by the heap.
My next thought was to just search the page directory for a null entry. A proper algorithm should take no more than 1024 + 1024 iterations to complete. I could use one of the unused bits in the page directory entries to store whether the page table, if present, was full. Then I would just iterate over the page directory until I found either an empty table or preferably a page table with a free page left, then iterate through the page table until I found a free page table entry, and return the appropriate address as a free virtual memory address where the frame could be mapped. I wonder if there are no free existing page tables and I must create one using the heap, but the heap is full and thus needs an addition page and frame, then I will be deadlocked and have to panic. That seems avoidable...
Is that the proper way to do this? Is there a better way?