OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Jul 20, 2019 7:37 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Kernel stack grows on each context switch
PostPosted: Tue Jun 25, 2019 3:13 pm 
Offline

Joined: Fri Jan 11, 2019 7:30 pm
Posts: 4
As per the title, every time the kernel switches the current process, the stack grows by exactly 28 bytes if the process is a user mode process. I have no idea what is causing this, and all of my debugging efforts have come up empty. I have no idea what is causing this, and the only fix I have come up with so far is to manually change TSS.ESP0 by adding 28.

Should this be happening, or am I doing something stupid that I didn't realize?

common IRQ handler:
Code:
irq_common:
   pusha
   push ds
   push es
   push fs
   push gs
   
   mov ax, 0x10
   mov ds, ax
   mov es, ax
   mov fs, ax
   mov gs, ax
   cld
   
   push esp
   call irq_intHandler
   add esp, 4
   
   pop gs
   pop fs
   pop es
   pop ds
   popa

   add esp, 8
   
   iret
}

process switcher:
Code:
void switchTask(regs_t *r){
   if(processList == NULL) return;
   if(processList->size <= 1) return;

   memcpy(&(currentProcess->task->state), r, sizeof(regs_t));

switchTask_nextproc:
   nextProcess = nextProcess->next;
   if(nextProcess == NULL){
      nextProcess = processList->tail;   
   }
   currentProcess = nextProcess->data;
   if(currentProcess->sleepCounter > 0){
      goto switchTask_nextproc;
   }
   
   memcpy(r, &(currentProcess->task->state), sizeof(regs_t));
   
   pg_switchDir(currentProcess->task->dir);   

   tss_setStack(r->esp);
}


Any help would be greatly appreciated

_________________
Working on modetOS https://www.github.com/Crupette/modetOS


Top
 Profile  
 
 Post subject: Re: Kernel stack grows on each context switch
PostPosted: Wed Jun 26, 2019 6:42 am 
Offline

Joined: Wed Jun 26, 2019 6:17 am
Posts: 1
Location: Netherlands
Code:
tss_setStack(r->esp);


r->esp in this call appears to be the value of ESP at the time of your PUSHA instruction, i.e. after (user)ss, (user)esp, eflags, cs, eip, err and num (28 bytes) have been pushed on the kernel stack. The next interrupt frame will be placed on top of this.

I think you should be able to set the TSS esp to the bottom of the kernel stack of the task you are switching to: there shouldn't be any code running on that stack when you are back in user-mode.


Top
 Profile  
 
 Post subject: Re: Kernel stack grows on each context switch
PostPosted: Wed Jun 26, 2019 2:14 pm 
Offline

Joined: Fri Jan 11, 2019 7:30 pm
Posts: 4
Thank you for your help!
I fixed my problem by adding a kstack_bottom variable to the task, and setting the TSS.ESP0 to the variable
at least I learned more about being careful with the stack

_________________
Working on modetOS https://www.github.com/Crupette/modetOS


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], GMorgan and 7 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