placement allocation

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
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

placement allocation

Post by mariuszp »

I looked at JamesM's code for the placement allocator and noticed that when we want the physical address, then before actually increasing the placement address, JamesM used the following code:

Code: Select all

if (phys)
{
    *phys = placement_address;
};
I just wondered, how does this code make sure the address is physical? It's more like storing a copy of placement_address before it is updated to allocate memory. I just don't get it. How does this make the address physical :?:

EDIT: So, basically, the *phys will become the return value of the function. It doesn't make sense!!!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: placement allocation

Post by Solar »

Since I never did read JamesM's code, and you didn't provide a link, I googled for it and came up with this page. I am assuming this is what you are talking about.

Right before the code is presented, I find this (emphasis mine):
JamesM wrote:Now, unfortunately, we have one more requirement, and I can't really explain to you why it is required until later in the tutorials. It has to do with when we clone a page directory (when fork()ing processes). At this point, paging will be fully enabled, and 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. Take it on faith for now - it's not much code anyway.
Did you actually continue on faith until the "later" he refers to, and still don't understand it - or did you lack faith? :wink:

Taking code out of the midst of some tutorial without actually working through the tutorial can be quite confusing sometimes, because you miss out on previous and later explanations on why the author did things the way he did. TIMTOWTDI - There is (always) more than one way to do it.
Every good solution is obvious once you've found it.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: placement allocation

Post by mariuszp »

Solar wrote:Since I never did read JamesM's code, and you didn't provide a link, I googled for it and came up with this page. I am assuming this is what you are talking about.

Right before the code is presented, I find this (emphasis mine):
JamesM wrote:Now, unfortunately, we have one more requirement, and I can't really explain to you why it is required until later in the tutorials. It has to do with when we clone a page directory (when fork()ing processes). At this point, paging will be fully enabled, and 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. Take it on faith for now - it's not much code anyway.
Did you actually continue on faith until the "later" he refers to, and still don't understand it - or did you lack faith? :wink:

Taking code out of the midst of some tutorial without actually working through the tutorial can be quite confusing sometimes, because you miss out on previous and later explanations on why the author did things the way he did. TIMTOWTDI - There is (always) more than one way to do it.
I read the whole tutorial before, and there was no explanation of how it works. I do understnd why we need physical addresses, but not how they are obtained. Storing the return value in a pointer does not make it physical. And since paging is not enabled, and the GDT 'base' for every gate is at 0, the address returned is physical either way.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: placement allocation

Post by gravaera »

Faith is the substance of code hoped for, the evidence of concepts not seen; Have faith my brother, and you will pull through. When you're done having faith in unsuitable material, you can begin reading up on proper memory management from these links:

http://en.wikipedia.org/wiki/Memory_management
http://tldp.org/LDP/tlk/mm/memory.html

And believe...
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: placement allocation

Post by Solar »

mariuszp wrote:I read the whole tutorial before, and there was no explanation of how it works.
I freely admit I've never read it before, and that I'm just using my cross-reading abilities to point out likely points of explanation.

The "take it on faith" part of the tutorial seems to reference Chapter 9.2 Cloning an address space. Is there anything in that chapter that sheds some light on your problem?

(Sorry, awfully tight on time myself, so I can't really read / think this through.)
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: placement allocation

Post by JamesM »

I just wondered, how does this code make sure the address is physical?
Because placement_address is only used in the pre-paging stages, it has to be physical by definition.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: placement allocation

Post by mariuszp »

JamesM wrote:
I just wondered, how does this code make sure the address is physical?
Because placement_address is only used in the pre-paging stages, it has to be physical by definition.
But if I do:

Code: Select all

// placement allocation still active!
u32int phys;
u32int out;
out = kmalloc_p(1234, &phys);
Then out == phys, so why not just:

Code: Select all

u32int phys;
phys = kmalloc_p(1234);
That's my question.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: placement allocation

Post by NickJohnson »

@berkus: yes, it is. The physical address parameter is redundant now, but becomes useful later.

However, I prefer to use a different method. Instead of making kmalloc give physical addresses for the virtual addresses it allocates, I find it easier to have a separate function (page_phys() in my kernel) that gives the physical address of a given virtual address. It simply uses the page tables to do this, in the same way that the MMU itself does. This lets you get the physical address of any pointer at any time, instead of only at allocation.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: placement allocation

Post by NickJohnson »

I know: it was just a suggestion.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: placement allocation

Post by JamesM »

berkus wrote:It's a tutorial code we're speaking about.
And **** tutorial code at that.

</author>
Post Reply