<div>On Tue, Apr 17, 2012 at 3:46 AM, Pranay Kumar Srivastava <span dir="ltr"><<a href="mailto:Pranay.Shrivastava@hcl.com">Pranay.Shrivastava@hcl.com</a>></span> wrote:<br></div><div class="gmail_quote"><blockquote style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid" class="gmail_quote">
<div class="HOEnZb"><div class="h5"><br>
<br>
> -----Original Message-----<br>
> From: Vaibhav Jain [mailto:<a href="mailto:vjoss197@gmail.com">vjoss197@gmail.com</a>]<br>
> Sent: Tuesday, April 17, 2012 4:07 PM<br>
> To: Pranay Kumar Srivastava<br>
> Cc: <a href="mailto:kernelnewbies@kernelnewbies.org">kernelnewbies@kernelnewbies.org</a><br>
> Subject: Re: identity mapped paging (Vaibhav Jain)<br>
><br>
><br>
> On Fri, Apr 13, 2012 at 2:15 AM, Vaibhav Jain <<a href="mailto:vjoss197@gmail.com">vjoss197@gmail.com</a>><br>
> wrote:<br>
><br>
><br>
> > I am not clear about the use of identity mapped paging while paging<br>
> is<br>
> > being enabled by the operating system. Also I don't understand at<br>
> what<br>
> > point are the<br>
> > identity mappings no longer useful.According to this article<br>
> > <a href="http://geezer.osdevbrasil.net/osd/mem/index.htm#identity" target="_blank">http://geezer.osdevbrasil.net/osd/mem/index.htm#identity</a> - "The page<br>
> > table<br>
> > entries used to identity-map kernel memory can be deleted once paging<br>
> > and<br>
> > virtual addresses are enabled." Can somebody please explain?<br>
> ><br>
><br>
> Identity mapping is when VA(Virt Address)=PA(Physical address).<br>
><br>
> So basically when you set up your page tables you need to make sure<br>
> they map identically. This is very easily done if you consider each 4KB<br>
> block as a page beginning from location 0 upto whatever you've found to<br>
> be the highest memory available either thru BIOS or GRUB.<br>
><br>
> Remember that while setting up your PTEs and PDE every address is a<br>
> physical one. So if you thought that your kernel would be linked<br>
> initially to a higher VA since you would remap it to a lower memory<br>
> physically then that would be WRONG!. Without PTEs and PDEs installed<br>
> don't do that!.<br>
><br>
> Why would you want it? Well for a simple reason, when your kernel<br>
> starts to boot there's no translator,(No PTEs/PDEs and the Paging<br>
> Enabled bit of processor is also cleared AFAIK just after the BIOS is<br>
> done), yet since you've not enabled your processor for that but you'll<br>
> be doing that in a moment.<br>
><br>
> So let's say you made your kernel to be linked to higher VA like 3Gigs.<br>
> Now the addresses would be generated beginning 3Gigs however you still<br>
> don't have the Page tables installed since your kernel just started. So<br>
> in that case the address is the physical address. And if you've not<br>
> loaded your kernel beginning 3Gigs then it would definitely come<br>
> crashing down.<br>
><br>
> To avoid the crash in case you made your kernel to link to higher half<br>
> of the memory, you can use GDT trick since segmentation is always on<br>
> and you can make the overflow of the address addition to translate to a<br>
> lower physical memory even if paging is not enabled yet. Thus it is<br>
> possible to load the kernel at lower memory addresses while the linkage<br>
> would be for higher VMA. And once your PTEs/PGD are enabled then you<br>
> can use those instead of the GDT trick.<br>
><br>
> Here's a link to that <a href="http://wiki.osdev.org/Higher_Half_With_GDT" target="_blank">http://wiki.osdev.org/Higher_Half_With_GDT</a><br>
><br>
> > Thanks<br>
> > Vaibhav Jain<br>
><br>
> Hi,<br>
><br>
> Thanks for replying but I am still confused. I continued reading about<br>
> this thing and what<br>
> I have understood is the following :<br>
> After the kernel executes the instruction to enable paging the<br>
> instruction pointer will contain the<br>
> address of the next instruction which will now be treated as a virtual<br>
> address. So for the next instruction to be executed<br>
> the page table should map this address to itself.<br>
> Please correct me if I am wrong.<br>
> I am confused by the point about linking the kernel to higher address.<br>
> Could you please put that in a step by step manner<br>
> to make it clear what happens before paging is enabled and what<br>
> happens after that.<br>
> Also, please explain at what point during the execution of kernel code<br>
> are the identity-mapped addresses no longer useful ?<br>
><br>
><br>
><br>
><br>
> Thanks<br>
> Vaibhav<br>
> Hi,<br>
><br>
> I am somewhat understanding your point. But I have some other queries<br>
> now in my mind.<br>
><br>
> If the kernel is linked to 3Gigs is there a way other than the GDT<br>
> trick.?<br>
<br>
</div></div>Make your load address = VA when you link so you won't have to worry about doing the GDT trick.<br>
<div class="im"><br>
><br>
> In fact I am wondering that if the kernel is linked to 3Gigs and Grub<br>
> loads it at 1MB physical, how will even the first instruction of kernel<br>
> execute ? I mean if all the address generated by kernel are above 3<br>
> Gigs and paging is not enabled how will it start<br>
> running ?<br>
<br>
</div>That's what the GDT trick is for. If you read the intel/amd processor manuals the segmentation is always on. So when the address get generated your segment's base address is still added to the generated address before it is put on wire. You can add a constant offset (in your GDT's base address part) to the generated address to get the address beginning from the load address of your kernel.<br>
<br>
I would suggest you make the higher half kernel later and try to first create some code that can fragment your available memory into pages and store this information so you'll know what all pages are there. Next would be to do identity mapping, since your kernel VMA=LMA in your linker script this would be easier to do.<br>
<br>
When you get that paging enabled you can move on to higher half kernel. I would suggest you to work on page replacement algos and virtual memory management code side by side for better integration with paging in later stages.<br>
<br>
Maybe you can post your code if you are allowed to then I can have a look at it.<br>
<br>
><br>
> Thanks<br>
> Vaibhav Jain<br>
<div class="HOEnZb"><div class="h5"><br>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------</div></div></blockquote></div><div><br> </div><div><div>Thanks for the explanation!! Please confirm what I have concluded :</div>
<div> </div><div>- If the kernel is linked to the same address it is loaded at , identiy-mapping is required so that all the addresses generated by the kernel map to correct memory.</div><div> </div><div>- If the kernel is NOT loaded at the same address it is linked to (for e.g. linked to 3 Gigs and loaded at 1MB) identity mapping is not required.</div>
<div> </div><div>- if the kernel is linked AND loaded to 1MB, then to move to higher half kernel two types of page mappings will be required - </div><div> </div><div>a) identity-mapping of lower addresses</div><div>b) mapping all the virutal addresses above 3Gigs to lower adresses</div>
<div> </div><div> </div><div> </div><div>Actually I am going through a tutorial to write a kernel. In the tutorial the linker script sets the location counter to 1MB and the code is working fine.</div><div>But I looked at some other tutorials and osdev articles which mention about higher half kernel and linking to 3Gigs and got confused.So I wanted to </div>
<div>be clear about the concepts before I make any changes to the code.</div><div> </div><div> </div><div> Thanks</div><div>Vaibhav Jain</div></div><div> </div><div> </div>