Page 1 of 1

Strange stack problem

Posted: Mon Jan 31, 2005 7:00 am
by Tyler Durden
Hi, i'm writing my simple 32bit OS (full sources included) using old stack descriptor (from the real mode ( i mean, I didn't change anything with the stack (SS:SP) when switching to PM), but this is wrong "a bit" :) So I began to use data descriptor (descriptor for PM data (DS)) (or add a new one (for SS), it doesn't metter/clear the bug) for the stack...
But the os began to hang after it (only (and I check this, ONLY) when you're exiting to the dos/it's not because of the "RETN" instruction, I've checked it too.) Sometimes the letter "a" is printed, sometimes the whole screen is cleared with the white text-attribute... Whatta f*** ?! :( What the problem ? The code (I delete almost everything from it to simplify it, now it's only 194b !) is here:

Code: Select all


;=[code16]===================================================================;
org 100h
use16
;test if in v86
mov eax,cr0
test al,1
jz .noV86
retn
.noV86:

;get descriptors bases
xor eax,eax
mov ax,cs
mov [code16],ax
shl eax,4
push eax
mov word [descriptorCode32+2],ax
mov word [descriptorData32+2],ax
mov word [descriptorCode16+2],ax
shr eax,16
mov byte [descriptorCode32+4],al
mov byte [descriptorData32+4],al
mov byte [descriptorCode16+4],al

;load gdtr
pop eax
add eax,gdt
mov dword [gdtr+2],eax
lgdt fword [gdtr]

;open a20
in al,92h
or al,2
out 92h,al

;disable interrupts (+nmi)
cli
in al,70h
or al,80h
out 70h,al

;switch to PM
mov eax,cr0
or al,1
mov cr0,eax

;jump to selectorCode32
db 66h
db 0eah
dd $+6
dw selectorCode32

Code: Select all

use32

;initialize segment registers
push selectorData32
pop ss
mov esp,stackTop
push selectorData32
pop ds

;initialize eflags (clear nt)
pushfd
pop eax
and eax,0ffffbfffh
push eax
popfd

;jump to selectorCode16
db 0eah
dd $+6
dw selectorCode16
use16

;switch to real mode
mov eax,cr0
and al,0feh
mov cr0,eax

;load cs/clear cashe
db 0eah
dw $+4
code16 dw ?

;set ss/ds/sp
mov ax,cs
mov ss,ax
mov ds,ax
mov sp,0fffeh

;enable interrupts (+nmi)
in al,70h
and al,07fh
out 70h,al
sti

;get out
retn

;=[const32]==================================================================;
;selectors
selectorCode32=1000b
selectorData32=10000b
selectorCode16=11000b
selectorFlat=100000b
;=[data32]===================================================================;
;global descriptors table
align 8
gdt:
rb 8
descriptorCode32 db 0ffh,0ffh,0,0,0,10011010b,11001111b,0
descriptorData32 db 0ffh,0ffh,0,0,0,10010010b,11001111b,0
descriptorCode16 db 0ffh,0ffh,0,0,0,10011010b,0,0
descriptorFlat db 0ffh,0ffh,0,0,0,10010010b,11001111b,0
gdtr dw $-gdt-1
dd ?
;stack
rb 200h
stackTop:
The full "test" of the OS (with PE loading, HDD/FAT32 handling and some other stuff, may be it would be usefull for someone) is here

Re:Strange stack problem

Posted: Mon Jan 31, 2005 7:45 am
by Pype.Clicker
hm. Maybe you could try to make your message more readable for the next time ??

Code: Select all

push selectorData32
pop ds
push selectorData32
pop fs
push selectorData32
pop gs
push selectorData32
pop es
this kind of things is unlikely to work at all before SS is defined...
something like

Code: Select all

mov ax,selectorData32
mov ss,ax
mov ds,ax
mov es,ax
would probably be preferred by the CPU ;)

Re:Strange stack problem

Posted: Mon Jan 31, 2005 8:31 am
by Tyler Durden
Sorry of my bad english :( I'm from Ukrane... "Mov ax," doesn't work too (I've tried that already...)

Re:Strange stack problem

Posted: Mon Jan 31, 2005 11:35 am
by Pype.Clicker
problem wasn't with english, but instead post (and code!!) readability.

btw, i think you may (more or less) drop that NMI enable/disable code.

Re:Strange stack problem

Posted: Mon Jan 31, 2005 11:47 am
by CloudNine
It looks like you haven't loaded the IDT, and any random processor interrupt will go haywire all over memory, usually resulting in a reset :).
If you don't know what the IDT is, I suggest you look up on osdever.net

Re:Strange stack problem

Posted: Mon Jan 31, 2005 12:28 pm
by Tyler Durden
Nop, with the IDT-version tha same problem occures, NMI works fine to me ;) Any way, rept. : THE CODE IN THE PROTCTED MODE WORKS !!! PROGRAM HANGS _AFTER_ ECIT TO DOS :( Is it a dos bug or something ? :(

Re:Strange stack problem

Posted: Tue Feb 01, 2005 3:17 am
by Pype.Clicker
oh, i haven't noticed that in your first post (readability, again :P)

If this is just about returning to DOS, i'm not surprised it doesn't work either: you're trying to use "RET" to return (which usually works with .COM programs because the loader sets up the stack so that it only contains the PSP address (which starts with an "INT 20h": terminate COM program)

In your case, you're *not* preserving that stack information (e.g. you switch to an arbitrary stack location of CS:FFFE). Imho, if you change the last "RETN" by "INT 20h", it should work much better.

Also if you had defined an IDT, you may need to restore IVT by re-loading IDTR with base=0, limit=256*4 (not 100% sure of that, though)

Ps: i really love your prehistorik shouting avatar, but please don't shout yourself, right ;D

Re:Strange stack problem

Posted: Tue Feb 01, 2005 4:20 am
by Tyler Durden
;) Nop again :( The "retn" instruction works well 'couse it jumps to the cs:0 address, where the "int 20h" instruction is placed... Anyway if I change "retn" to "mov ah,4ch,int 21h" or "int 20h" the result is still the same :( I know "about IDT i nreal mode" :) and do this in IDT-version, but as you can see in this code all interrupts (including NMI) are disabled (cli). And again, if this was some kind of IDT/PM/NF or something else PM-stuff bug in the code, the proggy hadn't work at all and reboot the CPU... But it hangs only after exit to DOS...

Re:Strange stack problem

Posted: Tue Feb 01, 2005 7:15 am
by Pype.Clicker
you means it displays "C:\MYOS\>" but you cannot type anything ?

If that's the case, that sounds much like if you forgot to restore IRQs handling (still i don't see how it could go wrong here. With an IDT-enabled version, that could be because of PIC not remapped to 8-F,70-78)

Re:Strange stack problem

Posted: Tue Feb 01, 2005 7:23 am
by Tyler Durden
All are remapped correctly, but any way, this isn't answer why this simple proggy hangs :( Can you please test it under VirtualPC or something ?

Re:Strange stack problem

Posted: Tue Feb 01, 2005 7:31 am
by Pype.Clicker
not at the moment, sorry.

I don't have a decent (nor even 1/n-decent) dos emulator here ...