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
gmoney
Member
Member
Posts: 106
Joined: Mon Jan 03, 2005 12:00 am
Location: Texas, Usa

multitasking

Post by gmoney »

I added multitasking code to my kernel but it doesn't work :(
can yall look at my code and tell me what im doing wrong
Attachments
alien os.zip
(43.41 KiB) Downloaded 93 times
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

gmoney wrote:I added multitasking code to my kernel but it doesn't work
In what way doesn't it work?


From looking at your code I don't think you set up any task switch code? You add the tasks to the list (assuming you uncomment SetupScheduler in main) but I can't see anywhere where you actually switch to the next task. Its not in the timer function, for example.

Also, in TaskAdd:

Code: Select all

tmpTASK->Ss  = (uint32_t) stackbase;
stackbase is the linear address of the bottom of your stack. The stack selector should be a data segment. As these are PL0 tasks, try:

Code: Select all

tmpTASK->Ss = 0x10
Although that shouldn't cause a problem (yet) as I can't see you use it anywhere.

I could easily have missed something though. Its sometimes not that easy to get your head around someone else's code without, hint, an idea of where the problem is.

For anyone that's interested, the scheduler code is in task/multi.c

Regards,
John.
gmoney
Member
Member
Posts: 106
Joined: Mon Jan 03, 2005 12:00 am
Location: Texas, Usa

reply

Post by gmoney »

i added it in the start.asm

Code: Select all

; 32: IRQ0
_irq0:
    cli
    push byte 0
    push byte 32
     push 	ds
	push 	es
	push 	fs
	push 	gs
	
	;call	_IRQ0
	
	mov		eax, esp
	push 	eax
	
	call	_Scheduler
	
	mov		esp, eax
	
	pop		gs
	pop		fs
	pop		es
	pop		ds
	
    jmp irq_common_stub
gmoney
Member
Member
Posts: 106
Joined: Mon Jan 03, 2005 12:00 am
Location: Texas, Usa

Post by gmoney »

the problem is none of the task are working, and i dont know why
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

Just changing esp does not a task switch make.

At the start of the task switch, you need to push all the registers in the order that you did in the TaskAdd function.

Then, you push the old esp, call Scheduler, restore the new esp.

Then, pop all the registers in the reverse order in which you initially pushed them.

Finally iret.

You can include a call to irq_common_stub just before the iret though.

You don't need cli if using interrupt gates. (I also note you didn't sti at the end).

e.g.

Code: Select all

push ebp
push esp
push edi
push esi
push edx
push ecx
push ebx
push eax
push ds
push es
push fs
push gs

push esp
call _Scheduler
mov esp, eax

pop gs
pop fs
pop es
pop ds
pop eax
pop ebx
pop ecx
pop edx
pop esi
pop edi
pop esp
pop ebp

; optional
push dword 0
push dword 32
jmp irq_common_stub

; if you don't use the above, you need:
iret
Regards,
John.
gmoney
Member
Member
Posts: 106
Joined: Mon Jan 03, 2005 12:00 am
Location: Texas, Usa

Post by gmoney »

I tried it but it still doesn't work, ima move the this form to my website so i can keep a closer watch on it. http://www.lpsoft.us
gmoney
Member
Member
Posts: 106
Joined: Mon Jan 03, 2005 12:00 am
Location: Texas, Usa

Post by gmoney »

start.asm

Code: Select all

; This is the kernel's entry point. We could either call main here,
; or we can use this to setup the stack or other nice stuff, like
; perhaps setting up the GDT and segments. Please note that interrupts
; are disabled at this point: More on interrupts later!
[BITS 32]
global start
start:
    mov esp, _sys_stack     ; This points the stack to our new stack area
    jmp stublet

; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
    ; Multiboot macros to make a few lines later more readable
    MULTIBOOT_PAGE_ALIGN	equ 1<<0
    MULTIBOOT_MEMORY_INFO	equ 1<<1
    MULTIBOOT_AOUT_KLUDGE	equ 1<<16
    MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
    MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
    MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
    EXTERN code, bss, end

    ; This is the GRUB Multiboot header. A boot signature
    dd MULTIBOOT_HEADER_MAGIC
    dd MULTIBOOT_HEADER_FLAGS
    dd MULTIBOOT_CHECKSUM
    
    ; AOUT kludge - must be physical addresses. Make a note of these:
    ; The linker script fills in the data for these ones!
    dd mboot
    dd code
    dd bss
    dd end
    dd start

; This is an endless loop here. Make a note of this: Later on, we
; will insert an 'extern _main', followed by 'call _main', right
; before the 'jmp $'.
stublet:
    extern _main
    extern __main
extern __atexit

call __main
call _main 			; Call our C++ code
call __atexit
    jmp $
[global _read_cr0]
_read_cr0:
	mov eax, cr0
	retn

[global _write_cr0]
_write_cr0:
	push ebp
	mov ebp, esp
	mov eax, [ebp+8]
	mov cr0,  eax
	pop ebp
	retn

[global _read_cr3]
_read_cr3:
	mov eax, cr3
	retn

[global _write_cr3]
_write_cr3:
	push ebp
	mov ebp, esp
	mov eax, [ebp+8]
	mov cr3, eax
	pop ebp
	retn
; This will set up our new segment registers. We need to do
; something special in order to set CS. We do what is called a
; far jump. A jump that includes a segment as well as an offset.
; This is declared in C as 'extern void gdt_flush();'
global _gdt_flush
extern _gp
_gdt_flush:
    lgdt [_gp]
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2
flush2:
    ret

; Loads the IDT defined in '_idtp' into the processor.
; This is declared in C as 'extern void idt_load();'
global _idt_load
extern _idtp
_idt_load:
    lidt [_idtp]
    ret

; In just a few pages in this tutorial, we will add our Interrupt
; Service Routines (ISRs) right here!
global _isr0
global _isr1
global _isr2
global _isr3
global _isr4
global _isr5
global _isr6
global _isr7
global _isr8
global _isr9
global _isr10
global _isr11
global _isr12
global _isr13
global _isr14
global _isr15
global _isr16
global _isr17
global _isr18
global _isr19
global _isr20
global _isr21
global _isr22
global _isr23
global _isr24
global _isr25
global _isr26
global _isr27
global _isr28
global _isr29
global _isr30
global _isr31

;  0: Divide By Zero Exception
_isr0:
    cli
    push byte 0
    push byte 0
    jmp isr_common_stub

;  1: Debug Exception
_isr1:
    cli
    push byte 0
    push byte 1
    jmp isr_common_stub

;  2: Non Maskable Interrupt Exception
_isr2:
    cli
    push byte 0
    push byte 2
    jmp isr_common_stub

;  3: Int 3 Exception
_isr3:
    cli
    push byte 0
    push byte 3
    jmp isr_common_stub

;  4: INTO Exception
_isr4:
    cli
    push byte 0
    push byte 4
    jmp isr_common_stub

;  5: Out of Bounds Exception
_isr5:
    cli
    push byte 0
    push byte 5
    jmp isr_common_stub

;  6: Invalid Opcode Exception
_isr6:
    cli
    push byte 0
    push byte 6
    jmp isr_common_stub

;  7: Coprocessor Not Available Exception
_isr7:
    cli
    push byte 0
    push byte 7
    jmp isr_common_stub

;  8: Double Fault Exception (With Error Code!)
_isr8:
    cli
    push byte 8
    jmp isr_common_stub

;  9: Coprocessor Segment Overrun Exception
_isr9:
    cli
    push byte 0
    push byte 9
    jmp isr_common_stub

; 10: Bad TSS Exception (With Error Code!)
_isr10:
    cli
    push byte 10
    jmp isr_common_stub

