Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Hello forum!
I'm new to OS development and now I'm developing my first OS. I've already implemented simple exception handler and now I'm trying to implement paging. I have decided to start with Identity Paging but for all memory because I have to enable paging for future multitasking, but still want to control memory same as before. So I wrote a simple function for Identify Paging. Here is the code:
But... this function is not working properly. Once I write PG bit to CR0, I get a Triple Fault. GDB tells me that my code doesn't even switch to next instruction and instantly resets I also know that page fault happens before reset (CR2 register != 0) but have no idea why. Can someone help me with explanation about what happens here? Or maybe Identify Paging for all memory is not needed and different memory models can be used.
Thank you for your reply.
That isn't right, if you're trying to calculate the highest possible address, which is what you appear to treat the returned value as. From the spec:
Lower memory starts at address 0, and upper memory starts at address 1 megabyte
I.e. the return should be mem_upper + 1MB, the lower memory amount doesn't even enter the equation. However I should add that using these is over-simplistic anyway, you should really use the full memory map (although I guess you could use these values just to get started). Note that:
The value returned for upper memory is maximally the address of the first upper memory hole minus 1 megabyte. It is not guaranteed to be this value.
I.e. mem_upper only goes so far as the first memory hole and might not even go that far, so it may report far less memory than is actually available.
Your page table array is a local variable which means that (a) you're allocating the whole thing on the stack which is probably going to overflow it and (b) it will become invalid once you return from the function. Same issue with the page directory.
Or maybe I don't have to use Identify Paging for all memory. Maybe I can do it only for first n megabytes and use remaining memory for heap and allocating programs and map them only in case of request or page fault.
What do you think is the best memory model?
KrotovOSdev wrote:What do you think is the best memory model?
"Best" is very subjective, but at least in my opinion, don't identity-map anything. Use paging to put your kernel somewhere in the upper half of the virtual address space and dynamically map memory as you need it.
typedef struct {
uint32_t address : 20; // 4 KiB aligned adress of PT
uint16_t avl : 4; // available
uint8_t flags : 8; // 5th bit is available
} __attribute__((packed)) pd_entry_t;
Isn't that back-to-front? The address should be the upper 20 bits IIRC.
Octocontrabass wrote:
KrotovOSdev wrote:What do you think is the best memory model?
"Best" is very subjective, but at least in my opinion, don't identity-map anything. Use paging to put your kernel somewhere in the upper half of the virtual address space and dynamically map memory as you need it.
Thank you for your replies, I did everything that you recommended to do but I still get a Triple Fault.
According to you recommendations, I've deleted my old code and wrote a new one. I started with Setting Up Paging tutorial and everything worked fine until I changed int type of page_directory to my structure. Once I do that, problem returns. I think that array of structures != array of integers with same size. Am I right?