PIT problem

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
TheLittleWho
Member
Member
Posts: 51
Joined: Sun Mar 01, 2015 7:58 am

PIT problem

Post by TheLittleWho »

I am taking the James Molly kernel developement tutorial (http://www.jamesmolloy.co.uk/tutorial_html/index.html). I configured a basic VGA driver, some standard functions of C library, a GDT and a IDT. Now, I am trying to configure the PIT. Most of code of it is taken from James Molly tutorial, but is written in my style. The problem is that my interrupt handler isn't called by 'irq_common_stub' function from interrupt.asm.

interrupt.asm:

Code: Select all

; This macro creates a stub for an ISR which does NOT pass it's own
; error code (adds a dummy errcode byte).
; define ISR codes
%macro ISR_NOERRCODE 1 ; define a macro, taking one parameter
global isr%1 ; %1 accesses the first parameter.
isr%1:
cli
push byte 0
push byte %1
jmp isr_common_stub
%endmacro
%macro ISR_ERRCODE 1
global isr%1
isr%1:
cli
push byte %1
jmp isr_common_stub
%endmacro
%macro IRQ 2
global irq%1
irq%1:
cli
push byte 0
push byte %2
jmp irq_common_stub
%endmacro
ISR_NOERRCODE 0
ISR_NOERRCODE 1
ISR_NOERRCODE 2
ISR_NOERRCODE 3
ISR_NOERRCODE 4
ISR_NOERRCODE 5
ISR_NOERRCODE 6
ISR_NOERRCODE 7
ISR_ERRCODE 8
ISR_NOERRCODE 9
ISR_ERRCODE 10
ISR_ERRCODE 11
ISR_ERRCODE 12
ISR_ERRCODE 13
ISR_ERRCODE 14
ISR_NOERRCODE 15
ISR_NOERRCODE 16
ISR_NOERRCODE 17
ISR_NOERRCODE 18
ISR_NOERRCODE 19
ISR_NOERRCODE 20
ISR_NOERRCODE 21
ISR_NOERRCODE 22
ISR_NOERRCODE 23
ISR_NOERRCODE 24
ISR_NOERRCODE 25
ISR_NOERRCODE 26
ISR_NOERRCODE 27
ISR_NOERRCODE 28
ISR_NOERRCODE 29
ISR_NOERRCODE 30
ISR_NOERRCODE 31
ISR_NOERRCODE 128
IRQ 0, 32
IRQ 1, 33
IRQ 2, 34
IRQ 3, 35
IRQ 4, 36
IRQ 5, 37
IRQ 6, 38
IRQ 7, 39
IRQ 8, 40
IRQ 9, 41
IRQ 10, 42
IRQ 11, 43
IRQ 12, 44
IRQ 13, 45
IRQ 14, 46
IRQ 15, 47
extern isr_handler
isr_common_stub:
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 isr_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
extern irq_handler
irq_common_stub:
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 irq_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
dt.asm (contain Descriptor Tables flush functions):

Code: Select all

[GLOBAL gdt_flush] ; Allows the C code to call gdt_flush().
gdt_flush:
mov eax, [esp+4] ; Get the pointer to the GDT, passed as a parameter.
lgdt [eax] ; Load the new GDT pointer
mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
mov ds, ax ; Load all data segment selectors
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:.flush ; 0x08 is the offset to our code segment: Far jump!
.flush:
ret
[GLOBAL idt_flush] ; Allows the C code to call idt_flush().
idt_flush:
mov eax, [esp+4] ; Get the pointer to the IDT, passed as a parameter.
lidt [eax] ; Load the IDT pointer.
ret
clock.c (here is PIC implemented):

Code: Select all

#include <clock.h>
#include <stddef.h>
#include <stdint.h>
#include <isr.h>
#include <io.h>
#include <tty.h>
#include <stdlib.h>
#include <stdio.h>
void timer_callback(registers_t regs)
{
tick++;
printf("Tick: %d\n", tick);
}
void init_timer(uint32_t frequency)
{
tick = 0;
// Firstly, register our timer callback.
register_interrupt_handler(IRQ0, &timer_callback);
// The value we send to the PIT is the value to divide it's input clock
// (1193180 Hz) by, to get our required frequency. Important to note is
// that the divisor must be small enough to fit into 16-bits.
uint32_t divisor = 1193180 / frequency;
// Send the command byte.
outb(0x43, 0x36);
// Divisor has to be sent byte-wise, so split here into upper/lower bytes.
uint8_t l = (uint8_t)(divisor & 0xFF);
uint8_t h = (uint8_t)( (divisor >> 8) & 0xFF );
// Send the frequency divisor.
outb(0x40, l);
outb(0x40, h);
}


Why it isn't working? It is implemented using the tutorial... Rest of kernel code can be found here: https://github.com/JustBeYou/MyOS/tree/devel/src/kernel and libc source: https://github.com/JustBeYou/MyOS/tree/devel/src/libc

Thanks!
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: PIT problem

Post by max »

Hey TheLittleWho,

welcome to the community!

First things first,

Code: Select all

#include <stdlib.h>
#include <stdio.h>
Are you using a cross compiler? Do it. Also, there is a list of known bugs with JamesM's tutorial that you should consider.

Your timer initialization itself looks okay. Are you remapping your IRQs properly, have you re-enabled interrupts and have you checked if your interrupts are working at all by for example using "int" to create one?

Greets,
Max
TheLittleWho
Member
Member
Posts: 51
Joined: Sun Mar 01, 2015 7:58 am

Re: PIT problem

Post by TheLittleWho »

Hey, Max! Yes, I am using a cross compiler configured using the tutorial from OS DEV and stdio.h and stdlib.h are implementes by me. I think I am remappig IRW properly, but I will take a look at it. After I initialized ticker, i use 'asm volatile ("int $0x4");' to test interrupts and it works, it prints: "received interrupt: 4"... i tried to halt the proccessor at end of the main function to be sure the interrupts doesn't get disabled, but it still doesn't work.

Any other suggestions?

PS I will take a look at IRQ remapping and known bugs with JamesM's tutorial.
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: PIT problem

Post by eryjus »

Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
TheLittleWho
Member
Member
Posts: 51
Joined: Sun Mar 01, 2015 7:58 am

Re: PIT problem

Post by TheLittleWho »

At last, I solved it. The problem was that I forgot to enable the interrupts after initialize of ticker and the main function was ending too soon, so I used `for (;;) asm volatile ("hlt");` to keep it running.
Thanks you all!
Post Reply