Memory/paging/malloc

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
Dan

Memory/paging/malloc

Post by Dan »

I thought about writing my memory manager..but there´s a few things I wonder about..
1. I can access 4 gbytes in PM? But what is paging? Is it something I need?
2. When I write a malloc functions... applications that I write must be able to use this.. but how should I make it possible for those applications to communicate with the OS? Should I use software interrupts to allocate memory? Cause I mabye write something like:
pointer *p = malloc(sizeof(pointer)); ... then I need to run a function in the kernel?
prabuinet

RE:Memory/paging/malloc

Post by prabuinet »

>1. I can access 4 gbytes in PM?
Yes. (Do you have physical chips of 4GB in your machine)

>But what is paging?
paging treats memory as blocks (usually 4k blocks)
You can control each block separately with the help of page tables.
ie. (readable)/(writable) and (user program block)/(os block) ...

>Is it something I need?
Not Indispensable. But it is easy to maintain, than your segmentation.
If u plan to implement virtual memory in your os things become more
easy(this is what i think.)

>2. When I write a malloc functions... applications that I write must be able
>  to use this.. but how should I make it possible for those applications to
> communicate with the OS? Should I use software interrupts to allocate memory?
> Cause I mabye write something like:
> pointer *p = malloc(sizeof(pointer)); ... then I need to run a function in
> the kernel?

Not all calls to malloc will call operating system. Malloc is a function within
the userprogram that come from library. Malloc will get a large block from memory and distribute it in pieces. When that large block of memory is used fully then it will call the operating system function again for another large block of memory.
Dan

One more question

Post by Dan »

Aldright thank you..
but how do I call the operating system function from my userprogram? I just mean the tecnique to call a function in a kernel From a userprogram
Cheers
carbonBased

RE:One more question

Post by carbonBased »

Most people would use a call gate, or task gate.  In the case of memory management, etc, I think a task gate is a bit extreme.  I'd go with a call gate.

Cheers,
Jeff
Dan

What is that?

Post by Dan »

Whats a call gate?
GT

RE:What is that?

Post by GT »

A call gate is a GDT or LDT entry that points to a routine in supervisor space that user code could not otherwise call directly.  You don't need to use one, you can instead use a trap gate in the IDT, that's what most operating systems do, although FreeBSD interestingly enough has both a trap gate and a call gate (INT 0x80 and CALL 7:0, respectively).  The advantage of a call gate is that it copies data to the supervisor stack for you.  The advantage of a trap gate is that it's smaller and faster.
Dan

RE:One more thing :)

Post by Dan »

So a trap is only a software interrupt? You know any place where I can learn about call gates, sounds good..thanks!
GT

RE:One more thing :)

Post by GT »

Yeah, a "trap" and a "software interrupt" are basically the same thing.  In the IDT, however, there's a difference between an interrupt gate and a trap gate -- an interrupt gate disables interrupts before calling your routine, whereas a trap gate leaves interrupts enabled (which is usually what you want -- definately if you want your kernel to be preemptible).  Call gates likewise leave interrupts enabled.

As for where to learn details about them, there are several massive PDF files you can download from Intel's site, specifically the IA-32 Intel Architecture Software Developer's Manual.  Download all three volumes, and you may even want to consider printing and binding Volume 3: System Programming Guide, if you have a decent laser printer, the ability to print double sided, and a fresh stack of paper (my copy is 688 pages, and this was from three years ago -- the latest is probably a few pages longer to cover new features like Hyperthreading and such).  Vol 3 covers all the fun stuff you want to know as an operating systems implementor.
Post Reply