2018-12-09 00:40:25 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */
|
|
|
|
/* */
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "gdt.h"
|
|
|
|
|
|
|
|
process *processes;
|
|
|
|
process *current;
|
2018-12-12 15:25:04 +01:00
|
|
|
u32 lastpid;
|
2018-12-09 00:40:25 +01:00
|
|
|
|
2018-12-09 11:11:39 +01:00
|
|
|
|
2018-12-12 15:25:04 +01:00
|
|
|
static u8 elf_errors1[] = "Aucune signature ELF";
|
|
|
|
static u8 elf_errors2[] = "Fichier au format ELF mais non 32 bits";
|
|
|
|
static u8 elf_errors3[] = "ELF non LSB";
|
|
|
|
static u8 elf_errors4[] = "ELF mauvaise version";
|
|
|
|
static u8 elf_errors5[] = "ELF pour OS ne correspondant pas";
|
|
|
|
static u8 elf_errors6[] = "Mauvais type de machine";
|
|
|
|
static u8 *elf_errors[] =
|
|
|
|
{ &elf_errors1, &elf_errors2, &elf_errors3, &elf_errors4,
|
2018-12-12 17:57:23 +01:00
|
|
|
&elf_errors5, &elf_errors6
|
|
|
|
};
|
2018-12-09 00:40:25 +01:00
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Vérifie la signature ELF
|
|
|
|
0 - RAS
|
|
|
|
1 - Pas la signature ELF
|
|
|
|
2 - pas ELF32
|
|
|
|
3 - pas bon organisation LSB/MSB
|
|
|
|
4 - pas bonne version ELF
|
|
|
|
5 - pas bon OS
|
|
|
|
6 - pas bon type machine */
|
2018-12-12 15:25:04 +01:00
|
|
|
|
|
|
|
u32 elf_test(u8 * src)
|
2018-12-09 00:40:25 +01:00
|
|
|
{
|
2018-12-12 15:25:04 +01:00
|
|
|
elf32 *header = (elf32 *) src;
|
|
|
|
if (header->e_ident[EI_MAG0] == ELFMAG0
|
|
|
|
&& header->e_ident[EI_MAG1] == ELFMAG1
|
|
|
|
&& header->e_ident[EI_MAG2] == ELFMAG2
|
|
|
|
&& header->e_ident[EI_MAG3] == ELFMAG3)
|
|
|
|
{
|
|
|
|
if (header->e_ident[EI_CLASS] != ELFCLASS32)
|
|
|
|
return 2;
|
|
|
|
if (header->e_ident[EI_DATA] != ELFDATA2LSB)
|
|
|
|
return 3;
|
|
|
|
if (header->e_ident[EI_VERSION] != EV_CURRENT
|
|
|
|
|| header->e_version != EV_CURRENT)
|
|
|
|
return 4;
|
|
|
|
if (header->e_ident[EI_OSABI] != ELFOSABI_COS2000)
|
|
|
|
return 5;
|
|
|
|
if (header->e_machine != EM_386)
|
|
|
|
return 6;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-09 00:40:25 +01:00
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-12-14 01:06:37 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Met fin à une tâche */
|
|
|
|
/* SYSCALL
|
|
|
|
{
|
|
|
|
"ID":5,
|
|
|
|
"LIBRARY":"libsys",
|
|
|
|
"NAME":"exit",
|
|
|
|
"INTERNALNAME":"exit",
|
|
|
|
"DESCRIPTION":"End a task for user or kernel domain",
|
|
|
|
"ARGS": [
|
|
|
|
{"TYPE":"u32","NAME":"resultcode","DESCRIPTION":"Code result of the execution"}
|
|
|
|
],
|
|
|
|
"RETURN":"void"
|
|
|
|
}
|
|
|
|
END */
|
|
|
|
|
|
|
|
void exit()
|
|
|
|
{
|
|
|
|
task_delete(getcurrentpid());
|
|
|
|
task_switch(0, false);
|
|
|
|
}
|
|
|
|
|
2018-12-09 00:40:25 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Charge le fichier ELF en mémoire et mets à jour les informations sur le processus */
|
|
|
|
|
2018-12-12 15:25:04 +01:00
|
|
|
u32 elf_load(u8 * src, u32 pid)
|
2018-12-09 00:40:25 +01:00
|
|
|
{
|
2018-12-12 15:25:04 +01:00
|
|
|
u8 *ptr;
|
|
|
|
u8 code;
|
|
|
|
u32 v_begin, v_end;
|
|
|
|
elf32 *header;
|
2018-12-09 00:40:25 +01:00
|
|
|
elf32p *program;
|
2018-12-12 15:25:04 +01:00
|
|
|
u32 i;
|
2018-12-09 00:40:25 +01:00
|
|
|
|
|
|
|
header = (elf32 *) src;
|
2018-12-09 11:11:39 +01:00
|
|
|
program = (elf32p *) (src + header->e_phoff);
|
2018-12-12 15:25:04 +01:00
|
|
|
code = elf_test(src);
|
|
|
|
if (code != 0)
|
|
|
|
{
|
|
|
|
printf("Erreur de chargement ELF, %s !\r\n",
|
|
|
|
elf_errors[code - 1]);
|
2018-12-09 00:40:25 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-12-12 15:25:04 +01:00
|
|
|
for (i = 0; i < header->e_phnum; i++, program++)
|
|
|
|
{
|
|
|
|
if (program->p_type == PT_LOAD)
|
|
|
|
{
|
2018-12-09 00:40:25 +01:00
|
|
|
v_begin = program->p_vaddr;
|
|
|
|
v_end = program->p_vaddr + program->p_memsz;
|
2018-12-12 15:25:04 +01:00
|
|
|
if (v_begin < USER_CODE)
|
|
|
|
{
|
|
|
|
printf("Ne peut charger l'executable en desssous de l'adresse %X\r\n", USER_CODE);
|
2018-12-09 00:40:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-12-12 15:25:04 +01:00
|
|
|
if (v_end > USER_STACK)
|
|
|
|
{
|
|
|
|
printf("Ne peut charger l'executable au desssus de l'adresse %X\r\n", USER_STACK);
|
2018-12-09 00:40:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-12-12 15:25:04 +01:00
|
|
|
if (program->p_flags == PF_X + PF_R)
|
|
|
|
{
|
|
|
|
processes[pid].exec_low = (u8 *) v_begin;
|
|
|
|
processes[pid].exec_high = (u8 *) v_end;
|
2018-12-09 00:40:25 +01:00
|
|
|
}
|
2018-12-12 15:25:04 +01:00
|
|
|
if (program->p_flags == PF_W + PF_R)
|
|
|
|
{
|
|
|
|
processes[pid].bss_low = (u8 *) v_begin;
|
|
|
|
processes[pid].bss_high = (u8 *) v_end;
|
2018-12-09 00:40:25 +01:00
|
|
|
}
|
2018-12-12 15:25:04 +01:00
|
|
|
memcpy((u8 *) (src + program->p_offset),
|
|
|
|
(u8 *) v_begin, program->p_filesz, 0);
|
2018-12-09 00:40:25 +01:00
|
|
|
if (program->p_memsz > program->p_filesz)
|
2018-12-12 15:25:04 +01:00
|
|
|
for (i = program->p_filesz, ptr =
|
|
|
|
(u8 *) program->p_vaddr;
|
|
|
|
i < program->p_memsz; i++)
|
2018-12-09 11:11:39 +01:00
|
|
|
ptr[i] = 0;
|
2018-12-09 00:40:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 15:25:04 +01:00
|
|
|
processes[pid].entry = header->e_entry;
|
2018-12-09 00:40:25 +01:00
|
|
|
return header->e_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Initialise la liste des processus */
|
|
|
|
|
2018-12-12 15:25:04 +01:00
|
|
|
void task_init()
|
|
|
|
{
|
2018-12-12 18:49:21 +01:00
|
|
|
u32 i = 1;
|
2018-12-12 15:25:04 +01:00
|
|
|
processes = (process *) vmalloc(sizeof(process) * MAXNUMPROCESS);
|
2018-12-12 18:49:21 +01:00
|
|
|
while (i < MAXNUMPROCESS)
|
2018-12-12 15:25:04 +01:00
|
|
|
{
|
|
|
|
processes[i].pid = NULL;
|
2018-12-12 18:49:21 +01:00
|
|
|
processes[i++].status = STATUS_FREE;
|
2018-12-12 15:25:04 +01:00
|
|
|
}
|
2018-12-12 10:14:31 +01:00
|
|
|
processes[0].dump.ss = SEL_KERNEL_STACK;
|
|
|
|
processes[0].dump.esp = KERNEL_STACK_ADDR;
|
|
|
|
processes[0].dump.eflags = 0x0;
|
|
|
|
processes[0].dump.cs = SEL_KERNEL_CODE;
|
|
|
|
processes[0].dump.eip = getinitretry();
|
|
|
|
processes[0].dump.ds = SEL_KERNEL_DATA;
|
|
|
|
processes[0].dump.es = SEL_KERNEL_DATA;
|
|
|
|
processes[0].dump.fs = SEL_KERNEL_DATA;
|
|
|
|
processes[0].dump.gs = SEL_KERNEL_DATA;
|
|
|
|
processes[0].dump.cr3 = KERNEL_PD_ADDR;
|
|
|
|
processes[0].dump.eax = 0;
|
|
|
|
processes[0].dump.ecx = 0;
|
|
|
|
processes[0].dump.edx = 0;
|
|
|
|
processes[0].dump.ebx = 0;
|
|
|
|
processes[0].dump.ebp = 0;
|
|
|
|
processes[0].dump.esi = 0;
|
|
|
|
processes[0].dump.edi = 0;
|
|
|
|
processes[0].result = 0;
|
|
|
|
processes[0].status = STATUS_READY;
|
|
|
|
processes[0].kernel = true;
|
2018-12-12 15:25:04 +01:00
|
|
|
current = &processes[0];
|
|
|
|
lastpid = NULL;
|
2018-12-09 00:40:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Récupère un emplacement dans la liste des processus */
|
|
|
|
|
2018-12-12 15:25:04 +01:00
|
|
|
u32 task_getfreePID()
|
2018-12-09 00:40:25 +01:00
|
|
|
{
|
2018-12-12 15:25:04 +01:00
|
|
|
u32 i = lastpid;
|
|
|
|
u32 parsed = 0;
|
|
|
|
while (processes[++i].status != STATUS_FREE
|
|
|
|
&& ++parsed < MAXNUMPROCESS)
|
|
|
|
{
|
|
|
|
if (i >= MAXNUMPROCESS)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
if (parsed > MAXNUMPROCESS)
|
|
|
|
{
|
2018-12-09 00:40:25 +01:00
|
|
|
printf("PANIC: plus d'emplacement disponible pour un novueau processus\n");
|
|
|
|
return NULL;
|
2018-12-12 15:25:04 +01:00
|
|
|
}
|
2018-12-09 00:40:25 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:03:52 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Récupère les informations sur le processus courant */
|
2018-12-12 10:14:31 +01:00
|
|
|
|
2018-12-10 17:03:52 +01:00
|
|
|
process *getcurrentprocess()
|
|
|
|
{
|
2018-12-12 15:25:04 +01:00
|
|
|
return current;
|
2018-12-10 17:03:52 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 17:57:23 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Récupère le PID du processus courant */
|
|
|
|
|
|
|
|
u32 getcurrentpid()
|
|
|
|
{
|
|
|
|
return current->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-09 00:40:25 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Determine le dernier PID occupé */
|
|
|
|
|
2018-12-12 15:25:04 +01:00
|
|
|
u32 task_usePID(u32 pid)
|
2018-12-09 00:40:25 +01:00
|
|
|
{
|
2018-12-12 15:25:04 +01:00
|
|
|
lastpid = pid;
|
2018-12-09 00:40:25 +01:00
|
|
|
}
|
|
|
|
|
2018-12-10 19:12:20 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Bascule vers une tâche */
|
|
|
|
|
|
|
|
void task_switch(u32 pid, bool fromkernelmode)
|
|
|
|
{
|
2018-12-12 15:25:04 +01:00
|
|
|
process *previous = current;
|
2018-12-10 19:12:20 +01:00
|
|
|
current = &processes[pid];
|
2018-12-12 10:14:31 +01:00
|
|
|
if (!current->kernel)
|
2018-12-12 15:25:04 +01:00
|
|
|
setTSS(current->kstack.ss0, current->kstack.esp0);
|
2018-12-12 10:14:31 +01:00
|
|
|
else
|
2018-12-12 15:25:04 +01:00
|
|
|
setTSS(0x0, 0x0);
|
2018-12-11 17:00:19 +01:00
|
|
|
current->dump.eflags = (current->dump.eflags | 0x200) & 0xFFFFBFFF;
|
2018-12-12 15:25:04 +01:00
|
|
|
createdump(current->dump);
|
|
|
|
restdebugcpu();
|
|
|
|
iret();
|
2018-12-10 19:12:20 +01:00
|
|
|
}
|
|
|
|
|
2018-12-10 17:03:52 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Execute une tâche */
|
|
|
|
|
|
|
|
void task_run(u32 pid)
|
2018-12-12 15:25:04 +01:00
|
|
|
{
|
2018-12-12 17:57:23 +01:00
|
|
|
if (processes[pid].status == STATUS_READY)
|
|
|
|
{
|
|
|
|
processes[pid].status = STATUS_RUN;
|
|
|
|
task_switch(pid, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Execute une tâche */
|
|
|
|
|
|
|
|
void task_delete(u32 pid)
|
|
|
|
{
|
|
|
|
if (processes[pid].status == STATUS_READY
|
|
|
|
|| processes[pid].status == STATUS_RUN)
|
|
|
|
{
|
|
|
|
processes[pid].status = STATUS_FREE;
|
|
|
|
}
|
2018-12-10 17:03:52 +01:00
|
|
|
}
|
|
|
|
|
2018-12-09 00:40:25 +01:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Initialise une tâche */
|
|
|
|
|
2018-12-12 15:25:04 +01:00
|
|
|
u32 task_create(u8 * code, bool kerneltask)
|
|
|
|
{
|
|
|
|
process *previous = current;
|
|
|
|
u32 pid = task_getfreePID();
|
|
|
|
task_usePID(pid);
|
|
|
|
page *kstack;
|
2018-12-09 00:40:25 +01:00
|
|
|
processes[pid].pid = pid;
|
|
|
|
processes[pid].pdd = virtual_pd_create();
|
|
|
|
TAILQ_INIT(&processes[pid].page_head);
|
2018-12-12 10:14:31 +01:00
|
|
|
if (kerneltask)
|
|
|
|
{
|
|
|
|
processes[pid].dump.ss = SEL_KERNEL_STACK;
|
2018-12-12 15:25:04 +01:00
|
|
|
processes[pid].dump.esp =
|
|
|
|
(u32) kstack->vaddr + PAGESIZE - 16;
|
2018-12-12 10:14:31 +01:00
|
|
|
processes[pid].dump.eflags = 0x0;
|
|
|
|
processes[pid].dump.cs = SEL_KERNEL_CODE;
|
2018-12-12 15:25:04 +01:00
|
|
|
processes[pid].dump.eip = elf_load(code, pid);
|
|
|
|
if (processes[pid].dump.eip == NULL)
|
2018-12-12 10:14:31 +01:00
|
|
|
return NULL;
|
|
|
|
processes[pid].dump.ds = SEL_KERNEL_DATA;
|
|
|
|
processes[pid].dump.es = SEL_KERNEL_DATA;
|
|
|
|
processes[pid].dump.fs = SEL_KERNEL_DATA;
|
|
|
|
processes[pid].dump.gs = SEL_KERNEL_DATA;
|
|
|
|
processes[pid].dump.cr3 = KERNEL_PD_ADDR;
|
|
|
|
processes[pid].dump.eax = 0;
|
|
|
|
processes[pid].dump.ecx = 0;
|
|
|
|
processes[pid].dump.edx = 0;
|
|
|
|
processes[pid].dump.ebx = 0;
|
|
|
|
processes[pid].dump.ebp = 0;
|
|
|
|
processes[pid].dump.esi = 0;
|
|
|
|
processes[pid].dump.edi = 0;
|
|
|
|
processes[pid].result = 0;
|
|
|
|
processes[pid].status = STATUS_READY;
|
|
|
|
processes[pid].kernel = true;
|
|
|
|
current = previous;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current = &processes[pid];
|
|
|
|
setcr3(processes[pid].pdd->addr->paddr);
|
|
|
|
kstack = virtual_page_getfree();
|
|
|
|
processes[pid].dump.ss = SEL_USER_STACK | RPL_RING3;
|
2018-12-12 15:25:04 +01:00
|
|
|
processes[pid].dump.esp = USER_STACK - 16;
|
2018-12-12 10:14:31 +01:00
|
|
|
processes[pid].dump.eflags = 0x0;
|
2018-12-12 15:25:04 +01:00
|
|
|
processes[pid].dump.cs = SEL_USER_CODE | RPL_RING3;
|
|
|
|
processes[pid].dump.eip = elf_load(code, pid);
|
|
|
|
if (processes[pid].dump.eip == NULL)
|
2018-12-12 10:14:31 +01:00
|
|
|
return NULL;
|
|
|
|
processes[pid].dump.ds = SEL_USER_DATA | RPL_RING3;
|
|
|
|
processes[pid].dump.es = SEL_USER_DATA | RPL_RING3;
|
|
|
|
processes[pid].dump.fs = SEL_USER_DATA | RPL_RING3;
|
|
|
|
processes[pid].dump.gs = SEL_USER_DATA | RPL_RING3;
|
2018-12-12 15:25:04 +01:00
|
|
|
processes[pid].dump.cr3 =
|
|
|
|
(u32) processes[pid].pdd->addr->paddr;
|
2018-12-12 10:14:31 +01:00
|
|
|
processes[pid].kstack.ss0 = SEL_KERNEL_STACK;
|
2018-12-12 15:25:04 +01:00
|
|
|
processes[pid].kstack.esp0 =
|
|
|
|
(u32) kstack->vaddr + PAGESIZE - 16;
|
2018-12-12 10:14:31 +01:00
|
|
|
processes[pid].dump.eax = 0;
|
|
|
|
processes[pid].dump.ecx = 0;
|
|
|
|
processes[pid].dump.edx = 0;
|
|
|
|
processes[pid].dump.ebx = 0;
|
|
|
|
processes[pid].dump.ebp = 0;
|
|
|
|
processes[pid].dump.esi = 0;
|
|
|
|
processes[pid].dump.edi = 0;
|
|
|
|
processes[pid].result = 0;
|
|
|
|
processes[pid].status = STATUS_READY;
|
|
|
|
processes[pid].kernel = false;
|
|
|
|
current = previous;
|
|
|
|
setcr3(current->dump.cr3);
|
|
|
|
}
|
2018-12-12 15:25:04 +01:00
|
|
|
return pid;
|
2018-12-09 00:40:25 +01:00
|
|
|
}
|