Questions about paging

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
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Questions about paging

Post by gzaloprgm »

Hi everyone!

I'm making a minimal OS in C, based in James M. and Brandon F. tutorials.

I want to add paging to it, so I got some questions.

As far as I know, paging is another way of accessing memory which provides memory protection and virtual address, Am I wrong?

If I want to enable it I'll do the following:
  • Load my kernel in a direction which can be divided by 4096
  • Add padding to all kernel with Align(4096) in the linker file in each section
  • Make a symbol at the end so I know where can I start allocating memory
  • Alloc page_directory and page_table aligned at 4K
  • Set all kernel pages to be read and writeable by the kernel but not user
  • Make kernel virtual address the same linear address
  • Set video framebuffer page/s read and write only by kernel
  • Add a Page fault handler at interrupt 14
  • Send the linear address of page_directory to CR3
  • Set on the paging bit in CR0
Am I missing something? Is something innecesary?

Thanks,

Gonzalo
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:

Post by Combuster »

Load my kernel in a direction which can be divided by 4096
Not strictly necessary. Only saves you a few computations and bytes.
Add padding to all kernel with Align(4096) in the linker file in each section
not necessary either. The only real reason to do this is that pages start on a 4096-byte boundary and you might waste a page if the kernel doesn't start on a boundary, or you want different permissions for code or data parts of the kernel.
Make a symbol at the end so I know where can I start allocating memory
that's for memory management. It's not necessarily part of initializing paging itself, but something you can use in the process. It's handy to have nevertheless as it allows you to dynamically determine how much space to reserve for the kernel
Alloc page_directory and page_table aligned at 4K
if you plan on using 4M pages, you don't necessarily need page tables. Conversely, you might want more than one page table to start with.
Set all kernel pages to be read and writeable by the kernel but not user
See the note above, you may want to map parts (code, readonly data) as not-writable.
Make kernel virtual address the same linear address
The main reason for this is that when paging is enabled the code that is currently being executed doesn't get "changed". For higher-half kernels the virtual address does not match the physical address.
Set video framebuffer page/s read and write only by kernel
Handy to have, but not strictly necessary either. My kernel for one has a different way of doing things.
Add a Page fault handler at interrupt 14
You haven't got a default handler yet?
Send the linear address of page_directory to CR3
it seems to me that you're confusing virtual, linear and physical addresses:

Code: Select all

virtual address -----------> linear address ------------> physical address
                segmentation                   paging
Set on the paging bit in CR0
Obviously :D
"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 ]
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post by gzaloprgm »

Lots of thanks Combuster!

You answered all my questions.
Combuster wrote:
Send the linear address of page_directory to CR3
it seems to me that you're confusing virtual, linear and physical addresses
I meant to send the physical address of it :P
Post Reply