OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 9:54 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: JamesMolloy paging code doesn't work?
PostPosted: Tue Jun 14, 2022 6:56 pm 
Offline

Joined: Wed Mar 02, 2022 4:21 pm
Posts: 8
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:
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.


Top
 Profile  
 
 Post subject: Re: JamesMolloy paging code doesn't work?
PostPosted: Tue Jun 14, 2022 7:03 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
There are a lot of problems with the code.


Top
 Profile  
 
 Post subject: Re: JamesMolloy paging code doesn't work?
PostPosted: Wed Jun 15, 2022 4:26 am 
Offline

Joined: Wed Mar 02, 2022 4:21 pm
Posts: 8
Octocontrabass wrote:


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.


Top
 Profile  
 
 Post subject: Re: JamesMolloy paging code doesn't work?
PostPosted: Wed Jun 15, 2022 6:10 am 
Offline
Member
Member

Joined: Mon Aug 27, 2018 12:50 pm
Posts: 54
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.

Top
 Profile  
 
 Post subject: Re: JamesMolloy paging code doesn't work?
PostPosted: Wed Jun 15, 2022 6:24 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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


Top
 Profile  
 
 Post subject: Re: JamesMolloy paging code doesn't work?
PostPosted: Thu Jun 16, 2022 12:04 am 
Offline
Member
Member
User avatar

Joined: Sun Feb 18, 2007 7:28 pm
Posts: 1564
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.

_________________
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}


Top
 Profile  
 
 Post subject: Re: JamesMolloy paging code doesn't work?
PostPosted: Wed Jun 22, 2022 12:15 pm 
Offline

Joined: Wed Mar 02, 2022 4:21 pm
Posts: 8
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot], SemrushBot [Bot] and 65 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group