; 11: Segment Not Present Exception (With Error Code!)
_isr11:
    cli
    push byte 11
    jmp isr_common_stub

; 12: Stack Fault Exception (With Error Code!)
_isr12:
    cli
    push byte 12
    jmp isr_common_stub

; 13: General Protection Fault Exception (With Error Code!)
_isr13:
    cli
    push byte 13
    jmp isr_common_stub

; 14: Page Fault Exception (With Error Code!)
_isr14:
    cli
    push byte 14
    jmp isr_common_stub

; 15: Reserved Exception
_isr15:
    cli
    push byte 0
    push byte 15
    jmp isr_common_stub

; 16: Floating Point Exception
_isr16:
    cli
    push byte 0
    push byte 16
    jmp isr_common_stub

; 17: Alignment Check Exception
_isr17:
    cli
    push byte 0
    push byte 17
    jmp isr_common_stub

; 18: Machine Check Exception
_isr18:
    cli
    push byte 0
    push byte 18
    jmp isr_common_stub

; 19: Reserved
_isr19:
    cli
    push byte 0
    push byte 19
    jmp isr_common_stub

; 20: Reserved
_isr20:
    cli
    push byte 0
    push byte 20
    jmp isr_common_stub

; 21: Reserved
_isr21:
    cli
    push byte 0
    push byte 21
    jmp isr_common_stub

; 22: Reserved
_isr22:
    cli
    push byte 0
    push byte 22
    jmp isr_common_stub

; 23: Reserved
_isr23:
    cli
    push byte 0
    push byte 23
    jmp isr_common_stub

; 24: Reserved
_isr24:
    cli
    push byte 0
    push byte 24
    jmp isr_common_stub

; 25: Reserved
_isr25:
    cli
    push byte 0
    push byte 25
    jmp isr_common_stub

; 26: Reserved
_isr26:
    cli
    push byte 0
    push byte 26
    jmp isr_common_stub

; 27: Reserved
_isr27:
    cli
    push byte 0
    push byte 27
    jmp isr_common_stub

; 28: Reserved
_isr28:
    cli
    push byte 0
    push byte 28
    jmp isr_common_stub

; 29: Reserved
_isr29:
    cli
    push byte 0
    push byte 29
    jmp isr_common_stub

; 30: Reserved
_isr30:
    cli
    push byte 0
    push byte 30
    jmp isr_common_stub

; 31: Reserved
_isr31:
    cli
    push byte 0
    push byte 31
    jmp isr_common_stub


; We call a C function in here. We need to let the assembler know
; that '_fault_handler' exists in another file
extern _fault_handler

; This is our common ISR stub. It saves the processor state, sets
; up for kernel mode segments, calls the C-level fault handler,
; and finally restores the stack frame.
isr_common_stub:
    pusha
    push ds
    push es
    push fs
    push gs
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp
    push eax
    mov eax, _fault_handler
    call eax
    pop eax
    pop gs
    pop fs
    pop es
    pop ds
    popa
    add esp, 8
    iret

global _irq0
global _irq1
global _irq2
global _irq3
global _irq4
global _irq5
global _irq6
global _irq7
global _irq8
global _irq9
global _irq10
global _irq11
global _irq12
global _irq13
global _irq14
global _irq15
extern _Scheduler
; 32: IRQ0
_irq0:
    cli
push ebp
push esp
push edi
push esi
push edx
push ecx
push ebx
push eax
push ds
push es
push fs
push gs

push esp
call _Scheduler
mov esp, eax

pop gs
pop fs
pop es
pop ds
pop eax
pop ebx
pop ecx
pop edx
pop esi
pop edi
pop esp
pop ebp 
	
    jmp irq_common_stub

; 33: IRQ1
_irq1:
    cli
    push byte 0
    push byte 33
    jmp irq_common_stub

; 34: IRQ2
_irq2:
    cli
    push byte 0
    push byte 34
    jmp irq_common_stub

