(Solved) Perplexing Multitasking

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
adamh
Posts: 2
Joined: Sun Dec 23, 2007 2:32 am

(Solved) Perplexing Multitasking

Post by adamh »

So, loong time reader, first time (in 3 years) poster.

I'm working on getting basic multitasking/threading going. It's working ok. I can jump around from task a, b, c, when I break() in each task. However, when I throw preemptive multitasking in, it doesn't work right. I've been working on this problem the past week and need some direction / help. Everything (so far) runs in kernel space. Some observations:

* When there are no breaks() other then the one initiated by the timer's isr, things will run fine for a few seconds and then tripple fault.
* When there are only breaks() in each tasks code, and none in the timer's isr, then things work as expected (however not as ultimately desired)
* When there are only breaks() in each tasks code, and a break() in my get_char() routine (while waiting for a keypress), a predefined and constant value of an integer, which is set programatically at runtime, changes.
* The stack won't stick - I can't seem to get each task's stack pointer to stay. When I set ESP to say 5000 for task 1, ESP will later appear around 1000, where 1000 being where my kernel's stack resides. HOWEVER, esp does reflect the correct set value when only the timer has a break() in it.

As you can no doubt tell, I'm quite perplexed and any help is appreciated. If you need to see more code, I can post.

Thanks much!


Here's my keyboard code:

Here's my ISR code (implemented as a macro w/nasm):

Code: Select all

        push	0					; Error No
	push	i					; Interrupt / Exception No
	pusha
		push	gs
		push	fs
		push	es
		push	ds
			mov	eax, 0x10		; Set to CPL 0 selectors
			mov	ds, eax
			mov	es, eax
			cld
			mov	eax, esp		; EAX = Stack pointer, ie: register structure
			push	eax			; Push pointer to register structure
			call	interrupt_dispatcher
			pop	eax
		pop	ds
		pop	es
		pop	fs
		pop	gs
	popa
	add	esp, 8					; Do away with error no and interrupt no
	iretd

Here's my task switching code:

Code: Select all

			t[cur_task].r.gs = r->gs;			// Copy over the registers into a saved state
			t[cur_task].r.fs = r->fs;
			t[cur_task].r.es = r->es;
			t[cur_task].r.ds = r->ds;
			t[cur_task].r.eax = r->eax;
			t[cur_task].r.ecx = r->ecx;
			t[cur_task].r.edx = r->edx;
			t[cur_task].r.ebx = r->ebx;
			t[cur_task].r.esp = r->esp;
			t[cur_task].r.ebp = r->ebp;
			t[cur_task].r.esi = r->esi;
			t[cur_task].r.edi = r->edi;
			t[cur_task].r.int_no = r->int_no;
			t[cur_task].r.error_code = r->error_code;
			t[cur_task].r.eip = r->eip;
			t[cur_task].r.cs = r->cs;
			t[cur_task].r.eflags = r->eflags;
			t[cur_task].r.user_esp = r->user_esp;
			t[cur_task].r.user_ss = r->user_ss;

			if( cur_task == no_tasks - 1) {			// Iterate through the tasks based on no_tasks
				cur_task = 0;
			} else {
				cur_task++;
			}
			
			sched_force_switch = 0;				// Reset PIT force switch

			r->gs = t[cur_task].r.gs;		// Copy over the new task no to the current register set that
			r->fs = t[cur_task].r.fs;		// will be poped off the stack upon compleation.
			r->es = t[cur_task].r.es;
			r->ds = t[cur_task].r.ds;
			r->eax = t[cur_task].r.eax;
			r->ecx = t[cur_task].r.ecx;
			r->edx = t[cur_task].r.edx;
			r->ebx = t[cur_task].r.ebx;
			r->esp = t[cur_task].r.esp;
			r->ebp = t[cur_task].r.ebp;
			r->esi = t[cur_task].r.esi;
			r->edi = t[cur_task].r.edi;
			r->int_no = t[cur_task].r.int_no;
			r->error_code = t[cur_task].r.error_code;
			r->eip = t[cur_task].r.eip;
			r->cs = t[cur_task].r.cs;
			r->eflags = t[cur_task].r.eflags;
			r->user_esp = t[cur_task].r.user_esp;
			r->user_ss = t[cur_task].r.user_ss;
