Page 2 of 2

Re:Bootloader - a20 and loading kernel

Posted: Sat Apr 02, 2005 11:05 am
by AR
I already answered that in my above post, you may be able to trick the BIOS into loading it at 1MB but if you need to load more than 64KB then you'll have to load the data into low memory first before copying it to 1MB in unreal/protected mode. Where you put it in low memory doesn't matter as long as it's above 0x400 [IIRC] and below 0xA0000.

Re:Bootloader - a20 and loading kernel

Posted: Sat Apr 02, 2005 11:12 am
by Brendan
Hi,
tm- wrote:What I asked before:
Is it possible to put the kernel over 1MB when I have enabled A20, but not entered pmode? ???
Yes.

There's 2 ways do do it (sort of). The first way is to copy it to 0xFFFF:0x0010 in real mode (or perhaps try to get the BIOS to load it there - not too sure if that'd work on every computer). The problem with this is that your kernel must be small enough to fit in less than 64 KB, and sooner or later it will (probably) become too big and you'll have to find another way!

The second way is to use "unreal mode". The idea here is to switch into protected mode, load some segment registers (so that the limit is 4 GB) and then switch back to real mode WITHOUT resetting the segment limits. This leaves you with real mode, but with 4 GB segment limits rather than 64 KB limits.

The problem here is that the BIOS still won't be able to load data above 1 Mb. To get around this use a buffer below 1 MB. You'd get the BIOS to load data into the buffer, then copy from the buffer to above 1 MB (and then keep loading and copying the next buffer full of data until it's all loaded). This method lets you load heaps of data (until you run out of memory, or hit a "hole" in physical memory - up to 14 Mb of data should be safe on modern computers though).



Cheers,

Brendan

Re:Bootloader - a20 and loading kernel

Posted: Sat Apr 02, 2005 11:18 am
by Brendan
Hi,
AR wrote:Where you put it in low memory doesn't matter as long as it's above 0x400 [IIRC] and below 0xA0000.
I'd be careful with memory below 0x0600 when the BIOS is still expected to work. Most modern BIOS's use an "extended BIOS data area" (EBDA) below 0xA0000 that I wouldn't overwrite either (see http://www.ctyme.com/intr/rb-0598.htm for determining the "safe" top of conventional memory).


Cheers,

Brendan

Re:Bootloader - a20 and loading kernel

Posted: Sat Apr 02, 2005 11:56 am
by tm-
I loaded the kernel to 0x1000:0x0000, it should be free:
"0x0000:0x0500 -> 0x0000:0x7BFF = Free Useable Memory!" (from osfaq - How do I determine the amount of ram?)

Re:Bootloader - a20 and loading kernel

Posted: Sat Apr 02, 2005 12:09 pm
by Ishbosh
Brendan wrote: The second way is to use "unreal mode". The idea here is to switch into protected mode, load some segment registers (so that the limit is 4 GB) and then switch back to real mode WITHOUT resetting the segment limits.
Note, however, that you do need to load the segregs again after you return to real mode.

E.g.:

Code: Select all

push ds
push es
; enter pmode
; extend segments
; leave pmode
pop ds
pop es
I've noticed in my bootsector that getting rid of the pushes and pops causes resets. Also, be sure to have appropriate delays before loading segregs after changing bit one of cr0 (jmp $+2, enabling a20, etc. should do the trick).

Re:Bootloader - a20 and loading kernel

Posted: Sat Apr 02, 2005 1:35 pm
by tm-
OK, now I have created somekind of boot loader. It should do the things in right order now. But next I have to put the kernel to a floppy disk, in a right format.

How do I put the kernel to a floppy disk? What format it should be?

My boot loader: http://pastebin.ca/8680

And kernel:

Code: Select all

void main(void)
{
  int n = 25;
  char *str = "Kernel Loaded";
  while(1)
}

Re:Bootloader - a20 and loading kernel

Posted: Sat Apr 02, 2005 1:41 pm
by Pype.Clicker
preferably flat, binary format. with startup code at offset 0 and pre-located at the address it will be loaded.

Re:Bootloader - a20 and loading kernel

Posted: Sun Apr 03, 2005 5:53 am
by Jerkko
I didn't get it quite yet. If i load my kernel to 0x1000:0x0000 then I have free memory for it about 576 kB (0x1000:0x0000 -> 0xA000:0x0000 is free space) and that's quite small place for kernel. Am I right or is it enough? Before doinng that i have enabled A20-line. Then I switch to PMode. Can I then copy my kernel again to >1MB if I want it to be there? Would it be better to use unreal mode?

Re:Bootloader - a20 and loading kernel

Posted: Sun Apr 03, 2005 8:49 am
by Candy
for a windows kernel with bloat or a linux kernel with all the drivers you could want it would be tight. For your own kernel it should suffice for at least a decade, if properly designed, forever.

Re:Bootloader - a20 and loading kernel

Posted: Sun Apr 03, 2005 10:46 am
by Brendan
Hi,
Candy wrote: for a windows kernel with bloat or a linux kernel with all the drivers you could want it would be tight. For your own kernel it should suffice for at least a decade, if properly designed, forever.
I'd say it may depend on what the kernel would contain - a large monolithic kernel that contains a GUI (with icons, logos, etc) , code to handle a few different file systems, plus device detection code (PCI, USB, etc), and code to handle common hardware (PCI bridges, HIDs, floppy, IDE/ATAPI, etc), and a few CPU options (PAE, SMP, NUMA, etc), all written in a high level language might be larger than you first expect. 576 KB isn't too much in this case, even if most device drivers are loaded as modules..

I expect my micro-kernel (which only contains memory management, multi-CPU/NUMA, PAE, IPC, scheduler and not much else) to be around 64 Kb (probably a bit more). The previous version's micro-kernel was around 60 KB, but was much simpler. All my kernels are written in assembly.

My server's Gentoo Linux kernel is over 5 Mb, with drivers all built in (only what's needed). Of course, this doesn't include any GUI/CLI or anything...


