Page 1 of 1
JamesMolloy paging code doesn't work?
Posted: Tue Jun 14, 2022 6:56 pm
by optimisticnugget
Does the paging code in James Molly's tutorial work?
http://www.jamesmolloy.co.uk/tutorial_h ... aging.html
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:
Code: Select all
asm volatile("mov %0, %%cr0":: "r"(cr0));
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.
Re: JamesMolloy paging code doesn't work?
Posted: Tue Jun 14, 2022 7:03 pm
by Octocontrabass
Re: JamesMolloy paging code doesn't work?
Posted: Wed Jun 15, 2022 4:26 am
by optimisticnugget
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.
Re: JamesMolloy paging code doesn't work?
Posted: Wed Jun 15, 2022 6:10 am
by Barry
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
Re: JamesMolloy paging code doesn't work?
Posted: Wed Jun 15, 2022 6:24 am
by nexos
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!
Re: JamesMolloy paging code doesn't work?
Posted: Thu Jun 16, 2022 12:04 am
by neon
Hi,
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.
Re: JamesMolloy paging code doesn't work?
Posted: Wed Jun 22, 2022 12:15 pm
by optimisticnugget
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.