; 35: IRQ3
_irq3:
    cli
    push byte 0
    push byte 35
    jmp irq_common_stub

; 36: IRQ4
_irq4:
    cli
    push byte 0
    push byte 36
    jmp irq_common_stub

; 37: IRQ5
_irq5:
    cli
    push byte 0
    push byte 37
    jmp irq_common_stub

; 38: IRQ6
_irq6:
    cli
    push byte 0
    push byte 38
    jmp irq_common_stub

; 39: IRQ7
_irq7:
    cli
    push byte 0
    push byte 39
    jmp irq_common_stub

; 40: IRQ8
_irq8:
    cli
    push byte 0
    push byte 40
    jmp irq_common_stub

; 41: IRQ9
_irq9:
    cli
    push byte 0
    push byte 41
    jmp irq_common_stub

; 42: IRQ10
_irq10:
    cli
    push byte 0
    push byte 42
    jmp irq_common_stub

; 43: IRQ11
_irq11:
    cli
    push byte 0
    push byte 43
    jmp irq_common_stub

; 44: IRQ12
_irq12:
    cli
    push byte 0
    push byte 44
    jmp irq_common_stub

; 45: IRQ13
_irq13:
    cli
    push byte 0
    push byte 45
    jmp irq_common_stub

; 46: IRQ14
_irq14:
    cli
    push byte 0
    push byte 46
    jmp irq_common_stub

; 47: IRQ15
_irq15:
    cli
    push byte 0
    push byte 47
    jmp irq_common_stub

extern _irq_handler

irq_common_stub:
    pusha
    push ds
    push es
    push fs
    push gs

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp

    push eax
    mov eax, _irq_handler
    call eax
    pop eax

    pop gs
    pop fs
    pop es
    pop ds
    popa
    add esp, 8
    iret

; Here is the definition of our BSS section. Right now, we'll use
; it just to store the stack. Remember that a stack actually grows
; downwards, so we declare the size of the data before declaring
; the identifier '_sys_stack'
SECTION .bss
    resb 8192               ; This reserves 8KBytes of memory here
_sys_stack:

multi.c

Code: Select all


#include <task/multi.h>
#include <sys/error.h>
#include <mm/mm.h>
#include <task/list.h>


/*------------GLOBAL VARS----------------*/
List 	*TaskList;
uint32_t TaskID;
uint8_t	 initialized = false;
TASK 	*CurrentTask;

////////////////////START REMOVE AFTER TESTING
void task1();
void task2();
//////////////////////////////////////////////////////////////END REMOVING


//This one setup the Scheduler. It must setup at least 2 tasks
// IDLE -> don't make anything
// And surely one task for the Kernel wich is executed first.
// The Kernel task is a litle bit special because he is runing till now
// and needn't a extra new stack 0.o
uint8_t SetupScheduler()
{
	TaskList = NewList();
	TaskID = 0;
	CurrentTask = NULL;
	
	SetupKernelTask();
	TaskAdd(task1, 		"task1", 	USER_TASK, 		NORMAL_PRIORITY);
	TaskAdd(task2, 		"task2", 	USER_TASK, 		NORMAL_PRIORITY);
//	TaskAdd(Task3, 		"task3", 	USER_TASK, 		NORMAL_PRIORITY);
	
	TaskAdd(IdleTask, 	"idle",  	KERNEL_TASK, 	SUPER_LOW_PRIORITY);
	
	
	
	initialized = true;
	
	return OK;
}

