Page 1 of 2

[Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 6:42 am
by LegendDairy
Hi,
Normally I always use Qemu, because I'm used to it, and because when I start Bochs on Ubuntu I get vga image directive malformed, and I don't know how to work with Bochs, on Ubuntu. But now something strange happend with qemu:

I was working on multithreading, after a while I got it to work but, when the list of threads reached its end and switched back to the main thread, I got a General Protection Fault, first I checked my code, but I couldn't see something that could have caused it, so I started Bochs,(after a reboot into Windows, because I can't get it to work in Ubuntu :c ), but strangely everything ran perfect...

So, could it be a bug in Qemu, or did I do something wrong? Is there a way to fix it, because I prefer Qemu over Bochs.
Also, what could I do to get Bochs working on Ubuntu?(Now I get "Panic: vgarom directive malformed"(or something similar).

If you wish to try it for yourself:

http://www.megaupload.com/?d=JD2KW0CT

Or I you wish to examine my threading code:

Code: Select all

unsigned int current_thid = 0;
thread_t *current_thread;
thread_t *ready_queue;

void init_threading (void)
{
	dissable();
	current_thread = ready_queue = (thread_t*)kmalloc(sizeof (thread_t));
	
	current_thread->regs.eip = 0;
	current_thread->regs.edi = 0;
	current_thread->regs.esi = 0;
	current_thread->regs.ebx = 0;
	current_thread->regs.edx = 0;
	current_thread->regs.ecx = 0;

    
    current_thread->thid = current_thid;
    current_thread->name = "Main";
    current_thread->next = 0;
	enable();
}
void switch_thread( registers_t *regs )
{

	current_thread->regs.edi = regs->edi;
	current_thread->regs.esi = regs->esi;


	current_thread->regs.eax = regs->eax;
	current_thread->regs.ebx = regs->ebx;
	current_thread->regs.ecx = regs->ecx;
	current_thread->regs.edx = regs->edx;

	current_thread->regs.ebp = regs->ebp;
	current_thread->regs.esp = regs->esp;

	current_thread->regs.eip = regs->eip;

    	
current_thread = current_thread->next;
if(!current_thread) current_thread = ready_queue;


	regs->edi = current_thread->regs.edi;
	regs->esi = current_thread->regs.esi;

	//regs->ebp = current_thread->regs.ebp;
	//regs->esp = current_thread->regs.esp;

	regs->eax = current_thread->regs.eax;
	regs->ebx = current_thread->regs.ebx;
	regs->ecx = current_thread->regs.ecx;
	regs->edx = current_thread->regs.edx;

	regs->eip = current_thread->regs.eip;
	

}
int install_thread(tf_t eip, char *name /*, u32int *stack*/ )
{
    dissable();
    current_thid++;
    thread_t *new_thread = (thread_t*)kmalloc(sizeof(thread_t));
    new_thread->regs.eip = eip;
    new_thread->name = name;
    new_thread->thid = current_thid;
    new_thread->next = 0;

	
	/*u32int esp; asm volatile("mov %%esp, %0" : "=r"(esp));
	u32int ebp; asm volatile("mov %%ebp, %0" : "=r"(ebp));
	new_thread->regs.esp = esp;
	new_thread->regs.ebp = ebp;*/
	

    thread_t *tmp = (thread_t*)ready_queue;
    while(tmp->next)
    {
        tmp = tmp->next;
    }
    tmp->next = new_thread;
    enable();
    return current_thid;
}
void timer_handler(registers_t regs)
{
    	// Send reset signal to master. (As well as slave, if necessary).
  	outb(0x20, 0x20);
	ticks++;
	switch_thread( &regs );
}

Code: Select all

[GLOBAL irq0]
irq0:
cli
push byte 0
push byte 32
jmp irq_pit

[EXTERN timer_handler]

irq_pit:
pusha                        ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax

    mov ax, ds               ; Lower 16-bits of eax = ds.
    push eax                 ; save the data segment descriptor

    mov ax, 0x10  ; load the kernel data segment descriptor
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    call timer_handler


    pop ebx        ; reload the original data segment descriptor
    mov ds, bx
    mov es, bx
    mov fs, bx
    mov gs, bx

    popa                     ; Pops edi,esi,ebp...
    add esp, 8     ; Cleans up the pushed error code and pushed ISR number
    sti
    iret           ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 7:39 am
by xenos
Do you set up a stack for each thread somewhere else in your code? It looks to me as if they were all sharing the same stack at the moment, which will be fatal sooner or later.
Legendmythe wrote:Also, what could I do to get Bochs working on Ubuntu?(Now I get "Panic: vgarom directive malformed"(or something similar).
This sounds like I problem I once encountered when I tried to run Bochs on Ubuntu with an old bochsrc file written for the Windows version. The problem was the usage of backslashes (\) as path separators in the "vgaromimage" command - I changed them to ordinary slashes (/) and everything worked fine.

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 7:47 am
by LegendDairy
XenOS wrote:Do you set up a stack for each thread somewhere else in your code? It looks to me as if they were all sharing the same stack at the moment, which will be fatal sooner or later.
Yes, I will implant a personal stack for every thread, I just changed it because I was searching for a solution, I'll change it back.
XenOS wrote: This sounds like I problem I once encountered when I tried to run Bochs on Ubuntu with an old bochsrc file written for the Windows version. The problem was the usage of backslashes (\) as path separators in the "vgaromimage" command - I changed them to ordinary slashes (/) and everything worked fine.
Nope I've got this:

Code: Select all

megs: 32
romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xfffff
vgaromimage: /usr/share/bochs/VGABIOS-elpin-2.40
floppya: 1_44=floppy.img, status=inserted
boot: a
log: bochsout.txt
mouse: enabled=0
clock: sync=realtime
cpu: ips=500000


Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 9:09 am
by xenos
I compared it with my bochsrc file - it seems you are missing a file= in the vgaromimage command (I think this was changed in some Bochs version). Apart from that, the megs command has been superseded by memory:

Code: Select all

memory: guest=32, host=32
romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xfffff
vgaromimage: file=/usr/share/bochs/VGABIOS-elpin-2.40
floppya: 1_44=floppy.img, status=inserted
boot: a
log: bochsout.txt
mouse: enabled=0
clock: sync=realtime
cpu: ips=500000

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 9:42 am
by LegendDairy
XenOS wrote:I compared it with my bochsrc file - it seems you are missing a file= in the vgaromimage command (I think this was changed in some Bochs version). Apart from that, the megs command has been superseded by memory:

Code: Select all

memory: guest=32, host=32
romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xfffff
vgaromimage: file=/usr/share/bochs/VGABIOS-elpin-2.40
floppya: 1_44=floppy.img, status=inserted
boot: a
log: bochsout.txt
mouse: enabled=0
clock: sync=realtime
cpu: ips=500000
I get:
Bochs wrote: ========================================================================
00000000000i[ ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/lib/bochs/plugins'
00000000000i[ ] BXSHARE not set. using compile time default '/usr/share/bochs'
00000000000i[ ] reading configuration from bochsrc.txt
00000000000i[ ] lt_dlhandle is (nil)
00000000000p[ ] >>PANIC<< dlopen failed for module 'x': file not found
========================================================================
Event type: PANIC
Device: [ ]
Message: dlopen failed for module 'x': file not found

A PANIC has occurred.
Here's my bochsrc.txt:

Code: Select all

memory: guest=32, host=32
romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0x00000
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
floppya: 1_44="floppy.img", status=inserted
boot: floppy
log: bochsout.txt
mouse: enabled=0
clock: sync=realtime
cpu: ips=500000
My directory looks like this:
->bochsrc.txt
->floppy.img

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 10:42 am
by Combuster
Message: dlopen failed for module 'x': file not found
How about google? You are missing the bochs-x package

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 12:36 pm
by stlw
Legendmythe wrote:Nope I've got this:

Code: Select all

megs: 32
romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xfffff
vgaromimage: /usr/share/bochs/VGABIOS-elpin-2.40
floppya: 1_44=floppy.img, status=inserted
boot: a
log: bochsout.txt
mouse: enabled=0
clock: sync=realtime
cpu: ips=500000

with ips=500000 you have to be running Bochs on 10-year old cell phone device. Even iphone3GS is capable to run with 12MIPS (24x faster than your setting).
RTFM about IPS, it is very well explained even in .bochsrc example that provided with the sources package.

Stanislav

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 12:45 pm
by LegendDairy
stlw wrote:
Legendmythe wrote:Nope I've got this:

Code: Select all

megs: 32
romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xfffff
vgaromimage: /usr/share/bochs/VGABIOS-elpin-2.40
floppya: 1_44=floppy.img, status=inserted
boot: a
log: bochsout.txt
mouse: enabled=0
clock: sync=realtime
cpu: ips=500000

with ips=500000 you have to be running Bochs on 10-year old cell phone device. Even iphone3GS is capable to run with 12MIPS (24x faster than your setting).
RTFM about IPS, it is very well explained even in .bochsrc example that provided with the sources package.

Stanislav
Dude, it's a beginners hobby kernel, even that 10 year old cellphone has a heavier OS...

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 12:49 pm
by Combuster
Wow, THE Stanislav posting an subliminal insult on an somewhat irrelevant topic and someone returning the favour. That's new :shock:

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 1:03 pm
by LegendDairy
Wow, all very interesting -_- , but please stay on-topic. Does anyone have an idea why it works on Bochs but not on Qemu.

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Mon Aug 08, 2011 1:37 pm
by xenos
Legendmythe wrote:Wow, all very interesting -_- , but please stay on-topic. Does anyone have an idea why it works on Bochs but not on Qemu.
Have you already followed the advice to install the bochs-x package on Ubuntu?

If that doesn't work, download the source, configure with the --with-x11 option, compile and install. It's very simple, and it should solve the error message you posted.

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Tue Aug 09, 2011 11:20 am
by LegendDairy
XenOS wrote:
Legendmythe wrote:Wow, all very interesting -_- , but please stay on-topic. Does anyone have an idea why it works on Bochs but not on Qemu.
Have you already followed the advice to install the bochs-x package on Ubuntu?

If that doesn't work, download the source, configure with the --with-x11 option, compile and install. It's very simple, and it should solve the error message you posted.
It works perfectly thanks, but I still wonder why it won't work on qemu...

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Tue Aug 09, 2011 11:44 am
by Combuster
I still wonder why it won't work on qemu...
I got a General Protection Fault
Well, down to the typical debugging session: a general protection fault has a nice list of causes. Have you tried limiting it down to a faulting instruction? Have you tried interpreting the error code? It should be possible to get a coredump by triplefaulting (try something like deliberately breaking the GPF and DF entries in the IDT by using the null segment selector for the handler)

To be honest, I can't think of anything bochs-qemu specific that's both relevant and a likely explanation. For all I know you might've just been lucky that bochs didn't break on you.

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Tue Aug 09, 2011 11:57 am
by Neolander
My take on the subject is, check your code for bad pointers, and have a closer look at Bochs' output. It is well possible that you're playing with some uninitialized stuff, that just happens to be initialized to the right value through some bochs-specific behaviour (like the zeroed-out uninitialized mem)...

Re: [Help]: Kernel works on BOCHS not on Qemu

Posted: Tue Aug 09, 2011 2:58 pm
by LegendDairy
I don't know if this is important but I found it quit weird:
My fault handlers always give a register dump and in that register dump I found that some registers: EDI, ESI, SS and EAX have value 0, for eax this could be the result of a return value, but I can't think of a function that would be executed at that moment and could return an integer.

Another strange thing is that it claims it is working in a thread without a name / or that that name has been erased.

Quick question: Could it be that at address 0x0 there is nothing in Bochs, but something in Qemu? Because I think that Kmalloc might have returned a NULL pointer.

::EDIT::
Got it, I've adapted my kmalloc function:

Code: Select all

u32int kmalloc_int(u32int sz, int align, u32int *phys)

{

	if (align == 1 && (placement_address & 0xFFFFF000) )

        {

            placement_address &= 0xFFFFF000;

            placement_address += 0x1000;

        }

        if (phys)

        {

            *phys = placement_address + 0x40000000;

        }

        u32int tmp = placement_address;

        placement_address += sz;
	if (tmp == 0) cls(); settextcolour(0xF,0x0); printf("\nERROR: Null Pointer!");
        return tmp;

}
One strange thing though, the screen won't clear, and that function uses memcpy totally independent from any other code...