Re: Page Frame Allocation
Posted: Sun Mar 14, 2021 3:23 am
...
The Place to Start for Operating System Developers
http://f.osdev.org/
You could clear page frames on pfree().ngx wrote: And should the frames be zeroes on pfree(), because otherwise I think it is a security risk that exposes the memory contents of previous user?
Well, I do my real hardware testing on a 10+ year old laptop, and initializing 4GB of RAM as a linked list slows down the boot by at most about half a second. It does require more reads and writes than a bitmap, including updating the pointer to the head of the list. Some detailed testing would need to be done to determine if the speed of allocating frames by popping the first item off the stack makes up for the increased memory activity.ngx wrote:I like the algorithm of managing pages with a linked list that acts like a stack. But the problem I have with it is that I think it will be extremely slow to initialize(it is only theory I haven't conducted any tests) if I run my OS on a real PC(oldest PC I can find has 8GB of RAM) or anything with like a GB of RAM or more, am I wrong somewhere? - Inserting address of next free frame in to each frame in the system would take a LONG time.
LONG time is relative. To a GHz CPU, yes, you'll have to write on the order of 20,000,000 words, all of which will probably result in a cache miss, and in CPU terms, it will take an age.ngx wrote: I like the algorithm of managing pages with a linked list that acts like a stack. But the problem I have with it is that I think it will be extremely slow to initialize(it is only theory I haven't conducted any tests) if I run my OS on a real PC(oldest PC I can find has 8GB of RAM) or anything with like a GB of RAM or more, am I wrong somewhere? - Inserting address of next free frame in to each frame in the system would take a LONG time.
LOL, ha, welcome to C and systems programming.ngx wrote: Another problem I have is that what if a free frame is freed - then it will go into the linked list twice(or even more times) and could possibly get used by two different programs at the same time causing serious problems?
That's also another argument for a hybrid system.ngx wrote: Also - how do I allocate several consecutive frames(for huge pages...)?
Ask yourself why you particularly want contiguous allocations.ngx wrote: If the boot time is fast at even 4GB of RAM then it is pretty good and pushes me even more to using this algorithm. But still I have can't think of a way of giving continuous frames, all other things are very good - like requiring no additional memory which also would need to be mapped to virtual memory
Well, it is the kernel's responsibility to manage this. Nothing can or should allocate memory if you don't give it to them.ngx wrote:Actually I agree with you here and probably I would go with this algorithm and just forget about huge pages for now. But the thing that I still think about is how would I stop free pages from being freed - I thought of maybe having some sort of checksum or something like that, but I don't know how to implement it
My own VM implementation, all page references are wrapped by a vmpage_t structure, which contains:ngx wrote:Actually I agree with you here and probably I would go with this algorithm and just forget about huge pages for now. But the thing that I still think about is how would I stop free pages from being freed - I thought of maybe having some sort of checksum or something like that, but I don't know how to implement itthewrongchristian wrote: I'm not sure it's worth optimizing for user cases you're unlikely to need in the short or medium term. First, get it working. Then get it working fast.
No, because the linked list will track regions instead of individual pages. You might need to split or merge regions as drivers start and stop, but it will usually be smaller than a bitmap covering the same range of addresses. You won't track ordinary memory allocations in the linked list, only MMIO. (But you might still add ordinary memory to the linked list, flagged as "not MMIO" so drivers can't try to access it.)ngx wrote:Isn't the linked list very slow to initialize(in this case)?
Once you have a VMM, you'll tell it to map the physical addresses belonging to the bitmap to a reasonable virtual address.ngx wrote:Also I wanted to ask - how do I make sure that the bitmap is mapped into virtual memory if I just put it in the largest free area, because I don't have any allocator when the OS starts(and I still don't have a VMM at all)?
I use bit 11 to indicate that a page is a duplicate/alias and thus that physical memory should not be freed when the linear page is freed. I use this bit for aliasing but also for MMIO, so if the linear area of the MMIO is freed the physical memory will not be added as free. I use bit 10 for copy-on-write (cow), and when a page has this bit set and a write is attempted, a new copy of the page is allocated and the cow bits are cleared. However, cow requires complicated logic, and so there is a need to manage other things too here. Since I typically won't use fork to create new address spaces, rather launch applications with spawn or directly replace the forked environment with exec(), I don't feel there is a need for extensive reference counting.sj95126 wrote: Well, here's one way. You have three bits (9,10,11) in each page table entry you can use for anything you want. Set those bits accordingly when you create a linear address mapping to a newly allocated frame. When you free an address, check the bits. Is this entry used for shared memory? MMIO? A stack barrier? You have 3 bits and 7 combinations to work with. The eighth (000) means simple allocation, so just return the frame to the free list. If not, consult your secondary source.