// The point of this method is to save the new stack top of the current task
// in the struct of the task. on which stack all registere were pushed.
// the select a new task and return his esp. back to the lowlevel routine
// asm pops the registers and we have loaded a new task. however, as simple 
// this one sounds, it brings problems with it...
// will be executed by isr.asm -> irq0
uint32_t Scheduler(uint32_t _oldEsp)
{
	//if the scheduler isn't right initialized return 
	//only the old esp...
	if (!initialized) 
		return _oldEsp;
	
	//this is only for the case that no Task is current running...
	if (CurrentTask != NULL)
		CurrentTask->Esp = _oldEsp; //Save the current ESP to the current task
	
	uint8_t		ratio  	= 2;
	uint32_t 	i 		= 0;
	uint32_t 	max 	= 0;
	uint32_t	weight  = 0;
	
	TASK * tmpTASK = (TASK *) ListGet(TaskList, i);
	TASK * maxTASK = NULL;
	
	while (tmpTASK != NULL)
	{	
		if (tmpTASK->State != TASK_SLEEPING) 
		{
			weight = ( tmpTASK->Priority * ratio ) + tmpTASK->Timetorun;
			if (weight > max)
			{
				max = weight;
				maxTASK = tmpTASK;
			}
				
			tmpTASK->Timetorun++;
		}	
		i++;
		tmpTASK = (TASK *) ListGet(TaskList, i);
	}
	
	maxTASK->Timetorun = 0;
	CurrentTask = maxTASK;
	
	return maxTASK->Esp;
	

}


// This is the most complicatet one. we must generate a new stack for the
// new task, also a new TAS struct, overgive the properties, add to the 
// tasklist and push register onto the stack...
uint32_t TaskAdd(void (*_entrypoint), uint8_t *_name, uint8_t _type, uint8_t _priority)
{
	TASK *tmpTASK = malloc(sizeof(TASK)); //Allocate  a new TASK struct
	
	//Seting various properties to the new TASK
	tmpTASK->ID = ++TaskID;
	tmpTASK->Priority = _priority;
	tmpTASK->Type = _type;
	tmpTASK->State = TASK_RUNNING;
	tmpTASK->Timetorun = 10;
	tmpTASK->box = msgsetup();
	//puts("task name");
//	puts(_name);
//	puts("\nbox =");
//	puts(tmpTASK->box);
	strcpy(tmpTASK->Name, _name);
	
	//Allocating space for a 4kb stack
	uint32_t *stackbase = (uint32_t *) ((uint32_t)malloc(4096) + 4096);
	uint32_t *stackptr  = stackbase;
	
	//now pushing all registers to the stack
	*--stackptr = 0x0202; //EFLAGS				 #
	*--stackptr = 0x08;	  //CS					 |---> iret
	*--stackptr = (uint32_t) _entrypoint;//		 #
	
	*--stackptr = 0; //EBP					  	 #
	*--stackptr = 0; //ESP						 |
	*--stackptr = 0; //EDI						 |
	*--stackptr = 0; //ESI						 |----- pushad
	*--stackptr = 0; //EDX						 |
	*--stackptr = 0; //ECX						 |
	*--stackptr = 0; //EBX						 |
	*--stackptr = 0; //EAX						 #
	
	*--stackptr = 0x10; //DS
	*--stackptr = 0x10; //ES
	*--stackptr = 0x10; //FS
	*--stackptr = 0x10; //GS
	
	tmpTASK->Ss  = 0x10;
	tmpTASK->Esp = (uint32_t) stackptr;
	
	ListAdd(TaskList, tmpTASK); // Add the Task to the TaskList
	
	return tmpTASK->ID;
}

uint8_t TaskWait(uint32_t _id);
uint8_t TaskSleep(uint32_t _id);
uint8_t TaskWake(uint32_t _id);
uint8_t TaskResume(uint32_t _id);
uint8_t TaskKill(uint32_t _id);
uint8_t TaskClone(uint32_t _id, uint32_t _id2);
uint8_t TaskSendName(uint32_t _id, uint32_t _id2);
uint8_t TaskSendId(uint32_t _id, uint32_t _id2);
uint8_t TaskRequestName(uint32_t _id, uint32_t _id2);
uint8_t TaskRequestId(uint32_t _id, uint32_t _id2);
uint32_t TaskBoxId()
{
 TASK *tmpTASK = TaskGetCurrent();
 return tmpTASK->box;
}
uint8_t TaskRemove(uint32_t _id)
{
	TASK *tmpTASK = TaskGetById(_id);
	
	if (tmpTASK == NULL)
		return ERR_NOT_FOUND;
		
	free(&tmpTASK->Ss);
	
	ListRemove(TaskList, tmpTASK);
	free(tmpTASK);

}

