Page 1 of 1

Kernel won't keep executing

Posted: Sat Aug 12, 2006 5:24 pm
by Candamir
When I start multitasking in my kernel and start a new kernel thread, it works (it actually prints out some stuff in a loop), but this thread wouldn't allow the kernel to keep executing (with a single thread registered, the scheduler would just never change anything and all that runs would be this new thread).

To solve this problem, I created a task entry in the linked list that represents the kernel process (got it's register values during the first call to the scheduler) and then treated the kernel just as any ordinary task. Creating the kernel thread still worked, and switching from the kernel to the thread did work, the thread still did it's work, but with the next scheduler call, when switching back to the kernel, nothing happens when putting the kernel's values into the regs structure - everything OK so far -, but afterwards, a page fault is triggered with 0x10 in CR2.

Could anyone help me? I know my writing is a little bit complicated, and I don't know if registering the kernel as a task was a good idea...

This is the Bochs snapshot:

Code: Select all

0 Multiboot Module(s) loaden. Please reboot if you didn't expect this.
Physical Memory Manager: 120M avaiRegistered kernel with ID 0
lable on RAM
Task Manager...
Switched to task 1: CS: 0x8,DS: 0x10,ES: 0x10,FS: 0x10,GS: 0x10,SS: 0xC01088F4
AAAAAAAAAAAAAAAAAAAASwitched to task 0: CS: 0x8,DS: 0x10,ES: 0x2BAD0010,FS: 0xC0
100010,GS: 0xC0101869,SS: 0xC0100788

Kernel Panic!!!
Page Fault => 0x10
The Hermes Kernel cannot recover from this fault.
If your computer doesn't reboot in 5 seconds, please do it manually.
Candamir

Edit: Here the piece of code which most probably triggered the error (after returning from irq_handler):

Code: Select all

irq_common_stub:
    pushad
    push ds
    push es
    push fs
    push gs

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp

    push eax
    mov eax, irq_handler
    call eax
    pop eax

    pop gs
    pop fs
    pop es
    pop ds
    popad
    add esp, 8
    iret
The only register with value 0x10 is ds, but I don't see how this could trigger a page fault...

Re:Kernel won't keep executing

Posted: Sun Aug 13, 2006 5:06 am
by Bob the Avenger
You appear to be having a similar problem to the one i'm having currently, whats happening is, somehow (i'm yet to work out) your value loaded into esp isnt the address of the stack, it is slightly less or more, so when your stub pops all the registers off the stack, the wrong values are pop'd into the registers. It appears that your stub leaves one of the seg reg values to be pop'd into eip, so when the iret occurs, it tries to ret to 0x10, thus page faulting.

I have the same problem, but my kernel tried to load a bad value into one of the seg regs, andso GPFs, i havent been able to fix this problem yet, so if you figure out a way of fixing it, pm me becuase i would find it useful

Re:Kernel won't keep executing

Posted: Sun Aug 13, 2006 6:26 am
by distantvoices
So you have entered your thread and aren't able to schedule any other ones, eh?

This is no problem: spawn any needed threads off before you start scheduling (i.e. switching context to the first executable thread)

any additional stuff like spawing servers you'd do in some thread at very low level - so it can be preempted by services & drivers.

just take care that you don't break the pyramid of execution, which is necessary to build in a micro kernel.

Further: no endless loop without at least a yield() - to invoke the scheduler so it can pick the next task - that's just a proposal for you to ponder with. you can also invoke the scheduler with a key press: press a key and it switches tasks at your will. just add the tasks BEFORE starting the whole stuff, because after starting multitasking, you'Re out of your kernels main(), Execution will never return to it if everythings going right.

Re:Kernel won't keep executing

Posted: Sun Aug 13, 2006 9:01 am
by Candamir
I added the kernels main() as another kernel thread to the scheduler. This allows the kernel setup to keep running after starting scheduling. When this 'main' thread reaches the idle loop, one can set it's priority to 0. That's because in my kernel, I still have a lot of things to do after setting up multitasking (set up servers, which can only be done after setting up multitasking).
just take care that you don't break the pyramid of execution, which is necessary to build in a micro kernel.
Would you mind explaing about this pyramid of execution? I've never heard about it in your tutorial nor elsewhere...

Thanks

Candamir

Re:Kernel won't keep executing

Posted: Sun Aug 13, 2006 9:39 pm
by deadmutex
you only have to put "add esp, 8" if the cpu pushes an error code for the interrupt. On a page fault, the interrupt number and error code are pushed by the cpu, but on a Div-by-zero exception, for example, the cpu doesnt push an error code.

Re:Kernel won't keep executing

Posted: Sun Aug 13, 2006 9:51 pm
by Candamir
Candamir wrote:

Code: Select all

irq_common_stub:
    pushad
    push ds
    push es
    push fs
    push gs

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp

    push eax
    mov eax, irq_handler
    call eax
    pop eax

    pop gs
    pop fs
    pop es
    pop ds
    popad
    add esp, 8
    iret
I'm pretty sure now it has something to do with ss, but i still don't know what's exactly the matter... I'll keep trying!

Candamir

Re:Kernel won't keep executing

Posted: Sun Aug 13, 2006 11:57 pm
by distantvoices
@candemir:

with Pyramid of Execution I mean the following:

Code: Select all

                            kernel

                 driver1 driver2 driver3 

   service1 service2 service3 service4 service5 

user1 user2 user3 user4 user5 user6 user7 ... userN

Kernel routes messages between all the entities working in the system. YOu have to take care: user calls service. service calls driver. Driver reports to service. service reports to user. So that no driver does stuff on its own nor a service starts doing weird things it has not to do. They have to act upon request. A driver has only to act upon service request. a service has to act upon user request.

Look at it as if it were a chain of function calls. just that they are propagated via messages between the communicating components.

Re:Kernel won't keep executing

Posted: Mon Aug 14, 2006 8:52 am
by Candamir

Code: Select all

                            kernel               => Ring 0

                 driver1 driver2 driver3           => Ring 1

   service1 service2 service3 service4 service5       => Ring 2

user1 user2 user3 user4 user5 user6 user7 ... userN   => Ring 3

Would this make sense? BTW, what would you call a service: The filesystem, network stack, GUI Server, etc.?

Candamir

Re:Kernel won't keep executing

Posted: Mon Aug 14, 2006 12:30 pm
by distantvoices
ah ... that's what minix is doing. It is not bad. You'l just have some overhead with bookkeeping of all the segments. :-)

I keep it the following in my current approach - which is to change in the future as time permits (oh, time doesn't permit, but I give a damn you see, studying in the evenings & working for a living don't leave too much room for osdeving - but it happens sometimes)

kernel -> ring0
drivers -> ring0
services -> ring3
userland -> ring3

Mark that this is only one way to do it. There are several approaches. F. ex. in my network stack I've got a section which deals with driver stuff. It implements the drivers as classes (well sort of) which execute as threads in the network stack. It seemed to me to be the best approach for the network stuff - you see, buffer chains & the like - fragmented ip packets just to mention one thing which causes buffer chains to jump into existence - ut I'm dwaddling off the topic...

You ask what I mean with service and you've got it right:

services (in my opinion) are: gui service, file system service (is responsible for VFS stuff & dealing with block & char devices), network service, dhcp client service ... and many others. sound service jumps into mind.

hth & stay safe. :-)