OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 3:40 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Virtual vs physical address in kmalloc
PostPosted: Sun Apr 29, 2018 3:46 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
I am following James Molloy's Kernel tutorial. In it, he has this bit of code for kmalloc:
Code:
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;
}


Accompanied with the description:
Quote:
kmalloc will return a virtual address. But, we also (bear with me, you'll be glad we did later) need to get the physical address of the memory allocated.


I have rewritten the code snippet as follows:
Code:
u32int kmalloc_ ( u32int size, int align, u32int *physical_address )
{
   // Page align the address
   if ( align == 1 && ( placement_address & 0xFFFFF000 ) )
   {
      placement_address &= 0xFFFFF000;
      placement_address += 0x1000;
   }

   u32int base = placement_address;

   // Share physical address
   if ( physical_address )
   {
      *physical_address = base;
   }

   placement_address += size;

   return base;  // how is this different from physical_address?
}


It seems to me that the virtual address returned is the same as the physical address shared. So why make the distinction?


Top
 Profile  
 
 Post subject: Re: Virtual vs physical address in kmalloc
PostPosted: Sun Apr 29, 2018 4:33 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

quadrant wrote:

quadrant wrote:
It seems to me that the virtual address returned is the same as the physical address shared. So why make the distinction?


To be "perfect", a tutorial needs to be simple enough for beginners to learn from but also needs to be complex enough to cover all useful information. These requirements are mutually exclusive (it's impossible for a tutorial to be simple enough and complex enough at the same time) and therefore tutorials must be "bad" in some way; so almost all tutorials aim to be simple enough (and therefore deliberately fail to cover anything complex enough to be useful in practice).

The distinction is made because in a (more complex) real operating system the virtual address would be different to the physical address.


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.


Top
 Profile  
 
 Post subject: Re: Virtual vs physical address in kmalloc
PostPosted: Sun Apr 29, 2018 5:05 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
Ah ok, thanks!


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: DotBot [Bot], Majestic-12 [Bot], SemrushBot [Bot] and 131 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