uint8_t TaskSetPriority(uint32_t _id, uint8_t _priority)
{
	TASK *tmpTASK = TaskGetById(_id);
	if (tmpTASK == NULL)
		return ERR_NOT_FOUND;
	
	tmpTASK->Priority = _priority;
	return OK;
}

uint8_t TaskSetState(uint32_t _id, uint8_t _state)
{
	TASK *tmpTASK = TaskGetById(_id);
	if (tmpTASK == NULL)
		return ERR_NOT_FOUND;
		
	tmpTASK->State = _state;
	return OK;
}

TASK * TaskGetCurrent()
{
	return CurrentTask;
}

TASK * TaskGetById(uint32_t _id)
{
	TASK *tmpTASK = NULL;
	
	uint32_t i = 0;
	
	do
	{
		tmpTASK = (TASK *) ListGet(TaskList, i);
		i++;
	} while (tmpTASK->ID != _id && i < TaskList->size); 
	
	return (TASK *) tmpTASK;
}

uint8_t SetupKernelTask()
{
	TASK *tmpTASK = malloc(sizeof(TASK)); //Allocate  a new TASK struct
	
	//Seting various properties to the new TASK
	tmpTASK->ID = ++TaskID;
	tmpTASK->Priority = NORMAL_PRIORITY;
	tmpTASK->Type = KERNEL_TASK;
	tmpTASK->State = TASK_RUNNING;
	tmpTASK->Timetorun = 0;
	
	strcpy(tmpTASK->Name, "kernel");
	
	ListAdd(TaskList, tmpTASK); // Add the Task to the TaskList
	
	CurrentTask = tmpTASK;
	return OK;
}

void IdleTask()
{
	/* This Task does hardly nothing except looping around          */
	/* This is mayby the most easy part of my os to understand.. :) */
	while(1);

}
list.c

Code: Select all

#include <task/list.h>
#include <mm/mm.h>

List * NewList()
{
	List *tmpLst = malloc(sizeof(List));
	tmpLst->size = 0;
	tmpLst->first = NULL;
	tmpLst->last = NULL;
	
	return tmpLst;

}


void ListAdd(List *_root, void *_item)
{
	ListElement *tmpLstE = malloc(sizeof(ListElement));
	
	tmpLstE->item = _item;
	tmpLstE->prev = _root->last;
	tmpLstE->next = NULL;
	
	if (_root->first == NULL) _root->first 		= tmpLstE;
	if (_root->last  != NULL) _root->last->next	= tmpLstE;
	
	_root->last = tmpLstE;
	
	_root->size++;

}


void ListRemove(List *_root, void *_item)
{	
	if (_root->size == 0)
		return;
		
	ListElement *tmpListE = ListFind(_root, _item);
	
	if (tmpListE == NULL)
		return;
	
	if (_root->first == tmpListE)
		_root->first = tmpListE->next;
		
	if (_root->last  == tmpListE)
		_root->last  = tmpListE->prev;
		
	if (tmpListE->next != NULL)
		tmpListE->next->prev = tmpListE->prev;
	
	if (tmpListE->prev != NULL)
		tmpListE->prev->next = tmpListE->next;
		
	free(tmpListE);
	
	_root->size--;
	
}

ListElement * ListFind(List *_root, void *_item)
{
	ListElement *tmpListE = _root->first;
	
	while (tmpListE)
	{
		if (tmpListE->item == _item)
			break;	
		tmpListE = tmpListE->next;
	}
	
	return tmpListE;
}

