OSDev.org

The Place to Start for Operating System Developers
It is currently Sun May 05, 2024 6:32 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: SigOPS OS
PostPosted: Thu Jun 12, 2003 3:27 pm 
Hi. I just started looking into OS development a couple weeks ago. I'd been toying with OSD for a while but then moved on to the SigOPS DemOS because I thought the code/libc was much cleaner. Anyway, I've been trying to get interrupts to work. I noticed with DemOS there are no ASM files, I think it's all generated in their bootmaker program... And so all the ASM I need to do has to be inline. I've read about 30 pages of this forum and at least 3-4 tutorials on IDT and remapping the PICs, but I still can't get it to work. Sometimes (after I remap the PICs) when I do asm("sti");, the machine will reboot. Othertimes when I try to unmask the IRQs, it will reboot.

Is anyone familiar with DemOS? It already handles exceptions, and has the ASM for interrupts, but it's not documented at all and I'm having no luck getting it to work.


Top
  
 
 Post subject: Re:SigOPS OS
PostPosted: Thu Jun 12, 2003 9:20 pm 
Okay, there was a bug in the code that was causing the reboots. But now when I try to remap the PICs, I always get a Divide by Zero exception.

Code:
    printf( "Initializing Exceptions: " );
    init_ints();
    printf( "done\n" );

    printf("Remapping: " );
    do_it();
    printf("done\n");


That's in the main body of the kernel. I'm not sure if the remapping should be before or after the exceptions are initialized... I tried it both ways with the exact same results.

Code:
void do_it()
{
    asm("cli");
    remap_pics(0x20, 0x28);
    disable_irq(0);
    enable_irq(1);
    asm("sti");
}


If I comment out the asm("sti"); or the enable_irq(1); in this function, I do not get the Div/0 exception. But it also probably silently breaks it.

Code:
void enable_irq(int irq)
{
    if (irq == 0xFF)
    {
        outb(0x00, PIC1 + 1);
        outb(0x00, PIC2 + 1);
    } else
    {
        irq = irq & (1 << irq);
        if (irq < 8)
            outb(irq & 0xFF, PIC1 + 1);
        else
            outb(irq >> 8, PIC2 + 1);
    }
}

void disable_irq(int irq)
{
    if (irq == 0xFF)
    {
        outb(0xFF, PIC1 + 1);
        outb(0xFF, PIC2 + 1);
    }
    else
    {
        irq = irq | (1 << irq);
        if (irq < 8)
            outb(irq & 0xFF, PIC1 + 1);
        else
            outb(irq >> 8, PIC2 + 1);
    }
}

void remap_pics(int pic1, int pic2)
{

    unsigned char md, sd;
    md = inb(PIC1 + 1);
    sd = inb(PIC2 + 1);

    outb(EOI, PIC1); /* send EOI to master */

    /* send ICW1 */
    outb(0x11, PIC1);
    outb(0x11, PIC2);

    /* send ICW2 */
    outb(pic1, PIC1 + 1);
    outb(pic2, PIC2 + 2);


    /* send ICW3 */
    outb(0x04, PIC1 + 1);
    outb(0x02, PIC2 + 1);


    /* send ICW4 */
    outb(0x01, PIC1 + 1);
    outb(0x01, PIC2 + 1);

    outb(md, PIC1 + 1);
    outb(sd, PIC2 + 1);

}



Does anyone see anything wrong with this? I think it's all correct, but I don't know what else could be causing it.


Top
  
 
 Post subject: Re:SigOPS OS
PostPosted: Fri Jun 13, 2003 2:34 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
you suffer the same ICW4 error as somebody else on this thread

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:SigOPS OS
PostPosted: Fri Jun 13, 2003 3:26 am 
I don't know if that helps me or not.

Here's what it's doing now:

1) I initialize the IDT
At this point, exceptions work. Doing a 1/0 in the code will trigger the Div/0 exception on purpose.

2) I remap the PICs.
I have no idea if the PICs -really- get remapped. Can I tell? The code is up there, though. It looks about the same as every tutorial I've read.

3) I enable the IRQs with enable_irq(0xFC)

4) I do: asm("sti");
As soon as I do this, I immediately get a Div/0 error. I am not certain, but I think it -might- be the timer though. Maybe the remap didn't work? Because if I send EOI back to the Div/0 exception, it keeps doing it over and over endlessly. If I cli, the exception will only execute once.

In the code, if I do an "int 0", it will trigger my Div/0 functions. If I do an "int 20", it will trigger my timer function. I guess that means my IDT works, but why won't the machine automatically call my routines? :( My timer never fires unless I manually trigger it.


Top
  
 
 Post subject: Re:SigOPS OS
PostPosted: Fri Jun 13, 2003 7:23 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
indeed, it seems you're messed up things in the PIC remapping function.

here's the code i use ...

