Page 1 of 2

Multitasking

Posted: Sat Dec 27, 2008 3:10 am
by System123
Just a few questions on multitasking:

- Is there a set way to implement software multitasking or can I 'make it up'?
- Is it necessary to clone page directories for software multitasking?

Thanks

Re: Multitasking

Posted: Sat Dec 27, 2008 3:53 am
by dosfan
I prefer to call it context switching.

There are no fixed rules on implementing software based multitasking. You are only restricted by the design of the hardware :)

It boils down to somehow saving the context of one thread and switching to another. Personally I use the stack-based method on x86. Interesting registers are pushed onto the stack upon interrupt, current esp is saved, esp set to esp of next thread and then iret to restore next 'saved' context. (I use paging so page directory is also swapped)

Cloning of page directories is really dependent on your virtual memory design... ie. does each process have it's own address space or not?

Re: Multitasking

Posted: Tue Dec 30, 2008 1:53 am
by leledumbo
Check how A2 (formerly BlueBottle) does it. It doesn't use any context switching for multitasking, instead ... just read it.

Re: Multitasking

Posted: Wed Dec 31, 2008 12:04 am
by System123
Is it recommended to clone your page directory to give each task its own address space? And if I do clone my age directories what must be cloned? I know the Kernel Stack and Code areas but does anything else need to be? Also do I change the memory map of the cloned direcory ie: so virtual address 0xC0000000 which points to physical address 0x100000 in the one page directory will now point to 0x20000? So I don't overwrite code.

Just another question quick. How would the following C code be defined in Pascal? I have my paging working but the way it works is going to make my life hard in the end.

Code: Select all

typedef struct page
{
   u32int present    : 1;   // Page present in memory
   u32int rw         : 1;   // Read-only if clear, readwrite if set
   u32int user       : 1;   // Supervisor level only if clear
   u32int accessed   : 1;   // Has the page been accessed since last refresh?
   u32int dirty      : 1;   // Has the page been written to since last refresh?
   u32int unused     : 7;   // Amalgamation of unused and reserved bits
   u32int frame      : 20;  // Frame address (shifted right 12 bits)
} page_t;
Thanks

Re: Multitasking

Posted: Wed Dec 31, 2008 2:47 am
by ru2aqare
System123 wrote:Just another question quick. How would the following C code be defined in Pascal? I have my paging working but the way it works is going to make my life hard in the end.

Code: Select all

typedef struct page
{
   u32int present    : 1;   // Page present in memory
   u32int rw         : 1;   // Read-only if clear, readwrite if set
   u32int user       : 1;   // Supervisor level only if clear
   u32int accessed   : 1;   // Has the page been accessed since last refresh?
   u32int dirty      : 1;   // Has the page been written to since last refresh?
   u32int unused     : 7;   // Amalgamation of unused and reserved bits
   u32int frame      : 20;  // Frame address (shifted right 12 bits)
} page_t;
Thanks

Code: Select all

Const
  dwPresent = (1 Shl 0);
  dwReadWrite = (1 Shl 1);
  dwUser = (1 Shl 2);
  etc
Type
  PPage = ^TPage;
  TPage = Record
    Value : Dword;
  End;

Function IsPagePresent(page : PPage) : Boolean
Begin
  IsPagePresent = (page^.Value and dwPresent) <> 0;
End;

Procedure SetPagePresent(page : PPage, value : Boolean)
Begin
  If (value) Then
    page^.Value = page^.Value Or dwPresent;
  Else
    page^.Value = page^.Value And Not dwPresent;
End;

Or something similar. Pascal doesn't have bit fields (at least Borland Pascal doesn't have, don't know about the BP clones)

Re: Multitasking

Posted: Sun Jan 04, 2009 10:08 am
by System123
Ok I am really battling with this. I am trying to setup the most basic multitasking system for now. I think the problem with mine arises either when I change esp, I think it changes back due to pascal. OR I am loading the wrong EIP. I really don't know where to go. I am trying to implement stack based switching. Is there another way of doing it, Not stack based??

Re: Multitasking

Posted: Sun Jan 04, 2009 11:40 am
by ru2aqare
System123 wrote:Ok I am really battling with this. I am trying to setup the most basic multitasking system for now. I think the problem with mine arises either when I change esp, I think it changes back due to pascal. OR I am loading the wrong EIP. I really don't know where to go. I am trying to implement stack based switching. Is there another way of doing it, Not stack based??
There is the TSS-based (hardware-assisted) task switching, but the problem of changing stacks won't go away entirely.

Re: Multitasking

Posted: Sun Jan 04, 2009 10:53 pm
by System123
I would prefer to stay with software based switching. I think the problem is pascal. Pascal pushes the esp at the start of a procedure and then pops it off at the end. even though I have specified NOSTACKFRAME it still seems to be doing this. I might have to write my switching code in assembler. So I guess I will have to learn some more assembler.

Re: Multitasking

Posted: Sun Jan 04, 2009 11:44 pm
by System123
How would this be done in Pascal?

Code: Select all

void move_stack(void *new_stack_start, u32int size)
I know how to do everything except then

Code: Select all

void *new_stack_start
part. What is a void variable?

Re: Multitasking

Posted: Sun Jan 04, 2009 11:59 pm
by Zenith
A void pointer variable is an uncasted pointer to some data in memory.

Re: Multitasking

Posted: Mon Jan 05, 2009 2:12 am
by leledumbo
How would this be done in Pascal?
renamed using Pascal casing:

Code: Select all

procedure MoveStack(NewStackStart: Pointer; Size: LongWord);
Size can also be DWord or PtrUInt or whatever that equals to 32-bit unsigned integer.

Re: Multitasking

Posted: Mon Jan 05, 2009 2:00 pm
by System123
Thanks. I thought it was something like that.

Re: Multitasking

Posted: Tue Jan 06, 2009 8:11 am
by jal
System123 wrote:I think the problem is pascal. Pascal pushes the esp at the start of a procedure and then pops it off at the end.
Of course it does, it needs to remove the local variables of the stack.
even though I have specified NOSTACKFRAME it still seems to be doing this.
I don't know what compiler you are using, but in Borland Pascal there were various keywords for playing around with stackframes and the like. I remember 'interrupt;' and 'assembler;'.
I might have to write my switching code in assembler.
Of course it must be done in assembler, there's no way to avoid that, regardless of the high level language you use.


JAL

Re: Multitasking

Posted: Tue Jan 06, 2009 9:33 am
by System123
jal wrote:Of course it must be done in assembler, there's no way to avoid that, regardless of the high level language you use

Well I just did it in Pascal now. The Timer interrupt is handled with assembler. That I know I was referring to my scheduler code.

Only Problem I am having now is that the multitasker switched tasks 7 times then randomly page faults. I am trying to find the source of the fault, looks like it happens when I print to my screen, even though I have done that alot.

Re: Multitasking

Posted: Tue Jan 06, 2009 11:47 am
by System123
Can someone please explain these two Bochs errors to me.

1:

Code: Select all

>>PANIC<< prefetch: getHostMemAddr vetoed direct read, pAddr=0x000b80a0
2:

Code: Select all

00011919940i[BIOS ] int13_harddisk: function 41, unmapped device for ELDL=80
00011924720i[BIOS ] int13_harddisk: function 08, unmapped device for ELDL=80
00011929375i[BIOS ] *** int 15h function AX=00c0, BX=0000 not yet supported!
Thanks

Regards
System123