void * ListGet(List *_root, uint32_t _number)
{
	uint32_t i = 0;
	
	ListElement *tmpListE = _root->first;
	
	while (tmpListE && i != _number)
	{
		tmpListE = tmpListE->next;
		i++;
	}
	
	return tmpListE->item;
	


}

void DeleteList(List *_root)
{
	ListElement *tmpListE = _root->first;
	ListElement *tmpListEDel = NULL;
	
	while (tmpListE)
	{
		tmpListEDel = tmpListE;
		tmpListE = tmpListE->next;
		free(tmpListEDel);
	}
	
	free(_root);
}

void ListMoveToFirst(List *_root, void *_item)
{
	if (_root->size == 0 || _root->size == 1)
		return;
		
	ListElement *tmpListE = ListFind(_root, _item);
	
	if (tmpListE == NULL)
		return;
	
	if (_root->first == tmpListE)
		return;
		
	if (_root->last  == tmpListE)
	{
		_root->last->prev->next = NULL;
		_root->last  = tmpListE->prev;
	}	
	else
	{
		tmpListE->next->prev = tmpListE->prev;
		tmpListE->prev->next = tmpListE->next;
	}
	
	tmpListE->prev = NULL;
	tmpListE->next = _root->first;
	_root->first = tmpListE;
}

void ListMoveToLast(List *_root, void *_item)
{
	if (_root->size == 0 || _root->size == 1)
		return;
		
	ListElement *tmpListE = ListFind(_root, _item);
	
	if (tmpListE == NULL)
		return;
	
	if (_root->last == tmpListE)
		return;
		
	if (_root->first  == tmpListE)
	{
		_root->first->next->prev = NULL;
		_root->first  = tmpListE->next;
	}	
	else
	{
		tmpListE->next->prev = tmpListE->prev;
		tmpListE->prev->next = tmpListE->next;
	}
	
	tmpListE->next = NULL;
	tmpListE->prev = _root->last;
	_root->last = tmpListE;
}
multi.h

Code: Select all

#ifndef MULTITASKING_H
#define MULTITASKING_H

#include <x32.h>

//PRIORITYS
#define HYPER_PRIORITY		6
#define SUPER_HIGH_PRIORITY	5
#define HIGH_PRIORITY		4
#define NORMAL_PRIORITY		3
#define LOW_PRIORITY		2
#define SUPER_LOW_PRIORITY	1

//TASK STATES
#define TASK_READY			0
#define TASK_RUNNING		1
#define TASK_WAITING		2
#define TASK_SLEEPING		3

#define KERNEL_TASK			0
#define USER_TASK			1

/*-------------TASK STRUCTURE------------*/
typedef struct {
    uint32_t 	ID;
    uint32_t 	Esp;
    uint32_t	Ss;
        
    uint32_t 	Parent;
    uint32_t 	Owner;
    uint32_t 	Group;
    uint32_t 	Timetorun;
    uint8_t 	State;
    uint8_t 	Priority;
    uint8_t		Type;
    uint32_t    box;
   
    uint8_t 	Name[32];
} __attribute__ ((packed)) TASK;



uint32_t	Scheduler		(uint32_t);
uint8_t 	SetupScheduler	();
uint32_t 	TaskAdd			(void *, uint8_t *, uint8_t, uint8_t);
uint8_t 	TaskRemove		(uint32_t);
uint8_t 	TaskSetPriority	(uint32_t, uint8_t);
uint8_t 	TaskSetState	(uint32_t, uint8_t);
TASK * 		TaskGetById		(uint32_t);
TASK * 		TaskGetCurrent	();

uint8_t		SetupKernelTask ();

//IDLE
void		IdleTask 		();

#endif
list.h

Code: Select all

#ifndef LIST_H
#define LIST_H

#include <x32.h>

typedef struct tagListElement {
	
	struct tagListElement * prev;
	struct tagListElement * next;
	
	uint32_t	   * item;

} __attribute__ ((packed)) ListElement;