Code:
        mov al,11h
        out 20h,al      ;ICW1:=__init__         ;master
        out 0A0h,al     ;ICW1:=__init__         ;slave
        call __wait
        mov al,bl
        out 21h,al      ;ICW2:=irq0slot
        mov al,bh
        out 0A1h,al     ;ICW2:=irq8slot
        call __wait
        mov al,4        ;bit 2 set => master recieves on irq 2
        out 21h,al      ;ICW3:=IRQ2 (Attach)
        mov al,2        ;value=2 => slave sends to irq 2
        out 0A1h,al     ;ICW3:=IRQ2 (Attach)
        call __wait
        mov al,0xd
        out 21h,al      ;ICW4:=IntelEnvironnement
        mov al,9
        out 0A1h,al     ;ICW4:=IntelEnvironnement
        call __wait
        pop edx
        pop eax
        ret
__wait:

        push ecx
        mov ecx,4096
.here:  loop .here
        pop ecx
        ret

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:SigOPS OS
PostPosted: Fri Jun 13, 2003 2:08 pm 
I translated that into C and plugged it into my code and now when I asm("sti"); it immediately triggers a general protection fault as opposed to the Div/0 exception. I wonder which one's worse. ::)

Any more ideas? With that pic remapping function, doing a 1/0 in the code still triggers exception 0 (Good, I think.), and the GPF loops forever if I respond with EOI (I'm not sure if that's desired behavior or not.)


Top
  
 
 Post subject: Re:SigOPS OS
PostPosted: Fri Jun 13, 2003 5:20 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
i guess we have progressed, because if there's something wrong with your interrupt code before the "print" command is issued, then it will actually issue a GPF.
Now, i think you should try to see *where* the GPF comes from (i.e. look at the stack and try to get the value of eip, or if you're using bochs try to enable calls trace through the debug interface).

either the GPF comes from your "normal code", which may mean the IRQ0 is actually mapped to INT0xD, but i don't think that's the case, or the GPF has been produced by your interrupt handler -- in which case you'll have to debug that handler.

A last option is that the GPF is generated by the maincode because your ISR returns an unexpected state, but in that case, you should see the 'timer output'.

Remember there are rules for the hardware ISR, such as they must be at code level 0, etc. if you break those rules, you'll get a GPF with the interrupt number as the error code.

Also try to get the error code (pushed on the stack) of your GPF, so that you know what's the item that goes bad.

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:SigOPS OS
PostPosted: Fri Jun 13, 2003 6:03 pm 
Thanks for all your help thus far.

Okay: So. I'm not using bochs, but I do have a debug routine that will dump all the registers. Here's what it looks like:

General Protection Exception:
EIP: 0x00100183 CS: 0x00000008 EFLAGS: 0x00010246 errcode: 0x00101165
eax: 0x00000101 ebx: 0x00101080 ecx: 0x0000000e edx: 0x000003d5
esi: 0x0000a400 edi: 0x00000010 esp: 0x0000ffc0 ebp: 0x0000ffe8

I tried mapping the IRQs to different addresses and the GPF still happened, so I think that rules out being mapped to INT0xD.

I was looking this up on google and some page said that if my CPL is less than the IOPL, executing cli/sti/out/in would cause an INT0xD... Could this be the case? How could I check/fix it?


Top
  
 
 Post subject: Re:SigOPS OS
PostPosted: Sat Jun 14, 2003 4:32 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 22, 2004 11:00 pm
Posts: 1076
you can see your EIP where it happens, so you should be able to disassembler your kernel wou will know what routine triggered the fault.. unless tyhe EIP printed is not the EIP of the fault...

the other thing i should add, is, have you dummy handlers for your interrupts? eg: plain IRETs for clock etc, since they will trigger as soon as you enable interrupts.

_________________
-- Stu --


Top
 Profile  
 
 Post subject: Re:SigOPS OS
PostPosted: Sat Jun 14, 2003 5:05 am 
I'm using linux and I have only used ndisasm, which didn't provide very much use to me. Although, I'm not too familiar with it, I might not have been using it correctly here.

The dummy handlers are set up correctly, but I don't believe they are getting called at all because none of the printfs inside of them are executing.


Top
  
 
 Post subject: Re:SigOPS OS
PostPosted: Sat Jun 14, 2003 5:31 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
if you have linux, you can try objdump -x to get information about your symbol map. Also try to ask the linker generating that symbol map for you (ld -Map <mapfile>).

if you have a "-g" version of your kernel, also try objdump -dlS to get a line-number dissassembly which interleaves C source lines and disassembled instructions.

However, by the look at your error code, i would say some "pop segment" have gone wrong, or something alike.

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:SigOPS OS
PostPosted: Sat Jun 14, 2003 8:44 am 
The debugging continues...

I ripped out the IDT (which basically just consisted of C macros and inline assembly) and made a new one in asm. That seemed to fix some things... I'm not getting the GPF when I asm("sti"); in my kernel anymore, but as soon as I hit a key on the keyboard, I get an exception 13. Not sure what could be causing that yet either, since ripping out the old IDT broke the register dump.


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Amazonbot [bot], Google [Bot], iansjack and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group