PIT not working

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
KSMb
Posts: 7
Joined: Tue Sep 11, 2018 9:42 am

PIT not working

Post by KSMb »

Hello,

I have another problem with my "OS": i'm unable to get the PIT to work.
This is what i've tried to do:
  • Remapped PICs using:

    Code: Select all

    void irq_remap() {
        outb(0x20, 0x11);
        outb(0xA0, 0x11);
        outb(0x21, 0x20);
        outb(0xA1, 0x28);
        outb(0x21, 0x04);
        outb(0xA1, 0x02);
        outb(0x21, 0x01);
        outb(0xA1, 0x01);
        outb(0x21, 0x0);
        outb(0xA1, 0x0);    
    }
    
  • An IRQ handler that is being called from assembly irq_common_stub:

    Code: Select all

    void irq_handler(registers_t regs) {
        if(regs.int_no >= 40) // If IRQ > 7(int_no > 40) reset slave PIC
            outb(0xA0, 0x20);
        outb(0x20, 0x20); // In either cases, reset master PIC
    
        // Register a new interrupt handler
        if(interrupt_handlers[regs.int_no] != 0) {
            isr_t handler = interrupt_handlers[regs.int_no];
            handler(regs);
        }
    }
    
  • A timer initializer

    Code: Select all

    void init_timer(uint32_t frequency) {
        register_interrupt_handler(32, &timer_callback);
    
        uint32_t divisor = 1193180 / frequency;
    
        outb(0x43, 0x36);
    
        uint8_t low = (uint8_t)(divisor & 0xFF);
        uint8_t high = (uint8_t)((divisor >> 8) & 0xFF);
    
        outb(0x40, low);
        outb(0x40, high);
    }
    
The problem is that, when i try to call init_timer() from the kernel nothing happen. The weird thing is that if i try to run the OS with Bochs(starting it with GRUB's countdown, without touching the keyboard) it prints this error:

Code: Select all

03150576036e[CPU0  ] check_cs(0x0206): not a valid code segment !
.
Otherwise(with QEMU or even with Bochs, pressing "enter" from GRUB's menu)nothing gets printed at all.

This is the repo, just in case.
nullplan
Member
Member
Posts: 1801
Joined: Wed Aug 30, 2017 8:24 am

Re: PIT not working

Post by nullplan »

KSMb wrote:

Code: Select all

void irq_handler(registers_t regs) {
Not this old chestnut again. Since I highly doubt you are handing a structure over correctly (according to the ABI, that is), I suggest you make the parameter into a pointer and hand that over.
KSMb wrote:

Code: Select all

03150576036e[CPU0  ] check_cs(0x0206): not a valid code segment !
.
The hint is in the message. There are only two ways how a code segment would be loaded: far jump or interrupt. So if you can exclude far jumps, this message is caused by an interrupt. And it means that either the CS reference in the relevant IDT entry is wrong, or the CS entry in the GDT is wrong. Happy debugging!
Carpe diem!
MichaelPetch
Member
Member
Posts: 798
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: PIT not working

Post by MichaelPetch »

The issue you had with registers_t vs. registers_t * previously with isr_handler also applies to irq_handler
Post Reply