OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Modify values on stack in c
PostPosted: Tue Jan 31, 2017 7:19 am 
Offline
User avatar

Joined: Tue Jun 21, 2016 6:41 am
Posts: 21
Hello,

I am trying to write multithreading code for my OS.

'push eax' pushes the esp of the interrupted process onto the stack.

Code:
;push process stack
  push eax
 
  mov eax, irq_handler
  call eax

  pop eax


In C:

Code:
void irq_handler(struct x86_registers *regs)
{
   void (*handler)(struct x86_registers *regs);

   handler = irq_handlers[regs->int_no - 32];
   if (handler)
      handler(regs);

   irq_send_EOI_8259(regs->int_no);
}


*regs is that eax value. However, if I modify it to point to a different thread's stack, will the new value be what the handler removes from the stack? (pop eax) Or does the value just get cloned? With the *regs struct, I can modify all cpu registers of the interrupted process, except for the pointer to the processes curent stack!

(BTW When an interrupt happens on my OS all registers are pushed onto the stack, then esp is put in eax. The stack is changed to a predefined 'interrupt stack' then eax is pushed, and the handler is called. eax is popped back off, stack is changed back, and regs are popped off. IRET)
Code:
_irq_stub:

  ;push all data onto current stack
  pusha
  push ds
  push es
  push fs
  push gs

  ;set up for handler
  mov ax, 0x10
  mov ds, ax
  mov es, ax
  mov fs, ax
  mov gs, ax

  mov eax, esp
 
  ; ====switch stacks:
   
  ; load interrupt stack
  mov esp, [int_stack]
  mov ebp, [int_stack+4]

  ;push process stack
  push eax
 
  mov eax, irq_handler
  call eax

  pop eax

  ; save interrupt stack
  mov [int_stack], esp
  mov [int_stack+4], ebp

  ;reload process stack
  mov esp, eax
 
  pop gs
  pop fs
  pop es
  pop ds
  popa
  add esp, 8 ; jump past interrupt number and code

  iret


https://github.com/michaellangford99/Papal-OS-3---Clement

Thanks so much!

_________________
"Out of memory: Please memorize the following numbers and type them back in when asked for page number 42". - linguofreak

"Quote me in your forum signature" - Sortie (Check!)


Top
 Profile  
 
 Post subject: Re: Modify values on stack in c
PostPosted: Tue Jan 31, 2017 9:34 am 
Offline
User avatar

Joined: Tue Jun 21, 2016 6:41 am
Posts: 21
Simply stated, does a value pushed onto the stack as a parameter remain after the function returns, with a possibly modified value, but at the same address? Or is the value cloned and not modified in its original location? Also, can I rely on the compiler to always behave in one of these two ways?

_________________
"Out of memory: Please memorize the following numbers and type them back in when asked for page number 42". - linguofreak

"Quote me in your forum signature" - Sortie (Check!)


Top
 Profile  
 
 Post subject: Re: Modify values on stack in c
PostPosted: Tue Jan 31, 2017 11:11 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
michaellangford wrote:
Simply stated, does a value pushed onto the stack as a parameter remain after the function returns, with a possibly modified value, but at the same address? Or is the value cloned and not modified in its original location?

Neither. The parameter on the stack may be overwritten with anything the compiler needs temporary space to store.

If you need your function to return a value, you must either explicitly return that value in a return statement, or return that value through a pointer passed to the function.

Is there any particular reason you do "mov eax, irq_handler; call eax" instead of "call irq_handler"?


Top
 Profile  
 
 Post subject: Re: Modify values on stack in c
PostPosted: Tue Jan 31, 2017 11:21 am 
Offline
User avatar

Joined: Tue Jun 21, 2016 6:41 am
Posts: 21
Thanks! I will change it from a void function to a uint32_t irq_handler(...)

I was actually not aware that I could do that in NASM! I will change that as it will be more readable.

_________________
"Out of memory: Please memorize the following numbers and type them back in when asked for page number 42". - linguofreak

"Quote me in your forum signature" - Sortie (Check!)


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: DotBot [Bot] and 61 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