Some questions - tasks, the stack

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.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Some questions - tasks, the stack

Post by egos »

You should save/restore general purpose and segment registers within interrupt handler. And I think you will have some problems with supporting user mode while using that task switching technique.

Look at my simplified example. Maybe it will be useful for you.

Code: Select all

struc TS ; ThreadStructure
  {
  .pred dd ?
  .next dd ?
  .stackpointer dd ?
  .activepriority dd ?
  .priority dd ?
  }

  proc SwitchToNext ; cs=KCODE, ds=es=ss=KDATA
  push fs
  push gs
  pushf
  cli
;  mov eax,[TSS.sp0]
  mov [eax+TS.stackpointer],esp
  mov eax,[eax+TS.next]
  mov esp,[eax+TS.stackpointer]
  mov [TSS.sp0],eax
  popf
  pop gs
  pop fs
  ret

  proc IRQ0Handler
  pusha
  push ds
  push es

  mov al,20h
  out 20h,al

  push ss
  pop ds ; ds <- KDATA
  push ss
  pop es ; es <- KDATA

  mov eax,[TSS.sp0]

;  dec,[eax+TS.activepriority]
;  jnz @f
;  mov ecx,[eax+TS.priority]
;  mov [eax+TS.activepriority],ecx

;  cmp eax,[eax+TS.next]
;  je @f

  call SwitchToNext
@@:
  pop es
  pop ds
  popa
  iret
Initial stack frame for kernel thread:

Code: Select all

TS+16:  PRIORITY
TS+12:  PRIORITY
TS+08:  TS-16
TS+04:  ? ; undefined before InsertTSInRunQueue operation
TS+00:  ? ; undefined before InsertTSInRunQueue operation
TS-04:  START
TS-08:  FS
TS-12:  GS
TS-16:  EFLAGS
Last edited by egos on Tue Aug 02, 2011 12:27 pm, edited 1 time in total.
If you have seen bad English in my words, tell me what's wrong, please.
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Some questions - tasks, the stack

Post by Luns »

#-o of course! I use the general registers in the ISR, so clearly I need to save them before that. Saving them after that saves the modified values, like what was happening before with ESP.

It seems so obvious now, but I'm not sure if I would have ever figured that out on my own. Thank you so much for helping me out!

If you don't mind though, what do you think the problems will be with supporting user mode?
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Some questions - tasks, the stack

Post by egos »

Luns wrote:If you don't mind though, what do you think the problems will be with supporting user mode?
If timer interrupt will occur in user mode then kernel stack will contain return address for user mode (user mode code selector, offset in user space) and moreover stack pointer for user mode. In this case you can't jump to the return address. I also mentioned about saving/restoring segment registers for supporting user mode because in kernel mode you should use them with specific values to be assured of their correctness.
If you have seen bad English in my words, tell me what's wrong, please.
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Some questions - tasks, the stack

Post by Luns »

Ah, alright. I don't plan on implementing a user mode for quite some time, so I won't worry about that now.

Thanks again for all your help!
Post Reply