Here's my timer isr, for what its worth:

Code: Select all

void timer_irh( registers *r ) {
	if( sched_force_switch == 10 ) {
		sched_force_switch = 0;
		r->error_code = D_syscall_break;
		sys_call(r);
	} else { 
		sched_force_switch++;
	}
	
	outportb( 0x20, 0x20 );
}
Here's my keyboard irh:

Code: Select all

void keyboard_irh( void ) {
	unsigned int		scan_code = 0;
	int			i;
	static int		pb_handler = 0;

	scan_code = inportb( 0x60 );
	
	scan_code = scan_code & 0xFF;
	
	// Grab the pause/break key here...
	if( pb_handler > 0 ) {
		pb_handler++;
		if( pb_handler == 6 ) {
			pb_handler = 0;
			// Call debugger handler
			printf( "\nDebugger would be called here.\n" );
		}
	} else if ( scan_code == 0xE1 ) {
		pb_handler = 1;
	} else {
		enque_scancode( &key_que, scan_code );
	}

	
	i = inportb( 0x61 );
	outportb( 0x61, i | 0x80 );
	outportb( 0x61, i & 0x7F );
	
	outportb( 0x20, 0x20 );
}
P/S Notes:

* Interrupts are disabled starting at the beginning of my master ISR (interrupt_dispatcher), enabled after the given isr has returned
* Memory model, gdt, etc... are flat. Works as expected.
Last edited by adamh on Sun Dec 30, 2007 2:47 am, edited 1 time in total.
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Post by ucosty »

Find the osdever tutorial on software task switching and dump your register switching code. It is a broken design and can't be fixed.
The cake is a lie | rackbits.com
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

you probably want to have a pop esp, instead of pop eax after the call?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
adamh
Posts: 2
Joined: Sun Dec 23, 2007 2:32 am

Post by adamh »

Thanks for the replies.

OSDever had a tutorial I hadn't looked at in a while, which (combined with combuster's esp suggestion) got me looking very closely at what was happening to my stack before, during, and after the initial execution of the context switching. Turns out, the stack wasn't moving out of the kernel's stack, and while switching was done right, it was always switching to the wrong thing.

I was luckily, for now, able to keep the register switching code. I'm interested to know though, what's broken about its design?

Thanks for the points, always a good thing after a weeks of work pays off!

Best,

adamh.
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Q

Post by urxae »

adamh wrote:I was luckily, for now, able to keep the register switching code. I'm interested to know though, what's broken about its design?
It seems to assume both the task being switched from and the task being switched to are user-level (ring 3) tasks (it saves and restores user_ss and user_esp unconditionally, while ring-0 tasks won't have those on the stack).
This means that if you're switching from a ring-0 task some data from the stack (or just after it) overwrites task[cur].r.user_ss and user_esp, which may cause trouble if that task later returns to ring-3 code.
On the other hand, if you're switching to a ring-0 task, the top two items on its stack (or data just after it, if there's less than two items on it) are overwritten by r.user_ss and r.user_esp. Also very bad.

The best way to switch tasks that I know of is using an assembly-only function that saves all callee-save registers (all but eax, ecx & edx on x86, IIRC) to a task-specific data structure, then loads the values for the next task. This is a bit tricky, since one of the registers to save & restore is esp, and the parameter telling you which task to switch to is typically on the stack (this is where not having to restore caller-save registers may come in handy).
Afterwards, it just returns using the return address on the new stack, jumping back to where that task was last interrupted (typically in an interrupt handler or system call, in kernel mode). If you're calling this function from an IRQ handler (typically for IRQ 0, the timer), you may want to ack() that IRQ before switching or you may not get any new ones (since there's no guarantee the new task was switched from during that IRQ handler as well, and may thus not ack() it itself).
The stack of a new thread should look like the stack of a thread that's just been switched away from, with values for callee-save registers as stored by above function and a return address (and optionally extra data after that for the code it's "returning" to).
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Post by ucosty »

Plain stack switching will be a lot faster. It also involves a lot less code.
The cake is a lie | rackbits.com
Post Reply