Skip to content

Commit

Permalink
idle: create idle task when there is nothing to schedule
Browse files Browse the repository at this point in the history
* Serves as place holder when all tasks are blocked/suspended
* Acts as place holder for freeing up kernel resources when task exits
* Power management driver

Signed-off-by: Mahavir Jain <[email protected]>
  • Loading branch information
mahavirj committed Mar 13, 2017
1 parent c2a630e commit 4ab9394
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,33 @@ int create_init_task()
return 0;
}

void idle_loop()
{
while (1) {
/* Allowed following instruction in previlege mode only */
asm volatile("hlt");
}
}

int create_idle_task()
{
struct task *idle = alloctask();
if (!idle) {
printk("failed to create idle task\n");
return -1;
}

/* Task will start in CPL = 0, i.e. kernel mode */
idle->irqf->cs = (SEG_KCODE << 3);
idle->irqf->ds = (SEG_KDATA << 3);
idle->irqf->eflags = 0x200;
idle->irqf->eip = (uint32_t) idle_loop;

idle->state = TASK_READY;

return 0;
}

int sys_fork()
{
int ret;
Expand Down Expand Up @@ -209,7 +236,7 @@ int sys_exec()
void *fstart = cpio_get_file(_binary_ramfs_cpio_start,
fname, &size);
if (!fstart) {
printk("no %s in initramfs\n", fname);
printk("no `%s` in ramfs\n", fname);
return -1;
}

Expand Down Expand Up @@ -286,6 +313,7 @@ void init_scheduler()
printk("Scheduler invoked before creating task\n");
return;
}
create_idle_task();
current_task = init_task;
set_kernel_stack((uint32_t) current_task->kstack);
switch_pgdir(V2P(current_task->pd));
Expand Down

0 comments on commit 4ab9394

Please sign in to comment.