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.
I know it is old, but does it work with a modern setup? I'm using Bochs 2.7 and it seems to just instantly reboot on enabling paging. In particular, this:
which enables paging by setting the PG flag to 1, seems to cause it to reboot. I've tried to look at the code itself, but it seems correct to me.
I'm just curious if this is something related to my setup, if there's some quirk with Bochs that needs to be enabled or disabled to make this work, or if the problem is actually with JamesMolly's example code.
Yeah, I know, but if you actually read the page you're linking, you'd find that the paging code isn't listed as not working, just that there's some obvious room for improvement, like no need to always re-enable paging.
A reboot is probably a triple fault, which occurs when the double fault handler can't be accessed, which occurs when an exception handler (probably the page fault handler in this case) can't be accessed.
Make sure you're mapping your interrupt handlers into the address space. Looking at the JamesMolloy code, it seems that it only identity pages up to placement_address, so I don't know how far that is in your code, but make sure that's above where you put your IDT.
I'd recommend setting up handlers for all exceptions that just panic by default, and print out as much information about the exception as they can. For a page fault this would be things like the address of the fault, the address of the code that caused the fault, whether it was a read or a write, etc.
It seems like the JamesMolloy code maps everything as accessible to user-mode, which can actually be a pretty big security issue later down the line, so once you've fixed whatever is causing your issue, fix that before you go too much further. I think the JamesMolloy tutorial relies on that for switching to user-mode, so you'll have to rethink how to do that yourself.
Thanks,
Barry
Last edited by Barry on Wed Jun 15, 2022 11:02 am, edited 1 time in total.
TBH, the James Molloy tutorial is the worst tutorial on OSDev I've encountered. If you want a tutorial, the "better" tutorial is this one: http://www.brokenthorn.com/Resources/OSDevIndex.html
It's written for a Windows host tough, so you'll have to adapt it.
Also, try to take time to understand the concepts. It's the only way you'll every be able to debug. Tutorials tend to leave some corners untouched, so be careful!
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Er, probably should respond to this one. Never, under any condition, copy and paste paging code from any source. This is for your benefit. It simply does not work and never does. Learn and understand different ways to implement paging (recursive mapping vs temporary mapping vs full mapping in 64 bit) and how the pieces work together for a complete memory hierarchy. I use recursive mapping but if targeting 64 bit mapping all of the physical address space into the VAS is a lot easier.
With that said, if enabling paging causes a triple fault then the paging structures in memory are invalid. If you want to find out why, you'll have to use a debugger -- bochs has a lot of useful commands for looking at the paging structures.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Thanks for the help guys. I don't intend to just copy James' code, but it is nice to have an example that actually works, speeds up the understanding, at least for me.
I eventually realized that James appears to forget to set the new pages to zero, so all the bits in each entry are toggled in random ways.
Setting the memory to zero in get_page (when making a new page) and initialise_paging for the kernel directory, made the paging code work. It seems like he actually does say to do this in the example code on the website, but in the downloaded code, this is omitted. Weird.