trouble with multitasking and user mode

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
User avatar
ahmedhalawa
Member
Member
Posts: 28
Joined: Wed Jul 02, 2008 9:28 am

trouble with multitasking and user mode

Post by ahmedhalawa »

erro when trying to run multi tasking and user mode
images(int attachment)will explain every thing
gdt code:

Code: Select all

struct gdt_entry
{
	ushort limit;
    ushort base;
	uchar  base_1;
	uchar  type:4;
	uchar  syst:1;
	uchar  dpl:2;
	uchar  pres:1;
	uchar  limit_2:4;
	uchar  bit64:1;
    uchar  bit641:1;
	uchar  d_b:1;
	uchar  gran:1;
	uchar  base_2;
}__attribute__((packed));
/*********************/
void gdttable::setgate(int num,int dpl,bool code,int pres)
{   /** entry limite **/
	gdt[num].limit  = 0xFFFF;
	gdt[num].limit_2= 0xF;
	/** entry base   **/
	gdt[num].base   = 0;
	gdt[num].base_1 = 0;
	gdt[num].base_2 = 0;
	/** bit 40:48   **/
	gdt[num].pres   = pres;
	gdt[num].dpl    = dpl&3;
	gdt[num].syst   = 1;
	gdt[num].type   = code?0xA:0x2;
	/** bit 52:56   **/
    gdt[num].gran   = 1;
	gdt[num].d_b    = 1;
	gdt[num].bit64  = 1;
	gdt[num].bit641 =0;
}
void gdttable::install()
{
	/**  Null entry **/
	memset((uchar*)&gdt ,0,sizeof(gdt_entry));
	/**S Code entry **/
	gdttable::setgate(1,0,true,1);
	/**S  Data entry **/
	gdttable::setgate(2,0,false,1);
    /**u  code entry **/
	gdttable::setgate(3,3,true,1);
    /**u  Data entry **/
	gdttable::setgate(4,3,false,1);

    /** Install it **/
	gdtp.limit =  (sizeof(gdt_entry)*5) -1;
	gdtp.address =(uint)&gdt;
	 asm volatile ("lgdt %0"::"m"(gdtp));
     asm volatile ("ljmp $8,$1f");
     asm volatile ("1:");
     asm volatile ("mov $16,%ax");
	 asm volatile ("mov %ax,%es");
	 asm volatile ("mov %ax,%ds");
	 asm volatile ("mov %ax,%gs");
	 asm volatile ("mov %ax,%fs");
	 asm volatile ("mov %ax,%ss");
}


multitasking.cpp

Code: Select all

struct task
{
    bool isp;
    bool iss;
    uint rsp; //start of stack
    uint esp; //real stack
};
/*********/
task tasks[20];int c_task = -1;
/***************************************/
extern "C" int change_task(uint oldesp)
{
    video v;outb(0x20, 0x20);
    /*******************/
    /*** for waiting   */
    if(ticks >=0)ticks++;
    /*** For multitask */
    tasks[c_task].esp = oldesp;
    if(c_task == -1)
    {
        c_task = 0;
        goto ret;
    }else{
        for(int i=0;i<=19;i++)
        {
            c_task++;
            if(tasks[c_task].isp == true)goto ret;
            if(c_task == 19)c_task=-1;
        }
        c_task = 0;
        goto ret;
    }
ret:
    return tasks[c_task].esp;
}
void multitask::install_multitask()
{
    int b= 1193180/50;
 	outb(0x43 , 0x36);
	outb(0x40 ,b&0xff);
	outb(0x40 ,b >> 8);
    for(int i=0;i<20;i++)tasks[i].isp=false;
    for(int i=0;i<20;i++)tasks[i].iss= true;
    creat_task(0,(uint)&idle ,0x1000,true);
    creat_task(1,(uint)&idle1,0x1000,false);
}
void multitask::creat_task(int id,uint add,int stacksz,bool iss)
{
    kheap kh;
    if(tasks[id].isp == false)
    {
        tasks[id].rsp = kh.alloc(stacksz);
        uint *stack = (uint*)tasks[id].rsp+stacksz-1;
        *--stack =0x0   ;
        *--stack =0x0   ;
        *--stack =0x0202;         //eflags
        *--stack =(iss)?0x8:0x1b; //cs
        *--stack =add   ;         //eip
        *--stack =0x0   ;         //edi
        *--stack =0x0   ;         //esi
        *--stack =0x0   ;         //ebp
        *--stack =0x0   ;         //unused
        *--stack =0x0   ;         //ebx
        *--stack =0x0   ;         //edx
        *--stack =0x0   ;         //ecx
        *--stack =0x0   ;         //eax
        *--stack =(iss)?0x10:0x23;//ds
        *--stack =(iss)?0x10:0x23;//es
        *--stack =(iss)?0x10:0x23;//fs
        *--stack =(iss)?0x10:0x23;//gs
        *--stack =(iss)?0x10:0x10;//ss
        tasks[id].esp = (uint)stack;
        tasks[id].isp = true;
        tasks[id].iss =(iss)?true:false;
    }
}
the errro happing when going to entr user mode
Attachments
images.zip
(41.96 KiB) Downloaded 37 times
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: trouble with multitasking and user mode

