Page 1 of 1

Problem with PIT

Posted: Mon Sep 19, 2011 4:22 am
by Dumah
I am following Bran's Kernel Development Tutorial. I am using Ubuntu (as a guest os on virtualbox) and using qemu as my emulator to test the code. All's well up until I try to use the Programmable Interval Timer on this part of the tutorial - http://www.osdever.net/bkerndev/Docs/pit.htm

The problem is where I call "timer_phase" - namely the last line of that function (according to gdb):

Code: Select all

outportb(0x40, divisor >> 8);     /* Set high byte of divisor */
Once that code is run, I get a total crash on qemu with the following output on the console
Could not initialize KVM, will disable KVM support
QEMU 0.12.5 monitor - type 'help' for more information
(qemu) qemu: fatal: Trying to execute code outside RAM or ROM at 0x000a0000

EAX=00003100 EBX=00000000 ECX=fff6d54f EDX=000131a0
ESI=001142ed EDI=00101162 EBP=00104eb0 ESP=00104e8c
EIP=0009ff23 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 00105850 00000017
IDT= 00105040 000007ff
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00104e8c CCO=LOGICL
FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000
./start_qemu: line 1: 2564 Aborted qemu -kernel kernel/kernel.bin -monitor stdio
I dont know if not having kvm is the problem. I cant seem to get it going and am wondering if it's because the OS I am using (Ubuntu) is itself a guest os running on Virtualbox (I am using a windows pc in work and use virtualbox to give me linux environment to code in). Again, not sure if this is the real problem or not.

Any suggestions?

Re: Problem with PIT

Posted: Mon Sep 19, 2011 5:12 am
by Chandra
(qemu) qemu: fatal: Trying to execute code outside RAM or ROM at 0x000a0000
That gives me the feeling that you're trying to execute code from the Video RAM(MMIO). You need to check that to be sure.

Re: Problem with PIT

Posted: Mon Sep 19, 2011 5:42 am
by Chandra
berkus wrote:Doesn't look like this is the line, because neither DX nor AL contain the right data.

Code: Select all

EIP=0009ff23
That looked pretty obvious. I'm sure he loaded his kernel somewhere just below the Memory-mapped Video RAM and then when his kernel finally grew in size, execution at 0xA0000 crashed it.

@OP: If this really is the case, you need to load your kernel to somewhere else. Just make sure it's safe enough to deal with whatever size your kernel is.

Re: Problem with PIT

Posted: Mon Sep 19, 2011 6:13 am
by Dumah
Chandra wrote:
berkus wrote:Doesn't look like this is the line, because neither DX nor AL contain the right data.

Code: Select all

EIP=0009ff23
That looked pretty obvious. I'm sure he loaded his kernel somewhere just below the Memory-mapped Video RAM and then when his kernel finally grew in size, execution at 0xA0000 crashed it.

@OP: If this really is the case, you need to load your kernel to somewhere else. Just make sure it's safe enough to deal with whatever size your kernel is.
Here's my linker script
OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}
I'm assuming that's what specifies the load address that grub uses for the load address? :-k
Anything I could change there?

Re: Problem with PIT

Posted: Mon Sep 19, 2011 8:33 am
by Chandra
Since you are booted via GRUB, I'd assume your kernel is loaded at a safe address. Even then, from the dump you provided, your EIP looks definately wrong. And since the CRx register shows that paging is not yet enabled, there should be no issue with mapping. So I end up asking for the source and if possible the floppy image of your OS. That could help perhaps.

Re: Problem with PIT

Posted: Fri Sep 23, 2011 4:10 am
by Dumah
Just an update if anyone reads this thread in the future - the problem (AFAIK) was with qemu. If I boot it with the -kernel command I don't always get the bss zeroed. This might be my installation, but it has been mentioned on the web recently (http://lists.gnu.org/archive/html/qemu- ... 02524.html). I've tried all sorts of load scripts but no real success that lasted very long. Also, loading my kernel on a floppy image with grub doesn't lead to this problem so I think it's qemu when used in this way.

I found that my static irq_table was filled with junk and this was actually causing the crash. Writing some asm code that cleared the bss section fixed things and now I can move on learning more. :D

Thanks to those that replied.