Page 2 of 4
Posted: Tue Jun 05, 2007 12:14 pm
by Lprogster
Any ideas?
Lster
Posted: Tue Jun 05, 2007 3:39 pm
by JAAman
bochs comes with a utility to make disk images (at least the windows-binary version does, dont know about the others), just tell it the size of the disk you want to make, and it will make the image file and give you the line to put into the bochsrc file
Posted: Wed Jun 06, 2007 1:48 am
by Lprogster
...
Posted: Wed Jun 06, 2007 2:11 am
by Lprogster
Thanks again,
Lster
Posted: Wed Jun 06, 2007 2:18 am
by Lprogster
OK! Yet again I've managed to run my disk image with Bochs.
This is the screen shot:
...
Thanks,
Lster
Posted: Wed Jun 06, 2007 2:51 am
by AJ
OK,
Bochs is screaming about the RPL of SS not being equal to CPL. Check the priorities of your stack segment.
Also, your screen dump is incorrect - it says you are using the code segment (0x08) as data segments, whereas the Bochs dump indicates that you are using 0x10 as your data segment and 0x08 as your code segment.
Cheers,
Adam
Posted: Wed Jun 06, 2007 3:45 am
by Lprogster
Thanks,
Lster
Posted: Wed Jun 06, 2007 4:32 am
by AJ
Ok,
One more niggle about your exception handler - please could you get it to print in hex - it's a lot easier to think about the PL bits that way.
I make your SS->DS 0x23. That looks fine, as long as segment index 4 contains your data segment
Next, CS = 27=0x1B which also looks OK.
What I don't like the look of is CS = 0x08 (which I assume is your kernel mode code segment) which is next to SS = 35 (0x23). When the fault occurs, the SS is loaded from your TSS. This should be a kernel-privilege (RPL=0) stack segment selector. Could you check what's going on there?
Cheers,
Adam
Posted: Wed Jun 06, 2007 5:09 am
by Lprogster
Thanks,
Lster
Posted: Wed Jun 06, 2007 5:41 am
by AJ
Hi,
I'm glad you don't get the GPF now. Heres basically what should be happening:
Your task is running happily in ring 3 (CPL3) when an interrupt happens.
In all likelyhood, the DPL of your interrupt descriptor (say, the timer interrupt (IRQ0)) is ring 0. This is sensible as it allows you to access kernel data.
but
As you are running in ring 3, you currently have a CS and SS with a PL of 3. Your kernel needs to run with CS and SS PL 0.
so
The kernel loads SS0 and ESP0 from your TSS *at the same time* as loading your ring 0 code segment descriptor. It then pushes the ring 3 SS, ESP, EFLAGS and CS on to the ring 0 stack. As SS and CS *must* always be the same PL, this is the only way it can be done.
If your TSS had a SS with PL3, it will fail as CPL now = 0 bur RPL of the SS is 3.
In the same way, when your task is restored, the only way to do this (in software multitasking) is using iret - an instruction which can simultaneously load SS, CS, ESP, EIP and EFLAGS!
As this all happens automagically, the bottom of your ring 0 stack *must* be in the order:
[stack base]
ring 3 SS
ring 3 ESP
EFLAGS
CS
EIP
...
[stack top]
THe other registers can be stored any way you like, as long as your C code and ASM code treat them in the same way(!). For example, if you want to use PUSHA/POPA, your general purpose registers will need to be in the correct order.
Remember to change SS0 in your TSS each time you switch to a new task, bearing in mind the information above. That's a possible cause of the kernel freezing, if you are using the wrong ring 0 stack for task switching.
HTH
Adam
Posted: Wed Jun 06, 2007 6:25 am
by Lprogster
Thanks,
Lster
Posted: Wed Jun 06, 2007 6:41 am
by AJ
No - if you pop a ring 3 CS, you also pop a ring 3 ESP and SS.
Adam
Posted: Wed Jun 06, 2007 7:15 am
by Lprogster
Thank you,
Lster
Posted: Wed Jun 06, 2007 8:13 am
by JAAman
yes, you need 1 stack for each ring you are using
How is this for a stack structure:
without looking closely at your EFLAGS, it looks correct (ref: intel 3A:5.12.1:fig 5-4)
however, most people use separate data segment discriptors for different rings -- that way they can restrict access to the kernel stack (normally, tasks will only run in ring3, ring0 is reserved for kernel usage only)
imho, the NASM manual is only good for a quick reference, not research, make sure you check with the intel manuals (volume 2A does list this information) -- the intel manuals are longer and much more detailed
Posted: Wed Jun 06, 2007 8:54 am
by Lprogster
What should I do next? (The kernel is still freezing...)
Thanks,
Lster