Post by gzaloprgm »

I think you are pushing wrong values into the stack, before iret'ing.

In a RING0 -> RING3 transition, iret pops 5 values: EIP, CS, EFLAGS, ESP, SS.
In a RING0 -> RING0 transition, it pops only the first 2: EIP & CS

Cheers,
Gzaloprgm
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
User avatar
ahmedhalawa
Member
Member
Posts: 28
Joined: Wed Jul 02, 2008 9:28 am

Re: trouble with multitasking and user mode

Post by ahmedhalawa »

The error still happing
this is bochs msg

Code: Select all

 fetch_raw_descriptor: GDT: index (2007)400 > limit (27)
 fetch_raw_descriptor: GDT: index (2007)400 > limit (27)
 CPU is in protected mode (active)
 CS.d_b = 32 bit
 SS.d_b = 32 bit
 EFER   = 0x00000000
 | RAX=0000000000000000  RBX=0000000000000000
 | RCX=0000000000000000  RDX=0000000000000000
 | RSP=00000000c0001fff  RBP=0000000000000000
 | RSI=0000000000000000  RDI=0000000000000000
 |  R8=0000000000000000   R9=0000000000000000
 | R10=0000000000000000  R11=0000000000000000
 | R12=0000000000000000  R13=0000000000000000
 | R14=0000000000000000  R15=0000000000000000
 | IOPL=0 id vip vif ac vm RF nt of df IF tf sf zf af pf cf
 | SEG selector     base    limit G D
 | SEG sltr(index|ti|rpl)     base    limit G D
 |  CS:001b( 0003| 0|  3) 00000000 000fffff 1 1
 |  DS:0023( 0004| 0|  3) 00000000 000fffff 1 1
 |  SS:0023( 0004| 0|  3) 00000000 000fffff 1 1
 |  ES:0023( 0004| 0|  3) 00000000 000fffff 1 1
 |  FS:0023( 0004| 0|  3) 00000000 000fffff 1 1
 |  GS:0023( 0004| 0|  3) 00000000 000fffff 1 1
 |  MSR_FS_BASE:0000000000000000
 |  MSR_GS_BASE:0000000000000000
 | RIP=0000000000102d5c (0000000000102d5c)
 | CR0=0xe0000011 CR1=0x0 CR2=0x0000000000102d5c
 | CR3=0x0009f000 CR4=0x00000000
 >> push ebp : 55
User avatar
ahmedhalawa
Member
Member
Posts: 28
Joined: Wed Jul 02, 2008 9:28 am

Re: trouble with multitasking and user mode

Post by ahmedhalawa »

which value should i put in esp (eip cs eflagsesp ss) :?:
Post Reply