typedef struct tagList {
	ListElement * first;
	ListElement * last;
	
	uint32_t		 size;

} __attribute__ ((packed)) List;

List *			NewList			();
void			ListAdd			(List *, void *);
void 			ListRemove		(List *, void *);
ListElement * 	ListFind		(List *, void *);
void *			ListGet			(List *, uint32_t);
void			DeleteList		(List *);
void			ListMoveToFirst	(List *, void *);
void			ListMoveToLast	(List *, void *);

#endif
main.c

Code: Select all


#include <system.h>
#include <mm/mm.h>
#include <sys/msg.h>

unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count)
{
    unsigned short *temp = (unsigned short *)dest;
    for( ; count != 0; count--) *temp++ = val;
    return dest;
}
int test(int x, int num);
void task1()
{
     int box = TaskBoxId();
     mail_t *mail;
     int x = -2;
     puts("hello from task1\n");
     while(1)
     {
     x++;
     mail = msgget(box);
     if(mail->cm == MSG_IDLE) puts("msg idle\n");
     if(mail->cm == MSG_READ) puts("msg read\n");
     if(mail->cm == MSG_WRITE) puts("msg write\n");
     if(mail->cm == MSG_IOCTL) puts("msg ioctl\n");
     if(mail->cm == MSG_COPY) puts("msg copy\n");
     if(mail->cm == MSG_DELETE) puts("msg delete\n");
     if(mail->cm == MSG_LIST) puts("msg list\n");
     if(mail->cm == MSG_SEND) puts("msg send\n");
     if(mail->cm == MSG_REQUEST) puts("msg request\n");
     if(mail->cm == MSG_OK) puts("msg ok\n");
     if(mail->cm == MSG_NOK) puts("msg nok\n");
     if(mail->cm == MSG_RESUME) puts("msg resume\n");
     if(mail->cm == MSG_WAIT) puts("msg wait\n");
     msgrefresh(box);
     test(x, box);
     }
     }
     
     

void task2()
{
     while (1);
     }
     int test(int x, int num)
     {
        mail_t *mail = (mail_t*) malloc(sizeof(mail_t));
        mail->cm = x;
        msgsend(num, mail);
        } 

void main()
{
    int i;

    gdt_install();
    SetupPaging();
    init_msg();
    idt_install();
    isrs_install();
    irq_install();
    init_video();
    timer_install();
    keyboard_install();
   
    

    __asm__ __volatile__ ("sti");

    puts("Hello World!\n");
SetupScheduler();
//    i = 10 / 0;
//    putch(i);

    for (;;);
}
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 forgot to push the irq number before the jmp irq_common_stub (_irq0)
"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 ]
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post by urxae »

Here:
gmoney wrote:start.asm

Code: Select all

push esp
call _Scheduler
mov esp, eax

pop gs
you're not cleaning up the stack after calling a C function. Unless you've somehow changed the default calling convention for GCC[1] you need to pop something (or add 4 to esp) after the function returns to remove the argument to _Scheduler() from the stack. As it is now, it looks like gs gets set to an old esp value.

You also don't seem to be updating the esp0 field in your TSS to the new kernel stack.

You didn't post your irq_handler(), but you should make sure it sends an EOI when a timer interrupt fires, or you won't get more than one.


[1]: (I'm assuming you use GCC because your code uses __attribute__)
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

urxae wrote:you're not cleaning up the stack after calling a C function.
Oops, that's my fault for not checking the reference on PUSH. It decrements esp first, then pushes.

Should be:

Code: Select all

mov eax, esp
push eax
call _Scheduler
mov esp, eax
Note that you still don't need to add to esp: it is stored before the push.

Regards,
John.
gmoney
Member
Member
Posts: 106
Joined: Mon Jan 03, 2005 12:00 am
Location: Texas, Usa

Post by gmoney »

thanks i got it to work
Post Reply