Page 1 of 1

Modify values on stack in c

Posted: Tue Jan 31, 2017 7:19 am
by michaellangford
Hello,

I am trying to write multithreading code for my OS.

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

Code: Select all

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

  pop eax
In C:

Code: Select all

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

_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/Pa ... ---Clement

Thanks so much!

Re: Modify values on stack in c

Posted: Tue Jan 31, 2017 9:34 am
by michaellangford
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?

Re: Modify values on stack in c

Posted: Tue Jan 31, 2017 11:11 am
by Octocontrabass
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"?

Re: Modify values on stack in c

Posted: Tue Jan 31, 2017 11:21 am
by michaellangford
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.