what happens if i don't set ESP

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
shaz

what happens if i don't set ESP

Post by shaz »

my code to switch to protected mode is

Code: Select all

        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.
Tim

Re:what happens if i don't set ESP

Post by Tim »

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: Select all

mov esp, stack_end
jmp some_c_code
...
stack:
    resb 4096
stack_end:
    
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.
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).
shaz

Re:what happens if i don't set ESP

Post by shaz »

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.
TB

Re:what happens if i don't set ESP

Post by TB »

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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:what happens if i don't set ESP

Post by Pype.Clicker »

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 ...
Post Reply