Page 2 of 3

Re: Issues with system call

Posted: Thu Dec 12, 2013 5:47 pm
by riparoony
When going through my IRQ handler, I get a general protection fault. The only thing I can think might be causing this would be IRQ map conflicts, as the fault only happens when I call "int $0x7F".

Re: Issues with system call

Posted: Thu Dec 12, 2013 7:48 pm
by zhiayang
Hello again.
A couple of issues:

1. I don't know if this is intentional, but your isr_handler simply calls core::panic().
2. I think your problem lies in the fact that you didn't do a idt::set_gate for your sys call handler.

Code: Select all

extern "C" irq127();
...

idt::set_gate(127, (unsigned long)irq127, 0x08, 0xEE);

3. As it is now, when you do int $0x7F, the CPU will start faulting all over the place, because you don't have an interrupt handler set for that vector. You simply set an entry in the array (which, BTW, only needs to be 256 - 32 entries long) -- the thing is, you never get to irq_handler to call the system call handler inside the array, because you didn't register your irq127() function (in irq.asm) inside the IDT, which is why the CPU faults.

4. Because your system call goes through your global handler (ie. irq_handler() ), you need to either get rid of the clause { if (r->int_no > 47 || r->int_no < 32) }, or modify it to allow your system call through.

5. Remove the isr127() function in your ASM, it's not required.

Re: Issues with system call

Posted: Sun Dec 15, 2013 6:45 pm
by riparoony
As it is now, when you do int $0x7F, the CPU will start faulting all over the place, because you don't have an interrupt handler set for that vector. You simply set an entry in the array (which, BTW, only needs to be 256 - 32 entries long) -- the thing is, you never get to irq_handler to call the system call handler inside the array, because you didn't register your irq127() function (in irq.asm) inside the IDT, which is why the CPU faults.
I've got those things, though it still isn't working. I register the entry in the IDT and with my IRQ handler, but I'm still getting garbage values.

Code: Select all

cpu::idt::set_gate(0x7F, (unsigned long)syscall_handler, 0x08, 0x8E);
cpu::irq::install_handler(127, syscall_handler);

Re: Issues with system call

Posted: Sun Dec 15, 2013 7:23 pm
by BMW
joshbeitler wrote:I've got those things, though it still isn't working. I register the entry in the IDT and with my IRQ handler, but I'm still getting garbage values.

Code: Select all

cpu::idt::set_gate(0x7F, (unsigned long)syscall_handler, 0x08, 0x8E);
cpu::irq::install_handler(127, syscall_handler);
In your irq::gates function, you setup the system call IRQ so that it goes through your global IRQ handler (which is correct)

Code: Select all

