Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

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.
Post Reply
jaswax
Posts: 23
Joined: Sat Jan 19, 2008 11:26 am
Location: Texas

Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by jaswax »

Deleted
Last edited by jaswax on Fri Oct 09, 2009 6:52 pm, edited 1 time in total.
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: Help with setting up paging with ASM

Post by Hangin10 »

You don't need to set anything else in CR3 besides the address of the page directory.
In particular, bits 0 and 1 are reserved. Check the layout of the control registers in the intel manuals (in the slightly older hardcopy I have at hand, Section 2.5 has a nice figure of CR0-4).

A few things about your assembly code:
You can XOR a register with itself to make it zero, smaller instruction, and easier on the eyes. When you are setting entries in the tables, why not start with 0x3? Why shuffle the values around to avoid it, adding 0x1000 to a number never alters the 0x3 at the end.
jaswax
Posts: 23
Joined: Sat Jan 19, 2008 11:26 am
Location: Texas

Re: Help with setting up paging with ASM

Post by jaswax »

Deleted
Last edited by jaswax on Fri Oct 09, 2009 6:53 pm, edited 1 time in total.
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: Help with setting up paging with ASM

Post by Hangin10 »

When you add to EDI to get to the next entry, you're only adding one. These entries are 4 bytes long. Also, you don't necessarily know what is going to be in CR3, so you should really just move the value into a register, and then put that into CR3.

I should have seen that the first time. Shows how well I think in assembly! #-o
Why not just use C? It seems like you would have had things working by now.
jaswax
Posts: 23
Joined: Sat Jan 19, 2008 11:26 am
Location: Texas

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by jaswax »

Deleted
Last edited by jaswax on Fri Oct 09, 2009 6:54 pm, edited 1 time in total.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by geppyfx »

this code appears to be correct to me but I haven't tested it

Code: Select all

     mov   ecx, 1024
   mov   edi, 0x701000        ;address of the page table
   mov   ebx, 3               ;set attributes to present and read/write
.npte:
   mov   [edi], ebx           ;write the data to memory
   add   edi, 4               ;go to next memory location
   add   ebx, 4096
   dec   ecx
   jnz   .npte

  ;make all PDEs not present by default
   cld
   xor   eax, eax
   mov   ecx, 4096/4
   mov   edi, 0x700000
   mov   ebx, edi             ;to save bytes later
   rep   stosd
   
   mov   dword [ebx], 0x701003    ;set 1st PDE to point to PT
   mov   cr3, ebx

   mov   eax, cr0
   or    eax, 0x80000000
   mov   cr0, eax              
jaswax
Posts: 23
Joined: Sat Jan 19, 2008 11:26 am
Location: Texas

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by jaswax »

Deleted
Last edited by jaswax on Fri Oct 09, 2009 6:54 pm, edited 1 time in total.
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by Hangin10 »

If it's outside of any segment, you can't access it.

Any writes (except ones that use ESP/EBP for a base) use the data segment by default.

The GDT needs to be mapped in.
The base address of the GDT in the GDTR is virtual if paging is on.

So my two questions for you are:
1) Is your kernel being identity mapped?
2) Why are all of your segments not base zero and limit 4GB? (I don't know if you are trying to do something that involves a segmented memory model, but just making everything flat is way easier.)
jaswax
Posts: 23
Joined: Sat Jan 19, 2008 11:26 am
Location: Texas

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by jaswax »

Deleted
Last edited by jaswax on Fri Oct 09, 2009 6:54 pm, edited 1 time in total.
stlw
Member
Member
Posts: 357
Joined: Fri Apr 04, 2008 6:43 am
Contact:

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by stlw »

jaswax wrote:I think that I am just going to go back to my little cave and hide.

I really don't understand paging. I understand segmentation and could split memory up into segments until the cows come home.
So where to go from here?

I won't be mad if there are no replies. Thanks to all who tried to help.
You told you using emulator. Are you using Bochs ? So why don't just quote its messages when tripple faulting ?
If you enable debug:report instead of debug:ignore you will get much more verbose paging messaging as well.

Stanislav
jaswax
Posts: 23
Joined: Sat Jan 19, 2008 11:26 am
Location: Texas

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by jaswax »

Deleted
Last edited by jaswax on Fri Oct 09, 2009 6:55 pm, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Setting Up Paging [CR3, Flags, Assembly] [Little Guidance]

Post by Combuster »

Regarding questions 1..4, I really recommend that you look up how virtual memory works (Paging, Segmentation, Intel software developer manual 3A), read some tutorials, then think about what *you* want. It's pretty useless to give suggestions right now when you are unlikely to understand the suggestions made.

For question #5, see the FAQ, although it is a bad idea to think about actually doing that while you haven't grabbed the basics...
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply