OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 7:49 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: what happens if i don't set ESP
PostPosted: Mon Dec 22, 2003 5:02 am 
my code to switch to protected mode is
Code:
        xor ax,ax
        mov ds,ax
        lgdt [gdtr]
; set PE [protected mode enable] bit and go
   mov eax,cr0
   or al,1
   mov cr0,eax

        jmp SYS_CODE_SEL:do_pm
[BITS 32]
do_pm:

        mov ax,SYS_DATA_SEL
   mov ds,ax      
   mov ss,ax
   mov es,ax
   mov fs,ax

.
.
.
.
.
.

SYS_CODE_SEL   equ   $-gdt
gdt1:   dw 0xFFFF
   dw 0         
   db 0
   db 0x9A         ; present, ring 0, code, non-conforming, readable
   db 0xCF
   db 0

SYS_DATA_SEL   equ   $-gdt
  gdt2:   dw 0xFFFF
   dw 0         
   db 0
   db 0x92   ; present, ring 0, data, expand-up, writable
   db 0xCF
   db 0


in this code i havn't set esp.
What is contained in esp as i havn't set it.
I'll jump to C code for rest of my kernel.I think c language uses this stack pointer.

Can u please explain the role of stack pointer .and what might happen if i don't set it.

As u see in the code both code and data selectors have same base and limit.But code segment is readable and data segment is writable.

Please explain how same memory can be both readable and writable as both code and data selectors
point to same memory.

Is there any possibility that i may overwrite my code as both code and data segments point to same memory.And if this possibility is true please suggest me what should i move in code and data selectors to avoid code overwriting by data.


Top
  
 
 Post subject: Re:what happens if i don't set ESP
PostPosted: Mon Dec 22, 2003 7:11 am 
shaz wrote:
Can u please explain the role of stack pointer .and what might happen if i don't set it.

The stack pointer (ESP) points to the end of the stack. When the CPU PUSHes a value onto the stack, it subtracts 4 from ESP then writes the value at that address. To POP a value, it read the value at ESP, then adds 4 to ESP.

C relies on the stack to pass parameters, as do most high-level languages. So before your kernel jumps to any C code, it must set aside a sufficiently large stack region and set ESP to the highest address of that region.

For example:
Code:
mov esp, stack_end
jmp some_c_code
...
stack:
    resb 4096
stack_end:
   

Quote:
As u see in the code both code and data selectors have same base and limit.But code segment is readable and data segment is writable.

Please explain how same memory can be both readable and writable as both code and data selectors
point to same memory.

Because they both point to the same memory :). Segment protection says, "if my code accesses memory through this selector, apply this protection". If two segments point to the same memory, then you can use either protection.

However, you can't access data through a code segment, and you can't run code in a data segment, so you need at least one of each.
Quote:
Is there any possibility that i may overwrite my code as both code and data segments point to same memory.And if this possibility is true please suggest me what should i move in code and data selectors to avoid code overwriting by data.
If you really want to stop code from modifying other code, you'll either need to keep all your code and data separate and allocate segments for each (not recommended), or use paging and make your code pages read-only (recommended).


Top
  
 
 Post subject: Re:what happens if i don't set ESP
PostPosted: Mon Dec 22, 2003 12:11 pm 
OK.Now i understand why it is neccassary to set ESP before going to C code.

But tell me one more ,previously i was not setting ESP but still my C code was running correctly.
Was it happening by chance?

When i switch back to real mode to get a BIOS interrupt,should i again set the stack pointer or my protected mode ESP will be enough.


Top
  
 
 Post subject: Re:what happens if i don't set ESP
PostPosted: Mon Dec 22, 2003 7:26 pm 
shaz wrote:
But tell me one more ,previously i was not setting ESP but still my C code was running correctly.
Was it happening by chance?


Nothing weird is happening because the stack already points to some valid memory address (thanks to BIOS or a bootloader). But the problem is, that you can easily overwrite critical kernel structures or even kernel code, because you don't know where your stack points to. Note that it can be extremely hard to find such a bugs.

Solution: setup your own stack.

Regards,
TB.


Top
  
 
 Post subject: Re:what happens if i don't set ESP
PostPosted: Tue Dec 23, 2003 8:01 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
shaz wrote:
When i switch back to real mode to get a BIOS interrupt,should i again set the stack pointer or my protected mode ESP will be enough.

This basically depends on how your protected mode stack was looking like, but as there are chances that:
- your stack pointer in pmode is larger than 16 bits, or
- your stack segment in pmode does not have the same base as your stack segment in real mode, or
- your pmode stack is beyond the 1MB available through real mode

you should consider setting back a realmode-specific stack when you're switching from pmode to realmode and keep track of where you were in pmode to restore the 'correct' pmode stack.

You should also consider using unreal mode or virtual mode if you have to do those switches quite often, depending on what you're trying to achive. The BIOS 'high memory copy' function (INT15, somwhere) may also be convenient ...

_________________
Image May the source be with you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], SemrushBot [Bot] and 59 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group