Cheers,

Brendan

Re:Bootloader - a20 and loading kernel

Posted: Sun Apr 03, 2005 2:36 pm
by tm-
The bootloader didn't manage to load the kernel properly :(
OK, there are a lot of things that may be wrong:

1) I would like to put the kernel to the second sector of the floppy disk, just after the bootloader in the first sector. So when I use partcopy to put it there, what parameters should I give it?(like "partcopy kernel.bin 0 200 -f0 <what here??>" )

2) If the kernel is on the second sector of the floppy disk, what value should I put to CL when I call int 0x13 in the bootloader? 0x01 or 0x02??

There might be other bugs in the code too, but I try to fix these first.

And thanks for help! :)

Re:Bootloader - a20 and loading kernel

Posted: Mon Apr 04, 2005 5:18 am
by tm-
I fixed the problem and now everything works. The problem was that interrupts were disabled when I called int 0x13, and then I just enabled them while I call that interrupt, and it worked.

Re:Bootloader - a20 and loading kernel

Posted: Mon Apr 04, 2005 5:46 am
by Pype.Clicker
Brendan wrote: I'd say it may depend on what the kernel would contain - a large monolithic kernel that contains a GUI (with icons, logos, etc) ,
Dude, icons and logos (as well as most fonts) are certainly *not* to be put in the kernel image. Keep them separated files ...
code to handle a few different file systems, plus device detection code (PCI, USB, etc), and code to handle common hardware (PCI bridges, HIDs, floppy, IDE/ATAPI, etc),
With the exception of the "native" filesystem (e.g. the one needed to access system files), all those stuff should be separate device driver.
and a few CPU options (PAE, SMP, NUMA, etc),
better detect them at kernel-load time and pick the appropriate kernel image (e.g. MyOS.ki MyOs-SMP.ki or MyOS-SMP-NUMA.ki)
My server's Gentoo Linux kernel is over 5 Mb, with drivers all built in (only what's needed). Of course, this doesn't include any GUI/CLI or anything...
But it contains fully-featuring IP router with support for multicast, IPv6 and the like ... and it is loaded by GRUB ;)
And if by any chance it contains the nvidia driver aswell (3 or 4MB on its own ... erhm) my vmlinuz is 1.2MB ... and i haven't even tweaked it to make it smaller...


Sorry if i sound rude. that's not the intent ... just a big "c'mon" ... For sure you can built stuff within the kernel up to a point you'll need 64K for the bootloader, but doesn't mind sanity rather advocates to keep the kernel protected from all that bloating stuff ? hmm ?

Shouldn't the kernel contain only what cannot be removed ? (e.g. if you might wish it to be swapped out, that's clearly not a good idea to have it in the kernel)

Re:Bootloader - a20 and loading kernel

Posted: Mon Apr 04, 2005 12:12 pm
by Brendan
Hi,
Pype.Clicker wrote:With the exception of the "native" filesystem (e.g. the one needed to access system files), all those stuff should be separate device driver.
The native file system code would be useless without an IDE hard disk driver, where the IDE controller is probably going to be a PCI device.
Pype.Clicker wrote:Sorry if i sound rude. that's not the intent ... just a big "c'mon" ... For sure you can built stuff within the kernel up to a point you'll need 64K for the bootloader, but doesn't mind sanity rather advocates to keep the kernel protected from all that bloating stuff ? hmm ?

Shouldn't the kernel contain only what cannot be removed ? (e.g. if you might wish it to be swapped out, that's clearly not a good idea to have it in the kernel)
I'm just pointing out that the size of the kernel (or rather, the amount of kernel that a boot loader needs to be able to load) will depend on what that kernel is expected to contain. Another example would be a "Java Kernel" that contains a complete JIT compiler..

For my own OS, the kernel doesn't even include it's own initialization code (it's seperated into a "kernel setup driver"). Regardless, for performance reasons (IMHO) the kernel should *not* contain only what cannot be removed (unless the OS is purely for research purposes).

Also, writing a micro-kernel (or smaller) means you need to load some device drivers, file system code, etc into memory before the kernel starts (you need to get device drivers into memory when there's no file system to load them from). In this case the boot loader should load a boot image and/or multiple files (rather than just a kernel). This puts further restrictions on the suitability of using conventional memory only (as implied by the original poster's 576 KB kernel size limit).

In summary: A monolithic kernel may become too large for conventional memory alone, and all files needed to boot an OS that uses a micro-kernel may also be too much for conventional memory only. Therefore the use of "unreal mode" (or GRUB) may be needed. [note: use of the word "may" is deliberate].


Cheers,

Brendan