Understanding James Molloy kmalloc

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
endeavor
Posts: 2
Joined: Wed Dec 14, 2011 1:26 pm

Understanding James Molloy kmalloc

Post by endeavor »

First post. I'll cut to the chase.

I've been reading and follow a combination of Bran's and James Molloy's tutorials for kernel development. I'm currently on James Molloy's tutorial for paging. I understand that cr3 needs a physical address to the page table. What I don't understand is how James Molloy's kmalloc accomplishes giving both a physical and virtual address.

A link to the tutorial is here.

The relevant code is here:

Code: Select all

u32int kmalloc(u32int sz, int align, u32int *phys)
{
  if (align == 1 && (placement_address & 0xFFFFF000)) // If the address is not already page-aligned
  {
    // Align it.
    placement_address &= 0xFFFFF000;
    placement_address += 0x1000;
  }
  if (phys)
  {
    *phys = placement_address;
  }
  u32int tmp = placement_address;
  placement_address += sz;
  return tmp;
}
Unless I have complete lost it, both *phs and tmp should hold the same value. However, according to the tutorial, tmp is a virtual address and *phs is a physical address.

Can someone explain this to me?
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Understanding James Molloy kmalloc

Post by NickJohnson »

Since the tutorial has you writing a lower-half kernel, at least at the beginning (before you do proper physical memory allocation), physical addresses will always be the same as virtual addresses. I think this changes later on.

IMO, JamesM's tutorial makes a bunch of terrible design decisions for the sake of 'simplicity'. I consider it more of a guided tour of kernel components with example implementations instead of something you would actually base a kernel off of.

No offense, JamesM :P
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Understanding James Molloy kmalloc

Post by Brendan »

Hi,
NickJohnson wrote:IMO, JamesM's tutorial makes a bunch of terrible design decisions for the sake of 'simplicity'. I consider it more of a guided tour of kernel components with example implementations instead of something you would actually base a kernel off of.
I'm not even convinced that it's possible to make a tutorial that is something you would actually base a kernel off of. If you tried, it'd be too complex to illustrate the ideas (and you'd need a "complex tutorial tutorial" to explain your tutorial).. ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
endeavor
Posts: 2
Joined: Wed Dec 14, 2011 1:26 pm

Re: Understanding James Molloy kmalloc

Post by endeavor »

Good to know. Looks like my life just became a bit more complicated.
Post Reply