void irq::gates()
{
    ...

    idt::set_gate(127, (unsigned long)irq127, 0x08, 0xEE);
}
However, after you call the irq::gates function, you call your init_syscalls function, which incorrectly remaps the system call IRQ to your syscall_handler (this will make it skip your global handler, meaning registers aren't pushed onto the stack).

Code: Select all

void init_syscalls()
{
    cpu::idt::set_gate(0x7F, (unsigned long)syscall_handler, 0x08, 0x8E); // This line is incorrect
    cpu::irq::install_handler(127, syscall_handler);
}
So what you need to do to get it working is remove the first line of your init_syscalls function

EDIT: While browsing your source, I have spotted some more issues:
You are meant to send an EOI to the slave controller if the IRQ number is greater than or equal to 8, however you are only sending it if the IRQ number is greater than or equal to 12:

Code: Select all

void irq::ack(int irq_no)
{
    if (irq_no >= 12)
    {
        outb(0xA0, 0x20);
    }

    outb(0x20, 0x20);
}
So you need to change "if(irq_no >= 12)" to "if(irq_no >= 8 )"

In your irq_handler, you only send an EOI if there is no handler. Why? You should send an EOI whether you handled the IRQ or not.

Code: Select all

extern "C" void irq_handler(struct regs* r)
{
    void (*handler)(struct regs* r);
    handler = irq_routines[r->int_no - 32];

    if (handler)
        handler(r);
    else
        irq::ack(r->int_no - 32);
}
Change your irq_handler function to this:

Code: Select all

extern "C" void irq_handler(struct regs* r)
{
    void (*handler)(struct regs* r);
    handler = irq_routines[r->int_no - 32];

    if (handler)
        handler(r);
    
    irq::ack(r->int_no - 32);
}
EDIT 2: Removed incorrect statement.

Re: Issues with system call

Posted: Mon Dec 16, 2013 3:53 am
by Combuster
BMW wrote:IRQ 95
What nonsense is this? :shock:

IRQ0 - IRQ15 are the names for the (virtual) pins on the PIC, each connected to a specific legacy device. The PIC maps these to interrupt numbers, which means that IRQ 0 can manifest itself as a wide range of interrupt numbers. Traditionally the likely results for IRQ0 are either interrupt 8 or 32, but any multiple of 8 is technically possible.

If you have modern hardware with the necessary driver support, each hardware interrupt source can be mapped to any interrupt number, and in many cases there isn't even an physical interrupt line involved, let alone being numbered.

Re: Issues with system call

Posted: Mon Dec 16, 2013 4:13 am
by BMW
Combuster wrote:
BMW wrote:IRQ 95
What nonsense is this? :shock:

IRQ0 - IRQ15 are the names for the (virtual) pins on the PIC, each connected to a specific legacy device. The PIC maps these to interrupt numbers, which means that IRQ 0 can manifest itself as a wide range of interrupt numbers. Traditionally the likely results for IRQ0 are either interrupt 8 or 32, but any multiple of 8 is technically possible.

If you have modern hardware with the necessary driver support, each hardware interrupt source can be mapped to any interrupt number, and in many cases there isn't even an physical interrupt line involved, let alone being numbered.
Sorry, I was waffling based on a stupid assumption. #-o

What is the actual difference between an interrupt and an IRQ? Is the interrupt the OP is using for system calls actually an IRQ? I thought IRQs were caused by hardware.

Re: Issues with system call

Posted: Mon Dec 16, 2013 4:17 am
by Combuster
What is the actual difference between an interrupt and an IRQ?
What is the difference between an actual interrupt and an Interrupt ReQuest? :-k

Re: Issues with system call

Posted: Mon Dec 16, 2013 4:23 am
by BMW
Combuster wrote:
What is the actual difference between an interrupt and an IRQ?
What is the difference between an actual interrupt and an Interrupt ReQuest? :-k
Wait, I think I just got it. An Interrupt ReQuest (IRQ) is a interrupt requested by a hardware device, hence the name IRQ. IRQs are mapped to interrupts by sending commands to the PIC. So if you call an interrupt from your program which corresponds to an IRQ e.g. INT 34, although it may go through an IRQ handler, it is not actually an IRQ?

Is that correct, or am I waffling based on more assumptions? :-s

Re: Issues with system call

Posted: Mon Dec 16, 2013 12:10 pm
by Gigasoft
BMW wrote:So, what you need to do is change the init_syscalls() function so that it sets up your system call handler correctly by going to your IRQ handler. So if you were using IRQ 2 for system calls (what I use in my OS):
Wow. That has to be some of the worst advice I've seen on the Internet in a while.

Re: Issues with system call

Posted: Mon Dec 16, 2013 3:32 pm
by BMW
Gigasoft wrote:Wow. That has to be some of the worst advice I've seen on the Internet in a while.
And that has to be one of the worst posts I've seen in a while. Aside from being void of any informational quality, it criticizes without reason. If you are going to criticize my advice, fine, but at least tell me why you are criticizing it. :-x

Re: Issues with system call

Posted: Mon Dec 16, 2013 5:28 pm
by Combuster
What lack of reason? Your fundamental ideas have been broken and you already admitted to waffling. And while you might have cut out one particular source of the comment, there's still an entire rest of a post originally written with a lack of understanding you did not fix. You can expect mockery and people putting up warning signs for the OP - they're the same text anyway.

And let's be honest here, how do you imagine software performing a system call by invoking IRQ2 - which is a wire outside of the processor? ](*,)


The forum rules wrote:When this happens, the worst thing you can do is whine about the experience, claim to have been verbally assaulted, demand apologies, scream, hold your breath, threaten lawsuits, complain to people's employers, leave the toilet seat up, etc. Instead, here's what you do:

Get over it. It's normal. In fact, it's healthy and appropriate.
And by the way, it looks like you gave someone else something memorable to talk about to his friends. Congratulations. :mrgreen:

Re: Issues with system call

Posted: Mon Dec 16, 2013 7:49 pm
by BMW
@Gigasoft, I did not advise the OP to use IRQ 2, I only said (erroneously, I admit) that I use it it my OS. That erroneous statement was due to a lack of understanding, on my part, of IRQs, which Combuster kindly pointed out and corrected me on. I learned from that mistake because Combuster constructively criticized me, however the criticism given by you was pure criticism, and therefore pointless.
Combuster wrote:What lack of reason?
By "lack of reason" I meant he didn't supply a reason. He may have a good reason, but he didn't tell me. How do you expect me to learn from a mistake if I don't know the mistake?
Combuster wrote:And let's be honest here, how do you imagine software performing a system call by invoking IRQ2 - which is a wire outside of the processor? ](*,)
#-o Thanks for pointing that out.
The forum rules wrote:When this happens, the worst thing you can do is whine about the experience, claim to have been verbally assaulted, demand apologies, scream, hold your breath, threaten lawsuits, complain to people's employers, leave the toilet seat up, etc. Instead, here's what you do:
Get over it. It's normal. In fact, it's healthy and appropriate.
  1. Click: http://forum.osdev.org/viewtopic.php?f=1&t=16944
  2. Ctrl + F -> Type "When this happens, the worst thing you can do"
  3. Observe the result - no matches
  4. Wonder what Combuster was quoting from

Re: Issues with system call

Posted: Tue Dec 17, 2013 4:19 am
by Combuster
Observe the result - no matches
Image

Follow the #includes. Particularly the one in point 4.

Re: Issues with system call

Posted: Thu Dec 19, 2013 5:27 pm
by riparoony
@requimrar: After I implemented your changes, the handler isn't even getting called anymore.

Re: Issues with system call

Posted: Thu Dec 19, 2013 11:21 pm
by zhiayang
Hello.

In your irq::gates() function, you correctly set irq127() to be called on irq 127. From there, it enters your global handler; all expected.

Here's your problem: in your syscall_install() function, you install your system call handler into entry number 127. However, in your global handler, you attempt to call the handler at position (127 - 32).

Since your syscall uses interrupt 0x7F, entry 127 is the absolute index -- there are two wags to fix this.

1. In your irq_install() function, subtract 32 from the given index. (ie. handlers[num - 32])
2. In your global handler, don't subtract 32. (ie. call handlers[regs->id] directly)

Both will require you add 32 to existing calls to irq_install; irq 0, the PIT interrupt, will now be vector 32. (0 + 32), and so on.

The other solution is not to modify install_handler(), but instead change install_syscall() to call irq_install with (127 - 32).

Choose whichever you think is best.