A few newbie questions (not too stupid, I hope)

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
ngrawlings
Posts: 4
Joined: Sun Apr 15, 2007 1:13 am
Location: Spain
Contact:

A few newbie questions (not too stupid, I hope)

Post by ngrawlings »

Ok now first memory management, I understand this is completely up to the designer to decide but obviously at the same time there are some very wrong things and I might overlook.

As I understand memory before 0x7c00 is used for bios and other system specifics. Now my question on this is, must it be left alone or can it be replaced?

Is it advised to created the stack before kernel or after and what is the most popular size?

On the wiki, in the higher half tutorial it places the stack just after the first half of the kernel and then placed the second half at 0x40000000 which translates to 1Gb.

Basically if you have a moment can you explain to me how you've mapped memory in your own OS and why you chose to do it that way?

Thanks so much
INF1n1t
Member
Member
Posts: 60
Joined: Fri Dec 22, 2006 5:32 pm
Location: Somewhere Down...

Re: A few newbie questions (not too stupid, I hope)

Post by INF1n1t »

ngrawlings wrote:Ok now first memory management, I understand this is completely up to the designer to decide but obviously at the same time there are some very wrong things and I might overlook.

As I understand memory before 0x7c00 is used for bios and other system specifics. Now my question on this is, must it be left alone or can it be replaced?
Well, I think it depends much on the BIOS, himself (yes it is HE actually!). I put my second boot loader at 0x1000 (1000 < 7C00) and it works with my computers (i have two computers).
What do you mean, when you say: "or it can be replaced". What does "memory replacing" means? I don't know and I'll leave this to the others in the forum, who know more than me. EDIT: Now I saw, what do you mean: it must not be replaced. Hm, somewhere in this forum I've read Brendan's post (I think it was his post), where he talks about the BIOS memory map and functions. It happends that you should check what are the free and the reserved zones in the memory. You MUST NOT touch the reserved memory intervals.
Is it advised to created the stack before kernel or after and what is the most popular size?
Hm, depends on the design, but let me think over that: either you put the stack after or before the kernel doesn't matter, unless you don't use kernel-dependent stack allocation. I mean, that you can set your stack to 8 KB after the end of the kernel, so no matter what is the size of the kernel, you will have 8 Kilobytes for your stack.
The most popular size is the size you choose, when you design your operating system. BUT! Sizes you might want to use are in page-boundaries. If you use pages, you MUST use such sizes. For example: 4 KB, 8 KB...
On the wiki, in the higher half tutorial it places the stack just after the first half of the kernel and then placed the second half at 0x40000000 which translates to 1Gb.
Hm, I think they show you how to map the kernel in the higher half of the memory: you put the kernel for example at the 1 MB mark in the physical memory, but you map it to stay in the upper 2 GBs of the virtual memory.
I think, I have problems with Bochs. The biggest one: Bochs hates me!
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:

Re: A few newbie questions (not too stupid, I hope)

Post by Combuster »

ngrawlings wrote:Ok now first memory management, I understand this is completely up to the designer to decide but obviously at the same time there are some very wrong things and I might overlook.

As I understand memory before 0x7c00 is used for bios and other system specifics. Now my question on this is, must it be left alone or can it be replaced?
not all memory before 0x07c00 is reserved. There exists only the IVT which is 1024 bytes, and the BDA which comes directly after that. said 0x01000 is safe enough to store your data.

Once you are in protected mode, the IVT + BDA (0x00000-0x00xxx) and EBDA (0x9xxxx-0x9ffff) need not be of any use and can in that case be overwritten. The bios wont be accessing it once you have taken over control.
Is it advised to created the stack before kernel or after and what is the most popular size?
the first stack is usually defined in some bss section, which is somewhere in the middle of the kernel. Afterwards, the memory manager can be used to create new stacks as necessary.
A common stack size is 4KiB (= the size of one page)
On the wiki, in the higher half tutorial it places the stack just after the first half of the kernel and then placed the second half at 0x40000000 which translates to 1Gb.
If you are referring to Higher Half With GDT, you have most obviously not understood it. It abuses segmentation to fool the processor. The kernel is virtually located at 0xC0000000 (3GiB), and 0x40000000 is added to get the physical address (0xC...+0x4... =0x10..., which is truncated to 0: the start of the address range)
Basically if you have a moment can you explain to me how you've mapped memory in your own OS and why you chose to do it that way?
My kernel is identity mapped to 1MB mark. That means that the appearance of the kernel does not change when paging is disabled or enabled (which happens to be optional in my kernel).
"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
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: A few newbie questions (not too stupid, I hope)

Post by Brendan »

HI,
Combuster wrote:Once you are in protected mode, the IVT + BDA (0x00000-0x00xxx) and EBDA (0x9xxxx-0x9ffff) need not be of any use and can in that case be overwritten. The bios wont be accessing it once you have taken over control.
I'm not so sure about the EBDA.

The BIOS does things without the OS's knowledge using System Management Mode and SMI's (which itself isn't a problem as the SMI handler can/should use SMRAM). When "run-time" ACPI is used most of the things done by the BIOS's SMI handler are done by ACPI instead, but not necessarily everything.

In this case certain pieces of hardware may be shared between the SMI handler and the OS's "ACPI driver". The ACPI specification describes a type of lock that is used by the SMI handler and the ACPI driver to prevent conflicts (i.e. the BIOS and the OS trying to mess with the same hardware at the same time).

This lock (and the structure it's surrounded by) must be accessible by the OS, so it can't live in SMRAM space. It can't live in the ACPI reclaimable area either (as it's meant to recliamed by the OS and used as normal RAM after the ACPI tables have been parsed). I wouldn't expect this lock to be in the "ACPI NVRAM" area either, as I think this area is used to store data for suspend and hybernate. The only place I can think of is the EBDA.

Therefore I'd treat the EBDA as a "system area", which is what the BIOS usually reports it as via. "INT 0x15, eax=0xE820".

It should be safe to use the EBDA as normal RAM if you never use ACPI, but I'm being very careful with my wording here - "should be safe" isn't the same as "will be safe" and there's no guarantee that the BIOS's SMI handler doesn't use this area for something in some computers.

In theory, this could be extended to any area of RAM. For example the BIOS could use N KB at the top of RAM for anything it likes and report this area as a "system area" (and reduce the value returned as the "top of RAM" by older BIOS functions) to ensure that the OS doesn't use the RAM.

Basically the only RAM that is safe for an OS to use is RAM that is reported as usable RAM by the BIOS. For backwards compatability purposes you can be reasonably sure that (for 80286 and later) the area from 0x0000000 to 0x0080000 will always be reported as usable RAM, and you can also be reasonably sure that (for 80486 and later) there will be at least 1 MB of RAM from 0x00100000 to 0x001FFFFF.


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.
Post Reply